blob: b45ef47554a2dc8b99b8e11d344acb6ed9f39064 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
15#include "clang/Parse/DeclSpec.h"
Chris Lattnera7549902007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "llvm/ADT/SmallSet.h"
18using namespace clang;
19
20//===----------------------------------------------------------------------===//
21// C99 6.7: Declarations.
22//===----------------------------------------------------------------------===//
23
24/// ParseTypeName
25/// type-name: [C99 6.7.6]
26/// specifier-qualifier-list abstract-declarator[opt]
27Parser::TypeTy *Parser::ParseTypeName() {
28 // Parse the common declaration-specifiers piece.
29 DeclSpec DS;
30 ParseSpecifierQualifierList(DS);
31
32 // Parse the abstract-declarator, if present.
33 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
34 ParseDeclarator(DeclaratorInfo);
35
Steve Naroff0acc9c92007-09-15 18:49:24 +000036 return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
Chris Lattner4b009652007-07-25 00:24:17 +000037}
38
39/// ParseAttributes - Parse a non-empty attributes list.
40///
41/// [GNU] attributes:
42/// attribute
43/// attributes attribute
44///
45/// [GNU] attribute:
46/// '__attribute__' '(' '(' attribute-list ')' ')'
47///
48/// [GNU] attribute-list:
49/// attrib
50/// attribute_list ',' attrib
51///
52/// [GNU] attrib:
53/// empty
54/// attrib-name
55/// attrib-name '(' identifier ')'
56/// attrib-name '(' identifier ',' nonempty-expr-list ')'
57/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
58///
59/// [GNU] attrib-name:
60/// identifier
61/// typespec
62/// typequal
63/// storageclass
64///
65/// FIXME: The GCC grammar/code for this construct implies we need two
66/// token lookahead. Comment from gcc: "If they start with an identifier
67/// which is followed by a comma or close parenthesis, then the arguments
68/// start with that identifier; otherwise they are an expression list."
69///
70/// At the moment, I am not doing 2 token lookahead. I am also unaware of
71/// any attributes that don't work (based on my limited testing). Most
72/// attributes are very simple in practice. Until we find a bug, I don't see
73/// a pressing need to implement the 2 token lookahead.
74
75AttributeList *Parser::ParseAttributes() {
Chris Lattner34a01ad2007-10-09 17:33:22 +000076 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Chris Lattner4b009652007-07-25 00:24:17 +000077
78 AttributeList *CurrAttr = 0;
79
Chris Lattner34a01ad2007-10-09 17:33:22 +000080 while (Tok.is(tok::kw___attribute)) {
Chris Lattner4b009652007-07-25 00:24:17 +000081 ConsumeToken();
82 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
83 "attribute")) {
84 SkipUntil(tok::r_paren, true); // skip until ) or ;
85 return CurrAttr;
86 }
87 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
88 SkipUntil(tok::r_paren, true); // skip until ) or ;
89 return CurrAttr;
90 }
91 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner34a01ad2007-10-09 17:33:22 +000092 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
93 Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +000094
Chris Lattner34a01ad2007-10-09 17:33:22 +000095 if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +000096 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
97 ConsumeToken();
98 continue;
99 }
100 // we have an identifier or declaration specifier (const, int, etc.)
101 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
102 SourceLocation AttrNameLoc = ConsumeToken();
103
104 // check if we have a "paramterized" attribute
Chris Lattner34a01ad2007-10-09 17:33:22 +0000105 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000106 ConsumeParen(); // ignore the left paren loc for now
107
Chris Lattner34a01ad2007-10-09 17:33:22 +0000108 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000109 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
110 SourceLocation ParmLoc = ConsumeToken();
111
Chris Lattner34a01ad2007-10-09 17:33:22 +0000112 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000113 // __attribute__(( mode(byte) ))
114 ConsumeParen(); // ignore the right paren loc for now
115 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
116 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000117 } else if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000118 ConsumeToken();
119 // __attribute__(( format(printf, 1, 2) ))
120 llvm::SmallVector<ExprTy*, 8> ArgExprs;
121 bool ArgExprsOk = true;
122
123 // now parse the non-empty comma separated list of expressions
124 while (1) {
125 ExprResult ArgExpr = ParseAssignmentExpression();
126 if (ArgExpr.isInvalid) {
127 ArgExprsOk = false;
128 SkipUntil(tok::r_paren);
129 break;
130 } else {
131 ArgExprs.push_back(ArgExpr.Val);
132 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000133 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000134 break;
135 ConsumeToken(); // Eat the comma, move to the next argument
136 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000137 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000138 ConsumeParen(); // ignore the right paren loc for now
139 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
140 ParmLoc, &ArgExprs[0], ArgExprs.size(), CurrAttr);
141 }
142 }
143 } else { // not an identifier
144 // parse a possibly empty comma separated list of expressions
Chris Lattner34a01ad2007-10-09 17:33:22 +0000145 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000146 // __attribute__(( nonnull() ))
147 ConsumeParen(); // ignore the right paren loc for now
148 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
149 0, SourceLocation(), 0, 0, CurrAttr);
150 } else {
151 // __attribute__(( aligned(16) ))
152 llvm::SmallVector<ExprTy*, 8> ArgExprs;
153 bool ArgExprsOk = true;
154
155 // now parse the list of expressions
156 while (1) {
157 ExprResult ArgExpr = ParseAssignmentExpression();
158 if (ArgExpr.isInvalid) {
159 ArgExprsOk = false;
160 SkipUntil(tok::r_paren);
161 break;
162 } else {
163 ArgExprs.push_back(ArgExpr.Val);
164 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000165 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000166 break;
167 ConsumeToken(); // Eat the comma, move to the next argument
168 }
169 // Match the ')'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000170 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000171 ConsumeParen(); // ignore the right paren loc for now
172 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
173 SourceLocation(), &ArgExprs[0], ArgExprs.size(),
174 CurrAttr);
175 }
176 }
177 }
178 } else {
179 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
180 0, SourceLocation(), 0, 0, CurrAttr);
181 }
182 }
183 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
184 SkipUntil(tok::r_paren, false);
185 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
186 SkipUntil(tok::r_paren, false);
187 }
188 return CurrAttr;
189}
190
191/// ParseDeclaration - Parse a full 'declaration', which consists of
192/// declaration-specifiers, some number of declarators, and a semicolon.
193/// 'Context' should be a Declarator::TheContext value.
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000194///
195/// declaration: [C99 6.7]
196/// block-declaration ->
197/// simple-declaration
198/// others [FIXME]
199/// [C++] namespace-definition
200/// others... [FIXME]
201///
Chris Lattner4b009652007-07-25 00:24:17 +0000202Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000203 switch (Tok.getKind()) {
204 case tok::kw_namespace:
205 return ParseNamespace(Context);
206 default:
207 return ParseSimpleDeclaration(Context);
208 }
209}
210
211/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
212/// declaration-specifiers init-declarator-list[opt] ';'
213///[C90/C++]init-declarator-list ';' [TODO]
214/// [OMP] threadprivate-directive [TODO]
215Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Chris Lattner4b009652007-07-25 00:24:17 +0000216 // Parse the common declaration-specifiers piece.
217 DeclSpec DS;
218 ParseDeclarationSpecifiers(DS);
219
220 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
221 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000222 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000223 ConsumeToken();
224 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
225 }
226
227 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
228 ParseDeclarator(DeclaratorInfo);
229
230 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
231}
232
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000233
Chris Lattner4b009652007-07-25 00:24:17 +0000234/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
235/// parsing 'declaration-specifiers declarator'. This method is split out this
236/// way to handle the ambiguity between top-level function-definitions and
237/// declarations.
238///
Chris Lattner4b009652007-07-25 00:24:17 +0000239/// init-declarator-list: [C99 6.7]
240/// init-declarator
241/// init-declarator-list ',' init-declarator
242/// init-declarator: [C99 6.7]
243/// declarator
244/// declarator '=' initializer
245/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
246/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
247///
248Parser::DeclTy *Parser::
249ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
250
251 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
252 // that they can be chained properly if the actions want this.
253 Parser::DeclTy *LastDeclInGroup = 0;
254
255 // At this point, we know that it is not a function definition. Parse the
256 // rest of the init-declarator-list.
257 while (1) {
258 // If a simple-asm-expr is present, parse it.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000259 if (Tok.is(tok::kw_asm))
Chris Lattner4b009652007-07-25 00:24:17 +0000260 ParseSimpleAsm();
261
262 // If attributes are present, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000263 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000264 D.AddAttributes(ParseAttributes());
Steve Naroff6a0e2092007-09-12 14:07:44 +0000265
266 // Inform the current actions module that we just parsed this declarator.
267 // FIXME: pass asm & attributes.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000268 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Steve Naroff6a0e2092007-09-12 14:07:44 +0000269
Chris Lattner4b009652007-07-25 00:24:17 +0000270 // Parse declarator '=' initializer.
271 ExprResult Init;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000272 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000273 ConsumeToken();
274 Init = ParseInitializer();
275 if (Init.isInvalid) {
276 SkipUntil(tok::semi);
277 return 0;
278 }
Steve Naroff6a0e2092007-09-12 14:07:44 +0000279 Actions.AddInitializerToDecl(LastDeclInGroup, Init.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000280 }
281
Chris Lattner4b009652007-07-25 00:24:17 +0000282 // If we don't have a comma, it is either the end of the list (a ';') or an
283 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000284 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000285 break;
286
287 // Consume the comma.
288 ConsumeToken();
289
290 // Parse the next declarator.
291 D.clear();
292 ParseDeclarator(D);
293 }
294
Chris Lattner34a01ad2007-10-09 17:33:22 +0000295 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000296 ConsumeToken();
297 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
298 }
299
300 Diag(Tok, diag::err_parse_error);
301 // Skip to end of block or statement
Chris Lattnerf491b412007-08-21 18:36:18 +0000302 SkipUntil(tok::r_brace, true, true);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000303 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000304 ConsumeToken();
305 return 0;
306}
307
308/// ParseSpecifierQualifierList
309/// specifier-qualifier-list:
310/// type-specifier specifier-qualifier-list[opt]
311/// type-qualifier specifier-qualifier-list[opt]
312/// [GNU] attributes specifier-qualifier-list[opt]
313///
314void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
315 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
316 /// parse declaration-specifiers and complain about extra stuff.
317 ParseDeclarationSpecifiers(DS);
318
319 // Validate declspec for type-name.
320 unsigned Specs = DS.getParsedSpecifiers();
321 if (Specs == DeclSpec::PQ_None)
322 Diag(Tok, diag::err_typename_requires_specqual);
323
324 // Issue diagnostic and remove storage class if present.
325 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
326 if (DS.getStorageClassSpecLoc().isValid())
327 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
328 else
329 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
330 DS.ClearStorageClassSpecs();
331 }
332
333 // Issue diagnostic and remove function specfier if present.
334 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
335 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
336 DS.ClearFunctionSpecs();
337 }
338}
339
340/// ParseDeclarationSpecifiers
341/// declaration-specifiers: [C99 6.7]
342/// storage-class-specifier declaration-specifiers[opt]
343/// type-specifier declaration-specifiers[opt]
344/// type-qualifier declaration-specifiers[opt]
345/// [C99] function-specifier declaration-specifiers[opt]
346/// [GNU] attributes declaration-specifiers[opt]
347///
348/// storage-class-specifier: [C99 6.7.1]
349/// 'typedef'
350/// 'extern'
351/// 'static'
352/// 'auto'
353/// 'register'
354/// [GNU] '__thread'
355/// type-specifier: [C99 6.7.2]
356/// 'void'
357/// 'char'
358/// 'short'
359/// 'int'
360/// 'long'
361/// 'float'
362/// 'double'
363/// 'signed'
364/// 'unsigned'
365/// struct-or-union-specifier
366/// enum-specifier
367/// typedef-name
368/// [C++] 'bool'
369/// [C99] '_Bool'
370/// [C99] '_Complex'
371/// [C99] '_Imaginary' // Removed in TC2?
372/// [GNU] '_Decimal32'
373/// [GNU] '_Decimal64'
374/// [GNU] '_Decimal128'
Steve Naroff4c255ab2007-07-31 23:56:32 +0000375/// [GNU] typeof-specifier
Chris Lattner4b009652007-07-25 00:24:17 +0000376/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroffa8ee2262007-08-22 23:18:22 +0000377/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattner4b009652007-07-25 00:24:17 +0000378/// type-qualifier:
379/// 'const'
380/// 'volatile'
381/// [C99] 'restrict'
382/// function-specifier: [C99 6.7.4]
383/// [C99] 'inline'
384///
385void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
386 DS.Range.setBegin(Tok.getLocation());
387 while (1) {
388 int isInvalid = false;
389 const char *PrevSpec = 0;
390 SourceLocation Loc = Tok.getLocation();
391
392 switch (Tok.getKind()) {
393 // typedef-name
394 case tok::identifier:
395 // This identifier can only be a typedef name if we haven't already seen
396 // a type-specifier. Without this check we misparse:
397 // typedef int X; struct Y { short X; }; as 'short int'.
398 if (!DS.hasTypeSpecifier()) {
399 // It has to be available as a typedef too!
400 if (void *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(),
401 CurScope)) {
402 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
403 TypeRep);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000404 if (isInvalid)
405 break;
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000406 // FIXME: restrict this to "id" and ObjC classnames.
407 DS.Range.setEnd(Tok.getLocation());
408 ConsumeToken(); // The identifier
409 if (Tok.is(tok::less)) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000410 SourceLocation endProtoLoc;
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000411 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroffef20ed32007-10-30 02:23:23 +0000412 ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000413 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
414 new llvm::SmallVector<DeclTy *, 8>;
415 DS.setProtocolQualifiers(ProtocolDecl);
416 Actions.FindProtocolDeclaration(Loc,
417 &ProtocolRefs[0], ProtocolRefs.size(),
418 *ProtocolDecl);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000419 }
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000420 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000421 }
422 }
423 // FALL THROUGH.
424 default:
425 // If this is not a declaration specifier token, we're done reading decl
426 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000427 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000428 return;
429
430 // GNU attributes support.
431 case tok::kw___attribute:
432 DS.AddAttributes(ParseAttributes());
433 continue;
434
435 // storage-class-specifier
436 case tok::kw_typedef:
437 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
438 break;
439 case tok::kw_extern:
440 if (DS.isThreadSpecified())
441 Diag(Tok, diag::ext_thread_before, "extern");
442 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
443 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000444 case tok::kw___private_extern__:
445 // FIXME: Implement private extern.
446 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
447 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000448 case tok::kw_static:
449 if (DS.isThreadSpecified())
450 Diag(Tok, diag::ext_thread_before, "static");
451 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
452 break;
453 case tok::kw_auto:
454 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
455 break;
456 case tok::kw_register:
457 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
458 break;
459 case tok::kw___thread:
460 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
461 break;
462
463 // type-specifiers
464 case tok::kw_short:
465 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
466 break;
467 case tok::kw_long:
468 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
469 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
470 else
471 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
472 break;
473 case tok::kw_signed:
474 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
475 break;
476 case tok::kw_unsigned:
477 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
478 break;
479 case tok::kw__Complex:
480 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
481 break;
482 case tok::kw__Imaginary:
483 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
484 break;
485 case tok::kw_void:
486 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
487 break;
488 case tok::kw_char:
489 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
490 break;
491 case tok::kw_int:
492 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
493 break;
494 case tok::kw_float:
495 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
496 break;
497 case tok::kw_double:
498 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
499 break;
500 case tok::kw_bool: // [C++ 2.11p1]
501 case tok::kw__Bool:
502 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
503 break;
504 case tok::kw__Decimal32:
505 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
506 break;
507 case tok::kw__Decimal64:
508 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
509 break;
510 case tok::kw__Decimal128:
511 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
512 break;
513
514 case tok::kw_struct:
515 case tok::kw_union:
516 ParseStructUnionSpecifier(DS);
517 continue;
518 case tok::kw_enum:
519 ParseEnumSpecifier(DS);
520 continue;
521
Steve Naroff7cbb1462007-07-31 12:34:36 +0000522 // GNU typeof support.
523 case tok::kw_typeof:
524 ParseTypeofSpecifier(DS);
525 continue;
526
Chris Lattner4b009652007-07-25 00:24:17 +0000527 // type-qualifier
528 case tok::kw_const:
529 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
530 getLang())*2;
531 break;
532 case tok::kw_volatile:
533 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
534 getLang())*2;
535 break;
536 case tok::kw_restrict:
537 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
538 getLang())*2;
539 break;
540
541 // function-specifier
542 case tok::kw_inline:
543 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
544 break;
545 }
546 // If the specifier combination wasn't legal, issue a diagnostic.
547 if (isInvalid) {
548 assert(PrevSpec && "Method did not return previous specifier!");
549 if (isInvalid == 1) // Error.
550 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
551 else // extwarn.
552 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
553 }
554 DS.Range.setEnd(Tok.getLocation());
555 ConsumeToken();
556 }
557}
558
559/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
560/// the first token has already been read and has been turned into an instance
561/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
562/// otherwise it returns false and fills in Decl.
563bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
564 AttributeList *Attr = 0;
565 // If attributes exist after tag, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000566 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000567 Attr = ParseAttributes();
568
569 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000570 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000571 Diag(Tok, diag::err_expected_ident_lbrace);
572
573 // Skip the rest of this declarator, up until the comma or semicolon.
574 SkipUntil(tok::comma, true);
575 return true;
576 }
577
578 // If an identifier is present, consume and remember it.
579 IdentifierInfo *Name = 0;
580 SourceLocation NameLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000581 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000582 Name = Tok.getIdentifierInfo();
583 NameLoc = ConsumeToken();
584 }
585
586 // There are three options here. If we have 'struct foo;', then this is a
587 // forward declaration. If we have 'struct foo {...' then this is a
588 // definition. Otherwise we have something like 'struct foo xyz', a reference.
589 //
590 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
591 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
592 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
593 //
594 Action::TagKind TK;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000595 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000596 TK = Action::TK_Definition;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000597 else if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000598 TK = Action::TK_Declaration;
599 else
600 TK = Action::TK_Reference;
Steve Naroff0acc9c92007-09-15 18:49:24 +0000601 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Chris Lattner4b009652007-07-25 00:24:17 +0000602 return false;
603}
604
605
606/// ParseStructUnionSpecifier
607/// struct-or-union-specifier: [C99 6.7.2.1]
608/// struct-or-union identifier[opt] '{' struct-contents '}'
609/// struct-or-union identifier
610/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
611/// '}' attributes[opt]
612/// [GNU] struct-or-union attributes[opt] identifier
613/// struct-or-union:
614/// 'struct'
615/// 'union'
616///
617void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000618 assert((Tok.is(tok::kw_struct) || Tok.is(tok::kw_union)) &&
619 "Not a struct/union specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000620 DeclSpec::TST TagType =
Chris Lattner34a01ad2007-10-09 17:33:22 +0000621 Tok.is(tok::kw_union) ? DeclSpec::TST_union : DeclSpec::TST_struct;
Chris Lattner4b009652007-07-25 00:24:17 +0000622 SourceLocation StartLoc = ConsumeToken();
623
624 // Parse the tag portion of this.
625 DeclTy *TagDecl;
626 if (ParseTag(TagDecl, TagType, StartLoc))
627 return;
628
629 // If there is a body, parse it and inform the actions module.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000630 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000631 ParseStructUnionBody(StartLoc, TagType, TagDecl);
632
633 const char *PrevSpec = 0;
634 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
635 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
636}
637
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000638/// ParseStructDeclaration - Parse a struct declaration without the terminating
639/// semicolon.
640///
Chris Lattner4b009652007-07-25 00:24:17 +0000641/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000642/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000643/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000644/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000645/// struct-declarator-list:
646/// struct-declarator
647/// struct-declarator-list ',' struct-declarator
648/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
649/// struct-declarator:
650/// declarator
651/// [GNU] declarator attributes[opt]
652/// declarator[opt] ':' constant-expression
653/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
654///
Steve Naroffa9adf112007-08-20 22:28:22 +0000655void Parser::ParseStructDeclaration(DeclTy *TagDecl,
Steve Naroffc02f4a92007-08-28 16:31:47 +0000656 llvm::SmallVectorImpl<DeclTy*> &FieldDecls) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000657 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000658 if (Tok.is(tok::kw___extension__))
Steve Naroffa9adf112007-08-20 22:28:22 +0000659 ConsumeToken();
660
661 // Parse the common specifier-qualifiers-list piece.
662 DeclSpec DS;
663 SourceLocation SpecQualLoc = Tok.getLocation();
664 ParseSpecifierQualifierList(DS);
665 // TODO: Does specifier-qualifier list correctly check that *something* is
666 // specified?
667
668 // If there are no declarators, issue a warning.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000669 if (Tok.is(tok::semi)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000670 Diag(SpecQualLoc, diag::w_no_declarators);
Steve Naroffa9adf112007-08-20 22:28:22 +0000671 return;
672 }
673
674 // Read struct-declarators until we find the semicolon.
675 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
676
677 while (1) {
678 /// struct-declarator: declarator
679 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000680 if (Tok.isNot(tok::colon))
Steve Naroffa9adf112007-08-20 22:28:22 +0000681 ParseDeclarator(DeclaratorInfo);
682
683 ExprTy *BitfieldSize = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000684 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000685 ConsumeToken();
686 ExprResult Res = ParseConstantExpression();
687 if (Res.isInvalid) {
688 SkipUntil(tok::semi, true, true);
689 } else {
690 BitfieldSize = Res.Val;
691 }
692 }
693
694 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000695 if (Tok.is(tok::kw___attribute))
Steve Naroffa9adf112007-08-20 22:28:22 +0000696 DeclaratorInfo.AddAttributes(ParseAttributes());
697
698 // Install the declarator into the current TagDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000699 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl, SpecQualLoc,
Steve Naroffa9adf112007-08-20 22:28:22 +0000700 DeclaratorInfo, BitfieldSize);
701 FieldDecls.push_back(Field);
702
703 // If we don't have a comma, it is either the end of the list (a ';')
704 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000705 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000706 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000707
708 // Consume the comma.
709 ConsumeToken();
710
711 // Parse the next declarator.
712 DeclaratorInfo.clear();
713
714 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000715 if (Tok.is(tok::kw___attribute))
Steve Naroffa9adf112007-08-20 22:28:22 +0000716 DeclaratorInfo.AddAttributes(ParseAttributes());
717 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000718}
719
720/// ParseStructUnionBody
721/// struct-contents:
722/// struct-declaration-list
723/// [EXT] empty
724/// [GNU] "struct-declaration-list" without terminatoring ';'
725/// struct-declaration-list:
726/// struct-declaration
727/// struct-declaration-list struct-declaration
728/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
729///
Chris Lattner4b009652007-07-25 00:24:17 +0000730void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
731 unsigned TagType, DeclTy *TagDecl) {
732 SourceLocation LBraceLoc = ConsumeBrace();
733
734 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
735 // C++.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000736 if (Tok.is(tok::r_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000737 Diag(Tok, diag::ext_empty_struct_union_enum,
738 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
739
740 llvm::SmallVector<DeclTy*, 32> FieldDecls;
741
742 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000743 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000744 // Each iteration of this loop reads one struct-declaration.
745
746 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000747 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000748 Diag(Tok, diag::ext_extra_struct_semi);
749 ConsumeToken();
750 continue;
751 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000752 ParseStructDeclaration(TagDecl, FieldDecls);
Chris Lattner4b009652007-07-25 00:24:17 +0000753
Chris Lattner34a01ad2007-10-09 17:33:22 +0000754 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000755 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +0000756 } else if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000757 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
758 break;
759 } else {
760 Diag(Tok, diag::err_expected_semi_decl_list);
761 // Skip to end of block or statement
762 SkipUntil(tok::r_brace, true, true);
763 }
764 }
765
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000766 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000767
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000768 Actions.ActOnFields(CurScope,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000769 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
770 LBraceLoc, RBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000771
772 AttributeList *AttrList = 0;
773 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000774 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000775 AttrList = ParseAttributes(); // FIXME: where should I put them?
776}
777
778
779/// ParseEnumSpecifier
780/// enum-specifier: [C99 6.7.2.2]
781/// 'enum' identifier[opt] '{' enumerator-list '}'
782/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
783/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
784/// '}' attributes[opt]
785/// 'enum' identifier
786/// [GNU] 'enum' attributes[opt] identifier
787void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000788 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000789 SourceLocation StartLoc = ConsumeToken();
790
791 // Parse the tag portion of this.
792 DeclTy *TagDecl;
793 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
794 return;
795
Chris Lattner34a01ad2007-10-09 17:33:22 +0000796 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000797 ParseEnumBody(StartLoc, TagDecl);
798
799 // TODO: semantic analysis on the declspec for enums.
800 const char *PrevSpec = 0;
801 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
802 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
803}
804
805/// ParseEnumBody - Parse a {} enclosed enumerator-list.
806/// enumerator-list:
807/// enumerator
808/// enumerator-list ',' enumerator
809/// enumerator:
810/// enumeration-constant
811/// enumeration-constant '=' constant-expression
812/// enumeration-constant:
813/// identifier
814///
815void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
816 SourceLocation LBraceLoc = ConsumeBrace();
817
Chris Lattnerc9a92452007-08-27 17:24:30 +0000818 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +0000819 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000820 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
821
822 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
823
824 DeclTy *LastEnumConstDecl = 0;
825
826 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000827 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000828 IdentifierInfo *Ident = Tok.getIdentifierInfo();
829 SourceLocation IdentLoc = ConsumeToken();
830
831 SourceLocation EqualLoc;
832 ExprTy *AssignedVal = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000833 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000834 EqualLoc = ConsumeToken();
835 ExprResult Res = ParseConstantExpression();
836 if (Res.isInvalid)
837 SkipUntil(tok::comma, tok::r_brace, true, true);
838 else
839 AssignedVal = Res.Val;
840 }
841
842 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000843 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +0000844 LastEnumConstDecl,
845 IdentLoc, Ident,
846 EqualLoc, AssignedVal);
847 EnumConstantDecls.push_back(EnumConstDecl);
848 LastEnumConstDecl = EnumConstDecl;
849
Chris Lattner34a01ad2007-10-09 17:33:22 +0000850 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000851 break;
852 SourceLocation CommaLoc = ConsumeToken();
853
Chris Lattner34a01ad2007-10-09 17:33:22 +0000854 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +0000855 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
856 }
857
858 // Eat the }.
859 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
860
Steve Naroff0acc9c92007-09-15 18:49:24 +0000861 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +0000862 EnumConstantDecls.size());
863
864 DeclTy *AttrList = 0;
865 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000866 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000867 AttrList = ParseAttributes(); // FIXME: where do they do?
868}
869
870/// isTypeSpecifierQualifier - Return true if the current token could be the
871/// start of a specifier-qualifier-list.
872bool Parser::isTypeSpecifierQualifier() const {
873 switch (Tok.getKind()) {
874 default: return false;
875 // GNU attributes support.
876 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000877 // GNU typeof support.
878 case tok::kw_typeof:
879
Chris Lattner4b009652007-07-25 00:24:17 +0000880 // type-specifiers
881 case tok::kw_short:
882 case tok::kw_long:
883 case tok::kw_signed:
884 case tok::kw_unsigned:
885 case tok::kw__Complex:
886 case tok::kw__Imaginary:
887 case tok::kw_void:
888 case tok::kw_char:
889 case tok::kw_int:
890 case tok::kw_float:
891 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000892 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +0000893 case tok::kw__Bool:
894 case tok::kw__Decimal32:
895 case tok::kw__Decimal64:
896 case tok::kw__Decimal128:
897
898 // struct-or-union-specifier
899 case tok::kw_struct:
900 case tok::kw_union:
901 // enum-specifier
902 case tok::kw_enum:
903
904 // type-qualifier
905 case tok::kw_const:
906 case tok::kw_volatile:
907 case tok::kw_restrict:
908 return true;
909
910 // typedef-name
911 case tok::identifier:
912 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000913 }
914}
915
916/// isDeclarationSpecifier() - Return true if the current token is part of a
917/// declaration specifier.
918bool Parser::isDeclarationSpecifier() const {
919 switch (Tok.getKind()) {
920 default: return false;
921 // storage-class-specifier
922 case tok::kw_typedef:
923 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +0000924 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +0000925 case tok::kw_static:
926 case tok::kw_auto:
927 case tok::kw_register:
928 case tok::kw___thread:
929
930 // type-specifiers
931 case tok::kw_short:
932 case tok::kw_long:
933 case tok::kw_signed:
934 case tok::kw_unsigned:
935 case tok::kw__Complex:
936 case tok::kw__Imaginary:
937 case tok::kw_void:
938 case tok::kw_char:
939 case tok::kw_int:
940 case tok::kw_float:
941 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000942 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +0000943 case tok::kw__Bool:
944 case tok::kw__Decimal32:
945 case tok::kw__Decimal64:
946 case tok::kw__Decimal128:
947
948 // struct-or-union-specifier
949 case tok::kw_struct:
950 case tok::kw_union:
951 // enum-specifier
952 case tok::kw_enum:
953
954 // type-qualifier
955 case tok::kw_const:
956 case tok::kw_volatile:
957 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000958
Chris Lattner4b009652007-07-25 00:24:17 +0000959 // function-specifier
960 case tok::kw_inline:
Chris Lattnere35d2582007-08-09 16:40:21 +0000961
Chris Lattnerb707a7a2007-08-09 17:01:07 +0000962 // GNU typeof support.
963 case tok::kw_typeof:
964
965 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +0000966 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +0000967 return true;
968
969 // typedef-name
970 case tok::identifier:
971 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000972 }
973}
974
975
976/// ParseTypeQualifierListOpt
977/// type-qualifier-list: [C99 6.7.5]
978/// type-qualifier
979/// [GNU] attributes
980/// type-qualifier-list type-qualifier
981/// [GNU] type-qualifier-list attributes
982///
983void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
984 while (1) {
985 int isInvalid = false;
986 const char *PrevSpec = 0;
987 SourceLocation Loc = Tok.getLocation();
988
989 switch (Tok.getKind()) {
990 default:
991 // If this is not a type-qualifier token, we're done reading type
992 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000993 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000994 return;
995 case tok::kw_const:
996 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
997 getLang())*2;
998 break;
999 case tok::kw_volatile:
1000 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1001 getLang())*2;
1002 break;
1003 case tok::kw_restrict:
1004 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1005 getLang())*2;
1006 break;
1007 case tok::kw___attribute:
1008 DS.AddAttributes(ParseAttributes());
1009 continue; // do *not* consume the next token!
1010 }
1011
1012 // If the specifier combination wasn't legal, issue a diagnostic.
1013 if (isInvalid) {
1014 assert(PrevSpec && "Method did not return previous specifier!");
1015 if (isInvalid == 1) // Error.
1016 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1017 else // extwarn.
1018 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1019 }
1020 ConsumeToken();
1021 }
1022}
1023
1024
1025/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1026///
1027void Parser::ParseDeclarator(Declarator &D) {
1028 /// This implements the 'declarator' production in the C grammar, then checks
1029 /// for well-formedness and issues diagnostics.
1030 ParseDeclaratorInternal(D);
1031
1032 // TODO: validate D.
1033
1034}
1035
1036/// ParseDeclaratorInternal
1037/// declarator: [C99 6.7.5]
1038/// pointer[opt] direct-declarator
1039/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1040/// [GNU] '&' restrict[opt] attributes[opt] declarator
1041///
1042/// pointer: [C99 6.7.5]
1043/// '*' type-qualifier-list[opt]
1044/// '*' type-qualifier-list[opt] pointer
1045///
1046void Parser::ParseDeclaratorInternal(Declarator &D) {
1047 tok::TokenKind Kind = Tok.getKind();
1048
1049 // Not a pointer or C++ reference.
1050 if (Kind != tok::star && !(Kind == tok::amp && getLang().CPlusPlus))
1051 return ParseDirectDeclarator(D);
1052
1053 // Otherwise, '*' -> pointer or '&' -> reference.
1054 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1055
1056 if (Kind == tok::star) {
1057 // Is a pointer
1058 DeclSpec DS;
1059
1060 ParseTypeQualifierListOpt(DS);
1061
1062 // Recursively parse the declarator.
1063 ParseDeclaratorInternal(D);
1064
1065 // Remember that we parsed a pointer type, and remember the type-quals.
1066 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc));
1067 } else {
1068 // Is a reference
1069 DeclSpec DS;
1070
1071 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1072 // cv-qualifiers are introduced through the use of a typedef or of a
1073 // template type argument, in which case the cv-qualifiers are ignored.
1074 //
1075 // [GNU] Retricted references are allowed.
1076 // [GNU] Attributes on references are allowed.
1077 ParseTypeQualifierListOpt(DS);
1078
1079 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1080 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1081 Diag(DS.getConstSpecLoc(),
1082 diag::err_invalid_reference_qualifier_application,
1083 "const");
1084 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1085 Diag(DS.getVolatileSpecLoc(),
1086 diag::err_invalid_reference_qualifier_application,
1087 "volatile");
1088 }
1089
1090 // Recursively parse the declarator.
1091 ParseDeclaratorInternal(D);
1092
1093 // Remember that we parsed a reference type. It doesn't have type-quals.
1094 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc));
1095 }
1096}
1097
1098/// ParseDirectDeclarator
1099/// direct-declarator: [C99 6.7.5]
1100/// identifier
1101/// '(' declarator ')'
1102/// [GNU] '(' attributes declarator ')'
1103/// [C90] direct-declarator '[' constant-expression[opt] ']'
1104/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1105/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1106/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1107/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1108/// direct-declarator '(' parameter-type-list ')'
1109/// direct-declarator '(' identifier-list[opt] ')'
1110/// [GNU] direct-declarator '(' parameter-forward-declarations
1111/// parameter-type-list[opt] ')'
1112///
1113void Parser::ParseDirectDeclarator(Declarator &D) {
1114 // Parse the first direct-declarator seen.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001115 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001116 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1117 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1118 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001119 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001120 // direct-declarator: '(' declarator ')'
1121 // direct-declarator: '(' attributes declarator ')'
1122 // Example: 'char (*X)' or 'int (*XX)(void)'
1123 ParseParenDeclarator(D);
1124 } else if (D.mayOmitIdentifier()) {
1125 // This could be something simple like "int" (in which case the declarator
1126 // portion is empty), if an abstract-declarator is allowed.
1127 D.SetIdentifier(0, Tok.getLocation());
1128 } else {
1129 // Expected identifier or '('.
1130 Diag(Tok, diag::err_expected_ident_lparen);
1131 D.SetIdentifier(0, Tok.getLocation());
1132 }
1133
1134 assert(D.isPastIdentifier() &&
1135 "Haven't past the location of the identifier yet?");
1136
1137 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001138 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001139 ParseParenDeclarator(D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001140 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001141 ParseBracketDeclarator(D);
1142 } else {
1143 break;
1144 }
1145 }
1146}
1147
1148/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
1149/// either be before the identifier (in which case these are just grouping
1150/// parens for precedence) or it may be after the identifier, in which case
1151/// these are function arguments.
1152///
1153/// This method also handles this portion of the grammar:
1154/// parameter-type-list: [C99 6.7.5]
1155/// parameter-list
1156/// parameter-list ',' '...'
1157///
1158/// parameter-list: [C99 6.7.5]
1159/// parameter-declaration
1160/// parameter-list ',' parameter-declaration
1161///
1162/// parameter-declaration: [C99 6.7.5]
1163/// declaration-specifiers declarator
1164/// [GNU] declaration-specifiers declarator attributes
1165/// declaration-specifiers abstract-declarator[opt]
1166/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1167///
1168/// identifier-list: [C99 6.7.5]
1169/// identifier
1170/// identifier-list ',' identifier
1171///
1172void Parser::ParseParenDeclarator(Declarator &D) {
1173 SourceLocation StartLoc = ConsumeParen();
1174
1175 // If we haven't past the identifier yet (or where the identifier would be
1176 // stored, if this is an abstract declarator), then this is probably just
1177 // grouping parens.
1178 if (!D.isPastIdentifier()) {
1179 // Okay, this is probably a grouping paren. However, if this could be an
1180 // abstract-declarator, then this could also be the start of function
1181 // arguments (consider 'void()').
1182 bool isGrouping;
1183
1184 if (!D.mayOmitIdentifier()) {
1185 // If this can't be an abstract-declarator, this *must* be a grouping
1186 // paren, because we haven't seen the identifier yet.
1187 isGrouping = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001188 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Chris Lattner4b009652007-07-25 00:24:17 +00001189 isDeclarationSpecifier()) { // 'int(int)' is a function.
1190 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1191 // considered to be a type, not a K&R identifier-list.
1192 isGrouping = false;
1193 } else {
1194 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1195 isGrouping = true;
1196 }
1197
1198 // If this is a grouping paren, handle:
1199 // direct-declarator: '(' declarator ')'
1200 // direct-declarator: '(' attributes declarator ')'
1201 if (isGrouping) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001202 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001203 D.AddAttributes(ParseAttributes());
1204
1205 ParseDeclaratorInternal(D);
1206 // Match the ')'.
1207 MatchRHSPunctuation(tok::r_paren, StartLoc);
1208 return;
1209 }
1210
1211 // Okay, if this wasn't a grouping paren, it must be the start of a function
1212 // argument list. Recognize that this declarator will never have an
1213 // identifier (and remember where it would have been), then fall through to
1214 // the handling of argument lists.
1215 D.SetIdentifier(0, Tok.getLocation());
1216 }
1217
1218 // Okay, this is the parameter list of a function definition, or it is an
1219 // identifier list of a K&R-style function.
1220 bool IsVariadic;
1221 bool HasPrototype;
1222 bool ErrorEmitted = false;
1223
1224 // Build up an array of information about the parsed arguments.
1225 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1226 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1227
Chris Lattner34a01ad2007-10-09 17:33:22 +00001228 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001229 // int() -> no prototype, no '...'.
1230 IsVariadic = false;
1231 HasPrototype = false;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001232 } else if (Tok.is(tok::identifier) &&
Chris Lattner4b009652007-07-25 00:24:17 +00001233 // K&R identifier lists can't have typedefs as identifiers, per
1234 // C99 6.7.5.3p11.
1235 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1236 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1237 // normal declarators, not for abstract-declarators.
1238 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
1239
1240 // If there was no identifier specified, either we are in an
1241 // abstract-declarator, or we are in a parameter declarator which was found
1242 // to be abstract. In abstract-declarators, identifier lists are not valid,
1243 // diagnose this.
1244 if (!D.getIdentifier())
1245 Diag(Tok, diag::ext_ident_list_in_param);
1246
1247 // Remember this identifier in ParamInfo.
1248 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1249 Tok.getLocation(), 0));
1250
1251 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001252 while (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001253 // Eat the comma.
1254 ConsumeToken();
1255
Chris Lattner34a01ad2007-10-09 17:33:22 +00001256 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001257 Diag(Tok, diag::err_expected_ident);
1258 ErrorEmitted = true;
1259 break;
1260 }
1261
1262 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
1263
1264 // Verify that the argument identifier has not already been mentioned.
1265 if (!ParamsSoFar.insert(ParmII)) {
1266 Diag(Tok.getLocation(), diag::err_param_redefinition,ParmII->getName());
1267 ParmII = 0;
1268 }
1269
1270 // Remember this identifier in ParamInfo.
1271 if (ParmII)
1272 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1273 Tok.getLocation(), 0));
1274
1275 // Eat the identifier.
1276 ConsumeToken();
1277 }
1278
1279 // K&R 'prototype'.
1280 IsVariadic = false;
1281 HasPrototype = false;
1282 } else {
1283 // Finally, a normal, non-empty parameter type list.
1284
1285 // Enter function-declaration scope, limiting any declarators for struct
1286 // tags to the function prototype scope.
1287 // FIXME: is this needed?
Chris Lattnera7549902007-08-26 06:24:45 +00001288 EnterScope(Scope::DeclScope);
Chris Lattner4b009652007-07-25 00:24:17 +00001289
1290 IsVariadic = false;
1291 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001292 if (Tok.is(tok::ellipsis)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001293 IsVariadic = true;
1294
1295 // Check to see if this is "void(...)" which is not allowed.
1296 if (ParamInfo.empty()) {
1297 // Otherwise, parse parameter type list. If it starts with an
1298 // ellipsis, diagnose the malformed function.
1299 Diag(Tok, diag::err_ellipsis_first_arg);
1300 IsVariadic = false; // Treat this like 'void()'.
1301 }
1302
1303 // Consume the ellipsis.
1304 ConsumeToken();
1305 break;
1306 }
1307
1308 // Parse the declaration-specifiers.
1309 DeclSpec DS;
1310 ParseDeclarationSpecifiers(DS);
1311
1312 // Parse the declarator. This is "PrototypeContext", because we must
1313 // accept either 'declarator' or 'abstract-declarator' here.
1314 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1315 ParseDeclarator(ParmDecl);
1316
1317 // Parse GNU attributes, if present.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001318 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001319 ParmDecl.AddAttributes(ParseAttributes());
1320
1321 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
1322 // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted.
1323 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1324 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
1325 Diag(DS.getStorageClassSpecLoc(),
1326 diag::err_invalid_storage_class_in_func_decl);
1327 DS.ClearStorageClassSpecs();
1328 }
1329 if (DS.isThreadSpecified()) {
1330 Diag(DS.getThreadSpecLoc(),
1331 diag::err_invalid_storage_class_in_func_decl);
1332 DS.ClearStorageClassSpecs();
1333 }
1334
1335 // Inform the actions module about the parameter declarator, so it gets
1336 // added to the current scope.
1337 Action::TypeResult ParamTy =
Steve Naroff0acc9c92007-09-15 18:49:24 +00001338 Actions.ActOnParamDeclaratorType(CurScope, ParmDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001339
1340 // Remember this parsed parameter in ParamInfo.
1341 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1342
1343 // Verify that the argument identifier has not already been mentioned.
1344 if (ParmII && !ParamsSoFar.insert(ParmII)) {
1345 Diag(ParmDecl.getIdentifierLoc(), diag::err_param_redefinition,
1346 ParmII->getName());
1347 ParmII = 0;
1348 }
1349
Steve Naroff91b03f72007-08-28 03:03:08 +00001350 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Nate Begeman84079d72007-11-13 22:14:47 +00001351 ParmDecl.getIdentifierLoc(), ParamTy.Val, ParmDecl.getInvalidType(),
1352 ParmDecl.getDeclSpec().getAttributes()));
1353
1354 // Ownership of DeclSpec has been handed off to ParamInfo.
1355 DS.clearAttributes();
Chris Lattner4b009652007-07-25 00:24:17 +00001356
1357 // If the next token is a comma, consume it and keep reading arguments.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001358 if (Tok.isNot(tok::comma)) break;
Chris Lattner4b009652007-07-25 00:24:17 +00001359
1360 // Consume the comma.
1361 ConsumeToken();
1362 }
1363
1364 HasPrototype = true;
1365
1366 // Leave prototype scope.
1367 ExitScope();
1368 }
1369
1370 // Remember that we parsed a function type, and remember the attributes.
1371 if (!ErrorEmitted)
1372 D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic,
1373 &ParamInfo[0], ParamInfo.size(),
1374 StartLoc));
1375
1376 // If we have the closing ')', eat it and we're done.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001377 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001378 ConsumeParen();
1379 } else {
1380 // If an error happened earlier parsing something else in the proto, don't
1381 // issue another error.
1382 if (!ErrorEmitted)
1383 Diag(Tok, diag::err_expected_rparen);
1384 SkipUntil(tok::r_paren);
1385 }
1386}
1387
1388
1389/// [C90] direct-declarator '[' constant-expression[opt] ']'
1390/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1391/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1392/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1393/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1394void Parser::ParseBracketDeclarator(Declarator &D) {
1395 SourceLocation StartLoc = ConsumeBracket();
1396
1397 // If valid, this location is the position where we read the 'static' keyword.
1398 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001399 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001400 StaticLoc = ConsumeToken();
1401
1402 // If there is a type-qualifier-list, read it now.
1403 DeclSpec DS;
1404 ParseTypeQualifierListOpt(DS);
1405
1406 // If we haven't already read 'static', check to see if there is one after the
1407 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001408 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001409 StaticLoc = ConsumeToken();
1410
1411 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1412 bool isStar = false;
1413 ExprResult NumElements(false);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001414 if (Tok.is(tok::star)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001415 // Remember the '*' token, in case we have to un-get it.
1416 Token StarTok = Tok;
1417 ConsumeToken();
1418
1419 // Check that the ']' token is present to avoid incorrectly parsing
1420 // expressions starting with '*' as [*].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001421 if (Tok.is(tok::r_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001422 if (StaticLoc.isValid())
1423 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1424 StaticLoc = SourceLocation(); // Drop the static.
1425 isStar = true;
1426 } else {
1427 // Otherwise, the * must have been some expression (such as '*ptr') that
1428 // started an assignment-expr. We already consumed the token, but now we
1429 // need to reparse it. This handles cases like 'X[*p + 4]'
1430 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
1431 }
Chris Lattner34a01ad2007-10-09 17:33:22 +00001432 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001433 // Parse the assignment-expression now.
1434 NumElements = ParseAssignmentExpression();
1435 }
1436
1437 // If there was an error parsing the assignment-expression, recover.
1438 if (NumElements.isInvalid) {
1439 // If the expression was invalid, skip it.
1440 SkipUntil(tok::r_square);
1441 return;
1442 }
1443
1444 MatchRHSPunctuation(tok::r_square, StartLoc);
1445
1446 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1447 // it was not a constant expression.
1448 if (!getLang().C99) {
1449 // TODO: check C90 array constant exprness.
1450 if (isStar || StaticLoc.isValid() ||
1451 0/*TODO: NumElts is not a C90 constantexpr */)
1452 Diag(StartLoc, diag::ext_c99_array_usage);
1453 }
1454
1455 // Remember that we parsed a pointer type, and remember the type-quals.
1456 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1457 StaticLoc.isValid(), isStar,
1458 NumElements.Val, StartLoc));
1459}
1460
Steve Naroff7cbb1462007-07-31 12:34:36 +00001461/// [GNU] typeof-specifier:
1462/// typeof ( expressions )
1463/// typeof ( type-name )
1464///
1465void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001466 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00001467 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00001468 SourceLocation StartLoc = ConsumeToken();
1469
Chris Lattner34a01ad2007-10-09 17:33:22 +00001470 if (Tok.isNot(tok::l_paren)) {
Steve Naroff14bbce82007-08-02 02:53:48 +00001471 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1472 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00001473 }
1474 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1475
1476 if (isTypeSpecifierQualifier()) {
1477 TypeTy *Ty = ParseTypeName();
1478
Steve Naroff4c255ab2007-07-31 23:56:32 +00001479 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1480
Chris Lattner34a01ad2007-10-09 17:33:22 +00001481 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001482 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001483 return;
1484 }
1485 RParenLoc = ConsumeParen();
1486 const char *PrevSpec = 0;
1487 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1488 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1489 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001490 } else { // we have an expression.
1491 ExprResult Result = ParseExpression();
Steve Naroff4c255ab2007-07-31 23:56:32 +00001492
Chris Lattner34a01ad2007-10-09 17:33:22 +00001493 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001494 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001495 return;
1496 }
1497 RParenLoc = ConsumeParen();
1498 const char *PrevSpec = 0;
1499 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1500 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1501 Result.Val))
1502 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001503 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00001504}
1505