blob: dccc00e671e1df60767db1b284c049efd3794cda [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 Jahanian335a2d42008-01-04 23:04:08 +0000299 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000300 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
301 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 Diag(Tok, diag::err_parse_error);
303 // Skip to end of block or statement
Chris Lattnered442382007-08-21 18:36:18 +0000304 SkipUntil(tok::r_brace, true, true);
Chris Lattner04d66662007-10-09 17:33:22 +0000305 if (Tok.is(tok::semi))
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 ConsumeToken();
307 return 0;
308}
309
310/// ParseSpecifierQualifierList
311/// specifier-qualifier-list:
312/// type-specifier specifier-qualifier-list[opt]
313/// type-qualifier specifier-qualifier-list[opt]
314/// [GNU] attributes specifier-qualifier-list[opt]
315///
316void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
317 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
318 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 ParseDeclarationSpecifiers(DS);
320
321 // Validate declspec for type-name.
322 unsigned Specs = DS.getParsedSpecifiers();
323 if (Specs == DeclSpec::PQ_None)
324 Diag(Tok, diag::err_typename_requires_specqual);
325
326 // Issue diagnostic and remove storage class if present.
327 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
328 if (DS.getStorageClassSpecLoc().isValid())
329 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
330 else
331 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
332 DS.ClearStorageClassSpecs();
333 }
334
335 // Issue diagnostic and remove function specfier if present.
336 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
337 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
338 DS.ClearFunctionSpecs();
339 }
340}
341
342/// ParseDeclarationSpecifiers
343/// declaration-specifiers: [C99 6.7]
344/// storage-class-specifier declaration-specifiers[opt]
345/// type-specifier declaration-specifiers[opt]
346/// type-qualifier declaration-specifiers[opt]
347/// [C99] function-specifier declaration-specifiers[opt]
348/// [GNU] attributes declaration-specifiers[opt]
349///
350/// storage-class-specifier: [C99 6.7.1]
351/// 'typedef'
352/// 'extern'
353/// 'static'
354/// 'auto'
355/// 'register'
356/// [GNU] '__thread'
357/// type-specifier: [C99 6.7.2]
358/// 'void'
359/// 'char'
360/// 'short'
361/// 'int'
362/// 'long'
363/// 'float'
364/// 'double'
365/// 'signed'
366/// 'unsigned'
367/// struct-or-union-specifier
368/// enum-specifier
369/// typedef-name
370/// [C++] 'bool'
371/// [C99] '_Bool'
372/// [C99] '_Complex'
373/// [C99] '_Imaginary' // Removed in TC2?
374/// [GNU] '_Decimal32'
375/// [GNU] '_Decimal64'
376/// [GNU] '_Decimal128'
Steve Naroff2cb64ec2007-07-31 23:56:32 +0000377/// [GNU] typeof-specifier
Reid Spencer5f016e22007-07-11 17:01:13 +0000378/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000379/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Reid Spencer5f016e22007-07-11 17:01:13 +0000380/// type-qualifier:
381/// 'const'
382/// 'volatile'
383/// [C99] 'restrict'
384/// function-specifier: [C99 6.7.4]
385/// [C99] 'inline'
386///
387void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattnere80a59c2007-07-25 00:24:17 +0000388 DS.Range.setBegin(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000389 while (1) {
390 int isInvalid = false;
391 const char *PrevSpec = 0;
392 SourceLocation Loc = Tok.getLocation();
393
394 switch (Tok.getKind()) {
395 // typedef-name
396 case tok::identifier:
397 // This identifier can only be a typedef name if we haven't already seen
398 // a type-specifier. Without this check we misparse:
399 // typedef int X; struct Y { short X; }; as 'short int'.
400 if (!DS.hasTypeSpecifier()) {
401 // It has to be available as a typedef too!
402 if (void *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(),
403 CurScope)) {
404 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
405 TypeRep);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000406 if (isInvalid)
407 break;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000408 // FIXME: restrict this to "id" and ObjC classnames.
409 DS.Range.setEnd(Tok.getLocation());
410 ConsumeToken(); // The identifier
411 if (Tok.is(tok::less)) {
Steve Narofff908a872007-10-30 02:23:23 +0000412 SourceLocation endProtoLoc;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000413 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofff908a872007-10-30 02:23:23 +0000414 ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000415 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
416 new llvm::SmallVector<DeclTy *, 8>;
417 DS.setProtocolQualifiers(ProtocolDecl);
418 Actions.FindProtocolDeclaration(Loc,
419 &ProtocolRefs[0], ProtocolRefs.size(),
420 *ProtocolDecl);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000421 }
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000422 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000423 }
424 }
425 // FALL THROUGH.
426 default:
427 // If this is not a declaration specifier token, we're done reading decl
428 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000429 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +0000430 return;
431
432 // GNU attributes support.
433 case tok::kw___attribute:
434 DS.AddAttributes(ParseAttributes());
435 continue;
436
437 // storage-class-specifier
438 case tok::kw_typedef:
439 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
440 break;
441 case tok::kw_extern:
442 if (DS.isThreadSpecified())
443 Diag(Tok, diag::ext_thread_before, "extern");
444 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
445 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000446 case tok::kw___private_extern__:
447 // FIXME: Implement private extern.
448 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
449 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000450 case tok::kw_static:
451 if (DS.isThreadSpecified())
452 Diag(Tok, diag::ext_thread_before, "static");
453 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
454 break;
455 case tok::kw_auto:
456 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
457 break;
458 case tok::kw_register:
459 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
460 break;
461 case tok::kw___thread:
462 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
463 break;
464
465 // type-specifiers
466 case tok::kw_short:
467 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
468 break;
469 case tok::kw_long:
470 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
471 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
472 else
473 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
474 break;
475 case tok::kw_signed:
476 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
477 break;
478 case tok::kw_unsigned:
479 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
480 break;
481 case tok::kw__Complex:
482 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
483 break;
484 case tok::kw__Imaginary:
485 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
486 break;
487 case tok::kw_void:
488 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
489 break;
490 case tok::kw_char:
491 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
492 break;
493 case tok::kw_int:
494 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
495 break;
496 case tok::kw_float:
497 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
498 break;
499 case tok::kw_double:
500 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
501 break;
502 case tok::kw_bool: // [C++ 2.11p1]
503 case tok::kw__Bool:
504 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
505 break;
506 case tok::kw__Decimal32:
507 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
508 break;
509 case tok::kw__Decimal64:
510 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
511 break;
512 case tok::kw__Decimal128:
513 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
514 break;
515
516 case tok::kw_struct:
517 case tok::kw_union:
518 ParseStructUnionSpecifier(DS);
519 continue;
520 case tok::kw_enum:
521 ParseEnumSpecifier(DS);
522 continue;
523
Steve Naroffd1861fd2007-07-31 12:34:36 +0000524 // GNU typeof support.
525 case tok::kw_typeof:
526 ParseTypeofSpecifier(DS);
527 continue;
528
Reid Spencer5f016e22007-07-11 17:01:13 +0000529 // type-qualifier
530 case tok::kw_const:
531 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
532 getLang())*2;
533 break;
534 case tok::kw_volatile:
535 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
536 getLang())*2;
537 break;
538 case tok::kw_restrict:
539 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
540 getLang())*2;
541 break;
542
543 // function-specifier
544 case tok::kw_inline:
545 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
546 break;
547 }
548 // If the specifier combination wasn't legal, issue a diagnostic.
549 if (isInvalid) {
550 assert(PrevSpec && "Method did not return previous specifier!");
551 if (isInvalid == 1) // Error.
552 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
553 else // extwarn.
554 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
555 }
Chris Lattnere80a59c2007-07-25 00:24:17 +0000556 DS.Range.setEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000557 ConsumeToken();
558 }
559}
560
561/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
562/// the first token has already been read and has been turned into an instance
563/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
564/// otherwise it returns false and fills in Decl.
565bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
566 AttributeList *Attr = 0;
567 // If attributes exist after tag, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000568 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000569 Attr = ParseAttributes();
570
571 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner04d66662007-10-09 17:33:22 +0000572 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000573 Diag(Tok, diag::err_expected_ident_lbrace);
Chris Lattnere80a59c2007-07-25 00:24:17 +0000574
575 // Skip the rest of this declarator, up until the comma or semicolon.
576 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000577 return true;
578 }
579
580 // If an identifier is present, consume and remember it.
581 IdentifierInfo *Name = 0;
582 SourceLocation NameLoc;
Chris Lattner04d66662007-10-09 17:33:22 +0000583 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000584 Name = Tok.getIdentifierInfo();
585 NameLoc = ConsumeToken();
586 }
587
588 // There are three options here. If we have 'struct foo;', then this is a
589 // forward declaration. If we have 'struct foo {...' then this is a
590 // definition. Otherwise we have something like 'struct foo xyz', a reference.
591 //
592 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
593 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
594 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
595 //
596 Action::TagKind TK;
Chris Lattner04d66662007-10-09 17:33:22 +0000597 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 TK = Action::TK_Definition;
Chris Lattner04d66662007-10-09 17:33:22 +0000599 else if (Tok.is(tok::semi))
Reid Spencer5f016e22007-07-11 17:01:13 +0000600 TK = Action::TK_Declaration;
601 else
602 TK = Action::TK_Reference;
Steve Naroff08d92e42007-09-15 18:49:24 +0000603 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 return false;
605}
606
607
608/// ParseStructUnionSpecifier
609/// struct-or-union-specifier: [C99 6.7.2.1]
610/// struct-or-union identifier[opt] '{' struct-contents '}'
611/// struct-or-union identifier
612/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
613/// '}' attributes[opt]
614/// [GNU] struct-or-union attributes[opt] identifier
615/// struct-or-union:
616/// 'struct'
617/// 'union'
618///
619void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +0000620 assert((Tok.is(tok::kw_struct) || Tok.is(tok::kw_union)) &&
621 "Not a struct/union specifier");
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 DeclSpec::TST TagType =
Chris Lattner04d66662007-10-09 17:33:22 +0000623 Tok.is(tok::kw_union) ? DeclSpec::TST_union : DeclSpec::TST_struct;
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 SourceLocation StartLoc = ConsumeToken();
625
626 // Parse the tag portion of this.
627 DeclTy *TagDecl;
628 if (ParseTag(TagDecl, TagType, StartLoc))
629 return;
630
631 // If there is a body, parse it and inform the actions module.
Chris Lattner04d66662007-10-09 17:33:22 +0000632 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 ParseStructUnionBody(StartLoc, TagType, TagDecl);
634
635 const char *PrevSpec = 0;
636 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
637 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
638}
639
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000640/// ParseStructDeclaration - Parse a struct declaration without the terminating
641/// semicolon.
642///
Reid Spencer5f016e22007-07-11 17:01:13 +0000643/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000644/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000645/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000646/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000647/// struct-declarator-list:
648/// struct-declarator
649/// struct-declarator-list ',' struct-declarator
650/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
651/// struct-declarator:
652/// declarator
653/// [GNU] declarator attributes[opt]
654/// declarator[opt] ':' constant-expression
655/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
656///
Steve Naroff28a7ca82007-08-20 22:28:22 +0000657void Parser::ParseStructDeclaration(DeclTy *TagDecl,
Steve Naroff4e6526b2007-08-28 16:31:47 +0000658 llvm::SmallVectorImpl<DeclTy*> &FieldDecls) {
Steve Naroff28a7ca82007-08-20 22:28:22 +0000659 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattner04d66662007-10-09 17:33:22 +0000660 if (Tok.is(tok::kw___extension__))
Steve Naroff28a7ca82007-08-20 22:28:22 +0000661 ConsumeToken();
662
663 // Parse the common specifier-qualifiers-list piece.
664 DeclSpec DS;
665 SourceLocation SpecQualLoc = Tok.getLocation();
666 ParseSpecifierQualifierList(DS);
667 // TODO: Does specifier-qualifier list correctly check that *something* is
668 // specified?
669
670 // If there are no declarators, issue a warning.
Chris Lattner04d66662007-10-09 17:33:22 +0000671 if (Tok.is(tok::semi)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +0000672 Diag(SpecQualLoc, diag::w_no_declarators);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000673 return;
674 }
675
676 // Read struct-declarators until we find the semicolon.
677 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
678
679 while (1) {
680 /// struct-declarator: declarator
681 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +0000682 if (Tok.isNot(tok::colon))
Steve Naroff28a7ca82007-08-20 22:28:22 +0000683 ParseDeclarator(DeclaratorInfo);
684
685 ExprTy *BitfieldSize = 0;
Chris Lattner04d66662007-10-09 17:33:22 +0000686 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +0000687 ConsumeToken();
688 ExprResult Res = ParseConstantExpression();
689 if (Res.isInvalid) {
690 SkipUntil(tok::semi, true, true);
691 } else {
692 BitfieldSize = Res.Val;
693 }
694 }
695
696 // If attributes exist after the declarator, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000697 if (Tok.is(tok::kw___attribute))
Steve Naroff28a7ca82007-08-20 22:28:22 +0000698 DeclaratorInfo.AddAttributes(ParseAttributes());
699
700 // Install the declarator into the current TagDecl.
Steve Naroff08d92e42007-09-15 18:49:24 +0000701 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl, SpecQualLoc,
Steve Naroff28a7ca82007-08-20 22:28:22 +0000702 DeclaratorInfo, BitfieldSize);
703 FieldDecls.push_back(Field);
704
705 // If we don't have a comma, it is either the end of the list (a ';')
706 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000707 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000708 return;
Steve Naroff28a7ca82007-08-20 22:28:22 +0000709
710 // Consume the comma.
711 ConsumeToken();
712
713 // Parse the next declarator.
714 DeclaratorInfo.clear();
715
716 // Attributes are only allowed on the second declarator.
Chris Lattner04d66662007-10-09 17:33:22 +0000717 if (Tok.is(tok::kw___attribute))
Steve Naroff28a7ca82007-08-20 22:28:22 +0000718 DeclaratorInfo.AddAttributes(ParseAttributes());
719 }
Steve Naroff28a7ca82007-08-20 22:28:22 +0000720}
721
722/// ParseStructUnionBody
723/// struct-contents:
724/// struct-declaration-list
725/// [EXT] empty
726/// [GNU] "struct-declaration-list" without terminatoring ';'
727/// struct-declaration-list:
728/// struct-declaration
729/// struct-declaration-list struct-declaration
730/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
731///
Reid Spencer5f016e22007-07-11 17:01:13 +0000732void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
733 unsigned TagType, DeclTy *TagDecl) {
734 SourceLocation LBraceLoc = ConsumeBrace();
735
736 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
737 // C++.
Chris Lattner04d66662007-10-09 17:33:22 +0000738 if (Tok.is(tok::r_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +0000739 Diag(Tok, diag::ext_empty_struct_union_enum,
740 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
741
742 llvm::SmallVector<DeclTy*, 32> FieldDecls;
743
744 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +0000745 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000746 // Each iteration of this loop reads one struct-declaration.
747
748 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +0000749 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000750 Diag(Tok, diag::ext_extra_struct_semi);
751 ConsumeToken();
752 continue;
753 }
Steve Naroff28a7ca82007-08-20 22:28:22 +0000754 ParseStructDeclaration(TagDecl, FieldDecls);
Reid Spencer5f016e22007-07-11 17:01:13 +0000755
Chris Lattner04d66662007-10-09 17:33:22 +0000756 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000757 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +0000758 } else if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
760 break;
761 } else {
762 Diag(Tok, diag::err_expected_semi_decl_list);
763 // Skip to end of block or statement
764 SkipUntil(tok::r_brace, true, true);
765 }
766 }
767
Steve Naroff60fccee2007-10-29 21:38:07 +0000768 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000769
Fariborz Jahanian9d048ff2007-09-29 00:54:24 +0000770 Actions.ActOnFields(CurScope,
Steve Naroff60fccee2007-10-29 21:38:07 +0000771 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
772 LBraceLoc, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000773
774 AttributeList *AttrList = 0;
775 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000776 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000777 AttrList = ParseAttributes(); // FIXME: where should I put them?
778}
779
780
781/// ParseEnumSpecifier
782/// enum-specifier: [C99 6.7.2.2]
783/// 'enum' identifier[opt] '{' enumerator-list '}'
784/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
785/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
786/// '}' attributes[opt]
787/// 'enum' identifier
788/// [GNU] 'enum' attributes[opt] identifier
789void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +0000790 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Reid Spencer5f016e22007-07-11 17:01:13 +0000791 SourceLocation StartLoc = ConsumeToken();
792
793 // Parse the tag portion of this.
794 DeclTy *TagDecl;
795 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
796 return;
797
Chris Lattner04d66662007-10-09 17:33:22 +0000798 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +0000799 ParseEnumBody(StartLoc, TagDecl);
800
801 // TODO: semantic analysis on the declspec for enums.
802 const char *PrevSpec = 0;
803 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
804 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
805}
806
807/// ParseEnumBody - Parse a {} enclosed enumerator-list.
808/// enumerator-list:
809/// enumerator
810/// enumerator-list ',' enumerator
811/// enumerator:
812/// enumeration-constant
813/// enumeration-constant '=' constant-expression
814/// enumeration-constant:
815/// identifier
816///
817void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
818 SourceLocation LBraceLoc = ConsumeBrace();
819
Chris Lattner7946dd32007-08-27 17:24:30 +0000820 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +0000821 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Reid Spencer5f016e22007-07-11 17:01:13 +0000822 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
823
824 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
825
826 DeclTy *LastEnumConstDecl = 0;
827
828 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +0000829 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000830 IdentifierInfo *Ident = Tok.getIdentifierInfo();
831 SourceLocation IdentLoc = ConsumeToken();
832
833 SourceLocation EqualLoc;
834 ExprTy *AssignedVal = 0;
Chris Lattner04d66662007-10-09 17:33:22 +0000835 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000836 EqualLoc = ConsumeToken();
837 ExprResult Res = ParseConstantExpression();
838 if (Res.isInvalid)
839 SkipUntil(tok::comma, tok::r_brace, true, true);
840 else
841 AssignedVal = Res.Val;
842 }
843
844 // Install the enumerator constant into EnumDecl.
Steve Naroff08d92e42007-09-15 18:49:24 +0000845 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +0000846 LastEnumConstDecl,
847 IdentLoc, Ident,
848 EqualLoc, AssignedVal);
849 EnumConstantDecls.push_back(EnumConstDecl);
850 LastEnumConstDecl = EnumConstDecl;
851
Chris Lattner04d66662007-10-09 17:33:22 +0000852 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000853 break;
854 SourceLocation CommaLoc = ConsumeToken();
855
Chris Lattner04d66662007-10-09 17:33:22 +0000856 if (Tok.isNot(tok::identifier) && !getLang().C99)
Reid Spencer5f016e22007-07-11 17:01:13 +0000857 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
858 }
859
860 // Eat the }.
861 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
862
Steve Naroff08d92e42007-09-15 18:49:24 +0000863 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Reid Spencer5f016e22007-07-11 17:01:13 +0000864 EnumConstantDecls.size());
865
866 DeclTy *AttrList = 0;
867 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000868 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000869 AttrList = ParseAttributes(); // FIXME: where do they do?
870}
871
872/// isTypeSpecifierQualifier - Return true if the current token could be the
873/// start of a specifier-qualifier-list.
874bool Parser::isTypeSpecifierQualifier() const {
875 switch (Tok.getKind()) {
876 default: return false;
877 // GNU attributes support.
878 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +0000879 // GNU typeof support.
880 case tok::kw_typeof:
881
Reid Spencer5f016e22007-07-11 17:01:13 +0000882 // type-specifiers
883 case tok::kw_short:
884 case tok::kw_long:
885 case tok::kw_signed:
886 case tok::kw_unsigned:
887 case tok::kw__Complex:
888 case tok::kw__Imaginary:
889 case tok::kw_void:
890 case tok::kw_char:
891 case tok::kw_int:
892 case tok::kw_float:
893 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +0000894 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +0000895 case tok::kw__Bool:
896 case tok::kw__Decimal32:
897 case tok::kw__Decimal64:
898 case tok::kw__Decimal128:
899
900 // struct-or-union-specifier
901 case tok::kw_struct:
902 case tok::kw_union:
903 // enum-specifier
904 case tok::kw_enum:
905
906 // type-qualifier
907 case tok::kw_const:
908 case tok::kw_volatile:
909 case tok::kw_restrict:
910 return true;
911
912 // typedef-name
913 case tok::identifier:
914 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000915 }
916}
917
918/// isDeclarationSpecifier() - Return true if the current token is part of a
919/// declaration specifier.
920bool Parser::isDeclarationSpecifier() const {
921 switch (Tok.getKind()) {
922 default: return false;
923 // storage-class-specifier
924 case tok::kw_typedef:
925 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +0000926 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +0000927 case tok::kw_static:
928 case tok::kw_auto:
929 case tok::kw_register:
930 case tok::kw___thread:
931
932 // type-specifiers
933 case tok::kw_short:
934 case tok::kw_long:
935 case tok::kw_signed:
936 case tok::kw_unsigned:
937 case tok::kw__Complex:
938 case tok::kw__Imaginary:
939 case tok::kw_void:
940 case tok::kw_char:
941 case tok::kw_int:
942 case tok::kw_float:
943 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +0000944 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +0000945 case tok::kw__Bool:
946 case tok::kw__Decimal32:
947 case tok::kw__Decimal64:
948 case tok::kw__Decimal128:
949
950 // struct-or-union-specifier
951 case tok::kw_struct:
952 case tok::kw_union:
953 // enum-specifier
954 case tok::kw_enum:
955
956 // type-qualifier
957 case tok::kw_const:
958 case tok::kw_volatile:
959 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +0000960
Reid Spencer5f016e22007-07-11 17:01:13 +0000961 // function-specifier
962 case tok::kw_inline:
Chris Lattnerd6c7c182007-08-09 16:40:21 +0000963
Chris Lattner1ef08762007-08-09 17:01:07 +0000964 // GNU typeof support.
965 case tok::kw_typeof:
966
967 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +0000968 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +0000969 return true;
970
971 // typedef-name
972 case tok::identifier:
973 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 }
975}
976
977
978/// ParseTypeQualifierListOpt
979/// type-qualifier-list: [C99 6.7.5]
980/// type-qualifier
981/// [GNU] attributes
982/// type-qualifier-list type-qualifier
983/// [GNU] type-qualifier-list attributes
984///
985void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
986 while (1) {
987 int isInvalid = false;
988 const char *PrevSpec = 0;
989 SourceLocation Loc = Tok.getLocation();
990
991 switch (Tok.getKind()) {
992 default:
993 // If this is not a type-qualifier token, we're done reading type
994 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000995 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 return;
997 case tok::kw_const:
998 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
999 getLang())*2;
1000 break;
1001 case tok::kw_volatile:
1002 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1003 getLang())*2;
1004 break;
1005 case tok::kw_restrict:
1006 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1007 getLang())*2;
1008 break;
1009 case tok::kw___attribute:
1010 DS.AddAttributes(ParseAttributes());
1011 continue; // do *not* consume the next token!
1012 }
1013
1014 // If the specifier combination wasn't legal, issue a diagnostic.
1015 if (isInvalid) {
1016 assert(PrevSpec && "Method did not return previous specifier!");
1017 if (isInvalid == 1) // Error.
1018 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1019 else // extwarn.
1020 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1021 }
1022 ConsumeToken();
1023 }
1024}
1025
1026
1027/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1028///
1029void Parser::ParseDeclarator(Declarator &D) {
1030 /// This implements the 'declarator' production in the C grammar, then checks
1031 /// for well-formedness and issues diagnostics.
1032 ParseDeclaratorInternal(D);
1033
1034 // TODO: validate D.
1035
1036}
1037
1038/// ParseDeclaratorInternal
1039/// declarator: [C99 6.7.5]
1040/// pointer[opt] direct-declarator
1041/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1042/// [GNU] '&' restrict[opt] attributes[opt] declarator
1043///
1044/// pointer: [C99 6.7.5]
1045/// '*' type-qualifier-list[opt]
1046/// '*' type-qualifier-list[opt] pointer
1047///
1048void Parser::ParseDeclaratorInternal(Declarator &D) {
1049 tok::TokenKind Kind = Tok.getKind();
1050
1051 // Not a pointer or C++ reference.
1052 if (Kind != tok::star && !(Kind == tok::amp && getLang().CPlusPlus))
1053 return ParseDirectDeclarator(D);
1054
1055 // Otherwise, '*' -> pointer or '&' -> reference.
1056 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1057
1058 if (Kind == tok::star) {
1059 // Is a pointer
1060 DeclSpec DS;
1061
1062 ParseTypeQualifierListOpt(DS);
1063
1064 // Recursively parse the declarator.
1065 ParseDeclaratorInternal(D);
1066
1067 // Remember that we parsed a pointer type, and remember the type-quals.
1068 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc));
1069 } else {
1070 // Is a reference
1071 DeclSpec DS;
1072
1073 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1074 // cv-qualifiers are introduced through the use of a typedef or of a
1075 // template type argument, in which case the cv-qualifiers are ignored.
1076 //
1077 // [GNU] Retricted references are allowed.
1078 // [GNU] Attributes on references are allowed.
1079 ParseTypeQualifierListOpt(DS);
1080
1081 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1082 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1083 Diag(DS.getConstSpecLoc(),
1084 diag::err_invalid_reference_qualifier_application,
1085 "const");
1086 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1087 Diag(DS.getVolatileSpecLoc(),
1088 diag::err_invalid_reference_qualifier_application,
1089 "volatile");
1090 }
1091
1092 // Recursively parse the declarator.
1093 ParseDeclaratorInternal(D);
1094
1095 // Remember that we parsed a reference type. It doesn't have type-quals.
1096 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc));
1097 }
1098}
1099
1100/// ParseDirectDeclarator
1101/// direct-declarator: [C99 6.7.5]
1102/// identifier
1103/// '(' declarator ')'
1104/// [GNU] '(' attributes declarator ')'
1105/// [C90] direct-declarator '[' constant-expression[opt] ']'
1106/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1107/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1108/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1109/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1110/// direct-declarator '(' parameter-type-list ')'
1111/// direct-declarator '(' identifier-list[opt] ')'
1112/// [GNU] direct-declarator '(' parameter-forward-declarations
1113/// parameter-type-list[opt] ')'
1114///
1115void Parser::ParseDirectDeclarator(Declarator &D) {
1116 // Parse the first direct-declarator seen.
Chris Lattner04d66662007-10-09 17:33:22 +00001117 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001118 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1119 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1120 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001121 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001122 // direct-declarator: '(' declarator ')'
1123 // direct-declarator: '(' attributes declarator ')'
1124 // Example: 'char (*X)' or 'int (*XX)(void)'
1125 ParseParenDeclarator(D);
1126 } else if (D.mayOmitIdentifier()) {
1127 // This could be something simple like "int" (in which case the declarator
1128 // portion is empty), if an abstract-declarator is allowed.
1129 D.SetIdentifier(0, Tok.getLocation());
1130 } else {
1131 // Expected identifier or '('.
1132 Diag(Tok, diag::err_expected_ident_lparen);
1133 D.SetIdentifier(0, Tok.getLocation());
1134 }
1135
1136 assert(D.isPastIdentifier() &&
1137 "Haven't past the location of the identifier yet?");
1138
1139 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00001140 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001141 ParseParenDeclarator(D);
Chris Lattner04d66662007-10-09 17:33:22 +00001142 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001143 ParseBracketDeclarator(D);
1144 } else {
1145 break;
1146 }
1147 }
1148}
1149
1150/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
1151/// either be before the identifier (in which case these are just grouping
1152/// parens for precedence) or it may be after the identifier, in which case
1153/// these are function arguments.
1154///
1155/// This method also handles this portion of the grammar:
1156/// parameter-type-list: [C99 6.7.5]
1157/// parameter-list
1158/// parameter-list ',' '...'
1159///
1160/// parameter-list: [C99 6.7.5]
1161/// parameter-declaration
1162/// parameter-list ',' parameter-declaration
1163///
1164/// parameter-declaration: [C99 6.7.5]
1165/// declaration-specifiers declarator
1166/// [GNU] declaration-specifiers declarator attributes
1167/// declaration-specifiers abstract-declarator[opt]
1168/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1169///
1170/// identifier-list: [C99 6.7.5]
1171/// identifier
1172/// identifier-list ',' identifier
1173///
1174void Parser::ParseParenDeclarator(Declarator &D) {
1175 SourceLocation StartLoc = ConsumeParen();
1176
1177 // If we haven't past the identifier yet (or where the identifier would be
1178 // stored, if this is an abstract declarator), then this is probably just
1179 // grouping parens.
1180 if (!D.isPastIdentifier()) {
1181 // Okay, this is probably a grouping paren. However, if this could be an
1182 // abstract-declarator, then this could also be the start of function
1183 // arguments (consider 'void()').
1184 bool isGrouping;
1185
1186 if (!D.mayOmitIdentifier()) {
1187 // If this can't be an abstract-declarator, this *must* be a grouping
1188 // paren, because we haven't seen the identifier yet.
1189 isGrouping = true;
Chris Lattner04d66662007-10-09 17:33:22 +00001190 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Reid Spencer5f016e22007-07-11 17:01:13 +00001191 isDeclarationSpecifier()) { // 'int(int)' is a function.
1192 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1193 // considered to be a type, not a K&R identifier-list.
1194 isGrouping = false;
1195 } else {
1196 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1197 isGrouping = true;
1198 }
1199
1200 // If this is a grouping paren, handle:
1201 // direct-declarator: '(' declarator ')'
1202 // direct-declarator: '(' attributes declarator ')'
1203 if (isGrouping) {
Chris Lattner04d66662007-10-09 17:33:22 +00001204 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001205 D.AddAttributes(ParseAttributes());
1206
1207 ParseDeclaratorInternal(D);
1208 // Match the ')'.
1209 MatchRHSPunctuation(tok::r_paren, StartLoc);
1210 return;
1211 }
1212
1213 // Okay, if this wasn't a grouping paren, it must be the start of a function
1214 // argument list. Recognize that this declarator will never have an
1215 // identifier (and remember where it would have been), then fall through to
1216 // the handling of argument lists.
1217 D.SetIdentifier(0, Tok.getLocation());
1218 }
1219
1220 // Okay, this is the parameter list of a function definition, or it is an
1221 // identifier list of a K&R-style function.
1222 bool IsVariadic;
1223 bool HasPrototype;
1224 bool ErrorEmitted = false;
1225
1226 // Build up an array of information about the parsed arguments.
1227 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1228 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1229
Chris Lattner04d66662007-10-09 17:33:22 +00001230 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001231 // int() -> no prototype, no '...'.
1232 IsVariadic = false;
1233 HasPrototype = false;
Chris Lattner04d66662007-10-09 17:33:22 +00001234 } else if (Tok.is(tok::identifier) &&
Reid Spencer5f016e22007-07-11 17:01:13 +00001235 // K&R identifier lists can't have typedefs as identifiers, per
1236 // C99 6.7.5.3p11.
1237 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1238 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1239 // normal declarators, not for abstract-declarators.
1240 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
1241
1242 // If there was no identifier specified, either we are in an
1243 // abstract-declarator, or we are in a parameter declarator which was found
1244 // to be abstract. In abstract-declarators, identifier lists are not valid,
1245 // diagnose this.
1246 if (!D.getIdentifier())
1247 Diag(Tok, diag::ext_ident_list_in_param);
1248
1249 // Remember this identifier in ParamInfo.
1250 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1251 Tok.getLocation(), 0));
1252
1253 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001254 while (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 // Eat the comma.
1256 ConsumeToken();
1257
Chris Lattner04d66662007-10-09 17:33:22 +00001258 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001259 Diag(Tok, diag::err_expected_ident);
1260 ErrorEmitted = true;
1261 break;
1262 }
1263
1264 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
1265
1266 // Verify that the argument identifier has not already been mentioned.
1267 if (!ParamsSoFar.insert(ParmII)) {
1268 Diag(Tok.getLocation(), diag::err_param_redefinition,ParmII->getName());
1269 ParmII = 0;
1270 }
1271
1272 // Remember this identifier in ParamInfo.
1273 if (ParmII)
1274 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1275 Tok.getLocation(), 0));
1276
1277 // Eat the identifier.
1278 ConsumeToken();
1279 }
1280
1281 // K&R 'prototype'.
1282 IsVariadic = false;
1283 HasPrototype = false;
1284 } else {
1285 // Finally, a normal, non-empty parameter type list.
1286
1287 // Enter function-declaration scope, limiting any declarators for struct
1288 // tags to the function prototype scope.
1289 // FIXME: is this needed?
Chris Lattner31e05722007-08-26 06:24:45 +00001290 EnterScope(Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001291
1292 IsVariadic = false;
1293 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00001294 if (Tok.is(tok::ellipsis)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001295 IsVariadic = true;
1296
1297 // Check to see if this is "void(...)" which is not allowed.
1298 if (ParamInfo.empty()) {
1299 // Otherwise, parse parameter type list. If it starts with an
1300 // ellipsis, diagnose the malformed function.
1301 Diag(Tok, diag::err_ellipsis_first_arg);
1302 IsVariadic = false; // Treat this like 'void()'.
1303 }
1304
1305 // Consume the ellipsis.
1306 ConsumeToken();
1307 break;
1308 }
1309
1310 // Parse the declaration-specifiers.
1311 DeclSpec DS;
1312 ParseDeclarationSpecifiers(DS);
1313
1314 // Parse the declarator. This is "PrototypeContext", because we must
1315 // accept either 'declarator' or 'abstract-declarator' here.
1316 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1317 ParseDeclarator(ParmDecl);
1318
1319 // Parse GNU attributes, if present.
Chris Lattner04d66662007-10-09 17:33:22 +00001320 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001321 ParmDecl.AddAttributes(ParseAttributes());
1322
1323 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
1324 // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted.
1325 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1326 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
1327 Diag(DS.getStorageClassSpecLoc(),
1328 diag::err_invalid_storage_class_in_func_decl);
1329 DS.ClearStorageClassSpecs();
1330 }
1331 if (DS.isThreadSpecified()) {
1332 Diag(DS.getThreadSpecLoc(),
1333 diag::err_invalid_storage_class_in_func_decl);
1334 DS.ClearStorageClassSpecs();
1335 }
1336
1337 // Inform the actions module about the parameter declarator, so it gets
1338 // added to the current scope.
1339 Action::TypeResult ParamTy =
Steve Naroff08d92e42007-09-15 18:49:24 +00001340 Actions.ActOnParamDeclaratorType(CurScope, ParmDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001341
1342 // Remember this parsed parameter in ParamInfo.
1343 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1344
1345 // Verify that the argument identifier has not already been mentioned.
1346 if (ParmII && !ParamsSoFar.insert(ParmII)) {
1347 Diag(ParmDecl.getIdentifierLoc(), diag::err_param_redefinition,
1348 ParmII->getName());
1349 ParmII = 0;
1350 }
1351
Steve Naroffe1223f72007-08-28 03:03:08 +00001352 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Nate Begeman1b4e2512007-11-13 22:14:47 +00001353 ParmDecl.getIdentifierLoc(), ParamTy.Val, ParmDecl.getInvalidType(),
1354 ParmDecl.getDeclSpec().getAttributes()));
1355
1356 // Ownership of DeclSpec has been handed off to ParamInfo.
1357 DS.clearAttributes();
Reid Spencer5f016e22007-07-11 17:01:13 +00001358
1359 // If the next token is a comma, consume it and keep reading arguments.
Chris Lattner04d66662007-10-09 17:33:22 +00001360 if (Tok.isNot(tok::comma)) break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001361
1362 // Consume the comma.
1363 ConsumeToken();
1364 }
1365
1366 HasPrototype = true;
1367
1368 // Leave prototype scope.
1369 ExitScope();
1370 }
1371
1372 // Remember that we parsed a function type, and remember the attributes.
1373 if (!ErrorEmitted)
1374 D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic,
1375 &ParamInfo[0], ParamInfo.size(),
1376 StartLoc));
1377
1378 // If we have the closing ')', eat it and we're done.
Chris Lattner04d66662007-10-09 17:33:22 +00001379 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001380 ConsumeParen();
1381 } else {
1382 // If an error happened earlier parsing something else in the proto, don't
1383 // issue another error.
1384 if (!ErrorEmitted)
1385 Diag(Tok, diag::err_expected_rparen);
1386 SkipUntil(tok::r_paren);
1387 }
1388}
1389
1390
1391/// [C90] direct-declarator '[' constant-expression[opt] ']'
1392/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1393/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1394/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1395/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1396void Parser::ParseBracketDeclarator(Declarator &D) {
1397 SourceLocation StartLoc = ConsumeBracket();
1398
1399 // If valid, this location is the position where we read the 'static' keyword.
1400 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00001401 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00001402 StaticLoc = ConsumeToken();
1403
1404 // If there is a type-qualifier-list, read it now.
1405 DeclSpec DS;
1406 ParseTypeQualifierListOpt(DS);
1407
1408 // If we haven't already read 'static', check to see if there is one after the
1409 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001410 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00001411 StaticLoc = ConsumeToken();
1412
1413 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1414 bool isStar = false;
1415 ExprResult NumElements(false);
Chris Lattner04d66662007-10-09 17:33:22 +00001416 if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001417 // Remember the '*' token, in case we have to un-get it.
Chris Lattnerd2177732007-07-20 16:59:19 +00001418 Token StarTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001419 ConsumeToken();
1420
1421 // Check that the ']' token is present to avoid incorrectly parsing
1422 // expressions starting with '*' as [*].
Chris Lattner04d66662007-10-09 17:33:22 +00001423 if (Tok.is(tok::r_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001424 if (StaticLoc.isValid())
1425 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1426 StaticLoc = SourceLocation(); // Drop the static.
1427 isStar = true;
1428 } else {
1429 // Otherwise, the * must have been some expression (such as '*ptr') that
1430 // started an assignment-expr. We already consumed the token, but now we
1431 // need to reparse it. This handles cases like 'X[*p + 4]'
1432 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
1433 }
Chris Lattner04d66662007-10-09 17:33:22 +00001434 } else if (Tok.isNot(tok::r_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001435 // Parse the assignment-expression now.
1436 NumElements = ParseAssignmentExpression();
1437 }
1438
1439 // If there was an error parsing the assignment-expression, recover.
1440 if (NumElements.isInvalid) {
1441 // If the expression was invalid, skip it.
1442 SkipUntil(tok::r_square);
1443 return;
1444 }
1445
1446 MatchRHSPunctuation(tok::r_square, StartLoc);
1447
1448 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1449 // it was not a constant expression.
1450 if (!getLang().C99) {
1451 // TODO: check C90 array constant exprness.
1452 if (isStar || StaticLoc.isValid() ||
1453 0/*TODO: NumElts is not a C90 constantexpr */)
1454 Diag(StartLoc, diag::ext_c99_array_usage);
1455 }
1456
1457 // Remember that we parsed a pointer type, and remember the type-quals.
1458 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1459 StaticLoc.isValid(), isStar,
1460 NumElements.Val, StartLoc));
1461}
1462
Steve Naroffd1861fd2007-07-31 12:34:36 +00001463/// [GNU] typeof-specifier:
1464/// typeof ( expressions )
1465/// typeof ( type-name )
1466///
1467void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00001468 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001469 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffd1861fd2007-07-31 12:34:36 +00001470 SourceLocation StartLoc = ConsumeToken();
1471
Chris Lattner04d66662007-10-09 17:33:22 +00001472 if (Tok.isNot(tok::l_paren)) {
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001473 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1474 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001475 }
1476 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1477
1478 if (isTypeSpecifierQualifier()) {
1479 TypeTy *Ty = ParseTypeName();
1480
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001481 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1482
Chris Lattner04d66662007-10-09 17:33:22 +00001483 if (Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001484 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001485 return;
1486 }
1487 RParenLoc = ConsumeParen();
1488 const char *PrevSpec = 0;
1489 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1490 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1491 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001492 } else { // we have an expression.
1493 ExprResult Result = ParseExpression();
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001494
Chris Lattner04d66662007-10-09 17:33:22 +00001495 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001496 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001497 return;
1498 }
1499 RParenLoc = ConsumeParen();
1500 const char *PrevSpec = 0;
1501 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1502 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1503 Result.Val))
1504 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001505 }
Steve Naroffd1861fd2007-07-31 12:34:36 +00001506}
1507