blob: 39eaf36c9574c08a57a406edcea12b637e35d0c6 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner545f39e2009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattnera7549902007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerdaa5c002008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Chris Lattner4b009652007-07-25 00:24:17 +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 Redl19fec9d2008-11-21 19:14:01 +000028///
29/// Called type-id in C++.
Sebastian Redlaaacda92009-05-29 18:02:33 +000030Action::TypeResult Parser::ParseTypeName(SourceRange *Range) {
Chris Lattner4b009652007-07-25 00:24:17 +000031 // Parse the common declaration-specifiers piece.
32 DeclSpec DS;
33 ParseSpecifierQualifierList(DS);
Sebastian Redlaaacda92009-05-29 18:02:33 +000034
Chris Lattner4b009652007-07-25 00:24:17 +000035 // Parse the abstract-declarator, if present.
36 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
37 ParseDeclarator(DeclaratorInfo);
Sebastian Redlaaacda92009-05-29 18:02:33 +000038 if (Range)
39 *Range = DeclaratorInfo.getSourceRange();
40
Chris Lattner34c61332009-04-25 08:06:05 +000041 if (DeclaratorInfo.isInvalidType())
Douglas Gregor6c0f4062009-02-18 17:45:20 +000042 return true;
43
44 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Chris Lattner4b009652007-07-25 00:24:17 +000045}
46
47/// ParseAttributes - Parse a non-empty attributes list.
48///
49/// [GNU] attributes:
50/// attribute
51/// attributes attribute
52///
53/// [GNU] attribute:
54/// '__attribute__' '(' '(' attribute-list ')' ')'
55///
56/// [GNU] attribute-list:
57/// attrib
58/// attribute_list ',' attrib
59///
60/// [GNU] attrib:
61/// empty
62/// attrib-name
63/// attrib-name '(' identifier ')'
64/// attrib-name '(' identifier ',' nonempty-expr-list ')'
65/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
66///
67/// [GNU] attrib-name:
68/// identifier
69/// typespec
70/// typequal
71/// storageclass
72///
73/// FIXME: The GCC grammar/code for this construct implies we need two
74/// token lookahead. Comment from gcc: "If they start with an identifier
75/// which is followed by a comma or close parenthesis, then the arguments
76/// start with that identifier; otherwise they are an expression list."
77///
78/// At the moment, I am not doing 2 token lookahead. I am also unaware of
79/// any attributes that don't work (based on my limited testing). Most
80/// attributes are very simple in practice. Until we find a bug, I don't see
81/// a pressing need to implement the 2 token lookahead.
82
Sebastian Redl0c986032009-02-09 18:23:29 +000083AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner34a01ad2007-10-09 17:33:22 +000084 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Chris Lattner4b009652007-07-25 00:24:17 +000085
86 AttributeList *CurrAttr = 0;
87
Chris Lattner34a01ad2007-10-09 17:33:22 +000088 while (Tok.is(tok::kw___attribute)) {
Chris Lattner4b009652007-07-25 00:24:17 +000089 ConsumeToken();
90 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
91 "attribute")) {
92 SkipUntil(tok::r_paren, true); // skip until ) or ;
93 return CurrAttr;
94 }
95 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
96 SkipUntil(tok::r_paren, true); // skip until ) or ;
97 return CurrAttr;
98 }
99 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner34a01ad2007-10-09 17:33:22 +0000100 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
101 Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000102
Chris Lattner34a01ad2007-10-09 17:33:22 +0000103 if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000104 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
105 ConsumeToken();
106 continue;
107 }
108 // we have an identifier or declaration specifier (const, int, etc.)
109 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
110 SourceLocation AttrNameLoc = ConsumeToken();
111
112 // check if we have a "paramterized" attribute
Chris Lattner34a01ad2007-10-09 17:33:22 +0000113 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000114 ConsumeParen(); // ignore the left paren loc for now
115
Chris Lattner34a01ad2007-10-09 17:33:22 +0000116 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000117 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
118 SourceLocation ParmLoc = ConsumeToken();
119
Chris Lattner34a01ad2007-10-09 17:33:22 +0000120 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000121 // __attribute__(( mode(byte) ))
122 ConsumeParen(); // ignore the right paren loc for now
123 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
124 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000125 } else if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000126 ConsumeToken();
127 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000128 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000129 bool ArgExprsOk = true;
130
131 // now parse the non-empty comma separated list of expressions
132 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000133 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000134 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000135 ArgExprsOk = false;
136 SkipUntil(tok::r_paren);
137 break;
138 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000139 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000140 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000141 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000142 break;
143 ConsumeToken(); // Eat the comma, move to the next argument
144 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000145 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000146 ConsumeParen(); // ignore the right paren loc for now
147 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redl6008ac32008-11-25 22:21:31 +0000148 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Chris Lattner4b009652007-07-25 00:24:17 +0000149 }
150 }
151 } else { // not an identifier
152 // parse a possibly empty comma separated list of expressions
Chris Lattner34a01ad2007-10-09 17:33:22 +0000153 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000154 // __attribute__(( nonnull() ))
155 ConsumeParen(); // ignore the right paren loc for now
156 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
157 0, SourceLocation(), 0, 0, CurrAttr);
158 } else {
159 // __attribute__(( aligned(16) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000160 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000161 bool ArgExprsOk = true;
162
163 // now parse the list of expressions
164 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000165 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000166 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000167 ArgExprsOk = false;
168 SkipUntil(tok::r_paren);
169 break;
170 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000171 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000172 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000173 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000174 break;
175 ConsumeToken(); // Eat the comma, move to the next argument
176 }
177 // Match the ')'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000178 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000179 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl6008ac32008-11-25 22:21:31 +0000180 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
181 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Chris Lattner4b009652007-07-25 00:24:17 +0000182 CurrAttr);
183 }
184 }
185 }
186 } else {
187 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
188 0, SourceLocation(), 0, 0, CurrAttr);
189 }
190 }
191 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Chris Lattner4b009652007-07-25 00:24:17 +0000192 SkipUntil(tok::r_paren, false);
Sebastian Redl0c986032009-02-09 18:23:29 +0000193 SourceLocation Loc = Tok.getLocation();;
194 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
195 SkipUntil(tok::r_paren, false);
196 }
197 if (EndLoc)
198 *EndLoc = Loc;
Chris Lattner4b009652007-07-25 00:24:17 +0000199 }
200 return CurrAttr;
201}
202
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000203/// FuzzyParseMicrosoftDeclSpec. When -fms-extensions is enabled, this
204/// routine is called to skip/ignore tokens that comprise the MS declspec.
205void Parser::FuzzyParseMicrosoftDeclSpec() {
206 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
207 ConsumeToken();
208 if (Tok.is(tok::l_paren)) {
209 unsigned short savedParenCount = ParenCount;
210 do {
211 ConsumeAnyToken();
212 } while (ParenCount > savedParenCount && Tok.isNot(tok::eof));
213 }
214 return;
215}
216
Chris Lattner4b009652007-07-25 00:24:17 +0000217/// ParseDeclaration - Parse a full 'declaration', which consists of
218/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000219/// 'Context' should be a Declarator::TheContext value. This returns the
220/// location of the semicolon in DeclEnd.
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000221///
222/// declaration: [C99 6.7]
223/// block-declaration ->
224/// simple-declaration
225/// others [FIXME]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000226/// [C++] template-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000227/// [C++] namespace-definition
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000228/// [C++] using-directive
229/// [C++] using-declaration [TODO]
Sebastian Redla8cecf62009-03-24 22:27:57 +0000230/// [C++0x] static_assert-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000231/// others... [FIXME]
232///
Chris Lattner9802a0a2009-04-02 04:16:50 +0000233Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
234 SourceLocation &DeclEnd) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000235 DeclPtrTy SingleDecl;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000236 switch (Tok.getKind()) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000237 case tok::kw_template:
Douglas Gregore3298aa2009-05-12 21:31:51 +0000238 case tok::kw_export:
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000239 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000240 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000241 case tok::kw_namespace:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000242 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000243 break;
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000244 case tok::kw_using:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000245 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000246 break;
Anders Carlssonab041982009-03-11 16:27:10 +0000247 case tok::kw_static_assert:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000248 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000249 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000250 default:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000251 return ParseSimpleDeclaration(Context, DeclEnd);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000252 }
Chris Lattnera17991f2009-03-29 16:50:03 +0000253
254 // This routine returns a DeclGroup, if the thing we parsed only contains a
255 // single decl, convert it now.
256 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000257}
258
259/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
260/// declaration-specifiers init-declarator-list[opt] ';'
261///[C90/C++]init-declarator-list ';' [TODO]
262/// [OMP] threadprivate-directive [TODO]
Chris Lattnerf8016042009-03-29 17:27:48 +0000263///
264/// If RequireSemi is false, this does not check for a ';' at the end of the
265/// declaration.
266Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Chris Lattner9802a0a2009-04-02 04:16:50 +0000267 SourceLocation &DeclEnd,
Chris Lattnerf8016042009-03-29 17:27:48 +0000268 bool RequireSemi) {
Chris Lattner4b009652007-07-25 00:24:17 +0000269 // Parse the common declaration-specifiers piece.
270 DeclSpec DS;
271 ParseDeclarationSpecifiers(DS);
272
273 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
274 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000275 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000276 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000277 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
278 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000279 }
280
281 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
282 ParseDeclarator(DeclaratorInfo);
283
Chris Lattner2c41d482009-03-29 17:18:04 +0000284 DeclGroupPtrTy DG =
285 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattnerf8016042009-03-29 17:27:48 +0000286
Chris Lattner9802a0a2009-04-02 04:16:50 +0000287 DeclEnd = Tok.getLocation();
288
Chris Lattnerf8016042009-03-29 17:27:48 +0000289 // If the client wants to check what comes after the declaration, just return
290 // immediately without checking anything!
291 if (!RequireSemi) return DG;
Chris Lattner2c41d482009-03-29 17:18:04 +0000292
293 if (Tok.is(tok::semi)) {
294 ConsumeToken();
Chris Lattner2c41d482009-03-29 17:18:04 +0000295 return DG;
296 }
297
Chris Lattner2c41d482009-03-29 17:18:04 +0000298 Diag(Tok, diag::err_expected_semi_declation);
299 // Skip to end of block or statement
300 SkipUntil(tok::r_brace, true, true);
301 if (Tok.is(tok::semi))
302 ConsumeToken();
303 return DG;
Chris Lattner4b009652007-07-25 00:24:17 +0000304}
305
Douglas Gregore3298aa2009-05-12 21:31:51 +0000306/// \brief Parse 'declaration' after parsing 'declaration-specifiers
307/// declarator'. This method parses the remainder of the declaration
308/// (including any attributes or initializer, among other things) and
309/// finalizes the declaration.
Chris Lattner4b009652007-07-25 00:24:17 +0000310///
Chris Lattner4b009652007-07-25 00:24:17 +0000311/// init-declarator: [C99 6.7]
312/// declarator
313/// declarator '=' initializer
314/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
315/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000316/// [C++] declarator initializer[opt]
317///
318/// [C++] initializer:
319/// [C++] '=' initializer-clause
320/// [C++] '(' expression-list ')'
Sebastian Redla8cecf62009-03-24 22:27:57 +0000321/// [C++0x] '=' 'default' [TODO]
322/// [C++0x] '=' 'delete'
323///
324/// According to the standard grammar, =default and =delete are function
325/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattner4b009652007-07-25 00:24:17 +0000326///
Douglas Gregore3298aa2009-05-12 21:31:51 +0000327Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D) {
328 // If a simple-asm-expr is present, parse it.
329 if (Tok.is(tok::kw_asm)) {
330 SourceLocation Loc;
331 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
332 if (AsmLabel.isInvalid()) {
333 SkipUntil(tok::semi, true, true);
334 return DeclPtrTy();
335 }
336
337 D.setAsmLabel(AsmLabel.release());
338 D.SetRangeEnd(Loc);
339 }
340
341 // If attributes are present, parse them.
342 if (Tok.is(tok::kw___attribute)) {
343 SourceLocation Loc;
344 AttributeList *AttrList = ParseAttributes(&Loc);
345 D.AddAttributes(AttrList, Loc);
346 }
347
348 // Inform the current actions module that we just parsed this declarator.
349 DeclPtrTy ThisDecl = Actions.ActOnDeclarator(CurScope, D);
350
351 // Parse declarator '=' initializer.
352 if (Tok.is(tok::equal)) {
353 ConsumeToken();
354 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
355 SourceLocation DelLoc = ConsumeToken();
356 Actions.SetDeclDeleted(ThisDecl, DelLoc);
357 } else {
358 OwningExprResult Init(ParseInitializer());
359 if (Init.isInvalid()) {
360 SkipUntil(tok::semi, true, true);
361 return DeclPtrTy();
362 }
Anders Carlssonf9f05b82009-05-30 21:37:25 +0000363 Actions.AddInitializerToDecl(ThisDecl, Actions.FullExpr(Init));
Douglas Gregore3298aa2009-05-12 21:31:51 +0000364 }
365 } else if (Tok.is(tok::l_paren)) {
366 // Parse C++ direct initializer: '(' expression-list ')'
367 SourceLocation LParenLoc = ConsumeParen();
368 ExprVector Exprs(Actions);
369 CommaLocsTy CommaLocs;
370
371 if (ParseExpressionList(Exprs, CommaLocs)) {
372 SkipUntil(tok::r_paren);
373 } else {
374 // Match the ')'.
375 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
376
377 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
378 "Unexpected number of commas!");
379 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
380 move_arg(Exprs),
Jay Foad9e6bef42009-05-21 09:52:38 +0000381 CommaLocs.data(), RParenLoc);
Douglas Gregore3298aa2009-05-12 21:31:51 +0000382 }
383 } else {
384 Actions.ActOnUninitializedDecl(ThisDecl);
385 }
386
387 return ThisDecl;
388}
389
390/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
391/// parsing 'declaration-specifiers declarator'. This method is split out this
392/// way to handle the ambiguity between top-level function-definitions and
393/// declarations.
394///
395/// init-declarator-list: [C99 6.7]
396/// init-declarator
397/// init-declarator-list ',' init-declarator
398///
399/// According to the standard grammar, =default and =delete are function
400/// definitions, but that definitely doesn't fit with the parser here.
401///
Chris Lattnera17991f2009-03-29 16:50:03 +0000402Parser::DeclGroupPtrTy Parser::
Chris Lattner4b009652007-07-25 00:24:17 +0000403ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000404 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
405 // that we parse together here.
406 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Chris Lattner4b009652007-07-25 00:24:17 +0000407
408 // At this point, we know that it is not a function definition. Parse the
409 // rest of the init-declarator-list.
410 while (1) {
Douglas Gregore3298aa2009-05-12 21:31:51 +0000411 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
412 if (ThisDecl.get())
413 DeclsInGroup.push_back(ThisDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000414
Chris Lattner4b009652007-07-25 00:24:17 +0000415 // If we don't have a comma, it is either the end of the list (a ';') or an
416 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000417 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000418 break;
419
420 // Consume the comma.
421 ConsumeToken();
422
423 // Parse the next declarator.
424 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000425
426 // Accept attributes in an init-declarator. In the first declarator in a
427 // declaration, these would be part of the declspec. In subsequent
428 // declarators, they become part of the declarator itself, so that they
429 // don't apply to declarators after *this* one. Examples:
430 // short __attribute__((common)) var; -> declspec
431 // short var __attribute__((common)); -> declarator
432 // short x, __attribute__((common)) var; -> declarator
Sebastian Redl0c986032009-02-09 18:23:29 +0000433 if (Tok.is(tok::kw___attribute)) {
434 SourceLocation Loc;
435 AttributeList *AttrList = ParseAttributes(&Loc);
436 D.AddAttributes(AttrList, Loc);
437 }
Chris Lattner926cf542008-10-20 04:57:38 +0000438
Chris Lattner4b009652007-07-25 00:24:17 +0000439 ParseDeclarator(D);
440 }
441
Eli Friedman4d57af22009-05-29 01:49:24 +0000442 return Actions.FinalizeDeclaratorGroup(CurScope, D.getDeclSpec(),
443 DeclsInGroup.data(),
Chris Lattner2c41d482009-03-29 17:18:04 +0000444 DeclsInGroup.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000445}
446
447/// ParseSpecifierQualifierList
448/// specifier-qualifier-list:
449/// type-specifier specifier-qualifier-list[opt]
450/// type-qualifier specifier-qualifier-list[opt]
451/// [GNU] attributes specifier-qualifier-list[opt]
452///
453void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
454 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
455 /// parse declaration-specifiers and complain about extra stuff.
456 ParseDeclarationSpecifiers(DS);
457
458 // Validate declspec for type-name.
459 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera52aec42009-04-14 21:16:09 +0000460 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
461 !DS.getAttributes())
Chris Lattner4b009652007-07-25 00:24:17 +0000462 Diag(Tok, diag::err_typename_requires_specqual);
463
464 // Issue diagnostic and remove storage class if present.
465 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
466 if (DS.getStorageClassSpecLoc().isValid())
467 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
468 else
469 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
470 DS.ClearStorageClassSpecs();
471 }
472
473 // Issue diagnostic and remove function specfier if present.
474 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000475 if (DS.isInlineSpecified())
476 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
477 if (DS.isVirtualSpecified())
478 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
479 if (DS.isExplicitSpecified())
480 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000481 DS.ClearFunctionSpecs();
482 }
483}
484
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000485/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
486/// specified token is valid after the identifier in a declarator which
487/// immediately follows the declspec. For example, these things are valid:
488///
489/// int x [ 4]; // direct-declarator
490/// int x ( int y); // direct-declarator
491/// int(int x ) // direct-declarator
492/// int x ; // simple-declaration
493/// int x = 17; // init-declarator-list
494/// int x , y; // init-declarator-list
495/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera52aec42009-04-14 21:16:09 +0000496/// int x : 4; // struct-declarator
Chris Lattnerca6cc362009-04-12 22:29:43 +0000497/// int x { 5}; // C++'0x unified initializers
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000498///
499/// This is not, because 'x' does not immediately follow the declspec (though
500/// ')' happens to be valid anyway).
501/// int (x)
502///
503static bool isValidAfterIdentifierInDeclarator(const Token &T) {
504 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
505 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera52aec42009-04-14 21:16:09 +0000506 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000507}
508
Chris Lattner82353c62009-04-14 21:34:55 +0000509
510/// ParseImplicitInt - This method is called when we have an non-typename
511/// identifier in a declspec (which normally terminates the decl spec) when
512/// the declspec has no type specifier. In this case, the declspec is either
513/// malformed or is "implicit int" (in K&R and C89).
514///
515/// This method handles diagnosing this prettily and returns false if the
516/// declspec is done being processed. If it recovers and thinks there may be
517/// other pieces of declspec after it, it returns true.
518///
Chris Lattner52cd7622009-04-14 22:17:06 +0000519bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000520 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner82353c62009-04-14 21:34:55 +0000521 AccessSpecifier AS) {
Chris Lattner52cd7622009-04-14 22:17:06 +0000522 assert(Tok.is(tok::identifier) && "should have identifier");
523
Chris Lattner82353c62009-04-14 21:34:55 +0000524 SourceLocation Loc = Tok.getLocation();
525 // If we see an identifier that is not a type name, we normally would
526 // parse it as the identifer being declared. However, when a typename
527 // is typo'd or the definition is not included, this will incorrectly
528 // parse the typename as the identifier name and fall over misparsing
529 // later parts of the diagnostic.
530 //
531 // As such, we try to do some look-ahead in cases where this would
532 // otherwise be an "implicit-int" case to see if this is invalid. For
533 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
534 // an identifier with implicit int, we'd get a parse error because the
535 // next token is obviously invalid for a type. Parse these as a case
536 // with an invalid type specifier.
537 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
538
539 // Since we know that this either implicit int (which is rare) or an
540 // error, we'd do lookahead to try to do better recovery.
541 if (isValidAfterIdentifierInDeclarator(NextToken())) {
542 // If this token is valid for implicit int, e.g. "static x = 4", then
543 // we just avoid eating the identifier, so it will be parsed as the
544 // identifier in the declarator.
545 return false;
546 }
547
548 // Otherwise, if we don't consume this token, we are going to emit an
549 // error anyway. Try to recover from various common problems. Check
550 // to see if this was a reference to a tag name without a tag specified.
551 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattner52cd7622009-04-14 22:17:06 +0000552 //
553 // C++ doesn't need this, and isTagName doesn't take SS.
554 if (SS == 0) {
555 const char *TagName = 0;
556 tok::TokenKind TagKind = tok::unknown;
Chris Lattner82353c62009-04-14 21:34:55 +0000557
Chris Lattner82353c62009-04-14 21:34:55 +0000558 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
559 default: break;
560 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
561 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
562 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
563 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
564 }
Chris Lattner82353c62009-04-14 21:34:55 +0000565
Chris Lattner52cd7622009-04-14 22:17:06 +0000566 if (TagName) {
567 Diag(Loc, diag::err_use_of_tag_name_without_tag)
568 << Tok.getIdentifierInfo() << TagName
569 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
570
571 // Parse this as a tag as if the missing tag were present.
572 if (TagKind == tok::kw_enum)
573 ParseEnumSpecifier(Loc, DS, AS);
574 else
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000575 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattner52cd7622009-04-14 22:17:06 +0000576 return true;
577 }
Chris Lattner82353c62009-04-14 21:34:55 +0000578 }
579
580 // Since this is almost certainly an invalid type name, emit a
581 // diagnostic that says it, eat the token, and mark the declspec as
582 // invalid.
Chris Lattner52cd7622009-04-14 22:17:06 +0000583 SourceRange R;
584 if (SS) R = SS->getRange();
585
586 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
Chris Lattner82353c62009-04-14 21:34:55 +0000587 const char *PrevSpec;
588 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec);
589 DS.SetRangeEnd(Tok.getLocation());
590 ConsumeToken();
591
592 // TODO: Could inject an invalid typedef decl in an enclosing scope to
593 // avoid rippling error messages on subsequent uses of the same type,
594 // could be useful if #include was forgotten.
595 return false;
596}
597
Chris Lattner4b009652007-07-25 00:24:17 +0000598/// ParseDeclarationSpecifiers
599/// declaration-specifiers: [C99 6.7]
600/// storage-class-specifier declaration-specifiers[opt]
601/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000602/// [C99] function-specifier declaration-specifiers[opt]
603/// [GNU] attributes declaration-specifiers[opt]
604///
605/// storage-class-specifier: [C99 6.7.1]
606/// 'typedef'
607/// 'extern'
608/// 'static'
609/// 'auto'
610/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000611/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000612/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000613/// function-specifier: [C99 6.7.4]
614/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000615/// [C++] 'virtual'
616/// [C++] 'explicit'
Anders Carlsson6c2ad5a2009-05-06 04:46:28 +0000617/// 'friend': [C++ dcl.friend]
618
Chris Lattner4b009652007-07-25 00:24:17 +0000619///
Douglas Gregor52473432008-12-24 02:52:09 +0000620void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000621 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000622 AccessSpecifier AS) {
Chris Lattnera4ff4272008-03-13 06:29:04 +0000623 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000624 while (1) {
625 int isInvalid = false;
626 const char *PrevSpec = 0;
627 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000628
Chris Lattner4b009652007-07-25 00:24:17 +0000629 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000630 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000631 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000632 // If this is not a declaration specifier token, we're done reading decl
633 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000634 DS.Finish(Diags, PP);
Chris Lattner4b009652007-07-25 00:24:17 +0000635 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000636
637 case tok::coloncolon: // ::foo::bar
638 // Annotate C++ scope specifiers. If we get one, loop.
639 if (TryAnnotateCXXScopeToken())
640 continue;
641 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000642
643 case tok::annot_cxxscope: {
644 if (DS.hasTypeSpecifier())
645 goto DoneWithDeclSpec;
646
647 // We are looking for a qualified typename.
Douglas Gregor80b95c52009-03-25 15:40:00 +0000648 Token Next = NextToken();
649 if (Next.is(tok::annot_template_id) &&
650 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregoraabb8502009-03-31 00:43:58 +0000651 ->Kind == TNK_Type_template) {
Douglas Gregor80b95c52009-03-25 15:40:00 +0000652 // We have a qualified template-id, e.g., N::A<int>
653 CXXScopeSpec SS;
654 ParseOptionalCXXScopeSpecifier(SS);
655 assert(Tok.is(tok::annot_template_id) &&
656 "ParseOptionalCXXScopeSpecifier not working");
657 AnnotateTemplateIdTokenAsType(&SS);
658 continue;
659 }
660
661 if (Next.isNot(tok::identifier))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000662 goto DoneWithDeclSpec;
663
664 CXXScopeSpec SS;
Douglas Gregor041e9292009-03-26 23:56:24 +0000665 SS.setScopeRep(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000666 SS.setRange(Tok.getAnnotationRange());
667
668 // If the next token is the name of the class type that the C++ scope
669 // denotes, followed by a '(', then this is a constructor declaration.
670 // We're done with the decl-specifiers.
Chris Lattner52cd7622009-04-14 22:17:06 +0000671 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000672 CurScope, &SS) &&
673 GetLookAheadToken(2).is(tok::l_paren))
674 goto DoneWithDeclSpec;
675
Douglas Gregor1075a162009-02-04 17:00:24 +0000676 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
677 Next.getLocation(), CurScope, &SS);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000678
Chris Lattner52cd7622009-04-14 22:17:06 +0000679 // If the referenced identifier is not a type, then this declspec is
680 // erroneous: We already checked about that it has no type specifier, and
681 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
682 // typename.
683 if (TypeRep == 0) {
684 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000685 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000686 goto DoneWithDeclSpec;
Chris Lattner52cd7622009-04-14 22:17:06 +0000687 }
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000688
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000689 ConsumeToken(); // The C++ scope.
690
Douglas Gregora60c62e2009-02-09 15:09:02 +0000691 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000692 TypeRep);
693 if (isInvalid)
694 break;
695
696 DS.SetRangeEnd(Tok.getLocation());
697 ConsumeToken(); // The typename.
698
699 continue;
700 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000701
702 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +0000703 if (Tok.getAnnotationValue())
704 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
705 Tok.getAnnotationValue());
706 else
707 DS.SetTypeSpecError();
Chris Lattnerc297b722009-01-21 19:48:37 +0000708 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
709 ConsumeToken(); // The typename
710
711 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
712 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
713 // Objective-C interface. If we don't have Objective-C or a '<', this is
714 // just a normal reference to a typedef name.
715 if (!Tok.is(tok::less) || !getLang().ObjC1)
716 continue;
717
718 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000719 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnerc297b722009-01-21 19:48:37 +0000720 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
721 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
722
723 DS.SetRangeEnd(EndProtoLoc);
724 continue;
725 }
726
Chris Lattnerfda18db2008-07-26 01:18:38 +0000727 // typedef-name
728 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000729 // In C++, check to see if this is a scope specifier like foo::bar::, if
730 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000731 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
732 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000733
Chris Lattnerfda18db2008-07-26 01:18:38 +0000734 // This identifier can only be a typedef name if we haven't already seen
735 // a type-specifier. Without this check we misparse:
736 // typedef int X; struct Y { short X; }; as 'short int'.
737 if (DS.hasTypeSpecifier())
738 goto DoneWithDeclSpec;
739
740 // It has to be available as a typedef too!
Douglas Gregor1075a162009-02-04 17:00:24 +0000741 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
742 Tok.getLocation(), CurScope);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000743
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000744 // If this is not a typedef name, don't parse it as part of the declspec,
745 // it must be an implicit int or an error.
746 if (TypeRep == 0) {
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000747 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000748 goto DoneWithDeclSpec;
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000749 }
Douglas Gregor8e458f42009-02-09 18:46:07 +0000750
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000751 // C++: If the identifier is actually the name of the class type
752 // being defined and the next token is a '(', then this is a
753 // constructor declaration. We're done with the decl-specifiers
754 // and will treat this token as an identifier.
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000755 if (getLang().CPlusPlus && CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000756 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
757 NextToken().getKind() == tok::l_paren)
758 goto DoneWithDeclSpec;
759
Douglas Gregora60c62e2009-02-09 15:09:02 +0000760 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerfda18db2008-07-26 01:18:38 +0000761 TypeRep);
762 if (isInvalid)
763 break;
764
765 DS.SetRangeEnd(Tok.getLocation());
766 ConsumeToken(); // The identifier
767
768 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
769 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
770 // Objective-C interface. If we don't have Objective-C or a '<', this is
771 // just a normal reference to a typedef name.
772 if (!Tok.is(tok::less) || !getLang().ObjC1)
773 continue;
774
775 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000776 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000777 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000778 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000779
780 DS.SetRangeEnd(EndProtoLoc);
781
Steve Narofff7683302008-09-22 10:28:57 +0000782 // Need to support trailing type qualifiers (e.g. "id<p> const").
783 // If a type specifier follows, it will be diagnosed elsewhere.
784 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000785 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000786
787 // type-name
788 case tok::annot_template_id: {
789 TemplateIdAnnotation *TemplateId
790 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregoraabb8502009-03-31 00:43:58 +0000791 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor0c281a82009-02-25 19:37:18 +0000792 // This template-id does not refer to a type name, so we're
793 // done with the type-specifiers.
794 goto DoneWithDeclSpec;
795 }
796
797 // Turn the template-id annotation token into a type annotation
798 // token, then try again to parse it as a type-specifier.
Douglas Gregord7cb0372009-04-01 21:51:26 +0000799 AnnotateTemplateIdTokenAsType();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000800 continue;
801 }
802
Chris Lattner4b009652007-07-25 00:24:17 +0000803 // GNU attributes support.
804 case tok::kw___attribute:
805 DS.AddAttributes(ParseAttributes());
806 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000807
808 // Microsoft declspec support.
809 case tok::kw___declspec:
810 if (!PP.getLangOptions().Microsoft)
811 goto DoneWithDeclSpec;
812 FuzzyParseMicrosoftDeclSpec();
813 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000814
Steve Naroffedd04d52008-12-25 14:16:32 +0000815 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000816 case tok::kw___forceinline:
817 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000818 case tok::kw___cdecl:
819 case tok::kw___stdcall:
820 case tok::kw___fastcall:
821 if (!PP.getLangOptions().Microsoft)
822 goto DoneWithDeclSpec;
823 // Just ignore it.
824 break;
825
Chris Lattner4b009652007-07-25 00:24:17 +0000826 // storage-class-specifier
827 case tok::kw_typedef:
828 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
829 break;
830 case tok::kw_extern:
831 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000832 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000833 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
834 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000835 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000836 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
837 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000838 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000839 case tok::kw_static:
840 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000841 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000842 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
843 break;
844 case tok::kw_auto:
845 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
846 break;
847 case tok::kw_register:
848 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
849 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000850 case tok::kw_mutable:
851 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
852 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000853 case tok::kw___thread:
854 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
855 break;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000856
Chris Lattner4b009652007-07-25 00:24:17 +0000857 // function-specifier
858 case tok::kw_inline:
859 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
860 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000861 case tok::kw_virtual:
862 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
863 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000864 case tok::kw_explicit:
865 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
866 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000867
Anders Carlsson6c2ad5a2009-05-06 04:46:28 +0000868 // friend
869 case tok::kw_friend:
870 isInvalid = DS.SetFriendSpec(Loc, PrevSpec);
871 break;
872
Chris Lattnerc297b722009-01-21 19:48:37 +0000873 // type-specifier
874 case tok::kw_short:
875 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
876 break;
877 case tok::kw_long:
878 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
879 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
880 else
881 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
882 break;
883 case tok::kw_signed:
884 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
885 break;
886 case tok::kw_unsigned:
887 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
888 break;
889 case tok::kw__Complex:
890 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
891 break;
892 case tok::kw__Imaginary:
893 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
894 break;
895 case tok::kw_void:
896 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
897 break;
898 case tok::kw_char:
899 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
900 break;
901 case tok::kw_int:
902 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
903 break;
904 case tok::kw_float:
905 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
906 break;
907 case tok::kw_double:
908 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
909 break;
910 case tok::kw_wchar_t:
911 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
912 break;
913 case tok::kw_bool:
914 case tok::kw__Bool:
915 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
916 break;
917 case tok::kw__Decimal32:
918 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
919 break;
920 case tok::kw__Decimal64:
921 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
922 break;
923 case tok::kw__Decimal128:
924 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
925 break;
926
927 // class-specifier:
928 case tok::kw_class:
929 case tok::kw_struct:
Chris Lattner197b4342009-04-12 21:49:30 +0000930 case tok::kw_union: {
931 tok::TokenKind Kind = Tok.getKind();
932 ConsumeToken();
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000933 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000934 continue;
Chris Lattner197b4342009-04-12 21:49:30 +0000935 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000936
937 // enum-specifier:
938 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +0000939 ConsumeToken();
940 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000941 continue;
942
943 // cv-qualifier:
944 case tok::kw_const:
945 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
946 break;
947 case tok::kw_volatile:
948 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
949 getLang())*2;
950 break;
951 case tok::kw_restrict:
952 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
953 getLang())*2;
954 break;
955
Douglas Gregord3022602009-03-27 23:10:48 +0000956 // C++ typename-specifier:
957 case tok::kw_typename:
958 if (TryAnnotateTypeOrScopeToken())
959 continue;
960 break;
961
Chris Lattnerc297b722009-01-21 19:48:37 +0000962 // GNU typeof support.
963 case tok::kw_typeof:
964 ParseTypeofSpecifier(DS);
965 continue;
966
Steve Naroff5f0466b2008-06-05 00:02:44 +0000967 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000968 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000969 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
970 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000971 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000972 goto DoneWithDeclSpec;
973
974 {
975 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000976 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000977 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000978 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000979 DS.SetRangeEnd(EndProtoLoc);
980
Chris Lattnerf006a222008-11-18 07:48:38 +0000981 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattnerb980c732009-04-03 18:38:42 +0000982 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattnerf006a222008-11-18 07:48:38 +0000983 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000984 // Need to support trailing type qualifiers (e.g. "id<p> const").
985 // If a type specifier follows, it will be diagnosed elsewhere.
986 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000987 }
Chris Lattner4b009652007-07-25 00:24:17 +0000988 }
989 // If the specifier combination wasn't legal, issue a diagnostic.
990 if (isInvalid) {
991 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000992 // Pick between error or extwarn.
993 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
994 : diag::ext_duplicate_declspec;
995 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000996 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000997 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000998 ConsumeToken();
999 }
1000}
Douglas Gregorb3bec712008-12-01 23:54:00 +00001001
Chris Lattnerd706dc82009-01-06 06:59:53 +00001002/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001003/// primarily follow the C++ grammar with additions for C99 and GNU,
1004/// which together subsume the C grammar. Note that the C++
1005/// type-specifier also includes the C type-qualifier (for const,
1006/// volatile, and C99 restrict). Returns true if a type-specifier was
1007/// found (and parsed), false otherwise.
1008///
1009/// type-specifier: [C++ 7.1.5]
1010/// simple-type-specifier
1011/// class-specifier
1012/// enum-specifier
1013/// elaborated-type-specifier [TODO]
1014/// cv-qualifier
1015///
1016/// cv-qualifier: [C++ 7.1.5.1]
1017/// 'const'
1018/// 'volatile'
1019/// [C99] 'restrict'
1020///
1021/// simple-type-specifier: [ C++ 7.1.5.2]
1022/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1023/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1024/// 'char'
1025/// 'wchar_t'
1026/// 'bool'
1027/// 'short'
1028/// 'int'
1029/// 'long'
1030/// 'signed'
1031/// 'unsigned'
1032/// 'float'
1033/// 'double'
1034/// 'void'
1035/// [C99] '_Bool'
1036/// [C99] '_Complex'
1037/// [C99] '_Imaginary' // Removed in TC2?
1038/// [GNU] '_Decimal32'
1039/// [GNU] '_Decimal64'
1040/// [GNU] '_Decimal128'
1041/// [GNU] typeof-specifier
1042/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1043/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerd706dc82009-01-06 06:59:53 +00001044bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
1045 const char *&PrevSpec,
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001046 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001047 SourceLocation Loc = Tok.getLocation();
1048
1049 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +00001050 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001051 case tok::kw_typename: // typename foo::bar
Chris Lattnerb75fde62009-01-04 23:41:41 +00001052 // Annotate typenames and C++ scope specifiers. If we get one, just
1053 // recurse to handle whatever we get.
1054 if (TryAnnotateTypeOrScopeToken())
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001055 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
Chris Lattnerb75fde62009-01-04 23:41:41 +00001056 // Otherwise, not a type specifier.
1057 return false;
1058 case tok::coloncolon: // ::foo::bar
1059 if (NextToken().is(tok::kw_new) || // ::new
1060 NextToken().is(tok::kw_delete)) // ::delete
1061 return false;
1062
1063 // Annotate typenames and C++ scope specifiers. If we get one, just
1064 // recurse to handle whatever we get.
1065 if (TryAnnotateTypeOrScopeToken())
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001066 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateInfo);
Chris Lattnerb75fde62009-01-04 23:41:41 +00001067 // Otherwise, not a type specifier.
1068 return false;
1069
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001070 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +00001071 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +00001072 if (Tok.getAnnotationValue())
1073 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
1074 Tok.getAnnotationValue());
1075 else
1076 DS.SetTypeSpecError();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001077 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1078 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001079
1080 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1081 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1082 // Objective-C interface. If we don't have Objective-C or a '<', this is
1083 // just a normal reference to a typedef name.
1084 if (!Tok.is(tok::less) || !getLang().ObjC1)
1085 return true;
1086
1087 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001088 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001089 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
1090 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
1091
1092 DS.SetRangeEnd(EndProtoLoc);
1093 return true;
1094 }
1095
1096 case tok::kw_short:
1097 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
1098 break;
1099 case tok::kw_long:
1100 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
1101 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
1102 else
1103 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
1104 break;
1105 case tok::kw_signed:
1106 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
1107 break;
1108 case tok::kw_unsigned:
1109 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
1110 break;
1111 case tok::kw__Complex:
1112 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
1113 break;
1114 case tok::kw__Imaginary:
1115 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
1116 break;
1117 case tok::kw_void:
1118 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
1119 break;
1120 case tok::kw_char:
1121 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
1122 break;
1123 case tok::kw_int:
1124 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
1125 break;
1126 case tok::kw_float:
1127 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
1128 break;
1129 case tok::kw_double:
1130 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
1131 break;
1132 case tok::kw_wchar_t:
1133 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
1134 break;
1135 case tok::kw_bool:
1136 case tok::kw__Bool:
1137 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
1138 break;
1139 case tok::kw__Decimal32:
1140 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
1141 break;
1142 case tok::kw__Decimal64:
1143 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
1144 break;
1145 case tok::kw__Decimal128:
1146 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
1147 break;
1148
1149 // class-specifier:
1150 case tok::kw_class:
1151 case tok::kw_struct:
Chris Lattner197b4342009-04-12 21:49:30 +00001152 case tok::kw_union: {
1153 tok::TokenKind Kind = Tok.getKind();
1154 ConsumeToken();
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001155 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001156 return true;
Chris Lattner197b4342009-04-12 21:49:30 +00001157 }
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001158
1159 // enum-specifier:
1160 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +00001161 ConsumeToken();
1162 ParseEnumSpecifier(Loc, DS);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001163 return true;
1164
1165 // cv-qualifier:
1166 case tok::kw_const:
1167 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1168 getLang())*2;
1169 break;
1170 case tok::kw_volatile:
1171 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1172 getLang())*2;
1173 break;
1174 case tok::kw_restrict:
1175 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1176 getLang())*2;
1177 break;
1178
1179 // GNU typeof support.
1180 case tok::kw_typeof:
1181 ParseTypeofSpecifier(DS);
1182 return true;
1183
Steve Naroffedd04d52008-12-25 14:16:32 +00001184 case tok::kw___cdecl:
1185 case tok::kw___stdcall:
1186 case tok::kw___fastcall:
Chris Lattner5bb837e2009-01-21 19:19:26 +00001187 if (!PP.getLangOptions().Microsoft) return false;
1188 ConsumeToken();
1189 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +00001190
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001191 default:
1192 // Not a type-specifier; do nothing.
1193 return false;
1194 }
1195
1196 // If the specifier combination wasn't legal, issue a diagnostic.
1197 if (isInvalid) {
1198 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001199 // Pick between error or extwarn.
1200 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1201 : diag::ext_duplicate_declspec;
1202 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001203 }
1204 DS.SetRangeEnd(Tok.getLocation());
1205 ConsumeToken(); // whatever we parsed above.
1206 return true;
1207}
Chris Lattner4b009652007-07-25 00:24:17 +00001208
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001209/// ParseStructDeclaration - Parse a struct declaration without the terminating
1210/// semicolon.
1211///
Chris Lattner4b009652007-07-25 00:24:17 +00001212/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001213/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001214/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001215/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +00001216/// struct-declarator-list:
1217/// struct-declarator
1218/// struct-declarator-list ',' struct-declarator
1219/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1220/// struct-declarator:
1221/// declarator
1222/// [GNU] declarator attributes[opt]
1223/// declarator[opt] ':' constant-expression
1224/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1225///
Chris Lattner3dd8d392008-04-10 06:46:29 +00001226void Parser::
1227ParseStructDeclaration(DeclSpec &DS,
1228 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001229 if (Tok.is(tok::kw___extension__)) {
1230 // __extension__ silences extension warnings in the subexpression.
1231 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001232 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001233 return ParseStructDeclaration(DS, Fields);
1234 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001235
1236 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001237 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001238 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001239
Douglas Gregorb748fc52009-01-12 22:49:06 +00001240 // If there are no declarators, this is a free-standing declaration
1241 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001242 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001243 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001244 return;
1245 }
1246
1247 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001248 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001249 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001250 FieldDeclarator &DeclaratorInfo = Fields.back();
1251
Steve Naroffa9adf112007-08-20 22:28:22 +00001252 /// struct-declarator: declarator
1253 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001254 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001255 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001256
Chris Lattner34a01ad2007-10-09 17:33:22 +00001257 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001258 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001259 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001260 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001261 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001262 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001263 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001264 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001265
Steve Naroffa9adf112007-08-20 22:28:22 +00001266 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001267 if (Tok.is(tok::kw___attribute)) {
1268 SourceLocation Loc;
1269 AttributeList *AttrList = ParseAttributes(&Loc);
1270 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1271 }
1272
Steve Naroffa9adf112007-08-20 22:28:22 +00001273 // If we don't have a comma, it is either the end of the list (a ';')
1274 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001275 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001276 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001277
Steve Naroffa9adf112007-08-20 22:28:22 +00001278 // Consume the comma.
1279 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001280
Steve Naroffa9adf112007-08-20 22:28:22 +00001281 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001282 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001283
Steve Naroffa9adf112007-08-20 22:28:22 +00001284 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001285 if (Tok.is(tok::kw___attribute)) {
1286 SourceLocation Loc;
1287 AttributeList *AttrList = ParseAttributes(&Loc);
1288 Fields.back().D.AddAttributes(AttrList, Loc);
1289 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001290 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001291}
1292
1293/// ParseStructUnionBody
1294/// struct-contents:
1295/// struct-declaration-list
1296/// [EXT] empty
1297/// [GNU] "struct-declaration-list" without terminatoring ';'
1298/// struct-declaration-list:
1299/// struct-declaration
1300/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001301/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001302///
Chris Lattner4b009652007-07-25 00:24:17 +00001303void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001304 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001305 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1306 PP.getSourceManager(),
1307 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001308
Chris Lattner4b009652007-07-25 00:24:17 +00001309 SourceLocation LBraceLoc = ConsumeBrace();
1310
Douglas Gregorcab994d2009-01-09 22:42:13 +00001311 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001312 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1313
Chris Lattner4b009652007-07-25 00:24:17 +00001314 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1315 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001316 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001317 Diag(Tok, diag::ext_empty_struct_union_enum)
1318 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001319
Chris Lattner5261d0c2009-03-28 19:18:32 +00001320 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001321 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1322
Chris Lattner4b009652007-07-25 00:24:17 +00001323 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001324 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001325 // Each iteration of this loop reads one struct-declaration.
1326
1327 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001328 if (Tok.is(tok::semi)) {
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001329 Diag(Tok, diag::ext_extra_struct_semi)
1330 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +00001331 ConsumeToken();
1332 continue;
1333 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001334
1335 // Parse all the comma separated declarators.
1336 DeclSpec DS;
1337 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001338 if (!Tok.is(tok::at)) {
1339 ParseStructDeclaration(DS, FieldDeclarators);
1340
1341 // Convert them all to fields.
1342 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1343 FieldDeclarator &FD = FieldDeclarators[i];
1344 // Install the declarator into the current TagDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001345 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1346 DS.getSourceRange().getBegin(),
1347 FD.D, FD.BitfieldSize);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001348 FieldDecls.push_back(Field);
1349 }
1350 } else { // Handle @defs
1351 ConsumeToken();
1352 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1353 Diag(Tok, diag::err_unexpected_at);
1354 SkipUntil(tok::semi, true, true);
1355 continue;
1356 }
1357 ConsumeToken();
1358 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1359 if (!Tok.is(tok::identifier)) {
1360 Diag(Tok, diag::err_expected_ident);
1361 SkipUntil(tok::semi, true, true);
1362 continue;
1363 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001364 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001365 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1366 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001367 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1368 ConsumeToken();
1369 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1370 }
Chris Lattner4b009652007-07-25 00:24:17 +00001371
Chris Lattner34a01ad2007-10-09 17:33:22 +00001372 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001373 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001374 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001375 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001376 break;
1377 } else {
1378 Diag(Tok, diag::err_expected_semi_decl_list);
1379 // Skip to end of block or statement
1380 SkipUntil(tok::r_brace, true, true);
1381 }
1382 }
1383
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001384 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001385
Chris Lattner4b009652007-07-25 00:24:17 +00001386 AttributeList *AttrList = 0;
1387 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001388 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001389 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001390
1391 Actions.ActOnFields(CurScope,
Jay Foad9e6bef42009-05-21 09:52:38 +00001392 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbarf3944442008-10-03 02:03:53 +00001393 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001394 AttrList);
1395 StructScope.Exit();
1396 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001397}
1398
1399
1400/// ParseEnumSpecifier
1401/// enum-specifier: [C99 6.7.2.2]
1402/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001403///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001404/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1405/// '}' attributes[opt]
1406/// 'enum' identifier
1407/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001408///
1409/// [C++] elaborated-type-specifier:
1410/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1411///
Chris Lattner197b4342009-04-12 21:49:30 +00001412void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1413 AccessSpecifier AS) {
Chris Lattner4b009652007-07-25 00:24:17 +00001414 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001415
1416 AttributeList *Attr = 0;
1417 // If attributes exist after tag, parse them.
1418 if (Tok.is(tok::kw___attribute))
1419 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001420
1421 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001422 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001423 if (Tok.isNot(tok::identifier)) {
1424 Diag(Tok, diag::err_expected_ident);
1425 if (Tok.isNot(tok::l_brace)) {
1426 // Has no name and is not a definition.
1427 // Skip the rest of this declarator, up until the comma or semicolon.
1428 SkipUntil(tok::comma, true);
1429 return;
1430 }
1431 }
1432 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001433
1434 // Must have either 'enum name' or 'enum {...}'.
1435 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1436 Diag(Tok, diag::err_expected_ident_lbrace);
1437
1438 // Skip the rest of this declarator, up until the comma or semicolon.
1439 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001440 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001441 }
1442
1443 // If an identifier is present, consume and remember it.
1444 IdentifierInfo *Name = 0;
1445 SourceLocation NameLoc;
1446 if (Tok.is(tok::identifier)) {
1447 Name = Tok.getIdentifierInfo();
1448 NameLoc = ConsumeToken();
1449 }
1450
1451 // There are three options here. If we have 'enum foo;', then this is a
1452 // forward declaration. If we have 'enum foo {...' then this is a
1453 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1454 //
1455 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1456 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1457 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1458 //
1459 Action::TagKind TK;
1460 if (Tok.is(tok::l_brace))
1461 TK = Action::TK_Definition;
1462 else if (Tok.is(tok::semi))
1463 TK = Action::TK_Declaration;
1464 else
1465 TK = Action::TK_Reference;
Douglas Gregor71f06032009-05-28 23:31:59 +00001466 bool Owned = false;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001467 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
Douglas Gregor71f06032009-05-28 23:31:59 +00001468 StartLoc, SS, Name, NameLoc, Attr, AS,
1469 Owned);
Chris Lattner4b009652007-07-25 00:24:17 +00001470
Chris Lattner34a01ad2007-10-09 17:33:22 +00001471 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001472 ParseEnumBody(StartLoc, TagDecl);
1473
1474 // TODO: semantic analysis on the declspec for enums.
1475 const char *PrevSpec = 0;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001476 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
Douglas Gregor71f06032009-05-28 23:31:59 +00001477 TagDecl.getAs<void>(), Owned))
Chris Lattnerf006a222008-11-18 07:48:38 +00001478 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001479}
1480
1481/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1482/// enumerator-list:
1483/// enumerator
1484/// enumerator-list ',' enumerator
1485/// enumerator:
1486/// enumeration-constant
1487/// enumeration-constant '=' constant-expression
1488/// enumeration-constant:
1489/// identifier
1490///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001491void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001492 // Enter the scope of the enum body and start the definition.
1493 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001494 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001495
Chris Lattner4b009652007-07-25 00:24:17 +00001496 SourceLocation LBraceLoc = ConsumeBrace();
1497
Chris Lattnerc9a92452007-08-27 17:24:30 +00001498 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001499 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001500 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001501
Chris Lattner5261d0c2009-03-28 19:18:32 +00001502 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattner4b009652007-07-25 00:24:17 +00001503
Chris Lattner5261d0c2009-03-28 19:18:32 +00001504 DeclPtrTy LastEnumConstDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001505
1506 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001507 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001508 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1509 SourceLocation IdentLoc = ConsumeToken();
1510
1511 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001512 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001513 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001514 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001515 AssignedVal = ParseConstantExpression();
1516 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001517 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001518 }
1519
1520 // Install the enumerator constant into EnumDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001521 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1522 LastEnumConstDecl,
1523 IdentLoc, Ident,
1524 EqualLoc,
1525 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001526 EnumConstantDecls.push_back(EnumConstDecl);
1527 LastEnumConstDecl = EnumConstDecl;
1528
Chris Lattner34a01ad2007-10-09 17:33:22 +00001529 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001530 break;
1531 SourceLocation CommaLoc = ConsumeToken();
1532
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001533 if (Tok.isNot(tok::identifier) &&
1534 !(getLang().C99 || getLang().CPlusPlus0x))
1535 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1536 << getLang().CPlusPlus
1537 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Chris Lattner4b009652007-07-25 00:24:17 +00001538 }
1539
1540 // Eat the }.
Mike Stump155750e2009-05-16 07:06:02 +00001541 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001542
Mike Stump155750e2009-05-16 07:06:02 +00001543 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
Jay Foad9e6bef42009-05-21 09:52:38 +00001544 EnumConstantDecls.data(), EnumConstantDecls.size());
Chris Lattner4b009652007-07-25 00:24:17 +00001545
Chris Lattner5261d0c2009-03-28 19:18:32 +00001546 Action::AttrTy *AttrList = 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001547 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001548 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001549 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001550
1551 EnumScope.Exit();
1552 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001553}
1554
1555/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001556/// start of a type-qualifier-list.
1557bool Parser::isTypeQualifier() const {
1558 switch (Tok.getKind()) {
1559 default: return false;
1560 // type-qualifier
1561 case tok::kw_const:
1562 case tok::kw_volatile:
1563 case tok::kw_restrict:
1564 return true;
1565 }
1566}
1567
1568/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001569/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001570bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001571 switch (Tok.getKind()) {
1572 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001573
1574 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001575 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001576 // Annotate typenames and C++ scope specifiers. If we get one, just
1577 // recurse to handle whatever we get.
1578 if (TryAnnotateTypeOrScopeToken())
1579 return isTypeSpecifierQualifier();
1580 // Otherwise, not a type specifier.
1581 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001582
Chris Lattnerb75fde62009-01-04 23:41:41 +00001583 case tok::coloncolon: // ::foo::bar
1584 if (NextToken().is(tok::kw_new) || // ::new
1585 NextToken().is(tok::kw_delete)) // ::delete
1586 return false;
1587
1588 // Annotate typenames and C++ scope specifiers. If we get one, just
1589 // recurse to handle whatever we get.
1590 if (TryAnnotateTypeOrScopeToken())
1591 return isTypeSpecifierQualifier();
1592 // Otherwise, not a type specifier.
1593 return false;
1594
Chris Lattner4b009652007-07-25 00:24:17 +00001595 // GNU attributes support.
1596 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001597 // GNU typeof support.
1598 case tok::kw_typeof:
1599
Chris Lattner4b009652007-07-25 00:24:17 +00001600 // type-specifiers
1601 case tok::kw_short:
1602 case tok::kw_long:
1603 case tok::kw_signed:
1604 case tok::kw_unsigned:
1605 case tok::kw__Complex:
1606 case tok::kw__Imaginary:
1607 case tok::kw_void:
1608 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001609 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001610 case tok::kw_int:
1611 case tok::kw_float:
1612 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001613 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001614 case tok::kw__Bool:
1615 case tok::kw__Decimal32:
1616 case tok::kw__Decimal64:
1617 case tok::kw__Decimal128:
1618
Chris Lattner2e78db32008-04-13 18:59:07 +00001619 // struct-or-union-specifier (C99) or class-specifier (C++)
1620 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001621 case tok::kw_struct:
1622 case tok::kw_union:
1623 // enum-specifier
1624 case tok::kw_enum:
1625
1626 // type-qualifier
1627 case tok::kw_const:
1628 case tok::kw_volatile:
1629 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001630
1631 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001632 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001633 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001634
1635 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1636 case tok::less:
1637 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001638
1639 case tok::kw___cdecl:
1640 case tok::kw___stdcall:
1641 case tok::kw___fastcall:
1642 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001643 }
1644}
1645
1646/// isDeclarationSpecifier() - Return true if the current token is part of a
1647/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001648bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001649 switch (Tok.getKind()) {
1650 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001651
1652 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-03-09 21:12:44 +00001653 // Unfortunate hack to support "Class.factoryMethod" notation.
1654 if (getLang().ObjC1 && NextToken().is(tok::period))
1655 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001656 // Fall through
Steve Naroff73ec9322009-03-09 21:12:44 +00001657
Douglas Gregord3022602009-03-27 23:10:48 +00001658 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001659 // Annotate typenames and C++ scope specifiers. If we get one, just
1660 // recurse to handle whatever we get.
1661 if (TryAnnotateTypeOrScopeToken())
1662 return isDeclarationSpecifier();
1663 // Otherwise, not a declaration specifier.
1664 return false;
1665 case tok::coloncolon: // ::foo::bar
1666 if (NextToken().is(tok::kw_new) || // ::new
1667 NextToken().is(tok::kw_delete)) // ::delete
1668 return false;
1669
1670 // Annotate typenames and C++ scope specifiers. If we get one, just
1671 // recurse to handle whatever we get.
1672 if (TryAnnotateTypeOrScopeToken())
1673 return isDeclarationSpecifier();
1674 // Otherwise, not a declaration specifier.
1675 return false;
1676
Chris Lattner4b009652007-07-25 00:24:17 +00001677 // storage-class-specifier
1678 case tok::kw_typedef:
1679 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001680 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001681 case tok::kw_static:
1682 case tok::kw_auto:
1683 case tok::kw_register:
1684 case tok::kw___thread:
1685
1686 // type-specifiers
1687 case tok::kw_short:
1688 case tok::kw_long:
1689 case tok::kw_signed:
1690 case tok::kw_unsigned:
1691 case tok::kw__Complex:
1692 case tok::kw__Imaginary:
1693 case tok::kw_void:
1694 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001695 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001696 case tok::kw_int:
1697 case tok::kw_float:
1698 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001699 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001700 case tok::kw__Bool:
1701 case tok::kw__Decimal32:
1702 case tok::kw__Decimal64:
1703 case tok::kw__Decimal128:
1704
Chris Lattner2e78db32008-04-13 18:59:07 +00001705 // struct-or-union-specifier (C99) or class-specifier (C++)
1706 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001707 case tok::kw_struct:
1708 case tok::kw_union:
1709 // enum-specifier
1710 case tok::kw_enum:
1711
1712 // type-qualifier
1713 case tok::kw_const:
1714 case tok::kw_volatile:
1715 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001716
Chris Lattner4b009652007-07-25 00:24:17 +00001717 // function-specifier
1718 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001719 case tok::kw_virtual:
1720 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001721
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001722 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001723 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001724
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001725 // GNU typeof support.
1726 case tok::kw_typeof:
1727
1728 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001729 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001730 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001731
1732 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1733 case tok::less:
1734 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001735
Steve Naroffab1a3632009-01-06 19:34:12 +00001736 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001737 case tok::kw___cdecl:
1738 case tok::kw___stdcall:
1739 case tok::kw___fastcall:
1740 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001741 }
1742}
1743
1744
1745/// ParseTypeQualifierListOpt
1746/// type-qualifier-list: [C99 6.7.5]
1747/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001748/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001749/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001750/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001751///
Chris Lattner460696f2008-12-18 07:02:59 +00001752void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001753 while (1) {
1754 int isInvalid = false;
1755 const char *PrevSpec = 0;
1756 SourceLocation Loc = Tok.getLocation();
1757
1758 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001759 case tok::kw_const:
1760 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1761 getLang())*2;
1762 break;
1763 case tok::kw_volatile:
1764 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1765 getLang())*2;
1766 break;
1767 case tok::kw_restrict:
1768 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1769 getLang())*2;
1770 break;
Steve Naroffad620402008-12-25 14:41:26 +00001771 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001772 case tok::kw___cdecl:
1773 case tok::kw___stdcall:
1774 case tok::kw___fastcall:
1775 if (!PP.getLangOptions().Microsoft)
1776 goto DoneWithTypeQuals;
1777 // Just ignore it.
1778 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001779 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001780 if (AttributesAllowed) {
1781 DS.AddAttributes(ParseAttributes());
1782 continue; // do *not* consume the next token!
1783 }
1784 // otherwise, FALL THROUGH!
1785 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001786 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001787 // If this is not a type-qualifier token, we're done reading type
1788 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001789 DS.Finish(Diags, PP);
Chris Lattner460696f2008-12-18 07:02:59 +00001790 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001791 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001792
Chris Lattner4b009652007-07-25 00:24:17 +00001793 // If the specifier combination wasn't legal, issue a diagnostic.
1794 if (isInvalid) {
1795 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001796 // Pick between error or extwarn.
1797 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1798 : diag::ext_duplicate_declspec;
1799 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001800 }
1801 ConsumeToken();
1802 }
1803}
1804
1805
1806/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1807///
1808void Parser::ParseDeclarator(Declarator &D) {
1809 /// This implements the 'declarator' production in the C grammar, then checks
1810 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001811 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001812}
1813
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001814/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1815/// is parsed by the function passed to it. Pass null, and the direct-declarator
1816/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001817/// ptr-operator production.
1818///
Sebastian Redl75555032009-01-24 21:16:55 +00001819/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1820/// [C] pointer[opt] direct-declarator
1821/// [C++] direct-declarator
1822/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001823///
1824/// pointer: [C99 6.7.5]
1825/// '*' type-qualifier-list[opt]
1826/// '*' type-qualifier-list[opt] pointer
1827///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001828/// ptr-operator:
1829/// '*' cv-qualifier-seq[opt]
1830/// '&'
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001831/// [C++0x] '&&'
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001832/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001833/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001834/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001835void Parser::ParseDeclaratorInternal(Declarator &D,
1836 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001837
Sebastian Redl75555032009-01-24 21:16:55 +00001838 // C++ member pointers start with a '::' or a nested-name.
1839 // Member pointers get special handling, since there's no place for the
1840 // scope spec in the generic path below.
Chris Lattner053dd2d2009-03-24 17:04:48 +00001841 if (getLang().CPlusPlus &&
1842 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1843 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl75555032009-01-24 21:16:55 +00001844 CXXScopeSpec SS;
1845 if (ParseOptionalCXXScopeSpecifier(SS)) {
1846 if(Tok.isNot(tok::star)) {
1847 // The scope spec really belongs to the direct-declarator.
1848 D.getCXXScopeSpec() = SS;
1849 if (DirectDeclParser)
1850 (this->*DirectDeclParser)(D);
1851 return;
1852 }
1853
1854 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001855 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00001856 DeclSpec DS;
1857 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001858 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001859
1860 // Recurse to parse whatever is left.
1861 ParseDeclaratorInternal(D, DirectDeclParser);
1862
1863 // Sema will have to catch (syntactically invalid) pointers into global
1864 // scope. It has to catch pointers into namespace scope anyway.
1865 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001866 Loc, DS.TakeAttributes()),
1867 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00001868 return;
1869 }
1870 }
1871
1872 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001873 // Not a pointer, C++ reference, or block.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001874 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner053dd2d2009-03-24 17:04:48 +00001875 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001876 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001877 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001878 if (DirectDeclParser)
1879 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001880 return;
1881 }
Sebastian Redl75555032009-01-24 21:16:55 +00001882
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001883 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1884 // '&&' -> rvalue reference
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001885 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redl0c986032009-02-09 18:23:29 +00001886 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001887
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001888 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner69f01932008-02-21 01:32:26 +00001889 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001890 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001891
Chris Lattner4b009652007-07-25 00:24:17 +00001892 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001893 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001894
Chris Lattner4b009652007-07-25 00:24:17 +00001895 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001896 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001897 if (Kind == tok::star)
1898 // Remember that we parsed a pointer type, and remember the type-quals.
1899 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001900 DS.TakeAttributes()),
1901 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00001902 else
1903 // Remember that we parsed a Block type, and remember the type-quals.
1904 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump7ff82e72009-04-21 00:51:43 +00001905 Loc, DS.TakeAttributes()),
Sebastian Redl0c986032009-02-09 18:23:29 +00001906 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001907 } else {
1908 // Is a reference
1909 DeclSpec DS;
1910
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001911 // Complain about rvalue references in C++03, but then go on and build
1912 // the declarator.
1913 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1914 Diag(Loc, diag::err_rvalue_reference);
1915
Chris Lattner4b009652007-07-25 00:24:17 +00001916 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1917 // cv-qualifiers are introduced through the use of a typedef or of a
1918 // template type argument, in which case the cv-qualifiers are ignored.
1919 //
1920 // [GNU] Retricted references are allowed.
1921 // [GNU] Attributes on references are allowed.
1922 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001923 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00001924
1925 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1926 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1927 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001928 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001929 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1930 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001931 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001932 }
1933
1934 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001935 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001936
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001937 if (D.getNumTypeObjects() > 0) {
1938 // C++ [dcl.ref]p4: There shall be no references to references.
1939 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1940 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001941 if (const IdentifierInfo *II = D.getIdentifier())
1942 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1943 << II;
1944 else
1945 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1946 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001947
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001948 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001949 // can go ahead and build the (technically ill-formed)
1950 // declarator: reference collapsing will take care of it.
1951 }
1952 }
1953
Chris Lattner4b009652007-07-25 00:24:17 +00001954 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001955 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001956 DS.TakeAttributes(),
1957 Kind == tok::amp),
Sebastian Redl0c986032009-02-09 18:23:29 +00001958 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001959 }
1960}
1961
1962/// ParseDirectDeclarator
1963/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001964/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001965/// '(' declarator ')'
1966/// [GNU] '(' attributes declarator ')'
1967/// [C90] direct-declarator '[' constant-expression[opt] ']'
1968/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1969/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1970/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1971/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1972/// direct-declarator '(' parameter-type-list ')'
1973/// direct-declarator '(' identifier-list[opt] ')'
1974/// [GNU] direct-declarator '(' parameter-forward-declarations
1975/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001976/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1977/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001978/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001979///
1980/// declarator-id: [C++ 8]
1981/// id-expression
1982/// '::'[opt] nested-name-specifier[opt] type-name
1983///
1984/// id-expression: [C++ 5.1]
1985/// unqualified-id
1986/// qualified-id [TODO]
1987///
1988/// unqualified-id: [C++ 5.1]
1989/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001990/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001991/// conversion-function-id [TODO]
1992/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00001993/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001994///
Chris Lattner4b009652007-07-25 00:24:17 +00001995void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001996 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001997
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001998 if (getLang().CPlusPlus) {
1999 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00002000 // ParseDeclaratorInternal might already have parsed the scope.
2001 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2002 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002003 if (afterCXXScope) {
2004 // Change the declaration context for name lookup, until this function
2005 // is exited (and the declarator has been parsed).
2006 DeclScopeObj.EnterDeclaratorScope();
2007 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002008
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002009 if (Tok.is(tok::identifier)) {
2010 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Anders Carlssone19759d2009-04-30 22:41:11 +00002011
2012 // If this identifier is the name of the current class, it's a
2013 // constructor name.
2014 if (!D.getDeclSpec().hasTypeSpecifier() &&
2015 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
2016 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
2017 Tok.getLocation(), CurScope),
2018 Tok.getLocation());
2019 // This is a normal identifier.
2020 } else
2021 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002022 ConsumeToken();
2023 goto PastIdentifier;
Douglas Gregor0c281a82009-02-25 19:37:18 +00002024 } else if (Tok.is(tok::annot_template_id)) {
2025 TemplateIdAnnotation *TemplateId
2026 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
2027
2028 // FIXME: Could this template-id name a constructor?
2029
2030 // FIXME: This is an egregious hack, where we silently ignore
2031 // the specialization (which should be a function template
2032 // specialization name) and use the name instead. This hack
2033 // will go away when we have support for function
2034 // specializations.
2035 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
2036 TemplateId->Destroy();
2037 ConsumeToken();
2038 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00002039 } else if (Tok.is(tok::kw_operator)) {
2040 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00002041 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00002042
Douglas Gregor853dd392008-12-26 15:00:45 +00002043 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00002044 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
2045 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00002046 } else {
2047 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00002048 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
2049 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
2050 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00002051 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00002052 }
Douglas Gregor853dd392008-12-26 15:00:45 +00002053 }
2054 goto PastIdentifier;
2055 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002056 // This should be a C++ destructor.
2057 SourceLocation TildeLoc = ConsumeToken();
2058 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002059 // FIXME: Inaccurate.
2060 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00002061 SourceLocation EndLoc;
Douglas Gregord7cb0372009-04-01 21:51:26 +00002062 TypeResult Type = ParseClassName(EndLoc);
2063 if (Type.isInvalid())
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002064 D.SetIdentifier(0, TildeLoc);
Douglas Gregord7cb0372009-04-01 21:51:26 +00002065 else
2066 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002067 } else {
2068 Diag(Tok, diag::err_expected_class_name);
2069 D.SetIdentifier(0, TildeLoc);
2070 }
2071 goto PastIdentifier;
2072 }
2073
2074 // If we reached this point, token is not identifier and not '~'.
2075
2076 if (afterCXXScope) {
2077 Diag(Tok, diag::err_expected_unqualified_id);
2078 D.SetIdentifier(0, Tok.getLocation());
2079 D.setInvalidType(true);
2080 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00002081 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00002082 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002083 }
2084
2085 // If we reached this point, we are either in C/ObjC or the token didn't
2086 // satisfy any of the C++-specific checks.
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002087 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2088 assert(!getLang().CPlusPlus &&
2089 "There's a C++-specific check for tok::identifier above");
2090 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2091 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2092 ConsumeToken();
2093 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002094 // direct-declarator: '(' declarator ')'
2095 // direct-declarator: '(' attributes declarator ')'
2096 // Example: 'char (*X)' or 'int (*XX)(void)'
2097 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002098 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002099 // This could be something simple like "int" (in which case the declarator
2100 // portion is empty), if an abstract-declarator is allowed.
2101 D.SetIdentifier(0, Tok.getLocation());
2102 } else {
Douglas Gregorf03265d2009-03-06 23:28:18 +00002103 if (D.getContext() == Declarator::MemberContext)
2104 Diag(Tok, diag::err_expected_member_name_or_semi)
2105 << D.getDeclSpec().getSourceRange();
2106 else if (getLang().CPlusPlus)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002107 Diag(Tok, diag::err_expected_unqualified_id);
2108 else
Chris Lattnerf006a222008-11-18 07:48:38 +00002109 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00002110 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00002111 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00002112 }
2113
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002114 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00002115 assert(D.isPastIdentifier() &&
2116 "Haven't past the location of the identifier yet?");
2117
2118 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002119 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002120 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2121 // In such a case, check if we actually have a function declarator; if it
2122 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00002123 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2124 // When not in file scope, warn for ambiguous function declarators, just
2125 // in case the author intended it as a variable definition.
2126 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2127 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2128 break;
2129 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00002130 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00002131 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002132 ParseBracketDeclarator(D);
2133 } else {
2134 break;
2135 }
2136 }
2137}
2138
Chris Lattnera0d056d2008-04-06 05:45:57 +00002139/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2140/// only called before the identifier, so these are most likely just grouping
2141/// parens for precedence. If we find that these are actually function
2142/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2143///
2144/// direct-declarator:
2145/// '(' declarator ')'
2146/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00002147/// direct-declarator '(' parameter-type-list ')'
2148/// direct-declarator '(' identifier-list[opt] ')'
2149/// [GNU] direct-declarator '(' parameter-forward-declarations
2150/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00002151///
2152void Parser::ParseParenDeclarator(Declarator &D) {
2153 SourceLocation StartLoc = ConsumeParen();
2154 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
2155
Chris Lattner1f185292008-10-20 02:05:46 +00002156 // Eat any attributes before we look at whether this is a grouping or function
2157 // declarator paren. If this is a grouping paren, the attribute applies to
2158 // the type being built up, for example:
2159 // int (__attribute__(()) *x)(long y)
2160 // If this ends up not being a grouping paren, the attribute applies to the
2161 // first argument, for example:
2162 // int (__attribute__(()) int x)
2163 // In either case, we need to eat any attributes to be able to determine what
2164 // sort of paren this is.
2165 //
2166 AttributeList *AttrList = 0;
2167 bool RequiresArg = false;
2168 if (Tok.is(tok::kw___attribute)) {
2169 AttrList = ParseAttributes();
2170
2171 // We require that the argument list (if this is a non-grouping paren) be
2172 // present even if the attribute list was empty.
2173 RequiresArg = true;
2174 }
Steve Naroffedd04d52008-12-25 14:16:32 +00002175 // Eat any Microsoft extensions.
Douglas Gregore51b7c82009-01-10 00:48:18 +00002176 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2177 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroffedd04d52008-12-25 14:16:32 +00002178 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00002179
Chris Lattnera0d056d2008-04-06 05:45:57 +00002180 // If we haven't past the identifier yet (or where the identifier would be
2181 // stored, if this is an abstract declarator), then this is probably just
2182 // grouping parens. However, if this could be an abstract-declarator, then
2183 // this could also be the start of function arguments (consider 'void()').
2184 bool isGrouping;
2185
2186 if (!D.mayOmitIdentifier()) {
2187 // If this can't be an abstract-declarator, this *must* be a grouping
2188 // paren, because we haven't seen the identifier yet.
2189 isGrouping = true;
2190 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002191 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00002192 isDeclarationSpecifier()) { // 'int(int)' is a function.
2193 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2194 // considered to be a type, not a K&R identifier-list.
2195 isGrouping = false;
2196 } else {
2197 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2198 isGrouping = true;
2199 }
2200
2201 // If this is a grouping paren, handle:
2202 // direct-declarator: '(' declarator ')'
2203 // direct-declarator: '(' attributes declarator ')'
2204 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002205 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002206 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00002207 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00002208 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002209
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002210 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002211 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00002212 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002213
2214 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00002215 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002216 return;
2217 }
2218
2219 // Okay, if this wasn't a grouping paren, it must be the start of a function
2220 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00002221 // identifier (and remember where it would have been), then call into
2222 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00002223 D.SetIdentifier(0, Tok.getLocation());
2224
Chris Lattner1f185292008-10-20 02:05:46 +00002225 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002226}
2227
2228/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2229/// declarator D up to a paren, which indicates that we are parsing function
2230/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00002231///
Chris Lattner1f185292008-10-20 02:05:46 +00002232/// If AttrList is non-null, then the caller parsed those arguments immediately
2233/// after the open paren - they should be considered to be the first argument of
2234/// a parameter. If RequiresArg is true, then the first argument of the
2235/// function is required to be present and required to not be an identifier
2236/// list.
2237///
Chris Lattner4b009652007-07-25 00:24:17 +00002238/// This method also handles this portion of the grammar:
2239/// parameter-type-list: [C99 6.7.5]
2240/// parameter-list
2241/// parameter-list ',' '...'
2242///
2243/// parameter-list: [C99 6.7.5]
2244/// parameter-declaration
2245/// parameter-list ',' parameter-declaration
2246///
2247/// parameter-declaration: [C99 6.7.5]
2248/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00002249/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002250/// [GNU] declaration-specifiers declarator attributes
Sebastian Redla8cecf62009-03-24 22:27:57 +00002251/// declaration-specifiers abstract-declarator[opt]
2252/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002253/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002254/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2255///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002256/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redla8cecf62009-03-24 22:27:57 +00002257/// and "exception-specification[opt]".
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002258///
Chris Lattner1f185292008-10-20 02:05:46 +00002259void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2260 AttributeList *AttrList,
2261 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002262 // lparen is already consumed!
2263 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002264
Chris Lattner1f185292008-10-20 02:05:46 +00002265 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002266 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002267 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002268 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002269 delete AttrList;
2270 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002271
Sebastian Redl0c986032009-02-09 18:23:29 +00002272 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002273
2274 // cv-qualifier-seq[opt].
2275 DeclSpec DS;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002276 bool hasExceptionSpec = false;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002277 SourceLocation ThrowLoc;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002278 bool hasAnyExceptionSpec = false;
Sebastian Redlaaacda92009-05-29 18:02:33 +00002279 llvm::SmallVector<TypeTy*, 2> Exceptions;
2280 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002281 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002282 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002283 if (!DS.getSourceRange().getEnd().isInvalid())
2284 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002285
2286 // Parse exception-specification[opt].
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002287 if (Tok.is(tok::kw_throw)) {
2288 hasExceptionSpec = true;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002289 ThrowLoc = Tok.getLocation();
Sebastian Redlaaacda92009-05-29 18:02:33 +00002290 ParseExceptionSpecification(Loc, Exceptions, ExceptionRanges,
2291 hasAnyExceptionSpec);
2292 assert(Exceptions.size() == ExceptionRanges.size() &&
2293 "Produced different number of exception types and ranges.");
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002294 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002295 }
2296
Chris Lattner9f7564b2008-04-06 06:57:35 +00002297 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002298 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002299 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002300 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002301 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002302 /*arglist*/ 0, 0,
2303 DS.getTypeQualifiers(),
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002304 hasExceptionSpec, ThrowLoc,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002305 hasAnyExceptionSpec,
Sebastian Redlaaacda92009-05-29 18:02:33 +00002306 Exceptions.data(),
2307 ExceptionRanges.data(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002308 Exceptions.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002309 LParenLoc, D),
2310 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002311 return;
Sebastian Redlaaacda92009-05-29 18:02:33 +00002312 }
2313
Chris Lattner1f185292008-10-20 02:05:46 +00002314 // Alternatively, this parameter list may be an identifier list form for a
2315 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002316 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002317 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002318 // K&R identifier lists can't have typedefs as identifiers, per
2319 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002320 if (RequiresArg) {
2321 Diag(Tok, diag::err_argument_required_after_attribute);
2322 delete AttrList;
2323 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002324 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2325 // normal declarators, not for abstract-declarators.
2326 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002327 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002328 }
2329
2330 // Finally, a normal, non-empty parameter type list.
2331
2332 // Build up an array of information about the parsed arguments.
2333 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002334
2335 // Enter function-declaration scope, limiting any declarators to the
2336 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002337 ParseScope PrototypeScope(this,
2338 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002339
2340 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002341 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002342 while (1) {
2343 if (Tok.is(tok::ellipsis)) {
2344 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002345 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002346 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002347 }
2348
Chris Lattner9f7564b2008-04-06 06:57:35 +00002349 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002350
Chris Lattner9f7564b2008-04-06 06:57:35 +00002351 // Parse the declaration-specifiers.
2352 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002353
2354 // If the caller parsed attributes for the first argument, add them now.
2355 if (AttrList) {
2356 DS.AddAttributes(AttrList);
2357 AttrList = 0; // Only apply the attributes to the first parameter.
2358 }
Chris Lattner9e785f52009-02-27 18:38:20 +00002359 ParseDeclarationSpecifiers(DS);
2360
Chris Lattner9f7564b2008-04-06 06:57:35 +00002361 // Parse the declarator. This is "PrototypeContext", because we must
2362 // accept either 'declarator' or 'abstract-declarator' here.
2363 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2364 ParseDeclarator(ParmDecl);
2365
2366 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002367 if (Tok.is(tok::kw___attribute)) {
2368 SourceLocation Loc;
2369 AttributeList *AttrList = ParseAttributes(&Loc);
2370 ParmDecl.AddAttributes(AttrList, Loc);
2371 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002372
Chris Lattner9f7564b2008-04-06 06:57:35 +00002373 // Remember this parsed parameter in ParamInfo.
2374 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2375
Douglas Gregor605de8d2008-12-16 21:30:33 +00002376 // DefArgToks is used when the parsing of default arguments needs
2377 // to be delayed.
2378 CachedTokens *DefArgToks = 0;
2379
Chris Lattner9f7564b2008-04-06 06:57:35 +00002380 // If no parameter was specified, verify that *something* was specified,
2381 // otherwise we have a missing type and identifier.
Chris Lattner9e785f52009-02-27 18:38:20 +00002382 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2383 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002384 // Completely missing, emit error.
2385 Diag(DSStart, diag::err_missing_param);
2386 } else {
2387 // Otherwise, we have something. Add it and let semantic analysis try
2388 // to grok it and add the result to the ParamInfo we are building.
2389
2390 // Inform the actions module about the parameter declarator, so it gets
2391 // added to the current scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002392 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002393
2394 // Parse the default argument, if any. We parse the default
2395 // arguments in all dialects; the semantic analysis in
2396 // ActOnParamDefaultArgument will reject the default argument in
2397 // C.
2398 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002399 SourceLocation EqualLoc = Tok.getLocation();
2400
Chris Lattner3e254fb2008-04-08 04:40:51 +00002401 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002402 if (D.getContext() == Declarator::MemberContext) {
2403 // If we're inside a class definition, cache the tokens
2404 // corresponding to the default argument. We'll actually parse
2405 // them when we see the end of the class definition.
2406 // FIXME: Templates will require something similar.
2407 // FIXME: Can we use a smart pointer for Toks?
2408 DefArgToks = new CachedTokens;
2409
2410 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2411 tok::semi, false)) {
2412 delete DefArgToks;
2413 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002414 Actions.ActOnParamDefaultArgumentError(Param);
2415 } else
2416 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002417 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002418 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002419 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002420
2421 OwningExprResult DefArgResult(ParseAssignmentExpression());
2422 if (DefArgResult.isInvalid()) {
2423 Actions.ActOnParamDefaultArgumentError(Param);
2424 SkipUntil(tok::comma, tok::r_paren, true, true);
2425 } else {
2426 // Inform the actions module about the default argument
2427 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00002428 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +00002429 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002430 }
2431 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002432
2433 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002434 ParmDecl.getIdentifierLoc(), Param,
2435 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002436 }
2437
2438 // If the next token is a comma, consume it and keep reading arguments.
2439 if (Tok.isNot(tok::comma)) break;
2440
2441 // Consume the comma.
2442 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002443 }
2444
Chris Lattner9f7564b2008-04-06 06:57:35 +00002445 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002446 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002447
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002448 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002449 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002450
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002451 DeclSpec DS;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002452 bool hasExceptionSpec = false;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002453 SourceLocation ThrowLoc;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002454 bool hasAnyExceptionSpec = false;
Sebastian Redlaaacda92009-05-29 18:02:33 +00002455 llvm::SmallVector<TypeTy*, 2> Exceptions;
2456 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002457 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002458 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002459 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002460 if (!DS.getSourceRange().getEnd().isInvalid())
2461 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002462
2463 // Parse exception-specification[opt].
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002464 if (Tok.is(tok::kw_throw)) {
2465 hasExceptionSpec = true;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002466 ThrowLoc = Tok.getLocation();
Sebastian Redlaaacda92009-05-29 18:02:33 +00002467 ParseExceptionSpecification(Loc, Exceptions, ExceptionRanges,
2468 hasAnyExceptionSpec);
2469 assert(Exceptions.size() == ExceptionRanges.size() &&
2470 "Produced different number of exception types and ranges.");
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002471 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002472 }
2473
Chris Lattner4b009652007-07-25 00:24:17 +00002474 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002475 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002476 EllipsisLoc,
Jay Foad9e6bef42009-05-21 09:52:38 +00002477 ParamInfo.data(), ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002478 DS.getTypeQualifiers(),
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002479 hasExceptionSpec, ThrowLoc,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002480 hasAnyExceptionSpec,
Sebastian Redlaaacda92009-05-29 18:02:33 +00002481 Exceptions.data(),
2482 ExceptionRanges.data(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002483 Exceptions.size(), LParenLoc, D),
Sebastian Redl0c986032009-02-09 18:23:29 +00002484 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002485}
2486
Chris Lattner35d9c912008-04-06 06:34:08 +00002487/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2488/// we found a K&R-style identifier list instead of a type argument list. The
2489/// current token is known to be the first identifier in the list.
2490///
2491/// identifier-list: [C99 6.7.5]
2492/// identifier
2493/// identifier-list ',' identifier
2494///
2495void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2496 Declarator &D) {
2497 // Build up an array of information about the parsed arguments.
2498 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2499 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2500
2501 // If there was no identifier specified for the declarator, either we are in
2502 // an abstract-declarator, or we are in a parameter declarator which was found
2503 // to be abstract. In abstract-declarators, identifier lists are not valid:
2504 // diagnose this.
2505 if (!D.getIdentifier())
2506 Diag(Tok, diag::ext_ident_list_in_param);
2507
2508 // Tok is known to be the first identifier in the list. Remember this
2509 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002510 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002511 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner5261d0c2009-03-28 19:18:32 +00002512 Tok.getLocation(),
2513 DeclPtrTy()));
Chris Lattner35d9c912008-04-06 06:34:08 +00002514
Chris Lattner113a56b2008-04-06 06:39:19 +00002515 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002516
2517 while (Tok.is(tok::comma)) {
2518 // Eat the comma.
2519 ConsumeToken();
2520
Chris Lattner113a56b2008-04-06 06:39:19 +00002521 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002522 if (Tok.isNot(tok::identifier)) {
2523 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002524 SkipUntil(tok::r_paren);
2525 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002526 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002527
Chris Lattner35d9c912008-04-06 06:34:08 +00002528 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002529
2530 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002531 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002532 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002533
2534 // Verify that the argument identifier has not already been mentioned.
2535 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002536 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002537 } else {
2538 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002539 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002540 Tok.getLocation(),
2541 DeclPtrTy()));
Chris Lattner113a56b2008-04-06 06:39:19 +00002542 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002543
2544 // Eat the identifier.
2545 ConsumeToken();
2546 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002547
2548 // If we have the closing ')', eat it and we're done.
2549 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2550
Chris Lattner113a56b2008-04-06 06:39:19 +00002551 // Remember that we parsed a function type, and remember the attributes. This
2552 // function type is always a K&R style function type, which is not varargs and
2553 // has no prototype.
2554 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002555 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002556 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002557 /*TypeQuals*/0,
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002558 /*exception*/false,
2559 SourceLocation(), false, 0, 0, 0,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002560 LParenLoc, D),
Sebastian Redl0c986032009-02-09 18:23:29 +00002561 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002562}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002563
Chris Lattner4b009652007-07-25 00:24:17 +00002564/// [C90] direct-declarator '[' constant-expression[opt] ']'
2565/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2566/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2567/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2568/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2569void Parser::ParseBracketDeclarator(Declarator &D) {
2570 SourceLocation StartLoc = ConsumeBracket();
2571
Chris Lattner1525c3a2008-12-18 07:27:21 +00002572 // C array syntax has many features, but by-far the most common is [] and [4].
2573 // This code does a fast path to handle some of the most obvious cases.
2574 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002575 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002576 // Remember that we parsed the empty array type.
2577 OwningExprResult NumElements(Actions);
Sebastian Redl0c986032009-02-09 18:23:29 +00002578 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2579 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002580 return;
2581 } else if (Tok.getKind() == tok::numeric_constant &&
2582 GetLookAheadToken(1).is(tok::r_square)) {
2583 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002584 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002585 ConsumeToken();
2586
Sebastian Redl0c986032009-02-09 18:23:29 +00002587 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002588
2589 // If there was an error parsing the assignment-expression, recover.
2590 if (ExprRes.isInvalid())
2591 ExprRes.release(); // Deallocate expr, just use [].
2592
2593 // Remember that we parsed a array type, and remember its features.
2594 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redl0c986032009-02-09 18:23:29 +00002595 ExprRes.release(), StartLoc),
2596 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002597 return;
2598 }
2599
Chris Lattner4b009652007-07-25 00:24:17 +00002600 // If valid, this location is the position where we read the 'static' keyword.
2601 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002602 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002603 StaticLoc = ConsumeToken();
2604
2605 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002606 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002607 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002608 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002609
2610 // If we haven't already read 'static', check to see if there is one after the
2611 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002612 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002613 StaticLoc = ConsumeToken();
2614
2615 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2616 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002617 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002618
2619 // Handle the case where we have '[*]' as the array size. However, a leading
2620 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2621 // the the token after the star is a ']'. Since stars in arrays are
2622 // infrequent, use of lookahead is not costly here.
2623 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002624 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002625
Chris Lattner306d4df2008-12-18 06:50:14 +00002626 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002627 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002628 StaticLoc = SourceLocation(); // Drop the static.
2629 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002630 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002631 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002632 // Note, in C89, this production uses the constant-expr production instead
2633 // of assignment-expr. The only difference is that assignment-expr allows
2634 // things like '=' and '*='. Sema rejects these in C89 mode because they
2635 // are not i-c-e's, so we don't need to distinguish between the two here.
2636
Chris Lattner4b009652007-07-25 00:24:17 +00002637 // Parse the assignment-expression now.
2638 NumElements = ParseAssignmentExpression();
2639 }
2640
2641 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002642 if (NumElements.isInvalid()) {
Chris Lattnerf3ce8572009-04-24 22:30:50 +00002643 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00002644 // If the expression was invalid, skip it.
2645 SkipUntil(tok::r_square);
2646 return;
2647 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002648
2649 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2650
Chris Lattner1525c3a2008-12-18 07:27:21 +00002651 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002652 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2653 StaticLoc.isValid(), isStar,
Sebastian Redl0c986032009-02-09 18:23:29 +00002654 NumElements.release(), StartLoc),
2655 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002656}
2657
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002658/// [GNU] typeof-specifier:
2659/// typeof ( expressions )
2660/// typeof ( type-name )
2661/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002662///
2663void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002664 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002665 Token OpTok = Tok;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002666 SourceLocation StartLoc = ConsumeToken();
2667
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002668 bool isCastExpr;
2669 TypeTy *CastTy;
2670 SourceRange CastRange;
2671 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
2672 isCastExpr,
2673 CastTy,
2674 CastRange);
2675
2676 if (CastRange.getEnd().isInvalid())
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002677 // FIXME: Not accurate, the range gets one token more than it should.
2678 DS.SetRangeEnd(Tok.getLocation());
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002679 else
2680 DS.SetRangeEnd(CastRange.getEnd());
2681
2682 if (isCastExpr) {
2683 if (!CastTy) {
2684 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002685 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002686 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002687
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002688 const char *PrevSpec = 0;
2689 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2690 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2691 CastTy))
2692 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2693 return;
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002694 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002695
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002696 // If we get here, the operand to the typeof was an expresion.
2697 if (Operand.isInvalid()) {
2698 DS.SetTypeSpecError();
Steve Naroff14bbce82007-08-02 02:53:48 +00002699 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002700 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002701
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002702 const char *PrevSpec = 0;
2703 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2704 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
2705 Operand.release()))
2706 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002707}