blob: 77287043a53c6b79e8490331c6ba49cfeb8b7787 [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();
326 if (Specs == DeclSpec::PQ_None)
327 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;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000416 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofff908a872007-10-30 02:23:23 +0000417 ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000418 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
419 new llvm::SmallVector<DeclTy *, 8>;
420 DS.setProtocolQualifiers(ProtocolDecl);
421 Actions.FindProtocolDeclaration(Loc,
422 &ProtocolRefs[0], ProtocolRefs.size(),
423 *ProtocolDecl);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000424 }
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000425 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000426 }
427 }
428 // FALL THROUGH.
429 default:
430 // If this is not a declaration specifier token, we're done reading decl
431 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000432 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +0000433 return;
434
435 // GNU attributes support.
436 case tok::kw___attribute:
437 DS.AddAttributes(ParseAttributes());
438 continue;
439
440 // storage-class-specifier
441 case tok::kw_typedef:
442 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
443 break;
444 case tok::kw_extern:
445 if (DS.isThreadSpecified())
446 Diag(Tok, diag::ext_thread_before, "extern");
447 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
448 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000449 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000450 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
451 PrevSpec);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000452 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000453 case tok::kw_static:
454 if (DS.isThreadSpecified())
455 Diag(Tok, diag::ext_thread_before, "static");
456 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
457 break;
458 case tok::kw_auto:
459 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
460 break;
461 case tok::kw_register:
462 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
463 break;
464 case tok::kw___thread:
465 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
466 break;
467
468 // type-specifiers
469 case tok::kw_short:
470 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
471 break;
472 case tok::kw_long:
473 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
474 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
475 else
476 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
477 break;
478 case tok::kw_signed:
479 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
480 break;
481 case tok::kw_unsigned:
482 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
483 break;
484 case tok::kw__Complex:
485 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
486 break;
487 case tok::kw__Imaginary:
488 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
489 break;
490 case tok::kw_void:
491 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
492 break;
493 case tok::kw_char:
494 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
495 break;
496 case tok::kw_int:
497 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
498 break;
499 case tok::kw_float:
500 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
501 break;
502 case tok::kw_double:
503 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
504 break;
505 case tok::kw_bool: // [C++ 2.11p1]
506 case tok::kw__Bool:
507 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
508 break;
509 case tok::kw__Decimal32:
510 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
511 break;
512 case tok::kw__Decimal64:
513 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
514 break;
515 case tok::kw__Decimal128:
516 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
517 break;
518
519 case tok::kw_struct:
520 case tok::kw_union:
521 ParseStructUnionSpecifier(DS);
522 continue;
523 case tok::kw_enum:
524 ParseEnumSpecifier(DS);
525 continue;
526
Steve Naroffd1861fd2007-07-31 12:34:36 +0000527 // GNU typeof support.
528 case tok::kw_typeof:
529 ParseTypeofSpecifier(DS);
530 continue;
531
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 // type-qualifier
533 case tok::kw_const:
534 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
535 getLang())*2;
536 break;
537 case tok::kw_volatile:
538 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
539 getLang())*2;
540 break;
541 case tok::kw_restrict:
542 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
543 getLang())*2;
544 break;
545
546 // function-specifier
547 case tok::kw_inline:
548 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
549 break;
550 }
551 // If the specifier combination wasn't legal, issue a diagnostic.
552 if (isInvalid) {
553 assert(PrevSpec && "Method did not return previous specifier!");
554 if (isInvalid == 1) // Error.
555 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
556 else // extwarn.
557 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
558 }
Chris Lattner81c018d2008-03-13 06:29:04 +0000559 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000560 ConsumeToken();
561 }
562}
563
564/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
565/// the first token has already been read and has been turned into an instance
566/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
567/// otherwise it returns false and fills in Decl.
568bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
569 AttributeList *Attr = 0;
570 // If attributes exist after tag, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000571 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 Attr = ParseAttributes();
573
574 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner04d66662007-10-09 17:33:22 +0000575 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000576 Diag(Tok, diag::err_expected_ident_lbrace);
Chris Lattnere80a59c2007-07-25 00:24:17 +0000577
578 // Skip the rest of this declarator, up until the comma or semicolon.
579 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000580 return true;
581 }
582
583 // If an identifier is present, consume and remember it.
584 IdentifierInfo *Name = 0;
585 SourceLocation NameLoc;
Chris Lattner04d66662007-10-09 17:33:22 +0000586 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000587 Name = Tok.getIdentifierInfo();
588 NameLoc = ConsumeToken();
589 }
590
591 // There are three options here. If we have 'struct foo;', then this is a
592 // forward declaration. If we have 'struct foo {...' then this is a
593 // definition. Otherwise we have something like 'struct foo xyz', a reference.
594 //
595 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
596 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
597 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
598 //
599 Action::TagKind TK;
Chris Lattner04d66662007-10-09 17:33:22 +0000600 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +0000601 TK = Action::TK_Definition;
Chris Lattner04d66662007-10-09 17:33:22 +0000602 else if (Tok.is(tok::semi))
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 TK = Action::TK_Declaration;
604 else
605 TK = Action::TK_Reference;
Steve Naroff08d92e42007-09-15 18:49:24 +0000606 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 return false;
608}
609
610
611/// ParseStructUnionSpecifier
612/// struct-or-union-specifier: [C99 6.7.2.1]
613/// struct-or-union identifier[opt] '{' struct-contents '}'
614/// struct-or-union identifier
615/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
616/// '}' attributes[opt]
617/// [GNU] struct-or-union attributes[opt] identifier
618/// struct-or-union:
619/// 'struct'
620/// 'union'
621///
622void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +0000623 assert((Tok.is(tok::kw_struct) || Tok.is(tok::kw_union)) &&
624 "Not a struct/union specifier");
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 DeclSpec::TST TagType =
Chris Lattner04d66662007-10-09 17:33:22 +0000626 Tok.is(tok::kw_union) ? DeclSpec::TST_union : DeclSpec::TST_struct;
Reid Spencer5f016e22007-07-11 17:01:13 +0000627 SourceLocation StartLoc = ConsumeToken();
628
629 // Parse the tag portion of this.
630 DeclTy *TagDecl;
631 if (ParseTag(TagDecl, TagType, StartLoc))
632 return;
633
634 // If there is a body, parse it and inform the actions module.
Chris Lattner04d66662007-10-09 17:33:22 +0000635 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 ParseStructUnionBody(StartLoc, TagType, TagDecl);
637
638 const char *PrevSpec = 0;
639 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
640 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
641}
642
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000643/// ParseStructDeclaration - Parse a struct declaration without the terminating
644/// semicolon.
645///
Reid Spencer5f016e22007-07-11 17:01:13 +0000646/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000647/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000648/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000649/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000650/// struct-declarator-list:
651/// struct-declarator
652/// struct-declarator-list ',' struct-declarator
653/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
654/// struct-declarator:
655/// declarator
656/// [GNU] declarator attributes[opt]
657/// declarator[opt] ':' constant-expression
658/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
659///
Steve Naroff28a7ca82007-08-20 22:28:22 +0000660void Parser::ParseStructDeclaration(DeclTy *TagDecl,
Steve Naroff4e6526b2007-08-28 16:31:47 +0000661 llvm::SmallVectorImpl<DeclTy*> &FieldDecls) {
Steve Naroff28a7ca82007-08-20 22:28:22 +0000662 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattner04d66662007-10-09 17:33:22 +0000663 if (Tok.is(tok::kw___extension__))
Steve Naroff28a7ca82007-08-20 22:28:22 +0000664 ConsumeToken();
665
666 // Parse the common specifier-qualifiers-list piece.
667 DeclSpec DS;
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000668 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +0000669 ParseSpecifierQualifierList(DS);
670 // TODO: Does specifier-qualifier list correctly check that *something* is
671 // specified?
672
673 // If there are no declarators, issue a warning.
Chris Lattner04d66662007-10-09 17:33:22 +0000674 if (Tok.is(tok::semi)) {
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000675 Diag(DSStart, diag::w_no_declarators);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000676 return;
677 }
678
679 // Read struct-declarators until we find the semicolon.
680 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
681
682 while (1) {
683 /// struct-declarator: declarator
684 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +0000685 if (Tok.isNot(tok::colon))
Steve Naroff28a7ca82007-08-20 22:28:22 +0000686 ParseDeclarator(DeclaratorInfo);
687
688 ExprTy *BitfieldSize = 0;
Chris Lattner04d66662007-10-09 17:33:22 +0000689 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +0000690 ConsumeToken();
691 ExprResult Res = ParseConstantExpression();
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000692 if (Res.isInvalid)
Steve Naroff28a7ca82007-08-20 22:28:22 +0000693 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000694 else
Steve Naroff28a7ca82007-08-20 22:28:22 +0000695 BitfieldSize = Res.Val;
Steve Naroff28a7ca82007-08-20 22:28:22 +0000696 }
697
698 // If attributes exist after the declarator, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000699 if (Tok.is(tok::kw___attribute))
Steve Naroff28a7ca82007-08-20 22:28:22 +0000700 DeclaratorInfo.AddAttributes(ParseAttributes());
701
702 // Install the declarator into the current TagDecl.
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000703 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
704 DS.getSourceRange().getBegin(),
Steve Naroff28a7ca82007-08-20 22:28:22 +0000705 DeclaratorInfo, BitfieldSize);
706 FieldDecls.push_back(Field);
707
708 // If we don't have a comma, it is either the end of the list (a ';')
709 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000710 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000711 return;
Steve Naroff28a7ca82007-08-20 22:28:22 +0000712
713 // Consume the comma.
714 ConsumeToken();
715
716 // Parse the next declarator.
717 DeclaratorInfo.clear();
718
719 // Attributes are only allowed on the second declarator.
Chris Lattner04d66662007-10-09 17:33:22 +0000720 if (Tok.is(tok::kw___attribute))
Steve Naroff28a7ca82007-08-20 22:28:22 +0000721 DeclaratorInfo.AddAttributes(ParseAttributes());
722 }
Steve Naroff28a7ca82007-08-20 22:28:22 +0000723}
724
725/// ParseStructUnionBody
726/// struct-contents:
727/// struct-declaration-list
728/// [EXT] empty
729/// [GNU] "struct-declaration-list" without terminatoring ';'
730/// struct-declaration-list:
731/// struct-declaration
732/// struct-declaration-list struct-declaration
733/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
734///
Reid Spencer5f016e22007-07-11 17:01:13 +0000735void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
736 unsigned TagType, DeclTy *TagDecl) {
737 SourceLocation LBraceLoc = ConsumeBrace();
738
739 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
740 // C++.
Chris Lattner04d66662007-10-09 17:33:22 +0000741 if (Tok.is(tok::r_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +0000742 Diag(Tok, diag::ext_empty_struct_union_enum,
743 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
744
745 llvm::SmallVector<DeclTy*, 32> FieldDecls;
746
747 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +0000748 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000749 // Each iteration of this loop reads one struct-declaration.
750
751 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +0000752 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 Diag(Tok, diag::ext_extra_struct_semi);
754 ConsumeToken();
755 continue;
756 }
Steve Naroff28a7ca82007-08-20 22:28:22 +0000757 ParseStructDeclaration(TagDecl, FieldDecls);
Reid Spencer5f016e22007-07-11 17:01:13 +0000758
Chris Lattner04d66662007-10-09 17:33:22 +0000759 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000760 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +0000761 } else if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000762 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
763 break;
764 } else {
765 Diag(Tok, diag::err_expected_semi_decl_list);
766 // Skip to end of block or statement
767 SkipUntil(tok::r_brace, true, true);
768 }
769 }
770
Steve Naroff60fccee2007-10-29 21:38:07 +0000771 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000772
Fariborz Jahanian9d048ff2007-09-29 00:54:24 +0000773 Actions.ActOnFields(CurScope,
Chris Lattnerc81c8142008-02-25 21:04:36 +0000774 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
Steve Naroff60fccee2007-10-29 21:38:07 +0000775 LBraceLoc, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000776
777 AttributeList *AttrList = 0;
778 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000779 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000780 AttrList = ParseAttributes(); // FIXME: where should I put them?
781}
782
783
784/// ParseEnumSpecifier
785/// enum-specifier: [C99 6.7.2.2]
786/// 'enum' identifier[opt] '{' enumerator-list '}'
787/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
788/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
789/// '}' attributes[opt]
790/// 'enum' identifier
791/// [GNU] 'enum' attributes[opt] identifier
792void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +0000793 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Reid Spencer5f016e22007-07-11 17:01:13 +0000794 SourceLocation StartLoc = ConsumeToken();
795
796 // Parse the tag portion of this.
797 DeclTy *TagDecl;
798 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
799 return;
800
Chris Lattner04d66662007-10-09 17:33:22 +0000801 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +0000802 ParseEnumBody(StartLoc, TagDecl);
803
804 // TODO: semantic analysis on the declspec for enums.
805 const char *PrevSpec = 0;
806 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
807 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
808}
809
810/// ParseEnumBody - Parse a {} enclosed enumerator-list.
811/// enumerator-list:
812/// enumerator
813/// enumerator-list ',' enumerator
814/// enumerator:
815/// enumeration-constant
816/// enumeration-constant '=' constant-expression
817/// enumeration-constant:
818/// identifier
819///
820void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
821 SourceLocation LBraceLoc = ConsumeBrace();
822
Chris Lattner7946dd32007-08-27 17:24:30 +0000823 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +0000824 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Reid Spencer5f016e22007-07-11 17:01:13 +0000825 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
826
827 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
828
829 DeclTy *LastEnumConstDecl = 0;
830
831 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +0000832 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000833 IdentifierInfo *Ident = Tok.getIdentifierInfo();
834 SourceLocation IdentLoc = ConsumeToken();
835
836 SourceLocation EqualLoc;
837 ExprTy *AssignedVal = 0;
Chris Lattner04d66662007-10-09 17:33:22 +0000838 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000839 EqualLoc = ConsumeToken();
840 ExprResult Res = ParseConstantExpression();
841 if (Res.isInvalid)
842 SkipUntil(tok::comma, tok::r_brace, true, true);
843 else
844 AssignedVal = Res.Val;
845 }
846
847 // Install the enumerator constant into EnumDecl.
Steve Naroff08d92e42007-09-15 18:49:24 +0000848 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +0000849 LastEnumConstDecl,
850 IdentLoc, Ident,
851 EqualLoc, AssignedVal);
852 EnumConstantDecls.push_back(EnumConstDecl);
853 LastEnumConstDecl = EnumConstDecl;
854
Chris Lattner04d66662007-10-09 17:33:22 +0000855 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 break;
857 SourceLocation CommaLoc = ConsumeToken();
858
Chris Lattner04d66662007-10-09 17:33:22 +0000859 if (Tok.isNot(tok::identifier) && !getLang().C99)
Reid Spencer5f016e22007-07-11 17:01:13 +0000860 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
861 }
862
863 // Eat the }.
864 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
865
Steve Naroff08d92e42007-09-15 18:49:24 +0000866 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Reid Spencer5f016e22007-07-11 17:01:13 +0000867 EnumConstantDecls.size());
868
869 DeclTy *AttrList = 0;
870 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000871 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000872 AttrList = ParseAttributes(); // FIXME: where do they do?
873}
874
875/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +0000876/// start of a type-qualifier-list.
877bool Parser::isTypeQualifier() const {
878 switch (Tok.getKind()) {
879 default: return false;
880 // type-qualifier
881 case tok::kw_const:
882 case tok::kw_volatile:
883 case tok::kw_restrict:
884 return true;
885 }
886}
887
888/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +0000889/// start of a specifier-qualifier-list.
890bool Parser::isTypeSpecifierQualifier() const {
891 switch (Tok.getKind()) {
892 default: return false;
893 // GNU attributes support.
894 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +0000895 // GNU typeof support.
896 case tok::kw_typeof:
897
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 // type-specifiers
899 case tok::kw_short:
900 case tok::kw_long:
901 case tok::kw_signed:
902 case tok::kw_unsigned:
903 case tok::kw__Complex:
904 case tok::kw__Imaginary:
905 case tok::kw_void:
906 case tok::kw_char:
907 case tok::kw_int:
908 case tok::kw_float:
909 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +0000910 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +0000911 case tok::kw__Bool:
912 case tok::kw__Decimal32:
913 case tok::kw__Decimal64:
914 case tok::kw__Decimal128:
915
916 // struct-or-union-specifier
917 case tok::kw_struct:
918 case tok::kw_union:
919 // enum-specifier
920 case tok::kw_enum:
921
922 // type-qualifier
923 case tok::kw_const:
924 case tok::kw_volatile:
925 case tok::kw_restrict:
926 return true;
927
928 // typedef-name
929 case tok::identifier:
930 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000931 }
932}
933
934/// isDeclarationSpecifier() - Return true if the current token is part of a
935/// declaration specifier.
936bool Parser::isDeclarationSpecifier() const {
937 switch (Tok.getKind()) {
938 default: return false;
939 // storage-class-specifier
940 case tok::kw_typedef:
941 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +0000942 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +0000943 case tok::kw_static:
944 case tok::kw_auto:
945 case tok::kw_register:
946 case tok::kw___thread:
947
948 // type-specifiers
949 case tok::kw_short:
950 case tok::kw_long:
951 case tok::kw_signed:
952 case tok::kw_unsigned:
953 case tok::kw__Complex:
954 case tok::kw__Imaginary:
955 case tok::kw_void:
956 case tok::kw_char:
957 case tok::kw_int:
958 case tok::kw_float:
959 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +0000960 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +0000961 case tok::kw__Bool:
962 case tok::kw__Decimal32:
963 case tok::kw__Decimal64:
964 case tok::kw__Decimal128:
965
966 // struct-or-union-specifier
967 case tok::kw_struct:
968 case tok::kw_union:
969 // enum-specifier
970 case tok::kw_enum:
971
972 // type-qualifier
973 case tok::kw_const:
974 case tok::kw_volatile:
975 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +0000976
Reid Spencer5f016e22007-07-11 17:01:13 +0000977 // function-specifier
978 case tok::kw_inline:
Chris Lattnerd6c7c182007-08-09 16:40:21 +0000979
Chris Lattner1ef08762007-08-09 17:01:07 +0000980 // GNU typeof support.
981 case tok::kw_typeof:
982
983 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +0000984 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +0000985 return true;
986
987 // typedef-name
988 case tok::identifier:
989 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000990 }
991}
992
993
994/// ParseTypeQualifierListOpt
995/// type-qualifier-list: [C99 6.7.5]
996/// type-qualifier
997/// [GNU] attributes
998/// type-qualifier-list type-qualifier
999/// [GNU] type-qualifier-list attributes
1000///
1001void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1002 while (1) {
1003 int isInvalid = false;
1004 const char *PrevSpec = 0;
1005 SourceLocation Loc = Tok.getLocation();
1006
1007 switch (Tok.getKind()) {
1008 default:
1009 // If this is not a type-qualifier token, we're done reading type
1010 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +00001011 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001012 return;
1013 case tok::kw_const:
1014 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1015 getLang())*2;
1016 break;
1017 case tok::kw_volatile:
1018 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1019 getLang())*2;
1020 break;
1021 case tok::kw_restrict:
1022 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1023 getLang())*2;
1024 break;
1025 case tok::kw___attribute:
1026 DS.AddAttributes(ParseAttributes());
1027 continue; // do *not* consume the next token!
1028 }
1029
1030 // If the specifier combination wasn't legal, issue a diagnostic.
1031 if (isInvalid) {
1032 assert(PrevSpec && "Method did not return previous specifier!");
1033 if (isInvalid == 1) // Error.
1034 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1035 else // extwarn.
1036 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1037 }
1038 ConsumeToken();
1039 }
1040}
1041
1042
1043/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1044///
1045void Parser::ParseDeclarator(Declarator &D) {
1046 /// This implements the 'declarator' production in the C grammar, then checks
1047 /// for well-formedness and issues diagnostics.
1048 ParseDeclaratorInternal(D);
Reid Spencer5f016e22007-07-11 17:01:13 +00001049}
1050
1051/// ParseDeclaratorInternal
1052/// declarator: [C99 6.7.5]
1053/// pointer[opt] direct-declarator
1054/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1055/// [GNU] '&' restrict[opt] attributes[opt] declarator
1056///
1057/// pointer: [C99 6.7.5]
1058/// '*' type-qualifier-list[opt]
1059/// '*' type-qualifier-list[opt] pointer
1060///
1061void Parser::ParseDeclaratorInternal(Declarator &D) {
1062 tok::TokenKind Kind = Tok.getKind();
1063
1064 // Not a pointer or C++ reference.
Chris Lattner76549142008-02-21 01:32:26 +00001065 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus))
Reid Spencer5f016e22007-07-11 17:01:13 +00001066 return ParseDirectDeclarator(D);
1067
1068 // Otherwise, '*' -> pointer or '&' -> reference.
1069 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1070
1071 if (Kind == tok::star) {
Chris Lattner76549142008-02-21 01:32:26 +00001072 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00001073 DeclSpec DS;
1074
1075 ParseTypeQualifierListOpt(DS);
1076
1077 // Recursively parse the declarator.
1078 ParseDeclaratorInternal(D);
1079
1080 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001081 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1082 DS.TakeAttributes()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 } else {
1084 // Is a reference
1085 DeclSpec DS;
1086
1087 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1088 // cv-qualifiers are introduced through the use of a typedef or of a
1089 // template type argument, in which case the cv-qualifiers are ignored.
1090 //
1091 // [GNU] Retricted references are allowed.
1092 // [GNU] Attributes on references are allowed.
1093 ParseTypeQualifierListOpt(DS);
1094
1095 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1096 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1097 Diag(DS.getConstSpecLoc(),
1098 diag::err_invalid_reference_qualifier_application,
1099 "const");
1100 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1101 Diag(DS.getVolatileSpecLoc(),
1102 diag::err_invalid_reference_qualifier_application,
1103 "volatile");
1104 }
1105
1106 // Recursively parse the declarator.
1107 ParseDeclaratorInternal(D);
1108
1109 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001110 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1111 DS.TakeAttributes()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001112 }
1113}
1114
1115/// ParseDirectDeclarator
1116/// direct-declarator: [C99 6.7.5]
1117/// identifier
1118/// '(' declarator ')'
1119/// [GNU] '(' attributes declarator ')'
1120/// [C90] direct-declarator '[' constant-expression[opt] ']'
1121/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1122/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1123/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1124/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1125/// direct-declarator '(' parameter-type-list ')'
1126/// direct-declarator '(' identifier-list[opt] ')'
1127/// [GNU] direct-declarator '(' parameter-forward-declarations
1128/// parameter-type-list[opt] ')'
1129///
1130void Parser::ParseDirectDeclarator(Declarator &D) {
1131 // Parse the first direct-declarator seen.
Chris Lattner04d66662007-10-09 17:33:22 +00001132 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001133 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1134 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1135 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001136 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001137 // direct-declarator: '(' declarator ')'
1138 // direct-declarator: '(' attributes declarator ')'
1139 // Example: 'char (*X)' or 'int (*XX)(void)'
1140 ParseParenDeclarator(D);
1141 } else if (D.mayOmitIdentifier()) {
1142 // This could be something simple like "int" (in which case the declarator
1143 // portion is empty), if an abstract-declarator is allowed.
1144 D.SetIdentifier(0, Tok.getLocation());
1145 } else {
1146 // Expected identifier or '('.
1147 Diag(Tok, diag::err_expected_ident_lparen);
1148 D.SetIdentifier(0, Tok.getLocation());
1149 }
1150
1151 assert(D.isPastIdentifier() &&
1152 "Haven't past the location of the identifier yet?");
1153
1154 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00001155 if (Tok.is(tok::l_paren)) {
Chris Lattneref4715c2008-04-06 05:45:57 +00001156 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00001157 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001158 ParseBracketDeclarator(D);
1159 } else {
1160 break;
1161 }
1162 }
1163}
1164
Chris Lattneref4715c2008-04-06 05:45:57 +00001165/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1166/// only called before the identifier, so these are most likely just grouping
1167/// parens for precedence. If we find that these are actually function
1168/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1169///
1170/// direct-declarator:
1171/// '(' declarator ')'
1172/// [GNU] '(' attributes declarator ')'
1173///
1174void Parser::ParseParenDeclarator(Declarator &D) {
1175 SourceLocation StartLoc = ConsumeParen();
1176 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1177
1178 // If we haven't past the identifier yet (or where the identifier would be
1179 // stored, if this is an abstract declarator), then this is probably just
1180 // grouping parens. However, if this could be an abstract-declarator, then
1181 // this could also be the start of function arguments (consider 'void()').
1182 bool isGrouping;
1183
1184 if (!D.mayOmitIdentifier()) {
1185 // If this can't be an abstract-declarator, this *must* be a grouping
1186 // paren, because we haven't seen the identifier yet.
1187 isGrouping = true;
1188 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
1189 isDeclarationSpecifier()) { // 'int(int)' is a function.
1190 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1191 // considered to be a type, not a K&R identifier-list.
1192 isGrouping = false;
1193 } else {
1194 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1195 isGrouping = true;
1196 }
1197
1198 // If this is a grouping paren, handle:
1199 // direct-declarator: '(' declarator ')'
1200 // direct-declarator: '(' attributes declarator ')'
1201 if (isGrouping) {
1202 if (Tok.is(tok::kw___attribute))
1203 D.AddAttributes(ParseAttributes());
1204
1205 ParseDeclaratorInternal(D);
1206 // Match the ')'.
1207 MatchRHSPunctuation(tok::r_paren, StartLoc);
1208 return;
1209 }
1210
1211 // Okay, if this wasn't a grouping paren, it must be the start of a function
1212 // argument list. Recognize that this declarator will never have an
1213 // identifier (and remember where it would have been), then fall through to
1214 // the handling of argument lists.
1215 D.SetIdentifier(0, Tok.getLocation());
1216
1217 ParseFunctionDeclarator(StartLoc, D);
1218}
1219
1220/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1221/// declarator D up to a paren, which indicates that we are parsing function
1222/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00001223///
1224/// This method also handles this portion of the grammar:
1225/// parameter-type-list: [C99 6.7.5]
1226/// parameter-list
1227/// parameter-list ',' '...'
1228///
1229/// parameter-list: [C99 6.7.5]
1230/// parameter-declaration
1231/// parameter-list ',' parameter-declaration
1232///
1233/// parameter-declaration: [C99 6.7.5]
1234/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00001235/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001236/// [GNU] declaration-specifiers declarator attributes
1237/// declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00001238/// [C++] declaration-specifiers abstract-declarator[opt]
1239/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001240/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1241///
Chris Lattneref4715c2008-04-06 05:45:57 +00001242void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D) {
1243 // lparen is already consumed!
1244 assert(D.isPastIdentifier() && "Should not call before identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001245
1246 // Okay, this is the parameter list of a function definition, or it is an
1247 // identifier list of a K&R-style function.
Reid Spencer5f016e22007-07-11 17:01:13 +00001248
Chris Lattner04d66662007-10-09 17:33:22 +00001249 if (Tok.is(tok::r_paren)) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00001250 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00001251 // int() -> no prototype, no '...'.
Chris Lattnerf97409f2008-04-06 06:57:35 +00001252 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/ false,
1253 /*variadic*/ false,
1254 /*arglist*/ 0, 0, LParenLoc));
1255
1256 ConsumeParen(); // Eat the closing ')'.
1257 return;
Chris Lattner04d66662007-10-09 17:33:22 +00001258 } else if (Tok.is(tok::identifier) &&
Reid Spencer5f016e22007-07-11 17:01:13 +00001259 // K&R identifier lists can't have typedefs as identifiers, per
1260 // C99 6.7.5.3p11.
1261 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1262 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1263 // normal declarators, not for abstract-declarators.
Chris Lattner66d28652008-04-06 06:34:08 +00001264 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattnerf97409f2008-04-06 06:57:35 +00001265 }
1266
1267 // Finally, a normal, non-empty parameter type list.
1268
1269 // Build up an array of information about the parsed arguments.
1270 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00001271
1272 // Enter function-declaration scope, limiting any declarators to the
1273 // function prototype scope, including parameter declarators.
Chris Lattnerf97409f2008-04-06 06:57:35 +00001274 EnterScope(Scope::DeclScope);
1275
1276 bool IsVariadic = false;
1277 while (1) {
1278 if (Tok.is(tok::ellipsis)) {
1279 IsVariadic = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001280
Chris Lattnerf97409f2008-04-06 06:57:35 +00001281 // Check to see if this is "void(...)" which is not allowed.
1282 if (ParamInfo.empty()) {
1283 // Otherwise, parse parameter type list. If it starts with an
1284 // ellipsis, diagnose the malformed function.
1285 Diag(Tok, diag::err_ellipsis_first_arg);
1286 IsVariadic = false; // Treat this like 'void()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001287 }
Chris Lattnere0e713b2008-01-31 06:10:07 +00001288
Chris Lattnerf97409f2008-04-06 06:57:35 +00001289 ConsumeToken(); // Consume the ellipsis.
1290 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001291 }
1292
Chris Lattnerf97409f2008-04-06 06:57:35 +00001293 SourceLocation DSStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00001294
Chris Lattnerf97409f2008-04-06 06:57:35 +00001295 // Parse the declaration-specifiers.
1296 DeclSpec DS;
1297 ParseDeclarationSpecifiers(DS);
1298
1299 // Parse the declarator. This is "PrototypeContext", because we must
1300 // accept either 'declarator' or 'abstract-declarator' here.
1301 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1302 ParseDeclarator(ParmDecl);
1303
1304 // Parse GNU attributes, if present.
1305 if (Tok.is(tok::kw___attribute))
1306 ParmDecl.AddAttributes(ParseAttributes());
1307
Chris Lattnerf97409f2008-04-06 06:57:35 +00001308 // Remember this parsed parameter in ParamInfo.
1309 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1310
Chris Lattnerf97409f2008-04-06 06:57:35 +00001311 // If no parameter was specified, verify that *something* was specified,
1312 // otherwise we have a missing type and identifier.
1313 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1314 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1315 // Completely missing, emit error.
1316 Diag(DSStart, diag::err_missing_param);
1317 } else {
1318 // Otherwise, we have something. Add it and let semantic analysis try
1319 // to grok it and add the result to the ParamInfo we are building.
1320
1321 // Inform the actions module about the parameter declarator, so it gets
1322 // added to the current scope.
Chris Lattner04421082008-04-08 04:40:51 +00001323 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1324
1325 // Parse the default argument, if any. We parse the default
1326 // arguments in all dialects; the semantic analysis in
1327 // ActOnParamDefaultArgument will reject the default argument in
1328 // C.
1329 if (Tok.is(tok::equal)) {
1330 SourceLocation EqualLoc = Tok.getLocation();
1331
1332 // Consume the '='.
1333 ConsumeToken();
1334
1335 // Parse the default argument
1336 // FIXME: For C++, name lookup from within the default argument
1337 // should be able to find parameter names, but we haven't put them
1338 // in the scope. This means that we will accept ill-formed code
1339 // such as:
1340 //
1341 // int x;
1342 // void f(int x = x) { }
1343 ExprResult DefArgResult = ParseAssignmentExpression();
1344 if (DefArgResult.isInvalid) {
1345 SkipUntil(tok::comma, tok::r_paren, true, true);
1346 } else {
1347 // Inform the actions module about the default argument
1348 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1349 }
1350 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00001351
1352 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner04421082008-04-08 04:40:51 +00001353 ParmDecl.getIdentifierLoc(), Param));
Chris Lattnerf97409f2008-04-06 06:57:35 +00001354 }
1355
1356 // If the next token is a comma, consume it and keep reading arguments.
1357 if (Tok.isNot(tok::comma)) break;
1358
1359 // Consume the comma.
1360 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001361 }
1362
Chris Lattnerf97409f2008-04-06 06:57:35 +00001363 // Leave prototype scope.
1364 ExitScope();
1365
Reid Spencer5f016e22007-07-11 17:01:13 +00001366 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00001367 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1368 &ParamInfo[0], ParamInfo.size(),
1369 LParenLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001370
1371 // If we have the closing ')', eat it and we're done.
Chris Lattnerf97409f2008-04-06 06:57:35 +00001372 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001373}
1374
Chris Lattner66d28652008-04-06 06:34:08 +00001375/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1376/// we found a K&R-style identifier list instead of a type argument list. The
1377/// current token is known to be the first identifier in the list.
1378///
1379/// identifier-list: [C99 6.7.5]
1380/// identifier
1381/// identifier-list ',' identifier
1382///
1383void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1384 Declarator &D) {
1385 // Build up an array of information about the parsed arguments.
1386 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1387 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1388
1389 // If there was no identifier specified for the declarator, either we are in
1390 // an abstract-declarator, or we are in a parameter declarator which was found
1391 // to be abstract. In abstract-declarators, identifier lists are not valid:
1392 // diagnose this.
1393 if (!D.getIdentifier())
1394 Diag(Tok, diag::ext_ident_list_in_param);
1395
1396 // Tok is known to be the first identifier in the list. Remember this
1397 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00001398 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00001399 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1400 Tok.getLocation(), 0));
1401
Chris Lattner50c64772008-04-06 06:39:19 +00001402 ConsumeToken(); // eat the first identifier.
Chris Lattner66d28652008-04-06 06:34:08 +00001403
1404 while (Tok.is(tok::comma)) {
1405 // Eat the comma.
1406 ConsumeToken();
1407
Chris Lattner50c64772008-04-06 06:39:19 +00001408 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00001409 if (Tok.isNot(tok::identifier)) {
1410 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00001411 SkipUntil(tok::r_paren);
1412 return;
Chris Lattner66d28652008-04-06 06:34:08 +00001413 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00001414
Chris Lattner66d28652008-04-06 06:34:08 +00001415 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00001416
1417 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1418 if (Actions.isTypeName(*ParmII, CurScope))
1419 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner66d28652008-04-06 06:34:08 +00001420
1421 // Verify that the argument identifier has not already been mentioned.
1422 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner50c64772008-04-06 06:39:19 +00001423 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1424 } else {
1425 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00001426 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1427 Tok.getLocation(), 0));
Chris Lattner50c64772008-04-06 06:39:19 +00001428 }
Chris Lattner66d28652008-04-06 06:34:08 +00001429
1430 // Eat the identifier.
1431 ConsumeToken();
1432 }
1433
Chris Lattner50c64772008-04-06 06:39:19 +00001434 // Remember that we parsed a function type, and remember the attributes. This
1435 // function type is always a K&R style function type, which is not varargs and
1436 // has no prototype.
1437 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1438 &ParamInfo[0], ParamInfo.size(),
1439 LParenLoc));
Chris Lattner66d28652008-04-06 06:34:08 +00001440
1441 // If we have the closing ')', eat it and we're done.
Chris Lattner50c64772008-04-06 06:39:19 +00001442 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00001443}
Chris Lattneref4715c2008-04-06 05:45:57 +00001444
Reid Spencer5f016e22007-07-11 17:01:13 +00001445/// [C90] direct-declarator '[' constant-expression[opt] ']'
1446/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1447/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1448/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1449/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1450void Parser::ParseBracketDeclarator(Declarator &D) {
1451 SourceLocation StartLoc = ConsumeBracket();
1452
1453 // If valid, this location is the position where we read the 'static' keyword.
1454 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00001455 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00001456 StaticLoc = ConsumeToken();
1457
1458 // If there is a type-qualifier-list, read it now.
1459 DeclSpec DS;
1460 ParseTypeQualifierListOpt(DS);
1461
1462 // If we haven't already read 'static', check to see if there is one after the
1463 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001464 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00001465 StaticLoc = ConsumeToken();
1466
1467 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1468 bool isStar = false;
1469 ExprResult NumElements(false);
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00001470
1471 // Handle the case where we have '[*]' as the array size. However, a leading
1472 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1473 // the the token after the star is a ']'. Since stars in arrays are
1474 // infrequent, use of lookahead is not costly here.
1475 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00001476 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001477
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00001478 if (StaticLoc.isValid())
1479 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1480 StaticLoc = SourceLocation(); // Drop the static.
1481 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00001482 } else if (Tok.isNot(tok::r_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001483 // Parse the assignment-expression now.
1484 NumElements = ParseAssignmentExpression();
1485 }
1486
1487 // If there was an error parsing the assignment-expression, recover.
1488 if (NumElements.isInvalid) {
1489 // If the expression was invalid, skip it.
1490 SkipUntil(tok::r_square);
1491 return;
1492 }
1493
1494 MatchRHSPunctuation(tok::r_square, StartLoc);
1495
1496 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1497 // it was not a constant expression.
1498 if (!getLang().C99) {
1499 // TODO: check C90 array constant exprness.
1500 if (isStar || StaticLoc.isValid() ||
1501 0/*TODO: NumElts is not a C90 constantexpr */)
1502 Diag(StartLoc, diag::ext_c99_array_usage);
1503 }
1504
1505 // Remember that we parsed a pointer type, and remember the type-quals.
1506 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1507 StaticLoc.isValid(), isStar,
1508 NumElements.Val, StartLoc));
1509}
1510
Steve Naroffd1861fd2007-07-31 12:34:36 +00001511/// [GNU] typeof-specifier:
1512/// typeof ( expressions )
1513/// typeof ( type-name )
1514///
1515void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00001516 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001517 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffd1861fd2007-07-31 12:34:36 +00001518 SourceLocation StartLoc = ConsumeToken();
1519
Chris Lattner04d66662007-10-09 17:33:22 +00001520 if (Tok.isNot(tok::l_paren)) {
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001521 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1522 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001523 }
1524 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1525
1526 if (isTypeSpecifierQualifier()) {
1527 TypeTy *Ty = ParseTypeName();
1528
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001529 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1530
Chris Lattner04d66662007-10-09 17:33:22 +00001531 if (Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001532 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001533 return;
1534 }
1535 RParenLoc = ConsumeParen();
1536 const char *PrevSpec = 0;
1537 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1538 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1539 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001540 } else { // we have an expression.
1541 ExprResult Result = ParseExpression();
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001542
Chris Lattner04d66662007-10-09 17:33:22 +00001543 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001544 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001545 return;
1546 }
1547 RParenLoc = ConsumeParen();
1548 const char *PrevSpec = 0;
1549 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1550 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1551 Result.Val))
1552 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001553 }
Steve Naroffd1861fd2007-07-31 12:34:36 +00001554}
1555