blob: 23c0c61517d2ef93d8058a26a8541f9c049f6156 [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.
Steve Naroffad620402008-12-25 14:41:26 +0000564 case tok::kw___forceinline:
565 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000566 case tok::kw___cdecl:
567 case tok::kw___stdcall:
568 case tok::kw___fastcall:
569 if (!PP.getLangOptions().Microsoft)
570 goto DoneWithDeclSpec;
571 // Just ignore it.
572 break;
573
Chris Lattner4b009652007-07-25 00:24:17 +0000574 // storage-class-specifier
575 case tok::kw_typedef:
576 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
577 break;
578 case tok::kw_extern:
579 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000580 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000581 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
582 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000583 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000584 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
585 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000586 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000587 case tok::kw_static:
588 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000589 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000590 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
591 break;
592 case tok::kw_auto:
593 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
594 break;
595 case tok::kw_register:
596 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
597 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000598 case tok::kw_mutable:
599 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
600 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000601 case tok::kw___thread:
602 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
603 break;
604
Chris Lattner4b009652007-07-25 00:24:17 +0000605 continue;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000606
Chris Lattner4b009652007-07-25 00:24:17 +0000607 // function-specifier
608 case tok::kw_inline:
609 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
610 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000611
612 case tok::kw_virtual:
613 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
614 break;
615
616 case tok::kw_explicit:
617 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
618 break;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000619
Steve Naroff5f0466b2008-06-05 00:02:44 +0000620 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000621 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000622 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
623 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000624 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000625 goto DoneWithDeclSpec;
626
627 {
628 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000629 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000630 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000631 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000632 DS.SetRangeEnd(EndProtoLoc);
633
Chris Lattnerf006a222008-11-18 07:48:38 +0000634 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
635 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000636 // Need to support trailing type qualifiers (e.g. "id<p> const").
637 // If a type specifier follows, it will be diagnosed elsewhere.
638 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000639 }
Chris Lattner4b009652007-07-25 00:24:17 +0000640 }
641 // If the specifier combination wasn't legal, issue a diagnostic.
642 if (isInvalid) {
643 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000644 // Pick between error or extwarn.
645 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
646 : diag::ext_duplicate_declspec;
647 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000648 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000649 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000650 ConsumeToken();
651 }
652}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000653
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000654/// MaybeParseTypeSpecifier - Try to parse a single type-specifier. We
655/// primarily follow the C++ grammar with additions for C99 and GNU,
656/// which together subsume the C grammar. Note that the C++
657/// type-specifier also includes the C type-qualifier (for const,
658/// volatile, and C99 restrict). Returns true if a type-specifier was
659/// found (and parsed), false otherwise.
660///
661/// type-specifier: [C++ 7.1.5]
662/// simple-type-specifier
663/// class-specifier
664/// enum-specifier
665/// elaborated-type-specifier [TODO]
666/// cv-qualifier
667///
668/// cv-qualifier: [C++ 7.1.5.1]
669/// 'const'
670/// 'volatile'
671/// [C99] 'restrict'
672///
673/// simple-type-specifier: [ C++ 7.1.5.2]
674/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
675/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
676/// 'char'
677/// 'wchar_t'
678/// 'bool'
679/// 'short'
680/// 'int'
681/// 'long'
682/// 'signed'
683/// 'unsigned'
684/// 'float'
685/// 'double'
686/// 'void'
687/// [C99] '_Bool'
688/// [C99] '_Complex'
689/// [C99] '_Imaginary' // Removed in TC2?
690/// [GNU] '_Decimal32'
691/// [GNU] '_Decimal64'
692/// [GNU] '_Decimal128'
693/// [GNU] typeof-specifier
694/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
695/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
696bool Parser::MaybeParseTypeSpecifier(DeclSpec &DS, int& isInvalid,
Douglas Gregor52473432008-12-24 02:52:09 +0000697 const char *&PrevSpec,
698 TemplateParameterLists *TemplateParams) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000699 // Annotate typenames and C++ scope specifiers.
700 TryAnnotateTypeOrScopeToken();
701
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000702 SourceLocation Loc = Tok.getLocation();
703
704 switch (Tok.getKind()) {
705 // simple-type-specifier:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000706 case tok::annot_qualtypename: {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000707 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000708 Tok.getAnnotationValue());
709 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
710 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000711
712 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
713 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
714 // Objective-C interface. If we don't have Objective-C or a '<', this is
715 // just a normal reference to a typedef name.
716 if (!Tok.is(tok::less) || !getLang().ObjC1)
717 return true;
718
719 SourceLocation EndProtoLoc;
720 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
721 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
722 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
723
724 DS.SetRangeEnd(EndProtoLoc);
725 return true;
726 }
727
728 case tok::kw_short:
729 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
730 break;
731 case tok::kw_long:
732 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
733 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
734 else
735 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
736 break;
737 case tok::kw_signed:
738 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
739 break;
740 case tok::kw_unsigned:
741 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
742 break;
743 case tok::kw__Complex:
744 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
745 break;
746 case tok::kw__Imaginary:
747 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
748 break;
749 case tok::kw_void:
750 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
751 break;
752 case tok::kw_char:
753 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
754 break;
755 case tok::kw_int:
756 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
757 break;
758 case tok::kw_float:
759 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
760 break;
761 case tok::kw_double:
762 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
763 break;
764 case tok::kw_wchar_t:
765 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
766 break;
767 case tok::kw_bool:
768 case tok::kw__Bool:
769 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
770 break;
771 case tok::kw__Decimal32:
772 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
773 break;
774 case tok::kw__Decimal64:
775 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
776 break;
777 case tok::kw__Decimal128:
778 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
779 break;
780
781 // class-specifier:
782 case tok::kw_class:
783 case tok::kw_struct:
784 case tok::kw_union:
Douglas Gregor52473432008-12-24 02:52:09 +0000785 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000786 return true;
787
788 // enum-specifier:
789 case tok::kw_enum:
790 ParseEnumSpecifier(DS);
791 return true;
792
793 // cv-qualifier:
794 case tok::kw_const:
795 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
796 getLang())*2;
797 break;
798 case tok::kw_volatile:
799 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
800 getLang())*2;
801 break;
802 case tok::kw_restrict:
803 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
804 getLang())*2;
805 break;
806
807 // GNU typeof support.
808 case tok::kw_typeof:
809 ParseTypeofSpecifier(DS);
810 return true;
811
Steve Naroffedd04d52008-12-25 14:16:32 +0000812 case tok::kw___cdecl:
813 case tok::kw___stdcall:
814 case tok::kw___fastcall:
815 return PP.getLangOptions().Microsoft;
816
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000817 default:
818 // Not a type-specifier; do nothing.
819 return false;
820 }
821
822 // If the specifier combination wasn't legal, issue a diagnostic.
823 if (isInvalid) {
824 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000825 // Pick between error or extwarn.
826 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
827 : diag::ext_duplicate_declspec;
828 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000829 }
830 DS.SetRangeEnd(Tok.getLocation());
831 ConsumeToken(); // whatever we parsed above.
832 return true;
833}
Chris Lattner4b009652007-07-25 00:24:17 +0000834
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000835/// ParseStructDeclaration - Parse a struct declaration without the terminating
836/// semicolon.
837///
Chris Lattner4b009652007-07-25 00:24:17 +0000838/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000839/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000840/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000841/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000842/// struct-declarator-list:
843/// struct-declarator
844/// struct-declarator-list ',' struct-declarator
845/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
846/// struct-declarator:
847/// declarator
848/// [GNU] declarator attributes[opt]
849/// declarator[opt] ':' constant-expression
850/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
851///
Chris Lattner3dd8d392008-04-10 06:46:29 +0000852void Parser::
853ParseStructDeclaration(DeclSpec &DS,
854 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000855 if (Tok.is(tok::kw___extension__)) {
856 // __extension__ silences extension warnings in the subexpression.
857 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +0000858 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000859 return ParseStructDeclaration(DS, Fields);
860 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000861
862 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000863 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +0000864 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +0000865
866 // If there are no declarators, issue a warning.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000867 if (Tok.is(tok::semi)) {
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000868 Diag(DSStart, diag::w_no_declarators);
Steve Naroffa9adf112007-08-20 22:28:22 +0000869 return;
870 }
871
872 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000873 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000874 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +0000875 FieldDeclarator &DeclaratorInfo = Fields.back();
876
Steve Naroffa9adf112007-08-20 22:28:22 +0000877 /// struct-declarator: declarator
878 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000879 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000880 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +0000881
Chris Lattner34a01ad2007-10-09 17:33:22 +0000882 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000883 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000884 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000885 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +0000886 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000887 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000888 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +0000889 }
890
891 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000892 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000893 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000894
895 // If we don't have a comma, it is either the end of the list (a ';')
896 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000897 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000898 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000899
900 // Consume the comma.
901 ConsumeToken();
902
903 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000904 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000905
906 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000907 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000908 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000909 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000910}
911
912/// ParseStructUnionBody
913/// struct-contents:
914/// struct-declaration-list
915/// [EXT] empty
916/// [GNU] "struct-declaration-list" without terminatoring ';'
917/// struct-declaration-list:
918/// struct-declaration
919/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +0000920/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +0000921///
Chris Lattner4b009652007-07-25 00:24:17 +0000922void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
923 unsigned TagType, DeclTy *TagDecl) {
924 SourceLocation LBraceLoc = ConsumeBrace();
925
926 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
927 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +0000928 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +0000929 Diag(Tok, diag::ext_empty_struct_union_enum)
930 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +0000931
932 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000933 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
934
Chris Lattner4b009652007-07-25 00:24:17 +0000935 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000936 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000937 // Each iteration of this loop reads one struct-declaration.
938
939 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000940 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000941 Diag(Tok, diag::ext_extra_struct_semi);
942 ConsumeToken();
943 continue;
944 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000945
946 // Parse all the comma separated declarators.
947 DeclSpec DS;
948 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +0000949 if (!Tok.is(tok::at)) {
950 ParseStructDeclaration(DS, FieldDeclarators);
951
952 // Convert them all to fields.
953 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
954 FieldDeclarator &FD = FieldDeclarators[i];
955 // Install the declarator into the current TagDecl.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000956 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
Chris Lattner1bf58f62008-06-21 19:39:06 +0000957 DS.getSourceRange().getBegin(),
958 FD.D, FD.BitfieldSize);
959 FieldDecls.push_back(Field);
960 }
961 } else { // Handle @defs
962 ConsumeToken();
963 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
964 Diag(Tok, diag::err_unexpected_at);
965 SkipUntil(tok::semi, true, true);
966 continue;
967 }
968 ConsumeToken();
969 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
970 if (!Tok.is(tok::identifier)) {
971 Diag(Tok, diag::err_expected_ident);
972 SkipUntil(tok::semi, true, true);
973 continue;
974 }
975 llvm::SmallVector<DeclTy*, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000976 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
977 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +0000978 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
979 ConsumeToken();
980 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
981 }
Chris Lattner4b009652007-07-25 00:24:17 +0000982
Chris Lattner34a01ad2007-10-09 17:33:22 +0000983 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000984 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +0000985 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000986 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +0000987 break;
988 } else {
989 Diag(Tok, diag::err_expected_semi_decl_list);
990 // Skip to end of block or statement
991 SkipUntil(tok::r_brace, true, true);
992 }
993 }
994
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000995 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000996
Chris Lattner4b009652007-07-25 00:24:17 +0000997 AttributeList *AttrList = 0;
998 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000999 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001000 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001001
1002 Actions.ActOnFields(CurScope,
1003 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1004 LBraceLoc, RBraceLoc,
1005 AttrList);
Chris Lattner4b009652007-07-25 00:24:17 +00001006}
1007
1008
1009/// ParseEnumSpecifier
1010/// enum-specifier: [C99 6.7.2.2]
1011/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001012///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001013/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1014/// '}' attributes[opt]
1015/// 'enum' identifier
1016/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001017///
1018/// [C++] elaborated-type-specifier:
1019/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1020///
Chris Lattner4b009652007-07-25 00:24:17 +00001021void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001022 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +00001023 SourceLocation StartLoc = ConsumeToken();
1024
1025 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001026
1027 AttributeList *Attr = 0;
1028 // If attributes exist after tag, parse them.
1029 if (Tok.is(tok::kw___attribute))
1030 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001031
1032 CXXScopeSpec SS;
Argiris Kirtzidis91c80dc2008-11-26 21:41:52 +00001033 if (getLang().CPlusPlus && MaybeParseCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001034 if (Tok.isNot(tok::identifier)) {
1035 Diag(Tok, diag::err_expected_ident);
1036 if (Tok.isNot(tok::l_brace)) {
1037 // Has no name and is not a definition.
1038 // Skip the rest of this declarator, up until the comma or semicolon.
1039 SkipUntil(tok::comma, true);
1040 return;
1041 }
1042 }
1043 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001044
1045 // Must have either 'enum name' or 'enum {...}'.
1046 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1047 Diag(Tok, diag::err_expected_ident_lbrace);
1048
1049 // Skip the rest of this declarator, up until the comma or semicolon.
1050 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001051 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001052 }
1053
1054 // If an identifier is present, consume and remember it.
1055 IdentifierInfo *Name = 0;
1056 SourceLocation NameLoc;
1057 if (Tok.is(tok::identifier)) {
1058 Name = Tok.getIdentifierInfo();
1059 NameLoc = ConsumeToken();
1060 }
1061
1062 // There are three options here. If we have 'enum foo;', then this is a
1063 // forward declaration. If we have 'enum foo {...' then this is a
1064 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1065 //
1066 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1067 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1068 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1069 //
1070 Action::TagKind TK;
1071 if (Tok.is(tok::l_brace))
1072 TK = Action::TK_Definition;
1073 else if (Tok.is(tok::semi))
1074 TK = Action::TK_Declaration;
1075 else
1076 TK = Action::TK_Reference;
1077 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Douglas Gregor52473432008-12-24 02:52:09 +00001078 SS, Name, NameLoc, Attr,
1079 Action::MultiTemplateParamsArg(Actions));
Chris Lattner4b009652007-07-25 00:24:17 +00001080
Chris Lattner34a01ad2007-10-09 17:33:22 +00001081 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001082 ParseEnumBody(StartLoc, TagDecl);
1083
1084 // TODO: semantic analysis on the declspec for enums.
1085 const char *PrevSpec = 0;
1086 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerf006a222008-11-18 07:48:38 +00001087 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001088}
1089
1090/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1091/// enumerator-list:
1092/// enumerator
1093/// enumerator-list ',' enumerator
1094/// enumerator:
1095/// enumeration-constant
1096/// enumeration-constant '=' constant-expression
1097/// enumeration-constant:
1098/// identifier
1099///
1100void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
1101 SourceLocation LBraceLoc = ConsumeBrace();
1102
Chris Lattnerc9a92452007-08-27 17:24:30 +00001103 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001104 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001105 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001106
1107 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1108
1109 DeclTy *LastEnumConstDecl = 0;
1110
1111 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001112 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001113 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1114 SourceLocation IdentLoc = ConsumeToken();
1115
1116 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001117 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001118 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001119 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001120 AssignedVal = ParseConstantExpression();
1121 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001122 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001123 }
1124
1125 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +00001126 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +00001127 LastEnumConstDecl,
1128 IdentLoc, Ident,
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001129 EqualLoc,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001130 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001131 EnumConstantDecls.push_back(EnumConstDecl);
1132 LastEnumConstDecl = EnumConstDecl;
1133
Chris Lattner34a01ad2007-10-09 17:33:22 +00001134 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001135 break;
1136 SourceLocation CommaLoc = ConsumeToken();
1137
Chris Lattner34a01ad2007-10-09 17:33:22 +00001138 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +00001139 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1140 }
1141
1142 // Eat the }.
1143 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1144
Steve Naroff0acc9c92007-09-15 18:49:24 +00001145 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001146 EnumConstantDecls.size());
1147
1148 DeclTy *AttrList = 0;
1149 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001150 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001151 AttrList = ParseAttributes(); // FIXME: where do they do?
1152}
1153
1154/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001155/// start of a type-qualifier-list.
1156bool Parser::isTypeQualifier() const {
1157 switch (Tok.getKind()) {
1158 default: return false;
1159 // type-qualifier
1160 case tok::kw_const:
1161 case tok::kw_volatile:
1162 case tok::kw_restrict:
1163 return true;
1164 }
1165}
1166
1167/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001168/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001169bool Parser::isTypeSpecifierQualifier() {
1170 // Annotate typenames and C++ scope specifiers.
1171 TryAnnotateTypeOrScopeToken();
1172
Chris Lattner4b009652007-07-25 00:24:17 +00001173 switch (Tok.getKind()) {
1174 default: return false;
1175 // GNU attributes support.
1176 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001177 // GNU typeof support.
1178 case tok::kw_typeof:
1179
Chris Lattner4b009652007-07-25 00:24:17 +00001180 // type-specifiers
1181 case tok::kw_short:
1182 case tok::kw_long:
1183 case tok::kw_signed:
1184 case tok::kw_unsigned:
1185 case tok::kw__Complex:
1186 case tok::kw__Imaginary:
1187 case tok::kw_void:
1188 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001189 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001190 case tok::kw_int:
1191 case tok::kw_float:
1192 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001193 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001194 case tok::kw__Bool:
1195 case tok::kw__Decimal32:
1196 case tok::kw__Decimal64:
1197 case tok::kw__Decimal128:
1198
Chris Lattner2e78db32008-04-13 18:59:07 +00001199 // struct-or-union-specifier (C99) or class-specifier (C++)
1200 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001201 case tok::kw_struct:
1202 case tok::kw_union:
1203 // enum-specifier
1204 case tok::kw_enum:
1205
1206 // type-qualifier
1207 case tok::kw_const:
1208 case tok::kw_volatile:
1209 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001210
1211 // typedef-name
1212 case tok::annot_qualtypename:
Chris Lattner4b009652007-07-25 00:24:17 +00001213 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001214
1215 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1216 case tok::less:
1217 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001218
1219 case tok::kw___cdecl:
1220 case tok::kw___stdcall:
1221 case tok::kw___fastcall:
1222 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001223 }
1224}
1225
1226/// isDeclarationSpecifier() - Return true if the current token is part of a
1227/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001228bool Parser::isDeclarationSpecifier() {
1229 // Annotate typenames and C++ scope specifiers.
1230 TryAnnotateTypeOrScopeToken();
1231
Chris Lattner4b009652007-07-25 00:24:17 +00001232 switch (Tok.getKind()) {
1233 default: return false;
1234 // storage-class-specifier
1235 case tok::kw_typedef:
1236 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001237 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001238 case tok::kw_static:
1239 case tok::kw_auto:
1240 case tok::kw_register:
1241 case tok::kw___thread:
1242
1243 // type-specifiers
1244 case tok::kw_short:
1245 case tok::kw_long:
1246 case tok::kw_signed:
1247 case tok::kw_unsigned:
1248 case tok::kw__Complex:
1249 case tok::kw__Imaginary:
1250 case tok::kw_void:
1251 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001252 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001253 case tok::kw_int:
1254 case tok::kw_float:
1255 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001256 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001257 case tok::kw__Bool:
1258 case tok::kw__Decimal32:
1259 case tok::kw__Decimal64:
1260 case tok::kw__Decimal128:
1261
Chris Lattner2e78db32008-04-13 18:59:07 +00001262 // struct-or-union-specifier (C99) or class-specifier (C++)
1263 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001264 case tok::kw_struct:
1265 case tok::kw_union:
1266 // enum-specifier
1267 case tok::kw_enum:
1268
1269 // type-qualifier
1270 case tok::kw_const:
1271 case tok::kw_volatile:
1272 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001273
Chris Lattner4b009652007-07-25 00:24:17 +00001274 // function-specifier
1275 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001276 case tok::kw_virtual:
1277 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001278
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001279 // typedef-name
1280 case tok::annot_qualtypename:
1281
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001282 // GNU typeof support.
1283 case tok::kw_typeof:
1284
1285 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001286 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001287 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001288
1289 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1290 case tok::less:
1291 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001292
1293 case tok::kw___cdecl:
1294 case tok::kw___stdcall:
1295 case tok::kw___fastcall:
1296 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001297 }
1298}
1299
1300
1301/// ParseTypeQualifierListOpt
1302/// type-qualifier-list: [C99 6.7.5]
1303/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001304/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001305/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001306/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001307///
Chris Lattner460696f2008-12-18 07:02:59 +00001308void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001309 while (1) {
1310 int isInvalid = false;
1311 const char *PrevSpec = 0;
1312 SourceLocation Loc = Tok.getLocation();
1313
1314 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001315 case tok::kw_const:
1316 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1317 getLang())*2;
1318 break;
1319 case tok::kw_volatile:
1320 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1321 getLang())*2;
1322 break;
1323 case tok::kw_restrict:
1324 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1325 getLang())*2;
1326 break;
Steve Naroffad620402008-12-25 14:41:26 +00001327 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001328 case tok::kw___cdecl:
1329 case tok::kw___stdcall:
1330 case tok::kw___fastcall:
1331 if (!PP.getLangOptions().Microsoft)
1332 goto DoneWithTypeQuals;
1333 // Just ignore it.
1334 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001335 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001336 if (AttributesAllowed) {
1337 DS.AddAttributes(ParseAttributes());
1338 continue; // do *not* consume the next token!
1339 }
1340 // otherwise, FALL THROUGH!
1341 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001342 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001343 // If this is not a type-qualifier token, we're done reading type
1344 // qualifiers. First verify that DeclSpec's are consistent.
1345 DS.Finish(Diags, PP.getSourceManager(), getLang());
1346 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001347 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001348
Chris Lattner4b009652007-07-25 00:24:17 +00001349 // If the specifier combination wasn't legal, issue a diagnostic.
1350 if (isInvalid) {
1351 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001352 // Pick between error or extwarn.
1353 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1354 : diag::ext_duplicate_declspec;
1355 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001356 }
1357 ConsumeToken();
1358 }
1359}
1360
1361
1362/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1363///
1364void Parser::ParseDeclarator(Declarator &D) {
1365 /// This implements the 'declarator' production in the C grammar, then checks
1366 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001367 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001368}
1369
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001370/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1371/// is parsed by the function passed to it. Pass null, and the direct-declarator
1372/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001373/// ptr-operator production.
1374///
Chris Lattner4b009652007-07-25 00:24:17 +00001375/// declarator: [C99 6.7.5]
1376/// pointer[opt] direct-declarator
1377/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1378/// [GNU] '&' restrict[opt] attributes[opt] declarator
1379///
1380/// pointer: [C99 6.7.5]
1381/// '*' type-qualifier-list[opt]
1382/// '*' type-qualifier-list[opt] pointer
1383///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001384/// ptr-operator:
1385/// '*' cv-qualifier-seq[opt]
1386/// '&'
1387/// [GNU] '&' restrict[opt] attributes[opt]
1388/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] [TODO]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001389void Parser::ParseDeclaratorInternal(Declarator &D,
1390 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001391 tok::TokenKind Kind = Tok.getKind();
1392
Steve Naroff7aa54752008-08-27 16:04:49 +00001393 // Not a pointer, C++ reference, or block.
1394 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001395 (Kind != tok::caret || !getLang().Blocks)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001396 if (DirectDeclParser)
1397 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001398 return;
1399 }
Chris Lattner4b009652007-07-25 00:24:17 +00001400
Steve Naroffdc22f212008-08-28 10:07:06 +00001401 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Chris Lattner4b009652007-07-25 00:24:17 +00001402 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1403
Steve Naroffdc22f212008-08-28 10:07:06 +00001404 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner69f01932008-02-21 01:32:26 +00001405 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001406 DeclSpec DS;
1407
1408 ParseTypeQualifierListOpt(DS);
1409
1410 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001411 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001412 if (Kind == tok::star)
1413 // Remember that we parsed a pointer type, and remember the type-quals.
1414 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1415 DS.TakeAttributes()));
1416 else
1417 // Remember that we parsed a Block type, and remember the type-quals.
1418 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1419 Loc));
Chris Lattner4b009652007-07-25 00:24:17 +00001420 } else {
1421 // Is a reference
1422 DeclSpec DS;
1423
1424 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1425 // cv-qualifiers are introduced through the use of a typedef or of a
1426 // template type argument, in which case the cv-qualifiers are ignored.
1427 //
1428 // [GNU] Retricted references are allowed.
1429 // [GNU] Attributes on references are allowed.
1430 ParseTypeQualifierListOpt(DS);
1431
1432 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1433 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1434 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001435 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001436 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1437 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001438 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001439 }
1440
1441 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001442 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001443
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001444 if (D.getNumTypeObjects() > 0) {
1445 // C++ [dcl.ref]p4: There shall be no references to references.
1446 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1447 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001448 if (const IdentifierInfo *II = D.getIdentifier())
1449 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1450 << II;
1451 else
1452 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1453 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001454
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001455 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001456 // can go ahead and build the (technically ill-formed)
1457 // declarator: reference collapsing will take care of it.
1458 }
1459 }
1460
Chris Lattner4b009652007-07-25 00:24:17 +00001461 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001462 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1463 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001464 }
1465}
1466
1467/// ParseDirectDeclarator
1468/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001469/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001470/// '(' declarator ')'
1471/// [GNU] '(' attributes declarator ')'
1472/// [C90] direct-declarator '[' constant-expression[opt] ']'
1473/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1474/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1475/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1476/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1477/// direct-declarator '(' parameter-type-list ')'
1478/// direct-declarator '(' identifier-list[opt] ')'
1479/// [GNU] direct-declarator '(' parameter-forward-declarations
1480/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001481/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1482/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001483/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001484///
1485/// declarator-id: [C++ 8]
1486/// id-expression
1487/// '::'[opt] nested-name-specifier[opt] type-name
1488///
1489/// id-expression: [C++ 5.1]
1490/// unqualified-id
1491/// qualified-id [TODO]
1492///
1493/// unqualified-id: [C++ 5.1]
1494/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001495/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001496/// conversion-function-id [TODO]
1497/// '~' class-name
1498/// template-id [TODO]
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001499///
Chris Lattner4b009652007-07-25 00:24:17 +00001500void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001501 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001502
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001503 if (getLang().CPlusPlus) {
1504 if (D.mayHaveIdentifier()) {
1505 bool afterCXXScope = MaybeParseCXXScopeSpecifier(D.getCXXScopeSpec());
1506 if (afterCXXScope) {
1507 // Change the declaration context for name lookup, until this function
1508 // is exited (and the declarator has been parsed).
1509 DeclScopeObj.EnterDeclaratorScope();
1510 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001511
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001512 if (Tok.is(tok::identifier)) {
1513 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor2fa10442008-12-18 19:37:40 +00001514
1515 // If this identifier is followed by a '<', we may have a template-id.
1516 DeclTy *Template;
Douglas Gregor853dd392008-12-26 15:00:45 +00001517 if (NextToken().is(tok::less) &&
Douglas Gregor2fa10442008-12-18 19:37:40 +00001518 (Template = Actions.isTemplateName(*Tok.getIdentifierInfo(),
1519 CurScope))) {
1520 IdentifierInfo *II = Tok.getIdentifierInfo();
1521 AnnotateTemplateIdToken(Template, 0);
1522 // FIXME: Set the declarator to a template-id. How? I don't
1523 // know... for now, just use the identifier.
1524 D.SetIdentifier(II, Tok.getLocation());
1525 }
1526 // If this identifier is the name of the current class, it's a
1527 // constructor name.
Douglas Gregor853dd392008-12-26 15:00:45 +00001528 else if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope))
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001529 D.setConstructor(Actions.isTypeName(*Tok.getIdentifierInfo(),
1530 CurScope),
1531 Tok.getLocation());
Douglas Gregor2fa10442008-12-18 19:37:40 +00001532 // This is a normal identifier.
1533 else
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001534 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1535 ConsumeToken();
1536 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00001537 } else if (Tok.is(tok::kw_operator)) {
1538 SourceLocation OperatorLoc = Tok.getLocation();
Douglas Gregore60e5d32008-11-06 22:13:31 +00001539
Douglas Gregor853dd392008-12-26 15:00:45 +00001540 // First try the name of an overloaded operator
1541 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId()) {
1542 D.setOverloadedOperator(Op, OperatorLoc);
1543 } else {
1544 // This must be a conversion function (C++ [class.conv.fct]).
1545 if (TypeTy *ConvType = ParseConversionFunctionId())
1546 D.setConversionFunction(ConvType, OperatorLoc);
1547 else
1548 D.SetIdentifier(0, Tok.getLocation());
1549 }
1550 goto PastIdentifier;
1551 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001552 // This should be a C++ destructor.
1553 SourceLocation TildeLoc = ConsumeToken();
1554 if (Tok.is(tok::identifier)) {
1555 if (TypeTy *Type = ParseClassName())
1556 D.setDestructor(Type, TildeLoc);
1557 else
1558 D.SetIdentifier(0, TildeLoc);
1559 } else {
1560 Diag(Tok, diag::err_expected_class_name);
1561 D.SetIdentifier(0, TildeLoc);
1562 }
1563 goto PastIdentifier;
1564 }
1565
1566 // If we reached this point, token is not identifier and not '~'.
1567
1568 if (afterCXXScope) {
1569 Diag(Tok, diag::err_expected_unqualified_id);
1570 D.SetIdentifier(0, Tok.getLocation());
1571 D.setInvalidType(true);
1572 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001573 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001574 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001575 }
1576
1577 // If we reached this point, we are either in C/ObjC or the token didn't
1578 // satisfy any of the C++-specific checks.
1579
1580 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1581 assert(!getLang().CPlusPlus &&
1582 "There's a C++-specific check for tok::identifier above");
1583 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1584 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1585 ConsumeToken();
1586 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001587 // direct-declarator: '(' declarator ')'
1588 // direct-declarator: '(' attributes declarator ')'
1589 // Example: 'char (*X)' or 'int (*XX)(void)'
1590 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001591 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001592 // This could be something simple like "int" (in which case the declarator
1593 // portion is empty), if an abstract-declarator is allowed.
1594 D.SetIdentifier(0, Tok.getLocation());
1595 } else {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001596 if (getLang().CPlusPlus)
1597 Diag(Tok, diag::err_expected_unqualified_id);
1598 else
Chris Lattnerf006a222008-11-18 07:48:38 +00001599 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00001600 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00001601 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001602 }
1603
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001604 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00001605 assert(D.isPastIdentifier() &&
1606 "Haven't past the location of the identifier yet?");
1607
1608 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001609 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001610 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1611 // In such a case, check if we actually have a function declarator; if it
1612 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001613 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1614 // When not in file scope, warn for ambiguous function declarators, just
1615 // in case the author intended it as a variable definition.
1616 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1617 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1618 break;
1619 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001620 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001621 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001622 ParseBracketDeclarator(D);
1623 } else {
1624 break;
1625 }
1626 }
1627}
1628
Chris Lattnera0d056d2008-04-06 05:45:57 +00001629/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1630/// only called before the identifier, so these are most likely just grouping
1631/// parens for precedence. If we find that these are actually function
1632/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1633///
1634/// direct-declarator:
1635/// '(' declarator ')'
1636/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001637/// direct-declarator '(' parameter-type-list ')'
1638/// direct-declarator '(' identifier-list[opt] ')'
1639/// [GNU] direct-declarator '(' parameter-forward-declarations
1640/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001641///
1642void Parser::ParseParenDeclarator(Declarator &D) {
1643 SourceLocation StartLoc = ConsumeParen();
1644 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1645
Chris Lattner1f185292008-10-20 02:05:46 +00001646 // Eat any attributes before we look at whether this is a grouping or function
1647 // declarator paren. If this is a grouping paren, the attribute applies to
1648 // the type being built up, for example:
1649 // int (__attribute__(()) *x)(long y)
1650 // If this ends up not being a grouping paren, the attribute applies to the
1651 // first argument, for example:
1652 // int (__attribute__(()) int x)
1653 // In either case, we need to eat any attributes to be able to determine what
1654 // sort of paren this is.
1655 //
1656 AttributeList *AttrList = 0;
1657 bool RequiresArg = false;
1658 if (Tok.is(tok::kw___attribute)) {
1659 AttrList = ParseAttributes();
1660
1661 // We require that the argument list (if this is a non-grouping paren) be
1662 // present even if the attribute list was empty.
1663 RequiresArg = true;
1664 }
Steve Naroffedd04d52008-12-25 14:16:32 +00001665 // Eat any Microsoft extensions.
1666 if ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
1667 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
1668 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00001669
Chris Lattnera0d056d2008-04-06 05:45:57 +00001670 // If we haven't past the identifier yet (or where the identifier would be
1671 // stored, if this is an abstract declarator), then this is probably just
1672 // grouping parens. However, if this could be an abstract-declarator, then
1673 // this could also be the start of function arguments (consider 'void()').
1674 bool isGrouping;
1675
1676 if (!D.mayOmitIdentifier()) {
1677 // If this can't be an abstract-declarator, this *must* be a grouping
1678 // paren, because we haven't seen the identifier yet.
1679 isGrouping = true;
1680 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001681 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00001682 isDeclarationSpecifier()) { // 'int(int)' is a function.
1683 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1684 // considered to be a type, not a K&R identifier-list.
1685 isGrouping = false;
1686 } else {
1687 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1688 isGrouping = true;
1689 }
1690
1691 // If this is a grouping paren, handle:
1692 // direct-declarator: '(' declarator ')'
1693 // direct-declarator: '(' attributes declarator ')'
1694 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001695 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001696 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00001697 if (AttrList)
1698 D.AddAttributes(AttrList);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001699
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001700 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001701 // Match the ')'.
1702 MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001703
1704 D.setGroupingParens(hadGroupingParens);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001705 return;
1706 }
1707
1708 // Okay, if this wasn't a grouping paren, it must be the start of a function
1709 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00001710 // identifier (and remember where it would have been), then call into
1711 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00001712 D.SetIdentifier(0, Tok.getLocation());
1713
Chris Lattner1f185292008-10-20 02:05:46 +00001714 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001715}
1716
1717/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1718/// declarator D up to a paren, which indicates that we are parsing function
1719/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001720///
Chris Lattner1f185292008-10-20 02:05:46 +00001721/// If AttrList is non-null, then the caller parsed those arguments immediately
1722/// after the open paren - they should be considered to be the first argument of
1723/// a parameter. If RequiresArg is true, then the first argument of the
1724/// function is required to be present and required to not be an identifier
1725/// list.
1726///
Chris Lattner4b009652007-07-25 00:24:17 +00001727/// This method also handles this portion of the grammar:
1728/// parameter-type-list: [C99 6.7.5]
1729/// parameter-list
1730/// parameter-list ',' '...'
1731///
1732/// parameter-list: [C99 6.7.5]
1733/// parameter-declaration
1734/// parameter-list ',' parameter-declaration
1735///
1736/// parameter-declaration: [C99 6.7.5]
1737/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00001738/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001739/// [GNU] declaration-specifiers declarator attributes
1740/// declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00001741/// [C++] declaration-specifiers abstract-declarator[opt]
1742/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001743/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1744///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001745/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
1746/// and "exception-specification[opt]"(TODO).
1747///
Chris Lattner1f185292008-10-20 02:05:46 +00001748void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
1749 AttributeList *AttrList,
1750 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00001751 // lparen is already consumed!
1752 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00001753
Chris Lattner1f185292008-10-20 02:05:46 +00001754 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001755 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00001756 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001757 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00001758 delete AttrList;
1759 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001760
1761 ConsumeParen(); // Eat the closing ')'.
1762
1763 // cv-qualifier-seq[opt].
1764 DeclSpec DS;
1765 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00001766 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor90a2c972008-11-25 03:22:00 +00001767
1768 // Parse exception-specification[opt].
1769 if (Tok.is(tok::kw_throw))
1770 ParseExceptionSpecification();
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001771 }
1772
Chris Lattner9f7564b2008-04-06 06:57:35 +00001773 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00001774 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001775 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00001776 /*variadic*/ false,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001777 /*arglist*/ 0, 0,
1778 DS.getTypeQualifiers(),
1779 LParenLoc));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001780 return;
Chris Lattner1f185292008-10-20 02:05:46 +00001781 }
1782
1783 // Alternatively, this parameter list may be an identifier list form for a
1784 // K&R-style function: void foo(a,b,c)
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001785 if (!getLang().CPlusPlus && Tok.is(tok::identifier) &&
Chris Lattner1f185292008-10-20 02:05:46 +00001786 // K&R identifier lists can't have typedefs as identifiers, per
1787 // C99 6.7.5.3p11.
1788 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1789 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001790 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00001791 delete AttrList;
1792 }
1793
Chris Lattner4b009652007-07-25 00:24:17 +00001794 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1795 // normal declarators, not for abstract-declarators.
Chris Lattner35d9c912008-04-06 06:34:08 +00001796 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001797 }
1798
1799 // Finally, a normal, non-empty parameter type list.
1800
1801 // Build up an array of information about the parsed arguments.
1802 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00001803
1804 // Enter function-declaration scope, limiting any declarators to the
1805 // function prototype scope, including parameter declarators.
Douglas Gregor95d40792008-12-10 06:34:36 +00001806 ParseScope PrototypeScope(this, Scope::FnScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001807
1808 bool IsVariadic = false;
1809 while (1) {
1810 if (Tok.is(tok::ellipsis)) {
1811 IsVariadic = true;
Chris Lattner4b009652007-07-25 00:24:17 +00001812
Chris Lattner9f7564b2008-04-06 06:57:35 +00001813 // Check to see if this is "void(...)" which is not allowed.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001814 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00001815 // Otherwise, parse parameter type list. If it starts with an
1816 // ellipsis, diagnose the malformed function.
1817 Diag(Tok, diag::err_ellipsis_first_arg);
1818 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001819 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00001820
Chris Lattner9f7564b2008-04-06 06:57:35 +00001821 ConsumeToken(); // Consume the ellipsis.
1822 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001823 }
1824
Chris Lattner9f7564b2008-04-06 06:57:35 +00001825 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00001826
Chris Lattner9f7564b2008-04-06 06:57:35 +00001827 // Parse the declaration-specifiers.
1828 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00001829
1830 // If the caller parsed attributes for the first argument, add them now.
1831 if (AttrList) {
1832 DS.AddAttributes(AttrList);
1833 AttrList = 0; // Only apply the attributes to the first parameter.
1834 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001835 ParseDeclarationSpecifiers(DS);
1836
1837 // Parse the declarator. This is "PrototypeContext", because we must
1838 // accept either 'declarator' or 'abstract-declarator' here.
1839 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1840 ParseDeclarator(ParmDecl);
1841
1842 // Parse GNU attributes, if present.
1843 if (Tok.is(tok::kw___attribute))
1844 ParmDecl.AddAttributes(ParseAttributes());
1845
Chris Lattner9f7564b2008-04-06 06:57:35 +00001846 // Remember this parsed parameter in ParamInfo.
1847 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1848
Douglas Gregor605de8d2008-12-16 21:30:33 +00001849 // DefArgToks is used when the parsing of default arguments needs
1850 // to be delayed.
1851 CachedTokens *DefArgToks = 0;
1852
Chris Lattner9f7564b2008-04-06 06:57:35 +00001853 // If no parameter was specified, verify that *something* was specified,
1854 // otherwise we have a missing type and identifier.
1855 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1856 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1857 // Completely missing, emit error.
1858 Diag(DSStart, diag::err_missing_param);
1859 } else {
1860 // Otherwise, we have something. Add it and let semantic analysis try
1861 // to grok it and add the result to the ParamInfo we are building.
1862
1863 // Inform the actions module about the parameter declarator, so it gets
1864 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00001865 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1866
1867 // Parse the default argument, if any. We parse the default
1868 // arguments in all dialects; the semantic analysis in
1869 // ActOnParamDefaultArgument will reject the default argument in
1870 // C.
1871 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00001872 SourceLocation EqualLoc = Tok.getLocation();
1873
Chris Lattner3e254fb2008-04-08 04:40:51 +00001874 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00001875 if (D.getContext() == Declarator::MemberContext) {
1876 // If we're inside a class definition, cache the tokens
1877 // corresponding to the default argument. We'll actually parse
1878 // them when we see the end of the class definition.
1879 // FIXME: Templates will require something similar.
1880 // FIXME: Can we use a smart pointer for Toks?
1881 DefArgToks = new CachedTokens;
1882
1883 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
1884 tok::semi, false)) {
1885 delete DefArgToks;
1886 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00001887 Actions.ActOnParamDefaultArgumentError(Param);
1888 } else
1889 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00001890 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00001891 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00001892 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00001893
1894 OwningExprResult DefArgResult(ParseAssignmentExpression());
1895 if (DefArgResult.isInvalid()) {
1896 Actions.ActOnParamDefaultArgumentError(Param);
1897 SkipUntil(tok::comma, tok::r_paren, true, true);
1898 } else {
1899 // Inform the actions module about the default argument
1900 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
1901 DefArgResult.release());
1902 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00001903 }
1904 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001905
1906 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00001907 ParmDecl.getIdentifierLoc(), Param,
1908 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001909 }
1910
1911 // If the next token is a comma, consume it and keep reading arguments.
1912 if (Tok.isNot(tok::comma)) break;
1913
1914 // Consume the comma.
1915 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00001916 }
1917
Chris Lattner9f7564b2008-04-06 06:57:35 +00001918 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00001919 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00001920
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001921 // If we have the closing ')', eat it.
1922 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1923
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001924 DeclSpec DS;
1925 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00001926 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00001927 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor90a2c972008-11-25 03:22:00 +00001928
1929 // Parse exception-specification[opt].
1930 if (Tok.is(tok::kw_throw))
1931 ParseExceptionSpecification();
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001932 }
1933
Chris Lattner4b009652007-07-25 00:24:17 +00001934 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001935 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1936 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001937 DS.getTypeQualifiers(),
Chris Lattner9f7564b2008-04-06 06:57:35 +00001938 LParenLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00001939}
1940
Chris Lattner35d9c912008-04-06 06:34:08 +00001941/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1942/// we found a K&R-style identifier list instead of a type argument list. The
1943/// current token is known to be the first identifier in the list.
1944///
1945/// identifier-list: [C99 6.7.5]
1946/// identifier
1947/// identifier-list ',' identifier
1948///
1949void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1950 Declarator &D) {
1951 // Build up an array of information about the parsed arguments.
1952 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1953 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1954
1955 // If there was no identifier specified for the declarator, either we are in
1956 // an abstract-declarator, or we are in a parameter declarator which was found
1957 // to be abstract. In abstract-declarators, identifier lists are not valid:
1958 // diagnose this.
1959 if (!D.getIdentifier())
1960 Diag(Tok, diag::ext_ident_list_in_param);
1961
1962 // Tok is known to be the first identifier in the list. Remember this
1963 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00001964 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00001965 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1966 Tok.getLocation(), 0));
1967
Chris Lattner113a56b2008-04-06 06:39:19 +00001968 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00001969
1970 while (Tok.is(tok::comma)) {
1971 // Eat the comma.
1972 ConsumeToken();
1973
Chris Lattner113a56b2008-04-06 06:39:19 +00001974 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00001975 if (Tok.isNot(tok::identifier)) {
1976 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00001977 SkipUntil(tok::r_paren);
1978 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00001979 }
Chris Lattneracb67d92008-04-06 06:47:48 +00001980
Chris Lattner35d9c912008-04-06 06:34:08 +00001981 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00001982
1983 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1984 if (Actions.isTypeName(*ParmII, CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00001985 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00001986
1987 // Verify that the argument identifier has not already been mentioned.
1988 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001989 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00001990 } else {
1991 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00001992 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1993 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00001994 }
Chris Lattner35d9c912008-04-06 06:34:08 +00001995
1996 // Eat the identifier.
1997 ConsumeToken();
1998 }
1999
Chris Lattner113a56b2008-04-06 06:39:19 +00002000 // Remember that we parsed a function type, and remember the attributes. This
2001 // function type is always a K&R style function type, which is not varargs and
2002 // has no prototype.
2003 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
2004 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002005 /*TypeQuals*/0, LParenLoc));
Chris Lattner35d9c912008-04-06 06:34:08 +00002006
2007 // If we have the closing ')', eat it and we're done.
Chris Lattner113a56b2008-04-06 06:39:19 +00002008 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002009}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002010
Chris Lattner4b009652007-07-25 00:24:17 +00002011/// [C90] direct-declarator '[' constant-expression[opt] ']'
2012/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2013/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2014/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2015/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2016void Parser::ParseBracketDeclarator(Declarator &D) {
2017 SourceLocation StartLoc = ConsumeBracket();
2018
Chris Lattner1525c3a2008-12-18 07:27:21 +00002019 // C array syntax has many features, but by-far the most common is [] and [4].
2020 // This code does a fast path to handle some of the most obvious cases.
2021 if (Tok.getKind() == tok::r_square) {
2022 MatchRHSPunctuation(tok::r_square, StartLoc);
2023 // Remember that we parsed the empty array type.
2024 OwningExprResult NumElements(Actions);
2025 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc));
2026 return;
2027 } else if (Tok.getKind() == tok::numeric_constant &&
2028 GetLookAheadToken(1).is(tok::r_square)) {
2029 // [4] is very common. Parse the numeric constant expression.
2030 OwningExprResult ExprRes(Actions, Actions.ActOnNumericConstant(Tok));
2031 ConsumeToken();
2032
2033 MatchRHSPunctuation(tok::r_square, StartLoc);
2034
2035 // If there was an error parsing the assignment-expression, recover.
2036 if (ExprRes.isInvalid())
2037 ExprRes.release(); // Deallocate expr, just use [].
2038
2039 // Remember that we parsed a array type, and remember its features.
2040 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
2041 ExprRes.release(), StartLoc));
2042 return;
2043 }
2044
Chris Lattner4b009652007-07-25 00:24:17 +00002045 // If valid, this location is the position where we read the 'static' keyword.
2046 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002047 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002048 StaticLoc = ConsumeToken();
2049
2050 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002051 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002052 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002053 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002054
2055 // If we haven't already read 'static', check to see if there is one after the
2056 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002057 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002058 StaticLoc = ConsumeToken();
2059
2060 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2061 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002062 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002063
2064 // Handle the case where we have '[*]' as the array size. However, a leading
2065 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2066 // the the token after the star is a ']'. Since stars in arrays are
2067 // infrequent, use of lookahead is not costly here.
2068 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002069 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002070
Chris Lattner306d4df2008-12-18 06:50:14 +00002071 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002072 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002073 StaticLoc = SourceLocation(); // Drop the static.
2074 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002075 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002076 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002077 // Note, in C89, this production uses the constant-expr production instead
2078 // of assignment-expr. The only difference is that assignment-expr allows
2079 // things like '=' and '*='. Sema rejects these in C89 mode because they
2080 // are not i-c-e's, so we don't need to distinguish between the two here.
2081
Chris Lattner4b009652007-07-25 00:24:17 +00002082 // Parse the assignment-expression now.
2083 NumElements = ParseAssignmentExpression();
2084 }
2085
2086 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002087 if (NumElements.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002088 // If the expression was invalid, skip it.
2089 SkipUntil(tok::r_square);
2090 return;
2091 }
2092
2093 MatchRHSPunctuation(tok::r_square, StartLoc);
2094
Chris Lattner1525c3a2008-12-18 07:27:21 +00002095 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002096 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2097 StaticLoc.isValid(), isStar,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002098 NumElements.release(), StartLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00002099}
2100
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002101/// [GNU] typeof-specifier:
2102/// typeof ( expressions )
2103/// typeof ( type-name )
2104/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002105///
2106void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002107 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002108 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002109 SourceLocation StartLoc = ConsumeToken();
2110
Chris Lattner34a01ad2007-10-09 17:33:22 +00002111 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002112 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002113 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002114 return;
2115 }
2116
Sebastian Redl14ca7412008-12-11 21:36:32 +00002117 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002118 if (Result.isInvalid())
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002119 return;
2120
2121 const char *PrevSpec = 0;
2122 // Check for duplicate type specifiers.
2123 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002124 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002125 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002126
2127 // FIXME: Not accurate, the range gets one token more than it should.
2128 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002129 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002130 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002131
Steve Naroff7cbb1462007-07-31 12:34:36 +00002132 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2133
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002134 if (isTypeIdInParens()) {
Steve Naroff7cbb1462007-07-31 12:34:36 +00002135 TypeTy *Ty = ParseTypeName();
2136
Steve Naroff4c255ab2007-07-31 23:56:32 +00002137 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
2138
Chris Lattner34a01ad2007-10-09 17:33:22 +00002139 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002140 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002141 return;
2142 }
2143 RParenLoc = ConsumeParen();
2144 const char *PrevSpec = 0;
2145 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2146 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
Chris Lattnerf006a222008-11-18 07:48:38 +00002147 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002148 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002149 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002150
2151 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002152 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002153 return;
2154 }
2155 RParenLoc = ConsumeParen();
2156 const char *PrevSpec = 0;
2157 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2158 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002159 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002160 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002161 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002162 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002163}
2164
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002165