blob: a8052dc2904292573d1626f70976d4cdfcae065e [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
563 // storage-class-specifier
564 case tok::kw_typedef:
565 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
566 break;
567 case tok::kw_extern:
568 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000569 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000570 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
571 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000572 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000573 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
574 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000575 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000576 case tok::kw_static:
577 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000578 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000579 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
580 break;
581 case tok::kw_auto:
582 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
583 break;
584 case tok::kw_register:
585 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
586 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000587 case tok::kw_mutable:
588 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
589 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000590 case tok::kw___thread:
591 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
592 break;
593
Chris Lattner4b009652007-07-25 00:24:17 +0000594 continue;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000595
Chris Lattner4b009652007-07-25 00:24:17 +0000596 // function-specifier
597 case tok::kw_inline:
598 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
599 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000600
601 case tok::kw_virtual:
602 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
603 break;
604
605 case tok::kw_explicit:
606 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
607 break;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000608
Steve Naroff5f0466b2008-06-05 00:02:44 +0000609 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000610 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000611 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
612 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000613 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000614 goto DoneWithDeclSpec;
615
616 {
617 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000618 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000619 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000620 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000621 DS.SetRangeEnd(EndProtoLoc);
622
Chris Lattnerf006a222008-11-18 07:48:38 +0000623 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
624 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000625 // Need to support trailing type qualifiers (e.g. "id<p> const").
626 // If a type specifier follows, it will be diagnosed elsewhere.
627 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000628 }
Chris Lattner4b009652007-07-25 00:24:17 +0000629 }
630 // If the specifier combination wasn't legal, issue a diagnostic.
631 if (isInvalid) {
632 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000633 // Pick between error or extwarn.
634 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
635 : diag::ext_duplicate_declspec;
636 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000637 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000638 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000639 ConsumeToken();
640 }
641}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000642
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000643/// MaybeParseTypeSpecifier - Try to parse a single type-specifier. We
644/// primarily follow the C++ grammar with additions for C99 and GNU,
645/// which together subsume the C grammar. Note that the C++
646/// type-specifier also includes the C type-qualifier (for const,
647/// volatile, and C99 restrict). Returns true if a type-specifier was
648/// found (and parsed), false otherwise.
649///
650/// type-specifier: [C++ 7.1.5]
651/// simple-type-specifier
652/// class-specifier
653/// enum-specifier
654/// elaborated-type-specifier [TODO]
655/// cv-qualifier
656///
657/// cv-qualifier: [C++ 7.1.5.1]
658/// 'const'
659/// 'volatile'
660/// [C99] 'restrict'
661///
662/// simple-type-specifier: [ C++ 7.1.5.2]
663/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
664/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
665/// 'char'
666/// 'wchar_t'
667/// 'bool'
668/// 'short'
669/// 'int'
670/// 'long'
671/// 'signed'
672/// 'unsigned'
673/// 'float'
674/// 'double'
675/// 'void'
676/// [C99] '_Bool'
677/// [C99] '_Complex'
678/// [C99] '_Imaginary' // Removed in TC2?
679/// [GNU] '_Decimal32'
680/// [GNU] '_Decimal64'
681/// [GNU] '_Decimal128'
682/// [GNU] typeof-specifier
683/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
684/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
685bool Parser::MaybeParseTypeSpecifier(DeclSpec &DS, int& isInvalid,
Douglas Gregor52473432008-12-24 02:52:09 +0000686 const char *&PrevSpec,
687 TemplateParameterLists *TemplateParams) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000688 // Annotate typenames and C++ scope specifiers.
689 TryAnnotateTypeOrScopeToken();
690
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000691 SourceLocation Loc = Tok.getLocation();
692
693 switch (Tok.getKind()) {
694 // simple-type-specifier:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000695 case tok::annot_qualtypename: {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000696 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000697 Tok.getAnnotationValue());
698 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
699 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000700
701 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
702 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
703 // Objective-C interface. If we don't have Objective-C or a '<', this is
704 // just a normal reference to a typedef name.
705 if (!Tok.is(tok::less) || !getLang().ObjC1)
706 return true;
707
708 SourceLocation EndProtoLoc;
709 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
710 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
711 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
712
713 DS.SetRangeEnd(EndProtoLoc);
714 return true;
715 }
716
717 case tok::kw_short:
718 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
719 break;
720 case tok::kw_long:
721 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
722 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
723 else
724 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
725 break;
726 case tok::kw_signed:
727 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
728 break;
729 case tok::kw_unsigned:
730 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
731 break;
732 case tok::kw__Complex:
733 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
734 break;
735 case tok::kw__Imaginary:
736 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
737 break;
738 case tok::kw_void:
739 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
740 break;
741 case tok::kw_char:
742 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
743 break;
744 case tok::kw_int:
745 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
746 break;
747 case tok::kw_float:
748 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
749 break;
750 case tok::kw_double:
751 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
752 break;
753 case tok::kw_wchar_t:
754 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
755 break;
756 case tok::kw_bool:
757 case tok::kw__Bool:
758 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
759 break;
760 case tok::kw__Decimal32:
761 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
762 break;
763 case tok::kw__Decimal64:
764 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
765 break;
766 case tok::kw__Decimal128:
767 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
768 break;
769
770 // class-specifier:
771 case tok::kw_class:
772 case tok::kw_struct:
773 case tok::kw_union:
Douglas Gregor52473432008-12-24 02:52:09 +0000774 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000775 return true;
776
777 // enum-specifier:
778 case tok::kw_enum:
779 ParseEnumSpecifier(DS);
780 return true;
781
782 // cv-qualifier:
783 case tok::kw_const:
784 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
785 getLang())*2;
786 break;
787 case tok::kw_volatile:
788 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
789 getLang())*2;
790 break;
791 case tok::kw_restrict:
792 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
793 getLang())*2;
794 break;
795
796 // GNU typeof support.
797 case tok::kw_typeof:
798 ParseTypeofSpecifier(DS);
799 return true;
800
801 default:
802 // Not a type-specifier; do nothing.
803 return false;
804 }
805
806 // If the specifier combination wasn't legal, issue a diagnostic.
807 if (isInvalid) {
808 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000809 // Pick between error or extwarn.
810 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
811 : diag::ext_duplicate_declspec;
812 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000813 }
814 DS.SetRangeEnd(Tok.getLocation());
815 ConsumeToken(); // whatever we parsed above.
816 return true;
817}
Chris Lattner4b009652007-07-25 00:24:17 +0000818
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000819/// ParseStructDeclaration - Parse a struct declaration without the terminating
820/// semicolon.
821///
Chris Lattner4b009652007-07-25 00:24:17 +0000822/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000823/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000824/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000825/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000826/// struct-declarator-list:
827/// struct-declarator
828/// struct-declarator-list ',' struct-declarator
829/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
830/// struct-declarator:
831/// declarator
832/// [GNU] declarator attributes[opt]
833/// declarator[opt] ':' constant-expression
834/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
835///
Chris Lattner3dd8d392008-04-10 06:46:29 +0000836void Parser::
837ParseStructDeclaration(DeclSpec &DS,
838 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000839 if (Tok.is(tok::kw___extension__)) {
840 // __extension__ silences extension warnings in the subexpression.
841 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +0000842 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000843 return ParseStructDeclaration(DS, Fields);
844 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000845
846 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000847 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +0000848 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +0000849
850 // If there are no declarators, issue a warning.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000851 if (Tok.is(tok::semi)) {
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000852 Diag(DSStart, diag::w_no_declarators);
Steve Naroffa9adf112007-08-20 22:28:22 +0000853 return;
854 }
855
856 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000857 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000858 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +0000859 FieldDeclarator &DeclaratorInfo = Fields.back();
860
Steve Naroffa9adf112007-08-20 22:28:22 +0000861 /// struct-declarator: declarator
862 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000863 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000864 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +0000865
Chris Lattner34a01ad2007-10-09 17:33:22 +0000866 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000867 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000868 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000869 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +0000870 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000871 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000872 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +0000873 }
874
875 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000876 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000877 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000878
879 // If we don't have a comma, it is either the end of the list (a ';')
880 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000881 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000882 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000883
884 // Consume the comma.
885 ConsumeToken();
886
887 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000888 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000889
890 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000891 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000892 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000893 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000894}
895
896/// ParseStructUnionBody
897/// struct-contents:
898/// struct-declaration-list
899/// [EXT] empty
900/// [GNU] "struct-declaration-list" without terminatoring ';'
901/// struct-declaration-list:
902/// struct-declaration
903/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +0000904/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +0000905///
Chris Lattner4b009652007-07-25 00:24:17 +0000906void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
907 unsigned TagType, DeclTy *TagDecl) {
908 SourceLocation LBraceLoc = ConsumeBrace();
909
910 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
911 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +0000912 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +0000913 Diag(Tok, diag::ext_empty_struct_union_enum)
914 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +0000915
916 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000917 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
918
Chris Lattner4b009652007-07-25 00:24:17 +0000919 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000920 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000921 // Each iteration of this loop reads one struct-declaration.
922
923 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000924 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000925 Diag(Tok, diag::ext_extra_struct_semi);
926 ConsumeToken();
927 continue;
928 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000929
930 // Parse all the comma separated declarators.
931 DeclSpec DS;
932 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +0000933 if (!Tok.is(tok::at)) {
934 ParseStructDeclaration(DS, FieldDeclarators);
935
936 // Convert them all to fields.
937 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
938 FieldDeclarator &FD = FieldDeclarators[i];
939 // Install the declarator into the current TagDecl.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000940 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
Chris Lattner1bf58f62008-06-21 19:39:06 +0000941 DS.getSourceRange().getBegin(),
942 FD.D, FD.BitfieldSize);
943 FieldDecls.push_back(Field);
944 }
945 } else { // Handle @defs
946 ConsumeToken();
947 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
948 Diag(Tok, diag::err_unexpected_at);
949 SkipUntil(tok::semi, true, true);
950 continue;
951 }
952 ConsumeToken();
953 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
954 if (!Tok.is(tok::identifier)) {
955 Diag(Tok, diag::err_expected_ident);
956 SkipUntil(tok::semi, true, true);
957 continue;
958 }
959 llvm::SmallVector<DeclTy*, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000960 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
961 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +0000962 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
963 ConsumeToken();
964 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
965 }
Chris Lattner4b009652007-07-25 00:24:17 +0000966
Chris Lattner34a01ad2007-10-09 17:33:22 +0000967 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000968 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +0000969 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000970 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +0000971 break;
972 } else {
973 Diag(Tok, diag::err_expected_semi_decl_list);
974 // Skip to end of block or statement
975 SkipUntil(tok::r_brace, true, true);
976 }
977 }
978
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000979 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000980
Chris Lattner4b009652007-07-25 00:24:17 +0000981 AttributeList *AttrList = 0;
982 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000983 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +0000984 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +0000985
986 Actions.ActOnFields(CurScope,
987 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
988 LBraceLoc, RBraceLoc,
989 AttrList);
Chris Lattner4b009652007-07-25 00:24:17 +0000990}
991
992
993/// ParseEnumSpecifier
994/// enum-specifier: [C99 6.7.2.2]
995/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000996///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +0000997/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
998/// '}' attributes[opt]
999/// 'enum' identifier
1000/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001001///
1002/// [C++] elaborated-type-specifier:
1003/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1004///
Chris Lattner4b009652007-07-25 00:24:17 +00001005void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001006 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +00001007 SourceLocation StartLoc = ConsumeToken();
1008
1009 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001010
1011 AttributeList *Attr = 0;
1012 // If attributes exist after tag, parse them.
1013 if (Tok.is(tok::kw___attribute))
1014 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001015
1016 CXXScopeSpec SS;
Argiris Kirtzidis91c80dc2008-11-26 21:41:52 +00001017 if (getLang().CPlusPlus && MaybeParseCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001018 if (Tok.isNot(tok::identifier)) {
1019 Diag(Tok, diag::err_expected_ident);
1020 if (Tok.isNot(tok::l_brace)) {
1021 // Has no name and is not a definition.
1022 // Skip the rest of this declarator, up until the comma or semicolon.
1023 SkipUntil(tok::comma, true);
1024 return;
1025 }
1026 }
1027 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001028
1029 // Must have either 'enum name' or 'enum {...}'.
1030 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1031 Diag(Tok, diag::err_expected_ident_lbrace);
1032
1033 // Skip the rest of this declarator, up until the comma or semicolon.
1034 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001035 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001036 }
1037
1038 // If an identifier is present, consume and remember it.
1039 IdentifierInfo *Name = 0;
1040 SourceLocation NameLoc;
1041 if (Tok.is(tok::identifier)) {
1042 Name = Tok.getIdentifierInfo();
1043 NameLoc = ConsumeToken();
1044 }
1045
1046 // There are three options here. If we have 'enum foo;', then this is a
1047 // forward declaration. If we have 'enum foo {...' then this is a
1048 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1049 //
1050 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1051 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1052 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1053 //
1054 Action::TagKind TK;
1055 if (Tok.is(tok::l_brace))
1056 TK = Action::TK_Definition;
1057 else if (Tok.is(tok::semi))
1058 TK = Action::TK_Declaration;
1059 else
1060 TK = Action::TK_Reference;
1061 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Douglas Gregor52473432008-12-24 02:52:09 +00001062 SS, Name, NameLoc, Attr,
1063 Action::MultiTemplateParamsArg(Actions));
Chris Lattner4b009652007-07-25 00:24:17 +00001064
Chris Lattner34a01ad2007-10-09 17:33:22 +00001065 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001066 ParseEnumBody(StartLoc, TagDecl);
1067
1068 // TODO: semantic analysis on the declspec for enums.
1069 const char *PrevSpec = 0;
1070 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerf006a222008-11-18 07:48:38 +00001071 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001072}
1073
1074/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1075/// enumerator-list:
1076/// enumerator
1077/// enumerator-list ',' enumerator
1078/// enumerator:
1079/// enumeration-constant
1080/// enumeration-constant '=' constant-expression
1081/// enumeration-constant:
1082/// identifier
1083///
1084void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
1085 SourceLocation LBraceLoc = ConsumeBrace();
1086
Chris Lattnerc9a92452007-08-27 17:24:30 +00001087 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001088 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001089 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001090
1091 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1092
1093 DeclTy *LastEnumConstDecl = 0;
1094
1095 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001096 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001097 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1098 SourceLocation IdentLoc = ConsumeToken();
1099
1100 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001101 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001102 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001103 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001104 AssignedVal = ParseConstantExpression();
1105 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001106 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001107 }
1108
1109 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +00001110 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +00001111 LastEnumConstDecl,
1112 IdentLoc, Ident,
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001113 EqualLoc,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001114 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001115 EnumConstantDecls.push_back(EnumConstDecl);
1116 LastEnumConstDecl = EnumConstDecl;
1117
Chris Lattner34a01ad2007-10-09 17:33:22 +00001118 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001119 break;
1120 SourceLocation CommaLoc = ConsumeToken();
1121
Chris Lattner34a01ad2007-10-09 17:33:22 +00001122 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +00001123 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1124 }
1125
1126 // Eat the }.
1127 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1128
Steve Naroff0acc9c92007-09-15 18:49:24 +00001129 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001130 EnumConstantDecls.size());
1131
1132 DeclTy *AttrList = 0;
1133 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001134 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001135 AttrList = ParseAttributes(); // FIXME: where do they do?
1136}
1137
1138/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001139/// start of a type-qualifier-list.
1140bool Parser::isTypeQualifier() const {
1141 switch (Tok.getKind()) {
1142 default: return false;
1143 // type-qualifier
1144 case tok::kw_const:
1145 case tok::kw_volatile:
1146 case tok::kw_restrict:
1147 return true;
1148 }
1149}
1150
1151/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001152/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001153bool Parser::isTypeSpecifierQualifier() {
1154 // Annotate typenames and C++ scope specifiers.
1155 TryAnnotateTypeOrScopeToken();
1156
Chris Lattner4b009652007-07-25 00:24:17 +00001157 switch (Tok.getKind()) {
1158 default: return false;
1159 // GNU attributes support.
1160 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001161 // GNU typeof support.
1162 case tok::kw_typeof:
1163
Chris Lattner4b009652007-07-25 00:24:17 +00001164 // type-specifiers
1165 case tok::kw_short:
1166 case tok::kw_long:
1167 case tok::kw_signed:
1168 case tok::kw_unsigned:
1169 case tok::kw__Complex:
1170 case tok::kw__Imaginary:
1171 case tok::kw_void:
1172 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001173 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001174 case tok::kw_int:
1175 case tok::kw_float:
1176 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001177 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001178 case tok::kw__Bool:
1179 case tok::kw__Decimal32:
1180 case tok::kw__Decimal64:
1181 case tok::kw__Decimal128:
1182
Chris Lattner2e78db32008-04-13 18:59:07 +00001183 // struct-or-union-specifier (C99) or class-specifier (C++)
1184 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001185 case tok::kw_struct:
1186 case tok::kw_union:
1187 // enum-specifier
1188 case tok::kw_enum:
1189
1190 // type-qualifier
1191 case tok::kw_const:
1192 case tok::kw_volatile:
1193 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001194
1195 // typedef-name
1196 case tok::annot_qualtypename:
Chris Lattner4b009652007-07-25 00:24:17 +00001197 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001198
1199 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1200 case tok::less:
1201 return getLang().ObjC1;
Chris Lattner4b009652007-07-25 00:24:17 +00001202 }
1203}
1204
1205/// isDeclarationSpecifier() - Return true if the current token is part of a
1206/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001207bool Parser::isDeclarationSpecifier() {
1208 // Annotate typenames and C++ scope specifiers.
1209 TryAnnotateTypeOrScopeToken();
1210
Chris Lattner4b009652007-07-25 00:24:17 +00001211 switch (Tok.getKind()) {
1212 default: return false;
1213 // storage-class-specifier
1214 case tok::kw_typedef:
1215 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001216 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001217 case tok::kw_static:
1218 case tok::kw_auto:
1219 case tok::kw_register:
1220 case tok::kw___thread:
1221
1222 // type-specifiers
1223 case tok::kw_short:
1224 case tok::kw_long:
1225 case tok::kw_signed:
1226 case tok::kw_unsigned:
1227 case tok::kw__Complex:
1228 case tok::kw__Imaginary:
1229 case tok::kw_void:
1230 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001231 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001232 case tok::kw_int:
1233 case tok::kw_float:
1234 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001235 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001236 case tok::kw__Bool:
1237 case tok::kw__Decimal32:
1238 case tok::kw__Decimal64:
1239 case tok::kw__Decimal128:
1240
Chris Lattner2e78db32008-04-13 18:59:07 +00001241 // struct-or-union-specifier (C99) or class-specifier (C++)
1242 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001243 case tok::kw_struct:
1244 case tok::kw_union:
1245 // enum-specifier
1246 case tok::kw_enum:
1247
1248 // type-qualifier
1249 case tok::kw_const:
1250 case tok::kw_volatile:
1251 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001252
Chris Lattner4b009652007-07-25 00:24:17 +00001253 // function-specifier
1254 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001255 case tok::kw_virtual:
1256 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001257
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001258 // typedef-name
1259 case tok::annot_qualtypename:
1260
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001261 // GNU typeof support.
1262 case tok::kw_typeof:
1263
1264 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001265 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001266 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001267
1268 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1269 case tok::less:
1270 return getLang().ObjC1;
Chris Lattner4b009652007-07-25 00:24:17 +00001271 }
1272}
1273
1274
1275/// ParseTypeQualifierListOpt
1276/// type-qualifier-list: [C99 6.7.5]
1277/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001278/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001279/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001280/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001281///
Chris Lattner460696f2008-12-18 07:02:59 +00001282void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001283 while (1) {
1284 int isInvalid = false;
1285 const char *PrevSpec = 0;
1286 SourceLocation Loc = Tok.getLocation();
1287
1288 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001289 case tok::kw_const:
1290 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1291 getLang())*2;
1292 break;
1293 case tok::kw_volatile:
1294 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1295 getLang())*2;
1296 break;
1297 case tok::kw_restrict:
1298 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1299 getLang())*2;
1300 break;
1301 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001302 if (AttributesAllowed) {
1303 DS.AddAttributes(ParseAttributes());
1304 continue; // do *not* consume the next token!
1305 }
1306 // otherwise, FALL THROUGH!
1307 default:
1308 // If this is not a type-qualifier token, we're done reading type
1309 // qualifiers. First verify that DeclSpec's are consistent.
1310 DS.Finish(Diags, PP.getSourceManager(), getLang());
1311 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001312 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001313
Chris Lattner4b009652007-07-25 00:24:17 +00001314 // If the specifier combination wasn't legal, issue a diagnostic.
1315 if (isInvalid) {
1316 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001317 // Pick between error or extwarn.
1318 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1319 : diag::ext_duplicate_declspec;
1320 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001321 }
1322 ConsumeToken();
1323 }
1324}
1325
1326
1327/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1328///
1329void Parser::ParseDeclarator(Declarator &D) {
1330 /// This implements the 'declarator' production in the C grammar, then checks
1331 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001332 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001333}
1334
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001335/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1336/// is parsed by the function passed to it. Pass null, and the direct-declarator
1337/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001338/// ptr-operator production.
1339///
Chris Lattner4b009652007-07-25 00:24:17 +00001340/// declarator: [C99 6.7.5]
1341/// pointer[opt] direct-declarator
1342/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1343/// [GNU] '&' restrict[opt] attributes[opt] declarator
1344///
1345/// pointer: [C99 6.7.5]
1346/// '*' type-qualifier-list[opt]
1347/// '*' type-qualifier-list[opt] pointer
1348///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001349/// ptr-operator:
1350/// '*' cv-qualifier-seq[opt]
1351/// '&'
1352/// [GNU] '&' restrict[opt] attributes[opt]
1353/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] [TODO]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001354void Parser::ParseDeclaratorInternal(Declarator &D,
1355 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001356 tok::TokenKind Kind = Tok.getKind();
1357
Steve Naroff7aa54752008-08-27 16:04:49 +00001358 // Not a pointer, C++ reference, or block.
1359 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001360 (Kind != tok::caret || !getLang().Blocks)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001361 if (DirectDeclParser)
1362 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001363 return;
1364 }
Chris Lattner4b009652007-07-25 00:24:17 +00001365
Steve Naroffdc22f212008-08-28 10:07:06 +00001366 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Chris Lattner4b009652007-07-25 00:24:17 +00001367 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1368
Steve Naroffdc22f212008-08-28 10:07:06 +00001369 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner69f01932008-02-21 01:32:26 +00001370 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001371 DeclSpec DS;
1372
1373 ParseTypeQualifierListOpt(DS);
1374
1375 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001376 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001377 if (Kind == tok::star)
1378 // Remember that we parsed a pointer type, and remember the type-quals.
1379 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1380 DS.TakeAttributes()));
1381 else
1382 // Remember that we parsed a Block type, and remember the type-quals.
1383 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1384 Loc));
Chris Lattner4b009652007-07-25 00:24:17 +00001385 } else {
1386 // Is a reference
1387 DeclSpec DS;
1388
1389 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1390 // cv-qualifiers are introduced through the use of a typedef or of a
1391 // template type argument, in which case the cv-qualifiers are ignored.
1392 //
1393 // [GNU] Retricted references are allowed.
1394 // [GNU] Attributes on references are allowed.
1395 ParseTypeQualifierListOpt(DS);
1396
1397 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1398 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1399 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001400 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001401 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1402 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001403 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001404 }
1405
1406 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001407 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001408
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001409 if (D.getNumTypeObjects() > 0) {
1410 // C++ [dcl.ref]p4: There shall be no references to references.
1411 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1412 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001413 if (const IdentifierInfo *II = D.getIdentifier())
1414 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1415 << II;
1416 else
1417 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1418 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001419
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001420 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001421 // can go ahead and build the (technically ill-formed)
1422 // declarator: reference collapsing will take care of it.
1423 }
1424 }
1425
Chris Lattner4b009652007-07-25 00:24:17 +00001426 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001427 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1428 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001429 }
1430}
1431
1432/// ParseDirectDeclarator
1433/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001434/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001435/// '(' declarator ')'
1436/// [GNU] '(' attributes declarator ')'
1437/// [C90] direct-declarator '[' constant-expression[opt] ']'
1438/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1439/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1440/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1441/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1442/// direct-declarator '(' parameter-type-list ')'
1443/// direct-declarator '(' identifier-list[opt] ')'
1444/// [GNU] direct-declarator '(' parameter-forward-declarations
1445/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001446/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1447/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001448/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001449///
1450/// declarator-id: [C++ 8]
1451/// id-expression
1452/// '::'[opt] nested-name-specifier[opt] type-name
1453///
1454/// id-expression: [C++ 5.1]
1455/// unqualified-id
1456/// qualified-id [TODO]
1457///
1458/// unqualified-id: [C++ 5.1]
1459/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001460/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001461/// conversion-function-id [TODO]
1462/// '~' class-name
1463/// template-id [TODO]
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001464///
Chris Lattner4b009652007-07-25 00:24:17 +00001465void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001466 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001467
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001468 if (getLang().CPlusPlus) {
1469 if (D.mayHaveIdentifier()) {
1470 bool afterCXXScope = MaybeParseCXXScopeSpecifier(D.getCXXScopeSpec());
1471 if (afterCXXScope) {
1472 // Change the declaration context for name lookup, until this function
1473 // is exited (and the declarator has been parsed).
1474 DeclScopeObj.EnterDeclaratorScope();
1475 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001476
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001477 if (Tok.is(tok::identifier)) {
1478 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor2fa10442008-12-18 19:37:40 +00001479
1480 // If this identifier is followed by a '<', we may have a template-id.
1481 DeclTy *Template;
1482 if (getLang().CPlusPlus && NextToken().is(tok::less) &&
1483 (Template = Actions.isTemplateName(*Tok.getIdentifierInfo(),
1484 CurScope))) {
1485 IdentifierInfo *II = Tok.getIdentifierInfo();
1486 AnnotateTemplateIdToken(Template, 0);
1487 // FIXME: Set the declarator to a template-id. How? I don't
1488 // know... for now, just use the identifier.
1489 D.SetIdentifier(II, Tok.getLocation());
1490 }
1491 // If this identifier is the name of the current class, it's a
1492 // constructor name.
1493 else if (getLang().CPlusPlus &&
1494 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope))
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001495 D.setConstructor(Actions.isTypeName(*Tok.getIdentifierInfo(),
1496 CurScope),
1497 Tok.getLocation());
Douglas Gregor2fa10442008-12-18 19:37:40 +00001498 // This is a normal identifier.
1499 else
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001500 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1501 ConsumeToken();
1502 goto PastIdentifier;
1503 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001504
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001505 if (Tok.is(tok::tilde)) {
1506 // This should be a C++ destructor.
1507 SourceLocation TildeLoc = ConsumeToken();
1508 if (Tok.is(tok::identifier)) {
1509 if (TypeTy *Type = ParseClassName())
1510 D.setDestructor(Type, TildeLoc);
1511 else
1512 D.SetIdentifier(0, TildeLoc);
1513 } else {
1514 Diag(Tok, diag::err_expected_class_name);
1515 D.SetIdentifier(0, TildeLoc);
1516 }
1517 goto PastIdentifier;
1518 }
1519
1520 // If we reached this point, token is not identifier and not '~'.
1521
1522 if (afterCXXScope) {
1523 Diag(Tok, diag::err_expected_unqualified_id);
1524 D.SetIdentifier(0, Tok.getLocation());
1525 D.setInvalidType(true);
1526 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001527 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001528 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001529
1530 if (Tok.is(tok::kw_operator)) {
1531 SourceLocation OperatorLoc = Tok.getLocation();
1532
1533 // First try the name of an overloaded operator
1534 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId()) {
1535 D.setOverloadedOperator(Op, OperatorLoc);
1536 } else {
1537 // This must be a conversion function (C++ [class.conv.fct]).
1538 if (TypeTy *ConvType = ParseConversionFunctionId())
1539 D.setConversionFunction(ConvType, OperatorLoc);
1540 else
1541 D.SetIdentifier(0, Tok.getLocation());
1542 }
1543 goto PastIdentifier;
1544 }
1545 }
1546
1547 // If we reached this point, we are either in C/ObjC or the token didn't
1548 // satisfy any of the C++-specific checks.
1549
1550 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1551 assert(!getLang().CPlusPlus &&
1552 "There's a C++-specific check for tok::identifier above");
1553 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1554 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1555 ConsumeToken();
1556 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001557 // direct-declarator: '(' declarator ')'
1558 // direct-declarator: '(' attributes declarator ')'
1559 // Example: 'char (*X)' or 'int (*XX)(void)'
1560 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001561 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001562 // This could be something simple like "int" (in which case the declarator
1563 // portion is empty), if an abstract-declarator is allowed.
1564 D.SetIdentifier(0, Tok.getLocation());
1565 } else {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001566 if (getLang().CPlusPlus)
1567 Diag(Tok, diag::err_expected_unqualified_id);
1568 else
Chris Lattnerf006a222008-11-18 07:48:38 +00001569 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00001570 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00001571 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001572 }
1573
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001574 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00001575 assert(D.isPastIdentifier() &&
1576 "Haven't past the location of the identifier yet?");
1577
1578 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001579 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001580 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1581 // In such a case, check if we actually have a function declarator; if it
1582 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001583 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1584 // When not in file scope, warn for ambiguous function declarators, just
1585 // in case the author intended it as a variable definition.
1586 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1587 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1588 break;
1589 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001590 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001591 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001592 ParseBracketDeclarator(D);
1593 } else {
1594 break;
1595 }
1596 }
1597}
1598
Chris Lattnera0d056d2008-04-06 05:45:57 +00001599/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1600/// only called before the identifier, so these are most likely just grouping
1601/// parens for precedence. If we find that these are actually function
1602/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1603///
1604/// direct-declarator:
1605/// '(' declarator ')'
1606/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001607/// direct-declarator '(' parameter-type-list ')'
1608/// direct-declarator '(' identifier-list[opt] ')'
1609/// [GNU] direct-declarator '(' parameter-forward-declarations
1610/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001611///
1612void Parser::ParseParenDeclarator(Declarator &D) {
1613 SourceLocation StartLoc = ConsumeParen();
1614 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1615
Chris Lattner1f185292008-10-20 02:05:46 +00001616 // Eat any attributes before we look at whether this is a grouping or function
1617 // declarator paren. If this is a grouping paren, the attribute applies to
1618 // the type being built up, for example:
1619 // int (__attribute__(()) *x)(long y)
1620 // If this ends up not being a grouping paren, the attribute applies to the
1621 // first argument, for example:
1622 // int (__attribute__(()) int x)
1623 // In either case, we need to eat any attributes to be able to determine what
1624 // sort of paren this is.
1625 //
1626 AttributeList *AttrList = 0;
1627 bool RequiresArg = false;
1628 if (Tok.is(tok::kw___attribute)) {
1629 AttrList = ParseAttributes();
1630
1631 // We require that the argument list (if this is a non-grouping paren) be
1632 // present even if the attribute list was empty.
1633 RequiresArg = true;
1634 }
1635
Chris Lattnera0d056d2008-04-06 05:45:57 +00001636 // If we haven't past the identifier yet (or where the identifier would be
1637 // stored, if this is an abstract declarator), then this is probably just
1638 // grouping parens. However, if this could be an abstract-declarator, then
1639 // this could also be the start of function arguments (consider 'void()').
1640 bool isGrouping;
1641
1642 if (!D.mayOmitIdentifier()) {
1643 // If this can't be an abstract-declarator, this *must* be a grouping
1644 // paren, because we haven't seen the identifier yet.
1645 isGrouping = true;
1646 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001647 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00001648 isDeclarationSpecifier()) { // 'int(int)' is a function.
1649 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1650 // considered to be a type, not a K&R identifier-list.
1651 isGrouping = false;
1652 } else {
1653 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1654 isGrouping = true;
1655 }
1656
1657 // If this is a grouping paren, handle:
1658 // direct-declarator: '(' declarator ')'
1659 // direct-declarator: '(' attributes declarator ')'
1660 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001661 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001662 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00001663 if (AttrList)
1664 D.AddAttributes(AttrList);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001665
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001666 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001667 // Match the ')'.
1668 MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001669
1670 D.setGroupingParens(hadGroupingParens);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001671 return;
1672 }
1673
1674 // Okay, if this wasn't a grouping paren, it must be the start of a function
1675 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00001676 // identifier (and remember where it would have been), then call into
1677 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00001678 D.SetIdentifier(0, Tok.getLocation());
1679
Chris Lattner1f185292008-10-20 02:05:46 +00001680 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001681}
1682
1683/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1684/// declarator D up to a paren, which indicates that we are parsing function
1685/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001686///
Chris Lattner1f185292008-10-20 02:05:46 +00001687/// If AttrList is non-null, then the caller parsed those arguments immediately
1688/// after the open paren - they should be considered to be the first argument of
1689/// a parameter. If RequiresArg is true, then the first argument of the
1690/// function is required to be present and required to not be an identifier
1691/// list.
1692///
Chris Lattner4b009652007-07-25 00:24:17 +00001693/// This method also handles this portion of the grammar:
1694/// parameter-type-list: [C99 6.7.5]
1695/// parameter-list
1696/// parameter-list ',' '...'
1697///
1698/// parameter-list: [C99 6.7.5]
1699/// parameter-declaration
1700/// parameter-list ',' parameter-declaration
1701///
1702/// parameter-declaration: [C99 6.7.5]
1703/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00001704/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001705/// [GNU] declaration-specifiers declarator attributes
1706/// declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00001707/// [C++] declaration-specifiers abstract-declarator[opt]
1708/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001709/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1710///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001711/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
1712/// and "exception-specification[opt]"(TODO).
1713///
Chris Lattner1f185292008-10-20 02:05:46 +00001714void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
1715 AttributeList *AttrList,
1716 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00001717 // lparen is already consumed!
1718 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00001719
Chris Lattner1f185292008-10-20 02:05:46 +00001720 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001721 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00001722 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001723 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00001724 delete AttrList;
1725 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001726
1727 ConsumeParen(); // Eat the closing ')'.
1728
1729 // cv-qualifier-seq[opt].
1730 DeclSpec DS;
1731 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00001732 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor90a2c972008-11-25 03:22:00 +00001733
1734 // Parse exception-specification[opt].
1735 if (Tok.is(tok::kw_throw))
1736 ParseExceptionSpecification();
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001737 }
1738
Chris Lattner9f7564b2008-04-06 06:57:35 +00001739 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00001740 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001741 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00001742 /*variadic*/ false,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001743 /*arglist*/ 0, 0,
1744 DS.getTypeQualifiers(),
1745 LParenLoc));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001746 return;
Chris Lattner1f185292008-10-20 02:05:46 +00001747 }
1748
1749 // Alternatively, this parameter list may be an identifier list form for a
1750 // K&R-style function: void foo(a,b,c)
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001751 if (!getLang().CPlusPlus && Tok.is(tok::identifier) &&
Chris Lattner1f185292008-10-20 02:05:46 +00001752 // K&R identifier lists can't have typedefs as identifiers, per
1753 // C99 6.7.5.3p11.
1754 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1755 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001756 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00001757 delete AttrList;
1758 }
1759
Chris Lattner4b009652007-07-25 00:24:17 +00001760 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1761 // normal declarators, not for abstract-declarators.
Chris Lattner35d9c912008-04-06 06:34:08 +00001762 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001763 }
1764
1765 // Finally, a normal, non-empty parameter type list.
1766
1767 // Build up an array of information about the parsed arguments.
1768 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00001769
1770 // Enter function-declaration scope, limiting any declarators to the
1771 // function prototype scope, including parameter declarators.
Douglas Gregor95d40792008-12-10 06:34:36 +00001772 ParseScope PrototypeScope(this, Scope::FnScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001773
1774 bool IsVariadic = false;
1775 while (1) {
1776 if (Tok.is(tok::ellipsis)) {
1777 IsVariadic = true;
Chris Lattner4b009652007-07-25 00:24:17 +00001778
Chris Lattner9f7564b2008-04-06 06:57:35 +00001779 // Check to see if this is "void(...)" which is not allowed.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001780 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00001781 // Otherwise, parse parameter type list. If it starts with an
1782 // ellipsis, diagnose the malformed function.
1783 Diag(Tok, diag::err_ellipsis_first_arg);
1784 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001785 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00001786
Chris Lattner9f7564b2008-04-06 06:57:35 +00001787 ConsumeToken(); // Consume the ellipsis.
1788 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001789 }
1790
Chris Lattner9f7564b2008-04-06 06:57:35 +00001791 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00001792
Chris Lattner9f7564b2008-04-06 06:57:35 +00001793 // Parse the declaration-specifiers.
1794 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00001795
1796 // If the caller parsed attributes for the first argument, add them now.
1797 if (AttrList) {
1798 DS.AddAttributes(AttrList);
1799 AttrList = 0; // Only apply the attributes to the first parameter.
1800 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001801 ParseDeclarationSpecifiers(DS);
1802
1803 // Parse the declarator. This is "PrototypeContext", because we must
1804 // accept either 'declarator' or 'abstract-declarator' here.
1805 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1806 ParseDeclarator(ParmDecl);
1807
1808 // Parse GNU attributes, if present.
1809 if (Tok.is(tok::kw___attribute))
1810 ParmDecl.AddAttributes(ParseAttributes());
1811
Chris Lattner9f7564b2008-04-06 06:57:35 +00001812 // Remember this parsed parameter in ParamInfo.
1813 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1814
Douglas Gregor605de8d2008-12-16 21:30:33 +00001815 // DefArgToks is used when the parsing of default arguments needs
1816 // to be delayed.
1817 CachedTokens *DefArgToks = 0;
1818
Chris Lattner9f7564b2008-04-06 06:57:35 +00001819 // If no parameter was specified, verify that *something* was specified,
1820 // otherwise we have a missing type and identifier.
1821 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1822 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1823 // Completely missing, emit error.
1824 Diag(DSStart, diag::err_missing_param);
1825 } else {
1826 // Otherwise, we have something. Add it and let semantic analysis try
1827 // to grok it and add the result to the ParamInfo we are building.
1828
1829 // Inform the actions module about the parameter declarator, so it gets
1830 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00001831 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1832
1833 // Parse the default argument, if any. We parse the default
1834 // arguments in all dialects; the semantic analysis in
1835 // ActOnParamDefaultArgument will reject the default argument in
1836 // C.
1837 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00001838 SourceLocation EqualLoc = Tok.getLocation();
1839
Chris Lattner3e254fb2008-04-08 04:40:51 +00001840 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00001841 if (D.getContext() == Declarator::MemberContext) {
1842 // If we're inside a class definition, cache the tokens
1843 // corresponding to the default argument. We'll actually parse
1844 // them when we see the end of the class definition.
1845 // FIXME: Templates will require something similar.
1846 // FIXME: Can we use a smart pointer for Toks?
1847 DefArgToks = new CachedTokens;
1848
1849 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
1850 tok::semi, false)) {
1851 delete DefArgToks;
1852 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00001853 Actions.ActOnParamDefaultArgumentError(Param);
1854 } else
1855 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00001856 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00001857 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00001858 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00001859
1860 OwningExprResult DefArgResult(ParseAssignmentExpression());
1861 if (DefArgResult.isInvalid()) {
1862 Actions.ActOnParamDefaultArgumentError(Param);
1863 SkipUntil(tok::comma, tok::r_paren, true, true);
1864 } else {
1865 // Inform the actions module about the default argument
1866 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
1867 DefArgResult.release());
1868 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00001869 }
1870 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001871
1872 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00001873 ParmDecl.getIdentifierLoc(), Param,
1874 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001875 }
1876
1877 // If the next token is a comma, consume it and keep reading arguments.
1878 if (Tok.isNot(tok::comma)) break;
1879
1880 // Consume the comma.
1881 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00001882 }
1883
Chris Lattner9f7564b2008-04-06 06:57:35 +00001884 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00001885 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00001886
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001887 // If we have the closing ')', eat it.
1888 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1889
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001890 DeclSpec DS;
1891 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00001892 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00001893 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor90a2c972008-11-25 03:22:00 +00001894
1895 // Parse exception-specification[opt].
1896 if (Tok.is(tok::kw_throw))
1897 ParseExceptionSpecification();
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001898 }
1899
Chris Lattner4b009652007-07-25 00:24:17 +00001900 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001901 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1902 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001903 DS.getTypeQualifiers(),
Chris Lattner9f7564b2008-04-06 06:57:35 +00001904 LParenLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00001905}
1906
Chris Lattner35d9c912008-04-06 06:34:08 +00001907/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1908/// we found a K&R-style identifier list instead of a type argument list. The
1909/// current token is known to be the first identifier in the list.
1910///
1911/// identifier-list: [C99 6.7.5]
1912/// identifier
1913/// identifier-list ',' identifier
1914///
1915void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1916 Declarator &D) {
1917 // Build up an array of information about the parsed arguments.
1918 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1919 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1920
1921 // If there was no identifier specified for the declarator, either we are in
1922 // an abstract-declarator, or we are in a parameter declarator which was found
1923 // to be abstract. In abstract-declarators, identifier lists are not valid:
1924 // diagnose this.
1925 if (!D.getIdentifier())
1926 Diag(Tok, diag::ext_ident_list_in_param);
1927
1928 // Tok is known to be the first identifier in the list. Remember this
1929 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00001930 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00001931 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1932 Tok.getLocation(), 0));
1933
Chris Lattner113a56b2008-04-06 06:39:19 +00001934 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00001935
1936 while (Tok.is(tok::comma)) {
1937 // Eat the comma.
1938 ConsumeToken();
1939
Chris Lattner113a56b2008-04-06 06:39:19 +00001940 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00001941 if (Tok.isNot(tok::identifier)) {
1942 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00001943 SkipUntil(tok::r_paren);
1944 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00001945 }
Chris Lattneracb67d92008-04-06 06:47:48 +00001946
Chris Lattner35d9c912008-04-06 06:34:08 +00001947 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00001948
1949 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1950 if (Actions.isTypeName(*ParmII, CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00001951 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00001952
1953 // Verify that the argument identifier has not already been mentioned.
1954 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001955 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00001956 } else {
1957 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00001958 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1959 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00001960 }
Chris Lattner35d9c912008-04-06 06:34:08 +00001961
1962 // Eat the identifier.
1963 ConsumeToken();
1964 }
1965
Chris Lattner113a56b2008-04-06 06:39:19 +00001966 // Remember that we parsed a function type, and remember the attributes. This
1967 // function type is always a K&R style function type, which is not varargs and
1968 // has no prototype.
1969 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1970 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001971 /*TypeQuals*/0, LParenLoc));
Chris Lattner35d9c912008-04-06 06:34:08 +00001972
1973 // If we have the closing ')', eat it and we're done.
Chris Lattner113a56b2008-04-06 06:39:19 +00001974 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00001975}
Chris Lattnera0d056d2008-04-06 05:45:57 +00001976
Chris Lattner4b009652007-07-25 00:24:17 +00001977/// [C90] direct-declarator '[' constant-expression[opt] ']'
1978/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1979/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1980/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1981/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1982void Parser::ParseBracketDeclarator(Declarator &D) {
1983 SourceLocation StartLoc = ConsumeBracket();
1984
Chris Lattner1525c3a2008-12-18 07:27:21 +00001985 // C array syntax has many features, but by-far the most common is [] and [4].
1986 // This code does a fast path to handle some of the most obvious cases.
1987 if (Tok.getKind() == tok::r_square) {
1988 MatchRHSPunctuation(tok::r_square, StartLoc);
1989 // Remember that we parsed the empty array type.
1990 OwningExprResult NumElements(Actions);
1991 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc));
1992 return;
1993 } else if (Tok.getKind() == tok::numeric_constant &&
1994 GetLookAheadToken(1).is(tok::r_square)) {
1995 // [4] is very common. Parse the numeric constant expression.
1996 OwningExprResult ExprRes(Actions, Actions.ActOnNumericConstant(Tok));
1997 ConsumeToken();
1998
1999 MatchRHSPunctuation(tok::r_square, StartLoc);
2000
2001 // If there was an error parsing the assignment-expression, recover.
2002 if (ExprRes.isInvalid())
2003 ExprRes.release(); // Deallocate expr, just use [].
2004
2005 // Remember that we parsed a array type, and remember its features.
2006 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
2007 ExprRes.release(), StartLoc));
2008 return;
2009 }
2010
Chris Lattner4b009652007-07-25 00:24:17 +00002011 // If valid, this location is the position where we read the 'static' keyword.
2012 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002013 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002014 StaticLoc = ConsumeToken();
2015
2016 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002017 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002018 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002019 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002020
2021 // If we haven't already read 'static', check to see if there is one after the
2022 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002023 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002024 StaticLoc = ConsumeToken();
2025
2026 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2027 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002028 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002029
2030 // Handle the case where we have '[*]' as the array size. However, a leading
2031 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2032 // the the token after the star is a ']'. Since stars in arrays are
2033 // infrequent, use of lookahead is not costly here.
2034 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002035 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002036
Chris Lattner306d4df2008-12-18 06:50:14 +00002037 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002038 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002039 StaticLoc = SourceLocation(); // Drop the static.
2040 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002041 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002042 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002043 // Note, in C89, this production uses the constant-expr production instead
2044 // of assignment-expr. The only difference is that assignment-expr allows
2045 // things like '=' and '*='. Sema rejects these in C89 mode because they
2046 // are not i-c-e's, so we don't need to distinguish between the two here.
2047
Chris Lattner4b009652007-07-25 00:24:17 +00002048 // Parse the assignment-expression now.
2049 NumElements = ParseAssignmentExpression();
2050 }
2051
2052 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002053 if (NumElements.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002054 // If the expression was invalid, skip it.
2055 SkipUntil(tok::r_square);
2056 return;
2057 }
2058
2059 MatchRHSPunctuation(tok::r_square, StartLoc);
2060
Chris Lattner1525c3a2008-12-18 07:27:21 +00002061 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002062 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2063 StaticLoc.isValid(), isStar,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002064 NumElements.release(), StartLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00002065}
2066
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002067/// [GNU] typeof-specifier:
2068/// typeof ( expressions )
2069/// typeof ( type-name )
2070/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002071///
2072void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002073 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002074 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002075 SourceLocation StartLoc = ConsumeToken();
2076
Chris Lattner34a01ad2007-10-09 17:33:22 +00002077 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002078 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002079 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002080 return;
2081 }
2082
Sebastian Redl14ca7412008-12-11 21:36:32 +00002083 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002084 if (Result.isInvalid())
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002085 return;
2086
2087 const char *PrevSpec = 0;
2088 // Check for duplicate type specifiers.
2089 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002090 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002091 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002092
2093 // FIXME: Not accurate, the range gets one token more than it should.
2094 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002095 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002096 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002097
Steve Naroff7cbb1462007-07-31 12:34:36 +00002098 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2099
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002100 if (isTypeIdInParens()) {
Steve Naroff7cbb1462007-07-31 12:34:36 +00002101 TypeTy *Ty = ParseTypeName();
2102
Steve Naroff4c255ab2007-07-31 23:56:32 +00002103 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
2104
Chris Lattner34a01ad2007-10-09 17:33:22 +00002105 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002106 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002107 return;
2108 }
2109 RParenLoc = ConsumeParen();
2110 const char *PrevSpec = 0;
2111 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2112 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
Chris Lattnerf006a222008-11-18 07:48:38 +00002113 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002114 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002115 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002116
2117 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002118 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002119 return;
2120 }
2121 RParenLoc = ConsumeParen();
2122 const char *PrevSpec = 0;
2123 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2124 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002125 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002126 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002127 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002128 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002129}
2130
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002131