blob: 8b81c312d2e015e9c8e0f3104d93b849620f0b3d [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner545f39e2009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattnera7549902007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerdaa5c002008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Sebastian Redl6008ac32008-11-25 22:21:31 +000018#include "AstGuard.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "llvm/ADT/SmallSet.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
26/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Douglas Gregor6c0f4062009-02-18 17:45:20 +000031Action::TypeResult Parser::ParseTypeName() {
Chris Lattner4b009652007-07-25 00:24:17 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
34 ParseSpecifierQualifierList(DS);
35
36 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
39
Chris Lattner34c61332009-04-25 08:06:05 +000040 if (DeclaratorInfo.isInvalidType())
Douglas Gregor6c0f4062009-02-18 17:45:20 +000041 return true;
42
43 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Chris Lattner4b009652007-07-25 00:24:17 +000044}
45
46/// ParseAttributes - Parse a non-empty attributes list.
47///
48/// [GNU] attributes:
49/// attribute
50/// attributes attribute
51///
52/// [GNU] attribute:
53/// '__attribute__' '(' '(' attribute-list ')' ')'
54///
55/// [GNU] attribute-list:
56/// attrib
57/// attribute_list ',' attrib
58///
59/// [GNU] attrib:
60/// empty
61/// attrib-name
62/// attrib-name '(' identifier ')'
63/// attrib-name '(' identifier ',' nonempty-expr-list ')'
64/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
65///
66/// [GNU] attrib-name:
67/// identifier
68/// typespec
69/// typequal
70/// storageclass
71///
72/// FIXME: The GCC grammar/code for this construct implies we need two
73/// token lookahead. Comment from gcc: "If they start with an identifier
74/// which is followed by a comma or close parenthesis, then the arguments
75/// start with that identifier; otherwise they are an expression list."
76///
77/// At the moment, I am not doing 2 token lookahead. I am also unaware of
78/// any attributes that don't work (based on my limited testing). Most
79/// attributes are very simple in practice. Until we find a bug, I don't see
80/// a pressing need to implement the 2 token lookahead.
81
Sebastian Redl0c986032009-02-09 18:23:29 +000082AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner34a01ad2007-10-09 17:33:22 +000083 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Chris Lattner4b009652007-07-25 00:24:17 +000084
85 AttributeList *CurrAttr = 0;
86
Chris Lattner34a01ad2007-10-09 17:33:22 +000087 while (Tok.is(tok::kw___attribute)) {
Chris Lattner4b009652007-07-25 00:24:17 +000088 ConsumeToken();
89 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
90 "attribute")) {
91 SkipUntil(tok::r_paren, true); // skip until ) or ;
92 return CurrAttr;
93 }
94 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
95 SkipUntil(tok::r_paren, true); // skip until ) or ;
96 return CurrAttr;
97 }
98 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner34a01ad2007-10-09 17:33:22 +000099 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
100 Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000101
Chris Lattner34a01ad2007-10-09 17:33:22 +0000102 if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000103 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
104 ConsumeToken();
105 continue;
106 }
107 // we have an identifier or declaration specifier (const, int, etc.)
108 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
109 SourceLocation AttrNameLoc = ConsumeToken();
110
111 // check if we have a "paramterized" attribute
Chris Lattner34a01ad2007-10-09 17:33:22 +0000112 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000113 ConsumeParen(); // ignore the left paren loc for now
114
Chris Lattner34a01ad2007-10-09 17:33:22 +0000115 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000116 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
117 SourceLocation ParmLoc = ConsumeToken();
118
Chris Lattner34a01ad2007-10-09 17:33:22 +0000119 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000120 // __attribute__(( mode(byte) ))
121 ConsumeParen(); // ignore the right paren loc for now
122 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
123 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000124 } else if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000125 ConsumeToken();
126 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000127 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000128 bool ArgExprsOk = true;
129
130 // now parse the non-empty comma separated list of expressions
131 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000132 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000133 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000134 ArgExprsOk = false;
135 SkipUntil(tok::r_paren);
136 break;
137 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000138 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000139 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000140 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000141 break;
142 ConsumeToken(); // Eat the comma, move to the next argument
143 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000144 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000145 ConsumeParen(); // ignore the right paren loc for now
146 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redl6008ac32008-11-25 22:21:31 +0000147 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Chris Lattner4b009652007-07-25 00:24:17 +0000148 }
149 }
150 } else { // not an identifier
151 // parse a possibly empty comma separated list of expressions
Chris Lattner34a01ad2007-10-09 17:33:22 +0000152 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000153 // __attribute__(( nonnull() ))
154 ConsumeParen(); // ignore the right paren loc for now
155 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
156 0, SourceLocation(), 0, 0, CurrAttr);
157 } else {
158 // __attribute__(( aligned(16) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000159 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000160 bool ArgExprsOk = true;
161
162 // now parse the list of expressions
163 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000164 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000165 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000166 ArgExprsOk = false;
167 SkipUntil(tok::r_paren);
168 break;
169 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000170 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000171 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000172 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000173 break;
174 ConsumeToken(); // Eat the comma, move to the next argument
175 }
176 // Match the ')'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000177 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000178 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl6008ac32008-11-25 22:21:31 +0000179 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
180 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Chris Lattner4b009652007-07-25 00:24:17 +0000181 CurrAttr);
182 }
183 }
184 }
185 } else {
186 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
187 0, SourceLocation(), 0, 0, CurrAttr);
188 }
189 }
190 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Chris Lattner4b009652007-07-25 00:24:17 +0000191 SkipUntil(tok::r_paren, false);
Sebastian Redl0c986032009-02-09 18:23:29 +0000192 SourceLocation Loc = Tok.getLocation();;
193 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
194 SkipUntil(tok::r_paren, false);
195 }
196 if (EndLoc)
197 *EndLoc = Loc;
Chris Lattner4b009652007-07-25 00:24:17 +0000198 }
199 return CurrAttr;
200}
201
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000202/// FuzzyParseMicrosoftDeclSpec. When -fms-extensions is enabled, this
203/// routine is called to skip/ignore tokens that comprise the MS declspec.
204void Parser::FuzzyParseMicrosoftDeclSpec() {
205 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
206 ConsumeToken();
207 if (Tok.is(tok::l_paren)) {
208 unsigned short savedParenCount = ParenCount;
209 do {
210 ConsumeAnyToken();
211 } while (ParenCount > savedParenCount && Tok.isNot(tok::eof));
212 }
213 return;
214}
215
Chris Lattner4b009652007-07-25 00:24:17 +0000216/// ParseDeclaration - Parse a full 'declaration', which consists of
217/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000218/// 'Context' should be a Declarator::TheContext value. This returns the
219/// location of the semicolon in DeclEnd.
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000220///
221/// declaration: [C99 6.7]
222/// block-declaration ->
223/// simple-declaration
224/// others [FIXME]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000225/// [C++] template-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000226/// [C++] namespace-definition
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000227/// [C++] using-directive
228/// [C++] using-declaration [TODO]
Sebastian Redla8cecf62009-03-24 22:27:57 +0000229/// [C++0x] static_assert-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000230/// others... [FIXME]
231///
Chris Lattner9802a0a2009-04-02 04:16:50 +0000232Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
233 SourceLocation &DeclEnd) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000234 DeclPtrTy SingleDecl;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000235 switch (Tok.getKind()) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000236 case tok::kw_export:
237 case tok::kw_template:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000238 SingleDecl = ParseTemplateDeclarationOrSpecialization(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000239 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000240 case tok::kw_namespace:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000241 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000242 break;
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000243 case tok::kw_using:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000244 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000245 break;
Anders Carlssonab041982009-03-11 16:27:10 +0000246 case tok::kw_static_assert:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000247 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000248 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000249 default:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000250 return ParseSimpleDeclaration(Context, DeclEnd);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000251 }
Chris Lattnera17991f2009-03-29 16:50:03 +0000252
253 // This routine returns a DeclGroup, if the thing we parsed only contains a
254 // single decl, convert it now.
255 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000256}
257
258/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
259/// declaration-specifiers init-declarator-list[opt] ';'
260///[C90/C++]init-declarator-list ';' [TODO]
261/// [OMP] threadprivate-directive [TODO]
Chris Lattnerf8016042009-03-29 17:27:48 +0000262///
263/// If RequireSemi is false, this does not check for a ';' at the end of the
264/// declaration.
265Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Chris Lattner9802a0a2009-04-02 04:16:50 +0000266 SourceLocation &DeclEnd,
Chris Lattnerf8016042009-03-29 17:27:48 +0000267 bool RequireSemi) {
Chris Lattner4b009652007-07-25 00:24:17 +0000268 // Parse the common declaration-specifiers piece.
269 DeclSpec DS;
270 ParseDeclarationSpecifiers(DS);
271
272 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
273 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000274 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000275 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000276 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
277 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000278 }
279
280 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
281 ParseDeclarator(DeclaratorInfo);
282
Chris Lattner2c41d482009-03-29 17:18:04 +0000283 DeclGroupPtrTy DG =
284 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattnerf8016042009-03-29 17:27:48 +0000285
Chris Lattner9802a0a2009-04-02 04:16:50 +0000286 DeclEnd = Tok.getLocation();
287
Chris Lattnerf8016042009-03-29 17:27:48 +0000288 // If the client wants to check what comes after the declaration, just return
289 // immediately without checking anything!
290 if (!RequireSemi) return DG;
Chris Lattner2c41d482009-03-29 17:18:04 +0000291
292 if (Tok.is(tok::semi)) {
293 ConsumeToken();
Chris Lattner2c41d482009-03-29 17:18:04 +0000294 return DG;
295 }
296
Chris Lattner2c41d482009-03-29 17:18:04 +0000297 Diag(Tok, diag::err_expected_semi_declation);
298 // Skip to end of block or statement
299 SkipUntil(tok::r_brace, true, true);
300 if (Tok.is(tok::semi))
301 ConsumeToken();
302 return DG;
Chris Lattner4b009652007-07-25 00:24:17 +0000303}
304
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000305
Chris Lattner4b009652007-07-25 00:24:17 +0000306/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
307/// parsing 'declaration-specifiers declarator'. This method is split out this
308/// way to handle the ambiguity between top-level function-definitions and
309/// declarations.
310///
Chris Lattner4b009652007-07-25 00:24:17 +0000311/// init-declarator-list: [C99 6.7]
312/// init-declarator
313/// init-declarator-list ',' init-declarator
314/// init-declarator: [C99 6.7]
315/// declarator
316/// declarator '=' initializer
317/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
318/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000319/// [C++] declarator initializer[opt]
320///
321/// [C++] initializer:
322/// [C++] '=' initializer-clause
323/// [C++] '(' expression-list ')'
Sebastian Redla8cecf62009-03-24 22:27:57 +0000324/// [C++0x] '=' 'default' [TODO]
325/// [C++0x] '=' 'delete'
326///
327/// According to the standard grammar, =default and =delete are function
328/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattner4b009652007-07-25 00:24:17 +0000329///
Chris Lattnera17991f2009-03-29 16:50:03 +0000330Parser::DeclGroupPtrTy Parser::
Chris Lattner4b009652007-07-25 00:24:17 +0000331ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000332 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
333 // that we parse together here.
334 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Chris Lattner4b009652007-07-25 00:24:17 +0000335
336 // At this point, we know that it is not a function definition. Parse the
337 // rest of the init-declarator-list.
338 while (1) {
339 // If a simple-asm-expr is present, parse it.
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000340 if (Tok.is(tok::kw_asm)) {
Sebastian Redl0c986032009-02-09 18:23:29 +0000341 SourceLocation Loc;
342 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000343 if (AsmLabel.isInvalid()) {
Chris Lattner2c41d482009-03-29 17:18:04 +0000344 SkipUntil(tok::semi, true, true);
Chris Lattnera17991f2009-03-29 16:50:03 +0000345 return DeclGroupPtrTy();
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000346 }
Sebastian Redl0c986032009-02-09 18:23:29 +0000347
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000348 D.setAsmLabel(AsmLabel.release());
Sebastian Redl0c986032009-02-09 18:23:29 +0000349 D.SetRangeEnd(Loc);
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000350 }
Chris Lattner4b009652007-07-25 00:24:17 +0000351
352 // If attributes are present, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +0000353 if (Tok.is(tok::kw___attribute)) {
354 SourceLocation Loc;
355 AttributeList *AttrList = ParseAttributes(&Loc);
356 D.AddAttributes(AttrList, Loc);
357 }
Steve Naroff6a0e2092007-09-12 14:07:44 +0000358
359 // Inform the current actions module that we just parsed this declarator.
Chris Lattnera17991f2009-03-29 16:50:03 +0000360 DeclPtrTy ThisDecl = Actions.ActOnDeclarator(CurScope, D);
361 DeclsInGroup.push_back(ThisDecl);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000362
Chris Lattner4b009652007-07-25 00:24:17 +0000363 // Parse declarator '=' initializer.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000364 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000365 ConsumeToken();
Sebastian Redla8cecf62009-03-24 22:27:57 +0000366 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
367 SourceLocation DelLoc = ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000368 Actions.SetDeclDeleted(ThisDecl, DelLoc);
Sebastian Redla8cecf62009-03-24 22:27:57 +0000369 } else {
370 OwningExprResult Init(ParseInitializer());
371 if (Init.isInvalid()) {
Chris Lattner2c41d482009-03-29 17:18:04 +0000372 SkipUntil(tok::semi, true, true);
Chris Lattnera17991f2009-03-29 16:50:03 +0000373 return DeclGroupPtrTy();
Sebastian Redla8cecf62009-03-24 22:27:57 +0000374 }
Chris Lattnera17991f2009-03-29 16:50:03 +0000375 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Chris Lattner4b009652007-07-25 00:24:17 +0000376 }
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000377 } else if (Tok.is(tok::l_paren)) {
378 // Parse C++ direct initializer: '(' expression-list ')'
379 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl6008ac32008-11-25 22:21:31 +0000380 ExprVector Exprs(Actions);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000381 CommaLocsTy CommaLocs;
382
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000383 if (ParseExpressionList(Exprs, CommaLocs)) {
384 SkipUntil(tok::r_paren);
Chris Lattner1b8e26c2009-04-12 22:23:27 +0000385 } else {
386 // Match the ')'.
387 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000388
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000389 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
390 "Unexpected number of commas!");
Chris Lattnera17991f2009-03-29 16:50:03 +0000391 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +0000392 move_arg(Exprs),
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000393 &CommaLocs[0], RParenLoc);
394 }
Douglas Gregor81c29152008-10-29 00:13:59 +0000395 } else {
Chris Lattnera17991f2009-03-29 16:50:03 +0000396 Actions.ActOnUninitializedDecl(ThisDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000397 }
398
Chris Lattner4b009652007-07-25 00:24:17 +0000399 // If we don't have a comma, it is either the end of the list (a ';') or an
400 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000401 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000402 break;
403
404 // Consume the comma.
405 ConsumeToken();
406
407 // Parse the next declarator.
408 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000409
410 // Accept attributes in an init-declarator. In the first declarator in a
411 // declaration, these would be part of the declspec. In subsequent
412 // declarators, they become part of the declarator itself, so that they
413 // don't apply to declarators after *this* one. Examples:
414 // short __attribute__((common)) var; -> declspec
415 // short var __attribute__((common)); -> declarator
416 // short x, __attribute__((common)) var; -> declarator
Sebastian Redl0c986032009-02-09 18:23:29 +0000417 if (Tok.is(tok::kw___attribute)) {
418 SourceLocation Loc;
419 AttributeList *AttrList = ParseAttributes(&Loc);
420 D.AddAttributes(AttrList, Loc);
421 }
Chris Lattner926cf542008-10-20 04:57:38 +0000422
Chris Lattner4b009652007-07-25 00:24:17 +0000423 ParseDeclarator(D);
424 }
425
Chris Lattner2c41d482009-03-29 17:18:04 +0000426 return Actions.FinalizeDeclaratorGroup(CurScope, &DeclsInGroup[0],
427 DeclsInGroup.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000428}
429
430/// ParseSpecifierQualifierList
431/// specifier-qualifier-list:
432/// type-specifier specifier-qualifier-list[opt]
433/// type-qualifier specifier-qualifier-list[opt]
434/// [GNU] attributes specifier-qualifier-list[opt]
435///
436void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
437 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
438 /// parse declaration-specifiers and complain about extra stuff.
439 ParseDeclarationSpecifiers(DS);
440
441 // Validate declspec for type-name.
442 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera52aec42009-04-14 21:16:09 +0000443 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
444 !DS.getAttributes())
Chris Lattner4b009652007-07-25 00:24:17 +0000445 Diag(Tok, diag::err_typename_requires_specqual);
446
447 // Issue diagnostic and remove storage class if present.
448 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
449 if (DS.getStorageClassSpecLoc().isValid())
450 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
451 else
452 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
453 DS.ClearStorageClassSpecs();
454 }
455
456 // Issue diagnostic and remove function specfier if present.
457 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000458 if (DS.isInlineSpecified())
459 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
460 if (DS.isVirtualSpecified())
461 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
462 if (DS.isExplicitSpecified())
463 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000464 DS.ClearFunctionSpecs();
465 }
466}
467
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000468/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
469/// specified token is valid after the identifier in a declarator which
470/// immediately follows the declspec. For example, these things are valid:
471///
472/// int x [ 4]; // direct-declarator
473/// int x ( int y); // direct-declarator
474/// int(int x ) // direct-declarator
475/// int x ; // simple-declaration
476/// int x = 17; // init-declarator-list
477/// int x , y; // init-declarator-list
478/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera52aec42009-04-14 21:16:09 +0000479/// int x : 4; // struct-declarator
Chris Lattnerca6cc362009-04-12 22:29:43 +0000480/// int x { 5}; // C++'0x unified initializers
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000481///
482/// This is not, because 'x' does not immediately follow the declspec (though
483/// ')' happens to be valid anyway).
484/// int (x)
485///
486static bool isValidAfterIdentifierInDeclarator(const Token &T) {
487 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
488 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera52aec42009-04-14 21:16:09 +0000489 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000490}
491
Chris Lattner82353c62009-04-14 21:34:55 +0000492
493/// ParseImplicitInt - This method is called when we have an non-typename
494/// identifier in a declspec (which normally terminates the decl spec) when
495/// the declspec has no type specifier. In this case, the declspec is either
496/// malformed or is "implicit int" (in K&R and C89).
497///
498/// This method handles diagnosing this prettily and returns false if the
499/// declspec is done being processed. If it recovers and thinks there may be
500/// other pieces of declspec after it, it returns true.
501///
Chris Lattner52cd7622009-04-14 22:17:06 +0000502bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Chris Lattner82353c62009-04-14 21:34:55 +0000503 TemplateParameterLists *TemplateParams,
504 AccessSpecifier AS) {
Chris Lattner52cd7622009-04-14 22:17:06 +0000505 assert(Tok.is(tok::identifier) && "should have identifier");
506
Chris Lattner82353c62009-04-14 21:34:55 +0000507 SourceLocation Loc = Tok.getLocation();
508 // If we see an identifier that is not a type name, we normally would
509 // parse it as the identifer being declared. However, when a typename
510 // is typo'd or the definition is not included, this will incorrectly
511 // parse the typename as the identifier name and fall over misparsing
512 // later parts of the diagnostic.
513 //
514 // As such, we try to do some look-ahead in cases where this would
515 // otherwise be an "implicit-int" case to see if this is invalid. For
516 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
517 // an identifier with implicit int, we'd get a parse error because the
518 // next token is obviously invalid for a type. Parse these as a case
519 // with an invalid type specifier.
520 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
521
522 // Since we know that this either implicit int (which is rare) or an
523 // error, we'd do lookahead to try to do better recovery.
524 if (isValidAfterIdentifierInDeclarator(NextToken())) {
525 // If this token is valid for implicit int, e.g. "static x = 4", then
526 // we just avoid eating the identifier, so it will be parsed as the
527 // identifier in the declarator.
528 return false;
529 }
530
531 // Otherwise, if we don't consume this token, we are going to emit an
532 // error anyway. Try to recover from various common problems. Check
533 // to see if this was a reference to a tag name without a tag specified.
534 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattner52cd7622009-04-14 22:17:06 +0000535 //
536 // C++ doesn't need this, and isTagName doesn't take SS.
537 if (SS == 0) {
538 const char *TagName = 0;
539 tok::TokenKind TagKind = tok::unknown;
Chris Lattner82353c62009-04-14 21:34:55 +0000540
Chris Lattner82353c62009-04-14 21:34:55 +0000541 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
542 default: break;
543 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
544 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
545 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
546 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
547 }
Chris Lattner82353c62009-04-14 21:34:55 +0000548
Chris Lattner52cd7622009-04-14 22:17:06 +0000549 if (TagName) {
550 Diag(Loc, diag::err_use_of_tag_name_without_tag)
551 << Tok.getIdentifierInfo() << TagName
552 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
553
554 // Parse this as a tag as if the missing tag were present.
555 if (TagKind == tok::kw_enum)
556 ParseEnumSpecifier(Loc, DS, AS);
557 else
558 ParseClassSpecifier(TagKind, Loc, DS, TemplateParams, AS);
559 return true;
560 }
Chris Lattner82353c62009-04-14 21:34:55 +0000561 }
562
563 // Since this is almost certainly an invalid type name, emit a
564 // diagnostic that says it, eat the token, and mark the declspec as
565 // invalid.
Chris Lattner52cd7622009-04-14 22:17:06 +0000566 SourceRange R;
567 if (SS) R = SS->getRange();
568
569 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
Chris Lattner82353c62009-04-14 21:34:55 +0000570 const char *PrevSpec;
571 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec);
572 DS.SetRangeEnd(Tok.getLocation());
573 ConsumeToken();
574
575 // TODO: Could inject an invalid typedef decl in an enclosing scope to
576 // avoid rippling error messages on subsequent uses of the same type,
577 // could be useful if #include was forgotten.
578 return false;
579}
580
Chris Lattner4b009652007-07-25 00:24:17 +0000581/// ParseDeclarationSpecifiers
582/// declaration-specifiers: [C99 6.7]
583/// storage-class-specifier declaration-specifiers[opt]
584/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000585/// [C99] function-specifier declaration-specifiers[opt]
586/// [GNU] attributes declaration-specifiers[opt]
587///
588/// storage-class-specifier: [C99 6.7.1]
589/// 'typedef'
590/// 'extern'
591/// 'static'
592/// 'auto'
593/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000594/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000595/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000596/// function-specifier: [C99 6.7.4]
597/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000598/// [C++] 'virtual'
599/// [C++] 'explicit'
Chris Lattner4b009652007-07-25 00:24:17 +0000600///
Douglas Gregor52473432008-12-24 02:52:09 +0000601void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000602 TemplateParameterLists *TemplateParams,
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000603 AccessSpecifier AS) {
Chris Lattnera4ff4272008-03-13 06:29:04 +0000604 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000605 while (1) {
606 int isInvalid = false;
607 const char *PrevSpec = 0;
608 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000609
Chris Lattner4b009652007-07-25 00:24:17 +0000610 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000611 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000612 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000613 // If this is not a declaration specifier token, we're done reading decl
614 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000615 DS.Finish(Diags, PP);
Chris Lattner4b009652007-07-25 00:24:17 +0000616 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000617
618 case tok::coloncolon: // ::foo::bar
619 // Annotate C++ scope specifiers. If we get one, loop.
620 if (TryAnnotateCXXScopeToken())
621 continue;
622 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000623
624 case tok::annot_cxxscope: {
625 if (DS.hasTypeSpecifier())
626 goto DoneWithDeclSpec;
627
628 // We are looking for a qualified typename.
Douglas Gregor80b95c52009-03-25 15:40:00 +0000629 Token Next = NextToken();
630 if (Next.is(tok::annot_template_id) &&
631 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregoraabb8502009-03-31 00:43:58 +0000632 ->Kind == TNK_Type_template) {
Douglas Gregor80b95c52009-03-25 15:40:00 +0000633 // We have a qualified template-id, e.g., N::A<int>
634 CXXScopeSpec SS;
635 ParseOptionalCXXScopeSpecifier(SS);
636 assert(Tok.is(tok::annot_template_id) &&
637 "ParseOptionalCXXScopeSpecifier not working");
638 AnnotateTemplateIdTokenAsType(&SS);
639 continue;
640 }
641
642 if (Next.isNot(tok::identifier))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000643 goto DoneWithDeclSpec;
644
645 CXXScopeSpec SS;
Douglas Gregor041e9292009-03-26 23:56:24 +0000646 SS.setScopeRep(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000647 SS.setRange(Tok.getAnnotationRange());
648
649 // If the next token is the name of the class type that the C++ scope
650 // denotes, followed by a '(', then this is a constructor declaration.
651 // We're done with the decl-specifiers.
Chris Lattner52cd7622009-04-14 22:17:06 +0000652 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000653 CurScope, &SS) &&
654 GetLookAheadToken(2).is(tok::l_paren))
655 goto DoneWithDeclSpec;
656
Douglas Gregor1075a162009-02-04 17:00:24 +0000657 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
658 Next.getLocation(), CurScope, &SS);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000659
Chris Lattner52cd7622009-04-14 22:17:06 +0000660 // If the referenced identifier is not a type, then this declspec is
661 // erroneous: We already checked about that it has no type specifier, and
662 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
663 // typename.
664 if (TypeRep == 0) {
665 ConsumeToken(); // Eat the scope spec so the identifier is current.
666 if (ParseImplicitInt(DS, &SS, TemplateParams, AS)) continue;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000667 goto DoneWithDeclSpec;
Chris Lattner52cd7622009-04-14 22:17:06 +0000668 }
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000669
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000670 ConsumeToken(); // The C++ scope.
671
Douglas Gregora60c62e2009-02-09 15:09:02 +0000672 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000673 TypeRep);
674 if (isInvalid)
675 break;
676
677 DS.SetRangeEnd(Tok.getLocation());
678 ConsumeToken(); // The typename.
679
680 continue;
681 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000682
683 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +0000684 if (Tok.getAnnotationValue())
685 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
686 Tok.getAnnotationValue());
687 else
688 DS.SetTypeSpecError();
Chris Lattnerc297b722009-01-21 19:48:37 +0000689 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
690 ConsumeToken(); // The typename
691
692 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
693 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
694 // Objective-C interface. If we don't have Objective-C or a '<', this is
695 // just a normal reference to a typedef name.
696 if (!Tok.is(tok::less) || !getLang().ObjC1)
697 continue;
698
699 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000700 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnerc297b722009-01-21 19:48:37 +0000701 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
702 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
703
704 DS.SetRangeEnd(EndProtoLoc);
705 continue;
706 }
707
Chris Lattnerfda18db2008-07-26 01:18:38 +0000708 // typedef-name
709 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000710 // In C++, check to see if this is a scope specifier like foo::bar::, if
711 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000712 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
713 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000714
Chris Lattnerfda18db2008-07-26 01:18:38 +0000715 // This identifier can only be a typedef name if we haven't already seen
716 // a type-specifier. Without this check we misparse:
717 // typedef int X; struct Y { short X; }; as 'short int'.
718 if (DS.hasTypeSpecifier())
719 goto DoneWithDeclSpec;
720
721 // It has to be available as a typedef too!
Douglas Gregor1075a162009-02-04 17:00:24 +0000722 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
723 Tok.getLocation(), CurScope);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000724
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000725 // If this is not a typedef name, don't parse it as part of the declspec,
726 // it must be an implicit int or an error.
727 if (TypeRep == 0) {
Chris Lattner52cd7622009-04-14 22:17:06 +0000728 if (ParseImplicitInt(DS, 0, TemplateParams, AS)) continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000729 goto DoneWithDeclSpec;
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000730 }
Douglas Gregor8e458f42009-02-09 18:46:07 +0000731
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000732 // C++: If the identifier is actually the name of the class type
733 // being defined and the next token is a '(', then this is a
734 // constructor declaration. We're done with the decl-specifiers
735 // and will treat this token as an identifier.
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000736 if (getLang().CPlusPlus && CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000737 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
738 NextToken().getKind() == tok::l_paren)
739 goto DoneWithDeclSpec;
740
Douglas Gregora60c62e2009-02-09 15:09:02 +0000741 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerfda18db2008-07-26 01:18:38 +0000742 TypeRep);
743 if (isInvalid)
744 break;
745
746 DS.SetRangeEnd(Tok.getLocation());
747 ConsumeToken(); // The identifier
748
749 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
750 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
751 // Objective-C interface. If we don't have Objective-C or a '<', this is
752 // just a normal reference to a typedef name.
753 if (!Tok.is(tok::less) || !getLang().ObjC1)
754 continue;
755
756 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000757 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000758 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000759 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000760
761 DS.SetRangeEnd(EndProtoLoc);
762
Steve Narofff7683302008-09-22 10:28:57 +0000763 // Need to support trailing type qualifiers (e.g. "id<p> const").
764 // If a type specifier follows, it will be diagnosed elsewhere.
765 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000766 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000767
768 // type-name
769 case tok::annot_template_id: {
770 TemplateIdAnnotation *TemplateId
771 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregoraabb8502009-03-31 00:43:58 +0000772 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor0c281a82009-02-25 19:37:18 +0000773 // This template-id does not refer to a type name, so we're
774 // done with the type-specifiers.
775 goto DoneWithDeclSpec;
776 }
777
778 // Turn the template-id annotation token into a type annotation
779 // token, then try again to parse it as a type-specifier.
Douglas Gregord7cb0372009-04-01 21:51:26 +0000780 AnnotateTemplateIdTokenAsType();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000781 continue;
782 }
783
Chris Lattner4b009652007-07-25 00:24:17 +0000784 // GNU attributes support.
785 case tok::kw___attribute:
786 DS.AddAttributes(ParseAttributes());
787 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000788
789 // Microsoft declspec support.
790 case tok::kw___declspec:
791 if (!PP.getLangOptions().Microsoft)
792 goto DoneWithDeclSpec;
793 FuzzyParseMicrosoftDeclSpec();
794 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000795
Steve Naroffedd04d52008-12-25 14:16:32 +0000796 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000797 case tok::kw___forceinline:
798 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000799 case tok::kw___cdecl:
800 case tok::kw___stdcall:
801 case tok::kw___fastcall:
802 if (!PP.getLangOptions().Microsoft)
803 goto DoneWithDeclSpec;
804 // Just ignore it.
805 break;
806
Chris Lattner4b009652007-07-25 00:24:17 +0000807 // storage-class-specifier
808 case tok::kw_typedef:
809 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
810 break;
811 case tok::kw_extern:
812 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000813 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000814 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
815 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000816 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000817 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
818 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000819 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000820 case tok::kw_static:
821 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000822 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000823 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
824 break;
825 case tok::kw_auto:
826 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
827 break;
828 case tok::kw_register:
829 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
830 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000831 case tok::kw_mutable:
832 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
833 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000834 case tok::kw___thread:
835 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
836 break;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000837
Chris Lattner4b009652007-07-25 00:24:17 +0000838 // function-specifier
839 case tok::kw_inline:
840 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
841 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000842 case tok::kw_virtual:
843 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
844 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000845 case tok::kw_explicit:
846 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
847 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000848
849 // type-specifier
850 case tok::kw_short:
851 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
852 break;
853 case tok::kw_long:
854 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
855 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
856 else
857 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
858 break;
859 case tok::kw_signed:
860 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
861 break;
862 case tok::kw_unsigned:
863 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
864 break;
865 case tok::kw__Complex:
866 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
867 break;
868 case tok::kw__Imaginary:
869 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
870 break;
871 case tok::kw_void:
872 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
873 break;
874 case tok::kw_char:
875 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
876 break;
877 case tok::kw_int:
878 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
879 break;
880 case tok::kw_float:
881 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
882 break;
883 case tok::kw_double:
884 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
885 break;
886 case tok::kw_wchar_t:
887 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
888 break;
889 case tok::kw_bool:
890 case tok::kw__Bool:
891 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
892 break;
893 case tok::kw__Decimal32:
894 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
895 break;
896 case tok::kw__Decimal64:
897 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
898 break;
899 case tok::kw__Decimal128:
900 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
901 break;
902
903 // class-specifier:
904 case tok::kw_class:
905 case tok::kw_struct:
Chris Lattner197b4342009-04-12 21:49:30 +0000906 case tok::kw_union: {
907 tok::TokenKind Kind = Tok.getKind();
908 ConsumeToken();
909 ParseClassSpecifier(Kind, Loc, DS, TemplateParams, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000910 continue;
Chris Lattner197b4342009-04-12 21:49:30 +0000911 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000912
913 // enum-specifier:
914 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +0000915 ConsumeToken();
916 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000917 continue;
918
919 // cv-qualifier:
920 case tok::kw_const:
921 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
922 break;
923 case tok::kw_volatile:
924 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
925 getLang())*2;
926 break;
927 case tok::kw_restrict:
928 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
929 getLang())*2;
930 break;
931
Douglas Gregord3022602009-03-27 23:10:48 +0000932 // C++ typename-specifier:
933 case tok::kw_typename:
934 if (TryAnnotateTypeOrScopeToken())
935 continue;
936 break;
937
Chris Lattnerc297b722009-01-21 19:48:37 +0000938 // GNU typeof support.
939 case tok::kw_typeof:
940 ParseTypeofSpecifier(DS);
941 continue;
942
Steve Naroff5f0466b2008-06-05 00:02:44 +0000943 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000944 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000945 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
946 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000947 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000948 goto DoneWithDeclSpec;
949
950 {
951 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000952 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000953 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000954 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000955 DS.SetRangeEnd(EndProtoLoc);
956
Chris Lattnerf006a222008-11-18 07:48:38 +0000957 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattnerb980c732009-04-03 18:38:42 +0000958 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattnerf006a222008-11-18 07:48:38 +0000959 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000960 // Need to support trailing type qualifiers (e.g. "id<p> const").
961 // If a type specifier follows, it will be diagnosed elsewhere.
962 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000963 }
Chris Lattner4b009652007-07-25 00:24:17 +0000964 }
965 // If the specifier combination wasn't legal, issue a diagnostic.
966 if (isInvalid) {
967 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000968 // Pick between error or extwarn.
969 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
970 : diag::ext_duplicate_declspec;
971 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000972 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000973 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000974 ConsumeToken();
975 }
976}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000977
Chris Lattnerd706dc82009-01-06 06:59:53 +0000978/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000979/// primarily follow the C++ grammar with additions for C99 and GNU,
980/// which together subsume the C grammar. Note that the C++
981/// type-specifier also includes the C type-qualifier (for const,
982/// volatile, and C99 restrict). Returns true if a type-specifier was
983/// found (and parsed), false otherwise.
984///
985/// type-specifier: [C++ 7.1.5]
986/// simple-type-specifier
987/// class-specifier
988/// enum-specifier
989/// elaborated-type-specifier [TODO]
990/// cv-qualifier
991///
992/// cv-qualifier: [C++ 7.1.5.1]
993/// 'const'
994/// 'volatile'
995/// [C99] 'restrict'
996///
997/// simple-type-specifier: [ C++ 7.1.5.2]
998/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
999/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1000/// 'char'
1001/// 'wchar_t'
1002/// 'bool'
1003/// 'short'
1004/// 'int'
1005/// 'long'
1006/// 'signed'
1007/// 'unsigned'
1008/// 'float'
1009/// 'double'
1010/// 'void'
1011/// [C99] '_Bool'
1012/// [C99] '_Complex'
1013/// [C99] '_Imaginary' // Removed in TC2?
1014/// [GNU] '_Decimal32'
1015/// [GNU] '_Decimal64'
1016/// [GNU] '_Decimal128'
1017/// [GNU] typeof-specifier
1018/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1019/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerd706dc82009-01-06 06:59:53 +00001020bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
1021 const char *&PrevSpec,
1022 TemplateParameterLists *TemplateParams){
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001023 SourceLocation Loc = Tok.getLocation();
1024
1025 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +00001026 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001027 case tok::kw_typename: // typename foo::bar
Chris Lattnerb75fde62009-01-04 23:41:41 +00001028 // Annotate typenames and C++ scope specifiers. If we get one, just
1029 // recurse to handle whatever we get.
1030 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +00001031 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +00001032 // Otherwise, not a type specifier.
1033 return false;
1034 case tok::coloncolon: // ::foo::bar
1035 if (NextToken().is(tok::kw_new) || // ::new
1036 NextToken().is(tok::kw_delete)) // ::delete
1037 return false;
1038
1039 // Annotate typenames and C++ scope specifiers. If we get one, just
1040 // recurse to handle whatever we get.
1041 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +00001042 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +00001043 // Otherwise, not a type specifier.
1044 return false;
1045
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001046 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +00001047 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +00001048 if (Tok.getAnnotationValue())
1049 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
1050 Tok.getAnnotationValue());
1051 else
1052 DS.SetTypeSpecError();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001053 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1054 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001055
1056 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1057 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1058 // Objective-C interface. If we don't have Objective-C or a '<', this is
1059 // just a normal reference to a typedef name.
1060 if (!Tok.is(tok::less) || !getLang().ObjC1)
1061 return true;
1062
1063 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001064 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001065 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
1066 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
1067
1068 DS.SetRangeEnd(EndProtoLoc);
1069 return true;
1070 }
1071
1072 case tok::kw_short:
1073 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
1074 break;
1075 case tok::kw_long:
1076 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
1077 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
1078 else
1079 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
1080 break;
1081 case tok::kw_signed:
1082 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
1083 break;
1084 case tok::kw_unsigned:
1085 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
1086 break;
1087 case tok::kw__Complex:
1088 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
1089 break;
1090 case tok::kw__Imaginary:
1091 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
1092 break;
1093 case tok::kw_void:
1094 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
1095 break;
1096 case tok::kw_char:
1097 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
1098 break;
1099 case tok::kw_int:
1100 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
1101 break;
1102 case tok::kw_float:
1103 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
1104 break;
1105 case tok::kw_double:
1106 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
1107 break;
1108 case tok::kw_wchar_t:
1109 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
1110 break;
1111 case tok::kw_bool:
1112 case tok::kw__Bool:
1113 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
1114 break;
1115 case tok::kw__Decimal32:
1116 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
1117 break;
1118 case tok::kw__Decimal64:
1119 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
1120 break;
1121 case tok::kw__Decimal128:
1122 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
1123 break;
1124
1125 // class-specifier:
1126 case tok::kw_class:
1127 case tok::kw_struct:
Chris Lattner197b4342009-04-12 21:49:30 +00001128 case tok::kw_union: {
1129 tok::TokenKind Kind = Tok.getKind();
1130 ConsumeToken();
1131 ParseClassSpecifier(Kind, Loc, DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001132 return true;
Chris Lattner197b4342009-04-12 21:49:30 +00001133 }
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001134
1135 // enum-specifier:
1136 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +00001137 ConsumeToken();
1138 ParseEnumSpecifier(Loc, DS);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001139 return true;
1140
1141 // cv-qualifier:
1142 case tok::kw_const:
1143 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1144 getLang())*2;
1145 break;
1146 case tok::kw_volatile:
1147 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1148 getLang())*2;
1149 break;
1150 case tok::kw_restrict:
1151 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1152 getLang())*2;
1153 break;
1154
1155 // GNU typeof support.
1156 case tok::kw_typeof:
1157 ParseTypeofSpecifier(DS);
1158 return true;
1159
Steve Naroffedd04d52008-12-25 14:16:32 +00001160 case tok::kw___cdecl:
1161 case tok::kw___stdcall:
1162 case tok::kw___fastcall:
Chris Lattner5bb837e2009-01-21 19:19:26 +00001163 if (!PP.getLangOptions().Microsoft) return false;
1164 ConsumeToken();
1165 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +00001166
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001167 default:
1168 // Not a type-specifier; do nothing.
1169 return false;
1170 }
1171
1172 // If the specifier combination wasn't legal, issue a diagnostic.
1173 if (isInvalid) {
1174 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001175 // Pick between error or extwarn.
1176 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1177 : diag::ext_duplicate_declspec;
1178 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001179 }
1180 DS.SetRangeEnd(Tok.getLocation());
1181 ConsumeToken(); // whatever we parsed above.
1182 return true;
1183}
Chris Lattner4b009652007-07-25 00:24:17 +00001184
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001185/// ParseStructDeclaration - Parse a struct declaration without the terminating
1186/// semicolon.
1187///
Chris Lattner4b009652007-07-25 00:24:17 +00001188/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001189/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001190/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001191/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +00001192/// struct-declarator-list:
1193/// struct-declarator
1194/// struct-declarator-list ',' struct-declarator
1195/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1196/// struct-declarator:
1197/// declarator
1198/// [GNU] declarator attributes[opt]
1199/// declarator[opt] ':' constant-expression
1200/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1201///
Chris Lattner3dd8d392008-04-10 06:46:29 +00001202void Parser::
1203ParseStructDeclaration(DeclSpec &DS,
1204 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001205 if (Tok.is(tok::kw___extension__)) {
1206 // __extension__ silences extension warnings in the subexpression.
1207 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001208 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001209 return ParseStructDeclaration(DS, Fields);
1210 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001211
1212 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001213 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001214 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001215
Douglas Gregorb748fc52009-01-12 22:49:06 +00001216 // If there are no declarators, this is a free-standing declaration
1217 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001218 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001219 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001220 return;
1221 }
1222
1223 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001224 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001225 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001226 FieldDeclarator &DeclaratorInfo = Fields.back();
1227
Steve Naroffa9adf112007-08-20 22:28:22 +00001228 /// struct-declarator: declarator
1229 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001230 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001231 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001232
Chris Lattner34a01ad2007-10-09 17:33:22 +00001233 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001234 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001235 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001236 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001237 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001238 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001239 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001240 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001241
Steve Naroffa9adf112007-08-20 22:28:22 +00001242 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001243 if (Tok.is(tok::kw___attribute)) {
1244 SourceLocation Loc;
1245 AttributeList *AttrList = ParseAttributes(&Loc);
1246 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1247 }
1248
Steve Naroffa9adf112007-08-20 22:28:22 +00001249 // If we don't have a comma, it is either the end of the list (a ';')
1250 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001251 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001252 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001253
Steve Naroffa9adf112007-08-20 22:28:22 +00001254 // Consume the comma.
1255 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001256
Steve Naroffa9adf112007-08-20 22:28:22 +00001257 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001258 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001259
Steve Naroffa9adf112007-08-20 22:28:22 +00001260 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001261 if (Tok.is(tok::kw___attribute)) {
1262 SourceLocation Loc;
1263 AttributeList *AttrList = ParseAttributes(&Loc);
1264 Fields.back().D.AddAttributes(AttrList, Loc);
1265 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001266 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001267}
1268
1269/// ParseStructUnionBody
1270/// struct-contents:
1271/// struct-declaration-list
1272/// [EXT] empty
1273/// [GNU] "struct-declaration-list" without terminatoring ';'
1274/// struct-declaration-list:
1275/// struct-declaration
1276/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001277/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001278///
Chris Lattner4b009652007-07-25 00:24:17 +00001279void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001280 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001281 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1282 PP.getSourceManager(),
1283 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001284
Chris Lattner4b009652007-07-25 00:24:17 +00001285 SourceLocation LBraceLoc = ConsumeBrace();
1286
Douglas Gregorcab994d2009-01-09 22:42:13 +00001287 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001288 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1289
Chris Lattner4b009652007-07-25 00:24:17 +00001290 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1291 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001292 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001293 Diag(Tok, diag::ext_empty_struct_union_enum)
1294 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001295
Chris Lattner5261d0c2009-03-28 19:18:32 +00001296 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001297 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1298
Chris Lattner4b009652007-07-25 00:24:17 +00001299 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001300 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001301 // Each iteration of this loop reads one struct-declaration.
1302
1303 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001304 if (Tok.is(tok::semi)) {
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001305 Diag(Tok, diag::ext_extra_struct_semi)
1306 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +00001307 ConsumeToken();
1308 continue;
1309 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001310
1311 // Parse all the comma separated declarators.
1312 DeclSpec DS;
1313 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001314 if (!Tok.is(tok::at)) {
1315 ParseStructDeclaration(DS, FieldDeclarators);
1316
1317 // Convert them all to fields.
1318 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1319 FieldDeclarator &FD = FieldDeclarators[i];
1320 // Install the declarator into the current TagDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001321 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1322 DS.getSourceRange().getBegin(),
1323 FD.D, FD.BitfieldSize);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001324 FieldDecls.push_back(Field);
1325 }
1326 } else { // Handle @defs
1327 ConsumeToken();
1328 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1329 Diag(Tok, diag::err_unexpected_at);
1330 SkipUntil(tok::semi, true, true);
1331 continue;
1332 }
1333 ConsumeToken();
1334 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1335 if (!Tok.is(tok::identifier)) {
1336 Diag(Tok, diag::err_expected_ident);
1337 SkipUntil(tok::semi, true, true);
1338 continue;
1339 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001340 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001341 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1342 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001343 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1344 ConsumeToken();
1345 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1346 }
Chris Lattner4b009652007-07-25 00:24:17 +00001347
Chris Lattner34a01ad2007-10-09 17:33:22 +00001348 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001349 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001350 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001351 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001352 break;
1353 } else {
1354 Diag(Tok, diag::err_expected_semi_decl_list);
1355 // Skip to end of block or statement
1356 SkipUntil(tok::r_brace, true, true);
1357 }
1358 }
1359
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001360 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001361
Chris Lattner4b009652007-07-25 00:24:17 +00001362 AttributeList *AttrList = 0;
1363 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001364 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001365 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001366
1367 Actions.ActOnFields(CurScope,
1368 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1369 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001370 AttrList);
1371 StructScope.Exit();
1372 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001373}
1374
1375
1376/// ParseEnumSpecifier
1377/// enum-specifier: [C99 6.7.2.2]
1378/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001379///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001380/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1381/// '}' attributes[opt]
1382/// 'enum' identifier
1383/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001384///
1385/// [C++] elaborated-type-specifier:
1386/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1387///
Chris Lattner197b4342009-04-12 21:49:30 +00001388void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1389 AccessSpecifier AS) {
Chris Lattner4b009652007-07-25 00:24:17 +00001390 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001391
1392 AttributeList *Attr = 0;
1393 // If attributes exist after tag, parse them.
1394 if (Tok.is(tok::kw___attribute))
1395 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001396
1397 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001398 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001399 if (Tok.isNot(tok::identifier)) {
1400 Diag(Tok, diag::err_expected_ident);
1401 if (Tok.isNot(tok::l_brace)) {
1402 // Has no name and is not a definition.
1403 // Skip the rest of this declarator, up until the comma or semicolon.
1404 SkipUntil(tok::comma, true);
1405 return;
1406 }
1407 }
1408 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001409
1410 // Must have either 'enum name' or 'enum {...}'.
1411 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1412 Diag(Tok, diag::err_expected_ident_lbrace);
1413
1414 // Skip the rest of this declarator, up until the comma or semicolon.
1415 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001416 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001417 }
1418
1419 // If an identifier is present, consume and remember it.
1420 IdentifierInfo *Name = 0;
1421 SourceLocation NameLoc;
1422 if (Tok.is(tok::identifier)) {
1423 Name = Tok.getIdentifierInfo();
1424 NameLoc = ConsumeToken();
1425 }
1426
1427 // There are three options here. If we have 'enum foo;', then this is a
1428 // forward declaration. If we have 'enum foo {...' then this is a
1429 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1430 //
1431 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1432 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1433 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1434 //
1435 Action::TagKind TK;
1436 if (Tok.is(tok::l_brace))
1437 TK = Action::TK_Definition;
1438 else if (Tok.is(tok::semi))
1439 TK = Action::TK_Declaration;
1440 else
1441 TK = Action::TK_Reference;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001442 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
1443 StartLoc, SS, Name, NameLoc, Attr, AS);
Chris Lattner4b009652007-07-25 00:24:17 +00001444
Chris Lattner34a01ad2007-10-09 17:33:22 +00001445 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001446 ParseEnumBody(StartLoc, TagDecl);
1447
1448 // TODO: semantic analysis on the declspec for enums.
1449 const char *PrevSpec = 0;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001450 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
1451 TagDecl.getAs<void>()))
Chris Lattnerf006a222008-11-18 07:48:38 +00001452 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001453}
1454
1455/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1456/// enumerator-list:
1457/// enumerator
1458/// enumerator-list ',' enumerator
1459/// enumerator:
1460/// enumeration-constant
1461/// enumeration-constant '=' constant-expression
1462/// enumeration-constant:
1463/// identifier
1464///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001465void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001466 // Enter the scope of the enum body and start the definition.
1467 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001468 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001469
Chris Lattner4b009652007-07-25 00:24:17 +00001470 SourceLocation LBraceLoc = ConsumeBrace();
1471
Chris Lattnerc9a92452007-08-27 17:24:30 +00001472 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001473 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001474 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001475
Chris Lattner5261d0c2009-03-28 19:18:32 +00001476 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattner4b009652007-07-25 00:24:17 +00001477
Chris Lattner5261d0c2009-03-28 19:18:32 +00001478 DeclPtrTy LastEnumConstDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001479
1480 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001481 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001482 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1483 SourceLocation IdentLoc = ConsumeToken();
1484
1485 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001486 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001487 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001488 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001489 AssignedVal = ParseConstantExpression();
1490 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001491 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001492 }
1493
1494 // Install the enumerator constant into EnumDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001495 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1496 LastEnumConstDecl,
1497 IdentLoc, Ident,
1498 EqualLoc,
1499 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001500 EnumConstantDecls.push_back(EnumConstDecl);
1501 LastEnumConstDecl = EnumConstDecl;
1502
Chris Lattner34a01ad2007-10-09 17:33:22 +00001503 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001504 break;
1505 SourceLocation CommaLoc = ConsumeToken();
1506
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001507 if (Tok.isNot(tok::identifier) &&
1508 !(getLang().C99 || getLang().CPlusPlus0x))
1509 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1510 << getLang().CPlusPlus
1511 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Chris Lattner4b009652007-07-25 00:24:17 +00001512 }
1513
1514 // Eat the }.
1515 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1516
Steve Naroff0acc9c92007-09-15 18:49:24 +00001517 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001518 EnumConstantDecls.size());
1519
Chris Lattner5261d0c2009-03-28 19:18:32 +00001520 Action::AttrTy *AttrList = 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001521 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001522 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001523 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001524
1525 EnumScope.Exit();
1526 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001527}
1528
1529/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001530/// start of a type-qualifier-list.
1531bool Parser::isTypeQualifier() const {
1532 switch (Tok.getKind()) {
1533 default: return false;
1534 // type-qualifier
1535 case tok::kw_const:
1536 case tok::kw_volatile:
1537 case tok::kw_restrict:
1538 return true;
1539 }
1540}
1541
1542/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001543/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001544bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001545 switch (Tok.getKind()) {
1546 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001547
1548 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001549 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001550 // Annotate typenames and C++ scope specifiers. If we get one, just
1551 // recurse to handle whatever we get.
1552 if (TryAnnotateTypeOrScopeToken())
1553 return isTypeSpecifierQualifier();
1554 // Otherwise, not a type specifier.
1555 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001556
Chris Lattnerb75fde62009-01-04 23:41:41 +00001557 case tok::coloncolon: // ::foo::bar
1558 if (NextToken().is(tok::kw_new) || // ::new
1559 NextToken().is(tok::kw_delete)) // ::delete
1560 return false;
1561
1562 // Annotate typenames and C++ scope specifiers. If we get one, just
1563 // recurse to handle whatever we get.
1564 if (TryAnnotateTypeOrScopeToken())
1565 return isTypeSpecifierQualifier();
1566 // Otherwise, not a type specifier.
1567 return false;
1568
Chris Lattner4b009652007-07-25 00:24:17 +00001569 // GNU attributes support.
1570 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001571 // GNU typeof support.
1572 case tok::kw_typeof:
1573
Chris Lattner4b009652007-07-25 00:24:17 +00001574 // type-specifiers
1575 case tok::kw_short:
1576 case tok::kw_long:
1577 case tok::kw_signed:
1578 case tok::kw_unsigned:
1579 case tok::kw__Complex:
1580 case tok::kw__Imaginary:
1581 case tok::kw_void:
1582 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001583 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001584 case tok::kw_int:
1585 case tok::kw_float:
1586 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001587 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001588 case tok::kw__Bool:
1589 case tok::kw__Decimal32:
1590 case tok::kw__Decimal64:
1591 case tok::kw__Decimal128:
1592
Chris Lattner2e78db32008-04-13 18:59:07 +00001593 // struct-or-union-specifier (C99) or class-specifier (C++)
1594 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001595 case tok::kw_struct:
1596 case tok::kw_union:
1597 // enum-specifier
1598 case tok::kw_enum:
1599
1600 // type-qualifier
1601 case tok::kw_const:
1602 case tok::kw_volatile:
1603 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001604
1605 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001606 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001607 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001608
1609 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1610 case tok::less:
1611 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001612
1613 case tok::kw___cdecl:
1614 case tok::kw___stdcall:
1615 case tok::kw___fastcall:
1616 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001617 }
1618}
1619
1620/// isDeclarationSpecifier() - Return true if the current token is part of a
1621/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001622bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001623 switch (Tok.getKind()) {
1624 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001625
1626 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-03-09 21:12:44 +00001627 // Unfortunate hack to support "Class.factoryMethod" notation.
1628 if (getLang().ObjC1 && NextToken().is(tok::period))
1629 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001630 // Fall through
Steve Naroff73ec9322009-03-09 21:12:44 +00001631
Douglas Gregord3022602009-03-27 23:10:48 +00001632 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001633 // Annotate typenames and C++ scope specifiers. If we get one, just
1634 // recurse to handle whatever we get.
1635 if (TryAnnotateTypeOrScopeToken())
1636 return isDeclarationSpecifier();
1637 // Otherwise, not a declaration specifier.
1638 return false;
1639 case tok::coloncolon: // ::foo::bar
1640 if (NextToken().is(tok::kw_new) || // ::new
1641 NextToken().is(tok::kw_delete)) // ::delete
1642 return false;
1643
1644 // Annotate typenames and C++ scope specifiers. If we get one, just
1645 // recurse to handle whatever we get.
1646 if (TryAnnotateTypeOrScopeToken())
1647 return isDeclarationSpecifier();
1648 // Otherwise, not a declaration specifier.
1649 return false;
1650
Chris Lattner4b009652007-07-25 00:24:17 +00001651 // storage-class-specifier
1652 case tok::kw_typedef:
1653 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001654 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001655 case tok::kw_static:
1656 case tok::kw_auto:
1657 case tok::kw_register:
1658 case tok::kw___thread:
1659
1660 // type-specifiers
1661 case tok::kw_short:
1662 case tok::kw_long:
1663 case tok::kw_signed:
1664 case tok::kw_unsigned:
1665 case tok::kw__Complex:
1666 case tok::kw__Imaginary:
1667 case tok::kw_void:
1668 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001669 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001670 case tok::kw_int:
1671 case tok::kw_float:
1672 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001673 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001674 case tok::kw__Bool:
1675 case tok::kw__Decimal32:
1676 case tok::kw__Decimal64:
1677 case tok::kw__Decimal128:
1678
Chris Lattner2e78db32008-04-13 18:59:07 +00001679 // struct-or-union-specifier (C99) or class-specifier (C++)
1680 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001681 case tok::kw_struct:
1682 case tok::kw_union:
1683 // enum-specifier
1684 case tok::kw_enum:
1685
1686 // type-qualifier
1687 case tok::kw_const:
1688 case tok::kw_volatile:
1689 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001690
Chris Lattner4b009652007-07-25 00:24:17 +00001691 // function-specifier
1692 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001693 case tok::kw_virtual:
1694 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001695
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001696 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001697 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001698
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001699 // GNU typeof support.
1700 case tok::kw_typeof:
1701
1702 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001703 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001704 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001705
1706 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1707 case tok::less:
1708 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001709
Steve Naroffab1a3632009-01-06 19:34:12 +00001710 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001711 case tok::kw___cdecl:
1712 case tok::kw___stdcall:
1713 case tok::kw___fastcall:
1714 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001715 }
1716}
1717
1718
1719/// ParseTypeQualifierListOpt
1720/// type-qualifier-list: [C99 6.7.5]
1721/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001722/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001723/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001724/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001725///
Chris Lattner460696f2008-12-18 07:02:59 +00001726void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001727 while (1) {
1728 int isInvalid = false;
1729 const char *PrevSpec = 0;
1730 SourceLocation Loc = Tok.getLocation();
1731
1732 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001733 case tok::kw_const:
1734 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1735 getLang())*2;
1736 break;
1737 case tok::kw_volatile:
1738 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1739 getLang())*2;
1740 break;
1741 case tok::kw_restrict:
1742 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1743 getLang())*2;
1744 break;
Steve Naroffad620402008-12-25 14:41:26 +00001745 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001746 case tok::kw___cdecl:
1747 case tok::kw___stdcall:
1748 case tok::kw___fastcall:
1749 if (!PP.getLangOptions().Microsoft)
1750 goto DoneWithTypeQuals;
1751 // Just ignore it.
1752 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001753 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001754 if (AttributesAllowed) {
1755 DS.AddAttributes(ParseAttributes());
1756 continue; // do *not* consume the next token!
1757 }
1758 // otherwise, FALL THROUGH!
1759 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001760 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001761 // If this is not a type-qualifier token, we're done reading type
1762 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001763 DS.Finish(Diags, PP);
Chris Lattner460696f2008-12-18 07:02:59 +00001764 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001765 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001766
Chris Lattner4b009652007-07-25 00:24:17 +00001767 // If the specifier combination wasn't legal, issue a diagnostic.
1768 if (isInvalid) {
1769 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001770 // Pick between error or extwarn.
1771 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1772 : diag::ext_duplicate_declspec;
1773 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001774 }
1775 ConsumeToken();
1776 }
1777}
1778
1779
1780/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1781///
1782void Parser::ParseDeclarator(Declarator &D) {
1783 /// This implements the 'declarator' production in the C grammar, then checks
1784 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001785 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001786}
1787
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001788/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1789/// is parsed by the function passed to it. Pass null, and the direct-declarator
1790/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001791/// ptr-operator production.
1792///
Sebastian Redl75555032009-01-24 21:16:55 +00001793/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1794/// [C] pointer[opt] direct-declarator
1795/// [C++] direct-declarator
1796/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001797///
1798/// pointer: [C99 6.7.5]
1799/// '*' type-qualifier-list[opt]
1800/// '*' type-qualifier-list[opt] pointer
1801///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001802/// ptr-operator:
1803/// '*' cv-qualifier-seq[opt]
1804/// '&'
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001805/// [C++0x] '&&'
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001806/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001807/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001808/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001809void Parser::ParseDeclaratorInternal(Declarator &D,
1810 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001811
Sebastian Redl75555032009-01-24 21:16:55 +00001812 // C++ member pointers start with a '::' or a nested-name.
1813 // Member pointers get special handling, since there's no place for the
1814 // scope spec in the generic path below.
Chris Lattner053dd2d2009-03-24 17:04:48 +00001815 if (getLang().CPlusPlus &&
1816 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1817 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl75555032009-01-24 21:16:55 +00001818 CXXScopeSpec SS;
1819 if (ParseOptionalCXXScopeSpecifier(SS)) {
1820 if(Tok.isNot(tok::star)) {
1821 // The scope spec really belongs to the direct-declarator.
1822 D.getCXXScopeSpec() = SS;
1823 if (DirectDeclParser)
1824 (this->*DirectDeclParser)(D);
1825 return;
1826 }
1827
1828 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001829 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00001830 DeclSpec DS;
1831 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001832 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001833
1834 // Recurse to parse whatever is left.
1835 ParseDeclaratorInternal(D, DirectDeclParser);
1836
1837 // Sema will have to catch (syntactically invalid) pointers into global
1838 // scope. It has to catch pointers into namespace scope anyway.
1839 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001840 Loc, DS.TakeAttributes()),
1841 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00001842 return;
1843 }
1844 }
1845
1846 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001847 // Not a pointer, C++ reference, or block.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001848 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner053dd2d2009-03-24 17:04:48 +00001849 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001850 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001851 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001852 if (DirectDeclParser)
1853 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001854 return;
1855 }
Sebastian Redl75555032009-01-24 21:16:55 +00001856
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001857 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1858 // '&&' -> rvalue reference
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001859 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redl0c986032009-02-09 18:23:29 +00001860 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001861
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001862 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner69f01932008-02-21 01:32:26 +00001863 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001864 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001865
Chris Lattner4b009652007-07-25 00:24:17 +00001866 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001867 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001868
Chris Lattner4b009652007-07-25 00:24:17 +00001869 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001870 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001871 if (Kind == tok::star)
1872 // Remember that we parsed a pointer type, and remember the type-quals.
1873 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001874 DS.TakeAttributes()),
1875 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00001876 else
1877 // Remember that we parsed a Block type, and remember the type-quals.
1878 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump7ff82e72009-04-21 00:51:43 +00001879 Loc, DS.TakeAttributes()),
Sebastian Redl0c986032009-02-09 18:23:29 +00001880 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001881 } else {
1882 // Is a reference
1883 DeclSpec DS;
1884
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001885 // Complain about rvalue references in C++03, but then go on and build
1886 // the declarator.
1887 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1888 Diag(Loc, diag::err_rvalue_reference);
1889
Chris Lattner4b009652007-07-25 00:24:17 +00001890 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1891 // cv-qualifiers are introduced through the use of a typedef or of a
1892 // template type argument, in which case the cv-qualifiers are ignored.
1893 //
1894 // [GNU] Retricted references are allowed.
1895 // [GNU] Attributes on references are allowed.
1896 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001897 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00001898
1899 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1900 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1901 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001902 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001903 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1904 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001905 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001906 }
1907
1908 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001909 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001910
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001911 if (D.getNumTypeObjects() > 0) {
1912 // C++ [dcl.ref]p4: There shall be no references to references.
1913 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1914 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001915 if (const IdentifierInfo *II = D.getIdentifier())
1916 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1917 << II;
1918 else
1919 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1920 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001921
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001922 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001923 // can go ahead and build the (technically ill-formed)
1924 // declarator: reference collapsing will take care of it.
1925 }
1926 }
1927
Chris Lattner4b009652007-07-25 00:24:17 +00001928 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001929 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001930 DS.TakeAttributes(),
1931 Kind == tok::amp),
Sebastian Redl0c986032009-02-09 18:23:29 +00001932 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001933 }
1934}
1935
1936/// ParseDirectDeclarator
1937/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001938/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001939/// '(' declarator ')'
1940/// [GNU] '(' attributes declarator ')'
1941/// [C90] direct-declarator '[' constant-expression[opt] ']'
1942/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1943/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1944/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1945/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1946/// direct-declarator '(' parameter-type-list ')'
1947/// direct-declarator '(' identifier-list[opt] ')'
1948/// [GNU] direct-declarator '(' parameter-forward-declarations
1949/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001950/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1951/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001952/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001953///
1954/// declarator-id: [C++ 8]
1955/// id-expression
1956/// '::'[opt] nested-name-specifier[opt] type-name
1957///
1958/// id-expression: [C++ 5.1]
1959/// unqualified-id
1960/// qualified-id [TODO]
1961///
1962/// unqualified-id: [C++ 5.1]
1963/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001964/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001965/// conversion-function-id [TODO]
1966/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00001967/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001968///
Chris Lattner4b009652007-07-25 00:24:17 +00001969void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001970 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001971
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001972 if (getLang().CPlusPlus) {
1973 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00001974 // ParseDeclaratorInternal might already have parsed the scope.
1975 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1976 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001977 if (afterCXXScope) {
1978 // Change the declaration context for name lookup, until this function
1979 // is exited (and the declarator has been parsed).
1980 DeclScopeObj.EnterDeclaratorScope();
1981 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001982
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001983 if (Tok.is(tok::identifier)) {
1984 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Anders Carlssone19759d2009-04-30 22:41:11 +00001985
1986 // If this identifier is the name of the current class, it's a
1987 // constructor name.
1988 if (!D.getDeclSpec().hasTypeSpecifier() &&
1989 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
1990 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
1991 Tok.getLocation(), CurScope),
1992 Tok.getLocation());
1993 // This is a normal identifier.
1994 } else
1995 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001996 ConsumeToken();
1997 goto PastIdentifier;
Douglas Gregor0c281a82009-02-25 19:37:18 +00001998 } else if (Tok.is(tok::annot_template_id)) {
1999 TemplateIdAnnotation *TemplateId
2000 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
2001
2002 // FIXME: Could this template-id name a constructor?
2003
2004 // FIXME: This is an egregious hack, where we silently ignore
2005 // the specialization (which should be a function template
2006 // specialization name) and use the name instead. This hack
2007 // will go away when we have support for function
2008 // specializations.
2009 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
2010 TemplateId->Destroy();
2011 ConsumeToken();
2012 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00002013 } else if (Tok.is(tok::kw_operator)) {
2014 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00002015 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00002016
Douglas Gregor853dd392008-12-26 15:00:45 +00002017 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00002018 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
2019 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00002020 } else {
2021 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00002022 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
2023 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
2024 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00002025 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00002026 }
Douglas Gregor853dd392008-12-26 15:00:45 +00002027 }
2028 goto PastIdentifier;
2029 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002030 // This should be a C++ destructor.
2031 SourceLocation TildeLoc = ConsumeToken();
2032 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002033 // FIXME: Inaccurate.
2034 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00002035 SourceLocation EndLoc;
Douglas Gregord7cb0372009-04-01 21:51:26 +00002036 TypeResult Type = ParseClassName(EndLoc);
2037 if (Type.isInvalid())
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002038 D.SetIdentifier(0, TildeLoc);
Douglas Gregord7cb0372009-04-01 21:51:26 +00002039 else
2040 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002041 } else {
2042 Diag(Tok, diag::err_expected_class_name);
2043 D.SetIdentifier(0, TildeLoc);
2044 }
2045 goto PastIdentifier;
2046 }
2047
2048 // If we reached this point, token is not identifier and not '~'.
2049
2050 if (afterCXXScope) {
2051 Diag(Tok, diag::err_expected_unqualified_id);
2052 D.SetIdentifier(0, Tok.getLocation());
2053 D.setInvalidType(true);
2054 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00002055 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00002056 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002057 }
2058
2059 // If we reached this point, we are either in C/ObjC or the token didn't
2060 // satisfy any of the C++-specific checks.
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002061 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2062 assert(!getLang().CPlusPlus &&
2063 "There's a C++-specific check for tok::identifier above");
2064 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2065 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2066 ConsumeToken();
2067 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002068 // direct-declarator: '(' declarator ')'
2069 // direct-declarator: '(' attributes declarator ')'
2070 // Example: 'char (*X)' or 'int (*XX)(void)'
2071 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002072 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002073 // This could be something simple like "int" (in which case the declarator
2074 // portion is empty), if an abstract-declarator is allowed.
2075 D.SetIdentifier(0, Tok.getLocation());
2076 } else {
Douglas Gregorf03265d2009-03-06 23:28:18 +00002077 if (D.getContext() == Declarator::MemberContext)
2078 Diag(Tok, diag::err_expected_member_name_or_semi)
2079 << D.getDeclSpec().getSourceRange();
2080 else if (getLang().CPlusPlus)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002081 Diag(Tok, diag::err_expected_unqualified_id);
2082 else
Chris Lattnerf006a222008-11-18 07:48:38 +00002083 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00002084 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00002085 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00002086 }
2087
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002088 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00002089 assert(D.isPastIdentifier() &&
2090 "Haven't past the location of the identifier yet?");
2091
2092 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002093 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002094 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2095 // In such a case, check if we actually have a function declarator; if it
2096 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00002097 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2098 // When not in file scope, warn for ambiguous function declarators, just
2099 // in case the author intended it as a variable definition.
2100 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2101 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2102 break;
2103 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00002104 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00002105 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002106 ParseBracketDeclarator(D);
2107 } else {
2108 break;
2109 }
2110 }
2111}
2112
Chris Lattnera0d056d2008-04-06 05:45:57 +00002113/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2114/// only called before the identifier, so these are most likely just grouping
2115/// parens for precedence. If we find that these are actually function
2116/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2117///
2118/// direct-declarator:
2119/// '(' declarator ')'
2120/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00002121/// direct-declarator '(' parameter-type-list ')'
2122/// direct-declarator '(' identifier-list[opt] ')'
2123/// [GNU] direct-declarator '(' parameter-forward-declarations
2124/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00002125///
2126void Parser::ParseParenDeclarator(Declarator &D) {
2127 SourceLocation StartLoc = ConsumeParen();
2128 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
2129
Chris Lattner1f185292008-10-20 02:05:46 +00002130 // Eat any attributes before we look at whether this is a grouping or function
2131 // declarator paren. If this is a grouping paren, the attribute applies to
2132 // the type being built up, for example:
2133 // int (__attribute__(()) *x)(long y)
2134 // If this ends up not being a grouping paren, the attribute applies to the
2135 // first argument, for example:
2136 // int (__attribute__(()) int x)
2137 // In either case, we need to eat any attributes to be able to determine what
2138 // sort of paren this is.
2139 //
2140 AttributeList *AttrList = 0;
2141 bool RequiresArg = false;
2142 if (Tok.is(tok::kw___attribute)) {
2143 AttrList = ParseAttributes();
2144
2145 // We require that the argument list (if this is a non-grouping paren) be
2146 // present even if the attribute list was empty.
2147 RequiresArg = true;
2148 }
Steve Naroffedd04d52008-12-25 14:16:32 +00002149 // Eat any Microsoft extensions.
Douglas Gregore51b7c82009-01-10 00:48:18 +00002150 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2151 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroffedd04d52008-12-25 14:16:32 +00002152 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00002153
Chris Lattnera0d056d2008-04-06 05:45:57 +00002154 // If we haven't past the identifier yet (or where the identifier would be
2155 // stored, if this is an abstract declarator), then this is probably just
2156 // grouping parens. However, if this could be an abstract-declarator, then
2157 // this could also be the start of function arguments (consider 'void()').
2158 bool isGrouping;
2159
2160 if (!D.mayOmitIdentifier()) {
2161 // If this can't be an abstract-declarator, this *must* be a grouping
2162 // paren, because we haven't seen the identifier yet.
2163 isGrouping = true;
2164 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002165 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00002166 isDeclarationSpecifier()) { // 'int(int)' is a function.
2167 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2168 // considered to be a type, not a K&R identifier-list.
2169 isGrouping = false;
2170 } else {
2171 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2172 isGrouping = true;
2173 }
2174
2175 // If this is a grouping paren, handle:
2176 // direct-declarator: '(' declarator ')'
2177 // direct-declarator: '(' attributes declarator ')'
2178 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002179 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002180 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00002181 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00002182 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002183
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002184 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002185 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00002186 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002187
2188 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00002189 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002190 return;
2191 }
2192
2193 // Okay, if this wasn't a grouping paren, it must be the start of a function
2194 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00002195 // identifier (and remember where it would have been), then call into
2196 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00002197 D.SetIdentifier(0, Tok.getLocation());
2198
Chris Lattner1f185292008-10-20 02:05:46 +00002199 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002200}
2201
2202/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2203/// declarator D up to a paren, which indicates that we are parsing function
2204/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00002205///
Chris Lattner1f185292008-10-20 02:05:46 +00002206/// If AttrList is non-null, then the caller parsed those arguments immediately
2207/// after the open paren - they should be considered to be the first argument of
2208/// a parameter. If RequiresArg is true, then the first argument of the
2209/// function is required to be present and required to not be an identifier
2210/// list.
2211///
Chris Lattner4b009652007-07-25 00:24:17 +00002212/// This method also handles this portion of the grammar:
2213/// parameter-type-list: [C99 6.7.5]
2214/// parameter-list
2215/// parameter-list ',' '...'
2216///
2217/// parameter-list: [C99 6.7.5]
2218/// parameter-declaration
2219/// parameter-list ',' parameter-declaration
2220///
2221/// parameter-declaration: [C99 6.7.5]
2222/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00002223/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002224/// [GNU] declaration-specifiers declarator attributes
Sebastian Redla8cecf62009-03-24 22:27:57 +00002225/// declaration-specifiers abstract-declarator[opt]
2226/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002227/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002228/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2229///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002230/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redla8cecf62009-03-24 22:27:57 +00002231/// and "exception-specification[opt]".
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002232///
Chris Lattner1f185292008-10-20 02:05:46 +00002233void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2234 AttributeList *AttrList,
2235 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002236 // lparen is already consumed!
2237 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002238
Chris Lattner1f185292008-10-20 02:05:46 +00002239 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002240 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002241 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002242 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002243 delete AttrList;
2244 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002245
Sebastian Redl0c986032009-02-09 18:23:29 +00002246 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002247
2248 // cv-qualifier-seq[opt].
2249 DeclSpec DS;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002250 bool hasExceptionSpec = false;
2251 bool hasAnyExceptionSpec = false;
2252 // FIXME: Does an empty vector ever allocate? Exception specifications are
2253 // extremely rare, so we want something like a SmallVector<TypeTy*, 0>. :-)
2254 std::vector<TypeTy*> Exceptions;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002255 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002256 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002257 if (!DS.getSourceRange().getEnd().isInvalid())
2258 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002259
2260 // Parse exception-specification[opt].
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002261 if (Tok.is(tok::kw_throw)) {
2262 hasExceptionSpec = true;
2263 ParseExceptionSpecification(Loc, Exceptions, hasAnyExceptionSpec);
2264 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002265 }
2266
Chris Lattner9f7564b2008-04-06 06:57:35 +00002267 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002268 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002269 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002270 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002271 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002272 /*arglist*/ 0, 0,
2273 DS.getTypeQualifiers(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002274 hasExceptionSpec,
2275 hasAnyExceptionSpec,
2276 Exceptions.empty() ? 0 :
2277 &Exceptions[0],
2278 Exceptions.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002279 LParenLoc, D),
2280 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002281 return;
Chris Lattner1f185292008-10-20 02:05:46 +00002282 }
2283
2284 // Alternatively, this parameter list may be an identifier list form for a
2285 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002286 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002287 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002288 // K&R identifier lists can't have typedefs as identifiers, per
2289 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002290 if (RequiresArg) {
2291 Diag(Tok, diag::err_argument_required_after_attribute);
2292 delete AttrList;
2293 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002294 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2295 // normal declarators, not for abstract-declarators.
2296 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002297 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002298 }
2299
2300 // Finally, a normal, non-empty parameter type list.
2301
2302 // Build up an array of information about the parsed arguments.
2303 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002304
2305 // Enter function-declaration scope, limiting any declarators to the
2306 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002307 ParseScope PrototypeScope(this,
2308 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002309
2310 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002311 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002312 while (1) {
2313 if (Tok.is(tok::ellipsis)) {
2314 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002315 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002316 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002317 }
2318
Chris Lattner9f7564b2008-04-06 06:57:35 +00002319 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002320
Chris Lattner9f7564b2008-04-06 06:57:35 +00002321 // Parse the declaration-specifiers.
2322 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002323
2324 // If the caller parsed attributes for the first argument, add them now.
2325 if (AttrList) {
2326 DS.AddAttributes(AttrList);
2327 AttrList = 0; // Only apply the attributes to the first parameter.
2328 }
Chris Lattner9e785f52009-02-27 18:38:20 +00002329 ParseDeclarationSpecifiers(DS);
2330
Chris Lattner9f7564b2008-04-06 06:57:35 +00002331 // Parse the declarator. This is "PrototypeContext", because we must
2332 // accept either 'declarator' or 'abstract-declarator' here.
2333 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2334 ParseDeclarator(ParmDecl);
2335
2336 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002337 if (Tok.is(tok::kw___attribute)) {
2338 SourceLocation Loc;
2339 AttributeList *AttrList = ParseAttributes(&Loc);
2340 ParmDecl.AddAttributes(AttrList, Loc);
2341 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002342
Chris Lattner9f7564b2008-04-06 06:57:35 +00002343 // Remember this parsed parameter in ParamInfo.
2344 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2345
Douglas Gregor605de8d2008-12-16 21:30:33 +00002346 // DefArgToks is used when the parsing of default arguments needs
2347 // to be delayed.
2348 CachedTokens *DefArgToks = 0;
2349
Chris Lattner9f7564b2008-04-06 06:57:35 +00002350 // If no parameter was specified, verify that *something* was specified,
2351 // otherwise we have a missing type and identifier.
Chris Lattner9e785f52009-02-27 18:38:20 +00002352 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2353 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002354 // Completely missing, emit error.
2355 Diag(DSStart, diag::err_missing_param);
2356 } else {
2357 // Otherwise, we have something. Add it and let semantic analysis try
2358 // to grok it and add the result to the ParamInfo we are building.
2359
2360 // Inform the actions module about the parameter declarator, so it gets
2361 // added to the current scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002362 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002363
2364 // Parse the default argument, if any. We parse the default
2365 // arguments in all dialects; the semantic analysis in
2366 // ActOnParamDefaultArgument will reject the default argument in
2367 // C.
2368 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002369 SourceLocation EqualLoc = Tok.getLocation();
2370
Chris Lattner3e254fb2008-04-08 04:40:51 +00002371 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002372 if (D.getContext() == Declarator::MemberContext) {
2373 // If we're inside a class definition, cache the tokens
2374 // corresponding to the default argument. We'll actually parse
2375 // them when we see the end of the class definition.
2376 // FIXME: Templates will require something similar.
2377 // FIXME: Can we use a smart pointer for Toks?
2378 DefArgToks = new CachedTokens;
2379
2380 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2381 tok::semi, false)) {
2382 delete DefArgToks;
2383 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002384 Actions.ActOnParamDefaultArgumentError(Param);
2385 } else
2386 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002387 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002388 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002389 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002390
2391 OwningExprResult DefArgResult(ParseAssignmentExpression());
2392 if (DefArgResult.isInvalid()) {
2393 Actions.ActOnParamDefaultArgumentError(Param);
2394 SkipUntil(tok::comma, tok::r_paren, true, true);
2395 } else {
2396 // Inform the actions module about the default argument
2397 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00002398 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +00002399 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002400 }
2401 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002402
2403 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002404 ParmDecl.getIdentifierLoc(), Param,
2405 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002406 }
2407
2408 // If the next token is a comma, consume it and keep reading arguments.
2409 if (Tok.isNot(tok::comma)) break;
2410
2411 // Consume the comma.
2412 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002413 }
2414
Chris Lattner9f7564b2008-04-06 06:57:35 +00002415 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002416 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002417
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002418 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002419 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002420
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002421 DeclSpec DS;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002422 bool hasExceptionSpec = false;
2423 bool hasAnyExceptionSpec = false;
2424 // FIXME: Does an empty vector ever allocate? Exception specifications are
2425 // extremely rare, so we want something like a SmallVector<TypeTy*, 0>. :-)
2426 std::vector<TypeTy*> Exceptions;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002427 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002428 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002429 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002430 if (!DS.getSourceRange().getEnd().isInvalid())
2431 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002432
2433 // Parse exception-specification[opt].
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002434 if (Tok.is(tok::kw_throw)) {
2435 hasExceptionSpec = true;
2436 ParseExceptionSpecification(Loc, Exceptions, hasAnyExceptionSpec);
2437 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002438 }
2439
Chris Lattner4b009652007-07-25 00:24:17 +00002440 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002441 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002442 EllipsisLoc,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002443 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002444 DS.getTypeQualifiers(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002445 hasExceptionSpec,
2446 hasAnyExceptionSpec,
2447 Exceptions.empty() ? 0 :
2448 &Exceptions[0],
2449 Exceptions.size(), LParenLoc, D),
Sebastian Redl0c986032009-02-09 18:23:29 +00002450 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002451}
2452
Chris Lattner35d9c912008-04-06 06:34:08 +00002453/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2454/// we found a K&R-style identifier list instead of a type argument list. The
2455/// current token is known to be the first identifier in the list.
2456///
2457/// identifier-list: [C99 6.7.5]
2458/// identifier
2459/// identifier-list ',' identifier
2460///
2461void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2462 Declarator &D) {
2463 // Build up an array of information about the parsed arguments.
2464 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2465 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2466
2467 // If there was no identifier specified for the declarator, either we are in
2468 // an abstract-declarator, or we are in a parameter declarator which was found
2469 // to be abstract. In abstract-declarators, identifier lists are not valid:
2470 // diagnose this.
2471 if (!D.getIdentifier())
2472 Diag(Tok, diag::ext_ident_list_in_param);
2473
2474 // Tok is known to be the first identifier in the list. Remember this
2475 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002476 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002477 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner5261d0c2009-03-28 19:18:32 +00002478 Tok.getLocation(),
2479 DeclPtrTy()));
Chris Lattner35d9c912008-04-06 06:34:08 +00002480
Chris Lattner113a56b2008-04-06 06:39:19 +00002481 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002482
2483 while (Tok.is(tok::comma)) {
2484 // Eat the comma.
2485 ConsumeToken();
2486
Chris Lattner113a56b2008-04-06 06:39:19 +00002487 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002488 if (Tok.isNot(tok::identifier)) {
2489 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002490 SkipUntil(tok::r_paren);
2491 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002492 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002493
Chris Lattner35d9c912008-04-06 06:34:08 +00002494 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002495
2496 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002497 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002498 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002499
2500 // Verify that the argument identifier has not already been mentioned.
2501 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002502 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002503 } else {
2504 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002505 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002506 Tok.getLocation(),
2507 DeclPtrTy()));
Chris Lattner113a56b2008-04-06 06:39:19 +00002508 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002509
2510 // Eat the identifier.
2511 ConsumeToken();
2512 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002513
2514 // If we have the closing ')', eat it and we're done.
2515 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2516
Chris Lattner113a56b2008-04-06 06:39:19 +00002517 // Remember that we parsed a function type, and remember the attributes. This
2518 // function type is always a K&R style function type, which is not varargs and
2519 // has no prototype.
2520 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002521 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002522 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002523 /*TypeQuals*/0,
2524 /*exception*/false, false, 0, 0,
2525 LParenLoc, D),
Sebastian Redl0c986032009-02-09 18:23:29 +00002526 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002527}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002528
Chris Lattner4b009652007-07-25 00:24:17 +00002529/// [C90] direct-declarator '[' constant-expression[opt] ']'
2530/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2531/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2532/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2533/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2534void Parser::ParseBracketDeclarator(Declarator &D) {
2535 SourceLocation StartLoc = ConsumeBracket();
2536
Chris Lattner1525c3a2008-12-18 07:27:21 +00002537 // C array syntax has many features, but by-far the most common is [] and [4].
2538 // This code does a fast path to handle some of the most obvious cases.
2539 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002540 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002541 // Remember that we parsed the empty array type.
2542 OwningExprResult NumElements(Actions);
Sebastian Redl0c986032009-02-09 18:23:29 +00002543 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2544 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002545 return;
2546 } else if (Tok.getKind() == tok::numeric_constant &&
2547 GetLookAheadToken(1).is(tok::r_square)) {
2548 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002549 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002550 ConsumeToken();
2551
Sebastian Redl0c986032009-02-09 18:23:29 +00002552 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002553
2554 // If there was an error parsing the assignment-expression, recover.
2555 if (ExprRes.isInvalid())
2556 ExprRes.release(); // Deallocate expr, just use [].
2557
2558 // Remember that we parsed a array type, and remember its features.
2559 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redl0c986032009-02-09 18:23:29 +00002560 ExprRes.release(), StartLoc),
2561 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002562 return;
2563 }
2564
Chris Lattner4b009652007-07-25 00:24:17 +00002565 // If valid, this location is the position where we read the 'static' keyword.
2566 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002567 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002568 StaticLoc = ConsumeToken();
2569
2570 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002571 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002572 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002573 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002574
2575 // If we haven't already read 'static', check to see if there is one after the
2576 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002577 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002578 StaticLoc = ConsumeToken();
2579
2580 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2581 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002582 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002583
2584 // Handle the case where we have '[*]' as the array size. However, a leading
2585 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2586 // the the token after the star is a ']'. Since stars in arrays are
2587 // infrequent, use of lookahead is not costly here.
2588 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002589 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002590
Chris Lattner306d4df2008-12-18 06:50:14 +00002591 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002592 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002593 StaticLoc = SourceLocation(); // Drop the static.
2594 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002595 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002596 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002597 // Note, in C89, this production uses the constant-expr production instead
2598 // of assignment-expr. The only difference is that assignment-expr allows
2599 // things like '=' and '*='. Sema rejects these in C89 mode because they
2600 // are not i-c-e's, so we don't need to distinguish between the two here.
2601
Chris Lattner4b009652007-07-25 00:24:17 +00002602 // Parse the assignment-expression now.
2603 NumElements = ParseAssignmentExpression();
2604 }
2605
2606 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002607 if (NumElements.isInvalid()) {
Chris Lattnerf3ce8572009-04-24 22:30:50 +00002608 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00002609 // If the expression was invalid, skip it.
2610 SkipUntil(tok::r_square);
2611 return;
2612 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002613
2614 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2615
Chris Lattner1525c3a2008-12-18 07:27:21 +00002616 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002617 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2618 StaticLoc.isValid(), isStar,
Sebastian Redl0c986032009-02-09 18:23:29 +00002619 NumElements.release(), StartLoc),
2620 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002621}
2622
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002623/// [GNU] typeof-specifier:
2624/// typeof ( expressions )
2625/// typeof ( type-name )
2626/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002627///
2628void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002629 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002630 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002631 SourceLocation StartLoc = ConsumeToken();
2632
Chris Lattner34a01ad2007-10-09 17:33:22 +00002633 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002634 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002635 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002636 return;
2637 }
2638
Sebastian Redl14ca7412008-12-11 21:36:32 +00002639 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002640 if (Result.isInvalid()) {
2641 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002642 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002643 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002644
2645 const char *PrevSpec = 0;
2646 // Check for duplicate type specifiers.
2647 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002648 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002649 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002650
2651 // FIXME: Not accurate, the range gets one token more than it should.
2652 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002653 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002654 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002655
Steve Naroff7cbb1462007-07-31 12:34:36 +00002656 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2657
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002658 if (isTypeIdInParens()) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002659 Action::TypeResult Ty = ParseTypeName();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002660
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002661 assert((Ty.isInvalid() || Ty.get()) &&
2662 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff4c255ab2007-07-31 23:56:32 +00002663
Chris Lattner34a01ad2007-10-09 17:33:22 +00002664 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002665 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002666 return;
2667 }
2668 RParenLoc = ConsumeParen();
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002669
2670 if (Ty.isInvalid())
2671 DS.SetTypeSpecError();
2672 else {
2673 const char *PrevSpec = 0;
2674 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2675 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2676 Ty.get()))
2677 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2678 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00002679 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002680 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002681
2682 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002683 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002684 DS.SetTypeSpecError();
Steve Naroff14bbce82007-08-02 02:53:48 +00002685 return;
2686 }
2687 RParenLoc = ConsumeParen();
2688 const char *PrevSpec = 0;
2689 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2690 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002691 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002692 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002693 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002694 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002695}
2696
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002697