blob: bf75ab0e93e2a6563e6aab6a2525ff3da0484631 [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
219/// others... [FIXME]
220///
Chris Lattner4b009652007-07-25 00:24:17 +0000221Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000222 switch (Tok.getKind()) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000223 case tok::kw_export:
224 case tok::kw_template:
225 return ParseTemplateDeclaration(Context);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000226 case tok::kw_namespace:
227 return ParseNamespace(Context);
228 default:
229 return ParseSimpleDeclaration(Context);
230 }
231}
232
233/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
234/// declaration-specifiers init-declarator-list[opt] ';'
235///[C90/C++]init-declarator-list ';' [TODO]
236/// [OMP] threadprivate-directive [TODO]
237Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Chris Lattner4b009652007-07-25 00:24:17 +0000238 // Parse the common declaration-specifiers piece.
239 DeclSpec DS;
240 ParseDeclarationSpecifiers(DS);
241
242 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
243 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000244 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000245 ConsumeToken();
246 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
247 }
248
249 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
250 ParseDeclarator(DeclaratorInfo);
251
252 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
253}
254
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000255
Chris Lattner4b009652007-07-25 00:24:17 +0000256/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
257/// parsing 'declaration-specifiers declarator'. This method is split out this
258/// way to handle the ambiguity between top-level function-definitions and
259/// declarations.
260///
Chris Lattner4b009652007-07-25 00:24:17 +0000261/// init-declarator-list: [C99 6.7]
262/// init-declarator
263/// init-declarator-list ',' init-declarator
264/// init-declarator: [C99 6.7]
265/// declarator
266/// declarator '=' initializer
267/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
268/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000269/// [C++] declarator initializer[opt]
270///
271/// [C++] initializer:
272/// [C++] '=' initializer-clause
273/// [C++] '(' expression-list ')'
Chris Lattner4b009652007-07-25 00:24:17 +0000274///
275Parser::DeclTy *Parser::
276ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
277
278 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
279 // that they can be chained properly if the actions want this.
280 Parser::DeclTy *LastDeclInGroup = 0;
281
282 // At this point, we know that it is not a function definition. Parse the
283 // rest of the init-declarator-list.
284 while (1) {
285 // If a simple-asm-expr is present, parse it.
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000286 if (Tok.is(tok::kw_asm)) {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000287 OwningExprResult AsmLabel(ParseSimpleAsm());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000288 if (AsmLabel.isInvalid()) {
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000289 SkipUntil(tok::semi);
290 return 0;
291 }
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000292
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000293 D.setAsmLabel(AsmLabel.release());
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000294 }
Chris Lattner4b009652007-07-25 00:24:17 +0000295
296 // If attributes are present, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000297 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000298 D.AddAttributes(ParseAttributes());
Steve Naroff6a0e2092007-09-12 14:07:44 +0000299
300 // Inform the current actions module that we just parsed this declarator.
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000301 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000302
Chris Lattner4b009652007-07-25 00:24:17 +0000303 // Parse declarator '=' initializer.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000304 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000305 ConsumeToken();
Sebastian Redl39d4f022008-12-11 22:51:44 +0000306 OwningExprResult Init(ParseInitializer());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000307 if (Init.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000308 SkipUntil(tok::semi);
309 return 0;
310 }
Sebastian Redl91f9b0a2008-12-13 16:23:55 +0000311 Actions.AddInitializerToDecl(LastDeclInGroup, move_convert(Init));
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000312 } else if (Tok.is(tok::l_paren)) {
313 // Parse C++ direct initializer: '(' expression-list ')'
314 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl6008ac32008-11-25 22:21:31 +0000315 ExprVector Exprs(Actions);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000316 CommaLocsTy CommaLocs;
317
318 bool InvalidExpr = false;
319 if (ParseExpressionList(Exprs, CommaLocs)) {
320 SkipUntil(tok::r_paren);
321 InvalidExpr = true;
322 }
323 // Match the ')'.
324 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
325
326 if (!InvalidExpr) {
327 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
328 "Unexpected number of commas!");
329 Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc,
Sebastian Redl6008ac32008-11-25 22:21:31 +0000330 Exprs.take(), Exprs.size(),
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000331 &CommaLocs[0], RParenLoc);
332 }
Douglas Gregor81c29152008-10-29 00:13:59 +0000333 } else {
334 Actions.ActOnUninitializedDecl(LastDeclInGroup);
Chris Lattner4b009652007-07-25 00:24:17 +0000335 }
336
Chris Lattner4b009652007-07-25 00:24:17 +0000337 // If we don't have a comma, it is either the end of the list (a ';') or an
338 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000339 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000340 break;
341
342 // Consume the comma.
343 ConsumeToken();
344
345 // Parse the next declarator.
346 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000347
348 // Accept attributes in an init-declarator. In the first declarator in a
349 // declaration, these would be part of the declspec. In subsequent
350 // declarators, they become part of the declarator itself, so that they
351 // don't apply to declarators after *this* one. Examples:
352 // short __attribute__((common)) var; -> declspec
353 // short var __attribute__((common)); -> declarator
354 // short x, __attribute__((common)) var; -> declarator
355 if (Tok.is(tok::kw___attribute))
356 D.AddAttributes(ParseAttributes());
357
Chris Lattner4b009652007-07-25 00:24:17 +0000358 ParseDeclarator(D);
359 }
360
Chris Lattner34a01ad2007-10-09 17:33:22 +0000361 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000362 ConsumeToken();
363 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
364 }
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000365 // If this is an ObjC2 for-each loop, this is a successful declarator
366 // parse. The syntax for these looks like:
367 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000368 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000369 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
370 }
Chris Lattner4b009652007-07-25 00:24:17 +0000371 Diag(Tok, diag::err_parse_error);
372 // Skip to end of block or statement
Chris Lattnerf491b412007-08-21 18:36:18 +0000373 SkipUntil(tok::r_brace, true, true);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000374 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000375 ConsumeToken();
376 return 0;
377}
378
379/// ParseSpecifierQualifierList
380/// specifier-qualifier-list:
381/// type-specifier specifier-qualifier-list[opt]
382/// type-qualifier specifier-qualifier-list[opt]
383/// [GNU] attributes specifier-qualifier-list[opt]
384///
385void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
386 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
387 /// parse declaration-specifiers and complain about extra stuff.
388 ParseDeclarationSpecifiers(DS);
389
390 // Validate declspec for type-name.
391 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000392 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000393 Diag(Tok, diag::err_typename_requires_specqual);
394
395 // Issue diagnostic and remove storage class if present.
396 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
397 if (DS.getStorageClassSpecLoc().isValid())
398 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
399 else
400 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
401 DS.ClearStorageClassSpecs();
402 }
403
404 // Issue diagnostic and remove function specfier if present.
405 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000406 if (DS.isInlineSpecified())
407 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
408 if (DS.isVirtualSpecified())
409 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
410 if (DS.isExplicitSpecified())
411 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000412 DS.ClearFunctionSpecs();
413 }
414}
415
416/// ParseDeclarationSpecifiers
417/// declaration-specifiers: [C99 6.7]
418/// storage-class-specifier declaration-specifiers[opt]
419/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000420/// [C99] function-specifier declaration-specifiers[opt]
421/// [GNU] attributes declaration-specifiers[opt]
422///
423/// storage-class-specifier: [C99 6.7.1]
424/// 'typedef'
425/// 'extern'
426/// 'static'
427/// 'auto'
428/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000429/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000430/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000431/// function-specifier: [C99 6.7.4]
432/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000433/// [C++] 'virtual'
434/// [C++] 'explicit'
Chris Lattner4b009652007-07-25 00:24:17 +0000435///
Douglas Gregor52473432008-12-24 02:52:09 +0000436void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
437 TemplateParameterLists *TemplateParams)
Douglas Gregorb3bec712008-12-01 23:54:00 +0000438{
Chris Lattnera4ff4272008-03-13 06:29:04 +0000439 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000440 while (1) {
441 int isInvalid = false;
442 const char *PrevSpec = 0;
443 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000444
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000445 // Only annotate C++ scope. Allow class-name as an identifier in case
446 // it's a constructor.
Daniel Dunbar1afd88d2008-11-25 23:05:24 +0000447 if (getLang().CPlusPlus)
Argiris Kirtzidis91c80dc2008-11-26 21:41:52 +0000448 TryAnnotateCXXScopeToken();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000449
Chris Lattner4b009652007-07-25 00:24:17 +0000450 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000451 default:
Douglas Gregorb3bec712008-12-01 23:54:00 +0000452 // Try to parse a type-specifier; if we found one, continue. If it's not
453 // a type, this falls through.
Douglas Gregor52473432008-12-24 02:52:09 +0000454 if (MaybeParseTypeSpecifier(DS, isInvalid, PrevSpec, TemplateParams)) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000455 continue;
Douglas Gregorb3bec712008-12-01 23:54:00 +0000456 }
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000457
Chris Lattnerb99d7492008-07-26 00:20:22 +0000458 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000459 // If this is not a declaration specifier token, we're done reading decl
460 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000461 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000462 return;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000463
464 case tok::annot_cxxscope: {
465 if (DS.hasTypeSpecifier())
466 goto DoneWithDeclSpec;
467
468 // We are looking for a qualified typename.
469 if (NextToken().isNot(tok::identifier))
470 goto DoneWithDeclSpec;
471
472 CXXScopeSpec SS;
473 SS.setScopeRep(Tok.getAnnotationValue());
474 SS.setRange(Tok.getAnnotationRange());
475
476 // If the next token is the name of the class type that the C++ scope
477 // denotes, followed by a '(', then this is a constructor declaration.
478 // We're done with the decl-specifiers.
479 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
480 CurScope, &SS) &&
481 GetLookAheadToken(2).is(tok::l_paren))
482 goto DoneWithDeclSpec;
483
484 TypeTy *TypeRep = Actions.isTypeName(*NextToken().getIdentifierInfo(),
485 CurScope, &SS);
486 if (TypeRep == 0)
487 goto DoneWithDeclSpec;
488
489 ConsumeToken(); // The C++ scope.
490
491 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
492 TypeRep);
493 if (isInvalid)
494 break;
495
496 DS.SetRangeEnd(Tok.getLocation());
497 ConsumeToken(); // The typename.
498
499 continue;
500 }
501
Chris Lattnerfda18db2008-07-26 01:18:38 +0000502 // typedef-name
503 case tok::identifier: {
504 // This identifier can only be a typedef name if we haven't already seen
505 // a type-specifier. Without this check we misparse:
506 // typedef int X; struct Y { short X; }; as 'short int'.
507 if (DS.hasTypeSpecifier())
508 goto DoneWithDeclSpec;
509
510 // It has to be available as a typedef too!
Argiris Kirtzidis46403632008-08-01 10:35:27 +0000511 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattnerfda18db2008-07-26 01:18:38 +0000512 if (TypeRep == 0)
513 goto DoneWithDeclSpec;
514
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000515 // C++: If the identifier is actually the name of the class type
516 // being defined and the next token is a '(', then this is a
517 // constructor declaration. We're done with the decl-specifiers
518 // and will treat this token as an identifier.
519 if (getLang().CPlusPlus &&
520 CurScope->isCXXClassScope() &&
521 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
522 NextToken().getKind() == tok::l_paren)
523 goto DoneWithDeclSpec;
524
Chris Lattnerfda18db2008-07-26 01:18:38 +0000525 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
526 TypeRep);
527 if (isInvalid)
528 break;
529
530 DS.SetRangeEnd(Tok.getLocation());
531 ConsumeToken(); // The identifier
532
533 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
534 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
535 // Objective-C interface. If we don't have Objective-C or a '<', this is
536 // just a normal reference to a typedef name.
537 if (!Tok.is(tok::less) || !getLang().ObjC1)
538 continue;
539
540 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000541 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000542 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000543 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000544
545 DS.SetRangeEnd(EndProtoLoc);
546
Steve Narofff7683302008-09-22 10:28:57 +0000547 // Need to support trailing type qualifiers (e.g. "id<p> const").
548 // If a type specifier follows, it will be diagnosed elsewhere.
549 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000550 }
Chris Lattner4b009652007-07-25 00:24:17 +0000551 // GNU attributes support.
552 case tok::kw___attribute:
553 DS.AddAttributes(ParseAttributes());
554 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000555
556 // Microsoft declspec support.
557 case tok::kw___declspec:
558 if (!PP.getLangOptions().Microsoft)
559 goto DoneWithDeclSpec;
560 FuzzyParseMicrosoftDeclSpec();
561 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000562
Steve Naroffedd04d52008-12-25 14:16:32 +0000563 // Microsoft single token adornments.
564 case tok::kw___cdecl:
565 case tok::kw___stdcall:
566 case tok::kw___fastcall:
567 if (!PP.getLangOptions().Microsoft)
568 goto DoneWithDeclSpec;
569 // Just ignore it.
570 break;
571
Chris Lattner4b009652007-07-25 00:24:17 +0000572 // storage-class-specifier
573 case tok::kw_typedef:
574 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
575 break;
576 case tok::kw_extern:
577 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000578 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000579 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
580 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000581 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000582 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
583 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000584 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000585 case tok::kw_static:
586 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000587 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000588 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
589 break;
590 case tok::kw_auto:
591 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
592 break;
593 case tok::kw_register:
594 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
595 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000596 case tok::kw_mutable:
597 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
598 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000599 case tok::kw___thread:
600 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
601 break;
602
Chris Lattner4b009652007-07-25 00:24:17 +0000603 continue;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000604
Chris Lattner4b009652007-07-25 00:24:17 +0000605 // function-specifier
606 case tok::kw_inline:
607 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
608 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000609
610 case tok::kw_virtual:
611 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
612 break;
613
614 case tok::kw_explicit:
615 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
616 break;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000617
Steve Naroff5f0466b2008-06-05 00:02:44 +0000618 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000619 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000620 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
621 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000622 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000623 goto DoneWithDeclSpec;
624
625 {
626 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000627 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000628 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000629 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000630 DS.SetRangeEnd(EndProtoLoc);
631
Chris Lattnerf006a222008-11-18 07:48:38 +0000632 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
633 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000634 // Need to support trailing type qualifiers (e.g. "id<p> const").
635 // If a type specifier follows, it will be diagnosed elsewhere.
636 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000637 }
Chris Lattner4b009652007-07-25 00:24:17 +0000638 }
639 // If the specifier combination wasn't legal, issue a diagnostic.
640 if (isInvalid) {
641 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000642 // Pick between error or extwarn.
643 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
644 : diag::ext_duplicate_declspec;
645 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000646 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000647 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000648 ConsumeToken();
649 }
650}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000651
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000652/// MaybeParseTypeSpecifier - Try to parse a single type-specifier. We
653/// primarily follow the C++ grammar with additions for C99 and GNU,
654/// which together subsume the C grammar. Note that the C++
655/// type-specifier also includes the C type-qualifier (for const,
656/// volatile, and C99 restrict). Returns true if a type-specifier was
657/// found (and parsed), false otherwise.
658///
659/// type-specifier: [C++ 7.1.5]
660/// simple-type-specifier
661/// class-specifier
662/// enum-specifier
663/// elaborated-type-specifier [TODO]
664/// cv-qualifier
665///
666/// cv-qualifier: [C++ 7.1.5.1]
667/// 'const'
668/// 'volatile'
669/// [C99] 'restrict'
670///
671/// simple-type-specifier: [ C++ 7.1.5.2]
672/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
673/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
674/// 'char'
675/// 'wchar_t'
676/// 'bool'
677/// 'short'
678/// 'int'
679/// 'long'
680/// 'signed'
681/// 'unsigned'
682/// 'float'
683/// 'double'
684/// 'void'
685/// [C99] '_Bool'
686/// [C99] '_Complex'
687/// [C99] '_Imaginary' // Removed in TC2?
688/// [GNU] '_Decimal32'
689/// [GNU] '_Decimal64'
690/// [GNU] '_Decimal128'
691/// [GNU] typeof-specifier
692/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
693/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
694bool Parser::MaybeParseTypeSpecifier(DeclSpec &DS, int& isInvalid,
Douglas Gregor52473432008-12-24 02:52:09 +0000695 const char *&PrevSpec,
696 TemplateParameterLists *TemplateParams) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000697 // Annotate typenames and C++ scope specifiers.
698 TryAnnotateTypeOrScopeToken();
699
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000700 SourceLocation Loc = Tok.getLocation();
701
702 switch (Tok.getKind()) {
703 // simple-type-specifier:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000704 case tok::annot_qualtypename: {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000705 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000706 Tok.getAnnotationValue());
707 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
708 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000709
710 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
711 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
712 // Objective-C interface. If we don't have Objective-C or a '<', this is
713 // just a normal reference to a typedef name.
714 if (!Tok.is(tok::less) || !getLang().ObjC1)
715 return true;
716
717 SourceLocation EndProtoLoc;
718 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
719 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
720 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
721
722 DS.SetRangeEnd(EndProtoLoc);
723 return true;
724 }
725
726 case tok::kw_short:
727 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
728 break;
729 case tok::kw_long:
730 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
731 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
732 else
733 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
734 break;
735 case tok::kw_signed:
736 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
737 break;
738 case tok::kw_unsigned:
739 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
740 break;
741 case tok::kw__Complex:
742 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
743 break;
744 case tok::kw__Imaginary:
745 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
746 break;
747 case tok::kw_void:
748 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
749 break;
750 case tok::kw_char:
751 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
752 break;
753 case tok::kw_int:
754 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
755 break;
756 case tok::kw_float:
757 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
758 break;
759 case tok::kw_double:
760 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
761 break;
762 case tok::kw_wchar_t:
763 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
764 break;
765 case tok::kw_bool:
766 case tok::kw__Bool:
767 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
768 break;
769 case tok::kw__Decimal32:
770 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
771 break;
772 case tok::kw__Decimal64:
773 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
774 break;
775 case tok::kw__Decimal128:
776 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
777 break;
778
779 // class-specifier:
780 case tok::kw_class:
781 case tok::kw_struct:
782 case tok::kw_union:
Douglas Gregor52473432008-12-24 02:52:09 +0000783 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000784 return true;
785
786 // enum-specifier:
787 case tok::kw_enum:
788 ParseEnumSpecifier(DS);
789 return true;
790
791 // cv-qualifier:
792 case tok::kw_const:
793 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
794 getLang())*2;
795 break;
796 case tok::kw_volatile:
797 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
798 getLang())*2;
799 break;
800 case tok::kw_restrict:
801 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
802 getLang())*2;
803 break;
804
805 // GNU typeof support.
806 case tok::kw_typeof:
807 ParseTypeofSpecifier(DS);
808 return true;
809
Steve Naroffedd04d52008-12-25 14:16:32 +0000810 case tok::kw___cdecl:
811 case tok::kw___stdcall:
812 case tok::kw___fastcall:
813 return PP.getLangOptions().Microsoft;
814
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000815 default:
816 // Not a type-specifier; do nothing.
817 return false;
818 }
819
820 // If the specifier combination wasn't legal, issue a diagnostic.
821 if (isInvalid) {
822 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000823 // Pick between error or extwarn.
824 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
825 : diag::ext_duplicate_declspec;
826 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000827 }
828 DS.SetRangeEnd(Tok.getLocation());
829 ConsumeToken(); // whatever we parsed above.
830 return true;
831}
Chris Lattner4b009652007-07-25 00:24:17 +0000832
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000833/// ParseStructDeclaration - Parse a struct declaration without the terminating
834/// semicolon.
835///
Chris Lattner4b009652007-07-25 00:24:17 +0000836/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000837/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000838/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000839/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000840/// struct-declarator-list:
841/// struct-declarator
842/// struct-declarator-list ',' struct-declarator
843/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
844/// struct-declarator:
845/// declarator
846/// [GNU] declarator attributes[opt]
847/// declarator[opt] ':' constant-expression
848/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
849///
Chris Lattner3dd8d392008-04-10 06:46:29 +0000850void Parser::
851ParseStructDeclaration(DeclSpec &DS,
852 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000853 if (Tok.is(tok::kw___extension__)) {
854 // __extension__ silences extension warnings in the subexpression.
855 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +0000856 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000857 return ParseStructDeclaration(DS, Fields);
858 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000859
860 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000861 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +0000862 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +0000863
864 // If there are no declarators, issue a warning.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000865 if (Tok.is(tok::semi)) {
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000866 Diag(DSStart, diag::w_no_declarators);
Steve Naroffa9adf112007-08-20 22:28:22 +0000867 return;
868 }
869
870 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000871 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000872 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +0000873 FieldDeclarator &DeclaratorInfo = Fields.back();
874
Steve Naroffa9adf112007-08-20 22:28:22 +0000875 /// struct-declarator: declarator
876 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000877 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000878 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +0000879
Chris Lattner34a01ad2007-10-09 17:33:22 +0000880 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000881 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000882 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000883 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +0000884 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000885 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000886 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +0000887 }
888
889 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000890 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000891 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000892
893 // If we don't have a comma, it is either the end of the list (a ';')
894 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000895 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000896 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000897
898 // Consume the comma.
899 ConsumeToken();
900
901 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000902 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000903
904 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000905 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000906 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000907 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000908}
909
910/// ParseStructUnionBody
911/// struct-contents:
912/// struct-declaration-list
913/// [EXT] empty
914/// [GNU] "struct-declaration-list" without terminatoring ';'
915/// struct-declaration-list:
916/// struct-declaration
917/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +0000918/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +0000919///
Chris Lattner4b009652007-07-25 00:24:17 +0000920void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
921 unsigned TagType, DeclTy *TagDecl) {
922 SourceLocation LBraceLoc = ConsumeBrace();
923
924 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
925 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +0000926 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +0000927 Diag(Tok, diag::ext_empty_struct_union_enum)
928 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +0000929
930 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000931 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
932
Chris Lattner4b009652007-07-25 00:24:17 +0000933 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000934 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000935 // Each iteration of this loop reads one struct-declaration.
936
937 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000938 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000939 Diag(Tok, diag::ext_extra_struct_semi);
940 ConsumeToken();
941 continue;
942 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000943
944 // Parse all the comma separated declarators.
945 DeclSpec DS;
946 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +0000947 if (!Tok.is(tok::at)) {
948 ParseStructDeclaration(DS, FieldDeclarators);
949
950 // Convert them all to fields.
951 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
952 FieldDeclarator &FD = FieldDeclarators[i];
953 // Install the declarator into the current TagDecl.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000954 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
Chris Lattner1bf58f62008-06-21 19:39:06 +0000955 DS.getSourceRange().getBegin(),
956 FD.D, FD.BitfieldSize);
957 FieldDecls.push_back(Field);
958 }
959 } else { // Handle @defs
960 ConsumeToken();
961 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
962 Diag(Tok, diag::err_unexpected_at);
963 SkipUntil(tok::semi, true, true);
964 continue;
965 }
966 ConsumeToken();
967 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
968 if (!Tok.is(tok::identifier)) {
969 Diag(Tok, diag::err_expected_ident);
970 SkipUntil(tok::semi, true, true);
971 continue;
972 }
973 llvm::SmallVector<DeclTy*, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000974 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
975 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +0000976 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
977 ConsumeToken();
978 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
979 }
Chris Lattner4b009652007-07-25 00:24:17 +0000980
Chris Lattner34a01ad2007-10-09 17:33:22 +0000981 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000982 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +0000983 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000984 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +0000985 break;
986 } else {
987 Diag(Tok, diag::err_expected_semi_decl_list);
988 // Skip to end of block or statement
989 SkipUntil(tok::r_brace, true, true);
990 }
991 }
992
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000993 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000994
Chris Lattner4b009652007-07-25 00:24:17 +0000995 AttributeList *AttrList = 0;
996 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000997 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +0000998 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +0000999
1000 Actions.ActOnFields(CurScope,
1001 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1002 LBraceLoc, RBraceLoc,
1003 AttrList);
Chris Lattner4b009652007-07-25 00:24:17 +00001004}
1005
1006
1007/// ParseEnumSpecifier
1008/// enum-specifier: [C99 6.7.2.2]
1009/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001010///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001011/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1012/// '}' attributes[opt]
1013/// 'enum' identifier
1014/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001015///
1016/// [C++] elaborated-type-specifier:
1017/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1018///
Chris Lattner4b009652007-07-25 00:24:17 +00001019void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001020 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +00001021 SourceLocation StartLoc = ConsumeToken();
1022
1023 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001024
1025 AttributeList *Attr = 0;
1026 // If attributes exist after tag, parse them.
1027 if (Tok.is(tok::kw___attribute))
1028 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001029
1030 CXXScopeSpec SS;
Argiris Kirtzidis91c80dc2008-11-26 21:41:52 +00001031 if (getLang().CPlusPlus && MaybeParseCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001032 if (Tok.isNot(tok::identifier)) {
1033 Diag(Tok, diag::err_expected_ident);
1034 if (Tok.isNot(tok::l_brace)) {
1035 // Has no name and is not a definition.
1036 // Skip the rest of this declarator, up until the comma or semicolon.
1037 SkipUntil(tok::comma, true);
1038 return;
1039 }
1040 }
1041 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001042
1043 // Must have either 'enum name' or 'enum {...}'.
1044 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1045 Diag(Tok, diag::err_expected_ident_lbrace);
1046
1047 // Skip the rest of this declarator, up until the comma or semicolon.
1048 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001049 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001050 }
1051
1052 // If an identifier is present, consume and remember it.
1053 IdentifierInfo *Name = 0;
1054 SourceLocation NameLoc;
1055 if (Tok.is(tok::identifier)) {
1056 Name = Tok.getIdentifierInfo();
1057 NameLoc = ConsumeToken();
1058 }
1059
1060 // There are three options here. If we have 'enum foo;', then this is a
1061 // forward declaration. If we have 'enum foo {...' then this is a
1062 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1063 //
1064 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1065 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1066 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1067 //
1068 Action::TagKind TK;
1069 if (Tok.is(tok::l_brace))
1070 TK = Action::TK_Definition;
1071 else if (Tok.is(tok::semi))
1072 TK = Action::TK_Declaration;
1073 else
1074 TK = Action::TK_Reference;
1075 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Douglas Gregor52473432008-12-24 02:52:09 +00001076 SS, Name, NameLoc, Attr,
1077 Action::MultiTemplateParamsArg(Actions));
Chris Lattner4b009652007-07-25 00:24:17 +00001078
Chris Lattner34a01ad2007-10-09 17:33:22 +00001079 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001080 ParseEnumBody(StartLoc, TagDecl);
1081
1082 // TODO: semantic analysis on the declspec for enums.
1083 const char *PrevSpec = 0;
1084 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerf006a222008-11-18 07:48:38 +00001085 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001086}
1087
1088/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1089/// enumerator-list:
1090/// enumerator
1091/// enumerator-list ',' enumerator
1092/// enumerator:
1093/// enumeration-constant
1094/// enumeration-constant '=' constant-expression
1095/// enumeration-constant:
1096/// identifier
1097///
1098void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
1099 SourceLocation LBraceLoc = ConsumeBrace();
1100
Chris Lattnerc9a92452007-08-27 17:24:30 +00001101 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001102 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001103 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001104
1105 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1106
1107 DeclTy *LastEnumConstDecl = 0;
1108
1109 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001110 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001111 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1112 SourceLocation IdentLoc = ConsumeToken();
1113
1114 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001115 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001116 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001117 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001118 AssignedVal = ParseConstantExpression();
1119 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001120 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001121 }
1122
1123 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +00001124 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +00001125 LastEnumConstDecl,
1126 IdentLoc, Ident,
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001127 EqualLoc,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001128 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001129 EnumConstantDecls.push_back(EnumConstDecl);
1130 LastEnumConstDecl = EnumConstDecl;
1131
Chris Lattner34a01ad2007-10-09 17:33:22 +00001132 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001133 break;
1134 SourceLocation CommaLoc = ConsumeToken();
1135
Chris Lattner34a01ad2007-10-09 17:33:22 +00001136 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +00001137 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1138 }
1139
1140 // Eat the }.
1141 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1142
Steve Naroff0acc9c92007-09-15 18:49:24 +00001143 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001144 EnumConstantDecls.size());
1145
1146 DeclTy *AttrList = 0;
1147 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001148 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001149 AttrList = ParseAttributes(); // FIXME: where do they do?
1150}
1151
1152/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001153/// start of a type-qualifier-list.
1154bool Parser::isTypeQualifier() const {
1155 switch (Tok.getKind()) {
1156 default: return false;
1157 // type-qualifier
1158 case tok::kw_const:
1159 case tok::kw_volatile:
1160 case tok::kw_restrict:
1161 return true;
1162 }
1163}
1164
1165/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001166/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001167bool Parser::isTypeSpecifierQualifier() {
1168 // Annotate typenames and C++ scope specifiers.
1169 TryAnnotateTypeOrScopeToken();
1170
Chris Lattner4b009652007-07-25 00:24:17 +00001171 switch (Tok.getKind()) {
1172 default: return false;
1173 // GNU attributes support.
1174 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001175 // GNU typeof support.
1176 case tok::kw_typeof:
1177
Chris Lattner4b009652007-07-25 00:24:17 +00001178 // type-specifiers
1179 case tok::kw_short:
1180 case tok::kw_long:
1181 case tok::kw_signed:
1182 case tok::kw_unsigned:
1183 case tok::kw__Complex:
1184 case tok::kw__Imaginary:
1185 case tok::kw_void:
1186 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001187 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001188 case tok::kw_int:
1189 case tok::kw_float:
1190 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001191 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001192 case tok::kw__Bool:
1193 case tok::kw__Decimal32:
1194 case tok::kw__Decimal64:
1195 case tok::kw__Decimal128:
1196
Chris Lattner2e78db32008-04-13 18:59:07 +00001197 // struct-or-union-specifier (C99) or class-specifier (C++)
1198 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001199 case tok::kw_struct:
1200 case tok::kw_union:
1201 // enum-specifier
1202 case tok::kw_enum:
1203
1204 // type-qualifier
1205 case tok::kw_const:
1206 case tok::kw_volatile:
1207 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001208
1209 // typedef-name
1210 case tok::annot_qualtypename:
Chris Lattner4b009652007-07-25 00:24:17 +00001211 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001212
1213 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1214 case tok::less:
1215 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001216
1217 case tok::kw___cdecl:
1218 case tok::kw___stdcall:
1219 case tok::kw___fastcall:
1220 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001221 }
1222}
1223
1224/// isDeclarationSpecifier() - Return true if the current token is part of a
1225/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001226bool Parser::isDeclarationSpecifier() {
1227 // Annotate typenames and C++ scope specifiers.
1228 TryAnnotateTypeOrScopeToken();
1229
Chris Lattner4b009652007-07-25 00:24:17 +00001230 switch (Tok.getKind()) {
1231 default: return false;
1232 // storage-class-specifier
1233 case tok::kw_typedef:
1234 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001235 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001236 case tok::kw_static:
1237 case tok::kw_auto:
1238 case tok::kw_register:
1239 case tok::kw___thread:
1240
1241 // type-specifiers
1242 case tok::kw_short:
1243 case tok::kw_long:
1244 case tok::kw_signed:
1245 case tok::kw_unsigned:
1246 case tok::kw__Complex:
1247 case tok::kw__Imaginary:
1248 case tok::kw_void:
1249 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001250 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001251 case tok::kw_int:
1252 case tok::kw_float:
1253 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001254 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001255 case tok::kw__Bool:
1256 case tok::kw__Decimal32:
1257 case tok::kw__Decimal64:
1258 case tok::kw__Decimal128:
1259
Chris Lattner2e78db32008-04-13 18:59:07 +00001260 // struct-or-union-specifier (C99) or class-specifier (C++)
1261 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001262 case tok::kw_struct:
1263 case tok::kw_union:
1264 // enum-specifier
1265 case tok::kw_enum:
1266
1267 // type-qualifier
1268 case tok::kw_const:
1269 case tok::kw_volatile:
1270 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001271
Chris Lattner4b009652007-07-25 00:24:17 +00001272 // function-specifier
1273 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001274 case tok::kw_virtual:
1275 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001276
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001277 // typedef-name
1278 case tok::annot_qualtypename:
1279
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001280 // GNU typeof support.
1281 case tok::kw_typeof:
1282
1283 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001284 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001285 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001286
1287 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1288 case tok::less:
1289 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001290
1291 case tok::kw___cdecl:
1292 case tok::kw___stdcall:
1293 case tok::kw___fastcall:
1294 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001295 }
1296}
1297
1298
1299/// ParseTypeQualifierListOpt
1300/// type-qualifier-list: [C99 6.7.5]
1301/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001302/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001303/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001304/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001305///
Chris Lattner460696f2008-12-18 07:02:59 +00001306void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001307 while (1) {
1308 int isInvalid = false;
1309 const char *PrevSpec = 0;
1310 SourceLocation Loc = Tok.getLocation();
1311
1312 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001313 case tok::kw_const:
1314 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1315 getLang())*2;
1316 break;
1317 case tok::kw_volatile:
1318 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1319 getLang())*2;
1320 break;
1321 case tok::kw_restrict:
1322 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1323 getLang())*2;
1324 break;
Steve Naroffedd04d52008-12-25 14:16:32 +00001325 case tok::kw___cdecl:
1326 case tok::kw___stdcall:
1327 case tok::kw___fastcall:
1328 if (!PP.getLangOptions().Microsoft)
1329 goto DoneWithTypeQuals;
1330 // Just ignore it.
1331 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001332 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001333 if (AttributesAllowed) {
1334 DS.AddAttributes(ParseAttributes());
1335 continue; // do *not* consume the next token!
1336 }
1337 // otherwise, FALL THROUGH!
1338 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001339 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001340 // If this is not a type-qualifier token, we're done reading type
1341 // qualifiers. First verify that DeclSpec's are consistent.
1342 DS.Finish(Diags, PP.getSourceManager(), getLang());
1343 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001344 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001345
Chris Lattner4b009652007-07-25 00:24:17 +00001346 // If the specifier combination wasn't legal, issue a diagnostic.
1347 if (isInvalid) {
1348 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001349 // Pick between error or extwarn.
1350 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1351 : diag::ext_duplicate_declspec;
1352 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001353 }
1354 ConsumeToken();
1355 }
1356}
1357
1358
1359/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1360///
1361void Parser::ParseDeclarator(Declarator &D) {
1362 /// This implements the 'declarator' production in the C grammar, then checks
1363 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001364 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001365}
1366
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001367/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1368/// is parsed by the function passed to it. Pass null, and the direct-declarator
1369/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001370/// ptr-operator production.
1371///
Chris Lattner4b009652007-07-25 00:24:17 +00001372/// declarator: [C99 6.7.5]
1373/// pointer[opt] direct-declarator
1374/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1375/// [GNU] '&' restrict[opt] attributes[opt] declarator
1376///
1377/// pointer: [C99 6.7.5]
1378/// '*' type-qualifier-list[opt]
1379/// '*' type-qualifier-list[opt] pointer
1380///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001381/// ptr-operator:
1382/// '*' cv-qualifier-seq[opt]
1383/// '&'
1384/// [GNU] '&' restrict[opt] attributes[opt]
1385/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] [TODO]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001386void Parser::ParseDeclaratorInternal(Declarator &D,
1387 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001388 tok::TokenKind Kind = Tok.getKind();
1389
Steve Naroff7aa54752008-08-27 16:04:49 +00001390 // Not a pointer, C++ reference, or block.
1391 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001392 (Kind != tok::caret || !getLang().Blocks)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001393 if (DirectDeclParser)
1394 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001395 return;
1396 }
Chris Lattner4b009652007-07-25 00:24:17 +00001397
Steve Naroffdc22f212008-08-28 10:07:06 +00001398 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Chris Lattner4b009652007-07-25 00:24:17 +00001399 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1400
Steve Naroffdc22f212008-08-28 10:07:06 +00001401 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner69f01932008-02-21 01:32:26 +00001402 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001403 DeclSpec DS;
1404
1405 ParseTypeQualifierListOpt(DS);
1406
1407 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001408 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001409 if (Kind == tok::star)
1410 // Remember that we parsed a pointer type, and remember the type-quals.
1411 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1412 DS.TakeAttributes()));
1413 else
1414 // Remember that we parsed a Block type, and remember the type-quals.
1415 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1416 Loc));
Chris Lattner4b009652007-07-25 00:24:17 +00001417 } else {
1418 // Is a reference
1419 DeclSpec DS;
1420
1421 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1422 // cv-qualifiers are introduced through the use of a typedef or of a
1423 // template type argument, in which case the cv-qualifiers are ignored.
1424 //
1425 // [GNU] Retricted references are allowed.
1426 // [GNU] Attributes on references are allowed.
1427 ParseTypeQualifierListOpt(DS);
1428
1429 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1430 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1431 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001432 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001433 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1434 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001435 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001436 }
1437
1438 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001439 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001440
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001441 if (D.getNumTypeObjects() > 0) {
1442 // C++ [dcl.ref]p4: There shall be no references to references.
1443 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1444 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001445 if (const IdentifierInfo *II = D.getIdentifier())
1446 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1447 << II;
1448 else
1449 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1450 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001451
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001452 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001453 // can go ahead and build the (technically ill-formed)
1454 // declarator: reference collapsing will take care of it.
1455 }
1456 }
1457
Chris Lattner4b009652007-07-25 00:24:17 +00001458 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001459 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1460 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001461 }
1462}
1463
1464/// ParseDirectDeclarator
1465/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001466/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001467/// '(' declarator ')'
1468/// [GNU] '(' attributes declarator ')'
1469/// [C90] direct-declarator '[' constant-expression[opt] ']'
1470/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1471/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1472/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1473/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1474/// direct-declarator '(' parameter-type-list ')'
1475/// direct-declarator '(' identifier-list[opt] ')'
1476/// [GNU] direct-declarator '(' parameter-forward-declarations
1477/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001478/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1479/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001480/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001481///
1482/// declarator-id: [C++ 8]
1483/// id-expression
1484/// '::'[opt] nested-name-specifier[opt] type-name
1485///
1486/// id-expression: [C++ 5.1]
1487/// unqualified-id
1488/// qualified-id [TODO]
1489///
1490/// unqualified-id: [C++ 5.1]
1491/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001492/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001493/// conversion-function-id [TODO]
1494/// '~' class-name
1495/// template-id [TODO]
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001496///
Chris Lattner4b009652007-07-25 00:24:17 +00001497void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001498 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001499
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001500 if (getLang().CPlusPlus) {
1501 if (D.mayHaveIdentifier()) {
1502 bool afterCXXScope = MaybeParseCXXScopeSpecifier(D.getCXXScopeSpec());
1503 if (afterCXXScope) {
1504 // Change the declaration context for name lookup, until this function
1505 // is exited (and the declarator has been parsed).
1506 DeclScopeObj.EnterDeclaratorScope();
1507 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001508
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001509 if (Tok.is(tok::identifier)) {
1510 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor2fa10442008-12-18 19:37:40 +00001511
1512 // If this identifier is followed by a '<', we may have a template-id.
1513 DeclTy *Template;
1514 if (getLang().CPlusPlus && NextToken().is(tok::less) &&
1515 (Template = Actions.isTemplateName(*Tok.getIdentifierInfo(),
1516 CurScope))) {
1517 IdentifierInfo *II = Tok.getIdentifierInfo();
1518 AnnotateTemplateIdToken(Template, 0);
1519 // FIXME: Set the declarator to a template-id. How? I don't
1520 // know... for now, just use the identifier.
1521 D.SetIdentifier(II, Tok.getLocation());
1522 }
1523 // If this identifier is the name of the current class, it's a
1524 // constructor name.
1525 else if (getLang().CPlusPlus &&
1526 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope))
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001527 D.setConstructor(Actions.isTypeName(*Tok.getIdentifierInfo(),
1528 CurScope),
1529 Tok.getLocation());
Douglas Gregor2fa10442008-12-18 19:37:40 +00001530 // This is a normal identifier.
1531 else
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001532 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1533 ConsumeToken();
1534 goto PastIdentifier;
1535 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001536
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001537 if (Tok.is(tok::tilde)) {
1538 // This should be a C++ destructor.
1539 SourceLocation TildeLoc = ConsumeToken();
1540 if (Tok.is(tok::identifier)) {
1541 if (TypeTy *Type = ParseClassName())
1542 D.setDestructor(Type, TildeLoc);
1543 else
1544 D.SetIdentifier(0, TildeLoc);
1545 } else {
1546 Diag(Tok, diag::err_expected_class_name);
1547 D.SetIdentifier(0, TildeLoc);
1548 }
1549 goto PastIdentifier;
1550 }
1551
1552 // If we reached this point, token is not identifier and not '~'.
1553
1554 if (afterCXXScope) {
1555 Diag(Tok, diag::err_expected_unqualified_id);
1556 D.SetIdentifier(0, Tok.getLocation());
1557 D.setInvalidType(true);
1558 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001559 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001560 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001561
1562 if (Tok.is(tok::kw_operator)) {
1563 SourceLocation OperatorLoc = Tok.getLocation();
1564
1565 // First try the name of an overloaded operator
1566 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId()) {
1567 D.setOverloadedOperator(Op, OperatorLoc);
1568 } else {
1569 // This must be a conversion function (C++ [class.conv.fct]).
1570 if (TypeTy *ConvType = ParseConversionFunctionId())
1571 D.setConversionFunction(ConvType, OperatorLoc);
1572 else
1573 D.SetIdentifier(0, Tok.getLocation());
1574 }
1575 goto PastIdentifier;
1576 }
1577 }
1578
1579 // If we reached this point, we are either in C/ObjC or the token didn't
1580 // satisfy any of the C++-specific checks.
1581
1582 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1583 assert(!getLang().CPlusPlus &&
1584 "There's a C++-specific check for tok::identifier above");
1585 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1586 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1587 ConsumeToken();
1588 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001589 // direct-declarator: '(' declarator ')'
1590 // direct-declarator: '(' attributes declarator ')'
1591 // Example: 'char (*X)' or 'int (*XX)(void)'
1592 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001593 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001594 // This could be something simple like "int" (in which case the declarator
1595 // portion is empty), if an abstract-declarator is allowed.
1596 D.SetIdentifier(0, Tok.getLocation());
1597 } else {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001598 if (getLang().CPlusPlus)
1599 Diag(Tok, diag::err_expected_unqualified_id);
1600 else
Chris Lattnerf006a222008-11-18 07:48:38 +00001601 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00001602 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00001603 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001604 }
1605
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001606 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00001607 assert(D.isPastIdentifier() &&
1608 "Haven't past the location of the identifier yet?");
1609
1610 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001611 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001612 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1613 // In such a case, check if we actually have a function declarator; if it
1614 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001615 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1616 // When not in file scope, warn for ambiguous function declarators, just
1617 // in case the author intended it as a variable definition.
1618 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1619 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1620 break;
1621 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001622 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001623 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001624 ParseBracketDeclarator(D);
1625 } else {
1626 break;
1627 }
1628 }
1629}
1630
Chris Lattnera0d056d2008-04-06 05:45:57 +00001631/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1632/// only called before the identifier, so these are most likely just grouping
1633/// parens for precedence. If we find that these are actually function
1634/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1635///
1636/// direct-declarator:
1637/// '(' declarator ')'
1638/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001639/// direct-declarator '(' parameter-type-list ')'
1640/// direct-declarator '(' identifier-list[opt] ')'
1641/// [GNU] direct-declarator '(' parameter-forward-declarations
1642/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001643///
1644void Parser::ParseParenDeclarator(Declarator &D) {
1645 SourceLocation StartLoc = ConsumeParen();
1646 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1647
Chris Lattner1f185292008-10-20 02:05:46 +00001648 // Eat any attributes before we look at whether this is a grouping or function
1649 // declarator paren. If this is a grouping paren, the attribute applies to
1650 // the type being built up, for example:
1651 // int (__attribute__(()) *x)(long y)
1652 // If this ends up not being a grouping paren, the attribute applies to the
1653 // first argument, for example:
1654 // int (__attribute__(()) int x)
1655 // In either case, we need to eat any attributes to be able to determine what
1656 // sort of paren this is.
1657 //
1658 AttributeList *AttrList = 0;
1659 bool RequiresArg = false;
1660 if (Tok.is(tok::kw___attribute)) {
1661 AttrList = ParseAttributes();
1662
1663 // We require that the argument list (if this is a non-grouping paren) be
1664 // present even if the attribute list was empty.
1665 RequiresArg = true;
1666 }
Steve Naroffedd04d52008-12-25 14:16:32 +00001667 // Eat any Microsoft extensions.
1668 if ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
1669 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
1670 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00001671
Chris Lattnera0d056d2008-04-06 05:45:57 +00001672 // If we haven't past the identifier yet (or where the identifier would be
1673 // stored, if this is an abstract declarator), then this is probably just
1674 // grouping parens. However, if this could be an abstract-declarator, then
1675 // this could also be the start of function arguments (consider 'void()').
1676 bool isGrouping;
1677
1678 if (!D.mayOmitIdentifier()) {
1679 // If this can't be an abstract-declarator, this *must* be a grouping
1680 // paren, because we haven't seen the identifier yet.
1681 isGrouping = true;
1682 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001683 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00001684 isDeclarationSpecifier()) { // 'int(int)' is a function.
1685 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1686 // considered to be a type, not a K&R identifier-list.
1687 isGrouping = false;
1688 } else {
1689 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1690 isGrouping = true;
1691 }
1692
1693 // If this is a grouping paren, handle:
1694 // direct-declarator: '(' declarator ')'
1695 // direct-declarator: '(' attributes declarator ')'
1696 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001697 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001698 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00001699 if (AttrList)
1700 D.AddAttributes(AttrList);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001701
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001702 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001703 // Match the ')'.
1704 MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001705
1706 D.setGroupingParens(hadGroupingParens);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001707 return;
1708 }
1709
1710 // Okay, if this wasn't a grouping paren, it must be the start of a function
1711 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00001712 // identifier (and remember where it would have been), then call into
1713 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00001714 D.SetIdentifier(0, Tok.getLocation());
1715
Chris Lattner1f185292008-10-20 02:05:46 +00001716 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001717}
1718
1719/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1720/// declarator D up to a paren, which indicates that we are parsing function
1721/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001722///
Chris Lattner1f185292008-10-20 02:05:46 +00001723/// If AttrList is non-null, then the caller parsed those arguments immediately
1724/// after the open paren - they should be considered to be the first argument of
1725/// a parameter. If RequiresArg is true, then the first argument of the
1726/// function is required to be present and required to not be an identifier
1727/// list.
1728///
Chris Lattner4b009652007-07-25 00:24:17 +00001729/// This method also handles this portion of the grammar:
1730/// parameter-type-list: [C99 6.7.5]
1731/// parameter-list
1732/// parameter-list ',' '...'
1733///
1734/// parameter-list: [C99 6.7.5]
1735/// parameter-declaration
1736/// parameter-list ',' parameter-declaration
1737///
1738/// parameter-declaration: [C99 6.7.5]
1739/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00001740/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001741/// [GNU] declaration-specifiers declarator attributes
1742/// declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00001743/// [C++] declaration-specifiers abstract-declarator[opt]
1744/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001745/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1746///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001747/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
1748/// and "exception-specification[opt]"(TODO).
1749///
Chris Lattner1f185292008-10-20 02:05:46 +00001750void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
1751 AttributeList *AttrList,
1752 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00001753 // lparen is already consumed!
1754 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00001755
Chris Lattner1f185292008-10-20 02:05:46 +00001756 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001757 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00001758 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001759 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00001760 delete AttrList;
1761 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001762
1763 ConsumeParen(); // Eat the closing ')'.
1764
1765 // cv-qualifier-seq[opt].
1766 DeclSpec DS;
1767 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00001768 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor90a2c972008-11-25 03:22:00 +00001769
1770 // Parse exception-specification[opt].
1771 if (Tok.is(tok::kw_throw))
1772 ParseExceptionSpecification();
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001773 }
1774
Chris Lattner9f7564b2008-04-06 06:57:35 +00001775 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00001776 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001777 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00001778 /*variadic*/ false,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001779 /*arglist*/ 0, 0,
1780 DS.getTypeQualifiers(),
1781 LParenLoc));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001782 return;
Chris Lattner1f185292008-10-20 02:05:46 +00001783 }
1784
1785 // Alternatively, this parameter list may be an identifier list form for a
1786 // K&R-style function: void foo(a,b,c)
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001787 if (!getLang().CPlusPlus && Tok.is(tok::identifier) &&
Chris Lattner1f185292008-10-20 02:05:46 +00001788 // K&R identifier lists can't have typedefs as identifiers, per
1789 // C99 6.7.5.3p11.
1790 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1791 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001792 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00001793 delete AttrList;
1794 }
1795
Chris Lattner4b009652007-07-25 00:24:17 +00001796 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1797 // normal declarators, not for abstract-declarators.
Chris Lattner35d9c912008-04-06 06:34:08 +00001798 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001799 }
1800
1801 // Finally, a normal, non-empty parameter type list.
1802
1803 // Build up an array of information about the parsed arguments.
1804 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00001805
1806 // Enter function-declaration scope, limiting any declarators to the
1807 // function prototype scope, including parameter declarators.
Douglas Gregor95d40792008-12-10 06:34:36 +00001808 ParseScope PrototypeScope(this, Scope::FnScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001809
1810 bool IsVariadic = false;
1811 while (1) {
1812 if (Tok.is(tok::ellipsis)) {
1813 IsVariadic = true;
Chris Lattner4b009652007-07-25 00:24:17 +00001814
Chris Lattner9f7564b2008-04-06 06:57:35 +00001815 // Check to see if this is "void(...)" which is not allowed.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001816 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00001817 // Otherwise, parse parameter type list. If it starts with an
1818 // ellipsis, diagnose the malformed function.
1819 Diag(Tok, diag::err_ellipsis_first_arg);
1820 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001821 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00001822
Chris Lattner9f7564b2008-04-06 06:57:35 +00001823 ConsumeToken(); // Consume the ellipsis.
1824 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001825 }
1826
Chris Lattner9f7564b2008-04-06 06:57:35 +00001827 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00001828
Chris Lattner9f7564b2008-04-06 06:57:35 +00001829 // Parse the declaration-specifiers.
1830 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00001831
1832 // If the caller parsed attributes for the first argument, add them now.
1833 if (AttrList) {
1834 DS.AddAttributes(AttrList);
1835 AttrList = 0; // Only apply the attributes to the first parameter.
1836 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001837 ParseDeclarationSpecifiers(DS);
1838
1839 // Parse the declarator. This is "PrototypeContext", because we must
1840 // accept either 'declarator' or 'abstract-declarator' here.
1841 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1842 ParseDeclarator(ParmDecl);
1843
1844 // Parse GNU attributes, if present.
1845 if (Tok.is(tok::kw___attribute))
1846 ParmDecl.AddAttributes(ParseAttributes());
1847
Chris Lattner9f7564b2008-04-06 06:57:35 +00001848 // Remember this parsed parameter in ParamInfo.
1849 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1850
Douglas Gregor605de8d2008-12-16 21:30:33 +00001851 // DefArgToks is used when the parsing of default arguments needs
1852 // to be delayed.
1853 CachedTokens *DefArgToks = 0;
1854
Chris Lattner9f7564b2008-04-06 06:57:35 +00001855 // If no parameter was specified, verify that *something* was specified,
1856 // otherwise we have a missing type and identifier.
1857 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1858 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1859 // Completely missing, emit error.
1860 Diag(DSStart, diag::err_missing_param);
1861 } else {
1862 // Otherwise, we have something. Add it and let semantic analysis try
1863 // to grok it and add the result to the ParamInfo we are building.
1864
1865 // Inform the actions module about the parameter declarator, so it gets
1866 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00001867 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1868
1869 // Parse the default argument, if any. We parse the default
1870 // arguments in all dialects; the semantic analysis in
1871 // ActOnParamDefaultArgument will reject the default argument in
1872 // C.
1873 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00001874 SourceLocation EqualLoc = Tok.getLocation();
1875
Chris Lattner3e254fb2008-04-08 04:40:51 +00001876 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00001877 if (D.getContext() == Declarator::MemberContext) {
1878 // If we're inside a class definition, cache the tokens
1879 // corresponding to the default argument. We'll actually parse
1880 // them when we see the end of the class definition.
1881 // FIXME: Templates will require something similar.
1882 // FIXME: Can we use a smart pointer for Toks?
1883 DefArgToks = new CachedTokens;
1884
1885 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
1886 tok::semi, false)) {
1887 delete DefArgToks;
1888 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00001889 Actions.ActOnParamDefaultArgumentError(Param);
1890 } else
1891 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00001892 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00001893 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00001894 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00001895
1896 OwningExprResult DefArgResult(ParseAssignmentExpression());
1897 if (DefArgResult.isInvalid()) {
1898 Actions.ActOnParamDefaultArgumentError(Param);
1899 SkipUntil(tok::comma, tok::r_paren, true, true);
1900 } else {
1901 // Inform the actions module about the default argument
1902 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
1903 DefArgResult.release());
1904 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00001905 }
1906 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001907
1908 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00001909 ParmDecl.getIdentifierLoc(), Param,
1910 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001911 }
1912
1913 // If the next token is a comma, consume it and keep reading arguments.
1914 if (Tok.isNot(tok::comma)) break;
1915
1916 // Consume the comma.
1917 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00001918 }
1919
Chris Lattner9f7564b2008-04-06 06:57:35 +00001920 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00001921 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00001922
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001923 // If we have the closing ')', eat it.
1924 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1925
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001926 DeclSpec DS;
1927 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00001928 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00001929 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor90a2c972008-11-25 03:22:00 +00001930
1931 // Parse exception-specification[opt].
1932 if (Tok.is(tok::kw_throw))
1933 ParseExceptionSpecification();
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001934 }
1935
Chris Lattner4b009652007-07-25 00:24:17 +00001936 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001937 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1938 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001939 DS.getTypeQualifiers(),
Chris Lattner9f7564b2008-04-06 06:57:35 +00001940 LParenLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00001941}
1942
Chris Lattner35d9c912008-04-06 06:34:08 +00001943/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1944/// we found a K&R-style identifier list instead of a type argument list. The
1945/// current token is known to be the first identifier in the list.
1946///
1947/// identifier-list: [C99 6.7.5]
1948/// identifier
1949/// identifier-list ',' identifier
1950///
1951void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1952 Declarator &D) {
1953 // Build up an array of information about the parsed arguments.
1954 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1955 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1956
1957 // If there was no identifier specified for the declarator, either we are in
1958 // an abstract-declarator, or we are in a parameter declarator which was found
1959 // to be abstract. In abstract-declarators, identifier lists are not valid:
1960 // diagnose this.
1961 if (!D.getIdentifier())
1962 Diag(Tok, diag::ext_ident_list_in_param);
1963
1964 // Tok is known to be the first identifier in the list. Remember this
1965 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00001966 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00001967 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1968 Tok.getLocation(), 0));
1969
Chris Lattner113a56b2008-04-06 06:39:19 +00001970 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00001971
1972 while (Tok.is(tok::comma)) {
1973 // Eat the comma.
1974 ConsumeToken();
1975
Chris Lattner113a56b2008-04-06 06:39:19 +00001976 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00001977 if (Tok.isNot(tok::identifier)) {
1978 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00001979 SkipUntil(tok::r_paren);
1980 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00001981 }
Chris Lattneracb67d92008-04-06 06:47:48 +00001982
Chris Lattner35d9c912008-04-06 06:34:08 +00001983 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00001984
1985 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1986 if (Actions.isTypeName(*ParmII, CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00001987 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00001988
1989 // Verify that the argument identifier has not already been mentioned.
1990 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001991 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00001992 } else {
1993 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00001994 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1995 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00001996 }
Chris Lattner35d9c912008-04-06 06:34:08 +00001997
1998 // Eat the identifier.
1999 ConsumeToken();
2000 }
2001
Chris Lattner113a56b2008-04-06 06:39:19 +00002002 // Remember that we parsed a function type, and remember the attributes. This
2003 // function type is always a K&R style function type, which is not varargs and
2004 // has no prototype.
2005 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
2006 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002007 /*TypeQuals*/0, LParenLoc));
Chris Lattner35d9c912008-04-06 06:34:08 +00002008
2009 // If we have the closing ')', eat it and we're done.
Chris Lattner113a56b2008-04-06 06:39:19 +00002010 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002011}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002012
Chris Lattner4b009652007-07-25 00:24:17 +00002013/// [C90] direct-declarator '[' constant-expression[opt] ']'
2014/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2015/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2016/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2017/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2018void Parser::ParseBracketDeclarator(Declarator &D) {
2019 SourceLocation StartLoc = ConsumeBracket();
2020
Chris Lattner1525c3a2008-12-18 07:27:21 +00002021 // C array syntax has many features, but by-far the most common is [] and [4].
2022 // This code does a fast path to handle some of the most obvious cases.
2023 if (Tok.getKind() == tok::r_square) {
2024 MatchRHSPunctuation(tok::r_square, StartLoc);
2025 // Remember that we parsed the empty array type.
2026 OwningExprResult NumElements(Actions);
2027 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc));
2028 return;
2029 } else if (Tok.getKind() == tok::numeric_constant &&
2030 GetLookAheadToken(1).is(tok::r_square)) {
2031 // [4] is very common. Parse the numeric constant expression.
2032 OwningExprResult ExprRes(Actions, Actions.ActOnNumericConstant(Tok));
2033 ConsumeToken();
2034
2035 MatchRHSPunctuation(tok::r_square, StartLoc);
2036
2037 // If there was an error parsing the assignment-expression, recover.
2038 if (ExprRes.isInvalid())
2039 ExprRes.release(); // Deallocate expr, just use [].
2040
2041 // Remember that we parsed a array type, and remember its features.
2042 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
2043 ExprRes.release(), StartLoc));
2044 return;
2045 }
2046
Chris Lattner4b009652007-07-25 00:24:17 +00002047 // If valid, this location is the position where we read the 'static' keyword.
2048 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002049 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002050 StaticLoc = ConsumeToken();
2051
2052 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002053 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002054 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002055 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002056
2057 // If we haven't already read 'static', check to see if there is one after the
2058 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002059 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002060 StaticLoc = ConsumeToken();
2061
2062 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2063 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002064 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002065
2066 // Handle the case where we have '[*]' as the array size. However, a leading
2067 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2068 // the the token after the star is a ']'. Since stars in arrays are
2069 // infrequent, use of lookahead is not costly here.
2070 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002071 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002072
Chris Lattner306d4df2008-12-18 06:50:14 +00002073 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002074 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002075 StaticLoc = SourceLocation(); // Drop the static.
2076 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002077 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002078 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002079 // Note, in C89, this production uses the constant-expr production instead
2080 // of assignment-expr. The only difference is that assignment-expr allows
2081 // things like '=' and '*='. Sema rejects these in C89 mode because they
2082 // are not i-c-e's, so we don't need to distinguish between the two here.
2083
Chris Lattner4b009652007-07-25 00:24:17 +00002084 // Parse the assignment-expression now.
2085 NumElements = ParseAssignmentExpression();
2086 }
2087
2088 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002089 if (NumElements.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002090 // If the expression was invalid, skip it.
2091 SkipUntil(tok::r_square);
2092 return;
2093 }
2094
2095 MatchRHSPunctuation(tok::r_square, StartLoc);
2096
Chris Lattner1525c3a2008-12-18 07:27:21 +00002097 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002098 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2099 StaticLoc.isValid(), isStar,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002100 NumElements.release(), StartLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00002101}
2102
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002103/// [GNU] typeof-specifier:
2104/// typeof ( expressions )
2105/// typeof ( type-name )
2106/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002107///
2108void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002109 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002110 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002111 SourceLocation StartLoc = ConsumeToken();
2112
Chris Lattner34a01ad2007-10-09 17:33:22 +00002113 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002114 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002115 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002116 return;
2117 }
2118
Sebastian Redl14ca7412008-12-11 21:36:32 +00002119 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002120 if (Result.isInvalid())
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002121 return;
2122
2123 const char *PrevSpec = 0;
2124 // Check for duplicate type specifiers.
2125 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002126 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002127 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002128
2129 // FIXME: Not accurate, the range gets one token more than it should.
2130 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002131 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002132 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002133
Steve Naroff7cbb1462007-07-31 12:34:36 +00002134 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2135
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002136 if (isTypeIdInParens()) {
Steve Naroff7cbb1462007-07-31 12:34:36 +00002137 TypeTy *Ty = ParseTypeName();
2138
Steve Naroff4c255ab2007-07-31 23:56:32 +00002139 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
2140
Chris Lattner34a01ad2007-10-09 17:33:22 +00002141 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002142 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002143 return;
2144 }
2145 RParenLoc = ConsumeParen();
2146 const char *PrevSpec = 0;
2147 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2148 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
Chris Lattnerf006a222008-11-18 07:48:38 +00002149 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002150 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002151 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002152
2153 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002154 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002155 return;
2156 }
2157 RParenLoc = ConsumeParen();
2158 const char *PrevSpec = 0;
2159 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2160 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002161 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002162 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002163 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002164 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002165}
2166
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002167