blob: 1f8522f094ce932c94a4f2c259b3645f3f5d64fb [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 Lattnera17991f2009-03-29 16:50:03 +0000260Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context) {
Chris Lattner4b009652007-07-25 00:24:17 +0000261 // Parse the common declaration-specifiers piece.
262 DeclSpec DS;
263 ParseDeclarationSpecifiers(DS);
264
265 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
266 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000267 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000268 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000269 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
270 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000271 }
272
273 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
274 ParseDeclarator(DeclaratorInfo);
275
276 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
277}
278
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000279
Chris Lattner4b009652007-07-25 00:24:17 +0000280/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
281/// parsing 'declaration-specifiers declarator'. This method is split out this
282/// way to handle the ambiguity between top-level function-definitions and
283/// declarations.
284///
Chris Lattner4b009652007-07-25 00:24:17 +0000285/// init-declarator-list: [C99 6.7]
286/// init-declarator
287/// init-declarator-list ',' init-declarator
288/// init-declarator: [C99 6.7]
289/// declarator
290/// declarator '=' initializer
291/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
292/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000293/// [C++] declarator initializer[opt]
294///
295/// [C++] initializer:
296/// [C++] '=' initializer-clause
297/// [C++] '(' expression-list ')'
Sebastian Redla8cecf62009-03-24 22:27:57 +0000298/// [C++0x] '=' 'default' [TODO]
299/// [C++0x] '=' 'delete'
300///
301/// According to the standard grammar, =default and =delete are function
302/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattner4b009652007-07-25 00:24:17 +0000303///
Chris Lattnera17991f2009-03-29 16:50:03 +0000304Parser::DeclGroupPtrTy Parser::
Chris Lattner4b009652007-07-25 00:24:17 +0000305ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000306 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
307 // that we parse together here.
308 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Chris Lattner4b009652007-07-25 00:24:17 +0000309
310 // At this point, we know that it is not a function definition. Parse the
311 // rest of the init-declarator-list.
312 while (1) {
313 // If a simple-asm-expr is present, parse it.
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000314 if (Tok.is(tok::kw_asm)) {
Sebastian Redl0c986032009-02-09 18:23:29 +0000315 SourceLocation Loc;
316 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000317 if (AsmLabel.isInvalid()) {
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000318 SkipUntil(tok::semi);
Chris Lattnera17991f2009-03-29 16:50:03 +0000319 return DeclGroupPtrTy();
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000320 }
Sebastian Redl0c986032009-02-09 18:23:29 +0000321
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000322 D.setAsmLabel(AsmLabel.release());
Sebastian Redl0c986032009-02-09 18:23:29 +0000323 D.SetRangeEnd(Loc);
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000324 }
Chris Lattner4b009652007-07-25 00:24:17 +0000325
326 // If attributes are present, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +0000327 if (Tok.is(tok::kw___attribute)) {
328 SourceLocation Loc;
329 AttributeList *AttrList = ParseAttributes(&Loc);
330 D.AddAttributes(AttrList, Loc);
331 }
Steve Naroff6a0e2092007-09-12 14:07:44 +0000332
333 // Inform the current actions module that we just parsed this declarator.
Chris Lattnera17991f2009-03-29 16:50:03 +0000334 DeclPtrTy ThisDecl = Actions.ActOnDeclarator(CurScope, D);
335 DeclsInGroup.push_back(ThisDecl);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000336
Chris Lattner4b009652007-07-25 00:24:17 +0000337 // Parse declarator '=' initializer.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000338 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000339 ConsumeToken();
Sebastian Redla8cecf62009-03-24 22:27:57 +0000340 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
341 SourceLocation DelLoc = ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000342 Actions.SetDeclDeleted(ThisDecl, DelLoc);
Sebastian Redla8cecf62009-03-24 22:27:57 +0000343 } else {
344 OwningExprResult Init(ParseInitializer());
345 if (Init.isInvalid()) {
346 SkipUntil(tok::semi);
Chris Lattnera17991f2009-03-29 16:50:03 +0000347 return DeclGroupPtrTy();
Sebastian Redla8cecf62009-03-24 22:27:57 +0000348 }
Chris Lattnera17991f2009-03-29 16:50:03 +0000349 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Chris Lattner4b009652007-07-25 00:24:17 +0000350 }
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000351 } else if (Tok.is(tok::l_paren)) {
352 // Parse C++ direct initializer: '(' expression-list ')'
353 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl6008ac32008-11-25 22:21:31 +0000354 ExprVector Exprs(Actions);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000355 CommaLocsTy CommaLocs;
356
357 bool InvalidExpr = false;
358 if (ParseExpressionList(Exprs, CommaLocs)) {
359 SkipUntil(tok::r_paren);
360 InvalidExpr = true;
361 }
362 // Match the ')'.
363 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
364
365 if (!InvalidExpr) {
366 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
367 "Unexpected number of commas!");
Chris Lattnera17991f2009-03-29 16:50:03 +0000368 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +0000369 move_arg(Exprs),
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000370 &CommaLocs[0], RParenLoc);
371 }
Douglas Gregor81c29152008-10-29 00:13:59 +0000372 } else {
Chris Lattnera17991f2009-03-29 16:50:03 +0000373 Actions.ActOnUninitializedDecl(ThisDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000374 }
375
Chris Lattner4b009652007-07-25 00:24:17 +0000376 // If we don't have a comma, it is either the end of the list (a ';') or an
377 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000378 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000379 break;
380
381 // Consume the comma.
382 ConsumeToken();
383
384 // Parse the next declarator.
385 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000386
387 // Accept attributes in an init-declarator. In the first declarator in a
388 // declaration, these would be part of the declspec. In subsequent
389 // declarators, they become part of the declarator itself, so that they
390 // don't apply to declarators after *this* one. Examples:
391 // short __attribute__((common)) var; -> declspec
392 // short var __attribute__((common)); -> declarator
393 // short x, __attribute__((common)) var; -> declarator
Sebastian Redl0c986032009-02-09 18:23:29 +0000394 if (Tok.is(tok::kw___attribute)) {
395 SourceLocation Loc;
396 AttributeList *AttrList = ParseAttributes(&Loc);
397 D.AddAttributes(AttrList, Loc);
398 }
Chris Lattner926cf542008-10-20 04:57:38 +0000399
Chris Lattner4b009652007-07-25 00:24:17 +0000400 ParseDeclarator(D);
401 }
402
Chris Lattner34a01ad2007-10-09 17:33:22 +0000403 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000404 ConsumeToken();
Fariborz Jahanianc1509b02009-01-17 00:00:40 +0000405 // for(is key; in keys) is error.
Chris Lattner7f6c2872009-03-28 06:13:37 +0000406 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanianc1509b02009-01-17 00:00:40 +0000407 Diag(Tok, diag::err_parse_error);
Chris Lattnera17991f2009-03-29 16:50:03 +0000408 return DeclGroupPtrTy();
Fariborz Jahanianc1509b02009-01-17 00:00:40 +0000409 }
Chris Lattnera17991f2009-03-29 16:50:03 +0000410
411 return Actions.FinalizeDeclaratorGroup(CurScope, &DeclsInGroup[0],
412 DeclsInGroup.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000413 }
Chris Lattner7f6c2872009-03-28 06:13:37 +0000414
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000415 // If this is an ObjC2 for-each loop, this is a successful declarator
416 // parse. The syntax for these looks like:
417 // 'for' '(' declaration 'in' expr ')' statement
Chris Lattner7f6c2872009-03-28 06:13:37 +0000418 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in())
Chris Lattnera17991f2009-03-29 16:50:03 +0000419 return Actions.FinalizeDeclaratorGroup(CurScope, &DeclsInGroup[0],
420 DeclsInGroup.size());
Chris Lattner7f6c2872009-03-28 06:13:37 +0000421
Chris Lattner4b009652007-07-25 00:24:17 +0000422 Diag(Tok, diag::err_parse_error);
423 // Skip to end of block or statement
Chris Lattnerf491b412007-08-21 18:36:18 +0000424 SkipUntil(tok::r_brace, true, true);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000425 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000426 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000427 return DeclGroupPtrTy();
Chris Lattner4b009652007-07-25 00:24:17 +0000428}
429
430/// ParseSpecifierQualifierList
431/// specifier-qualifier-list:
432/// type-specifier specifier-qualifier-list[opt]
433/// type-qualifier specifier-qualifier-list[opt]
434/// [GNU] attributes specifier-qualifier-list[opt]
435///
436void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
437 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
438 /// parse declaration-specifiers and complain about extra stuff.
439 ParseDeclarationSpecifiers(DS);
440
441 // Validate declspec for type-name.
442 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000443 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000444 Diag(Tok, diag::err_typename_requires_specqual);
445
446 // Issue diagnostic and remove storage class if present.
447 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
448 if (DS.getStorageClassSpecLoc().isValid())
449 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
450 else
451 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
452 DS.ClearStorageClassSpecs();
453 }
454
455 // Issue diagnostic and remove function specfier if present.
456 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000457 if (DS.isInlineSpecified())
458 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
459 if (DS.isVirtualSpecified())
460 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
461 if (DS.isExplicitSpecified())
462 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000463 DS.ClearFunctionSpecs();
464 }
465}
466
467/// ParseDeclarationSpecifiers
468/// declaration-specifiers: [C99 6.7]
469/// storage-class-specifier declaration-specifiers[opt]
470/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000471/// [C99] function-specifier declaration-specifiers[opt]
472/// [GNU] attributes declaration-specifiers[opt]
473///
474/// storage-class-specifier: [C99 6.7.1]
475/// 'typedef'
476/// 'extern'
477/// 'static'
478/// 'auto'
479/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000480/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000481/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000482/// function-specifier: [C99 6.7.4]
483/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000484/// [C++] 'virtual'
485/// [C++] 'explicit'
Chris Lattner4b009652007-07-25 00:24:17 +0000486///
Douglas Gregor52473432008-12-24 02:52:09 +0000487void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000488 TemplateParameterLists *TemplateParams,
489 AccessSpecifier AS){
Chris Lattnera4ff4272008-03-13 06:29:04 +0000490 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000491 while (1) {
492 int isInvalid = false;
493 const char *PrevSpec = 0;
494 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000495
Chris Lattner4b009652007-07-25 00:24:17 +0000496 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000497 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000498 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000499 // If this is not a declaration specifier token, we're done reading decl
500 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000501 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000502 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000503
504 case tok::coloncolon: // ::foo::bar
505 // Annotate C++ scope specifiers. If we get one, loop.
506 if (TryAnnotateCXXScopeToken())
507 continue;
508 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000509
510 case tok::annot_cxxscope: {
511 if (DS.hasTypeSpecifier())
512 goto DoneWithDeclSpec;
513
514 // We are looking for a qualified typename.
Douglas Gregor80b95c52009-03-25 15:40:00 +0000515 Token Next = NextToken();
516 if (Next.is(tok::annot_template_id) &&
517 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
518 ->Kind == TNK_Class_template) {
519 // We have a qualified template-id, e.g., N::A<int>
520 CXXScopeSpec SS;
521 ParseOptionalCXXScopeSpecifier(SS);
522 assert(Tok.is(tok::annot_template_id) &&
523 "ParseOptionalCXXScopeSpecifier not working");
524 AnnotateTemplateIdTokenAsType(&SS);
525 continue;
526 }
527
528 if (Next.isNot(tok::identifier))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000529 goto DoneWithDeclSpec;
530
531 CXXScopeSpec SS;
Douglas Gregor041e9292009-03-26 23:56:24 +0000532 SS.setScopeRep(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000533 SS.setRange(Tok.getAnnotationRange());
534
535 // If the next token is the name of the class type that the C++ scope
536 // denotes, followed by a '(', then this is a constructor declaration.
537 // We're done with the decl-specifiers.
538 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
539 CurScope, &SS) &&
540 GetLookAheadToken(2).is(tok::l_paren))
541 goto DoneWithDeclSpec;
542
Douglas Gregor1075a162009-02-04 17:00:24 +0000543 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
544 Next.getLocation(), CurScope, &SS);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000545
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000546 if (TypeRep == 0)
547 goto DoneWithDeclSpec;
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000548
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000549 ConsumeToken(); // The C++ scope.
550
Douglas Gregora60c62e2009-02-09 15:09:02 +0000551 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000552 TypeRep);
553 if (isInvalid)
554 break;
555
556 DS.SetRangeEnd(Tok.getLocation());
557 ConsumeToken(); // The typename.
558
559 continue;
560 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000561
562 case tok::annot_typename: {
Douglas Gregora60c62e2009-02-09 15:09:02 +0000563 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerc297b722009-01-21 19:48:37 +0000564 Tok.getAnnotationValue());
565 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
566 ConsumeToken(); // The typename
567
568 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
569 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
570 // Objective-C interface. If we don't have Objective-C or a '<', this is
571 // just a normal reference to a typedef name.
572 if (!Tok.is(tok::less) || !getLang().ObjC1)
573 continue;
574
575 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000576 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnerc297b722009-01-21 19:48:37 +0000577 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
578 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
579
580 DS.SetRangeEnd(EndProtoLoc);
581 continue;
582 }
583
Chris Lattnerfda18db2008-07-26 01:18:38 +0000584 // typedef-name
585 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000586 // In C++, check to see if this is a scope specifier like foo::bar::, if
587 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000588 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
589 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000590
Chris Lattnerfda18db2008-07-26 01:18:38 +0000591 // This identifier can only be a typedef name if we haven't already seen
592 // a type-specifier. Without this check we misparse:
593 // typedef int X; struct Y { short X; }; as 'short int'.
594 if (DS.hasTypeSpecifier())
595 goto DoneWithDeclSpec;
596
597 // It has to be available as a typedef too!
Douglas Gregor1075a162009-02-04 17:00:24 +0000598 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
599 Tok.getLocation(), CurScope);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000600
Chris Lattnerfda18db2008-07-26 01:18:38 +0000601 if (TypeRep == 0)
602 goto DoneWithDeclSpec;
Douglas Gregor8e458f42009-02-09 18:46:07 +0000603
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000604 // C++: If the identifier is actually the name of the class type
605 // being defined and the next token is a '(', then this is a
606 // constructor declaration. We're done with the decl-specifiers
607 // and will treat this token as an identifier.
608 if (getLang().CPlusPlus &&
Douglas Gregorcab994d2009-01-09 22:42:13 +0000609 CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000610 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
611 NextToken().getKind() == tok::l_paren)
612 goto DoneWithDeclSpec;
613
Douglas Gregora60c62e2009-02-09 15:09:02 +0000614 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattnerfda18db2008-07-26 01:18:38 +0000615 TypeRep);
616 if (isInvalid)
617 break;
618
619 DS.SetRangeEnd(Tok.getLocation());
620 ConsumeToken(); // The identifier
621
622 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
623 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
624 // Objective-C interface. If we don't have Objective-C or a '<', this is
625 // just a normal reference to a typedef name.
626 if (!Tok.is(tok::less) || !getLang().ObjC1)
627 continue;
628
629 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000630 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000631 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000632 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000633
634 DS.SetRangeEnd(EndProtoLoc);
635
Steve Narofff7683302008-09-22 10:28:57 +0000636 // Need to support trailing type qualifiers (e.g. "id<p> const").
637 // If a type specifier follows, it will be diagnosed elsewhere.
638 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000639 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000640
641 // type-name
642 case tok::annot_template_id: {
643 TemplateIdAnnotation *TemplateId
644 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
645 if (TemplateId->Kind != TNK_Class_template) {
646 // This template-id does not refer to a type name, so we're
647 // done with the type-specifiers.
648 goto DoneWithDeclSpec;
649 }
650
651 // Turn the template-id annotation token into a type annotation
652 // token, then try again to parse it as a type-specifier.
653 if (AnnotateTemplateIdTokenAsType())
654 DS.SetTypeSpecError();
655
656 continue;
657 }
658
Chris Lattner4b009652007-07-25 00:24:17 +0000659 // GNU attributes support.
660 case tok::kw___attribute:
661 DS.AddAttributes(ParseAttributes());
662 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000663
664 // Microsoft declspec support.
665 case tok::kw___declspec:
666 if (!PP.getLangOptions().Microsoft)
667 goto DoneWithDeclSpec;
668 FuzzyParseMicrosoftDeclSpec();
669 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000670
Steve Naroffedd04d52008-12-25 14:16:32 +0000671 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000672 case tok::kw___forceinline:
673 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000674 case tok::kw___cdecl:
675 case tok::kw___stdcall:
676 case tok::kw___fastcall:
677 if (!PP.getLangOptions().Microsoft)
678 goto DoneWithDeclSpec;
679 // Just ignore it.
680 break;
681
Chris Lattner4b009652007-07-25 00:24:17 +0000682 // storage-class-specifier
683 case tok::kw_typedef:
684 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
685 break;
686 case tok::kw_extern:
687 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000688 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000689 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
690 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000691 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000692 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
693 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000694 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000695 case tok::kw_static:
696 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000697 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000698 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
699 break;
700 case tok::kw_auto:
701 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
702 break;
703 case tok::kw_register:
704 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
705 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000706 case tok::kw_mutable:
707 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
708 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000709 case tok::kw___thread:
710 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
711 break;
712
Chris Lattner4b009652007-07-25 00:24:17 +0000713 continue;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000714
Chris Lattner4b009652007-07-25 00:24:17 +0000715 // function-specifier
716 case tok::kw_inline:
717 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
718 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000719 case tok::kw_virtual:
720 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
721 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000722 case tok::kw_explicit:
723 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
724 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000725
726 // type-specifier
727 case tok::kw_short:
728 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
729 break;
730 case tok::kw_long:
731 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
732 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
733 else
734 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
735 break;
736 case tok::kw_signed:
737 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
738 break;
739 case tok::kw_unsigned:
740 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
741 break;
742 case tok::kw__Complex:
743 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
744 break;
745 case tok::kw__Imaginary:
746 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
747 break;
748 case tok::kw_void:
749 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
750 break;
751 case tok::kw_char:
752 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
753 break;
754 case tok::kw_int:
755 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
756 break;
757 case tok::kw_float:
758 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
759 break;
760 case tok::kw_double:
761 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
762 break;
763 case tok::kw_wchar_t:
764 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
765 break;
766 case tok::kw_bool:
767 case tok::kw__Bool:
768 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
769 break;
770 case tok::kw__Decimal32:
771 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
772 break;
773 case tok::kw__Decimal64:
774 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
775 break;
776 case tok::kw__Decimal128:
777 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
778 break;
779
780 // class-specifier:
781 case tok::kw_class:
782 case tok::kw_struct:
783 case tok::kw_union:
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000784 ParseClassSpecifier(DS, TemplateParams, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000785 continue;
786
787 // enum-specifier:
788 case tok::kw_enum:
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000789 ParseEnumSpecifier(DS, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +0000790 continue;
791
792 // cv-qualifier:
793 case tok::kw_const:
794 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
795 break;
796 case tok::kw_volatile:
797 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
798 getLang())*2;
799 break;
800 case tok::kw_restrict:
801 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
802 getLang())*2;
803 break;
804
Douglas Gregord3022602009-03-27 23:10:48 +0000805 // C++ typename-specifier:
806 case tok::kw_typename:
807 if (TryAnnotateTypeOrScopeToken())
808 continue;
809 break;
810
Chris Lattnerc297b722009-01-21 19:48:37 +0000811 // GNU typeof support.
812 case tok::kw_typeof:
813 ParseTypeofSpecifier(DS);
814 continue;
815
Steve Naroff5f0466b2008-06-05 00:02:44 +0000816 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000817 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000818 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
819 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000820 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000821 goto DoneWithDeclSpec;
822
823 {
824 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000825 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000826 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000827 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000828 DS.SetRangeEnd(EndProtoLoc);
829
Chris Lattnerf006a222008-11-18 07:48:38 +0000830 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
831 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000832 // Need to support trailing type qualifiers (e.g. "id<p> const").
833 // If a type specifier follows, it will be diagnosed elsewhere.
834 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000835 }
Chris Lattner4b009652007-07-25 00:24:17 +0000836 }
837 // If the specifier combination wasn't legal, issue a diagnostic.
838 if (isInvalid) {
839 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000840 // Pick between error or extwarn.
841 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
842 : diag::ext_duplicate_declspec;
843 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000844 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000845 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000846 ConsumeToken();
847 }
848}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000849
Chris Lattnerd706dc82009-01-06 06:59:53 +0000850/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000851/// primarily follow the C++ grammar with additions for C99 and GNU,
852/// which together subsume the C grammar. Note that the C++
853/// type-specifier also includes the C type-qualifier (for const,
854/// volatile, and C99 restrict). Returns true if a type-specifier was
855/// found (and parsed), false otherwise.
856///
857/// type-specifier: [C++ 7.1.5]
858/// simple-type-specifier
859/// class-specifier
860/// enum-specifier
861/// elaborated-type-specifier [TODO]
862/// cv-qualifier
863///
864/// cv-qualifier: [C++ 7.1.5.1]
865/// 'const'
866/// 'volatile'
867/// [C99] 'restrict'
868///
869/// simple-type-specifier: [ C++ 7.1.5.2]
870/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
871/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
872/// 'char'
873/// 'wchar_t'
874/// 'bool'
875/// 'short'
876/// 'int'
877/// 'long'
878/// 'signed'
879/// 'unsigned'
880/// 'float'
881/// 'double'
882/// 'void'
883/// [C99] '_Bool'
884/// [C99] '_Complex'
885/// [C99] '_Imaginary' // Removed in TC2?
886/// [GNU] '_Decimal32'
887/// [GNU] '_Decimal64'
888/// [GNU] '_Decimal128'
889/// [GNU] typeof-specifier
890/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
891/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerd706dc82009-01-06 06:59:53 +0000892bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
893 const char *&PrevSpec,
894 TemplateParameterLists *TemplateParams){
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000895 SourceLocation Loc = Tok.getLocation();
896
897 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +0000898 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +0000899 case tok::kw_typename: // typename foo::bar
Chris Lattnerb75fde62009-01-04 23:41:41 +0000900 // Annotate typenames and C++ scope specifiers. If we get one, just
901 // recurse to handle whatever we get.
902 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000903 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000904 // Otherwise, not a type specifier.
905 return false;
906 case tok::coloncolon: // ::foo::bar
907 if (NextToken().is(tok::kw_new) || // ::new
908 NextToken().is(tok::kw_delete)) // ::delete
909 return false;
910
911 // Annotate typenames and C++ scope specifiers. If we get one, just
912 // recurse to handle whatever we get.
913 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000914 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000915 // Otherwise, not a type specifier.
916 return false;
917
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000918 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +0000919 case tok::annot_typename: {
Douglas Gregora60c62e2009-02-09 15:09:02 +0000920 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000921 Tok.getAnnotationValue());
922 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
923 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000924
925 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
926 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
927 // Objective-C interface. If we don't have Objective-C or a '<', this is
928 // just a normal reference to a typedef name.
929 if (!Tok.is(tok::less) || !getLang().ObjC1)
930 return true;
931
932 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000933 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000934 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
935 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
936
937 DS.SetRangeEnd(EndProtoLoc);
938 return true;
939 }
940
941 case tok::kw_short:
942 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
943 break;
944 case tok::kw_long:
945 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
946 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
947 else
948 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
949 break;
950 case tok::kw_signed:
951 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
952 break;
953 case tok::kw_unsigned:
954 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
955 break;
956 case tok::kw__Complex:
957 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
958 break;
959 case tok::kw__Imaginary:
960 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
961 break;
962 case tok::kw_void:
963 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
964 break;
965 case tok::kw_char:
966 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
967 break;
968 case tok::kw_int:
969 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
970 break;
971 case tok::kw_float:
972 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
973 break;
974 case tok::kw_double:
975 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
976 break;
977 case tok::kw_wchar_t:
978 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
979 break;
980 case tok::kw_bool:
981 case tok::kw__Bool:
982 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
983 break;
984 case tok::kw__Decimal32:
985 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
986 break;
987 case tok::kw__Decimal64:
988 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
989 break;
990 case tok::kw__Decimal128:
991 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
992 break;
993
994 // class-specifier:
995 case tok::kw_class:
996 case tok::kw_struct:
997 case tok::kw_union:
Douglas Gregor52473432008-12-24 02:52:09 +0000998 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000999 return true;
1000
1001 // enum-specifier:
1002 case tok::kw_enum:
1003 ParseEnumSpecifier(DS);
1004 return true;
1005
1006 // cv-qualifier:
1007 case tok::kw_const:
1008 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1009 getLang())*2;
1010 break;
1011 case tok::kw_volatile:
1012 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1013 getLang())*2;
1014 break;
1015 case tok::kw_restrict:
1016 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1017 getLang())*2;
1018 break;
1019
1020 // GNU typeof support.
1021 case tok::kw_typeof:
1022 ParseTypeofSpecifier(DS);
1023 return true;
1024
Steve Naroffedd04d52008-12-25 14:16:32 +00001025 case tok::kw___cdecl:
1026 case tok::kw___stdcall:
1027 case tok::kw___fastcall:
Chris Lattner5bb837e2009-01-21 19:19:26 +00001028 if (!PP.getLangOptions().Microsoft) return false;
1029 ConsumeToken();
1030 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +00001031
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001032 default:
1033 // Not a type-specifier; do nothing.
1034 return false;
1035 }
1036
1037 // If the specifier combination wasn't legal, issue a diagnostic.
1038 if (isInvalid) {
1039 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001040 // Pick between error or extwarn.
1041 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1042 : diag::ext_duplicate_declspec;
1043 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001044 }
1045 DS.SetRangeEnd(Tok.getLocation());
1046 ConsumeToken(); // whatever we parsed above.
1047 return true;
1048}
Chris Lattner4b009652007-07-25 00:24:17 +00001049
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001050/// ParseStructDeclaration - Parse a struct declaration without the terminating
1051/// semicolon.
1052///
Chris Lattner4b009652007-07-25 00:24:17 +00001053/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001054/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001055/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001056/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +00001057/// struct-declarator-list:
1058/// struct-declarator
1059/// struct-declarator-list ',' struct-declarator
1060/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1061/// struct-declarator:
1062/// declarator
1063/// [GNU] declarator attributes[opt]
1064/// declarator[opt] ':' constant-expression
1065/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1066///
Chris Lattner3dd8d392008-04-10 06:46:29 +00001067void Parser::
1068ParseStructDeclaration(DeclSpec &DS,
1069 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001070 if (Tok.is(tok::kw___extension__)) {
1071 // __extension__ silences extension warnings in the subexpression.
1072 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001073 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001074 return ParseStructDeclaration(DS, Fields);
1075 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001076
1077 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001078 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001079 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001080
Douglas Gregorb748fc52009-01-12 22:49:06 +00001081 // If there are no declarators, this is a free-standing declaration
1082 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001083 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001084 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001085 return;
1086 }
1087
1088 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001089 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001090 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001091 FieldDeclarator &DeclaratorInfo = Fields.back();
1092
Steve Naroffa9adf112007-08-20 22:28:22 +00001093 /// struct-declarator: declarator
1094 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001095 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001096 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001097
Chris Lattner34a01ad2007-10-09 17:33:22 +00001098 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001099 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001100 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001101 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001102 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001103 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001104 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001105 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001106
Steve Naroffa9adf112007-08-20 22:28:22 +00001107 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001108 if (Tok.is(tok::kw___attribute)) {
1109 SourceLocation Loc;
1110 AttributeList *AttrList = ParseAttributes(&Loc);
1111 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1112 }
1113
Steve Naroffa9adf112007-08-20 22:28:22 +00001114 // If we don't have a comma, it is either the end of the list (a ';')
1115 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001116 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001117 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001118
Steve Naroffa9adf112007-08-20 22:28:22 +00001119 // Consume the comma.
1120 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001121
Steve Naroffa9adf112007-08-20 22:28:22 +00001122 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001123 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001124
Steve Naroffa9adf112007-08-20 22:28:22 +00001125 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001126 if (Tok.is(tok::kw___attribute)) {
1127 SourceLocation Loc;
1128 AttributeList *AttrList = ParseAttributes(&Loc);
1129 Fields.back().D.AddAttributes(AttrList, Loc);
1130 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001131 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001132}
1133
1134/// ParseStructUnionBody
1135/// struct-contents:
1136/// struct-declaration-list
1137/// [EXT] empty
1138/// [GNU] "struct-declaration-list" without terminatoring ';'
1139/// struct-declaration-list:
1140/// struct-declaration
1141/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001142/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001143///
Chris Lattner4b009652007-07-25 00:24:17 +00001144void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001145 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001146 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1147 PP.getSourceManager(),
1148 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001149
Chris Lattner4b009652007-07-25 00:24:17 +00001150 SourceLocation LBraceLoc = ConsumeBrace();
1151
Douglas Gregorcab994d2009-01-09 22:42:13 +00001152 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001153 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1154
Chris Lattner4b009652007-07-25 00:24:17 +00001155 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1156 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001157 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001158 Diag(Tok, diag::ext_empty_struct_union_enum)
1159 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001160
Chris Lattner5261d0c2009-03-28 19:18:32 +00001161 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001162 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1163
Chris Lattner4b009652007-07-25 00:24:17 +00001164 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001165 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001166 // Each iteration of this loop reads one struct-declaration.
1167
1168 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001169 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001170 Diag(Tok, diag::ext_extra_struct_semi);
1171 ConsumeToken();
1172 continue;
1173 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001174
1175 // Parse all the comma separated declarators.
1176 DeclSpec DS;
1177 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001178 if (!Tok.is(tok::at)) {
1179 ParseStructDeclaration(DS, FieldDeclarators);
1180
1181 // Convert them all to fields.
1182 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1183 FieldDeclarator &FD = FieldDeclarators[i];
1184 // Install the declarator into the current TagDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001185 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1186 DS.getSourceRange().getBegin(),
1187 FD.D, FD.BitfieldSize);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001188 FieldDecls.push_back(Field);
1189 }
1190 } else { // Handle @defs
1191 ConsumeToken();
1192 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1193 Diag(Tok, diag::err_unexpected_at);
1194 SkipUntil(tok::semi, true, true);
1195 continue;
1196 }
1197 ConsumeToken();
1198 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1199 if (!Tok.is(tok::identifier)) {
1200 Diag(Tok, diag::err_expected_ident);
1201 SkipUntil(tok::semi, true, true);
1202 continue;
1203 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001204 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001205 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1206 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001207 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1208 ConsumeToken();
1209 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1210 }
Chris Lattner4b009652007-07-25 00:24:17 +00001211
Chris Lattner34a01ad2007-10-09 17:33:22 +00001212 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001213 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001214 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001215 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001216 break;
1217 } else {
1218 Diag(Tok, diag::err_expected_semi_decl_list);
1219 // Skip to end of block or statement
1220 SkipUntil(tok::r_brace, true, true);
1221 }
1222 }
1223
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001224 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001225
Chris Lattner4b009652007-07-25 00:24:17 +00001226 AttributeList *AttrList = 0;
1227 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001228 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001229 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001230
1231 Actions.ActOnFields(CurScope,
1232 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1233 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001234 AttrList);
1235 StructScope.Exit();
1236 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001237}
1238
1239
1240/// ParseEnumSpecifier
1241/// enum-specifier: [C99 6.7.2.2]
1242/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001243///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001244/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1245/// '}' attributes[opt]
1246/// 'enum' identifier
1247/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001248///
1249/// [C++] elaborated-type-specifier:
1250/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1251///
Douglas Gregor0c793bb2009-03-25 22:00:53 +00001252void Parser::ParseEnumSpecifier(DeclSpec &DS, AccessSpecifier AS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001253 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +00001254 SourceLocation StartLoc = ConsumeToken();
1255
1256 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001257
1258 AttributeList *Attr = 0;
1259 // If attributes exist after tag, parse them.
1260 if (Tok.is(tok::kw___attribute))
1261 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001262
1263 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001264 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001265 if (Tok.isNot(tok::identifier)) {
1266 Diag(Tok, diag::err_expected_ident);
1267 if (Tok.isNot(tok::l_brace)) {
1268 // Has no name and is not a definition.
1269 // Skip the rest of this declarator, up until the comma or semicolon.
1270 SkipUntil(tok::comma, true);
1271 return;
1272 }
1273 }
1274 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001275
1276 // Must have either 'enum name' or 'enum {...}'.
1277 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1278 Diag(Tok, diag::err_expected_ident_lbrace);
1279
1280 // Skip the rest of this declarator, up until the comma or semicolon.
1281 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001282 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001283 }
1284
1285 // If an identifier is present, consume and remember it.
1286 IdentifierInfo *Name = 0;
1287 SourceLocation NameLoc;
1288 if (Tok.is(tok::identifier)) {
1289 Name = Tok.getIdentifierInfo();
1290 NameLoc = ConsumeToken();
1291 }
1292
1293 // There are three options here. If we have 'enum foo;', then this is a
1294 // forward declaration. If we have 'enum foo {...' then this is a
1295 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1296 //
1297 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1298 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1299 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1300 //
1301 Action::TagKind TK;
1302 if (Tok.is(tok::l_brace))
1303 TK = Action::TK_Definition;
1304 else if (Tok.is(tok::semi))
1305 TK = Action::TK_Declaration;
1306 else
1307 TK = Action::TK_Reference;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001308 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
1309 StartLoc, SS, Name, NameLoc, Attr, AS);
Chris Lattner4b009652007-07-25 00:24:17 +00001310
Chris Lattner34a01ad2007-10-09 17:33:22 +00001311 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001312 ParseEnumBody(StartLoc, TagDecl);
1313
1314 // TODO: semantic analysis on the declspec for enums.
1315 const char *PrevSpec = 0;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001316 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
1317 TagDecl.getAs<void>()))
Chris Lattnerf006a222008-11-18 07:48:38 +00001318 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001319}
1320
1321/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1322/// enumerator-list:
1323/// enumerator
1324/// enumerator-list ',' enumerator
1325/// enumerator:
1326/// enumeration-constant
1327/// enumeration-constant '=' constant-expression
1328/// enumeration-constant:
1329/// identifier
1330///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001331void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001332 // Enter the scope of the enum body and start the definition.
1333 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001334 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001335
Chris Lattner4b009652007-07-25 00:24:17 +00001336 SourceLocation LBraceLoc = ConsumeBrace();
1337
Chris Lattnerc9a92452007-08-27 17:24:30 +00001338 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001339 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001340 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001341
Chris Lattner5261d0c2009-03-28 19:18:32 +00001342 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattner4b009652007-07-25 00:24:17 +00001343
Chris Lattner5261d0c2009-03-28 19:18:32 +00001344 DeclPtrTy LastEnumConstDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001345
1346 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001347 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001348 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1349 SourceLocation IdentLoc = ConsumeToken();
1350
1351 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001352 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001353 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001354 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001355 AssignedVal = ParseConstantExpression();
1356 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001357 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001358 }
1359
1360 // Install the enumerator constant into EnumDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001361 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1362 LastEnumConstDecl,
1363 IdentLoc, Ident,
1364 EqualLoc,
1365 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001366 EnumConstantDecls.push_back(EnumConstDecl);
1367 LastEnumConstDecl = EnumConstDecl;
1368
Chris Lattner34a01ad2007-10-09 17:33:22 +00001369 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001370 break;
1371 SourceLocation CommaLoc = ConsumeToken();
1372
Chris Lattner34a01ad2007-10-09 17:33:22 +00001373 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +00001374 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1375 }
1376
1377 // Eat the }.
1378 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1379
Steve Naroff0acc9c92007-09-15 18:49:24 +00001380 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001381 EnumConstantDecls.size());
1382
Chris Lattner5261d0c2009-03-28 19:18:32 +00001383 Action::AttrTy *AttrList = 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001384 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001385 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001386 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001387
1388 EnumScope.Exit();
1389 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001390}
1391
1392/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001393/// start of a type-qualifier-list.
1394bool Parser::isTypeQualifier() const {
1395 switch (Tok.getKind()) {
1396 default: return false;
1397 // type-qualifier
1398 case tok::kw_const:
1399 case tok::kw_volatile:
1400 case tok::kw_restrict:
1401 return true;
1402 }
1403}
1404
1405/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001406/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001407bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001408 switch (Tok.getKind()) {
1409 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001410
1411 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001412 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001413 // Annotate typenames and C++ scope specifiers. If we get one, just
1414 // recurse to handle whatever we get.
1415 if (TryAnnotateTypeOrScopeToken())
1416 return isTypeSpecifierQualifier();
1417 // Otherwise, not a type specifier.
1418 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001419
Chris Lattnerb75fde62009-01-04 23:41:41 +00001420 case tok::coloncolon: // ::foo::bar
1421 if (NextToken().is(tok::kw_new) || // ::new
1422 NextToken().is(tok::kw_delete)) // ::delete
1423 return false;
1424
1425 // Annotate typenames and C++ scope specifiers. If we get one, just
1426 // recurse to handle whatever we get.
1427 if (TryAnnotateTypeOrScopeToken())
1428 return isTypeSpecifierQualifier();
1429 // Otherwise, not a type specifier.
1430 return false;
1431
Chris Lattner4b009652007-07-25 00:24:17 +00001432 // GNU attributes support.
1433 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001434 // GNU typeof support.
1435 case tok::kw_typeof:
1436
Chris Lattner4b009652007-07-25 00:24:17 +00001437 // type-specifiers
1438 case tok::kw_short:
1439 case tok::kw_long:
1440 case tok::kw_signed:
1441 case tok::kw_unsigned:
1442 case tok::kw__Complex:
1443 case tok::kw__Imaginary:
1444 case tok::kw_void:
1445 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001446 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001447 case tok::kw_int:
1448 case tok::kw_float:
1449 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001450 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001451 case tok::kw__Bool:
1452 case tok::kw__Decimal32:
1453 case tok::kw__Decimal64:
1454 case tok::kw__Decimal128:
1455
Chris Lattner2e78db32008-04-13 18:59:07 +00001456 // struct-or-union-specifier (C99) or class-specifier (C++)
1457 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001458 case tok::kw_struct:
1459 case tok::kw_union:
1460 // enum-specifier
1461 case tok::kw_enum:
1462
1463 // type-qualifier
1464 case tok::kw_const:
1465 case tok::kw_volatile:
1466 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001467
1468 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001469 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001470 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001471
1472 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1473 case tok::less:
1474 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001475
1476 case tok::kw___cdecl:
1477 case tok::kw___stdcall:
1478 case tok::kw___fastcall:
1479 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001480 }
1481}
1482
1483/// isDeclarationSpecifier() - Return true if the current token is part of a
1484/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001485bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001486 switch (Tok.getKind()) {
1487 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001488
1489 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-03-09 21:12:44 +00001490 // Unfortunate hack to support "Class.factoryMethod" notation.
1491 if (getLang().ObjC1 && NextToken().is(tok::period))
1492 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001493 // Fall through
Steve Naroff73ec9322009-03-09 21:12:44 +00001494
Douglas Gregord3022602009-03-27 23:10:48 +00001495 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001496 // Annotate typenames and C++ scope specifiers. If we get one, just
1497 // recurse to handle whatever we get.
1498 if (TryAnnotateTypeOrScopeToken())
1499 return isDeclarationSpecifier();
1500 // Otherwise, not a declaration specifier.
1501 return false;
1502 case tok::coloncolon: // ::foo::bar
1503 if (NextToken().is(tok::kw_new) || // ::new
1504 NextToken().is(tok::kw_delete)) // ::delete
1505 return false;
1506
1507 // Annotate typenames and C++ scope specifiers. If we get one, just
1508 // recurse to handle whatever we get.
1509 if (TryAnnotateTypeOrScopeToken())
1510 return isDeclarationSpecifier();
1511 // Otherwise, not a declaration specifier.
1512 return false;
1513
Chris Lattner4b009652007-07-25 00:24:17 +00001514 // storage-class-specifier
1515 case tok::kw_typedef:
1516 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001517 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001518 case tok::kw_static:
1519 case tok::kw_auto:
1520 case tok::kw_register:
1521 case tok::kw___thread:
1522
1523 // type-specifiers
1524 case tok::kw_short:
1525 case tok::kw_long:
1526 case tok::kw_signed:
1527 case tok::kw_unsigned:
1528 case tok::kw__Complex:
1529 case tok::kw__Imaginary:
1530 case tok::kw_void:
1531 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001532 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001533 case tok::kw_int:
1534 case tok::kw_float:
1535 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001536 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001537 case tok::kw__Bool:
1538 case tok::kw__Decimal32:
1539 case tok::kw__Decimal64:
1540 case tok::kw__Decimal128:
1541
Chris Lattner2e78db32008-04-13 18:59:07 +00001542 // struct-or-union-specifier (C99) or class-specifier (C++)
1543 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001544 case tok::kw_struct:
1545 case tok::kw_union:
1546 // enum-specifier
1547 case tok::kw_enum:
1548
1549 // type-qualifier
1550 case tok::kw_const:
1551 case tok::kw_volatile:
1552 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001553
Chris Lattner4b009652007-07-25 00:24:17 +00001554 // function-specifier
1555 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001556 case tok::kw_virtual:
1557 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001558
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001559 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001560 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001561
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001562 // GNU typeof support.
1563 case tok::kw_typeof:
1564
1565 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001566 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001567 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001568
1569 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1570 case tok::less:
1571 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001572
Steve Naroffab1a3632009-01-06 19:34:12 +00001573 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001574 case tok::kw___cdecl:
1575 case tok::kw___stdcall:
1576 case tok::kw___fastcall:
1577 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001578 }
1579}
1580
1581
1582/// ParseTypeQualifierListOpt
1583/// type-qualifier-list: [C99 6.7.5]
1584/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001585/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001586/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001587/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001588///
Chris Lattner460696f2008-12-18 07:02:59 +00001589void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001590 while (1) {
1591 int isInvalid = false;
1592 const char *PrevSpec = 0;
1593 SourceLocation Loc = Tok.getLocation();
1594
1595 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001596 case tok::kw_const:
1597 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1598 getLang())*2;
1599 break;
1600 case tok::kw_volatile:
1601 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1602 getLang())*2;
1603 break;
1604 case tok::kw_restrict:
1605 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1606 getLang())*2;
1607 break;
Steve Naroffad620402008-12-25 14:41:26 +00001608 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001609 case tok::kw___cdecl:
1610 case tok::kw___stdcall:
1611 case tok::kw___fastcall:
1612 if (!PP.getLangOptions().Microsoft)
1613 goto DoneWithTypeQuals;
1614 // Just ignore it.
1615 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001616 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001617 if (AttributesAllowed) {
1618 DS.AddAttributes(ParseAttributes());
1619 continue; // do *not* consume the next token!
1620 }
1621 // otherwise, FALL THROUGH!
1622 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001623 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001624 // If this is not a type-qualifier token, we're done reading type
1625 // qualifiers. First verify that DeclSpec's are consistent.
1626 DS.Finish(Diags, PP.getSourceManager(), getLang());
1627 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001628 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001629
Chris Lattner4b009652007-07-25 00:24:17 +00001630 // If the specifier combination wasn't legal, issue a diagnostic.
1631 if (isInvalid) {
1632 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001633 // Pick between error or extwarn.
1634 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1635 : diag::ext_duplicate_declspec;
1636 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001637 }
1638 ConsumeToken();
1639 }
1640}
1641
1642
1643/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1644///
1645void Parser::ParseDeclarator(Declarator &D) {
1646 /// This implements the 'declarator' production in the C grammar, then checks
1647 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001648 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001649}
1650
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001651/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1652/// is parsed by the function passed to it. Pass null, and the direct-declarator
1653/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001654/// ptr-operator production.
1655///
Sebastian Redl75555032009-01-24 21:16:55 +00001656/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1657/// [C] pointer[opt] direct-declarator
1658/// [C++] direct-declarator
1659/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001660///
1661/// pointer: [C99 6.7.5]
1662/// '*' type-qualifier-list[opt]
1663/// '*' type-qualifier-list[opt] pointer
1664///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001665/// ptr-operator:
1666/// '*' cv-qualifier-seq[opt]
1667/// '&'
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001668/// [C++0x] '&&'
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001669/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001670/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001671/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001672void Parser::ParseDeclaratorInternal(Declarator &D,
1673 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001674
Sebastian Redl75555032009-01-24 21:16:55 +00001675 // C++ member pointers start with a '::' or a nested-name.
1676 // Member pointers get special handling, since there's no place for the
1677 // scope spec in the generic path below.
Chris Lattner053dd2d2009-03-24 17:04:48 +00001678 if (getLang().CPlusPlus &&
1679 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1680 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl75555032009-01-24 21:16:55 +00001681 CXXScopeSpec SS;
1682 if (ParseOptionalCXXScopeSpecifier(SS)) {
1683 if(Tok.isNot(tok::star)) {
1684 // The scope spec really belongs to the direct-declarator.
1685 D.getCXXScopeSpec() = SS;
1686 if (DirectDeclParser)
1687 (this->*DirectDeclParser)(D);
1688 return;
1689 }
1690
1691 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001692 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00001693 DeclSpec DS;
1694 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001695 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001696
1697 // Recurse to parse whatever is left.
1698 ParseDeclaratorInternal(D, DirectDeclParser);
1699
1700 // Sema will have to catch (syntactically invalid) pointers into global
1701 // scope. It has to catch pointers into namespace scope anyway.
1702 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001703 Loc, DS.TakeAttributes()),
1704 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00001705 return;
1706 }
1707 }
1708
1709 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001710 // Not a pointer, C++ reference, or block.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001711 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner053dd2d2009-03-24 17:04:48 +00001712 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001713 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001714 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001715 if (DirectDeclParser)
1716 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001717 return;
1718 }
Sebastian Redl75555032009-01-24 21:16:55 +00001719
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001720 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1721 // '&&' -> rvalue reference
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001722 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redl0c986032009-02-09 18:23:29 +00001723 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00001724
Chris Lattnerc14c7f02009-03-27 04:18:06 +00001725 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner69f01932008-02-21 01:32:26 +00001726 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001727 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001728
Chris Lattner4b009652007-07-25 00:24:17 +00001729 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001730 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001731
Chris Lattner4b009652007-07-25 00:24:17 +00001732 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001733 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001734 if (Kind == tok::star)
1735 // Remember that we parsed a pointer type, and remember the type-quals.
1736 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00001737 DS.TakeAttributes()),
1738 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00001739 else
1740 // Remember that we parsed a Block type, and remember the type-quals.
1741 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00001742 Loc),
1743 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001744 } else {
1745 // Is a reference
1746 DeclSpec DS;
1747
Sebastian Redl4e67adb2009-03-23 00:00:23 +00001748 // Complain about rvalue references in C++03, but then go on and build
1749 // the declarator.
1750 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1751 Diag(Loc, diag::err_rvalue_reference);
1752
Chris Lattner4b009652007-07-25 00:24:17 +00001753 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1754 // cv-qualifiers are introduced through the use of a typedef or of a
1755 // template type argument, in which case the cv-qualifiers are ignored.
1756 //
1757 // [GNU] Retricted references are allowed.
1758 // [GNU] Attributes on references are allowed.
1759 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00001760 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00001761
1762 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1763 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1764 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001765 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001766 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1767 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001768 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001769 }
1770
1771 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001772 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001773
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001774 if (D.getNumTypeObjects() > 0) {
1775 // C++ [dcl.ref]p4: There shall be no references to references.
1776 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1777 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001778 if (const IdentifierInfo *II = D.getIdentifier())
1779 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1780 << II;
1781 else
1782 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1783 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001784
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001785 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001786 // can go ahead and build the (technically ill-formed)
1787 // declarator: reference collapsing will take care of it.
1788 }
1789 }
1790
Chris Lattner4b009652007-07-25 00:24:17 +00001791 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001792 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl9951dbc2009-03-15 22:02:01 +00001793 DS.TakeAttributes(),
1794 Kind == tok::amp),
Sebastian Redl0c986032009-02-09 18:23:29 +00001795 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001796 }
1797}
1798
1799/// ParseDirectDeclarator
1800/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001801/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001802/// '(' declarator ')'
1803/// [GNU] '(' attributes declarator ')'
1804/// [C90] direct-declarator '[' constant-expression[opt] ']'
1805/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1806/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1807/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1808/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1809/// direct-declarator '(' parameter-type-list ')'
1810/// direct-declarator '(' identifier-list[opt] ')'
1811/// [GNU] direct-declarator '(' parameter-forward-declarations
1812/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001813/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1814/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001815/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001816///
1817/// declarator-id: [C++ 8]
1818/// id-expression
1819/// '::'[opt] nested-name-specifier[opt] type-name
1820///
1821/// id-expression: [C++ 5.1]
1822/// unqualified-id
1823/// qualified-id [TODO]
1824///
1825/// unqualified-id: [C++ 5.1]
1826/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001827/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001828/// conversion-function-id [TODO]
1829/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00001830/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001831///
Chris Lattner4b009652007-07-25 00:24:17 +00001832void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001833 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001834
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001835 if (getLang().CPlusPlus) {
1836 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00001837 // ParseDeclaratorInternal might already have parsed the scope.
1838 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1839 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001840 if (afterCXXScope) {
1841 // Change the declaration context for name lookup, until this function
1842 // is exited (and the declarator has been parsed).
1843 DeclScopeObj.EnterDeclaratorScope();
1844 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001845
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001846 if (Tok.is(tok::identifier)) {
1847 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor2fa10442008-12-18 19:37:40 +00001848
Douglas Gregor2fa10442008-12-18 19:37:40 +00001849 // If this identifier is the name of the current class, it's a
1850 // constructor name.
Douglas Gregor0c281a82009-02-25 19:37:18 +00001851 if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)){
Steve Naroff7b36a1b2009-01-28 19:39:02 +00001852 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor1075a162009-02-04 17:00:24 +00001853 Tok.getLocation(), CurScope),
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001854 Tok.getLocation());
Douglas Gregor2fa10442008-12-18 19:37:40 +00001855 // This is a normal identifier.
Sebastian Redl0c986032009-02-09 18:23:29 +00001856 } else
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001857 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1858 ConsumeToken();
1859 goto PastIdentifier;
Douglas Gregor0c281a82009-02-25 19:37:18 +00001860 } else if (Tok.is(tok::annot_template_id)) {
1861 TemplateIdAnnotation *TemplateId
1862 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1863
1864 // FIXME: Could this template-id name a constructor?
1865
1866 // FIXME: This is an egregious hack, where we silently ignore
1867 // the specialization (which should be a function template
1868 // specialization name) and use the name instead. This hack
1869 // will go away when we have support for function
1870 // specializations.
1871 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
1872 TemplateId->Destroy();
1873 ConsumeToken();
1874 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00001875 } else if (Tok.is(tok::kw_operator)) {
1876 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00001877 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00001878
Douglas Gregor853dd392008-12-26 15:00:45 +00001879 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00001880 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
1881 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00001882 } else {
1883 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00001884 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
1885 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
1886 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00001887 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00001888 }
Douglas Gregor853dd392008-12-26 15:00:45 +00001889 }
1890 goto PastIdentifier;
1891 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001892 // This should be a C++ destructor.
1893 SourceLocation TildeLoc = ConsumeToken();
1894 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001895 // FIXME: Inaccurate.
1896 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00001897 SourceLocation EndLoc;
1898 if (TypeTy *Type = ParseClassName(EndLoc)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00001899 D.setDestructor(Type, TildeLoc, NameLoc);
1900 } else {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001901 D.SetIdentifier(0, TildeLoc);
Sebastian Redl0c986032009-02-09 18:23:29 +00001902 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001903 } else {
1904 Diag(Tok, diag::err_expected_class_name);
1905 D.SetIdentifier(0, TildeLoc);
1906 }
1907 goto PastIdentifier;
1908 }
1909
1910 // If we reached this point, token is not identifier and not '~'.
1911
1912 if (afterCXXScope) {
1913 Diag(Tok, diag::err_expected_unqualified_id);
1914 D.SetIdentifier(0, Tok.getLocation());
1915 D.setInvalidType(true);
1916 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001917 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001918 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001919 }
1920
1921 // If we reached this point, we are either in C/ObjC or the token didn't
1922 // satisfy any of the C++-specific checks.
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001923 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1924 assert(!getLang().CPlusPlus &&
1925 "There's a C++-specific check for tok::identifier above");
1926 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1927 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1928 ConsumeToken();
1929 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001930 // direct-declarator: '(' declarator ')'
1931 // direct-declarator: '(' attributes declarator ')'
1932 // Example: 'char (*X)' or 'int (*XX)(void)'
1933 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001934 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001935 // This could be something simple like "int" (in which case the declarator
1936 // portion is empty), if an abstract-declarator is allowed.
1937 D.SetIdentifier(0, Tok.getLocation());
1938 } else {
Douglas Gregorf03265d2009-03-06 23:28:18 +00001939 if (D.getContext() == Declarator::MemberContext)
1940 Diag(Tok, diag::err_expected_member_name_or_semi)
1941 << D.getDeclSpec().getSourceRange();
1942 else if (getLang().CPlusPlus)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001943 Diag(Tok, diag::err_expected_unqualified_id);
1944 else
Chris Lattnerf006a222008-11-18 07:48:38 +00001945 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00001946 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00001947 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001948 }
1949
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001950 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00001951 assert(D.isPastIdentifier() &&
1952 "Haven't past the location of the identifier yet?");
1953
1954 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001955 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001956 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1957 // In such a case, check if we actually have a function declarator; if it
1958 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001959 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1960 // When not in file scope, warn for ambiguous function declarators, just
1961 // in case the author intended it as a variable definition.
1962 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1963 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1964 break;
1965 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001966 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001967 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001968 ParseBracketDeclarator(D);
1969 } else {
1970 break;
1971 }
1972 }
1973}
1974
Chris Lattnera0d056d2008-04-06 05:45:57 +00001975/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1976/// only called before the identifier, so these are most likely just grouping
1977/// parens for precedence. If we find that these are actually function
1978/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1979///
1980/// direct-declarator:
1981/// '(' declarator ')'
1982/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001983/// direct-declarator '(' parameter-type-list ')'
1984/// direct-declarator '(' identifier-list[opt] ')'
1985/// [GNU] direct-declarator '(' parameter-forward-declarations
1986/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001987///
1988void Parser::ParseParenDeclarator(Declarator &D) {
1989 SourceLocation StartLoc = ConsumeParen();
1990 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1991
Chris Lattner1f185292008-10-20 02:05:46 +00001992 // Eat any attributes before we look at whether this is a grouping or function
1993 // declarator paren. If this is a grouping paren, the attribute applies to
1994 // the type being built up, for example:
1995 // int (__attribute__(()) *x)(long y)
1996 // If this ends up not being a grouping paren, the attribute applies to the
1997 // first argument, for example:
1998 // int (__attribute__(()) int x)
1999 // In either case, we need to eat any attributes to be able to determine what
2000 // sort of paren this is.
2001 //
2002 AttributeList *AttrList = 0;
2003 bool RequiresArg = false;
2004 if (Tok.is(tok::kw___attribute)) {
2005 AttrList = ParseAttributes();
2006
2007 // We require that the argument list (if this is a non-grouping paren) be
2008 // present even if the attribute list was empty.
2009 RequiresArg = true;
2010 }
Steve Naroffedd04d52008-12-25 14:16:32 +00002011 // Eat any Microsoft extensions.
Douglas Gregore51b7c82009-01-10 00:48:18 +00002012 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2013 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroffedd04d52008-12-25 14:16:32 +00002014 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00002015
Chris Lattnera0d056d2008-04-06 05:45:57 +00002016 // If we haven't past the identifier yet (or where the identifier would be
2017 // stored, if this is an abstract declarator), then this is probably just
2018 // grouping parens. However, if this could be an abstract-declarator, then
2019 // this could also be the start of function arguments (consider 'void()').
2020 bool isGrouping;
2021
2022 if (!D.mayOmitIdentifier()) {
2023 // If this can't be an abstract-declarator, this *must* be a grouping
2024 // paren, because we haven't seen the identifier yet.
2025 isGrouping = true;
2026 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002027 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00002028 isDeclarationSpecifier()) { // 'int(int)' is a function.
2029 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2030 // considered to be a type, not a K&R identifier-list.
2031 isGrouping = false;
2032 } else {
2033 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2034 isGrouping = true;
2035 }
2036
2037 // If this is a grouping paren, handle:
2038 // direct-declarator: '(' declarator ')'
2039 // direct-declarator: '(' attributes declarator ')'
2040 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002041 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002042 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00002043 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00002044 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002045
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002046 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002047 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00002048 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002049
2050 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00002051 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002052 return;
2053 }
2054
2055 // Okay, if this wasn't a grouping paren, it must be the start of a function
2056 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00002057 // identifier (and remember where it would have been), then call into
2058 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00002059 D.SetIdentifier(0, Tok.getLocation());
2060
Chris Lattner1f185292008-10-20 02:05:46 +00002061 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002062}
2063
2064/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2065/// declarator D up to a paren, which indicates that we are parsing function
2066/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00002067///
Chris Lattner1f185292008-10-20 02:05:46 +00002068/// If AttrList is non-null, then the caller parsed those arguments immediately
2069/// after the open paren - they should be considered to be the first argument of
2070/// a parameter. If RequiresArg is true, then the first argument of the
2071/// function is required to be present and required to not be an identifier
2072/// list.
2073///
Chris Lattner4b009652007-07-25 00:24:17 +00002074/// This method also handles this portion of the grammar:
2075/// parameter-type-list: [C99 6.7.5]
2076/// parameter-list
2077/// parameter-list ',' '...'
2078///
2079/// parameter-list: [C99 6.7.5]
2080/// parameter-declaration
2081/// parameter-list ',' parameter-declaration
2082///
2083/// parameter-declaration: [C99 6.7.5]
2084/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00002085/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002086/// [GNU] declaration-specifiers declarator attributes
Sebastian Redla8cecf62009-03-24 22:27:57 +00002087/// declaration-specifiers abstract-declarator[opt]
2088/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002089/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002090/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2091///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002092/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redla8cecf62009-03-24 22:27:57 +00002093/// and "exception-specification[opt]".
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002094///
Chris Lattner1f185292008-10-20 02:05:46 +00002095void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2096 AttributeList *AttrList,
2097 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002098 // lparen is already consumed!
2099 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002100
Chris Lattner1f185292008-10-20 02:05:46 +00002101 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002102 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002103 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002104 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002105 delete AttrList;
2106 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002107
Sebastian Redl0c986032009-02-09 18:23:29 +00002108 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002109
2110 // cv-qualifier-seq[opt].
2111 DeclSpec DS;
2112 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002113 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002114 if (!DS.getSourceRange().getEnd().isInvalid())
2115 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002116
2117 // Parse exception-specification[opt].
2118 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002119 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002120 }
2121
Chris Lattner9f7564b2008-04-06 06:57:35 +00002122 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002123 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002124 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002125 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002126 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002127 /*arglist*/ 0, 0,
2128 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002129 LParenLoc, D),
2130 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002131 return;
Chris Lattner1f185292008-10-20 02:05:46 +00002132 }
2133
2134 // Alternatively, this parameter list may be an identifier list form for a
2135 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002136 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002137 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002138 // K&R identifier lists can't have typedefs as identifiers, per
2139 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002140 if (RequiresArg) {
2141 Diag(Tok, diag::err_argument_required_after_attribute);
2142 delete AttrList;
2143 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002144 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2145 // normal declarators, not for abstract-declarators.
2146 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002147 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002148 }
2149
2150 // Finally, a normal, non-empty parameter type list.
2151
2152 // Build up an array of information about the parsed arguments.
2153 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002154
2155 // Enter function-declaration scope, limiting any declarators to the
2156 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002157 ParseScope PrototypeScope(this,
2158 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002159
2160 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002161 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002162 while (1) {
2163 if (Tok.is(tok::ellipsis)) {
2164 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002165 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002166 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002167 }
2168
Chris Lattner9f7564b2008-04-06 06:57:35 +00002169 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002170
Chris Lattner9f7564b2008-04-06 06:57:35 +00002171 // Parse the declaration-specifiers.
2172 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002173
2174 // If the caller parsed attributes for the first argument, add them now.
2175 if (AttrList) {
2176 DS.AddAttributes(AttrList);
2177 AttrList = 0; // Only apply the attributes to the first parameter.
2178 }
Chris Lattner9e785f52009-02-27 18:38:20 +00002179 ParseDeclarationSpecifiers(DS);
2180
Chris Lattner9f7564b2008-04-06 06:57:35 +00002181 // Parse the declarator. This is "PrototypeContext", because we must
2182 // accept either 'declarator' or 'abstract-declarator' here.
2183 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2184 ParseDeclarator(ParmDecl);
2185
2186 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002187 if (Tok.is(tok::kw___attribute)) {
2188 SourceLocation Loc;
2189 AttributeList *AttrList = ParseAttributes(&Loc);
2190 ParmDecl.AddAttributes(AttrList, Loc);
2191 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002192
Chris Lattner9f7564b2008-04-06 06:57:35 +00002193 // Remember this parsed parameter in ParamInfo.
2194 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2195
Douglas Gregor605de8d2008-12-16 21:30:33 +00002196 // DefArgToks is used when the parsing of default arguments needs
2197 // to be delayed.
2198 CachedTokens *DefArgToks = 0;
2199
Chris Lattner9f7564b2008-04-06 06:57:35 +00002200 // If no parameter was specified, verify that *something* was specified,
2201 // otherwise we have a missing type and identifier.
Chris Lattner9e785f52009-02-27 18:38:20 +00002202 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2203 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002204 // Completely missing, emit error.
2205 Diag(DSStart, diag::err_missing_param);
2206 } else {
2207 // Otherwise, we have something. Add it and let semantic analysis try
2208 // to grok it and add the result to the ParamInfo we are building.
2209
2210 // Inform the actions module about the parameter declarator, so it gets
2211 // added to the current scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002212 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002213
2214 // Parse the default argument, if any. We parse the default
2215 // arguments in all dialects; the semantic analysis in
2216 // ActOnParamDefaultArgument will reject the default argument in
2217 // C.
2218 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002219 SourceLocation EqualLoc = Tok.getLocation();
2220
Chris Lattner3e254fb2008-04-08 04:40:51 +00002221 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002222 if (D.getContext() == Declarator::MemberContext) {
2223 // If we're inside a class definition, cache the tokens
2224 // corresponding to the default argument. We'll actually parse
2225 // them when we see the end of the class definition.
2226 // FIXME: Templates will require something similar.
2227 // FIXME: Can we use a smart pointer for Toks?
2228 DefArgToks = new CachedTokens;
2229
2230 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2231 tok::semi, false)) {
2232 delete DefArgToks;
2233 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002234 Actions.ActOnParamDefaultArgumentError(Param);
2235 } else
2236 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002237 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002238 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002239 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002240
2241 OwningExprResult DefArgResult(ParseAssignmentExpression());
2242 if (DefArgResult.isInvalid()) {
2243 Actions.ActOnParamDefaultArgumentError(Param);
2244 SkipUntil(tok::comma, tok::r_paren, true, true);
2245 } else {
2246 // Inform the actions module about the default argument
2247 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00002248 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +00002249 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002250 }
2251 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002252
2253 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002254 ParmDecl.getIdentifierLoc(), Param,
2255 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002256 }
2257
2258 // If the next token is a comma, consume it and keep reading arguments.
2259 if (Tok.isNot(tok::comma)) break;
2260
2261 // Consume the comma.
2262 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002263 }
2264
Chris Lattner9f7564b2008-04-06 06:57:35 +00002265 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002266 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002267
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002268 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002269 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002270
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002271 DeclSpec DS;
2272 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002273 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002274 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002275 if (!DS.getSourceRange().getEnd().isInvalid())
2276 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002277
2278 // Parse exception-specification[opt].
2279 if (Tok.is(tok::kw_throw))
Sebastian Redl0c986032009-02-09 18:23:29 +00002280 ParseExceptionSpecification(Loc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002281 }
2282
Chris Lattner4b009652007-07-25 00:24:17 +00002283 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002284 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002285 EllipsisLoc,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002286 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002287 DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002288 LParenLoc, D),
2289 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002290}
2291
Chris Lattner35d9c912008-04-06 06:34:08 +00002292/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2293/// we found a K&R-style identifier list instead of a type argument list. The
2294/// current token is known to be the first identifier in the list.
2295///
2296/// identifier-list: [C99 6.7.5]
2297/// identifier
2298/// identifier-list ',' identifier
2299///
2300void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2301 Declarator &D) {
2302 // Build up an array of information about the parsed arguments.
2303 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2304 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2305
2306 // If there was no identifier specified for the declarator, either we are in
2307 // an abstract-declarator, or we are in a parameter declarator which was found
2308 // to be abstract. In abstract-declarators, identifier lists are not valid:
2309 // diagnose this.
2310 if (!D.getIdentifier())
2311 Diag(Tok, diag::ext_ident_list_in_param);
2312
2313 // Tok is known to be the first identifier in the list. Remember this
2314 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002315 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002316 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner5261d0c2009-03-28 19:18:32 +00002317 Tok.getLocation(),
2318 DeclPtrTy()));
Chris Lattner35d9c912008-04-06 06:34:08 +00002319
Chris Lattner113a56b2008-04-06 06:39:19 +00002320 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002321
2322 while (Tok.is(tok::comma)) {
2323 // Eat the comma.
2324 ConsumeToken();
2325
Chris Lattner113a56b2008-04-06 06:39:19 +00002326 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002327 if (Tok.isNot(tok::identifier)) {
2328 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002329 SkipUntil(tok::r_paren);
2330 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002331 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002332
Chris Lattner35d9c912008-04-06 06:34:08 +00002333 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002334
2335 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002336 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002337 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002338
2339 // Verify that the argument identifier has not already been mentioned.
2340 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002341 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002342 } else {
2343 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002344 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002345 Tok.getLocation(),
2346 DeclPtrTy()));
Chris Lattner113a56b2008-04-06 06:39:19 +00002347 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002348
2349 // Eat the identifier.
2350 ConsumeToken();
2351 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002352
2353 // If we have the closing ')', eat it and we're done.
2354 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2355
Chris Lattner113a56b2008-04-06 06:39:19 +00002356 // Remember that we parsed a function type, and remember the attributes. This
2357 // function type is always a K&R style function type, which is not varargs and
2358 // has no prototype.
2359 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002360 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002361 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002362 /*TypeQuals*/0, LParenLoc, D),
2363 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002364}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002365
Chris Lattner4b009652007-07-25 00:24:17 +00002366/// [C90] direct-declarator '[' constant-expression[opt] ']'
2367/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2368/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2369/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2370/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2371void Parser::ParseBracketDeclarator(Declarator &D) {
2372 SourceLocation StartLoc = ConsumeBracket();
2373
Chris Lattner1525c3a2008-12-18 07:27:21 +00002374 // C array syntax has many features, but by-far the most common is [] and [4].
2375 // This code does a fast path to handle some of the most obvious cases.
2376 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002377 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002378 // Remember that we parsed the empty array type.
2379 OwningExprResult NumElements(Actions);
Sebastian Redl0c986032009-02-09 18:23:29 +00002380 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2381 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002382 return;
2383 } else if (Tok.getKind() == tok::numeric_constant &&
2384 GetLookAheadToken(1).is(tok::r_square)) {
2385 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002386 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002387 ConsumeToken();
2388
Sebastian Redl0c986032009-02-09 18:23:29 +00002389 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002390
2391 // If there was an error parsing the assignment-expression, recover.
2392 if (ExprRes.isInvalid())
2393 ExprRes.release(); // Deallocate expr, just use [].
2394
2395 // Remember that we parsed a array type, and remember its features.
2396 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redl0c986032009-02-09 18:23:29 +00002397 ExprRes.release(), StartLoc),
2398 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002399 return;
2400 }
2401
Chris Lattner4b009652007-07-25 00:24:17 +00002402 // If valid, this location is the position where we read the 'static' keyword.
2403 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002404 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002405 StaticLoc = ConsumeToken();
2406
2407 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002408 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002409 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002410 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002411
2412 // If we haven't already read 'static', check to see if there is one after the
2413 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002414 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002415 StaticLoc = ConsumeToken();
2416
2417 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2418 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002419 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002420
2421 // Handle the case where we have '[*]' as the array size. However, a leading
2422 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2423 // the the token after the star is a ']'. Since stars in arrays are
2424 // infrequent, use of lookahead is not costly here.
2425 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002426 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002427
Chris Lattner306d4df2008-12-18 06:50:14 +00002428 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002429 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002430 StaticLoc = SourceLocation(); // Drop the static.
2431 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002432 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002433 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002434 // Note, in C89, this production uses the constant-expr production instead
2435 // of assignment-expr. The only difference is that assignment-expr allows
2436 // things like '=' and '*='. Sema rejects these in C89 mode because they
2437 // are not i-c-e's, so we don't need to distinguish between the two here.
2438
Chris Lattner4b009652007-07-25 00:24:17 +00002439 // Parse the assignment-expression now.
2440 NumElements = ParseAssignmentExpression();
2441 }
2442
2443 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002444 if (NumElements.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002445 // If the expression was invalid, skip it.
2446 SkipUntil(tok::r_square);
2447 return;
2448 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002449
2450 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2451
Chris Lattner1525c3a2008-12-18 07:27:21 +00002452 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002453 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2454 StaticLoc.isValid(), isStar,
Sebastian Redl0c986032009-02-09 18:23:29 +00002455 NumElements.release(), StartLoc),
2456 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002457}
2458
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002459/// [GNU] typeof-specifier:
2460/// typeof ( expressions )
2461/// typeof ( type-name )
2462/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002463///
2464void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002465 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002466 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002467 SourceLocation StartLoc = ConsumeToken();
2468
Chris Lattner34a01ad2007-10-09 17:33:22 +00002469 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002470 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002471 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002472 return;
2473 }
2474
Sebastian Redl14ca7412008-12-11 21:36:32 +00002475 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002476 if (Result.isInvalid()) {
2477 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002478 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002479 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002480
2481 const char *PrevSpec = 0;
2482 // Check for duplicate type specifiers.
2483 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002484 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002485 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002486
2487 // FIXME: Not accurate, the range gets one token more than it should.
2488 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002489 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002490 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002491
Steve Naroff7cbb1462007-07-31 12:34:36 +00002492 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2493
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002494 if (isTypeIdInParens()) {
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002495 Action::TypeResult Ty = ParseTypeName();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002496
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002497 assert((Ty.isInvalid() || Ty.get()) &&
2498 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff4c255ab2007-07-31 23:56:32 +00002499
Chris Lattner34a01ad2007-10-09 17:33:22 +00002500 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002501 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002502 return;
2503 }
2504 RParenLoc = ConsumeParen();
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002505
2506 if (Ty.isInvalid())
2507 DS.SetTypeSpecError();
2508 else {
2509 const char *PrevSpec = 0;
2510 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2511 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2512 Ty.get()))
2513 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2514 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00002515 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002516 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002517
2518 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002519 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002520 DS.SetTypeSpecError();
Steve Naroff14bbce82007-08-02 02:53:48 +00002521 return;
2522 }
2523 RParenLoc = ConsumeParen();
2524 const char *PrevSpec = 0;
2525 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2526 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002527 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002528 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002529 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002530 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002531}
2532
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002533