blob: e31b87bd4322bd3854d81d90cd0607ffe2eec29c [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Daniel Dunbare4858a62008-08-11 03:45:03 +000015#include "clang/Basic/Diagnostic.h"
Chris Lattner31e05722007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Sebastian Redla55e52c2008-11-25 22:21:31 +000018#include "AstGuard.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl4c5d3202008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Sebastian Redlcee63fb2008-12-02 14:43:59 +000031Parser::TypeTy *Parser::ParseTypeName() {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redlcee63fb2008-12-02 14:43:59 +000040 return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +000080 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Reid Spencer5f016e22007-07-11 17:01:13 +000081
82 AttributeList *CurrAttr = 0;
83
Chris Lattner04d66662007-10-09 17:33:22 +000084 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +000096 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
97 Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000098
Chris Lattner04d66662007-10-09 17:33:22 +000099 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000109 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 ConsumeParen(); // ignore the left paren loc for now
111
Chris Lattner04d66662007-10-09 17:33:22 +0000112 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
114 SourceLocation ParmLoc = ConsumeToken();
115
Chris Lattner04d66662007-10-09 17:33:22 +0000116 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000121 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 ConsumeToken();
123 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000124 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 bool ArgExprsOk = true;
126
127 // now parse the non-empty comma separated list of expressions
128 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000129 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000130 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 ArgExprsOk = false;
132 SkipUntil(tok::r_paren);
133 break;
134 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000135 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 }
Chris Lattner04d66662007-10-09 17:33:22 +0000137 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 break;
139 ConsumeToken(); // Eat the comma, move to the next argument
140 }
Chris Lattner04d66662007-10-09 17:33:22 +0000141 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 ConsumeParen(); // ignore the right paren loc for now
143 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000144 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 }
146 }
147 } else { // not an identifier
148 // parse a possibly empty comma separated list of expressions
Chris Lattner04d66662007-10-09 17:33:22 +0000149 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redla55e52c2008-11-25 22:21:31 +0000156 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 bool ArgExprsOk = true;
158
159 // now parse the list of expressions
160 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000161 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000162 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 ArgExprsOk = false;
164 SkipUntil(tok::r_paren);
165 break;
166 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000167 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 }
Chris Lattner04d66662007-10-09 17:33:22 +0000169 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 break;
171 ConsumeToken(); // Eat the comma, move to the next argument
172 }
173 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000174 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000176 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
177 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +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
195/// ParseDeclaration - Parse a full 'declaration', which consists of
196/// declaration-specifiers, some number of declarators, and a semicolon.
197/// 'Context' should be a Declarator::TheContext value.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000198///
199/// declaration: [C99 6.7]
200/// block-declaration ->
201/// simple-declaration
202/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000203/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000204/// [C++] namespace-definition
205/// others... [FIXME]
206///
Reid Spencer5f016e22007-07-11 17:01:13 +0000207Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattner8f08cb72007-08-25 06:57:03 +0000208 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000209 case tok::kw_export:
210 case tok::kw_template:
211 return ParseTemplateDeclaration(Context);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000212 case tok::kw_namespace:
213 return ParseNamespace(Context);
214 default:
215 return ParseSimpleDeclaration(Context);
216 }
217}
218
219/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
220/// declaration-specifiers init-declarator-list[opt] ';'
221///[C90/C++]init-declarator-list ';' [TODO]
222/// [OMP] threadprivate-directive [TODO]
223Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 // Parse the common declaration-specifiers piece.
225 DeclSpec DS;
226 ParseDeclarationSpecifiers(DS);
227
228 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
229 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000230 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 ConsumeToken();
232 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
233 }
234
235 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
236 ParseDeclarator(DeclaratorInfo);
237
238 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
239}
240
Chris Lattner8f08cb72007-08-25 06:57:03 +0000241
Reid Spencer5f016e22007-07-11 17:01:13 +0000242/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
243/// parsing 'declaration-specifiers declarator'. This method is split out this
244/// way to handle the ambiguity between top-level function-definitions and
245/// declarations.
246///
Reid Spencer5f016e22007-07-11 17:01:13 +0000247/// init-declarator-list: [C99 6.7]
248/// init-declarator
249/// init-declarator-list ',' init-declarator
250/// init-declarator: [C99 6.7]
251/// declarator
252/// declarator '=' initializer
253/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
254/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000255/// [C++] declarator initializer[opt]
256///
257/// [C++] initializer:
258/// [C++] '=' initializer-clause
259/// [C++] '(' expression-list ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000260///
261Parser::DeclTy *Parser::
262ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
263
264 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
265 // that they can be chained properly if the actions want this.
266 Parser::DeclTy *LastDeclInGroup = 0;
267
268 // At this point, we know that it is not a function definition. Parse the
269 // rest of the init-declarator-list.
270 while (1) {
271 // If a simple-asm-expr is present, parse it.
Daniel Dunbara80f8742008-08-05 01:35:17 +0000272 if (Tok.is(tok::kw_asm)) {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000273 OwningExprResult AsmLabel(ParseSimpleAsm());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000274 if (AsmLabel.isInvalid()) {
Daniel Dunbara80f8742008-08-05 01:35:17 +0000275 SkipUntil(tok::semi);
276 return 0;
277 }
Daniel Dunbar914701e2008-08-05 16:28:08 +0000278
Sebastian Redleffa8d12008-12-10 00:02:53 +0000279 D.setAsmLabel(AsmLabel.release());
Daniel Dunbara80f8742008-08-05 01:35:17 +0000280 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000281
282 // If attributes are present, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000283 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 D.AddAttributes(ParseAttributes());
Steve Naroffbb204692007-09-12 14:07:44 +0000285
286 // Inform the current actions module that we just parsed this declarator.
Daniel Dunbar914701e2008-08-05 16:28:08 +0000287 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000288
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 // Parse declarator '=' initializer.
Chris Lattner04d66662007-10-09 17:33:22 +0000290 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 ConsumeToken();
Sebastian Redl20df9b72008-12-11 22:51:44 +0000292 OwningExprResult Init(ParseInitializer());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000293 if (Init.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 SkipUntil(tok::semi);
295 return 0;
296 }
Sebastian Redl798d1192008-12-13 16:23:55 +0000297 Actions.AddInitializerToDecl(LastDeclInGroup, move_convert(Init));
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000298 } else if (Tok.is(tok::l_paren)) {
299 // Parse C++ direct initializer: '(' expression-list ')'
300 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redla55e52c2008-11-25 22:21:31 +0000301 ExprVector Exprs(Actions);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000302 CommaLocsTy CommaLocs;
303
304 bool InvalidExpr = false;
305 if (ParseExpressionList(Exprs, CommaLocs)) {
306 SkipUntil(tok::r_paren);
307 InvalidExpr = true;
308 }
309 // Match the ')'.
310 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
311
312 if (!InvalidExpr) {
313 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
314 "Unexpected number of commas!");
315 Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000316 Exprs.take(), Exprs.size(),
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000317 &CommaLocs[0], RParenLoc);
318 }
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000319 } else {
320 Actions.ActOnUninitializedDecl(LastDeclInGroup);
Reid Spencer5f016e22007-07-11 17:01:13 +0000321 }
322
Reid Spencer5f016e22007-07-11 17:01:13 +0000323 // If we don't have a comma, it is either the end of the list (a ';') or an
324 // error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000325 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000326 break;
327
328 // Consume the comma.
329 ConsumeToken();
330
331 // Parse the next declarator.
332 D.clear();
Chris Lattneraab740a2008-10-20 04:57:38 +0000333
334 // Accept attributes in an init-declarator. In the first declarator in a
335 // declaration, these would be part of the declspec. In subsequent
336 // declarators, they become part of the declarator itself, so that they
337 // don't apply to declarators after *this* one. Examples:
338 // short __attribute__((common)) var; -> declspec
339 // short var __attribute__((common)); -> declarator
340 // short x, __attribute__((common)) var; -> declarator
341 if (Tok.is(tok::kw___attribute))
342 D.AddAttributes(ParseAttributes());
343
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 ParseDeclarator(D);
345 }
346
Chris Lattner04d66662007-10-09 17:33:22 +0000347 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 ConsumeToken();
349 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
350 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000351 // If this is an ObjC2 for-each loop, this is a successful declarator
352 // parse. The syntax for these looks like:
353 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000354 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000355 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
356 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 Diag(Tok, diag::err_parse_error);
358 // Skip to end of block or statement
Chris Lattnered442382007-08-21 18:36:18 +0000359 SkipUntil(tok::r_brace, true, true);
Chris Lattner04d66662007-10-09 17:33:22 +0000360 if (Tok.is(tok::semi))
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 ConsumeToken();
362 return 0;
363}
364
365/// ParseSpecifierQualifierList
366/// specifier-qualifier-list:
367/// type-specifier specifier-qualifier-list[opt]
368/// type-qualifier specifier-qualifier-list[opt]
369/// [GNU] attributes specifier-qualifier-list[opt]
370///
371void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
372 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
373 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000374 ParseDeclarationSpecifiers(DS);
375
376 // Validate declspec for type-name.
377 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000378 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Reid Spencer5f016e22007-07-11 17:01:13 +0000379 Diag(Tok, diag::err_typename_requires_specqual);
380
381 // Issue diagnostic and remove storage class if present.
382 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
383 if (DS.getStorageClassSpecLoc().isValid())
384 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
385 else
386 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
387 DS.ClearStorageClassSpecs();
388 }
389
390 // Issue diagnostic and remove function specfier if present.
391 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000392 if (DS.isInlineSpecified())
393 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
394 if (DS.isVirtualSpecified())
395 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
396 if (DS.isExplicitSpecified())
397 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 DS.ClearFunctionSpecs();
399 }
400}
401
402/// ParseDeclarationSpecifiers
403/// declaration-specifiers: [C99 6.7]
404/// storage-class-specifier declaration-specifiers[opt]
405/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000406/// [C99] function-specifier declaration-specifiers[opt]
407/// [GNU] attributes declaration-specifiers[opt]
408///
409/// storage-class-specifier: [C99 6.7.1]
410/// 'typedef'
411/// 'extern'
412/// 'static'
413/// 'auto'
414/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000415/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000416/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000417/// function-specifier: [C99 6.7.4]
418/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000419/// [C++] 'virtual'
420/// [C++] 'explicit'
Reid Spencer5f016e22007-07-11 17:01:13 +0000421///
Douglas Gregoradcac882008-12-01 23:54:00 +0000422void Parser::ParseDeclarationSpecifiers(DeclSpec &DS)
423{
Chris Lattner81c018d2008-03-13 06:29:04 +0000424 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 while (1) {
426 int isInvalid = false;
427 const char *PrevSpec = 0;
428 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000429
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000430 // Only annotate C++ scope. Allow class-name as an identifier in case
431 // it's a constructor.
Daniel Dunbarab4c91c2008-11-25 23:05:24 +0000432 if (getLang().CPlusPlus)
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +0000433 TryAnnotateCXXScopeToken();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000434
Reid Spencer5f016e22007-07-11 17:01:13 +0000435 switch (Tok.getKind()) {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000436 default:
Douglas Gregoradcac882008-12-01 23:54:00 +0000437 // Try to parse a type-specifier; if we found one, continue. If it's not
438 // a type, this falls through.
439 if (MaybeParseTypeSpecifier(DS, isInvalid, PrevSpec)) {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000440 continue;
Douglas Gregoradcac882008-12-01 23:54:00 +0000441 }
Douglas Gregor12e083c2008-11-07 15:42:26 +0000442
Chris Lattnerbce61352008-07-26 00:20:22 +0000443 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000444 // If this is not a declaration specifier token, we're done reading decl
445 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000446 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +0000447 return;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000448
449 case tok::annot_cxxscope: {
450 if (DS.hasTypeSpecifier())
451 goto DoneWithDeclSpec;
452
453 // We are looking for a qualified typename.
454 if (NextToken().isNot(tok::identifier))
455 goto DoneWithDeclSpec;
456
457 CXXScopeSpec SS;
458 SS.setScopeRep(Tok.getAnnotationValue());
459 SS.setRange(Tok.getAnnotationRange());
460
461 // If the next token is the name of the class type that the C++ scope
462 // denotes, followed by a '(', then this is a constructor declaration.
463 // We're done with the decl-specifiers.
464 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
465 CurScope, &SS) &&
466 GetLookAheadToken(2).is(tok::l_paren))
467 goto DoneWithDeclSpec;
468
469 TypeTy *TypeRep = Actions.isTypeName(*NextToken().getIdentifierInfo(),
470 CurScope, &SS);
471 if (TypeRep == 0)
472 goto DoneWithDeclSpec;
473
474 ConsumeToken(); // The C++ scope.
475
476 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
477 TypeRep);
478 if (isInvalid)
479 break;
480
481 DS.SetRangeEnd(Tok.getLocation());
482 ConsumeToken(); // The typename.
483
484 continue;
485 }
486
Chris Lattner3bd934a2008-07-26 01:18:38 +0000487 // typedef-name
488 case tok::identifier: {
489 // This identifier can only be a typedef name if we haven't already seen
490 // a type-specifier. Without this check we misparse:
491 // typedef int X; struct Y { short X; }; as 'short int'.
492 if (DS.hasTypeSpecifier())
493 goto DoneWithDeclSpec;
494
495 // It has to be available as a typedef too!
Argyrios Kyrtzidis39caa082008-08-01 10:35:27 +0000496 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattner3bd934a2008-07-26 01:18:38 +0000497 if (TypeRep == 0)
498 goto DoneWithDeclSpec;
499
Douglas Gregorb48fe382008-10-31 09:07:45 +0000500 // C++: If the identifier is actually the name of the class type
501 // being defined and the next token is a '(', then this is a
502 // constructor declaration. We're done with the decl-specifiers
503 // and will treat this token as an identifier.
504 if (getLang().CPlusPlus &&
505 CurScope->isCXXClassScope() &&
506 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
507 NextToken().getKind() == tok::l_paren)
508 goto DoneWithDeclSpec;
509
Chris Lattner3bd934a2008-07-26 01:18:38 +0000510 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
511 TypeRep);
512 if (isInvalid)
513 break;
514
515 DS.SetRangeEnd(Tok.getLocation());
516 ConsumeToken(); // The identifier
517
518 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
519 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
520 // Objective-C interface. If we don't have Objective-C or a '<', this is
521 // just a normal reference to a typedef name.
522 if (!Tok.is(tok::less) || !getLang().ObjC1)
523 continue;
524
525 SourceLocation EndProtoLoc;
Chris Lattnerae4da612008-07-26 01:53:50 +0000526 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000527 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000528 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000529
530 DS.SetRangeEnd(EndProtoLoc);
531
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000532 // Need to support trailing type qualifiers (e.g. "id<p> const").
533 // If a type specifier follows, it will be diagnosed elsewhere.
534 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000535 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000536 // GNU attributes support.
537 case tok::kw___attribute:
538 DS.AddAttributes(ParseAttributes());
539 continue;
540
541 // storage-class-specifier
542 case tok::kw_typedef:
543 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
544 break;
545 case tok::kw_extern:
546 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000547 Diag(Tok, diag::ext_thread_before) << "extern";
Reid Spencer5f016e22007-07-11 17:01:13 +0000548 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
549 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000550 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000551 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
552 PrevSpec);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000553 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 case tok::kw_static:
555 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000556 Diag(Tok, diag::ext_thread_before) << "static";
Reid Spencer5f016e22007-07-11 17:01:13 +0000557 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
558 break;
559 case tok::kw_auto:
560 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
561 break;
562 case tok::kw_register:
563 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
564 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000565 case tok::kw_mutable:
566 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
567 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000568 case tok::kw___thread:
569 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
570 break;
571
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 continue;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000573
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 // function-specifier
575 case tok::kw_inline:
576 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
577 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000578
579 case tok::kw_virtual:
580 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
581 break;
582
583 case tok::kw_explicit:
584 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
585 break;
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000586
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000587 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +0000588 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +0000589 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
590 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +0000591 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +0000592 goto DoneWithDeclSpec;
593
594 {
595 SourceLocation EndProtoLoc;
Chris Lattnerae4da612008-07-26 01:53:50 +0000596 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000597 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000598 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000599 DS.SetRangeEnd(EndProtoLoc);
600
Chris Lattner1ab3b962008-11-18 07:48:38 +0000601 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
602 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000603 // Need to support trailing type qualifiers (e.g. "id<p> const").
604 // If a type specifier follows, it will be diagnosed elsewhere.
605 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000606 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 }
608 // If the specifier combination wasn't legal, issue a diagnostic.
609 if (isInvalid) {
610 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +0000611 // Pick between error or extwarn.
612 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
613 : diag::ext_duplicate_declspec;
614 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 }
Chris Lattner81c018d2008-03-13 06:29:04 +0000616 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 ConsumeToken();
618 }
619}
Douglas Gregoradcac882008-12-01 23:54:00 +0000620
Douglas Gregor12e083c2008-11-07 15:42:26 +0000621/// MaybeParseTypeSpecifier - Try to parse a single type-specifier. We
622/// primarily follow the C++ grammar with additions for C99 and GNU,
623/// which together subsume the C grammar. Note that the C++
624/// type-specifier also includes the C type-qualifier (for const,
625/// volatile, and C99 restrict). Returns true if a type-specifier was
626/// found (and parsed), false otherwise.
627///
628/// type-specifier: [C++ 7.1.5]
629/// simple-type-specifier
630/// class-specifier
631/// enum-specifier
632/// elaborated-type-specifier [TODO]
633/// cv-qualifier
634///
635/// cv-qualifier: [C++ 7.1.5.1]
636/// 'const'
637/// 'volatile'
638/// [C99] 'restrict'
639///
640/// simple-type-specifier: [ C++ 7.1.5.2]
641/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
642/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
643/// 'char'
644/// 'wchar_t'
645/// 'bool'
646/// 'short'
647/// 'int'
648/// 'long'
649/// 'signed'
650/// 'unsigned'
651/// 'float'
652/// 'double'
653/// 'void'
654/// [C99] '_Bool'
655/// [C99] '_Complex'
656/// [C99] '_Imaginary' // Removed in TC2?
657/// [GNU] '_Decimal32'
658/// [GNU] '_Decimal64'
659/// [GNU] '_Decimal128'
660/// [GNU] typeof-specifier
661/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
662/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
663bool Parser::MaybeParseTypeSpecifier(DeclSpec &DS, int& isInvalid,
664 const char *&PrevSpec) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000665 // Annotate typenames and C++ scope specifiers.
666 TryAnnotateTypeOrScopeToken();
667
Douglas Gregor12e083c2008-11-07 15:42:26 +0000668 SourceLocation Loc = Tok.getLocation();
669
670 switch (Tok.getKind()) {
671 // simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000672 case tok::annot_qualtypename: {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000673 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000674 Tok.getAnnotationValue());
675 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
676 ConsumeToken(); // The typename
Douglas Gregor12e083c2008-11-07 15:42:26 +0000677
678 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
679 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
680 // Objective-C interface. If we don't have Objective-C or a '<', this is
681 // just a normal reference to a typedef name.
682 if (!Tok.is(tok::less) || !getLang().ObjC1)
683 return true;
684
685 SourceLocation EndProtoLoc;
686 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
687 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
688 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
689
690 DS.SetRangeEnd(EndProtoLoc);
691 return true;
692 }
693
694 case tok::kw_short:
695 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
696 break;
697 case tok::kw_long:
698 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
699 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
700 else
701 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
702 break;
703 case tok::kw_signed:
704 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
705 break;
706 case tok::kw_unsigned:
707 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
708 break;
709 case tok::kw__Complex:
710 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
711 break;
712 case tok::kw__Imaginary:
713 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
714 break;
715 case tok::kw_void:
716 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
717 break;
718 case tok::kw_char:
719 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
720 break;
721 case tok::kw_int:
722 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
723 break;
724 case tok::kw_float:
725 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
726 break;
727 case tok::kw_double:
728 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
729 break;
730 case tok::kw_wchar_t:
731 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
732 break;
733 case tok::kw_bool:
734 case tok::kw__Bool:
735 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
736 break;
737 case tok::kw__Decimal32:
738 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
739 break;
740 case tok::kw__Decimal64:
741 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
742 break;
743 case tok::kw__Decimal128:
744 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
745 break;
746
747 // class-specifier:
748 case tok::kw_class:
749 case tok::kw_struct:
750 case tok::kw_union:
751 ParseClassSpecifier(DS);
752 return true;
753
754 // enum-specifier:
755 case tok::kw_enum:
756 ParseEnumSpecifier(DS);
757 return true;
758
759 // cv-qualifier:
760 case tok::kw_const:
761 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
762 getLang())*2;
763 break;
764 case tok::kw_volatile:
765 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
766 getLang())*2;
767 break;
768 case tok::kw_restrict:
769 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
770 getLang())*2;
771 break;
772
773 // GNU typeof support.
774 case tok::kw_typeof:
775 ParseTypeofSpecifier(DS);
776 return true;
777
778 default:
779 // Not a type-specifier; do nothing.
780 return false;
781 }
782
783 // If the specifier combination wasn't legal, issue a diagnostic.
784 if (isInvalid) {
785 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +0000786 // Pick between error or extwarn.
787 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
788 : diag::ext_duplicate_declspec;
789 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000790 }
791 DS.SetRangeEnd(Tok.getLocation());
792 ConsumeToken(); // whatever we parsed above.
793 return true;
794}
Reid Spencer5f016e22007-07-11 17:01:13 +0000795
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000796/// ParseStructDeclaration - Parse a struct declaration without the terminating
797/// semicolon.
798///
Reid Spencer5f016e22007-07-11 17:01:13 +0000799/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000800/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000801/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000802/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000803/// struct-declarator-list:
804/// struct-declarator
805/// struct-declarator-list ',' struct-declarator
806/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
807/// struct-declarator:
808/// declarator
809/// [GNU] declarator attributes[opt]
810/// declarator[opt] ':' constant-expression
811/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
812///
Chris Lattnere1359422008-04-10 06:46:29 +0000813void Parser::
814ParseStructDeclaration(DeclSpec &DS,
815 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000816 if (Tok.is(tok::kw___extension__)) {
817 // __extension__ silences extension warnings in the subexpression.
818 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +0000819 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000820 return ParseStructDeclaration(DS, Fields);
821 }
Steve Naroff28a7ca82007-08-20 22:28:22 +0000822
823 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000824 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +0000825 ParseSpecifierQualifierList(DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000826
827 // If there are no declarators, issue a warning.
Chris Lattner04d66662007-10-09 17:33:22 +0000828 if (Tok.is(tok::semi)) {
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000829 Diag(DSStart, diag::w_no_declarators);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000830 return;
831 }
832
833 // Read struct-declarators until we find the semicolon.
Chris Lattnerebe457c2008-04-10 16:37:40 +0000834 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +0000835 while (1) {
Chris Lattnere1359422008-04-10 06:46:29 +0000836 FieldDeclarator &DeclaratorInfo = Fields.back();
837
Steve Naroff28a7ca82007-08-20 22:28:22 +0000838 /// struct-declarator: declarator
839 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +0000840 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +0000841 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000842
Chris Lattner04d66662007-10-09 17:33:22 +0000843 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +0000844 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000845 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000846 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +0000847 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000848 else
Sebastian Redleffa8d12008-12-10 00:02:53 +0000849 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +0000850 }
851
852 // If attributes exist after the declarator, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000853 if (Tok.is(tok::kw___attribute))
Chris Lattnere1359422008-04-10 06:46:29 +0000854 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroff28a7ca82007-08-20 22:28:22 +0000855
856 // If we don't have a comma, it is either the end of the list (a ';')
857 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000858 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000859 return;
Steve Naroff28a7ca82007-08-20 22:28:22 +0000860
861 // Consume the comma.
862 ConsumeToken();
863
864 // Parse the next declarator.
Chris Lattnerebe457c2008-04-10 16:37:40 +0000865 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +0000866
867 // Attributes are only allowed on the second declarator.
Chris Lattner04d66662007-10-09 17:33:22 +0000868 if (Tok.is(tok::kw___attribute))
Chris Lattnere1359422008-04-10 06:46:29 +0000869 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroff28a7ca82007-08-20 22:28:22 +0000870 }
Steve Naroff28a7ca82007-08-20 22:28:22 +0000871}
872
873/// ParseStructUnionBody
874/// struct-contents:
875/// struct-declaration-list
876/// [EXT] empty
877/// [GNU] "struct-declaration-list" without terminatoring ';'
878/// struct-declaration-list:
879/// struct-declaration
880/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +0000881/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +0000882///
Reid Spencer5f016e22007-07-11 17:01:13 +0000883void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
884 unsigned TagType, DeclTy *TagDecl) {
885 SourceLocation LBraceLoc = ConsumeBrace();
886
887 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
888 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000889 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +0000890 Diag(Tok, diag::ext_empty_struct_union_enum)
891 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000892
893 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000894 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
895
Reid Spencer5f016e22007-07-11 17:01:13 +0000896 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +0000897 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 // Each iteration of this loop reads one struct-declaration.
899
900 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +0000901 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000902 Diag(Tok, diag::ext_extra_struct_semi);
903 ConsumeToken();
904 continue;
905 }
Chris Lattnere1359422008-04-10 06:46:29 +0000906
907 // Parse all the comma separated declarators.
908 DeclSpec DS;
909 FieldDeclarators.clear();
Chris Lattner5a6ddbf2008-06-21 19:39:06 +0000910 if (!Tok.is(tok::at)) {
911 ParseStructDeclaration(DS, FieldDeclarators);
912
913 // Convert them all to fields.
914 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
915 FieldDeclarator &FD = FieldDeclarators[i];
916 // Install the declarator into the current TagDecl.
Douglas Gregor44b43212008-12-11 16:49:14 +0000917 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
Chris Lattner5a6ddbf2008-06-21 19:39:06 +0000918 DS.getSourceRange().getBegin(),
919 FD.D, FD.BitfieldSize);
920 FieldDecls.push_back(Field);
921 }
922 } else { // Handle @defs
923 ConsumeToken();
924 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
925 Diag(Tok, diag::err_unexpected_at);
926 SkipUntil(tok::semi, true, true);
927 continue;
928 }
929 ConsumeToken();
930 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
931 if (!Tok.is(tok::identifier)) {
932 Diag(Tok, diag::err_expected_ident);
933 SkipUntil(tok::semi, true, true);
934 continue;
935 }
936 llvm::SmallVector<DeclTy*, 16> Fields;
Douglas Gregor44b43212008-12-11 16:49:14 +0000937 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
938 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +0000939 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
940 ConsumeToken();
941 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
942 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000943
Chris Lattner04d66662007-10-09 17:33:22 +0000944 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000945 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +0000946 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000947 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +0000948 break;
949 } else {
950 Diag(Tok, diag::err_expected_semi_decl_list);
951 // Skip to end of block or statement
952 SkipUntil(tok::r_brace, true, true);
953 }
954 }
955
Steve Naroff60fccee2007-10-29 21:38:07 +0000956 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000957
Reid Spencer5f016e22007-07-11 17:01:13 +0000958 AttributeList *AttrList = 0;
959 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000960 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +0000961 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +0000962
963 Actions.ActOnFields(CurScope,
964 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
965 LBraceLoc, RBraceLoc,
966 AttrList);
Reid Spencer5f016e22007-07-11 17:01:13 +0000967}
968
969
970/// ParseEnumSpecifier
971/// enum-specifier: [C99 6.7.2.2]
972/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000973///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +0000974/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
975/// '}' attributes[opt]
976/// 'enum' identifier
977/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000978///
979/// [C++] elaborated-type-specifier:
980/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
981///
Reid Spencer5f016e22007-07-11 17:01:13 +0000982void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +0000983 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Reid Spencer5f016e22007-07-11 17:01:13 +0000984 SourceLocation StartLoc = ConsumeToken();
985
986 // Parse the tag portion of this.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +0000987
988 AttributeList *Attr = 0;
989 // If attributes exist after tag, parse them.
990 if (Tok.is(tok::kw___attribute))
991 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000992
993 CXXScopeSpec SS;
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +0000994 if (getLang().CPlusPlus && MaybeParseCXXScopeSpecifier(SS)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000995 if (Tok.isNot(tok::identifier)) {
996 Diag(Tok, diag::err_expected_ident);
997 if (Tok.isNot(tok::l_brace)) {
998 // Has no name and is not a definition.
999 // Skip the rest of this declarator, up until the comma or semicolon.
1000 SkipUntil(tok::comma, true);
1001 return;
1002 }
1003 }
1004 }
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001005
1006 // Must have either 'enum name' or 'enum {...}'.
1007 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1008 Diag(Tok, diag::err_expected_ident_lbrace);
1009
1010 // Skip the rest of this declarator, up until the comma or semicolon.
1011 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001012 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001013 }
1014
1015 // If an identifier is present, consume and remember it.
1016 IdentifierInfo *Name = 0;
1017 SourceLocation NameLoc;
1018 if (Tok.is(tok::identifier)) {
1019 Name = Tok.getIdentifierInfo();
1020 NameLoc = ConsumeToken();
1021 }
1022
1023 // There are three options here. If we have 'enum foo;', then this is a
1024 // forward declaration. If we have 'enum foo {...' then this is a
1025 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1026 //
1027 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1028 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1029 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1030 //
1031 Action::TagKind TK;
1032 if (Tok.is(tok::l_brace))
1033 TK = Action::TK_Definition;
1034 else if (Tok.is(tok::semi))
1035 TK = Action::TK_Declaration;
1036 else
1037 TK = Action::TK_Reference;
1038 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001039 SS, Name, NameLoc, Attr);
Reid Spencer5f016e22007-07-11 17:01:13 +00001040
Chris Lattner04d66662007-10-09 17:33:22 +00001041 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001042 ParseEnumBody(StartLoc, TagDecl);
1043
1044 // TODO: semantic analysis on the declspec for enums.
1045 const char *PrevSpec = 0;
1046 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001047 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001048}
1049
1050/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1051/// enumerator-list:
1052/// enumerator
1053/// enumerator-list ',' enumerator
1054/// enumerator:
1055/// enumeration-constant
1056/// enumeration-constant '=' constant-expression
1057/// enumeration-constant:
1058/// identifier
1059///
1060void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
1061 SourceLocation LBraceLoc = ConsumeBrace();
1062
Chris Lattner7946dd32007-08-27 17:24:30 +00001063 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001064 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001065 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Reid Spencer5f016e22007-07-11 17:01:13 +00001066
1067 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1068
1069 DeclTy *LastEnumConstDecl = 0;
1070
1071 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001072 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001073 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1074 SourceLocation IdentLoc = ConsumeToken();
1075
1076 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001077 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001078 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001079 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001080 AssignedVal = ParseConstantExpression();
1081 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001082 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 }
1084
1085 // Install the enumerator constant into EnumDecl.
Steve Naroff08d92e42007-09-15 18:49:24 +00001086 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +00001087 LastEnumConstDecl,
1088 IdentLoc, Ident,
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001089 EqualLoc,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001090 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001091 EnumConstantDecls.push_back(EnumConstDecl);
1092 LastEnumConstDecl = EnumConstDecl;
1093
Chris Lattner04d66662007-10-09 17:33:22 +00001094 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001095 break;
1096 SourceLocation CommaLoc = ConsumeToken();
1097
Chris Lattner04d66662007-10-09 17:33:22 +00001098 if (Tok.isNot(tok::identifier) && !getLang().C99)
Reid Spencer5f016e22007-07-11 17:01:13 +00001099 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1100 }
1101
1102 // Eat the }.
1103 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1104
Steve Naroff08d92e42007-09-15 18:49:24 +00001105 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Reid Spencer5f016e22007-07-11 17:01:13 +00001106 EnumConstantDecls.size());
1107
1108 DeclTy *AttrList = 0;
1109 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001110 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001111 AttrList = ParseAttributes(); // FIXME: where do they do?
1112}
1113
1114/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001115/// start of a type-qualifier-list.
1116bool Parser::isTypeQualifier() const {
1117 switch (Tok.getKind()) {
1118 default: return false;
1119 // type-qualifier
1120 case tok::kw_const:
1121 case tok::kw_volatile:
1122 case tok::kw_restrict:
1123 return true;
1124 }
1125}
1126
1127/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001128/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001129bool Parser::isTypeSpecifierQualifier() {
1130 // Annotate typenames and C++ scope specifiers.
1131 TryAnnotateTypeOrScopeToken();
1132
Reid Spencer5f016e22007-07-11 17:01:13 +00001133 switch (Tok.getKind()) {
1134 default: return false;
1135 // GNU attributes support.
1136 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001137 // GNU typeof support.
1138 case tok::kw_typeof:
1139
Reid Spencer5f016e22007-07-11 17:01:13 +00001140 // type-specifiers
1141 case tok::kw_short:
1142 case tok::kw_long:
1143 case tok::kw_signed:
1144 case tok::kw_unsigned:
1145 case tok::kw__Complex:
1146 case tok::kw__Imaginary:
1147 case tok::kw_void:
1148 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001149 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001150 case tok::kw_int:
1151 case tok::kw_float:
1152 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001153 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001154 case tok::kw__Bool:
1155 case tok::kw__Decimal32:
1156 case tok::kw__Decimal64:
1157 case tok::kw__Decimal128:
1158
Chris Lattner99dc9142008-04-13 18:59:07 +00001159 // struct-or-union-specifier (C99) or class-specifier (C++)
1160 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001161 case tok::kw_struct:
1162 case tok::kw_union:
1163 // enum-specifier
1164 case tok::kw_enum:
1165
1166 // type-qualifier
1167 case tok::kw_const:
1168 case tok::kw_volatile:
1169 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001170
1171 // typedef-name
1172 case tok::annot_qualtypename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001173 return true;
Chris Lattner7c186be2008-10-20 00:25:30 +00001174
1175 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1176 case tok::less:
1177 return getLang().ObjC1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001178 }
1179}
1180
1181/// isDeclarationSpecifier() - Return true if the current token is part of a
1182/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001183bool Parser::isDeclarationSpecifier() {
1184 // Annotate typenames and C++ scope specifiers.
1185 TryAnnotateTypeOrScopeToken();
1186
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 switch (Tok.getKind()) {
1188 default: return false;
1189 // storage-class-specifier
1190 case tok::kw_typedef:
1191 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001192 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001193 case tok::kw_static:
1194 case tok::kw_auto:
1195 case tok::kw_register:
1196 case tok::kw___thread:
1197
1198 // type-specifiers
1199 case tok::kw_short:
1200 case tok::kw_long:
1201 case tok::kw_signed:
1202 case tok::kw_unsigned:
1203 case tok::kw__Complex:
1204 case tok::kw__Imaginary:
1205 case tok::kw_void:
1206 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001207 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001208 case tok::kw_int:
1209 case tok::kw_float:
1210 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001211 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001212 case tok::kw__Bool:
1213 case tok::kw__Decimal32:
1214 case tok::kw__Decimal64:
1215 case tok::kw__Decimal128:
1216
Chris Lattner99dc9142008-04-13 18:59:07 +00001217 // struct-or-union-specifier (C99) or class-specifier (C++)
1218 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 case tok::kw_struct:
1220 case tok::kw_union:
1221 // enum-specifier
1222 case tok::kw_enum:
1223
1224 // type-qualifier
1225 case tok::kw_const:
1226 case tok::kw_volatile:
1227 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001228
Reid Spencer5f016e22007-07-11 17:01:13 +00001229 // function-specifier
1230 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00001231 case tok::kw_virtual:
1232 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001233
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001234 // typedef-name
1235 case tok::annot_qualtypename:
1236
Chris Lattner1ef08762007-08-09 17:01:07 +00001237 // GNU typeof support.
1238 case tok::kw_typeof:
1239
1240 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001241 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 return true;
Chris Lattnerf3948c42008-07-26 03:38:44 +00001243
1244 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1245 case tok::less:
1246 return getLang().ObjC1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 }
1248}
1249
1250
1251/// ParseTypeQualifierListOpt
1252/// type-qualifier-list: [C99 6.7.5]
1253/// type-qualifier
1254/// [GNU] attributes
1255/// type-qualifier-list type-qualifier
1256/// [GNU] type-qualifier-list attributes
1257///
1258void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1259 while (1) {
1260 int isInvalid = false;
1261 const char *PrevSpec = 0;
1262 SourceLocation Loc = Tok.getLocation();
1263
1264 switch (Tok.getKind()) {
1265 default:
1266 // If this is not a type-qualifier token, we're done reading type
1267 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +00001268 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001269 return;
1270 case tok::kw_const:
1271 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1272 getLang())*2;
1273 break;
1274 case tok::kw_volatile:
1275 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1276 getLang())*2;
1277 break;
1278 case tok::kw_restrict:
1279 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1280 getLang())*2;
1281 break;
1282 case tok::kw___attribute:
1283 DS.AddAttributes(ParseAttributes());
1284 continue; // do *not* consume the next token!
1285 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00001286
Reid Spencer5f016e22007-07-11 17:01:13 +00001287 // If the specifier combination wasn't legal, issue a diagnostic.
1288 if (isInvalid) {
1289 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001290 // Pick between error or extwarn.
1291 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1292 : diag::ext_duplicate_declspec;
1293 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001294 }
1295 ConsumeToken();
1296 }
1297}
1298
1299
1300/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1301///
1302void Parser::ParseDeclarator(Declarator &D) {
1303 /// This implements the 'declarator' production in the C grammar, then checks
1304 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001305 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00001306}
1307
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001308/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1309/// is parsed by the function passed to it. Pass null, and the direct-declarator
1310/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001311/// ptr-operator production.
1312///
Reid Spencer5f016e22007-07-11 17:01:13 +00001313/// declarator: [C99 6.7.5]
1314/// pointer[opt] direct-declarator
1315/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1316/// [GNU] '&' restrict[opt] attributes[opt] declarator
1317///
1318/// pointer: [C99 6.7.5]
1319/// '*' type-qualifier-list[opt]
1320/// '*' type-qualifier-list[opt] pointer
1321///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001322/// ptr-operator:
1323/// '*' cv-qualifier-seq[opt]
1324/// '&'
1325/// [GNU] '&' restrict[opt] attributes[opt]
1326/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] [TODO]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001327void Parser::ParseDeclaratorInternal(Declarator &D,
1328 DirectDeclParseFunction DirectDeclParser) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001329 tok::TokenKind Kind = Tok.getKind();
1330
Steve Naroff5618bd42008-08-27 16:04:49 +00001331 // Not a pointer, C++ reference, or block.
1332 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001333 (Kind != tok::caret || !getLang().Blocks)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001334 if (DirectDeclParser)
1335 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001336 return;
1337 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001338
Steve Naroff4ef1c992008-08-28 10:07:06 +00001339 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Reid Spencer5f016e22007-07-11 17:01:13 +00001340 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1341
Steve Naroff4ef1c992008-08-28 10:07:06 +00001342 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner76549142008-02-21 01:32:26 +00001343 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00001344 DeclSpec DS;
1345
1346 ParseTypeQualifierListOpt(DS);
1347
1348 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001349 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00001350 if (Kind == tok::star)
1351 // Remember that we parsed a pointer type, and remember the type-quals.
1352 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1353 DS.TakeAttributes()));
1354 else
1355 // Remember that we parsed a Block type, and remember the type-quals.
1356 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1357 Loc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001358 } else {
1359 // Is a reference
1360 DeclSpec DS;
1361
1362 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1363 // cv-qualifiers are introduced through the use of a typedef or of a
1364 // template type argument, in which case the cv-qualifiers are ignored.
1365 //
1366 // [GNU] Retricted references are allowed.
1367 // [GNU] Attributes on references are allowed.
1368 ParseTypeQualifierListOpt(DS);
1369
1370 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1371 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1372 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001373 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001374 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1375 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001376 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00001377 }
1378
1379 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001380 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00001381
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001382 if (D.getNumTypeObjects() > 0) {
1383 // C++ [dcl.ref]p4: There shall be no references to references.
1384 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1385 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00001386 if (const IdentifierInfo *II = D.getIdentifier())
1387 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1388 << II;
1389 else
1390 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1391 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001392
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001393 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001394 // can go ahead and build the (technically ill-formed)
1395 // declarator: reference collapsing will take care of it.
1396 }
1397 }
1398
Reid Spencer5f016e22007-07-11 17:01:13 +00001399 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001400 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1401 DS.TakeAttributes()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001402 }
1403}
1404
1405/// ParseDirectDeclarator
1406/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00001407/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00001408/// '(' declarator ')'
1409/// [GNU] '(' attributes declarator ')'
1410/// [C90] direct-declarator '[' constant-expression[opt] ']'
1411/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1412/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1413/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1414/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1415/// direct-declarator '(' parameter-type-list ')'
1416/// direct-declarator '(' identifier-list[opt] ')'
1417/// [GNU] direct-declarator '(' parameter-forward-declarations
1418/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001419/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1420/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00001421/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001422///
1423/// declarator-id: [C++ 8]
1424/// id-expression
1425/// '::'[opt] nested-name-specifier[opt] type-name
1426///
1427/// id-expression: [C++ 5.1]
1428/// unqualified-id
1429/// qualified-id [TODO]
1430///
1431/// unqualified-id: [C++ 5.1]
1432/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001433/// operator-function-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001434/// conversion-function-id [TODO]
1435/// '~' class-name
1436/// template-id [TODO]
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001437///
Reid Spencer5f016e22007-07-11 17:01:13 +00001438void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001439 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001440
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001441 if (getLang().CPlusPlus) {
1442 if (D.mayHaveIdentifier()) {
1443 bool afterCXXScope = MaybeParseCXXScopeSpecifier(D.getCXXScopeSpec());
1444 if (afterCXXScope) {
1445 // Change the declaration context for name lookup, until this function
1446 // is exited (and the declarator has been parsed).
1447 DeclScopeObj.EnterDeclaratorScope();
1448 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001449
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001450 if (Tok.is(tok::identifier)) {
1451 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1452 // Determine whether this identifier is a C++ constructor name or
1453 // a normal identifier.
1454 if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope)) {
1455 D.setConstructor(Actions.isTypeName(*Tok.getIdentifierInfo(),
1456 CurScope),
1457 Tok.getLocation());
1458 } else
1459 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1460 ConsumeToken();
1461 goto PastIdentifier;
1462 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001463
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001464 if (Tok.is(tok::tilde)) {
1465 // This should be a C++ destructor.
1466 SourceLocation TildeLoc = ConsumeToken();
1467 if (Tok.is(tok::identifier)) {
1468 if (TypeTy *Type = ParseClassName())
1469 D.setDestructor(Type, TildeLoc);
1470 else
1471 D.SetIdentifier(0, TildeLoc);
1472 } else {
1473 Diag(Tok, diag::err_expected_class_name);
1474 D.SetIdentifier(0, TildeLoc);
1475 }
1476 goto PastIdentifier;
1477 }
1478
1479 // If we reached this point, token is not identifier and not '~'.
1480
1481 if (afterCXXScope) {
1482 Diag(Tok, diag::err_expected_unqualified_id);
1483 D.SetIdentifier(0, Tok.getLocation());
1484 D.setInvalidType(true);
1485 goto PastIdentifier;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001486 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001487 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001488
1489 if (Tok.is(tok::kw_operator)) {
1490 SourceLocation OperatorLoc = Tok.getLocation();
1491
1492 // First try the name of an overloaded operator
1493 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId()) {
1494 D.setOverloadedOperator(Op, OperatorLoc);
1495 } else {
1496 // This must be a conversion function (C++ [class.conv.fct]).
1497 if (TypeTy *ConvType = ParseConversionFunctionId())
1498 D.setConversionFunction(ConvType, OperatorLoc);
1499 else
1500 D.SetIdentifier(0, Tok.getLocation());
1501 }
1502 goto PastIdentifier;
1503 }
1504 }
1505
1506 // If we reached this point, we are either in C/ObjC or the token didn't
1507 // satisfy any of the C++-specific checks.
1508
1509 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1510 assert(!getLang().CPlusPlus &&
1511 "There's a C++-specific check for tok::identifier above");
1512 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1513 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1514 ConsumeToken();
1515 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001516 // direct-declarator: '(' declarator ')'
1517 // direct-declarator: '(' attributes declarator ')'
1518 // Example: 'char (*X)' or 'int (*XX)(void)'
1519 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001520 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001521 // This could be something simple like "int" (in which case the declarator
1522 // portion is empty), if an abstract-declarator is allowed.
1523 D.SetIdentifier(0, Tok.getLocation());
1524 } else {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001525 if (getLang().CPlusPlus)
1526 Diag(Tok, diag::err_expected_unqualified_id);
1527 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00001528 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00001529 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00001530 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001531 }
1532
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001533 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00001534 assert(D.isPastIdentifier() &&
1535 "Haven't past the location of the identifier yet?");
1536
1537 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00001538 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001539 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1540 // In such a case, check if we actually have a function declarator; if it
1541 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00001542 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1543 // When not in file scope, warn for ambiguous function declarators, just
1544 // in case the author intended it as a variable definition.
1545 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1546 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1547 break;
1548 }
Chris Lattneref4715c2008-04-06 05:45:57 +00001549 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00001550 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001551 ParseBracketDeclarator(D);
1552 } else {
1553 break;
1554 }
1555 }
1556}
1557
Chris Lattneref4715c2008-04-06 05:45:57 +00001558/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1559/// only called before the identifier, so these are most likely just grouping
1560/// parens for precedence. If we find that these are actually function
1561/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1562///
1563/// direct-declarator:
1564/// '(' declarator ')'
1565/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00001566/// direct-declarator '(' parameter-type-list ')'
1567/// direct-declarator '(' identifier-list[opt] ')'
1568/// [GNU] direct-declarator '(' parameter-forward-declarations
1569/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00001570///
1571void Parser::ParseParenDeclarator(Declarator &D) {
1572 SourceLocation StartLoc = ConsumeParen();
1573 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1574
Chris Lattner7399ee02008-10-20 02:05:46 +00001575 // Eat any attributes before we look at whether this is a grouping or function
1576 // declarator paren. If this is a grouping paren, the attribute applies to
1577 // the type being built up, for example:
1578 // int (__attribute__(()) *x)(long y)
1579 // If this ends up not being a grouping paren, the attribute applies to the
1580 // first argument, for example:
1581 // int (__attribute__(()) int x)
1582 // In either case, we need to eat any attributes to be able to determine what
1583 // sort of paren this is.
1584 //
1585 AttributeList *AttrList = 0;
1586 bool RequiresArg = false;
1587 if (Tok.is(tok::kw___attribute)) {
1588 AttrList = ParseAttributes();
1589
1590 // We require that the argument list (if this is a non-grouping paren) be
1591 // present even if the attribute list was empty.
1592 RequiresArg = true;
1593 }
1594
Chris Lattneref4715c2008-04-06 05:45:57 +00001595 // If we haven't past the identifier yet (or where the identifier would be
1596 // stored, if this is an abstract declarator), then this is probably just
1597 // grouping parens. However, if this could be an abstract-declarator, then
1598 // this could also be the start of function arguments (consider 'void()').
1599 bool isGrouping;
1600
1601 if (!D.mayOmitIdentifier()) {
1602 // If this can't be an abstract-declarator, this *must* be a grouping
1603 // paren, because we haven't seen the identifier yet.
1604 isGrouping = true;
1605 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00001606 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00001607 isDeclarationSpecifier()) { // 'int(int)' is a function.
1608 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1609 // considered to be a type, not a K&R identifier-list.
1610 isGrouping = false;
1611 } else {
1612 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1613 isGrouping = true;
1614 }
1615
1616 // If this is a grouping paren, handle:
1617 // direct-declarator: '(' declarator ')'
1618 // direct-declarator: '(' attributes declarator ')'
1619 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00001620 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001621 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00001622 if (AttrList)
1623 D.AddAttributes(AttrList);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001624
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001625 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00001626 // Match the ')'.
1627 MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00001628
1629 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00001630 return;
1631 }
1632
1633 // Okay, if this wasn't a grouping paren, it must be the start of a function
1634 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00001635 // identifier (and remember where it would have been), then call into
1636 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00001637 D.SetIdentifier(0, Tok.getLocation());
1638
Chris Lattner7399ee02008-10-20 02:05:46 +00001639 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00001640}
1641
1642/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1643/// declarator D up to a paren, which indicates that we are parsing function
1644/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00001645///
Chris Lattner7399ee02008-10-20 02:05:46 +00001646/// If AttrList is non-null, then the caller parsed those arguments immediately
1647/// after the open paren - they should be considered to be the first argument of
1648/// a parameter. If RequiresArg is true, then the first argument of the
1649/// function is required to be present and required to not be an identifier
1650/// list.
1651///
Reid Spencer5f016e22007-07-11 17:01:13 +00001652/// This method also handles this portion of the grammar:
1653/// parameter-type-list: [C99 6.7.5]
1654/// parameter-list
1655/// parameter-list ',' '...'
1656///
1657/// parameter-list: [C99 6.7.5]
1658/// parameter-declaration
1659/// parameter-list ',' parameter-declaration
1660///
1661/// parameter-declaration: [C99 6.7.5]
1662/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00001663/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001664/// [GNU] declaration-specifiers declarator attributes
1665/// declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00001666/// [C++] declaration-specifiers abstract-declarator[opt]
1667/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001668/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1669///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001670/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
1671/// and "exception-specification[opt]"(TODO).
1672///
Chris Lattner7399ee02008-10-20 02:05:46 +00001673void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
1674 AttributeList *AttrList,
1675 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00001676 // lparen is already consumed!
1677 assert(D.isPastIdentifier() && "Should not call before identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001678
Chris Lattner7399ee02008-10-20 02:05:46 +00001679 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00001680 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00001681 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001682 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00001683 delete AttrList;
1684 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001685
1686 ConsumeParen(); // Eat the closing ')'.
1687
1688 // cv-qualifier-seq[opt].
1689 DeclSpec DS;
1690 if (getLang().CPlusPlus) {
1691 ParseTypeQualifierListOpt(DS);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001692
1693 // Parse exception-specification[opt].
1694 if (Tok.is(tok::kw_throw))
1695 ParseExceptionSpecification();
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001696 }
1697
Chris Lattnerf97409f2008-04-06 06:57:35 +00001698 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00001699 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001700 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00001701 /*variadic*/ false,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001702 /*arglist*/ 0, 0,
1703 DS.getTypeQualifiers(),
1704 LParenLoc));
Chris Lattnerf97409f2008-04-06 06:57:35 +00001705 return;
Chris Lattner7399ee02008-10-20 02:05:46 +00001706 }
1707
1708 // Alternatively, this parameter list may be an identifier list form for a
1709 // K&R-style function: void foo(a,b,c)
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001710 if (!getLang().CPlusPlus && Tok.is(tok::identifier) &&
Chris Lattner7399ee02008-10-20 02:05:46 +00001711 // K&R identifier lists can't have typedefs as identifiers, per
1712 // C99 6.7.5.3p11.
1713 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1714 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001715 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00001716 delete AttrList;
1717 }
1718
Reid Spencer5f016e22007-07-11 17:01:13 +00001719 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1720 // normal declarators, not for abstract-declarators.
Chris Lattner66d28652008-04-06 06:34:08 +00001721 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattnerf97409f2008-04-06 06:57:35 +00001722 }
1723
1724 // Finally, a normal, non-empty parameter type list.
1725
1726 // Build up an array of information about the parsed arguments.
1727 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00001728
1729 // Enter function-declaration scope, limiting any declarators to the
1730 // function prototype scope, including parameter declarators.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001731 ParseScope PrototypeScope(this, Scope::FnScope|Scope::DeclScope);
Chris Lattnerf97409f2008-04-06 06:57:35 +00001732
1733 bool IsVariadic = false;
1734 while (1) {
1735 if (Tok.is(tok::ellipsis)) {
1736 IsVariadic = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001737
Chris Lattnerf97409f2008-04-06 06:57:35 +00001738 // Check to see if this is "void(...)" which is not allowed.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00001739 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00001740 // Otherwise, parse parameter type list. If it starts with an
1741 // ellipsis, diagnose the malformed function.
1742 Diag(Tok, diag::err_ellipsis_first_arg);
1743 IsVariadic = false; // Treat this like 'void()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001744 }
Chris Lattnere0e713b2008-01-31 06:10:07 +00001745
Chris Lattnerf97409f2008-04-06 06:57:35 +00001746 ConsumeToken(); // Consume the ellipsis.
1747 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001748 }
1749
Chris Lattnerf97409f2008-04-06 06:57:35 +00001750 SourceLocation DSStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00001751
Chris Lattnerf97409f2008-04-06 06:57:35 +00001752 // Parse the declaration-specifiers.
1753 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00001754
1755 // If the caller parsed attributes for the first argument, add them now.
1756 if (AttrList) {
1757 DS.AddAttributes(AttrList);
1758 AttrList = 0; // Only apply the attributes to the first parameter.
1759 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00001760 ParseDeclarationSpecifiers(DS);
1761
1762 // Parse the declarator. This is "PrototypeContext", because we must
1763 // accept either 'declarator' or 'abstract-declarator' here.
1764 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1765 ParseDeclarator(ParmDecl);
1766
1767 // Parse GNU attributes, if present.
1768 if (Tok.is(tok::kw___attribute))
1769 ParmDecl.AddAttributes(ParseAttributes());
1770
Chris Lattnerf97409f2008-04-06 06:57:35 +00001771 // Remember this parsed parameter in ParamInfo.
1772 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1773
Douglas Gregor72b505b2008-12-16 21:30:33 +00001774 // DefArgToks is used when the parsing of default arguments needs
1775 // to be delayed.
1776 CachedTokens *DefArgToks = 0;
1777
Chris Lattnerf97409f2008-04-06 06:57:35 +00001778 // If no parameter was specified, verify that *something* was specified,
1779 // otherwise we have a missing type and identifier.
1780 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1781 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1782 // Completely missing, emit error.
1783 Diag(DSStart, diag::err_missing_param);
1784 } else {
1785 // Otherwise, we have something. Add it and let semantic analysis try
1786 // to grok it and add the result to the ParamInfo we are building.
1787
1788 // Inform the actions module about the parameter declarator, so it gets
1789 // added to the current scope.
Chris Lattner04421082008-04-08 04:40:51 +00001790 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1791
1792 // Parse the default argument, if any. We parse the default
1793 // arguments in all dialects; the semantic analysis in
1794 // ActOnParamDefaultArgument will reject the default argument in
1795 // C.
1796 if (Tok.is(tok::equal)) {
Chris Lattner04421082008-04-08 04:40:51 +00001797 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00001798 if (D.getContext() == Declarator::MemberContext) {
1799 // If we're inside a class definition, cache the tokens
1800 // corresponding to the default argument. We'll actually parse
1801 // them when we see the end of the class definition.
1802 // FIXME: Templates will require something similar.
1803 // FIXME: Can we use a smart pointer for Toks?
1804 DefArgToks = new CachedTokens;
1805
1806 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
1807 tok::semi, false)) {
1808 delete DefArgToks;
1809 DefArgToks = 0;
1810 }
Chris Lattner04421082008-04-08 04:40:51 +00001811 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00001812 // Consume the '='.
1813 SourceLocation EqualLoc = ConsumeToken();
1814
1815 OwningExprResult DefArgResult(ParseAssignmentExpression());
1816 if (DefArgResult.isInvalid()) {
1817 Actions.ActOnParamDefaultArgumentError(Param);
1818 SkipUntil(tok::comma, tok::r_paren, true, true);
1819 } else {
1820 // Inform the actions module about the default argument
1821 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
1822 DefArgResult.release());
1823 }
Chris Lattner04421082008-04-08 04:40:51 +00001824 }
1825 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00001826
1827 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor72b505b2008-12-16 21:30:33 +00001828 ParmDecl.getIdentifierLoc(), Param,
1829 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00001830 }
1831
1832 // If the next token is a comma, consume it and keep reading arguments.
1833 if (Tok.isNot(tok::comma)) break;
1834
1835 // Consume the comma.
1836 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001837 }
1838
Chris Lattnerf97409f2008-04-06 06:57:35 +00001839 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001840 PrototypeScope.Exit();
Chris Lattnerf97409f2008-04-06 06:57:35 +00001841
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001842 // If we have the closing ')', eat it.
1843 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1844
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001845 DeclSpec DS;
1846 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001847 // Parse cv-qualifier-seq[opt].
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001848 ParseTypeQualifierListOpt(DS);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001849
1850 // Parse exception-specification[opt].
1851 if (Tok.is(tok::kw_throw))
1852 ParseExceptionSpecification();
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001853 }
1854
Reid Spencer5f016e22007-07-11 17:01:13 +00001855 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00001856 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1857 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001858 DS.getTypeQualifiers(),
Chris Lattnerf97409f2008-04-06 06:57:35 +00001859 LParenLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001860}
1861
Chris Lattner66d28652008-04-06 06:34:08 +00001862/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1863/// we found a K&R-style identifier list instead of a type argument list. The
1864/// current token is known to be the first identifier in the list.
1865///
1866/// identifier-list: [C99 6.7.5]
1867/// identifier
1868/// identifier-list ',' identifier
1869///
1870void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1871 Declarator &D) {
1872 // Build up an array of information about the parsed arguments.
1873 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1874 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1875
1876 // If there was no identifier specified for the declarator, either we are in
1877 // an abstract-declarator, or we are in a parameter declarator which was found
1878 // to be abstract. In abstract-declarators, identifier lists are not valid:
1879 // diagnose this.
1880 if (!D.getIdentifier())
1881 Diag(Tok, diag::ext_ident_list_in_param);
1882
1883 // Tok is known to be the first identifier in the list. Remember this
1884 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00001885 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00001886 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1887 Tok.getLocation(), 0));
1888
Chris Lattner50c64772008-04-06 06:39:19 +00001889 ConsumeToken(); // eat the first identifier.
Chris Lattner66d28652008-04-06 06:34:08 +00001890
1891 while (Tok.is(tok::comma)) {
1892 // Eat the comma.
1893 ConsumeToken();
1894
Chris Lattner50c64772008-04-06 06:39:19 +00001895 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00001896 if (Tok.isNot(tok::identifier)) {
1897 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00001898 SkipUntil(tok::r_paren);
1899 return;
Chris Lattner66d28652008-04-06 06:34:08 +00001900 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00001901
Chris Lattner66d28652008-04-06 06:34:08 +00001902 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00001903
1904 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1905 if (Actions.isTypeName(*ParmII, CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00001906 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner66d28652008-04-06 06:34:08 +00001907
1908 // Verify that the argument identifier has not already been mentioned.
1909 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00001910 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00001911 } else {
1912 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00001913 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1914 Tok.getLocation(), 0));
Chris Lattner50c64772008-04-06 06:39:19 +00001915 }
Chris Lattner66d28652008-04-06 06:34:08 +00001916
1917 // Eat the identifier.
1918 ConsumeToken();
1919 }
1920
Chris Lattner50c64772008-04-06 06:39:19 +00001921 // Remember that we parsed a function type, and remember the attributes. This
1922 // function type is always a K&R style function type, which is not varargs and
1923 // has no prototype.
1924 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1925 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001926 /*TypeQuals*/0, LParenLoc));
Chris Lattner66d28652008-04-06 06:34:08 +00001927
1928 // If we have the closing ')', eat it and we're done.
Chris Lattner50c64772008-04-06 06:39:19 +00001929 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00001930}
Chris Lattneref4715c2008-04-06 05:45:57 +00001931
Reid Spencer5f016e22007-07-11 17:01:13 +00001932/// [C90] direct-declarator '[' constant-expression[opt] ']'
1933/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1934/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1935/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1936/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1937void Parser::ParseBracketDeclarator(Declarator &D) {
1938 SourceLocation StartLoc = ConsumeBracket();
1939
1940 // If valid, this location is the position where we read the 'static' keyword.
1941 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00001942 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00001943 StaticLoc = ConsumeToken();
1944
1945 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00001946 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00001947 DeclSpec DS;
1948 ParseTypeQualifierListOpt(DS);
1949
1950 // If we haven't already read 'static', check to see if there is one after the
1951 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001952 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00001953 StaticLoc = ConsumeToken();
1954
1955 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1956 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001957 OwningExprResult NumElements(Actions);
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00001958
1959 // Handle the case where we have '[*]' as the array size. However, a leading
1960 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1961 // the the token after the star is a ']'. Since stars in arrays are
1962 // infrequent, use of lookahead is not costly here.
1963 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00001964 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001965
Chris Lattnera1fcbad2008-12-18 06:50:14 +00001966 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00001967 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00001968 StaticLoc = SourceLocation(); // Drop the static.
1969 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00001970 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00001971 } else if (Tok.isNot(tok::r_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001972 // Parse the assignment-expression now.
1973 NumElements = ParseAssignmentExpression();
1974 }
1975
1976 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001977 if (NumElements.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001978 // If the expression was invalid, skip it.
1979 SkipUntil(tok::r_square);
1980 return;
1981 }
1982
1983 MatchRHSPunctuation(tok::r_square, StartLoc);
1984
Reid Spencer5f016e22007-07-11 17:01:13 +00001985 // Remember that we parsed a pointer type, and remember the type-quals.
1986 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1987 StaticLoc.isValid(), isStar,
Sebastian Redleffa8d12008-12-10 00:02:53 +00001988 NumElements.release(), StartLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001989}
1990
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00001991/// [GNU] typeof-specifier:
1992/// typeof ( expressions )
1993/// typeof ( type-name )
1994/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00001995///
1996void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00001997 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001998 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffd1861fd2007-07-31 12:34:36 +00001999 SourceLocation StartLoc = ConsumeToken();
2000
Chris Lattner04d66662007-10-09 17:33:22 +00002001 if (Tok.isNot(tok::l_paren)) {
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002002 if (!getLang().CPlusPlus) {
Chris Lattner08631c52008-11-23 21:45:46 +00002003 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002004 return;
2005 }
2006
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002007 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002008 if (Result.isInvalid())
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002009 return;
2010
2011 const char *PrevSpec = 0;
2012 // Check for duplicate type specifiers.
2013 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002014 Result.release()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002015 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002016
2017 // FIXME: Not accurate, the range gets one token more than it should.
2018 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002019 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002020 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002021
Steve Naroffd1861fd2007-07-31 12:34:36 +00002022 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2023
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00002024 if (isTypeIdInParens()) {
Steve Naroffd1861fd2007-07-31 12:34:36 +00002025 TypeTy *Ty = ParseTypeName();
2026
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002027 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
2028
Chris Lattner04d66662007-10-09 17:33:22 +00002029 if (Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002030 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002031 return;
2032 }
2033 RParenLoc = ConsumeParen();
2034 const char *PrevSpec = 0;
2035 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2036 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002037 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002038 } else { // we have an expression.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002039 OwningExprResult Result(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002040
2041 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002042 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002043 return;
2044 }
2045 RParenLoc = ConsumeParen();
2046 const char *PrevSpec = 0;
2047 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2048 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002049 Result.release()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002050 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002051 }
Argyrios Kyrtzidis0919f9e2008-08-16 10:21:33 +00002052 DS.SetRangeEnd(RParenLoc);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002053}
2054
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00002055