blob: 2fc4d0de216d991c5c830c01987a101e489b2c50 [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
519 case tok::kw_short:
520 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
521 break;
522 case tok::kw_long:
523 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
524 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
525 else
526 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
527 break;
528 case tok::kw_signed:
529 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
530 break;
531 case tok::kw_unsigned:
532 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
533 break;
534 case tok::kw__Complex:
535 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
536 break;
537 case tok::kw__Imaginary:
538 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
539 break;
540 case tok::kw_void:
541 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
542 break;
543 case tok::kw_char:
544 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
545 break;
546 case tok::kw_int:
547 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
548 break;
549 case tok::kw_float:
550 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
551 break;
552 case tok::kw_double:
553 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
554 break;
555 case tok::kw_bool: // [C++ 2.11p1]
556 case tok::kw__Bool:
557 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
558 break;
559 case tok::kw__Decimal32:
560 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
561 break;
562 case tok::kw__Decimal64:
563 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
564 break;
565 case tok::kw__Decimal128:
566 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
567 break;
568
569 case tok::kw_struct:
570 case tok::kw_union:
571 ParseStructUnionSpecifier(DS);
572 continue;
573 case tok::kw_enum:
574 ParseEnumSpecifier(DS);
575 continue;
576
Steve Naroff7cbb1462007-07-31 12:34:36 +0000577 // GNU typeof support.
578 case tok::kw_typeof:
579 ParseTypeofSpecifier(DS);
580 continue;
581
Chris Lattner4b009652007-07-25 00:24:17 +0000582 // type-qualifier
583 case tok::kw_const:
584 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
585 getLang())*2;
586 break;
587 case tok::kw_volatile:
588 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
589 getLang())*2;
590 break;
591 case tok::kw_restrict:
592 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
593 getLang())*2;
594 break;
595
596 // function-specifier
597 case tok::kw_inline:
598 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
599 break;
600 }
601 // If the specifier combination wasn't legal, issue a diagnostic.
602 if (isInvalid) {
603 assert(PrevSpec && "Method did not return previous specifier!");
604 if (isInvalid == 1) // Error.
605 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
606 else // extwarn.
607 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
608 }
609 DS.Range.setEnd(Tok.getLocation());
610 ConsumeToken();
611 }
612}
613
614/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
615/// the first token has already been read and has been turned into an instance
616/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
617/// otherwise it returns false and fills in Decl.
618bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
619 AttributeList *Attr = 0;
620 // If attributes exist after tag, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000621 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000622 Attr = ParseAttributes();
623
624 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000625 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000626 Diag(Tok, diag::err_expected_ident_lbrace);
627
628 // Skip the rest of this declarator, up until the comma or semicolon.
629 SkipUntil(tok::comma, true);
630 return true;
631 }
632
633 // If an identifier is present, consume and remember it.
634 IdentifierInfo *Name = 0;
635 SourceLocation NameLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000636 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000637 Name = Tok.getIdentifierInfo();
638 NameLoc = ConsumeToken();
639 }
640
641 // There are three options here. If we have 'struct foo;', then this is a
642 // forward declaration. If we have 'struct foo {...' then this is a
643 // definition. Otherwise we have something like 'struct foo xyz', a reference.
644 //
645 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
646 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
647 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
648 //
649 Action::TagKind TK;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000650 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000651 TK = Action::TK_Definition;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000652 else if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000653 TK = Action::TK_Declaration;
654 else
655 TK = Action::TK_Reference;
Steve Naroff0acc9c92007-09-15 18:49:24 +0000656 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Chris Lattner4b009652007-07-25 00:24:17 +0000657 return false;
658}
659
660
661/// ParseStructUnionSpecifier
662/// struct-or-union-specifier: [C99 6.7.2.1]
663/// struct-or-union identifier[opt] '{' struct-contents '}'
664/// struct-or-union identifier
665/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
666/// '}' attributes[opt]
667/// [GNU] struct-or-union attributes[opt] identifier
668/// struct-or-union:
669/// 'struct'
670/// 'union'
671///
672void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000673 assert((Tok.is(tok::kw_struct) || Tok.is(tok::kw_union)) &&
674 "Not a struct/union specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000675 DeclSpec::TST TagType =
Chris Lattner34a01ad2007-10-09 17:33:22 +0000676 Tok.is(tok::kw_union) ? DeclSpec::TST_union : DeclSpec::TST_struct;
Chris Lattner4b009652007-07-25 00:24:17 +0000677 SourceLocation StartLoc = ConsumeToken();
678
Steve Naroff73a07032008-02-07 03:50:06 +0000679 if (getLang().Microsoft && Tok.is(tok::kw___declspec))
680 FuzzyParseMicrosoftDeclspec();
681
Chris Lattner4b009652007-07-25 00:24:17 +0000682 // Parse the tag portion of this.
683 DeclTy *TagDecl;
684 if (ParseTag(TagDecl, TagType, StartLoc))
685 return;
686
687 // If there is a body, parse it and inform the actions module.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000688 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000689 ParseStructUnionBody(StartLoc, TagType, TagDecl);
690
691 const char *PrevSpec = 0;
692 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
693 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
694}
695
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000696/// ParseStructDeclaration - Parse a struct declaration without the terminating
697/// semicolon.
698///
Chris Lattner4b009652007-07-25 00:24:17 +0000699/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000700/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000701/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000702/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000703/// struct-declarator-list:
704/// struct-declarator
705/// struct-declarator-list ',' struct-declarator
706/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
707/// struct-declarator:
708/// declarator
709/// [GNU] declarator attributes[opt]
710/// declarator[opt] ':' constant-expression
711/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
712///
Steve Naroffa9adf112007-08-20 22:28:22 +0000713void Parser::ParseStructDeclaration(DeclTy *TagDecl,
Steve Naroffc02f4a92007-08-28 16:31:47 +0000714 llvm::SmallVectorImpl<DeclTy*> &FieldDecls) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000715 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000716 if (Tok.is(tok::kw___extension__))
Steve Naroffa9adf112007-08-20 22:28:22 +0000717 ConsumeToken();
718
719 // Parse the common specifier-qualifiers-list piece.
720 DeclSpec DS;
721 SourceLocation SpecQualLoc = Tok.getLocation();
722 ParseSpecifierQualifierList(DS);
723 // TODO: Does specifier-qualifier list correctly check that *something* is
724 // specified?
725
726 // If there are no declarators, issue a warning.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000727 if (Tok.is(tok::semi)) {
Steve Naroff73a07032008-02-07 03:50:06 +0000728 if (!getLang().Microsoft) // MS allows unnamed struct/union fields.
729 Diag(SpecQualLoc, diag::w_no_declarators);
Steve Naroffa9adf112007-08-20 22:28:22 +0000730 return;
731 }
732
733 // Read struct-declarators until we find the semicolon.
734 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
735
736 while (1) {
737 /// struct-declarator: declarator
738 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000739 if (Tok.isNot(tok::colon))
Steve Naroffa9adf112007-08-20 22:28:22 +0000740 ParseDeclarator(DeclaratorInfo);
741
742 ExprTy *BitfieldSize = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000743 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000744 ConsumeToken();
745 ExprResult Res = ParseConstantExpression();
746 if (Res.isInvalid) {
747 SkipUntil(tok::semi, true, true);
748 } else {
749 BitfieldSize = Res.Val;
750 }
751 }
752
753 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000754 if (Tok.is(tok::kw___attribute))
Steve Naroffa9adf112007-08-20 22:28:22 +0000755 DeclaratorInfo.AddAttributes(ParseAttributes());
756
757 // Install the declarator into the current TagDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000758 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl, SpecQualLoc,
Steve Naroffa9adf112007-08-20 22:28:22 +0000759 DeclaratorInfo, BitfieldSize);
760 FieldDecls.push_back(Field);
761
762 // If we don't have a comma, it is either the end of the list (a ';')
763 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000764 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000765 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000766
767 // Consume the comma.
768 ConsumeToken();
769
770 // Parse the next declarator.
771 DeclaratorInfo.clear();
772
773 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000774 if (Tok.is(tok::kw___attribute))
Steve Naroffa9adf112007-08-20 22:28:22 +0000775 DeclaratorInfo.AddAttributes(ParseAttributes());
776 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000777}
778
779/// ParseStructUnionBody
780/// struct-contents:
781/// struct-declaration-list
782/// [EXT] empty
783/// [GNU] "struct-declaration-list" without terminatoring ';'
784/// struct-declaration-list:
785/// struct-declaration
786/// struct-declaration-list struct-declaration
787/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
788///
Chris Lattner4b009652007-07-25 00:24:17 +0000789void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
790 unsigned TagType, DeclTy *TagDecl) {
791 SourceLocation LBraceLoc = ConsumeBrace();
792
793 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
794 // C++.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000795 if (Tok.is(tok::r_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000796 Diag(Tok, diag::ext_empty_struct_union_enum,
797 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
798
799 llvm::SmallVector<DeclTy*, 32> FieldDecls;
800
801 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000802 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000803 // Each iteration of this loop reads one struct-declaration.
804
805 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000806 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000807 Diag(Tok, diag::ext_extra_struct_semi);
808 ConsumeToken();
809 continue;
810 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000811 ParseStructDeclaration(TagDecl, FieldDecls);
Chris Lattner4b009652007-07-25 00:24:17 +0000812
Chris Lattner34a01ad2007-10-09 17:33:22 +0000813 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000814 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +0000815 } else if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000816 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
817 break;
818 } else {
819 Diag(Tok, diag::err_expected_semi_decl_list);
820 // Skip to end of block or statement
821 SkipUntil(tok::r_brace, true, true);
822 }
823 }
824
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000825 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000826
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000827 Actions.ActOnFields(CurScope,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000828 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
829 LBraceLoc, RBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000830
831 AttributeList *AttrList = 0;
832 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000833 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000834 AttrList = ParseAttributes(); // FIXME: where should I put them?
835}
836
837
838/// ParseEnumSpecifier
839/// enum-specifier: [C99 6.7.2.2]
840/// 'enum' identifier[opt] '{' enumerator-list '}'
841/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
842/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
843/// '}' attributes[opt]
844/// 'enum' identifier
845/// [GNU] 'enum' attributes[opt] identifier
846void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000847 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000848 SourceLocation StartLoc = ConsumeToken();
849
850 // Parse the tag portion of this.
851 DeclTy *TagDecl;
852 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
853 return;
854
Chris Lattner34a01ad2007-10-09 17:33:22 +0000855 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000856 ParseEnumBody(StartLoc, TagDecl);
857
858 // TODO: semantic analysis on the declspec for enums.
859 const char *PrevSpec = 0;
860 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
861 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
862}
863
864/// ParseEnumBody - Parse a {} enclosed enumerator-list.
865/// enumerator-list:
866/// enumerator
867/// enumerator-list ',' enumerator
868/// enumerator:
869/// enumeration-constant
870/// enumeration-constant '=' constant-expression
871/// enumeration-constant:
872/// identifier
873///
874void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
875 SourceLocation LBraceLoc = ConsumeBrace();
876
Chris Lattnerc9a92452007-08-27 17:24:30 +0000877 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +0000878 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000879 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
880
881 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
882
883 DeclTy *LastEnumConstDecl = 0;
884
885 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000886 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000887 IdentifierInfo *Ident = Tok.getIdentifierInfo();
888 SourceLocation IdentLoc = ConsumeToken();
889
890 SourceLocation EqualLoc;
891 ExprTy *AssignedVal = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000892 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000893 EqualLoc = ConsumeToken();
894 ExprResult Res = ParseConstantExpression();
895 if (Res.isInvalid)
896 SkipUntil(tok::comma, tok::r_brace, true, true);
897 else
898 AssignedVal = Res.Val;
899 }
900
901 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000902 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +0000903 LastEnumConstDecl,
904 IdentLoc, Ident,
905 EqualLoc, AssignedVal);
906 EnumConstantDecls.push_back(EnumConstDecl);
907 LastEnumConstDecl = EnumConstDecl;
908
Chris Lattner34a01ad2007-10-09 17:33:22 +0000909 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000910 break;
911 SourceLocation CommaLoc = ConsumeToken();
912
Chris Lattner34a01ad2007-10-09 17:33:22 +0000913 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +0000914 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
915 }
916
917 // Eat the }.
918 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
919
Steve Naroff0acc9c92007-09-15 18:49:24 +0000920 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +0000921 EnumConstantDecls.size());
922
923 DeclTy *AttrList = 0;
924 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000925 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000926 AttrList = ParseAttributes(); // FIXME: where do they do?
927}
928
929/// isTypeSpecifierQualifier - Return true if the current token could be the
930/// start of a specifier-qualifier-list.
931bool Parser::isTypeSpecifierQualifier() const {
932 switch (Tok.getKind()) {
933 default: return false;
934 // GNU attributes support.
935 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000936 // GNU typeof support.
937 case tok::kw_typeof:
938
Chris Lattner4b009652007-07-25 00:24:17 +0000939 // type-specifiers
940 case tok::kw_short:
941 case tok::kw_long:
942 case tok::kw_signed:
943 case tok::kw_unsigned:
944 case tok::kw__Complex:
945 case tok::kw__Imaginary:
946 case tok::kw_void:
947 case tok::kw_char:
948 case tok::kw_int:
949 case tok::kw_float:
950 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000951 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +0000952 case tok::kw__Bool:
953 case tok::kw__Decimal32:
954 case tok::kw__Decimal64:
955 case tok::kw__Decimal128:
956
957 // struct-or-union-specifier
958 case tok::kw_struct:
959 case tok::kw_union:
960 // enum-specifier
961 case tok::kw_enum:
962
963 // type-qualifier
964 case tok::kw_const:
965 case tok::kw_volatile:
966 case tok::kw_restrict:
967 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/// isDeclarationSpecifier() - Return true if the current token is part of a
976/// declaration specifier.
977bool Parser::isDeclarationSpecifier() const {
978 switch (Tok.getKind()) {
979 default: return false;
980 // storage-class-specifier
981 case tok::kw_typedef:
982 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +0000983 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +0000984 case tok::kw_static:
985 case tok::kw_auto:
986 case tok::kw_register:
987 case tok::kw___thread:
988
989 // type-specifiers
990 case tok::kw_short:
991 case tok::kw_long:
992 case tok::kw_signed:
993 case tok::kw_unsigned:
994 case tok::kw__Complex:
995 case tok::kw__Imaginary:
996 case tok::kw_void:
997 case tok::kw_char:
998 case tok::kw_int:
999 case tok::kw_float:
1000 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001001 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001002 case tok::kw__Bool:
1003 case tok::kw__Decimal32:
1004 case tok::kw__Decimal64:
1005 case tok::kw__Decimal128:
1006
1007 // struct-or-union-specifier
1008 case tok::kw_struct:
1009 case tok::kw_union:
1010 // enum-specifier
1011 case tok::kw_enum:
1012
1013 // type-qualifier
1014 case tok::kw_const:
1015 case tok::kw_volatile:
1016 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001017
Chris Lattner4b009652007-07-25 00:24:17 +00001018 // function-specifier
1019 case tok::kw_inline:
Chris Lattnere35d2582007-08-09 16:40:21 +00001020
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001021 // GNU typeof support.
1022 case tok::kw_typeof:
1023
1024 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001025 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001026 return true;
1027
1028 // typedef-name
1029 case tok::identifier:
1030 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001031 }
1032}
1033
1034
1035/// ParseTypeQualifierListOpt
1036/// type-qualifier-list: [C99 6.7.5]
1037/// type-qualifier
1038/// [GNU] attributes
1039/// type-qualifier-list type-qualifier
1040/// [GNU] type-qualifier-list attributes
1041///
1042void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1043 while (1) {
1044 int isInvalid = false;
1045 const char *PrevSpec = 0;
1046 SourceLocation Loc = Tok.getLocation();
1047
1048 switch (Tok.getKind()) {
1049 default:
1050 // If this is not a type-qualifier token, we're done reading type
1051 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +00001052 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +00001053 return;
1054 case tok::kw_const:
1055 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1056 getLang())*2;
1057 break;
1058 case tok::kw_volatile:
1059 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1060 getLang())*2;
1061 break;
1062 case tok::kw_restrict:
1063 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1064 getLang())*2;
1065 break;
1066 case tok::kw___attribute:
1067 DS.AddAttributes(ParseAttributes());
1068 continue; // do *not* consume the next token!
1069 }
1070
1071 // If the specifier combination wasn't legal, issue a diagnostic.
1072 if (isInvalid) {
1073 assert(PrevSpec && "Method did not return previous specifier!");
1074 if (isInvalid == 1) // Error.
1075 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1076 else // extwarn.
1077 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1078 }
1079 ConsumeToken();
1080 }
1081}
1082
1083
1084/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1085///
1086void Parser::ParseDeclarator(Declarator &D) {
1087 /// This implements the 'declarator' production in the C grammar, then checks
1088 /// for well-formedness and issues diagnostics.
1089 ParseDeclaratorInternal(D);
1090
1091 // TODO: validate D.
1092
1093}
1094
1095/// ParseDeclaratorInternal
1096/// declarator: [C99 6.7.5]
1097/// pointer[opt] direct-declarator
1098/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1099/// [GNU] '&' restrict[opt] attributes[opt] declarator
1100///
1101/// pointer: [C99 6.7.5]
1102/// '*' type-qualifier-list[opt]
1103/// '*' type-qualifier-list[opt] pointer
1104///
1105void Parser::ParseDeclaratorInternal(Declarator &D) {
1106 tok::TokenKind Kind = Tok.getKind();
1107
1108 // Not a pointer or C++ reference.
1109 if (Kind != tok::star && !(Kind == tok::amp && getLang().CPlusPlus))
1110 return ParseDirectDeclarator(D);
1111
1112 // Otherwise, '*' -> pointer or '&' -> reference.
1113 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1114
1115 if (Kind == tok::star) {
1116 // Is a pointer
1117 DeclSpec DS;
1118
1119 ParseTypeQualifierListOpt(DS);
1120
1121 // Recursively parse the declarator.
1122 ParseDeclaratorInternal(D);
1123
1124 // Remember that we parsed a pointer type, and remember the type-quals.
1125 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc));
1126 } else {
1127 // Is a reference
1128 DeclSpec DS;
1129
1130 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1131 // cv-qualifiers are introduced through the use of a typedef or of a
1132 // template type argument, in which case the cv-qualifiers are ignored.
1133 //
1134 // [GNU] Retricted references are allowed.
1135 // [GNU] Attributes on references are allowed.
1136 ParseTypeQualifierListOpt(DS);
1137
1138 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1139 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1140 Diag(DS.getConstSpecLoc(),
1141 diag::err_invalid_reference_qualifier_application,
1142 "const");
1143 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1144 Diag(DS.getVolatileSpecLoc(),
1145 diag::err_invalid_reference_qualifier_application,
1146 "volatile");
1147 }
1148
1149 // Recursively parse the declarator.
1150 ParseDeclaratorInternal(D);
1151
1152 // Remember that we parsed a reference type. It doesn't have type-quals.
1153 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc));
1154 }
1155}
1156
1157/// ParseDirectDeclarator
1158/// direct-declarator: [C99 6.7.5]
1159/// identifier
1160/// '(' declarator ')'
1161/// [GNU] '(' attributes declarator ')'
1162/// [C90] direct-declarator '[' constant-expression[opt] ']'
1163/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1164/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1165/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1166/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1167/// direct-declarator '(' parameter-type-list ')'
1168/// direct-declarator '(' identifier-list[opt] ')'
1169/// [GNU] direct-declarator '(' parameter-forward-declarations
1170/// parameter-type-list[opt] ')'
1171///
1172void Parser::ParseDirectDeclarator(Declarator &D) {
1173 // Parse the first direct-declarator seen.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001174 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001175 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1176 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1177 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001178 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001179 // direct-declarator: '(' declarator ')'
1180 // direct-declarator: '(' attributes declarator ')'
1181 // Example: 'char (*X)' or 'int (*XX)(void)'
1182 ParseParenDeclarator(D);
1183 } else if (D.mayOmitIdentifier()) {
1184 // This could be something simple like "int" (in which case the declarator
1185 // portion is empty), if an abstract-declarator is allowed.
1186 D.SetIdentifier(0, Tok.getLocation());
1187 } else {
1188 // Expected identifier or '('.
1189 Diag(Tok, diag::err_expected_ident_lparen);
1190 D.SetIdentifier(0, Tok.getLocation());
1191 }
1192
1193 assert(D.isPastIdentifier() &&
1194 "Haven't past the location of the identifier yet?");
1195
1196 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001197 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001198 ParseParenDeclarator(D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001199 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001200 ParseBracketDeclarator(D);
1201 } else {
1202 break;
1203 }
1204 }
1205}
1206
1207/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
1208/// either be before the identifier (in which case these are just grouping
1209/// parens for precedence) or it may be after the identifier, in which case
1210/// these are function arguments.
1211///
1212/// This method also handles this portion of the grammar:
1213/// parameter-type-list: [C99 6.7.5]
1214/// parameter-list
1215/// parameter-list ',' '...'
1216///
1217/// parameter-list: [C99 6.7.5]
1218/// parameter-declaration
1219/// parameter-list ',' parameter-declaration
1220///
1221/// parameter-declaration: [C99 6.7.5]
1222/// declaration-specifiers declarator
1223/// [GNU] declaration-specifiers declarator attributes
1224/// declaration-specifiers abstract-declarator[opt]
1225/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1226///
1227/// identifier-list: [C99 6.7.5]
1228/// identifier
1229/// identifier-list ',' identifier
1230///
1231void Parser::ParseParenDeclarator(Declarator &D) {
1232 SourceLocation StartLoc = ConsumeParen();
1233
1234 // If we haven't past the identifier yet (or where the identifier would be
1235 // stored, if this is an abstract declarator), then this is probably just
1236 // grouping parens.
1237 if (!D.isPastIdentifier()) {
1238 // Okay, this is probably a grouping paren. However, if this could be an
1239 // abstract-declarator, then this could also be the start of function
1240 // arguments (consider 'void()').
1241 bool isGrouping;
1242
1243 if (!D.mayOmitIdentifier()) {
1244 // If this can't be an abstract-declarator, this *must* be a grouping
1245 // paren, because we haven't seen the identifier yet.
1246 isGrouping = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001247 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Chris Lattner4b009652007-07-25 00:24:17 +00001248 isDeclarationSpecifier()) { // 'int(int)' is a function.
1249 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1250 // considered to be a type, not a K&R identifier-list.
1251 isGrouping = false;
1252 } else {
1253 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1254 isGrouping = true;
1255 }
1256
1257 // If this is a grouping paren, handle:
1258 // direct-declarator: '(' declarator ')'
1259 // direct-declarator: '(' attributes declarator ')'
1260 if (isGrouping) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001261 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001262 D.AddAttributes(ParseAttributes());
1263
1264 ParseDeclaratorInternal(D);
1265 // Match the ')'.
1266 MatchRHSPunctuation(tok::r_paren, StartLoc);
1267 return;
1268 }
1269
1270 // Okay, if this wasn't a grouping paren, it must be the start of a function
1271 // argument list. Recognize that this declarator will never have an
1272 // identifier (and remember where it would have been), then fall through to
1273 // the handling of argument lists.
1274 D.SetIdentifier(0, Tok.getLocation());
1275 }
1276
1277 // Okay, this is the parameter list of a function definition, or it is an
1278 // identifier list of a K&R-style function.
1279 bool IsVariadic;
1280 bool HasPrototype;
1281 bool ErrorEmitted = false;
1282
1283 // Build up an array of information about the parsed arguments.
1284 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1285 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1286
Chris Lattner34a01ad2007-10-09 17:33:22 +00001287 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001288 // int() -> no prototype, no '...'.
1289 IsVariadic = false;
1290 HasPrototype = false;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001291 } else if (Tok.is(tok::identifier) &&
Chris Lattner4b009652007-07-25 00:24:17 +00001292 // K&R identifier lists can't have typedefs as identifiers, per
1293 // C99 6.7.5.3p11.
1294 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1295 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1296 // normal declarators, not for abstract-declarators.
1297 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
1298
1299 // If there was no identifier specified, either we are in an
1300 // abstract-declarator, or we are in a parameter declarator which was found
1301 // to be abstract. In abstract-declarators, identifier lists are not valid,
1302 // diagnose this.
1303 if (!D.getIdentifier())
1304 Diag(Tok, diag::ext_ident_list_in_param);
1305
1306 // Remember this identifier in ParamInfo.
1307 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1308 Tok.getLocation(), 0));
1309
1310 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001311 while (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001312 // Eat the comma.
1313 ConsumeToken();
1314
Chris Lattner34a01ad2007-10-09 17:33:22 +00001315 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001316 Diag(Tok, diag::err_expected_ident);
1317 ErrorEmitted = true;
1318 break;
1319 }
1320
1321 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
1322
1323 // Verify that the argument identifier has not already been mentioned.
1324 if (!ParamsSoFar.insert(ParmII)) {
1325 Diag(Tok.getLocation(), diag::err_param_redefinition,ParmII->getName());
1326 ParmII = 0;
1327 }
1328
1329 // Remember this identifier in ParamInfo.
1330 if (ParmII)
1331 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1332 Tok.getLocation(), 0));
1333
1334 // Eat the identifier.
1335 ConsumeToken();
1336 }
1337
1338 // K&R 'prototype'.
1339 IsVariadic = false;
1340 HasPrototype = false;
1341 } else {
1342 // Finally, a normal, non-empty parameter type list.
1343
1344 // Enter function-declaration scope, limiting any declarators for struct
1345 // tags to the function prototype scope.
1346 // FIXME: is this needed?
Chris Lattnera7549902007-08-26 06:24:45 +00001347 EnterScope(Scope::DeclScope);
Chris Lattner4b009652007-07-25 00:24:17 +00001348
1349 IsVariadic = false;
1350 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001351 if (Tok.is(tok::ellipsis)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001352 IsVariadic = true;
1353
1354 // Check to see if this is "void(...)" which is not allowed.
1355 if (ParamInfo.empty()) {
1356 // Otherwise, parse parameter type list. If it starts with an
1357 // ellipsis, diagnose the malformed function.
1358 Diag(Tok, diag::err_ellipsis_first_arg);
1359 IsVariadic = false; // Treat this like 'void()'.
1360 }
1361
1362 // Consume the ellipsis.
1363 ConsumeToken();
1364 break;
1365 }
1366
1367 // Parse the declaration-specifiers.
1368 DeclSpec DS;
1369 ParseDeclarationSpecifiers(DS);
1370
1371 // Parse the declarator. This is "PrototypeContext", because we must
1372 // accept either 'declarator' or 'abstract-declarator' here.
1373 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1374 ParseDeclarator(ParmDecl);
1375
1376 // Parse GNU attributes, if present.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001377 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001378 ParmDecl.AddAttributes(ParseAttributes());
1379
1380 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
1381 // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted.
1382 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1383 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
1384 Diag(DS.getStorageClassSpecLoc(),
1385 diag::err_invalid_storage_class_in_func_decl);
1386 DS.ClearStorageClassSpecs();
1387 }
1388 if (DS.isThreadSpecified()) {
1389 Diag(DS.getThreadSpecLoc(),
1390 diag::err_invalid_storage_class_in_func_decl);
1391 DS.ClearStorageClassSpecs();
1392 }
1393
1394 // Inform the actions module about the parameter declarator, so it gets
1395 // added to the current scope.
1396 Action::TypeResult ParamTy =
Steve Naroff0acc9c92007-09-15 18:49:24 +00001397 Actions.ActOnParamDeclaratorType(CurScope, ParmDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001398
1399 // Remember this parsed parameter in ParamInfo.
1400 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1401
1402 // Verify that the argument identifier has not already been mentioned.
1403 if (ParmII && !ParamsSoFar.insert(ParmII)) {
1404 Diag(ParmDecl.getIdentifierLoc(), diag::err_param_redefinition,
1405 ParmII->getName());
1406 ParmII = 0;
1407 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00001408
1409 // If no parameter was specified, verify that *something* was specified,
1410 // otherwise we have a missing type and identifier.
1411 if (!DS.hasTypeSpecifier()) {
1412 if (ParmII)
1413 Diag(ParmDecl.getIdentifierLoc(),
1414 diag::err_param_requires_type_specifier, ParmII->getName());
1415 else
1416 Diag(Tok.getLocation(), diag::err_anon_param_requires_type_specifier);
1417
1418 // Default the parameter to 'int'.
1419 const char *PrevSpec = 0;
1420 DS.SetTypeSpecType(DeclSpec::TST_int, Tok.getLocation(), PrevSpec);
1421 }
Chris Lattner4b009652007-07-25 00:24:17 +00001422
Steve Naroff91b03f72007-08-28 03:03:08 +00001423 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Nate Begeman84079d72007-11-13 22:14:47 +00001424 ParmDecl.getIdentifierLoc(), ParamTy.Val, ParmDecl.getInvalidType(),
1425 ParmDecl.getDeclSpec().getAttributes()));
1426
1427 // Ownership of DeclSpec has been handed off to ParamInfo.
1428 DS.clearAttributes();
Chris Lattner4b009652007-07-25 00:24:17 +00001429
1430 // If the next token is a comma, consume it and keep reading arguments.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001431 if (Tok.isNot(tok::comma)) break;
Chris Lattner4b009652007-07-25 00:24:17 +00001432
1433 // Consume the comma.
1434 ConsumeToken();
1435 }
1436
1437 HasPrototype = true;
1438
1439 // Leave prototype scope.
1440 ExitScope();
1441 }
1442
1443 // Remember that we parsed a function type, and remember the attributes.
1444 if (!ErrorEmitted)
1445 D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic,
1446 &ParamInfo[0], ParamInfo.size(),
1447 StartLoc));
1448
1449 // If we have the closing ')', eat it and we're done.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001450 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001451 ConsumeParen();
1452 } else {
1453 // If an error happened earlier parsing something else in the proto, don't
1454 // issue another error.
1455 if (!ErrorEmitted)
1456 Diag(Tok, diag::err_expected_rparen);
1457 SkipUntil(tok::r_paren);
1458 }
1459}
1460
1461
1462/// [C90] direct-declarator '[' constant-expression[opt] ']'
1463/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1464/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1465/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1466/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1467void Parser::ParseBracketDeclarator(Declarator &D) {
1468 SourceLocation StartLoc = ConsumeBracket();
1469
1470 // If valid, this location is the position where we read the 'static' keyword.
1471 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001472 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001473 StaticLoc = ConsumeToken();
1474
1475 // If there is a type-qualifier-list, read it now.
1476 DeclSpec DS;
1477 ParseTypeQualifierListOpt(DS);
1478
1479 // If we haven't already read 'static', check to see if there is one after the
1480 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001481 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001482 StaticLoc = ConsumeToken();
1483
1484 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1485 bool isStar = false;
1486 ExprResult NumElements(false);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001487 if (Tok.is(tok::star)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001488 // Remember the '*' token, in case we have to un-get it.
1489 Token StarTok = Tok;
1490 ConsumeToken();
1491
1492 // Check that the ']' token is present to avoid incorrectly parsing
1493 // expressions starting with '*' as [*].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001494 if (Tok.is(tok::r_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001495 if (StaticLoc.isValid())
1496 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1497 StaticLoc = SourceLocation(); // Drop the static.
1498 isStar = true;
1499 } else {
1500 // Otherwise, the * must have been some expression (such as '*ptr') that
1501 // started an assignment-expr. We already consumed the token, but now we
1502 // need to reparse it. This handles cases like 'X[*p + 4]'
1503 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
1504 }
Chris Lattner34a01ad2007-10-09 17:33:22 +00001505 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001506 // Parse the assignment-expression now.
1507 NumElements = ParseAssignmentExpression();
1508 }
1509
1510 // If there was an error parsing the assignment-expression, recover.
1511 if (NumElements.isInvalid) {
1512 // If the expression was invalid, skip it.
1513 SkipUntil(tok::r_square);
1514 return;
1515 }
1516
1517 MatchRHSPunctuation(tok::r_square, StartLoc);
1518
1519 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1520 // it was not a constant expression.
1521 if (!getLang().C99) {
1522 // TODO: check C90 array constant exprness.
1523 if (isStar || StaticLoc.isValid() ||
1524 0/*TODO: NumElts is not a C90 constantexpr */)
1525 Diag(StartLoc, diag::ext_c99_array_usage);
1526 }
1527
1528 // Remember that we parsed a pointer type, and remember the type-quals.
1529 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1530 StaticLoc.isValid(), isStar,
1531 NumElements.Val, StartLoc));
1532}
1533
Steve Naroff7cbb1462007-07-31 12:34:36 +00001534/// [GNU] typeof-specifier:
1535/// typeof ( expressions )
1536/// typeof ( type-name )
1537///
1538void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001539 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00001540 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00001541 SourceLocation StartLoc = ConsumeToken();
1542
Chris Lattner34a01ad2007-10-09 17:33:22 +00001543 if (Tok.isNot(tok::l_paren)) {
Steve Naroff14bbce82007-08-02 02:53:48 +00001544 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1545 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00001546 }
1547 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1548
1549 if (isTypeSpecifierQualifier()) {
1550 TypeTy *Ty = ParseTypeName();
1551
Steve Naroff4c255ab2007-07-31 23:56:32 +00001552 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1553
Chris Lattner34a01ad2007-10-09 17:33:22 +00001554 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001555 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001556 return;
1557 }
1558 RParenLoc = ConsumeParen();
1559 const char *PrevSpec = 0;
1560 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1561 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1562 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001563 } else { // we have an expression.
1564 ExprResult Result = ParseExpression();
Steve Naroff4c255ab2007-07-31 23:56:32 +00001565
Chris Lattner34a01ad2007-10-09 17:33:22 +00001566 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001567 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001568 return;
1569 }
1570 RParenLoc = ConsumeParen();
1571 const char *PrevSpec = 0;
1572 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1573 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1574 Result.Val))
1575 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001576 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00001577}
1578