blob: 83c02490fe4fa6bc4e0023a0a085111cb6c495cd [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
Douglas Gregor6c0f4062009-02-18 17:45:20 +000040 if (DeclaratorInfo.getInvalidType())
41 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
383 bool InvalidExpr = false;
384 if (ParseExpressionList(Exprs, CommaLocs)) {
385 SkipUntil(tok::r_paren);
386 InvalidExpr = true;
387 }
388 // Match the ')'.
389 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
390
391 if (!InvalidExpr) {
392 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
393 "Unexpected number of commas!");
Chris Lattnera17991f2009-03-29 16:50:03 +0000394 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +0000395 move_arg(Exprs),
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000396 &CommaLocs[0], RParenLoc);
397 }
Douglas Gregor81c29152008-10-29 00:13:59 +0000398 } else {
Chris Lattnera17991f2009-03-29 16:50:03 +0000399 Actions.ActOnUninitializedDecl(ThisDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000400 }
401
Chris Lattner4b009652007-07-25 00:24:17 +0000402 // If we don't have a comma, it is either the end of the list (a ';') or an
403 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000404 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000405 break;
406
407 // Consume the comma.
408 ConsumeToken();
409
410 // Parse the next declarator.
411 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000412
413 // Accept attributes in an init-declarator. In the first declarator in a
414 // declaration, these would be part of the declspec. In subsequent
415 // declarators, they become part of the declarator itself, so that they
416 // don't apply to declarators after *this* one. Examples:
417 // short __attribute__((common)) var; -> declspec
418 // short var __attribute__((common)); -> declarator
419 // short x, __attribute__((common)) var; -> declarator
Sebastian Redl0c986032009-02-09 18:23:29 +0000420 if (Tok.is(tok::kw___attribute)) {
421 SourceLocation Loc;
422 AttributeList *AttrList = ParseAttributes(&Loc);
423 D.AddAttributes(AttrList, Loc);
424 }
Chris Lattner926cf542008-10-20 04:57:38 +0000425
Chris Lattner4b009652007-07-25 00:24:17 +0000426 ParseDeclarator(D);
427 }
428
Chris Lattner2c41d482009-03-29 17:18:04 +0000429 return Actions.FinalizeDeclaratorGroup(CurScope, &DeclsInGroup[0],
430 DeclsInGroup.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000431}
432
433/// ParseSpecifierQualifierList
434/// specifier-qualifier-list:
435/// type-specifier specifier-qualifier-list[opt]
436/// type-qualifier specifier-qualifier-list[opt]
437/// [GNU] attributes specifier-qualifier-list[opt]
438///
439void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
440 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
441 /// parse declaration-specifiers and complain about extra stuff.
442 ParseDeclarationSpecifiers(DS);
443
444 // Validate declspec for type-name.
445 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000446 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000447 Diag(Tok, diag::err_typename_requires_specqual);
448
449 // Issue diagnostic and remove storage class if present.
450 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
451 if (DS.getStorageClassSpecLoc().isValid())
452 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
453 else
454 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
455 DS.ClearStorageClassSpecs();
456 }
457
458 // Issue diagnostic and remove function specfier if present.
459 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000460 if (DS.isInlineSpecified())
461 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
462 if (DS.isVirtualSpecified())
463 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
464 if (DS.isExplicitSpecified())
465 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000466 DS.ClearFunctionSpecs();
467 }
468}
469
470/// ParseDeclarationSpecifiers
471/// declaration-specifiers: [C99 6.7]
472/// storage-class-specifier declaration-specifiers[opt]
473/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000474/// [C99] function-specifier declaration-specifiers[opt]
475/// [GNU] attributes declaration-specifiers[opt]
476///
477/// storage-class-specifier: [C99 6.7.1]
478/// 'typedef'
479/// 'extern'
480/// 'static'
481/// 'auto'
482/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000483/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000484/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000485/// function-specifier: [C99 6.7.4]
486/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000487/// [C++] 'virtual'
488/// [C++] 'explicit'
Chris Lattner4b009652007-07-25 00:24:17 +0000489///
Douglas Gregor52473432008-12-24 02:52:09 +0000490void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000491 TemplateParameterLists *TemplateParams,
492 AccessSpecifier AS){
Chris Lattnera4ff4272008-03-13 06:29:04 +0000493 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000494 while (1) {
495 int isInvalid = false;
496 const char *PrevSpec = 0;
497 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000498
Chris Lattner4b009652007-07-25 00:24:17 +0000499 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000500 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000501 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000502 // If this is not a declaration specifier token, we're done reading decl
503 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000504 DS.Finish(Diags, PP);
Chris Lattner4b009652007-07-25 00:24:17 +0000505 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000506
507 case tok::coloncolon: // ::foo::bar
508 // Annotate C++ scope specifiers. If we get one, loop.
509 if (TryAnnotateCXXScopeToken())
510 continue;
511 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000512
513 case tok::annot_cxxscope: {
514 if (DS.hasTypeSpecifier())
515 goto DoneWithDeclSpec;
516
517 // We are looking for a qualified typename.
Douglas Gregor80b95c52009-03-25 15:40:00 +0000518 Token Next = NextToken();
519 if (Next.is(tok::annot_template_id) &&
520 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregoraabb8502009-03-31 00:43:58 +0000521 ->Kind == TNK_Type_template) {
Douglas Gregor80b95c52009-03-25 15:40:00 +0000522 // We have a qualified template-id, e.g., N::A<int>
523 CXXScopeSpec SS;
524 ParseOptionalCXXScopeSpecifier(SS);
525 assert(Tok.is(tok::annot_template_id) &&
526 "ParseOptionalCXXScopeSpecifier not working");
527 AnnotateTemplateIdTokenAsType(&SS);
528 continue;
529 }
530
531 if (Next.isNot(tok::identifier))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000532 goto DoneWithDeclSpec;
533
534 CXXScopeSpec SS;
Douglas Gregor041e9292009-03-26 23:56:24 +0000535 SS.setScopeRep(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000536 SS.setRange(Tok.getAnnotationRange());
537
538 // If the next token is the name of the class type that the C++ scope
539 // denotes, followed by a '(', then this is a constructor declaration.
540 // We're done with the decl-specifiers.
541 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
542 CurScope, &SS) &&
543 GetLookAheadToken(2).is(tok::l_paren))
544 goto DoneWithDeclSpec;
545
Douglas Gregor1075a162009-02-04 17:00:24 +0000546 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
547 Next.getLocation(), CurScope, &SS);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000548
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000549 if (TypeRep == 0)
550 goto DoneWithDeclSpec;
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000551
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000552 ConsumeToken(); // The C++ scope.
553
Douglas Gregora60c62e2009-02-09 15:09:02 +0000554 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000555 TypeRep);
556 if (isInvalid)
557 break;
558
559 DS.SetRangeEnd(Tok.getLocation());
560 ConsumeToken(); // The typename.
561
562 continue;
563 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000564
565 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +0000566 if (Tok.getAnnotationValue())
567 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
568 Tok.getAnnotationValue());
569 else
570 DS.SetTypeSpecError();
Chris Lattnerc297b722009-01-21 19:48:37 +0000571 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
572 ConsumeToken(); // The typename
573
574 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
575 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
576 // Objective-C interface. If we don't have Objective-C or a '<', this is
577 // just a normal reference to a typedef name.
578 if (!Tok.is(tok::less) || !getLang().ObjC1)
579 continue;
580
581 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000582 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnerc297b722009-01-21 19:48:37 +0000583 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
584 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
585
586 DS.SetRangeEnd(EndProtoLoc);
587 continue;
588 }
589
Chris Lattnerfda18db2008-07-26 01:18:38 +0000590 // typedef-name
591 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000592 // In C++, check to see if this is a scope specifier like foo::bar::, if
593 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000594 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
595 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000596
Chris Lattnerfda18db2008-07-26 01:18:38 +0000597 // This identifier can only be a typedef name if we haven't already seen
598 // a type-specifier. Without this check we misparse:
599 // typedef int X; struct Y { short X; }; as 'short int'.
600 if (DS.hasTypeSpecifier())
601 goto DoneWithDeclSpec;
602
603 // It has to be available as a typedef too!
Douglas Gregor1075a162009-02-04 17:00:24 +0000604 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
605 Tok.getLocation(), CurScope);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000606
Chris Lattnerfda18db2008-07-26 01:18:38 +0000607 if (TypeRep == 0)
608 goto DoneWithDeclSpec;
Douglas Gregor8e458f42009-02-09 18:46:07 +0000609
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000610 // C++: If the identifier is actually the name of the class type
611 // being defined and the next token is a '(', then this is a
612 // constructor declaration. We're done with the decl-specifiers
613 // and will treat this token as an identifier.
614 if (getLang().CPlusPlus &&
Douglas Gregorcab994d2009-01-09 22:42:13 +0000615 CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000616 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
617 NextToken().getKind() == tok::l_paren)
618 goto DoneWithDeclSpec;
619
Douglas Gregora60c62e2009-02-09 15:09:02 +0000620 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerfda18db2008-07-26 01:18:38 +0000621 TypeRep);
622 if (isInvalid)
623 break;
624
625 DS.SetRangeEnd(Tok.getLocation());
626 ConsumeToken(); // The identifier
627
628 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
629 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
630 // Objective-C interface. If we don't have Objective-C or a '<', this is
631 // just a normal reference to a typedef name.
632 if (!Tok.is(tok::less) || !getLang().ObjC1)
633 continue;
634
635 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000636 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000637 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000638 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000639
640 DS.SetRangeEnd(EndProtoLoc);
641
Steve Narofff7683302008-09-22 10:28:57 +0000642 // Need to support trailing type qualifiers (e.g. "id<p> const").
643 // If a type specifier follows, it will be diagnosed elsewhere.
644 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000645 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000646
647 // type-name
648 case tok::annot_template_id: {
649 TemplateIdAnnotation *TemplateId
650 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregoraabb8502009-03-31 00:43:58 +0000651 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor0c281a82009-02-25 19:37:18 +0000652 // This template-id does not refer to a type name, so we're
653 // done with the type-specifiers.
654 goto DoneWithDeclSpec;
655 }
656
657 // Turn the template-id annotation token into a type annotation
658 // token, then try again to parse it as a type-specifier.
Douglas Gregord7cb0372009-04-01 21:51:26 +0000659 AnnotateTemplateIdTokenAsType();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000660 continue;
661 }
662
Chris Lattner4b009652007-07-25 00:24:17 +0000663 // GNU attributes support.
664 case tok::kw___attribute:
665 DS.AddAttributes(ParseAttributes());
666 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000667
668 // Microsoft declspec support.
669 case tok::kw___declspec:
670 if (!PP.getLangOptions().Microsoft)
671 goto DoneWithDeclSpec;
672 FuzzyParseMicrosoftDeclSpec();
673 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000674
Steve Naroffedd04d52008-12-25 14:16:32 +0000675 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000676 case tok::kw___forceinline:
677 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000678 case tok::kw___cdecl:
679 case tok::kw___stdcall:
680 case tok::kw___fastcall:
681 if (!PP.getLangOptions().Microsoft)
682 goto DoneWithDeclSpec;
683 // Just ignore it.
684 break;
685
Chris Lattner4b009652007-07-25 00:24:17 +0000686 // storage-class-specifier
687 case tok::kw_typedef:
688 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
689 break;
690 case tok::kw_extern:
691 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000692 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000693 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
694 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000695 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000696 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
697 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000698 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000699 case tok::kw_static:
700 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000701 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000702 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
703 break;
704 case tok::kw_auto:
705 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
706 break;
707 case tok::kw_register:
708 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
709 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000710 case tok::kw_mutable:
711 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
712 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000713 case tok::kw___thread:
714 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
715 break;
716
Chris Lattner4b009652007-07-25 00:24:17 +0000717 continue;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000718
Chris Lattner4b009652007-07-25 00:24:17 +0000719 // function-specifier
720 case tok::kw_inline:
721 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
722 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000723 case tok::kw_virtual:
724 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
725 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000726 case tok::kw_explicit:
727 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
728 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000729
730 // type-specifier
731 case tok::kw_short:
732 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
733 break;
734 case tok::kw_long:
735 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
736 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
737 else
738 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
739 break;
740 case tok::kw_signed:
741 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
742 break;
743 case tok::kw_unsigned:
744 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
745 break;
746 case tok::kw__Complex:
747 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
748 break;
749 case tok::kw__Imaginary:
750 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
751 break;
752 case tok::kw_void:
753 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
754 break;
755 case tok::kw_char:
756 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
757 break;
758 case tok::kw_int:
759 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
760 break;
761 case tok::kw_float:
762 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
763 break;
764 case tok::kw_double:
765 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
766 break;
767 case tok::kw_wchar_t:
768 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
769 break;
770 case tok::kw_bool:
771 case tok::kw__Bool:
772 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
773 break;
774 case tok::kw__Decimal32:
775 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
776 break;
777 case tok::kw__Decimal64:
778 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
779 break;
780 case tok::kw__Decimal128:
781 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
782 break;
783
784 // class-specifier:
785 case tok::kw_class:
786 case tok::kw_struct:
787 case tok::kw_union:
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000788 ParseClassSpecifier(DS, TemplateParams, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000789 continue;
790
791 // enum-specifier:
792 case tok::kw_enum:
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000793 ParseEnumSpecifier(DS, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000794 continue;
795
796 // cv-qualifier:
797 case tok::kw_const:
798 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
799 break;
800 case tok::kw_volatile:
801 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
802 getLang())*2;
803 break;
804 case tok::kw_restrict:
805 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
806 getLang())*2;
807 break;
808
Douglas Gregord3022602009-03-27 23:10:48 +0000809 // C++ typename-specifier:
810 case tok::kw_typename:
811 if (TryAnnotateTypeOrScopeToken())
812 continue;
813 break;
814
Chris Lattnerc297b722009-01-21 19:48:37 +0000815 // GNU typeof support.
816 case tok::kw_typeof:
817 ParseTypeofSpecifier(DS);
818 continue;
819
Steve Naroff5f0466b2008-06-05 00:02:44 +0000820 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000821 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000822 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
823 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000824 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000825 goto DoneWithDeclSpec;
826
827 {
828 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000829 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000830 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000831 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000832 DS.SetRangeEnd(EndProtoLoc);
833
Chris Lattnerf006a222008-11-18 07:48:38 +0000834 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattnerb980c732009-04-03 18:38:42 +0000835 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattnerf006a222008-11-18 07:48:38 +0000836 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000837 // Need to support trailing type qualifiers (e.g. "id<p> const").
838 // If a type specifier follows, it will be diagnosed elsewhere.
839 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000840 }
Chris Lattner4b009652007-07-25 00:24:17 +0000841 }
842 // If the specifier combination wasn't legal, issue a diagnostic.
843 if (isInvalid) {
844 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000845 // Pick between error or extwarn.
846 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
847 : diag::ext_duplicate_declspec;
848 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000849 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000850 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000851 ConsumeToken();
852 }
853}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000854
Chris Lattnerd706dc82009-01-06 06:59:53 +0000855/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000856/// primarily follow the C++ grammar with additions for C99 and GNU,
857/// which together subsume the C grammar. Note that the C++
858/// type-specifier also includes the C type-qualifier (for const,
859/// volatile, and C99 restrict). Returns true if a type-specifier was
860/// found (and parsed), false otherwise.
861///
862/// type-specifier: [C++ 7.1.5]
863/// simple-type-specifier
864/// class-specifier
865/// enum-specifier
866/// elaborated-type-specifier [TODO]
867/// cv-qualifier
868///
869/// cv-qualifier: [C++ 7.1.5.1]
870/// 'const'
871/// 'volatile'
872/// [C99] 'restrict'
873///
874/// simple-type-specifier: [ C++ 7.1.5.2]
875/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
876/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
877/// 'char'
878/// 'wchar_t'
879/// 'bool'
880/// 'short'
881/// 'int'
882/// 'long'
883/// 'signed'
884/// 'unsigned'
885/// 'float'
886/// 'double'
887/// 'void'
888/// [C99] '_Bool'
889/// [C99] '_Complex'
890/// [C99] '_Imaginary' // Removed in TC2?
891/// [GNU] '_Decimal32'
892/// [GNU] '_Decimal64'
893/// [GNU] '_Decimal128'
894/// [GNU] typeof-specifier
895/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
896/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerd706dc82009-01-06 06:59:53 +0000897bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
898 const char *&PrevSpec,
899 TemplateParameterLists *TemplateParams){
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000900 SourceLocation Loc = Tok.getLocation();
901
902 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +0000903 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +0000904 case tok::kw_typename: // typename foo::bar
Chris Lattnerb75fde62009-01-04 23:41:41 +0000905 // Annotate typenames and C++ scope specifiers. If we get one, just
906 // recurse to handle whatever we get.
907 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000908 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000909 // Otherwise, not a type specifier.
910 return false;
911 case tok::coloncolon: // ::foo::bar
912 if (NextToken().is(tok::kw_new) || // ::new
913 NextToken().is(tok::kw_delete)) // ::delete
914 return false;
915
916 // Annotate typenames and C++ scope specifiers. If we get one, just
917 // recurse to handle whatever we get.
918 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000919 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000920 // Otherwise, not a type specifier.
921 return false;
922
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000923 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +0000924 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +0000925 if (Tok.getAnnotationValue())
926 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
927 Tok.getAnnotationValue());
928 else
929 DS.SetTypeSpecError();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000930 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
931 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000932
933 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
934 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
935 // Objective-C interface. If we don't have Objective-C or a '<', this is
936 // just a normal reference to a typedef name.
937 if (!Tok.is(tok::less) || !getLang().ObjC1)
938 return true;
939
940 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000941 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000942 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
943 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
944
945 DS.SetRangeEnd(EndProtoLoc);
946 return true;
947 }
948
949 case tok::kw_short:
950 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
951 break;
952 case tok::kw_long:
953 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
954 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
955 else
956 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
957 break;
958 case tok::kw_signed:
959 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
960 break;
961 case tok::kw_unsigned:
962 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
963 break;
964 case tok::kw__Complex:
965 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
966 break;
967 case tok::kw__Imaginary:
968 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
969 break;
970 case tok::kw_void:
971 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
972 break;
973 case tok::kw_char:
974 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
975 break;
976 case tok::kw_int:
977 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
978 break;
979 case tok::kw_float:
980 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
981 break;
982 case tok::kw_double:
983 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
984 break;
985 case tok::kw_wchar_t:
986 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
987 break;
988 case tok::kw_bool:
989 case tok::kw__Bool:
990 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
991 break;
992 case tok::kw__Decimal32:
993 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
994 break;
995 case tok::kw__Decimal64:
996 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
997 break;
998 case tok::kw__Decimal128:
999 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
1000 break;
1001
1002 // class-specifier:
1003 case tok::kw_class:
1004 case tok::kw_struct:
1005 case tok::kw_union:
Douglas Gregor52473432008-12-24 02:52:09 +00001006 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001007 return true;
1008
1009 // enum-specifier:
1010 case tok::kw_enum:
1011 ParseEnumSpecifier(DS);
1012 return true;
1013
1014 // cv-qualifier:
1015 case tok::kw_const:
1016 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1017 getLang())*2;
1018 break;
1019 case tok::kw_volatile:
1020 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1021 getLang())*2;
1022 break;
1023 case tok::kw_restrict:
1024 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1025 getLang())*2;
1026 break;
1027
1028 // GNU typeof support.
1029 case tok::kw_typeof:
1030 ParseTypeofSpecifier(DS);
1031 return true;
1032
Steve Naroffedd04d52008-12-25 14:16:32 +00001033 case tok::kw___cdecl:
1034 case tok::kw___stdcall:
1035 case tok::kw___fastcall:
Chris Lattner5bb837e2009-01-21 19:19:26 +00001036 if (!PP.getLangOptions().Microsoft) return false;
1037 ConsumeToken();
1038 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +00001039
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001040 default:
1041 // Not a type-specifier; do nothing.
1042 return false;
1043 }
1044
1045 // If the specifier combination wasn't legal, issue a diagnostic.
1046 if (isInvalid) {
1047 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001048 // Pick between error or extwarn.
1049 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1050 : diag::ext_duplicate_declspec;
1051 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001052 }
1053 DS.SetRangeEnd(Tok.getLocation());
1054 ConsumeToken(); // whatever we parsed above.
1055 return true;
1056}
Chris Lattner4b009652007-07-25 00:24:17 +00001057
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001058/// ParseStructDeclaration - Parse a struct declaration without the terminating
1059/// semicolon.
1060///
Chris Lattner4b009652007-07-25 00:24:17 +00001061/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001062/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001063/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001064/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +00001065/// struct-declarator-list:
1066/// struct-declarator
1067/// struct-declarator-list ',' struct-declarator
1068/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1069/// struct-declarator:
1070/// declarator
1071/// [GNU] declarator attributes[opt]
1072/// declarator[opt] ':' constant-expression
1073/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1074///
Chris Lattner3dd8d392008-04-10 06:46:29 +00001075void Parser::
1076ParseStructDeclaration(DeclSpec &DS,
1077 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001078 if (Tok.is(tok::kw___extension__)) {
1079 // __extension__ silences extension warnings in the subexpression.
1080 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001081 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001082 return ParseStructDeclaration(DS, Fields);
1083 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001084
1085 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001086 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001087 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001088
Douglas Gregorb748fc52009-01-12 22:49:06 +00001089 // If there are no declarators, this is a free-standing declaration
1090 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001091 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001092 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001093 return;
1094 }
1095
1096 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001097 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001098 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001099 FieldDeclarator &DeclaratorInfo = Fields.back();
1100
Steve Naroffa9adf112007-08-20 22:28:22 +00001101 /// struct-declarator: declarator
1102 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001103 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001104 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001105
Chris Lattner34a01ad2007-10-09 17:33:22 +00001106 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001107 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001108 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001109 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001110 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001111 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001112 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001113 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001114
Steve Naroffa9adf112007-08-20 22:28:22 +00001115 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001116 if (Tok.is(tok::kw___attribute)) {
1117 SourceLocation Loc;
1118 AttributeList *AttrList = ParseAttributes(&Loc);
1119 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1120 }
1121
Steve Naroffa9adf112007-08-20 22:28:22 +00001122 // If we don't have a comma, it is either the end of the list (a ';')
1123 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001124 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001125 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001126
Steve Naroffa9adf112007-08-20 22:28:22 +00001127 // Consume the comma.
1128 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001129
Steve Naroffa9adf112007-08-20 22:28:22 +00001130 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001131 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001132
Steve Naroffa9adf112007-08-20 22:28:22 +00001133 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001134 if (Tok.is(tok::kw___attribute)) {
1135 SourceLocation Loc;
1136 AttributeList *AttrList = ParseAttributes(&Loc);
1137 Fields.back().D.AddAttributes(AttrList, Loc);
1138 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001139 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001140}
1141
1142/// ParseStructUnionBody
1143/// struct-contents:
1144/// struct-declaration-list
1145/// [EXT] empty
1146/// [GNU] "struct-declaration-list" without terminatoring ';'
1147/// struct-declaration-list:
1148/// struct-declaration
1149/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001150/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001151///
Chris Lattner4b009652007-07-25 00:24:17 +00001152void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001153 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001154 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1155 PP.getSourceManager(),
1156 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001157
Chris Lattner4b009652007-07-25 00:24:17 +00001158 SourceLocation LBraceLoc = ConsumeBrace();
1159
Douglas Gregorcab994d2009-01-09 22:42:13 +00001160 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001161 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1162
Chris Lattner4b009652007-07-25 00:24:17 +00001163 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1164 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001165 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001166 Diag(Tok, diag::ext_empty_struct_union_enum)
1167 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001168
Chris Lattner5261d0c2009-03-28 19:18:32 +00001169 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001170 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1171
Chris Lattner4b009652007-07-25 00:24:17 +00001172 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001173 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001174 // Each iteration of this loop reads one struct-declaration.
1175
1176 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001177 if (Tok.is(tok::semi)) {
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001178 Diag(Tok, diag::ext_extra_struct_semi)
1179 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +00001180 ConsumeToken();
1181 continue;
1182 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001183
1184 // Parse all the comma separated declarators.
1185 DeclSpec DS;
1186 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001187 if (!Tok.is(tok::at)) {
1188 ParseStructDeclaration(DS, FieldDeclarators);
1189
1190 // Convert them all to fields.
1191 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1192 FieldDeclarator &FD = FieldDeclarators[i];
1193 // Install the declarator into the current TagDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001194 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1195 DS.getSourceRange().getBegin(),
1196 FD.D, FD.BitfieldSize);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001197 FieldDecls.push_back(Field);
1198 }
1199 } else { // Handle @defs
1200 ConsumeToken();
1201 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1202 Diag(Tok, diag::err_unexpected_at);
1203 SkipUntil(tok::semi, true, true);
1204 continue;
1205 }
1206 ConsumeToken();
1207 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1208 if (!Tok.is(tok::identifier)) {
1209 Diag(Tok, diag::err_expected_ident);
1210 SkipUntil(tok::semi, true, true);
1211 continue;
1212 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001213 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001214 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1215 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001216 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1217 ConsumeToken();
1218 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1219 }
Chris Lattner4b009652007-07-25 00:24:17 +00001220
Chris Lattner34a01ad2007-10-09 17:33:22 +00001221 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001222 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001223 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001224 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001225 break;
1226 } else {
1227 Diag(Tok, diag::err_expected_semi_decl_list);
1228 // Skip to end of block or statement
1229 SkipUntil(tok::r_brace, true, true);
1230 }
1231 }
1232
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001233 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001234
Chris Lattner4b009652007-07-25 00:24:17 +00001235 AttributeList *AttrList = 0;
1236 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001237 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001238 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001239
1240 Actions.ActOnFields(CurScope,
1241 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1242 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001243 AttrList);
1244 StructScope.Exit();
1245 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001246}
1247
1248
1249/// ParseEnumSpecifier
1250/// enum-specifier: [C99 6.7.2.2]
1251/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001252///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001253/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1254/// '}' attributes[opt]
1255/// 'enum' identifier
1256/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001257///
1258/// [C++] elaborated-type-specifier:
1259/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1260///
Douglas Gregor0c793bb2009-03-25 22:00:53 +00001261void Parser::ParseEnumSpecifier(DeclSpec &DS, AccessSpecifier AS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001262 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +00001263 SourceLocation StartLoc = ConsumeToken();
1264
1265 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001266
1267 AttributeList *Attr = 0;
1268 // If attributes exist after tag, parse them.
1269 if (Tok.is(tok::kw___attribute))
1270 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001271
1272 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001273 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001274 if (Tok.isNot(tok::identifier)) {
1275 Diag(Tok, diag::err_expected_ident);
1276 if (Tok.isNot(tok::l_brace)) {
1277 // Has no name and is not a definition.
1278 // Skip the rest of this declarator, up until the comma or semicolon.
1279 SkipUntil(tok::comma, true);
1280 return;
1281 }
1282 }
1283 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001284
1285 // Must have either 'enum name' or 'enum {...}'.
1286 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1287 Diag(Tok, diag::err_expected_ident_lbrace);
1288
1289 // Skip the rest of this declarator, up until the comma or semicolon.
1290 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001291 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001292 }
1293
1294 // If an identifier is present, consume and remember it.
1295 IdentifierInfo *Name = 0;
1296 SourceLocation NameLoc;
1297 if (Tok.is(tok::identifier)) {
1298 Name = Tok.getIdentifierInfo();
1299 NameLoc = ConsumeToken();
1300 }
1301
1302 // There are three options here. If we have 'enum foo;', then this is a
1303 // forward declaration. If we have 'enum foo {...' then this is a
1304 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1305 //
1306 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1307 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1308 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1309 //
1310 Action::TagKind TK;
1311 if (Tok.is(tok::l_brace))
1312 TK = Action::TK_Definition;
1313 else if (Tok.is(tok::semi))
1314 TK = Action::TK_Declaration;
1315 else
1316 TK = Action::TK_Reference;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001317 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
1318 StartLoc, SS, Name, NameLoc, Attr, AS);
Chris Lattner4b009652007-07-25 00:24:17 +00001319
Chris Lattner34a01ad2007-10-09 17:33:22 +00001320 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001321 ParseEnumBody(StartLoc, TagDecl);
1322
1323 // TODO: semantic analysis on the declspec for enums.
1324 const char *PrevSpec = 0;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001325 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
1326 TagDecl.getAs<void>()))
Chris Lattnerf006a222008-11-18 07:48:38 +00001327 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001328}
1329
1330/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1331/// enumerator-list:
1332/// enumerator
1333/// enumerator-list ',' enumerator
1334/// enumerator:
1335/// enumeration-constant
1336/// enumeration-constant '=' constant-expression
1337/// enumeration-constant:
1338/// identifier
1339///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001340void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001341 // Enter the scope of the enum body and start the definition.
1342 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001343 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001344
Chris Lattner4b009652007-07-25 00:24:17 +00001345 SourceLocation LBraceLoc = ConsumeBrace();
1346
Chris Lattnerc9a92452007-08-27 17:24:30 +00001347 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001348 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001349 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001350
Chris Lattner5261d0c2009-03-28 19:18:32 +00001351 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattner4b009652007-07-25 00:24:17 +00001352
Chris Lattner5261d0c2009-03-28 19:18:32 +00001353 DeclPtrTy LastEnumConstDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001354
1355 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001356 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001357 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1358 SourceLocation IdentLoc = ConsumeToken();
1359
1360 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001361 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001362 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001363 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001364 AssignedVal = ParseConstantExpression();
1365 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001366 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001367 }
1368
1369 // Install the enumerator constant into EnumDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001370 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1371 LastEnumConstDecl,
1372 IdentLoc, Ident,
1373 EqualLoc,
1374 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001375 EnumConstantDecls.push_back(EnumConstDecl);
1376 LastEnumConstDecl = EnumConstDecl;
1377
Chris Lattner34a01ad2007-10-09 17:33:22 +00001378 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001379 break;
1380 SourceLocation CommaLoc = ConsumeToken();
1381
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001382 if (Tok.isNot(tok::identifier) &&
1383 !(getLang().C99 || getLang().CPlusPlus0x))
1384 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1385 << getLang().CPlusPlus
1386 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Chris Lattner4b009652007-07-25 00:24:17 +00001387 }
1388
1389 // Eat the }.
1390 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1391
Steve Naroff0acc9c92007-09-15 18:49:24 +00001392 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001393 EnumConstantDecls.size());
1394
Chris Lattner5261d0c2009-03-28 19:18:32 +00001395 Action::AttrTy *AttrList = 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001396 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001397 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001398 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001399
1400 EnumScope.Exit();
1401 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001402}
1403
1404/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001405/// start of a type-qualifier-list.
1406bool Parser::isTypeQualifier() const {
1407 switch (Tok.getKind()) {
1408 default: return false;
1409 // type-qualifier
1410 case tok::kw_const:
1411 case tok::kw_volatile:
1412 case tok::kw_restrict:
1413 return true;
1414 }
1415}
1416
1417/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001418/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001419bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001420 switch (Tok.getKind()) {
1421 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001422
1423 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001424 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001425 // Annotate typenames and C++ scope specifiers. If we get one, just
1426 // recurse to handle whatever we get.
1427 if (TryAnnotateTypeOrScopeToken())
1428 return isTypeSpecifierQualifier();
1429 // Otherwise, not a type specifier.
1430 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001431
Chris Lattnerb75fde62009-01-04 23:41:41 +00001432 case tok::coloncolon: // ::foo::bar
1433 if (NextToken().is(tok::kw_new) || // ::new
1434 NextToken().is(tok::kw_delete)) // ::delete
1435 return false;
1436
1437 // Annotate typenames and C++ scope specifiers. If we get one, just
1438 // recurse to handle whatever we get.
1439 if (TryAnnotateTypeOrScopeToken())
1440 return isTypeSpecifierQualifier();
1441 // Otherwise, not a type specifier.
1442 return false;
1443
Chris Lattner4b009652007-07-25 00:24:17 +00001444 // GNU attributes support.
1445 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001446 // GNU typeof support.
1447 case tok::kw_typeof:
1448
Chris Lattner4b009652007-07-25 00:24:17 +00001449 // type-specifiers
1450 case tok::kw_short:
1451 case tok::kw_long:
1452 case tok::kw_signed:
1453 case tok::kw_unsigned:
1454 case tok::kw__Complex:
1455 case tok::kw__Imaginary:
1456 case tok::kw_void:
1457 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001458 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001459 case tok::kw_int:
1460 case tok::kw_float:
1461 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001462 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001463 case tok::kw__Bool:
1464 case tok::kw__Decimal32:
1465 case tok::kw__Decimal64:
1466 case tok::kw__Decimal128:
1467
Chris Lattner2e78db32008-04-13 18:59:07 +00001468 // struct-or-union-specifier (C99) or class-specifier (C++)
1469 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001470 case tok::kw_struct:
1471 case tok::kw_union:
1472 // enum-specifier
1473 case tok::kw_enum:
1474
1475 // type-qualifier
1476 case tok::kw_const:
1477 case tok::kw_volatile:
1478 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001479
1480 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001481 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001482 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001483
1484 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1485 case tok::less:
1486 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001487
1488 case tok::kw___cdecl:
1489 case tok::kw___stdcall:
1490 case tok::kw___fastcall:
1491 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001492 }
1493}
1494
1495/// isDeclarationSpecifier() - Return true if the current token is part of a
1496/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001497bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001498 switch (Tok.getKind()) {
1499 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001500
1501 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-03-09 21:12:44 +00001502 // Unfortunate hack to support "Class.factoryMethod" notation.
1503 if (getLang().ObjC1 && NextToken().is(tok::period))
1504 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001505 // Fall through
Steve Naroff73ec9322009-03-09 21:12:44 +00001506
Douglas Gregord3022602009-03-27 23:10:48 +00001507 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001508 // Annotate typenames and C++ scope specifiers. If we get one, just
1509 // recurse to handle whatever we get.
1510 if (TryAnnotateTypeOrScopeToken())
1511 return isDeclarationSpecifier();
1512 // Otherwise, not a declaration specifier.
1513 return false;
1514 case tok::coloncolon: // ::foo::bar
1515 if (NextToken().is(tok::kw_new) || // ::new
1516 NextToken().is(tok::kw_delete)) // ::delete
1517 return false;
1518
1519 // Annotate typenames and C++ scope specifiers. If we get one, just
1520 // recurse to handle whatever we get.
1521 if (TryAnnotateTypeOrScopeToken())
1522 return isDeclarationSpecifier();
1523 // Otherwise, not a declaration specifier.
1524 return false;
1525
Chris Lattner4b009652007-07-25 00:24:17 +00001526 // storage-class-specifier
1527 case tok::kw_typedef:
1528 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001529 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001530 case tok::kw_static:
1531 case tok::kw_auto:
1532 case tok::kw_register:
1533 case tok::kw___thread:
1534
1535 // type-specifiers
1536 case tok::kw_short:
1537 case tok::kw_long:
1538 case tok::kw_signed:
1539 case tok::kw_unsigned:
1540 case tok::kw__Complex:
1541 case tok::kw__Imaginary:
1542 case tok::kw_void:
1543 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001544 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001545 case tok::kw_int:
1546 case tok::kw_float:
1547 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001548 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001549 case tok::kw__Bool:
1550 case tok::kw__Decimal32:
1551 case tok::kw__Decimal64:
1552 case tok::kw__Decimal128:
1553
Chris Lattner2e78db32008-04-13 18:59:07 +00001554 // struct-or-union-specifier (C99) or class-specifier (C++)
1555 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001556 case tok::kw_struct:
1557 case tok::kw_union:
1558 // enum-specifier
1559 case tok::kw_enum:
1560
1561 // type-qualifier
1562 case tok::kw_const:
1563 case tok::kw_volatile:
1564 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001565
Chris Lattner4b009652007-07-25 00:24:17 +00001566 // function-specifier
1567 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001568 case tok::kw_virtual:
1569 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001570
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001571 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001572 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001573
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001574 // GNU typeof support.
1575 case tok::kw_typeof:
1576
1577 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001578 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001579 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001580
1581 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1582 case tok::less:
1583 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001584
Steve Naroffab1a3632009-01-06 19:34:12 +00001585 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001586 case tok::kw___cdecl:
1587 case tok::kw___stdcall:
1588 case tok::kw___fastcall:
1589 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001590 }
1591}
1592
1593
1594/// ParseTypeQualifierListOpt
1595/// type-qualifier-list: [C99 6.7.5]
1596/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001597/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001598/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001599/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001600///
Chris Lattner460696f2008-12-18 07:02:59 +00001601void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001602 while (1) {
1603 int isInvalid = false;
1604 const char *PrevSpec = 0;
1605 SourceLocation Loc = Tok.getLocation();
1606
1607 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001608 case tok::kw_const:
1609 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1610 getLang())*2;
1611 break;
1612 case tok::kw_volatile:
1613 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1614 getLang())*2;
1615 break;
1616 case tok::kw_restrict:
1617 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1618 getLang())*2;
1619 break;
Steve Naroffad620402008-12-25 14:41:26 +00001620 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001621 case tok::kw___cdecl:
1622 case tok::kw___stdcall:
1623 case tok::kw___fastcall:
1624 if (!PP.getLangOptions().Microsoft)
1625 goto DoneWithTypeQuals;
1626 // Just ignore it.
1627 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001628 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001629 if (AttributesAllowed) {
1630 DS.AddAttributes(ParseAttributes());
1631 continue; // do *not* consume the next token!
1632 }
1633 // otherwise, FALL THROUGH!
1634 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001635 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001636 // If this is not a type-qualifier token, we're done reading type
1637 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001638 DS.Finish(Diags, PP);
Chris Lattner460696f2008-12-18 07:02:59 +00001639 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001640 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001641
Chris Lattner4b009652007-07-25 00:24:17 +00001642 // If the specifier combination wasn't legal, issue a diagnostic.
1643 if (isInvalid) {
1644 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001645 // Pick between error or extwarn.
1646 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1647 : diag::ext_duplicate_declspec;
1648 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001649 }
1650 ConsumeToken();
1651 }
1652}
1653
1654
1655/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1656///
1657void Parser::ParseDeclarator(Declarator &D) {
1658 /// This implements the 'declarator' production in the C grammar, then checks
1659 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001660 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001661}
1662
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001663/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1664/// is parsed by the function passed to it. Pass null, and the direct-declarator
1665/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001666/// ptr-operator production.
1667///
Sebastian Redl75555032009-01-24 21:16:55 +00001668/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1669/// [C] pointer[opt] direct-declarator
1670/// [C++] direct-declarator
1671/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001672///
1673/// pointer: [C99 6.7.5]
1674/// '*' type-qualifier-list[opt]
1675/// '*' type-qualifier-list[opt] pointer
1676///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001677/// ptr-operator:
1678/// '*' cv-qualifier-seq[opt]
1679/// '&'
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001680/// [C++0x] '&&'
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001681/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001682/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001683/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001684void Parser::ParseDeclaratorInternal(Declarator &D,
1685 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001686
Sebastian Redl75555032009-01-24 21:16:55 +00001687 // C++ member pointers start with a '::' or a nested-name.
1688 // Member pointers get special handling, since there's no place for the
1689 // scope spec in the generic path below.
Chris Lattner053dd2d2009-03-24 17:04:48 +00001690 if (getLang().CPlusPlus &&
1691 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1692 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl75555032009-01-24 21:16:55 +00001693 CXXScopeSpec SS;
1694 if (ParseOptionalCXXScopeSpecifier(SS)) {
1695 if(Tok.isNot(tok::star)) {
1696 // The scope spec really belongs to the direct-declarator.
1697 D.getCXXScopeSpec() = SS;
1698 if (DirectDeclParser)
1699 (this->*DirectDeclParser)(D);
1700 return;
1701 }
1702
1703 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001704 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00001705 DeclSpec DS;
1706 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001707 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001708
1709 // Recurse to parse whatever is left.
1710 ParseDeclaratorInternal(D, DirectDeclParser);
1711
1712 // Sema will have to catch (syntactically invalid) pointers into global
1713 // scope. It has to catch pointers into namespace scope anyway.
1714 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001715 Loc, DS.TakeAttributes()),
1716 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00001717 return;
1718 }
1719 }
1720
1721 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001722 // Not a pointer, C++ reference, or block.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001723 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner053dd2d2009-03-24 17:04:48 +00001724 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001725 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001726 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001727 if (DirectDeclParser)
1728 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001729 return;
1730 }
Sebastian Redl75555032009-01-24 21:16:55 +00001731
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001732 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1733 // '&&' -> rvalue reference
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001734 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redl0c986032009-02-09 18:23:29 +00001735 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001736
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001737 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner69f01932008-02-21 01:32:26 +00001738 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001739 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001740
Chris Lattner4b009652007-07-25 00:24:17 +00001741 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001742 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001743
Chris Lattner4b009652007-07-25 00:24:17 +00001744 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001745 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001746 if (Kind == tok::star)
1747 // Remember that we parsed a pointer type, and remember the type-quals.
1748 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001749 DS.TakeAttributes()),
1750 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00001751 else
1752 // Remember that we parsed a Block type, and remember the type-quals.
1753 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001754 Loc),
1755 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001756 } else {
1757 // Is a reference
1758 DeclSpec DS;
1759
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001760 // Complain about rvalue references in C++03, but then go on and build
1761 // the declarator.
1762 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1763 Diag(Loc, diag::err_rvalue_reference);
1764
Chris Lattner4b009652007-07-25 00:24:17 +00001765 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1766 // cv-qualifiers are introduced through the use of a typedef or of a
1767 // template type argument, in which case the cv-qualifiers are ignored.
1768 //
1769 // [GNU] Retricted references are allowed.
1770 // [GNU] Attributes on references are allowed.
1771 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001772 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00001773
1774 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1775 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1776 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001777 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001778 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1779 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001780 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001781 }
1782
1783 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001784 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001785
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001786 if (D.getNumTypeObjects() > 0) {
1787 // C++ [dcl.ref]p4: There shall be no references to references.
1788 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1789 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001790 if (const IdentifierInfo *II = D.getIdentifier())
1791 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1792 << II;
1793 else
1794 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1795 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001796
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001797 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001798 // can go ahead and build the (technically ill-formed)
1799 // declarator: reference collapsing will take care of it.
1800 }
1801 }
1802
Chris Lattner4b009652007-07-25 00:24:17 +00001803 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001804 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001805 DS.TakeAttributes(),
1806 Kind == tok::amp),
Sebastian Redl0c986032009-02-09 18:23:29 +00001807 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001808 }
1809}
1810
1811/// ParseDirectDeclarator
1812/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001813/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001814/// '(' declarator ')'
1815/// [GNU] '(' attributes declarator ')'
1816/// [C90] direct-declarator '[' constant-expression[opt] ']'
1817/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1818/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1819/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1820/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1821/// direct-declarator '(' parameter-type-list ')'
1822/// direct-declarator '(' identifier-list[opt] ')'
1823/// [GNU] direct-declarator '(' parameter-forward-declarations
1824/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001825/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1826/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001827/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001828///
1829/// declarator-id: [C++ 8]
1830/// id-expression
1831/// '::'[opt] nested-name-specifier[opt] type-name
1832///
1833/// id-expression: [C++ 5.1]
1834/// unqualified-id
1835/// qualified-id [TODO]
1836///
1837/// unqualified-id: [C++ 5.1]
1838/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001839/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001840/// conversion-function-id [TODO]
1841/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00001842/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001843///
Chris Lattner4b009652007-07-25 00:24:17 +00001844void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001845 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001846
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001847 if (getLang().CPlusPlus) {
1848 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00001849 // ParseDeclaratorInternal might already have parsed the scope.
1850 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1851 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001852 if (afterCXXScope) {
1853 // Change the declaration context for name lookup, until this function
1854 // is exited (and the declarator has been parsed).
1855 DeclScopeObj.EnterDeclaratorScope();
1856 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001857
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001858 if (Tok.is(tok::identifier)) {
1859 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor2fa10442008-12-18 19:37:40 +00001860
Douglas Gregor2fa10442008-12-18 19:37:40 +00001861 // If this identifier is the name of the current class, it's a
1862 // constructor name.
Douglas Gregor0c281a82009-02-25 19:37:18 +00001863 if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)){
Steve Naroff7b36a1b2009-01-28 19:39:02 +00001864 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor1075a162009-02-04 17:00:24 +00001865 Tok.getLocation(), CurScope),
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001866 Tok.getLocation());
Douglas Gregor2fa10442008-12-18 19:37:40 +00001867 // This is a normal identifier.
Sebastian Redl0c986032009-02-09 18:23:29 +00001868 } else
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001869 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1870 ConsumeToken();
1871 goto PastIdentifier;
Douglas Gregor0c281a82009-02-25 19:37:18 +00001872 } else if (Tok.is(tok::annot_template_id)) {
1873 TemplateIdAnnotation *TemplateId
1874 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1875
1876 // FIXME: Could this template-id name a constructor?
1877
1878 // FIXME: This is an egregious hack, where we silently ignore
1879 // the specialization (which should be a function template
1880 // specialization name) and use the name instead. This hack
1881 // will go away when we have support for function
1882 // specializations.
1883 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
1884 TemplateId->Destroy();
1885 ConsumeToken();
1886 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00001887 } else if (Tok.is(tok::kw_operator)) {
1888 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00001889 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00001890
Douglas Gregor853dd392008-12-26 15:00:45 +00001891 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00001892 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
1893 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00001894 } else {
1895 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00001896 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
1897 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
1898 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00001899 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00001900 }
Douglas Gregor853dd392008-12-26 15:00:45 +00001901 }
1902 goto PastIdentifier;
1903 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001904 // This should be a C++ destructor.
1905 SourceLocation TildeLoc = ConsumeToken();
1906 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001907 // FIXME: Inaccurate.
1908 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00001909 SourceLocation EndLoc;
Douglas Gregord7cb0372009-04-01 21:51:26 +00001910 TypeResult Type = ParseClassName(EndLoc);
1911 if (Type.isInvalid())
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001912 D.SetIdentifier(0, TildeLoc);
Douglas Gregord7cb0372009-04-01 21:51:26 +00001913 else
1914 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001915 } else {
1916 Diag(Tok, diag::err_expected_class_name);
1917 D.SetIdentifier(0, TildeLoc);
1918 }
1919 goto PastIdentifier;
1920 }
1921
1922 // If we reached this point, token is not identifier and not '~'.
1923
1924 if (afterCXXScope) {
1925 Diag(Tok, diag::err_expected_unqualified_id);
1926 D.SetIdentifier(0, Tok.getLocation());
1927 D.setInvalidType(true);
1928 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001929 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001930 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001931 }
1932
1933 // If we reached this point, we are either in C/ObjC or the token didn't
1934 // satisfy any of the C++-specific checks.
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001935 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1936 assert(!getLang().CPlusPlus &&
1937 "There's a C++-specific check for tok::identifier above");
1938 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1939 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1940 ConsumeToken();
1941 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001942 // direct-declarator: '(' declarator ')'
1943 // direct-declarator: '(' attributes declarator ')'
1944 // Example: 'char (*X)' or 'int (*XX)(void)'
1945 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001946 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001947 // This could be something simple like "int" (in which case the declarator
1948 // portion is empty), if an abstract-declarator is allowed.
1949 D.SetIdentifier(0, Tok.getLocation());
1950 } else {
Douglas Gregorf03265d2009-03-06 23:28:18 +00001951 if (D.getContext() == Declarator::MemberContext)
1952 Diag(Tok, diag::err_expected_member_name_or_semi)
1953 << D.getDeclSpec().getSourceRange();
1954 else if (getLang().CPlusPlus)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001955 Diag(Tok, diag::err_expected_unqualified_id);
1956 else
Chris Lattnerf006a222008-11-18 07:48:38 +00001957 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00001958 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00001959 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001960 }
1961
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001962 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00001963 assert(D.isPastIdentifier() &&
1964 "Haven't past the location of the identifier yet?");
1965
1966 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001967 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001968 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1969 // In such a case, check if we actually have a function declarator; if it
1970 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001971 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1972 // When not in file scope, warn for ambiguous function declarators, just
1973 // in case the author intended it as a variable definition.
1974 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1975 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1976 break;
1977 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001978 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001979 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001980 ParseBracketDeclarator(D);
1981 } else {
1982 break;
1983 }
1984 }
1985}
1986
Chris Lattnera0d056d2008-04-06 05:45:57 +00001987/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1988/// only called before the identifier, so these are most likely just grouping
1989/// parens for precedence. If we find that these are actually function
1990/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1991///
1992/// direct-declarator:
1993/// '(' declarator ')'
1994/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001995/// direct-declarator '(' parameter-type-list ')'
1996/// direct-declarator '(' identifier-list[opt] ')'
1997/// [GNU] direct-declarator '(' parameter-forward-declarations
1998/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001999///
2000void Parser::ParseParenDeclarator(Declarator &D) {
2001 SourceLocation StartLoc = ConsumeParen();
2002 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
2003
Chris Lattner1f185292008-10-20 02:05:46 +00002004 // Eat any attributes before we look at whether this is a grouping or function
2005 // declarator paren. If this is a grouping paren, the attribute applies to
2006 // the type being built up, for example:
2007 // int (__attribute__(()) *x)(long y)
2008 // If this ends up not being a grouping paren, the attribute applies to the
2009 // first argument, for example:
2010 // int (__attribute__(()) int x)
2011 // In either case, we need to eat any attributes to be able to determine what
2012 // sort of paren this is.
2013 //
2014 AttributeList *AttrList = 0;
2015 bool RequiresArg = false;
2016 if (Tok.is(tok::kw___attribute)) {
2017 AttrList = ParseAttributes();
2018
2019 // We require that the argument list (if this is a non-grouping paren) be
2020 // present even if the attribute list was empty.
2021 RequiresArg = true;
2022 }
Steve Naroffedd04d52008-12-25 14:16:32 +00002023 // Eat any Microsoft extensions.
Douglas Gregore51b7c82009-01-10 00:48:18 +00002024 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2025 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroffedd04d52008-12-25 14:16:32 +00002026 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00002027
Chris Lattnera0d056d2008-04-06 05:45:57 +00002028 // If we haven't past the identifier yet (or where the identifier would be
2029 // stored, if this is an abstract declarator), then this is probably just
2030 // grouping parens. However, if this could be an abstract-declarator, then
2031 // this could also be the start of function arguments (consider 'void()').
2032 bool isGrouping;
2033
2034 if (!D.mayOmitIdentifier()) {
2035 // If this can't be an abstract-declarator, this *must* be a grouping
2036 // paren, because we haven't seen the identifier yet.
2037 isGrouping = true;
2038 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002039 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00002040 isDeclarationSpecifier()) { // 'int(int)' is a function.
2041 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2042 // considered to be a type, not a K&R identifier-list.
2043 isGrouping = false;
2044 } else {
2045 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2046 isGrouping = true;
2047 }
2048
2049 // If this is a grouping paren, handle:
2050 // direct-declarator: '(' declarator ')'
2051 // direct-declarator: '(' attributes declarator ')'
2052 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002053 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002054 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00002055 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00002056 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002057
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002058 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002059 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00002060 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002061
2062 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00002063 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002064 return;
2065 }
2066
2067 // Okay, if this wasn't a grouping paren, it must be the start of a function
2068 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00002069 // identifier (and remember where it would have been), then call into
2070 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00002071 D.SetIdentifier(0, Tok.getLocation());
2072
Chris Lattner1f185292008-10-20 02:05:46 +00002073 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002074}
2075
2076/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2077/// declarator D up to a paren, which indicates that we are parsing function
2078/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00002079///
Chris Lattner1f185292008-10-20 02:05:46 +00002080/// If AttrList is non-null, then the caller parsed those arguments immediately
2081/// after the open paren - they should be considered to be the first argument of
2082/// a parameter. If RequiresArg is true, then the first argument of the
2083/// function is required to be present and required to not be an identifier
2084/// list.
2085///
Chris Lattner4b009652007-07-25 00:24:17 +00002086/// This method also handles this portion of the grammar:
2087/// parameter-type-list: [C99 6.7.5]
2088/// parameter-list
2089/// parameter-list ',' '...'
2090///
2091/// parameter-list: [C99 6.7.5]
2092/// parameter-declaration
2093/// parameter-list ',' parameter-declaration
2094///
2095/// parameter-declaration: [C99 6.7.5]
2096/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00002097/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002098/// [GNU] declaration-specifiers declarator attributes
Sebastian Redla8cecf62009-03-24 22:27:57 +00002099/// declaration-specifiers abstract-declarator[opt]
2100/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002101/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002102/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2103///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002104/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redla8cecf62009-03-24 22:27:57 +00002105/// and "exception-specification[opt]".
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002106///
Chris Lattner1f185292008-10-20 02:05:46 +00002107void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2108 AttributeList *AttrList,
2109 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002110 // lparen is already consumed!
2111 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002112
Chris Lattner1f185292008-10-20 02:05:46 +00002113 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002114 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002115 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002116 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002117 delete AttrList;
2118 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002119
Sebastian Redl0c986032009-02-09 18:23:29 +00002120 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002121
2122 // cv-qualifier-seq[opt].
2123 DeclSpec DS;
2124 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002125 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002126 if (!DS.getSourceRange().getEnd().isInvalid())
2127 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002128
2129 // Parse exception-specification[opt].
2130 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002131 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002132 }
2133
Chris Lattner9f7564b2008-04-06 06:57:35 +00002134 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002135 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002136 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002137 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002138 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002139 /*arglist*/ 0, 0,
2140 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002141 LParenLoc, D),
2142 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002143 return;
Chris Lattner1f185292008-10-20 02:05:46 +00002144 }
2145
2146 // Alternatively, this parameter list may be an identifier list form for a
2147 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002148 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002149 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002150 // K&R identifier lists can't have typedefs as identifiers, per
2151 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002152 if (RequiresArg) {
2153 Diag(Tok, diag::err_argument_required_after_attribute);
2154 delete AttrList;
2155 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002156 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2157 // normal declarators, not for abstract-declarators.
2158 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002159 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002160 }
2161
2162 // Finally, a normal, non-empty parameter type list.
2163
2164 // Build up an array of information about the parsed arguments.
2165 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002166
2167 // Enter function-declaration scope, limiting any declarators to the
2168 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002169 ParseScope PrototypeScope(this,
2170 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002171
2172 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002173 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002174 while (1) {
2175 if (Tok.is(tok::ellipsis)) {
2176 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002177 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002178 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002179 }
2180
Chris Lattner9f7564b2008-04-06 06:57:35 +00002181 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002182
Chris Lattner9f7564b2008-04-06 06:57:35 +00002183 // Parse the declaration-specifiers.
2184 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002185
2186 // If the caller parsed attributes for the first argument, add them now.
2187 if (AttrList) {
2188 DS.AddAttributes(AttrList);
2189 AttrList = 0; // Only apply the attributes to the first parameter.
2190 }
Chris Lattner9e785f52009-02-27 18:38:20 +00002191 ParseDeclarationSpecifiers(DS);
2192
Chris Lattner9f7564b2008-04-06 06:57:35 +00002193 // Parse the declarator. This is "PrototypeContext", because we must
2194 // accept either 'declarator' or 'abstract-declarator' here.
2195 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2196 ParseDeclarator(ParmDecl);
2197
2198 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002199 if (Tok.is(tok::kw___attribute)) {
2200 SourceLocation Loc;
2201 AttributeList *AttrList = ParseAttributes(&Loc);
2202 ParmDecl.AddAttributes(AttrList, Loc);
2203 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002204
Chris Lattner9f7564b2008-04-06 06:57:35 +00002205 // Remember this parsed parameter in ParamInfo.
2206 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2207
Douglas Gregor605de8d2008-12-16 21:30:33 +00002208 // DefArgToks is used when the parsing of default arguments needs
2209 // to be delayed.
2210 CachedTokens *DefArgToks = 0;
2211
Chris Lattner9f7564b2008-04-06 06:57:35 +00002212 // If no parameter was specified, verify that *something* was specified,
2213 // otherwise we have a missing type and identifier.
Chris Lattner9e785f52009-02-27 18:38:20 +00002214 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2215 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002216 // Completely missing, emit error.
2217 Diag(DSStart, diag::err_missing_param);
2218 } else {
2219 // Otherwise, we have something. Add it and let semantic analysis try
2220 // to grok it and add the result to the ParamInfo we are building.
2221
2222 // Inform the actions module about the parameter declarator, so it gets
2223 // added to the current scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002224 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002225
2226 // Parse the default argument, if any. We parse the default
2227 // arguments in all dialects; the semantic analysis in
2228 // ActOnParamDefaultArgument will reject the default argument in
2229 // C.
2230 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002231 SourceLocation EqualLoc = Tok.getLocation();
2232
Chris Lattner3e254fb2008-04-08 04:40:51 +00002233 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002234 if (D.getContext() == Declarator::MemberContext) {
2235 // If we're inside a class definition, cache the tokens
2236 // corresponding to the default argument. We'll actually parse
2237 // them when we see the end of the class definition.
2238 // FIXME: Templates will require something similar.
2239 // FIXME: Can we use a smart pointer for Toks?
2240 DefArgToks = new CachedTokens;
2241
2242 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2243 tok::semi, false)) {
2244 delete DefArgToks;
2245 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002246 Actions.ActOnParamDefaultArgumentError(Param);
2247 } else
2248 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002249 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002250 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002251 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002252
2253 OwningExprResult DefArgResult(ParseAssignmentExpression());
2254 if (DefArgResult.isInvalid()) {
2255 Actions.ActOnParamDefaultArgumentError(Param);
2256 SkipUntil(tok::comma, tok::r_paren, true, true);
2257 } else {
2258 // Inform the actions module about the default argument
2259 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00002260 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +00002261 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002262 }
2263 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002264
2265 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002266 ParmDecl.getIdentifierLoc(), Param,
2267 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002268 }
2269
2270 // If the next token is a comma, consume it and keep reading arguments.
2271 if (Tok.isNot(tok::comma)) break;
2272
2273 // Consume the comma.
2274 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002275 }
2276
Chris Lattner9f7564b2008-04-06 06:57:35 +00002277 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002278 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002279
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002280 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002281 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002282
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002283 DeclSpec DS;
2284 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002285 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002286 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002287 if (!DS.getSourceRange().getEnd().isInvalid())
2288 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002289
2290 // Parse exception-specification[opt].
2291 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002292 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002293 }
2294
Chris Lattner4b009652007-07-25 00:24:17 +00002295 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002296 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002297 EllipsisLoc,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002298 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002299 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002300 LParenLoc, D),
2301 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002302}
2303
Chris Lattner35d9c912008-04-06 06:34:08 +00002304/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2305/// we found a K&R-style identifier list instead of a type argument list. The
2306/// current token is known to be the first identifier in the list.
2307///
2308/// identifier-list: [C99 6.7.5]
2309/// identifier
2310/// identifier-list ',' identifier
2311///
2312void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2313 Declarator &D) {
2314 // Build up an array of information about the parsed arguments.
2315 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2316 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2317
2318 // If there was no identifier specified for the declarator, either we are in
2319 // an abstract-declarator, or we are in a parameter declarator which was found
2320 // to be abstract. In abstract-declarators, identifier lists are not valid:
2321 // diagnose this.
2322 if (!D.getIdentifier())
2323 Diag(Tok, diag::ext_ident_list_in_param);
2324
2325 // Tok is known to be the first identifier in the list. Remember this
2326 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002327 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002328 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner5261d0c2009-03-28 19:18:32 +00002329 Tok.getLocation(),
2330 DeclPtrTy()));
Chris Lattner35d9c912008-04-06 06:34:08 +00002331
Chris Lattner113a56b2008-04-06 06:39:19 +00002332 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002333
2334 while (Tok.is(tok::comma)) {
2335 // Eat the comma.
2336 ConsumeToken();
2337
Chris Lattner113a56b2008-04-06 06:39:19 +00002338 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002339 if (Tok.isNot(tok::identifier)) {
2340 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002341 SkipUntil(tok::r_paren);
2342 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002343 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002344
Chris Lattner35d9c912008-04-06 06:34:08 +00002345 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002346
2347 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002348 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002349 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002350
2351 // Verify that the argument identifier has not already been mentioned.
2352 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002353 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002354 } else {
2355 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002356 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002357 Tok.getLocation(),
2358 DeclPtrTy()));
Chris Lattner113a56b2008-04-06 06:39:19 +00002359 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002360
2361 // Eat the identifier.
2362 ConsumeToken();
2363 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002364
2365 // If we have the closing ')', eat it and we're done.
2366 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2367
Chris Lattner113a56b2008-04-06 06:39:19 +00002368 // Remember that we parsed a function type, and remember the attributes. This
2369 // function type is always a K&R style function type, which is not varargs and
2370 // has no prototype.
2371 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002372 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002373 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002374 /*TypeQuals*/0, LParenLoc, D),
2375 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002376}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002377
Chris Lattner4b009652007-07-25 00:24:17 +00002378/// [C90] direct-declarator '[' constant-expression[opt] ']'
2379/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2380/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2381/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2382/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2383void Parser::ParseBracketDeclarator(Declarator &D) {
2384 SourceLocation StartLoc = ConsumeBracket();
2385
Chris Lattner1525c3a2008-12-18 07:27:21 +00002386 // C array syntax has many features, but by-far the most common is [] and [4].
2387 // This code does a fast path to handle some of the most obvious cases.
2388 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002389 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002390 // Remember that we parsed the empty array type.
2391 OwningExprResult NumElements(Actions);
Sebastian Redl0c986032009-02-09 18:23:29 +00002392 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2393 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002394 return;
2395 } else if (Tok.getKind() == tok::numeric_constant &&
2396 GetLookAheadToken(1).is(tok::r_square)) {
2397 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002398 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002399 ConsumeToken();
2400
Sebastian Redl0c986032009-02-09 18:23:29 +00002401 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002402
2403 // If there was an error parsing the assignment-expression, recover.
2404 if (ExprRes.isInvalid())
2405 ExprRes.release(); // Deallocate expr, just use [].
2406
2407 // Remember that we parsed a array type, and remember its features.
2408 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redl0c986032009-02-09 18:23:29 +00002409 ExprRes.release(), StartLoc),
2410 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002411 return;
2412 }
2413
Chris Lattner4b009652007-07-25 00:24:17 +00002414 // If valid, this location is the position where we read the 'static' keyword.
2415 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002416 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002417 StaticLoc = ConsumeToken();
2418
2419 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002420 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002421 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002422 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002423
2424 // If we haven't already read 'static', check to see if there is one after the
2425 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002426 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002427 StaticLoc = ConsumeToken();
2428
2429 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2430 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002431 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002432
2433 // Handle the case where we have '[*]' as the array size. However, a leading
2434 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2435 // the the token after the star is a ']'. Since stars in arrays are
2436 // infrequent, use of lookahead is not costly here.
2437 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002438 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002439
Chris Lattner306d4df2008-12-18 06:50:14 +00002440 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002441 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002442 StaticLoc = SourceLocation(); // Drop the static.
2443 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002444 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002445 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002446 // Note, in C89, this production uses the constant-expr production instead
2447 // of assignment-expr. The only difference is that assignment-expr allows
2448 // things like '=' and '*='. Sema rejects these in C89 mode because they
2449 // are not i-c-e's, so we don't need to distinguish between the two here.
2450
Chris Lattner4b009652007-07-25 00:24:17 +00002451 // Parse the assignment-expression now.
2452 NumElements = ParseAssignmentExpression();
2453 }
2454
2455 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002456 if (NumElements.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002457 // If the expression was invalid, skip it.
2458 SkipUntil(tok::r_square);
2459 return;
2460 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002461
2462 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2463
Chris Lattner1525c3a2008-12-18 07:27:21 +00002464 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002465 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2466 StaticLoc.isValid(), isStar,
Sebastian Redl0c986032009-02-09 18:23:29 +00002467 NumElements.release(), StartLoc),
2468 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002469}
2470
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002471/// [GNU] typeof-specifier:
2472/// typeof ( expressions )
2473/// typeof ( type-name )
2474/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002475///
2476void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002477 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002478 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002479 SourceLocation StartLoc = ConsumeToken();
2480
Chris Lattner34a01ad2007-10-09 17:33:22 +00002481 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002482 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002483 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002484 return;
2485 }
2486
Sebastian Redl14ca7412008-12-11 21:36:32 +00002487 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002488 if (Result.isInvalid()) {
2489 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002490 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002491 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002492
2493 const char *PrevSpec = 0;
2494 // Check for duplicate type specifiers.
2495 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002496 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002497 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002498
2499 // FIXME: Not accurate, the range gets one token more than it should.
2500 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002501 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002502 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002503
Steve Naroff7cbb1462007-07-31 12:34:36 +00002504 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2505
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002506 if (isTypeIdInParens()) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002507 Action::TypeResult Ty = ParseTypeName();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002508
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002509 assert((Ty.isInvalid() || Ty.get()) &&
2510 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff4c255ab2007-07-31 23:56:32 +00002511
Chris Lattner34a01ad2007-10-09 17:33:22 +00002512 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002513 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002514 return;
2515 }
2516 RParenLoc = ConsumeParen();
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002517
2518 if (Ty.isInvalid())
2519 DS.SetTypeSpecError();
2520 else {
2521 const char *PrevSpec = 0;
2522 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2523 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2524 Ty.get()))
2525 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2526 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00002527 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002528 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002529
2530 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002531 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002532 DS.SetTypeSpecError();
Steve Naroff14bbce82007-08-02 02:53:48 +00002533 return;
2534 }
2535 RParenLoc = ConsumeParen();
2536 const char *PrevSpec = 0;
2537 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2538 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002539 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002540 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002541 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002542 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002543}
2544
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002545