blob: 11658d4c500b655de157d46e1297bdf252125aa5 [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.
218/// 'Context' should be a Declarator::TheContext value.
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000219///
220/// declaration: [C99 6.7]
221/// block-declaration ->
222/// simple-declaration
223/// others [FIXME]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000224/// [C++] template-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000225/// [C++] namespace-definition
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000226/// [C++] using-directive
227/// [C++] using-declaration [TODO]
Sebastian Redla8cecf62009-03-24 22:27:57 +0000228/// [C++0x] static_assert-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000229/// others... [FIXME]
230///
Chris Lattnera17991f2009-03-29 16:50:03 +0000231Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context) {
232 DeclPtrTy SingleDecl;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000233 switch (Tok.getKind()) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000234 case tok::kw_export:
235 case tok::kw_template:
Chris Lattnera17991f2009-03-29 16:50:03 +0000236 SingleDecl = ParseTemplateDeclarationOrSpecialization(Context);
237 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000238 case tok::kw_namespace:
Chris Lattnera17991f2009-03-29 16:50:03 +0000239 SingleDecl = ParseNamespace(Context);
240 break;
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000241 case tok::kw_using:
Chris Lattnera17991f2009-03-29 16:50:03 +0000242 SingleDecl = ParseUsingDirectiveOrDeclaration(Context);
243 break;
Anders Carlssonab041982009-03-11 16:27:10 +0000244 case tok::kw_static_assert:
Chris Lattnera17991f2009-03-29 16:50:03 +0000245 SingleDecl = ParseStaticAssertDeclaration();
246 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000247 default:
248 return ParseSimpleDeclaration(Context);
249 }
Chris Lattnera17991f2009-03-29 16:50:03 +0000250
251 // This routine returns a DeclGroup, if the thing we parsed only contains a
252 // single decl, convert it now.
253 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000254}
255
256/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
257/// declaration-specifiers init-declarator-list[opt] ';'
258///[C90/C++]init-declarator-list ';' [TODO]
259/// [OMP] threadprivate-directive [TODO]
Chris Lattnerf8016042009-03-29 17:27:48 +0000260///
261/// If RequireSemi is false, this does not check for a ';' at the end of the
262/// declaration.
263Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
264 bool RequireSemi) {
Chris Lattner4b009652007-07-25 00:24:17 +0000265 // Parse the common declaration-specifiers piece.
266 DeclSpec DS;
267 ParseDeclarationSpecifiers(DS);
268
269 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
270 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000271 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000272 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000273 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
274 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000275 }
276
277 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
278 ParseDeclarator(DeclaratorInfo);
279
Chris Lattner2c41d482009-03-29 17:18:04 +0000280 DeclGroupPtrTy DG =
281 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattnerf8016042009-03-29 17:27:48 +0000282
283 // If the client wants to check what comes after the declaration, just return
284 // immediately without checking anything!
285 if (!RequireSemi) return DG;
Chris Lattner2c41d482009-03-29 17:18:04 +0000286
287 if (Tok.is(tok::semi)) {
288 ConsumeToken();
Chris Lattner2c41d482009-03-29 17:18:04 +0000289 return DG;
290 }
291
Chris Lattner2c41d482009-03-29 17:18:04 +0000292 Diag(Tok, diag::err_expected_semi_declation);
293 // Skip to end of block or statement
294 SkipUntil(tok::r_brace, true, true);
295 if (Tok.is(tok::semi))
296 ConsumeToken();
297 return DG;
Chris Lattner4b009652007-07-25 00:24:17 +0000298}
299
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000300
Chris Lattner4b009652007-07-25 00:24:17 +0000301/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
302/// parsing 'declaration-specifiers declarator'. This method is split out this
303/// way to handle the ambiguity between top-level function-definitions and
304/// declarations.
305///
Chris Lattner4b009652007-07-25 00:24:17 +0000306/// init-declarator-list: [C99 6.7]
307/// init-declarator
308/// init-declarator-list ',' init-declarator
309/// init-declarator: [C99 6.7]
310/// declarator
311/// declarator '=' initializer
312/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
313/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000314/// [C++] declarator initializer[opt]
315///
316/// [C++] initializer:
317/// [C++] '=' initializer-clause
318/// [C++] '(' expression-list ')'
Sebastian Redla8cecf62009-03-24 22:27:57 +0000319/// [C++0x] '=' 'default' [TODO]
320/// [C++0x] '=' 'delete'
321///
322/// According to the standard grammar, =default and =delete are function
323/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattner4b009652007-07-25 00:24:17 +0000324///
Chris Lattnera17991f2009-03-29 16:50:03 +0000325Parser::DeclGroupPtrTy Parser::
Chris Lattner4b009652007-07-25 00:24:17 +0000326ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000327 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
328 // that we parse together here.
329 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Chris Lattner4b009652007-07-25 00:24:17 +0000330
331 // At this point, we know that it is not a function definition. Parse the
332 // rest of the init-declarator-list.
333 while (1) {
334 // If a simple-asm-expr is present, parse it.
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000335 if (Tok.is(tok::kw_asm)) {
Sebastian Redl0c986032009-02-09 18:23:29 +0000336 SourceLocation Loc;
337 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000338 if (AsmLabel.isInvalid()) {
Chris Lattner2c41d482009-03-29 17:18:04 +0000339 SkipUntil(tok::semi, true, true);
Chris Lattnera17991f2009-03-29 16:50:03 +0000340 return DeclGroupPtrTy();
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000341 }
Sebastian Redl0c986032009-02-09 18:23:29 +0000342
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000343 D.setAsmLabel(AsmLabel.release());
Sebastian Redl0c986032009-02-09 18:23:29 +0000344 D.SetRangeEnd(Loc);
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000345 }
Chris Lattner4b009652007-07-25 00:24:17 +0000346
347 // If attributes are present, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +0000348 if (Tok.is(tok::kw___attribute)) {
349 SourceLocation Loc;
350 AttributeList *AttrList = ParseAttributes(&Loc);
351 D.AddAttributes(AttrList, Loc);
352 }
Steve Naroff6a0e2092007-09-12 14:07:44 +0000353
354 // Inform the current actions module that we just parsed this declarator.
Chris Lattnera17991f2009-03-29 16:50:03 +0000355 DeclPtrTy ThisDecl = Actions.ActOnDeclarator(CurScope, D);
356 DeclsInGroup.push_back(ThisDecl);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000357
Chris Lattner4b009652007-07-25 00:24:17 +0000358 // Parse declarator '=' initializer.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000359 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000360 ConsumeToken();
Sebastian Redla8cecf62009-03-24 22:27:57 +0000361 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
362 SourceLocation DelLoc = ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000363 Actions.SetDeclDeleted(ThisDecl, DelLoc);
Sebastian Redla8cecf62009-03-24 22:27:57 +0000364 } else {
365 OwningExprResult Init(ParseInitializer());
366 if (Init.isInvalid()) {
Chris Lattner2c41d482009-03-29 17:18:04 +0000367 SkipUntil(tok::semi, true, true);
Chris Lattnera17991f2009-03-29 16:50:03 +0000368 return DeclGroupPtrTy();
Sebastian Redla8cecf62009-03-24 22:27:57 +0000369 }
Chris Lattnera17991f2009-03-29 16:50:03 +0000370 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Chris Lattner4b009652007-07-25 00:24:17 +0000371 }
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000372 } else if (Tok.is(tok::l_paren)) {
373 // Parse C++ direct initializer: '(' expression-list ')'
374 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl6008ac32008-11-25 22:21:31 +0000375 ExprVector Exprs(Actions);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000376 CommaLocsTy CommaLocs;
377
378 bool InvalidExpr = false;
379 if (ParseExpressionList(Exprs, CommaLocs)) {
380 SkipUntil(tok::r_paren);
381 InvalidExpr = true;
382 }
383 // Match the ')'.
384 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
385
386 if (!InvalidExpr) {
387 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
388 "Unexpected number of commas!");
Chris Lattnera17991f2009-03-29 16:50:03 +0000389 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +0000390 move_arg(Exprs),
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000391 &CommaLocs[0], RParenLoc);
392 }
Douglas Gregor81c29152008-10-29 00:13:59 +0000393 } else {
Chris Lattnera17991f2009-03-29 16:50:03 +0000394 Actions.ActOnUninitializedDecl(ThisDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000395 }
396
Chris Lattner4b009652007-07-25 00:24:17 +0000397 // If we don't have a comma, it is either the end of the list (a ';') or an
398 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000399 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000400 break;
401
402 // Consume the comma.
403 ConsumeToken();
404
405 // Parse the next declarator.
406 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000407
408 // Accept attributes in an init-declarator. In the first declarator in a
409 // declaration, these would be part of the declspec. In subsequent
410 // declarators, they become part of the declarator itself, so that they
411 // don't apply to declarators after *this* one. Examples:
412 // short __attribute__((common)) var; -> declspec
413 // short var __attribute__((common)); -> declarator
414 // short x, __attribute__((common)) var; -> declarator
Sebastian Redl0c986032009-02-09 18:23:29 +0000415 if (Tok.is(tok::kw___attribute)) {
416 SourceLocation Loc;
417 AttributeList *AttrList = ParseAttributes(&Loc);
418 D.AddAttributes(AttrList, Loc);
419 }
Chris Lattner926cf542008-10-20 04:57:38 +0000420
Chris Lattner4b009652007-07-25 00:24:17 +0000421 ParseDeclarator(D);
422 }
423
Chris Lattner2c41d482009-03-29 17:18:04 +0000424 return Actions.FinalizeDeclaratorGroup(CurScope, &DeclsInGroup[0],
425 DeclsInGroup.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000426}
427
428/// ParseSpecifierQualifierList
429/// specifier-qualifier-list:
430/// type-specifier specifier-qualifier-list[opt]
431/// type-qualifier specifier-qualifier-list[opt]
432/// [GNU] attributes specifier-qualifier-list[opt]
433///
434void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
435 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
436 /// parse declaration-specifiers and complain about extra stuff.
437 ParseDeclarationSpecifiers(DS);
438
439 // Validate declspec for type-name.
440 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000441 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000442 Diag(Tok, diag::err_typename_requires_specqual);
443
444 // Issue diagnostic and remove storage class if present.
445 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
446 if (DS.getStorageClassSpecLoc().isValid())
447 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
448 else
449 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
450 DS.ClearStorageClassSpecs();
451 }
452
453 // Issue diagnostic and remove function specfier if present.
454 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000455 if (DS.isInlineSpecified())
456 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
457 if (DS.isVirtualSpecified())
458 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
459 if (DS.isExplicitSpecified())
460 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000461 DS.ClearFunctionSpecs();
462 }
463}
464
465/// ParseDeclarationSpecifiers
466/// declaration-specifiers: [C99 6.7]
467/// storage-class-specifier declaration-specifiers[opt]
468/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000469/// [C99] function-specifier declaration-specifiers[opt]
470/// [GNU] attributes declaration-specifiers[opt]
471///
472/// storage-class-specifier: [C99 6.7.1]
473/// 'typedef'
474/// 'extern'
475/// 'static'
476/// 'auto'
477/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000478/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000479/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000480/// function-specifier: [C99 6.7.4]
481/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000482/// [C++] 'virtual'
483/// [C++] 'explicit'
Chris Lattner4b009652007-07-25 00:24:17 +0000484///
Douglas Gregor52473432008-12-24 02:52:09 +0000485void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000486 TemplateParameterLists *TemplateParams,
487 AccessSpecifier AS){
Chris Lattnera4ff4272008-03-13 06:29:04 +0000488 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000489 while (1) {
490 int isInvalid = false;
491 const char *PrevSpec = 0;
492 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000493
Chris Lattner4b009652007-07-25 00:24:17 +0000494 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000495 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000496 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000497 // If this is not a declaration specifier token, we're done reading decl
498 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000499 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000500 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000501
502 case tok::coloncolon: // ::foo::bar
503 // Annotate C++ scope specifiers. If we get one, loop.
504 if (TryAnnotateCXXScopeToken())
505 continue;
506 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000507
508 case tok::annot_cxxscope: {
509 if (DS.hasTypeSpecifier())
510 goto DoneWithDeclSpec;
511
512 // We are looking for a qualified typename.
Douglas Gregor80b95c52009-03-25 15:40:00 +0000513 Token Next = NextToken();
514 if (Next.is(tok::annot_template_id) &&
515 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregoraabb8502009-03-31 00:43:58 +0000516 ->Kind == TNK_Type_template) {
Douglas Gregor80b95c52009-03-25 15:40:00 +0000517 // We have a qualified template-id, e.g., N::A<int>
518 CXXScopeSpec SS;
519 ParseOptionalCXXScopeSpecifier(SS);
520 assert(Tok.is(tok::annot_template_id) &&
521 "ParseOptionalCXXScopeSpecifier not working");
522 AnnotateTemplateIdTokenAsType(&SS);
523 continue;
524 }
525
526 if (Next.isNot(tok::identifier))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000527 goto DoneWithDeclSpec;
528
529 CXXScopeSpec SS;
Douglas Gregor041e9292009-03-26 23:56:24 +0000530 SS.setScopeRep(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000531 SS.setRange(Tok.getAnnotationRange());
532
533 // If the next token is the name of the class type that the C++ scope
534 // denotes, followed by a '(', then this is a constructor declaration.
535 // We're done with the decl-specifiers.
536 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
537 CurScope, &SS) &&
538 GetLookAheadToken(2).is(tok::l_paren))
539 goto DoneWithDeclSpec;
540
Douglas Gregor1075a162009-02-04 17:00:24 +0000541 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
542 Next.getLocation(), CurScope, &SS);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000543
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000544 if (TypeRep == 0)
545 goto DoneWithDeclSpec;
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000546
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000547 ConsumeToken(); // The C++ scope.
548
Douglas Gregora60c62e2009-02-09 15:09:02 +0000549 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000550 TypeRep);
551 if (isInvalid)
552 break;
553
554 DS.SetRangeEnd(Tok.getLocation());
555 ConsumeToken(); // The typename.
556
557 continue;
558 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000559
560 case tok::annot_typename: {
Douglas Gregora60c62e2009-02-09 15:09:02 +0000561 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerc297b722009-01-21 19:48:37 +0000562 Tok.getAnnotationValue());
563 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
564 ConsumeToken(); // The typename
565
566 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
567 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
568 // Objective-C interface. If we don't have Objective-C or a '<', this is
569 // just a normal reference to a typedef name.
570 if (!Tok.is(tok::less) || !getLang().ObjC1)
571 continue;
572
573 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000574 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnerc297b722009-01-21 19:48:37 +0000575 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
576 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
577
578 DS.SetRangeEnd(EndProtoLoc);
579 continue;
580 }
581
Chris Lattnerfda18db2008-07-26 01:18:38 +0000582 // typedef-name
583 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000584 // In C++, check to see if this is a scope specifier like foo::bar::, if
585 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000586 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
587 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000588
Chris Lattnerfda18db2008-07-26 01:18:38 +0000589 // This identifier can only be a typedef name if we haven't already seen
590 // a type-specifier. Without this check we misparse:
591 // typedef int X; struct Y { short X; }; as 'short int'.
592 if (DS.hasTypeSpecifier())
593 goto DoneWithDeclSpec;
594
595 // It has to be available as a typedef too!
Douglas Gregor1075a162009-02-04 17:00:24 +0000596 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
597 Tok.getLocation(), CurScope);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000598
Chris Lattnerfda18db2008-07-26 01:18:38 +0000599 if (TypeRep == 0)
600 goto DoneWithDeclSpec;
Douglas Gregor8e458f42009-02-09 18:46:07 +0000601
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000602 // C++: If the identifier is actually the name of the class type
603 // being defined and the next token is a '(', then this is a
604 // constructor declaration. We're done with the decl-specifiers
605 // and will treat this token as an identifier.
606 if (getLang().CPlusPlus &&
Douglas Gregorcab994d2009-01-09 22:42:13 +0000607 CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000608 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
609 NextToken().getKind() == tok::l_paren)
610 goto DoneWithDeclSpec;
611
Douglas Gregora60c62e2009-02-09 15:09:02 +0000612 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerfda18db2008-07-26 01:18:38 +0000613 TypeRep);
614 if (isInvalid)
615 break;
616
617 DS.SetRangeEnd(Tok.getLocation());
618 ConsumeToken(); // The identifier
619
620 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
621 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
622 // Objective-C interface. If we don't have Objective-C or a '<', this is
623 // just a normal reference to a typedef name.
624 if (!Tok.is(tok::less) || !getLang().ObjC1)
625 continue;
626
627 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000628 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000629 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000630 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000631
632 DS.SetRangeEnd(EndProtoLoc);
633
Steve Narofff7683302008-09-22 10:28:57 +0000634 // Need to support trailing type qualifiers (e.g. "id<p> const").
635 // If a type specifier follows, it will be diagnosed elsewhere.
636 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000637 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000638
639 // type-name
640 case tok::annot_template_id: {
641 TemplateIdAnnotation *TemplateId
642 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregoraabb8502009-03-31 00:43:58 +0000643 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor0c281a82009-02-25 19:37:18 +0000644 // This template-id does not refer to a type name, so we're
645 // done with the type-specifiers.
646 goto DoneWithDeclSpec;
647 }
648
649 // Turn the template-id annotation token into a type annotation
650 // token, then try again to parse it as a type-specifier.
651 if (AnnotateTemplateIdTokenAsType())
652 DS.SetTypeSpecError();
653
654 continue;
655 }
656
Chris Lattner4b009652007-07-25 00:24:17 +0000657 // GNU attributes support.
658 case tok::kw___attribute:
659 DS.AddAttributes(ParseAttributes());
660 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000661
662 // Microsoft declspec support.
663 case tok::kw___declspec:
664 if (!PP.getLangOptions().Microsoft)
665 goto DoneWithDeclSpec;
666 FuzzyParseMicrosoftDeclSpec();
667 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000668
Steve Naroffedd04d52008-12-25 14:16:32 +0000669 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000670 case tok::kw___forceinline:
671 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000672 case tok::kw___cdecl:
673 case tok::kw___stdcall:
674 case tok::kw___fastcall:
675 if (!PP.getLangOptions().Microsoft)
676 goto DoneWithDeclSpec;
677 // Just ignore it.
678 break;
679
Chris Lattner4b009652007-07-25 00:24:17 +0000680 // storage-class-specifier
681 case tok::kw_typedef:
682 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
683 break;
684 case tok::kw_extern:
685 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000686 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000687 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
688 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000689 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000690 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
691 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000692 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000693 case tok::kw_static:
694 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000695 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000696 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
697 break;
698 case tok::kw_auto:
699 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
700 break;
701 case tok::kw_register:
702 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
703 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000704 case tok::kw_mutable:
705 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
706 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000707 case tok::kw___thread:
708 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
709 break;
710
Chris Lattner4b009652007-07-25 00:24:17 +0000711 continue;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000712
Chris Lattner4b009652007-07-25 00:24:17 +0000713 // function-specifier
714 case tok::kw_inline:
715 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
716 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000717 case tok::kw_virtual:
718 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
719 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000720 case tok::kw_explicit:
721 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
722 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000723
724 // type-specifier
725 case tok::kw_short:
726 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
727 break;
728 case tok::kw_long:
729 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
730 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
731 else
732 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
733 break;
734 case tok::kw_signed:
735 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
736 break;
737 case tok::kw_unsigned:
738 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
739 break;
740 case tok::kw__Complex:
741 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
742 break;
743 case tok::kw__Imaginary:
744 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
745 break;
746 case tok::kw_void:
747 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
748 break;
749 case tok::kw_char:
750 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
751 break;
752 case tok::kw_int:
753 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
754 break;
755 case tok::kw_float:
756 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
757 break;
758 case tok::kw_double:
759 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
760 break;
761 case tok::kw_wchar_t:
762 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
763 break;
764 case tok::kw_bool:
765 case tok::kw__Bool:
766 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
767 break;
768 case tok::kw__Decimal32:
769 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
770 break;
771 case tok::kw__Decimal64:
772 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
773 break;
774 case tok::kw__Decimal128:
775 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
776 break;
777
778 // class-specifier:
779 case tok::kw_class:
780 case tok::kw_struct:
781 case tok::kw_union:
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000782 ParseClassSpecifier(DS, TemplateParams, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000783 continue;
784
785 // enum-specifier:
786 case tok::kw_enum:
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000787 ParseEnumSpecifier(DS, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000788 continue;
789
790 // cv-qualifier:
791 case tok::kw_const:
792 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
793 break;
794 case tok::kw_volatile:
795 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
796 getLang())*2;
797 break;
798 case tok::kw_restrict:
799 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
800 getLang())*2;
801 break;
802
Douglas Gregord3022602009-03-27 23:10:48 +0000803 // C++ typename-specifier:
804 case tok::kw_typename:
805 if (TryAnnotateTypeOrScopeToken())
806 continue;
807 break;
808
Chris Lattnerc297b722009-01-21 19:48:37 +0000809 // GNU typeof support.
810 case tok::kw_typeof:
811 ParseTypeofSpecifier(DS);
812 continue;
813
Steve Naroff5f0466b2008-06-05 00:02:44 +0000814 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000815 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000816 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
817 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000818 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000819 goto DoneWithDeclSpec;
820
821 {
822 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000823 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000824 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000825 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000826 DS.SetRangeEnd(EndProtoLoc);
827
Chris Lattnerf006a222008-11-18 07:48:38 +0000828 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
829 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000830 // Need to support trailing type qualifiers (e.g. "id<p> const").
831 // If a type specifier follows, it will be diagnosed elsewhere.
832 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000833 }
Chris Lattner4b009652007-07-25 00:24:17 +0000834 }
835 // If the specifier combination wasn't legal, issue a diagnostic.
836 if (isInvalid) {
837 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000838 // Pick between error or extwarn.
839 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
840 : diag::ext_duplicate_declspec;
841 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000842 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000843 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000844 ConsumeToken();
845 }
846}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000847
Chris Lattnerd706dc82009-01-06 06:59:53 +0000848/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000849/// primarily follow the C++ grammar with additions for C99 and GNU,
850/// which together subsume the C grammar. Note that the C++
851/// type-specifier also includes the C type-qualifier (for const,
852/// volatile, and C99 restrict). Returns true if a type-specifier was
853/// found (and parsed), false otherwise.
854///
855/// type-specifier: [C++ 7.1.5]
856/// simple-type-specifier
857/// class-specifier
858/// enum-specifier
859/// elaborated-type-specifier [TODO]
860/// cv-qualifier
861///
862/// cv-qualifier: [C++ 7.1.5.1]
863/// 'const'
864/// 'volatile'
865/// [C99] 'restrict'
866///
867/// simple-type-specifier: [ C++ 7.1.5.2]
868/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
869/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
870/// 'char'
871/// 'wchar_t'
872/// 'bool'
873/// 'short'
874/// 'int'
875/// 'long'
876/// 'signed'
877/// 'unsigned'
878/// 'float'
879/// 'double'
880/// 'void'
881/// [C99] '_Bool'
882/// [C99] '_Complex'
883/// [C99] '_Imaginary' // Removed in TC2?
884/// [GNU] '_Decimal32'
885/// [GNU] '_Decimal64'
886/// [GNU] '_Decimal128'
887/// [GNU] typeof-specifier
888/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
889/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerd706dc82009-01-06 06:59:53 +0000890bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
891 const char *&PrevSpec,
892 TemplateParameterLists *TemplateParams){
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000893 SourceLocation Loc = Tok.getLocation();
894
895 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +0000896 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +0000897 case tok::kw_typename: // typename foo::bar
Chris Lattnerb75fde62009-01-04 23:41:41 +0000898 // Annotate typenames and C++ scope specifiers. If we get one, just
899 // recurse to handle whatever we get.
900 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000901 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000902 // Otherwise, not a type specifier.
903 return false;
904 case tok::coloncolon: // ::foo::bar
905 if (NextToken().is(tok::kw_new) || // ::new
906 NextToken().is(tok::kw_delete)) // ::delete
907 return false;
908
909 // Annotate typenames and C++ scope specifiers. If we get one, just
910 // recurse to handle whatever we get.
911 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000912 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000913 // Otherwise, not a type specifier.
914 return false;
915
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000916 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +0000917 case tok::annot_typename: {
Douglas Gregora60c62e2009-02-09 15:09:02 +0000918 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000919 Tok.getAnnotationValue());
920 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
921 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000922
923 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
924 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
925 // Objective-C interface. If we don't have Objective-C or a '<', this is
926 // just a normal reference to a typedef name.
927 if (!Tok.is(tok::less) || !getLang().ObjC1)
928 return true;
929
930 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000931 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000932 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
933 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
934
935 DS.SetRangeEnd(EndProtoLoc);
936 return true;
937 }
938
939 case tok::kw_short:
940 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
941 break;
942 case tok::kw_long:
943 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
944 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
945 else
946 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
947 break;
948 case tok::kw_signed:
949 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
950 break;
951 case tok::kw_unsigned:
952 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
953 break;
954 case tok::kw__Complex:
955 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
956 break;
957 case tok::kw__Imaginary:
958 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
959 break;
960 case tok::kw_void:
961 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
962 break;
963 case tok::kw_char:
964 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
965 break;
966 case tok::kw_int:
967 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
968 break;
969 case tok::kw_float:
970 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
971 break;
972 case tok::kw_double:
973 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
974 break;
975 case tok::kw_wchar_t:
976 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
977 break;
978 case tok::kw_bool:
979 case tok::kw__Bool:
980 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
981 break;
982 case tok::kw__Decimal32:
983 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
984 break;
985 case tok::kw__Decimal64:
986 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
987 break;
988 case tok::kw__Decimal128:
989 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
990 break;
991
992 // class-specifier:
993 case tok::kw_class:
994 case tok::kw_struct:
995 case tok::kw_union:
Douglas Gregor52473432008-12-24 02:52:09 +0000996 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000997 return true;
998
999 // enum-specifier:
1000 case tok::kw_enum:
1001 ParseEnumSpecifier(DS);
1002 return true;
1003
1004 // cv-qualifier:
1005 case tok::kw_const:
1006 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1007 getLang())*2;
1008 break;
1009 case tok::kw_volatile:
1010 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1011 getLang())*2;
1012 break;
1013 case tok::kw_restrict:
1014 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1015 getLang())*2;
1016 break;
1017
1018 // GNU typeof support.
1019 case tok::kw_typeof:
1020 ParseTypeofSpecifier(DS);
1021 return true;
1022
Steve Naroffedd04d52008-12-25 14:16:32 +00001023 case tok::kw___cdecl:
1024 case tok::kw___stdcall:
1025 case tok::kw___fastcall:
Chris Lattner5bb837e2009-01-21 19:19:26 +00001026 if (!PP.getLangOptions().Microsoft) return false;
1027 ConsumeToken();
1028 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +00001029
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001030 default:
1031 // Not a type-specifier; do nothing.
1032 return false;
1033 }
1034
1035 // If the specifier combination wasn't legal, issue a diagnostic.
1036 if (isInvalid) {
1037 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001038 // Pick between error or extwarn.
1039 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1040 : diag::ext_duplicate_declspec;
1041 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001042 }
1043 DS.SetRangeEnd(Tok.getLocation());
1044 ConsumeToken(); // whatever we parsed above.
1045 return true;
1046}
Chris Lattner4b009652007-07-25 00:24:17 +00001047
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001048/// ParseStructDeclaration - Parse a struct declaration without the terminating
1049/// semicolon.
1050///
Chris Lattner4b009652007-07-25 00:24:17 +00001051/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001052/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001053/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001054/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +00001055/// struct-declarator-list:
1056/// struct-declarator
1057/// struct-declarator-list ',' struct-declarator
1058/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1059/// struct-declarator:
1060/// declarator
1061/// [GNU] declarator attributes[opt]
1062/// declarator[opt] ':' constant-expression
1063/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1064///
Chris Lattner3dd8d392008-04-10 06:46:29 +00001065void Parser::
1066ParseStructDeclaration(DeclSpec &DS,
1067 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001068 if (Tok.is(tok::kw___extension__)) {
1069 // __extension__ silences extension warnings in the subexpression.
1070 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001071 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001072 return ParseStructDeclaration(DS, Fields);
1073 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001074
1075 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001076 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001077 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001078
Douglas Gregorb748fc52009-01-12 22:49:06 +00001079 // If there are no declarators, this is a free-standing declaration
1080 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001081 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001082 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001083 return;
1084 }
1085
1086 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001087 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001088 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001089 FieldDeclarator &DeclaratorInfo = Fields.back();
1090
Steve Naroffa9adf112007-08-20 22:28:22 +00001091 /// struct-declarator: declarator
1092 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001093 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001094 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001095
Chris Lattner34a01ad2007-10-09 17:33:22 +00001096 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001097 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001098 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001099 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001100 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001101 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001102 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001103 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001104
Steve Naroffa9adf112007-08-20 22:28:22 +00001105 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001106 if (Tok.is(tok::kw___attribute)) {
1107 SourceLocation Loc;
1108 AttributeList *AttrList = ParseAttributes(&Loc);
1109 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1110 }
1111
Steve Naroffa9adf112007-08-20 22:28:22 +00001112 // If we don't have a comma, it is either the end of the list (a ';')
1113 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001114 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001115 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001116
Steve Naroffa9adf112007-08-20 22:28:22 +00001117 // Consume the comma.
1118 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001119
Steve Naroffa9adf112007-08-20 22:28:22 +00001120 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001121 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001122
Steve Naroffa9adf112007-08-20 22:28:22 +00001123 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001124 if (Tok.is(tok::kw___attribute)) {
1125 SourceLocation Loc;
1126 AttributeList *AttrList = ParseAttributes(&Loc);
1127 Fields.back().D.AddAttributes(AttrList, Loc);
1128 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001129 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001130}
1131
1132/// ParseStructUnionBody
1133/// struct-contents:
1134/// struct-declaration-list
1135/// [EXT] empty
1136/// [GNU] "struct-declaration-list" without terminatoring ';'
1137/// struct-declaration-list:
1138/// struct-declaration
1139/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001140/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001141///
Chris Lattner4b009652007-07-25 00:24:17 +00001142void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001143 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001144 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1145 PP.getSourceManager(),
1146 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001147
Chris Lattner4b009652007-07-25 00:24:17 +00001148 SourceLocation LBraceLoc = ConsumeBrace();
1149
Douglas Gregorcab994d2009-01-09 22:42:13 +00001150 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001151 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1152
Chris Lattner4b009652007-07-25 00:24:17 +00001153 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1154 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001155 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001156 Diag(Tok, diag::ext_empty_struct_union_enum)
1157 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001158
Chris Lattner5261d0c2009-03-28 19:18:32 +00001159 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001160 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1161
Chris Lattner4b009652007-07-25 00:24:17 +00001162 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001163 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001164 // Each iteration of this loop reads one struct-declaration.
1165
1166 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001167 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001168 Diag(Tok, diag::ext_extra_struct_semi);
1169 ConsumeToken();
1170 continue;
1171 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001172
1173 // Parse all the comma separated declarators.
1174 DeclSpec DS;
1175 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001176 if (!Tok.is(tok::at)) {
1177 ParseStructDeclaration(DS, FieldDeclarators);
1178
1179 // Convert them all to fields.
1180 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1181 FieldDeclarator &FD = FieldDeclarators[i];
1182 // Install the declarator into the current TagDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001183 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1184 DS.getSourceRange().getBegin(),
1185 FD.D, FD.BitfieldSize);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001186 FieldDecls.push_back(Field);
1187 }
1188 } else { // Handle @defs
1189 ConsumeToken();
1190 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1191 Diag(Tok, diag::err_unexpected_at);
1192 SkipUntil(tok::semi, true, true);
1193 continue;
1194 }
1195 ConsumeToken();
1196 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1197 if (!Tok.is(tok::identifier)) {
1198 Diag(Tok, diag::err_expected_ident);
1199 SkipUntil(tok::semi, true, true);
1200 continue;
1201 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001202 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001203 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1204 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001205 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1206 ConsumeToken();
1207 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1208 }
Chris Lattner4b009652007-07-25 00:24:17 +00001209
Chris Lattner34a01ad2007-10-09 17:33:22 +00001210 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001211 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001212 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001213 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001214 break;
1215 } else {
1216 Diag(Tok, diag::err_expected_semi_decl_list);
1217 // Skip to end of block or statement
1218 SkipUntil(tok::r_brace, true, true);
1219 }
1220 }
1221
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001222 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001223
Chris Lattner4b009652007-07-25 00:24:17 +00001224 AttributeList *AttrList = 0;
1225 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001226 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001227 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001228
1229 Actions.ActOnFields(CurScope,
1230 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1231 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001232 AttrList);
1233 StructScope.Exit();
1234 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001235}
1236
1237
1238/// ParseEnumSpecifier
1239/// enum-specifier: [C99 6.7.2.2]
1240/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001241///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001242/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1243/// '}' attributes[opt]
1244/// 'enum' identifier
1245/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001246///
1247/// [C++] elaborated-type-specifier:
1248/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1249///
Douglas Gregor0c793bb2009-03-25 22:00:53 +00001250void Parser::ParseEnumSpecifier(DeclSpec &DS, AccessSpecifier AS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001251 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +00001252 SourceLocation StartLoc = ConsumeToken();
1253
1254 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001255
1256 AttributeList *Attr = 0;
1257 // If attributes exist after tag, parse them.
1258 if (Tok.is(tok::kw___attribute))
1259 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001260
1261 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001262 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001263 if (Tok.isNot(tok::identifier)) {
1264 Diag(Tok, diag::err_expected_ident);
1265 if (Tok.isNot(tok::l_brace)) {
1266 // Has no name and is not a definition.
1267 // Skip the rest of this declarator, up until the comma or semicolon.
1268 SkipUntil(tok::comma, true);
1269 return;
1270 }
1271 }
1272 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001273
1274 // Must have either 'enum name' or 'enum {...}'.
1275 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1276 Diag(Tok, diag::err_expected_ident_lbrace);
1277
1278 // Skip the rest of this declarator, up until the comma or semicolon.
1279 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001280 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001281 }
1282
1283 // If an identifier is present, consume and remember it.
1284 IdentifierInfo *Name = 0;
1285 SourceLocation NameLoc;
1286 if (Tok.is(tok::identifier)) {
1287 Name = Tok.getIdentifierInfo();
1288 NameLoc = ConsumeToken();
1289 }
1290
1291 // There are three options here. If we have 'enum foo;', then this is a
1292 // forward declaration. If we have 'enum foo {...' then this is a
1293 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1294 //
1295 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1296 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1297 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1298 //
1299 Action::TagKind TK;
1300 if (Tok.is(tok::l_brace))
1301 TK = Action::TK_Definition;
1302 else if (Tok.is(tok::semi))
1303 TK = Action::TK_Declaration;
1304 else
1305 TK = Action::TK_Reference;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001306 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
1307 StartLoc, SS, Name, NameLoc, Attr, AS);
Chris Lattner4b009652007-07-25 00:24:17 +00001308
Chris Lattner34a01ad2007-10-09 17:33:22 +00001309 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001310 ParseEnumBody(StartLoc, TagDecl);
1311
1312 // TODO: semantic analysis on the declspec for enums.
1313 const char *PrevSpec = 0;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001314 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
1315 TagDecl.getAs<void>()))
Chris Lattnerf006a222008-11-18 07:48:38 +00001316 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001317}
1318
1319/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1320/// enumerator-list:
1321/// enumerator
1322/// enumerator-list ',' enumerator
1323/// enumerator:
1324/// enumeration-constant
1325/// enumeration-constant '=' constant-expression
1326/// enumeration-constant:
1327/// identifier
1328///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001329void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001330 // Enter the scope of the enum body and start the definition.
1331 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001332 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001333
Chris Lattner4b009652007-07-25 00:24:17 +00001334 SourceLocation LBraceLoc = ConsumeBrace();
1335
Chris Lattnerc9a92452007-08-27 17:24:30 +00001336 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001337 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001338 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001339
Chris Lattner5261d0c2009-03-28 19:18:32 +00001340 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattner4b009652007-07-25 00:24:17 +00001341
Chris Lattner5261d0c2009-03-28 19:18:32 +00001342 DeclPtrTy LastEnumConstDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001343
1344 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001345 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001346 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1347 SourceLocation IdentLoc = ConsumeToken();
1348
1349 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001350 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001351 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001352 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001353 AssignedVal = ParseConstantExpression();
1354 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001355 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001356 }
1357
1358 // Install the enumerator constant into EnumDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001359 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1360 LastEnumConstDecl,
1361 IdentLoc, Ident,
1362 EqualLoc,
1363 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001364 EnumConstantDecls.push_back(EnumConstDecl);
1365 LastEnumConstDecl = EnumConstDecl;
1366
Chris Lattner34a01ad2007-10-09 17:33:22 +00001367 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001368 break;
1369 SourceLocation CommaLoc = ConsumeToken();
1370
Chris Lattner34a01ad2007-10-09 17:33:22 +00001371 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +00001372 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1373 }
1374
1375 // Eat the }.
1376 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1377
Steve Naroff0acc9c92007-09-15 18:49:24 +00001378 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001379 EnumConstantDecls.size());
1380
Chris Lattner5261d0c2009-03-28 19:18:32 +00001381 Action::AttrTy *AttrList = 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001382 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001383 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001384 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001385
1386 EnumScope.Exit();
1387 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001388}
1389
1390/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001391/// start of a type-qualifier-list.
1392bool Parser::isTypeQualifier() const {
1393 switch (Tok.getKind()) {
1394 default: return false;
1395 // type-qualifier
1396 case tok::kw_const:
1397 case tok::kw_volatile:
1398 case tok::kw_restrict:
1399 return true;
1400 }
1401}
1402
1403/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001404/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001405bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001406 switch (Tok.getKind()) {
1407 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001408
1409 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001410 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001411 // Annotate typenames and C++ scope specifiers. If we get one, just
1412 // recurse to handle whatever we get.
1413 if (TryAnnotateTypeOrScopeToken())
1414 return isTypeSpecifierQualifier();
1415 // Otherwise, not a type specifier.
1416 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001417
Chris Lattnerb75fde62009-01-04 23:41:41 +00001418 case tok::coloncolon: // ::foo::bar
1419 if (NextToken().is(tok::kw_new) || // ::new
1420 NextToken().is(tok::kw_delete)) // ::delete
1421 return false;
1422
1423 // Annotate typenames and C++ scope specifiers. If we get one, just
1424 // recurse to handle whatever we get.
1425 if (TryAnnotateTypeOrScopeToken())
1426 return isTypeSpecifierQualifier();
1427 // Otherwise, not a type specifier.
1428 return false;
1429
Chris Lattner4b009652007-07-25 00:24:17 +00001430 // GNU attributes support.
1431 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001432 // GNU typeof support.
1433 case tok::kw_typeof:
1434
Chris Lattner4b009652007-07-25 00:24:17 +00001435 // type-specifiers
1436 case tok::kw_short:
1437 case tok::kw_long:
1438 case tok::kw_signed:
1439 case tok::kw_unsigned:
1440 case tok::kw__Complex:
1441 case tok::kw__Imaginary:
1442 case tok::kw_void:
1443 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001444 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001445 case tok::kw_int:
1446 case tok::kw_float:
1447 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001448 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001449 case tok::kw__Bool:
1450 case tok::kw__Decimal32:
1451 case tok::kw__Decimal64:
1452 case tok::kw__Decimal128:
1453
Chris Lattner2e78db32008-04-13 18:59:07 +00001454 // struct-or-union-specifier (C99) or class-specifier (C++)
1455 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001456 case tok::kw_struct:
1457 case tok::kw_union:
1458 // enum-specifier
1459 case tok::kw_enum:
1460
1461 // type-qualifier
1462 case tok::kw_const:
1463 case tok::kw_volatile:
1464 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001465
1466 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001467 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001468 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001469
1470 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1471 case tok::less:
1472 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001473
1474 case tok::kw___cdecl:
1475 case tok::kw___stdcall:
1476 case tok::kw___fastcall:
1477 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001478 }
1479}
1480
1481/// isDeclarationSpecifier() - Return true if the current token is part of a
1482/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001483bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001484 switch (Tok.getKind()) {
1485 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001486
1487 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-03-09 21:12:44 +00001488 // Unfortunate hack to support "Class.factoryMethod" notation.
1489 if (getLang().ObjC1 && NextToken().is(tok::period))
1490 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001491 // Fall through
Steve Naroff73ec9322009-03-09 21:12:44 +00001492
Douglas Gregord3022602009-03-27 23:10:48 +00001493 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001494 // Annotate typenames and C++ scope specifiers. If we get one, just
1495 // recurse to handle whatever we get.
1496 if (TryAnnotateTypeOrScopeToken())
1497 return isDeclarationSpecifier();
1498 // Otherwise, not a declaration specifier.
1499 return false;
1500 case tok::coloncolon: // ::foo::bar
1501 if (NextToken().is(tok::kw_new) || // ::new
1502 NextToken().is(tok::kw_delete)) // ::delete
1503 return false;
1504
1505 // Annotate typenames and C++ scope specifiers. If we get one, just
1506 // recurse to handle whatever we get.
1507 if (TryAnnotateTypeOrScopeToken())
1508 return isDeclarationSpecifier();
1509 // Otherwise, not a declaration specifier.
1510 return false;
1511
Chris Lattner4b009652007-07-25 00:24:17 +00001512 // storage-class-specifier
1513 case tok::kw_typedef:
1514 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001515 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001516 case tok::kw_static:
1517 case tok::kw_auto:
1518 case tok::kw_register:
1519 case tok::kw___thread:
1520
1521 // type-specifiers
1522 case tok::kw_short:
1523 case tok::kw_long:
1524 case tok::kw_signed:
1525 case tok::kw_unsigned:
1526 case tok::kw__Complex:
1527 case tok::kw__Imaginary:
1528 case tok::kw_void:
1529 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001530 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001531 case tok::kw_int:
1532 case tok::kw_float:
1533 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001534 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001535 case tok::kw__Bool:
1536 case tok::kw__Decimal32:
1537 case tok::kw__Decimal64:
1538 case tok::kw__Decimal128:
1539
Chris Lattner2e78db32008-04-13 18:59:07 +00001540 // struct-or-union-specifier (C99) or class-specifier (C++)
1541 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001542 case tok::kw_struct:
1543 case tok::kw_union:
1544 // enum-specifier
1545 case tok::kw_enum:
1546
1547 // type-qualifier
1548 case tok::kw_const:
1549 case tok::kw_volatile:
1550 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001551
Chris Lattner4b009652007-07-25 00:24:17 +00001552 // function-specifier
1553 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001554 case tok::kw_virtual:
1555 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001556
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001557 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001558 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001559
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001560 // GNU typeof support.
1561 case tok::kw_typeof:
1562
1563 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001564 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001565 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001566
1567 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1568 case tok::less:
1569 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001570
Steve Naroffab1a3632009-01-06 19:34:12 +00001571 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001572 case tok::kw___cdecl:
1573 case tok::kw___stdcall:
1574 case tok::kw___fastcall:
1575 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001576 }
1577}
1578
1579
1580/// ParseTypeQualifierListOpt
1581/// type-qualifier-list: [C99 6.7.5]
1582/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001583/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001584/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001585/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001586///
Chris Lattner460696f2008-12-18 07:02:59 +00001587void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001588 while (1) {
1589 int isInvalid = false;
1590 const char *PrevSpec = 0;
1591 SourceLocation Loc = Tok.getLocation();
1592
1593 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001594 case tok::kw_const:
1595 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1596 getLang())*2;
1597 break;
1598 case tok::kw_volatile:
1599 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1600 getLang())*2;
1601 break;
1602 case tok::kw_restrict:
1603 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1604 getLang())*2;
1605 break;
Steve Naroffad620402008-12-25 14:41:26 +00001606 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001607 case tok::kw___cdecl:
1608 case tok::kw___stdcall:
1609 case tok::kw___fastcall:
1610 if (!PP.getLangOptions().Microsoft)
1611 goto DoneWithTypeQuals;
1612 // Just ignore it.
1613 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001614 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001615 if (AttributesAllowed) {
1616 DS.AddAttributes(ParseAttributes());
1617 continue; // do *not* consume the next token!
1618 }
1619 // otherwise, FALL THROUGH!
1620 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001621 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001622 // If this is not a type-qualifier token, we're done reading type
1623 // qualifiers. First verify that DeclSpec's are consistent.
1624 DS.Finish(Diags, PP.getSourceManager(), getLang());
1625 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001626 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001627
Chris Lattner4b009652007-07-25 00:24:17 +00001628 // If the specifier combination wasn't legal, issue a diagnostic.
1629 if (isInvalid) {
1630 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001631 // Pick between error or extwarn.
1632 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1633 : diag::ext_duplicate_declspec;
1634 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001635 }
1636 ConsumeToken();
1637 }
1638}
1639
1640
1641/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1642///
1643void Parser::ParseDeclarator(Declarator &D) {
1644 /// This implements the 'declarator' production in the C grammar, then checks
1645 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001646 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001647}
1648
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001649/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1650/// is parsed by the function passed to it. Pass null, and the direct-declarator
1651/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001652/// ptr-operator production.
1653///
Sebastian Redl75555032009-01-24 21:16:55 +00001654/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1655/// [C] pointer[opt] direct-declarator
1656/// [C++] direct-declarator
1657/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001658///
1659/// pointer: [C99 6.7.5]
1660/// '*' type-qualifier-list[opt]
1661/// '*' type-qualifier-list[opt] pointer
1662///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001663/// ptr-operator:
1664/// '*' cv-qualifier-seq[opt]
1665/// '&'
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001666/// [C++0x] '&&'
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001667/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001668/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001669/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001670void Parser::ParseDeclaratorInternal(Declarator &D,
1671 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001672
Sebastian Redl75555032009-01-24 21:16:55 +00001673 // C++ member pointers start with a '::' or a nested-name.
1674 // Member pointers get special handling, since there's no place for the
1675 // scope spec in the generic path below.
Chris Lattner053dd2d2009-03-24 17:04:48 +00001676 if (getLang().CPlusPlus &&
1677 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1678 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl75555032009-01-24 21:16:55 +00001679 CXXScopeSpec SS;
1680 if (ParseOptionalCXXScopeSpecifier(SS)) {
1681 if(Tok.isNot(tok::star)) {
1682 // The scope spec really belongs to the direct-declarator.
1683 D.getCXXScopeSpec() = SS;
1684 if (DirectDeclParser)
1685 (this->*DirectDeclParser)(D);
1686 return;
1687 }
1688
1689 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001690 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00001691 DeclSpec DS;
1692 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001693 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001694
1695 // Recurse to parse whatever is left.
1696 ParseDeclaratorInternal(D, DirectDeclParser);
1697
1698 // Sema will have to catch (syntactically invalid) pointers into global
1699 // scope. It has to catch pointers into namespace scope anyway.
1700 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001701 Loc, DS.TakeAttributes()),
1702 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00001703 return;
1704 }
1705 }
1706
1707 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001708 // Not a pointer, C++ reference, or block.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001709 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner053dd2d2009-03-24 17:04:48 +00001710 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001711 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001712 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001713 if (DirectDeclParser)
1714 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001715 return;
1716 }
Sebastian Redl75555032009-01-24 21:16:55 +00001717
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001718 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1719 // '&&' -> rvalue reference
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001720 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redl0c986032009-02-09 18:23:29 +00001721 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001722
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001723 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner69f01932008-02-21 01:32:26 +00001724 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001725 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001726
Chris Lattner4b009652007-07-25 00:24:17 +00001727 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001728 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001729
Chris Lattner4b009652007-07-25 00:24:17 +00001730 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001731 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001732 if (Kind == tok::star)
1733 // Remember that we parsed a pointer type, and remember the type-quals.
1734 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001735 DS.TakeAttributes()),
1736 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00001737 else
1738 // Remember that we parsed a Block type, and remember the type-quals.
1739 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001740 Loc),
1741 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001742 } else {
1743 // Is a reference
1744 DeclSpec DS;
1745
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001746 // Complain about rvalue references in C++03, but then go on and build
1747 // the declarator.
1748 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1749 Diag(Loc, diag::err_rvalue_reference);
1750
Chris Lattner4b009652007-07-25 00:24:17 +00001751 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1752 // cv-qualifiers are introduced through the use of a typedef or of a
1753 // template type argument, in which case the cv-qualifiers are ignored.
1754 //
1755 // [GNU] Retricted references are allowed.
1756 // [GNU] Attributes on references are allowed.
1757 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001758 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00001759
1760 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1761 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1762 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001763 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001764 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1765 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001766 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001767 }
1768
1769 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001770 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001771
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001772 if (D.getNumTypeObjects() > 0) {
1773 // C++ [dcl.ref]p4: There shall be no references to references.
1774 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1775 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001776 if (const IdentifierInfo *II = D.getIdentifier())
1777 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1778 << II;
1779 else
1780 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1781 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001782
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001783 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001784 // can go ahead and build the (technically ill-formed)
1785 // declarator: reference collapsing will take care of it.
1786 }
1787 }
1788
Chris Lattner4b009652007-07-25 00:24:17 +00001789 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001790 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001791 DS.TakeAttributes(),
1792 Kind == tok::amp),
Sebastian Redl0c986032009-02-09 18:23:29 +00001793 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001794 }
1795}
1796
1797/// ParseDirectDeclarator
1798/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001799/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001800/// '(' declarator ')'
1801/// [GNU] '(' attributes declarator ')'
1802/// [C90] direct-declarator '[' constant-expression[opt] ']'
1803/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1804/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1805/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1806/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1807/// direct-declarator '(' parameter-type-list ')'
1808/// direct-declarator '(' identifier-list[opt] ')'
1809/// [GNU] direct-declarator '(' parameter-forward-declarations
1810/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001811/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1812/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001813/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001814///
1815/// declarator-id: [C++ 8]
1816/// id-expression
1817/// '::'[opt] nested-name-specifier[opt] type-name
1818///
1819/// id-expression: [C++ 5.1]
1820/// unqualified-id
1821/// qualified-id [TODO]
1822///
1823/// unqualified-id: [C++ 5.1]
1824/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001825/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001826/// conversion-function-id [TODO]
1827/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00001828/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001829///
Chris Lattner4b009652007-07-25 00:24:17 +00001830void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001831 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001832
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001833 if (getLang().CPlusPlus) {
1834 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00001835 // ParseDeclaratorInternal might already have parsed the scope.
1836 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1837 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001838 if (afterCXXScope) {
1839 // Change the declaration context for name lookup, until this function
1840 // is exited (and the declarator has been parsed).
1841 DeclScopeObj.EnterDeclaratorScope();
1842 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001843
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001844 if (Tok.is(tok::identifier)) {
1845 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor2fa10442008-12-18 19:37:40 +00001846
Douglas Gregor2fa10442008-12-18 19:37:40 +00001847 // If this identifier is the name of the current class, it's a
1848 // constructor name.
Douglas Gregor0c281a82009-02-25 19:37:18 +00001849 if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)){
Steve Naroff7b36a1b2009-01-28 19:39:02 +00001850 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor1075a162009-02-04 17:00:24 +00001851 Tok.getLocation(), CurScope),
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001852 Tok.getLocation());
Douglas Gregor2fa10442008-12-18 19:37:40 +00001853 // This is a normal identifier.
Sebastian Redl0c986032009-02-09 18:23:29 +00001854 } else
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001855 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1856 ConsumeToken();
1857 goto PastIdentifier;
Douglas Gregor0c281a82009-02-25 19:37:18 +00001858 } else if (Tok.is(tok::annot_template_id)) {
1859 TemplateIdAnnotation *TemplateId
1860 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1861
1862 // FIXME: Could this template-id name a constructor?
1863
1864 // FIXME: This is an egregious hack, where we silently ignore
1865 // the specialization (which should be a function template
1866 // specialization name) and use the name instead. This hack
1867 // will go away when we have support for function
1868 // specializations.
1869 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
1870 TemplateId->Destroy();
1871 ConsumeToken();
1872 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00001873 } else if (Tok.is(tok::kw_operator)) {
1874 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00001875 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00001876
Douglas Gregor853dd392008-12-26 15:00:45 +00001877 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00001878 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
1879 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00001880 } else {
1881 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00001882 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
1883 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
1884 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00001885 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00001886 }
Douglas Gregor853dd392008-12-26 15:00:45 +00001887 }
1888 goto PastIdentifier;
1889 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001890 // This should be a C++ destructor.
1891 SourceLocation TildeLoc = ConsumeToken();
1892 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001893 // FIXME: Inaccurate.
1894 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00001895 SourceLocation EndLoc;
1896 if (TypeTy *Type = ParseClassName(EndLoc)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001897 D.setDestructor(Type, TildeLoc, NameLoc);
1898 } else {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001899 D.SetIdentifier(0, TildeLoc);
Sebastian Redl0c986032009-02-09 18:23:29 +00001900 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001901 } else {
1902 Diag(Tok, diag::err_expected_class_name);
1903 D.SetIdentifier(0, TildeLoc);
1904 }
1905 goto PastIdentifier;
1906 }
1907
1908 // If we reached this point, token is not identifier and not '~'.
1909
1910 if (afterCXXScope) {
1911 Diag(Tok, diag::err_expected_unqualified_id);
1912 D.SetIdentifier(0, Tok.getLocation());
1913 D.setInvalidType(true);
1914 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001915 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001916 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001917 }
1918
1919 // If we reached this point, we are either in C/ObjC or the token didn't
1920 // satisfy any of the C++-specific checks.
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001921 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1922 assert(!getLang().CPlusPlus &&
1923 "There's a C++-specific check for tok::identifier above");
1924 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1925 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1926 ConsumeToken();
1927 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001928 // direct-declarator: '(' declarator ')'
1929 // direct-declarator: '(' attributes declarator ')'
1930 // Example: 'char (*X)' or 'int (*XX)(void)'
1931 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001932 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001933 // This could be something simple like "int" (in which case the declarator
1934 // portion is empty), if an abstract-declarator is allowed.
1935 D.SetIdentifier(0, Tok.getLocation());
1936 } else {
Douglas Gregorf03265d2009-03-06 23:28:18 +00001937 if (D.getContext() == Declarator::MemberContext)
1938 Diag(Tok, diag::err_expected_member_name_or_semi)
1939 << D.getDeclSpec().getSourceRange();
1940 else if (getLang().CPlusPlus)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001941 Diag(Tok, diag::err_expected_unqualified_id);
1942 else
Chris Lattnerf006a222008-11-18 07:48:38 +00001943 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00001944 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00001945 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001946 }
1947
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001948 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00001949 assert(D.isPastIdentifier() &&
1950 "Haven't past the location of the identifier yet?");
1951
1952 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001953 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001954 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1955 // In such a case, check if we actually have a function declarator; if it
1956 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001957 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1958 // When not in file scope, warn for ambiguous function declarators, just
1959 // in case the author intended it as a variable definition.
1960 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1961 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1962 break;
1963 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001964 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001965 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001966 ParseBracketDeclarator(D);
1967 } else {
1968 break;
1969 }
1970 }
1971}
1972
Chris Lattnera0d056d2008-04-06 05:45:57 +00001973/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1974/// only called before the identifier, so these are most likely just grouping
1975/// parens for precedence. If we find that these are actually function
1976/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1977///
1978/// direct-declarator:
1979/// '(' declarator ')'
1980/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001981/// direct-declarator '(' parameter-type-list ')'
1982/// direct-declarator '(' identifier-list[opt] ')'
1983/// [GNU] direct-declarator '(' parameter-forward-declarations
1984/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001985///
1986void Parser::ParseParenDeclarator(Declarator &D) {
1987 SourceLocation StartLoc = ConsumeParen();
1988 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1989
Chris Lattner1f185292008-10-20 02:05:46 +00001990 // Eat any attributes before we look at whether this is a grouping or function
1991 // declarator paren. If this is a grouping paren, the attribute applies to
1992 // the type being built up, for example:
1993 // int (__attribute__(()) *x)(long y)
1994 // If this ends up not being a grouping paren, the attribute applies to the
1995 // first argument, for example:
1996 // int (__attribute__(()) int x)
1997 // In either case, we need to eat any attributes to be able to determine what
1998 // sort of paren this is.
1999 //
2000 AttributeList *AttrList = 0;
2001 bool RequiresArg = false;
2002 if (Tok.is(tok::kw___attribute)) {
2003 AttrList = ParseAttributes();
2004
2005 // We require that the argument list (if this is a non-grouping paren) be
2006 // present even if the attribute list was empty.
2007 RequiresArg = true;
2008 }
Steve Naroffedd04d52008-12-25 14:16:32 +00002009 // Eat any Microsoft extensions.
Douglas Gregore51b7c82009-01-10 00:48:18 +00002010 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2011 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroffedd04d52008-12-25 14:16:32 +00002012 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00002013
Chris Lattnera0d056d2008-04-06 05:45:57 +00002014 // If we haven't past the identifier yet (or where the identifier would be
2015 // stored, if this is an abstract declarator), then this is probably just
2016 // grouping parens. However, if this could be an abstract-declarator, then
2017 // this could also be the start of function arguments (consider 'void()').
2018 bool isGrouping;
2019
2020 if (!D.mayOmitIdentifier()) {
2021 // If this can't be an abstract-declarator, this *must* be a grouping
2022 // paren, because we haven't seen the identifier yet.
2023 isGrouping = true;
2024 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002025 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00002026 isDeclarationSpecifier()) { // 'int(int)' is a function.
2027 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2028 // considered to be a type, not a K&R identifier-list.
2029 isGrouping = false;
2030 } else {
2031 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2032 isGrouping = true;
2033 }
2034
2035 // If this is a grouping paren, handle:
2036 // direct-declarator: '(' declarator ')'
2037 // direct-declarator: '(' attributes declarator ')'
2038 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002039 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002040 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00002041 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00002042 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002043
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002044 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002045 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00002046 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002047
2048 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00002049 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002050 return;
2051 }
2052
2053 // Okay, if this wasn't a grouping paren, it must be the start of a function
2054 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00002055 // identifier (and remember where it would have been), then call into
2056 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00002057 D.SetIdentifier(0, Tok.getLocation());
2058
Chris Lattner1f185292008-10-20 02:05:46 +00002059 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002060}
2061
2062/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2063/// declarator D up to a paren, which indicates that we are parsing function
2064/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00002065///
Chris Lattner1f185292008-10-20 02:05:46 +00002066/// If AttrList is non-null, then the caller parsed those arguments immediately
2067/// after the open paren - they should be considered to be the first argument of
2068/// a parameter. If RequiresArg is true, then the first argument of the
2069/// function is required to be present and required to not be an identifier
2070/// list.
2071///
Chris Lattner4b009652007-07-25 00:24:17 +00002072/// This method also handles this portion of the grammar:
2073/// parameter-type-list: [C99 6.7.5]
2074/// parameter-list
2075/// parameter-list ',' '...'
2076///
2077/// parameter-list: [C99 6.7.5]
2078/// parameter-declaration
2079/// parameter-list ',' parameter-declaration
2080///
2081/// parameter-declaration: [C99 6.7.5]
2082/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00002083/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002084/// [GNU] declaration-specifiers declarator attributes
Sebastian Redla8cecf62009-03-24 22:27:57 +00002085/// declaration-specifiers abstract-declarator[opt]
2086/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002087/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002088/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2089///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002090/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redla8cecf62009-03-24 22:27:57 +00002091/// and "exception-specification[opt]".
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002092///
Chris Lattner1f185292008-10-20 02:05:46 +00002093void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2094 AttributeList *AttrList,
2095 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002096 // lparen is already consumed!
2097 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002098
Chris Lattner1f185292008-10-20 02:05:46 +00002099 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002100 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002101 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002102 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002103 delete AttrList;
2104 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002105
Sebastian Redl0c986032009-02-09 18:23:29 +00002106 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002107
2108 // cv-qualifier-seq[opt].
2109 DeclSpec DS;
2110 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002111 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002112 if (!DS.getSourceRange().getEnd().isInvalid())
2113 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002114
2115 // Parse exception-specification[opt].
2116 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002117 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002118 }
2119
Chris Lattner9f7564b2008-04-06 06:57:35 +00002120 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002121 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002122 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002123 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002124 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002125 /*arglist*/ 0, 0,
2126 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002127 LParenLoc, D),
2128 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002129 return;
Chris Lattner1f185292008-10-20 02:05:46 +00002130 }
2131
2132 // Alternatively, this parameter list may be an identifier list form for a
2133 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002134 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002135 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002136 // K&R identifier lists can't have typedefs as identifiers, per
2137 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002138 if (RequiresArg) {
2139 Diag(Tok, diag::err_argument_required_after_attribute);
2140 delete AttrList;
2141 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002142 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2143 // normal declarators, not for abstract-declarators.
2144 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002145 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002146 }
2147
2148 // Finally, a normal, non-empty parameter type list.
2149
2150 // Build up an array of information about the parsed arguments.
2151 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002152
2153 // Enter function-declaration scope, limiting any declarators to the
2154 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002155 ParseScope PrototypeScope(this,
2156 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002157
2158 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002159 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002160 while (1) {
2161 if (Tok.is(tok::ellipsis)) {
2162 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002163 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002164 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002165 }
2166
Chris Lattner9f7564b2008-04-06 06:57:35 +00002167 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002168
Chris Lattner9f7564b2008-04-06 06:57:35 +00002169 // Parse the declaration-specifiers.
2170 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002171
2172 // If the caller parsed attributes for the first argument, add them now.
2173 if (AttrList) {
2174 DS.AddAttributes(AttrList);
2175 AttrList = 0; // Only apply the attributes to the first parameter.
2176 }
Chris Lattner9e785f52009-02-27 18:38:20 +00002177 ParseDeclarationSpecifiers(DS);
2178
Chris Lattner9f7564b2008-04-06 06:57:35 +00002179 // Parse the declarator. This is "PrototypeContext", because we must
2180 // accept either 'declarator' or 'abstract-declarator' here.
2181 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2182 ParseDeclarator(ParmDecl);
2183
2184 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002185 if (Tok.is(tok::kw___attribute)) {
2186 SourceLocation Loc;
2187 AttributeList *AttrList = ParseAttributes(&Loc);
2188 ParmDecl.AddAttributes(AttrList, Loc);
2189 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002190
Chris Lattner9f7564b2008-04-06 06:57:35 +00002191 // Remember this parsed parameter in ParamInfo.
2192 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2193
Douglas Gregor605de8d2008-12-16 21:30:33 +00002194 // DefArgToks is used when the parsing of default arguments needs
2195 // to be delayed.
2196 CachedTokens *DefArgToks = 0;
2197
Chris Lattner9f7564b2008-04-06 06:57:35 +00002198 // If no parameter was specified, verify that *something* was specified,
2199 // otherwise we have a missing type and identifier.
Chris Lattner9e785f52009-02-27 18:38:20 +00002200 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2201 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002202 // Completely missing, emit error.
2203 Diag(DSStart, diag::err_missing_param);
2204 } else {
2205 // Otherwise, we have something. Add it and let semantic analysis try
2206 // to grok it and add the result to the ParamInfo we are building.
2207
2208 // Inform the actions module about the parameter declarator, so it gets
2209 // added to the current scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002210 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002211
2212 // Parse the default argument, if any. We parse the default
2213 // arguments in all dialects; the semantic analysis in
2214 // ActOnParamDefaultArgument will reject the default argument in
2215 // C.
2216 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002217 SourceLocation EqualLoc = Tok.getLocation();
2218
Chris Lattner3e254fb2008-04-08 04:40:51 +00002219 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002220 if (D.getContext() == Declarator::MemberContext) {
2221 // If we're inside a class definition, cache the tokens
2222 // corresponding to the default argument. We'll actually parse
2223 // them when we see the end of the class definition.
2224 // FIXME: Templates will require something similar.
2225 // FIXME: Can we use a smart pointer for Toks?
2226 DefArgToks = new CachedTokens;
2227
2228 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2229 tok::semi, false)) {
2230 delete DefArgToks;
2231 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002232 Actions.ActOnParamDefaultArgumentError(Param);
2233 } else
2234 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002235 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002236 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002237 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002238
2239 OwningExprResult DefArgResult(ParseAssignmentExpression());
2240 if (DefArgResult.isInvalid()) {
2241 Actions.ActOnParamDefaultArgumentError(Param);
2242 SkipUntil(tok::comma, tok::r_paren, true, true);
2243 } else {
2244 // Inform the actions module about the default argument
2245 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00002246 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +00002247 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002248 }
2249 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002250
2251 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002252 ParmDecl.getIdentifierLoc(), Param,
2253 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002254 }
2255
2256 // If the next token is a comma, consume it and keep reading arguments.
2257 if (Tok.isNot(tok::comma)) break;
2258
2259 // Consume the comma.
2260 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002261 }
2262
Chris Lattner9f7564b2008-04-06 06:57:35 +00002263 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002264 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002265
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002266 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002267 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002268
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002269 DeclSpec DS;
2270 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002271 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002272 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002273 if (!DS.getSourceRange().getEnd().isInvalid())
2274 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002275
2276 // Parse exception-specification[opt].
2277 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002278 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002279 }
2280
Chris Lattner4b009652007-07-25 00:24:17 +00002281 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002282 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002283 EllipsisLoc,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002284 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002285 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002286 LParenLoc, D),
2287 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002288}
2289
Chris Lattner35d9c912008-04-06 06:34:08 +00002290/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2291/// we found a K&R-style identifier list instead of a type argument list. The
2292/// current token is known to be the first identifier in the list.
2293///
2294/// identifier-list: [C99 6.7.5]
2295/// identifier
2296/// identifier-list ',' identifier
2297///
2298void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2299 Declarator &D) {
2300 // Build up an array of information about the parsed arguments.
2301 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2302 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2303
2304 // If there was no identifier specified for the declarator, either we are in
2305 // an abstract-declarator, or we are in a parameter declarator which was found
2306 // to be abstract. In abstract-declarators, identifier lists are not valid:
2307 // diagnose this.
2308 if (!D.getIdentifier())
2309 Diag(Tok, diag::ext_ident_list_in_param);
2310
2311 // Tok is known to be the first identifier in the list. Remember this
2312 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002313 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002314 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner5261d0c2009-03-28 19:18:32 +00002315 Tok.getLocation(),
2316 DeclPtrTy()));
Chris Lattner35d9c912008-04-06 06:34:08 +00002317
Chris Lattner113a56b2008-04-06 06:39:19 +00002318 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002319
2320 while (Tok.is(tok::comma)) {
2321 // Eat the comma.
2322 ConsumeToken();
2323
Chris Lattner113a56b2008-04-06 06:39:19 +00002324 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002325 if (Tok.isNot(tok::identifier)) {
2326 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002327 SkipUntil(tok::r_paren);
2328 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002329 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002330
Chris Lattner35d9c912008-04-06 06:34:08 +00002331 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002332
2333 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002334 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002335 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002336
2337 // Verify that the argument identifier has not already been mentioned.
2338 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002339 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002340 } else {
2341 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002342 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002343 Tok.getLocation(),
2344 DeclPtrTy()));
Chris Lattner113a56b2008-04-06 06:39:19 +00002345 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002346
2347 // Eat the identifier.
2348 ConsumeToken();
2349 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002350
2351 // If we have the closing ')', eat it and we're done.
2352 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2353
Chris Lattner113a56b2008-04-06 06:39:19 +00002354 // Remember that we parsed a function type, and remember the attributes. This
2355 // function type is always a K&R style function type, which is not varargs and
2356 // has no prototype.
2357 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002358 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002359 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002360 /*TypeQuals*/0, LParenLoc, D),
2361 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002362}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002363
Chris Lattner4b009652007-07-25 00:24:17 +00002364/// [C90] direct-declarator '[' constant-expression[opt] ']'
2365/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2366/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2367/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2368/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2369void Parser::ParseBracketDeclarator(Declarator &D) {
2370 SourceLocation StartLoc = ConsumeBracket();
2371
Chris Lattner1525c3a2008-12-18 07:27:21 +00002372 // C array syntax has many features, but by-far the most common is [] and [4].
2373 // This code does a fast path to handle some of the most obvious cases.
2374 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002375 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002376 // Remember that we parsed the empty array type.
2377 OwningExprResult NumElements(Actions);
Sebastian Redl0c986032009-02-09 18:23:29 +00002378 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2379 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002380 return;
2381 } else if (Tok.getKind() == tok::numeric_constant &&
2382 GetLookAheadToken(1).is(tok::r_square)) {
2383 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002384 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002385 ConsumeToken();
2386
Sebastian Redl0c986032009-02-09 18:23:29 +00002387 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002388
2389 // If there was an error parsing the assignment-expression, recover.
2390 if (ExprRes.isInvalid())
2391 ExprRes.release(); // Deallocate expr, just use [].
2392
2393 // Remember that we parsed a array type, and remember its features.
2394 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redl0c986032009-02-09 18:23:29 +00002395 ExprRes.release(), StartLoc),
2396 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002397 return;
2398 }
2399
Chris Lattner4b009652007-07-25 00:24:17 +00002400 // If valid, this location is the position where we read the 'static' keyword.
2401 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002402 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002403 StaticLoc = ConsumeToken();
2404
2405 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002406 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002407 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002408 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002409
2410 // If we haven't already read 'static', check to see if there is one after the
2411 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002412 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002413 StaticLoc = ConsumeToken();
2414
2415 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2416 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002417 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002418
2419 // Handle the case where we have '[*]' as the array size. However, a leading
2420 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2421 // the the token after the star is a ']'. Since stars in arrays are
2422 // infrequent, use of lookahead is not costly here.
2423 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002424 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002425
Chris Lattner306d4df2008-12-18 06:50:14 +00002426 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002427 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002428 StaticLoc = SourceLocation(); // Drop the static.
2429 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002430 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002431 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002432 // Note, in C89, this production uses the constant-expr production instead
2433 // of assignment-expr. The only difference is that assignment-expr allows
2434 // things like '=' and '*='. Sema rejects these in C89 mode because they
2435 // are not i-c-e's, so we don't need to distinguish between the two here.
2436
Chris Lattner4b009652007-07-25 00:24:17 +00002437 // Parse the assignment-expression now.
2438 NumElements = ParseAssignmentExpression();
2439 }
2440
2441 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002442 if (NumElements.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002443 // If the expression was invalid, skip it.
2444 SkipUntil(tok::r_square);
2445 return;
2446 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002447
2448 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2449
Chris Lattner1525c3a2008-12-18 07:27:21 +00002450 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002451 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2452 StaticLoc.isValid(), isStar,
Sebastian Redl0c986032009-02-09 18:23:29 +00002453 NumElements.release(), StartLoc),
2454 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002455}
2456
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002457/// [GNU] typeof-specifier:
2458/// typeof ( expressions )
2459/// typeof ( type-name )
2460/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002461///
2462void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002463 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002464 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002465 SourceLocation StartLoc = ConsumeToken();
2466
Chris Lattner34a01ad2007-10-09 17:33:22 +00002467 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002468 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002469 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002470 return;
2471 }
2472
Sebastian Redl14ca7412008-12-11 21:36:32 +00002473 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002474 if (Result.isInvalid()) {
2475 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002476 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002477 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002478
2479 const char *PrevSpec = 0;
2480 // Check for duplicate type specifiers.
2481 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002482 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002483 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002484
2485 // FIXME: Not accurate, the range gets one token more than it should.
2486 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002487 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002488 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002489
Steve Naroff7cbb1462007-07-31 12:34:36 +00002490 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2491
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002492 if (isTypeIdInParens()) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002493 Action::TypeResult Ty = ParseTypeName();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002494
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002495 assert((Ty.isInvalid() || Ty.get()) &&
2496 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff4c255ab2007-07-31 23:56:32 +00002497
Chris Lattner34a01ad2007-10-09 17:33:22 +00002498 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002499 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002500 return;
2501 }
2502 RParenLoc = ConsumeParen();
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002503
2504 if (Ty.isInvalid())
2505 DS.SetTypeSpecError();
2506 else {
2507 const char *PrevSpec = 0;
2508 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2509 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2510 Ty.get()))
2511 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2512 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00002513 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002514 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002515
2516 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002517 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002518 DS.SetTypeSpecError();
Steve Naroff14bbce82007-08-02 02:53:48 +00002519 return;
2520 }
2521 RParenLoc = ConsumeParen();
2522 const char *PrevSpec = 0;
2523 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2524 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002525 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002526 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002527 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002528 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002529}
2530
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002531