blob: 98c2d897d5d305792d5ff0bdf3ac87508506fde4 [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()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000399 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 // If this is not a declaration specifier token, we're done reading decl
401 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000402 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +0000403 return;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000404
405 // typedef-name
406 case tok::identifier: {
407 // This identifier can only be a typedef name if we haven't already seen
408 // a type-specifier. Without this check we misparse:
409 // typedef int X; struct Y { short X; }; as 'short int'.
410 if (DS.hasTypeSpecifier())
411 goto DoneWithDeclSpec;
412
413 // It has to be available as a typedef too!
414 void *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
415 if (TypeRep == 0)
416 goto DoneWithDeclSpec;
417
418 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
419 TypeRep);
420 if (isInvalid)
421 break;
422
423 DS.SetRangeEnd(Tok.getLocation());
424 ConsumeToken(); // The identifier
425
426 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
427 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
428 // Objective-C interface. If we don't have Objective-C or a '<', this is
429 // just a normal reference to a typedef name.
430 if (!Tok.is(tok::less) || !getLang().ObjC1)
431 continue;
432
433 SourceLocation EndProtoLoc;
434 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
435 ParseObjCProtocolReferences(ProtocolRefs, EndProtoLoc);
436
437 // FIXME: New'ing this here seems wrong, why not have the action do it?
438 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
439 new llvm::SmallVector<DeclTy *, 8>;
440 DS.setProtocolQualifiers(ProtocolDecl);
441 Actions.FindProtocolDeclaration(Loc,
442 &ProtocolRefs[0], ProtocolRefs.size(),
443 *ProtocolDecl);
444
445 DS.SetRangeEnd(EndProtoLoc);
446
447 // Do not allow any other declspecs after the protocol qualifier list
448 // "<foo,bar>short" is not allowed.
449 goto DoneWithDeclSpec;
450 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000451 // GNU attributes support.
452 case tok::kw___attribute:
453 DS.AddAttributes(ParseAttributes());
454 continue;
455
456 // storage-class-specifier
457 case tok::kw_typedef:
458 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
459 break;
460 case tok::kw_extern:
461 if (DS.isThreadSpecified())
462 Diag(Tok, diag::ext_thread_before, "extern");
463 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
464 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000465 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000466 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
467 PrevSpec);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000468 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 case tok::kw_static:
470 if (DS.isThreadSpecified())
471 Diag(Tok, diag::ext_thread_before, "static");
472 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
473 break;
474 case tok::kw_auto:
475 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
476 break;
477 case tok::kw_register:
478 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
479 break;
480 case tok::kw___thread:
481 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
482 break;
483
484 // type-specifiers
485 case tok::kw_short:
486 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
487 break;
488 case tok::kw_long:
489 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
490 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
491 else
492 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
493 break;
494 case tok::kw_signed:
495 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
496 break;
497 case tok::kw_unsigned:
498 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
499 break;
500 case tok::kw__Complex:
501 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
502 break;
503 case tok::kw__Imaginary:
504 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
505 break;
506 case tok::kw_void:
507 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
508 break;
509 case tok::kw_char:
510 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
511 break;
512 case tok::kw_int:
513 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
514 break;
515 case tok::kw_float:
516 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
517 break;
518 case tok::kw_double:
519 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
520 break;
521 case tok::kw_bool: // [C++ 2.11p1]
522 case tok::kw__Bool:
523 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
524 break;
525 case tok::kw__Decimal32:
526 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
527 break;
528 case tok::kw__Decimal64:
529 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
530 break;
531 case tok::kw__Decimal128:
532 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
533 break;
Chris Lattner99dc9142008-04-13 18:59:07 +0000534
535 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000536 case tok::kw_struct:
537 case tok::kw_union:
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000538 ParseClassSpecifier(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +0000539 continue;
540 case tok::kw_enum:
541 ParseEnumSpecifier(DS);
542 continue;
543
Steve Naroffd1861fd2007-07-31 12:34:36 +0000544 // GNU typeof support.
545 case tok::kw_typeof:
546 ParseTypeofSpecifier(DS);
547 continue;
548
Reid Spencer5f016e22007-07-11 17:01:13 +0000549 // type-qualifier
550 case tok::kw_const:
551 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
552 getLang())*2;
553 break;
554 case tok::kw_volatile:
555 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
556 getLang())*2;
557 break;
558 case tok::kw_restrict:
559 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
560 getLang())*2;
561 break;
562
563 // function-specifier
564 case tok::kw_inline:
565 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
566 break;
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000567
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000568 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +0000569 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +0000570 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
571 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +0000572 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +0000573 goto DoneWithDeclSpec;
574
575 {
576 SourceLocation EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000577 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerbce61352008-07-26 00:20:22 +0000578 ParseObjCProtocolReferences(ProtocolRefs, EndProtoLoc);
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000579 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
580 new llvm::SmallVector<DeclTy *, 8>;
581 DS.setProtocolQualifiers(ProtocolDecl);
582 Actions.FindProtocolDeclaration(Loc,
Chris Lattnerbce61352008-07-26 00:20:22 +0000583 &ProtocolRefs[0], ProtocolRefs.size(),
584 *ProtocolDecl);
Chris Lattner3bd934a2008-07-26 01:18:38 +0000585 DS.SetRangeEnd(EndProtoLoc);
586
Chris Lattnerbce61352008-07-26 00:20:22 +0000587 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id,
588 SourceRange(Loc, EndProtoLoc));
Chris Lattner3bd934a2008-07-26 01:18:38 +0000589 // Do not allow any other declspecs after the protocol qualifier list
590 // "<foo,bar>short" is not allowed.
591 goto DoneWithDeclSpec;
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000592 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000593 }
594 // If the specifier combination wasn't legal, issue a diagnostic.
595 if (isInvalid) {
596 assert(PrevSpec && "Method did not return previous specifier!");
597 if (isInvalid == 1) // Error.
598 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
599 else // extwarn.
600 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
601 }
Chris Lattner81c018d2008-03-13 06:29:04 +0000602 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 ConsumeToken();
604 }
605}
606
607/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
608/// the first token has already been read and has been turned into an instance
609/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
610/// otherwise it returns false and fills in Decl.
611bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
612 AttributeList *Attr = 0;
613 // If attributes exist after tag, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000614 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 Attr = ParseAttributes();
616
617 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner04d66662007-10-09 17:33:22 +0000618 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 Diag(Tok, diag::err_expected_ident_lbrace);
Chris Lattnere80a59c2007-07-25 00:24:17 +0000620
621 // Skip the rest of this declarator, up until the comma or semicolon.
622 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 return true;
624 }
625
626 // If an identifier is present, consume and remember it.
627 IdentifierInfo *Name = 0;
628 SourceLocation NameLoc;
Chris Lattner04d66662007-10-09 17:33:22 +0000629 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000630 Name = Tok.getIdentifierInfo();
631 NameLoc = ConsumeToken();
632 }
633
634 // There are three options here. If we have 'struct foo;', then this is a
635 // forward declaration. If we have 'struct foo {...' then this is a
636 // definition. Otherwise we have something like 'struct foo xyz', a reference.
637 //
638 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
639 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
640 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
641 //
642 Action::TagKind TK;
Chris Lattner04d66662007-10-09 17:33:22 +0000643 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 TK = Action::TK_Definition;
Chris Lattner04d66662007-10-09 17:33:22 +0000645 else if (Tok.is(tok::semi))
Reid Spencer5f016e22007-07-11 17:01:13 +0000646 TK = Action::TK_Declaration;
647 else
648 TK = Action::TK_Reference;
Steve Naroff08d92e42007-09-15 18:49:24 +0000649 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 return false;
651}
652
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000653/// ParseStructDeclaration - Parse a struct declaration without the terminating
654/// semicolon.
655///
Reid Spencer5f016e22007-07-11 17:01:13 +0000656/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000657/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000658/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000659/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000660/// struct-declarator-list:
661/// struct-declarator
662/// struct-declarator-list ',' struct-declarator
663/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
664/// struct-declarator:
665/// declarator
666/// [GNU] declarator attributes[opt]
667/// declarator[opt] ':' constant-expression
668/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
669///
Chris Lattnere1359422008-04-10 06:46:29 +0000670void Parser::
671ParseStructDeclaration(DeclSpec &DS,
672 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Steve Naroff28a7ca82007-08-20 22:28:22 +0000673 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattnere1359422008-04-10 06:46:29 +0000674 while (Tok.is(tok::kw___extension__))
Steve Naroff28a7ca82007-08-20 22:28:22 +0000675 ConsumeToken();
676
677 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000678 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +0000679 ParseSpecifierQualifierList(DS);
680 // TODO: Does specifier-qualifier list correctly check that *something* is
681 // specified?
682
683 // If there are no declarators, issue a warning.
Chris Lattner04d66662007-10-09 17:33:22 +0000684 if (Tok.is(tok::semi)) {
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000685 Diag(DSStart, diag::w_no_declarators);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000686 return;
687 }
688
689 // Read struct-declarators until we find the semicolon.
Chris Lattnerebe457c2008-04-10 16:37:40 +0000690 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +0000691 while (1) {
Chris Lattnere1359422008-04-10 06:46:29 +0000692 FieldDeclarator &DeclaratorInfo = Fields.back();
693
Steve Naroff28a7ca82007-08-20 22:28:22 +0000694 /// struct-declarator: declarator
695 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +0000696 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +0000697 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000698
Chris Lattner04d66662007-10-09 17:33:22 +0000699 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +0000700 ConsumeToken();
701 ExprResult Res = ParseConstantExpression();
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000702 if (Res.isInvalid)
Steve Naroff28a7ca82007-08-20 22:28:22 +0000703 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000704 else
Chris Lattnere1359422008-04-10 06:46:29 +0000705 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroff28a7ca82007-08-20 22:28:22 +0000706 }
707
708 // If attributes exist after the declarator, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000709 if (Tok.is(tok::kw___attribute))
Chris Lattnere1359422008-04-10 06:46:29 +0000710 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroff28a7ca82007-08-20 22:28:22 +0000711
712 // If we don't have a comma, it is either the end of the list (a ';')
713 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000714 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000715 return;
Steve Naroff28a7ca82007-08-20 22:28:22 +0000716
717 // Consume the comma.
718 ConsumeToken();
719
720 // Parse the next declarator.
Chris Lattnerebe457c2008-04-10 16:37:40 +0000721 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +0000722
723 // Attributes are only allowed on the second declarator.
Chris Lattner04d66662007-10-09 17:33:22 +0000724 if (Tok.is(tok::kw___attribute))
Chris Lattnere1359422008-04-10 06:46:29 +0000725 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroff28a7ca82007-08-20 22:28:22 +0000726 }
Steve Naroff28a7ca82007-08-20 22:28:22 +0000727}
728
729/// ParseStructUnionBody
730/// struct-contents:
731/// struct-declaration-list
732/// [EXT] empty
733/// [GNU] "struct-declaration-list" without terminatoring ';'
734/// struct-declaration-list:
735/// struct-declaration
736/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +0000737/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +0000738///
Reid Spencer5f016e22007-07-11 17:01:13 +0000739void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
740 unsigned TagType, DeclTy *TagDecl) {
741 SourceLocation LBraceLoc = ConsumeBrace();
742
743 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
744 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000745 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Reid Spencer5f016e22007-07-11 17:01:13 +0000746 Diag(Tok, diag::ext_empty_struct_union_enum,
747 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
748
749 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000750 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
751
Reid Spencer5f016e22007-07-11 17:01:13 +0000752 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +0000753 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000754 // Each iteration of this loop reads one struct-declaration.
755
756 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +0000757 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000758 Diag(Tok, diag::ext_extra_struct_semi);
759 ConsumeToken();
760 continue;
761 }
Chris Lattnere1359422008-04-10 06:46:29 +0000762
763 // Parse all the comma separated declarators.
764 DeclSpec DS;
765 FieldDeclarators.clear();
Chris Lattner5a6ddbf2008-06-21 19:39:06 +0000766 if (!Tok.is(tok::at)) {
767 ParseStructDeclaration(DS, FieldDeclarators);
768
769 // Convert them all to fields.
770 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
771 FieldDeclarator &FD = FieldDeclarators[i];
772 // Install the declarator into the current TagDecl.
773 DeclTy *Field = Actions.ActOnField(CurScope,
774 DS.getSourceRange().getBegin(),
775 FD.D, FD.BitfieldSize);
776 FieldDecls.push_back(Field);
777 }
778 } else { // Handle @defs
779 ConsumeToken();
780 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
781 Diag(Tok, diag::err_unexpected_at);
782 SkipUntil(tok::semi, true, true);
783 continue;
784 }
785 ConsumeToken();
786 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
787 if (!Tok.is(tok::identifier)) {
788 Diag(Tok, diag::err_expected_ident);
789 SkipUntil(tok::semi, true, true);
790 continue;
791 }
792 llvm::SmallVector<DeclTy*, 16> Fields;
793 Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
794 Fields);
795 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
796 ConsumeToken();
797 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
798 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000799
Chris Lattner04d66662007-10-09 17:33:22 +0000800 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000801 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +0000802 } else if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
804 break;
805 } else {
806 Diag(Tok, diag::err_expected_semi_decl_list);
807 // Skip to end of block or statement
808 SkipUntil(tok::r_brace, true, true);
809 }
810 }
811
Steve Naroff60fccee2007-10-29 21:38:07 +0000812 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000813
Fariborz Jahanian9d048ff2007-09-29 00:54:24 +0000814 Actions.ActOnFields(CurScope,
Chris Lattnerc81c8142008-02-25 21:04:36 +0000815 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
Steve Naroff60fccee2007-10-29 21:38:07 +0000816 LBraceLoc, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000817
818 AttributeList *AttrList = 0;
819 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000820 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000821 AttrList = ParseAttributes(); // FIXME: where should I put them?
822}
823
824
825/// ParseEnumSpecifier
826/// enum-specifier: [C99 6.7.2.2]
827/// 'enum' identifier[opt] '{' enumerator-list '}'
828/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
829/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
830/// '}' attributes[opt]
831/// 'enum' identifier
832/// [GNU] 'enum' attributes[opt] identifier
833void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +0000834 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Reid Spencer5f016e22007-07-11 17:01:13 +0000835 SourceLocation StartLoc = ConsumeToken();
836
837 // Parse the tag portion of this.
838 DeclTy *TagDecl;
839 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
840 return;
841
Chris Lattner04d66662007-10-09 17:33:22 +0000842 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +0000843 ParseEnumBody(StartLoc, TagDecl);
844
845 // TODO: semantic analysis on the declspec for enums.
846 const char *PrevSpec = 0;
847 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
848 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
849}
850
851/// ParseEnumBody - Parse a {} enclosed enumerator-list.
852/// enumerator-list:
853/// enumerator
854/// enumerator-list ',' enumerator
855/// enumerator:
856/// enumeration-constant
857/// enumeration-constant '=' constant-expression
858/// enumeration-constant:
859/// identifier
860///
861void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
862 SourceLocation LBraceLoc = ConsumeBrace();
863
Chris Lattner7946dd32007-08-27 17:24:30 +0000864 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +0000865 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Reid Spencer5f016e22007-07-11 17:01:13 +0000866 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
867
868 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
869
870 DeclTy *LastEnumConstDecl = 0;
871
872 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +0000873 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000874 IdentifierInfo *Ident = Tok.getIdentifierInfo();
875 SourceLocation IdentLoc = ConsumeToken();
876
877 SourceLocation EqualLoc;
878 ExprTy *AssignedVal = 0;
Chris Lattner04d66662007-10-09 17:33:22 +0000879 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000880 EqualLoc = ConsumeToken();
881 ExprResult Res = ParseConstantExpression();
882 if (Res.isInvalid)
883 SkipUntil(tok::comma, tok::r_brace, true, true);
884 else
885 AssignedVal = Res.Val;
886 }
887
888 // Install the enumerator constant into EnumDecl.
Steve Naroff08d92e42007-09-15 18:49:24 +0000889 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +0000890 LastEnumConstDecl,
891 IdentLoc, Ident,
892 EqualLoc, AssignedVal);
893 EnumConstantDecls.push_back(EnumConstDecl);
894 LastEnumConstDecl = EnumConstDecl;
895
Chris Lattner04d66662007-10-09 17:33:22 +0000896 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000897 break;
898 SourceLocation CommaLoc = ConsumeToken();
899
Chris Lattner04d66662007-10-09 17:33:22 +0000900 if (Tok.isNot(tok::identifier) && !getLang().C99)
Reid Spencer5f016e22007-07-11 17:01:13 +0000901 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
902 }
903
904 // Eat the }.
905 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
906
Steve Naroff08d92e42007-09-15 18:49:24 +0000907 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Reid Spencer5f016e22007-07-11 17:01:13 +0000908 EnumConstantDecls.size());
909
910 DeclTy *AttrList = 0;
911 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000912 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000913 AttrList = ParseAttributes(); // FIXME: where do they do?
914}
915
916/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +0000917/// start of a type-qualifier-list.
918bool Parser::isTypeQualifier() const {
919 switch (Tok.getKind()) {
920 default: return false;
921 // type-qualifier
922 case tok::kw_const:
923 case tok::kw_volatile:
924 case tok::kw_restrict:
925 return true;
926 }
927}
928
929/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +0000930/// start of a specifier-qualifier-list.
931bool Parser::isTypeSpecifierQualifier() const {
932 switch (Tok.getKind()) {
933 default: return false;
934 // GNU attributes support.
935 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +0000936 // GNU typeof support.
937 case tok::kw_typeof:
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000938 // GNU bizarre protocol extension. FIXME: make an extension?
939 case tok::less:
Steve Naroffd1861fd2007-07-31 12:34:36 +0000940
Reid Spencer5f016e22007-07-11 17:01:13 +0000941 // type-specifiers
942 case tok::kw_short:
943 case tok::kw_long:
944 case tok::kw_signed:
945 case tok::kw_unsigned:
946 case tok::kw__Complex:
947 case tok::kw__Imaginary:
948 case tok::kw_void:
949 case tok::kw_char:
950 case tok::kw_int:
951 case tok::kw_float:
952 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +0000953 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +0000954 case tok::kw__Bool:
955 case tok::kw__Decimal32:
956 case tok::kw__Decimal64:
957 case tok::kw__Decimal128:
958
Chris Lattner99dc9142008-04-13 18:59:07 +0000959 // struct-or-union-specifier (C99) or class-specifier (C++)
960 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000961 case tok::kw_struct:
962 case tok::kw_union:
963 // enum-specifier
964 case tok::kw_enum:
965
966 // type-qualifier
967 case tok::kw_const:
968 case tok::kw_volatile:
969 case tok::kw_restrict:
970 return true;
971
972 // typedef-name
973 case tok::identifier:
974 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000975 }
976}
977
978/// isDeclarationSpecifier() - Return true if the current token is part of a
979/// declaration specifier.
980bool Parser::isDeclarationSpecifier() const {
981 switch (Tok.getKind()) {
982 default: return false;
983 // storage-class-specifier
984 case tok::kw_typedef:
985 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +0000986 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +0000987 case tok::kw_static:
988 case tok::kw_auto:
989 case tok::kw_register:
990 case tok::kw___thread:
991
992 // type-specifiers
993 case tok::kw_short:
994 case tok::kw_long:
995 case tok::kw_signed:
996 case tok::kw_unsigned:
997 case tok::kw__Complex:
998 case tok::kw__Imaginary:
999 case tok::kw_void:
1000 case tok::kw_char:
1001 case tok::kw_int:
1002 case tok::kw_float:
1003 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001004 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001005 case tok::kw__Bool:
1006 case tok::kw__Decimal32:
1007 case tok::kw__Decimal64:
1008 case tok::kw__Decimal128:
1009
Chris Lattner99dc9142008-04-13 18:59:07 +00001010 // struct-or-union-specifier (C99) or class-specifier (C++)
1011 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001012 case tok::kw_struct:
1013 case tok::kw_union:
1014 // enum-specifier
1015 case tok::kw_enum:
1016
1017 // type-qualifier
1018 case tok::kw_const:
1019 case tok::kw_volatile:
1020 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001021
Reid Spencer5f016e22007-07-11 17:01:13 +00001022 // function-specifier
1023 case tok::kw_inline:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001024
Chris Lattner1ef08762007-08-09 17:01:07 +00001025 // GNU typeof support.
1026 case tok::kw_typeof:
1027
1028 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001029 case tok::kw___attribute:
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001030
1031 // GNU bizarre protocol extension. FIXME: make an extension?
1032 case tok::less:
Reid Spencer5f016e22007-07-11 17:01:13 +00001033 return true;
1034
1035 // typedef-name
1036 case tok::identifier:
1037 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001038 }
1039}
1040
1041
1042/// ParseTypeQualifierListOpt
1043/// type-qualifier-list: [C99 6.7.5]
1044/// type-qualifier
1045/// [GNU] attributes
1046/// type-qualifier-list type-qualifier
1047/// [GNU] type-qualifier-list attributes
1048///
1049void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1050 while (1) {
1051 int isInvalid = false;
1052 const char *PrevSpec = 0;
1053 SourceLocation Loc = Tok.getLocation();
1054
1055 switch (Tok.getKind()) {
1056 default:
1057 // If this is not a type-qualifier token, we're done reading type
1058 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +00001059 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001060 return;
1061 case tok::kw_const:
1062 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1063 getLang())*2;
1064 break;
1065 case tok::kw_volatile:
1066 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1067 getLang())*2;
1068 break;
1069 case tok::kw_restrict:
1070 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1071 getLang())*2;
1072 break;
1073 case tok::kw___attribute:
1074 DS.AddAttributes(ParseAttributes());
1075 continue; // do *not* consume the next token!
1076 }
1077
1078 // If the specifier combination wasn't legal, issue a diagnostic.
1079 if (isInvalid) {
1080 assert(PrevSpec && "Method did not return previous specifier!");
1081 if (isInvalid == 1) // Error.
1082 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1083 else // extwarn.
1084 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1085 }
1086 ConsumeToken();
1087 }
1088}
1089
1090
1091/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1092///
1093void Parser::ParseDeclarator(Declarator &D) {
1094 /// This implements the 'declarator' production in the C grammar, then checks
1095 /// for well-formedness and issues diagnostics.
1096 ParseDeclaratorInternal(D);
Reid Spencer5f016e22007-07-11 17:01:13 +00001097}
1098
1099/// ParseDeclaratorInternal
1100/// declarator: [C99 6.7.5]
1101/// pointer[opt] direct-declarator
1102/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1103/// [GNU] '&' restrict[opt] attributes[opt] declarator
1104///
1105/// pointer: [C99 6.7.5]
1106/// '*' type-qualifier-list[opt]
1107/// '*' type-qualifier-list[opt] pointer
1108///
1109void Parser::ParseDeclaratorInternal(Declarator &D) {
1110 tok::TokenKind Kind = Tok.getKind();
1111
1112 // Not a pointer or C++ reference.
Chris Lattner76549142008-02-21 01:32:26 +00001113 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus))
Reid Spencer5f016e22007-07-11 17:01:13 +00001114 return ParseDirectDeclarator(D);
1115
1116 // Otherwise, '*' -> pointer or '&' -> reference.
1117 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1118
1119 if (Kind == tok::star) {
Chris Lattner76549142008-02-21 01:32:26 +00001120 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00001121 DeclSpec DS;
1122
1123 ParseTypeQualifierListOpt(DS);
1124
1125 // Recursively parse the declarator.
1126 ParseDeclaratorInternal(D);
1127
1128 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001129 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1130 DS.TakeAttributes()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 } else {
1132 // Is a reference
1133 DeclSpec DS;
1134
1135 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1136 // cv-qualifiers are introduced through the use of a typedef or of a
1137 // template type argument, in which case the cv-qualifiers are ignored.
1138 //
1139 // [GNU] Retricted references are allowed.
1140 // [GNU] Attributes on references are allowed.
1141 ParseTypeQualifierListOpt(DS);
1142
1143 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1144 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1145 Diag(DS.getConstSpecLoc(),
1146 diag::err_invalid_reference_qualifier_application,
1147 "const");
1148 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1149 Diag(DS.getVolatileSpecLoc(),
1150 diag::err_invalid_reference_qualifier_application,
1151 "volatile");
1152 }
1153
1154 // Recursively parse the declarator.
1155 ParseDeclaratorInternal(D);
1156
1157 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001158 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1159 DS.TakeAttributes()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001160 }
1161}
1162
1163/// ParseDirectDeclarator
1164/// direct-declarator: [C99 6.7.5]
1165/// identifier
1166/// '(' declarator ')'
1167/// [GNU] '(' attributes declarator ')'
1168/// [C90] direct-declarator '[' constant-expression[opt] ']'
1169/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1170/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1171/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1172/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1173/// direct-declarator '(' parameter-type-list ')'
1174/// direct-declarator '(' identifier-list[opt] ')'
1175/// [GNU] direct-declarator '(' parameter-forward-declarations
1176/// parameter-type-list[opt] ')'
1177///
1178void Parser::ParseDirectDeclarator(Declarator &D) {
1179 // Parse the first direct-declarator seen.
Chris Lattner04d66662007-10-09 17:33:22 +00001180 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1182 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1183 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001184 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001185 // direct-declarator: '(' declarator ')'
1186 // direct-declarator: '(' attributes declarator ')'
1187 // Example: 'char (*X)' or 'int (*XX)(void)'
1188 ParseParenDeclarator(D);
1189 } else if (D.mayOmitIdentifier()) {
1190 // This could be something simple like "int" (in which case the declarator
1191 // portion is empty), if an abstract-declarator is allowed.
1192 D.SetIdentifier(0, Tok.getLocation());
1193 } else {
1194 // Expected identifier or '('.
1195 Diag(Tok, diag::err_expected_ident_lparen);
1196 D.SetIdentifier(0, Tok.getLocation());
1197 }
1198
1199 assert(D.isPastIdentifier() &&
1200 "Haven't past the location of the identifier yet?");
1201
1202 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00001203 if (Tok.is(tok::l_paren)) {
Chris Lattneref4715c2008-04-06 05:45:57 +00001204 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00001205 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 ParseBracketDeclarator(D);
1207 } else {
1208 break;
1209 }
1210 }
1211}
1212
Chris Lattneref4715c2008-04-06 05:45:57 +00001213/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1214/// only called before the identifier, so these are most likely just grouping
1215/// parens for precedence. If we find that these are actually function
1216/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1217///
1218/// direct-declarator:
1219/// '(' declarator ')'
1220/// [GNU] '(' attributes declarator ')'
1221///
1222void Parser::ParseParenDeclarator(Declarator &D) {
1223 SourceLocation StartLoc = ConsumeParen();
1224 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1225
1226 // If we haven't past the identifier yet (or where the identifier would be
1227 // stored, if this is an abstract declarator), then this is probably just
1228 // grouping parens. However, if this could be an abstract-declarator, then
1229 // this could also be the start of function arguments (consider 'void()').
1230 bool isGrouping;
1231
1232 if (!D.mayOmitIdentifier()) {
1233 // If this can't be an abstract-declarator, this *must* be a grouping
1234 // paren, because we haven't seen the identifier yet.
1235 isGrouping = true;
1236 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
1237 isDeclarationSpecifier()) { // 'int(int)' is a function.
1238 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1239 // considered to be a type, not a K&R identifier-list.
1240 isGrouping = false;
1241 } else {
1242 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1243 isGrouping = true;
1244 }
1245
1246 // If this is a grouping paren, handle:
1247 // direct-declarator: '(' declarator ')'
1248 // direct-declarator: '(' attributes declarator ')'
1249 if (isGrouping) {
1250 if (Tok.is(tok::kw___attribute))
1251 D.AddAttributes(ParseAttributes());
1252
1253 ParseDeclaratorInternal(D);
1254 // Match the ')'.
1255 MatchRHSPunctuation(tok::r_paren, StartLoc);
1256 return;
1257 }
1258
1259 // Okay, if this wasn't a grouping paren, it must be the start of a function
1260 // argument list. Recognize that this declarator will never have an
1261 // identifier (and remember where it would have been), then fall through to
1262 // the handling of argument lists.
1263 D.SetIdentifier(0, Tok.getLocation());
1264
1265 ParseFunctionDeclarator(StartLoc, D);
1266}
1267
1268/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1269/// declarator D up to a paren, which indicates that we are parsing function
1270/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00001271///
1272/// This method also handles this portion of the grammar:
1273/// parameter-type-list: [C99 6.7.5]
1274/// parameter-list
1275/// parameter-list ',' '...'
1276///
1277/// parameter-list: [C99 6.7.5]
1278/// parameter-declaration
1279/// parameter-list ',' parameter-declaration
1280///
1281/// parameter-declaration: [C99 6.7.5]
1282/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00001283/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001284/// [GNU] declaration-specifiers declarator attributes
1285/// declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00001286/// [C++] declaration-specifiers abstract-declarator[opt]
1287/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001288/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1289///
Chris Lattneref4715c2008-04-06 05:45:57 +00001290void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D) {
1291 // lparen is already consumed!
1292 assert(D.isPastIdentifier() && "Should not call before identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001293
1294 // Okay, this is the parameter list of a function definition, or it is an
1295 // identifier list of a K&R-style function.
Reid Spencer5f016e22007-07-11 17:01:13 +00001296
Chris Lattner04d66662007-10-09 17:33:22 +00001297 if (Tok.is(tok::r_paren)) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00001298 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00001299 // int() -> no prototype, no '...'.
Chris Lattnerf97409f2008-04-06 06:57:35 +00001300 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/ false,
1301 /*variadic*/ false,
1302 /*arglist*/ 0, 0, LParenLoc));
1303
1304 ConsumeParen(); // Eat the closing ')'.
1305 return;
Chris Lattner04d66662007-10-09 17:33:22 +00001306 } else if (Tok.is(tok::identifier) &&
Reid Spencer5f016e22007-07-11 17:01:13 +00001307 // K&R identifier lists can't have typedefs as identifiers, per
1308 // C99 6.7.5.3p11.
1309 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1310 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1311 // normal declarators, not for abstract-declarators.
Chris Lattner66d28652008-04-06 06:34:08 +00001312 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattnerf97409f2008-04-06 06:57:35 +00001313 }
1314
1315 // Finally, a normal, non-empty parameter type list.
1316
1317 // Build up an array of information about the parsed arguments.
1318 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00001319
1320 // Enter function-declaration scope, limiting any declarators to the
1321 // function prototype scope, including parameter declarators.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00001322 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattnerf97409f2008-04-06 06:57:35 +00001323
1324 bool IsVariadic = false;
1325 while (1) {
1326 if (Tok.is(tok::ellipsis)) {
1327 IsVariadic = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001328
Chris Lattnerf97409f2008-04-06 06:57:35 +00001329 // Check to see if this is "void(...)" which is not allowed.
1330 if (ParamInfo.empty()) {
1331 // Otherwise, parse parameter type list. If it starts with an
1332 // ellipsis, diagnose the malformed function.
1333 Diag(Tok, diag::err_ellipsis_first_arg);
1334 IsVariadic = false; // Treat this like 'void()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 }
Chris Lattnere0e713b2008-01-31 06:10:07 +00001336
Chris Lattnerf97409f2008-04-06 06:57:35 +00001337 ConsumeToken(); // Consume the ellipsis.
1338 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001339 }
1340
Chris Lattnerf97409f2008-04-06 06:57:35 +00001341 SourceLocation DSStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00001342
Chris Lattnerf97409f2008-04-06 06:57:35 +00001343 // Parse the declaration-specifiers.
1344 DeclSpec DS;
1345 ParseDeclarationSpecifiers(DS);
1346
1347 // Parse the declarator. This is "PrototypeContext", because we must
1348 // accept either 'declarator' or 'abstract-declarator' here.
1349 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1350 ParseDeclarator(ParmDecl);
1351
1352 // Parse GNU attributes, if present.
1353 if (Tok.is(tok::kw___attribute))
1354 ParmDecl.AddAttributes(ParseAttributes());
1355
Chris Lattnerf97409f2008-04-06 06:57:35 +00001356 // Remember this parsed parameter in ParamInfo.
1357 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1358
Chris Lattnerf97409f2008-04-06 06:57:35 +00001359 // If no parameter was specified, verify that *something* was specified,
1360 // otherwise we have a missing type and identifier.
1361 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1362 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1363 // Completely missing, emit error.
1364 Diag(DSStart, diag::err_missing_param);
1365 } else {
1366 // Otherwise, we have something. Add it and let semantic analysis try
1367 // to grok it and add the result to the ParamInfo we are building.
1368
1369 // Inform the actions module about the parameter declarator, so it gets
1370 // added to the current scope.
Chris Lattner04421082008-04-08 04:40:51 +00001371 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1372
1373 // Parse the default argument, if any. We parse the default
1374 // arguments in all dialects; the semantic analysis in
1375 // ActOnParamDefaultArgument will reject the default argument in
1376 // C.
1377 if (Tok.is(tok::equal)) {
1378 SourceLocation EqualLoc = Tok.getLocation();
1379
1380 // Consume the '='.
1381 ConsumeToken();
1382
1383 // Parse the default argument
Chris Lattner04421082008-04-08 04:40:51 +00001384 ExprResult DefArgResult = ParseAssignmentExpression();
1385 if (DefArgResult.isInvalid) {
1386 SkipUntil(tok::comma, tok::r_paren, true, true);
1387 } else {
1388 // Inform the actions module about the default argument
1389 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1390 }
1391 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00001392
1393 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner04421082008-04-08 04:40:51 +00001394 ParmDecl.getIdentifierLoc(), Param));
Chris Lattnerf97409f2008-04-06 06:57:35 +00001395 }
1396
1397 // If the next token is a comma, consume it and keep reading arguments.
1398 if (Tok.isNot(tok::comma)) break;
1399
1400 // Consume the comma.
1401 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001402 }
1403
Chris Lattnerf97409f2008-04-06 06:57:35 +00001404 // Leave prototype scope.
1405 ExitScope();
1406
Reid Spencer5f016e22007-07-11 17:01:13 +00001407 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00001408 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1409 &ParamInfo[0], ParamInfo.size(),
1410 LParenLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001411
1412 // If we have the closing ')', eat it and we're done.
Chris Lattnerf97409f2008-04-06 06:57:35 +00001413 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001414}
1415
Chris Lattner66d28652008-04-06 06:34:08 +00001416/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1417/// we found a K&R-style identifier list instead of a type argument list. The
1418/// current token is known to be the first identifier in the list.
1419///
1420/// identifier-list: [C99 6.7.5]
1421/// identifier
1422/// identifier-list ',' identifier
1423///
1424void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1425 Declarator &D) {
1426 // Build up an array of information about the parsed arguments.
1427 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1428 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1429
1430 // If there was no identifier specified for the declarator, either we are in
1431 // an abstract-declarator, or we are in a parameter declarator which was found
1432 // to be abstract. In abstract-declarators, identifier lists are not valid:
1433 // diagnose this.
1434 if (!D.getIdentifier())
1435 Diag(Tok, diag::ext_ident_list_in_param);
1436
1437 // Tok is known to be the first identifier in the list. Remember this
1438 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00001439 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00001440 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1441 Tok.getLocation(), 0));
1442
Chris Lattner50c64772008-04-06 06:39:19 +00001443 ConsumeToken(); // eat the first identifier.
Chris Lattner66d28652008-04-06 06:34:08 +00001444
1445 while (Tok.is(tok::comma)) {
1446 // Eat the comma.
1447 ConsumeToken();
1448
Chris Lattner50c64772008-04-06 06:39:19 +00001449 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00001450 if (Tok.isNot(tok::identifier)) {
1451 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00001452 SkipUntil(tok::r_paren);
1453 return;
Chris Lattner66d28652008-04-06 06:34:08 +00001454 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00001455
Chris Lattner66d28652008-04-06 06:34:08 +00001456 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00001457
1458 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1459 if (Actions.isTypeName(*ParmII, CurScope))
1460 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner66d28652008-04-06 06:34:08 +00001461
1462 // Verify that the argument identifier has not already been mentioned.
1463 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner50c64772008-04-06 06:39:19 +00001464 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1465 } else {
1466 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00001467 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1468 Tok.getLocation(), 0));
Chris Lattner50c64772008-04-06 06:39:19 +00001469 }
Chris Lattner66d28652008-04-06 06:34:08 +00001470
1471 // Eat the identifier.
1472 ConsumeToken();
1473 }
1474
Chris Lattner50c64772008-04-06 06:39:19 +00001475 // Remember that we parsed a function type, and remember the attributes. This
1476 // function type is always a K&R style function type, which is not varargs and
1477 // has no prototype.
1478 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1479 &ParamInfo[0], ParamInfo.size(),
1480 LParenLoc));
Chris Lattner66d28652008-04-06 06:34:08 +00001481
1482 // If we have the closing ')', eat it and we're done.
Chris Lattner50c64772008-04-06 06:39:19 +00001483 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00001484}
Chris Lattneref4715c2008-04-06 05:45:57 +00001485
Reid Spencer5f016e22007-07-11 17:01:13 +00001486/// [C90] direct-declarator '[' constant-expression[opt] ']'
1487/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1488/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1489/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1490/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1491void Parser::ParseBracketDeclarator(Declarator &D) {
1492 SourceLocation StartLoc = ConsumeBracket();
1493
1494 // If valid, this location is the position where we read the 'static' keyword.
1495 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00001496 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00001497 StaticLoc = ConsumeToken();
1498
1499 // If there is a type-qualifier-list, read it now.
1500 DeclSpec DS;
1501 ParseTypeQualifierListOpt(DS);
1502
1503 // If we haven't already read 'static', check to see if there is one after the
1504 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001505 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00001506 StaticLoc = ConsumeToken();
1507
1508 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1509 bool isStar = false;
1510 ExprResult NumElements(false);
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00001511
1512 // Handle the case where we have '[*]' as the array size. However, a leading
1513 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1514 // the the token after the star is a ']'. Since stars in arrays are
1515 // infrequent, use of lookahead is not costly here.
1516 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00001517 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001518
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00001519 if (StaticLoc.isValid())
1520 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1521 StaticLoc = SourceLocation(); // Drop the static.
1522 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00001523 } else if (Tok.isNot(tok::r_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001524 // Parse the assignment-expression now.
1525 NumElements = ParseAssignmentExpression();
1526 }
1527
1528 // If there was an error parsing the assignment-expression, recover.
1529 if (NumElements.isInvalid) {
1530 // If the expression was invalid, skip it.
1531 SkipUntil(tok::r_square);
1532 return;
1533 }
1534
1535 MatchRHSPunctuation(tok::r_square, StartLoc);
1536
1537 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1538 // it was not a constant expression.
1539 if (!getLang().C99) {
1540 // TODO: check C90 array constant exprness.
1541 if (isStar || StaticLoc.isValid() ||
1542 0/*TODO: NumElts is not a C90 constantexpr */)
1543 Diag(StartLoc, diag::ext_c99_array_usage);
1544 }
1545
1546 // Remember that we parsed a pointer type, and remember the type-quals.
1547 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1548 StaticLoc.isValid(), isStar,
1549 NumElements.Val, StartLoc));
1550}
1551
Steve Naroffd1861fd2007-07-31 12:34:36 +00001552/// [GNU] typeof-specifier:
1553/// typeof ( expressions )
1554/// typeof ( type-name )
1555///
1556void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00001557 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001558 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffd1861fd2007-07-31 12:34:36 +00001559 SourceLocation StartLoc = ConsumeToken();
1560
Chris Lattner04d66662007-10-09 17:33:22 +00001561 if (Tok.isNot(tok::l_paren)) {
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001562 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1563 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001564 }
1565 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1566
1567 if (isTypeSpecifierQualifier()) {
1568 TypeTy *Ty = ParseTypeName();
1569
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001570 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1571
Chris Lattner04d66662007-10-09 17:33:22 +00001572 if (Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001573 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001574 return;
1575 }
1576 RParenLoc = ConsumeParen();
1577 const char *PrevSpec = 0;
1578 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1579 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1580 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001581 } else { // we have an expression.
1582 ExprResult Result = ParseExpression();
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001583
Chris Lattner04d66662007-10-09 17:33:22 +00001584 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001585 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001586 return;
1587 }
1588 RParenLoc = ConsumeParen();
1589 const char *PrevSpec = 0;
1590 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1591 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1592 Result.Val))
1593 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001594 }
Steve Naroffd1861fd2007-07-31 12:34:36 +00001595}
1596
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00001597