blob: c7f92cf014db033ff3feac36dfcd3d0580085ca8 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Daniel Dunbarcc7b1602008-08-11 03:45:03 +000015#include "clang/Basic/Diagnostic.h"
Chris Lattnera7549902007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerdaa5c002008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Sebastian Redl6008ac32008-11-25 22:21:31 +000018#include "AstGuard.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "llvm/ADT/SmallSet.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
26/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Sebastian Redl66df3ef2008-12-02 14:43:59 +000031Parser::TypeTy *Parser::ParseTypeName() {
Chris Lattner4b009652007-07-25 00:24:17 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
34 ParseSpecifierQualifierList(DS);
35
36 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
39
Sebastian Redl66df3ef2008-12-02 14:43:59 +000040 return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
Chris Lattner4b009652007-07-25 00:24:17 +000041}
42
43/// ParseAttributes - Parse a non-empty attributes list.
44///
45/// [GNU] attributes:
46/// attribute
47/// attributes attribute
48///
49/// [GNU] attribute:
50/// '__attribute__' '(' '(' attribute-list ')' ')'
51///
52/// [GNU] attribute-list:
53/// attrib
54/// attribute_list ',' attrib
55///
56/// [GNU] attrib:
57/// empty
58/// attrib-name
59/// attrib-name '(' identifier ')'
60/// attrib-name '(' identifier ',' nonempty-expr-list ')'
61/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
62///
63/// [GNU] attrib-name:
64/// identifier
65/// typespec
66/// typequal
67/// storageclass
68///
69/// FIXME: The GCC grammar/code for this construct implies we need two
70/// token lookahead. Comment from gcc: "If they start with an identifier
71/// which is followed by a comma or close parenthesis, then the arguments
72/// start with that identifier; otherwise they are an expression list."
73///
74/// At the moment, I am not doing 2 token lookahead. I am also unaware of
75/// any attributes that don't work (based on my limited testing). Most
76/// attributes are very simple in practice. Until we find a bug, I don't see
77/// a pressing need to implement the 2 token lookahead.
78
79AttributeList *Parser::ParseAttributes() {
Chris Lattner34a01ad2007-10-09 17:33:22 +000080 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Chris Lattner4b009652007-07-25 00:24:17 +000081
82 AttributeList *CurrAttr = 0;
83
Chris Lattner34a01ad2007-10-09 17:33:22 +000084 while (Tok.is(tok::kw___attribute)) {
Chris Lattner4b009652007-07-25 00:24:17 +000085 ConsumeToken();
86 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
87 "attribute")) {
88 SkipUntil(tok::r_paren, true); // skip until ) or ;
89 return CurrAttr;
90 }
91 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
92 SkipUntil(tok::r_paren, true); // skip until ) or ;
93 return CurrAttr;
94 }
95 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner34a01ad2007-10-09 17:33:22 +000096 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
97 Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +000098
Chris Lattner34a01ad2007-10-09 17:33:22 +000099 if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000100 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
101 ConsumeToken();
102 continue;
103 }
104 // we have an identifier or declaration specifier (const, int, etc.)
105 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
106 SourceLocation AttrNameLoc = ConsumeToken();
107
108 // check if we have a "paramterized" attribute
Chris Lattner34a01ad2007-10-09 17:33:22 +0000109 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000110 ConsumeParen(); // ignore the left paren loc for now
111
Chris Lattner34a01ad2007-10-09 17:33:22 +0000112 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000113 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
114 SourceLocation ParmLoc = ConsumeToken();
115
Chris Lattner34a01ad2007-10-09 17:33:22 +0000116 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000117 // __attribute__(( mode(byte) ))
118 ConsumeParen(); // ignore the right paren loc for now
119 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
120 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000121 } else if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000122 ConsumeToken();
123 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000124 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000125 bool ArgExprsOk = true;
126
127 // now parse the non-empty comma separated list of expressions
128 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000129 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000130 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000131 ArgExprsOk = false;
132 SkipUntil(tok::r_paren);
133 break;
134 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000135 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000136 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000137 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000138 break;
139 ConsumeToken(); // Eat the comma, move to the next argument
140 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000141 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000142 ConsumeParen(); // ignore the right paren loc for now
143 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redl6008ac32008-11-25 22:21:31 +0000144 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Chris Lattner4b009652007-07-25 00:24:17 +0000145 }
146 }
147 } else { // not an identifier
148 // parse a possibly empty comma separated list of expressions
Chris Lattner34a01ad2007-10-09 17:33:22 +0000149 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000150 // __attribute__(( nonnull() ))
151 ConsumeParen(); // ignore the right paren loc for now
152 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
153 0, SourceLocation(), 0, 0, CurrAttr);
154 } else {
155 // __attribute__(( aligned(16) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000156 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000157 bool ArgExprsOk = true;
158
159 // now parse the list of expressions
160 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000161 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000162 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000163 ArgExprsOk = false;
164 SkipUntil(tok::r_paren);
165 break;
166 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000167 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000168 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000169 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000170 break;
171 ConsumeToken(); // Eat the comma, move to the next argument
172 }
173 // Match the ')'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000174 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000175 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl6008ac32008-11-25 22:21:31 +0000176 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
177 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Chris Lattner4b009652007-07-25 00:24:17 +0000178 CurrAttr);
179 }
180 }
181 }
182 } else {
183 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
184 0, SourceLocation(), 0, 0, CurrAttr);
185 }
186 }
187 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
188 SkipUntil(tok::r_paren, false);
189 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
190 SkipUntil(tok::r_paren, false);
191 }
192 return CurrAttr;
193}
194
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 Lattnerf7b2e552007-08-25 06:57:03 +0000198///
199/// declaration: [C99 6.7]
200/// block-declaration ->
201/// simple-declaration
202/// others [FIXME]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000203/// [C++] template-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000204/// [C++] namespace-definition
205/// others... [FIXME]
206///
Chris Lattner4b009652007-07-25 00:24:17 +0000207Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000208 switch (Tok.getKind()) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000209 case tok::kw_export:
210 case tok::kw_template:
211 return ParseTemplateDeclaration(Context);
Chris Lattnerf7b2e552007-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) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner34a01ad2007-10-09 17:33:22 +0000230 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnerf7b2e552007-08-25 06:57:03 +0000241
Chris Lattner4b009652007-07-25 00:24:17 +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///
Chris Lattner4b009652007-07-25 00:24:17 +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
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000255/// [C++] declarator initializer[opt]
256///
257/// [C++] initializer:
258/// [C++] '=' initializer-clause
259/// [C++] '(' expression-list ')'
Chris Lattner4b009652007-07-25 00:24:17 +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 Dunbarc3540ff2008-08-05 01:35:17 +0000272 if (Tok.is(tok::kw_asm)) {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000273 OwningExprResult AsmLabel(ParseSimpleAsm());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000274 if (AsmLabel.isInvalid()) {
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000275 SkipUntil(tok::semi);
276 return 0;
277 }
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000278
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000279 D.setAsmLabel(AsmLabel.release());
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000280 }
Chris Lattner4b009652007-07-25 00:24:17 +0000281
282 // If attributes are present, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000283 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000284 D.AddAttributes(ParseAttributes());
Steve Naroff6a0e2092007-09-12 14:07:44 +0000285
286 // Inform the current actions module that we just parsed this declarator.
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000287 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000288
Chris Lattner4b009652007-07-25 00:24:17 +0000289 // Parse declarator '=' initializer.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000290 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000291 ConsumeToken();
Sebastian Redl39d4f022008-12-11 22:51:44 +0000292 OwningExprResult Init(ParseInitializer());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000293 if (Init.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000294 SkipUntil(tok::semi);
295 return 0;
296 }
Sebastian Redl91f9b0a2008-12-13 16:23:55 +0000297 Actions.AddInitializerToDecl(LastDeclInGroup, move_convert(Init));
Argiris Kirtzidis9e55d462008-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 Redl6008ac32008-11-25 22:21:31 +0000301 ExprVector Exprs(Actions);
Argiris Kirtzidis9e55d462008-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 Redl6008ac32008-11-25 22:21:31 +0000316 Exprs.take(), Exprs.size(),
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000317 &CommaLocs[0], RParenLoc);
318 }
Douglas Gregor81c29152008-10-29 00:13:59 +0000319 } else {
320 Actions.ActOnUninitializedDecl(LastDeclInGroup);
Chris Lattner4b009652007-07-25 00:24:17 +0000321 }
322
Chris Lattner4b009652007-07-25 00:24:17 +0000323 // If we don't have a comma, it is either the end of the list (a ';') or an
324 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000325 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000326 break;
327
328 // Consume the comma.
329 ConsumeToken();
330
331 // Parse the next declarator.
332 D.clear();
Chris Lattner926cf542008-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
Chris Lattner4b009652007-07-25 00:24:17 +0000344 ParseDeclarator(D);
345 }
346
Chris Lattner34a01ad2007-10-09 17:33:22 +0000347 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000348 ConsumeToken();
349 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
350 }
Fariborz Jahanian6e9c2b12008-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 Jahaniancadb0702008-01-04 23:04:08 +0000354 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000355 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
356 }
Chris Lattner4b009652007-07-25 00:24:17 +0000357 Diag(Tok, diag::err_parse_error);
358 // Skip to end of block or statement
Chris Lattnerf491b412007-08-21 18:36:18 +0000359 SkipUntil(tok::r_brace, true, true);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000360 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +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.
374 ParseDeclarationSpecifiers(DS);
375
376 // Validate declspec for type-name.
377 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000378 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +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 Gregorf15ac4b2008-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);
Chris Lattner4b009652007-07-25 00:24:17 +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]
Chris Lattner4b009652007-07-25 00:24:17 +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 Redl9f5337b2008-11-14 23:42:31 +0000415/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000416/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000417/// function-specifier: [C99 6.7.4]
418/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000419/// [C++] 'virtual'
420/// [C++] 'explicit'
Chris Lattner4b009652007-07-25 00:24:17 +0000421///
Douglas Gregor52473432008-12-24 02:52:09 +0000422void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
423 TemplateParameterLists *TemplateParams)
Douglas Gregorb3bec712008-12-01 23:54:00 +0000424{
Chris Lattnera4ff4272008-03-13 06:29:04 +0000425 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000426 while (1) {
427 int isInvalid = false;
428 const char *PrevSpec = 0;
429 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000430
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000431 // Only annotate C++ scope. Allow class-name as an identifier in case
432 // it's a constructor.
Daniel Dunbar1afd88d2008-11-25 23:05:24 +0000433 if (getLang().CPlusPlus)
Argiris Kirtzidis91c80dc2008-11-26 21:41:52 +0000434 TryAnnotateCXXScopeToken();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000435
Chris Lattner4b009652007-07-25 00:24:17 +0000436 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000437 default:
Douglas Gregorb3bec712008-12-01 23:54:00 +0000438 // Try to parse a type-specifier; if we found one, continue. If it's not
439 // a type, this falls through.
Douglas Gregor52473432008-12-24 02:52:09 +0000440 if (MaybeParseTypeSpecifier(DS, isInvalid, PrevSpec, TemplateParams)) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000441 continue;
Douglas Gregorb3bec712008-12-01 23:54:00 +0000442 }
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000443
Chris Lattnerb99d7492008-07-26 00:20:22 +0000444 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000445 // If this is not a declaration specifier token, we're done reading decl
446 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000447 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000448 return;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000449
450 case tok::annot_cxxscope: {
451 if (DS.hasTypeSpecifier())
452 goto DoneWithDeclSpec;
453
454 // We are looking for a qualified typename.
455 if (NextToken().isNot(tok::identifier))
456 goto DoneWithDeclSpec;
457
458 CXXScopeSpec SS;
459 SS.setScopeRep(Tok.getAnnotationValue());
460 SS.setRange(Tok.getAnnotationRange());
461
462 // If the next token is the name of the class type that the C++ scope
463 // denotes, followed by a '(', then this is a constructor declaration.
464 // We're done with the decl-specifiers.
465 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
466 CurScope, &SS) &&
467 GetLookAheadToken(2).is(tok::l_paren))
468 goto DoneWithDeclSpec;
469
470 TypeTy *TypeRep = Actions.isTypeName(*NextToken().getIdentifierInfo(),
471 CurScope, &SS);
472 if (TypeRep == 0)
473 goto DoneWithDeclSpec;
474
475 ConsumeToken(); // The C++ scope.
476
477 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
478 TypeRep);
479 if (isInvalid)
480 break;
481
482 DS.SetRangeEnd(Tok.getLocation());
483 ConsumeToken(); // The typename.
484
485 continue;
486 }
487
Chris Lattnerfda18db2008-07-26 01:18:38 +0000488 // typedef-name
489 case tok::identifier: {
490 // This identifier can only be a typedef name if we haven't already seen
491 // a type-specifier. Without this check we misparse:
492 // typedef int X; struct Y { short X; }; as 'short int'.
493 if (DS.hasTypeSpecifier())
494 goto DoneWithDeclSpec;
495
496 // It has to be available as a typedef too!
Argiris Kirtzidis46403632008-08-01 10:35:27 +0000497 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattnerfda18db2008-07-26 01:18:38 +0000498 if (TypeRep == 0)
499 goto DoneWithDeclSpec;
500
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000501 // C++: If the identifier is actually the name of the class type
502 // being defined and the next token is a '(', then this is a
503 // constructor declaration. We're done with the decl-specifiers
504 // and will treat this token as an identifier.
505 if (getLang().CPlusPlus &&
506 CurScope->isCXXClassScope() &&
507 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
508 NextToken().getKind() == tok::l_paren)
509 goto DoneWithDeclSpec;
510
Chris Lattnerfda18db2008-07-26 01:18:38 +0000511 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
512 TypeRep);
513 if (isInvalid)
514 break;
515
516 DS.SetRangeEnd(Tok.getLocation());
517 ConsumeToken(); // The identifier
518
519 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
520 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
521 // Objective-C interface. If we don't have Objective-C or a '<', this is
522 // just a normal reference to a typedef name.
523 if (!Tok.is(tok::less) || !getLang().ObjC1)
524 continue;
525
526 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000527 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000528 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000529 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000530
531 DS.SetRangeEnd(EndProtoLoc);
532
Steve Narofff7683302008-09-22 10:28:57 +0000533 // Need to support trailing type qualifiers (e.g. "id<p> const").
534 // If a type specifier follows, it will be diagnosed elsewhere.
535 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000536 }
Chris Lattner4b009652007-07-25 00:24:17 +0000537 // GNU attributes support.
538 case tok::kw___attribute:
539 DS.AddAttributes(ParseAttributes());
540 continue;
541
542 // storage-class-specifier
543 case tok::kw_typedef:
544 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
545 break;
546 case tok::kw_extern:
547 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000548 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000549 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
550 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000551 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000552 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
553 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000554 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000555 case tok::kw_static:
556 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000557 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000558 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
559 break;
560 case tok::kw_auto:
561 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
562 break;
563 case tok::kw_register:
564 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
565 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000566 case tok::kw_mutable:
567 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
568 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000569 case tok::kw___thread:
570 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
571 break;
572
Chris Lattner4b009652007-07-25 00:24:17 +0000573 continue;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000574
Chris Lattner4b009652007-07-25 00:24:17 +0000575 // function-specifier
576 case tok::kw_inline:
577 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
578 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000579
580 case tok::kw_virtual:
581 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
582 break;
583
584 case tok::kw_explicit:
585 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
586 break;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000587
Steve Naroff5f0466b2008-06-05 00:02:44 +0000588 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000589 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000590 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
591 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000592 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000593 goto DoneWithDeclSpec;
594
595 {
596 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000597 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000598 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000599 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000600 DS.SetRangeEnd(EndProtoLoc);
601
Chris Lattnerf006a222008-11-18 07:48:38 +0000602 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
603 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000604 // Need to support trailing type qualifiers (e.g. "id<p> const").
605 // If a type specifier follows, it will be diagnosed elsewhere.
606 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000607 }
Chris Lattner4b009652007-07-25 00:24:17 +0000608 }
609 // If the specifier combination wasn't legal, issue a diagnostic.
610 if (isInvalid) {
611 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000612 // Pick between error or extwarn.
613 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
614 : diag::ext_duplicate_declspec;
615 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000616 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000617 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000618 ConsumeToken();
619 }
620}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000621
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000622/// MaybeParseTypeSpecifier - Try to parse a single type-specifier. We
623/// primarily follow the C++ grammar with additions for C99 and GNU,
624/// which together subsume the C grammar. Note that the C++
625/// type-specifier also includes the C type-qualifier (for const,
626/// volatile, and C99 restrict). Returns true if a type-specifier was
627/// found (and parsed), false otherwise.
628///
629/// type-specifier: [C++ 7.1.5]
630/// simple-type-specifier
631/// class-specifier
632/// enum-specifier
633/// elaborated-type-specifier [TODO]
634/// cv-qualifier
635///
636/// cv-qualifier: [C++ 7.1.5.1]
637/// 'const'
638/// 'volatile'
639/// [C99] 'restrict'
640///
641/// simple-type-specifier: [ C++ 7.1.5.2]
642/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
643/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
644/// 'char'
645/// 'wchar_t'
646/// 'bool'
647/// 'short'
648/// 'int'
649/// 'long'
650/// 'signed'
651/// 'unsigned'
652/// 'float'
653/// 'double'
654/// 'void'
655/// [C99] '_Bool'
656/// [C99] '_Complex'
657/// [C99] '_Imaginary' // Removed in TC2?
658/// [GNU] '_Decimal32'
659/// [GNU] '_Decimal64'
660/// [GNU] '_Decimal128'
661/// [GNU] typeof-specifier
662/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
663/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
664bool Parser::MaybeParseTypeSpecifier(DeclSpec &DS, int& isInvalid,
Douglas Gregor52473432008-12-24 02:52:09 +0000665 const char *&PrevSpec,
666 TemplateParameterLists *TemplateParams) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000667 // Annotate typenames and C++ scope specifiers.
668 TryAnnotateTypeOrScopeToken();
669
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000670 SourceLocation Loc = Tok.getLocation();
671
672 switch (Tok.getKind()) {
673 // simple-type-specifier:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000674 case tok::annot_qualtypename: {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000675 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000676 Tok.getAnnotationValue());
677 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
678 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000679
680 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
681 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
682 // Objective-C interface. If we don't have Objective-C or a '<', this is
683 // just a normal reference to a typedef name.
684 if (!Tok.is(tok::less) || !getLang().ObjC1)
685 return true;
686
687 SourceLocation EndProtoLoc;
688 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
689 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
690 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
691
692 DS.SetRangeEnd(EndProtoLoc);
693 return true;
694 }
695
696 case tok::kw_short:
697 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
698 break;
699 case tok::kw_long:
700 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
701 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
702 else
703 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
704 break;
705 case tok::kw_signed:
706 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
707 break;
708 case tok::kw_unsigned:
709 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
710 break;
711 case tok::kw__Complex:
712 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
713 break;
714 case tok::kw__Imaginary:
715 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
716 break;
717 case tok::kw_void:
718 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
719 break;
720 case tok::kw_char:
721 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
722 break;
723 case tok::kw_int:
724 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
725 break;
726 case tok::kw_float:
727 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
728 break;
729 case tok::kw_double:
730 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
731 break;
732 case tok::kw_wchar_t:
733 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
734 break;
735 case tok::kw_bool:
736 case tok::kw__Bool:
737 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
738 break;
739 case tok::kw__Decimal32:
740 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
741 break;
742 case tok::kw__Decimal64:
743 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
744 break;
745 case tok::kw__Decimal128:
746 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
747 break;
748
749 // class-specifier:
750 case tok::kw_class:
751 case tok::kw_struct:
752 case tok::kw_union:
Douglas Gregor52473432008-12-24 02:52:09 +0000753 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000754 return true;
755
756 // enum-specifier:
757 case tok::kw_enum:
758 ParseEnumSpecifier(DS);
759 return true;
760
761 // cv-qualifier:
762 case tok::kw_const:
763 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
764 getLang())*2;
765 break;
766 case tok::kw_volatile:
767 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
768 getLang())*2;
769 break;
770 case tok::kw_restrict:
771 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
772 getLang())*2;
773 break;
774
775 // GNU typeof support.
776 case tok::kw_typeof:
777 ParseTypeofSpecifier(DS);
778 return true;
779
780 default:
781 // Not a type-specifier; do nothing.
782 return false;
783 }
784
785 // If the specifier combination wasn't legal, issue a diagnostic.
786 if (isInvalid) {
787 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000788 // Pick between error or extwarn.
789 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
790 : diag::ext_duplicate_declspec;
791 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000792 }
793 DS.SetRangeEnd(Tok.getLocation());
794 ConsumeToken(); // whatever we parsed above.
795 return true;
796}
Chris Lattner4b009652007-07-25 00:24:17 +0000797
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000798/// ParseStructDeclaration - Parse a struct declaration without the terminating
799/// semicolon.
800///
Chris Lattner4b009652007-07-25 00:24:17 +0000801/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000802/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000803/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000804/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000805/// struct-declarator-list:
806/// struct-declarator
807/// struct-declarator-list ',' struct-declarator
808/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
809/// struct-declarator:
810/// declarator
811/// [GNU] declarator attributes[opt]
812/// declarator[opt] ':' constant-expression
813/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
814///
Chris Lattner3dd8d392008-04-10 06:46:29 +0000815void Parser::
816ParseStructDeclaration(DeclSpec &DS,
817 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000818 if (Tok.is(tok::kw___extension__)) {
819 // __extension__ silences extension warnings in the subexpression.
820 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +0000821 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000822 return ParseStructDeclaration(DS, Fields);
823 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000824
825 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000826 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +0000827 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +0000828
829 // If there are no declarators, issue a warning.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000830 if (Tok.is(tok::semi)) {
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000831 Diag(DSStart, diag::w_no_declarators);
Steve Naroffa9adf112007-08-20 22:28:22 +0000832 return;
833 }
834
835 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000836 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000837 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +0000838 FieldDeclarator &DeclaratorInfo = Fields.back();
839
Steve Naroffa9adf112007-08-20 22:28:22 +0000840 /// struct-declarator: declarator
841 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000842 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000843 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +0000844
Chris Lattner34a01ad2007-10-09 17:33:22 +0000845 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000846 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000847 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000848 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +0000849 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000850 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000851 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +0000852 }
853
854 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000855 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000856 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000857
858 // If we don't have a comma, it is either the end of the list (a ';')
859 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000860 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000861 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000862
863 // Consume the comma.
864 ConsumeToken();
865
866 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000867 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000868
869 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000870 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000871 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000872 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000873}
874
875/// ParseStructUnionBody
876/// struct-contents:
877/// struct-declaration-list
878/// [EXT] empty
879/// [GNU] "struct-declaration-list" without terminatoring ';'
880/// struct-declaration-list:
881/// struct-declaration
882/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +0000883/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +0000884///
Chris Lattner4b009652007-07-25 00:24:17 +0000885void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
886 unsigned TagType, DeclTy *TagDecl) {
887 SourceLocation LBraceLoc = ConsumeBrace();
888
889 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
890 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +0000891 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +0000892 Diag(Tok, diag::ext_empty_struct_union_enum)
893 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +0000894
895 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000896 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
897
Chris Lattner4b009652007-07-25 00:24:17 +0000898 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000899 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000900 // Each iteration of this loop reads one struct-declaration.
901
902 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000903 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000904 Diag(Tok, diag::ext_extra_struct_semi);
905 ConsumeToken();
906 continue;
907 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000908
909 // Parse all the comma separated declarators.
910 DeclSpec DS;
911 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +0000912 if (!Tok.is(tok::at)) {
913 ParseStructDeclaration(DS, FieldDeclarators);
914
915 // Convert them all to fields.
916 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
917 FieldDeclarator &FD = FieldDeclarators[i];
918 // Install the declarator into the current TagDecl.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000919 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
Chris Lattner1bf58f62008-06-21 19:39:06 +0000920 DS.getSourceRange().getBegin(),
921 FD.D, FD.BitfieldSize);
922 FieldDecls.push_back(Field);
923 }
924 } else { // Handle @defs
925 ConsumeToken();
926 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
927 Diag(Tok, diag::err_unexpected_at);
928 SkipUntil(tok::semi, true, true);
929 continue;
930 }
931 ConsumeToken();
932 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
933 if (!Tok.is(tok::identifier)) {
934 Diag(Tok, diag::err_expected_ident);
935 SkipUntil(tok::semi, true, true);
936 continue;
937 }
938 llvm::SmallVector<DeclTy*, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000939 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
940 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +0000941 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
942 ConsumeToken();
943 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
944 }
Chris Lattner4b009652007-07-25 00:24:17 +0000945
Chris Lattner34a01ad2007-10-09 17:33:22 +0000946 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000947 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +0000948 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000949 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +0000950 break;
951 } else {
952 Diag(Tok, diag::err_expected_semi_decl_list);
953 // Skip to end of block or statement
954 SkipUntil(tok::r_brace, true, true);
955 }
956 }
957
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000958 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000959
Chris Lattner4b009652007-07-25 00:24:17 +0000960 AttributeList *AttrList = 0;
961 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000962 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +0000963 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +0000964
965 Actions.ActOnFields(CurScope,
966 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
967 LBraceLoc, RBraceLoc,
968 AttrList);
Chris Lattner4b009652007-07-25 00:24:17 +0000969}
970
971
972/// ParseEnumSpecifier
973/// enum-specifier: [C99 6.7.2.2]
974/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000975///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +0000976/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
977/// '}' attributes[opt]
978/// 'enum' identifier
979/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000980///
981/// [C++] elaborated-type-specifier:
982/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
983///
Chris Lattner4b009652007-07-25 00:24:17 +0000984void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000985 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000986 SourceLocation StartLoc = ConsumeToken();
987
988 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +0000989
990 AttributeList *Attr = 0;
991 // If attributes exist after tag, parse them.
992 if (Tok.is(tok::kw___attribute))
993 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000994
995 CXXScopeSpec SS;
Argiris Kirtzidis91c80dc2008-11-26 21:41:52 +0000996 if (getLang().CPlusPlus && MaybeParseCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000997 if (Tok.isNot(tok::identifier)) {
998 Diag(Tok, diag::err_expected_ident);
999 if (Tok.isNot(tok::l_brace)) {
1000 // Has no name and is not a definition.
1001 // Skip the rest of this declarator, up until the comma or semicolon.
1002 SkipUntil(tok::comma, true);
1003 return;
1004 }
1005 }
1006 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001007
1008 // Must have either 'enum name' or 'enum {...}'.
1009 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1010 Diag(Tok, diag::err_expected_ident_lbrace);
1011
1012 // Skip the rest of this declarator, up until the comma or semicolon.
1013 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001014 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001015 }
1016
1017 // If an identifier is present, consume and remember it.
1018 IdentifierInfo *Name = 0;
1019 SourceLocation NameLoc;
1020 if (Tok.is(tok::identifier)) {
1021 Name = Tok.getIdentifierInfo();
1022 NameLoc = ConsumeToken();
1023 }
1024
1025 // There are three options here. If we have 'enum foo;', then this is a
1026 // forward declaration. If we have 'enum foo {...' then this is a
1027 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1028 //
1029 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1030 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1031 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1032 //
1033 Action::TagKind TK;
1034 if (Tok.is(tok::l_brace))
1035 TK = Action::TK_Definition;
1036 else if (Tok.is(tok::semi))
1037 TK = Action::TK_Declaration;
1038 else
1039 TK = Action::TK_Reference;
1040 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Douglas Gregor52473432008-12-24 02:52:09 +00001041 SS, Name, NameLoc, Attr,
1042 Action::MultiTemplateParamsArg(Actions));
Chris Lattner4b009652007-07-25 00:24:17 +00001043
Chris Lattner34a01ad2007-10-09 17:33:22 +00001044 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001045 ParseEnumBody(StartLoc, TagDecl);
1046
1047 // TODO: semantic analysis on the declspec for enums.
1048 const char *PrevSpec = 0;
1049 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerf006a222008-11-18 07:48:38 +00001050 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001051}
1052
1053/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1054/// enumerator-list:
1055/// enumerator
1056/// enumerator-list ',' enumerator
1057/// enumerator:
1058/// enumeration-constant
1059/// enumeration-constant '=' constant-expression
1060/// enumeration-constant:
1061/// identifier
1062///
1063void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
1064 SourceLocation LBraceLoc = ConsumeBrace();
1065
Chris Lattnerc9a92452007-08-27 17:24:30 +00001066 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001067 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001068 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001069
1070 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1071
1072 DeclTy *LastEnumConstDecl = 0;
1073
1074 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001075 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001076 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1077 SourceLocation IdentLoc = ConsumeToken();
1078
1079 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001080 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001081 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001082 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001083 AssignedVal = ParseConstantExpression();
1084 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001085 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001086 }
1087
1088 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +00001089 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +00001090 LastEnumConstDecl,
1091 IdentLoc, Ident,
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001092 EqualLoc,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001093 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001094 EnumConstantDecls.push_back(EnumConstDecl);
1095 LastEnumConstDecl = EnumConstDecl;
1096
Chris Lattner34a01ad2007-10-09 17:33:22 +00001097 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001098 break;
1099 SourceLocation CommaLoc = ConsumeToken();
1100
Chris Lattner34a01ad2007-10-09 17:33:22 +00001101 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +00001102 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1103 }
1104
1105 // Eat the }.
1106 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1107
Steve Naroff0acc9c92007-09-15 18:49:24 +00001108 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001109 EnumConstantDecls.size());
1110
1111 DeclTy *AttrList = 0;
1112 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001113 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001114 AttrList = ParseAttributes(); // FIXME: where do they do?
1115}
1116
1117/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001118/// start of a type-qualifier-list.
1119bool Parser::isTypeQualifier() const {
1120 switch (Tok.getKind()) {
1121 default: return false;
1122 // type-qualifier
1123 case tok::kw_const:
1124 case tok::kw_volatile:
1125 case tok::kw_restrict:
1126 return true;
1127 }
1128}
1129
1130/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001131/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001132bool Parser::isTypeSpecifierQualifier() {
1133 // Annotate typenames and C++ scope specifiers.
1134 TryAnnotateTypeOrScopeToken();
1135
Chris Lattner4b009652007-07-25 00:24:17 +00001136 switch (Tok.getKind()) {
1137 default: return false;
1138 // GNU attributes support.
1139 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001140 // GNU typeof support.
1141 case tok::kw_typeof:
1142
Chris Lattner4b009652007-07-25 00:24:17 +00001143 // type-specifiers
1144 case tok::kw_short:
1145 case tok::kw_long:
1146 case tok::kw_signed:
1147 case tok::kw_unsigned:
1148 case tok::kw__Complex:
1149 case tok::kw__Imaginary:
1150 case tok::kw_void:
1151 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001152 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001153 case tok::kw_int:
1154 case tok::kw_float:
1155 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001156 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001157 case tok::kw__Bool:
1158 case tok::kw__Decimal32:
1159 case tok::kw__Decimal64:
1160 case tok::kw__Decimal128:
1161
Chris Lattner2e78db32008-04-13 18:59:07 +00001162 // struct-or-union-specifier (C99) or class-specifier (C++)
1163 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001164 case tok::kw_struct:
1165 case tok::kw_union:
1166 // enum-specifier
1167 case tok::kw_enum:
1168
1169 // type-qualifier
1170 case tok::kw_const:
1171 case tok::kw_volatile:
1172 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001173
1174 // typedef-name
1175 case tok::annot_qualtypename:
Chris Lattner4b009652007-07-25 00:24:17 +00001176 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001177
1178 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1179 case tok::less:
1180 return getLang().ObjC1;
Chris Lattner4b009652007-07-25 00:24:17 +00001181 }
1182}
1183
1184/// isDeclarationSpecifier() - Return true if the current token is part of a
1185/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001186bool Parser::isDeclarationSpecifier() {
1187 // Annotate typenames and C++ scope specifiers.
1188 TryAnnotateTypeOrScopeToken();
1189
Chris Lattner4b009652007-07-25 00:24:17 +00001190 switch (Tok.getKind()) {
1191 default: return false;
1192 // storage-class-specifier
1193 case tok::kw_typedef:
1194 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001195 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001196 case tok::kw_static:
1197 case tok::kw_auto:
1198 case tok::kw_register:
1199 case tok::kw___thread:
1200
1201 // type-specifiers
1202 case tok::kw_short:
1203 case tok::kw_long:
1204 case tok::kw_signed:
1205 case tok::kw_unsigned:
1206 case tok::kw__Complex:
1207 case tok::kw__Imaginary:
1208 case tok::kw_void:
1209 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001210 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001211 case tok::kw_int:
1212 case tok::kw_float:
1213 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001214 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001215 case tok::kw__Bool:
1216 case tok::kw__Decimal32:
1217 case tok::kw__Decimal64:
1218 case tok::kw__Decimal128:
1219
Chris Lattner2e78db32008-04-13 18:59:07 +00001220 // struct-or-union-specifier (C99) or class-specifier (C++)
1221 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001222 case tok::kw_struct:
1223 case tok::kw_union:
1224 // enum-specifier
1225 case tok::kw_enum:
1226
1227 // type-qualifier
1228 case tok::kw_const:
1229 case tok::kw_volatile:
1230 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001231
Chris Lattner4b009652007-07-25 00:24:17 +00001232 // function-specifier
1233 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001234 case tok::kw_virtual:
1235 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001236
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001237 // typedef-name
1238 case tok::annot_qualtypename:
1239
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001240 // GNU typeof support.
1241 case tok::kw_typeof:
1242
1243 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001244 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001245 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001246
1247 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1248 case tok::less:
1249 return getLang().ObjC1;
Chris Lattner4b009652007-07-25 00:24:17 +00001250 }
1251}
1252
1253
1254/// ParseTypeQualifierListOpt
1255/// type-qualifier-list: [C99 6.7.5]
1256/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001257/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001258/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001259/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001260///
Chris Lattner460696f2008-12-18 07:02:59 +00001261void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001262 while (1) {
1263 int isInvalid = false;
1264 const char *PrevSpec = 0;
1265 SourceLocation Loc = Tok.getLocation();
1266
1267 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001268 case tok::kw_const:
1269 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1270 getLang())*2;
1271 break;
1272 case tok::kw_volatile:
1273 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1274 getLang())*2;
1275 break;
1276 case tok::kw_restrict:
1277 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1278 getLang())*2;
1279 break;
1280 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001281 if (AttributesAllowed) {
1282 DS.AddAttributes(ParseAttributes());
1283 continue; // do *not* consume the next token!
1284 }
1285 // otherwise, FALL THROUGH!
1286 default:
1287 // If this is not a type-qualifier token, we're done reading type
1288 // qualifiers. First verify that DeclSpec's are consistent.
1289 DS.Finish(Diags, PP.getSourceManager(), getLang());
1290 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001291 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001292
Chris Lattner4b009652007-07-25 00:24:17 +00001293 // If the specifier combination wasn't legal, issue a diagnostic.
1294 if (isInvalid) {
1295 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001296 // Pick between error or extwarn.
1297 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1298 : diag::ext_duplicate_declspec;
1299 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001300 }
1301 ConsumeToken();
1302 }
1303}
1304
1305
1306/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1307///
1308void Parser::ParseDeclarator(Declarator &D) {
1309 /// This implements the 'declarator' production in the C grammar, then checks
1310 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001311 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001312}
1313
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001314/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1315/// is parsed by the function passed to it. Pass null, and the direct-declarator
1316/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001317/// ptr-operator production.
1318///
Chris Lattner4b009652007-07-25 00:24:17 +00001319/// declarator: [C99 6.7.5]
1320/// pointer[opt] direct-declarator
1321/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1322/// [GNU] '&' restrict[opt] attributes[opt] declarator
1323///
1324/// pointer: [C99 6.7.5]
1325/// '*' type-qualifier-list[opt]
1326/// '*' type-qualifier-list[opt] pointer
1327///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001328/// ptr-operator:
1329/// '*' cv-qualifier-seq[opt]
1330/// '&'
1331/// [GNU] '&' restrict[opt] attributes[opt]
1332/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] [TODO]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001333void Parser::ParseDeclaratorInternal(Declarator &D,
1334 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001335 tok::TokenKind Kind = Tok.getKind();
1336
Steve Naroff7aa54752008-08-27 16:04:49 +00001337 // Not a pointer, C++ reference, or block.
1338 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001339 (Kind != tok::caret || !getLang().Blocks)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001340 if (DirectDeclParser)
1341 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001342 return;
1343 }
Chris Lattner4b009652007-07-25 00:24:17 +00001344
Steve Naroffdc22f212008-08-28 10:07:06 +00001345 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Chris Lattner4b009652007-07-25 00:24:17 +00001346 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1347
Steve Naroffdc22f212008-08-28 10:07:06 +00001348 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner69f01932008-02-21 01:32:26 +00001349 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001350 DeclSpec DS;
1351
1352 ParseTypeQualifierListOpt(DS);
1353
1354 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001355 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001356 if (Kind == tok::star)
1357 // Remember that we parsed a pointer type, and remember the type-quals.
1358 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1359 DS.TakeAttributes()));
1360 else
1361 // Remember that we parsed a Block type, and remember the type-quals.
1362 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1363 Loc));
Chris Lattner4b009652007-07-25 00:24:17 +00001364 } else {
1365 // Is a reference
1366 DeclSpec DS;
1367
1368 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1369 // cv-qualifiers are introduced through the use of a typedef or of a
1370 // template type argument, in which case the cv-qualifiers are ignored.
1371 //
1372 // [GNU] Retricted references are allowed.
1373 // [GNU] Attributes on references are allowed.
1374 ParseTypeQualifierListOpt(DS);
1375
1376 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1377 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1378 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001379 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001380 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1381 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001382 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001383 }
1384
1385 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001386 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001387
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001388 if (D.getNumTypeObjects() > 0) {
1389 // C++ [dcl.ref]p4: There shall be no references to references.
1390 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1391 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001392 if (const IdentifierInfo *II = D.getIdentifier())
1393 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1394 << II;
1395 else
1396 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1397 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001398
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001399 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001400 // can go ahead and build the (technically ill-formed)
1401 // declarator: reference collapsing will take care of it.
1402 }
1403 }
1404
Chris Lattner4b009652007-07-25 00:24:17 +00001405 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001406 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1407 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001408 }
1409}
1410
1411/// ParseDirectDeclarator
1412/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001413/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001414/// '(' declarator ')'
1415/// [GNU] '(' attributes declarator ')'
1416/// [C90] direct-declarator '[' constant-expression[opt] ']'
1417/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1418/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1419/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1420/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1421/// direct-declarator '(' parameter-type-list ')'
1422/// direct-declarator '(' identifier-list[opt] ')'
1423/// [GNU] direct-declarator '(' parameter-forward-declarations
1424/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001425/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1426/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001427/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001428///
1429/// declarator-id: [C++ 8]
1430/// id-expression
1431/// '::'[opt] nested-name-specifier[opt] type-name
1432///
1433/// id-expression: [C++ 5.1]
1434/// unqualified-id
1435/// qualified-id [TODO]
1436///
1437/// unqualified-id: [C++ 5.1]
1438/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001439/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001440/// conversion-function-id [TODO]
1441/// '~' class-name
1442/// template-id [TODO]
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001443///
Chris Lattner4b009652007-07-25 00:24:17 +00001444void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001445 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001446
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001447 if (getLang().CPlusPlus) {
1448 if (D.mayHaveIdentifier()) {
1449 bool afterCXXScope = MaybeParseCXXScopeSpecifier(D.getCXXScopeSpec());
1450 if (afterCXXScope) {
1451 // Change the declaration context for name lookup, until this function
1452 // is exited (and the declarator has been parsed).
1453 DeclScopeObj.EnterDeclaratorScope();
1454 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001455
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001456 if (Tok.is(tok::identifier)) {
1457 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor2fa10442008-12-18 19:37:40 +00001458
1459 // If this identifier is followed by a '<', we may have a template-id.
1460 DeclTy *Template;
1461 if (getLang().CPlusPlus && NextToken().is(tok::less) &&
1462 (Template = Actions.isTemplateName(*Tok.getIdentifierInfo(),
1463 CurScope))) {
1464 IdentifierInfo *II = Tok.getIdentifierInfo();
1465 AnnotateTemplateIdToken(Template, 0);
1466 // FIXME: Set the declarator to a template-id. How? I don't
1467 // know... for now, just use the identifier.
1468 D.SetIdentifier(II, Tok.getLocation());
1469 }
1470 // If this identifier is the name of the current class, it's a
1471 // constructor name.
1472 else if (getLang().CPlusPlus &&
1473 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope))
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001474 D.setConstructor(Actions.isTypeName(*Tok.getIdentifierInfo(),
1475 CurScope),
1476 Tok.getLocation());
Douglas Gregor2fa10442008-12-18 19:37:40 +00001477 // This is a normal identifier.
1478 else
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001479 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1480 ConsumeToken();
1481 goto PastIdentifier;
1482 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001483
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001484 if (Tok.is(tok::tilde)) {
1485 // This should be a C++ destructor.
1486 SourceLocation TildeLoc = ConsumeToken();
1487 if (Tok.is(tok::identifier)) {
1488 if (TypeTy *Type = ParseClassName())
1489 D.setDestructor(Type, TildeLoc);
1490 else
1491 D.SetIdentifier(0, TildeLoc);
1492 } else {
1493 Diag(Tok, diag::err_expected_class_name);
1494 D.SetIdentifier(0, TildeLoc);
1495 }
1496 goto PastIdentifier;
1497 }
1498
1499 // If we reached this point, token is not identifier and not '~'.
1500
1501 if (afterCXXScope) {
1502 Diag(Tok, diag::err_expected_unqualified_id);
1503 D.SetIdentifier(0, Tok.getLocation());
1504 D.setInvalidType(true);
1505 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001506 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001507 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001508
1509 if (Tok.is(tok::kw_operator)) {
1510 SourceLocation OperatorLoc = Tok.getLocation();
1511
1512 // First try the name of an overloaded operator
1513 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId()) {
1514 D.setOverloadedOperator(Op, OperatorLoc);
1515 } else {
1516 // This must be a conversion function (C++ [class.conv.fct]).
1517 if (TypeTy *ConvType = ParseConversionFunctionId())
1518 D.setConversionFunction(ConvType, OperatorLoc);
1519 else
1520 D.SetIdentifier(0, Tok.getLocation());
1521 }
1522 goto PastIdentifier;
1523 }
1524 }
1525
1526 // If we reached this point, we are either in C/ObjC or the token didn't
1527 // satisfy any of the C++-specific checks.
1528
1529 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1530 assert(!getLang().CPlusPlus &&
1531 "There's a C++-specific check for tok::identifier above");
1532 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1533 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1534 ConsumeToken();
1535 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001536 // direct-declarator: '(' declarator ')'
1537 // direct-declarator: '(' attributes declarator ')'
1538 // Example: 'char (*X)' or 'int (*XX)(void)'
1539 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001540 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001541 // This could be something simple like "int" (in which case the declarator
1542 // portion is empty), if an abstract-declarator is allowed.
1543 D.SetIdentifier(0, Tok.getLocation());
1544 } else {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001545 if (getLang().CPlusPlus)
1546 Diag(Tok, diag::err_expected_unqualified_id);
1547 else
Chris Lattnerf006a222008-11-18 07:48:38 +00001548 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00001549 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00001550 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001551 }
1552
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001553 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00001554 assert(D.isPastIdentifier() &&
1555 "Haven't past the location of the identifier yet?");
1556
1557 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001558 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001559 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1560 // In such a case, check if we actually have a function declarator; if it
1561 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001562 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1563 // When not in file scope, warn for ambiguous function declarators, just
1564 // in case the author intended it as a variable definition.
1565 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1566 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1567 break;
1568 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001569 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001570 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001571 ParseBracketDeclarator(D);
1572 } else {
1573 break;
1574 }
1575 }
1576}
1577
Chris Lattnera0d056d2008-04-06 05:45:57 +00001578/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1579/// only called before the identifier, so these are most likely just grouping
1580/// parens for precedence. If we find that these are actually function
1581/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1582///
1583/// direct-declarator:
1584/// '(' declarator ')'
1585/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001586/// direct-declarator '(' parameter-type-list ')'
1587/// direct-declarator '(' identifier-list[opt] ')'
1588/// [GNU] direct-declarator '(' parameter-forward-declarations
1589/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001590///
1591void Parser::ParseParenDeclarator(Declarator &D) {
1592 SourceLocation StartLoc = ConsumeParen();
1593 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1594
Chris Lattner1f185292008-10-20 02:05:46 +00001595 // Eat any attributes before we look at whether this is a grouping or function
1596 // declarator paren. If this is a grouping paren, the attribute applies to
1597 // the type being built up, for example:
1598 // int (__attribute__(()) *x)(long y)
1599 // If this ends up not being a grouping paren, the attribute applies to the
1600 // first argument, for example:
1601 // int (__attribute__(()) int x)
1602 // In either case, we need to eat any attributes to be able to determine what
1603 // sort of paren this is.
1604 //
1605 AttributeList *AttrList = 0;
1606 bool RequiresArg = false;
1607 if (Tok.is(tok::kw___attribute)) {
1608 AttrList = ParseAttributes();
1609
1610 // We require that the argument list (if this is a non-grouping paren) be
1611 // present even if the attribute list was empty.
1612 RequiresArg = true;
1613 }
1614
Chris Lattnera0d056d2008-04-06 05:45:57 +00001615 // If we haven't past the identifier yet (or where the identifier would be
1616 // stored, if this is an abstract declarator), then this is probably just
1617 // grouping parens. However, if this could be an abstract-declarator, then
1618 // this could also be the start of function arguments (consider 'void()').
1619 bool isGrouping;
1620
1621 if (!D.mayOmitIdentifier()) {
1622 // If this can't be an abstract-declarator, this *must* be a grouping
1623 // paren, because we haven't seen the identifier yet.
1624 isGrouping = true;
1625 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001626 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00001627 isDeclarationSpecifier()) { // 'int(int)' is a function.
1628 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1629 // considered to be a type, not a K&R identifier-list.
1630 isGrouping = false;
1631 } else {
1632 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1633 isGrouping = true;
1634 }
1635
1636 // If this is a grouping paren, handle:
1637 // direct-declarator: '(' declarator ')'
1638 // direct-declarator: '(' attributes declarator ')'
1639 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001640 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001641 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00001642 if (AttrList)
1643 D.AddAttributes(AttrList);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001644
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001645 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001646 // Match the ')'.
1647 MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001648
1649 D.setGroupingParens(hadGroupingParens);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001650 return;
1651 }
1652
1653 // Okay, if this wasn't a grouping paren, it must be the start of a function
1654 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00001655 // identifier (and remember where it would have been), then call into
1656 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00001657 D.SetIdentifier(0, Tok.getLocation());
1658
Chris Lattner1f185292008-10-20 02:05:46 +00001659 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001660}
1661
1662/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1663/// declarator D up to a paren, which indicates that we are parsing function
1664/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001665///
Chris Lattner1f185292008-10-20 02:05:46 +00001666/// If AttrList is non-null, then the caller parsed those arguments immediately
1667/// after the open paren - they should be considered to be the first argument of
1668/// a parameter. If RequiresArg is true, then the first argument of the
1669/// function is required to be present and required to not be an identifier
1670/// list.
1671///
Chris Lattner4b009652007-07-25 00:24:17 +00001672/// This method also handles this portion of the grammar:
1673/// parameter-type-list: [C99 6.7.5]
1674/// parameter-list
1675/// parameter-list ',' '...'
1676///
1677/// parameter-list: [C99 6.7.5]
1678/// parameter-declaration
1679/// parameter-list ',' parameter-declaration
1680///
1681/// parameter-declaration: [C99 6.7.5]
1682/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00001683/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001684/// [GNU] declaration-specifiers declarator attributes
1685/// declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00001686/// [C++] declaration-specifiers abstract-declarator[opt]
1687/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001688/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1689///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001690/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
1691/// and "exception-specification[opt]"(TODO).
1692///
Chris Lattner1f185292008-10-20 02:05:46 +00001693void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
1694 AttributeList *AttrList,
1695 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00001696 // lparen is already consumed!
1697 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00001698
Chris Lattner1f185292008-10-20 02:05:46 +00001699 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001700 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00001701 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001702 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00001703 delete AttrList;
1704 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001705
1706 ConsumeParen(); // Eat the closing ')'.
1707
1708 // cv-qualifier-seq[opt].
1709 DeclSpec DS;
1710 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00001711 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor90a2c972008-11-25 03:22:00 +00001712
1713 // Parse exception-specification[opt].
1714 if (Tok.is(tok::kw_throw))
1715 ParseExceptionSpecification();
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001716 }
1717
Chris Lattner9f7564b2008-04-06 06:57:35 +00001718 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00001719 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001720 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00001721 /*variadic*/ false,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001722 /*arglist*/ 0, 0,
1723 DS.getTypeQualifiers(),
1724 LParenLoc));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001725 return;
Chris Lattner1f185292008-10-20 02:05:46 +00001726 }
1727
1728 // Alternatively, this parameter list may be an identifier list form for a
1729 // K&R-style function: void foo(a,b,c)
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001730 if (!getLang().CPlusPlus && Tok.is(tok::identifier) &&
Chris Lattner1f185292008-10-20 02:05:46 +00001731 // K&R identifier lists can't have typedefs as identifiers, per
1732 // C99 6.7.5.3p11.
1733 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1734 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001735 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00001736 delete AttrList;
1737 }
1738
Chris Lattner4b009652007-07-25 00:24:17 +00001739 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1740 // normal declarators, not for abstract-declarators.
Chris Lattner35d9c912008-04-06 06:34:08 +00001741 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001742 }
1743
1744 // Finally, a normal, non-empty parameter type list.
1745
1746 // Build up an array of information about the parsed arguments.
1747 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00001748
1749 // Enter function-declaration scope, limiting any declarators to the
1750 // function prototype scope, including parameter declarators.
Douglas Gregor95d40792008-12-10 06:34:36 +00001751 ParseScope PrototypeScope(this, Scope::FnScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001752
1753 bool IsVariadic = false;
1754 while (1) {
1755 if (Tok.is(tok::ellipsis)) {
1756 IsVariadic = true;
Chris Lattner4b009652007-07-25 00:24:17 +00001757
Chris Lattner9f7564b2008-04-06 06:57:35 +00001758 // Check to see if this is "void(...)" which is not allowed.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001759 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00001760 // Otherwise, parse parameter type list. If it starts with an
1761 // ellipsis, diagnose the malformed function.
1762 Diag(Tok, diag::err_ellipsis_first_arg);
1763 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001764 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00001765
Chris Lattner9f7564b2008-04-06 06:57:35 +00001766 ConsumeToken(); // Consume the ellipsis.
1767 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001768 }
1769
Chris Lattner9f7564b2008-04-06 06:57:35 +00001770 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00001771
Chris Lattner9f7564b2008-04-06 06:57:35 +00001772 // Parse the declaration-specifiers.
1773 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00001774
1775 // If the caller parsed attributes for the first argument, add them now.
1776 if (AttrList) {
1777 DS.AddAttributes(AttrList);
1778 AttrList = 0; // Only apply the attributes to the first parameter.
1779 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001780 ParseDeclarationSpecifiers(DS);
1781
1782 // Parse the declarator. This is "PrototypeContext", because we must
1783 // accept either 'declarator' or 'abstract-declarator' here.
1784 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1785 ParseDeclarator(ParmDecl);
1786
1787 // Parse GNU attributes, if present.
1788 if (Tok.is(tok::kw___attribute))
1789 ParmDecl.AddAttributes(ParseAttributes());
1790
Chris Lattner9f7564b2008-04-06 06:57:35 +00001791 // Remember this parsed parameter in ParamInfo.
1792 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1793
Douglas Gregor605de8d2008-12-16 21:30:33 +00001794 // DefArgToks is used when the parsing of default arguments needs
1795 // to be delayed.
1796 CachedTokens *DefArgToks = 0;
1797
Chris Lattner9f7564b2008-04-06 06:57:35 +00001798 // If no parameter was specified, verify that *something* was specified,
1799 // otherwise we have a missing type and identifier.
1800 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1801 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1802 // Completely missing, emit error.
1803 Diag(DSStart, diag::err_missing_param);
1804 } else {
1805 // Otherwise, we have something. Add it and let semantic analysis try
1806 // to grok it and add the result to the ParamInfo we are building.
1807
1808 // Inform the actions module about the parameter declarator, so it gets
1809 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00001810 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1811
1812 // Parse the default argument, if any. We parse the default
1813 // arguments in all dialects; the semantic analysis in
1814 // ActOnParamDefaultArgument will reject the default argument in
1815 // C.
1816 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00001817 SourceLocation EqualLoc = Tok.getLocation();
1818
Chris Lattner3e254fb2008-04-08 04:40:51 +00001819 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00001820 if (D.getContext() == Declarator::MemberContext) {
1821 // If we're inside a class definition, cache the tokens
1822 // corresponding to the default argument. We'll actually parse
1823 // them when we see the end of the class definition.
1824 // FIXME: Templates will require something similar.
1825 // FIXME: Can we use a smart pointer for Toks?
1826 DefArgToks = new CachedTokens;
1827
1828 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
1829 tok::semi, false)) {
1830 delete DefArgToks;
1831 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00001832 Actions.ActOnParamDefaultArgumentError(Param);
1833 } else
1834 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00001835 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00001836 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00001837 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00001838
1839 OwningExprResult DefArgResult(ParseAssignmentExpression());
1840 if (DefArgResult.isInvalid()) {
1841 Actions.ActOnParamDefaultArgumentError(Param);
1842 SkipUntil(tok::comma, tok::r_paren, true, true);
1843 } else {
1844 // Inform the actions module about the default argument
1845 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
1846 DefArgResult.release());
1847 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00001848 }
1849 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001850
1851 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00001852 ParmDecl.getIdentifierLoc(), Param,
1853 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001854 }
1855
1856 // If the next token is a comma, consume it and keep reading arguments.
1857 if (Tok.isNot(tok::comma)) break;
1858
1859 // Consume the comma.
1860 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00001861 }
1862
Chris Lattner9f7564b2008-04-06 06:57:35 +00001863 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00001864 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00001865
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001866 // If we have the closing ')', eat it.
1867 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1868
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001869 DeclSpec DS;
1870 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00001871 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00001872 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor90a2c972008-11-25 03:22:00 +00001873
1874 // Parse exception-specification[opt].
1875 if (Tok.is(tok::kw_throw))
1876 ParseExceptionSpecification();
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001877 }
1878
Chris Lattner4b009652007-07-25 00:24:17 +00001879 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001880 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1881 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001882 DS.getTypeQualifiers(),
Chris Lattner9f7564b2008-04-06 06:57:35 +00001883 LParenLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00001884}
1885
Chris Lattner35d9c912008-04-06 06:34:08 +00001886/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1887/// we found a K&R-style identifier list instead of a type argument list. The
1888/// current token is known to be the first identifier in the list.
1889///
1890/// identifier-list: [C99 6.7.5]
1891/// identifier
1892/// identifier-list ',' identifier
1893///
1894void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1895 Declarator &D) {
1896 // Build up an array of information about the parsed arguments.
1897 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1898 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1899
1900 // If there was no identifier specified for the declarator, either we are in
1901 // an abstract-declarator, or we are in a parameter declarator which was found
1902 // to be abstract. In abstract-declarators, identifier lists are not valid:
1903 // diagnose this.
1904 if (!D.getIdentifier())
1905 Diag(Tok, diag::ext_ident_list_in_param);
1906
1907 // Tok is known to be the first identifier in the list. Remember this
1908 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00001909 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00001910 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1911 Tok.getLocation(), 0));
1912
Chris Lattner113a56b2008-04-06 06:39:19 +00001913 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00001914
1915 while (Tok.is(tok::comma)) {
1916 // Eat the comma.
1917 ConsumeToken();
1918
Chris Lattner113a56b2008-04-06 06:39:19 +00001919 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00001920 if (Tok.isNot(tok::identifier)) {
1921 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00001922 SkipUntil(tok::r_paren);
1923 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00001924 }
Chris Lattneracb67d92008-04-06 06:47:48 +00001925
Chris Lattner35d9c912008-04-06 06:34:08 +00001926 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00001927
1928 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1929 if (Actions.isTypeName(*ParmII, CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00001930 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00001931
1932 // Verify that the argument identifier has not already been mentioned.
1933 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001934 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00001935 } else {
1936 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00001937 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1938 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00001939 }
Chris Lattner35d9c912008-04-06 06:34:08 +00001940
1941 // Eat the identifier.
1942 ConsumeToken();
1943 }
1944
Chris Lattner113a56b2008-04-06 06:39:19 +00001945 // Remember that we parsed a function type, and remember the attributes. This
1946 // function type is always a K&R style function type, which is not varargs and
1947 // has no prototype.
1948 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1949 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001950 /*TypeQuals*/0, LParenLoc));
Chris Lattner35d9c912008-04-06 06:34:08 +00001951
1952 // If we have the closing ')', eat it and we're done.
Chris Lattner113a56b2008-04-06 06:39:19 +00001953 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00001954}
Chris Lattnera0d056d2008-04-06 05:45:57 +00001955
Chris Lattner4b009652007-07-25 00:24:17 +00001956/// [C90] direct-declarator '[' constant-expression[opt] ']'
1957/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1958/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1959/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1960/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1961void Parser::ParseBracketDeclarator(Declarator &D) {
1962 SourceLocation StartLoc = ConsumeBracket();
1963
Chris Lattner1525c3a2008-12-18 07:27:21 +00001964 // C array syntax has many features, but by-far the most common is [] and [4].
1965 // This code does a fast path to handle some of the most obvious cases.
1966 if (Tok.getKind() == tok::r_square) {
1967 MatchRHSPunctuation(tok::r_square, StartLoc);
1968 // Remember that we parsed the empty array type.
1969 OwningExprResult NumElements(Actions);
1970 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc));
1971 return;
1972 } else if (Tok.getKind() == tok::numeric_constant &&
1973 GetLookAheadToken(1).is(tok::r_square)) {
1974 // [4] is very common. Parse the numeric constant expression.
1975 OwningExprResult ExprRes(Actions, Actions.ActOnNumericConstant(Tok));
1976 ConsumeToken();
1977
1978 MatchRHSPunctuation(tok::r_square, StartLoc);
1979
1980 // If there was an error parsing the assignment-expression, recover.
1981 if (ExprRes.isInvalid())
1982 ExprRes.release(); // Deallocate expr, just use [].
1983
1984 // Remember that we parsed a array type, and remember its features.
1985 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
1986 ExprRes.release(), StartLoc));
1987 return;
1988 }
1989
Chris Lattner4b009652007-07-25 00:24:17 +00001990 // If valid, this location is the position where we read the 'static' keyword.
1991 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001992 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001993 StaticLoc = ConsumeToken();
1994
1995 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00001996 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00001997 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00001998 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00001999
2000 // If we haven't already read 'static', check to see if there is one after the
2001 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002002 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002003 StaticLoc = ConsumeToken();
2004
2005 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2006 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002007 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002008
2009 // Handle the case where we have '[*]' as the array size. However, a leading
2010 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2011 // the the token after the star is a ']'. Since stars in arrays are
2012 // infrequent, use of lookahead is not costly here.
2013 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002014 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002015
Chris Lattner306d4df2008-12-18 06:50:14 +00002016 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002017 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002018 StaticLoc = SourceLocation(); // Drop the static.
2019 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002020 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002021 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002022 // Note, in C89, this production uses the constant-expr production instead
2023 // of assignment-expr. The only difference is that assignment-expr allows
2024 // things like '=' and '*='. Sema rejects these in C89 mode because they
2025 // are not i-c-e's, so we don't need to distinguish between the two here.
2026
Chris Lattner4b009652007-07-25 00:24:17 +00002027 // Parse the assignment-expression now.
2028 NumElements = ParseAssignmentExpression();
2029 }
2030
2031 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002032 if (NumElements.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002033 // If the expression was invalid, skip it.
2034 SkipUntil(tok::r_square);
2035 return;
2036 }
2037
2038 MatchRHSPunctuation(tok::r_square, StartLoc);
2039
Chris Lattner1525c3a2008-12-18 07:27:21 +00002040 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002041 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2042 StaticLoc.isValid(), isStar,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002043 NumElements.release(), StartLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00002044}
2045
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002046/// [GNU] typeof-specifier:
2047/// typeof ( expressions )
2048/// typeof ( type-name )
2049/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002050///
2051void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002052 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002053 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002054 SourceLocation StartLoc = ConsumeToken();
2055
Chris Lattner34a01ad2007-10-09 17:33:22 +00002056 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002057 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002058 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002059 return;
2060 }
2061
Sebastian Redl14ca7412008-12-11 21:36:32 +00002062 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002063 if (Result.isInvalid())
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002064 return;
2065
2066 const char *PrevSpec = 0;
2067 // Check for duplicate type specifiers.
2068 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002069 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002070 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002071
2072 // FIXME: Not accurate, the range gets one token more than it should.
2073 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002074 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002075 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002076
Steve Naroff7cbb1462007-07-31 12:34:36 +00002077 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2078
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002079 if (isTypeIdInParens()) {
Steve Naroff7cbb1462007-07-31 12:34:36 +00002080 TypeTy *Ty = ParseTypeName();
2081
Steve Naroff4c255ab2007-07-31 23:56:32 +00002082 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
2083
Chris Lattner34a01ad2007-10-09 17:33:22 +00002084 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002085 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002086 return;
2087 }
2088 RParenLoc = ConsumeParen();
2089 const char *PrevSpec = 0;
2090 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2091 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
Chris Lattnerf006a222008-11-18 07:48:38 +00002092 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002093 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002094 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002095
2096 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002097 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002098 return;
2099 }
2100 RParenLoc = ConsumeParen();
2101 const char *PrevSpec = 0;
2102 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2103 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002104 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002105 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002106 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002107 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002108}
2109
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002110