blob: 7d15e984ee62cc5baa77c794128d28633043a6ae [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner31e05722007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff08d92e42007-09-15 18:49:24 +000036 return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +000076 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Reid Spencer5f016e22007-07-11 17:01:13 +000077
78 AttributeList *CurrAttr = 0;
79
Chris Lattner04d66662007-10-09 17:33:22 +000080 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +000092 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
93 Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000094
Chris Lattner04d66662007-10-09 17:33:22 +000095 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000105 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 ConsumeParen(); // ignore the left paren loc for now
107
Chris Lattner04d66662007-10-09 17:33:22 +0000108 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
110 SourceLocation ParmLoc = ConsumeToken();
111
Chris Lattner04d66662007-10-09 17:33:22 +0000112 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000117 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000133 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 break;
135 ConsumeToken(); // Eat the comma, move to the next argument
136 }
Chris Lattner04d66662007-10-09 17:33:22 +0000137 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000145 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000165 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 break;
167 ConsumeToken(); // Eat the comma, move to the next argument
168 }
169 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000170 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner8f08cb72007-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///
Reid Spencer5f016e22007-07-11 17:01:13 +0000202Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattner8f08cb72007-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) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000222 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner8f08cb72007-08-25 06:57:03 +0000233
Reid Spencer5f016e22007-07-11 17:01:13 +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///
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000259 if (Tok.is(tok::kw_asm))
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 ParseSimpleAsm();
261
262 // If attributes are present, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000263 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 D.AddAttributes(ParseAttributes());
Steve Naroffbb204692007-09-12 14:07:44 +0000265
266 // Inform the current actions module that we just parsed this declarator.
267 // FIXME: pass asm & attributes.
Steve Naroff08d92e42007-09-15 18:49:24 +0000268 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Steve Naroffbb204692007-09-12 14:07:44 +0000269
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 // Parse declarator '=' initializer.
271 ExprResult Init;
Chris Lattner04d66662007-10-09 17:33:22 +0000272 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000273 ConsumeToken();
274 Init = ParseInitializer();
275 if (Init.isInvalid) {
276 SkipUntil(tok::semi);
277 return 0;
278 }
Steve Naroffbb204692007-09-12 14:07:44 +0000279 Actions.AddInitializerToDecl(LastDeclInGroup, Init.Val);
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 }
281
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 // If we don't have a comma, it is either the end of the list (a ';') or an
283 // error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000284 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 break;
286
287 // Consume the comma.
288 ConsumeToken();
289
290 // Parse the next declarator.
291 D.clear();
292 ParseDeclarator(D);
293 }
294
Chris Lattner04d66662007-10-09 17:33:22 +0000295 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000296 ConsumeToken();
297 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
298 }
Fariborz Jahanianbdd15f72008-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 Jahanian335a2d42008-01-04 23:04:08 +0000302 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000303 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
304 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 Diag(Tok, diag::err_parse_error);
306 // Skip to end of block or statement
Chris Lattnered442382007-08-21 18:36:18 +0000307 SkipUntil(tok::r_brace, true, true);
Chris Lattner04d66662007-10-09 17:33:22 +0000308 if (Tok.is(tok::semi))
Reid Spencer5f016e22007-07-11 17:01:13 +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.
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 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 Naroff2cb64ec2007-07-31 23:56:32 +0000380/// [GNU] typeof-specifier
Reid Spencer5f016e22007-07-11 17:01:13 +0000381/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000382/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner81c018d2008-03-13 06:29:04 +0000391 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff4fa7afd2007-08-22 23:18:22 +0000409 if (isInvalid)
410 break;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000411 // FIXME: restrict this to "id" and ObjC classnames.
Chris Lattner81c018d2008-03-13 06:29:04 +0000412 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000413 ConsumeToken(); // The identifier
414 if (Tok.is(tok::less)) {
Steve Narofff908a872007-10-30 02:23:23 +0000415 SourceLocation endProtoLoc;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000416 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofff908a872007-10-30 02:23:23 +0000417 ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc);
Fariborz Jahanian4b6c9052007-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 Naroff4fa7afd2007-08-22 23:18:22 +0000424 }
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000425 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Kremenek7a9d49f2007-12-11 21:27:55 +0000432 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff8d54bf22007-12-18 00:16:02 +0000449 case tok::kw___private_extern__:
Steve Naroffd6326c62008-01-25 22:14:40 +0000450 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc, PrevSpec);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000451 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000452 case tok::kw_static:
453 if (DS.isThreadSpecified())
454 Diag(Tok, diag::ext_thread_before, "static");
455 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
456 break;
457 case tok::kw_auto:
458 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
459 break;
460 case tok::kw_register:
461 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
462 break;
463 case tok::kw___thread:
464 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
465 break;
466
467 // type-specifiers
468 case tok::kw_short:
469 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
470 break;
471 case tok::kw_long:
472 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
473 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
474 else
475 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
476 break;
477 case tok::kw_signed:
478 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
479 break;
480 case tok::kw_unsigned:
481 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
482 break;
483 case tok::kw__Complex:
484 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
485 break;
486 case tok::kw__Imaginary:
487 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
488 break;
489 case tok::kw_void:
490 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
491 break;
492 case tok::kw_char:
493 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
494 break;
495 case tok::kw_int:
496 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
497 break;
498 case tok::kw_float:
499 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
500 break;
501 case tok::kw_double:
502 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
503 break;
504 case tok::kw_bool: // [C++ 2.11p1]
505 case tok::kw__Bool:
506 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
507 break;
508 case tok::kw__Decimal32:
509 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
510 break;
511 case tok::kw__Decimal64:
512 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
513 break;
514 case tok::kw__Decimal128:
515 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
516 break;
517
518 case tok::kw_struct:
519 case tok::kw_union:
520 ParseStructUnionSpecifier(DS);
521 continue;
522 case tok::kw_enum:
523 ParseEnumSpecifier(DS);
524 continue;
525
Steve Naroffd1861fd2007-07-31 12:34:36 +0000526 // GNU typeof support.
527 case tok::kw_typeof:
528 ParseTypeofSpecifier(DS);
529 continue;
530
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 // type-qualifier
532 case tok::kw_const:
533 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
534 getLang())*2;
535 break;
536 case tok::kw_volatile:
537 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
538 getLang())*2;
539 break;
540 case tok::kw_restrict:
541 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
542 getLang())*2;
543 break;
544
545 // function-specifier
546 case tok::kw_inline:
547 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
548 break;
549 }
550 // If the specifier combination wasn't legal, issue a diagnostic.
551 if (isInvalid) {
552 assert(PrevSpec && "Method did not return previous specifier!");
553 if (isInvalid == 1) // Error.
554 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
555 else // extwarn.
556 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
557 }
Chris Lattner81c018d2008-03-13 06:29:04 +0000558 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 ConsumeToken();
560 }
561}
562
563/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
564/// the first token has already been read and has been turned into an instance
565/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
566/// otherwise it returns false and fills in Decl.
567bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
568 AttributeList *Attr = 0;
569 // If attributes exist after tag, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000570 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000571 Attr = ParseAttributes();
572
573 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner04d66662007-10-09 17:33:22 +0000574 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000575 Diag(Tok, diag::err_expected_ident_lbrace);
Chris Lattnere80a59c2007-07-25 00:24:17 +0000576
577 // Skip the rest of this declarator, up until the comma or semicolon.
578 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000579 return true;
580 }
581
582 // If an identifier is present, consume and remember it.
583 IdentifierInfo *Name = 0;
584 SourceLocation NameLoc;
Chris Lattner04d66662007-10-09 17:33:22 +0000585 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 Name = Tok.getIdentifierInfo();
587 NameLoc = ConsumeToken();
588 }
589
590 // There are three options here. If we have 'struct foo;', then this is a
591 // forward declaration. If we have 'struct foo {...' then this is a
592 // definition. Otherwise we have something like 'struct foo xyz', a reference.
593 //
594 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
595 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
596 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
597 //
598 Action::TagKind TK;
Chris Lattner04d66662007-10-09 17:33:22 +0000599 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +0000600 TK = Action::TK_Definition;
Chris Lattner04d66662007-10-09 17:33:22 +0000601 else if (Tok.is(tok::semi))
Reid Spencer5f016e22007-07-11 17:01:13 +0000602 TK = Action::TK_Declaration;
603 else
604 TK = Action::TK_Reference;
Steve Naroff08d92e42007-09-15 18:49:24 +0000605 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 return false;
607}
608
609
610/// ParseStructUnionSpecifier
611/// struct-or-union-specifier: [C99 6.7.2.1]
612/// struct-or-union identifier[opt] '{' struct-contents '}'
613/// struct-or-union identifier
614/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
615/// '}' attributes[opt]
616/// [GNU] struct-or-union attributes[opt] identifier
617/// struct-or-union:
618/// 'struct'
619/// 'union'
620///
621void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +0000622 assert((Tok.is(tok::kw_struct) || Tok.is(tok::kw_union)) &&
623 "Not a struct/union specifier");
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 DeclSpec::TST TagType =
Chris Lattner04d66662007-10-09 17:33:22 +0000625 Tok.is(tok::kw_union) ? DeclSpec::TST_union : DeclSpec::TST_struct;
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 SourceLocation StartLoc = ConsumeToken();
627
628 // Parse the tag portion of this.
629 DeclTy *TagDecl;
630 if (ParseTag(TagDecl, TagType, StartLoc))
631 return;
632
633 // If there is a body, parse it and inform the actions module.
Chris Lattner04d66662007-10-09 17:33:22 +0000634 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 ParseStructUnionBody(StartLoc, TagType, TagDecl);
636
637 const char *PrevSpec = 0;
638 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
639 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
640}
641
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000642/// ParseStructDeclaration - Parse a struct declaration without the terminating
643/// semicolon.
644///
Reid Spencer5f016e22007-07-11 17:01:13 +0000645/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000646/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000647/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000648/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000649/// struct-declarator-list:
650/// struct-declarator
651/// struct-declarator-list ',' struct-declarator
652/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
653/// struct-declarator:
654/// declarator
655/// [GNU] declarator attributes[opt]
656/// declarator[opt] ':' constant-expression
657/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
658///
Steve Naroff28a7ca82007-08-20 22:28:22 +0000659void Parser::ParseStructDeclaration(DeclTy *TagDecl,
Steve Naroff4e6526b2007-08-28 16:31:47 +0000660 llvm::SmallVectorImpl<DeclTy*> &FieldDecls) {
Steve Naroff28a7ca82007-08-20 22:28:22 +0000661 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattner04d66662007-10-09 17:33:22 +0000662 if (Tok.is(tok::kw___extension__))
Steve Naroff28a7ca82007-08-20 22:28:22 +0000663 ConsumeToken();
664
665 // Parse the common specifier-qualifiers-list piece.
666 DeclSpec DS;
667 SourceLocation SpecQualLoc = Tok.getLocation();
668 ParseSpecifierQualifierList(DS);
669 // TODO: Does specifier-qualifier list correctly check that *something* is
670 // specified?
671
672 // If there are no declarators, issue a warning.
Chris Lattner04d66662007-10-09 17:33:22 +0000673 if (Tok.is(tok::semi)) {
Steve Naroffe7a37302008-02-11 22:40:08 +0000674 Diag(SpecQualLoc, diag::w_no_declarators);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000675 return;
676 }
677
678 // Read struct-declarators until we find the semicolon.
679 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
680
681 while (1) {
682 /// struct-declarator: declarator
683 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +0000684 if (Tok.isNot(tok::colon))
Steve Naroff28a7ca82007-08-20 22:28:22 +0000685 ParseDeclarator(DeclaratorInfo);
686
687 ExprTy *BitfieldSize = 0;
Chris Lattner04d66662007-10-09 17:33:22 +0000688 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +0000689 ConsumeToken();
690 ExprResult Res = ParseConstantExpression();
691 if (Res.isInvalid) {
692 SkipUntil(tok::semi, true, true);
693 } else {
694 BitfieldSize = Res.Val;
695 }
696 }
697
698 // If attributes exist after the declarator, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000699 if (Tok.is(tok::kw___attribute))
Steve Naroff28a7ca82007-08-20 22:28:22 +0000700 DeclaratorInfo.AddAttributes(ParseAttributes());
701
702 // Install the declarator into the current TagDecl.
Steve Naroff08d92e42007-09-15 18:49:24 +0000703 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl, SpecQualLoc,
Steve Naroff28a7ca82007-08-20 22:28:22 +0000704 DeclaratorInfo, BitfieldSize);
705 FieldDecls.push_back(Field);
706
707 // If we don't have a comma, it is either the end of the list (a ';')
708 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000709 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000710 return;
Steve Naroff28a7ca82007-08-20 22:28:22 +0000711
712 // Consume the comma.
713 ConsumeToken();
714
715 // Parse the next declarator.
716 DeclaratorInfo.clear();
717
718 // Attributes are only allowed on the second declarator.
Chris Lattner04d66662007-10-09 17:33:22 +0000719 if (Tok.is(tok::kw___attribute))
Steve Naroff28a7ca82007-08-20 22:28:22 +0000720 DeclaratorInfo.AddAttributes(ParseAttributes());
721 }
Steve Naroff28a7ca82007-08-20 22:28:22 +0000722}
723
724/// ParseStructUnionBody
725/// struct-contents:
726/// struct-declaration-list
727/// [EXT] empty
728/// [GNU] "struct-declaration-list" without terminatoring ';'
729/// struct-declaration-list:
730/// struct-declaration
731/// struct-declaration-list struct-declaration
732/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
733///
Reid Spencer5f016e22007-07-11 17:01:13 +0000734void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
735 unsigned TagType, DeclTy *TagDecl) {
736 SourceLocation LBraceLoc = ConsumeBrace();
737
738 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
739 // C++.
Chris Lattner04d66662007-10-09 17:33:22 +0000740 if (Tok.is(tok::r_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +0000741 Diag(Tok, diag::ext_empty_struct_union_enum,
742 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
743
744 llvm::SmallVector<DeclTy*, 32> FieldDecls;
745
746 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +0000747 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000748 // Each iteration of this loop reads one struct-declaration.
749
750 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +0000751 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000752 Diag(Tok, diag::ext_extra_struct_semi);
753 ConsumeToken();
754 continue;
755 }
Steve Naroff28a7ca82007-08-20 22:28:22 +0000756 ParseStructDeclaration(TagDecl, FieldDecls);
Reid Spencer5f016e22007-07-11 17:01:13 +0000757
Chris Lattner04d66662007-10-09 17:33:22 +0000758 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +0000760 } else if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000761 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
762 break;
763 } else {
764 Diag(Tok, diag::err_expected_semi_decl_list);
765 // Skip to end of block or statement
766 SkipUntil(tok::r_brace, true, true);
767 }
768 }
769
Steve Naroff60fccee2007-10-29 21:38:07 +0000770 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000771
Fariborz Jahanian9d048ff2007-09-29 00:54:24 +0000772 Actions.ActOnFields(CurScope,
Chris Lattnerc81c8142008-02-25 21:04:36 +0000773 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
Steve Naroff60fccee2007-10-29 21:38:07 +0000774 LBraceLoc, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000775
776 AttributeList *AttrList = 0;
777 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000778 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000779 AttrList = ParseAttributes(); // FIXME: where should I put them?
780}
781
782
783/// ParseEnumSpecifier
784/// enum-specifier: [C99 6.7.2.2]
785/// 'enum' identifier[opt] '{' enumerator-list '}'
786/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
787/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
788/// '}' attributes[opt]
789/// 'enum' identifier
790/// [GNU] 'enum' attributes[opt] identifier
791void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +0000792 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 SourceLocation StartLoc = ConsumeToken();
794
795 // Parse the tag portion of this.
796 DeclTy *TagDecl;
797 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
798 return;
799
Chris Lattner04d66662007-10-09 17:33:22 +0000800 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +0000801 ParseEnumBody(StartLoc, TagDecl);
802
803 // TODO: semantic analysis on the declspec for enums.
804 const char *PrevSpec = 0;
805 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
806 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
807}
808
809/// ParseEnumBody - Parse a {} enclosed enumerator-list.
810/// enumerator-list:
811/// enumerator
812/// enumerator-list ',' enumerator
813/// enumerator:
814/// enumeration-constant
815/// enumeration-constant '=' constant-expression
816/// enumeration-constant:
817/// identifier
818///
819void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
820 SourceLocation LBraceLoc = ConsumeBrace();
821
Chris Lattner7946dd32007-08-27 17:24:30 +0000822 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +0000823 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Reid Spencer5f016e22007-07-11 17:01:13 +0000824 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
825
826 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
827
828 DeclTy *LastEnumConstDecl = 0;
829
830 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +0000831 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 IdentifierInfo *Ident = Tok.getIdentifierInfo();
833 SourceLocation IdentLoc = ConsumeToken();
834
835 SourceLocation EqualLoc;
836 ExprTy *AssignedVal = 0;
Chris Lattner04d66662007-10-09 17:33:22 +0000837 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000838 EqualLoc = ConsumeToken();
839 ExprResult Res = ParseConstantExpression();
840 if (Res.isInvalid)
841 SkipUntil(tok::comma, tok::r_brace, true, true);
842 else
843 AssignedVal = Res.Val;
844 }
845
846 // Install the enumerator constant into EnumDecl.
Steve Naroff08d92e42007-09-15 18:49:24 +0000847 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +0000848 LastEnumConstDecl,
849 IdentLoc, Ident,
850 EqualLoc, AssignedVal);
851 EnumConstantDecls.push_back(EnumConstDecl);
852 LastEnumConstDecl = EnumConstDecl;
853
Chris Lattner04d66662007-10-09 17:33:22 +0000854 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000855 break;
856 SourceLocation CommaLoc = ConsumeToken();
857
Chris Lattner04d66662007-10-09 17:33:22 +0000858 if (Tok.isNot(tok::identifier) && !getLang().C99)
Reid Spencer5f016e22007-07-11 17:01:13 +0000859 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
860 }
861
862 // Eat the }.
863 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
864
Steve Naroff08d92e42007-09-15 18:49:24 +0000865 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Reid Spencer5f016e22007-07-11 17:01:13 +0000866 EnumConstantDecls.size());
867
868 DeclTy *AttrList = 0;
869 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000870 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000871 AttrList = ParseAttributes(); // FIXME: where do they do?
872}
873
874/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +0000875/// start of a type-qualifier-list.
876bool Parser::isTypeQualifier() const {
877 switch (Tok.getKind()) {
878 default: return false;
879 // type-qualifier
880 case tok::kw_const:
881 case tok::kw_volatile:
882 case tok::kw_restrict:
883 return true;
884 }
885}
886
887/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +0000888/// start of a specifier-qualifier-list.
889bool Parser::isTypeSpecifierQualifier() const {
890 switch (Tok.getKind()) {
891 default: return false;
892 // GNU attributes support.
893 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +0000894 // GNU typeof support.
895 case tok::kw_typeof:
896
Reid Spencer5f016e22007-07-11 17:01:13 +0000897 // type-specifiers
898 case tok::kw_short:
899 case tok::kw_long:
900 case tok::kw_signed:
901 case tok::kw_unsigned:
902 case tok::kw__Complex:
903 case tok::kw__Imaginary:
904 case tok::kw_void:
905 case tok::kw_char:
906 case tok::kw_int:
907 case tok::kw_float:
908 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +0000909 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +0000910 case tok::kw__Bool:
911 case tok::kw__Decimal32:
912 case tok::kw__Decimal64:
913 case tok::kw__Decimal128:
914
915 // struct-or-union-specifier
916 case tok::kw_struct:
917 case tok::kw_union:
918 // enum-specifier
919 case tok::kw_enum:
920
921 // type-qualifier
922 case tok::kw_const:
923 case tok::kw_volatile:
924 case tok::kw_restrict:
925 return true;
926
927 // typedef-name
928 case tok::identifier:
929 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000930 }
931}
932
933/// isDeclarationSpecifier() - Return true if the current token is part of a
934/// declaration specifier.
935bool Parser::isDeclarationSpecifier() const {
936 switch (Tok.getKind()) {
937 default: return false;
938 // storage-class-specifier
939 case tok::kw_typedef:
940 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +0000941 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +0000942 case tok::kw_static:
943 case tok::kw_auto:
944 case tok::kw_register:
945 case tok::kw___thread:
946
947 // type-specifiers
948 case tok::kw_short:
949 case tok::kw_long:
950 case tok::kw_signed:
951 case tok::kw_unsigned:
952 case tok::kw__Complex:
953 case tok::kw__Imaginary:
954 case tok::kw_void:
955 case tok::kw_char:
956 case tok::kw_int:
957 case tok::kw_float:
958 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +0000959 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +0000960 case tok::kw__Bool:
961 case tok::kw__Decimal32:
962 case tok::kw__Decimal64:
963 case tok::kw__Decimal128:
964
965 // struct-or-union-specifier
966 case tok::kw_struct:
967 case tok::kw_union:
968 // enum-specifier
969 case tok::kw_enum:
970
971 // type-qualifier
972 case tok::kw_const:
973 case tok::kw_volatile:
974 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +0000975
Reid Spencer5f016e22007-07-11 17:01:13 +0000976 // function-specifier
977 case tok::kw_inline:
Chris Lattnerd6c7c182007-08-09 16:40:21 +0000978
Chris Lattner1ef08762007-08-09 17:01:07 +0000979 // GNU typeof support.
980 case tok::kw_typeof:
981
982 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +0000983 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +0000984 return true;
985
986 // typedef-name
987 case tok::identifier:
988 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000989 }
990}
991
992
993/// ParseTypeQualifierListOpt
994/// type-qualifier-list: [C99 6.7.5]
995/// type-qualifier
996/// [GNU] attributes
997/// type-qualifier-list type-qualifier
998/// [GNU] type-qualifier-list attributes
999///
1000void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1001 while (1) {
1002 int isInvalid = false;
1003 const char *PrevSpec = 0;
1004 SourceLocation Loc = Tok.getLocation();
1005
1006 switch (Tok.getKind()) {
1007 default:
1008 // If this is not a type-qualifier token, we're done reading type
1009 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +00001010 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001011 return;
1012 case tok::kw_const:
1013 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1014 getLang())*2;
1015 break;
1016 case tok::kw_volatile:
1017 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1018 getLang())*2;
1019 break;
1020 case tok::kw_restrict:
1021 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1022 getLang())*2;
1023 break;
1024 case tok::kw___attribute:
1025 DS.AddAttributes(ParseAttributes());
1026 continue; // do *not* consume the next token!
1027 }
1028
1029 // If the specifier combination wasn't legal, issue a diagnostic.
1030 if (isInvalid) {
1031 assert(PrevSpec && "Method did not return previous specifier!");
1032 if (isInvalid == 1) // Error.
1033 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1034 else // extwarn.
1035 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1036 }
1037 ConsumeToken();
1038 }
1039}
1040
1041
1042/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1043///
1044void Parser::ParseDeclarator(Declarator &D) {
1045 /// This implements the 'declarator' production in the C grammar, then checks
1046 /// for well-formedness and issues diagnostics.
1047 ParseDeclaratorInternal(D);
1048
1049 // TODO: validate D.
1050
1051}
1052
1053/// ParseDeclaratorInternal
1054/// declarator: [C99 6.7.5]
1055/// pointer[opt] direct-declarator
1056/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1057/// [GNU] '&' restrict[opt] attributes[opt] declarator
1058///
1059/// pointer: [C99 6.7.5]
1060/// '*' type-qualifier-list[opt]
1061/// '*' type-qualifier-list[opt] pointer
1062///
1063void Parser::ParseDeclaratorInternal(Declarator &D) {
1064 tok::TokenKind Kind = Tok.getKind();
1065
1066 // Not a pointer or C++ reference.
Chris Lattner76549142008-02-21 01:32:26 +00001067 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus))
Reid Spencer5f016e22007-07-11 17:01:13 +00001068 return ParseDirectDeclarator(D);
1069
1070 // Otherwise, '*' -> pointer or '&' -> reference.
1071 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1072
1073 if (Kind == tok::star) {
Chris Lattner76549142008-02-21 01:32:26 +00001074 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00001075 DeclSpec DS;
1076
1077 ParseTypeQualifierListOpt(DS);
1078
1079 // Recursively parse the declarator.
1080 ParseDeclaratorInternal(D);
1081
1082 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001083 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1084 DS.TakeAttributes()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001085 } else {
1086 // Is a reference
1087 DeclSpec DS;
1088
1089 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1090 // cv-qualifiers are introduced through the use of a typedef or of a
1091 // template type argument, in which case the cv-qualifiers are ignored.
1092 //
1093 // [GNU] Retricted references are allowed.
1094 // [GNU] Attributes on references are allowed.
1095 ParseTypeQualifierListOpt(DS);
1096
1097 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1098 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1099 Diag(DS.getConstSpecLoc(),
1100 diag::err_invalid_reference_qualifier_application,
1101 "const");
1102 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1103 Diag(DS.getVolatileSpecLoc(),
1104 diag::err_invalid_reference_qualifier_application,
1105 "volatile");
1106 }
1107
1108 // Recursively parse the declarator.
1109 ParseDeclaratorInternal(D);
1110
1111 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001112 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1113 DS.TakeAttributes()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001114 }
1115}
1116
1117/// ParseDirectDeclarator
1118/// direct-declarator: [C99 6.7.5]
1119/// identifier
1120/// '(' declarator ')'
1121/// [GNU] '(' attributes declarator ')'
1122/// [C90] direct-declarator '[' constant-expression[opt] ']'
1123/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1124/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1125/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1126/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1127/// direct-declarator '(' parameter-type-list ')'
1128/// direct-declarator '(' identifier-list[opt] ')'
1129/// [GNU] direct-declarator '(' parameter-forward-declarations
1130/// parameter-type-list[opt] ')'
1131///
1132void Parser::ParseDirectDeclarator(Declarator &D) {
1133 // Parse the first direct-declarator seen.
Chris Lattner04d66662007-10-09 17:33:22 +00001134 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001135 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1136 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1137 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001138 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001139 // direct-declarator: '(' declarator ')'
1140 // direct-declarator: '(' attributes declarator ')'
1141 // Example: 'char (*X)' or 'int (*XX)(void)'
1142 ParseParenDeclarator(D);
1143 } else if (D.mayOmitIdentifier()) {
1144 // This could be something simple like "int" (in which case the declarator
1145 // portion is empty), if an abstract-declarator is allowed.
1146 D.SetIdentifier(0, Tok.getLocation());
1147 } else {
1148 // Expected identifier or '('.
1149 Diag(Tok, diag::err_expected_ident_lparen);
1150 D.SetIdentifier(0, Tok.getLocation());
1151 }
1152
1153 assert(D.isPastIdentifier() &&
1154 "Haven't past the location of the identifier yet?");
1155
1156 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00001157 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001158 ParseParenDeclarator(D);
Chris Lattner04d66662007-10-09 17:33:22 +00001159 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001160 ParseBracketDeclarator(D);
1161 } else {
1162 break;
1163 }
1164 }
1165}
1166
1167/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
1168/// either be before the identifier (in which case these are just grouping
1169/// parens for precedence) or it may be after the identifier, in which case
1170/// these are function arguments.
1171///
1172/// This method also handles this portion of the grammar:
1173/// parameter-type-list: [C99 6.7.5]
1174/// parameter-list
1175/// parameter-list ',' '...'
1176///
1177/// parameter-list: [C99 6.7.5]
1178/// parameter-declaration
1179/// parameter-list ',' parameter-declaration
1180///
1181/// parameter-declaration: [C99 6.7.5]
1182/// declaration-specifiers declarator
1183/// [GNU] declaration-specifiers declarator attributes
1184/// declaration-specifiers abstract-declarator[opt]
1185/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1186///
1187/// identifier-list: [C99 6.7.5]
1188/// identifier
1189/// identifier-list ',' identifier
1190///
1191void Parser::ParseParenDeclarator(Declarator &D) {
1192 SourceLocation StartLoc = ConsumeParen();
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.
1197 if (!D.isPastIdentifier()) {
1198 // Okay, this is probably a grouping paren. However, if this could be an
1199 // abstract-declarator, then this could also be the start of function
1200 // arguments (consider 'void()').
1201 bool isGrouping;
1202
1203 if (!D.mayOmitIdentifier()) {
1204 // If this can't be an abstract-declarator, this *must* be a grouping
1205 // paren, because we haven't seen the identifier yet.
1206 isGrouping = true;
Chris Lattner04d66662007-10-09 17:33:22 +00001207 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Reid Spencer5f016e22007-07-11 17:01:13 +00001208 isDeclarationSpecifier()) { // 'int(int)' is a function.
1209 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1210 // considered to be a type, not a K&R identifier-list.
1211 isGrouping = false;
1212 } else {
1213 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1214 isGrouping = true;
1215 }
1216
1217 // If this is a grouping paren, handle:
1218 // direct-declarator: '(' declarator ')'
1219 // direct-declarator: '(' attributes declarator ')'
1220 if (isGrouping) {
Chris Lattner04d66662007-10-09 17:33:22 +00001221 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001222 D.AddAttributes(ParseAttributes());
1223
1224 ParseDeclaratorInternal(D);
1225 // Match the ')'.
1226 MatchRHSPunctuation(tok::r_paren, StartLoc);
1227 return;
1228 }
1229
1230 // Okay, if this wasn't a grouping paren, it must be the start of a function
1231 // argument list. Recognize that this declarator will never have an
1232 // identifier (and remember where it would have been), then fall through to
1233 // the handling of argument lists.
1234 D.SetIdentifier(0, Tok.getLocation());
1235 }
1236
1237 // Okay, this is the parameter list of a function definition, or it is an
1238 // identifier list of a K&R-style function.
1239 bool IsVariadic;
1240 bool HasPrototype;
1241 bool ErrorEmitted = false;
1242
1243 // Build up an array of information about the parsed arguments.
1244 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1245 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1246
Chris Lattner04d66662007-10-09 17:33:22 +00001247 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 // int() -> no prototype, no '...'.
1249 IsVariadic = false;
1250 HasPrototype = false;
Chris Lattner04d66662007-10-09 17:33:22 +00001251 } else if (Tok.is(tok::identifier) &&
Reid Spencer5f016e22007-07-11 17:01:13 +00001252 // K&R identifier lists can't have typedefs as identifiers, per
1253 // C99 6.7.5.3p11.
1254 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1255 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1256 // normal declarators, not for abstract-declarators.
1257 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
1258
1259 // If there was no identifier specified, either we are in an
1260 // abstract-declarator, or we are in a parameter declarator which was found
1261 // to be abstract. In abstract-declarators, identifier lists are not valid,
1262 // diagnose this.
1263 if (!D.getIdentifier())
1264 Diag(Tok, diag::ext_ident_list_in_param);
1265
1266 // Remember this identifier in ParamInfo.
1267 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1268 Tok.getLocation(), 0));
1269
1270 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001271 while (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 // Eat the comma.
1273 ConsumeToken();
1274
Chris Lattner04d66662007-10-09 17:33:22 +00001275 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001276 Diag(Tok, diag::err_expected_ident);
1277 ErrorEmitted = true;
1278 break;
1279 }
1280
1281 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
1282
1283 // Verify that the argument identifier has not already been mentioned.
1284 if (!ParamsSoFar.insert(ParmII)) {
1285 Diag(Tok.getLocation(), diag::err_param_redefinition,ParmII->getName());
1286 ParmII = 0;
1287 }
1288
1289 // Remember this identifier in ParamInfo.
1290 if (ParmII)
1291 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1292 Tok.getLocation(), 0));
1293
1294 // Eat the identifier.
1295 ConsumeToken();
1296 }
1297
1298 // K&R 'prototype'.
1299 IsVariadic = false;
1300 HasPrototype = false;
1301 } else {
1302 // Finally, a normal, non-empty parameter type list.
1303
1304 // Enter function-declaration scope, limiting any declarators for struct
1305 // tags to the function prototype scope.
1306 // FIXME: is this needed?
Chris Lattner31e05722007-08-26 06:24:45 +00001307 EnterScope(Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001308
1309 IsVariadic = false;
1310 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00001311 if (Tok.is(tok::ellipsis)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001312 IsVariadic = true;
1313
1314 // Check to see if this is "void(...)" which is not allowed.
1315 if (ParamInfo.empty()) {
1316 // Otherwise, parse parameter type list. If it starts with an
1317 // ellipsis, diagnose the malformed function.
1318 Diag(Tok, diag::err_ellipsis_first_arg);
1319 IsVariadic = false; // Treat this like 'void()'.
1320 }
1321
1322 // Consume the ellipsis.
1323 ConsumeToken();
1324 break;
1325 }
1326
Chris Lattner99d724f2008-02-10 23:08:00 +00001327 SourceLocation DSStart = Tok.getLocation();
1328
Reid Spencer5f016e22007-07-11 17:01:13 +00001329 // Parse the declaration-specifiers.
1330 DeclSpec DS;
1331 ParseDeclarationSpecifiers(DS);
1332
1333 // Parse the declarator. This is "PrototypeContext", because we must
1334 // accept either 'declarator' or 'abstract-declarator' here.
1335 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1336 ParseDeclarator(ParmDecl);
1337
1338 // Parse GNU attributes, if present.
Chris Lattner04d66662007-10-09 17:33:22 +00001339 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001340 ParmDecl.AddAttributes(ParseAttributes());
1341
1342 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
1343 // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted.
1344 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1345 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
1346 Diag(DS.getStorageClassSpecLoc(),
1347 diag::err_invalid_storage_class_in_func_decl);
1348 DS.ClearStorageClassSpecs();
1349 }
1350 if (DS.isThreadSpecified()) {
1351 Diag(DS.getThreadSpecLoc(),
1352 diag::err_invalid_storage_class_in_func_decl);
1353 DS.ClearStorageClassSpecs();
1354 }
1355
1356 // Inform the actions module about the parameter declarator, so it gets
1357 // added to the current scope.
1358 Action::TypeResult ParamTy =
Steve Naroff08d92e42007-09-15 18:49:24 +00001359 Actions.ActOnParamDeclaratorType(CurScope, ParmDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001360
1361 // Remember this parsed parameter in ParamInfo.
1362 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1363
1364 // Verify that the argument identifier has not already been mentioned.
1365 if (ParmII && !ParamsSoFar.insert(ParmII)) {
1366 Diag(ParmDecl.getIdentifierLoc(), diag::err_param_redefinition,
1367 ParmII->getName());
1368 ParmII = 0;
1369 }
Chris Lattnere0e713b2008-01-31 06:10:07 +00001370
1371 // If no parameter was specified, verify that *something* was specified,
1372 // otherwise we have a missing type and identifier.
Chris Lattner99d724f2008-02-10 23:08:00 +00001373 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1374 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1375 Diag(DSStart, diag::err_missing_param);
1376 } else if (!DS.hasTypeSpecifier() &&
1377 (getLang().C99 || getLang().CPlusPlus)) {
1378 // Otherwise, if something was specified but a type specifier wasn't,
1379 // (e.g. "x" or "restrict x" or "restrict"), this is a use of implicit
1380 // int. This is valid in C90, but not in C99 or C++.
Chris Lattnere0e713b2008-01-31 06:10:07 +00001381 if (ParmII)
1382 Diag(ParmDecl.getIdentifierLoc(),
Chris Lattner99d724f2008-02-10 23:08:00 +00001383 diag::ext_param_requires_type_specifier, ParmII->getName());
Chris Lattnere0e713b2008-01-31 06:10:07 +00001384 else
Chris Lattner99d724f2008-02-10 23:08:00 +00001385 Diag(DSStart, diag::ext_anon_param_requires_type_specifier);
Chris Lattnere0e713b2008-01-31 06:10:07 +00001386 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001387
Steve Naroffe1223f72007-08-28 03:03:08 +00001388 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Nate Begeman1b4e2512007-11-13 22:14:47 +00001389 ParmDecl.getIdentifierLoc(), ParamTy.Val, ParmDecl.getInvalidType(),
Chris Lattner76549142008-02-21 01:32:26 +00001390 ParmDecl.getDeclSpec().TakeAttributes()));
Nate Begeman1b4e2512007-11-13 22:14:47 +00001391
Reid Spencer5f016e22007-07-11 17:01:13 +00001392 // If the next token is a comma, consume it and keep reading arguments.
Chris Lattner04d66662007-10-09 17:33:22 +00001393 if (Tok.isNot(tok::comma)) break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001394
1395 // Consume the comma.
1396 ConsumeToken();
1397 }
1398
1399 HasPrototype = true;
1400
1401 // Leave prototype scope.
1402 ExitScope();
1403 }
1404
1405 // Remember that we parsed a function type, and remember the attributes.
1406 if (!ErrorEmitted)
1407 D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic,
1408 &ParamInfo[0], ParamInfo.size(),
1409 StartLoc));
1410
1411 // If we have the closing ')', eat it and we're done.
Chris Lattner04d66662007-10-09 17:33:22 +00001412 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001413 ConsumeParen();
1414 } else {
1415 // If an error happened earlier parsing something else in the proto, don't
1416 // issue another error.
1417 if (!ErrorEmitted)
1418 Diag(Tok, diag::err_expected_rparen);
1419 SkipUntil(tok::r_paren);
1420 }
1421}
1422
1423
1424/// [C90] direct-declarator '[' constant-expression[opt] ']'
1425/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1426/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1427/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1428/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1429void Parser::ParseBracketDeclarator(Declarator &D) {
1430 SourceLocation StartLoc = ConsumeBracket();
1431
1432 // If valid, this location is the position where we read the 'static' keyword.
1433 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00001434 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00001435 StaticLoc = ConsumeToken();
1436
1437 // If there is a type-qualifier-list, read it now.
1438 DeclSpec DS;
1439 ParseTypeQualifierListOpt(DS);
1440
1441 // If we haven't already read 'static', check to see if there is one after the
1442 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001443 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00001444 StaticLoc = ConsumeToken();
1445
1446 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1447 bool isStar = false;
1448 ExprResult NumElements(false);
Chris Lattner04d66662007-10-09 17:33:22 +00001449 if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001450 // Remember the '*' token, in case we have to un-get it.
Chris Lattnerd2177732007-07-20 16:59:19 +00001451 Token StarTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001452 ConsumeToken();
1453
1454 // Check that the ']' token is present to avoid incorrectly parsing
1455 // expressions starting with '*' as [*].
Chris Lattner04d66662007-10-09 17:33:22 +00001456 if (Tok.is(tok::r_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001457 if (StaticLoc.isValid())
1458 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1459 StaticLoc = SourceLocation(); // Drop the static.
1460 isStar = true;
1461 } else {
1462 // Otherwise, the * must have been some expression (such as '*ptr') that
1463 // started an assignment-expr. We already consumed the token, but now we
1464 // need to reparse it. This handles cases like 'X[*p + 4]'
1465 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
1466 }
Chris Lattner04d66662007-10-09 17:33:22 +00001467 } else if (Tok.isNot(tok::r_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001468 // Parse the assignment-expression now.
1469 NumElements = ParseAssignmentExpression();
1470 }
1471
1472 // If there was an error parsing the assignment-expression, recover.
1473 if (NumElements.isInvalid) {
1474 // If the expression was invalid, skip it.
1475 SkipUntil(tok::r_square);
1476 return;
1477 }
1478
1479 MatchRHSPunctuation(tok::r_square, StartLoc);
1480
1481 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1482 // it was not a constant expression.
1483 if (!getLang().C99) {
1484 // TODO: check C90 array constant exprness.
1485 if (isStar || StaticLoc.isValid() ||
1486 0/*TODO: NumElts is not a C90 constantexpr */)
1487 Diag(StartLoc, diag::ext_c99_array_usage);
1488 }
1489
1490 // Remember that we parsed a pointer type, and remember the type-quals.
1491 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1492 StaticLoc.isValid(), isStar,
1493 NumElements.Val, StartLoc));
1494}
1495
Steve Naroffd1861fd2007-07-31 12:34:36 +00001496/// [GNU] typeof-specifier:
1497/// typeof ( expressions )
1498/// typeof ( type-name )
1499///
1500void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00001501 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001502 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffd1861fd2007-07-31 12:34:36 +00001503 SourceLocation StartLoc = ConsumeToken();
1504
Chris Lattner04d66662007-10-09 17:33:22 +00001505 if (Tok.isNot(tok::l_paren)) {
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001506 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1507 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001508 }
1509 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1510
1511 if (isTypeSpecifierQualifier()) {
1512 TypeTy *Ty = ParseTypeName();
1513
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001514 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1515
Chris Lattner04d66662007-10-09 17:33:22 +00001516 if (Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001517 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001518 return;
1519 }
1520 RParenLoc = ConsumeParen();
1521 const char *PrevSpec = 0;
1522 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1523 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1524 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001525 } else { // we have an expression.
1526 ExprResult Result = ParseExpression();
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001527
Chris Lattner04d66662007-10-09 17:33:22 +00001528 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001529 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001530 return;
1531 }
1532 RParenLoc = ConsumeParen();
1533 const char *PrevSpec = 0;
1534 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1535 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1536 Result.Val))
1537 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001538 }
Steve Naroffd1861fd2007-07-31 12:34:36 +00001539}
1540