blob: 1ac26a309fa2a93db3f7160c9465baa0d4c2c88e [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
15#include "clang/Parse/DeclSpec.h"
Chris Lattner31e05722007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "llvm/ADT/SmallSet.h"
18using namespace clang;
19
20//===----------------------------------------------------------------------===//
21// C99 6.7: Declarations.
22//===----------------------------------------------------------------------===//
23
24/// ParseTypeName
25/// type-name: [C99 6.7.6]
26/// specifier-qualifier-list abstract-declarator[opt]
27Parser::TypeTy *Parser::ParseTypeName() {
28 // Parse the common declaration-specifiers piece.
29 DeclSpec DS;
30 ParseSpecifierQualifierList(DS);
31
32 // Parse the abstract-declarator, if present.
33 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
34 ParseDeclarator(DeclaratorInfo);
35
Steve Naroff08d92e42007-09-15 18:49:24 +000036 return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
Reid Spencer5f016e22007-07-11 17:01:13 +000037}
38
39/// ParseAttributes - Parse a non-empty attributes list.
40///
41/// [GNU] attributes:
42/// attribute
43/// attributes attribute
44///
45/// [GNU] attribute:
46/// '__attribute__' '(' '(' attribute-list ')' ')'
47///
48/// [GNU] attribute-list:
49/// attrib
50/// attribute_list ',' attrib
51///
52/// [GNU] attrib:
53/// empty
54/// attrib-name
55/// attrib-name '(' identifier ')'
56/// attrib-name '(' identifier ',' nonempty-expr-list ')'
57/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
58///
59/// [GNU] attrib-name:
60/// identifier
61/// typespec
62/// typequal
63/// storageclass
64///
65/// FIXME: The GCC grammar/code for this construct implies we need two
66/// token lookahead. Comment from gcc: "If they start with an identifier
67/// which is followed by a comma or close parenthesis, then the arguments
68/// start with that identifier; otherwise they are an expression list."
69///
70/// At the moment, I am not doing 2 token lookahead. I am also unaware of
71/// any attributes that don't work (based on my limited testing). Most
72/// attributes are very simple in practice. Until we find a bug, I don't see
73/// a pressing need to implement the 2 token lookahead.
74
75AttributeList *Parser::ParseAttributes() {
Chris Lattner04d66662007-10-09 17:33:22 +000076 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Reid Spencer5f016e22007-07-11 17:01:13 +000077
78 AttributeList *CurrAttr = 0;
79
Chris Lattner04d66662007-10-09 17:33:22 +000080 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000081 ConsumeToken();
82 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
83 "attribute")) {
84 SkipUntil(tok::r_paren, true); // skip until ) or ;
85 return CurrAttr;
86 }
87 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
88 SkipUntil(tok::r_paren, true); // skip until ) or ;
89 return CurrAttr;
90 }
91 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +000092 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
93 Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000094
Chris Lattner04d66662007-10-09 17:33:22 +000095 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000096 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
97 ConsumeToken();
98 continue;
99 }
100 // we have an identifier or declaration specifier (const, int, etc.)
101 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
102 SourceLocation AttrNameLoc = ConsumeToken();
103
104 // check if we have a "paramterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000105 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 ConsumeParen(); // ignore the left paren loc for now
107
Chris Lattner04d66662007-10-09 17:33:22 +0000108 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
110 SourceLocation ParmLoc = ConsumeToken();
111
Chris Lattner04d66662007-10-09 17:33:22 +0000112 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 // __attribute__(( mode(byte) ))
114 ConsumeParen(); // ignore the right paren loc for now
115 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
116 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner04d66662007-10-09 17:33:22 +0000117 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 ConsumeToken();
119 // __attribute__(( format(printf, 1, 2) ))
120 llvm::SmallVector<ExprTy*, 8> ArgExprs;
121 bool ArgExprsOk = true;
122
123 // now parse the non-empty comma separated list of expressions
124 while (1) {
125 ExprResult ArgExpr = ParseAssignmentExpression();
126 if (ArgExpr.isInvalid) {
127 ArgExprsOk = false;
128 SkipUntil(tok::r_paren);
129 break;
130 } else {
131 ArgExprs.push_back(ArgExpr.Val);
132 }
Chris Lattner04d66662007-10-09 17:33:22 +0000133 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 break;
135 ConsumeToken(); // Eat the comma, move to the next argument
136 }
Chris Lattner04d66662007-10-09 17:33:22 +0000137 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 ConsumeParen(); // ignore the right paren loc for now
139 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
140 ParmLoc, &ArgExprs[0], ArgExprs.size(), CurrAttr);
141 }
142 }
143 } else { // not an identifier
144 // parse a possibly empty comma separated list of expressions
Chris Lattner04d66662007-10-09 17:33:22 +0000145 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 // __attribute__(( nonnull() ))
147 ConsumeParen(); // ignore the right paren loc for now
148 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
149 0, SourceLocation(), 0, 0, CurrAttr);
150 } else {
151 // __attribute__(( aligned(16) ))
152 llvm::SmallVector<ExprTy*, 8> ArgExprs;
153 bool ArgExprsOk = true;
154
155 // now parse the list of expressions
156 while (1) {
157 ExprResult ArgExpr = ParseAssignmentExpression();
158 if (ArgExpr.isInvalid) {
159 ArgExprsOk = false;
160 SkipUntil(tok::r_paren);
161 break;
162 } else {
163 ArgExprs.push_back(ArgExpr.Val);
164 }
Chris Lattner04d66662007-10-09 17:33:22 +0000165 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 break;
167 ConsumeToken(); // Eat the comma, move to the next argument
168 }
169 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000170 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 ConsumeParen(); // ignore the right paren loc for now
172 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
173 SourceLocation(), &ArgExprs[0], ArgExprs.size(),
174 CurrAttr);
175 }
176 }
177 }
178 } else {
179 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
180 0, SourceLocation(), 0, 0, CurrAttr);
181 }
182 }
183 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
184 SkipUntil(tok::r_paren, false);
185 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
186 SkipUntil(tok::r_paren, false);
187 }
188 return CurrAttr;
189}
190
191/// ParseDeclaration - Parse a full 'declaration', which consists of
192/// declaration-specifiers, some number of declarators, and a semicolon.
193/// 'Context' should be a Declarator::TheContext value.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000194///
195/// declaration: [C99 6.7]
196/// block-declaration ->
197/// simple-declaration
198/// others [FIXME]
199/// [C++] namespace-definition
200/// others... [FIXME]
201///
Reid Spencer5f016e22007-07-11 17:01:13 +0000202Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattner8f08cb72007-08-25 06:57:03 +0000203 switch (Tok.getKind()) {
204 case tok::kw_namespace:
205 return ParseNamespace(Context);
206 default:
207 return ParseSimpleDeclaration(Context);
208 }
209}
210
211/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
212/// declaration-specifiers init-declarator-list[opt] ';'
213///[C90/C++]init-declarator-list ';' [TODO]
214/// [OMP] threadprivate-directive [TODO]
215Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 // Parse the common declaration-specifiers piece.
217 DeclSpec DS;
218 ParseDeclarationSpecifiers(DS);
219
220 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
221 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000222 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 ConsumeToken();
224 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
225 }
226
227 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
228 ParseDeclarator(DeclaratorInfo);
229
230 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
231}
232
Chris Lattner8f08cb72007-08-25 06:57:03 +0000233
Reid Spencer5f016e22007-07-11 17:01:13 +0000234/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
235/// parsing 'declaration-specifiers declarator'. This method is split out this
236/// way to handle the ambiguity between top-level function-definitions and
237/// declarations.
238///
Reid Spencer5f016e22007-07-11 17:01:13 +0000239/// init-declarator-list: [C99 6.7]
240/// init-declarator
241/// init-declarator-list ',' init-declarator
242/// init-declarator: [C99 6.7]
243/// declarator
244/// declarator '=' initializer
245/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
246/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
247///
248Parser::DeclTy *Parser::
249ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
250
251 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
252 // that they can be chained properly if the actions want this.
253 Parser::DeclTy *LastDeclInGroup = 0;
254
255 // At this point, we know that it is not a function definition. Parse the
256 // rest of the init-declarator-list.
257 while (1) {
258 // If a simple-asm-expr is present, parse it.
Chris Lattner04d66662007-10-09 17:33:22 +0000259 if (Tok.is(tok::kw_asm))
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 ParseSimpleAsm();
261
262 // If attributes are present, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000263 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 D.AddAttributes(ParseAttributes());
Steve Naroffbb204692007-09-12 14:07:44 +0000265
266 // Inform the current actions module that we just parsed this declarator.
267 // FIXME: pass asm & attributes.
Steve Naroff08d92e42007-09-15 18:49:24 +0000268 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Steve Naroffbb204692007-09-12 14:07:44 +0000269
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 // Parse declarator '=' initializer.
271 ExprResult Init;
Chris Lattner04d66662007-10-09 17:33:22 +0000272 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000273 ConsumeToken();
274 Init = ParseInitializer();
275 if (Init.isInvalid) {
276 SkipUntil(tok::semi);
277 return 0;
278 }
Steve Naroffbb204692007-09-12 14:07:44 +0000279 Actions.AddInitializerToDecl(LastDeclInGroup, Init.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 }
281
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 // If we don't have a comma, it is either the end of the list (a ';') or an
283 // error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000284 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 break;
286
287 // Consume the comma.
288 ConsumeToken();
289
290 // Parse the next declarator.
291 D.clear();
292 ParseDeclarator(D);
293 }
294
Chris Lattner04d66662007-10-09 17:33:22 +0000295 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000296 ConsumeToken();
297 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
298 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000299 // If this is an ObjC2 for-each loop, this is a successful declarator
300 // parse. The syntax for these looks like:
301 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000302 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000303 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
304 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 Diag(Tok, diag::err_parse_error);
306 // Skip to end of block or statement
Chris Lattnered442382007-08-21 18:36:18 +0000307 SkipUntil(tok::r_brace, true, true);
Chris Lattner04d66662007-10-09 17:33:22 +0000308 if (Tok.is(tok::semi))
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 ConsumeToken();
310 return 0;
311}
312
313/// ParseSpecifierQualifierList
314/// specifier-qualifier-list:
315/// type-specifier specifier-qualifier-list[opt]
316/// type-qualifier specifier-qualifier-list[opt]
317/// [GNU] attributes specifier-qualifier-list[opt]
318///
319void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
320 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
321 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 ParseDeclarationSpecifiers(DS);
323
324 // Validate declspec for type-name.
325 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000326 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 Diag(Tok, diag::err_typename_requires_specqual);
328
329 // Issue diagnostic and remove storage class if present.
330 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
331 if (DS.getStorageClassSpecLoc().isValid())
332 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
333 else
334 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
335 DS.ClearStorageClassSpecs();
336 }
337
338 // Issue diagnostic and remove function specfier if present.
339 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
340 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
341 DS.ClearFunctionSpecs();
342 }
343}
344
345/// ParseDeclarationSpecifiers
346/// declaration-specifiers: [C99 6.7]
347/// storage-class-specifier declaration-specifiers[opt]
348/// type-specifier declaration-specifiers[opt]
349/// type-qualifier declaration-specifiers[opt]
350/// [C99] function-specifier declaration-specifiers[opt]
351/// [GNU] attributes declaration-specifiers[opt]
352///
353/// storage-class-specifier: [C99 6.7.1]
354/// 'typedef'
355/// 'extern'
356/// 'static'
357/// 'auto'
358/// 'register'
359/// [GNU] '__thread'
360/// type-specifier: [C99 6.7.2]
361/// 'void'
362/// 'char'
363/// 'short'
364/// 'int'
365/// 'long'
366/// 'float'
367/// 'double'
368/// 'signed'
369/// 'unsigned'
370/// struct-or-union-specifier
371/// enum-specifier
372/// typedef-name
373/// [C++] 'bool'
374/// [C99] '_Bool'
375/// [C99] '_Complex'
376/// [C99] '_Imaginary' // Removed in TC2?
377/// [GNU] '_Decimal32'
378/// [GNU] '_Decimal64'
379/// [GNU] '_Decimal128'
Steve Naroff2cb64ec2007-07-31 23:56:32 +0000380/// [GNU] typeof-specifier
Reid Spencer5f016e22007-07-11 17:01:13 +0000381/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000382/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Reid Spencer5f016e22007-07-11 17:01:13 +0000383/// type-qualifier:
384/// 'const'
385/// 'volatile'
386/// [C99] 'restrict'
387/// function-specifier: [C99 6.7.4]
388/// [C99] 'inline'
389///
390void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000391 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 while (1) {
393 int isInvalid = false;
394 const char *PrevSpec = 0;
395 SourceLocation Loc = Tok.getLocation();
396
397 switch (Tok.getKind()) {
398 // typedef-name
399 case tok::identifier:
400 // This identifier can only be a typedef name if we haven't already seen
401 // a type-specifier. Without this check we misparse:
402 // typedef int X; struct Y { short X; }; as 'short int'.
403 if (!DS.hasTypeSpecifier()) {
404 // It has to be available as a typedef too!
405 if (void *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(),
406 CurScope)) {
407 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
408 TypeRep);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000409 if (isInvalid)
410 break;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000411 // FIXME: restrict this to "id" and ObjC classnames.
Chris Lattner81c018d2008-03-13 06:29:04 +0000412 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000413 ConsumeToken(); // The identifier
414 if (Tok.is(tok::less)) {
Steve Narofff908a872007-10-30 02:23:23 +0000415 SourceLocation endProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000416 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Steve Narofff908a872007-10-30 02:23:23 +0000417 ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc);
Chris Lattner7caeabd2008-07-21 22:17:28 +0000418
419 // FIXME: New'ing this here seems wrong, why not have the action do
420 // it?
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000421 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
422 new llvm::SmallVector<DeclTy *, 8>;
423 DS.setProtocolQualifiers(ProtocolDecl);
424 Actions.FindProtocolDeclaration(Loc,
425 &ProtocolRefs[0], ProtocolRefs.size(),
426 *ProtocolDecl);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000427 }
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000428 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000429 }
430 }
431 // FALL THROUGH.
432 default:
433 // If this is not a declaration specifier token, we're done reading decl
434 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000435 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +0000436 return;
437
438 // GNU attributes support.
439 case tok::kw___attribute:
440 DS.AddAttributes(ParseAttributes());
441 continue;
442
443 // storage-class-specifier
444 case tok::kw_typedef:
445 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
446 break;
447 case tok::kw_extern:
448 if (DS.isThreadSpecified())
449 Diag(Tok, diag::ext_thread_before, "extern");
450 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
451 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000452 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000453 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
454 PrevSpec);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000455 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000456 case tok::kw_static:
457 if (DS.isThreadSpecified())
458 Diag(Tok, diag::ext_thread_before, "static");
459 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
460 break;
461 case tok::kw_auto:
462 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
463 break;
464 case tok::kw_register:
465 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
466 break;
467 case tok::kw___thread:
468 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
469 break;
470
471 // type-specifiers
472 case tok::kw_short:
473 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
474 break;
475 case tok::kw_long:
476 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
477 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
478 else
479 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
480 break;
481 case tok::kw_signed:
482 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
483 break;
484 case tok::kw_unsigned:
485 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
486 break;
487 case tok::kw__Complex:
488 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
489 break;
490 case tok::kw__Imaginary:
491 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
492 break;
493 case tok::kw_void:
494 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
495 break;
496 case tok::kw_char:
497 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
498 break;
499 case tok::kw_int:
500 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
501 break;
502 case tok::kw_float:
503 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
504 break;
505 case tok::kw_double:
506 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
507 break;
508 case tok::kw_bool: // [C++ 2.11p1]
509 case tok::kw__Bool:
510 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
511 break;
512 case tok::kw__Decimal32:
513 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
514 break;
515 case tok::kw__Decimal64:
516 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
517 break;
518 case tok::kw__Decimal128:
519 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
520 break;
Chris Lattner99dc9142008-04-13 18:59:07 +0000521
522 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000523 case tok::kw_struct:
524 case tok::kw_union:
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000525 ParseClassSpecifier(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000526 continue;
527 case tok::kw_enum:
528 ParseEnumSpecifier(DS);
529 continue;
530
Steve Naroffd1861fd2007-07-31 12:34:36 +0000531 // GNU typeof support.
532 case tok::kw_typeof:
533 ParseTypeofSpecifier(DS);
534 continue;
535
Reid Spencer5f016e22007-07-11 17:01:13 +0000536 // type-qualifier
537 case tok::kw_const:
538 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
539 getLang())*2;
540 break;
541 case tok::kw_volatile:
542 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
543 getLang())*2;
544 break;
545 case tok::kw_restrict:
546 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
547 getLang())*2;
548 break;
549
550 // function-specifier
551 case tok::kw_inline:
552 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
553 break;
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000554
555 // Gross GCC-ism that we are forced support. FIXME: make an extension?
556 case tok::less:
557 if (!DS.hasTypeSpecifier()) {
558 SourceLocation endProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000559 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000560 ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc);
561 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
562 new llvm::SmallVector<DeclTy *, 8>;
563 DS.setProtocolQualifiers(ProtocolDecl);
564 Actions.FindProtocolDeclaration(Loc,
565 &ProtocolRefs[0], ProtocolRefs.size(),
566 *ProtocolDecl);
567 }
568 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000569 }
570 // If the specifier combination wasn't legal, issue a diagnostic.
571 if (isInvalid) {
572 assert(PrevSpec && "Method did not return previous specifier!");
573 if (isInvalid == 1) // Error.
574 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
575 else // extwarn.
576 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
577 }
Chris Lattner81c018d2008-03-13 06:29:04 +0000578 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000579 ConsumeToken();
580 }
581}
582
583/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
584/// the first token has already been read and has been turned into an instance
585/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
586/// otherwise it returns false and fills in Decl.
587bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
588 AttributeList *Attr = 0;
589 // If attributes exist after tag, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000590 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 Attr = ParseAttributes();
592
593 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner04d66662007-10-09 17:33:22 +0000594 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000595 Diag(Tok, diag::err_expected_ident_lbrace);
Chris Lattnere80a59c2007-07-25 00:24:17 +0000596
597 // Skip the rest of this declarator, up until the comma or semicolon.
598 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 return true;
600 }
601
602 // If an identifier is present, consume and remember it.
603 IdentifierInfo *Name = 0;
604 SourceLocation NameLoc;
Chris Lattner04d66662007-10-09 17:33:22 +0000605 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 Name = Tok.getIdentifierInfo();
607 NameLoc = ConsumeToken();
608 }
609
610 // There are three options here. If we have 'struct foo;', then this is a
611 // forward declaration. If we have 'struct foo {...' then this is a
612 // definition. Otherwise we have something like 'struct foo xyz', a reference.
613 //
614 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
615 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
616 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
617 //
618 Action::TagKind TK;
Chris Lattner04d66662007-10-09 17:33:22 +0000619 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 TK = Action::TK_Definition;
Chris Lattner04d66662007-10-09 17:33:22 +0000621 else if (Tok.is(tok::semi))
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 TK = Action::TK_Declaration;
623 else
624 TK = Action::TK_Reference;
Steve Naroff08d92e42007-09-15 18:49:24 +0000625 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 return false;
627}
628
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000629/// ParseStructDeclaration - Parse a struct declaration without the terminating
630/// semicolon.
631///
Reid Spencer5f016e22007-07-11 17:01:13 +0000632/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000633/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000634/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000635/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000636/// struct-declarator-list:
637/// struct-declarator
638/// struct-declarator-list ',' struct-declarator
639/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
640/// struct-declarator:
641/// declarator
642/// [GNU] declarator attributes[opt]
643/// declarator[opt] ':' constant-expression
644/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
645///
Chris Lattnere1359422008-04-10 06:46:29 +0000646void Parser::
647ParseStructDeclaration(DeclSpec &DS,
648 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Steve Naroff28a7ca82007-08-20 22:28:22 +0000649 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattnere1359422008-04-10 06:46:29 +0000650 while (Tok.is(tok::kw___extension__))
Steve Naroff28a7ca82007-08-20 22:28:22 +0000651 ConsumeToken();
652
653 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000654 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +0000655 ParseSpecifierQualifierList(DS);
656 // TODO: Does specifier-qualifier list correctly check that *something* is
657 // specified?
658
659 // If there are no declarators, issue a warning.
Chris Lattner04d66662007-10-09 17:33:22 +0000660 if (Tok.is(tok::semi)) {
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000661 Diag(DSStart, diag::w_no_declarators);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000662 return;
663 }
664
665 // Read struct-declarators until we find the semicolon.
Chris Lattnerebe457c2008-04-10 16:37:40 +0000666 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +0000667 while (1) {
Chris Lattnere1359422008-04-10 06:46:29 +0000668 FieldDeclarator &DeclaratorInfo = Fields.back();
669
Steve Naroff28a7ca82007-08-20 22:28:22 +0000670 /// struct-declarator: declarator
671 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +0000672 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +0000673 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000674
Chris Lattner04d66662007-10-09 17:33:22 +0000675 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +0000676 ConsumeToken();
677 ExprResult Res = ParseConstantExpression();
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000678 if (Res.isInvalid)
Steve Naroff28a7ca82007-08-20 22:28:22 +0000679 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000680 else
Chris Lattnere1359422008-04-10 06:46:29 +0000681 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroff28a7ca82007-08-20 22:28:22 +0000682 }
683
684 // If attributes exist after the declarator, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000685 if (Tok.is(tok::kw___attribute))
Chris Lattnere1359422008-04-10 06:46:29 +0000686 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroff28a7ca82007-08-20 22:28:22 +0000687
688 // If we don't have a comma, it is either the end of the list (a ';')
689 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000690 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000691 return;
Steve Naroff28a7ca82007-08-20 22:28:22 +0000692
693 // Consume the comma.
694 ConsumeToken();
695
696 // Parse the next declarator.
Chris Lattnerebe457c2008-04-10 16:37:40 +0000697 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +0000698
699 // Attributes are only allowed on the second declarator.
Chris Lattner04d66662007-10-09 17:33:22 +0000700 if (Tok.is(tok::kw___attribute))
Chris Lattnere1359422008-04-10 06:46:29 +0000701 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroff28a7ca82007-08-20 22:28:22 +0000702 }
Steve Naroff28a7ca82007-08-20 22:28:22 +0000703}
704
705/// ParseStructUnionBody
706/// struct-contents:
707/// struct-declaration-list
708/// [EXT] empty
709/// [GNU] "struct-declaration-list" without terminatoring ';'
710/// struct-declaration-list:
711/// struct-declaration
712/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +0000713/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +0000714///
Reid Spencer5f016e22007-07-11 17:01:13 +0000715void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
716 unsigned TagType, DeclTy *TagDecl) {
717 SourceLocation LBraceLoc = ConsumeBrace();
718
719 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
720 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000721 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Reid Spencer5f016e22007-07-11 17:01:13 +0000722 Diag(Tok, diag::ext_empty_struct_union_enum,
723 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
724
725 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000726 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
727
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +0000729 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000730 // Each iteration of this loop reads one struct-declaration.
731
732 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +0000733 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000734 Diag(Tok, diag::ext_extra_struct_semi);
735 ConsumeToken();
736 continue;
737 }
Chris Lattnere1359422008-04-10 06:46:29 +0000738
739 // Parse all the comma separated declarators.
740 DeclSpec DS;
741 FieldDeclarators.clear();
Chris Lattner5a6ddbf2008-06-21 19:39:06 +0000742 if (!Tok.is(tok::at)) {
743 ParseStructDeclaration(DS, FieldDeclarators);
744
745 // Convert them all to fields.
746 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
747 FieldDeclarator &FD = FieldDeclarators[i];
748 // Install the declarator into the current TagDecl.
749 DeclTy *Field = Actions.ActOnField(CurScope,
750 DS.getSourceRange().getBegin(),
751 FD.D, FD.BitfieldSize);
752 FieldDecls.push_back(Field);
753 }
754 } else { // Handle @defs
755 ConsumeToken();
756 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
757 Diag(Tok, diag::err_unexpected_at);
758 SkipUntil(tok::semi, true, true);
759 continue;
760 }
761 ConsumeToken();
762 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
763 if (!Tok.is(tok::identifier)) {
764 Diag(Tok, diag::err_expected_ident);
765 SkipUntil(tok::semi, true, true);
766 continue;
767 }
768 llvm::SmallVector<DeclTy*, 16> Fields;
769 Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
770 Fields);
771 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
772 ConsumeToken();
773 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
774 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000775
Chris Lattner04d66662007-10-09 17:33:22 +0000776 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000777 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +0000778 } else if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000779 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
780 break;
781 } else {
782 Diag(Tok, diag::err_expected_semi_decl_list);
783 // Skip to end of block or statement
784 SkipUntil(tok::r_brace, true, true);
785 }
786 }
787
Steve Naroff60fccee2007-10-29 21:38:07 +0000788 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000789
Fariborz Jahanian9d048ff2007-09-29 00:54:24 +0000790 Actions.ActOnFields(CurScope,
Chris Lattnerc81c8142008-02-25 21:04:36 +0000791 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
Steve Naroff60fccee2007-10-29 21:38:07 +0000792 LBraceLoc, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000793
794 AttributeList *AttrList = 0;
795 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000796 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000797 AttrList = ParseAttributes(); // FIXME: where should I put them?
798}
799
800
801/// ParseEnumSpecifier
802/// enum-specifier: [C99 6.7.2.2]
803/// 'enum' identifier[opt] '{' enumerator-list '}'
804/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
805/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
806/// '}' attributes[opt]
807/// 'enum' identifier
808/// [GNU] 'enum' attributes[opt] identifier
809void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +0000810 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Reid Spencer5f016e22007-07-11 17:01:13 +0000811 SourceLocation StartLoc = ConsumeToken();
812
813 // Parse the tag portion of this.
814 DeclTy *TagDecl;
815 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
816 return;
817
Chris Lattner04d66662007-10-09 17:33:22 +0000818 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +0000819 ParseEnumBody(StartLoc, TagDecl);
820
821 // TODO: semantic analysis on the declspec for enums.
822 const char *PrevSpec = 0;
823 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
824 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
825}
826
827/// ParseEnumBody - Parse a {} enclosed enumerator-list.
828/// enumerator-list:
829/// enumerator
830/// enumerator-list ',' enumerator
831/// enumerator:
832/// enumeration-constant
833/// enumeration-constant '=' constant-expression
834/// enumeration-constant:
835/// identifier
836///
837void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
838 SourceLocation LBraceLoc = ConsumeBrace();
839
Chris Lattner7946dd32007-08-27 17:24:30 +0000840 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +0000841 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Reid Spencer5f016e22007-07-11 17:01:13 +0000842 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
843
844 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
845
846 DeclTy *LastEnumConstDecl = 0;
847
848 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +0000849 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000850 IdentifierInfo *Ident = Tok.getIdentifierInfo();
851 SourceLocation IdentLoc = ConsumeToken();
852
853 SourceLocation EqualLoc;
854 ExprTy *AssignedVal = 0;
Chris Lattner04d66662007-10-09 17:33:22 +0000855 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 EqualLoc = ConsumeToken();
857 ExprResult Res = ParseConstantExpression();
858 if (Res.isInvalid)
859 SkipUntil(tok::comma, tok::r_brace, true, true);
860 else
861 AssignedVal = Res.Val;
862 }
863
864 // Install the enumerator constant into EnumDecl.
Steve Naroff08d92e42007-09-15 18:49:24 +0000865 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +0000866 LastEnumConstDecl,
867 IdentLoc, Ident,
868 EqualLoc, AssignedVal);
869 EnumConstantDecls.push_back(EnumConstDecl);
870 LastEnumConstDecl = EnumConstDecl;
871
Chris Lattner04d66662007-10-09 17:33:22 +0000872 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000873 break;
874 SourceLocation CommaLoc = ConsumeToken();
875
Chris Lattner04d66662007-10-09 17:33:22 +0000876 if (Tok.isNot(tok::identifier) && !getLang().C99)
Reid Spencer5f016e22007-07-11 17:01:13 +0000877 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
878 }
879
880 // Eat the }.
881 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
882
Steve Naroff08d92e42007-09-15 18:49:24 +0000883 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Reid Spencer5f016e22007-07-11 17:01:13 +0000884 EnumConstantDecls.size());
885
886 DeclTy *AttrList = 0;
887 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000888 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 AttrList = ParseAttributes(); // FIXME: where do they do?
890}
891
892/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +0000893/// start of a type-qualifier-list.
894bool Parser::isTypeQualifier() const {
895 switch (Tok.getKind()) {
896 default: return false;
897 // type-qualifier
898 case tok::kw_const:
899 case tok::kw_volatile:
900 case tok::kw_restrict:
901 return true;
902 }
903}
904
905/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +0000906/// start of a specifier-qualifier-list.
907bool Parser::isTypeSpecifierQualifier() const {
908 switch (Tok.getKind()) {
909 default: return false;
910 // GNU attributes support.
911 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +0000912 // GNU typeof support.
913 case tok::kw_typeof:
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000914 // GNU bizarre protocol extension. FIXME: make an extension?
915 case tok::less:
Steve Naroffd1861fd2007-07-31 12:34:36 +0000916
Reid Spencer5f016e22007-07-11 17:01:13 +0000917 // type-specifiers
918 case tok::kw_short:
919 case tok::kw_long:
920 case tok::kw_signed:
921 case tok::kw_unsigned:
922 case tok::kw__Complex:
923 case tok::kw__Imaginary:
924 case tok::kw_void:
925 case tok::kw_char:
926 case tok::kw_int:
927 case tok::kw_float:
928 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +0000929 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +0000930 case tok::kw__Bool:
931 case tok::kw__Decimal32:
932 case tok::kw__Decimal64:
933 case tok::kw__Decimal128:
934
Chris Lattner99dc9142008-04-13 18:59:07 +0000935 // struct-or-union-specifier (C99) or class-specifier (C++)
936 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000937 case tok::kw_struct:
938 case tok::kw_union:
939 // enum-specifier
940 case tok::kw_enum:
941
942 // type-qualifier
943 case tok::kw_const:
944 case tok::kw_volatile:
945 case tok::kw_restrict:
946 return true;
947
948 // typedef-name
949 case tok::identifier:
950 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000951 }
952}
953
954/// isDeclarationSpecifier() - Return true if the current token is part of a
955/// declaration specifier.
956bool Parser::isDeclarationSpecifier() const {
957 switch (Tok.getKind()) {
958 default: return false;
959 // storage-class-specifier
960 case tok::kw_typedef:
961 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +0000962 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +0000963 case tok::kw_static:
964 case tok::kw_auto:
965 case tok::kw_register:
966 case tok::kw___thread:
967
968 // type-specifiers
969 case tok::kw_short:
970 case tok::kw_long:
971 case tok::kw_signed:
972 case tok::kw_unsigned:
973 case tok::kw__Complex:
974 case tok::kw__Imaginary:
975 case tok::kw_void:
976 case tok::kw_char:
977 case tok::kw_int:
978 case tok::kw_float:
979 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +0000980 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +0000981 case tok::kw__Bool:
982 case tok::kw__Decimal32:
983 case tok::kw__Decimal64:
984 case tok::kw__Decimal128:
985
Chris Lattner99dc9142008-04-13 18:59:07 +0000986 // struct-or-union-specifier (C99) or class-specifier (C++)
987 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000988 case tok::kw_struct:
989 case tok::kw_union:
990 // enum-specifier
991 case tok::kw_enum:
992
993 // type-qualifier
994 case tok::kw_const:
995 case tok::kw_volatile:
996 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +0000997
Reid Spencer5f016e22007-07-11 17:01:13 +0000998 // function-specifier
999 case tok::kw_inline:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001000
Chris Lattner1ef08762007-08-09 17:01:07 +00001001 // GNU typeof support.
1002 case tok::kw_typeof:
1003
1004 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001005 case tok::kw___attribute:
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001006
1007 // GNU bizarre protocol extension. FIXME: make an extension?
1008 case tok::less:
Reid Spencer5f016e22007-07-11 17:01:13 +00001009 return true;
1010
1011 // typedef-name
1012 case tok::identifier:
1013 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 }
1015}
1016
1017
1018/// ParseTypeQualifierListOpt
1019/// type-qualifier-list: [C99 6.7.5]
1020/// type-qualifier
1021/// [GNU] attributes
1022/// type-qualifier-list type-qualifier
1023/// [GNU] type-qualifier-list attributes
1024///
1025void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1026 while (1) {
1027 int isInvalid = false;
1028 const char *PrevSpec = 0;
1029 SourceLocation Loc = Tok.getLocation();
1030
1031 switch (Tok.getKind()) {
1032 default:
1033 // If this is not a type-qualifier token, we're done reading type
1034 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +00001035 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001036 return;
1037 case tok::kw_const:
1038 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1039 getLang())*2;
1040 break;
1041 case tok::kw_volatile:
1042 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1043 getLang())*2;
1044 break;
1045 case tok::kw_restrict:
1046 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1047 getLang())*2;
1048 break;
1049 case tok::kw___attribute:
1050 DS.AddAttributes(ParseAttributes());
1051 continue; // do *not* consume the next token!
1052 }
1053
1054 // If the specifier combination wasn't legal, issue a diagnostic.
1055 if (isInvalid) {
1056 assert(PrevSpec && "Method did not return previous specifier!");
1057 if (isInvalid == 1) // Error.
1058 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1059 else // extwarn.
1060 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1061 }
1062 ConsumeToken();
1063 }
1064}
1065
1066
1067/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1068///
1069void Parser::ParseDeclarator(Declarator &D) {
1070 /// This implements the 'declarator' production in the C grammar, then checks
1071 /// for well-formedness and issues diagnostics.
1072 ParseDeclaratorInternal(D);
Reid Spencer5f016e22007-07-11 17:01:13 +00001073}
1074
1075/// ParseDeclaratorInternal
1076/// declarator: [C99 6.7.5]
1077/// pointer[opt] direct-declarator
1078/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1079/// [GNU] '&' restrict[opt] attributes[opt] declarator
1080///
1081/// pointer: [C99 6.7.5]
1082/// '*' type-qualifier-list[opt]
1083/// '*' type-qualifier-list[opt] pointer
1084///
1085void Parser::ParseDeclaratorInternal(Declarator &D) {
1086 tok::TokenKind Kind = Tok.getKind();
1087
1088 // Not a pointer or C++ reference.
Chris Lattner76549142008-02-21 01:32:26 +00001089 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus))
Reid Spencer5f016e22007-07-11 17:01:13 +00001090 return ParseDirectDeclarator(D);
1091
1092 // Otherwise, '*' -> pointer or '&' -> reference.
1093 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1094
1095 if (Kind == tok::star) {
Chris Lattner76549142008-02-21 01:32:26 +00001096 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00001097 DeclSpec DS;
1098
1099 ParseTypeQualifierListOpt(DS);
1100
1101 // Recursively parse the declarator.
1102 ParseDeclaratorInternal(D);
1103
1104 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001105 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1106 DS.TakeAttributes()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001107 } else {
1108 // Is a reference
1109 DeclSpec DS;
1110
1111 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1112 // cv-qualifiers are introduced through the use of a typedef or of a
1113 // template type argument, in which case the cv-qualifiers are ignored.
1114 //
1115 // [GNU] Retricted references are allowed.
1116 // [GNU] Attributes on references are allowed.
1117 ParseTypeQualifierListOpt(DS);
1118
1119 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1120 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1121 Diag(DS.getConstSpecLoc(),
1122 diag::err_invalid_reference_qualifier_application,
1123 "const");
1124 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1125 Diag(DS.getVolatileSpecLoc(),
1126 diag::err_invalid_reference_qualifier_application,
1127 "volatile");
1128 }
1129
1130 // Recursively parse the declarator.
1131 ParseDeclaratorInternal(D);
1132
1133 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001134 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1135 DS.TakeAttributes()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001136 }
1137}
1138
1139/// ParseDirectDeclarator
1140/// direct-declarator: [C99 6.7.5]
1141/// identifier
1142/// '(' declarator ')'
1143/// [GNU] '(' attributes declarator ')'
1144/// [C90] direct-declarator '[' constant-expression[opt] ']'
1145/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1146/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1147/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1148/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1149/// direct-declarator '(' parameter-type-list ')'
1150/// direct-declarator '(' identifier-list[opt] ')'
1151/// [GNU] direct-declarator '(' parameter-forward-declarations
1152/// parameter-type-list[opt] ')'
1153///
1154void Parser::ParseDirectDeclarator(Declarator &D) {
1155 // Parse the first direct-declarator seen.
Chris Lattner04d66662007-10-09 17:33:22 +00001156 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001157 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1158 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1159 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001160 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001161 // direct-declarator: '(' declarator ')'
1162 // direct-declarator: '(' attributes declarator ')'
1163 // Example: 'char (*X)' or 'int (*XX)(void)'
1164 ParseParenDeclarator(D);
1165 } else if (D.mayOmitIdentifier()) {
1166 // This could be something simple like "int" (in which case the declarator
1167 // portion is empty), if an abstract-declarator is allowed.
1168 D.SetIdentifier(0, Tok.getLocation());
1169 } else {
1170 // Expected identifier or '('.
1171 Diag(Tok, diag::err_expected_ident_lparen);
1172 D.SetIdentifier(0, Tok.getLocation());
1173 }
1174
1175 assert(D.isPastIdentifier() &&
1176 "Haven't past the location of the identifier yet?");
1177
1178 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00001179 if (Tok.is(tok::l_paren)) {
Chris Lattneref4715c2008-04-06 05:45:57 +00001180 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00001181 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001182 ParseBracketDeclarator(D);
1183 } else {
1184 break;
1185 }
1186 }
1187}
1188
Chris Lattneref4715c2008-04-06 05:45:57 +00001189/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1190/// only called before the identifier, so these are most likely just grouping
1191/// parens for precedence. If we find that these are actually function
1192/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1193///
1194/// direct-declarator:
1195/// '(' declarator ')'
1196/// [GNU] '(' attributes declarator ')'
1197///
1198void Parser::ParseParenDeclarator(Declarator &D) {
1199 SourceLocation StartLoc = ConsumeParen();
1200 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1201
1202 // If we haven't past the identifier yet (or where the identifier would be
1203 // stored, if this is an abstract declarator), then this is probably just
1204 // grouping parens. However, if this could be an abstract-declarator, then
1205 // this could also be the start of function arguments (consider 'void()').
1206 bool isGrouping;
1207
1208 if (!D.mayOmitIdentifier()) {
1209 // If this can't be an abstract-declarator, this *must* be a grouping
1210 // paren, because we haven't seen the identifier yet.
1211 isGrouping = true;
1212 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
1213 isDeclarationSpecifier()) { // 'int(int)' is a function.
1214 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1215 // considered to be a type, not a K&R identifier-list.
1216 isGrouping = false;
1217 } else {
1218 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1219 isGrouping = true;
1220 }
1221
1222 // If this is a grouping paren, handle:
1223 // direct-declarator: '(' declarator ')'
1224 // direct-declarator: '(' attributes declarator ')'
1225 if (isGrouping) {
1226 if (Tok.is(tok::kw___attribute))
1227 D.AddAttributes(ParseAttributes());
1228
1229 ParseDeclaratorInternal(D);
1230 // Match the ')'.
1231 MatchRHSPunctuation(tok::r_paren, StartLoc);
1232 return;
1233 }
1234
1235 // Okay, if this wasn't a grouping paren, it must be the start of a function
1236 // argument list. Recognize that this declarator will never have an
1237 // identifier (and remember where it would have been), then fall through to
1238 // the handling of argument lists.
1239 D.SetIdentifier(0, Tok.getLocation());
1240
1241 ParseFunctionDeclarator(StartLoc, D);
1242}
1243
1244/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1245/// declarator D up to a paren, which indicates that we are parsing function
1246/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00001247///
1248/// This method also handles this portion of the grammar:
1249/// parameter-type-list: [C99 6.7.5]
1250/// parameter-list
1251/// parameter-list ',' '...'
1252///
1253/// parameter-list: [C99 6.7.5]
1254/// parameter-declaration
1255/// parameter-list ',' parameter-declaration
1256///
1257/// parameter-declaration: [C99 6.7.5]
1258/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00001259/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001260/// [GNU] declaration-specifiers declarator attributes
1261/// declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00001262/// [C++] declaration-specifiers abstract-declarator[opt]
1263/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001264/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1265///
Chris Lattneref4715c2008-04-06 05:45:57 +00001266void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D) {
1267 // lparen is already consumed!
1268 assert(D.isPastIdentifier() && "Should not call before identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001269
1270 // Okay, this is the parameter list of a function definition, or it is an
1271 // identifier list of a K&R-style function.
Reid Spencer5f016e22007-07-11 17:01:13 +00001272
Chris Lattner04d66662007-10-09 17:33:22 +00001273 if (Tok.is(tok::r_paren)) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00001274 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00001275 // int() -> no prototype, no '...'.
Chris Lattnerf97409f2008-04-06 06:57:35 +00001276 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/ false,
1277 /*variadic*/ false,
1278 /*arglist*/ 0, 0, LParenLoc));
1279
1280 ConsumeParen(); // Eat the closing ')'.
1281 return;
Chris Lattner04d66662007-10-09 17:33:22 +00001282 } else if (Tok.is(tok::identifier) &&
Reid Spencer5f016e22007-07-11 17:01:13 +00001283 // K&R identifier lists can't have typedefs as identifiers, per
1284 // C99 6.7.5.3p11.
1285 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1286 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1287 // normal declarators, not for abstract-declarators.
Chris Lattner66d28652008-04-06 06:34:08 +00001288 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattnerf97409f2008-04-06 06:57:35 +00001289 }
1290
1291 // Finally, a normal, non-empty parameter type list.
1292
1293 // Build up an array of information about the parsed arguments.
1294 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00001295
1296 // Enter function-declaration scope, limiting any declarators to the
1297 // function prototype scope, including parameter declarators.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00001298 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattnerf97409f2008-04-06 06:57:35 +00001299
1300 bool IsVariadic = false;
1301 while (1) {
1302 if (Tok.is(tok::ellipsis)) {
1303 IsVariadic = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001304
Chris Lattnerf97409f2008-04-06 06:57:35 +00001305 // Check to see if this is "void(...)" which is not allowed.
1306 if (ParamInfo.empty()) {
1307 // Otherwise, parse parameter type list. If it starts with an
1308 // ellipsis, diagnose the malformed function.
1309 Diag(Tok, diag::err_ellipsis_first_arg);
1310 IsVariadic = false; // Treat this like 'void()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001311 }
Chris Lattnere0e713b2008-01-31 06:10:07 +00001312
Chris Lattnerf97409f2008-04-06 06:57:35 +00001313 ConsumeToken(); // Consume the ellipsis.
1314 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001315 }
1316
Chris Lattnerf97409f2008-04-06 06:57:35 +00001317 SourceLocation DSStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00001318
Chris Lattnerf97409f2008-04-06 06:57:35 +00001319 // Parse the declaration-specifiers.
1320 DeclSpec DS;
1321 ParseDeclarationSpecifiers(DS);
1322
1323 // Parse the declarator. This is "PrototypeContext", because we must
1324 // accept either 'declarator' or 'abstract-declarator' here.
1325 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1326 ParseDeclarator(ParmDecl);
1327
1328 // Parse GNU attributes, if present.
1329 if (Tok.is(tok::kw___attribute))
1330 ParmDecl.AddAttributes(ParseAttributes());
1331
Chris Lattnerf97409f2008-04-06 06:57:35 +00001332 // Remember this parsed parameter in ParamInfo.
1333 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1334
Chris Lattnerf97409f2008-04-06 06:57:35 +00001335 // If no parameter was specified, verify that *something* was specified,
1336 // otherwise we have a missing type and identifier.
1337 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1338 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1339 // Completely missing, emit error.
1340 Diag(DSStart, diag::err_missing_param);
1341 } else {
1342 // Otherwise, we have something. Add it and let semantic analysis try
1343 // to grok it and add the result to the ParamInfo we are building.
1344
1345 // Inform the actions module about the parameter declarator, so it gets
1346 // added to the current scope.
Chris Lattner04421082008-04-08 04:40:51 +00001347 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1348
1349 // Parse the default argument, if any. We parse the default
1350 // arguments in all dialects; the semantic analysis in
1351 // ActOnParamDefaultArgument will reject the default argument in
1352 // C.
1353 if (Tok.is(tok::equal)) {
1354 SourceLocation EqualLoc = Tok.getLocation();
1355
1356 // Consume the '='.
1357 ConsumeToken();
1358
1359 // Parse the default argument
Chris Lattner04421082008-04-08 04:40:51 +00001360 ExprResult DefArgResult = ParseAssignmentExpression();
1361 if (DefArgResult.isInvalid) {
1362 SkipUntil(tok::comma, tok::r_paren, true, true);
1363 } else {
1364 // Inform the actions module about the default argument
1365 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1366 }
1367 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00001368
1369 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner04421082008-04-08 04:40:51 +00001370 ParmDecl.getIdentifierLoc(), Param));
Chris Lattnerf97409f2008-04-06 06:57:35 +00001371 }
1372
1373 // If the next token is a comma, consume it and keep reading arguments.
1374 if (Tok.isNot(tok::comma)) break;
1375
1376 // Consume the comma.
1377 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001378 }
1379
Chris Lattnerf97409f2008-04-06 06:57:35 +00001380 // Leave prototype scope.
1381 ExitScope();
1382
Reid Spencer5f016e22007-07-11 17:01:13 +00001383 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00001384 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1385 &ParamInfo[0], ParamInfo.size(),
1386 LParenLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001387
1388 // If we have the closing ')', eat it and we're done.
Chris Lattnerf97409f2008-04-06 06:57:35 +00001389 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001390}
1391
Chris Lattner66d28652008-04-06 06:34:08 +00001392/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1393/// we found a K&R-style identifier list instead of a type argument list. The
1394/// current token is known to be the first identifier in the list.
1395///
1396/// identifier-list: [C99 6.7.5]
1397/// identifier
1398/// identifier-list ',' identifier
1399///
1400void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1401 Declarator &D) {
1402 // Build up an array of information about the parsed arguments.
1403 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1404 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1405
1406 // If there was no identifier specified for the declarator, either we are in
1407 // an abstract-declarator, or we are in a parameter declarator which was found
1408 // to be abstract. In abstract-declarators, identifier lists are not valid:
1409 // diagnose this.
1410 if (!D.getIdentifier())
1411 Diag(Tok, diag::ext_ident_list_in_param);
1412
1413 // Tok is known to be the first identifier in the list. Remember this
1414 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00001415 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00001416 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1417 Tok.getLocation(), 0));
1418
Chris Lattner50c64772008-04-06 06:39:19 +00001419 ConsumeToken(); // eat the first identifier.
Chris Lattner66d28652008-04-06 06:34:08 +00001420
1421 while (Tok.is(tok::comma)) {
1422 // Eat the comma.
1423 ConsumeToken();
1424
Chris Lattner50c64772008-04-06 06:39:19 +00001425 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00001426 if (Tok.isNot(tok::identifier)) {
1427 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00001428 SkipUntil(tok::r_paren);
1429 return;
Chris Lattner66d28652008-04-06 06:34:08 +00001430 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00001431
Chris Lattner66d28652008-04-06 06:34:08 +00001432 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00001433
1434 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1435 if (Actions.isTypeName(*ParmII, CurScope))
1436 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner66d28652008-04-06 06:34:08 +00001437
1438 // Verify that the argument identifier has not already been mentioned.
1439 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner50c64772008-04-06 06:39:19 +00001440 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1441 } else {
1442 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00001443 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1444 Tok.getLocation(), 0));
Chris Lattner50c64772008-04-06 06:39:19 +00001445 }
Chris Lattner66d28652008-04-06 06:34:08 +00001446
1447 // Eat the identifier.
1448 ConsumeToken();
1449 }
1450
Chris Lattner50c64772008-04-06 06:39:19 +00001451 // Remember that we parsed a function type, and remember the attributes. This
1452 // function type is always a K&R style function type, which is not varargs and
1453 // has no prototype.
1454 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1455 &ParamInfo[0], ParamInfo.size(),
1456 LParenLoc));
Chris Lattner66d28652008-04-06 06:34:08 +00001457
1458 // If we have the closing ')', eat it and we're done.
Chris Lattner50c64772008-04-06 06:39:19 +00001459 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00001460}
Chris Lattneref4715c2008-04-06 05:45:57 +00001461
Reid Spencer5f016e22007-07-11 17:01:13 +00001462/// [C90] direct-declarator '[' constant-expression[opt] ']'
1463/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1464/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1465/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1466/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1467void Parser::ParseBracketDeclarator(Declarator &D) {
1468 SourceLocation StartLoc = ConsumeBracket();
1469
1470 // If valid, this location is the position where we read the 'static' keyword.
1471 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00001472 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00001473 StaticLoc = ConsumeToken();
1474
1475 // If there is a type-qualifier-list, read it now.
1476 DeclSpec DS;
1477 ParseTypeQualifierListOpt(DS);
1478
1479 // If we haven't already read 'static', check to see if there is one after the
1480 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001481 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00001482 StaticLoc = ConsumeToken();
1483
1484 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1485 bool isStar = false;
1486 ExprResult NumElements(false);
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00001487
1488 // Handle the case where we have '[*]' as the array size. However, a leading
1489 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1490 // the the token after the star is a ']'. Since stars in arrays are
1491 // infrequent, use of lookahead is not costly here.
1492 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00001493 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001494
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00001495 if (StaticLoc.isValid())
1496 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1497 StaticLoc = SourceLocation(); // Drop the static.
1498 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00001499 } else if (Tok.isNot(tok::r_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001500 // Parse the assignment-expression now.
1501 NumElements = ParseAssignmentExpression();
1502 }
1503
1504 // If there was an error parsing the assignment-expression, recover.
1505 if (NumElements.isInvalid) {
1506 // If the expression was invalid, skip it.
1507 SkipUntil(tok::r_square);
1508 return;
1509 }
1510
1511 MatchRHSPunctuation(tok::r_square, StartLoc);
1512
1513 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1514 // it was not a constant expression.
1515 if (!getLang().C99) {
1516 // TODO: check C90 array constant exprness.
1517 if (isStar || StaticLoc.isValid() ||
1518 0/*TODO: NumElts is not a C90 constantexpr */)
1519 Diag(StartLoc, diag::ext_c99_array_usage);
1520 }
1521
1522 // Remember that we parsed a pointer type, and remember the type-quals.
1523 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1524 StaticLoc.isValid(), isStar,
1525 NumElements.Val, StartLoc));
1526}
1527
Steve Naroffd1861fd2007-07-31 12:34:36 +00001528/// [GNU] typeof-specifier:
1529/// typeof ( expressions )
1530/// typeof ( type-name )
1531///
1532void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00001533 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001534 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffd1861fd2007-07-31 12:34:36 +00001535 SourceLocation StartLoc = ConsumeToken();
1536
Chris Lattner04d66662007-10-09 17:33:22 +00001537 if (Tok.isNot(tok::l_paren)) {
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001538 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1539 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001540 }
1541 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1542
1543 if (isTypeSpecifierQualifier()) {
1544 TypeTy *Ty = ParseTypeName();
1545
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001546 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1547
Chris Lattner04d66662007-10-09 17:33:22 +00001548 if (Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001549 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001550 return;
1551 }
1552 RParenLoc = ConsumeParen();
1553 const char *PrevSpec = 0;
1554 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1555 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1556 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001557 } else { // we have an expression.
1558 ExprResult Result = ParseExpression();
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001559
Chris Lattner04d66662007-10-09 17:33:22 +00001560 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001561 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001562 return;
1563 }
1564 RParenLoc = ConsumeParen();
1565 const char *PrevSpec = 0;
1566 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1567 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1568 Result.Val))
1569 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001570 }
Steve Naroffd1861fd2007-07-31 12:34:36 +00001571}
1572
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00001573