blob: 51230fc88f09d505434456e8eb5ee4cbaab767db [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
15#include "clang/Parse/DeclSpec.h"
Chris Lattnera7549902007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattner4b009652007-07-25 00:24:17 +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 Naroff0acc9c92007-09-15 18:49:24 +000036 return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner34a01ad2007-10-09 17:33:22 +000076 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Chris Lattner4b009652007-07-25 00:24:17 +000077
78 AttributeList *CurrAttr = 0;
79
Chris Lattner34a01ad2007-10-09 17:33:22 +000080 while (Tok.is(tok::kw___attribute)) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner34a01ad2007-10-09 17:33:22 +000092 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
93 Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +000094
Chris Lattner34a01ad2007-10-09 17:33:22 +000095 if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner34a01ad2007-10-09 17:33:22 +0000105 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000106 ConsumeParen(); // ignore the left paren loc for now
107
Chris Lattner34a01ad2007-10-09 17:33:22 +0000108 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000109 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
110 SourceLocation ParmLoc = ConsumeToken();
111
Chris Lattner34a01ad2007-10-09 17:33:22 +0000112 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner34a01ad2007-10-09 17:33:22 +0000117 } else if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner34a01ad2007-10-09 17:33:22 +0000133 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000134 break;
135 ConsumeToken(); // Eat the comma, move to the next argument
136 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000137 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner34a01ad2007-10-09 17:33:22 +0000145 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner34a01ad2007-10-09 17:33:22 +0000165 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000166 break;
167 ConsumeToken(); // Eat the comma, move to the next argument
168 }
169 // Match the ')'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000170 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnerf7b2e552007-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///
Chris Lattner4b009652007-07-25 00:24:17 +0000202Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattnerf7b2e552007-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) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner34a01ad2007-10-09 17:33:22 +0000222 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnerf7b2e552007-08-25 06:57:03 +0000233
Chris Lattner4b009652007-07-25 00:24:17 +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///
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner34a01ad2007-10-09 17:33:22 +0000259 if (Tok.is(tok::kw_asm))
Chris Lattner4b009652007-07-25 00:24:17 +0000260 ParseSimpleAsm();
261
262 // If attributes are present, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000263 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000264 D.AddAttributes(ParseAttributes());
Steve Naroff6a0e2092007-09-12 14:07:44 +0000265
266 // Inform the current actions module that we just parsed this declarator.
267 // FIXME: pass asm & attributes.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000268 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Steve Naroff6a0e2092007-09-12 14:07:44 +0000269
Chris Lattner4b009652007-07-25 00:24:17 +0000270 // Parse declarator '=' initializer.
271 ExprResult Init;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000272 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000273 ConsumeToken();
274 Init = ParseInitializer();
275 if (Init.isInvalid) {
276 SkipUntil(tok::semi);
277 return 0;
278 }
Steve Naroff6a0e2092007-09-12 14:07:44 +0000279 Actions.AddInitializerToDecl(LastDeclInGroup, Init.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000280 }
281
Chris Lattner4b009652007-07-25 00:24:17 +0000282 // If we don't have a comma, it is either the end of the list (a ';') or an
283 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000284 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000285 break;
286
287 // Consume the comma.
288 ConsumeToken();
289
290 // Parse the next declarator.
291 D.clear();
292 ParseDeclarator(D);
293 }
294
Chris Lattner34a01ad2007-10-09 17:33:22 +0000295 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000296 ConsumeToken();
297 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
298 }
Fariborz Jahanian6e9c2b12008-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 Jahaniancadb0702008-01-04 23:04:08 +0000302 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000303 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
304 }
Chris Lattner4b009652007-07-25 00:24:17 +0000305 Diag(Tok, diag::err_parse_error);
306 // Skip to end of block or statement
Chris Lattnerf491b412007-08-21 18:36:18 +0000307 SkipUntil(tok::r_brace, true, true);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000308 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +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.
322 ParseDeclarationSpecifiers(DS);
323
324 // Validate declspec for type-name.
325 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000326 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +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 Naroff4c255ab2007-07-31 23:56:32 +0000380/// [GNU] typeof-specifier
Chris Lattner4b009652007-07-25 00:24:17 +0000381/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroffa8ee2262007-08-22 23:18:22 +0000382/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnera4ff4272008-03-13 06:29:04 +0000391 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000392 while (1) {
393 int isInvalid = false;
394 const char *PrevSpec = 0;
395 SourceLocation Loc = Tok.getLocation();
396
397 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000398 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000399 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000400 // If this is not a declaration specifier token, we're done reading decl
401 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000402 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000403 return;
Chris Lattnerfda18db2008-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
Chris Lattnerada63792008-07-26 01:53:50 +0000437 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000438 Actions.FindProtocolDeclaration(Loc,
439 &ProtocolRefs[0], ProtocolRefs.size(),
Chris Lattnerada63792008-07-26 01:53:50 +0000440 ProtocolDecl);
441 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000442
443 DS.SetRangeEnd(EndProtoLoc);
444
445 // Do not allow any other declspecs after the protocol qualifier list
446 // "<foo,bar>short" is not allowed.
447 goto DoneWithDeclSpec;
448 }
Chris Lattner4b009652007-07-25 00:24:17 +0000449 // GNU attributes support.
450 case tok::kw___attribute:
451 DS.AddAttributes(ParseAttributes());
452 continue;
453
454 // storage-class-specifier
455 case tok::kw_typedef:
456 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
457 break;
458 case tok::kw_extern:
459 if (DS.isThreadSpecified())
460 Diag(Tok, diag::ext_thread_before, "extern");
461 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
462 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000463 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000464 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
465 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000466 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000467 case tok::kw_static:
468 if (DS.isThreadSpecified())
469 Diag(Tok, diag::ext_thread_before, "static");
470 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
471 break;
472 case tok::kw_auto:
473 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
474 break;
475 case tok::kw_register:
476 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
477 break;
478 case tok::kw___thread:
479 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
480 break;
481
482 // type-specifiers
483 case tok::kw_short:
484 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
485 break;
486 case tok::kw_long:
487 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
488 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
489 else
490 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
491 break;
492 case tok::kw_signed:
493 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
494 break;
495 case tok::kw_unsigned:
496 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
497 break;
498 case tok::kw__Complex:
499 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
500 break;
501 case tok::kw__Imaginary:
502 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
503 break;
504 case tok::kw_void:
505 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
506 break;
507 case tok::kw_char:
508 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
509 break;
510 case tok::kw_int:
511 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
512 break;
513 case tok::kw_float:
514 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
515 break;
516 case tok::kw_double:
517 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
518 break;
519 case tok::kw_bool: // [C++ 2.11p1]
520 case tok::kw__Bool:
521 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
522 break;
523 case tok::kw__Decimal32:
524 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
525 break;
526 case tok::kw__Decimal64:
527 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
528 break;
529 case tok::kw__Decimal128:
530 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
531 break;
Chris Lattner2e78db32008-04-13 18:59:07 +0000532
533 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000534 case tok::kw_struct:
535 case tok::kw_union:
Douglas Gregorec93f442008-04-13 21:30:24 +0000536 ParseClassSpecifier(DS);
Chris Lattner4b009652007-07-25 00:24:17 +0000537 continue;
538 case tok::kw_enum:
539 ParseEnumSpecifier(DS);
540 continue;
541
Steve Naroff7cbb1462007-07-31 12:34:36 +0000542 // GNU typeof support.
543 case tok::kw_typeof:
544 ParseTypeofSpecifier(DS);
545 continue;
546
Chris Lattner4b009652007-07-25 00:24:17 +0000547 // type-qualifier
548 case tok::kw_const:
549 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
550 getLang())*2;
551 break;
552 case tok::kw_volatile:
553 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
554 getLang())*2;
555 break;
556 case tok::kw_restrict:
557 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
558 getLang())*2;
559 break;
560
561 // function-specifier
562 case tok::kw_inline:
563 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
564 break;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000565
Steve Naroff5f0466b2008-06-05 00:02:44 +0000566 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000567 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000568 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
569 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000570 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000571 goto DoneWithDeclSpec;
572
573 {
574 SourceLocation EndProtoLoc;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000575 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattnerb99d7492008-07-26 00:20:22 +0000576 ParseObjCProtocolReferences(ProtocolRefs, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000577 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000578 Actions.FindProtocolDeclaration(Loc,
Chris Lattnerb99d7492008-07-26 00:20:22 +0000579 &ProtocolRefs[0], ProtocolRefs.size(),
Chris Lattnerada63792008-07-26 01:53:50 +0000580 ProtocolDecl);
581 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000582 DS.SetRangeEnd(EndProtoLoc);
583
Chris Lattnerb99d7492008-07-26 00:20:22 +0000584 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id,
585 SourceRange(Loc, EndProtoLoc));
Chris Lattnerfda18db2008-07-26 01:18:38 +0000586 // Do not allow any other declspecs after the protocol qualifier list
587 // "<foo,bar>short" is not allowed.
588 goto DoneWithDeclSpec;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000589 }
Chris Lattner4b009652007-07-25 00:24:17 +0000590 }
591 // If the specifier combination wasn't legal, issue a diagnostic.
592 if (isInvalid) {
593 assert(PrevSpec && "Method did not return previous specifier!");
594 if (isInvalid == 1) // Error.
595 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
596 else // extwarn.
597 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
598 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000599 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000600 ConsumeToken();
601 }
602}
603
604/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
605/// the first token has already been read and has been turned into an instance
606/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
607/// otherwise it returns false and fills in Decl.
608bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
609 AttributeList *Attr = 0;
610 // If attributes exist after tag, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000611 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000612 Attr = ParseAttributes();
613
614 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000615 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000616 Diag(Tok, diag::err_expected_ident_lbrace);
617
618 // Skip the rest of this declarator, up until the comma or semicolon.
619 SkipUntil(tok::comma, true);
620 return true;
621 }
622
623 // If an identifier is present, consume and remember it.
624 IdentifierInfo *Name = 0;
625 SourceLocation NameLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000626 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000627 Name = Tok.getIdentifierInfo();
628 NameLoc = ConsumeToken();
629 }
630
631 // There are three options here. If we have 'struct foo;', then this is a
632 // forward declaration. If we have 'struct foo {...' then this is a
633 // definition. Otherwise we have something like 'struct foo xyz', a reference.
634 //
635 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
636 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
637 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
638 //
639 Action::TagKind TK;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000640 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000641 TK = Action::TK_Definition;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000642 else if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000643 TK = Action::TK_Declaration;
644 else
645 TK = Action::TK_Reference;
Steve Naroff0acc9c92007-09-15 18:49:24 +0000646 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Chris Lattner4b009652007-07-25 00:24:17 +0000647 return false;
648}
649
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000650/// ParseStructDeclaration - Parse a struct declaration without the terminating
651/// semicolon.
652///
Chris Lattner4b009652007-07-25 00:24:17 +0000653/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000654/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000655/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000656/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000657/// struct-declarator-list:
658/// struct-declarator
659/// struct-declarator-list ',' struct-declarator
660/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
661/// struct-declarator:
662/// declarator
663/// [GNU] declarator attributes[opt]
664/// declarator[opt] ':' constant-expression
665/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
666///
Chris Lattner3dd8d392008-04-10 06:46:29 +0000667void Parser::
668ParseStructDeclaration(DeclSpec &DS,
669 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000670 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattner3dd8d392008-04-10 06:46:29 +0000671 while (Tok.is(tok::kw___extension__))
Steve Naroffa9adf112007-08-20 22:28:22 +0000672 ConsumeToken();
673
674 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000675 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +0000676 ParseSpecifierQualifierList(DS);
677 // TODO: Does specifier-qualifier list correctly check that *something* is
678 // specified?
679
680 // If there are no declarators, issue a warning.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000681 if (Tok.is(tok::semi)) {
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000682 Diag(DSStart, diag::w_no_declarators);
Steve Naroffa9adf112007-08-20 22:28:22 +0000683 return;
684 }
685
686 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000687 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000688 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +0000689 FieldDeclarator &DeclaratorInfo = Fields.back();
690
Steve Naroffa9adf112007-08-20 22:28:22 +0000691 /// struct-declarator: declarator
692 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000693 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000694 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +0000695
Chris Lattner34a01ad2007-10-09 17:33:22 +0000696 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000697 ConsumeToken();
698 ExprResult Res = ParseConstantExpression();
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000699 if (Res.isInvalid)
Steve Naroffa9adf112007-08-20 22:28:22 +0000700 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000701 else
Chris Lattner3dd8d392008-04-10 06:46:29 +0000702 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroffa9adf112007-08-20 22:28:22 +0000703 }
704
705 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000706 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000707 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000708
709 // If we don't have a comma, it is either the end of the list (a ';')
710 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000711 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000712 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000713
714 // Consume the comma.
715 ConsumeToken();
716
717 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000718 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000719
720 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000721 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000722 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000723 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000724}
725
726/// ParseStructUnionBody
727/// struct-contents:
728/// struct-declaration-list
729/// [EXT] empty
730/// [GNU] "struct-declaration-list" without terminatoring ';'
731/// struct-declaration-list:
732/// struct-declaration
733/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +0000734/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +0000735///
Chris Lattner4b009652007-07-25 00:24:17 +0000736void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
737 unsigned TagType, DeclTy *TagDecl) {
738 SourceLocation LBraceLoc = ConsumeBrace();
739
740 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
741 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +0000742 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000743 Diag(Tok, diag::ext_empty_struct_union_enum,
744 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
745
746 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000747 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
748
Chris Lattner4b009652007-07-25 00:24:17 +0000749 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000750 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000751 // Each iteration of this loop reads one struct-declaration.
752
753 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000754 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000755 Diag(Tok, diag::ext_extra_struct_semi);
756 ConsumeToken();
757 continue;
758 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000759
760 // Parse all the comma separated declarators.
761 DeclSpec DS;
762 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +0000763 if (!Tok.is(tok::at)) {
764 ParseStructDeclaration(DS, FieldDeclarators);
765
766 // Convert them all to fields.
767 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
768 FieldDeclarator &FD = FieldDeclarators[i];
769 // Install the declarator into the current TagDecl.
770 DeclTy *Field = Actions.ActOnField(CurScope,
771 DS.getSourceRange().getBegin(),
772 FD.D, FD.BitfieldSize);
773 FieldDecls.push_back(Field);
774 }
775 } else { // Handle @defs
776 ConsumeToken();
777 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
778 Diag(Tok, diag::err_unexpected_at);
779 SkipUntil(tok::semi, true, true);
780 continue;
781 }
782 ConsumeToken();
783 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
784 if (!Tok.is(tok::identifier)) {
785 Diag(Tok, diag::err_expected_ident);
786 SkipUntil(tok::semi, true, true);
787 continue;
788 }
789 llvm::SmallVector<DeclTy*, 16> Fields;
790 Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
791 Fields);
792 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
793 ConsumeToken();
794 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
795 }
Chris Lattner4b009652007-07-25 00:24:17 +0000796
Chris Lattner34a01ad2007-10-09 17:33:22 +0000797 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000798 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +0000799 } else if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000800 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
801 break;
802 } else {
803 Diag(Tok, diag::err_expected_semi_decl_list);
804 // Skip to end of block or statement
805 SkipUntil(tok::r_brace, true, true);
806 }
807 }
808
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000809 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000810
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000811 Actions.ActOnFields(CurScope,
Chris Lattner43b885f2008-02-25 21:04:36 +0000812 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000813 LBraceLoc, RBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000814
815 AttributeList *AttrList = 0;
816 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000817 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000818 AttrList = ParseAttributes(); // FIXME: where should I put them?
819}
820
821
822/// ParseEnumSpecifier
823/// enum-specifier: [C99 6.7.2.2]
824/// 'enum' identifier[opt] '{' enumerator-list '}'
825/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
826/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
827/// '}' attributes[opt]
828/// 'enum' identifier
829/// [GNU] 'enum' attributes[opt] identifier
830void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000831 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000832 SourceLocation StartLoc = ConsumeToken();
833
834 // Parse the tag portion of this.
835 DeclTy *TagDecl;
836 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
837 return;
838
Chris Lattner34a01ad2007-10-09 17:33:22 +0000839 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000840 ParseEnumBody(StartLoc, TagDecl);
841
842 // TODO: semantic analysis on the declspec for enums.
843 const char *PrevSpec = 0;
844 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
845 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
846}
847
848/// ParseEnumBody - Parse a {} enclosed enumerator-list.
849/// enumerator-list:
850/// enumerator
851/// enumerator-list ',' enumerator
852/// enumerator:
853/// enumeration-constant
854/// enumeration-constant '=' constant-expression
855/// enumeration-constant:
856/// identifier
857///
858void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
859 SourceLocation LBraceLoc = ConsumeBrace();
860
Chris Lattnerc9a92452007-08-27 17:24:30 +0000861 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +0000862 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000863 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
864
865 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
866
867 DeclTy *LastEnumConstDecl = 0;
868
869 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000870 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000871 IdentifierInfo *Ident = Tok.getIdentifierInfo();
872 SourceLocation IdentLoc = ConsumeToken();
873
874 SourceLocation EqualLoc;
875 ExprTy *AssignedVal = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000876 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000877 EqualLoc = ConsumeToken();
878 ExprResult Res = ParseConstantExpression();
879 if (Res.isInvalid)
880 SkipUntil(tok::comma, tok::r_brace, true, true);
881 else
882 AssignedVal = Res.Val;
883 }
884
885 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000886 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +0000887 LastEnumConstDecl,
888 IdentLoc, Ident,
889 EqualLoc, AssignedVal);
890 EnumConstantDecls.push_back(EnumConstDecl);
891 LastEnumConstDecl = EnumConstDecl;
892
Chris Lattner34a01ad2007-10-09 17:33:22 +0000893 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000894 break;
895 SourceLocation CommaLoc = ConsumeToken();
896
Chris Lattner34a01ad2007-10-09 17:33:22 +0000897 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +0000898 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
899 }
900
901 // Eat the }.
902 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
903
Steve Naroff0acc9c92007-09-15 18:49:24 +0000904 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +0000905 EnumConstantDecls.size());
906
907 DeclTy *AttrList = 0;
908 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000909 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000910 AttrList = ParseAttributes(); // FIXME: where do they do?
911}
912
913/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +0000914/// start of a type-qualifier-list.
915bool Parser::isTypeQualifier() const {
916 switch (Tok.getKind()) {
917 default: return false;
918 // type-qualifier
919 case tok::kw_const:
920 case tok::kw_volatile:
921 case tok::kw_restrict:
922 return true;
923 }
924}
925
926/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +0000927/// start of a specifier-qualifier-list.
928bool Parser::isTypeSpecifierQualifier() const {
929 switch (Tok.getKind()) {
930 default: return false;
931 // GNU attributes support.
932 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000933 // GNU typeof support.
934 case tok::kw_typeof:
Steve Naroff5f0466b2008-06-05 00:02:44 +0000935 // GNU bizarre protocol extension. FIXME: make an extension?
936 case tok::less:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000937
Chris Lattner4b009652007-07-25 00:24:17 +0000938 // type-specifiers
939 case tok::kw_short:
940 case tok::kw_long:
941 case tok::kw_signed:
942 case tok::kw_unsigned:
943 case tok::kw__Complex:
944 case tok::kw__Imaginary:
945 case tok::kw_void:
946 case tok::kw_char:
947 case tok::kw_int:
948 case tok::kw_float:
949 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000950 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +0000951 case tok::kw__Bool:
952 case tok::kw__Decimal32:
953 case tok::kw__Decimal64:
954 case tok::kw__Decimal128:
955
Chris Lattner2e78db32008-04-13 18:59:07 +0000956 // struct-or-union-specifier (C99) or class-specifier (C++)
957 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000958 case tok::kw_struct:
959 case tok::kw_union:
960 // enum-specifier
961 case tok::kw_enum:
962
963 // type-qualifier
964 case tok::kw_const:
965 case tok::kw_volatile:
966 case tok::kw_restrict:
967 return true;
968
969 // typedef-name
970 case tok::identifier:
971 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000972 }
973}
974
975/// isDeclarationSpecifier() - Return true if the current token is part of a
976/// declaration specifier.
977bool Parser::isDeclarationSpecifier() const {
978 switch (Tok.getKind()) {
979 default: return false;
980 // storage-class-specifier
981 case tok::kw_typedef:
982 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +0000983 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +0000984 case tok::kw_static:
985 case tok::kw_auto:
986 case tok::kw_register:
987 case tok::kw___thread:
988
989 // type-specifiers
990 case tok::kw_short:
991 case tok::kw_long:
992 case tok::kw_signed:
993 case tok::kw_unsigned:
994 case tok::kw__Complex:
995 case tok::kw__Imaginary:
996 case tok::kw_void:
997 case tok::kw_char:
998 case tok::kw_int:
999 case tok::kw_float:
1000 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001001 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001002 case tok::kw__Bool:
1003 case tok::kw__Decimal32:
1004 case tok::kw__Decimal64:
1005 case tok::kw__Decimal128:
1006
Chris Lattner2e78db32008-04-13 18:59:07 +00001007 // struct-or-union-specifier (C99) or class-specifier (C++)
1008 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001009 case tok::kw_struct:
1010 case tok::kw_union:
1011 // enum-specifier
1012 case tok::kw_enum:
1013
1014 // type-qualifier
1015 case tok::kw_const:
1016 case tok::kw_volatile:
1017 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001018
Chris Lattner4b009652007-07-25 00:24:17 +00001019 // function-specifier
1020 case tok::kw_inline:
Chris Lattnere35d2582007-08-09 16:40:21 +00001021
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001022 // GNU typeof support.
1023 case tok::kw_typeof:
1024
1025 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001026 case tok::kw___attribute:
Steve Naroff5f0466b2008-06-05 00:02:44 +00001027
1028 // GNU bizarre protocol extension. FIXME: make an extension?
1029 case tok::less:
Chris Lattner4b009652007-07-25 00:24:17 +00001030 return true;
1031
1032 // typedef-name
1033 case tok::identifier:
1034 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001035 }
1036}
1037
1038
1039/// ParseTypeQualifierListOpt
1040/// type-qualifier-list: [C99 6.7.5]
1041/// type-qualifier
1042/// [GNU] attributes
1043/// type-qualifier-list type-qualifier
1044/// [GNU] type-qualifier-list attributes
1045///
1046void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1047 while (1) {
1048 int isInvalid = false;
1049 const char *PrevSpec = 0;
1050 SourceLocation Loc = Tok.getLocation();
1051
1052 switch (Tok.getKind()) {
1053 default:
1054 // If this is not a type-qualifier token, we're done reading type
1055 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +00001056 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +00001057 return;
1058 case tok::kw_const:
1059 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1060 getLang())*2;
1061 break;
1062 case tok::kw_volatile:
1063 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1064 getLang())*2;
1065 break;
1066 case tok::kw_restrict:
1067 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1068 getLang())*2;
1069 break;
1070 case tok::kw___attribute:
1071 DS.AddAttributes(ParseAttributes());
1072 continue; // do *not* consume the next token!
1073 }
1074
1075 // If the specifier combination wasn't legal, issue a diagnostic.
1076 if (isInvalid) {
1077 assert(PrevSpec && "Method did not return previous specifier!");
1078 if (isInvalid == 1) // Error.
1079 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1080 else // extwarn.
1081 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1082 }
1083 ConsumeToken();
1084 }
1085}
1086
1087
1088/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1089///
1090void Parser::ParseDeclarator(Declarator &D) {
1091 /// This implements the 'declarator' production in the C grammar, then checks
1092 /// for well-formedness and issues diagnostics.
1093 ParseDeclaratorInternal(D);
Chris Lattner4b009652007-07-25 00:24:17 +00001094}
1095
1096/// ParseDeclaratorInternal
1097/// declarator: [C99 6.7.5]
1098/// pointer[opt] direct-declarator
1099/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1100/// [GNU] '&' restrict[opt] attributes[opt] declarator
1101///
1102/// pointer: [C99 6.7.5]
1103/// '*' type-qualifier-list[opt]
1104/// '*' type-qualifier-list[opt] pointer
1105///
1106void Parser::ParseDeclaratorInternal(Declarator &D) {
1107 tok::TokenKind Kind = Tok.getKind();
1108
1109 // Not a pointer or C++ reference.
Chris Lattner69f01932008-02-21 01:32:26 +00001110 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus))
Chris Lattner4b009652007-07-25 00:24:17 +00001111 return ParseDirectDeclarator(D);
1112
1113 // Otherwise, '*' -> pointer or '&' -> reference.
1114 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1115
1116 if (Kind == tok::star) {
Chris Lattner69f01932008-02-21 01:32:26 +00001117 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001118 DeclSpec DS;
1119
1120 ParseTypeQualifierListOpt(DS);
1121
1122 // Recursively parse the declarator.
1123 ParseDeclaratorInternal(D);
1124
1125 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001126 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1127 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001128 } else {
1129 // Is a reference
1130 DeclSpec DS;
1131
1132 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1133 // cv-qualifiers are introduced through the use of a typedef or of a
1134 // template type argument, in which case the cv-qualifiers are ignored.
1135 //
1136 // [GNU] Retricted references are allowed.
1137 // [GNU] Attributes on references are allowed.
1138 ParseTypeQualifierListOpt(DS);
1139
1140 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1141 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1142 Diag(DS.getConstSpecLoc(),
1143 diag::err_invalid_reference_qualifier_application,
1144 "const");
1145 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1146 Diag(DS.getVolatileSpecLoc(),
1147 diag::err_invalid_reference_qualifier_application,
1148 "volatile");
1149 }
1150
1151 // Recursively parse the declarator.
1152 ParseDeclaratorInternal(D);
1153
1154 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001155 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1156 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001157 }
1158}
1159
1160/// ParseDirectDeclarator
1161/// direct-declarator: [C99 6.7.5]
1162/// identifier
1163/// '(' declarator ')'
1164/// [GNU] '(' attributes declarator ')'
1165/// [C90] direct-declarator '[' constant-expression[opt] ']'
1166/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1167/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1168/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1169/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1170/// direct-declarator '(' parameter-type-list ')'
1171/// direct-declarator '(' identifier-list[opt] ')'
1172/// [GNU] direct-declarator '(' parameter-forward-declarations
1173/// parameter-type-list[opt] ')'
1174///
1175void Parser::ParseDirectDeclarator(Declarator &D) {
1176 // Parse the first direct-declarator seen.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001177 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001178 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1179 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1180 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001181 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001182 // direct-declarator: '(' declarator ')'
1183 // direct-declarator: '(' attributes declarator ')'
1184 // Example: 'char (*X)' or 'int (*XX)(void)'
1185 ParseParenDeclarator(D);
1186 } else if (D.mayOmitIdentifier()) {
1187 // This could be something simple like "int" (in which case the declarator
1188 // portion is empty), if an abstract-declarator is allowed.
1189 D.SetIdentifier(0, Tok.getLocation());
1190 } else {
1191 // Expected identifier or '('.
1192 Diag(Tok, diag::err_expected_ident_lparen);
1193 D.SetIdentifier(0, Tok.getLocation());
1194 }
1195
1196 assert(D.isPastIdentifier() &&
1197 "Haven't past the location of the identifier yet?");
1198
1199 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001200 if (Tok.is(tok::l_paren)) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00001201 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001202 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001203 ParseBracketDeclarator(D);
1204 } else {
1205 break;
1206 }
1207 }
1208}
1209
Chris Lattnera0d056d2008-04-06 05:45:57 +00001210/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1211/// only called before the identifier, so these are most likely just grouping
1212/// parens for precedence. If we find that these are actually function
1213/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1214///
1215/// direct-declarator:
1216/// '(' declarator ')'
1217/// [GNU] '(' attributes declarator ')'
1218///
1219void Parser::ParseParenDeclarator(Declarator &D) {
1220 SourceLocation StartLoc = ConsumeParen();
1221 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1222
1223 // If we haven't past the identifier yet (or where the identifier would be
1224 // stored, if this is an abstract declarator), then this is probably just
1225 // grouping parens. However, if this could be an abstract-declarator, then
1226 // this could also be the start of function arguments (consider 'void()').
1227 bool isGrouping;
1228
1229 if (!D.mayOmitIdentifier()) {
1230 // If this can't be an abstract-declarator, this *must* be a grouping
1231 // paren, because we haven't seen the identifier yet.
1232 isGrouping = true;
1233 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
1234 isDeclarationSpecifier()) { // 'int(int)' is a function.
1235 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1236 // considered to be a type, not a K&R identifier-list.
1237 isGrouping = false;
1238 } else {
1239 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1240 isGrouping = true;
1241 }
1242
1243 // If this is a grouping paren, handle:
1244 // direct-declarator: '(' declarator ')'
1245 // direct-declarator: '(' attributes declarator ')'
1246 if (isGrouping) {
1247 if (Tok.is(tok::kw___attribute))
1248 D.AddAttributes(ParseAttributes());
1249
1250 ParseDeclaratorInternal(D);
1251 // Match the ')'.
1252 MatchRHSPunctuation(tok::r_paren, StartLoc);
1253 return;
1254 }
1255
1256 // Okay, if this wasn't a grouping paren, it must be the start of a function
1257 // argument list. Recognize that this declarator will never have an
1258 // identifier (and remember where it would have been), then fall through to
1259 // the handling of argument lists.
1260 D.SetIdentifier(0, Tok.getLocation());
1261
1262 ParseFunctionDeclarator(StartLoc, D);
1263}
1264
1265/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1266/// declarator D up to a paren, which indicates that we are parsing function
1267/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001268///
1269/// This method also handles this portion of the grammar:
1270/// parameter-type-list: [C99 6.7.5]
1271/// parameter-list
1272/// parameter-list ',' '...'
1273///
1274/// parameter-list: [C99 6.7.5]
1275/// parameter-declaration
1276/// parameter-list ',' parameter-declaration
1277///
1278/// parameter-declaration: [C99 6.7.5]
1279/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00001280/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001281/// [GNU] declaration-specifiers declarator attributes
1282/// declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00001283/// [C++] declaration-specifiers abstract-declarator[opt]
1284/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001285/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1286///
Chris Lattnera0d056d2008-04-06 05:45:57 +00001287void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D) {
1288 // lparen is already consumed!
1289 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00001290
1291 // Okay, this is the parameter list of a function definition, or it is an
1292 // identifier list of a K&R-style function.
Chris Lattner4b009652007-07-25 00:24:17 +00001293
Chris Lattner34a01ad2007-10-09 17:33:22 +00001294 if (Tok.is(tok::r_paren)) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00001295 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00001296 // int() -> no prototype, no '...'.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001297 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/ false,
1298 /*variadic*/ false,
1299 /*arglist*/ 0, 0, LParenLoc));
1300
1301 ConsumeParen(); // Eat the closing ')'.
1302 return;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001303 } else if (Tok.is(tok::identifier) &&
Chris Lattner4b009652007-07-25 00:24:17 +00001304 // K&R identifier lists can't have typedefs as identifiers, per
1305 // C99 6.7.5.3p11.
1306 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1307 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1308 // normal declarators, not for abstract-declarators.
Chris Lattner35d9c912008-04-06 06:34:08 +00001309 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001310 }
1311
1312 // Finally, a normal, non-empty parameter type list.
1313
1314 // Build up an array of information about the parsed arguments.
1315 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00001316
1317 // Enter function-declaration scope, limiting any declarators to the
1318 // function prototype scope, including parameter declarators.
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00001319 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001320
1321 bool IsVariadic = false;
1322 while (1) {
1323 if (Tok.is(tok::ellipsis)) {
1324 IsVariadic = true;
Chris Lattner4b009652007-07-25 00:24:17 +00001325
Chris Lattner9f7564b2008-04-06 06:57:35 +00001326 // Check to see if this is "void(...)" which is not allowed.
1327 if (ParamInfo.empty()) {
1328 // Otherwise, parse parameter type list. If it starts with an
1329 // ellipsis, diagnose the malformed function.
1330 Diag(Tok, diag::err_ellipsis_first_arg);
1331 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001332 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00001333
Chris Lattner9f7564b2008-04-06 06:57:35 +00001334 ConsumeToken(); // Consume the ellipsis.
1335 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001336 }
1337
Chris Lattner9f7564b2008-04-06 06:57:35 +00001338 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00001339
Chris Lattner9f7564b2008-04-06 06:57:35 +00001340 // Parse the declaration-specifiers.
1341 DeclSpec DS;
1342 ParseDeclarationSpecifiers(DS);
1343
1344 // Parse the declarator. This is "PrototypeContext", because we must
1345 // accept either 'declarator' or 'abstract-declarator' here.
1346 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1347 ParseDeclarator(ParmDecl);
1348
1349 // Parse GNU attributes, if present.
1350 if (Tok.is(tok::kw___attribute))
1351 ParmDecl.AddAttributes(ParseAttributes());
1352
Chris Lattner9f7564b2008-04-06 06:57:35 +00001353 // Remember this parsed parameter in ParamInfo.
1354 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1355
Chris Lattner9f7564b2008-04-06 06:57:35 +00001356 // If no parameter was specified, verify that *something* was specified,
1357 // otherwise we have a missing type and identifier.
1358 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1359 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1360 // Completely missing, emit error.
1361 Diag(DSStart, diag::err_missing_param);
1362 } else {
1363 // Otherwise, we have something. Add it and let semantic analysis try
1364 // to grok it and add the result to the ParamInfo we are building.
1365
1366 // Inform the actions module about the parameter declarator, so it gets
1367 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00001368 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1369
1370 // Parse the default argument, if any. We parse the default
1371 // arguments in all dialects; the semantic analysis in
1372 // ActOnParamDefaultArgument will reject the default argument in
1373 // C.
1374 if (Tok.is(tok::equal)) {
1375 SourceLocation EqualLoc = Tok.getLocation();
1376
1377 // Consume the '='.
1378 ConsumeToken();
1379
1380 // Parse the default argument
Chris Lattner3e254fb2008-04-08 04:40:51 +00001381 ExprResult DefArgResult = ParseAssignmentExpression();
1382 if (DefArgResult.isInvalid) {
1383 SkipUntil(tok::comma, tok::r_paren, true, true);
1384 } else {
1385 // Inform the actions module about the default argument
1386 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1387 }
1388 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001389
1390 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001391 ParmDecl.getIdentifierLoc(), Param));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001392 }
1393
1394 // If the next token is a comma, consume it and keep reading arguments.
1395 if (Tok.isNot(tok::comma)) break;
1396
1397 // Consume the comma.
1398 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00001399 }
1400
Chris Lattner9f7564b2008-04-06 06:57:35 +00001401 // Leave prototype scope.
1402 ExitScope();
1403
Chris Lattner4b009652007-07-25 00:24:17 +00001404 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001405 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1406 &ParamInfo[0], ParamInfo.size(),
1407 LParenLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00001408
1409 // If we have the closing ')', eat it and we're done.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001410 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001411}
1412
Chris Lattner35d9c912008-04-06 06:34:08 +00001413/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1414/// we found a K&R-style identifier list instead of a type argument list. The
1415/// current token is known to be the first identifier in the list.
1416///
1417/// identifier-list: [C99 6.7.5]
1418/// identifier
1419/// identifier-list ',' identifier
1420///
1421void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1422 Declarator &D) {
1423 // Build up an array of information about the parsed arguments.
1424 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1425 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1426
1427 // If there was no identifier specified for the declarator, either we are in
1428 // an abstract-declarator, or we are in a parameter declarator which was found
1429 // to be abstract. In abstract-declarators, identifier lists are not valid:
1430 // diagnose this.
1431 if (!D.getIdentifier())
1432 Diag(Tok, diag::ext_ident_list_in_param);
1433
1434 // Tok is known to be the first identifier in the list. Remember this
1435 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00001436 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00001437 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1438 Tok.getLocation(), 0));
1439
Chris Lattner113a56b2008-04-06 06:39:19 +00001440 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00001441
1442 while (Tok.is(tok::comma)) {
1443 // Eat the comma.
1444 ConsumeToken();
1445
Chris Lattner113a56b2008-04-06 06:39:19 +00001446 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00001447 if (Tok.isNot(tok::identifier)) {
1448 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00001449 SkipUntil(tok::r_paren);
1450 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00001451 }
Chris Lattneracb67d92008-04-06 06:47:48 +00001452
Chris Lattner35d9c912008-04-06 06:34:08 +00001453 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00001454
1455 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1456 if (Actions.isTypeName(*ParmII, CurScope))
1457 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner35d9c912008-04-06 06:34:08 +00001458
1459 // Verify that the argument identifier has not already been mentioned.
1460 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner113a56b2008-04-06 06:39:19 +00001461 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1462 } else {
1463 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00001464 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1465 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00001466 }
Chris Lattner35d9c912008-04-06 06:34:08 +00001467
1468 // Eat the identifier.
1469 ConsumeToken();
1470 }
1471
Chris Lattner113a56b2008-04-06 06:39:19 +00001472 // Remember that we parsed a function type, and remember the attributes. This
1473 // function type is always a K&R style function type, which is not varargs and
1474 // has no prototype.
1475 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1476 &ParamInfo[0], ParamInfo.size(),
1477 LParenLoc));
Chris Lattner35d9c912008-04-06 06:34:08 +00001478
1479 // If we have the closing ')', eat it and we're done.
Chris Lattner113a56b2008-04-06 06:39:19 +00001480 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00001481}
Chris Lattnera0d056d2008-04-06 05:45:57 +00001482
Chris Lattner4b009652007-07-25 00:24:17 +00001483/// [C90] direct-declarator '[' constant-expression[opt] ']'
1484/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1485/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1486/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1487/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1488void Parser::ParseBracketDeclarator(Declarator &D) {
1489 SourceLocation StartLoc = ConsumeBracket();
1490
1491 // If valid, this location is the position where we read the 'static' keyword.
1492 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001493 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001494 StaticLoc = ConsumeToken();
1495
1496 // If there is a type-qualifier-list, read it now.
1497 DeclSpec DS;
1498 ParseTypeQualifierListOpt(DS);
1499
1500 // If we haven't already read 'static', check to see if there is one after the
1501 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001502 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001503 StaticLoc = ConsumeToken();
1504
1505 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1506 bool isStar = false;
1507 ExprResult NumElements(false);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001508
1509 // Handle the case where we have '[*]' as the array size. However, a leading
1510 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1511 // the the token after the star is a ']'. Since stars in arrays are
1512 // infrequent, use of lookahead is not costly here.
1513 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00001514 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00001515
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001516 if (StaticLoc.isValid())
1517 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1518 StaticLoc = SourceLocation(); // Drop the static.
1519 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001520 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001521 // Parse the assignment-expression now.
1522 NumElements = ParseAssignmentExpression();
1523 }
1524
1525 // If there was an error parsing the assignment-expression, recover.
1526 if (NumElements.isInvalid) {
1527 // If the expression was invalid, skip it.
1528 SkipUntil(tok::r_square);
1529 return;
1530 }
1531
1532 MatchRHSPunctuation(tok::r_square, StartLoc);
1533
1534 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1535 // it was not a constant expression.
1536 if (!getLang().C99) {
1537 // TODO: check C90 array constant exprness.
1538 if (isStar || StaticLoc.isValid() ||
1539 0/*TODO: NumElts is not a C90 constantexpr */)
1540 Diag(StartLoc, diag::ext_c99_array_usage);
1541 }
1542
1543 // Remember that we parsed a pointer type, and remember the type-quals.
1544 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1545 StaticLoc.isValid(), isStar,
1546 NumElements.Val, StartLoc));
1547}
1548
Steve Naroff7cbb1462007-07-31 12:34:36 +00001549/// [GNU] typeof-specifier:
1550/// typeof ( expressions )
1551/// typeof ( type-name )
1552///
1553void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001554 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00001555 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00001556 SourceLocation StartLoc = ConsumeToken();
1557
Chris Lattner34a01ad2007-10-09 17:33:22 +00001558 if (Tok.isNot(tok::l_paren)) {
Steve Naroff14bbce82007-08-02 02:53:48 +00001559 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1560 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00001561 }
1562 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1563
1564 if (isTypeSpecifierQualifier()) {
1565 TypeTy *Ty = ParseTypeName();
1566
Steve Naroff4c255ab2007-07-31 23:56:32 +00001567 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1568
Chris Lattner34a01ad2007-10-09 17:33:22 +00001569 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001570 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001571 return;
1572 }
1573 RParenLoc = ConsumeParen();
1574 const char *PrevSpec = 0;
1575 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1576 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1577 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001578 } else { // we have an expression.
1579 ExprResult Result = ParseExpression();
Steve Naroff4c255ab2007-07-31 23:56:32 +00001580
Chris Lattner34a01ad2007-10-09 17:33:22 +00001581 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001582 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001583 return;
1584 }
1585 RParenLoc = ConsumeParen();
1586 const char *PrevSpec = 0;
1587 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1588 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1589 Result.Val))
1590 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001591 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00001592}
1593
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00001594