blob: 1d412482ea90271ca966c0432b373f1d2f000157 [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();
326 if (Specs == DeclSpec::PQ_None)
327 Diag(Tok, diag::err_typename_requires_specqual);
328
329 // Issue diagnostic and remove storage class if present.
330 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
331 if (DS.getStorageClassSpecLoc().isValid())
332 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
333 else
334 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
335 DS.ClearStorageClassSpecs();
336 }
337
338 // Issue diagnostic and remove function specfier if present.
339 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
340 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
341 DS.ClearFunctionSpecs();
342 }
343}
344
345/// ParseDeclarationSpecifiers
346/// declaration-specifiers: [C99 6.7]
347/// storage-class-specifier declaration-specifiers[opt]
348/// type-specifier declaration-specifiers[opt]
349/// type-qualifier declaration-specifiers[opt]
350/// [C99] function-specifier declaration-specifiers[opt]
351/// [GNU] attributes declaration-specifiers[opt]
352///
353/// storage-class-specifier: [C99 6.7.1]
354/// 'typedef'
355/// 'extern'
356/// 'static'
357/// 'auto'
358/// 'register'
359/// [GNU] '__thread'
360/// type-specifier: [C99 6.7.2]
361/// 'void'
362/// 'char'
363/// 'short'
364/// 'int'
365/// 'long'
366/// 'float'
367/// 'double'
368/// 'signed'
369/// 'unsigned'
370/// struct-or-union-specifier
371/// enum-specifier
372/// typedef-name
373/// [C++] 'bool'
374/// [C99] '_Bool'
375/// [C99] '_Complex'
376/// [C99] '_Imaginary' // Removed in TC2?
377/// [GNU] '_Decimal32'
378/// [GNU] '_Decimal64'
379/// [GNU] '_Decimal128'
Steve 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;
518
519 case tok::kw_struct:
520 case tok::kw_union:
521 ParseStructUnionSpecifier(DS);
522 continue;
523 case tok::kw_enum:
524 ParseEnumSpecifier(DS);
525 continue;
526
Steve Naroff7cbb1462007-07-31 12:34:36 +0000527 // GNU typeof support.
528 case tok::kw_typeof:
529 ParseTypeofSpecifier(DS);
530 continue;
531
Chris Lattner4b009652007-07-25 00:24:17 +0000532 // type-qualifier
533 case tok::kw_const:
534 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
535 getLang())*2;
536 break;
537 case tok::kw_volatile:
538 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
539 getLang())*2;
540 break;
541 case tok::kw_restrict:
542 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
543 getLang())*2;
544 break;
545
546 // function-specifier
547 case tok::kw_inline:
548 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
549 break;
550 }
551 // If the specifier combination wasn't legal, issue a diagnostic.
552 if (isInvalid) {
553 assert(PrevSpec && "Method did not return previous specifier!");
554 if (isInvalid == 1) // Error.
555 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
556 else // extwarn.
557 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
558 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000559 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000560 ConsumeToken();
561 }
562}
563
564/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
565/// the first token has already been read and has been turned into an instance
566/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
567/// otherwise it returns false and fills in Decl.
568bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
569 AttributeList *Attr = 0;
570 // If attributes exist after tag, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000571 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000572 Attr = ParseAttributes();
573
574 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000575 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000576 Diag(Tok, diag::err_expected_ident_lbrace);
577
578 // Skip the rest of this declarator, up until the comma or semicolon.
579 SkipUntil(tok::comma, true);
580 return true;
581 }
582
583 // If an identifier is present, consume and remember it.
584 IdentifierInfo *Name = 0;
585 SourceLocation NameLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000586 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000587 Name = Tok.getIdentifierInfo();
588 NameLoc = ConsumeToken();
589 }
590
591 // There are three options here. If we have 'struct foo;', then this is a
592 // forward declaration. If we have 'struct foo {...' then this is a
593 // definition. Otherwise we have something like 'struct foo xyz', a reference.
594 //
595 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
596 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
597 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
598 //
599 Action::TagKind TK;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000600 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000601 TK = Action::TK_Definition;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000602 else if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000603 TK = Action::TK_Declaration;
604 else
605 TK = Action::TK_Reference;
Steve Naroff0acc9c92007-09-15 18:49:24 +0000606 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Chris Lattner4b009652007-07-25 00:24:17 +0000607 return false;
608}
609
610
611/// ParseStructUnionSpecifier
612/// struct-or-union-specifier: [C99 6.7.2.1]
613/// struct-or-union identifier[opt] '{' struct-contents '}'
614/// struct-or-union identifier
615/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
616/// '}' attributes[opt]
617/// [GNU] struct-or-union attributes[opt] identifier
618/// struct-or-union:
619/// 'struct'
620/// 'union'
621///
622void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000623 assert((Tok.is(tok::kw_struct) || Tok.is(tok::kw_union)) &&
624 "Not a struct/union specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000625 DeclSpec::TST TagType =
Chris Lattner34a01ad2007-10-09 17:33:22 +0000626 Tok.is(tok::kw_union) ? DeclSpec::TST_union : DeclSpec::TST_struct;
Chris Lattner4b009652007-07-25 00:24:17 +0000627 SourceLocation StartLoc = ConsumeToken();
628
629 // Parse the tag portion of this.
630 DeclTy *TagDecl;
631 if (ParseTag(TagDecl, TagType, StartLoc))
632 return;
633
634 // If there is a body, parse it and inform the actions module.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000635 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000636 ParseStructUnionBody(StartLoc, TagType, TagDecl);
637
638 const char *PrevSpec = 0;
639 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
640 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
641}
642
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000643/// ParseStructDeclaration - Parse a struct declaration without the terminating
644/// semicolon.
645///
Chris Lattner4b009652007-07-25 00:24:17 +0000646/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000647/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000648/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000649/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000650/// struct-declarator-list:
651/// struct-declarator
652/// struct-declarator-list ',' struct-declarator
653/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
654/// struct-declarator:
655/// declarator
656/// [GNU] declarator attributes[opt]
657/// declarator[opt] ':' constant-expression
658/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
659///
Chris Lattner3dd8d392008-04-10 06:46:29 +0000660void Parser::
661ParseStructDeclaration(DeclSpec &DS,
662 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000663 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattner3dd8d392008-04-10 06:46:29 +0000664 while (Tok.is(tok::kw___extension__))
Steve Naroffa9adf112007-08-20 22:28:22 +0000665 ConsumeToken();
666
667 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000668 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +0000669 ParseSpecifierQualifierList(DS);
670 // TODO: Does specifier-qualifier list correctly check that *something* is
671 // specified?
672
673 // If there are no declarators, issue a warning.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000674 if (Tok.is(tok::semi)) {
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000675 Diag(DSStart, diag::w_no_declarators);
Steve Naroffa9adf112007-08-20 22:28:22 +0000676 return;
677 }
678
679 // Read struct-declarators until we find the semicolon.
Chris Lattner3dd8d392008-04-10 06:46:29 +0000680 Fields.push_back(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +0000681 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +0000682 FieldDeclarator &DeclaratorInfo = Fields.back();
683
Steve Naroffa9adf112007-08-20 22:28:22 +0000684 /// struct-declarator: declarator
685 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000686 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000687 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +0000688
Chris Lattner34a01ad2007-10-09 17:33:22 +0000689 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000690 ConsumeToken();
691 ExprResult Res = ParseConstantExpression();
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000692 if (Res.isInvalid)
Steve Naroffa9adf112007-08-20 22:28:22 +0000693 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000694 else
Chris Lattner3dd8d392008-04-10 06:46:29 +0000695 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroffa9adf112007-08-20 22:28:22 +0000696 }
697
698 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000699 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000700 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000701
702 // If we don't have a comma, it is either the end of the list (a ';')
703 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000704 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000705 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000706
707 // Consume the comma.
708 ConsumeToken();
709
710 // Parse the next declarator.
Chris Lattner3dd8d392008-04-10 06:46:29 +0000711 Fields.push_back(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +0000712
713 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000714 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000715 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000716 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000717}
718
719/// ParseStructUnionBody
720/// struct-contents:
721/// struct-declaration-list
722/// [EXT] empty
723/// [GNU] "struct-declaration-list" without terminatoring ';'
724/// struct-declaration-list:
725/// struct-declaration
726/// struct-declaration-list struct-declaration
727/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
728///
Chris Lattner4b009652007-07-25 00:24:17 +0000729void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
730 unsigned TagType, DeclTy *TagDecl) {
731 SourceLocation LBraceLoc = ConsumeBrace();
732
733 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
734 // C++.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000735 if (Tok.is(tok::r_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000736 Diag(Tok, diag::ext_empty_struct_union_enum,
737 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
738
739 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000740 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
741
Chris Lattner4b009652007-07-25 00:24:17 +0000742 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000743 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000744 // Each iteration of this loop reads one struct-declaration.
745
746 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000747 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000748 Diag(Tok, diag::ext_extra_struct_semi);
749 ConsumeToken();
750 continue;
751 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000752
753 // Parse all the comma separated declarators.
754 DeclSpec DS;
755 FieldDeclarators.clear();
756 ParseStructDeclaration(DS, FieldDeclarators);
757
758 // Convert them all to fields.
759 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
760 FieldDeclarator &FD = FieldDeclarators[i];
761 // Install the declarator into the current TagDecl.
762 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
763 DS.getSourceRange().getBegin(),
764 FD.D, FD.BitfieldSize);
765 FieldDecls.push_back(Field);
766 }
767
Chris Lattner4b009652007-07-25 00:24:17 +0000768
Chris Lattner34a01ad2007-10-09 17:33:22 +0000769 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000770 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +0000771 } else if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000772 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
773 break;
774 } else {
775 Diag(Tok, diag::err_expected_semi_decl_list);
776 // Skip to end of block or statement
777 SkipUntil(tok::r_brace, true, true);
778 }
779 }
780
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000781 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000782
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000783 Actions.ActOnFields(CurScope,
Chris Lattner43b885f2008-02-25 21:04:36 +0000784 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000785 LBraceLoc, RBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000786
787 AttributeList *AttrList = 0;
788 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000789 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000790 AttrList = ParseAttributes(); // FIXME: where should I put them?
791}
792
793
794/// ParseEnumSpecifier
795/// enum-specifier: [C99 6.7.2.2]
796/// 'enum' identifier[opt] '{' enumerator-list '}'
797/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
798/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
799/// '}' attributes[opt]
800/// 'enum' identifier
801/// [GNU] 'enum' attributes[opt] identifier
802void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000803 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000804 SourceLocation StartLoc = ConsumeToken();
805
806 // Parse the tag portion of this.
807 DeclTy *TagDecl;
808 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
809 return;
810
Chris Lattner34a01ad2007-10-09 17:33:22 +0000811 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000812 ParseEnumBody(StartLoc, TagDecl);
813
814 // TODO: semantic analysis on the declspec for enums.
815 const char *PrevSpec = 0;
816 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
817 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
818}
819
820/// ParseEnumBody - Parse a {} enclosed enumerator-list.
821/// enumerator-list:
822/// enumerator
823/// enumerator-list ',' enumerator
824/// enumerator:
825/// enumeration-constant
826/// enumeration-constant '=' constant-expression
827/// enumeration-constant:
828/// identifier
829///
830void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
831 SourceLocation LBraceLoc = ConsumeBrace();
832
Chris Lattnerc9a92452007-08-27 17:24:30 +0000833 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +0000834 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000835 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
836
837 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
838
839 DeclTy *LastEnumConstDecl = 0;
840
841 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000842 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000843 IdentifierInfo *Ident = Tok.getIdentifierInfo();
844 SourceLocation IdentLoc = ConsumeToken();
845
846 SourceLocation EqualLoc;
847 ExprTy *AssignedVal = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000848 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000849 EqualLoc = ConsumeToken();
850 ExprResult Res = ParseConstantExpression();
851 if (Res.isInvalid)
852 SkipUntil(tok::comma, tok::r_brace, true, true);
853 else
854 AssignedVal = Res.Val;
855 }
856
857 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000858 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +0000859 LastEnumConstDecl,
860 IdentLoc, Ident,
861 EqualLoc, AssignedVal);
862 EnumConstantDecls.push_back(EnumConstDecl);
863 LastEnumConstDecl = EnumConstDecl;
864
Chris Lattner34a01ad2007-10-09 17:33:22 +0000865 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000866 break;
867 SourceLocation CommaLoc = ConsumeToken();
868
Chris Lattner34a01ad2007-10-09 17:33:22 +0000869 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +0000870 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
871 }
872
873 // Eat the }.
874 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
875
Steve Naroff0acc9c92007-09-15 18:49:24 +0000876 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +0000877 EnumConstantDecls.size());
878
879 DeclTy *AttrList = 0;
880 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000881 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000882 AttrList = ParseAttributes(); // FIXME: where do they do?
883}
884
885/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +0000886/// start of a type-qualifier-list.
887bool Parser::isTypeQualifier() const {
888 switch (Tok.getKind()) {
889 default: return false;
890 // type-qualifier
891 case tok::kw_const:
892 case tok::kw_volatile:
893 case tok::kw_restrict:
894 return true;
895 }
896}
897
898/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +0000899/// start of a specifier-qualifier-list.
900bool Parser::isTypeSpecifierQualifier() const {
901 switch (Tok.getKind()) {
902 default: return false;
903 // GNU attributes support.
904 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000905 // GNU typeof support.
906 case tok::kw_typeof:
907
Chris Lattner4b009652007-07-25 00:24:17 +0000908 // type-specifiers
909 case tok::kw_short:
910 case tok::kw_long:
911 case tok::kw_signed:
912 case tok::kw_unsigned:
913 case tok::kw__Complex:
914 case tok::kw__Imaginary:
915 case tok::kw_void:
916 case tok::kw_char:
917 case tok::kw_int:
918 case tok::kw_float:
919 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000920 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +0000921 case tok::kw__Bool:
922 case tok::kw__Decimal32:
923 case tok::kw__Decimal64:
924 case tok::kw__Decimal128:
925
926 // struct-or-union-specifier
927 case tok::kw_struct:
928 case tok::kw_union:
929 // enum-specifier
930 case tok::kw_enum:
931
932 // type-qualifier
933 case tok::kw_const:
934 case tok::kw_volatile:
935 case tok::kw_restrict:
936 return true;
937
938 // typedef-name
939 case tok::identifier:
940 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000941 }
942}
943
944/// isDeclarationSpecifier() - Return true if the current token is part of a
945/// declaration specifier.
946bool Parser::isDeclarationSpecifier() const {
947 switch (Tok.getKind()) {
948 default: return false;
949 // storage-class-specifier
950 case tok::kw_typedef:
951 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +0000952 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +0000953 case tok::kw_static:
954 case tok::kw_auto:
955 case tok::kw_register:
956 case tok::kw___thread:
957
958 // type-specifiers
959 case tok::kw_short:
960 case tok::kw_long:
961 case tok::kw_signed:
962 case tok::kw_unsigned:
963 case tok::kw__Complex:
964 case tok::kw__Imaginary:
965 case tok::kw_void:
966 case tok::kw_char:
967 case tok::kw_int:
968 case tok::kw_float:
969 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000970 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +0000971 case tok::kw__Bool:
972 case tok::kw__Decimal32:
973 case tok::kw__Decimal64:
974 case tok::kw__Decimal128:
975
976 // struct-or-union-specifier
977 case tok::kw_struct:
978 case tok::kw_union:
979 // enum-specifier
980 case tok::kw_enum:
981
982 // type-qualifier
983 case tok::kw_const:
984 case tok::kw_volatile:
985 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000986
Chris Lattner4b009652007-07-25 00:24:17 +0000987 // function-specifier
988 case tok::kw_inline:
Chris Lattnere35d2582007-08-09 16:40:21 +0000989
Chris Lattnerb707a7a2007-08-09 17:01:07 +0000990 // GNU typeof support.
991 case tok::kw_typeof:
992
993 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +0000994 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +0000995 return true;
996
997 // typedef-name
998 case tok::identifier:
999 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001000 }
1001}
1002
1003
1004/// ParseTypeQualifierListOpt
1005/// type-qualifier-list: [C99 6.7.5]
1006/// type-qualifier
1007/// [GNU] attributes
1008/// type-qualifier-list type-qualifier
1009/// [GNU] type-qualifier-list attributes
1010///
1011void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1012 while (1) {
1013 int isInvalid = false;
1014 const char *PrevSpec = 0;
1015 SourceLocation Loc = Tok.getLocation();
1016
1017 switch (Tok.getKind()) {
1018 default:
1019 // If this is not a type-qualifier token, we're done reading type
1020 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +00001021 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +00001022 return;
1023 case tok::kw_const:
1024 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1025 getLang())*2;
1026 break;
1027 case tok::kw_volatile:
1028 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1029 getLang())*2;
1030 break;
1031 case tok::kw_restrict:
1032 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1033 getLang())*2;
1034 break;
1035 case tok::kw___attribute:
1036 DS.AddAttributes(ParseAttributes());
1037 continue; // do *not* consume the next token!
1038 }
1039
1040 // If the specifier combination wasn't legal, issue a diagnostic.
1041 if (isInvalid) {
1042 assert(PrevSpec && "Method did not return previous specifier!");
1043 if (isInvalid == 1) // Error.
1044 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1045 else // extwarn.
1046 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1047 }
1048 ConsumeToken();
1049 }
1050}
1051
1052
1053/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1054///
1055void Parser::ParseDeclarator(Declarator &D) {
1056 /// This implements the 'declarator' production in the C grammar, then checks
1057 /// for well-formedness and issues diagnostics.
1058 ParseDeclaratorInternal(D);
Chris Lattner4b009652007-07-25 00:24:17 +00001059}
1060
1061/// ParseDeclaratorInternal
1062/// declarator: [C99 6.7.5]
1063/// pointer[opt] direct-declarator
1064/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1065/// [GNU] '&' restrict[opt] attributes[opt] declarator
1066///
1067/// pointer: [C99 6.7.5]
1068/// '*' type-qualifier-list[opt]
1069/// '*' type-qualifier-list[opt] pointer
1070///
1071void Parser::ParseDeclaratorInternal(Declarator &D) {
1072 tok::TokenKind Kind = Tok.getKind();
1073
1074 // Not a pointer or C++ reference.
Chris Lattner69f01932008-02-21 01:32:26 +00001075 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus))
Chris Lattner4b009652007-07-25 00:24:17 +00001076 return ParseDirectDeclarator(D);
1077
1078 // Otherwise, '*' -> pointer or '&' -> reference.
1079 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1080
1081 if (Kind == tok::star) {
Chris Lattner69f01932008-02-21 01:32:26 +00001082 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001083 DeclSpec DS;
1084
1085 ParseTypeQualifierListOpt(DS);
1086
1087 // Recursively parse the declarator.
1088 ParseDeclaratorInternal(D);
1089
1090 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001091 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1092 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001093 } else {
1094 // Is a reference
1095 DeclSpec DS;
1096
1097 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1098 // cv-qualifiers are introduced through the use of a typedef or of a
1099 // template type argument, in which case the cv-qualifiers are ignored.
1100 //
1101 // [GNU] Retricted references are allowed.
1102 // [GNU] Attributes on references are allowed.
1103 ParseTypeQualifierListOpt(DS);
1104
1105 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1106 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1107 Diag(DS.getConstSpecLoc(),
1108 diag::err_invalid_reference_qualifier_application,
1109 "const");
1110 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1111 Diag(DS.getVolatileSpecLoc(),
1112 diag::err_invalid_reference_qualifier_application,
1113 "volatile");
1114 }
1115
1116 // Recursively parse the declarator.
1117 ParseDeclaratorInternal(D);
1118
1119 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001120 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1121 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001122 }
1123}
1124
1125/// ParseDirectDeclarator
1126/// direct-declarator: [C99 6.7.5]
1127/// identifier
1128/// '(' declarator ')'
1129/// [GNU] '(' attributes declarator ')'
1130/// [C90] direct-declarator '[' constant-expression[opt] ']'
1131/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1132/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1133/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1134/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1135/// direct-declarator '(' parameter-type-list ')'
1136/// direct-declarator '(' identifier-list[opt] ')'
1137/// [GNU] direct-declarator '(' parameter-forward-declarations
1138/// parameter-type-list[opt] ')'
1139///
1140void Parser::ParseDirectDeclarator(Declarator &D) {
1141 // Parse the first direct-declarator seen.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001142 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001143 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1144 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1145 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001146 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001147 // direct-declarator: '(' declarator ')'
1148 // direct-declarator: '(' attributes declarator ')'
1149 // Example: 'char (*X)' or 'int (*XX)(void)'
1150 ParseParenDeclarator(D);
1151 } else if (D.mayOmitIdentifier()) {
1152 // This could be something simple like "int" (in which case the declarator
1153 // portion is empty), if an abstract-declarator is allowed.
1154 D.SetIdentifier(0, Tok.getLocation());
1155 } else {
1156 // Expected identifier or '('.
1157 Diag(Tok, diag::err_expected_ident_lparen);
1158 D.SetIdentifier(0, Tok.getLocation());
1159 }
1160
1161 assert(D.isPastIdentifier() &&
1162 "Haven't past the location of the identifier yet?");
1163
1164 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001165 if (Tok.is(tok::l_paren)) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00001166 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001167 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001168 ParseBracketDeclarator(D);
1169 } else {
1170 break;
1171 }
1172 }
1173}
1174
Chris Lattnera0d056d2008-04-06 05:45:57 +00001175/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1176/// only called before the identifier, so these are most likely just grouping
1177/// parens for precedence. If we find that these are actually function
1178/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1179///
1180/// direct-declarator:
1181/// '(' declarator ')'
1182/// [GNU] '(' attributes declarator ')'
1183///
1184void Parser::ParseParenDeclarator(Declarator &D) {
1185 SourceLocation StartLoc = ConsumeParen();
1186 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1187
1188 // If we haven't past the identifier yet (or where the identifier would be
1189 // stored, if this is an abstract declarator), then this is probably just
1190 // grouping parens. However, if this could be an abstract-declarator, then
1191 // this could also be the start of function arguments (consider 'void()').
1192 bool isGrouping;
1193
1194 if (!D.mayOmitIdentifier()) {
1195 // If this can't be an abstract-declarator, this *must* be a grouping
1196 // paren, because we haven't seen the identifier yet.
1197 isGrouping = true;
1198 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
1199 isDeclarationSpecifier()) { // 'int(int)' is a function.
1200 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1201 // considered to be a type, not a K&R identifier-list.
1202 isGrouping = false;
1203 } else {
1204 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1205 isGrouping = true;
1206 }
1207
1208 // If this is a grouping paren, handle:
1209 // direct-declarator: '(' declarator ')'
1210 // direct-declarator: '(' attributes declarator ')'
1211 if (isGrouping) {
1212 if (Tok.is(tok::kw___attribute))
1213 D.AddAttributes(ParseAttributes());
1214
1215 ParseDeclaratorInternal(D);
1216 // Match the ')'.
1217 MatchRHSPunctuation(tok::r_paren, StartLoc);
1218 return;
1219 }
1220
1221 // Okay, if this wasn't a grouping paren, it must be the start of a function
1222 // argument list. Recognize that this declarator will never have an
1223 // identifier (and remember where it would have been), then fall through to
1224 // the handling of argument lists.
1225 D.SetIdentifier(0, Tok.getLocation());
1226
1227 ParseFunctionDeclarator(StartLoc, D);
1228}
1229
1230/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1231/// declarator D up to a paren, which indicates that we are parsing function
1232/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001233///
1234/// This method also handles this portion of the grammar:
1235/// parameter-type-list: [C99 6.7.5]
1236/// parameter-list
1237/// parameter-list ',' '...'
1238///
1239/// parameter-list: [C99 6.7.5]
1240/// parameter-declaration
1241/// parameter-list ',' parameter-declaration
1242///
1243/// parameter-declaration: [C99 6.7.5]
1244/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00001245/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001246/// [GNU] declaration-specifiers declarator attributes
1247/// declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00001248/// [C++] declaration-specifiers abstract-declarator[opt]
1249/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001250/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1251///
Chris Lattnera0d056d2008-04-06 05:45:57 +00001252void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D) {
1253 // lparen is already consumed!
1254 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00001255
1256 // Okay, this is the parameter list of a function definition, or it is an
1257 // identifier list of a K&R-style function.
Chris Lattner4b009652007-07-25 00:24:17 +00001258
Chris Lattner34a01ad2007-10-09 17:33:22 +00001259 if (Tok.is(tok::r_paren)) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00001260 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00001261 // int() -> no prototype, no '...'.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001262 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/ false,
1263 /*variadic*/ false,
1264 /*arglist*/ 0, 0, LParenLoc));
1265
1266 ConsumeParen(); // Eat the closing ')'.
1267 return;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001268 } else if (Tok.is(tok::identifier) &&
Chris Lattner4b009652007-07-25 00:24:17 +00001269 // K&R identifier lists can't have typedefs as identifiers, per
1270 // C99 6.7.5.3p11.
1271 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1272 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1273 // normal declarators, not for abstract-declarators.
Chris Lattner35d9c912008-04-06 06:34:08 +00001274 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001275 }
1276
1277 // Finally, a normal, non-empty parameter type list.
1278
1279 // Build up an array of information about the parsed arguments.
1280 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00001281
1282 // Enter function-declaration scope, limiting any declarators to the
1283 // function prototype scope, including parameter declarators.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001284 EnterScope(Scope::DeclScope);
1285
1286 bool IsVariadic = false;
1287 while (1) {
1288 if (Tok.is(tok::ellipsis)) {
1289 IsVariadic = true;
Chris Lattner4b009652007-07-25 00:24:17 +00001290
Chris Lattner9f7564b2008-04-06 06:57:35 +00001291 // Check to see if this is "void(...)" which is not allowed.
1292 if (ParamInfo.empty()) {
1293 // Otherwise, parse parameter type list. If it starts with an
1294 // ellipsis, diagnose the malformed function.
1295 Diag(Tok, diag::err_ellipsis_first_arg);
1296 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001297 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00001298
Chris Lattner9f7564b2008-04-06 06:57:35 +00001299 ConsumeToken(); // Consume the ellipsis.
1300 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001301 }
1302
Chris Lattner9f7564b2008-04-06 06:57:35 +00001303 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00001304
Chris Lattner9f7564b2008-04-06 06:57:35 +00001305 // Parse the declaration-specifiers.
1306 DeclSpec DS;
1307 ParseDeclarationSpecifiers(DS);
1308
1309 // Parse the declarator. This is "PrototypeContext", because we must
1310 // accept either 'declarator' or 'abstract-declarator' here.
1311 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1312 ParseDeclarator(ParmDecl);
1313
1314 // Parse GNU attributes, if present.
1315 if (Tok.is(tok::kw___attribute))
1316 ParmDecl.AddAttributes(ParseAttributes());
1317
Chris Lattner9f7564b2008-04-06 06:57:35 +00001318 // Remember this parsed parameter in ParamInfo.
1319 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1320
Chris Lattner9f7564b2008-04-06 06:57:35 +00001321 // If no parameter was specified, verify that *something* was specified,
1322 // otherwise we have a missing type and identifier.
1323 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1324 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1325 // Completely missing, emit error.
1326 Diag(DSStart, diag::err_missing_param);
1327 } else {
1328 // Otherwise, we have something. Add it and let semantic analysis try
1329 // to grok it and add the result to the ParamInfo we are building.
1330
1331 // Inform the actions module about the parameter declarator, so it gets
1332 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00001333 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1334
1335 // Parse the default argument, if any. We parse the default
1336 // arguments in all dialects; the semantic analysis in
1337 // ActOnParamDefaultArgument will reject the default argument in
1338 // C.
1339 if (Tok.is(tok::equal)) {
1340 SourceLocation EqualLoc = Tok.getLocation();
1341
1342 // Consume the '='.
1343 ConsumeToken();
1344
1345 // Parse the default argument
1346 // FIXME: For C++, name lookup from within the default argument
1347 // should be able to find parameter names, but we haven't put them
1348 // in the scope. This means that we will accept ill-formed code
1349 // such as:
1350 //
1351 // int x;
1352 // void f(int x = x) { }
1353 ExprResult DefArgResult = ParseAssignmentExpression();
1354 if (DefArgResult.isInvalid) {
1355 SkipUntil(tok::comma, tok::r_paren, true, true);
1356 } else {
1357 // Inform the actions module about the default argument
1358 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1359 }
1360 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001361
1362 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001363 ParmDecl.getIdentifierLoc(), Param));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001364 }
1365
1366 // If the next token is a comma, consume it and keep reading arguments.
1367 if (Tok.isNot(tok::comma)) break;
1368
1369 // Consume the comma.
1370 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00001371 }
1372
Chris Lattner9f7564b2008-04-06 06:57:35 +00001373 // Leave prototype scope.
1374 ExitScope();
1375
Chris Lattner4b009652007-07-25 00:24:17 +00001376 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001377 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1378 &ParamInfo[0], ParamInfo.size(),
1379 LParenLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00001380
1381 // If we have the closing ')', eat it and we're done.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001382 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001383}
1384
Chris Lattner35d9c912008-04-06 06:34:08 +00001385/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1386/// we found a K&R-style identifier list instead of a type argument list. The
1387/// current token is known to be the first identifier in the list.
1388///
1389/// identifier-list: [C99 6.7.5]
1390/// identifier
1391/// identifier-list ',' identifier
1392///
1393void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1394 Declarator &D) {
1395 // Build up an array of information about the parsed arguments.
1396 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1397 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1398
1399 // If there was no identifier specified for the declarator, either we are in
1400 // an abstract-declarator, or we are in a parameter declarator which was found
1401 // to be abstract. In abstract-declarators, identifier lists are not valid:
1402 // diagnose this.
1403 if (!D.getIdentifier())
1404 Diag(Tok, diag::ext_ident_list_in_param);
1405
1406 // Tok is known to be the first identifier in the list. Remember this
1407 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00001408 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00001409 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1410 Tok.getLocation(), 0));
1411
Chris Lattner113a56b2008-04-06 06:39:19 +00001412 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00001413
1414 while (Tok.is(tok::comma)) {
1415 // Eat the comma.
1416 ConsumeToken();
1417
Chris Lattner113a56b2008-04-06 06:39:19 +00001418 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00001419 if (Tok.isNot(tok::identifier)) {
1420 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00001421 SkipUntil(tok::r_paren);
1422 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00001423 }
Chris Lattneracb67d92008-04-06 06:47:48 +00001424
Chris Lattner35d9c912008-04-06 06:34:08 +00001425 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00001426
1427 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1428 if (Actions.isTypeName(*ParmII, CurScope))
1429 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner35d9c912008-04-06 06:34:08 +00001430
1431 // Verify that the argument identifier has not already been mentioned.
1432 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner113a56b2008-04-06 06:39:19 +00001433 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1434 } else {
1435 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00001436 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1437 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00001438 }
Chris Lattner35d9c912008-04-06 06:34:08 +00001439
1440 // Eat the identifier.
1441 ConsumeToken();
1442 }
1443
Chris Lattner113a56b2008-04-06 06:39:19 +00001444 // Remember that we parsed a function type, and remember the attributes. This
1445 // function type is always a K&R style function type, which is not varargs and
1446 // has no prototype.
1447 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1448 &ParamInfo[0], ParamInfo.size(),
1449 LParenLoc));
Chris Lattner35d9c912008-04-06 06:34:08 +00001450
1451 // If we have the closing ')', eat it and we're done.
Chris Lattner113a56b2008-04-06 06:39:19 +00001452 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00001453}
Chris Lattnera0d056d2008-04-06 05:45:57 +00001454
Chris Lattner4b009652007-07-25 00:24:17 +00001455/// [C90] direct-declarator '[' constant-expression[opt] ']'
1456/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1457/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1458/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1459/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1460void Parser::ParseBracketDeclarator(Declarator &D) {
1461 SourceLocation StartLoc = ConsumeBracket();
1462
1463 // If valid, this location is the position where we read the 'static' keyword.
1464 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001465 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001466 StaticLoc = ConsumeToken();
1467
1468 // If there is a type-qualifier-list, read it now.
1469 DeclSpec DS;
1470 ParseTypeQualifierListOpt(DS);
1471
1472 // If we haven't already read 'static', check to see if there is one after the
1473 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001474 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001475 StaticLoc = ConsumeToken();
1476
1477 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1478 bool isStar = false;
1479 ExprResult NumElements(false);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001480
1481 // Handle the case where we have '[*]' as the array size. However, a leading
1482 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1483 // the the token after the star is a ']'. Since stars in arrays are
1484 // infrequent, use of lookahead is not costly here.
1485 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00001486 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00001487
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001488 if (StaticLoc.isValid())
1489 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1490 StaticLoc = SourceLocation(); // Drop the static.
1491 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001492 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001493 // Parse the assignment-expression now.
1494 NumElements = ParseAssignmentExpression();
1495 }
1496
1497 // If there was an error parsing the assignment-expression, recover.
1498 if (NumElements.isInvalid) {
1499 // If the expression was invalid, skip it.
1500 SkipUntil(tok::r_square);
1501 return;
1502 }
1503
1504 MatchRHSPunctuation(tok::r_square, StartLoc);
1505
1506 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1507 // it was not a constant expression.
1508 if (!getLang().C99) {
1509 // TODO: check C90 array constant exprness.
1510 if (isStar || StaticLoc.isValid() ||
1511 0/*TODO: NumElts is not a C90 constantexpr */)
1512 Diag(StartLoc, diag::ext_c99_array_usage);
1513 }
1514
1515 // Remember that we parsed a pointer type, and remember the type-quals.
1516 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1517 StaticLoc.isValid(), isStar,
1518 NumElements.Val, StartLoc));
1519}
1520
Steve Naroff7cbb1462007-07-31 12:34:36 +00001521/// [GNU] typeof-specifier:
1522/// typeof ( expressions )
1523/// typeof ( type-name )
1524///
1525void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001526 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00001527 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00001528 SourceLocation StartLoc = ConsumeToken();
1529
Chris Lattner34a01ad2007-10-09 17:33:22 +00001530 if (Tok.isNot(tok::l_paren)) {
Steve Naroff14bbce82007-08-02 02:53:48 +00001531 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1532 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00001533 }
1534 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1535
1536 if (isTypeSpecifierQualifier()) {
1537 TypeTy *Ty = ParseTypeName();
1538
Steve Naroff4c255ab2007-07-31 23:56:32 +00001539 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1540
Chris Lattner34a01ad2007-10-09 17:33:22 +00001541 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001542 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001543 return;
1544 }
1545 RParenLoc = ConsumeParen();
1546 const char *PrevSpec = 0;
1547 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1548 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1549 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001550 } else { // we have an expression.
1551 ExprResult Result = ParseExpression();
Steve Naroff4c255ab2007-07-31 23:56:32 +00001552
Chris Lattner34a01ad2007-10-09 17:33:22 +00001553 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001554 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001555 return;
1556 }
1557 RParenLoc = ConsumeParen();
1558 const char *PrevSpec = 0;
1559 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1560 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1561 Result.Val))
1562 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001563 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00001564}
1565