blob: 8a0dcce31c8b257bb060f924375e5bc521236b87 [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;
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:
522 ParseStructUnionSpecifier(DS);
523 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;
551 }
552 // If the specifier combination wasn't legal, issue a diagnostic.
553 if (isInvalid) {
554 assert(PrevSpec && "Method did not return previous specifier!");
555 if (isInvalid == 1) // Error.
556 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
557 else // extwarn.
558 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
559 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000560 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000561 ConsumeToken();
562 }
563}
564
565/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
566/// the first token has already been read and has been turned into an instance
567/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
568/// otherwise it returns false and fills in Decl.
569bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
570 AttributeList *Attr = 0;
571 // If attributes exist after tag, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000572 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000573 Attr = ParseAttributes();
574
575 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000576 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000577 Diag(Tok, diag::err_expected_ident_lbrace);
578
579 // Skip the rest of this declarator, up until the comma or semicolon.
580 SkipUntil(tok::comma, true);
581 return true;
582 }
583
584 // If an identifier is present, consume and remember it.
585 IdentifierInfo *Name = 0;
586 SourceLocation NameLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000587 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000588 Name = Tok.getIdentifierInfo();
589 NameLoc = ConsumeToken();
590 }
591
592 // There are three options here. If we have 'struct foo;', then this is a
593 // forward declaration. If we have 'struct foo {...' then this is a
594 // definition. Otherwise we have something like 'struct foo xyz', a reference.
595 //
596 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
597 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
598 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
599 //
600 Action::TagKind TK;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000601 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000602 TK = Action::TK_Definition;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000603 else if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000604 TK = Action::TK_Declaration;
605 else
606 TK = Action::TK_Reference;
Steve Naroff0acc9c92007-09-15 18:49:24 +0000607 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Chris Lattner4b009652007-07-25 00:24:17 +0000608 return false;
609}
610
611
612/// ParseStructUnionSpecifier
613/// struct-or-union-specifier: [C99 6.7.2.1]
614/// struct-or-union identifier[opt] '{' struct-contents '}'
615/// struct-or-union identifier
616/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
617/// '}' attributes[opt]
618/// [GNU] struct-or-union attributes[opt] identifier
619/// struct-or-union:
620/// 'struct'
621/// 'union'
622///
623void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
Chris Lattner2e78db32008-04-13 18:59:07 +0000624 assert((Tok.is(tok::kw_class) ||
625 Tok.is(tok::kw_struct) ||
626 Tok.is(tok::kw_union)) &&
627 "Not a class/struct/union specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000628 DeclSpec::TST TagType =
Chris Lattner2e78db32008-04-13 18:59:07 +0000629 Tok.is(tok::kw_class) ? DeclSpec::TST_class :
Chris Lattner34a01ad2007-10-09 17:33:22 +0000630 Tok.is(tok::kw_union) ? DeclSpec::TST_union : DeclSpec::TST_struct;
Chris Lattner4b009652007-07-25 00:24:17 +0000631 SourceLocation StartLoc = ConsumeToken();
632
633 // Parse the tag portion of this.
634 DeclTy *TagDecl;
635 if (ParseTag(TagDecl, TagType, StartLoc))
636 return;
637
638 // If there is a body, parse it and inform the actions module.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000639 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000640 ParseStructUnionBody(StartLoc, TagType, TagDecl);
641
642 const char *PrevSpec = 0;
643 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
644 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
645}
646
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000647/// ParseStructDeclaration - Parse a struct declaration without the terminating
648/// semicolon.
649///
Chris Lattner4b009652007-07-25 00:24:17 +0000650/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000651/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000652/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000653/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000654/// struct-declarator-list:
655/// struct-declarator
656/// struct-declarator-list ',' struct-declarator
657/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
658/// struct-declarator:
659/// declarator
660/// [GNU] declarator attributes[opt]
661/// declarator[opt] ':' constant-expression
662/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
663///
Chris Lattner3dd8d392008-04-10 06:46:29 +0000664void Parser::
665ParseStructDeclaration(DeclSpec &DS,
666 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000667 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattner3dd8d392008-04-10 06:46:29 +0000668 while (Tok.is(tok::kw___extension__))
Steve Naroffa9adf112007-08-20 22:28:22 +0000669 ConsumeToken();
670
671 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000672 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +0000673 ParseSpecifierQualifierList(DS);
674 // TODO: Does specifier-qualifier list correctly check that *something* is
675 // specified?
676
677 // If there are no declarators, issue a warning.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000678 if (Tok.is(tok::semi)) {
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000679 Diag(DSStart, diag::w_no_declarators);
Steve Naroffa9adf112007-08-20 22:28:22 +0000680 return;
681 }
682
683 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000684 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000685 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +0000686 FieldDeclarator &DeclaratorInfo = Fields.back();
687
Steve Naroffa9adf112007-08-20 22:28:22 +0000688 /// struct-declarator: declarator
689 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000690 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000691 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +0000692
Chris Lattner34a01ad2007-10-09 17:33:22 +0000693 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000694 ConsumeToken();
695 ExprResult Res = ParseConstantExpression();
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000696 if (Res.isInvalid)
Steve Naroffa9adf112007-08-20 22:28:22 +0000697 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000698 else
Chris Lattner3dd8d392008-04-10 06:46:29 +0000699 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroffa9adf112007-08-20 22:28:22 +0000700 }
701
702 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000703 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000704 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000705
706 // If we don't have a comma, it is either the end of the list (a ';')
707 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000708 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000709 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000710
711 // Consume the comma.
712 ConsumeToken();
713
714 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000715 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000716
717 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000718 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000719 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000720 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000721}
722
723/// ParseStructUnionBody
724/// struct-contents:
725/// struct-declaration-list
726/// [EXT] empty
727/// [GNU] "struct-declaration-list" without terminatoring ';'
728/// struct-declaration-list:
729/// struct-declaration
730/// struct-declaration-list struct-declaration
731/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
732///
Chris Lattner4b009652007-07-25 00:24:17 +0000733void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
734 unsigned TagType, DeclTy *TagDecl) {
735 SourceLocation LBraceLoc = ConsumeBrace();
736
737 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
738 // C++.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000739 if (Tok.is(tok::r_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000740 Diag(Tok, diag::ext_empty_struct_union_enum,
741 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
742
743 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000744 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
745
Chris Lattner4b009652007-07-25 00:24:17 +0000746 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000747 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000748 // Each iteration of this loop reads one struct-declaration.
749
750 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000751 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000752 Diag(Tok, diag::ext_extra_struct_semi);
753 ConsumeToken();
754 continue;
755 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000756
757 // Parse all the comma separated declarators.
758 DeclSpec DS;
759 FieldDeclarators.clear();
760 ParseStructDeclaration(DS, FieldDeclarators);
761
762 // Convert them all to fields.
763 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
764 FieldDeclarator &FD = FieldDeclarators[i];
765 // Install the declarator into the current TagDecl.
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000766 DeclTy *Field = Actions.ActOnField(CurScope,
Chris Lattner3dd8d392008-04-10 06:46:29 +0000767 DS.getSourceRange().getBegin(),
768 FD.D, FD.BitfieldSize);
769 FieldDecls.push_back(Field);
770 }
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:
911
Chris Lattner4b009652007-07-25 00:24:17 +0000912 // type-specifiers
913 case tok::kw_short:
914 case tok::kw_long:
915 case tok::kw_signed:
916 case tok::kw_unsigned:
917 case tok::kw__Complex:
918 case tok::kw__Imaginary:
919 case tok::kw_void:
920 case tok::kw_char:
921 case tok::kw_int:
922 case tok::kw_float:
923 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000924 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +0000925 case tok::kw__Bool:
926 case tok::kw__Decimal32:
927 case tok::kw__Decimal64:
928 case tok::kw__Decimal128:
929
Chris Lattner2e78db32008-04-13 18:59:07 +0000930 // struct-or-union-specifier (C99) or class-specifier (C++)
931 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000932 case tok::kw_struct:
933 case tok::kw_union:
934 // enum-specifier
935 case tok::kw_enum:
936
937 // type-qualifier
938 case tok::kw_const:
939 case tok::kw_volatile:
940 case tok::kw_restrict:
941 return true;
942
943 // typedef-name
944 case tok::identifier:
945 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000946 }
947}
948
949/// isDeclarationSpecifier() - Return true if the current token is part of a
950/// declaration specifier.
951bool Parser::isDeclarationSpecifier() const {
952 switch (Tok.getKind()) {
953 default: return false;
954 // storage-class-specifier
955 case tok::kw_typedef:
956 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +0000957 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +0000958 case tok::kw_static:
959 case tok::kw_auto:
960 case tok::kw_register:
961 case tok::kw___thread:
962
963 // type-specifiers
964 case tok::kw_short:
965 case tok::kw_long:
966 case tok::kw_signed:
967 case tok::kw_unsigned:
968 case tok::kw__Complex:
969 case tok::kw__Imaginary:
970 case tok::kw_void:
971 case tok::kw_char:
972 case tok::kw_int:
973 case tok::kw_float:
974 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000975 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +0000976 case tok::kw__Bool:
977 case tok::kw__Decimal32:
978 case tok::kw__Decimal64:
979 case tok::kw__Decimal128:
980
Chris Lattner2e78db32008-04-13 18:59:07 +0000981 // struct-or-union-specifier (C99) or class-specifier (C++)
982 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000983 case tok::kw_struct:
984 case tok::kw_union:
985 // enum-specifier
986 case tok::kw_enum:
987
988 // type-qualifier
989 case tok::kw_const:
990 case tok::kw_volatile:
991 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000992
Chris Lattner4b009652007-07-25 00:24:17 +0000993 // function-specifier
994 case tok::kw_inline:
Chris Lattnere35d2582007-08-09 16:40:21 +0000995
Chris Lattnerb707a7a2007-08-09 17:01:07 +0000996 // GNU typeof support.
997 case tok::kw_typeof:
998
999 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001000 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001001 return true;
1002
1003 // typedef-name
1004 case tok::identifier:
1005 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001006 }
1007}
1008
1009
1010/// ParseTypeQualifierListOpt
1011/// type-qualifier-list: [C99 6.7.5]
1012/// type-qualifier
1013/// [GNU] attributes
1014/// type-qualifier-list type-qualifier
1015/// [GNU] type-qualifier-list attributes
1016///
1017void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1018 while (1) {
1019 int isInvalid = false;
1020 const char *PrevSpec = 0;
1021 SourceLocation Loc = Tok.getLocation();
1022
1023 switch (Tok.getKind()) {
1024 default:
1025 // If this is not a type-qualifier token, we're done reading type
1026 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +00001027 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +00001028 return;
1029 case tok::kw_const:
1030 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1031 getLang())*2;
1032 break;
1033 case tok::kw_volatile:
1034 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1035 getLang())*2;
1036 break;
1037 case tok::kw_restrict:
1038 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1039 getLang())*2;
1040 break;
1041 case tok::kw___attribute:
1042 DS.AddAttributes(ParseAttributes());
1043 continue; // do *not* consume the next token!
1044 }
1045
1046 // If the specifier combination wasn't legal, issue a diagnostic.
1047 if (isInvalid) {
1048 assert(PrevSpec && "Method did not return previous specifier!");
1049 if (isInvalid == 1) // Error.
1050 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1051 else // extwarn.
1052 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1053 }
1054 ConsumeToken();
1055 }
1056}
1057
1058
1059/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1060///
1061void Parser::ParseDeclarator(Declarator &D) {
1062 /// This implements the 'declarator' production in the C grammar, then checks
1063 /// for well-formedness and issues diagnostics.
1064 ParseDeclaratorInternal(D);
Chris Lattner4b009652007-07-25 00:24:17 +00001065}
1066
1067/// ParseDeclaratorInternal
1068/// declarator: [C99 6.7.5]
1069/// pointer[opt] direct-declarator
1070/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1071/// [GNU] '&' restrict[opt] attributes[opt] declarator
1072///
1073/// pointer: [C99 6.7.5]
1074/// '*' type-qualifier-list[opt]
1075/// '*' type-qualifier-list[opt] pointer
1076///
1077void Parser::ParseDeclaratorInternal(Declarator &D) {
1078 tok::TokenKind Kind = Tok.getKind();
1079
1080 // Not a pointer or C++ reference.
Chris Lattner69f01932008-02-21 01:32:26 +00001081 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus))
Chris Lattner4b009652007-07-25 00:24:17 +00001082 return ParseDirectDeclarator(D);
1083
1084 // Otherwise, '*' -> pointer or '&' -> reference.
1085 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1086
1087 if (Kind == tok::star) {
Chris Lattner69f01932008-02-21 01:32:26 +00001088 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001089 DeclSpec DS;
1090
1091 ParseTypeQualifierListOpt(DS);
1092
1093 // Recursively parse the declarator.
1094 ParseDeclaratorInternal(D);
1095
1096 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001097 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1098 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001099 } else {
1100 // Is a reference
1101 DeclSpec DS;
1102
1103 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1104 // cv-qualifiers are introduced through the use of a typedef or of a
1105 // template type argument, in which case the cv-qualifiers are ignored.
1106 //
1107 // [GNU] Retricted references are allowed.
1108 // [GNU] Attributes on references are allowed.
1109 ParseTypeQualifierListOpt(DS);
1110
1111 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1112 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1113 Diag(DS.getConstSpecLoc(),
1114 diag::err_invalid_reference_qualifier_application,
1115 "const");
1116 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1117 Diag(DS.getVolatileSpecLoc(),
1118 diag::err_invalid_reference_qualifier_application,
1119 "volatile");
1120 }
1121
1122 // Recursively parse the declarator.
1123 ParseDeclaratorInternal(D);
1124
1125 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001126 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1127 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001128 }
1129}
1130
1131/// ParseDirectDeclarator
1132/// direct-declarator: [C99 6.7.5]
1133/// identifier
1134/// '(' declarator ')'
1135/// [GNU] '(' attributes declarator ')'
1136/// [C90] direct-declarator '[' constant-expression[opt] ']'
1137/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1138/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1139/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1140/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1141/// direct-declarator '(' parameter-type-list ')'
1142/// direct-declarator '(' identifier-list[opt] ')'
1143/// [GNU] direct-declarator '(' parameter-forward-declarations
1144/// parameter-type-list[opt] ')'
1145///
1146void Parser::ParseDirectDeclarator(Declarator &D) {
1147 // Parse the first direct-declarator seen.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001148 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001149 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1150 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1151 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001152 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001153 // direct-declarator: '(' declarator ')'
1154 // direct-declarator: '(' attributes declarator ')'
1155 // Example: 'char (*X)' or 'int (*XX)(void)'
1156 ParseParenDeclarator(D);
1157 } else if (D.mayOmitIdentifier()) {
1158 // This could be something simple like "int" (in which case the declarator
1159 // portion is empty), if an abstract-declarator is allowed.
1160 D.SetIdentifier(0, Tok.getLocation());
1161 } else {
1162 // Expected identifier or '('.
1163 Diag(Tok, diag::err_expected_ident_lparen);
1164 D.SetIdentifier(0, Tok.getLocation());
1165 }
1166
1167 assert(D.isPastIdentifier() &&
1168 "Haven't past the location of the identifier yet?");
1169
1170 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001171 if (Tok.is(tok::l_paren)) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00001172 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001173 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001174 ParseBracketDeclarator(D);
1175 } else {
1176 break;
1177 }
1178 }
1179}
1180
Chris Lattnera0d056d2008-04-06 05:45:57 +00001181/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1182/// only called before the identifier, so these are most likely just grouping
1183/// parens for precedence. If we find that these are actually function
1184/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1185///
1186/// direct-declarator:
1187/// '(' declarator ')'
1188/// [GNU] '(' attributes declarator ')'
1189///
1190void Parser::ParseParenDeclarator(Declarator &D) {
1191 SourceLocation StartLoc = ConsumeParen();
1192 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1193
1194 // If we haven't past the identifier yet (or where the identifier would be
1195 // stored, if this is an abstract declarator), then this is probably just
1196 // grouping parens. However, if this could be an abstract-declarator, then
1197 // this could also be the start of function arguments (consider 'void()').
1198 bool isGrouping;
1199
1200 if (!D.mayOmitIdentifier()) {
1201 // If this can't be an abstract-declarator, this *must* be a grouping
1202 // paren, because we haven't seen the identifier yet.
1203 isGrouping = true;
1204 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
1205 isDeclarationSpecifier()) { // 'int(int)' is a function.
1206 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1207 // considered to be a type, not a K&R identifier-list.
1208 isGrouping = false;
1209 } else {
1210 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1211 isGrouping = true;
1212 }
1213
1214 // If this is a grouping paren, handle:
1215 // direct-declarator: '(' declarator ')'
1216 // direct-declarator: '(' attributes declarator ')'
1217 if (isGrouping) {
1218 if (Tok.is(tok::kw___attribute))
1219 D.AddAttributes(ParseAttributes());
1220
1221 ParseDeclaratorInternal(D);
1222 // Match the ')'.
1223 MatchRHSPunctuation(tok::r_paren, StartLoc);
1224 return;
1225 }
1226
1227 // Okay, if this wasn't a grouping paren, it must be the start of a function
1228 // argument list. Recognize that this declarator will never have an
1229 // identifier (and remember where it would have been), then fall through to
1230 // the handling of argument lists.
1231 D.SetIdentifier(0, Tok.getLocation());
1232
1233 ParseFunctionDeclarator(StartLoc, D);
1234}
1235
1236/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1237/// declarator D up to a paren, which indicates that we are parsing function
1238/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001239///
1240/// This method also handles this portion of the grammar:
1241/// parameter-type-list: [C99 6.7.5]
1242/// parameter-list
1243/// parameter-list ',' '...'
1244///
1245/// parameter-list: [C99 6.7.5]
1246/// parameter-declaration
1247/// parameter-list ',' parameter-declaration
1248///
1249/// parameter-declaration: [C99 6.7.5]
1250/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00001251/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001252/// [GNU] declaration-specifiers declarator attributes
1253/// declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00001254/// [C++] declaration-specifiers abstract-declarator[opt]
1255/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001256/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1257///
Chris Lattnera0d056d2008-04-06 05:45:57 +00001258void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D) {
1259 // lparen is already consumed!
1260 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00001261
1262 // Okay, this is the parameter list of a function definition, or it is an
1263 // identifier list of a K&R-style function.
Chris Lattner4b009652007-07-25 00:24:17 +00001264
Chris Lattner34a01ad2007-10-09 17:33:22 +00001265 if (Tok.is(tok::r_paren)) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00001266 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00001267 // int() -> no prototype, no '...'.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001268 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/ false,
1269 /*variadic*/ false,
1270 /*arglist*/ 0, 0, LParenLoc));
1271
1272 ConsumeParen(); // Eat the closing ')'.
1273 return;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001274 } else if (Tok.is(tok::identifier) &&
Chris Lattner4b009652007-07-25 00:24:17 +00001275 // K&R identifier lists can't have typedefs as identifiers, per
1276 // C99 6.7.5.3p11.
1277 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1278 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1279 // normal declarators, not for abstract-declarators.
Chris Lattner35d9c912008-04-06 06:34:08 +00001280 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001281 }
1282
1283 // Finally, a normal, non-empty parameter type list.
1284
1285 // Build up an array of information about the parsed arguments.
1286 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00001287
1288 // Enter function-declaration scope, limiting any declarators to the
1289 // function prototype scope, including parameter declarators.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001290 EnterScope(Scope::DeclScope);
1291
1292 bool IsVariadic = false;
1293 while (1) {
1294 if (Tok.is(tok::ellipsis)) {
1295 IsVariadic = true;
Chris Lattner4b009652007-07-25 00:24:17 +00001296
Chris Lattner9f7564b2008-04-06 06:57:35 +00001297 // Check to see if this is "void(...)" which is not allowed.
1298 if (ParamInfo.empty()) {
1299 // Otherwise, parse parameter type list. If it starts with an
1300 // ellipsis, diagnose the malformed function.
1301 Diag(Tok, diag::err_ellipsis_first_arg);
1302 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001303 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00001304
Chris Lattner9f7564b2008-04-06 06:57:35 +00001305 ConsumeToken(); // Consume the ellipsis.
1306 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001307 }
1308
Chris Lattner9f7564b2008-04-06 06:57:35 +00001309 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00001310
Chris Lattner9f7564b2008-04-06 06:57:35 +00001311 // Parse the declaration-specifiers.
1312 DeclSpec DS;
1313 ParseDeclarationSpecifiers(DS);
1314
1315 // Parse the declarator. This is "PrototypeContext", because we must
1316 // accept either 'declarator' or 'abstract-declarator' here.
1317 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1318 ParseDeclarator(ParmDecl);
1319
1320 // Parse GNU attributes, if present.
1321 if (Tok.is(tok::kw___attribute))
1322 ParmDecl.AddAttributes(ParseAttributes());
1323
Chris Lattner9f7564b2008-04-06 06:57:35 +00001324 // Remember this parsed parameter in ParamInfo.
1325 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1326
Chris Lattner9f7564b2008-04-06 06:57:35 +00001327 // If no parameter was specified, verify that *something* was specified,
1328 // otherwise we have a missing type and identifier.
1329 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1330 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1331 // Completely missing, emit error.
1332 Diag(DSStart, diag::err_missing_param);
1333 } else {
1334 // Otherwise, we have something. Add it and let semantic analysis try
1335 // to grok it and add the result to the ParamInfo we are building.
1336
1337 // Inform the actions module about the parameter declarator, so it gets
1338 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00001339 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1340
1341 // Parse the default argument, if any. We parse the default
1342 // arguments in all dialects; the semantic analysis in
1343 // ActOnParamDefaultArgument will reject the default argument in
1344 // C.
1345 if (Tok.is(tok::equal)) {
1346 SourceLocation EqualLoc = Tok.getLocation();
1347
1348 // Consume the '='.
1349 ConsumeToken();
1350
1351 // Parse the default argument
Chris Lattner3e254fb2008-04-08 04:40:51 +00001352 ExprResult DefArgResult = ParseAssignmentExpression();
1353 if (DefArgResult.isInvalid) {
1354 SkipUntil(tok::comma, tok::r_paren, true, true);
1355 } else {
1356 // Inform the actions module about the default argument
1357 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1358 }
1359 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001360
1361 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001362 ParmDecl.getIdentifierLoc(), Param));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001363 }
1364
1365 // If the next token is a comma, consume it and keep reading arguments.
1366 if (Tok.isNot(tok::comma)) break;
1367
1368 // Consume the comma.
1369 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00001370 }
1371
Chris Lattner9f7564b2008-04-06 06:57:35 +00001372 // Leave prototype scope.
1373 ExitScope();
1374
Chris Lattner4b009652007-07-25 00:24:17 +00001375 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001376 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1377 &ParamInfo[0], ParamInfo.size(),
1378 LParenLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00001379
1380 // If we have the closing ')', eat it and we're done.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001381 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001382}
1383
Chris Lattner35d9c912008-04-06 06:34:08 +00001384/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1385/// we found a K&R-style identifier list instead of a type argument list. The
1386/// current token is known to be the first identifier in the list.
1387///
1388/// identifier-list: [C99 6.7.5]
1389/// identifier
1390/// identifier-list ',' identifier
1391///
1392void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1393 Declarator &D) {
1394 // Build up an array of information about the parsed arguments.
1395 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1396 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1397
1398 // If there was no identifier specified for the declarator, either we are in
1399 // an abstract-declarator, or we are in a parameter declarator which was found
1400 // to be abstract. In abstract-declarators, identifier lists are not valid:
1401 // diagnose this.
1402 if (!D.getIdentifier())
1403 Diag(Tok, diag::ext_ident_list_in_param);
1404
1405 // Tok is known to be the first identifier in the list. Remember this
1406 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00001407 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00001408 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1409 Tok.getLocation(), 0));
1410
Chris Lattner113a56b2008-04-06 06:39:19 +00001411 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00001412
1413 while (Tok.is(tok::comma)) {
1414 // Eat the comma.
1415 ConsumeToken();
1416
Chris Lattner113a56b2008-04-06 06:39:19 +00001417 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00001418 if (Tok.isNot(tok::identifier)) {
1419 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00001420 SkipUntil(tok::r_paren);
1421 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00001422 }
Chris Lattneracb67d92008-04-06 06:47:48 +00001423
Chris Lattner35d9c912008-04-06 06:34:08 +00001424 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00001425
1426 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1427 if (Actions.isTypeName(*ParmII, CurScope))
1428 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner35d9c912008-04-06 06:34:08 +00001429
1430 // Verify that the argument identifier has not already been mentioned.
1431 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner113a56b2008-04-06 06:39:19 +00001432 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1433 } else {
1434 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00001435 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1436 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00001437 }
Chris Lattner35d9c912008-04-06 06:34:08 +00001438
1439 // Eat the identifier.
1440 ConsumeToken();
1441 }
1442
Chris Lattner113a56b2008-04-06 06:39:19 +00001443 // Remember that we parsed a function type, and remember the attributes. This
1444 // function type is always a K&R style function type, which is not varargs and
1445 // has no prototype.
1446 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1447 &ParamInfo[0], ParamInfo.size(),
1448 LParenLoc));
Chris Lattner35d9c912008-04-06 06:34:08 +00001449
1450 // If we have the closing ')', eat it and we're done.
Chris Lattner113a56b2008-04-06 06:39:19 +00001451 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00001452}
Chris Lattnera0d056d2008-04-06 05:45:57 +00001453
Chris Lattner4b009652007-07-25 00:24:17 +00001454/// [C90] direct-declarator '[' constant-expression[opt] ']'
1455/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1456/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1457/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1458/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1459void Parser::ParseBracketDeclarator(Declarator &D) {
1460 SourceLocation StartLoc = ConsumeBracket();
1461
1462 // If valid, this location is the position where we read the 'static' keyword.
1463 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001464 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001465 StaticLoc = ConsumeToken();
1466
1467 // If there is a type-qualifier-list, read it now.
1468 DeclSpec DS;
1469 ParseTypeQualifierListOpt(DS);
1470
1471 // If we haven't already read 'static', check to see if there is one after the
1472 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001473 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001474 StaticLoc = ConsumeToken();
1475
1476 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1477 bool isStar = false;
1478 ExprResult NumElements(false);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001479
1480 // Handle the case where we have '[*]' as the array size. However, a leading
1481 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1482 // the the token after the star is a ']'. Since stars in arrays are
1483 // infrequent, use of lookahead is not costly here.
1484 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00001485 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00001486
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001487 if (StaticLoc.isValid())
1488 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1489 StaticLoc = SourceLocation(); // Drop the static.
1490 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001491 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001492 // Parse the assignment-expression now.
1493 NumElements = ParseAssignmentExpression();
1494 }
1495
1496 // If there was an error parsing the assignment-expression, recover.
1497 if (NumElements.isInvalid) {
1498 // If the expression was invalid, skip it.
1499 SkipUntil(tok::r_square);
1500 return;
1501 }
1502
1503 MatchRHSPunctuation(tok::r_square, StartLoc);
1504
1505 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1506 // it was not a constant expression.
1507 if (!getLang().C99) {
1508 // TODO: check C90 array constant exprness.
1509 if (isStar || StaticLoc.isValid() ||
1510 0/*TODO: NumElts is not a C90 constantexpr */)
1511 Diag(StartLoc, diag::ext_c99_array_usage);
1512 }
1513
1514 // Remember that we parsed a pointer type, and remember the type-quals.
1515 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1516 StaticLoc.isValid(), isStar,
1517 NumElements.Val, StartLoc));
1518}
1519
Steve Naroff7cbb1462007-07-31 12:34:36 +00001520/// [GNU] typeof-specifier:
1521/// typeof ( expressions )
1522/// typeof ( type-name )
1523///
1524void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001525 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00001526 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00001527 SourceLocation StartLoc = ConsumeToken();
1528
Chris Lattner34a01ad2007-10-09 17:33:22 +00001529 if (Tok.isNot(tok::l_paren)) {
Steve Naroff14bbce82007-08-02 02:53:48 +00001530 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1531 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00001532 }
1533 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1534
1535 if (isTypeSpecifierQualifier()) {
1536 TypeTy *Ty = ParseTypeName();
1537
Steve Naroff4c255ab2007-07-31 23:56:32 +00001538 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1539
Chris Lattner34a01ad2007-10-09 17:33:22 +00001540 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001541 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001542 return;
1543 }
1544 RParenLoc = ConsumeParen();
1545 const char *PrevSpec = 0;
1546 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1547 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1548 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001549 } else { // we have an expression.
1550 ExprResult Result = ParseExpression();
Steve Naroff4c255ab2007-07-31 23:56:32 +00001551
Chris Lattner34a01ad2007-10-09 17:33:22 +00001552 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001553 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001554 return;
1555 }
1556 RParenLoc = ConsumeParen();
1557 const char *PrevSpec = 0;
1558 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1559 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1560 Result.Val))
1561 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001562 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00001563}
1564