blob: 81cb0ce627169c7250c78e6d8248c3c42664f092 [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!
Argiris Kirtzidis46403632008-08-01 10:35:27 +0000414 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattnerfda18db2008-07-26 01:18:38 +0000415 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;
Chris Lattnerada63792008-07-26 01:53:50 +0000434 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000435 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000436 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000437
438 DS.SetRangeEnd(EndProtoLoc);
439
440 // Do not allow any other declspecs after the protocol qualifier list
441 // "<foo,bar>short" is not allowed.
442 goto DoneWithDeclSpec;
443 }
Chris Lattner4b009652007-07-25 00:24:17 +0000444 // GNU attributes support.
445 case tok::kw___attribute:
446 DS.AddAttributes(ParseAttributes());
447 continue;
448
449 // storage-class-specifier
450 case tok::kw_typedef:
451 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
452 break;
453 case tok::kw_extern:
454 if (DS.isThreadSpecified())
455 Diag(Tok, diag::ext_thread_before, "extern");
456 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
457 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000458 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000459 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
460 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000461 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000462 case tok::kw_static:
463 if (DS.isThreadSpecified())
464 Diag(Tok, diag::ext_thread_before, "static");
465 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
466 break;
467 case tok::kw_auto:
468 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
469 break;
470 case tok::kw_register:
471 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
472 break;
473 case tok::kw___thread:
474 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
475 break;
476
477 // type-specifiers
478 case tok::kw_short:
479 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
480 break;
481 case tok::kw_long:
482 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
483 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
484 else
485 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
486 break;
487 case tok::kw_signed:
488 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
489 break;
490 case tok::kw_unsigned:
491 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
492 break;
493 case tok::kw__Complex:
494 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
495 break;
496 case tok::kw__Imaginary:
497 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
498 break;
499 case tok::kw_void:
500 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
501 break;
502 case tok::kw_char:
503 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
504 break;
505 case tok::kw_int:
506 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
507 break;
508 case tok::kw_float:
509 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
510 break;
511 case tok::kw_double:
512 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
513 break;
514 case tok::kw_bool: // [C++ 2.11p1]
515 case tok::kw__Bool:
516 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
517 break;
518 case tok::kw__Decimal32:
519 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
520 break;
521 case tok::kw__Decimal64:
522 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
523 break;
524 case tok::kw__Decimal128:
525 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
526 break;
Chris Lattner2e78db32008-04-13 18:59:07 +0000527
528 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000529 case tok::kw_struct:
530 case tok::kw_union:
Douglas Gregorec93f442008-04-13 21:30:24 +0000531 ParseClassSpecifier(DS);
Chris Lattner4b009652007-07-25 00:24:17 +0000532 continue;
533 case tok::kw_enum:
534 ParseEnumSpecifier(DS);
535 continue;
536
Steve Naroff7cbb1462007-07-31 12:34:36 +0000537 // GNU typeof support.
538 case tok::kw_typeof:
539 ParseTypeofSpecifier(DS);
540 continue;
541
Chris Lattner4b009652007-07-25 00:24:17 +0000542 // type-qualifier
543 case tok::kw_const:
544 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
545 getLang())*2;
546 break;
547 case tok::kw_volatile:
548 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
549 getLang())*2;
550 break;
551 case tok::kw_restrict:
552 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
553 getLang())*2;
554 break;
555
556 // function-specifier
557 case tok::kw_inline:
558 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
559 break;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000560
Steve Naroff5f0466b2008-06-05 00:02:44 +0000561 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000562 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000563 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
564 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000565 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000566 goto DoneWithDeclSpec;
567
568 {
569 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000570 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000571 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000572 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000573 DS.SetRangeEnd(EndProtoLoc);
574
Chris Lattnerb99d7492008-07-26 00:20:22 +0000575 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id,
576 SourceRange(Loc, EndProtoLoc));
Chris Lattnerfda18db2008-07-26 01:18:38 +0000577 // Do not allow any other declspecs after the protocol qualifier list
578 // "<foo,bar>short" is not allowed.
579 goto DoneWithDeclSpec;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000580 }
Chris Lattner4b009652007-07-25 00:24:17 +0000581 }
582 // If the specifier combination wasn't legal, issue a diagnostic.
583 if (isInvalid) {
584 assert(PrevSpec && "Method did not return previous specifier!");
585 if (isInvalid == 1) // Error.
586 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
587 else // extwarn.
588 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
589 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000590 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000591 ConsumeToken();
592 }
593}
594
595/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
596/// the first token has already been read and has been turned into an instance
597/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
598/// otherwise it returns false and fills in Decl.
599bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
600 AttributeList *Attr = 0;
601 // If attributes exist after tag, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000602 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000603 Attr = ParseAttributes();
604
605 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000606 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000607 Diag(Tok, diag::err_expected_ident_lbrace);
608
609 // Skip the rest of this declarator, up until the comma or semicolon.
610 SkipUntil(tok::comma, true);
611 return true;
612 }
613
614 // If an identifier is present, consume and remember it.
615 IdentifierInfo *Name = 0;
616 SourceLocation NameLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000617 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000618 Name = Tok.getIdentifierInfo();
619 NameLoc = ConsumeToken();
620 }
621
622 // There are three options here. If we have 'struct foo;', then this is a
623 // forward declaration. If we have 'struct foo {...' then this is a
624 // definition. Otherwise we have something like 'struct foo xyz', a reference.
625 //
626 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
627 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
628 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
629 //
630 Action::TagKind TK;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000631 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000632 TK = Action::TK_Definition;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000633 else if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000634 TK = Action::TK_Declaration;
635 else
636 TK = Action::TK_Reference;
Steve Naroff0acc9c92007-09-15 18:49:24 +0000637 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Chris Lattner4b009652007-07-25 00:24:17 +0000638 return false;
639}
640
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000641/// ParseStructDeclaration - Parse a struct declaration without the terminating
642/// semicolon.
643///
Chris Lattner4b009652007-07-25 00:24:17 +0000644/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000645/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000646/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000647/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000648/// struct-declarator-list:
649/// struct-declarator
650/// struct-declarator-list ',' struct-declarator
651/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
652/// struct-declarator:
653/// declarator
654/// [GNU] declarator attributes[opt]
655/// declarator[opt] ':' constant-expression
656/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
657///
Chris Lattner3dd8d392008-04-10 06:46:29 +0000658void Parser::
659ParseStructDeclaration(DeclSpec &DS,
660 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000661 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattner3dd8d392008-04-10 06:46:29 +0000662 while (Tok.is(tok::kw___extension__))
Steve Naroffa9adf112007-08-20 22:28:22 +0000663 ConsumeToken();
664
665 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000666 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +0000667 ParseSpecifierQualifierList(DS);
668 // TODO: Does specifier-qualifier list correctly check that *something* is
669 // specified?
670
671 // If there are no declarators, issue a warning.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000672 if (Tok.is(tok::semi)) {
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000673 Diag(DSStart, diag::w_no_declarators);
Steve Naroffa9adf112007-08-20 22:28:22 +0000674 return;
675 }
676
677 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000678 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000679 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +0000680 FieldDeclarator &DeclaratorInfo = Fields.back();
681
Steve Naroffa9adf112007-08-20 22:28:22 +0000682 /// struct-declarator: declarator
683 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000684 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000685 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +0000686
Chris Lattner34a01ad2007-10-09 17:33:22 +0000687 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000688 ConsumeToken();
689 ExprResult Res = ParseConstantExpression();
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000690 if (Res.isInvalid)
Steve Naroffa9adf112007-08-20 22:28:22 +0000691 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000692 else
Chris Lattner3dd8d392008-04-10 06:46:29 +0000693 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroffa9adf112007-08-20 22:28:22 +0000694 }
695
696 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000697 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000698 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000699
700 // If we don't have a comma, it is either the end of the list (a ';')
701 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000702 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000703 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000704
705 // Consume the comma.
706 ConsumeToken();
707
708 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000709 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000710
711 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000712 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000713 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000714 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000715}
716
717/// ParseStructUnionBody
718/// struct-contents:
719/// struct-declaration-list
720/// [EXT] empty
721/// [GNU] "struct-declaration-list" without terminatoring ';'
722/// struct-declaration-list:
723/// struct-declaration
724/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +0000725/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +0000726///
Chris Lattner4b009652007-07-25 00:24:17 +0000727void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
728 unsigned TagType, DeclTy *TagDecl) {
729 SourceLocation LBraceLoc = ConsumeBrace();
730
731 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
732 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +0000733 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000734 Diag(Tok, diag::ext_empty_struct_union_enum,
735 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
736
737 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000738 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
739
Chris Lattner4b009652007-07-25 00:24:17 +0000740 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000741 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000742 // Each iteration of this loop reads one struct-declaration.
743
744 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000745 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000746 Diag(Tok, diag::ext_extra_struct_semi);
747 ConsumeToken();
748 continue;
749 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000750
751 // Parse all the comma separated declarators.
752 DeclSpec DS;
753 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +0000754 if (!Tok.is(tok::at)) {
755 ParseStructDeclaration(DS, FieldDeclarators);
756
757 // Convert them all to fields.
758 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
759 FieldDeclarator &FD = FieldDeclarators[i];
760 // Install the declarator into the current TagDecl.
761 DeclTy *Field = Actions.ActOnField(CurScope,
762 DS.getSourceRange().getBegin(),
763 FD.D, FD.BitfieldSize);
764 FieldDecls.push_back(Field);
765 }
766 } else { // Handle @defs
767 ConsumeToken();
768 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
769 Diag(Tok, diag::err_unexpected_at);
770 SkipUntil(tok::semi, true, true);
771 continue;
772 }
773 ConsumeToken();
774 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
775 if (!Tok.is(tok::identifier)) {
776 Diag(Tok, diag::err_expected_ident);
777 SkipUntil(tok::semi, true, true);
778 continue;
779 }
780 llvm::SmallVector<DeclTy*, 16> Fields;
781 Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
782 Fields);
783 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
784 ConsumeToken();
785 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
786 }
Chris Lattner4b009652007-07-25 00:24:17 +0000787
Chris Lattner34a01ad2007-10-09 17:33:22 +0000788 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000789 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +0000790 } else if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000791 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
792 break;
793 } else {
794 Diag(Tok, diag::err_expected_semi_decl_list);
795 // Skip to end of block or statement
796 SkipUntil(tok::r_brace, true, true);
797 }
798 }
799
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000800 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000801
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000802 Actions.ActOnFields(CurScope,
Chris Lattner43b885f2008-02-25 21:04:36 +0000803 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000804 LBraceLoc, RBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000805
806 AttributeList *AttrList = 0;
807 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000808 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000809 AttrList = ParseAttributes(); // FIXME: where should I put them?
810}
811
812
813/// ParseEnumSpecifier
814/// enum-specifier: [C99 6.7.2.2]
815/// 'enum' identifier[opt] '{' enumerator-list '}'
816/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
817/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
818/// '}' attributes[opt]
819/// 'enum' identifier
820/// [GNU] 'enum' attributes[opt] identifier
821void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000822 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000823 SourceLocation StartLoc = ConsumeToken();
824
825 // Parse the tag portion of this.
826 DeclTy *TagDecl;
827 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
828 return;
829
Chris Lattner34a01ad2007-10-09 17:33:22 +0000830 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000831 ParseEnumBody(StartLoc, TagDecl);
832
833 // TODO: semantic analysis on the declspec for enums.
834 const char *PrevSpec = 0;
835 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
836 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
837}
838
839/// ParseEnumBody - Parse a {} enclosed enumerator-list.
840/// enumerator-list:
841/// enumerator
842/// enumerator-list ',' enumerator
843/// enumerator:
844/// enumeration-constant
845/// enumeration-constant '=' constant-expression
846/// enumeration-constant:
847/// identifier
848///
849void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
850 SourceLocation LBraceLoc = ConsumeBrace();
851
Chris Lattnerc9a92452007-08-27 17:24:30 +0000852 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +0000853 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000854 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
855
856 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
857
858 DeclTy *LastEnumConstDecl = 0;
859
860 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000861 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000862 IdentifierInfo *Ident = Tok.getIdentifierInfo();
863 SourceLocation IdentLoc = ConsumeToken();
864
865 SourceLocation EqualLoc;
866 ExprTy *AssignedVal = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000867 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000868 EqualLoc = ConsumeToken();
869 ExprResult Res = ParseConstantExpression();
870 if (Res.isInvalid)
871 SkipUntil(tok::comma, tok::r_brace, true, true);
872 else
873 AssignedVal = Res.Val;
874 }
875
876 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000877 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +0000878 LastEnumConstDecl,
879 IdentLoc, Ident,
880 EqualLoc, AssignedVal);
881 EnumConstantDecls.push_back(EnumConstDecl);
882 LastEnumConstDecl = EnumConstDecl;
883
Chris Lattner34a01ad2007-10-09 17:33:22 +0000884 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000885 break;
886 SourceLocation CommaLoc = ConsumeToken();
887
Chris Lattner34a01ad2007-10-09 17:33:22 +0000888 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +0000889 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
890 }
891
892 // Eat the }.
893 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
894
Steve Naroff0acc9c92007-09-15 18:49:24 +0000895 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +0000896 EnumConstantDecls.size());
897
898 DeclTy *AttrList = 0;
899 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000900 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000901 AttrList = ParseAttributes(); // FIXME: where do they do?
902}
903
904/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +0000905/// start of a type-qualifier-list.
906bool Parser::isTypeQualifier() const {
907 switch (Tok.getKind()) {
908 default: return false;
909 // type-qualifier
910 case tok::kw_const:
911 case tok::kw_volatile:
912 case tok::kw_restrict:
913 return true;
914 }
915}
916
917/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +0000918/// start of a specifier-qualifier-list.
919bool Parser::isTypeSpecifierQualifier() const {
920 switch (Tok.getKind()) {
921 default: return false;
922 // GNU attributes support.
923 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000924 // GNU typeof support.
925 case tok::kw_typeof:
Steve Naroff5f0466b2008-06-05 00:02:44 +0000926 // GNU bizarre protocol extension. FIXME: make an extension?
927 case tok::less:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000928
Chris Lattner4b009652007-07-25 00:24:17 +0000929 // type-specifiers
930 case tok::kw_short:
931 case tok::kw_long:
932 case tok::kw_signed:
933 case tok::kw_unsigned:
934 case tok::kw__Complex:
935 case tok::kw__Imaginary:
936 case tok::kw_void:
937 case tok::kw_char:
938 case tok::kw_int:
939 case tok::kw_float:
940 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000941 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +0000942 case tok::kw__Bool:
943 case tok::kw__Decimal32:
944 case tok::kw__Decimal64:
945 case tok::kw__Decimal128:
946
Chris Lattner2e78db32008-04-13 18:59:07 +0000947 // struct-or-union-specifier (C99) or class-specifier (C++)
948 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000949 case tok::kw_struct:
950 case tok::kw_union:
951 // enum-specifier
952 case tok::kw_enum:
953
954 // type-qualifier
955 case tok::kw_const:
956 case tok::kw_volatile:
957 case tok::kw_restrict:
958 return true;
959
960 // typedef-name
961 case tok::identifier:
962 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000963 }
964}
965
966/// isDeclarationSpecifier() - Return true if the current token is part of a
967/// declaration specifier.
968bool Parser::isDeclarationSpecifier() const {
969 switch (Tok.getKind()) {
970 default: return false;
971 // storage-class-specifier
972 case tok::kw_typedef:
973 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +0000974 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +0000975 case tok::kw_static:
976 case tok::kw_auto:
977 case tok::kw_register:
978 case tok::kw___thread:
979
980 // type-specifiers
981 case tok::kw_short:
982 case tok::kw_long:
983 case tok::kw_signed:
984 case tok::kw_unsigned:
985 case tok::kw__Complex:
986 case tok::kw__Imaginary:
987 case tok::kw_void:
988 case tok::kw_char:
989 case tok::kw_int:
990 case tok::kw_float:
991 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000992 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +0000993 case tok::kw__Bool:
994 case tok::kw__Decimal32:
995 case tok::kw__Decimal64:
996 case tok::kw__Decimal128:
997
Chris Lattner2e78db32008-04-13 18:59:07 +0000998 // struct-or-union-specifier (C99) or class-specifier (C++)
999 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001000 case tok::kw_struct:
1001 case tok::kw_union:
1002 // enum-specifier
1003 case tok::kw_enum:
1004
1005 // type-qualifier
1006 case tok::kw_const:
1007 case tok::kw_volatile:
1008 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001009
Chris Lattner4b009652007-07-25 00:24:17 +00001010 // function-specifier
1011 case tok::kw_inline:
Chris Lattnere35d2582007-08-09 16:40:21 +00001012
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001013 // GNU typeof support.
1014 case tok::kw_typeof:
1015
1016 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001017 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001018 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001019
1020 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1021 case tok::less:
1022 return getLang().ObjC1;
Chris Lattner4b009652007-07-25 00:24:17 +00001023
1024 // typedef-name
1025 case tok::identifier:
1026 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001027 }
1028}
1029
1030
1031/// ParseTypeQualifierListOpt
1032/// type-qualifier-list: [C99 6.7.5]
1033/// type-qualifier
1034/// [GNU] attributes
1035/// type-qualifier-list type-qualifier
1036/// [GNU] type-qualifier-list attributes
1037///
1038void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1039 while (1) {
1040 int isInvalid = false;
1041 const char *PrevSpec = 0;
1042 SourceLocation Loc = Tok.getLocation();
1043
1044 switch (Tok.getKind()) {
1045 default:
1046 // If this is not a type-qualifier token, we're done reading type
1047 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +00001048 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +00001049 return;
1050 case tok::kw_const:
1051 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1052 getLang())*2;
1053 break;
1054 case tok::kw_volatile:
1055 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1056 getLang())*2;
1057 break;
1058 case tok::kw_restrict:
1059 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1060 getLang())*2;
1061 break;
1062 case tok::kw___attribute:
1063 DS.AddAttributes(ParseAttributes());
1064 continue; // do *not* consume the next token!
1065 }
1066
1067 // If the specifier combination wasn't legal, issue a diagnostic.
1068 if (isInvalid) {
1069 assert(PrevSpec && "Method did not return previous specifier!");
1070 if (isInvalid == 1) // Error.
1071 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1072 else // extwarn.
1073 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1074 }
1075 ConsumeToken();
1076 }
1077}
1078
1079
1080/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1081///
1082void Parser::ParseDeclarator(Declarator &D) {
1083 /// This implements the 'declarator' production in the C grammar, then checks
1084 /// for well-formedness and issues diagnostics.
1085 ParseDeclaratorInternal(D);
Chris Lattner4b009652007-07-25 00:24:17 +00001086}
1087
1088/// ParseDeclaratorInternal
1089/// declarator: [C99 6.7.5]
1090/// pointer[opt] direct-declarator
1091/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1092/// [GNU] '&' restrict[opt] attributes[opt] declarator
1093///
1094/// pointer: [C99 6.7.5]
1095/// '*' type-qualifier-list[opt]
1096/// '*' type-qualifier-list[opt] pointer
1097///
1098void Parser::ParseDeclaratorInternal(Declarator &D) {
1099 tok::TokenKind Kind = Tok.getKind();
1100
1101 // Not a pointer or C++ reference.
Chris Lattner69f01932008-02-21 01:32:26 +00001102 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus))
Chris Lattner4b009652007-07-25 00:24:17 +00001103 return ParseDirectDeclarator(D);
1104
1105 // Otherwise, '*' -> pointer or '&' -> reference.
1106 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1107
1108 if (Kind == tok::star) {
Chris Lattner69f01932008-02-21 01:32:26 +00001109 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001110 DeclSpec DS;
1111
1112 ParseTypeQualifierListOpt(DS);
1113
1114 // Recursively parse the declarator.
1115 ParseDeclaratorInternal(D);
1116
1117 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001118 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1119 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001120 } else {
1121 // Is a reference
1122 DeclSpec DS;
1123
1124 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1125 // cv-qualifiers are introduced through the use of a typedef or of a
1126 // template type argument, in which case the cv-qualifiers are ignored.
1127 //
1128 // [GNU] Retricted references are allowed.
1129 // [GNU] Attributes on references are allowed.
1130 ParseTypeQualifierListOpt(DS);
1131
1132 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1133 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1134 Diag(DS.getConstSpecLoc(),
1135 diag::err_invalid_reference_qualifier_application,
1136 "const");
1137 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1138 Diag(DS.getVolatileSpecLoc(),
1139 diag::err_invalid_reference_qualifier_application,
1140 "volatile");
1141 }
1142
1143 // Recursively parse the declarator.
1144 ParseDeclaratorInternal(D);
1145
1146 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001147 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1148 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001149 }
1150}
1151
1152/// ParseDirectDeclarator
1153/// direct-declarator: [C99 6.7.5]
1154/// identifier
1155/// '(' declarator ')'
1156/// [GNU] '(' attributes declarator ')'
1157/// [C90] direct-declarator '[' constant-expression[opt] ']'
1158/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1159/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1160/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1161/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1162/// direct-declarator '(' parameter-type-list ')'
1163/// direct-declarator '(' identifier-list[opt] ')'
1164/// [GNU] direct-declarator '(' parameter-forward-declarations
1165/// parameter-type-list[opt] ')'
1166///
1167void Parser::ParseDirectDeclarator(Declarator &D) {
1168 // Parse the first direct-declarator seen.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001169 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001170 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1171 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1172 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001173 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001174 // direct-declarator: '(' declarator ')'
1175 // direct-declarator: '(' attributes declarator ')'
1176 // Example: 'char (*X)' or 'int (*XX)(void)'
1177 ParseParenDeclarator(D);
1178 } else if (D.mayOmitIdentifier()) {
1179 // This could be something simple like "int" (in which case the declarator
1180 // portion is empty), if an abstract-declarator is allowed.
1181 D.SetIdentifier(0, Tok.getLocation());
1182 } else {
1183 // Expected identifier or '('.
1184 Diag(Tok, diag::err_expected_ident_lparen);
1185 D.SetIdentifier(0, Tok.getLocation());
1186 }
1187
1188 assert(D.isPastIdentifier() &&
1189 "Haven't past the location of the identifier yet?");
1190
1191 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001192 if (Tok.is(tok::l_paren)) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00001193 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001194 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001195 ParseBracketDeclarator(D);
1196 } else {
1197 break;
1198 }
1199 }
1200}
1201
Chris Lattnera0d056d2008-04-06 05:45:57 +00001202/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1203/// only called before the identifier, so these are most likely just grouping
1204/// parens for precedence. If we find that these are actually function
1205/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1206///
1207/// direct-declarator:
1208/// '(' declarator ')'
1209/// [GNU] '(' attributes declarator ')'
1210///
1211void Parser::ParseParenDeclarator(Declarator &D) {
1212 SourceLocation StartLoc = ConsumeParen();
1213 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1214
1215 // If we haven't past the identifier yet (or where the identifier would be
1216 // stored, if this is an abstract declarator), then this is probably just
1217 // grouping parens. However, if this could be an abstract-declarator, then
1218 // this could also be the start of function arguments (consider 'void()').
1219 bool isGrouping;
1220
1221 if (!D.mayOmitIdentifier()) {
1222 // If this can't be an abstract-declarator, this *must* be a grouping
1223 // paren, because we haven't seen the identifier yet.
1224 isGrouping = true;
1225 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
1226 isDeclarationSpecifier()) { // 'int(int)' is a function.
1227 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1228 // considered to be a type, not a K&R identifier-list.
1229 isGrouping = false;
1230 } else {
1231 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1232 isGrouping = true;
1233 }
1234
1235 // If this is a grouping paren, handle:
1236 // direct-declarator: '(' declarator ')'
1237 // direct-declarator: '(' attributes declarator ')'
1238 if (isGrouping) {
1239 if (Tok.is(tok::kw___attribute))
1240 D.AddAttributes(ParseAttributes());
1241
1242 ParseDeclaratorInternal(D);
1243 // Match the ')'.
1244 MatchRHSPunctuation(tok::r_paren, StartLoc);
1245 return;
1246 }
1247
1248 // Okay, if this wasn't a grouping paren, it must be the start of a function
1249 // argument list. Recognize that this declarator will never have an
1250 // identifier (and remember where it would have been), then fall through to
1251 // the handling of argument lists.
1252 D.SetIdentifier(0, Tok.getLocation());
1253
1254 ParseFunctionDeclarator(StartLoc, D);
1255}
1256
1257/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1258/// declarator D up to a paren, which indicates that we are parsing function
1259/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001260///
1261/// This method also handles this portion of the grammar:
1262/// parameter-type-list: [C99 6.7.5]
1263/// parameter-list
1264/// parameter-list ',' '...'
1265///
1266/// parameter-list: [C99 6.7.5]
1267/// parameter-declaration
1268/// parameter-list ',' parameter-declaration
1269///
1270/// parameter-declaration: [C99 6.7.5]
1271/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00001272/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001273/// [GNU] declaration-specifiers declarator attributes
1274/// declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00001275/// [C++] declaration-specifiers abstract-declarator[opt]
1276/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001277/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1278///
Chris Lattnera0d056d2008-04-06 05:45:57 +00001279void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D) {
1280 // lparen is already consumed!
1281 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00001282
1283 // Okay, this is the parameter list of a function definition, or it is an
1284 // identifier list of a K&R-style function.
Chris Lattner4b009652007-07-25 00:24:17 +00001285
Chris Lattner34a01ad2007-10-09 17:33:22 +00001286 if (Tok.is(tok::r_paren)) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00001287 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00001288 // int() -> no prototype, no '...'.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001289 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/ false,
1290 /*variadic*/ false,
1291 /*arglist*/ 0, 0, LParenLoc));
1292
1293 ConsumeParen(); // Eat the closing ')'.
1294 return;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001295 } else if (Tok.is(tok::identifier) &&
Chris Lattner4b009652007-07-25 00:24:17 +00001296 // K&R identifier lists can't have typedefs as identifiers, per
1297 // C99 6.7.5.3p11.
1298 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1299 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1300 // normal declarators, not for abstract-declarators.
Chris Lattner35d9c912008-04-06 06:34:08 +00001301 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001302 }
1303
1304 // Finally, a normal, non-empty parameter type list.
1305
1306 // Build up an array of information about the parsed arguments.
1307 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00001308
1309 // Enter function-declaration scope, limiting any declarators to the
1310 // function prototype scope, including parameter declarators.
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00001311 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001312
1313 bool IsVariadic = false;
1314 while (1) {
1315 if (Tok.is(tok::ellipsis)) {
1316 IsVariadic = true;
Chris Lattner4b009652007-07-25 00:24:17 +00001317
Chris Lattner9f7564b2008-04-06 06:57:35 +00001318 // Check to see if this is "void(...)" which is not allowed.
1319 if (ParamInfo.empty()) {
1320 // Otherwise, parse parameter type list. If it starts with an
1321 // ellipsis, diagnose the malformed function.
1322 Diag(Tok, diag::err_ellipsis_first_arg);
1323 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001324 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00001325
Chris Lattner9f7564b2008-04-06 06:57:35 +00001326 ConsumeToken(); // Consume the ellipsis.
1327 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001328 }
1329
Chris Lattner9f7564b2008-04-06 06:57:35 +00001330 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00001331
Chris Lattner9f7564b2008-04-06 06:57:35 +00001332 // Parse the declaration-specifiers.
1333 DeclSpec DS;
1334 ParseDeclarationSpecifiers(DS);
1335
1336 // Parse the declarator. This is "PrototypeContext", because we must
1337 // accept either 'declarator' or 'abstract-declarator' here.
1338 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1339 ParseDeclarator(ParmDecl);
1340
1341 // Parse GNU attributes, if present.
1342 if (Tok.is(tok::kw___attribute))
1343 ParmDecl.AddAttributes(ParseAttributes());
1344
Chris Lattner9f7564b2008-04-06 06:57:35 +00001345 // Remember this parsed parameter in ParamInfo.
1346 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1347
Chris Lattner9f7564b2008-04-06 06:57:35 +00001348 // If no parameter was specified, verify that *something* was specified,
1349 // otherwise we have a missing type and identifier.
1350 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1351 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1352 // Completely missing, emit error.
1353 Diag(DSStart, diag::err_missing_param);
1354 } else {
1355 // Otherwise, we have something. Add it and let semantic analysis try
1356 // to grok it and add the result to the ParamInfo we are building.
1357
1358 // Inform the actions module about the parameter declarator, so it gets
1359 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00001360 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1361
1362 // Parse the default argument, if any. We parse the default
1363 // arguments in all dialects; the semantic analysis in
1364 // ActOnParamDefaultArgument will reject the default argument in
1365 // C.
1366 if (Tok.is(tok::equal)) {
1367 SourceLocation EqualLoc = Tok.getLocation();
1368
1369 // Consume the '='.
1370 ConsumeToken();
1371
1372 // Parse the default argument
Chris Lattner3e254fb2008-04-08 04:40:51 +00001373 ExprResult DefArgResult = ParseAssignmentExpression();
1374 if (DefArgResult.isInvalid) {
1375 SkipUntil(tok::comma, tok::r_paren, true, true);
1376 } else {
1377 // Inform the actions module about the default argument
1378 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1379 }
1380 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001381
1382 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001383 ParmDecl.getIdentifierLoc(), Param));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001384 }
1385
1386 // If the next token is a comma, consume it and keep reading arguments.
1387 if (Tok.isNot(tok::comma)) break;
1388
1389 // Consume the comma.
1390 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00001391 }
1392
Chris Lattner9f7564b2008-04-06 06:57:35 +00001393 // Leave prototype scope.
1394 ExitScope();
1395
Chris Lattner4b009652007-07-25 00:24:17 +00001396 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001397 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1398 &ParamInfo[0], ParamInfo.size(),
1399 LParenLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00001400
1401 // If we have the closing ')', eat it and we're done.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001402 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001403}
1404
Chris Lattner35d9c912008-04-06 06:34:08 +00001405/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1406/// we found a K&R-style identifier list instead of a type argument list. The
1407/// current token is known to be the first identifier in the list.
1408///
1409/// identifier-list: [C99 6.7.5]
1410/// identifier
1411/// identifier-list ',' identifier
1412///
1413void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1414 Declarator &D) {
1415 // Build up an array of information about the parsed arguments.
1416 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1417 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1418
1419 // If there was no identifier specified for the declarator, either we are in
1420 // an abstract-declarator, or we are in a parameter declarator which was found
1421 // to be abstract. In abstract-declarators, identifier lists are not valid:
1422 // diagnose this.
1423 if (!D.getIdentifier())
1424 Diag(Tok, diag::ext_ident_list_in_param);
1425
1426 // Tok is known to be the first identifier in the list. Remember this
1427 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00001428 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00001429 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1430 Tok.getLocation(), 0));
1431
Chris Lattner113a56b2008-04-06 06:39:19 +00001432 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00001433
1434 while (Tok.is(tok::comma)) {
1435 // Eat the comma.
1436 ConsumeToken();
1437
Chris Lattner113a56b2008-04-06 06:39:19 +00001438 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00001439 if (Tok.isNot(tok::identifier)) {
1440 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00001441 SkipUntil(tok::r_paren);
1442 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00001443 }
Chris Lattneracb67d92008-04-06 06:47:48 +00001444
Chris Lattner35d9c912008-04-06 06:34:08 +00001445 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00001446
1447 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1448 if (Actions.isTypeName(*ParmII, CurScope))
1449 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner35d9c912008-04-06 06:34:08 +00001450
1451 // Verify that the argument identifier has not already been mentioned.
1452 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner113a56b2008-04-06 06:39:19 +00001453 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1454 } else {
1455 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00001456 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1457 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00001458 }
Chris Lattner35d9c912008-04-06 06:34:08 +00001459
1460 // Eat the identifier.
1461 ConsumeToken();
1462 }
1463
Chris Lattner113a56b2008-04-06 06:39:19 +00001464 // Remember that we parsed a function type, and remember the attributes. This
1465 // function type is always a K&R style function type, which is not varargs and
1466 // has no prototype.
1467 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1468 &ParamInfo[0], ParamInfo.size(),
1469 LParenLoc));
Chris Lattner35d9c912008-04-06 06:34:08 +00001470
1471 // If we have the closing ')', eat it and we're done.
Chris Lattner113a56b2008-04-06 06:39:19 +00001472 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00001473}
Chris Lattnera0d056d2008-04-06 05:45:57 +00001474
Chris Lattner4b009652007-07-25 00:24:17 +00001475/// [C90] direct-declarator '[' constant-expression[opt] ']'
1476/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1477/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1478/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1479/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1480void Parser::ParseBracketDeclarator(Declarator &D) {
1481 SourceLocation StartLoc = ConsumeBracket();
1482
1483 // If valid, this location is the position where we read the 'static' keyword.
1484 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001485 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001486 StaticLoc = ConsumeToken();
1487
1488 // If there is a type-qualifier-list, read it now.
1489 DeclSpec DS;
1490 ParseTypeQualifierListOpt(DS);
1491
1492 // If we haven't already read 'static', check to see if there is one after the
1493 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001494 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001495 StaticLoc = ConsumeToken();
1496
1497 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1498 bool isStar = false;
1499 ExprResult NumElements(false);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001500
1501 // Handle the case where we have '[*]' as the array size. However, a leading
1502 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1503 // the the token after the star is a ']'. Since stars in arrays are
1504 // infrequent, use of lookahead is not costly here.
1505 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00001506 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00001507
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001508 if (StaticLoc.isValid())
1509 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1510 StaticLoc = SourceLocation(); // Drop the static.
1511 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001512 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001513 // Parse the assignment-expression now.
1514 NumElements = ParseAssignmentExpression();
1515 }
1516
1517 // If there was an error parsing the assignment-expression, recover.
1518 if (NumElements.isInvalid) {
1519 // If the expression was invalid, skip it.
1520 SkipUntil(tok::r_square);
1521 return;
1522 }
1523
1524 MatchRHSPunctuation(tok::r_square, StartLoc);
1525
1526 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1527 // it was not a constant expression.
1528 if (!getLang().C99) {
1529 // TODO: check C90 array constant exprness.
1530 if (isStar || StaticLoc.isValid() ||
1531 0/*TODO: NumElts is not a C90 constantexpr */)
1532 Diag(StartLoc, diag::ext_c99_array_usage);
1533 }
1534
1535 // Remember that we parsed a pointer type, and remember the type-quals.
1536 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1537 StaticLoc.isValid(), isStar,
1538 NumElements.Val, StartLoc));
1539}
1540
Steve Naroff7cbb1462007-07-31 12:34:36 +00001541/// [GNU] typeof-specifier:
1542/// typeof ( expressions )
1543/// typeof ( type-name )
1544///
1545void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001546 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00001547 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00001548 SourceLocation StartLoc = ConsumeToken();
1549
Chris Lattner34a01ad2007-10-09 17:33:22 +00001550 if (Tok.isNot(tok::l_paren)) {
Steve Naroff14bbce82007-08-02 02:53:48 +00001551 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1552 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00001553 }
1554 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1555
1556 if (isTypeSpecifierQualifier()) {
1557 TypeTy *Ty = ParseTypeName();
1558
Steve Naroff4c255ab2007-07-31 23:56:32 +00001559 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1560
Chris Lattner34a01ad2007-10-09 17:33:22 +00001561 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001562 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001563 return;
1564 }
1565 RParenLoc = ConsumeParen();
1566 const char *PrevSpec = 0;
1567 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1568 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1569 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001570 } else { // we have an expression.
1571 ExprResult Result = ParseExpression();
Steve Naroff4c255ab2007-07-31 23:56:32 +00001572
Chris Lattner34a01ad2007-10-09 17:33:22 +00001573 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001574 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001575 return;
1576 }
1577 RParenLoc = ConsumeParen();
1578 const char *PrevSpec = 0;
1579 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1580 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1581 Result.Val))
1582 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001583 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00001584}
1585
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00001586