blob: 5d7c94cab066de4ec874a1d7e014c9ebc8323fcd [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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 }
299
300 Diag(Tok, diag::err_parse_error);
301 // Skip to end of block or statement
Chris Lattnerf491b412007-08-21 18:36:18 +0000302 SkipUntil(tok::r_brace, true, true);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000303 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000304 ConsumeToken();
305 return 0;
306}
307
308/// ParseSpecifierQualifierList
309/// specifier-qualifier-list:
310/// type-specifier specifier-qualifier-list[opt]
311/// type-qualifier specifier-qualifier-list[opt]
312/// [GNU] attributes specifier-qualifier-list[opt]
313///
314void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
315 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
316 /// parse declaration-specifiers and complain about extra stuff.
317 ParseDeclarationSpecifiers(DS);
318
319 // Validate declspec for type-name.
320 unsigned Specs = DS.getParsedSpecifiers();
321 if (Specs == DeclSpec::PQ_None)
322 Diag(Tok, diag::err_typename_requires_specqual);
323
324 // Issue diagnostic and remove storage class if present.
325 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
326 if (DS.getStorageClassSpecLoc().isValid())
327 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
328 else
329 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
330 DS.ClearStorageClassSpecs();
331 }
332
333 // Issue diagnostic and remove function specfier if present.
334 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
335 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
336 DS.ClearFunctionSpecs();
337 }
338}
339
340/// ParseDeclarationSpecifiers
341/// declaration-specifiers: [C99 6.7]
342/// storage-class-specifier declaration-specifiers[opt]
343/// type-specifier declaration-specifiers[opt]
344/// type-qualifier declaration-specifiers[opt]
345/// [C99] function-specifier declaration-specifiers[opt]
346/// [GNU] attributes declaration-specifiers[opt]
347///
348/// storage-class-specifier: [C99 6.7.1]
349/// 'typedef'
350/// 'extern'
351/// 'static'
352/// 'auto'
353/// 'register'
354/// [GNU] '__thread'
355/// type-specifier: [C99 6.7.2]
356/// 'void'
357/// 'char'
358/// 'short'
359/// 'int'
360/// 'long'
361/// 'float'
362/// 'double'
363/// 'signed'
364/// 'unsigned'
365/// struct-or-union-specifier
366/// enum-specifier
367/// typedef-name
368/// [C++] 'bool'
369/// [C99] '_Bool'
370/// [C99] '_Complex'
371/// [C99] '_Imaginary' // Removed in TC2?
372/// [GNU] '_Decimal32'
373/// [GNU] '_Decimal64'
374/// [GNU] '_Decimal128'
Steve Naroff4c255ab2007-07-31 23:56:32 +0000375/// [GNU] typeof-specifier
Chris Lattner4b009652007-07-25 00:24:17 +0000376/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroffa8ee2262007-08-22 23:18:22 +0000377/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattner4b009652007-07-25 00:24:17 +0000378/// type-qualifier:
379/// 'const'
380/// 'volatile'
381/// [C99] 'restrict'
382/// function-specifier: [C99 6.7.4]
383/// [C99] 'inline'
384///
385void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
386 DS.Range.setBegin(Tok.getLocation());
387 while (1) {
388 int isInvalid = false;
389 const char *PrevSpec = 0;
390 SourceLocation Loc = Tok.getLocation();
391
392 switch (Tok.getKind()) {
393 // typedef-name
394 case tok::identifier:
395 // This identifier can only be a typedef name if we haven't already seen
396 // a type-specifier. Without this check we misparse:
397 // typedef int X; struct Y { short X; }; as 'short int'.
398 if (!DS.hasTypeSpecifier()) {
399 // It has to be available as a typedef too!
400 if (void *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(),
401 CurScope)) {
402 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
403 TypeRep);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000404 if (isInvalid)
405 break;
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000406 // FIXME: restrict this to "id" and ObjC classnames.
407 DS.Range.setEnd(Tok.getLocation());
408 ConsumeToken(); // The identifier
409 if (Tok.is(tok::less)) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000410 SourceLocation endProtoLoc;
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000411 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroffef20ed32007-10-30 02:23:23 +0000412 ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000413 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
414 new llvm::SmallVector<DeclTy *, 8>;
415 DS.setProtocolQualifiers(ProtocolDecl);
416 Actions.FindProtocolDeclaration(Loc,
417 &ProtocolRefs[0], ProtocolRefs.size(),
418 *ProtocolDecl);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000419 }
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000420 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000421 }
422 }
423 // FALL THROUGH.
424 default:
425 // If this is not a declaration specifier token, we're done reading decl
426 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000427 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000428 return;
429
430 // GNU attributes support.
431 case tok::kw___attribute:
432 DS.AddAttributes(ParseAttributes());
433 continue;
434
435 // storage-class-specifier
436 case tok::kw_typedef:
437 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
438 break;
439 case tok::kw_extern:
440 if (DS.isThreadSpecified())
441 Diag(Tok, diag::ext_thread_before, "extern");
442 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
443 break;
444 case tok::kw_static:
445 if (DS.isThreadSpecified())
446 Diag(Tok, diag::ext_thread_before, "static");
447 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
448 break;
449 case tok::kw_auto:
450 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
451 break;
452 case tok::kw_register:
453 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
454 break;
455 case tok::kw___thread:
456 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
457 break;
458
459 // type-specifiers
460 case tok::kw_short:
461 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
462 break;
463 case tok::kw_long:
464 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
465 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
466 else
467 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
468 break;
469 case tok::kw_signed:
470 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
471 break;
472 case tok::kw_unsigned:
473 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
474 break;
475 case tok::kw__Complex:
476 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
477 break;
478 case tok::kw__Imaginary:
479 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
480 break;
481 case tok::kw_void:
482 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
483 break;
484 case tok::kw_char:
485 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
486 break;
487 case tok::kw_int:
488 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
489 break;
490 case tok::kw_float:
491 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
492 break;
493 case tok::kw_double:
494 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
495 break;
496 case tok::kw_bool: // [C++ 2.11p1]
497 case tok::kw__Bool:
498 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
499 break;
500 case tok::kw__Decimal32:
501 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
502 break;
503 case tok::kw__Decimal64:
504 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
505 break;
506 case tok::kw__Decimal128:
507 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
508 break;
509
510 case tok::kw_struct:
511 case tok::kw_union:
512 ParseStructUnionSpecifier(DS);
513 continue;
514 case tok::kw_enum:
515 ParseEnumSpecifier(DS);
516 continue;
517
Steve Naroff7cbb1462007-07-31 12:34:36 +0000518 // GNU typeof support.
519 case tok::kw_typeof:
520 ParseTypeofSpecifier(DS);
521 continue;
522
Chris Lattner4b009652007-07-25 00:24:17 +0000523 // type-qualifier
524 case tok::kw_const:
525 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
526 getLang())*2;
527 break;
528 case tok::kw_volatile:
529 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
530 getLang())*2;
531 break;
532 case tok::kw_restrict:
533 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
534 getLang())*2;
535 break;
536
537 // function-specifier
538 case tok::kw_inline:
539 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
540 break;
541 }
542 // If the specifier combination wasn't legal, issue a diagnostic.
543 if (isInvalid) {
544 assert(PrevSpec && "Method did not return previous specifier!");
545 if (isInvalid == 1) // Error.
546 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
547 else // extwarn.
548 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
549 }
550 DS.Range.setEnd(Tok.getLocation());
551 ConsumeToken();
552 }
553}
554
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000555/// ParseObjcTypeQualifierList - This routine parses the objective-c's type
556/// qualifier list and builds their bitmask representation in the input
557/// argument.
558void Parser::ParseObjcTypeQualifierList(ObjcDeclSpec &DS) {
Chris Lattner0d7804f2007-12-12 06:13:27 +0000559 while (1) {
560 if (!Tok.is(tok::identifier))
561 return;
562
563 const IdentifierInfo *II = Tok.getIdentifierInfo();
564 for (unsigned i = 0; i != objc_NumQuals; ++i) {
565 if (II != ObjcTypeQuals[i])
566 continue;
567
568 ObjcDeclSpec::ObjcDeclQualifier Qual;
569 switch (i) {
570 default: assert(0 && "Unknown decl qualifier");
571 case objc_in: Qual = ObjcDeclSpec::DQ_In; break;
572 case objc_out: Qual = ObjcDeclSpec::DQ_Out; break;
573 case objc_inout: Qual = ObjcDeclSpec::DQ_Inout; break;
574 case objc_oneway: Qual = ObjcDeclSpec::DQ_Oneway; break;
575 case objc_bycopy: Qual = ObjcDeclSpec::DQ_Bycopy; break;
576 case objc_byref: Qual = ObjcDeclSpec::DQ_Byref; break;
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000577 }
Chris Lattner0d7804f2007-12-12 06:13:27 +0000578 DS.setObjcDeclQualifier(Qual);
579 ConsumeToken();
580 II = 0;
581 break;
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000582 }
Chris Lattner0d7804f2007-12-12 06:13:27 +0000583
584 // If this wasn't a recognized qualifier, bail out.
585 if (II) return;
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000586 }
587}
588
Chris Lattner4b009652007-07-25 00:24:17 +0000589/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
590/// the first token has already been read and has been turned into an instance
591/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
592/// otherwise it returns false and fills in Decl.
593bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
594 AttributeList *Attr = 0;
595 // If attributes exist after tag, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000596 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000597 Attr = ParseAttributes();
598
599 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000600 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000601 Diag(Tok, diag::err_expected_ident_lbrace);
602
603 // Skip the rest of this declarator, up until the comma or semicolon.
604 SkipUntil(tok::comma, true);
605 return true;
606 }
607
608 // If an identifier is present, consume and remember it.
609 IdentifierInfo *Name = 0;
610 SourceLocation NameLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000611 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000612 Name = Tok.getIdentifierInfo();
613 NameLoc = ConsumeToken();
614 }
615
616 // There are three options here. If we have 'struct foo;', then this is a
617 // forward declaration. If we have 'struct foo {...' then this is a
618 // definition. Otherwise we have something like 'struct foo xyz', a reference.
619 //
620 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
621 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
622 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
623 //
624 Action::TagKind TK;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000625 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000626 TK = Action::TK_Definition;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000627 else if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000628 TK = Action::TK_Declaration;
629 else
630 TK = Action::TK_Reference;
Steve Naroff0acc9c92007-09-15 18:49:24 +0000631 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Chris Lattner4b009652007-07-25 00:24:17 +0000632 return false;
633}
634
635
636/// ParseStructUnionSpecifier
637/// struct-or-union-specifier: [C99 6.7.2.1]
638/// struct-or-union identifier[opt] '{' struct-contents '}'
639/// struct-or-union identifier
640/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
641/// '}' attributes[opt]
642/// [GNU] struct-or-union attributes[opt] identifier
643/// struct-or-union:
644/// 'struct'
645/// 'union'
646///
647void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000648 assert((Tok.is(tok::kw_struct) || Tok.is(tok::kw_union)) &&
649 "Not a struct/union specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000650 DeclSpec::TST TagType =
Chris Lattner34a01ad2007-10-09 17:33:22 +0000651 Tok.is(tok::kw_union) ? DeclSpec::TST_union : DeclSpec::TST_struct;
Chris Lattner4b009652007-07-25 00:24:17 +0000652 SourceLocation StartLoc = ConsumeToken();
653
654 // Parse the tag portion of this.
655 DeclTy *TagDecl;
656 if (ParseTag(TagDecl, TagType, StartLoc))
657 return;
658
659 // If there is a body, parse it and inform the actions module.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000660 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000661 ParseStructUnionBody(StartLoc, TagType, TagDecl);
662
663 const char *PrevSpec = 0;
664 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
665 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
666}
667
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000668/// ParseStructDeclaration - Parse a struct declaration without the terminating
669/// semicolon.
670///
Chris Lattner4b009652007-07-25 00:24:17 +0000671/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000672/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000673/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000674/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000675/// struct-declarator-list:
676/// struct-declarator
677/// struct-declarator-list ',' struct-declarator
678/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
679/// struct-declarator:
680/// declarator
681/// [GNU] declarator attributes[opt]
682/// declarator[opt] ':' constant-expression
683/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
684///
Steve Naroffa9adf112007-08-20 22:28:22 +0000685void Parser::ParseStructDeclaration(DeclTy *TagDecl,
Steve Naroffc02f4a92007-08-28 16:31:47 +0000686 llvm::SmallVectorImpl<DeclTy*> &FieldDecls) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000687 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000688 if (Tok.is(tok::kw___extension__))
Steve Naroffa9adf112007-08-20 22:28:22 +0000689 ConsumeToken();
690
691 // Parse the common specifier-qualifiers-list piece.
692 DeclSpec DS;
693 SourceLocation SpecQualLoc = Tok.getLocation();
694 ParseSpecifierQualifierList(DS);
695 // TODO: Does specifier-qualifier list correctly check that *something* is
696 // specified?
697
698 // If there are no declarators, issue a warning.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000699 if (Tok.is(tok::semi)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000700 Diag(SpecQualLoc, diag::w_no_declarators);
Steve Naroffa9adf112007-08-20 22:28:22 +0000701 return;
702 }
703
704 // Read struct-declarators until we find the semicolon.
705 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
706
707 while (1) {
708 /// struct-declarator: declarator
709 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000710 if (Tok.isNot(tok::colon))
Steve Naroffa9adf112007-08-20 22:28:22 +0000711 ParseDeclarator(DeclaratorInfo);
712
713 ExprTy *BitfieldSize = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000714 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000715 ConsumeToken();
716 ExprResult Res = ParseConstantExpression();
717 if (Res.isInvalid) {
718 SkipUntil(tok::semi, true, true);
719 } else {
720 BitfieldSize = Res.Val;
721 }
722 }
723
724 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000725 if (Tok.is(tok::kw___attribute))
Steve Naroffa9adf112007-08-20 22:28:22 +0000726 DeclaratorInfo.AddAttributes(ParseAttributes());
727
728 // Install the declarator into the current TagDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000729 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl, SpecQualLoc,
Steve Naroffa9adf112007-08-20 22:28:22 +0000730 DeclaratorInfo, BitfieldSize);
731 FieldDecls.push_back(Field);
732
733 // If we don't have a comma, it is either the end of the list (a ';')
734 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000735 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000736 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000737
738 // Consume the comma.
739 ConsumeToken();
740
741 // Parse the next declarator.
742 DeclaratorInfo.clear();
743
744 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000745 if (Tok.is(tok::kw___attribute))
Steve Naroffa9adf112007-08-20 22:28:22 +0000746 DeclaratorInfo.AddAttributes(ParseAttributes());
747 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000748}
749
750/// ParseStructUnionBody
751/// struct-contents:
752/// struct-declaration-list
753/// [EXT] empty
754/// [GNU] "struct-declaration-list" without terminatoring ';'
755/// struct-declaration-list:
756/// struct-declaration
757/// struct-declaration-list struct-declaration
758/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
759///
Chris Lattner4b009652007-07-25 00:24:17 +0000760void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
761 unsigned TagType, DeclTy *TagDecl) {
762 SourceLocation LBraceLoc = ConsumeBrace();
763
764 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
765 // C++.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000766 if (Tok.is(tok::r_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000767 Diag(Tok, diag::ext_empty_struct_union_enum,
768 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
769
770 llvm::SmallVector<DeclTy*, 32> FieldDecls;
771
772 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000773 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000774 // Each iteration of this loop reads one struct-declaration.
775
776 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000777 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000778 Diag(Tok, diag::ext_extra_struct_semi);
779 ConsumeToken();
780 continue;
781 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000782 ParseStructDeclaration(TagDecl, FieldDecls);
Chris Lattner4b009652007-07-25 00:24:17 +0000783
Chris Lattner34a01ad2007-10-09 17:33:22 +0000784 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000785 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +0000786 } else if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000787 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
788 break;
789 } else {
790 Diag(Tok, diag::err_expected_semi_decl_list);
791 // Skip to end of block or statement
792 SkipUntil(tok::r_brace, true, true);
793 }
794 }
795
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000796 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000797
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000798 Actions.ActOnFields(CurScope,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000799 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
800 LBraceLoc, RBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000801
802 AttributeList *AttrList = 0;
803 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000804 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000805 AttrList = ParseAttributes(); // FIXME: where should I put them?
806}
807
808
809/// ParseEnumSpecifier
810/// enum-specifier: [C99 6.7.2.2]
811/// 'enum' identifier[opt] '{' enumerator-list '}'
812/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
813/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
814/// '}' attributes[opt]
815/// 'enum' identifier
816/// [GNU] 'enum' attributes[opt] identifier
817void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000818 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000819 SourceLocation StartLoc = ConsumeToken();
820
821 // Parse the tag portion of this.
822 DeclTy *TagDecl;
823 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
824 return;
825
Chris Lattner34a01ad2007-10-09 17:33:22 +0000826 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000827 ParseEnumBody(StartLoc, TagDecl);
828
829 // TODO: semantic analysis on the declspec for enums.
830 const char *PrevSpec = 0;
831 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
832 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
833}
834
835/// ParseEnumBody - Parse a {} enclosed enumerator-list.
836/// enumerator-list:
837/// enumerator
838/// enumerator-list ',' enumerator
839/// enumerator:
840/// enumeration-constant
841/// enumeration-constant '=' constant-expression
842/// enumeration-constant:
843/// identifier
844///
845void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
846 SourceLocation LBraceLoc = ConsumeBrace();
847
Chris Lattnerc9a92452007-08-27 17:24:30 +0000848 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +0000849 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000850 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
851
852 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
853
854 DeclTy *LastEnumConstDecl = 0;
855
856 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000857 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000858 IdentifierInfo *Ident = Tok.getIdentifierInfo();
859 SourceLocation IdentLoc = ConsumeToken();
860
861 SourceLocation EqualLoc;
862 ExprTy *AssignedVal = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000863 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000864 EqualLoc = ConsumeToken();
865 ExprResult Res = ParseConstantExpression();
866 if (Res.isInvalid)
867 SkipUntil(tok::comma, tok::r_brace, true, true);
868 else
869 AssignedVal = Res.Val;
870 }
871
872 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000873 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +0000874 LastEnumConstDecl,
875 IdentLoc, Ident,
876 EqualLoc, AssignedVal);
877 EnumConstantDecls.push_back(EnumConstDecl);
878 LastEnumConstDecl = EnumConstDecl;
879
Chris Lattner34a01ad2007-10-09 17:33:22 +0000880 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000881 break;
882 SourceLocation CommaLoc = ConsumeToken();
883
Chris Lattner34a01ad2007-10-09 17:33:22 +0000884 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +0000885 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
886 }
887
888 // Eat the }.
889 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
890
Steve Naroff0acc9c92007-09-15 18:49:24 +0000891 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +0000892 EnumConstantDecls.size());
893
894 DeclTy *AttrList = 0;
895 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000896 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000897 AttrList = ParseAttributes(); // FIXME: where do they do?
898}
899
900/// isTypeSpecifierQualifier - Return true if the current token could be the
901/// start of a specifier-qualifier-list.
902bool Parser::isTypeSpecifierQualifier() const {
903 switch (Tok.getKind()) {
904 default: return false;
905 // GNU attributes support.
906 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000907 // GNU typeof support.
908 case tok::kw_typeof:
909
Chris Lattner4b009652007-07-25 00:24:17 +0000910 // type-specifiers
911 case tok::kw_short:
912 case tok::kw_long:
913 case tok::kw_signed:
914 case tok::kw_unsigned:
915 case tok::kw__Complex:
916 case tok::kw__Imaginary:
917 case tok::kw_void:
918 case tok::kw_char:
919 case tok::kw_int:
920 case tok::kw_float:
921 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000922 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +0000923 case tok::kw__Bool:
924 case tok::kw__Decimal32:
925 case tok::kw__Decimal64:
926 case tok::kw__Decimal128:
927
928 // struct-or-union-specifier
929 case tok::kw_struct:
930 case tok::kw_union:
931 // enum-specifier
932 case tok::kw_enum:
933
934 // type-qualifier
935 case tok::kw_const:
936 case tok::kw_volatile:
937 case tok::kw_restrict:
938 return true;
939
940 // typedef-name
941 case tok::identifier:
942 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000943 }
944}
945
946/// isDeclarationSpecifier() - Return true if the current token is part of a
947/// declaration specifier.
948bool Parser::isDeclarationSpecifier() const {
949 switch (Tok.getKind()) {
950 default: return false;
951 // storage-class-specifier
952 case tok::kw_typedef:
953 case tok::kw_extern:
954 case tok::kw_static:
955 case tok::kw_auto:
956 case tok::kw_register:
957 case tok::kw___thread:
958
959 // type-specifiers
960 case tok::kw_short:
961 case tok::kw_long:
962 case tok::kw_signed:
963 case tok::kw_unsigned:
964 case tok::kw__Complex:
965 case tok::kw__Imaginary:
966 case tok::kw_void:
967 case tok::kw_char:
968 case tok::kw_int:
969 case tok::kw_float:
970 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000971 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +0000972 case tok::kw__Bool:
973 case tok::kw__Decimal32:
974 case tok::kw__Decimal64:
975 case tok::kw__Decimal128:
976
977 // struct-or-union-specifier
978 case tok::kw_struct:
979 case tok::kw_union:
980 // enum-specifier
981 case tok::kw_enum:
982
983 // type-qualifier
984 case tok::kw_const:
985 case tok::kw_volatile:
986 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000987
Chris Lattner4b009652007-07-25 00:24:17 +0000988 // function-specifier
989 case tok::kw_inline:
Chris Lattnere35d2582007-08-09 16:40:21 +0000990
Chris Lattnerb707a7a2007-08-09 17:01:07 +0000991 // GNU typeof support.
992 case tok::kw_typeof:
993
994 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +0000995 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +0000996 return true;
997
998 // typedef-name
999 case tok::identifier:
1000 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001001 }
1002}
1003
1004
1005/// ParseTypeQualifierListOpt
1006/// type-qualifier-list: [C99 6.7.5]
1007/// type-qualifier
1008/// [GNU] attributes
1009/// type-qualifier-list type-qualifier
1010/// [GNU] type-qualifier-list attributes
1011///
1012void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1013 while (1) {
1014 int isInvalid = false;
1015 const char *PrevSpec = 0;
1016 SourceLocation Loc = Tok.getLocation();
1017
1018 switch (Tok.getKind()) {
1019 default:
1020 // If this is not a type-qualifier token, we're done reading type
1021 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +00001022 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +00001023 return;
1024 case tok::kw_const:
1025 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1026 getLang())*2;
1027 break;
1028 case tok::kw_volatile:
1029 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1030 getLang())*2;
1031 break;
1032 case tok::kw_restrict:
1033 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1034 getLang())*2;
1035 break;
1036 case tok::kw___attribute:
1037 DS.AddAttributes(ParseAttributes());
1038 continue; // do *not* consume the next token!
1039 }
1040
1041 // If the specifier combination wasn't legal, issue a diagnostic.
1042 if (isInvalid) {
1043 assert(PrevSpec && "Method did not return previous specifier!");
1044 if (isInvalid == 1) // Error.
1045 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1046 else // extwarn.
1047 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1048 }
1049 ConsumeToken();
1050 }
1051}
1052
1053
1054/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1055///
1056void Parser::ParseDeclarator(Declarator &D) {
1057 /// This implements the 'declarator' production in the C grammar, then checks
1058 /// for well-formedness and issues diagnostics.
1059 ParseDeclaratorInternal(D);
1060
1061 // TODO: validate D.
1062
1063}
1064
1065/// ParseDeclaratorInternal
1066/// declarator: [C99 6.7.5]
1067/// pointer[opt] direct-declarator
1068/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1069/// [GNU] '&' restrict[opt] attributes[opt] declarator
1070///
1071/// pointer: [C99 6.7.5]
1072/// '*' type-qualifier-list[opt]
1073/// '*' type-qualifier-list[opt] pointer
1074///
1075void Parser::ParseDeclaratorInternal(Declarator &D) {
1076 tok::TokenKind Kind = Tok.getKind();
1077
1078 // Not a pointer or C++ reference.
1079 if (Kind != tok::star && !(Kind == tok::amp && getLang().CPlusPlus))
1080 return ParseDirectDeclarator(D);
1081
1082 // Otherwise, '*' -> pointer or '&' -> reference.
1083 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1084
1085 if (Kind == tok::star) {
1086 // Is a pointer
1087 DeclSpec DS;
1088
1089 ParseTypeQualifierListOpt(DS);
1090
1091 // Recursively parse the declarator.
1092 ParseDeclaratorInternal(D);
1093
1094 // Remember that we parsed a pointer type, and remember the type-quals.
1095 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc));
1096 } else {
1097 // Is a reference
1098 DeclSpec DS;
1099
1100 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1101 // cv-qualifiers are introduced through the use of a typedef or of a
1102 // template type argument, in which case the cv-qualifiers are ignored.
1103 //
1104 // [GNU] Retricted references are allowed.
1105 // [GNU] Attributes on references are allowed.
1106 ParseTypeQualifierListOpt(DS);
1107
1108 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1109 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1110 Diag(DS.getConstSpecLoc(),
1111 diag::err_invalid_reference_qualifier_application,
1112 "const");
1113 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1114 Diag(DS.getVolatileSpecLoc(),
1115 diag::err_invalid_reference_qualifier_application,
1116 "volatile");
1117 }
1118
1119 // Recursively parse the declarator.
1120 ParseDeclaratorInternal(D);
1121
1122 // Remember that we parsed a reference type. It doesn't have type-quals.
1123 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc));
1124 }
1125}
1126
1127/// ParseDirectDeclarator
1128/// direct-declarator: [C99 6.7.5]
1129/// identifier
1130/// '(' declarator ')'
1131/// [GNU] '(' attributes declarator ')'
1132/// [C90] direct-declarator '[' constant-expression[opt] ']'
1133/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1134/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1135/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1136/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1137/// direct-declarator '(' parameter-type-list ')'
1138/// direct-declarator '(' identifier-list[opt] ')'
1139/// [GNU] direct-declarator '(' parameter-forward-declarations
1140/// parameter-type-list[opt] ')'
1141///
1142void Parser::ParseDirectDeclarator(Declarator &D) {
1143 // Parse the first direct-declarator seen.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001144 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001145 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1146 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1147 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001148 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001149 // direct-declarator: '(' declarator ')'
1150 // direct-declarator: '(' attributes declarator ')'
1151 // Example: 'char (*X)' or 'int (*XX)(void)'
1152 ParseParenDeclarator(D);
1153 } else if (D.mayOmitIdentifier()) {
1154 // This could be something simple like "int" (in which case the declarator
1155 // portion is empty), if an abstract-declarator is allowed.
1156 D.SetIdentifier(0, Tok.getLocation());
1157 } else {
1158 // Expected identifier or '('.
1159 Diag(Tok, diag::err_expected_ident_lparen);
1160 D.SetIdentifier(0, Tok.getLocation());
1161 }
1162
1163 assert(D.isPastIdentifier() &&
1164 "Haven't past the location of the identifier yet?");
1165
1166 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001167 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001168 ParseParenDeclarator(D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001169 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001170 ParseBracketDeclarator(D);
1171 } else {
1172 break;
1173 }
1174 }
1175}
1176
1177/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
1178/// either be before the identifier (in which case these are just grouping
1179/// parens for precedence) or it may be after the identifier, in which case
1180/// these are function arguments.
1181///
1182/// This method also handles this portion of the grammar:
1183/// parameter-type-list: [C99 6.7.5]
1184/// parameter-list
1185/// parameter-list ',' '...'
1186///
1187/// parameter-list: [C99 6.7.5]
1188/// parameter-declaration
1189/// parameter-list ',' parameter-declaration
1190///
1191/// parameter-declaration: [C99 6.7.5]
1192/// declaration-specifiers declarator
1193/// [GNU] declaration-specifiers declarator attributes
1194/// declaration-specifiers abstract-declarator[opt]
1195/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1196///
1197/// identifier-list: [C99 6.7.5]
1198/// identifier
1199/// identifier-list ',' identifier
1200///
1201void Parser::ParseParenDeclarator(Declarator &D) {
1202 SourceLocation StartLoc = ConsumeParen();
1203
1204 // If we haven't past the identifier yet (or where the identifier would be
1205 // stored, if this is an abstract declarator), then this is probably just
1206 // grouping parens.
1207 if (!D.isPastIdentifier()) {
1208 // Okay, this is probably a grouping paren. However, if this could be an
1209 // abstract-declarator, then this could also be the start of function
1210 // arguments (consider 'void()').
1211 bool isGrouping;
1212
1213 if (!D.mayOmitIdentifier()) {
1214 // If this can't be an abstract-declarator, this *must* be a grouping
1215 // paren, because we haven't seen the identifier yet.
1216 isGrouping = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001217 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Chris Lattner4b009652007-07-25 00:24:17 +00001218 isDeclarationSpecifier()) { // 'int(int)' is a function.
1219 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1220 // considered to be a type, not a K&R identifier-list.
1221 isGrouping = false;
1222 } else {
1223 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1224 isGrouping = true;
1225 }
1226
1227 // If this is a grouping paren, handle:
1228 // direct-declarator: '(' declarator ')'
1229 // direct-declarator: '(' attributes declarator ')'
1230 if (isGrouping) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001231 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001232 D.AddAttributes(ParseAttributes());
1233
1234 ParseDeclaratorInternal(D);
1235 // Match the ')'.
1236 MatchRHSPunctuation(tok::r_paren, StartLoc);
1237 return;
1238 }
1239
1240 // Okay, if this wasn't a grouping paren, it must be the start of a function
1241 // argument list. Recognize that this declarator will never have an
1242 // identifier (and remember where it would have been), then fall through to
1243 // the handling of argument lists.
1244 D.SetIdentifier(0, Tok.getLocation());
1245 }
1246
1247 // Okay, this is the parameter list of a function definition, or it is an
1248 // identifier list of a K&R-style function.
1249 bool IsVariadic;
1250 bool HasPrototype;
1251 bool ErrorEmitted = false;
1252
1253 // Build up an array of information about the parsed arguments.
1254 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1255 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1256
Chris Lattner34a01ad2007-10-09 17:33:22 +00001257 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001258 // int() -> no prototype, no '...'.
1259 IsVariadic = false;
1260 HasPrototype = false;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001261 } else if (Tok.is(tok::identifier) &&
Chris Lattner4b009652007-07-25 00:24:17 +00001262 // K&R identifier lists can't have typedefs as identifiers, per
1263 // C99 6.7.5.3p11.
1264 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1265 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1266 // normal declarators, not for abstract-declarators.
1267 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
1268
1269 // If there was no identifier specified, either we are in an
1270 // abstract-declarator, or we are in a parameter declarator which was found
1271 // to be abstract. In abstract-declarators, identifier lists are not valid,
1272 // diagnose this.
1273 if (!D.getIdentifier())
1274 Diag(Tok, diag::ext_ident_list_in_param);
1275
1276 // Remember this identifier in ParamInfo.
1277 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1278 Tok.getLocation(), 0));
1279
1280 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001281 while (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001282 // Eat the comma.
1283 ConsumeToken();
1284
Chris Lattner34a01ad2007-10-09 17:33:22 +00001285 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001286 Diag(Tok, diag::err_expected_ident);
1287 ErrorEmitted = true;
1288 break;
1289 }
1290
1291 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
1292
1293 // Verify that the argument identifier has not already been mentioned.
1294 if (!ParamsSoFar.insert(ParmII)) {
1295 Diag(Tok.getLocation(), diag::err_param_redefinition,ParmII->getName());
1296 ParmII = 0;
1297 }
1298
1299 // Remember this identifier in ParamInfo.
1300 if (ParmII)
1301 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1302 Tok.getLocation(), 0));
1303
1304 // Eat the identifier.
1305 ConsumeToken();
1306 }
1307
1308 // K&R 'prototype'.
1309 IsVariadic = false;
1310 HasPrototype = false;
1311 } else {
1312 // Finally, a normal, non-empty parameter type list.
1313
1314 // Enter function-declaration scope, limiting any declarators for struct
1315 // tags to the function prototype scope.
1316 // FIXME: is this needed?
Chris Lattnera7549902007-08-26 06:24:45 +00001317 EnterScope(Scope::DeclScope);
Chris Lattner4b009652007-07-25 00:24:17 +00001318
1319 IsVariadic = false;
1320 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001321 if (Tok.is(tok::ellipsis)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001322 IsVariadic = true;
1323
1324 // Check to see if this is "void(...)" which is not allowed.
1325 if (ParamInfo.empty()) {
1326 // Otherwise, parse parameter type list. If it starts with an
1327 // ellipsis, diagnose the malformed function.
1328 Diag(Tok, diag::err_ellipsis_first_arg);
1329 IsVariadic = false; // Treat this like 'void()'.
1330 }
1331
1332 // Consume the ellipsis.
1333 ConsumeToken();
1334 break;
1335 }
1336
1337 // Parse the declaration-specifiers.
1338 DeclSpec DS;
1339 ParseDeclarationSpecifiers(DS);
1340
1341 // Parse the declarator. This is "PrototypeContext", because we must
1342 // accept either 'declarator' or 'abstract-declarator' here.
1343 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1344 ParseDeclarator(ParmDecl);
1345
1346 // Parse GNU attributes, if present.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001347 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001348 ParmDecl.AddAttributes(ParseAttributes());
1349
1350 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
1351 // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted.
1352 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1353 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
1354 Diag(DS.getStorageClassSpecLoc(),
1355 diag::err_invalid_storage_class_in_func_decl);
1356 DS.ClearStorageClassSpecs();
1357 }
1358 if (DS.isThreadSpecified()) {
1359 Diag(DS.getThreadSpecLoc(),
1360 diag::err_invalid_storage_class_in_func_decl);
1361 DS.ClearStorageClassSpecs();
1362 }
1363
1364 // Inform the actions module about the parameter declarator, so it gets
1365 // added to the current scope.
1366 Action::TypeResult ParamTy =
Steve Naroff0acc9c92007-09-15 18:49:24 +00001367 Actions.ActOnParamDeclaratorType(CurScope, ParmDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001368
1369 // Remember this parsed parameter in ParamInfo.
1370 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1371
1372 // Verify that the argument identifier has not already been mentioned.
1373 if (ParmII && !ParamsSoFar.insert(ParmII)) {
1374 Diag(ParmDecl.getIdentifierLoc(), diag::err_param_redefinition,
1375 ParmII->getName());
1376 ParmII = 0;
1377 }
1378
Steve Naroff91b03f72007-08-28 03:03:08 +00001379 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Nate Begeman84079d72007-11-13 22:14:47 +00001380 ParmDecl.getIdentifierLoc(), ParamTy.Val, ParmDecl.getInvalidType(),
1381 ParmDecl.getDeclSpec().getAttributes()));
1382
1383 // Ownership of DeclSpec has been handed off to ParamInfo.
1384 DS.clearAttributes();
Chris Lattner4b009652007-07-25 00:24:17 +00001385
1386 // If the next token is a comma, consume it and keep reading arguments.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001387 if (Tok.isNot(tok::comma)) break;
Chris Lattner4b009652007-07-25 00:24:17 +00001388
1389 // Consume the comma.
1390 ConsumeToken();
1391 }
1392
1393 HasPrototype = true;
1394
1395 // Leave prototype scope.
1396 ExitScope();
1397 }
1398
1399 // Remember that we parsed a function type, and remember the attributes.
1400 if (!ErrorEmitted)
1401 D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic,
1402 &ParamInfo[0], ParamInfo.size(),
1403 StartLoc));
1404
1405 // If we have the closing ')', eat it and we're done.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001406 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001407 ConsumeParen();
1408 } else {
1409 // If an error happened earlier parsing something else in the proto, don't
1410 // issue another error.
1411 if (!ErrorEmitted)
1412 Diag(Tok, diag::err_expected_rparen);
1413 SkipUntil(tok::r_paren);
1414 }
1415}
1416
1417
1418/// [C90] direct-declarator '[' constant-expression[opt] ']'
1419/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1420/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1421/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1422/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1423void Parser::ParseBracketDeclarator(Declarator &D) {
1424 SourceLocation StartLoc = ConsumeBracket();
1425
1426 // If valid, this location is the position where we read the 'static' keyword.
1427 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001428 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001429 StaticLoc = ConsumeToken();
1430
1431 // If there is a type-qualifier-list, read it now.
1432 DeclSpec DS;
1433 ParseTypeQualifierListOpt(DS);
1434
1435 // If we haven't already read 'static', check to see if there is one after the
1436 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001437 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001438 StaticLoc = ConsumeToken();
1439
1440 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1441 bool isStar = false;
1442 ExprResult NumElements(false);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001443 if (Tok.is(tok::star)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001444 // Remember the '*' token, in case we have to un-get it.
1445 Token StarTok = Tok;
1446 ConsumeToken();
1447
1448 // Check that the ']' token is present to avoid incorrectly parsing
1449 // expressions starting with '*' as [*].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001450 if (Tok.is(tok::r_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001451 if (StaticLoc.isValid())
1452 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1453 StaticLoc = SourceLocation(); // Drop the static.
1454 isStar = true;
1455 } else {
1456 // Otherwise, the * must have been some expression (such as '*ptr') that
1457 // started an assignment-expr. We already consumed the token, but now we
1458 // need to reparse it. This handles cases like 'X[*p + 4]'
1459 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
1460 }
Chris Lattner34a01ad2007-10-09 17:33:22 +00001461 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001462 // Parse the assignment-expression now.
1463 NumElements = ParseAssignmentExpression();
1464 }
1465
1466 // If there was an error parsing the assignment-expression, recover.
1467 if (NumElements.isInvalid) {
1468 // If the expression was invalid, skip it.
1469 SkipUntil(tok::r_square);
1470 return;
1471 }
1472
1473 MatchRHSPunctuation(tok::r_square, StartLoc);
1474
1475 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1476 // it was not a constant expression.
1477 if (!getLang().C99) {
1478 // TODO: check C90 array constant exprness.
1479 if (isStar || StaticLoc.isValid() ||
1480 0/*TODO: NumElts is not a C90 constantexpr */)
1481 Diag(StartLoc, diag::ext_c99_array_usage);
1482 }
1483
1484 // Remember that we parsed a pointer type, and remember the type-quals.
1485 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1486 StaticLoc.isValid(), isStar,
1487 NumElements.Val, StartLoc));
1488}
1489
Steve Naroff7cbb1462007-07-31 12:34:36 +00001490/// [GNU] typeof-specifier:
1491/// typeof ( expressions )
1492/// typeof ( type-name )
1493///
1494void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001495 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00001496 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00001497 SourceLocation StartLoc = ConsumeToken();
1498
Chris Lattner34a01ad2007-10-09 17:33:22 +00001499 if (Tok.isNot(tok::l_paren)) {
Steve Naroff14bbce82007-08-02 02:53:48 +00001500 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1501 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00001502 }
1503 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1504
1505 if (isTypeSpecifierQualifier()) {
1506 TypeTy *Ty = ParseTypeName();
1507
Steve Naroff4c255ab2007-07-31 23:56:32 +00001508 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1509
Chris Lattner34a01ad2007-10-09 17:33:22 +00001510 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001511 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001512 return;
1513 }
1514 RParenLoc = ConsumeParen();
1515 const char *PrevSpec = 0;
1516 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1517 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1518 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001519 } else { // we have an expression.
1520 ExprResult Result = ParseExpression();
Steve Naroff4c255ab2007-07-31 23:56:32 +00001521
Chris Lattner34a01ad2007-10-09 17:33:22 +00001522 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001523 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001524 return;
1525 }
1526 RParenLoc = ConsumeParen();
1527 const char *PrevSpec = 0;
1528 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1529 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1530 Result.Val))
1531 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001532 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00001533}
1534