blob: 902c5230f166ac345f3c9c223623ba8bca9e818f [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
Steve Naroff73a07032008-02-07 03:50:06 +000039/// FuzzyParseMicrosoftDeclspec. The following construct is Microsoft's
40/// equivalent of GCC's __attribute__. The grammar below is taken from
41/// Microsoft's website. Unfortunately, it is incomplete. FIXME: If/when we
42/// parse this for real, we will need to get a real/current grammar.
43///
44/// decl-specifier:
45/// '__declspec' '(' extended-decl-modifier-seq ')'
46///
47/// extended-decl-modifier-seq:
48/// extended-decl-modifier opt
49/// extended-decl-modifier extended-decl-modifier-seq
50///
51/// extended-decl-modifier:
52/// align( # )
53/// allocate(" segname ")
54/// appdomain
55/// deprecated
56/// dllimport
57/// dllexport
58/// jitintrinsic
59/// naked
60/// noalias
61/// noinline
62/// noreturn
63/// nothrow
64/// novtable
65/// process
66/// property({get=get_func_name|,put=put_func_name})
67/// restrict
68/// selectany
69/// thread
70/// uuid(" ComObjectGUID ")
71///
72void Parser::FuzzyParseMicrosoftDeclspec() {
73 assert(Tok.is(tok::kw___declspec) && "Not an declspec!");
74 ConsumeToken();
75 do {
76 ConsumeAnyToken();
77 } while (ParenCount > 0 && Tok.isNot(tok::eof));
78 return;
79}
80
Chris Lattner4b009652007-07-25 00:24:17 +000081/// ParseAttributes - Parse a non-empty attributes list.
82///
83/// [GNU] attributes:
84/// attribute
85/// attributes attribute
86///
87/// [GNU] attribute:
88/// '__attribute__' '(' '(' attribute-list ')' ')'
89///
90/// [GNU] attribute-list:
91/// attrib
92/// attribute_list ',' attrib
93///
94/// [GNU] attrib:
95/// empty
96/// attrib-name
97/// attrib-name '(' identifier ')'
98/// attrib-name '(' identifier ',' nonempty-expr-list ')'
99/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
100///
101/// [GNU] attrib-name:
102/// identifier
103/// typespec
104/// typequal
105/// storageclass
106///
107/// FIXME: The GCC grammar/code for this construct implies we need two
108/// token lookahead. Comment from gcc: "If they start with an identifier
109/// which is followed by a comma or close parenthesis, then the arguments
110/// start with that identifier; otherwise they are an expression list."
111///
112/// At the moment, I am not doing 2 token lookahead. I am also unaware of
113/// any attributes that don't work (based on my limited testing). Most
114/// attributes are very simple in practice. Until we find a bug, I don't see
115/// a pressing need to implement the 2 token lookahead.
116
117AttributeList *Parser::ParseAttributes() {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000118 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Chris Lattner4b009652007-07-25 00:24:17 +0000119
120 AttributeList *CurrAttr = 0;
121
Chris Lattner34a01ad2007-10-09 17:33:22 +0000122 while (Tok.is(tok::kw___attribute)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000123 ConsumeToken();
124 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
125 "attribute")) {
126 SkipUntil(tok::r_paren, true); // skip until ) or ;
127 return CurrAttr;
128 }
129 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
130 SkipUntil(tok::r_paren, true); // skip until ) or ;
131 return CurrAttr;
132 }
133 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner34a01ad2007-10-09 17:33:22 +0000134 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
135 Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000136
Chris Lattner34a01ad2007-10-09 17:33:22 +0000137 if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000138 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
139 ConsumeToken();
140 continue;
141 }
142 // we have an identifier or declaration specifier (const, int, etc.)
143 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
144 SourceLocation AttrNameLoc = ConsumeToken();
145
146 // check if we have a "paramterized" attribute
Chris Lattner34a01ad2007-10-09 17:33:22 +0000147 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000148 ConsumeParen(); // ignore the left paren loc for now
149
Chris Lattner34a01ad2007-10-09 17:33:22 +0000150 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000151 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
152 SourceLocation ParmLoc = ConsumeToken();
153
Chris Lattner34a01ad2007-10-09 17:33:22 +0000154 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000155 // __attribute__(( mode(byte) ))
156 ConsumeParen(); // ignore the right paren loc for now
157 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
158 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000159 } else if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000160 ConsumeToken();
161 // __attribute__(( format(printf, 1, 2) ))
162 llvm::SmallVector<ExprTy*, 8> ArgExprs;
163 bool ArgExprsOk = true;
164
165 // now parse the non-empty comma separated list of expressions
166 while (1) {
167 ExprResult ArgExpr = ParseAssignmentExpression();
168 if (ArgExpr.isInvalid) {
169 ArgExprsOk = false;
170 SkipUntil(tok::r_paren);
171 break;
172 } else {
173 ArgExprs.push_back(ArgExpr.Val);
174 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000175 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000176 break;
177 ConsumeToken(); // Eat the comma, move to the next argument
178 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000179 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000180 ConsumeParen(); // ignore the right paren loc for now
181 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
182 ParmLoc, &ArgExprs[0], ArgExprs.size(), CurrAttr);
183 }
184 }
185 } else { // not an identifier
186 // parse a possibly empty comma separated list of expressions
Chris Lattner34a01ad2007-10-09 17:33:22 +0000187 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000188 // __attribute__(( nonnull() ))
189 ConsumeParen(); // ignore the right paren loc for now
190 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
191 0, SourceLocation(), 0, 0, CurrAttr);
192 } else {
193 // __attribute__(( aligned(16) ))
194 llvm::SmallVector<ExprTy*, 8> ArgExprs;
195 bool ArgExprsOk = true;
196
197 // now parse the list of expressions
198 while (1) {
199 ExprResult ArgExpr = ParseAssignmentExpression();
200 if (ArgExpr.isInvalid) {
201 ArgExprsOk = false;
202 SkipUntil(tok::r_paren);
203 break;
204 } else {
205 ArgExprs.push_back(ArgExpr.Val);
206 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000207 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000208 break;
209 ConsumeToken(); // Eat the comma, move to the next argument
210 }
211 // Match the ')'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000212 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000213 ConsumeParen(); // ignore the right paren loc for now
214 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
215 SourceLocation(), &ArgExprs[0], ArgExprs.size(),
216 CurrAttr);
217 }
218 }
219 }
220 } else {
221 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
222 0, SourceLocation(), 0, 0, CurrAttr);
223 }
224 }
225 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
226 SkipUntil(tok::r_paren, false);
227 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
228 SkipUntil(tok::r_paren, false);
229 }
230 return CurrAttr;
231}
232
233/// ParseDeclaration - Parse a full 'declaration', which consists of
234/// declaration-specifiers, some number of declarators, and a semicolon.
235/// 'Context' should be a Declarator::TheContext value.
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000236///
237/// declaration: [C99 6.7]
238/// block-declaration ->
239/// simple-declaration
240/// others [FIXME]
241/// [C++] namespace-definition
242/// others... [FIXME]
243///
Chris Lattner4b009652007-07-25 00:24:17 +0000244Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000245 switch (Tok.getKind()) {
246 case tok::kw_namespace:
247 return ParseNamespace(Context);
248 default:
249 return ParseSimpleDeclaration(Context);
250 }
251}
252
253/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
254/// declaration-specifiers init-declarator-list[opt] ';'
255///[C90/C++]init-declarator-list ';' [TODO]
256/// [OMP] threadprivate-directive [TODO]
257Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Chris Lattner4b009652007-07-25 00:24:17 +0000258 // Parse the common declaration-specifiers piece.
259 DeclSpec DS;
260 ParseDeclarationSpecifiers(DS);
261
262 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
263 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000264 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000265 ConsumeToken();
266 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
267 }
268
269 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
270 ParseDeclarator(DeclaratorInfo);
271
272 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
273}
274
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000275
Chris Lattner4b009652007-07-25 00:24:17 +0000276/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
277/// parsing 'declaration-specifiers declarator'. This method is split out this
278/// way to handle the ambiguity between top-level function-definitions and
279/// declarations.
280///
Chris Lattner4b009652007-07-25 00:24:17 +0000281/// init-declarator-list: [C99 6.7]
282/// init-declarator
283/// init-declarator-list ',' init-declarator
284/// init-declarator: [C99 6.7]
285/// declarator
286/// declarator '=' initializer
287/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
288/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
289///
290Parser::DeclTy *Parser::
291ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
292
293 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
294 // that they can be chained properly if the actions want this.
295 Parser::DeclTy *LastDeclInGroup = 0;
296
297 // At this point, we know that it is not a function definition. Parse the
298 // rest of the init-declarator-list.
299 while (1) {
300 // If a simple-asm-expr is present, parse it.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000301 if (Tok.is(tok::kw_asm))
Chris Lattner4b009652007-07-25 00:24:17 +0000302 ParseSimpleAsm();
303
304 // If attributes are present, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000305 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000306 D.AddAttributes(ParseAttributes());
Steve Naroff6a0e2092007-09-12 14:07:44 +0000307
308 // Inform the current actions module that we just parsed this declarator.
309 // FIXME: pass asm & attributes.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000310 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Steve Naroff6a0e2092007-09-12 14:07:44 +0000311
Chris Lattner4b009652007-07-25 00:24:17 +0000312 // Parse declarator '=' initializer.
313 ExprResult Init;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000314 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000315 ConsumeToken();
316 Init = ParseInitializer();
317 if (Init.isInvalid) {
318 SkipUntil(tok::semi);
319 return 0;
320 }
Steve Naroff6a0e2092007-09-12 14:07:44 +0000321 Actions.AddInitializerToDecl(LastDeclInGroup, Init.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000322 }
323
Chris Lattner4b009652007-07-25 00:24:17 +0000324 // If we don't have a comma, it is either the end of the list (a ';') or an
325 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000326 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000327 break;
328
329 // Consume the comma.
330 ConsumeToken();
331
332 // Parse the next declarator.
333 D.clear();
334 ParseDeclarator(D);
335 }
336
Chris Lattner34a01ad2007-10-09 17:33:22 +0000337 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000338 ConsumeToken();
339 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
340 }
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000341 // If this is an ObjC2 for-each loop, this is a successful declarator
342 // parse. The syntax for these looks like:
343 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000344 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000345 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
346 }
Chris Lattner4b009652007-07-25 00:24:17 +0000347 Diag(Tok, diag::err_parse_error);
348 // Skip to end of block or statement
Chris Lattnerf491b412007-08-21 18:36:18 +0000349 SkipUntil(tok::r_brace, true, true);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000350 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000351 ConsumeToken();
352 return 0;
353}
354
355/// ParseSpecifierQualifierList
356/// specifier-qualifier-list:
357/// type-specifier specifier-qualifier-list[opt]
358/// type-qualifier specifier-qualifier-list[opt]
359/// [GNU] attributes specifier-qualifier-list[opt]
360///
361void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
362 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
363 /// parse declaration-specifiers and complain about extra stuff.
364 ParseDeclarationSpecifiers(DS);
365
366 // Validate declspec for type-name.
367 unsigned Specs = DS.getParsedSpecifiers();
368 if (Specs == DeclSpec::PQ_None)
369 Diag(Tok, diag::err_typename_requires_specqual);
370
371 // Issue diagnostic and remove storage class if present.
372 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
373 if (DS.getStorageClassSpecLoc().isValid())
374 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
375 else
376 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
377 DS.ClearStorageClassSpecs();
378 }
379
380 // Issue diagnostic and remove function specfier if present.
381 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
382 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
383 DS.ClearFunctionSpecs();
384 }
385}
386
387/// ParseDeclarationSpecifiers
388/// declaration-specifiers: [C99 6.7]
389/// storage-class-specifier declaration-specifiers[opt]
390/// type-specifier declaration-specifiers[opt]
391/// type-qualifier declaration-specifiers[opt]
392/// [C99] function-specifier declaration-specifiers[opt]
393/// [GNU] attributes declaration-specifiers[opt]
394///
395/// storage-class-specifier: [C99 6.7.1]
396/// 'typedef'
397/// 'extern'
398/// 'static'
399/// 'auto'
400/// 'register'
401/// [GNU] '__thread'
402/// type-specifier: [C99 6.7.2]
403/// 'void'
404/// 'char'
405/// 'short'
406/// 'int'
407/// 'long'
408/// 'float'
409/// 'double'
410/// 'signed'
411/// 'unsigned'
412/// struct-or-union-specifier
413/// enum-specifier
414/// typedef-name
415/// [C++] 'bool'
416/// [C99] '_Bool'
417/// [C99] '_Complex'
418/// [C99] '_Imaginary' // Removed in TC2?
419/// [GNU] '_Decimal32'
420/// [GNU] '_Decimal64'
421/// [GNU] '_Decimal128'
Steve Naroff4c255ab2007-07-31 23:56:32 +0000422/// [GNU] typeof-specifier
Chris Lattner4b009652007-07-25 00:24:17 +0000423/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroffa8ee2262007-08-22 23:18:22 +0000424/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattner4b009652007-07-25 00:24:17 +0000425/// type-qualifier:
426/// 'const'
427/// 'volatile'
428/// [C99] 'restrict'
429/// function-specifier: [C99 6.7.4]
430/// [C99] 'inline'
431///
432void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
433 DS.Range.setBegin(Tok.getLocation());
434 while (1) {
435 int isInvalid = false;
436 const char *PrevSpec = 0;
437 SourceLocation Loc = Tok.getLocation();
438
439 switch (Tok.getKind()) {
440 // typedef-name
441 case tok::identifier:
442 // This identifier can only be a typedef name if we haven't already seen
443 // a type-specifier. Without this check we misparse:
444 // typedef int X; struct Y { short X; }; as 'short int'.
445 if (!DS.hasTypeSpecifier()) {
446 // It has to be available as a typedef too!
447 if (void *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(),
448 CurScope)) {
449 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
450 TypeRep);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000451 if (isInvalid)
452 break;
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000453 // FIXME: restrict this to "id" and ObjC classnames.
454 DS.Range.setEnd(Tok.getLocation());
455 ConsumeToken(); // The identifier
456 if (Tok.is(tok::less)) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000457 SourceLocation endProtoLoc;
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000458 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroffef20ed32007-10-30 02:23:23 +0000459 ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000460 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
461 new llvm::SmallVector<DeclTy *, 8>;
462 DS.setProtocolQualifiers(ProtocolDecl);
463 Actions.FindProtocolDeclaration(Loc,
464 &ProtocolRefs[0], ProtocolRefs.size(),
465 *ProtocolDecl);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000466 }
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000467 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000468 }
469 }
470 // FALL THROUGH.
471 default:
472 // If this is not a declaration specifier token, we're done reading decl
473 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000474 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000475 return;
476
477 // GNU attributes support.
478 case tok::kw___attribute:
479 DS.AddAttributes(ParseAttributes());
480 continue;
481
482 // storage-class-specifier
483 case tok::kw_typedef:
484 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
485 break;
Steve Naroff73a07032008-02-07 03:50:06 +0000486 case tok::kw___w64: // ignore Microsoft specifier
487 break;
488 case tok::kw___declspec:
489 FuzzyParseMicrosoftDeclspec();
490 // Don't consume the next token, __declspec's can appear one after
491 // another. For example:
492 // __declspec(deprecated("comment1"))
493 // __declspec(deprecated("comment2")) extern unsigned int _winmajor;
494 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000495 case tok::kw_extern:
496 if (DS.isThreadSpecified())
497 Diag(Tok, diag::ext_thread_before, "extern");
498 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
499 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000500 case tok::kw___private_extern__:
Steve Naroff1cbb2762008-01-25 22:14:40 +0000501 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc, PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000502 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000503 case tok::kw_static:
504 if (DS.isThreadSpecified())
505 Diag(Tok, diag::ext_thread_before, "static");
506 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
507 break;
508 case tok::kw_auto:
509 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
510 break;
511 case tok::kw_register:
512 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
513 break;
514 case tok::kw___thread:
515 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
516 break;
517
518 // type-specifiers
Steve Naroff73a07032008-02-07 03:50:06 +0000519 case tok::kw___int16:
Chris Lattner4b009652007-07-25 00:24:17 +0000520 case tok::kw_short:
521 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
522 break;
Steve Naroff73a07032008-02-07 03:50:06 +0000523 case tok::kw___int64:
Chris Lattner4b009652007-07-25 00:24:17 +0000524 case tok::kw_long:
525 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
526 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
527 else
528 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
529 break;
530 case tok::kw_signed:
531 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
532 break;
533 case tok::kw_unsigned:
534 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
535 break;
536 case tok::kw__Complex:
537 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
538 break;
539 case tok::kw__Imaginary:
540 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
541 break;
542 case tok::kw_void:
543 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
544 break;
Steve Naroff73a07032008-02-07 03:50:06 +0000545 case tok::kw___int8:
Chris Lattner4b009652007-07-25 00:24:17 +0000546 case tok::kw_char:
547 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
548 break;
Steve Naroff73a07032008-02-07 03:50:06 +0000549 case tok::kw___int32:
Chris Lattner4b009652007-07-25 00:24:17 +0000550 case tok::kw_int:
551 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
552 break;
553 case tok::kw_float:
554 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
555 break;
556 case tok::kw_double:
557 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
558 break;
559 case tok::kw_bool: // [C++ 2.11p1]
560 case tok::kw__Bool:
561 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
562 break;
563 case tok::kw__Decimal32:
564 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
565 break;
566 case tok::kw__Decimal64:
567 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
568 break;
569 case tok::kw__Decimal128:
570 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
571 break;
572
573 case tok::kw_struct:
574 case tok::kw_union:
575 ParseStructUnionSpecifier(DS);
576 continue;
577 case tok::kw_enum:
578 ParseEnumSpecifier(DS);
579 continue;
580
Steve Naroff7cbb1462007-07-31 12:34:36 +0000581 // GNU typeof support.
582 case tok::kw_typeof:
583 ParseTypeofSpecifier(DS);
584 continue;
585
Chris Lattner4b009652007-07-25 00:24:17 +0000586 // type-qualifier
587 case tok::kw_const:
588 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
589 getLang())*2;
590 break;
591 case tok::kw_volatile:
592 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
593 getLang())*2;
594 break;
595 case tok::kw_restrict:
596 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
597 getLang())*2;
598 break;
599
600 // function-specifier
601 case tok::kw_inline:
602 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
603 break;
604 }
605 // If the specifier combination wasn't legal, issue a diagnostic.
606 if (isInvalid) {
607 assert(PrevSpec && "Method did not return previous specifier!");
608 if (isInvalid == 1) // Error.
609 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
610 else // extwarn.
611 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
612 }
613 DS.Range.setEnd(Tok.getLocation());
614 ConsumeToken();
615 }
616}
617
618/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
619/// the first token has already been read and has been turned into an instance
620/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
621/// otherwise it returns false and fills in Decl.
622bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
623 AttributeList *Attr = 0;
624 // If attributes exist after tag, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000625 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000626 Attr = ParseAttributes();
627
628 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000629 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000630 Diag(Tok, diag::err_expected_ident_lbrace);
631
632 // Skip the rest of this declarator, up until the comma or semicolon.
633 SkipUntil(tok::comma, true);
634 return true;
635 }
636
637 // If an identifier is present, consume and remember it.
638 IdentifierInfo *Name = 0;
639 SourceLocation NameLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000640 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000641 Name = Tok.getIdentifierInfo();
642 NameLoc = ConsumeToken();
643 }
644
645 // There are three options here. If we have 'struct foo;', then this is a
646 // forward declaration. If we have 'struct foo {...' then this is a
647 // definition. Otherwise we have something like 'struct foo xyz', a reference.
648 //
649 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
650 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
651 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
652 //
653 Action::TagKind TK;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000654 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000655 TK = Action::TK_Definition;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000656 else if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000657 TK = Action::TK_Declaration;
658 else
659 TK = Action::TK_Reference;
Steve Naroff0acc9c92007-09-15 18:49:24 +0000660 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Chris Lattner4b009652007-07-25 00:24:17 +0000661 return false;
662}
663
664
665/// ParseStructUnionSpecifier
666/// struct-or-union-specifier: [C99 6.7.2.1]
667/// struct-or-union identifier[opt] '{' struct-contents '}'
668/// struct-or-union identifier
669/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
670/// '}' attributes[opt]
671/// [GNU] struct-or-union attributes[opt] identifier
672/// struct-or-union:
673/// 'struct'
674/// 'union'
675///
676void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000677 assert((Tok.is(tok::kw_struct) || Tok.is(tok::kw_union)) &&
678 "Not a struct/union specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000679 DeclSpec::TST TagType =
Chris Lattner34a01ad2007-10-09 17:33:22 +0000680 Tok.is(tok::kw_union) ? DeclSpec::TST_union : DeclSpec::TST_struct;
Chris Lattner4b009652007-07-25 00:24:17 +0000681 SourceLocation StartLoc = ConsumeToken();
682
Steve Naroff73a07032008-02-07 03:50:06 +0000683 if (getLang().Microsoft && Tok.is(tok::kw___declspec))
684 FuzzyParseMicrosoftDeclspec();
685
Chris Lattner4b009652007-07-25 00:24:17 +0000686 // Parse the tag portion of this.
687 DeclTy *TagDecl;
688 if (ParseTag(TagDecl, TagType, StartLoc))
689 return;
690
691 // If there is a body, parse it and inform the actions module.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000692 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000693 ParseStructUnionBody(StartLoc, TagType, TagDecl);
694
695 const char *PrevSpec = 0;
696 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
697 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
698}
699
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000700/// ParseStructDeclaration - Parse a struct declaration without the terminating
701/// semicolon.
702///
Chris Lattner4b009652007-07-25 00:24:17 +0000703/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000704/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000705/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000706/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000707/// struct-declarator-list:
708/// struct-declarator
709/// struct-declarator-list ',' struct-declarator
710/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
711/// struct-declarator:
712/// declarator
713/// [GNU] declarator attributes[opt]
714/// declarator[opt] ':' constant-expression
715/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
716///
Steve Naroffa9adf112007-08-20 22:28:22 +0000717void Parser::ParseStructDeclaration(DeclTy *TagDecl,
Steve Naroffc02f4a92007-08-28 16:31:47 +0000718 llvm::SmallVectorImpl<DeclTy*> &FieldDecls) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000719 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000720 if (Tok.is(tok::kw___extension__))
Steve Naroffa9adf112007-08-20 22:28:22 +0000721 ConsumeToken();
722
723 // Parse the common specifier-qualifiers-list piece.
724 DeclSpec DS;
725 SourceLocation SpecQualLoc = Tok.getLocation();
726 ParseSpecifierQualifierList(DS);
727 // TODO: Does specifier-qualifier list correctly check that *something* is
728 // specified?
729
730 // If there are no declarators, issue a warning.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000731 if (Tok.is(tok::semi)) {
Steve Naroff73a07032008-02-07 03:50:06 +0000732 if (!getLang().Microsoft) // MS allows unnamed struct/union fields.
733 Diag(SpecQualLoc, diag::w_no_declarators);
Steve Naroffa9adf112007-08-20 22:28:22 +0000734 return;
735 }
736
737 // Read struct-declarators until we find the semicolon.
738 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
739
740 while (1) {
741 /// struct-declarator: declarator
742 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000743 if (Tok.isNot(tok::colon))
Steve Naroffa9adf112007-08-20 22:28:22 +0000744 ParseDeclarator(DeclaratorInfo);
745
746 ExprTy *BitfieldSize = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000747 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000748 ConsumeToken();
749 ExprResult Res = ParseConstantExpression();
750 if (Res.isInvalid) {
751 SkipUntil(tok::semi, true, true);
752 } else {
753 BitfieldSize = Res.Val;
754 }
755 }
756
757 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000758 if (Tok.is(tok::kw___attribute))
Steve Naroffa9adf112007-08-20 22:28:22 +0000759 DeclaratorInfo.AddAttributes(ParseAttributes());
760
761 // Install the declarator into the current TagDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000762 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl, SpecQualLoc,
Steve Naroffa9adf112007-08-20 22:28:22 +0000763 DeclaratorInfo, BitfieldSize);
764 FieldDecls.push_back(Field);
765
766 // If we don't have a comma, it is either the end of the list (a ';')
767 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000768 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000769 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000770
771 // Consume the comma.
772 ConsumeToken();
773
774 // Parse the next declarator.
775 DeclaratorInfo.clear();
776
777 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000778 if (Tok.is(tok::kw___attribute))
Steve Naroffa9adf112007-08-20 22:28:22 +0000779 DeclaratorInfo.AddAttributes(ParseAttributes());
780 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000781}
782
783/// ParseStructUnionBody
784/// struct-contents:
785/// struct-declaration-list
786/// [EXT] empty
787/// [GNU] "struct-declaration-list" without terminatoring ';'
788/// struct-declaration-list:
789/// struct-declaration
790/// struct-declaration-list struct-declaration
791/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
792///
Chris Lattner4b009652007-07-25 00:24:17 +0000793void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
794 unsigned TagType, DeclTy *TagDecl) {
795 SourceLocation LBraceLoc = ConsumeBrace();
796
797 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
798 // C++.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000799 if (Tok.is(tok::r_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000800 Diag(Tok, diag::ext_empty_struct_union_enum,
801 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
802
803 llvm::SmallVector<DeclTy*, 32> FieldDecls;
804
805 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000806 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000807 // Each iteration of this loop reads one struct-declaration.
808
809 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000810 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000811 Diag(Tok, diag::ext_extra_struct_semi);
812 ConsumeToken();
813 continue;
814 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000815 ParseStructDeclaration(TagDecl, FieldDecls);
Chris Lattner4b009652007-07-25 00:24:17 +0000816
Chris Lattner34a01ad2007-10-09 17:33:22 +0000817 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000818 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +0000819 } else if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000820 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
821 break;
822 } else {
823 Diag(Tok, diag::err_expected_semi_decl_list);
824 // Skip to end of block or statement
825 SkipUntil(tok::r_brace, true, true);
826 }
827 }
828
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000829 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000830
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000831 Actions.ActOnFields(CurScope,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000832 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
833 LBraceLoc, RBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000834
835 AttributeList *AttrList = 0;
836 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000837 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000838 AttrList = ParseAttributes(); // FIXME: where should I put them?
839}
840
841
842/// ParseEnumSpecifier
843/// enum-specifier: [C99 6.7.2.2]
844/// 'enum' identifier[opt] '{' enumerator-list '}'
845/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
846/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
847/// '}' attributes[opt]
848/// 'enum' identifier
849/// [GNU] 'enum' attributes[opt] identifier
850void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000851 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000852 SourceLocation StartLoc = ConsumeToken();
853
854 // Parse the tag portion of this.
855 DeclTy *TagDecl;
856 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
857 return;
858
Chris Lattner34a01ad2007-10-09 17:33:22 +0000859 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000860 ParseEnumBody(StartLoc, TagDecl);
861
862 // TODO: semantic analysis on the declspec for enums.
863 const char *PrevSpec = 0;
864 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
865 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
866}
867
868/// ParseEnumBody - Parse a {} enclosed enumerator-list.
869/// enumerator-list:
870/// enumerator
871/// enumerator-list ',' enumerator
872/// enumerator:
873/// enumeration-constant
874/// enumeration-constant '=' constant-expression
875/// enumeration-constant:
876/// identifier
877///
878void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
879 SourceLocation LBraceLoc = ConsumeBrace();
880
Chris Lattnerc9a92452007-08-27 17:24:30 +0000881 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +0000882 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000883 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
884
885 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
886
887 DeclTy *LastEnumConstDecl = 0;
888
889 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000890 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000891 IdentifierInfo *Ident = Tok.getIdentifierInfo();
892 SourceLocation IdentLoc = ConsumeToken();
893
894 SourceLocation EqualLoc;
895 ExprTy *AssignedVal = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000896 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000897 EqualLoc = ConsumeToken();
898 ExprResult Res = ParseConstantExpression();
899 if (Res.isInvalid)
900 SkipUntil(tok::comma, tok::r_brace, true, true);
901 else
902 AssignedVal = Res.Val;
903 }
904
905 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000906 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +0000907 LastEnumConstDecl,
908 IdentLoc, Ident,
909 EqualLoc, AssignedVal);
910 EnumConstantDecls.push_back(EnumConstDecl);
911 LastEnumConstDecl = EnumConstDecl;
912
Chris Lattner34a01ad2007-10-09 17:33:22 +0000913 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000914 break;
915 SourceLocation CommaLoc = ConsumeToken();
916
Chris Lattner34a01ad2007-10-09 17:33:22 +0000917 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +0000918 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
919 }
920
921 // Eat the }.
922 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
923
Steve Naroff0acc9c92007-09-15 18:49:24 +0000924 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +0000925 EnumConstantDecls.size());
926
927 DeclTy *AttrList = 0;
928 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000929 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000930 AttrList = ParseAttributes(); // FIXME: where do they do?
931}
932
933/// isTypeSpecifierQualifier - Return true if the current token could be the
934/// start of a specifier-qualifier-list.
935bool Parser::isTypeSpecifierQualifier() const {
936 switch (Tok.getKind()) {
937 default: return false;
938 // GNU attributes support.
939 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000940 // GNU typeof support.
941 case tok::kw_typeof:
942
Chris Lattner4b009652007-07-25 00:24:17 +0000943 // type-specifiers
944 case tok::kw_short:
945 case tok::kw_long:
946 case tok::kw_signed:
947 case tok::kw_unsigned:
948 case tok::kw__Complex:
949 case tok::kw__Imaginary:
950 case tok::kw_void:
951 case tok::kw_char:
952 case tok::kw_int:
953 case tok::kw_float:
954 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000955 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +0000956 case tok::kw__Bool:
957 case tok::kw__Decimal32:
958 case tok::kw__Decimal64:
959 case tok::kw__Decimal128:
960
961 // struct-or-union-specifier
962 case tok::kw_struct:
963 case tok::kw_union:
964 // enum-specifier
965 case tok::kw_enum:
966
967 // type-qualifier
968 case tok::kw_const:
969 case tok::kw_volatile:
970 case tok::kw_restrict:
971 return true;
972
973 // typedef-name
974 case tok::identifier:
975 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000976 }
977}
978
979/// isDeclarationSpecifier() - Return true if the current token is part of a
980/// declaration specifier.
981bool Parser::isDeclarationSpecifier() const {
982 switch (Tok.getKind()) {
983 default: return false;
984 // storage-class-specifier
985 case tok::kw_typedef:
986 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +0000987 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +0000988 case tok::kw_static:
989 case tok::kw_auto:
990 case tok::kw_register:
991 case tok::kw___thread:
992
993 // type-specifiers
994 case tok::kw_short:
995 case tok::kw_long:
996 case tok::kw_signed:
997 case tok::kw_unsigned:
998 case tok::kw__Complex:
999 case tok::kw__Imaginary:
1000 case tok::kw_void:
1001 case tok::kw_char:
1002 case tok::kw_int:
1003 case tok::kw_float:
1004 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001005 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001006 case tok::kw__Bool:
1007 case tok::kw__Decimal32:
1008 case tok::kw__Decimal64:
1009 case tok::kw__Decimal128:
1010
1011 // struct-or-union-specifier
1012 case tok::kw_struct:
1013 case tok::kw_union:
1014 // enum-specifier
1015 case tok::kw_enum:
1016
1017 // type-qualifier
1018 case tok::kw_const:
1019 case tok::kw_volatile:
1020 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001021
Chris Lattner4b009652007-07-25 00:24:17 +00001022 // function-specifier
1023 case tok::kw_inline:
Chris Lattnere35d2582007-08-09 16:40:21 +00001024
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001025 // GNU typeof support.
1026 case tok::kw_typeof:
1027
1028 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001029 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001030 return true;
1031
1032 // typedef-name
1033 case tok::identifier:
1034 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001035 }
1036}
1037
1038
1039/// ParseTypeQualifierListOpt
1040/// type-qualifier-list: [C99 6.7.5]
1041/// type-qualifier
1042/// [GNU] attributes
1043/// type-qualifier-list type-qualifier
1044/// [GNU] type-qualifier-list attributes
1045///
1046void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1047 while (1) {
1048 int isInvalid = false;
1049 const char *PrevSpec = 0;
1050 SourceLocation Loc = Tok.getLocation();
1051
1052 switch (Tok.getKind()) {
1053 default:
1054 // If this is not a type-qualifier token, we're done reading type
1055 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +00001056 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +00001057 return;
1058 case tok::kw_const:
1059 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1060 getLang())*2;
1061 break;
1062 case tok::kw_volatile:
1063 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1064 getLang())*2;
1065 break;
1066 case tok::kw_restrict:
1067 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1068 getLang())*2;
1069 break;
1070 case tok::kw___attribute:
1071 DS.AddAttributes(ParseAttributes());
1072 continue; // do *not* consume the next token!
1073 }
1074
1075 // If the specifier combination wasn't legal, issue a diagnostic.
1076 if (isInvalid) {
1077 assert(PrevSpec && "Method did not return previous specifier!");
1078 if (isInvalid == 1) // Error.
1079 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1080 else // extwarn.
1081 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1082 }
1083 ConsumeToken();
1084 }
1085}
1086
1087
1088/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1089///
1090void Parser::ParseDeclarator(Declarator &D) {
1091 /// This implements the 'declarator' production in the C grammar, then checks
1092 /// for well-formedness and issues diagnostics.
1093 ParseDeclaratorInternal(D);
1094
1095 // TODO: validate D.
1096
1097}
1098
1099/// ParseDeclaratorInternal
1100/// declarator: [C99 6.7.5]
1101/// pointer[opt] direct-declarator
1102/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1103/// [GNU] '&' restrict[opt] attributes[opt] declarator
1104///
1105/// pointer: [C99 6.7.5]
1106/// '*' type-qualifier-list[opt]
1107/// '*' type-qualifier-list[opt] pointer
1108///
1109void Parser::ParseDeclaratorInternal(Declarator &D) {
1110 tok::TokenKind Kind = Tok.getKind();
1111
1112 // Not a pointer or C++ reference.
1113 if (Kind != tok::star && !(Kind == tok::amp && getLang().CPlusPlus))
1114 return ParseDirectDeclarator(D);
1115
1116 // Otherwise, '*' -> pointer or '&' -> reference.
1117 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1118
1119 if (Kind == tok::star) {
1120 // Is a pointer
1121 DeclSpec DS;
1122
1123 ParseTypeQualifierListOpt(DS);
1124
1125 // Recursively parse the declarator.
1126 ParseDeclaratorInternal(D);
1127
1128 // Remember that we parsed a pointer type, and remember the type-quals.
1129 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc));
1130 } else {
1131 // Is a reference
1132 DeclSpec DS;
1133
1134 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1135 // cv-qualifiers are introduced through the use of a typedef or of a
1136 // template type argument, in which case the cv-qualifiers are ignored.
1137 //
1138 // [GNU] Retricted references are allowed.
1139 // [GNU] Attributes on references are allowed.
1140 ParseTypeQualifierListOpt(DS);
1141
1142 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1143 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1144 Diag(DS.getConstSpecLoc(),
1145 diag::err_invalid_reference_qualifier_application,
1146 "const");
1147 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1148 Diag(DS.getVolatileSpecLoc(),
1149 diag::err_invalid_reference_qualifier_application,
1150 "volatile");
1151 }
1152
1153 // Recursively parse the declarator.
1154 ParseDeclaratorInternal(D);
1155
1156 // Remember that we parsed a reference type. It doesn't have type-quals.
1157 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc));
1158 }
1159}
1160
1161/// ParseDirectDeclarator
1162/// direct-declarator: [C99 6.7.5]
1163/// identifier
1164/// '(' declarator ')'
1165/// [GNU] '(' attributes declarator ')'
1166/// [C90] direct-declarator '[' constant-expression[opt] ']'
1167/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1168/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1169/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1170/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1171/// direct-declarator '(' parameter-type-list ')'
1172/// direct-declarator '(' identifier-list[opt] ')'
1173/// [GNU] direct-declarator '(' parameter-forward-declarations
1174/// parameter-type-list[opt] ')'
1175///
1176void Parser::ParseDirectDeclarator(Declarator &D) {
1177 // Parse the first direct-declarator seen.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001178 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001179 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1180 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1181 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001182 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001183 // direct-declarator: '(' declarator ')'
1184 // direct-declarator: '(' attributes declarator ')'
1185 // Example: 'char (*X)' or 'int (*XX)(void)'
1186 ParseParenDeclarator(D);
1187 } else if (D.mayOmitIdentifier()) {
1188 // This could be something simple like "int" (in which case the declarator
1189 // portion is empty), if an abstract-declarator is allowed.
1190 D.SetIdentifier(0, Tok.getLocation());
1191 } else {
1192 // Expected identifier or '('.
1193 Diag(Tok, diag::err_expected_ident_lparen);
1194 D.SetIdentifier(0, Tok.getLocation());
1195 }
1196
1197 assert(D.isPastIdentifier() &&
1198 "Haven't past the location of the identifier yet?");
1199
1200 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001201 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001202 ParseParenDeclarator(D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001203 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001204 ParseBracketDeclarator(D);
1205 } else {
1206 break;
1207 }
1208 }
1209}
1210
1211/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
1212/// either be before the identifier (in which case these are just grouping
1213/// parens for precedence) or it may be after the identifier, in which case
1214/// these are function arguments.
1215///
1216/// This method also handles this portion of the grammar:
1217/// parameter-type-list: [C99 6.7.5]
1218/// parameter-list
1219/// parameter-list ',' '...'
1220///
1221/// parameter-list: [C99 6.7.5]
1222/// parameter-declaration
1223/// parameter-list ',' parameter-declaration
1224///
1225/// parameter-declaration: [C99 6.7.5]
1226/// declaration-specifiers declarator
1227/// [GNU] declaration-specifiers declarator attributes
1228/// declaration-specifiers abstract-declarator[opt]
1229/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1230///
1231/// identifier-list: [C99 6.7.5]
1232/// identifier
1233/// identifier-list ',' identifier
1234///
1235void Parser::ParseParenDeclarator(Declarator &D) {
1236 SourceLocation StartLoc = ConsumeParen();
1237
1238 // If we haven't past the identifier yet (or where the identifier would be
1239 // stored, if this is an abstract declarator), then this is probably just
1240 // grouping parens.
1241 if (!D.isPastIdentifier()) {
1242 // Okay, this is probably a grouping paren. However, if this could be an
1243 // abstract-declarator, then this could also be the start of function
1244 // arguments (consider 'void()').
1245 bool isGrouping;
1246
1247 if (!D.mayOmitIdentifier()) {
1248 // If this can't be an abstract-declarator, this *must* be a grouping
1249 // paren, because we haven't seen the identifier yet.
1250 isGrouping = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001251 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Chris Lattner4b009652007-07-25 00:24:17 +00001252 isDeclarationSpecifier()) { // 'int(int)' is a function.
1253 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1254 // considered to be a type, not a K&R identifier-list.
1255 isGrouping = false;
1256 } else {
1257 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1258 isGrouping = true;
1259 }
1260
1261 // If this is a grouping paren, handle:
1262 // direct-declarator: '(' declarator ')'
1263 // direct-declarator: '(' attributes declarator ')'
1264 if (isGrouping) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001265 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001266 D.AddAttributes(ParseAttributes());
1267
1268 ParseDeclaratorInternal(D);
1269 // Match the ')'.
1270 MatchRHSPunctuation(tok::r_paren, StartLoc);
1271 return;
1272 }
1273
1274 // Okay, if this wasn't a grouping paren, it must be the start of a function
1275 // argument list. Recognize that this declarator will never have an
1276 // identifier (and remember where it would have been), then fall through to
1277 // the handling of argument lists.
1278 D.SetIdentifier(0, Tok.getLocation());
1279 }
1280
1281 // Okay, this is the parameter list of a function definition, or it is an
1282 // identifier list of a K&R-style function.
1283 bool IsVariadic;
1284 bool HasPrototype;
1285 bool ErrorEmitted = false;
1286
1287 // Build up an array of information about the parsed arguments.
1288 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1289 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1290
Chris Lattner34a01ad2007-10-09 17:33:22 +00001291 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001292 // int() -> no prototype, no '...'.
1293 IsVariadic = false;
1294 HasPrototype = false;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001295 } else if (Tok.is(tok::identifier) &&
Chris Lattner4b009652007-07-25 00:24:17 +00001296 // K&R identifier lists can't have typedefs as identifiers, per
1297 // C99 6.7.5.3p11.
1298 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1299 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1300 // normal declarators, not for abstract-declarators.
1301 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
1302
1303 // If there was no identifier specified, either we are in an
1304 // abstract-declarator, or we are in a parameter declarator which was found
1305 // to be abstract. In abstract-declarators, identifier lists are not valid,
1306 // diagnose this.
1307 if (!D.getIdentifier())
1308 Diag(Tok, diag::ext_ident_list_in_param);
1309
1310 // Remember this identifier in ParamInfo.
1311 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1312 Tok.getLocation(), 0));
1313
1314 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001315 while (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001316 // Eat the comma.
1317 ConsumeToken();
1318
Chris Lattner34a01ad2007-10-09 17:33:22 +00001319 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001320 Diag(Tok, diag::err_expected_ident);
1321 ErrorEmitted = true;
1322 break;
1323 }
1324
1325 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
1326
1327 // Verify that the argument identifier has not already been mentioned.
1328 if (!ParamsSoFar.insert(ParmII)) {
1329 Diag(Tok.getLocation(), diag::err_param_redefinition,ParmII->getName());
1330 ParmII = 0;
1331 }
1332
1333 // Remember this identifier in ParamInfo.
1334 if (ParmII)
1335 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1336 Tok.getLocation(), 0));
1337
1338 // Eat the identifier.
1339 ConsumeToken();
1340 }
1341
1342 // K&R 'prototype'.
1343 IsVariadic = false;
1344 HasPrototype = false;
1345 } else {
1346 // Finally, a normal, non-empty parameter type list.
1347
1348 // Enter function-declaration scope, limiting any declarators for struct
1349 // tags to the function prototype scope.
1350 // FIXME: is this needed?
Chris Lattnera7549902007-08-26 06:24:45 +00001351 EnterScope(Scope::DeclScope);
Chris Lattner4b009652007-07-25 00:24:17 +00001352
1353 IsVariadic = false;
1354 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001355 if (Tok.is(tok::ellipsis)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001356 IsVariadic = true;
1357
1358 // Check to see if this is "void(...)" which is not allowed.
1359 if (ParamInfo.empty()) {
1360 // Otherwise, parse parameter type list. If it starts with an
1361 // ellipsis, diagnose the malformed function.
1362 Diag(Tok, diag::err_ellipsis_first_arg);
1363 IsVariadic = false; // Treat this like 'void()'.
1364 }
1365
1366 // Consume the ellipsis.
1367 ConsumeToken();
1368 break;
1369 }
1370
1371 // Parse the declaration-specifiers.
1372 DeclSpec DS;
1373 ParseDeclarationSpecifiers(DS);
1374
1375 // Parse the declarator. This is "PrototypeContext", because we must
1376 // accept either 'declarator' or 'abstract-declarator' here.
1377 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1378 ParseDeclarator(ParmDecl);
1379
1380 // Parse GNU attributes, if present.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001381 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001382 ParmDecl.AddAttributes(ParseAttributes());
1383
1384 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
1385 // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted.
1386 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1387 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
1388 Diag(DS.getStorageClassSpecLoc(),
1389 diag::err_invalid_storage_class_in_func_decl);
1390 DS.ClearStorageClassSpecs();
1391 }
1392 if (DS.isThreadSpecified()) {
1393 Diag(DS.getThreadSpecLoc(),
1394 diag::err_invalid_storage_class_in_func_decl);
1395 DS.ClearStorageClassSpecs();
1396 }
1397
1398 // Inform the actions module about the parameter declarator, so it gets
1399 // added to the current scope.
1400 Action::TypeResult ParamTy =
Steve Naroff0acc9c92007-09-15 18:49:24 +00001401 Actions.ActOnParamDeclaratorType(CurScope, ParmDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001402
1403 // Remember this parsed parameter in ParamInfo.
1404 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1405
1406 // Verify that the argument identifier has not already been mentioned.
1407 if (ParmII && !ParamsSoFar.insert(ParmII)) {
1408 Diag(ParmDecl.getIdentifierLoc(), diag::err_param_redefinition,
1409 ParmII->getName());
1410 ParmII = 0;
1411 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00001412
1413 // If no parameter was specified, verify that *something* was specified,
1414 // otherwise we have a missing type and identifier.
1415 if (!DS.hasTypeSpecifier()) {
1416 if (ParmII)
1417 Diag(ParmDecl.getIdentifierLoc(),
1418 diag::err_param_requires_type_specifier, ParmII->getName());
1419 else
1420 Diag(Tok.getLocation(), diag::err_anon_param_requires_type_specifier);
1421
1422 // Default the parameter to 'int'.
1423 const char *PrevSpec = 0;
1424 DS.SetTypeSpecType(DeclSpec::TST_int, Tok.getLocation(), PrevSpec);
1425 }
Chris Lattner4b009652007-07-25 00:24:17 +00001426
Steve Naroff91b03f72007-08-28 03:03:08 +00001427 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Nate Begeman84079d72007-11-13 22:14:47 +00001428 ParmDecl.getIdentifierLoc(), ParamTy.Val, ParmDecl.getInvalidType(),
1429 ParmDecl.getDeclSpec().getAttributes()));
1430
1431 // Ownership of DeclSpec has been handed off to ParamInfo.
1432 DS.clearAttributes();
Chris Lattner4b009652007-07-25 00:24:17 +00001433
1434 // If the next token is a comma, consume it and keep reading arguments.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001435 if (Tok.isNot(tok::comma)) break;
Chris Lattner4b009652007-07-25 00:24:17 +00001436
1437 // Consume the comma.
1438 ConsumeToken();
1439 }
1440
1441 HasPrototype = true;
1442
1443 // Leave prototype scope.
1444 ExitScope();
1445 }
1446
1447 // Remember that we parsed a function type, and remember the attributes.
1448 if (!ErrorEmitted)
1449 D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic,
1450 &ParamInfo[0], ParamInfo.size(),
1451 StartLoc));
1452
1453 // If we have the closing ')', eat it and we're done.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001454 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001455 ConsumeParen();
1456 } else {
1457 // If an error happened earlier parsing something else in the proto, don't
1458 // issue another error.
1459 if (!ErrorEmitted)
1460 Diag(Tok, diag::err_expected_rparen);
1461 SkipUntil(tok::r_paren);
1462 }
1463}
1464
1465
1466/// [C90] direct-declarator '[' constant-expression[opt] ']'
1467/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1468/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1469/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1470/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1471void Parser::ParseBracketDeclarator(Declarator &D) {
1472 SourceLocation StartLoc = ConsumeBracket();
1473
1474 // If valid, this location is the position where we read the 'static' keyword.
1475 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001476 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001477 StaticLoc = ConsumeToken();
1478
1479 // If there is a type-qualifier-list, read it now.
1480 DeclSpec DS;
1481 ParseTypeQualifierListOpt(DS);
1482
1483 // If we haven't already read 'static', check to see if there is one after the
1484 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001485 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001486 StaticLoc = ConsumeToken();
1487
1488 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1489 bool isStar = false;
1490 ExprResult NumElements(false);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001491 if (Tok.is(tok::star)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001492 // Remember the '*' token, in case we have to un-get it.
1493 Token StarTok = Tok;
1494 ConsumeToken();
1495
1496 // Check that the ']' token is present to avoid incorrectly parsing
1497 // expressions starting with '*' as [*].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001498 if (Tok.is(tok::r_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001499 if (StaticLoc.isValid())
1500 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1501 StaticLoc = SourceLocation(); // Drop the static.
1502 isStar = true;
1503 } else {
1504 // Otherwise, the * must have been some expression (such as '*ptr') that
1505 // started an assignment-expr. We already consumed the token, but now we
1506 // need to reparse it. This handles cases like 'X[*p + 4]'
1507 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
1508 }
Chris Lattner34a01ad2007-10-09 17:33:22 +00001509 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001510 // Parse the assignment-expression now.
1511 NumElements = ParseAssignmentExpression();
1512 }
1513
1514 // If there was an error parsing the assignment-expression, recover.
1515 if (NumElements.isInvalid) {
1516 // If the expression was invalid, skip it.
1517 SkipUntil(tok::r_square);
1518 return;
1519 }
1520
1521 MatchRHSPunctuation(tok::r_square, StartLoc);
1522
1523 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1524 // it was not a constant expression.
1525 if (!getLang().C99) {
1526 // TODO: check C90 array constant exprness.
1527 if (isStar || StaticLoc.isValid() ||
1528 0/*TODO: NumElts is not a C90 constantexpr */)
1529 Diag(StartLoc, diag::ext_c99_array_usage);
1530 }
1531
1532 // Remember that we parsed a pointer type, and remember the type-quals.
1533 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1534 StaticLoc.isValid(), isStar,
1535 NumElements.Val, StartLoc));
1536}
1537
Steve Naroff7cbb1462007-07-31 12:34:36 +00001538/// [GNU] typeof-specifier:
1539/// typeof ( expressions )
1540/// typeof ( type-name )
1541///
1542void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001543 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00001544 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00001545 SourceLocation StartLoc = ConsumeToken();
1546
Chris Lattner34a01ad2007-10-09 17:33:22 +00001547 if (Tok.isNot(tok::l_paren)) {
Steve Naroff14bbce82007-08-02 02:53:48 +00001548 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1549 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00001550 }
1551 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1552
1553 if (isTypeSpecifierQualifier()) {
1554 TypeTy *Ty = ParseTypeName();
1555
Steve Naroff4c255ab2007-07-31 23:56:32 +00001556 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1557
Chris Lattner34a01ad2007-10-09 17:33:22 +00001558 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001559 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001560 return;
1561 }
1562 RParenLoc = ConsumeParen();
1563 const char *PrevSpec = 0;
1564 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1565 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1566 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001567 } else { // we have an expression.
1568 ExprResult Result = ParseExpression();
Steve Naroff4c255ab2007-07-31 23:56:32 +00001569
Chris Lattner34a01ad2007-10-09 17:33:22 +00001570 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001571 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001572 return;
1573 }
1574 RParenLoc = ConsumeParen();
1575 const char *PrevSpec = 0;
1576 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1577 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1578 Result.Val))
1579 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001580 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00001581}
1582