blob: d9ec4ddcf6db37d9a5e9309cedda5ddf8ca610fb [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +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 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 }
299
300 Diag(Tok, diag::err_parse_error);
301 // Skip to end of block or statement
Chris Lattnered442382007-08-21 18:36:18 +0000302 SkipUntil(tok::r_brace, true, true);
Chris Lattner04d66662007-10-09 17:33:22 +0000303 if (Tok.is(tok::semi))
Reid Spencer5f016e22007-07-11 17:01:13 +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.
Reid Spencer5f016e22007-07-11 17:01:13 +0000317 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 Naroff2cb64ec2007-07-31 23:56:32 +0000375/// [GNU] typeof-specifier
Reid Spencer5f016e22007-07-11 17:01:13 +0000376/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000377/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Reid Spencer5f016e22007-07-11 17:01:13 +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) {
Chris Lattnere80a59c2007-07-25 00:24:17 +0000386 DS.Range.setBegin(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 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 Naroff4fa7afd2007-08-22 23:18:22 +0000404 if (isInvalid)
405 break;
Fariborz Jahanian4b6c9052007-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 Narofff908a872007-10-30 02:23:23 +0000410 SourceLocation endProtoLoc;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000411 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofff908a872007-10-30 02:23:23 +0000412 ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc);
Fariborz Jahanian4b6c9052007-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 Naroff4fa7afd2007-08-22 23:18:22 +0000419 }
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000420 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Kremenek7a9d49f2007-12-11 21:27:55 +0000427 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroffd1861fd2007-07-31 12:34:36 +0000518 // GNU typeof support.
519 case tok::kw_typeof:
520 ParseTypeofSpecifier(DS);
521 continue;
522
Reid Spencer5f016e22007-07-11 17:01:13 +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 }
Chris Lattnere80a59c2007-07-25 00:24:17 +0000550 DS.Range.setEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000551 ConsumeToken();
552 }
553}
554
555/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
556/// the first token has already been read and has been turned into an instance
557/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
558/// otherwise it returns false and fills in Decl.
559bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
560 AttributeList *Attr = 0;
561 // If attributes exist after tag, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000562 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000563 Attr = ParseAttributes();
564
565 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner04d66662007-10-09 17:33:22 +0000566 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 Diag(Tok, diag::err_expected_ident_lbrace);
Chris Lattnere80a59c2007-07-25 00:24:17 +0000568
569 // Skip the rest of this declarator, up until the comma or semicolon.
570 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000571 return true;
572 }
573
574 // If an identifier is present, consume and remember it.
575 IdentifierInfo *Name = 0;
576 SourceLocation NameLoc;
Chris Lattner04d66662007-10-09 17:33:22 +0000577 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000578 Name = Tok.getIdentifierInfo();
579 NameLoc = ConsumeToken();
580 }
581
582 // There are three options here. If we have 'struct foo;', then this is a
583 // forward declaration. If we have 'struct foo {...' then this is a
584 // definition. Otherwise we have something like 'struct foo xyz', a reference.
585 //
586 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
587 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
588 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
589 //
590 Action::TagKind TK;
Chris Lattner04d66662007-10-09 17:33:22 +0000591 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 TK = Action::TK_Definition;
Chris Lattner04d66662007-10-09 17:33:22 +0000593 else if (Tok.is(tok::semi))
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 TK = Action::TK_Declaration;
595 else
596 TK = Action::TK_Reference;
Steve Naroff08d92e42007-09-15 18:49:24 +0000597 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 return false;
599}
600
601
602/// ParseStructUnionSpecifier
603/// struct-or-union-specifier: [C99 6.7.2.1]
604/// struct-or-union identifier[opt] '{' struct-contents '}'
605/// struct-or-union identifier
606/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
607/// '}' attributes[opt]
608/// [GNU] struct-or-union attributes[opt] identifier
609/// struct-or-union:
610/// 'struct'
611/// 'union'
612///
613void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +0000614 assert((Tok.is(tok::kw_struct) || Tok.is(tok::kw_union)) &&
615 "Not a struct/union specifier");
Reid Spencer5f016e22007-07-11 17:01:13 +0000616 DeclSpec::TST TagType =
Chris Lattner04d66662007-10-09 17:33:22 +0000617 Tok.is(tok::kw_union) ? DeclSpec::TST_union : DeclSpec::TST_struct;
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 SourceLocation StartLoc = ConsumeToken();
619
620 // Parse the tag portion of this.
621 DeclTy *TagDecl;
622 if (ParseTag(TagDecl, TagType, StartLoc))
623 return;
624
625 // If there is a body, parse it and inform the actions module.
Chris Lattner04d66662007-10-09 17:33:22 +0000626 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +0000627 ParseStructUnionBody(StartLoc, TagType, TagDecl);
628
629 const char *PrevSpec = 0;
630 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
631 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
632}
633
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000634/// ParseStructDeclaration - Parse a struct declaration without the terminating
635/// semicolon.
636///
Reid Spencer5f016e22007-07-11 17:01:13 +0000637/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000638/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000639/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000640/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000641/// struct-declarator-list:
642/// struct-declarator
643/// struct-declarator-list ',' struct-declarator
644/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
645/// struct-declarator:
646/// declarator
647/// [GNU] declarator attributes[opt]
648/// declarator[opt] ':' constant-expression
649/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
650///
Steve Naroff28a7ca82007-08-20 22:28:22 +0000651void Parser::ParseStructDeclaration(DeclTy *TagDecl,
Steve Naroff4e6526b2007-08-28 16:31:47 +0000652 llvm::SmallVectorImpl<DeclTy*> &FieldDecls) {
Steve Naroff28a7ca82007-08-20 22:28:22 +0000653 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattner04d66662007-10-09 17:33:22 +0000654 if (Tok.is(tok::kw___extension__))
Steve Naroff28a7ca82007-08-20 22:28:22 +0000655 ConsumeToken();
656
657 // Parse the common specifier-qualifiers-list piece.
658 DeclSpec DS;
659 SourceLocation SpecQualLoc = Tok.getLocation();
660 ParseSpecifierQualifierList(DS);
661 // TODO: Does specifier-qualifier list correctly check that *something* is
662 // specified?
663
664 // If there are no declarators, issue a warning.
Chris Lattner04d66662007-10-09 17:33:22 +0000665 if (Tok.is(tok::semi)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +0000666 Diag(SpecQualLoc, diag::w_no_declarators);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000667 return;
668 }
669
670 // Read struct-declarators until we find the semicolon.
671 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
672
673 while (1) {
674 /// struct-declarator: declarator
675 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +0000676 if (Tok.isNot(tok::colon))
Steve Naroff28a7ca82007-08-20 22:28:22 +0000677 ParseDeclarator(DeclaratorInfo);
678
679 ExprTy *BitfieldSize = 0;
Chris Lattner04d66662007-10-09 17:33:22 +0000680 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +0000681 ConsumeToken();
682 ExprResult Res = ParseConstantExpression();
683 if (Res.isInvalid) {
684 SkipUntil(tok::semi, true, true);
685 } else {
686 BitfieldSize = Res.Val;
687 }
688 }
689
690 // If attributes exist after the declarator, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000691 if (Tok.is(tok::kw___attribute))
Steve Naroff28a7ca82007-08-20 22:28:22 +0000692 DeclaratorInfo.AddAttributes(ParseAttributes());
693
694 // Install the declarator into the current TagDecl.
Steve Naroff08d92e42007-09-15 18:49:24 +0000695 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl, SpecQualLoc,
Steve Naroff28a7ca82007-08-20 22:28:22 +0000696 DeclaratorInfo, BitfieldSize);
697 FieldDecls.push_back(Field);
698
699 // If we don't have a comma, it is either the end of the list (a ';')
700 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000701 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000702 return;
Steve Naroff28a7ca82007-08-20 22:28:22 +0000703
704 // Consume the comma.
705 ConsumeToken();
706
707 // Parse the next declarator.
708 DeclaratorInfo.clear();
709
710 // Attributes are only allowed on the second declarator.
Chris Lattner04d66662007-10-09 17:33:22 +0000711 if (Tok.is(tok::kw___attribute))
Steve Naroff28a7ca82007-08-20 22:28:22 +0000712 DeclaratorInfo.AddAttributes(ParseAttributes());
713 }
Steve Naroff28a7ca82007-08-20 22:28:22 +0000714}
715
716/// ParseStructUnionBody
717/// struct-contents:
718/// struct-declaration-list
719/// [EXT] empty
720/// [GNU] "struct-declaration-list" without terminatoring ';'
721/// struct-declaration-list:
722/// struct-declaration
723/// struct-declaration-list struct-declaration
724/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
725///
Reid Spencer5f016e22007-07-11 17:01:13 +0000726void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
727 unsigned TagType, DeclTy *TagDecl) {
728 SourceLocation LBraceLoc = ConsumeBrace();
729
730 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
731 // C++.
Chris Lattner04d66662007-10-09 17:33:22 +0000732 if (Tok.is(tok::r_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +0000733 Diag(Tok, diag::ext_empty_struct_union_enum,
734 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
735
736 llvm::SmallVector<DeclTy*, 32> FieldDecls;
737
738 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +0000739 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000740 // Each iteration of this loop reads one struct-declaration.
741
742 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +0000743 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000744 Diag(Tok, diag::ext_extra_struct_semi);
745 ConsumeToken();
746 continue;
747 }
Steve Naroff28a7ca82007-08-20 22:28:22 +0000748 ParseStructDeclaration(TagDecl, FieldDecls);
Reid Spencer5f016e22007-07-11 17:01:13 +0000749
Chris Lattner04d66662007-10-09 17:33:22 +0000750 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +0000752 } else if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
754 break;
755 } else {
756 Diag(Tok, diag::err_expected_semi_decl_list);
757 // Skip to end of block or statement
758 SkipUntil(tok::r_brace, true, true);
759 }
760 }
761
Steve Naroff60fccee2007-10-29 21:38:07 +0000762 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000763
Fariborz Jahanian9d048ff2007-09-29 00:54:24 +0000764 Actions.ActOnFields(CurScope,
Steve Naroff60fccee2007-10-29 21:38:07 +0000765 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
766 LBraceLoc, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000767
768 AttributeList *AttrList = 0;
769 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000770 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000771 AttrList = ParseAttributes(); // FIXME: where should I put them?
772}
773
774
775/// ParseEnumSpecifier
776/// enum-specifier: [C99 6.7.2.2]
777/// 'enum' identifier[opt] '{' enumerator-list '}'
778/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
779/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
780/// '}' attributes[opt]
781/// 'enum' identifier
782/// [GNU] 'enum' attributes[opt] identifier
783void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +0000784 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Reid Spencer5f016e22007-07-11 17:01:13 +0000785 SourceLocation StartLoc = ConsumeToken();
786
787 // Parse the tag portion of this.
788 DeclTy *TagDecl;
789 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
790 return;
791
Chris Lattner04d66662007-10-09 17:33:22 +0000792 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 ParseEnumBody(StartLoc, TagDecl);
794
795 // TODO: semantic analysis on the declspec for enums.
796 const char *PrevSpec = 0;
797 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
798 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
799}
800
801/// ParseEnumBody - Parse a {} enclosed enumerator-list.
802/// enumerator-list:
803/// enumerator
804/// enumerator-list ',' enumerator
805/// enumerator:
806/// enumeration-constant
807/// enumeration-constant '=' constant-expression
808/// enumeration-constant:
809/// identifier
810///
811void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
812 SourceLocation LBraceLoc = ConsumeBrace();
813
Chris Lattner7946dd32007-08-27 17:24:30 +0000814 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +0000815 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Reid Spencer5f016e22007-07-11 17:01:13 +0000816 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
817
818 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
819
820 DeclTy *LastEnumConstDecl = 0;
821
822 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +0000823 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000824 IdentifierInfo *Ident = Tok.getIdentifierInfo();
825 SourceLocation IdentLoc = ConsumeToken();
826
827 SourceLocation EqualLoc;
828 ExprTy *AssignedVal = 0;
Chris Lattner04d66662007-10-09 17:33:22 +0000829 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000830 EqualLoc = ConsumeToken();
831 ExprResult Res = ParseConstantExpression();
832 if (Res.isInvalid)
833 SkipUntil(tok::comma, tok::r_brace, true, true);
834 else
835 AssignedVal = Res.Val;
836 }
837
838 // Install the enumerator constant into EnumDecl.
Steve Naroff08d92e42007-09-15 18:49:24 +0000839 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +0000840 LastEnumConstDecl,
841 IdentLoc, Ident,
842 EqualLoc, AssignedVal);
843 EnumConstantDecls.push_back(EnumConstDecl);
844 LastEnumConstDecl = EnumConstDecl;
845
Chris Lattner04d66662007-10-09 17:33:22 +0000846 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000847 break;
848 SourceLocation CommaLoc = ConsumeToken();
849
Chris Lattner04d66662007-10-09 17:33:22 +0000850 if (Tok.isNot(tok::identifier) && !getLang().C99)
Reid Spencer5f016e22007-07-11 17:01:13 +0000851 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
852 }
853
854 // Eat the }.
855 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
856
Steve Naroff08d92e42007-09-15 18:49:24 +0000857 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Reid Spencer5f016e22007-07-11 17:01:13 +0000858 EnumConstantDecls.size());
859
860 DeclTy *AttrList = 0;
861 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000862 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000863 AttrList = ParseAttributes(); // FIXME: where do they do?
864}
865
866/// isTypeSpecifierQualifier - Return true if the current token could be the
867/// start of a specifier-qualifier-list.
868bool Parser::isTypeSpecifierQualifier() const {
869 switch (Tok.getKind()) {
870 default: return false;
871 // GNU attributes support.
872 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +0000873 // GNU typeof support.
874 case tok::kw_typeof:
875
Reid Spencer5f016e22007-07-11 17:01:13 +0000876 // type-specifiers
877 case tok::kw_short:
878 case tok::kw_long:
879 case tok::kw_signed:
880 case tok::kw_unsigned:
881 case tok::kw__Complex:
882 case tok::kw__Imaginary:
883 case tok::kw_void:
884 case tok::kw_char:
885 case tok::kw_int:
886 case tok::kw_float:
887 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +0000888 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 case tok::kw__Bool:
890 case tok::kw__Decimal32:
891 case tok::kw__Decimal64:
892 case tok::kw__Decimal128:
893
894 // struct-or-union-specifier
895 case tok::kw_struct:
896 case tok::kw_union:
897 // enum-specifier
898 case tok::kw_enum:
899
900 // type-qualifier
901 case tok::kw_const:
902 case tok::kw_volatile:
903 case tok::kw_restrict:
904 return true;
905
906 // typedef-name
907 case tok::identifier:
908 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000909 }
910}
911
912/// isDeclarationSpecifier() - Return true if the current token is part of a
913/// declaration specifier.
914bool Parser::isDeclarationSpecifier() const {
915 switch (Tok.getKind()) {
916 default: return false;
917 // storage-class-specifier
918 case tok::kw_typedef:
919 case tok::kw_extern:
920 case tok::kw_static:
921 case tok::kw_auto:
922 case tok::kw_register:
923 case tok::kw___thread:
924
925 // type-specifiers
926 case tok::kw_short:
927 case tok::kw_long:
928 case tok::kw_signed:
929 case tok::kw_unsigned:
930 case tok::kw__Complex:
931 case tok::kw__Imaginary:
932 case tok::kw_void:
933 case tok::kw_char:
934 case tok::kw_int:
935 case tok::kw_float:
936 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +0000937 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +0000938 case tok::kw__Bool:
939 case tok::kw__Decimal32:
940 case tok::kw__Decimal64:
941 case tok::kw__Decimal128:
942
943 // struct-or-union-specifier
944 case tok::kw_struct:
945 case tok::kw_union:
946 // enum-specifier
947 case tok::kw_enum:
948
949 // type-qualifier
950 case tok::kw_const:
951 case tok::kw_volatile:
952 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +0000953
Reid Spencer5f016e22007-07-11 17:01:13 +0000954 // function-specifier
955 case tok::kw_inline:
Chris Lattnerd6c7c182007-08-09 16:40:21 +0000956
Chris Lattner1ef08762007-08-09 17:01:07 +0000957 // GNU typeof support.
958 case tok::kw_typeof:
959
960 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +0000961 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 return true;
963
964 // typedef-name
965 case tok::identifier:
966 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000967 }
968}
969
970
971/// ParseTypeQualifierListOpt
972/// type-qualifier-list: [C99 6.7.5]
973/// type-qualifier
974/// [GNU] attributes
975/// type-qualifier-list type-qualifier
976/// [GNU] type-qualifier-list attributes
977///
978void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
979 while (1) {
980 int isInvalid = false;
981 const char *PrevSpec = 0;
982 SourceLocation Loc = Tok.getLocation();
983
984 switch (Tok.getKind()) {
985 default:
986 // If this is not a type-qualifier token, we're done reading type
987 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000988 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +0000989 return;
990 case tok::kw_const:
991 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
992 getLang())*2;
993 break;
994 case tok::kw_volatile:
995 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
996 getLang())*2;
997 break;
998 case tok::kw_restrict:
999 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1000 getLang())*2;
1001 break;
1002 case tok::kw___attribute:
1003 DS.AddAttributes(ParseAttributes());
1004 continue; // do *not* consume the next token!
1005 }
1006
1007 // If the specifier combination wasn't legal, issue a diagnostic.
1008 if (isInvalid) {
1009 assert(PrevSpec && "Method did not return previous specifier!");
1010 if (isInvalid == 1) // Error.
1011 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1012 else // extwarn.
1013 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1014 }
1015 ConsumeToken();
1016 }
1017}
1018
1019
1020/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1021///
1022void Parser::ParseDeclarator(Declarator &D) {
1023 /// This implements the 'declarator' production in the C grammar, then checks
1024 /// for well-formedness and issues diagnostics.
1025 ParseDeclaratorInternal(D);
1026
1027 // TODO: validate D.
1028
1029}
1030
1031/// ParseDeclaratorInternal
1032/// declarator: [C99 6.7.5]
1033/// pointer[opt] direct-declarator
1034/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1035/// [GNU] '&' restrict[opt] attributes[opt] declarator
1036///
1037/// pointer: [C99 6.7.5]
1038/// '*' type-qualifier-list[opt]
1039/// '*' type-qualifier-list[opt] pointer
1040///
1041void Parser::ParseDeclaratorInternal(Declarator &D) {
1042 tok::TokenKind Kind = Tok.getKind();
1043
1044 // Not a pointer or C++ reference.
1045 if (Kind != tok::star && !(Kind == tok::amp && getLang().CPlusPlus))
1046 return ParseDirectDeclarator(D);
1047
1048 // Otherwise, '*' -> pointer or '&' -> reference.
1049 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1050
1051 if (Kind == tok::star) {
1052 // Is a pointer
1053 DeclSpec DS;
1054
1055 ParseTypeQualifierListOpt(DS);
1056
1057 // Recursively parse the declarator.
1058 ParseDeclaratorInternal(D);
1059
1060 // Remember that we parsed a pointer type, and remember the type-quals.
1061 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc));
1062 } else {
1063 // Is a reference
1064 DeclSpec DS;
1065
1066 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1067 // cv-qualifiers are introduced through the use of a typedef or of a
1068 // template type argument, in which case the cv-qualifiers are ignored.
1069 //
1070 // [GNU] Retricted references are allowed.
1071 // [GNU] Attributes on references are allowed.
1072 ParseTypeQualifierListOpt(DS);
1073
1074 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1075 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1076 Diag(DS.getConstSpecLoc(),
1077 diag::err_invalid_reference_qualifier_application,
1078 "const");
1079 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1080 Diag(DS.getVolatileSpecLoc(),
1081 diag::err_invalid_reference_qualifier_application,
1082 "volatile");
1083 }
1084
1085 // Recursively parse the declarator.
1086 ParseDeclaratorInternal(D);
1087
1088 // Remember that we parsed a reference type. It doesn't have type-quals.
1089 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc));
1090 }
1091}
1092
1093/// ParseDirectDeclarator
1094/// direct-declarator: [C99 6.7.5]
1095/// identifier
1096/// '(' declarator ')'
1097/// [GNU] '(' attributes declarator ')'
1098/// [C90] direct-declarator '[' constant-expression[opt] ']'
1099/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1100/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1101/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1102/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1103/// direct-declarator '(' parameter-type-list ')'
1104/// direct-declarator '(' identifier-list[opt] ')'
1105/// [GNU] direct-declarator '(' parameter-forward-declarations
1106/// parameter-type-list[opt] ')'
1107///
1108void Parser::ParseDirectDeclarator(Declarator &D) {
1109 // Parse the first direct-declarator seen.
Chris Lattner04d66662007-10-09 17:33:22 +00001110 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001111 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1112 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1113 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001114 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001115 // direct-declarator: '(' declarator ')'
1116 // direct-declarator: '(' attributes declarator ')'
1117 // Example: 'char (*X)' or 'int (*XX)(void)'
1118 ParseParenDeclarator(D);
1119 } else if (D.mayOmitIdentifier()) {
1120 // This could be something simple like "int" (in which case the declarator
1121 // portion is empty), if an abstract-declarator is allowed.
1122 D.SetIdentifier(0, Tok.getLocation());
1123 } else {
1124 // Expected identifier or '('.
1125 Diag(Tok, diag::err_expected_ident_lparen);
1126 D.SetIdentifier(0, Tok.getLocation());
1127 }
1128
1129 assert(D.isPastIdentifier() &&
1130 "Haven't past the location of the identifier yet?");
1131
1132 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00001133 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 ParseParenDeclarator(D);
Chris Lattner04d66662007-10-09 17:33:22 +00001135 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001136 ParseBracketDeclarator(D);
1137 } else {
1138 break;
1139 }
1140 }
1141}
1142
1143/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
1144/// either be before the identifier (in which case these are just grouping
1145/// parens for precedence) or it may be after the identifier, in which case
1146/// these are function arguments.
1147///
1148/// This method also handles this portion of the grammar:
1149/// parameter-type-list: [C99 6.7.5]
1150/// parameter-list
1151/// parameter-list ',' '...'
1152///
1153/// parameter-list: [C99 6.7.5]
1154/// parameter-declaration
1155/// parameter-list ',' parameter-declaration
1156///
1157/// parameter-declaration: [C99 6.7.5]
1158/// declaration-specifiers declarator
1159/// [GNU] declaration-specifiers declarator attributes
1160/// declaration-specifiers abstract-declarator[opt]
1161/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1162///
1163/// identifier-list: [C99 6.7.5]
1164/// identifier
1165/// identifier-list ',' identifier
1166///
1167void Parser::ParseParenDeclarator(Declarator &D) {
1168 SourceLocation StartLoc = ConsumeParen();
1169
1170 // If we haven't past the identifier yet (or where the identifier would be
1171 // stored, if this is an abstract declarator), then this is probably just
1172 // grouping parens.
1173 if (!D.isPastIdentifier()) {
1174 // Okay, this is probably a grouping paren. However, if this could be an
1175 // abstract-declarator, then this could also be the start of function
1176 // arguments (consider 'void()').
1177 bool isGrouping;
1178
1179 if (!D.mayOmitIdentifier()) {
1180 // If this can't be an abstract-declarator, this *must* be a grouping
1181 // paren, because we haven't seen the identifier yet.
1182 isGrouping = true;
Chris Lattner04d66662007-10-09 17:33:22 +00001183 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Reid Spencer5f016e22007-07-11 17:01:13 +00001184 isDeclarationSpecifier()) { // 'int(int)' is a function.
1185 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1186 // considered to be a type, not a K&R identifier-list.
1187 isGrouping = false;
1188 } else {
1189 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1190 isGrouping = true;
1191 }
1192
1193 // If this is a grouping paren, handle:
1194 // direct-declarator: '(' declarator ')'
1195 // direct-declarator: '(' attributes declarator ')'
1196 if (isGrouping) {
Chris Lattner04d66662007-10-09 17:33:22 +00001197 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001198 D.AddAttributes(ParseAttributes());
1199
1200 ParseDeclaratorInternal(D);
1201 // Match the ')'.
1202 MatchRHSPunctuation(tok::r_paren, StartLoc);
1203 return;
1204 }
1205
1206 // Okay, if this wasn't a grouping paren, it must be the start of a function
1207 // argument list. Recognize that this declarator will never have an
1208 // identifier (and remember where it would have been), then fall through to
1209 // the handling of argument lists.
1210 D.SetIdentifier(0, Tok.getLocation());
1211 }
1212
1213 // Okay, this is the parameter list of a function definition, or it is an
1214 // identifier list of a K&R-style function.
1215 bool IsVariadic;
1216 bool HasPrototype;
1217 bool ErrorEmitted = false;
1218
1219 // Build up an array of information about the parsed arguments.
1220 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1221 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1222
Chris Lattner04d66662007-10-09 17:33:22 +00001223 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001224 // int() -> no prototype, no '...'.
1225 IsVariadic = false;
1226 HasPrototype = false;
Chris Lattner04d66662007-10-09 17:33:22 +00001227 } else if (Tok.is(tok::identifier) &&
Reid Spencer5f016e22007-07-11 17:01:13 +00001228 // K&R identifier lists can't have typedefs as identifiers, per
1229 // C99 6.7.5.3p11.
1230 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1231 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1232 // normal declarators, not for abstract-declarators.
1233 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
1234
1235 // If there was no identifier specified, either we are in an
1236 // abstract-declarator, or we are in a parameter declarator which was found
1237 // to be abstract. In abstract-declarators, identifier lists are not valid,
1238 // diagnose this.
1239 if (!D.getIdentifier())
1240 Diag(Tok, diag::ext_ident_list_in_param);
1241
1242 // Remember this identifier in ParamInfo.
1243 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1244 Tok.getLocation(), 0));
1245
1246 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001247 while (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 // Eat the comma.
1249 ConsumeToken();
1250
Chris Lattner04d66662007-10-09 17:33:22 +00001251 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001252 Diag(Tok, diag::err_expected_ident);
1253 ErrorEmitted = true;
1254 break;
1255 }
1256
1257 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
1258
1259 // Verify that the argument identifier has not already been mentioned.
1260 if (!ParamsSoFar.insert(ParmII)) {
1261 Diag(Tok.getLocation(), diag::err_param_redefinition,ParmII->getName());
1262 ParmII = 0;
1263 }
1264
1265 // Remember this identifier in ParamInfo.
1266 if (ParmII)
1267 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1268 Tok.getLocation(), 0));
1269
1270 // Eat the identifier.
1271 ConsumeToken();
1272 }
1273
1274 // K&R 'prototype'.
1275 IsVariadic = false;
1276 HasPrototype = false;
1277 } else {
1278 // Finally, a normal, non-empty parameter type list.
1279
1280 // Enter function-declaration scope, limiting any declarators for struct
1281 // tags to the function prototype scope.
1282 // FIXME: is this needed?
Chris Lattner31e05722007-08-26 06:24:45 +00001283 EnterScope(Scope::DeclScope);
Reid Spencer5f016e22007-07-11 17:01:13 +00001284
1285 IsVariadic = false;
1286 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00001287 if (Tok.is(tok::ellipsis)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001288 IsVariadic = true;
1289
1290 // Check to see if this is "void(...)" which is not allowed.
1291 if (ParamInfo.empty()) {
1292 // Otherwise, parse parameter type list. If it starts with an
1293 // ellipsis, diagnose the malformed function.
1294 Diag(Tok, diag::err_ellipsis_first_arg);
1295 IsVariadic = false; // Treat this like 'void()'.
1296 }
1297
1298 // Consume the ellipsis.
1299 ConsumeToken();
1300 break;
1301 }
1302
1303 // Parse the declaration-specifiers.
1304 DeclSpec DS;
1305 ParseDeclarationSpecifiers(DS);
1306
1307 // Parse the declarator. This is "PrototypeContext", because we must
1308 // accept either 'declarator' or 'abstract-declarator' here.
1309 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1310 ParseDeclarator(ParmDecl);
1311
1312 // Parse GNU attributes, if present.
Chris Lattner04d66662007-10-09 17:33:22 +00001313 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001314 ParmDecl.AddAttributes(ParseAttributes());
1315
1316 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
1317 // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted.
1318 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1319 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
1320 Diag(DS.getStorageClassSpecLoc(),
1321 diag::err_invalid_storage_class_in_func_decl);
1322 DS.ClearStorageClassSpecs();
1323 }
1324 if (DS.isThreadSpecified()) {
1325 Diag(DS.getThreadSpecLoc(),
1326 diag::err_invalid_storage_class_in_func_decl);
1327 DS.ClearStorageClassSpecs();
1328 }
1329
1330 // Inform the actions module about the parameter declarator, so it gets
1331 // added to the current scope.
1332 Action::TypeResult ParamTy =
Steve Naroff08d92e42007-09-15 18:49:24 +00001333 Actions.ActOnParamDeclaratorType(CurScope, ParmDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001334
1335 // Remember this parsed parameter in ParamInfo.
1336 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1337
1338 // Verify that the argument identifier has not already been mentioned.
1339 if (ParmII && !ParamsSoFar.insert(ParmII)) {
1340 Diag(ParmDecl.getIdentifierLoc(), diag::err_param_redefinition,
1341 ParmII->getName());
1342 ParmII = 0;
1343 }
1344
Steve Naroffe1223f72007-08-28 03:03:08 +00001345 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Nate Begeman1b4e2512007-11-13 22:14:47 +00001346 ParmDecl.getIdentifierLoc(), ParamTy.Val, ParmDecl.getInvalidType(),
1347 ParmDecl.getDeclSpec().getAttributes()));
1348
1349 // Ownership of DeclSpec has been handed off to ParamInfo.
1350 DS.clearAttributes();
Reid Spencer5f016e22007-07-11 17:01:13 +00001351
1352 // If the next token is a comma, consume it and keep reading arguments.
Chris Lattner04d66662007-10-09 17:33:22 +00001353 if (Tok.isNot(tok::comma)) break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001354
1355 // Consume the comma.
1356 ConsumeToken();
1357 }
1358
1359 HasPrototype = true;
1360
1361 // Leave prototype scope.
1362 ExitScope();
1363 }
1364
1365 // Remember that we parsed a function type, and remember the attributes.
1366 if (!ErrorEmitted)
1367 D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic,
1368 &ParamInfo[0], ParamInfo.size(),
1369 StartLoc));
1370
1371 // If we have the closing ')', eat it and we're done.
Chris Lattner04d66662007-10-09 17:33:22 +00001372 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001373 ConsumeParen();
1374 } else {
1375 // If an error happened earlier parsing something else in the proto, don't
1376 // issue another error.
1377 if (!ErrorEmitted)
1378 Diag(Tok, diag::err_expected_rparen);
1379 SkipUntil(tok::r_paren);
1380 }
1381}
1382
1383
1384/// [C90] direct-declarator '[' constant-expression[opt] ']'
1385/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1386/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1387/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1388/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1389void Parser::ParseBracketDeclarator(Declarator &D) {
1390 SourceLocation StartLoc = ConsumeBracket();
1391
1392 // If valid, this location is the position where we read the 'static' keyword.
1393 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00001394 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00001395 StaticLoc = ConsumeToken();
1396
1397 // If there is a type-qualifier-list, read it now.
1398 DeclSpec DS;
1399 ParseTypeQualifierListOpt(DS);
1400
1401 // If we haven't already read 'static', check to see if there is one after the
1402 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001403 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00001404 StaticLoc = ConsumeToken();
1405
1406 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1407 bool isStar = false;
1408 ExprResult NumElements(false);
Chris Lattner04d66662007-10-09 17:33:22 +00001409 if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001410 // Remember the '*' token, in case we have to un-get it.
Chris Lattnerd2177732007-07-20 16:59:19 +00001411 Token StarTok = Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001412 ConsumeToken();
1413
1414 // Check that the ']' token is present to avoid incorrectly parsing
1415 // expressions starting with '*' as [*].
Chris Lattner04d66662007-10-09 17:33:22 +00001416 if (Tok.is(tok::r_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001417 if (StaticLoc.isValid())
1418 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1419 StaticLoc = SourceLocation(); // Drop the static.
1420 isStar = true;
1421 } else {
1422 // Otherwise, the * must have been some expression (such as '*ptr') that
1423 // started an assignment-expr. We already consumed the token, but now we
1424 // need to reparse it. This handles cases like 'X[*p + 4]'
1425 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
1426 }
Chris Lattner04d66662007-10-09 17:33:22 +00001427 } else if (Tok.isNot(tok::r_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001428 // Parse the assignment-expression now.
1429 NumElements = ParseAssignmentExpression();
1430 }
1431
1432 // If there was an error parsing the assignment-expression, recover.
1433 if (NumElements.isInvalid) {
1434 // If the expression was invalid, skip it.
1435 SkipUntil(tok::r_square);
1436 return;
1437 }
1438
1439 MatchRHSPunctuation(tok::r_square, StartLoc);
1440
1441 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1442 // it was not a constant expression.
1443 if (!getLang().C99) {
1444 // TODO: check C90 array constant exprness.
1445 if (isStar || StaticLoc.isValid() ||
1446 0/*TODO: NumElts is not a C90 constantexpr */)
1447 Diag(StartLoc, diag::ext_c99_array_usage);
1448 }
1449
1450 // Remember that we parsed a pointer type, and remember the type-quals.
1451 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1452 StaticLoc.isValid(), isStar,
1453 NumElements.Val, StartLoc));
1454}
1455
Steve Naroffd1861fd2007-07-31 12:34:36 +00001456/// [GNU] typeof-specifier:
1457/// typeof ( expressions )
1458/// typeof ( type-name )
1459///
1460void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00001461 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001462 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffd1861fd2007-07-31 12:34:36 +00001463 SourceLocation StartLoc = ConsumeToken();
1464
Chris Lattner04d66662007-10-09 17:33:22 +00001465 if (Tok.isNot(tok::l_paren)) {
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001466 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1467 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001468 }
1469 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1470
1471 if (isTypeSpecifierQualifier()) {
1472 TypeTy *Ty = ParseTypeName();
1473
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001474 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1475
Chris Lattner04d66662007-10-09 17:33:22 +00001476 if (Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001477 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001478 return;
1479 }
1480 RParenLoc = ConsumeParen();
1481 const char *PrevSpec = 0;
1482 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1483 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1484 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001485 } else { // we have an expression.
1486 ExprResult Result = ParseExpression();
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001487
Chris Lattner04d66662007-10-09 17:33:22 +00001488 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001489 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001490 return;
1491 }
1492 RParenLoc = ConsumeParen();
1493 const char *PrevSpec = 0;
1494 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1495 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1496 Result.Val))
1497 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001498 }
Steve Naroffd1861fd2007-07-31 12:34:36 +00001499}
1500