blob: 8d4705a7bf21a4ec0e7ed15c57f4c253e9e23333 [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"
Daniel Dunbarcc7b1602008-08-11 03:45:03 +000015#include "clang/Basic/Diagnostic.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++.
Sebastian Redl66df3ef2008-12-02 14:43:59 +000031Parser::TypeTy *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
Sebastian Redl66df3ef2008-12-02 14:43:59 +000040 return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
Chris Lattner4b009652007-07-25 00:24:17 +000041}
42
43/// ParseAttributes - Parse a non-empty attributes list.
44///
45/// [GNU] attributes:
46/// attribute
47/// attributes attribute
48///
49/// [GNU] attribute:
50/// '__attribute__' '(' '(' attribute-list ')' ')'
51///
52/// [GNU] attribute-list:
53/// attrib
54/// attribute_list ',' attrib
55///
56/// [GNU] attrib:
57/// empty
58/// attrib-name
59/// attrib-name '(' identifier ')'
60/// attrib-name '(' identifier ',' nonempty-expr-list ')'
61/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
62///
63/// [GNU] attrib-name:
64/// identifier
65/// typespec
66/// typequal
67/// storageclass
68///
69/// FIXME: The GCC grammar/code for this construct implies we need two
70/// token lookahead. Comment from gcc: "If they start with an identifier
71/// which is followed by a comma or close parenthesis, then the arguments
72/// start with that identifier; otherwise they are an expression list."
73///
74/// At the moment, I am not doing 2 token lookahead. I am also unaware of
75/// any attributes that don't work (based on my limited testing). Most
76/// attributes are very simple in practice. Until we find a bug, I don't see
77/// a pressing need to implement the 2 token lookahead.
78
79AttributeList *Parser::ParseAttributes() {
Chris Lattner34a01ad2007-10-09 17:33:22 +000080 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Chris Lattner4b009652007-07-25 00:24:17 +000081
82 AttributeList *CurrAttr = 0;
83
Chris Lattner34a01ad2007-10-09 17:33:22 +000084 while (Tok.is(tok::kw___attribute)) {
Chris Lattner4b009652007-07-25 00:24:17 +000085 ConsumeToken();
86 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
87 "attribute")) {
88 SkipUntil(tok::r_paren, true); // skip until ) or ;
89 return CurrAttr;
90 }
91 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
92 SkipUntil(tok::r_paren, true); // skip until ) or ;
93 return CurrAttr;
94 }
95 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner34a01ad2007-10-09 17:33:22 +000096 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
97 Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +000098
Chris Lattner34a01ad2007-10-09 17:33:22 +000099 if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000100 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
101 ConsumeToken();
102 continue;
103 }
104 // we have an identifier or declaration specifier (const, int, etc.)
105 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
106 SourceLocation AttrNameLoc = ConsumeToken();
107
108 // check if we have a "paramterized" attribute
Chris Lattner34a01ad2007-10-09 17:33:22 +0000109 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000110 ConsumeParen(); // ignore the left paren loc for now
111
Chris Lattner34a01ad2007-10-09 17:33:22 +0000112 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000113 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
114 SourceLocation ParmLoc = ConsumeToken();
115
Chris Lattner34a01ad2007-10-09 17:33:22 +0000116 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000117 // __attribute__(( mode(byte) ))
118 ConsumeParen(); // ignore the right paren loc for now
119 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
120 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000121 } else if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000122 ConsumeToken();
123 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000124 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000125 bool ArgExprsOk = true;
126
127 // now parse the non-empty comma separated list of expressions
128 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000129 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000130 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000131 ArgExprsOk = false;
132 SkipUntil(tok::r_paren);
133 break;
134 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000135 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000136 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000137 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000138 break;
139 ConsumeToken(); // Eat the comma, move to the next argument
140 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000141 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000142 ConsumeParen(); // ignore the right paren loc for now
143 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redl6008ac32008-11-25 22:21:31 +0000144 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Chris Lattner4b009652007-07-25 00:24:17 +0000145 }
146 }
147 } else { // not an identifier
148 // parse a possibly empty comma separated list of expressions
Chris Lattner34a01ad2007-10-09 17:33:22 +0000149 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000150 // __attribute__(( nonnull() ))
151 ConsumeParen(); // ignore the right paren loc for now
152 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
153 0, SourceLocation(), 0, 0, CurrAttr);
154 } else {
155 // __attribute__(( aligned(16) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000156 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000157 bool ArgExprsOk = true;
158
159 // now parse the list of expressions
160 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000161 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000162 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000163 ArgExprsOk = false;
164 SkipUntil(tok::r_paren);
165 break;
166 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000167 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000168 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000169 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000170 break;
171 ConsumeToken(); // Eat the comma, move to the next argument
172 }
173 // Match the ')'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000174 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000175 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl6008ac32008-11-25 22:21:31 +0000176 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
177 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Chris Lattner4b009652007-07-25 00:24:17 +0000178 CurrAttr);
179 }
180 }
181 }
182 } else {
183 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
184 0, SourceLocation(), 0, 0, CurrAttr);
185 }
186 }
187 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
188 SkipUntil(tok::r_paren, false);
189 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
190 SkipUntil(tok::r_paren, false);
191 }
192 return CurrAttr;
193}
194
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000195/// FuzzyParseMicrosoftDeclSpec. When -fms-extensions is enabled, this
196/// routine is called to skip/ignore tokens that comprise the MS declspec.
197void Parser::FuzzyParseMicrosoftDeclSpec() {
198 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
199 ConsumeToken();
200 if (Tok.is(tok::l_paren)) {
201 unsigned short savedParenCount = ParenCount;
202 do {
203 ConsumeAnyToken();
204 } while (ParenCount > savedParenCount && Tok.isNot(tok::eof));
205 }
206 return;
207}
208
Chris Lattner4b009652007-07-25 00:24:17 +0000209/// ParseDeclaration - Parse a full 'declaration', which consists of
210/// declaration-specifiers, some number of declarators, and a semicolon.
211/// 'Context' should be a Declarator::TheContext value.
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000212///
213/// declaration: [C99 6.7]
214/// block-declaration ->
215/// simple-declaration
216/// others [FIXME]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000217/// [C++] template-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000218/// [C++] namespace-definition
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000219/// [C++] using-directive
220/// [C++] using-declaration [TODO]
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000221/// others... [FIXME]
222///
Chris Lattner4b009652007-07-25 00:24:17 +0000223Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000224 switch (Tok.getKind()) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000225 case tok::kw_export:
226 case tok::kw_template:
227 return ParseTemplateDeclaration(Context);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000228 case tok::kw_namespace:
229 return ParseNamespace(Context);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000230 case tok::kw_using:
231 return ParseUsingDirectiveOrDeclaration(Context);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000232 default:
233 return ParseSimpleDeclaration(Context);
234 }
235}
236
237/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
238/// declaration-specifiers init-declarator-list[opt] ';'
239///[C90/C++]init-declarator-list ';' [TODO]
240/// [OMP] threadprivate-directive [TODO]
241Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Chris Lattner4b009652007-07-25 00:24:17 +0000242 // Parse the common declaration-specifiers piece.
243 DeclSpec DS;
244 ParseDeclarationSpecifiers(DS);
245
246 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
247 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000248 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000249 ConsumeToken();
250 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
251 }
252
253 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
254 ParseDeclarator(DeclaratorInfo);
255
256 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
257}
258
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000259
Chris Lattner4b009652007-07-25 00:24:17 +0000260/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
261/// parsing 'declaration-specifiers declarator'. This method is split out this
262/// way to handle the ambiguity between top-level function-definitions and
263/// declarations.
264///
Chris Lattner4b009652007-07-25 00:24:17 +0000265/// init-declarator-list: [C99 6.7]
266/// init-declarator
267/// init-declarator-list ',' init-declarator
268/// init-declarator: [C99 6.7]
269/// declarator
270/// declarator '=' initializer
271/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
272/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000273/// [C++] declarator initializer[opt]
274///
275/// [C++] initializer:
276/// [C++] '=' initializer-clause
277/// [C++] '(' expression-list ')'
Chris Lattner4b009652007-07-25 00:24:17 +0000278///
279Parser::DeclTy *Parser::
280ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
281
282 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
283 // that they can be chained properly if the actions want this.
284 Parser::DeclTy *LastDeclInGroup = 0;
285
286 // At this point, we know that it is not a function definition. Parse the
287 // rest of the init-declarator-list.
288 while (1) {
289 // If a simple-asm-expr is present, parse it.
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000290 if (Tok.is(tok::kw_asm)) {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000291 OwningExprResult AsmLabel(ParseSimpleAsm());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000292 if (AsmLabel.isInvalid()) {
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000293 SkipUntil(tok::semi);
294 return 0;
295 }
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000296
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000297 D.setAsmLabel(AsmLabel.release());
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000298 }
Chris Lattner4b009652007-07-25 00:24:17 +0000299
300 // If attributes are present, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000301 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000302 D.AddAttributes(ParseAttributes());
Steve Naroff6a0e2092007-09-12 14:07:44 +0000303
304 // Inform the current actions module that we just parsed this declarator.
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000305 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000306
Chris Lattner4b009652007-07-25 00:24:17 +0000307 // Parse declarator '=' initializer.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000308 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000309 ConsumeToken();
Sebastian Redl39d4f022008-12-11 22:51:44 +0000310 OwningExprResult Init(ParseInitializer());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000311 if (Init.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000312 SkipUntil(tok::semi);
313 return 0;
314 }
Sebastian Redl91f9b0a2008-12-13 16:23:55 +0000315 Actions.AddInitializerToDecl(LastDeclInGroup, move_convert(Init));
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000316 } else if (Tok.is(tok::l_paren)) {
317 // Parse C++ direct initializer: '(' expression-list ')'
318 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl6008ac32008-11-25 22:21:31 +0000319 ExprVector Exprs(Actions);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000320 CommaLocsTy CommaLocs;
321
322 bool InvalidExpr = false;
323 if (ParseExpressionList(Exprs, CommaLocs)) {
324 SkipUntil(tok::r_paren);
325 InvalidExpr = true;
326 }
327 // Match the ')'.
328 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
329
330 if (!InvalidExpr) {
331 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
332 "Unexpected number of commas!");
333 Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc,
Sebastian Redl6008ac32008-11-25 22:21:31 +0000334 Exprs.take(), Exprs.size(),
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000335 &CommaLocs[0], RParenLoc);
336 }
Douglas Gregor81c29152008-10-29 00:13:59 +0000337 } else {
338 Actions.ActOnUninitializedDecl(LastDeclInGroup);
Chris Lattner4b009652007-07-25 00:24:17 +0000339 }
340
Chris Lattner4b009652007-07-25 00:24:17 +0000341 // If we don't have a comma, it is either the end of the list (a ';') or an
342 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000343 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000344 break;
345
346 // Consume the comma.
347 ConsumeToken();
348
349 // Parse the next declarator.
350 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000351
352 // Accept attributes in an init-declarator. In the first declarator in a
353 // declaration, these would be part of the declspec. In subsequent
354 // declarators, they become part of the declarator itself, so that they
355 // don't apply to declarators after *this* one. Examples:
356 // short __attribute__((common)) var; -> declspec
357 // short var __attribute__((common)); -> declarator
358 // short x, __attribute__((common)) var; -> declarator
359 if (Tok.is(tok::kw___attribute))
360 D.AddAttributes(ParseAttributes());
361
Chris Lattner4b009652007-07-25 00:24:17 +0000362 ParseDeclarator(D);
363 }
364
Chris Lattner34a01ad2007-10-09 17:33:22 +0000365 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000366 ConsumeToken();
367 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
368 }
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000369 // If this is an ObjC2 for-each loop, this is a successful declarator
370 // parse. The syntax for these looks like:
371 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000372 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000373 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
374 }
Chris Lattner4b009652007-07-25 00:24:17 +0000375 Diag(Tok, diag::err_parse_error);
376 // Skip to end of block or statement
Chris Lattnerf491b412007-08-21 18:36:18 +0000377 SkipUntil(tok::r_brace, true, true);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000378 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000379 ConsumeToken();
380 return 0;
381}
382
383/// ParseSpecifierQualifierList
384/// specifier-qualifier-list:
385/// type-specifier specifier-qualifier-list[opt]
386/// type-qualifier specifier-qualifier-list[opt]
387/// [GNU] attributes specifier-qualifier-list[opt]
388///
389void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
390 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
391 /// parse declaration-specifiers and complain about extra stuff.
392 ParseDeclarationSpecifiers(DS);
393
394 // Validate declspec for type-name.
395 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000396 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000397 Diag(Tok, diag::err_typename_requires_specqual);
398
399 // Issue diagnostic and remove storage class if present.
400 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
401 if (DS.getStorageClassSpecLoc().isValid())
402 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
403 else
404 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
405 DS.ClearStorageClassSpecs();
406 }
407
408 // Issue diagnostic and remove function specfier if present.
409 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000410 if (DS.isInlineSpecified())
411 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
412 if (DS.isVirtualSpecified())
413 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
414 if (DS.isExplicitSpecified())
415 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000416 DS.ClearFunctionSpecs();
417 }
418}
419
420/// ParseDeclarationSpecifiers
421/// declaration-specifiers: [C99 6.7]
422/// storage-class-specifier declaration-specifiers[opt]
423/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000424/// [C99] function-specifier declaration-specifiers[opt]
425/// [GNU] attributes declaration-specifiers[opt]
426///
427/// storage-class-specifier: [C99 6.7.1]
428/// 'typedef'
429/// 'extern'
430/// 'static'
431/// 'auto'
432/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000433/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000434/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000435/// function-specifier: [C99 6.7.4]
436/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000437/// [C++] 'virtual'
438/// [C++] 'explicit'
Chris Lattner4b009652007-07-25 00:24:17 +0000439///
Douglas Gregor52473432008-12-24 02:52:09 +0000440void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Chris Lattner712f9a32009-01-05 00:07:25 +0000441 TemplateParameterLists *TemplateParams){
Chris Lattnera4ff4272008-03-13 06:29:04 +0000442 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000443 while (1) {
444 int isInvalid = false;
445 const char *PrevSpec = 0;
446 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000447
Chris Lattner4b009652007-07-25 00:24:17 +0000448 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000449 default:
Douglas Gregorb3bec712008-12-01 23:54:00 +0000450 // Try to parse a type-specifier; if we found one, continue. If it's not
451 // a type, this falls through.
Chris Lattnerd706dc82009-01-06 06:59:53 +0000452 if (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateParams))
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000453 continue;
454
Chris Lattnerb99d7492008-07-26 00:20:22 +0000455 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000456 // If this is not a declaration specifier token, we're done reading decl
457 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000458 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000459 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000460
461 case tok::coloncolon: // ::foo::bar
462 // Annotate C++ scope specifiers. If we get one, loop.
463 if (TryAnnotateCXXScopeToken())
464 continue;
465 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000466
467 case tok::annot_cxxscope: {
468 if (DS.hasTypeSpecifier())
469 goto DoneWithDeclSpec;
470
471 // We are looking for a qualified typename.
472 if (NextToken().isNot(tok::identifier))
473 goto DoneWithDeclSpec;
474
475 CXXScopeSpec SS;
476 SS.setScopeRep(Tok.getAnnotationValue());
477 SS.setRange(Tok.getAnnotationRange());
478
479 // If the next token is the name of the class type that the C++ scope
480 // denotes, followed by a '(', then this is a constructor declaration.
481 // We're done with the decl-specifiers.
482 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
483 CurScope, &SS) &&
484 GetLookAheadToken(2).is(tok::l_paren))
485 goto DoneWithDeclSpec;
486
487 TypeTy *TypeRep = Actions.isTypeName(*NextToken().getIdentifierInfo(),
488 CurScope, &SS);
489 if (TypeRep == 0)
490 goto DoneWithDeclSpec;
491
492 ConsumeToken(); // The C++ scope.
493
494 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
495 TypeRep);
496 if (isInvalid)
497 break;
498
499 DS.SetRangeEnd(Tok.getLocation());
500 ConsumeToken(); // The typename.
501
502 continue;
503 }
504
Chris Lattnerfda18db2008-07-26 01:18:38 +0000505 // typedef-name
506 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000507 // In C++, check to see if this is a scope specifier like foo::bar::, if
508 // so handle it as such. This is important for ctor parsing.
509 if (getLang().CPlusPlus &&
510 TryAnnotateCXXScopeToken())
511 continue;
512
Chris Lattnerfda18db2008-07-26 01:18:38 +0000513 // This identifier can only be a typedef name if we haven't already seen
514 // a type-specifier. Without this check we misparse:
515 // typedef int X; struct Y { short X; }; as 'short int'.
516 if (DS.hasTypeSpecifier())
517 goto DoneWithDeclSpec;
518
519 // It has to be available as a typedef too!
Argiris Kirtzidis46403632008-08-01 10:35:27 +0000520 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattnerfda18db2008-07-26 01:18:38 +0000521 if (TypeRep == 0)
522 goto DoneWithDeclSpec;
523
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000524 // C++: If the identifier is actually the name of the class type
525 // being defined and the next token is a '(', then this is a
526 // constructor declaration. We're done with the decl-specifiers
527 // and will treat this token as an identifier.
528 if (getLang().CPlusPlus &&
Douglas Gregorcab994d2009-01-09 22:42:13 +0000529 CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000530 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
531 NextToken().getKind() == tok::l_paren)
532 goto DoneWithDeclSpec;
533
Chris Lattnerfda18db2008-07-26 01:18:38 +0000534 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
535 TypeRep);
536 if (isInvalid)
537 break;
538
539 DS.SetRangeEnd(Tok.getLocation());
540 ConsumeToken(); // The identifier
541
542 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
543 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
544 // Objective-C interface. If we don't have Objective-C or a '<', this is
545 // just a normal reference to a typedef name.
546 if (!Tok.is(tok::less) || !getLang().ObjC1)
547 continue;
548
549 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000550 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000551 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000552 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000553
554 DS.SetRangeEnd(EndProtoLoc);
555
Steve Narofff7683302008-09-22 10:28:57 +0000556 // Need to support trailing type qualifiers (e.g. "id<p> const").
557 // If a type specifier follows, it will be diagnosed elsewhere.
558 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000559 }
Chris Lattner4b009652007-07-25 00:24:17 +0000560 // GNU attributes support.
561 case tok::kw___attribute:
562 DS.AddAttributes(ParseAttributes());
563 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000564
565 // Microsoft declspec support.
566 case tok::kw___declspec:
567 if (!PP.getLangOptions().Microsoft)
568 goto DoneWithDeclSpec;
569 FuzzyParseMicrosoftDeclSpec();
570 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000571
Steve Naroffedd04d52008-12-25 14:16:32 +0000572 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000573 case tok::kw___forceinline:
574 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000575 case tok::kw___cdecl:
576 case tok::kw___stdcall:
577 case tok::kw___fastcall:
578 if (!PP.getLangOptions().Microsoft)
579 goto DoneWithDeclSpec;
580 // Just ignore it.
581 break;
582
Chris Lattner4b009652007-07-25 00:24:17 +0000583 // storage-class-specifier
584 case tok::kw_typedef:
585 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
586 break;
587 case tok::kw_extern:
588 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000589 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000590 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
591 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000592 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000593 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
594 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000595 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000596 case tok::kw_static:
597 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000598 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000599 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
600 break;
601 case tok::kw_auto:
602 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
603 break;
604 case tok::kw_register:
605 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
606 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000607 case tok::kw_mutable:
608 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
609 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000610 case tok::kw___thread:
611 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
612 break;
613
Chris Lattner4b009652007-07-25 00:24:17 +0000614 continue;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000615
Chris Lattner4b009652007-07-25 00:24:17 +0000616 // function-specifier
617 case tok::kw_inline:
618 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
619 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000620
621 case tok::kw_virtual:
622 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
623 break;
624
625 case tok::kw_explicit:
626 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
627 break;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000628
Steve Naroff5f0466b2008-06-05 00:02:44 +0000629 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000630 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000631 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
632 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000633 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000634 goto DoneWithDeclSpec;
635
636 {
637 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000638 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000639 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000640 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000641 DS.SetRangeEnd(EndProtoLoc);
642
Chris Lattnerf006a222008-11-18 07:48:38 +0000643 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
644 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000645 // Need to support trailing type qualifiers (e.g. "id<p> const").
646 // If a type specifier follows, it will be diagnosed elsewhere.
647 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000648 }
Chris Lattner4b009652007-07-25 00:24:17 +0000649 }
650 // If the specifier combination wasn't legal, issue a diagnostic.
651 if (isInvalid) {
652 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000653 // Pick between error or extwarn.
654 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
655 : diag::ext_duplicate_declspec;
656 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000657 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000658 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000659 ConsumeToken();
660 }
661}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000662
Chris Lattnerd706dc82009-01-06 06:59:53 +0000663/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000664/// primarily follow the C++ grammar with additions for C99 and GNU,
665/// which together subsume the C grammar. Note that the C++
666/// type-specifier also includes the C type-qualifier (for const,
667/// volatile, and C99 restrict). Returns true if a type-specifier was
668/// found (and parsed), false otherwise.
669///
670/// type-specifier: [C++ 7.1.5]
671/// simple-type-specifier
672/// class-specifier
673/// enum-specifier
674/// elaborated-type-specifier [TODO]
675/// cv-qualifier
676///
677/// cv-qualifier: [C++ 7.1.5.1]
678/// 'const'
679/// 'volatile'
680/// [C99] 'restrict'
681///
682/// simple-type-specifier: [ C++ 7.1.5.2]
683/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
684/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
685/// 'char'
686/// 'wchar_t'
687/// 'bool'
688/// 'short'
689/// 'int'
690/// 'long'
691/// 'signed'
692/// 'unsigned'
693/// 'float'
694/// 'double'
695/// 'void'
696/// [C99] '_Bool'
697/// [C99] '_Complex'
698/// [C99] '_Imaginary' // Removed in TC2?
699/// [GNU] '_Decimal32'
700/// [GNU] '_Decimal64'
701/// [GNU] '_Decimal128'
702/// [GNU] typeof-specifier
703/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
704/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerd706dc82009-01-06 06:59:53 +0000705bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
706 const char *&PrevSpec,
707 TemplateParameterLists *TemplateParams){
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000708 SourceLocation Loc = Tok.getLocation();
709
710 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +0000711 case tok::identifier: // foo::bar
712 // Annotate typenames and C++ scope specifiers. If we get one, just
713 // recurse to handle whatever we get.
714 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000715 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000716 // Otherwise, not a type specifier.
717 return false;
718 case tok::coloncolon: // ::foo::bar
719 if (NextToken().is(tok::kw_new) || // ::new
720 NextToken().is(tok::kw_delete)) // ::delete
721 return false;
722
723 // Annotate typenames and C++ scope specifiers. If we get one, just
724 // recurse to handle whatever we get.
725 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000726 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000727 // Otherwise, not a type specifier.
728 return false;
729
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000730 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +0000731 case tok::annot_typename: {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000732 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000733 Tok.getAnnotationValue());
734 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
735 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000736
737 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
738 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
739 // Objective-C interface. If we don't have Objective-C or a '<', this is
740 // just a normal reference to a typedef name.
741 if (!Tok.is(tok::less) || !getLang().ObjC1)
742 return true;
743
744 SourceLocation EndProtoLoc;
745 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
746 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
747 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
748
749 DS.SetRangeEnd(EndProtoLoc);
750 return true;
751 }
752
753 case tok::kw_short:
754 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
755 break;
756 case tok::kw_long:
757 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
758 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
759 else
760 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
761 break;
762 case tok::kw_signed:
763 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
764 break;
765 case tok::kw_unsigned:
766 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
767 break;
768 case tok::kw__Complex:
769 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
770 break;
771 case tok::kw__Imaginary:
772 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
773 break;
774 case tok::kw_void:
775 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
776 break;
777 case tok::kw_char:
778 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
779 break;
780 case tok::kw_int:
781 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
782 break;
783 case tok::kw_float:
784 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
785 break;
786 case tok::kw_double:
787 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
788 break;
789 case tok::kw_wchar_t:
790 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
791 break;
792 case tok::kw_bool:
793 case tok::kw__Bool:
794 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
795 break;
796 case tok::kw__Decimal32:
797 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
798 break;
799 case tok::kw__Decimal64:
800 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
801 break;
802 case tok::kw__Decimal128:
803 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
804 break;
805
806 // class-specifier:
807 case tok::kw_class:
808 case tok::kw_struct:
809 case tok::kw_union:
Douglas Gregor52473432008-12-24 02:52:09 +0000810 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000811 return true;
812
813 // enum-specifier:
814 case tok::kw_enum:
815 ParseEnumSpecifier(DS);
816 return true;
817
818 // cv-qualifier:
819 case tok::kw_const:
820 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
821 getLang())*2;
822 break;
823 case tok::kw_volatile:
824 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
825 getLang())*2;
826 break;
827 case tok::kw_restrict:
828 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
829 getLang())*2;
830 break;
831
832 // GNU typeof support.
833 case tok::kw_typeof:
834 ParseTypeofSpecifier(DS);
835 return true;
836
Steve Naroffedd04d52008-12-25 14:16:32 +0000837 case tok::kw___cdecl:
838 case tok::kw___stdcall:
839 case tok::kw___fastcall:
840 return PP.getLangOptions().Microsoft;
841
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000842 default:
843 // Not a type-specifier; do nothing.
844 return false;
845 }
846
847 // If the specifier combination wasn't legal, issue a diagnostic.
848 if (isInvalid) {
849 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000850 // Pick between error or extwarn.
851 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
852 : diag::ext_duplicate_declspec;
853 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000854 }
855 DS.SetRangeEnd(Tok.getLocation());
856 ConsumeToken(); // whatever we parsed above.
857 return true;
858}
Chris Lattner4b009652007-07-25 00:24:17 +0000859
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000860/// ParseStructDeclaration - Parse a struct declaration without the terminating
861/// semicolon.
862///
Chris Lattner4b009652007-07-25 00:24:17 +0000863/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000864/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000865/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000866/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000867/// struct-declarator-list:
868/// struct-declarator
869/// struct-declarator-list ',' struct-declarator
870/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
871/// struct-declarator:
872/// declarator
873/// [GNU] declarator attributes[opt]
874/// declarator[opt] ':' constant-expression
875/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
876///
Chris Lattner3dd8d392008-04-10 06:46:29 +0000877void Parser::
878ParseStructDeclaration(DeclSpec &DS,
879 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000880 if (Tok.is(tok::kw___extension__)) {
881 // __extension__ silences extension warnings in the subexpression.
882 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +0000883 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000884 return ParseStructDeclaration(DS, Fields);
885 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000886
887 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000888 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +0000889 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +0000890
Douglas Gregorb748fc52009-01-12 22:49:06 +0000891 // If there are no declarators, this is a free-standing declaration
892 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000893 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +0000894 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +0000895 return;
896 }
897
898 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000899 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000900 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +0000901 FieldDeclarator &DeclaratorInfo = Fields.back();
902
Steve Naroffa9adf112007-08-20 22:28:22 +0000903 /// struct-declarator: declarator
904 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000905 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000906 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +0000907
Chris Lattner34a01ad2007-10-09 17:33:22 +0000908 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000909 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000910 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000911 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +0000912 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000913 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000914 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +0000915 }
916
917 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000918 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000919 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000920
921 // If we don't have a comma, it is either the end of the list (a ';')
922 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000923 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000924 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000925
926 // Consume the comma.
927 ConsumeToken();
928
929 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000930 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000931
932 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000933 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000934 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000935 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000936}
937
938/// ParseStructUnionBody
939/// struct-contents:
940/// struct-declaration-list
941/// [EXT] empty
942/// [GNU] "struct-declaration-list" without terminatoring ';'
943/// struct-declaration-list:
944/// struct-declaration
945/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +0000946/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +0000947///
Chris Lattner4b009652007-07-25 00:24:17 +0000948void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
949 unsigned TagType, DeclTy *TagDecl) {
950 SourceLocation LBraceLoc = ConsumeBrace();
951
Douglas Gregorcab994d2009-01-09 22:42:13 +0000952 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +0000953 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
954
Chris Lattner4b009652007-07-25 00:24:17 +0000955 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
956 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +0000957 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +0000958 Diag(Tok, diag::ext_empty_struct_union_enum)
959 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +0000960
961 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000962 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
963
Chris Lattner4b009652007-07-25 00:24:17 +0000964 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000965 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000966 // Each iteration of this loop reads one struct-declaration.
967
968 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000969 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000970 Diag(Tok, diag::ext_extra_struct_semi);
971 ConsumeToken();
972 continue;
973 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000974
975 // Parse all the comma separated declarators.
976 DeclSpec DS;
977 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +0000978 if (!Tok.is(tok::at)) {
979 ParseStructDeclaration(DS, FieldDeclarators);
980
981 // Convert them all to fields.
982 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
983 FieldDeclarator &FD = FieldDeclarators[i];
984 // Install the declarator into the current TagDecl.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000985 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
Chris Lattner1bf58f62008-06-21 19:39:06 +0000986 DS.getSourceRange().getBegin(),
987 FD.D, FD.BitfieldSize);
988 FieldDecls.push_back(Field);
989 }
990 } else { // Handle @defs
991 ConsumeToken();
992 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
993 Diag(Tok, diag::err_unexpected_at);
994 SkipUntil(tok::semi, true, true);
995 continue;
996 }
997 ConsumeToken();
998 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
999 if (!Tok.is(tok::identifier)) {
1000 Diag(Tok, diag::err_expected_ident);
1001 SkipUntil(tok::semi, true, true);
1002 continue;
1003 }
1004 llvm::SmallVector<DeclTy*, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001005 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1006 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001007 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1008 ConsumeToken();
1009 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1010 }
Chris Lattner4b009652007-07-25 00:24:17 +00001011
Chris Lattner34a01ad2007-10-09 17:33:22 +00001012 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001013 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001014 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001015 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001016 break;
1017 } else {
1018 Diag(Tok, diag::err_expected_semi_decl_list);
1019 // Skip to end of block or statement
1020 SkipUntil(tok::r_brace, true, true);
1021 }
1022 }
1023
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001024 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001025
Chris Lattner4b009652007-07-25 00:24:17 +00001026 AttributeList *AttrList = 0;
1027 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001028 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001029 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001030
1031 Actions.ActOnFields(CurScope,
1032 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1033 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001034 AttrList);
1035 StructScope.Exit();
1036 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001037}
1038
1039
1040/// ParseEnumSpecifier
1041/// enum-specifier: [C99 6.7.2.2]
1042/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001043///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001044/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1045/// '}' attributes[opt]
1046/// 'enum' identifier
1047/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001048///
1049/// [C++] elaborated-type-specifier:
1050/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1051///
Chris Lattner4b009652007-07-25 00:24:17 +00001052void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001053 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +00001054 SourceLocation StartLoc = ConsumeToken();
1055
1056 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001057
1058 AttributeList *Attr = 0;
1059 // If attributes exist after tag, parse them.
1060 if (Tok.is(tok::kw___attribute))
1061 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001062
1063 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001064 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001065 if (Tok.isNot(tok::identifier)) {
1066 Diag(Tok, diag::err_expected_ident);
1067 if (Tok.isNot(tok::l_brace)) {
1068 // Has no name and is not a definition.
1069 // Skip the rest of this declarator, up until the comma or semicolon.
1070 SkipUntil(tok::comma, true);
1071 return;
1072 }
1073 }
1074 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001075
1076 // Must have either 'enum name' or 'enum {...}'.
1077 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1078 Diag(Tok, diag::err_expected_ident_lbrace);
1079
1080 // Skip the rest of this declarator, up until the comma or semicolon.
1081 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001082 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001083 }
1084
1085 // If an identifier is present, consume and remember it.
1086 IdentifierInfo *Name = 0;
1087 SourceLocation NameLoc;
1088 if (Tok.is(tok::identifier)) {
1089 Name = Tok.getIdentifierInfo();
1090 NameLoc = ConsumeToken();
1091 }
1092
1093 // There are three options here. If we have 'enum foo;', then this is a
1094 // forward declaration. If we have 'enum foo {...' then this is a
1095 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1096 //
1097 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1098 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1099 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1100 //
1101 Action::TagKind TK;
1102 if (Tok.is(tok::l_brace))
1103 TK = Action::TK_Definition;
1104 else if (Tok.is(tok::semi))
1105 TK = Action::TK_Declaration;
1106 else
1107 TK = Action::TK_Reference;
1108 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Douglas Gregor52473432008-12-24 02:52:09 +00001109 SS, Name, NameLoc, Attr,
1110 Action::MultiTemplateParamsArg(Actions));
Chris Lattner4b009652007-07-25 00:24:17 +00001111
Chris Lattner34a01ad2007-10-09 17:33:22 +00001112 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001113 ParseEnumBody(StartLoc, TagDecl);
1114
1115 // TODO: semantic analysis on the declspec for enums.
1116 const char *PrevSpec = 0;
1117 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerf006a222008-11-18 07:48:38 +00001118 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001119}
1120
1121/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1122/// enumerator-list:
1123/// enumerator
1124/// enumerator-list ',' enumerator
1125/// enumerator:
1126/// enumeration-constant
1127/// enumeration-constant '=' constant-expression
1128/// enumeration-constant:
1129/// identifier
1130///
1131void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001132 // Enter the scope of the enum body and start the definition.
1133 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001134 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001135
Chris Lattner4b009652007-07-25 00:24:17 +00001136 SourceLocation LBraceLoc = ConsumeBrace();
1137
Chris Lattnerc9a92452007-08-27 17:24:30 +00001138 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001139 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001140 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001141
1142 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1143
1144 DeclTy *LastEnumConstDecl = 0;
1145
1146 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001147 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001148 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1149 SourceLocation IdentLoc = ConsumeToken();
1150
1151 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001152 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001153 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001154 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001155 AssignedVal = ParseConstantExpression();
1156 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001157 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001158 }
1159
1160 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +00001161 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +00001162 LastEnumConstDecl,
1163 IdentLoc, Ident,
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001164 EqualLoc,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001165 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001166 EnumConstantDecls.push_back(EnumConstDecl);
1167 LastEnumConstDecl = EnumConstDecl;
1168
Chris Lattner34a01ad2007-10-09 17:33:22 +00001169 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001170 break;
1171 SourceLocation CommaLoc = ConsumeToken();
1172
Chris Lattner34a01ad2007-10-09 17:33:22 +00001173 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +00001174 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1175 }
1176
1177 // Eat the }.
1178 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1179
Steve Naroff0acc9c92007-09-15 18:49:24 +00001180 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001181 EnumConstantDecls.size());
1182
1183 DeclTy *AttrList = 0;
1184 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001185 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001186 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001187
1188 EnumScope.Exit();
1189 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001190}
1191
1192/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001193/// start of a type-qualifier-list.
1194bool Parser::isTypeQualifier() const {
1195 switch (Tok.getKind()) {
1196 default: return false;
1197 // type-qualifier
1198 case tok::kw_const:
1199 case tok::kw_volatile:
1200 case tok::kw_restrict:
1201 return true;
1202 }
1203}
1204
1205/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001206/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001207bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001208 switch (Tok.getKind()) {
1209 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001210
1211 case tok::identifier: // foo::bar
1212 // Annotate typenames and C++ scope specifiers. If we get one, just
1213 // recurse to handle whatever we get.
1214 if (TryAnnotateTypeOrScopeToken())
1215 return isTypeSpecifierQualifier();
1216 // Otherwise, not a type specifier.
1217 return false;
1218 case tok::coloncolon: // ::foo::bar
1219 if (NextToken().is(tok::kw_new) || // ::new
1220 NextToken().is(tok::kw_delete)) // ::delete
1221 return false;
1222
1223 // Annotate typenames and C++ scope specifiers. If we get one, just
1224 // recurse to handle whatever we get.
1225 if (TryAnnotateTypeOrScopeToken())
1226 return isTypeSpecifierQualifier();
1227 // Otherwise, not a type specifier.
1228 return false;
1229
Chris Lattner4b009652007-07-25 00:24:17 +00001230 // GNU attributes support.
1231 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001232 // GNU typeof support.
1233 case tok::kw_typeof:
1234
Chris Lattner4b009652007-07-25 00:24:17 +00001235 // type-specifiers
1236 case tok::kw_short:
1237 case tok::kw_long:
1238 case tok::kw_signed:
1239 case tok::kw_unsigned:
1240 case tok::kw__Complex:
1241 case tok::kw__Imaginary:
1242 case tok::kw_void:
1243 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001244 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001245 case tok::kw_int:
1246 case tok::kw_float:
1247 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001248 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001249 case tok::kw__Bool:
1250 case tok::kw__Decimal32:
1251 case tok::kw__Decimal64:
1252 case tok::kw__Decimal128:
1253
Chris Lattner2e78db32008-04-13 18:59:07 +00001254 // struct-or-union-specifier (C99) or class-specifier (C++)
1255 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001256 case tok::kw_struct:
1257 case tok::kw_union:
1258 // enum-specifier
1259 case tok::kw_enum:
1260
1261 // type-qualifier
1262 case tok::kw_const:
1263 case tok::kw_volatile:
1264 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001265
1266 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001267 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001268 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001269
1270 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1271 case tok::less:
1272 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001273
1274 case tok::kw___cdecl:
1275 case tok::kw___stdcall:
1276 case tok::kw___fastcall:
1277 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001278 }
1279}
1280
1281/// isDeclarationSpecifier() - Return true if the current token is part of a
1282/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001283bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001284 switch (Tok.getKind()) {
1285 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001286
1287 case tok::identifier: // foo::bar
1288 // Annotate typenames and C++ scope specifiers. If we get one, just
1289 // recurse to handle whatever we get.
1290 if (TryAnnotateTypeOrScopeToken())
1291 return isDeclarationSpecifier();
1292 // Otherwise, not a declaration specifier.
1293 return false;
1294 case tok::coloncolon: // ::foo::bar
1295 if (NextToken().is(tok::kw_new) || // ::new
1296 NextToken().is(tok::kw_delete)) // ::delete
1297 return false;
1298
1299 // Annotate typenames and C++ scope specifiers. If we get one, just
1300 // recurse to handle whatever we get.
1301 if (TryAnnotateTypeOrScopeToken())
1302 return isDeclarationSpecifier();
1303 // Otherwise, not a declaration specifier.
1304 return false;
1305
Chris Lattner4b009652007-07-25 00:24:17 +00001306 // storage-class-specifier
1307 case tok::kw_typedef:
1308 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001309 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001310 case tok::kw_static:
1311 case tok::kw_auto:
1312 case tok::kw_register:
1313 case tok::kw___thread:
1314
1315 // type-specifiers
1316 case tok::kw_short:
1317 case tok::kw_long:
1318 case tok::kw_signed:
1319 case tok::kw_unsigned:
1320 case tok::kw__Complex:
1321 case tok::kw__Imaginary:
1322 case tok::kw_void:
1323 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001324 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001325 case tok::kw_int:
1326 case tok::kw_float:
1327 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001328 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001329 case tok::kw__Bool:
1330 case tok::kw__Decimal32:
1331 case tok::kw__Decimal64:
1332 case tok::kw__Decimal128:
1333
Chris Lattner2e78db32008-04-13 18:59:07 +00001334 // struct-or-union-specifier (C99) or class-specifier (C++)
1335 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001336 case tok::kw_struct:
1337 case tok::kw_union:
1338 // enum-specifier
1339 case tok::kw_enum:
1340
1341 // type-qualifier
1342 case tok::kw_const:
1343 case tok::kw_volatile:
1344 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001345
Chris Lattner4b009652007-07-25 00:24:17 +00001346 // function-specifier
1347 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001348 case tok::kw_virtual:
1349 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001350
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001351 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001352 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001353
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001354 // GNU typeof support.
1355 case tok::kw_typeof:
1356
1357 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001358 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001359 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001360
1361 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1362 case tok::less:
1363 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001364
Steve Naroffab1a3632009-01-06 19:34:12 +00001365 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001366 case tok::kw___cdecl:
1367 case tok::kw___stdcall:
1368 case tok::kw___fastcall:
1369 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001370 }
1371}
1372
1373
1374/// ParseTypeQualifierListOpt
1375/// type-qualifier-list: [C99 6.7.5]
1376/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001377/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001378/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001379/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001380///
Chris Lattner460696f2008-12-18 07:02:59 +00001381void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001382 while (1) {
1383 int isInvalid = false;
1384 const char *PrevSpec = 0;
1385 SourceLocation Loc = Tok.getLocation();
1386
1387 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001388 case tok::kw_const:
1389 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1390 getLang())*2;
1391 break;
1392 case tok::kw_volatile:
1393 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1394 getLang())*2;
1395 break;
1396 case tok::kw_restrict:
1397 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1398 getLang())*2;
1399 break;
Steve Naroffad620402008-12-25 14:41:26 +00001400 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001401 case tok::kw___cdecl:
1402 case tok::kw___stdcall:
1403 case tok::kw___fastcall:
1404 if (!PP.getLangOptions().Microsoft)
1405 goto DoneWithTypeQuals;
1406 // Just ignore it.
1407 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001408 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001409 if (AttributesAllowed) {
1410 DS.AddAttributes(ParseAttributes());
1411 continue; // do *not* consume the next token!
1412 }
1413 // otherwise, FALL THROUGH!
1414 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001415 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001416 // If this is not a type-qualifier token, we're done reading type
1417 // qualifiers. First verify that DeclSpec's are consistent.
1418 DS.Finish(Diags, PP.getSourceManager(), getLang());
1419 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001420 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001421
Chris Lattner4b009652007-07-25 00:24:17 +00001422 // If the specifier combination wasn't legal, issue a diagnostic.
1423 if (isInvalid) {
1424 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001425 // Pick between error or extwarn.
1426 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1427 : diag::ext_duplicate_declspec;
1428 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001429 }
1430 ConsumeToken();
1431 }
1432}
1433
1434
1435/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1436///
1437void Parser::ParseDeclarator(Declarator &D) {
1438 /// This implements the 'declarator' production in the C grammar, then checks
1439 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001440 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001441}
1442
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001443/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1444/// is parsed by the function passed to it. Pass null, and the direct-declarator
1445/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001446/// ptr-operator production.
1447///
Chris Lattner4b009652007-07-25 00:24:17 +00001448/// declarator: [C99 6.7.5]
1449/// pointer[opt] direct-declarator
1450/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1451/// [GNU] '&' restrict[opt] attributes[opt] declarator
1452///
1453/// pointer: [C99 6.7.5]
1454/// '*' type-qualifier-list[opt]
1455/// '*' type-qualifier-list[opt] pointer
1456///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001457/// ptr-operator:
1458/// '*' cv-qualifier-seq[opt]
1459/// '&'
1460/// [GNU] '&' restrict[opt] attributes[opt]
1461/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] [TODO]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001462void Parser::ParseDeclaratorInternal(Declarator &D,
1463 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001464 tok::TokenKind Kind = Tok.getKind();
1465
Steve Naroff7aa54752008-08-27 16:04:49 +00001466 // Not a pointer, C++ reference, or block.
1467 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001468 (Kind != tok::caret || !getLang().Blocks)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001469 if (DirectDeclParser)
1470 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001471 return;
1472 }
Chris Lattner4b009652007-07-25 00:24:17 +00001473
Steve Naroffdc22f212008-08-28 10:07:06 +00001474 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Chris Lattner4b009652007-07-25 00:24:17 +00001475 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1476
Steve Naroffdc22f212008-08-28 10:07:06 +00001477 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner69f01932008-02-21 01:32:26 +00001478 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001479 DeclSpec DS;
1480
1481 ParseTypeQualifierListOpt(DS);
1482
1483 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001484 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001485 if (Kind == tok::star)
1486 // Remember that we parsed a pointer type, and remember the type-quals.
1487 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1488 DS.TakeAttributes()));
1489 else
1490 // Remember that we parsed a Block type, and remember the type-quals.
1491 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1492 Loc));
Chris Lattner4b009652007-07-25 00:24:17 +00001493 } else {
1494 // Is a reference
1495 DeclSpec DS;
1496
1497 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1498 // cv-qualifiers are introduced through the use of a typedef or of a
1499 // template type argument, in which case the cv-qualifiers are ignored.
1500 //
1501 // [GNU] Retricted references are allowed.
1502 // [GNU] Attributes on references are allowed.
1503 ParseTypeQualifierListOpt(DS);
1504
1505 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1506 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1507 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001508 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001509 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1510 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001511 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001512 }
1513
1514 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001515 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001516
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001517 if (D.getNumTypeObjects() > 0) {
1518 // C++ [dcl.ref]p4: There shall be no references to references.
1519 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1520 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001521 if (const IdentifierInfo *II = D.getIdentifier())
1522 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1523 << II;
1524 else
1525 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1526 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001527
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001528 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001529 // can go ahead and build the (technically ill-formed)
1530 // declarator: reference collapsing will take care of it.
1531 }
1532 }
1533
Chris Lattner4b009652007-07-25 00:24:17 +00001534 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001535 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1536 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001537 }
1538}
1539
1540/// ParseDirectDeclarator
1541/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001542/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001543/// '(' declarator ')'
1544/// [GNU] '(' attributes declarator ')'
1545/// [C90] direct-declarator '[' constant-expression[opt] ']'
1546/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1547/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1548/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1549/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1550/// direct-declarator '(' parameter-type-list ')'
1551/// direct-declarator '(' identifier-list[opt] ')'
1552/// [GNU] direct-declarator '(' parameter-forward-declarations
1553/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001554/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1555/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001556/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001557///
1558/// declarator-id: [C++ 8]
1559/// id-expression
1560/// '::'[opt] nested-name-specifier[opt] type-name
1561///
1562/// id-expression: [C++ 5.1]
1563/// unqualified-id
1564/// qualified-id [TODO]
1565///
1566/// unqualified-id: [C++ 5.1]
1567/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001568/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001569/// conversion-function-id [TODO]
1570/// '~' class-name
1571/// template-id [TODO]
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001572///
Chris Lattner4b009652007-07-25 00:24:17 +00001573void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001574 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001575
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001576 if (getLang().CPlusPlus) {
1577 if (D.mayHaveIdentifier()) {
Chris Lattnerd706dc82009-01-06 06:59:53 +00001578 bool afterCXXScope = ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001579 if (afterCXXScope) {
1580 // Change the declaration context for name lookup, until this function
1581 // is exited (and the declarator has been parsed).
1582 DeclScopeObj.EnterDeclaratorScope();
1583 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001584
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001585 if (Tok.is(tok::identifier)) {
1586 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor2fa10442008-12-18 19:37:40 +00001587
1588 // If this identifier is followed by a '<', we may have a template-id.
1589 DeclTy *Template;
Douglas Gregor853dd392008-12-26 15:00:45 +00001590 if (NextToken().is(tok::less) &&
Douglas Gregor2fa10442008-12-18 19:37:40 +00001591 (Template = Actions.isTemplateName(*Tok.getIdentifierInfo(),
1592 CurScope))) {
1593 IdentifierInfo *II = Tok.getIdentifierInfo();
1594 AnnotateTemplateIdToken(Template, 0);
1595 // FIXME: Set the declarator to a template-id. How? I don't
1596 // know... for now, just use the identifier.
1597 D.SetIdentifier(II, Tok.getLocation());
1598 }
1599 // If this identifier is the name of the current class, it's a
1600 // constructor name.
Douglas Gregor853dd392008-12-26 15:00:45 +00001601 else if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope))
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001602 D.setConstructor(Actions.isTypeName(*Tok.getIdentifierInfo(),
1603 CurScope),
1604 Tok.getLocation());
Douglas Gregor2fa10442008-12-18 19:37:40 +00001605 // This is a normal identifier.
1606 else
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001607 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1608 ConsumeToken();
1609 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00001610 } else if (Tok.is(tok::kw_operator)) {
1611 SourceLocation OperatorLoc = Tok.getLocation();
Douglas Gregore60e5d32008-11-06 22:13:31 +00001612
Douglas Gregor853dd392008-12-26 15:00:45 +00001613 // First try the name of an overloaded operator
1614 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId()) {
1615 D.setOverloadedOperator(Op, OperatorLoc);
1616 } else {
1617 // This must be a conversion function (C++ [class.conv.fct]).
1618 if (TypeTy *ConvType = ParseConversionFunctionId())
1619 D.setConversionFunction(ConvType, OperatorLoc);
1620 else
1621 D.SetIdentifier(0, Tok.getLocation());
1622 }
1623 goto PastIdentifier;
1624 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001625 // This should be a C++ destructor.
1626 SourceLocation TildeLoc = ConsumeToken();
1627 if (Tok.is(tok::identifier)) {
1628 if (TypeTy *Type = ParseClassName())
1629 D.setDestructor(Type, TildeLoc);
1630 else
1631 D.SetIdentifier(0, TildeLoc);
1632 } else {
1633 Diag(Tok, diag::err_expected_class_name);
1634 D.SetIdentifier(0, TildeLoc);
1635 }
1636 goto PastIdentifier;
1637 }
1638
1639 // If we reached this point, token is not identifier and not '~'.
1640
1641 if (afterCXXScope) {
1642 Diag(Tok, diag::err_expected_unqualified_id);
1643 D.SetIdentifier(0, Tok.getLocation());
1644 D.setInvalidType(true);
1645 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001646 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001647 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001648 }
1649
1650 // If we reached this point, we are either in C/ObjC or the token didn't
1651 // satisfy any of the C++-specific checks.
1652
1653 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1654 assert(!getLang().CPlusPlus &&
1655 "There's a C++-specific check for tok::identifier above");
1656 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1657 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1658 ConsumeToken();
1659 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001660 // direct-declarator: '(' declarator ')'
1661 // direct-declarator: '(' attributes declarator ')'
1662 // Example: 'char (*X)' or 'int (*XX)(void)'
1663 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001664 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001665 // This could be something simple like "int" (in which case the declarator
1666 // portion is empty), if an abstract-declarator is allowed.
1667 D.SetIdentifier(0, Tok.getLocation());
1668 } else {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001669 if (getLang().CPlusPlus)
1670 Diag(Tok, diag::err_expected_unqualified_id);
1671 else
Chris Lattnerf006a222008-11-18 07:48:38 +00001672 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00001673 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00001674 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001675 }
1676
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001677 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00001678 assert(D.isPastIdentifier() &&
1679 "Haven't past the location of the identifier yet?");
1680
1681 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001682 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001683 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1684 // In such a case, check if we actually have a function declarator; if it
1685 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001686 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1687 // When not in file scope, warn for ambiguous function declarators, just
1688 // in case the author intended it as a variable definition.
1689 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1690 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1691 break;
1692 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001693 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001694 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001695 ParseBracketDeclarator(D);
1696 } else {
1697 break;
1698 }
1699 }
1700}
1701
Chris Lattnera0d056d2008-04-06 05:45:57 +00001702/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1703/// only called before the identifier, so these are most likely just grouping
1704/// parens for precedence. If we find that these are actually function
1705/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1706///
1707/// direct-declarator:
1708/// '(' declarator ')'
1709/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001710/// direct-declarator '(' parameter-type-list ')'
1711/// direct-declarator '(' identifier-list[opt] ')'
1712/// [GNU] direct-declarator '(' parameter-forward-declarations
1713/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001714///
1715void Parser::ParseParenDeclarator(Declarator &D) {
1716 SourceLocation StartLoc = ConsumeParen();
1717 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1718
Chris Lattner1f185292008-10-20 02:05:46 +00001719 // Eat any attributes before we look at whether this is a grouping or function
1720 // declarator paren. If this is a grouping paren, the attribute applies to
1721 // the type being built up, for example:
1722 // int (__attribute__(()) *x)(long y)
1723 // If this ends up not being a grouping paren, the attribute applies to the
1724 // first argument, for example:
1725 // int (__attribute__(()) int x)
1726 // In either case, we need to eat any attributes to be able to determine what
1727 // sort of paren this is.
1728 //
1729 AttributeList *AttrList = 0;
1730 bool RequiresArg = false;
1731 if (Tok.is(tok::kw___attribute)) {
1732 AttrList = ParseAttributes();
1733
1734 // We require that the argument list (if this is a non-grouping paren) be
1735 // present even if the attribute list was empty.
1736 RequiresArg = true;
1737 }
Steve Naroffedd04d52008-12-25 14:16:32 +00001738 // Eat any Microsoft extensions.
Douglas Gregore51b7c82009-01-10 00:48:18 +00001739 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
1740 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroffedd04d52008-12-25 14:16:32 +00001741 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00001742
Chris Lattnera0d056d2008-04-06 05:45:57 +00001743 // If we haven't past the identifier yet (or where the identifier would be
1744 // stored, if this is an abstract declarator), then this is probably just
1745 // grouping parens. However, if this could be an abstract-declarator, then
1746 // this could also be the start of function arguments (consider 'void()').
1747 bool isGrouping;
1748
1749 if (!D.mayOmitIdentifier()) {
1750 // If this can't be an abstract-declarator, this *must* be a grouping
1751 // paren, because we haven't seen the identifier yet.
1752 isGrouping = true;
1753 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001754 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00001755 isDeclarationSpecifier()) { // 'int(int)' is a function.
1756 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1757 // considered to be a type, not a K&R identifier-list.
1758 isGrouping = false;
1759 } else {
1760 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1761 isGrouping = true;
1762 }
1763
1764 // If this is a grouping paren, handle:
1765 // direct-declarator: '(' declarator ')'
1766 // direct-declarator: '(' attributes declarator ')'
1767 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001768 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001769 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00001770 if (AttrList)
1771 D.AddAttributes(AttrList);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001772
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001773 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001774 // Match the ')'.
1775 MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001776
1777 D.setGroupingParens(hadGroupingParens);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001778 return;
1779 }
1780
1781 // Okay, if this wasn't a grouping paren, it must be the start of a function
1782 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00001783 // identifier (and remember where it would have been), then call into
1784 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00001785 D.SetIdentifier(0, Tok.getLocation());
1786
Chris Lattner1f185292008-10-20 02:05:46 +00001787 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001788}
1789
1790/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1791/// declarator D up to a paren, which indicates that we are parsing function
1792/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001793///
Chris Lattner1f185292008-10-20 02:05:46 +00001794/// If AttrList is non-null, then the caller parsed those arguments immediately
1795/// after the open paren - they should be considered to be the first argument of
1796/// a parameter. If RequiresArg is true, then the first argument of the
1797/// function is required to be present and required to not be an identifier
1798/// list.
1799///
Chris Lattner4b009652007-07-25 00:24:17 +00001800/// This method also handles this portion of the grammar:
1801/// parameter-type-list: [C99 6.7.5]
1802/// parameter-list
1803/// parameter-list ',' '...'
1804///
1805/// parameter-list: [C99 6.7.5]
1806/// parameter-declaration
1807/// parameter-list ',' parameter-declaration
1808///
1809/// parameter-declaration: [C99 6.7.5]
1810/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00001811/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001812/// [GNU] declaration-specifiers declarator attributes
1813/// declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00001814/// [C++] declaration-specifiers abstract-declarator[opt]
1815/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001816/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1817///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001818/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
1819/// and "exception-specification[opt]"(TODO).
1820///
Chris Lattner1f185292008-10-20 02:05:46 +00001821void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
1822 AttributeList *AttrList,
1823 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00001824 // lparen is already consumed!
1825 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00001826
Chris Lattner1f185292008-10-20 02:05:46 +00001827 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001828 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00001829 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001830 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00001831 delete AttrList;
1832 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001833
1834 ConsumeParen(); // Eat the closing ')'.
1835
1836 // cv-qualifier-seq[opt].
1837 DeclSpec DS;
1838 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00001839 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor90a2c972008-11-25 03:22:00 +00001840
1841 // Parse exception-specification[opt].
1842 if (Tok.is(tok::kw_throw))
1843 ParseExceptionSpecification();
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001844 }
1845
Chris Lattner9f7564b2008-04-06 06:57:35 +00001846 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00001847 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001848 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00001849 /*variadic*/ false,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001850 /*arglist*/ 0, 0,
1851 DS.getTypeQualifiers(),
1852 LParenLoc));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001853 return;
Chris Lattner1f185292008-10-20 02:05:46 +00001854 }
1855
1856 // Alternatively, this parameter list may be an identifier list form for a
1857 // K&R-style function: void foo(a,b,c)
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001858 if (!getLang().CPlusPlus && Tok.is(tok::identifier) &&
Chris Lattner1f185292008-10-20 02:05:46 +00001859 // K&R identifier lists can't have typedefs as identifiers, per
1860 // C99 6.7.5.3p11.
1861 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1862 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001863 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00001864 delete AttrList;
1865 }
1866
Chris Lattner4b009652007-07-25 00:24:17 +00001867 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1868 // normal declarators, not for abstract-declarators.
Chris Lattner35d9c912008-04-06 06:34:08 +00001869 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001870 }
1871
1872 // Finally, a normal, non-empty parameter type list.
1873
1874 // Build up an array of information about the parsed arguments.
1875 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00001876
1877 // Enter function-declaration scope, limiting any declarators to the
1878 // function prototype scope, including parameter declarators.
Douglas Gregorcab994d2009-01-09 22:42:13 +00001879 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001880
1881 bool IsVariadic = false;
1882 while (1) {
1883 if (Tok.is(tok::ellipsis)) {
1884 IsVariadic = true;
Chris Lattner4b009652007-07-25 00:24:17 +00001885
Chris Lattner9f7564b2008-04-06 06:57:35 +00001886 // Check to see if this is "void(...)" which is not allowed.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001887 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00001888 // Otherwise, parse parameter type list. If it starts with an
1889 // ellipsis, diagnose the malformed function.
1890 Diag(Tok, diag::err_ellipsis_first_arg);
1891 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001892 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00001893
Chris Lattner9f7564b2008-04-06 06:57:35 +00001894 ConsumeToken(); // Consume the ellipsis.
1895 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001896 }
1897
Chris Lattner9f7564b2008-04-06 06:57:35 +00001898 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00001899
Chris Lattner9f7564b2008-04-06 06:57:35 +00001900 // Parse the declaration-specifiers.
1901 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00001902
1903 // If the caller parsed attributes for the first argument, add them now.
1904 if (AttrList) {
1905 DS.AddAttributes(AttrList);
1906 AttrList = 0; // Only apply the attributes to the first parameter.
1907 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001908 ParseDeclarationSpecifiers(DS);
1909
1910 // Parse the declarator. This is "PrototypeContext", because we must
1911 // accept either 'declarator' or 'abstract-declarator' here.
1912 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1913 ParseDeclarator(ParmDecl);
1914
1915 // Parse GNU attributes, if present.
1916 if (Tok.is(tok::kw___attribute))
1917 ParmDecl.AddAttributes(ParseAttributes());
1918
Chris Lattner9f7564b2008-04-06 06:57:35 +00001919 // Remember this parsed parameter in ParamInfo.
1920 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1921
Douglas Gregor605de8d2008-12-16 21:30:33 +00001922 // DefArgToks is used when the parsing of default arguments needs
1923 // to be delayed.
1924 CachedTokens *DefArgToks = 0;
1925
Chris Lattner9f7564b2008-04-06 06:57:35 +00001926 // If no parameter was specified, verify that *something* was specified,
1927 // otherwise we have a missing type and identifier.
1928 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1929 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1930 // Completely missing, emit error.
1931 Diag(DSStart, diag::err_missing_param);
1932 } else {
1933 // Otherwise, we have something. Add it and let semantic analysis try
1934 // to grok it and add the result to the ParamInfo we are building.
1935
1936 // Inform the actions module about the parameter declarator, so it gets
1937 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00001938 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1939
1940 // Parse the default argument, if any. We parse the default
1941 // arguments in all dialects; the semantic analysis in
1942 // ActOnParamDefaultArgument will reject the default argument in
1943 // C.
1944 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00001945 SourceLocation EqualLoc = Tok.getLocation();
1946
Chris Lattner3e254fb2008-04-08 04:40:51 +00001947 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00001948 if (D.getContext() == Declarator::MemberContext) {
1949 // If we're inside a class definition, cache the tokens
1950 // corresponding to the default argument. We'll actually parse
1951 // them when we see the end of the class definition.
1952 // FIXME: Templates will require something similar.
1953 // FIXME: Can we use a smart pointer for Toks?
1954 DefArgToks = new CachedTokens;
1955
1956 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
1957 tok::semi, false)) {
1958 delete DefArgToks;
1959 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00001960 Actions.ActOnParamDefaultArgumentError(Param);
1961 } else
1962 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00001963 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00001964 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00001965 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00001966
1967 OwningExprResult DefArgResult(ParseAssignmentExpression());
1968 if (DefArgResult.isInvalid()) {
1969 Actions.ActOnParamDefaultArgumentError(Param);
1970 SkipUntil(tok::comma, tok::r_paren, true, true);
1971 } else {
1972 // Inform the actions module about the default argument
1973 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
1974 DefArgResult.release());
1975 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00001976 }
1977 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001978
1979 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00001980 ParmDecl.getIdentifierLoc(), Param,
1981 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001982 }
1983
1984 // If the next token is a comma, consume it and keep reading arguments.
1985 if (Tok.isNot(tok::comma)) break;
1986
1987 // Consume the comma.
1988 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00001989 }
1990
Chris Lattner9f7564b2008-04-06 06:57:35 +00001991 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00001992 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00001993
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001994 // If we have the closing ')', eat it.
1995 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1996
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001997 DeclSpec DS;
1998 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00001999 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002000 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor90a2c972008-11-25 03:22:00 +00002001
2002 // Parse exception-specification[opt].
2003 if (Tok.is(tok::kw_throw))
2004 ParseExceptionSpecification();
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002005 }
2006
Chris Lattner4b009652007-07-25 00:24:17 +00002007 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002008 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
2009 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002010 DS.getTypeQualifiers(),
Chris Lattner9f7564b2008-04-06 06:57:35 +00002011 LParenLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00002012}
2013
Chris Lattner35d9c912008-04-06 06:34:08 +00002014/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2015/// we found a K&R-style identifier list instead of a type argument list. The
2016/// current token is known to be the first identifier in the list.
2017///
2018/// identifier-list: [C99 6.7.5]
2019/// identifier
2020/// identifier-list ',' identifier
2021///
2022void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2023 Declarator &D) {
2024 // Build up an array of information about the parsed arguments.
2025 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2026 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2027
2028 // If there was no identifier specified for the declarator, either we are in
2029 // an abstract-declarator, or we are in a parameter declarator which was found
2030 // to be abstract. In abstract-declarators, identifier lists are not valid:
2031 // diagnose this.
2032 if (!D.getIdentifier())
2033 Diag(Tok, diag::ext_ident_list_in_param);
2034
2035 // Tok is known to be the first identifier in the list. Remember this
2036 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002037 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002038 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
2039 Tok.getLocation(), 0));
2040
Chris Lattner113a56b2008-04-06 06:39:19 +00002041 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002042
2043 while (Tok.is(tok::comma)) {
2044 // Eat the comma.
2045 ConsumeToken();
2046
Chris Lattner113a56b2008-04-06 06:39:19 +00002047 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002048 if (Tok.isNot(tok::identifier)) {
2049 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002050 SkipUntil(tok::r_paren);
2051 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002052 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002053
Chris Lattner35d9c912008-04-06 06:34:08 +00002054 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002055
2056 // Reject 'typedef int y; int test(x, y)', but continue parsing.
2057 if (Actions.isTypeName(*ParmII, CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002058 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002059
2060 // Verify that the argument identifier has not already been mentioned.
2061 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002062 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002063 } else {
2064 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002065 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2066 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00002067 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002068
2069 // Eat the identifier.
2070 ConsumeToken();
2071 }
2072
Chris Lattner113a56b2008-04-06 06:39:19 +00002073 // Remember that we parsed a function type, and remember the attributes. This
2074 // function type is always a K&R style function type, which is not varargs and
2075 // has no prototype.
2076 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
2077 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002078 /*TypeQuals*/0, LParenLoc));
Chris Lattner35d9c912008-04-06 06:34:08 +00002079
2080 // If we have the closing ')', eat it and we're done.
Chris Lattner113a56b2008-04-06 06:39:19 +00002081 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002082}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002083
Chris Lattner4b009652007-07-25 00:24:17 +00002084/// [C90] direct-declarator '[' constant-expression[opt] ']'
2085/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2086/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2087/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2088/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2089void Parser::ParseBracketDeclarator(Declarator &D) {
2090 SourceLocation StartLoc = ConsumeBracket();
2091
Chris Lattner1525c3a2008-12-18 07:27:21 +00002092 // C array syntax has many features, but by-far the most common is [] and [4].
2093 // This code does a fast path to handle some of the most obvious cases.
2094 if (Tok.getKind() == tok::r_square) {
2095 MatchRHSPunctuation(tok::r_square, StartLoc);
2096 // Remember that we parsed the empty array type.
2097 OwningExprResult NumElements(Actions);
2098 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc));
2099 return;
2100 } else if (Tok.getKind() == tok::numeric_constant &&
2101 GetLookAheadToken(1).is(tok::r_square)) {
2102 // [4] is very common. Parse the numeric constant expression.
2103 OwningExprResult ExprRes(Actions, Actions.ActOnNumericConstant(Tok));
2104 ConsumeToken();
2105
2106 MatchRHSPunctuation(tok::r_square, StartLoc);
2107
2108 // If there was an error parsing the assignment-expression, recover.
2109 if (ExprRes.isInvalid())
2110 ExprRes.release(); // Deallocate expr, just use [].
2111
2112 // Remember that we parsed a array type, and remember its features.
2113 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
2114 ExprRes.release(), StartLoc));
2115 return;
2116 }
2117
Chris Lattner4b009652007-07-25 00:24:17 +00002118 // If valid, this location is the position where we read the 'static' keyword.
2119 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002120 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002121 StaticLoc = ConsumeToken();
2122
2123 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002124 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002125 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002126 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002127
2128 // If we haven't already read 'static', check to see if there is one after the
2129 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002130 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002131 StaticLoc = ConsumeToken();
2132
2133 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2134 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002135 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002136
2137 // Handle the case where we have '[*]' as the array size. However, a leading
2138 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2139 // the the token after the star is a ']'. Since stars in arrays are
2140 // infrequent, use of lookahead is not costly here.
2141 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002142 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002143
Chris Lattner306d4df2008-12-18 06:50:14 +00002144 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002145 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002146 StaticLoc = SourceLocation(); // Drop the static.
2147 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002148 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002149 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002150 // Note, in C89, this production uses the constant-expr production instead
2151 // of assignment-expr. The only difference is that assignment-expr allows
2152 // things like '=' and '*='. Sema rejects these in C89 mode because they
2153 // are not i-c-e's, so we don't need to distinguish between the two here.
2154
Chris Lattner4b009652007-07-25 00:24:17 +00002155 // Parse the assignment-expression now.
2156 NumElements = ParseAssignmentExpression();
2157 }
2158
2159 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002160 if (NumElements.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002161 // If the expression was invalid, skip it.
2162 SkipUntil(tok::r_square);
2163 return;
2164 }
2165
2166 MatchRHSPunctuation(tok::r_square, StartLoc);
2167
Chris Lattner1525c3a2008-12-18 07:27:21 +00002168 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002169 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2170 StaticLoc.isValid(), isStar,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002171 NumElements.release(), StartLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00002172}
2173
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002174/// [GNU] typeof-specifier:
2175/// typeof ( expressions )
2176/// typeof ( type-name )
2177/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002178///
2179void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002180 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002181 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002182 SourceLocation StartLoc = ConsumeToken();
2183
Chris Lattner34a01ad2007-10-09 17:33:22 +00002184 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002185 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002186 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002187 return;
2188 }
2189
Sebastian Redl14ca7412008-12-11 21:36:32 +00002190 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002191 if (Result.isInvalid())
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002192 return;
2193
2194 const char *PrevSpec = 0;
2195 // Check for duplicate type specifiers.
2196 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002197 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002198 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002199
2200 // FIXME: Not accurate, the range gets one token more than it should.
2201 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002202 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002203 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002204
Steve Naroff7cbb1462007-07-31 12:34:36 +00002205 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2206
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002207 if (isTypeIdInParens()) {
Steve Naroff7cbb1462007-07-31 12:34:36 +00002208 TypeTy *Ty = ParseTypeName();
2209
Steve Naroff4c255ab2007-07-31 23:56:32 +00002210 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
2211
Chris Lattner34a01ad2007-10-09 17:33:22 +00002212 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002213 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002214 return;
2215 }
2216 RParenLoc = ConsumeParen();
2217 const char *PrevSpec = 0;
2218 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2219 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
Chris Lattnerf006a222008-11-18 07:48:38 +00002220 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002221 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002222 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002223
2224 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002225 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002226 return;
2227 }
2228 RParenLoc = ConsumeParen();
2229 const char *PrevSpec = 0;
2230 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2231 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002232 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002233 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002234 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002235 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002236}
2237
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002238