blob: a410dfeda326fe635e56c82ae74fc44d990c8414 [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,
441 TemplateParameterLists *TemplateParams)
Douglas Gregorb3bec712008-12-01 23:54:00 +0000442{
Chris Lattnera4ff4272008-03-13 06:29:04 +0000443 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000444 while (1) {
445 int isInvalid = false;
446 const char *PrevSpec = 0;
447 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000448
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000449 // Only annotate C++ scope. Allow class-name as an identifier in case
450 // it's a constructor.
Daniel Dunbar1afd88d2008-11-25 23:05:24 +0000451 if (getLang().CPlusPlus)
Argiris Kirtzidis91c80dc2008-11-26 21:41:52 +0000452 TryAnnotateCXXScopeToken();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000453
Chris Lattner4b009652007-07-25 00:24:17 +0000454 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000455 default:
Douglas Gregorb3bec712008-12-01 23:54:00 +0000456 // Try to parse a type-specifier; if we found one, continue. If it's not
457 // a type, this falls through.
Douglas Gregor52473432008-12-24 02:52:09 +0000458 if (MaybeParseTypeSpecifier(DS, isInvalid, PrevSpec, TemplateParams)) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000459 continue;
Douglas Gregorb3bec712008-12-01 23:54:00 +0000460 }
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000461
Chris Lattnerb99d7492008-07-26 00:20:22 +0000462 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000463 // If this is not a declaration specifier token, we're done reading decl
464 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000465 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000466 return;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000467
468 case tok::annot_cxxscope: {
469 if (DS.hasTypeSpecifier())
470 goto DoneWithDeclSpec;
471
472 // We are looking for a qualified typename.
473 if (NextToken().isNot(tok::identifier))
474 goto DoneWithDeclSpec;
475
476 CXXScopeSpec SS;
477 SS.setScopeRep(Tok.getAnnotationValue());
478 SS.setRange(Tok.getAnnotationRange());
479
480 // If the next token is the name of the class type that the C++ scope
481 // denotes, followed by a '(', then this is a constructor declaration.
482 // We're done with the decl-specifiers.
483 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
484 CurScope, &SS) &&
485 GetLookAheadToken(2).is(tok::l_paren))
486 goto DoneWithDeclSpec;
487
488 TypeTy *TypeRep = Actions.isTypeName(*NextToken().getIdentifierInfo(),
489 CurScope, &SS);
490 if (TypeRep == 0)
491 goto DoneWithDeclSpec;
492
493 ConsumeToken(); // The C++ scope.
494
495 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
496 TypeRep);
497 if (isInvalid)
498 break;
499
500 DS.SetRangeEnd(Tok.getLocation());
501 ConsumeToken(); // The typename.
502
503 continue;
504 }
505
Chris Lattnerfda18db2008-07-26 01:18:38 +0000506 // typedef-name
507 case tok::identifier: {
508 // This identifier can only be a typedef name if we haven't already seen
509 // a type-specifier. Without this check we misparse:
510 // typedef int X; struct Y { short X; }; as 'short int'.
511 if (DS.hasTypeSpecifier())
512 goto DoneWithDeclSpec;
513
514 // It has to be available as a typedef too!
Argiris Kirtzidis46403632008-08-01 10:35:27 +0000515 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattnerfda18db2008-07-26 01:18:38 +0000516 if (TypeRep == 0)
517 goto DoneWithDeclSpec;
518
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000519 // C++: If the identifier is actually the name of the class type
520 // being defined and the next token is a '(', then this is a
521 // constructor declaration. We're done with the decl-specifiers
522 // and will treat this token as an identifier.
523 if (getLang().CPlusPlus &&
524 CurScope->isCXXClassScope() &&
525 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
526 NextToken().getKind() == tok::l_paren)
527 goto DoneWithDeclSpec;
528
Chris Lattnerfda18db2008-07-26 01:18:38 +0000529 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
530 TypeRep);
531 if (isInvalid)
532 break;
533
534 DS.SetRangeEnd(Tok.getLocation());
535 ConsumeToken(); // The identifier
536
537 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
538 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
539 // Objective-C interface. If we don't have Objective-C or a '<', this is
540 // just a normal reference to a typedef name.
541 if (!Tok.is(tok::less) || !getLang().ObjC1)
542 continue;
543
544 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000545 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000546 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000547 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000548
549 DS.SetRangeEnd(EndProtoLoc);
550
Steve Narofff7683302008-09-22 10:28:57 +0000551 // Need to support trailing type qualifiers (e.g. "id<p> const").
552 // If a type specifier follows, it will be diagnosed elsewhere.
553 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000554 }
Chris Lattner4b009652007-07-25 00:24:17 +0000555 // GNU attributes support.
556 case tok::kw___attribute:
557 DS.AddAttributes(ParseAttributes());
558 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000559
560 // Microsoft declspec support.
561 case tok::kw___declspec:
562 if (!PP.getLangOptions().Microsoft)
563 goto DoneWithDeclSpec;
564 FuzzyParseMicrosoftDeclSpec();
565 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000566
Steve Naroffedd04d52008-12-25 14:16:32 +0000567 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000568 case tok::kw___forceinline:
569 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000570 case tok::kw___cdecl:
571 case tok::kw___stdcall:
572 case tok::kw___fastcall:
573 if (!PP.getLangOptions().Microsoft)
574 goto DoneWithDeclSpec;
575 // Just ignore it.
576 break;
577
Chris Lattner4b009652007-07-25 00:24:17 +0000578 // storage-class-specifier
579 case tok::kw_typedef:
580 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
581 break;
582 case tok::kw_extern:
583 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000584 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000585 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
586 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000587 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000588 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
589 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000590 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000591 case tok::kw_static:
592 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000593 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000594 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
595 break;
596 case tok::kw_auto:
597 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
598 break;
599 case tok::kw_register:
600 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
601 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000602 case tok::kw_mutable:
603 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
604 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000605 case tok::kw___thread:
606 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
607 break;
608
Chris Lattner4b009652007-07-25 00:24:17 +0000609 continue;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000610
Chris Lattner4b009652007-07-25 00:24:17 +0000611 // function-specifier
612 case tok::kw_inline:
613 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
614 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000615
616 case tok::kw_virtual:
617 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
618 break;
619
620 case tok::kw_explicit:
621 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
622 break;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000623
Steve Naroff5f0466b2008-06-05 00:02:44 +0000624 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000625 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000626 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
627 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000628 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000629 goto DoneWithDeclSpec;
630
631 {
632 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000633 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000634 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000635 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000636 DS.SetRangeEnd(EndProtoLoc);
637
Chris Lattnerf006a222008-11-18 07:48:38 +0000638 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
639 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000640 // Need to support trailing type qualifiers (e.g. "id<p> const").
641 // If a type specifier follows, it will be diagnosed elsewhere.
642 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000643 }
Chris Lattner4b009652007-07-25 00:24:17 +0000644 }
645 // If the specifier combination wasn't legal, issue a diagnostic.
646 if (isInvalid) {
647 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000648 // Pick between error or extwarn.
649 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
650 : diag::ext_duplicate_declspec;
651 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000652 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000653 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000654 ConsumeToken();
655 }
656}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000657
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000658/// MaybeParseTypeSpecifier - Try to parse a single type-specifier. We
659/// primarily follow the C++ grammar with additions for C99 and GNU,
660/// which together subsume the C grammar. Note that the C++
661/// type-specifier also includes the C type-qualifier (for const,
662/// volatile, and C99 restrict). Returns true if a type-specifier was
663/// found (and parsed), false otherwise.
664///
665/// type-specifier: [C++ 7.1.5]
666/// simple-type-specifier
667/// class-specifier
668/// enum-specifier
669/// elaborated-type-specifier [TODO]
670/// cv-qualifier
671///
672/// cv-qualifier: [C++ 7.1.5.1]
673/// 'const'
674/// 'volatile'
675/// [C99] 'restrict'
676///
677/// simple-type-specifier: [ C++ 7.1.5.2]
678/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
679/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
680/// 'char'
681/// 'wchar_t'
682/// 'bool'
683/// 'short'
684/// 'int'
685/// 'long'
686/// 'signed'
687/// 'unsigned'
688/// 'float'
689/// 'double'
690/// 'void'
691/// [C99] '_Bool'
692/// [C99] '_Complex'
693/// [C99] '_Imaginary' // Removed in TC2?
694/// [GNU] '_Decimal32'
695/// [GNU] '_Decimal64'
696/// [GNU] '_Decimal128'
697/// [GNU] typeof-specifier
698/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
699/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
700bool Parser::MaybeParseTypeSpecifier(DeclSpec &DS, int& isInvalid,
Douglas Gregor52473432008-12-24 02:52:09 +0000701 const char *&PrevSpec,
702 TemplateParameterLists *TemplateParams) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000703 // Annotate typenames and C++ scope specifiers.
704 TryAnnotateTypeOrScopeToken();
705
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000706 SourceLocation Loc = Tok.getLocation();
707
708 switch (Tok.getKind()) {
709 // simple-type-specifier:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000710 case tok::annot_qualtypename: {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000711 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000712 Tok.getAnnotationValue());
713 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
714 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000715
716 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
717 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
718 // Objective-C interface. If we don't have Objective-C or a '<', this is
719 // just a normal reference to a typedef name.
720 if (!Tok.is(tok::less) || !getLang().ObjC1)
721 return true;
722
723 SourceLocation EndProtoLoc;
724 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
725 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
726 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
727
728 DS.SetRangeEnd(EndProtoLoc);
729 return true;
730 }
731
732 case tok::kw_short:
733 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
734 break;
735 case tok::kw_long:
736 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
737 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
738 else
739 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
740 break;
741 case tok::kw_signed:
742 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
743 break;
744 case tok::kw_unsigned:
745 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
746 break;
747 case tok::kw__Complex:
748 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
749 break;
750 case tok::kw__Imaginary:
751 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
752 break;
753 case tok::kw_void:
754 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
755 break;
756 case tok::kw_char:
757 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
758 break;
759 case tok::kw_int:
760 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
761 break;
762 case tok::kw_float:
763 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
764 break;
765 case tok::kw_double:
766 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
767 break;
768 case tok::kw_wchar_t:
769 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
770 break;
771 case tok::kw_bool:
772 case tok::kw__Bool:
773 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
774 break;
775 case tok::kw__Decimal32:
776 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
777 break;
778 case tok::kw__Decimal64:
779 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
780 break;
781 case tok::kw__Decimal128:
782 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
783 break;
784
785 // class-specifier:
786 case tok::kw_class:
787 case tok::kw_struct:
788 case tok::kw_union:
Douglas Gregor52473432008-12-24 02:52:09 +0000789 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000790 return true;
791
792 // enum-specifier:
793 case tok::kw_enum:
794 ParseEnumSpecifier(DS);
795 return true;
796
797 // cv-qualifier:
798 case tok::kw_const:
799 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
800 getLang())*2;
801 break;
802 case tok::kw_volatile:
803 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
804 getLang())*2;
805 break;
806 case tok::kw_restrict:
807 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
808 getLang())*2;
809 break;
810
811 // GNU typeof support.
812 case tok::kw_typeof:
813 ParseTypeofSpecifier(DS);
814 return true;
815
Steve Naroffedd04d52008-12-25 14:16:32 +0000816 case tok::kw___cdecl:
817 case tok::kw___stdcall:
818 case tok::kw___fastcall:
819 return PP.getLangOptions().Microsoft;
820
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000821 default:
822 // Not a type-specifier; do nothing.
823 return false;
824 }
825
826 // If the specifier combination wasn't legal, issue a diagnostic.
827 if (isInvalid) {
828 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000829 // Pick between error or extwarn.
830 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
831 : diag::ext_duplicate_declspec;
832 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000833 }
834 DS.SetRangeEnd(Tok.getLocation());
835 ConsumeToken(); // whatever we parsed above.
836 return true;
837}
Chris Lattner4b009652007-07-25 00:24:17 +0000838
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000839/// ParseStructDeclaration - Parse a struct declaration without the terminating
840/// semicolon.
841///
Chris Lattner4b009652007-07-25 00:24:17 +0000842/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000843/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000844/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000845/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000846/// struct-declarator-list:
847/// struct-declarator
848/// struct-declarator-list ',' struct-declarator
849/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
850/// struct-declarator:
851/// declarator
852/// [GNU] declarator attributes[opt]
853/// declarator[opt] ':' constant-expression
854/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
855///
Chris Lattner3dd8d392008-04-10 06:46:29 +0000856void Parser::
857ParseStructDeclaration(DeclSpec &DS,
858 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000859 if (Tok.is(tok::kw___extension__)) {
860 // __extension__ silences extension warnings in the subexpression.
861 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +0000862 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000863 return ParseStructDeclaration(DS, Fields);
864 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000865
866 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000867 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +0000868 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +0000869
870 // If there are no declarators, issue a warning.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000871 if (Tok.is(tok::semi)) {
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000872 Diag(DSStart, diag::w_no_declarators);
Steve Naroffa9adf112007-08-20 22:28:22 +0000873 return;
874 }
875
876 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000877 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000878 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +0000879 FieldDeclarator &DeclaratorInfo = Fields.back();
880
Steve Naroffa9adf112007-08-20 22:28:22 +0000881 /// struct-declarator: declarator
882 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000883 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000884 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +0000885
Chris Lattner34a01ad2007-10-09 17:33:22 +0000886 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000887 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000888 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000889 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +0000890 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000891 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000892 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +0000893 }
894
895 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000896 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000897 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000898
899 // If we don't have a comma, it is either the end of the list (a ';')
900 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000901 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000902 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000903
904 // Consume the comma.
905 ConsumeToken();
906
907 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000908 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000909
910 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000911 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000912 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000913 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000914}
915
916/// ParseStructUnionBody
917/// struct-contents:
918/// struct-declaration-list
919/// [EXT] empty
920/// [GNU] "struct-declaration-list" without terminatoring ';'
921/// struct-declaration-list:
922/// struct-declaration
923/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +0000924/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +0000925///
Chris Lattner4b009652007-07-25 00:24:17 +0000926void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
927 unsigned TagType, DeclTy *TagDecl) {
928 SourceLocation LBraceLoc = ConsumeBrace();
929
930 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
931 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +0000932 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +0000933 Diag(Tok, diag::ext_empty_struct_union_enum)
934 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +0000935
936 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000937 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
938
Chris Lattner4b009652007-07-25 00:24:17 +0000939 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000940 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000941 // Each iteration of this loop reads one struct-declaration.
942
943 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000944 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000945 Diag(Tok, diag::ext_extra_struct_semi);
946 ConsumeToken();
947 continue;
948 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000949
950 // Parse all the comma separated declarators.
951 DeclSpec DS;
952 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +0000953 if (!Tok.is(tok::at)) {
954 ParseStructDeclaration(DS, FieldDeclarators);
955
956 // Convert them all to fields.
957 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
958 FieldDeclarator &FD = FieldDeclarators[i];
959 // Install the declarator into the current TagDecl.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000960 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
Chris Lattner1bf58f62008-06-21 19:39:06 +0000961 DS.getSourceRange().getBegin(),
962 FD.D, FD.BitfieldSize);
963 FieldDecls.push_back(Field);
964 }
965 } else { // Handle @defs
966 ConsumeToken();
967 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
968 Diag(Tok, diag::err_unexpected_at);
969 SkipUntil(tok::semi, true, true);
970 continue;
971 }
972 ConsumeToken();
973 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
974 if (!Tok.is(tok::identifier)) {
975 Diag(Tok, diag::err_expected_ident);
976 SkipUntil(tok::semi, true, true);
977 continue;
978 }
979 llvm::SmallVector<DeclTy*, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000980 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
981 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +0000982 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
983 ConsumeToken();
984 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
985 }
Chris Lattner4b009652007-07-25 00:24:17 +0000986
Chris Lattner34a01ad2007-10-09 17:33:22 +0000987 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000988 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +0000989 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000990 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +0000991 break;
992 } else {
993 Diag(Tok, diag::err_expected_semi_decl_list);
994 // Skip to end of block or statement
995 SkipUntil(tok::r_brace, true, true);
996 }
997 }
998
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000999 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001000
Chris Lattner4b009652007-07-25 00:24:17 +00001001 AttributeList *AttrList = 0;
1002 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001003 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001004 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001005
1006 Actions.ActOnFields(CurScope,
1007 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1008 LBraceLoc, RBraceLoc,
1009 AttrList);
Chris Lattner4b009652007-07-25 00:24:17 +00001010}
1011
1012
1013/// ParseEnumSpecifier
1014/// enum-specifier: [C99 6.7.2.2]
1015/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001016///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001017/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1018/// '}' attributes[opt]
1019/// 'enum' identifier
1020/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001021///
1022/// [C++] elaborated-type-specifier:
1023/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1024///
Chris Lattner4b009652007-07-25 00:24:17 +00001025void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001026 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +00001027 SourceLocation StartLoc = ConsumeToken();
1028
1029 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001030
1031 AttributeList *Attr = 0;
1032 // If attributes exist after tag, parse them.
1033 if (Tok.is(tok::kw___attribute))
1034 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001035
1036 CXXScopeSpec SS;
Argiris Kirtzidis91c80dc2008-11-26 21:41:52 +00001037 if (getLang().CPlusPlus && MaybeParseCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001038 if (Tok.isNot(tok::identifier)) {
1039 Diag(Tok, diag::err_expected_ident);
1040 if (Tok.isNot(tok::l_brace)) {
1041 // Has no name and is not a definition.
1042 // Skip the rest of this declarator, up until the comma or semicolon.
1043 SkipUntil(tok::comma, true);
1044 return;
1045 }
1046 }
1047 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001048
1049 // Must have either 'enum name' or 'enum {...}'.
1050 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1051 Diag(Tok, diag::err_expected_ident_lbrace);
1052
1053 // Skip the rest of this declarator, up until the comma or semicolon.
1054 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001055 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001056 }
1057
1058 // If an identifier is present, consume and remember it.
1059 IdentifierInfo *Name = 0;
1060 SourceLocation NameLoc;
1061 if (Tok.is(tok::identifier)) {
1062 Name = Tok.getIdentifierInfo();
1063 NameLoc = ConsumeToken();
1064 }
1065
1066 // There are three options here. If we have 'enum foo;', then this is a
1067 // forward declaration. If we have 'enum foo {...' then this is a
1068 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1069 //
1070 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1071 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1072 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1073 //
1074 Action::TagKind TK;
1075 if (Tok.is(tok::l_brace))
1076 TK = Action::TK_Definition;
1077 else if (Tok.is(tok::semi))
1078 TK = Action::TK_Declaration;
1079 else
1080 TK = Action::TK_Reference;
1081 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Douglas Gregor52473432008-12-24 02:52:09 +00001082 SS, Name, NameLoc, Attr,
1083 Action::MultiTemplateParamsArg(Actions));
Chris Lattner4b009652007-07-25 00:24:17 +00001084
Chris Lattner34a01ad2007-10-09 17:33:22 +00001085 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001086 ParseEnumBody(StartLoc, TagDecl);
1087
1088 // TODO: semantic analysis on the declspec for enums.
1089 const char *PrevSpec = 0;
1090 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerf006a222008-11-18 07:48:38 +00001091 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001092}
1093
1094/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1095/// enumerator-list:
1096/// enumerator
1097/// enumerator-list ',' enumerator
1098/// enumerator:
1099/// enumeration-constant
1100/// enumeration-constant '=' constant-expression
1101/// enumeration-constant:
1102/// identifier
1103///
1104void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
1105 SourceLocation LBraceLoc = ConsumeBrace();
1106
Chris Lattnerc9a92452007-08-27 17:24:30 +00001107 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001108 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001109 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001110
1111 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1112
1113 DeclTy *LastEnumConstDecl = 0;
1114
1115 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001116 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001117 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1118 SourceLocation IdentLoc = ConsumeToken();
1119
1120 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001121 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001122 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001123 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001124 AssignedVal = ParseConstantExpression();
1125 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001126 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001127 }
1128
1129 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +00001130 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +00001131 LastEnumConstDecl,
1132 IdentLoc, Ident,
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001133 EqualLoc,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001134 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001135 EnumConstantDecls.push_back(EnumConstDecl);
1136 LastEnumConstDecl = EnumConstDecl;
1137
Chris Lattner34a01ad2007-10-09 17:33:22 +00001138 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001139 break;
1140 SourceLocation CommaLoc = ConsumeToken();
1141
Chris Lattner34a01ad2007-10-09 17:33:22 +00001142 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +00001143 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1144 }
1145
1146 // Eat the }.
1147 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1148
Steve Naroff0acc9c92007-09-15 18:49:24 +00001149 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001150 EnumConstantDecls.size());
1151
1152 DeclTy *AttrList = 0;
1153 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001154 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001155 AttrList = ParseAttributes(); // FIXME: where do they do?
1156}
1157
1158/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001159/// start of a type-qualifier-list.
1160bool Parser::isTypeQualifier() const {
1161 switch (Tok.getKind()) {
1162 default: return false;
1163 // type-qualifier
1164 case tok::kw_const:
1165 case tok::kw_volatile:
1166 case tok::kw_restrict:
1167 return true;
1168 }
1169}
1170
1171/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001172/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001173bool Parser::isTypeSpecifierQualifier() {
1174 // Annotate typenames and C++ scope specifiers.
1175 TryAnnotateTypeOrScopeToken();
1176
Chris Lattner4b009652007-07-25 00:24:17 +00001177 switch (Tok.getKind()) {
1178 default: return false;
1179 // GNU attributes support.
1180 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001181 // GNU typeof support.
1182 case tok::kw_typeof:
1183
Chris Lattner4b009652007-07-25 00:24:17 +00001184 // type-specifiers
1185 case tok::kw_short:
1186 case tok::kw_long:
1187 case tok::kw_signed:
1188 case tok::kw_unsigned:
1189 case tok::kw__Complex:
1190 case tok::kw__Imaginary:
1191 case tok::kw_void:
1192 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001193 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001194 case tok::kw_int:
1195 case tok::kw_float:
1196 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001197 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001198 case tok::kw__Bool:
1199 case tok::kw__Decimal32:
1200 case tok::kw__Decimal64:
1201 case tok::kw__Decimal128:
1202
Chris Lattner2e78db32008-04-13 18:59:07 +00001203 // struct-or-union-specifier (C99) or class-specifier (C++)
1204 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001205 case tok::kw_struct:
1206 case tok::kw_union:
1207 // enum-specifier
1208 case tok::kw_enum:
1209
1210 // type-qualifier
1211 case tok::kw_const:
1212 case tok::kw_volatile:
1213 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001214
1215 // typedef-name
1216 case tok::annot_qualtypename:
Chris Lattner4b009652007-07-25 00:24:17 +00001217 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001218
1219 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1220 case tok::less:
1221 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001222
1223 case tok::kw___cdecl:
1224 case tok::kw___stdcall:
1225 case tok::kw___fastcall:
1226 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001227 }
1228}
1229
1230/// isDeclarationSpecifier() - Return true if the current token is part of a
1231/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001232bool Parser::isDeclarationSpecifier() {
1233 // Annotate typenames and C++ scope specifiers.
1234 TryAnnotateTypeOrScopeToken();
1235
Chris Lattner4b009652007-07-25 00:24:17 +00001236 switch (Tok.getKind()) {
1237 default: return false;
1238 // storage-class-specifier
1239 case tok::kw_typedef:
1240 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001241 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001242 case tok::kw_static:
1243 case tok::kw_auto:
1244 case tok::kw_register:
1245 case tok::kw___thread:
1246
1247 // type-specifiers
1248 case tok::kw_short:
1249 case tok::kw_long:
1250 case tok::kw_signed:
1251 case tok::kw_unsigned:
1252 case tok::kw__Complex:
1253 case tok::kw__Imaginary:
1254 case tok::kw_void:
1255 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001256 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001257 case tok::kw_int:
1258 case tok::kw_float:
1259 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001260 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001261 case tok::kw__Bool:
1262 case tok::kw__Decimal32:
1263 case tok::kw__Decimal64:
1264 case tok::kw__Decimal128:
1265
Chris Lattner2e78db32008-04-13 18:59:07 +00001266 // struct-or-union-specifier (C99) or class-specifier (C++)
1267 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001268 case tok::kw_struct:
1269 case tok::kw_union:
1270 // enum-specifier
1271 case tok::kw_enum:
1272
1273 // type-qualifier
1274 case tok::kw_const:
1275 case tok::kw_volatile:
1276 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001277
Chris Lattner4b009652007-07-25 00:24:17 +00001278 // function-specifier
1279 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001280 case tok::kw_virtual:
1281 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001282
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001283 // typedef-name
1284 case tok::annot_qualtypename:
1285
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001286 // GNU typeof support.
1287 case tok::kw_typeof:
1288
1289 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001290 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001291 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001292
1293 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1294 case tok::less:
1295 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001296
1297 case tok::kw___cdecl:
1298 case tok::kw___stdcall:
1299 case tok::kw___fastcall:
1300 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001301 }
1302}
1303
1304
1305/// ParseTypeQualifierListOpt
1306/// type-qualifier-list: [C99 6.7.5]
1307/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001308/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001309/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001310/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001311///
Chris Lattner460696f2008-12-18 07:02:59 +00001312void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001313 while (1) {
1314 int isInvalid = false;
1315 const char *PrevSpec = 0;
1316 SourceLocation Loc = Tok.getLocation();
1317
1318 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001319 case tok::kw_const:
1320 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1321 getLang())*2;
1322 break;
1323 case tok::kw_volatile:
1324 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1325 getLang())*2;
1326 break;
1327 case tok::kw_restrict:
1328 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1329 getLang())*2;
1330 break;
Steve Naroffad620402008-12-25 14:41:26 +00001331 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001332 case tok::kw___cdecl:
1333 case tok::kw___stdcall:
1334 case tok::kw___fastcall:
1335 if (!PP.getLangOptions().Microsoft)
1336 goto DoneWithTypeQuals;
1337 // Just ignore it.
1338 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001339 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001340 if (AttributesAllowed) {
1341 DS.AddAttributes(ParseAttributes());
1342 continue; // do *not* consume the next token!
1343 }
1344 // otherwise, FALL THROUGH!
1345 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001346 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001347 // If this is not a type-qualifier token, we're done reading type
1348 // qualifiers. First verify that DeclSpec's are consistent.
1349 DS.Finish(Diags, PP.getSourceManager(), getLang());
1350 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001351 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001352
Chris Lattner4b009652007-07-25 00:24:17 +00001353 // If the specifier combination wasn't legal, issue a diagnostic.
1354 if (isInvalid) {
1355 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001356 // Pick between error or extwarn.
1357 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1358 : diag::ext_duplicate_declspec;
1359 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001360 }
1361 ConsumeToken();
1362 }
1363}
1364
1365
1366/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1367///
1368void Parser::ParseDeclarator(Declarator &D) {
1369 /// This implements the 'declarator' production in the C grammar, then checks
1370 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001371 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001372}
1373
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001374/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1375/// is parsed by the function passed to it. Pass null, and the direct-declarator
1376/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001377/// ptr-operator production.
1378///
Chris Lattner4b009652007-07-25 00:24:17 +00001379/// declarator: [C99 6.7.5]
1380/// pointer[opt] direct-declarator
1381/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1382/// [GNU] '&' restrict[opt] attributes[opt] declarator
1383///
1384/// pointer: [C99 6.7.5]
1385/// '*' type-qualifier-list[opt]
1386/// '*' type-qualifier-list[opt] pointer
1387///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001388/// ptr-operator:
1389/// '*' cv-qualifier-seq[opt]
1390/// '&'
1391/// [GNU] '&' restrict[opt] attributes[opt]
1392/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] [TODO]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001393void Parser::ParseDeclaratorInternal(Declarator &D,
1394 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001395 tok::TokenKind Kind = Tok.getKind();
1396
Steve Naroff7aa54752008-08-27 16:04:49 +00001397 // Not a pointer, C++ reference, or block.
1398 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001399 (Kind != tok::caret || !getLang().Blocks)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001400 if (DirectDeclParser)
1401 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001402 return;
1403 }
Chris Lattner4b009652007-07-25 00:24:17 +00001404
Steve Naroffdc22f212008-08-28 10:07:06 +00001405 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Chris Lattner4b009652007-07-25 00:24:17 +00001406 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1407
Steve Naroffdc22f212008-08-28 10:07:06 +00001408 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner69f01932008-02-21 01:32:26 +00001409 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001410 DeclSpec DS;
1411
1412 ParseTypeQualifierListOpt(DS);
1413
1414 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001415 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001416 if (Kind == tok::star)
1417 // Remember that we parsed a pointer type, and remember the type-quals.
1418 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1419 DS.TakeAttributes()));
1420 else
1421 // Remember that we parsed a Block type, and remember the type-quals.
1422 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1423 Loc));
Chris Lattner4b009652007-07-25 00:24:17 +00001424 } else {
1425 // Is a reference
1426 DeclSpec DS;
1427
1428 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1429 // cv-qualifiers are introduced through the use of a typedef or of a
1430 // template type argument, in which case the cv-qualifiers are ignored.
1431 //
1432 // [GNU] Retricted references are allowed.
1433 // [GNU] Attributes on references are allowed.
1434 ParseTypeQualifierListOpt(DS);
1435
1436 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1437 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1438 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001439 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001440 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1441 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001442 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001443 }
1444
1445 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001446 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001447
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001448 if (D.getNumTypeObjects() > 0) {
1449 // C++ [dcl.ref]p4: There shall be no references to references.
1450 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1451 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001452 if (const IdentifierInfo *II = D.getIdentifier())
1453 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1454 << II;
1455 else
1456 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1457 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001458
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001459 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001460 // can go ahead and build the (technically ill-formed)
1461 // declarator: reference collapsing will take care of it.
1462 }
1463 }
1464
Chris Lattner4b009652007-07-25 00:24:17 +00001465 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001466 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1467 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001468 }
1469}
1470
1471/// ParseDirectDeclarator
1472/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001473/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001474/// '(' declarator ')'
1475/// [GNU] '(' attributes declarator ')'
1476/// [C90] direct-declarator '[' constant-expression[opt] ']'
1477/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1478/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1479/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1480/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1481/// direct-declarator '(' parameter-type-list ')'
1482/// direct-declarator '(' identifier-list[opt] ')'
1483/// [GNU] direct-declarator '(' parameter-forward-declarations
1484/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001485/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1486/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001487/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001488///
1489/// declarator-id: [C++ 8]
1490/// id-expression
1491/// '::'[opt] nested-name-specifier[opt] type-name
1492///
1493/// id-expression: [C++ 5.1]
1494/// unqualified-id
1495/// qualified-id [TODO]
1496///
1497/// unqualified-id: [C++ 5.1]
1498/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001499/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001500/// conversion-function-id [TODO]
1501/// '~' class-name
1502/// template-id [TODO]
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001503///
Chris Lattner4b009652007-07-25 00:24:17 +00001504void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001505 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001506
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001507 if (getLang().CPlusPlus) {
1508 if (D.mayHaveIdentifier()) {
1509 bool afterCXXScope = MaybeParseCXXScopeSpecifier(D.getCXXScopeSpec());
1510 if (afterCXXScope) {
1511 // Change the declaration context for name lookup, until this function
1512 // is exited (and the declarator has been parsed).
1513 DeclScopeObj.EnterDeclaratorScope();
1514 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001515
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001516 if (Tok.is(tok::identifier)) {
1517 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor2fa10442008-12-18 19:37:40 +00001518
1519 // If this identifier is followed by a '<', we may have a template-id.
1520 DeclTy *Template;
Douglas Gregor853dd392008-12-26 15:00:45 +00001521 if (NextToken().is(tok::less) &&
Douglas Gregor2fa10442008-12-18 19:37:40 +00001522 (Template = Actions.isTemplateName(*Tok.getIdentifierInfo(),
1523 CurScope))) {
1524 IdentifierInfo *II = Tok.getIdentifierInfo();
1525 AnnotateTemplateIdToken(Template, 0);
1526 // FIXME: Set the declarator to a template-id. How? I don't
1527 // know... for now, just use the identifier.
1528 D.SetIdentifier(II, Tok.getLocation());
1529 }
1530 // If this identifier is the name of the current class, it's a
1531 // constructor name.
Douglas Gregor853dd392008-12-26 15:00:45 +00001532 else if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope))
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001533 D.setConstructor(Actions.isTypeName(*Tok.getIdentifierInfo(),
1534 CurScope),
1535 Tok.getLocation());
Douglas Gregor2fa10442008-12-18 19:37:40 +00001536 // This is a normal identifier.
1537 else
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001538 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1539 ConsumeToken();
1540 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00001541 } else if (Tok.is(tok::kw_operator)) {
1542 SourceLocation OperatorLoc = Tok.getLocation();
Douglas Gregore60e5d32008-11-06 22:13:31 +00001543
Douglas Gregor853dd392008-12-26 15:00:45 +00001544 // First try the name of an overloaded operator
1545 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId()) {
1546 D.setOverloadedOperator(Op, OperatorLoc);
1547 } else {
1548 // This must be a conversion function (C++ [class.conv.fct]).
1549 if (TypeTy *ConvType = ParseConversionFunctionId())
1550 D.setConversionFunction(ConvType, OperatorLoc);
1551 else
1552 D.SetIdentifier(0, Tok.getLocation());
1553 }
1554 goto PastIdentifier;
1555 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001556 // This should be a C++ destructor.
1557 SourceLocation TildeLoc = ConsumeToken();
1558 if (Tok.is(tok::identifier)) {
1559 if (TypeTy *Type = ParseClassName())
1560 D.setDestructor(Type, TildeLoc);
1561 else
1562 D.SetIdentifier(0, TildeLoc);
1563 } else {
1564 Diag(Tok, diag::err_expected_class_name);
1565 D.SetIdentifier(0, TildeLoc);
1566 }
1567 goto PastIdentifier;
1568 }
1569
1570 // If we reached this point, token is not identifier and not '~'.
1571
1572 if (afterCXXScope) {
1573 Diag(Tok, diag::err_expected_unqualified_id);
1574 D.SetIdentifier(0, Tok.getLocation());
1575 D.setInvalidType(true);
1576 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001577 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001578 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001579 }
1580
1581 // If we reached this point, we are either in C/ObjC or the token didn't
1582 // satisfy any of the C++-specific checks.
1583
1584 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1585 assert(!getLang().CPlusPlus &&
1586 "There's a C++-specific check for tok::identifier above");
1587 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1588 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1589 ConsumeToken();
1590 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001591 // direct-declarator: '(' declarator ')'
1592 // direct-declarator: '(' attributes declarator ')'
1593 // Example: 'char (*X)' or 'int (*XX)(void)'
1594 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001595 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001596 // This could be something simple like "int" (in which case the declarator
1597 // portion is empty), if an abstract-declarator is allowed.
1598 D.SetIdentifier(0, Tok.getLocation());
1599 } else {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001600 if (getLang().CPlusPlus)
1601 Diag(Tok, diag::err_expected_unqualified_id);
1602 else
Chris Lattnerf006a222008-11-18 07:48:38 +00001603 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00001604 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00001605 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001606 }
1607
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001608 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00001609 assert(D.isPastIdentifier() &&
1610 "Haven't past the location of the identifier yet?");
1611
1612 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001613 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001614 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1615 // In such a case, check if we actually have a function declarator; if it
1616 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001617 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1618 // When not in file scope, warn for ambiguous function declarators, just
1619 // in case the author intended it as a variable definition.
1620 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1621 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1622 break;
1623 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001624 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001625 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001626 ParseBracketDeclarator(D);
1627 } else {
1628 break;
1629 }
1630 }
1631}
1632
Chris Lattnera0d056d2008-04-06 05:45:57 +00001633/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1634/// only called before the identifier, so these are most likely just grouping
1635/// parens for precedence. If we find that these are actually function
1636/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1637///
1638/// direct-declarator:
1639/// '(' declarator ')'
1640/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001641/// direct-declarator '(' parameter-type-list ')'
1642/// direct-declarator '(' identifier-list[opt] ')'
1643/// [GNU] direct-declarator '(' parameter-forward-declarations
1644/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001645///
1646void Parser::ParseParenDeclarator(Declarator &D) {
1647 SourceLocation StartLoc = ConsumeParen();
1648 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1649
Chris Lattner1f185292008-10-20 02:05:46 +00001650 // Eat any attributes before we look at whether this is a grouping or function
1651 // declarator paren. If this is a grouping paren, the attribute applies to
1652 // the type being built up, for example:
1653 // int (__attribute__(()) *x)(long y)
1654 // If this ends up not being a grouping paren, the attribute applies to the
1655 // first argument, for example:
1656 // int (__attribute__(()) int x)
1657 // In either case, we need to eat any attributes to be able to determine what
1658 // sort of paren this is.
1659 //
1660 AttributeList *AttrList = 0;
1661 bool RequiresArg = false;
1662 if (Tok.is(tok::kw___attribute)) {
1663 AttrList = ParseAttributes();
1664
1665 // We require that the argument list (if this is a non-grouping paren) be
1666 // present even if the attribute list was empty.
1667 RequiresArg = true;
1668 }
Steve Naroffedd04d52008-12-25 14:16:32 +00001669 // Eat any Microsoft extensions.
1670 if ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
1671 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
1672 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00001673
Chris Lattnera0d056d2008-04-06 05:45:57 +00001674 // If we haven't past the identifier yet (or where the identifier would be
1675 // stored, if this is an abstract declarator), then this is probably just
1676 // grouping parens. However, if this could be an abstract-declarator, then
1677 // this could also be the start of function arguments (consider 'void()').
1678 bool isGrouping;
1679
1680 if (!D.mayOmitIdentifier()) {
1681 // If this can't be an abstract-declarator, this *must* be a grouping
1682 // paren, because we haven't seen the identifier yet.
1683 isGrouping = true;
1684 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001685 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00001686 isDeclarationSpecifier()) { // 'int(int)' is a function.
1687 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1688 // considered to be a type, not a K&R identifier-list.
1689 isGrouping = false;
1690 } else {
1691 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1692 isGrouping = true;
1693 }
1694
1695 // If this is a grouping paren, handle:
1696 // direct-declarator: '(' declarator ')'
1697 // direct-declarator: '(' attributes declarator ')'
1698 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001699 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001700 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00001701 if (AttrList)
1702 D.AddAttributes(AttrList);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001703
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001704 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001705 // Match the ')'.
1706 MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001707
1708 D.setGroupingParens(hadGroupingParens);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001709 return;
1710 }
1711
1712 // Okay, if this wasn't a grouping paren, it must be the start of a function
1713 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00001714 // identifier (and remember where it would have been), then call into
1715 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00001716 D.SetIdentifier(0, Tok.getLocation());
1717
Chris Lattner1f185292008-10-20 02:05:46 +00001718 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001719}
1720
1721/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1722/// declarator D up to a paren, which indicates that we are parsing function
1723/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001724///
Chris Lattner1f185292008-10-20 02:05:46 +00001725/// If AttrList is non-null, then the caller parsed those arguments immediately
1726/// after the open paren - they should be considered to be the first argument of
1727/// a parameter. If RequiresArg is true, then the first argument of the
1728/// function is required to be present and required to not be an identifier
1729/// list.
1730///
Chris Lattner4b009652007-07-25 00:24:17 +00001731/// This method also handles this portion of the grammar:
1732/// parameter-type-list: [C99 6.7.5]
1733/// parameter-list
1734/// parameter-list ',' '...'
1735///
1736/// parameter-list: [C99 6.7.5]
1737/// parameter-declaration
1738/// parameter-list ',' parameter-declaration
1739///
1740/// parameter-declaration: [C99 6.7.5]
1741/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00001742/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001743/// [GNU] declaration-specifiers declarator attributes
1744/// declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00001745/// [C++] declaration-specifiers abstract-declarator[opt]
1746/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001747/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1748///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001749/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
1750/// and "exception-specification[opt]"(TODO).
1751///
Chris Lattner1f185292008-10-20 02:05:46 +00001752void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
1753 AttributeList *AttrList,
1754 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00001755 // lparen is already consumed!
1756 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00001757
Chris Lattner1f185292008-10-20 02:05:46 +00001758 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001759 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00001760 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001761 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00001762 delete AttrList;
1763 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001764
1765 ConsumeParen(); // Eat the closing ')'.
1766
1767 // cv-qualifier-seq[opt].
1768 DeclSpec DS;
1769 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00001770 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor90a2c972008-11-25 03:22:00 +00001771
1772 // Parse exception-specification[opt].
1773 if (Tok.is(tok::kw_throw))
1774 ParseExceptionSpecification();
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001775 }
1776
Chris Lattner9f7564b2008-04-06 06:57:35 +00001777 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00001778 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001779 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00001780 /*variadic*/ false,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001781 /*arglist*/ 0, 0,
1782 DS.getTypeQualifiers(),
1783 LParenLoc));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001784 return;
Chris Lattner1f185292008-10-20 02:05:46 +00001785 }
1786
1787 // Alternatively, this parameter list may be an identifier list form for a
1788 // K&R-style function: void foo(a,b,c)
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001789 if (!getLang().CPlusPlus && Tok.is(tok::identifier) &&
Chris Lattner1f185292008-10-20 02:05:46 +00001790 // K&R identifier lists can't have typedefs as identifiers, per
1791 // C99 6.7.5.3p11.
1792 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1793 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001794 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00001795 delete AttrList;
1796 }
1797
Chris Lattner4b009652007-07-25 00:24:17 +00001798 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1799 // normal declarators, not for abstract-declarators.
Chris Lattner35d9c912008-04-06 06:34:08 +00001800 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001801 }
1802
1803 // Finally, a normal, non-empty parameter type list.
1804
1805 // Build up an array of information about the parsed arguments.
1806 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00001807
1808 // Enter function-declaration scope, limiting any declarators to the
1809 // function prototype scope, including parameter declarators.
Douglas Gregor95d40792008-12-10 06:34:36 +00001810 ParseScope PrototypeScope(this, Scope::FnScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001811
1812 bool IsVariadic = false;
1813 while (1) {
1814 if (Tok.is(tok::ellipsis)) {
1815 IsVariadic = true;
Chris Lattner4b009652007-07-25 00:24:17 +00001816
Chris Lattner9f7564b2008-04-06 06:57:35 +00001817 // Check to see if this is "void(...)" which is not allowed.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001818 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00001819 // Otherwise, parse parameter type list. If it starts with an
1820 // ellipsis, diagnose the malformed function.
1821 Diag(Tok, diag::err_ellipsis_first_arg);
1822 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001823 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00001824
Chris Lattner9f7564b2008-04-06 06:57:35 +00001825 ConsumeToken(); // Consume the ellipsis.
1826 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001827 }
1828
Chris Lattner9f7564b2008-04-06 06:57:35 +00001829 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00001830
Chris Lattner9f7564b2008-04-06 06:57:35 +00001831 // Parse the declaration-specifiers.
1832 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00001833
1834 // If the caller parsed attributes for the first argument, add them now.
1835 if (AttrList) {
1836 DS.AddAttributes(AttrList);
1837 AttrList = 0; // Only apply the attributes to the first parameter.
1838 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001839 ParseDeclarationSpecifiers(DS);
1840
1841 // Parse the declarator. This is "PrototypeContext", because we must
1842 // accept either 'declarator' or 'abstract-declarator' here.
1843 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1844 ParseDeclarator(ParmDecl);
1845
1846 // Parse GNU attributes, if present.
1847 if (Tok.is(tok::kw___attribute))
1848 ParmDecl.AddAttributes(ParseAttributes());
1849
Chris Lattner9f7564b2008-04-06 06:57:35 +00001850 // Remember this parsed parameter in ParamInfo.
1851 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1852
Douglas Gregor605de8d2008-12-16 21:30:33 +00001853 // DefArgToks is used when the parsing of default arguments needs
1854 // to be delayed.
1855 CachedTokens *DefArgToks = 0;
1856
Chris Lattner9f7564b2008-04-06 06:57:35 +00001857 // If no parameter was specified, verify that *something* was specified,
1858 // otherwise we have a missing type and identifier.
1859 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1860 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1861 // Completely missing, emit error.
1862 Diag(DSStart, diag::err_missing_param);
1863 } else {
1864 // Otherwise, we have something. Add it and let semantic analysis try
1865 // to grok it and add the result to the ParamInfo we are building.
1866
1867 // Inform the actions module about the parameter declarator, so it gets
1868 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00001869 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1870
1871 // Parse the default argument, if any. We parse the default
1872 // arguments in all dialects; the semantic analysis in
1873 // ActOnParamDefaultArgument will reject the default argument in
1874 // C.
1875 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00001876 SourceLocation EqualLoc = Tok.getLocation();
1877
Chris Lattner3e254fb2008-04-08 04:40:51 +00001878 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00001879 if (D.getContext() == Declarator::MemberContext) {
1880 // If we're inside a class definition, cache the tokens
1881 // corresponding to the default argument. We'll actually parse
1882 // them when we see the end of the class definition.
1883 // FIXME: Templates will require something similar.
1884 // FIXME: Can we use a smart pointer for Toks?
1885 DefArgToks = new CachedTokens;
1886
1887 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
1888 tok::semi, false)) {
1889 delete DefArgToks;
1890 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00001891 Actions.ActOnParamDefaultArgumentError(Param);
1892 } else
1893 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00001894 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00001895 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00001896 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00001897
1898 OwningExprResult DefArgResult(ParseAssignmentExpression());
1899 if (DefArgResult.isInvalid()) {
1900 Actions.ActOnParamDefaultArgumentError(Param);
1901 SkipUntil(tok::comma, tok::r_paren, true, true);
1902 } else {
1903 // Inform the actions module about the default argument
1904 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
1905 DefArgResult.release());
1906 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00001907 }
1908 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001909
1910 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00001911 ParmDecl.getIdentifierLoc(), Param,
1912 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001913 }
1914
1915 // If the next token is a comma, consume it and keep reading arguments.
1916 if (Tok.isNot(tok::comma)) break;
1917
1918 // Consume the comma.
1919 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00001920 }
1921
Chris Lattner9f7564b2008-04-06 06:57:35 +00001922 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00001923 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00001924
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001925 // If we have the closing ')', eat it.
1926 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1927
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001928 DeclSpec DS;
1929 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00001930 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00001931 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor90a2c972008-11-25 03:22:00 +00001932
1933 // Parse exception-specification[opt].
1934 if (Tok.is(tok::kw_throw))
1935 ParseExceptionSpecification();
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001936 }
1937
Chris Lattner4b009652007-07-25 00:24:17 +00001938 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001939 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1940 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001941 DS.getTypeQualifiers(),
Chris Lattner9f7564b2008-04-06 06:57:35 +00001942 LParenLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00001943}
1944
Chris Lattner35d9c912008-04-06 06:34:08 +00001945/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1946/// we found a K&R-style identifier list instead of a type argument list. The
1947/// current token is known to be the first identifier in the list.
1948///
1949/// identifier-list: [C99 6.7.5]
1950/// identifier
1951/// identifier-list ',' identifier
1952///
1953void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1954 Declarator &D) {
1955 // Build up an array of information about the parsed arguments.
1956 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1957 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1958
1959 // If there was no identifier specified for the declarator, either we are in
1960 // an abstract-declarator, or we are in a parameter declarator which was found
1961 // to be abstract. In abstract-declarators, identifier lists are not valid:
1962 // diagnose this.
1963 if (!D.getIdentifier())
1964 Diag(Tok, diag::ext_ident_list_in_param);
1965
1966 // Tok is known to be the first identifier in the list. Remember this
1967 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00001968 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00001969 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1970 Tok.getLocation(), 0));
1971
Chris Lattner113a56b2008-04-06 06:39:19 +00001972 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00001973
1974 while (Tok.is(tok::comma)) {
1975 // Eat the comma.
1976 ConsumeToken();
1977
Chris Lattner113a56b2008-04-06 06:39:19 +00001978 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00001979 if (Tok.isNot(tok::identifier)) {
1980 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00001981 SkipUntil(tok::r_paren);
1982 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00001983 }
Chris Lattneracb67d92008-04-06 06:47:48 +00001984
Chris Lattner35d9c912008-04-06 06:34:08 +00001985 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00001986
1987 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1988 if (Actions.isTypeName(*ParmII, CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00001989 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00001990
1991 // Verify that the argument identifier has not already been mentioned.
1992 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001993 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00001994 } else {
1995 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00001996 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1997 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00001998 }
Chris Lattner35d9c912008-04-06 06:34:08 +00001999
2000 // Eat the identifier.
2001 ConsumeToken();
2002 }
2003
Chris Lattner113a56b2008-04-06 06:39:19 +00002004 // Remember that we parsed a function type, and remember the attributes. This
2005 // function type is always a K&R style function type, which is not varargs and
2006 // has no prototype.
2007 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
2008 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002009 /*TypeQuals*/0, LParenLoc));
Chris Lattner35d9c912008-04-06 06:34:08 +00002010
2011 // If we have the closing ')', eat it and we're done.
Chris Lattner113a56b2008-04-06 06:39:19 +00002012 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002013}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002014
Chris Lattner4b009652007-07-25 00:24:17 +00002015/// [C90] direct-declarator '[' constant-expression[opt] ']'
2016/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2017/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2018/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2019/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2020void Parser::ParseBracketDeclarator(Declarator &D) {
2021 SourceLocation StartLoc = ConsumeBracket();
2022
Chris Lattner1525c3a2008-12-18 07:27:21 +00002023 // C array syntax has many features, but by-far the most common is [] and [4].
2024 // This code does a fast path to handle some of the most obvious cases.
2025 if (Tok.getKind() == tok::r_square) {
2026 MatchRHSPunctuation(tok::r_square, StartLoc);
2027 // Remember that we parsed the empty array type.
2028 OwningExprResult NumElements(Actions);
2029 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc));
2030 return;
2031 } else if (Tok.getKind() == tok::numeric_constant &&
2032 GetLookAheadToken(1).is(tok::r_square)) {
2033 // [4] is very common. Parse the numeric constant expression.
2034 OwningExprResult ExprRes(Actions, Actions.ActOnNumericConstant(Tok));
2035 ConsumeToken();
2036
2037 MatchRHSPunctuation(tok::r_square, StartLoc);
2038
2039 // If there was an error parsing the assignment-expression, recover.
2040 if (ExprRes.isInvalid())
2041 ExprRes.release(); // Deallocate expr, just use [].
2042
2043 // Remember that we parsed a array type, and remember its features.
2044 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
2045 ExprRes.release(), StartLoc));
2046 return;
2047 }
2048
Chris Lattner4b009652007-07-25 00:24:17 +00002049 // If valid, this location is the position where we read the 'static' keyword.
2050 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002051 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002052 StaticLoc = ConsumeToken();
2053
2054 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002055 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002056 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002057 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002058
2059 // If we haven't already read 'static', check to see if there is one after the
2060 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002061 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002062 StaticLoc = ConsumeToken();
2063
2064 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2065 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002066 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002067
2068 // Handle the case where we have '[*]' as the array size. However, a leading
2069 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2070 // the the token after the star is a ']'. Since stars in arrays are
2071 // infrequent, use of lookahead is not costly here.
2072 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002073 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002074
Chris Lattner306d4df2008-12-18 06:50:14 +00002075 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002076 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002077 StaticLoc = SourceLocation(); // Drop the static.
2078 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002079 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002080 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002081 // Note, in C89, this production uses the constant-expr production instead
2082 // of assignment-expr. The only difference is that assignment-expr allows
2083 // things like '=' and '*='. Sema rejects these in C89 mode because they
2084 // are not i-c-e's, so we don't need to distinguish between the two here.
2085
Chris Lattner4b009652007-07-25 00:24:17 +00002086 // Parse the assignment-expression now.
2087 NumElements = ParseAssignmentExpression();
2088 }
2089
2090 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002091 if (NumElements.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002092 // If the expression was invalid, skip it.
2093 SkipUntil(tok::r_square);
2094 return;
2095 }
2096
2097 MatchRHSPunctuation(tok::r_square, StartLoc);
2098
Chris Lattner1525c3a2008-12-18 07:27:21 +00002099 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002100 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2101 StaticLoc.isValid(), isStar,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002102 NumElements.release(), StartLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00002103}
2104
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002105/// [GNU] typeof-specifier:
2106/// typeof ( expressions )
2107/// typeof ( type-name )
2108/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002109///
2110void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002111 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002112 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002113 SourceLocation StartLoc = ConsumeToken();
2114
Chris Lattner34a01ad2007-10-09 17:33:22 +00002115 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002116 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002117 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002118 return;
2119 }
2120
Sebastian Redl14ca7412008-12-11 21:36:32 +00002121 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002122 if (Result.isInvalid())
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002123 return;
2124
2125 const char *PrevSpec = 0;
2126 // Check for duplicate type specifiers.
2127 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002128 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002129 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002130
2131 // FIXME: Not accurate, the range gets one token more than it should.
2132 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002133 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002134 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002135
Steve Naroff7cbb1462007-07-31 12:34:36 +00002136 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2137
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002138 if (isTypeIdInParens()) {
Steve Naroff7cbb1462007-07-31 12:34:36 +00002139 TypeTy *Ty = ParseTypeName();
2140
Steve Naroff4c255ab2007-07-31 23:56:32 +00002141 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
2142
Chris Lattner34a01ad2007-10-09 17:33:22 +00002143 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002144 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002145 return;
2146 }
2147 RParenLoc = ConsumeParen();
2148 const char *PrevSpec = 0;
2149 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2150 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
Chris Lattnerf006a222008-11-18 07:48:38 +00002151 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002152 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002153 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002154
2155 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002156 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002157 return;
2158 }
2159 RParenLoc = ConsumeParen();
2160 const char *PrevSpec = 0;
2161 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2162 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002163 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002164 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002165 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002166 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002167}
2168
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002169