blob: 95344c438ef73eadcbbc08846bb18bb4893fb9b4 [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()) {
398 // typedef-name
399 case tok::identifier:
400 // This identifier can only be a typedef name if we haven't already seen
401 // a type-specifier. Without this check we misparse:
402 // typedef int X; struct Y { short X; }; as 'short int'.
403 if (!DS.hasTypeSpecifier()) {
404 // It has to be available as a typedef too!
405 if (void *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(),
406 CurScope)) {
407 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
408 TypeRep);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000409 if (isInvalid)
410 break;
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000411 // FIXME: restrict this to "id" and ObjC classnames.
Chris Lattnera4ff4272008-03-13 06:29:04 +0000412 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000413 ConsumeToken(); // The identifier
414 if (Tok.is(tok::less)) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000415 SourceLocation endProtoLoc;
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000416 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroffef20ed32007-10-30 02:23:23 +0000417 ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000418 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
419 new llvm::SmallVector<DeclTy *, 8>;
420 DS.setProtocolQualifiers(ProtocolDecl);
421 Actions.FindProtocolDeclaration(Loc,
422 &ProtocolRefs[0], ProtocolRefs.size(),
423 *ProtocolDecl);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000424 }
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000425 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000426 }
427 }
428 // FALL THROUGH.
429 default:
430 // If this is not a declaration specifier token, we're done reading decl
431 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000432 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000433 return;
434
435 // GNU attributes support.
436 case tok::kw___attribute:
437 DS.AddAttributes(ParseAttributes());
438 continue;
439
440 // storage-class-specifier
441 case tok::kw_typedef:
442 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
443 break;
444 case tok::kw_extern:
445 if (DS.isThreadSpecified())
446 Diag(Tok, diag::ext_thread_before, "extern");
447 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
448 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000449 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000450 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
451 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000452 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000453 case tok::kw_static:
454 if (DS.isThreadSpecified())
455 Diag(Tok, diag::ext_thread_before, "static");
456 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
457 break;
458 case tok::kw_auto:
459 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
460 break;
461 case tok::kw_register:
462 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
463 break;
464 case tok::kw___thread:
465 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
466 break;
467
468 // type-specifiers
469 case tok::kw_short:
470 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
471 break;
472 case tok::kw_long:
473 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
474 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
475 else
476 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
477 break;
478 case tok::kw_signed:
479 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
480 break;
481 case tok::kw_unsigned:
482 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
483 break;
484 case tok::kw__Complex:
485 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
486 break;
487 case tok::kw__Imaginary:
488 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
489 break;
490 case tok::kw_void:
491 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
492 break;
493 case tok::kw_char:
494 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
495 break;
496 case tok::kw_int:
497 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
498 break;
499 case tok::kw_float:
500 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
501 break;
502 case tok::kw_double:
503 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
504 break;
505 case tok::kw_bool: // [C++ 2.11p1]
506 case tok::kw__Bool:
507 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
508 break;
509 case tok::kw__Decimal32:
510 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
511 break;
512 case tok::kw__Decimal64:
513 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
514 break;
515 case tok::kw__Decimal128:
516 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
517 break;
Chris Lattner2e78db32008-04-13 18:59:07 +0000518
519 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000520 case tok::kw_struct:
521 case tok::kw_union:
Douglas Gregorec93f442008-04-13 21:30:24 +0000522 ParseClassSpecifier(DS);
Chris Lattner4b009652007-07-25 00:24:17 +0000523 continue;
524 case tok::kw_enum:
525 ParseEnumSpecifier(DS);
526 continue;
527
Steve Naroff7cbb1462007-07-31 12:34:36 +0000528 // GNU typeof support.
529 case tok::kw_typeof:
530 ParseTypeofSpecifier(DS);
531 continue;
532
Chris Lattner4b009652007-07-25 00:24:17 +0000533 // type-qualifier
534 case tok::kw_const:
535 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
536 getLang())*2;
537 break;
538 case tok::kw_volatile:
539 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
540 getLang())*2;
541 break;
542 case tok::kw_restrict:
543 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
544 getLang())*2;
545 break;
546
547 // function-specifier
548 case tok::kw_inline:
549 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
550 break;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000551
552 // Gross GCC-ism that we are forced support. FIXME: make an extension?
553 case tok::less:
554 if (!DS.hasTypeSpecifier()) {
555 SourceLocation endProtoLoc;
556 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
557 ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc);
558 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
559 new llvm::SmallVector<DeclTy *, 8>;
560 DS.setProtocolQualifiers(ProtocolDecl);
561 Actions.FindProtocolDeclaration(Loc,
562 &ProtocolRefs[0], ProtocolRefs.size(),
563 *ProtocolDecl);
564 }
565 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000566 }
567 // If the specifier combination wasn't legal, issue a diagnostic.
568 if (isInvalid) {
569 assert(PrevSpec && "Method did not return previous specifier!");
570 if (isInvalid == 1) // Error.
571 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
572 else // extwarn.
573 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
574 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000575 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000576 ConsumeToken();
577 }
578}
579
580/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
581/// the first token has already been read and has been turned into an instance
582/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
583/// otherwise it returns false and fills in Decl.
584bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
585 AttributeList *Attr = 0;
586 // If attributes exist after tag, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000587 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000588 Attr = ParseAttributes();
589
590 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000591 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000592 Diag(Tok, diag::err_expected_ident_lbrace);
593
594 // Skip the rest of this declarator, up until the comma or semicolon.
595 SkipUntil(tok::comma, true);
596 return true;
597 }
598
599 // If an identifier is present, consume and remember it.
600 IdentifierInfo *Name = 0;
601 SourceLocation NameLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000602 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000603 Name = Tok.getIdentifierInfo();
604 NameLoc = ConsumeToken();
605 }
606
607 // There are three options here. If we have 'struct foo;', then this is a
608 // forward declaration. If we have 'struct foo {...' then this is a
609 // definition. Otherwise we have something like 'struct foo xyz', a reference.
610 //
611 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
612 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
613 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
614 //
615 Action::TagKind TK;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000616 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000617 TK = Action::TK_Definition;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000618 else if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000619 TK = Action::TK_Declaration;
620 else
621 TK = Action::TK_Reference;
Steve Naroff0acc9c92007-09-15 18:49:24 +0000622 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Chris Lattner4b009652007-07-25 00:24:17 +0000623 return false;
624}
625
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000626/// ParseStructDeclaration - Parse a struct declaration without the terminating
627/// semicolon.
628///
Chris Lattner4b009652007-07-25 00:24:17 +0000629/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000630/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000631/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000632/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000633/// struct-declarator-list:
634/// struct-declarator
635/// struct-declarator-list ',' struct-declarator
636/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
637/// struct-declarator:
638/// declarator
639/// [GNU] declarator attributes[opt]
640/// declarator[opt] ':' constant-expression
641/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
642///
Chris Lattner3dd8d392008-04-10 06:46:29 +0000643void Parser::
644ParseStructDeclaration(DeclSpec &DS,
645 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000646 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattner3dd8d392008-04-10 06:46:29 +0000647 while (Tok.is(tok::kw___extension__))
Steve Naroffa9adf112007-08-20 22:28:22 +0000648 ConsumeToken();
649
650 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000651 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +0000652 ParseSpecifierQualifierList(DS);
653 // TODO: Does specifier-qualifier list correctly check that *something* is
654 // specified?
655
656 // If there are no declarators, issue a warning.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000657 if (Tok.is(tok::semi)) {
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000658 Diag(DSStart, diag::w_no_declarators);
Steve Naroffa9adf112007-08-20 22:28:22 +0000659 return;
660 }
661
662 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000663 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000664 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +0000665 FieldDeclarator &DeclaratorInfo = Fields.back();
666
Steve Naroffa9adf112007-08-20 22:28:22 +0000667 /// struct-declarator: declarator
668 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000669 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000670 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +0000671
Chris Lattner34a01ad2007-10-09 17:33:22 +0000672 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000673 ConsumeToken();
674 ExprResult Res = ParseConstantExpression();
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000675 if (Res.isInvalid)
Steve Naroffa9adf112007-08-20 22:28:22 +0000676 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000677 else
Chris Lattner3dd8d392008-04-10 06:46:29 +0000678 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroffa9adf112007-08-20 22:28:22 +0000679 }
680
681 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000682 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000683 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000684
685 // If we don't have a comma, it is either the end of the list (a ';')
686 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000687 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000688 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000689
690 // Consume the comma.
691 ConsumeToken();
692
693 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000694 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000695
696 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000697 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000698 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000699 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000700}
701
702/// ParseStructUnionBody
703/// struct-contents:
704/// struct-declaration-list
705/// [EXT] empty
706/// [GNU] "struct-declaration-list" without terminatoring ';'
707/// struct-declaration-list:
708/// struct-declaration
709/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +0000710/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +0000711///
Chris Lattner4b009652007-07-25 00:24:17 +0000712void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
713 unsigned TagType, DeclTy *TagDecl) {
714 SourceLocation LBraceLoc = ConsumeBrace();
715
716 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
717 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +0000718 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000719 Diag(Tok, diag::ext_empty_struct_union_enum,
720 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
721
722 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000723 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
724
Chris Lattner4b009652007-07-25 00:24:17 +0000725 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000726 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000727 // Each iteration of this loop reads one struct-declaration.
728
729 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000730 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000731 Diag(Tok, diag::ext_extra_struct_semi);
732 ConsumeToken();
733 continue;
734 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000735
736 // Parse all the comma separated declarators.
737 DeclSpec DS;
738 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +0000739 if (!Tok.is(tok::at)) {
740 ParseStructDeclaration(DS, FieldDeclarators);
741
742 // Convert them all to fields.
743 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
744 FieldDeclarator &FD = FieldDeclarators[i];
745 // Install the declarator into the current TagDecl.
746 DeclTy *Field = Actions.ActOnField(CurScope,
747 DS.getSourceRange().getBegin(),
748 FD.D, FD.BitfieldSize);
749 FieldDecls.push_back(Field);
750 }
751 } else { // Handle @defs
752 ConsumeToken();
753 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
754 Diag(Tok, diag::err_unexpected_at);
755 SkipUntil(tok::semi, true, true);
756 continue;
757 }
758 ConsumeToken();
759 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
760 if (!Tok.is(tok::identifier)) {
761 Diag(Tok, diag::err_expected_ident);
762 SkipUntil(tok::semi, true, true);
763 continue;
764 }
765 llvm::SmallVector<DeclTy*, 16> Fields;
766 Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
767 Fields);
768 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
769 ConsumeToken();
770 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
771 }
Chris Lattner4b009652007-07-25 00:24:17 +0000772
Chris Lattner34a01ad2007-10-09 17:33:22 +0000773 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000774 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +0000775 } else if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000776 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
777 break;
778 } else {
779 Diag(Tok, diag::err_expected_semi_decl_list);
780 // Skip to end of block or statement
781 SkipUntil(tok::r_brace, true, true);
782 }
783 }
784
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000785 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000786
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000787 Actions.ActOnFields(CurScope,
Chris Lattner43b885f2008-02-25 21:04:36 +0000788 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000789 LBraceLoc, RBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000790
791 AttributeList *AttrList = 0;
792 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000793 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000794 AttrList = ParseAttributes(); // FIXME: where should I put them?
795}
796
797
798/// ParseEnumSpecifier
799/// enum-specifier: [C99 6.7.2.2]
800/// 'enum' identifier[opt] '{' enumerator-list '}'
801/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
802/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
803/// '}' attributes[opt]
804/// 'enum' identifier
805/// [GNU] 'enum' attributes[opt] identifier
806void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000807 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000808 SourceLocation StartLoc = ConsumeToken();
809
810 // Parse the tag portion of this.
811 DeclTy *TagDecl;
812 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
813 return;
814
Chris Lattner34a01ad2007-10-09 17:33:22 +0000815 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000816 ParseEnumBody(StartLoc, TagDecl);
817
818 // TODO: semantic analysis on the declspec for enums.
819 const char *PrevSpec = 0;
820 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
821 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
822}
823
824/// ParseEnumBody - Parse a {} enclosed enumerator-list.
825/// enumerator-list:
826/// enumerator
827/// enumerator-list ',' enumerator
828/// enumerator:
829/// enumeration-constant
830/// enumeration-constant '=' constant-expression
831/// enumeration-constant:
832/// identifier
833///
834void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
835 SourceLocation LBraceLoc = ConsumeBrace();
836
Chris Lattnerc9a92452007-08-27 17:24:30 +0000837 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +0000838 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000839 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
840
841 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
842
843 DeclTy *LastEnumConstDecl = 0;
844
845 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000846 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000847 IdentifierInfo *Ident = Tok.getIdentifierInfo();
848 SourceLocation IdentLoc = ConsumeToken();
849
850 SourceLocation EqualLoc;
851 ExprTy *AssignedVal = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000852 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000853 EqualLoc = ConsumeToken();
854 ExprResult Res = ParseConstantExpression();
855 if (Res.isInvalid)
856 SkipUntil(tok::comma, tok::r_brace, true, true);
857 else
858 AssignedVal = Res.Val;
859 }
860
861 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000862 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +0000863 LastEnumConstDecl,
864 IdentLoc, Ident,
865 EqualLoc, AssignedVal);
866 EnumConstantDecls.push_back(EnumConstDecl);
867 LastEnumConstDecl = EnumConstDecl;
868
Chris Lattner34a01ad2007-10-09 17:33:22 +0000869 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000870 break;
871 SourceLocation CommaLoc = ConsumeToken();
872
Chris Lattner34a01ad2007-10-09 17:33:22 +0000873 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +0000874 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
875 }
876
877 // Eat the }.
878 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
879
Steve Naroff0acc9c92007-09-15 18:49:24 +0000880 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +0000881 EnumConstantDecls.size());
882
883 DeclTy *AttrList = 0;
884 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000885 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000886 AttrList = ParseAttributes(); // FIXME: where do they do?
887}
888
889/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +0000890/// start of a type-qualifier-list.
891bool Parser::isTypeQualifier() const {
892 switch (Tok.getKind()) {
893 default: return false;
894 // type-qualifier
895 case tok::kw_const:
896 case tok::kw_volatile:
897 case tok::kw_restrict:
898 return true;
899 }
900}
901
902/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +0000903/// start of a specifier-qualifier-list.
904bool Parser::isTypeSpecifierQualifier() const {
905 switch (Tok.getKind()) {
906 default: return false;
907 // GNU attributes support.
908 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000909 // GNU typeof support.
910 case tok::kw_typeof:
Steve Naroff5f0466b2008-06-05 00:02:44 +0000911 // GNU bizarre protocol extension. FIXME: make an extension?
912 case tok::less:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000913
Chris Lattner4b009652007-07-25 00:24:17 +0000914 // type-specifiers
915 case tok::kw_short:
916 case tok::kw_long:
917 case tok::kw_signed:
918 case tok::kw_unsigned:
919 case tok::kw__Complex:
920 case tok::kw__Imaginary:
921 case tok::kw_void:
922 case tok::kw_char:
923 case tok::kw_int:
924 case tok::kw_float:
925 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000926 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +0000927 case tok::kw__Bool:
928 case tok::kw__Decimal32:
929 case tok::kw__Decimal64:
930 case tok::kw__Decimal128:
931
Chris Lattner2e78db32008-04-13 18:59:07 +0000932 // struct-or-union-specifier (C99) or class-specifier (C++)
933 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000934 case tok::kw_struct:
935 case tok::kw_union:
936 // enum-specifier
937 case tok::kw_enum:
938
939 // type-qualifier
940 case tok::kw_const:
941 case tok::kw_volatile:
942 case tok::kw_restrict:
943 return true;
944
945 // typedef-name
946 case tok::identifier:
947 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000948 }
949}
950
951/// isDeclarationSpecifier() - Return true if the current token is part of a
952/// declaration specifier.
953bool Parser::isDeclarationSpecifier() const {
954 switch (Tok.getKind()) {
955 default: return false;
956 // storage-class-specifier
957 case tok::kw_typedef:
958 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +0000959 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +0000960 case tok::kw_static:
961 case tok::kw_auto:
962 case tok::kw_register:
963 case tok::kw___thread:
964
965 // type-specifiers
966 case tok::kw_short:
967 case tok::kw_long:
968 case tok::kw_signed:
969 case tok::kw_unsigned:
970 case tok::kw__Complex:
971 case tok::kw__Imaginary:
972 case tok::kw_void:
973 case tok::kw_char:
974 case tok::kw_int:
975 case tok::kw_float:
976 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000977 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +0000978 case tok::kw__Bool:
979 case tok::kw__Decimal32:
980 case tok::kw__Decimal64:
981 case tok::kw__Decimal128:
982
Chris Lattner2e78db32008-04-13 18:59:07 +0000983 // struct-or-union-specifier (C99) or class-specifier (C++)
984 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000985 case tok::kw_struct:
986 case tok::kw_union:
987 // enum-specifier
988 case tok::kw_enum:
989
990 // type-qualifier
991 case tok::kw_const:
992 case tok::kw_volatile:
993 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000994
Chris Lattner4b009652007-07-25 00:24:17 +0000995 // function-specifier
996 case tok::kw_inline:
Chris Lattnere35d2582007-08-09 16:40:21 +0000997
Chris Lattnerb707a7a2007-08-09 17:01:07 +0000998 // GNU typeof support.
999 case tok::kw_typeof:
1000
1001 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001002 case tok::kw___attribute:
Steve Naroff5f0466b2008-06-05 00:02:44 +00001003
1004 // GNU bizarre protocol extension. FIXME: make an extension?
1005 case tok::less:
Chris Lattner4b009652007-07-25 00:24:17 +00001006 return true;
1007
1008 // typedef-name
1009 case tok::identifier:
1010 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001011 }
1012}
1013
1014
1015/// ParseTypeQualifierListOpt
1016/// type-qualifier-list: [C99 6.7.5]
1017/// type-qualifier
1018/// [GNU] attributes
1019/// type-qualifier-list type-qualifier
1020/// [GNU] type-qualifier-list attributes
1021///
1022void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1023 while (1) {
1024 int isInvalid = false;
1025 const char *PrevSpec = 0;
1026 SourceLocation Loc = Tok.getLocation();
1027
1028 switch (Tok.getKind()) {
1029 default:
1030 // If this is not a type-qualifier token, we're done reading type
1031 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +00001032 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +00001033 return;
1034 case tok::kw_const:
1035 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1036 getLang())*2;
1037 break;
1038 case tok::kw_volatile:
1039 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1040 getLang())*2;
1041 break;
1042 case tok::kw_restrict:
1043 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1044 getLang())*2;
1045 break;
1046 case tok::kw___attribute:
1047 DS.AddAttributes(ParseAttributes());
1048 continue; // do *not* consume the next token!
1049 }
1050
1051 // If the specifier combination wasn't legal, issue a diagnostic.
1052 if (isInvalid) {
1053 assert(PrevSpec && "Method did not return previous specifier!");
1054 if (isInvalid == 1) // Error.
1055 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1056 else // extwarn.
1057 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1058 }
1059 ConsumeToken();
1060 }
1061}
1062
1063
1064/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1065///
1066void Parser::ParseDeclarator(Declarator &D) {
1067 /// This implements the 'declarator' production in the C grammar, then checks
1068 /// for well-formedness and issues diagnostics.
1069 ParseDeclaratorInternal(D);
Chris Lattner4b009652007-07-25 00:24:17 +00001070}
1071
1072/// ParseDeclaratorInternal
1073/// declarator: [C99 6.7.5]
1074/// pointer[opt] direct-declarator
1075/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1076/// [GNU] '&' restrict[opt] attributes[opt] declarator
1077///
1078/// pointer: [C99 6.7.5]
1079/// '*' type-qualifier-list[opt]
1080/// '*' type-qualifier-list[opt] pointer
1081///
1082void Parser::ParseDeclaratorInternal(Declarator &D) {
1083 tok::TokenKind Kind = Tok.getKind();
1084
1085 // Not a pointer or C++ reference.
Chris Lattner69f01932008-02-21 01:32:26 +00001086 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus))
Chris Lattner4b009652007-07-25 00:24:17 +00001087 return ParseDirectDeclarator(D);
1088
1089 // Otherwise, '*' -> pointer or '&' -> reference.
1090 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1091
1092 if (Kind == tok::star) {
Chris Lattner69f01932008-02-21 01:32:26 +00001093 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001094 DeclSpec DS;
1095
1096 ParseTypeQualifierListOpt(DS);
1097
1098 // Recursively parse the declarator.
1099 ParseDeclaratorInternal(D);
1100
1101 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001102 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1103 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001104 } else {
1105 // Is a reference
1106 DeclSpec DS;
1107
1108 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1109 // cv-qualifiers are introduced through the use of a typedef or of a
1110 // template type argument, in which case the cv-qualifiers are ignored.
1111 //
1112 // [GNU] Retricted references are allowed.
1113 // [GNU] Attributes on references are allowed.
1114 ParseTypeQualifierListOpt(DS);
1115
1116 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1117 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1118 Diag(DS.getConstSpecLoc(),
1119 diag::err_invalid_reference_qualifier_application,
1120 "const");
1121 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1122 Diag(DS.getVolatileSpecLoc(),
1123 diag::err_invalid_reference_qualifier_application,
1124 "volatile");
1125 }
1126
1127 // Recursively parse the declarator.
1128 ParseDeclaratorInternal(D);
1129
1130 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001131 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1132 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001133 }
1134}
1135
1136/// ParseDirectDeclarator
1137/// direct-declarator: [C99 6.7.5]
1138/// identifier
1139/// '(' declarator ')'
1140/// [GNU] '(' attributes declarator ')'
1141/// [C90] direct-declarator '[' constant-expression[opt] ']'
1142/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1143/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1144/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1145/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1146/// direct-declarator '(' parameter-type-list ')'
1147/// direct-declarator '(' identifier-list[opt] ')'
1148/// [GNU] direct-declarator '(' parameter-forward-declarations
1149/// parameter-type-list[opt] ')'
1150///
1151void Parser::ParseDirectDeclarator(Declarator &D) {
1152 // Parse the first direct-declarator seen.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001153 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001154 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1155 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1156 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001157 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001158 // direct-declarator: '(' declarator ')'
1159 // direct-declarator: '(' attributes declarator ')'
1160 // Example: 'char (*X)' or 'int (*XX)(void)'
1161 ParseParenDeclarator(D);
1162 } else if (D.mayOmitIdentifier()) {
1163 // This could be something simple like "int" (in which case the declarator
1164 // portion is empty), if an abstract-declarator is allowed.
1165 D.SetIdentifier(0, Tok.getLocation());
1166 } else {
1167 // Expected identifier or '('.
1168 Diag(Tok, diag::err_expected_ident_lparen);
1169 D.SetIdentifier(0, Tok.getLocation());
1170 }
1171
1172 assert(D.isPastIdentifier() &&
1173 "Haven't past the location of the identifier yet?");
1174
1175 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001176 if (Tok.is(tok::l_paren)) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00001177 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001178 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001179 ParseBracketDeclarator(D);
1180 } else {
1181 break;
1182 }
1183 }
1184}
1185
Chris Lattnera0d056d2008-04-06 05:45:57 +00001186/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1187/// only called before the identifier, so these are most likely just grouping
1188/// parens for precedence. If we find that these are actually function
1189/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1190///
1191/// direct-declarator:
1192/// '(' declarator ')'
1193/// [GNU] '(' attributes declarator ')'
1194///
1195void Parser::ParseParenDeclarator(Declarator &D) {
1196 SourceLocation StartLoc = ConsumeParen();
1197 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1198
1199 // If we haven't past the identifier yet (or where the identifier would be
1200 // stored, if this is an abstract declarator), then this is probably just
1201 // grouping parens. However, if this could be an abstract-declarator, then
1202 // this could also be the start of function arguments (consider 'void()').
1203 bool isGrouping;
1204
1205 if (!D.mayOmitIdentifier()) {
1206 // If this can't be an abstract-declarator, this *must* be a grouping
1207 // paren, because we haven't seen the identifier yet.
1208 isGrouping = true;
1209 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
1210 isDeclarationSpecifier()) { // 'int(int)' is a function.
1211 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1212 // considered to be a type, not a K&R identifier-list.
1213 isGrouping = false;
1214 } else {
1215 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1216 isGrouping = true;
1217 }
1218
1219 // If this is a grouping paren, handle:
1220 // direct-declarator: '(' declarator ')'
1221 // direct-declarator: '(' attributes declarator ')'
1222 if (isGrouping) {
1223 if (Tok.is(tok::kw___attribute))
1224 D.AddAttributes(ParseAttributes());
1225
1226 ParseDeclaratorInternal(D);
1227 // Match the ')'.
1228 MatchRHSPunctuation(tok::r_paren, StartLoc);
1229 return;
1230 }
1231
1232 // Okay, if this wasn't a grouping paren, it must be the start of a function
1233 // argument list. Recognize that this declarator will never have an
1234 // identifier (and remember where it would have been), then fall through to
1235 // the handling of argument lists.
1236 D.SetIdentifier(0, Tok.getLocation());
1237
1238 ParseFunctionDeclarator(StartLoc, D);
1239}
1240
1241/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1242/// declarator D up to a paren, which indicates that we are parsing function
1243/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001244///
1245/// This method also handles this portion of the grammar:
1246/// parameter-type-list: [C99 6.7.5]
1247/// parameter-list
1248/// parameter-list ',' '...'
1249///
1250/// parameter-list: [C99 6.7.5]
1251/// parameter-declaration
1252/// parameter-list ',' parameter-declaration
1253///
1254/// parameter-declaration: [C99 6.7.5]
1255/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00001256/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001257/// [GNU] declaration-specifiers declarator attributes
1258/// declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00001259/// [C++] declaration-specifiers abstract-declarator[opt]
1260/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001261/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1262///
Chris Lattnera0d056d2008-04-06 05:45:57 +00001263void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D) {
1264 // lparen is already consumed!
1265 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00001266
1267 // Okay, this is the parameter list of a function definition, or it is an
1268 // identifier list of a K&R-style function.
Chris Lattner4b009652007-07-25 00:24:17 +00001269
Chris Lattner34a01ad2007-10-09 17:33:22 +00001270 if (Tok.is(tok::r_paren)) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00001271 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00001272 // int() -> no prototype, no '...'.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001273 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/ false,
1274 /*variadic*/ false,
1275 /*arglist*/ 0, 0, LParenLoc));
1276
1277 ConsumeParen(); // Eat the closing ')'.
1278 return;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001279 } else if (Tok.is(tok::identifier) &&
Chris Lattner4b009652007-07-25 00:24:17 +00001280 // K&R identifier lists can't have typedefs as identifiers, per
1281 // C99 6.7.5.3p11.
1282 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1283 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1284 // normal declarators, not for abstract-declarators.
Chris Lattner35d9c912008-04-06 06:34:08 +00001285 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001286 }
1287
1288 // Finally, a normal, non-empty parameter type list.
1289
1290 // Build up an array of information about the parsed arguments.
1291 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00001292
1293 // Enter function-declaration scope, limiting any declarators to the
1294 // function prototype scope, including parameter declarators.
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00001295 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001296
1297 bool IsVariadic = false;
1298 while (1) {
1299 if (Tok.is(tok::ellipsis)) {
1300 IsVariadic = true;
Chris Lattner4b009652007-07-25 00:24:17 +00001301
Chris Lattner9f7564b2008-04-06 06:57:35 +00001302 // Check to see if this is "void(...)" which is not allowed.
1303 if (ParamInfo.empty()) {
1304 // Otherwise, parse parameter type list. If it starts with an
1305 // ellipsis, diagnose the malformed function.
1306 Diag(Tok, diag::err_ellipsis_first_arg);
1307 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001308 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00001309
Chris Lattner9f7564b2008-04-06 06:57:35 +00001310 ConsumeToken(); // Consume the ellipsis.
1311 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001312 }
1313
Chris Lattner9f7564b2008-04-06 06:57:35 +00001314 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00001315
Chris Lattner9f7564b2008-04-06 06:57:35 +00001316 // Parse the declaration-specifiers.
1317 DeclSpec DS;
1318 ParseDeclarationSpecifiers(DS);
1319
1320 // Parse the declarator. This is "PrototypeContext", because we must
1321 // accept either 'declarator' or 'abstract-declarator' here.
1322 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1323 ParseDeclarator(ParmDecl);
1324
1325 // Parse GNU attributes, if present.
1326 if (Tok.is(tok::kw___attribute))
1327 ParmDecl.AddAttributes(ParseAttributes());
1328
Chris Lattner9f7564b2008-04-06 06:57:35 +00001329 // Remember this parsed parameter in ParamInfo.
1330 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1331
Chris Lattner9f7564b2008-04-06 06:57:35 +00001332 // If no parameter was specified, verify that *something* was specified,
1333 // otherwise we have a missing type and identifier.
1334 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1335 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1336 // Completely missing, emit error.
1337 Diag(DSStart, diag::err_missing_param);
1338 } else {
1339 // Otherwise, we have something. Add it and let semantic analysis try
1340 // to grok it and add the result to the ParamInfo we are building.
1341
1342 // Inform the actions module about the parameter declarator, so it gets
1343 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00001344 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1345
1346 // Parse the default argument, if any. We parse the default
1347 // arguments in all dialects; the semantic analysis in
1348 // ActOnParamDefaultArgument will reject the default argument in
1349 // C.
1350 if (Tok.is(tok::equal)) {
1351 SourceLocation EqualLoc = Tok.getLocation();
1352
1353 // Consume the '='.
1354 ConsumeToken();
1355
1356 // Parse the default argument
Chris Lattner3e254fb2008-04-08 04:40:51 +00001357 ExprResult DefArgResult = ParseAssignmentExpression();
1358 if (DefArgResult.isInvalid) {
1359 SkipUntil(tok::comma, tok::r_paren, true, true);
1360 } else {
1361 // Inform the actions module about the default argument
1362 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1363 }
1364 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001365
1366 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001367 ParmDecl.getIdentifierLoc(), Param));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001368 }
1369
1370 // If the next token is a comma, consume it and keep reading arguments.
1371 if (Tok.isNot(tok::comma)) break;
1372
1373 // Consume the comma.
1374 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00001375 }
1376
Chris Lattner9f7564b2008-04-06 06:57:35 +00001377 // Leave prototype scope.
1378 ExitScope();
1379
Chris Lattner4b009652007-07-25 00:24:17 +00001380 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001381 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1382 &ParamInfo[0], ParamInfo.size(),
1383 LParenLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00001384
1385 // If we have the closing ')', eat it and we're done.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001386 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001387}
1388
Chris Lattner35d9c912008-04-06 06:34:08 +00001389/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1390/// we found a K&R-style identifier list instead of a type argument list. The
1391/// current token is known to be the first identifier in the list.
1392///
1393/// identifier-list: [C99 6.7.5]
1394/// identifier
1395/// identifier-list ',' identifier
1396///
1397void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1398 Declarator &D) {
1399 // Build up an array of information about the parsed arguments.
1400 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1401 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1402
1403 // If there was no identifier specified for the declarator, either we are in
1404 // an abstract-declarator, or we are in a parameter declarator which was found
1405 // to be abstract. In abstract-declarators, identifier lists are not valid:
1406 // diagnose this.
1407 if (!D.getIdentifier())
1408 Diag(Tok, diag::ext_ident_list_in_param);
1409
1410 // Tok is known to be the first identifier in the list. Remember this
1411 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00001412 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00001413 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1414 Tok.getLocation(), 0));
1415
Chris Lattner113a56b2008-04-06 06:39:19 +00001416 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00001417
1418 while (Tok.is(tok::comma)) {
1419 // Eat the comma.
1420 ConsumeToken();
1421
Chris Lattner113a56b2008-04-06 06:39:19 +00001422 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00001423 if (Tok.isNot(tok::identifier)) {
1424 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00001425 SkipUntil(tok::r_paren);
1426 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00001427 }
Chris Lattneracb67d92008-04-06 06:47:48 +00001428
Chris Lattner35d9c912008-04-06 06:34:08 +00001429 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00001430
1431 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1432 if (Actions.isTypeName(*ParmII, CurScope))
1433 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner35d9c912008-04-06 06:34:08 +00001434
1435 // Verify that the argument identifier has not already been mentioned.
1436 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner113a56b2008-04-06 06:39:19 +00001437 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1438 } else {
1439 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00001440 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1441 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00001442 }
Chris Lattner35d9c912008-04-06 06:34:08 +00001443
1444 // Eat the identifier.
1445 ConsumeToken();
1446 }
1447
Chris Lattner113a56b2008-04-06 06:39:19 +00001448 // Remember that we parsed a function type, and remember the attributes. This
1449 // function type is always a K&R style function type, which is not varargs and
1450 // has no prototype.
1451 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1452 &ParamInfo[0], ParamInfo.size(),
1453 LParenLoc));
Chris Lattner35d9c912008-04-06 06:34:08 +00001454
1455 // If we have the closing ')', eat it and we're done.
Chris Lattner113a56b2008-04-06 06:39:19 +00001456 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00001457}
Chris Lattnera0d056d2008-04-06 05:45:57 +00001458
Chris Lattner4b009652007-07-25 00:24:17 +00001459/// [C90] direct-declarator '[' constant-expression[opt] ']'
1460/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1461/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1462/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1463/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1464void Parser::ParseBracketDeclarator(Declarator &D) {
1465 SourceLocation StartLoc = ConsumeBracket();
1466
1467 // If valid, this location is the position where we read the 'static' keyword.
1468 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001469 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001470 StaticLoc = ConsumeToken();
1471
1472 // If there is a type-qualifier-list, read it now.
1473 DeclSpec DS;
1474 ParseTypeQualifierListOpt(DS);
1475
1476 // If we haven't already read 'static', check to see if there is one after the
1477 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001478 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001479 StaticLoc = ConsumeToken();
1480
1481 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1482 bool isStar = false;
1483 ExprResult NumElements(false);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001484
1485 // Handle the case where we have '[*]' as the array size. However, a leading
1486 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1487 // the the token after the star is a ']'. Since stars in arrays are
1488 // infrequent, use of lookahead is not costly here.
1489 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00001490 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00001491
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001492 if (StaticLoc.isValid())
1493 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1494 StaticLoc = SourceLocation(); // Drop the static.
1495 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001496 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001497 // Parse the assignment-expression now.
1498 NumElements = ParseAssignmentExpression();
1499 }
1500
1501 // If there was an error parsing the assignment-expression, recover.
1502 if (NumElements.isInvalid) {
1503 // If the expression was invalid, skip it.
1504 SkipUntil(tok::r_square);
1505 return;
1506 }
1507
1508 MatchRHSPunctuation(tok::r_square, StartLoc);
1509
1510 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1511 // it was not a constant expression.
1512 if (!getLang().C99) {
1513 // TODO: check C90 array constant exprness.
1514 if (isStar || StaticLoc.isValid() ||
1515 0/*TODO: NumElts is not a C90 constantexpr */)
1516 Diag(StartLoc, diag::ext_c99_array_usage);
1517 }
1518
1519 // Remember that we parsed a pointer type, and remember the type-quals.
1520 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1521 StaticLoc.isValid(), isStar,
1522 NumElements.Val, StartLoc));
1523}
1524
Steve Naroff7cbb1462007-07-31 12:34:36 +00001525/// [GNU] typeof-specifier:
1526/// typeof ( expressions )
1527/// typeof ( type-name )
1528///
1529void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001530 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00001531 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00001532 SourceLocation StartLoc = ConsumeToken();
1533
Chris Lattner34a01ad2007-10-09 17:33:22 +00001534 if (Tok.isNot(tok::l_paren)) {
Steve Naroff14bbce82007-08-02 02:53:48 +00001535 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1536 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00001537 }
1538 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1539
1540 if (isTypeSpecifierQualifier()) {
1541 TypeTy *Ty = ParseTypeName();
1542
Steve Naroff4c255ab2007-07-31 23:56:32 +00001543 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1544
Chris Lattner34a01ad2007-10-09 17:33:22 +00001545 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001546 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001547 return;
1548 }
1549 RParenLoc = ConsumeParen();
1550 const char *PrevSpec = 0;
1551 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1552 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1553 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001554 } else { // we have an expression.
1555 ExprResult Result = ParseExpression();
Steve Naroff4c255ab2007-07-31 23:56:32 +00001556
Chris Lattner34a01ad2007-10-09 17:33:22 +00001557 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001558 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001559 return;
1560 }
1561 RParenLoc = ConsumeParen();
1562 const char *PrevSpec = 0;
1563 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1564 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1565 Result.Val))
1566 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001567 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00001568}
1569
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00001570