blob: 368887298e9a4c230cc57d854e0460d59ef58cde [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___declspec:
487 FuzzyParseMicrosoftDeclspec();
488 // Don't consume the next token, __declspec's can appear one after
489 // another. For example:
490 // __declspec(deprecated("comment1"))
491 // __declspec(deprecated("comment2")) extern unsigned int _winmajor;
492 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000493 case tok::kw_extern:
494 if (DS.isThreadSpecified())
495 Diag(Tok, diag::ext_thread_before, "extern");
496 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
497 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000498 case tok::kw___private_extern__:
Steve Naroff1cbb2762008-01-25 22:14:40 +0000499 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc, PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000500 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000501 case tok::kw_static:
502 if (DS.isThreadSpecified())
503 Diag(Tok, diag::ext_thread_before, "static");
504 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
505 break;
506 case tok::kw_auto:
507 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
508 break;
509 case tok::kw_register:
510 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
511 break;
512 case tok::kw___thread:
513 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
514 break;
515
516 // type-specifiers
517 case tok::kw_short:
518 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
519 break;
520 case tok::kw_long:
521 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
522 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
523 else
524 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
525 break;
526 case tok::kw_signed:
527 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
528 break;
529 case tok::kw_unsigned:
530 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
531 break;
532 case tok::kw__Complex:
533 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
534 break;
535 case tok::kw__Imaginary:
536 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
537 break;
538 case tok::kw_void:
539 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
540 break;
541 case tok::kw_char:
542 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
543 break;
544 case tok::kw_int:
545 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
546 break;
547 case tok::kw_float:
548 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
549 break;
550 case tok::kw_double:
551 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
552 break;
553 case tok::kw_bool: // [C++ 2.11p1]
554 case tok::kw__Bool:
555 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
556 break;
557 case tok::kw__Decimal32:
558 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
559 break;
560 case tok::kw__Decimal64:
561 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
562 break;
563 case tok::kw__Decimal128:
564 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
565 break;
566
567 case tok::kw_struct:
568 case tok::kw_union:
569 ParseStructUnionSpecifier(DS);
570 continue;
571 case tok::kw_enum:
572 ParseEnumSpecifier(DS);
573 continue;
574
Steve Naroff7cbb1462007-07-31 12:34:36 +0000575 // GNU typeof support.
576 case tok::kw_typeof:
577 ParseTypeofSpecifier(DS);
578 continue;
579
Chris Lattner4b009652007-07-25 00:24:17 +0000580 // type-qualifier
581 case tok::kw_const:
582 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
583 getLang())*2;
584 break;
585 case tok::kw_volatile:
586 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
587 getLang())*2;
588 break;
589 case tok::kw_restrict:
590 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
591 getLang())*2;
592 break;
593
594 // function-specifier
595 case tok::kw_inline:
596 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
597 break;
598 }
599 // If the specifier combination wasn't legal, issue a diagnostic.
600 if (isInvalid) {
601 assert(PrevSpec && "Method did not return previous specifier!");
602 if (isInvalid == 1) // Error.
603 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
604 else // extwarn.
605 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
606 }
607 DS.Range.setEnd(Tok.getLocation());
608 ConsumeToken();
609 }
610}
611
612/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
613/// the first token has already been read and has been turned into an instance
614/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
615/// otherwise it returns false and fills in Decl.
616bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
617 AttributeList *Attr = 0;
618 // If attributes exist after tag, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000619 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000620 Attr = ParseAttributes();
621
622 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000623 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000624 Diag(Tok, diag::err_expected_ident_lbrace);
625
626 // Skip the rest of this declarator, up until the comma or semicolon.
627 SkipUntil(tok::comma, true);
628 return true;
629 }
630
631 // If an identifier is present, consume and remember it.
632 IdentifierInfo *Name = 0;
633 SourceLocation NameLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000634 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000635 Name = Tok.getIdentifierInfo();
636 NameLoc = ConsumeToken();
637 }
638
639 // There are three options here. If we have 'struct foo;', then this is a
640 // forward declaration. If we have 'struct foo {...' then this is a
641 // definition. Otherwise we have something like 'struct foo xyz', a reference.
642 //
643 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
644 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
645 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
646 //
647 Action::TagKind TK;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000648 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000649 TK = Action::TK_Definition;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000650 else if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000651 TK = Action::TK_Declaration;
652 else
653 TK = Action::TK_Reference;
Steve Naroff0acc9c92007-09-15 18:49:24 +0000654 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Chris Lattner4b009652007-07-25 00:24:17 +0000655 return false;
656}
657
658
659/// ParseStructUnionSpecifier
660/// struct-or-union-specifier: [C99 6.7.2.1]
661/// struct-or-union identifier[opt] '{' struct-contents '}'
662/// struct-or-union identifier
663/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
664/// '}' attributes[opt]
665/// [GNU] struct-or-union attributes[opt] identifier
666/// struct-or-union:
667/// 'struct'
668/// 'union'
669///
670void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000671 assert((Tok.is(tok::kw_struct) || Tok.is(tok::kw_union)) &&
672 "Not a struct/union specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000673 DeclSpec::TST TagType =
Chris Lattner34a01ad2007-10-09 17:33:22 +0000674 Tok.is(tok::kw_union) ? DeclSpec::TST_union : DeclSpec::TST_struct;
Chris Lattner4b009652007-07-25 00:24:17 +0000675 SourceLocation StartLoc = ConsumeToken();
676
Steve Naroff73a07032008-02-07 03:50:06 +0000677 if (getLang().Microsoft && Tok.is(tok::kw___declspec))
678 FuzzyParseMicrosoftDeclspec();
679
Chris Lattner4b009652007-07-25 00:24:17 +0000680 // Parse the tag portion of this.
681 DeclTy *TagDecl;
682 if (ParseTag(TagDecl, TagType, StartLoc))
683 return;
684
685 // If there is a body, parse it and inform the actions module.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000686 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000687 ParseStructUnionBody(StartLoc, TagType, TagDecl);
688
689 const char *PrevSpec = 0;
690 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
691 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
692}
693
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000694/// ParseStructDeclaration - Parse a struct declaration without the terminating
695/// semicolon.
696///
Chris Lattner4b009652007-07-25 00:24:17 +0000697/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000698/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000699/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000700/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000701/// struct-declarator-list:
702/// struct-declarator
703/// struct-declarator-list ',' struct-declarator
704/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
705/// struct-declarator:
706/// declarator
707/// [GNU] declarator attributes[opt]
708/// declarator[opt] ':' constant-expression
709/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
710///
Steve Naroffa9adf112007-08-20 22:28:22 +0000711void Parser::ParseStructDeclaration(DeclTy *TagDecl,
Steve Naroffc02f4a92007-08-28 16:31:47 +0000712 llvm::SmallVectorImpl<DeclTy*> &FieldDecls) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000713 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000714 if (Tok.is(tok::kw___extension__))
Steve Naroffa9adf112007-08-20 22:28:22 +0000715 ConsumeToken();
716
717 // Parse the common specifier-qualifiers-list piece.
718 DeclSpec DS;
719 SourceLocation SpecQualLoc = Tok.getLocation();
720 ParseSpecifierQualifierList(DS);
721 // TODO: Does specifier-qualifier list correctly check that *something* is
722 // specified?
723
724 // If there are no declarators, issue a warning.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000725 if (Tok.is(tok::semi)) {
Steve Naroff73a07032008-02-07 03:50:06 +0000726 if (!getLang().Microsoft) // MS allows unnamed struct/union fields.
727 Diag(SpecQualLoc, diag::w_no_declarators);
Steve Naroffa9adf112007-08-20 22:28:22 +0000728 return;
729 }
730
731 // Read struct-declarators until we find the semicolon.
732 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
733
734 while (1) {
735 /// struct-declarator: declarator
736 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000737 if (Tok.isNot(tok::colon))
Steve Naroffa9adf112007-08-20 22:28:22 +0000738 ParseDeclarator(DeclaratorInfo);
739
740 ExprTy *BitfieldSize = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000741 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000742 ConsumeToken();
743 ExprResult Res = ParseConstantExpression();
744 if (Res.isInvalid) {
745 SkipUntil(tok::semi, true, true);
746 } else {
747 BitfieldSize = Res.Val;
748 }
749 }
750
751 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000752 if (Tok.is(tok::kw___attribute))
Steve Naroffa9adf112007-08-20 22:28:22 +0000753 DeclaratorInfo.AddAttributes(ParseAttributes());
754
755 // Install the declarator into the current TagDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000756 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl, SpecQualLoc,
Steve Naroffa9adf112007-08-20 22:28:22 +0000757 DeclaratorInfo, BitfieldSize);
758 FieldDecls.push_back(Field);
759
760 // If we don't have a comma, it is either the end of the list (a ';')
761 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000762 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000763 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000764
765 // Consume the comma.
766 ConsumeToken();
767
768 // Parse the next declarator.
769 DeclaratorInfo.clear();
770
771 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000772 if (Tok.is(tok::kw___attribute))
Steve Naroffa9adf112007-08-20 22:28:22 +0000773 DeclaratorInfo.AddAttributes(ParseAttributes());
774 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000775}
776
777/// ParseStructUnionBody
778/// struct-contents:
779/// struct-declaration-list
780/// [EXT] empty
781/// [GNU] "struct-declaration-list" without terminatoring ';'
782/// struct-declaration-list:
783/// struct-declaration
784/// struct-declaration-list struct-declaration
785/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
786///
Chris Lattner4b009652007-07-25 00:24:17 +0000787void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
788 unsigned TagType, DeclTy *TagDecl) {
789 SourceLocation LBraceLoc = ConsumeBrace();
790
791 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
792 // C++.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000793 if (Tok.is(tok::r_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000794 Diag(Tok, diag::ext_empty_struct_union_enum,
795 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
796
797 llvm::SmallVector<DeclTy*, 32> FieldDecls;
798
799 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000800 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000801 // Each iteration of this loop reads one struct-declaration.
802
803 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000804 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000805 Diag(Tok, diag::ext_extra_struct_semi);
806 ConsumeToken();
807 continue;
808 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000809 ParseStructDeclaration(TagDecl, FieldDecls);
Chris Lattner4b009652007-07-25 00:24:17 +0000810
Chris Lattner34a01ad2007-10-09 17:33:22 +0000811 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000812 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +0000813 } else if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000814 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
815 break;
816 } else {
817 Diag(Tok, diag::err_expected_semi_decl_list);
818 // Skip to end of block or statement
819 SkipUntil(tok::r_brace, true, true);
820 }
821 }
822
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000823 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000824
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000825 Actions.ActOnFields(CurScope,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000826 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
827 LBraceLoc, RBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000828
829 AttributeList *AttrList = 0;
830 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000831 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000832 AttrList = ParseAttributes(); // FIXME: where should I put them?
833}
834
835
836/// ParseEnumSpecifier
837/// enum-specifier: [C99 6.7.2.2]
838/// 'enum' identifier[opt] '{' enumerator-list '}'
839/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
840/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
841/// '}' attributes[opt]
842/// 'enum' identifier
843/// [GNU] 'enum' attributes[opt] identifier
844void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000845 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000846 SourceLocation StartLoc = ConsumeToken();
847
848 // Parse the tag portion of this.
849 DeclTy *TagDecl;
850 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
851 return;
852
Chris Lattner34a01ad2007-10-09 17:33:22 +0000853 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000854 ParseEnumBody(StartLoc, TagDecl);
855
856 // TODO: semantic analysis on the declspec for enums.
857 const char *PrevSpec = 0;
858 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
859 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
860}
861
862/// ParseEnumBody - Parse a {} enclosed enumerator-list.
863/// enumerator-list:
864/// enumerator
865/// enumerator-list ',' enumerator
866/// enumerator:
867/// enumeration-constant
868/// enumeration-constant '=' constant-expression
869/// enumeration-constant:
870/// identifier
871///
872void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
873 SourceLocation LBraceLoc = ConsumeBrace();
874
Chris Lattnerc9a92452007-08-27 17:24:30 +0000875 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +0000876 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000877 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
878
879 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
880
881 DeclTy *LastEnumConstDecl = 0;
882
883 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000884 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000885 IdentifierInfo *Ident = Tok.getIdentifierInfo();
886 SourceLocation IdentLoc = ConsumeToken();
887
888 SourceLocation EqualLoc;
889 ExprTy *AssignedVal = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000890 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000891 EqualLoc = ConsumeToken();
892 ExprResult Res = ParseConstantExpression();
893 if (Res.isInvalid)
894 SkipUntil(tok::comma, tok::r_brace, true, true);
895 else
896 AssignedVal = Res.Val;
897 }
898
899 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000900 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +0000901 LastEnumConstDecl,
902 IdentLoc, Ident,
903 EqualLoc, AssignedVal);
904 EnumConstantDecls.push_back(EnumConstDecl);
905 LastEnumConstDecl = EnumConstDecl;
906
Chris Lattner34a01ad2007-10-09 17:33:22 +0000907 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000908 break;
909 SourceLocation CommaLoc = ConsumeToken();
910
Chris Lattner34a01ad2007-10-09 17:33:22 +0000911 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +0000912 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
913 }
914
915 // Eat the }.
916 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
917
Steve Naroff0acc9c92007-09-15 18:49:24 +0000918 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +0000919 EnumConstantDecls.size());
920
921 DeclTy *AttrList = 0;
922 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000923 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000924 AttrList = ParseAttributes(); // FIXME: where do they do?
925}
926
927/// isTypeSpecifierQualifier - Return true if the current token could be the
928/// start of a specifier-qualifier-list.
929bool Parser::isTypeSpecifierQualifier() const {
930 switch (Tok.getKind()) {
931 default: return false;
932 // GNU attributes support.
933 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000934 // GNU typeof support.
935 case tok::kw_typeof:
936
Chris Lattner4b009652007-07-25 00:24:17 +0000937 // type-specifiers
938 case tok::kw_short:
939 case tok::kw_long:
940 case tok::kw_signed:
941 case tok::kw_unsigned:
942 case tok::kw__Complex:
943 case tok::kw__Imaginary:
944 case tok::kw_void:
945 case tok::kw_char:
946 case tok::kw_int:
947 case tok::kw_float:
948 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000949 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +0000950 case tok::kw__Bool:
951 case tok::kw__Decimal32:
952 case tok::kw__Decimal64:
953 case tok::kw__Decimal128:
954
955 // struct-or-union-specifier
956 case tok::kw_struct:
957 case tok::kw_union:
958 // enum-specifier
959 case tok::kw_enum:
960
961 // type-qualifier
962 case tok::kw_const:
963 case tok::kw_volatile:
964 case tok::kw_restrict:
965 return true;
966
967 // typedef-name
968 case tok::identifier:
969 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000970 }
971}
972
973/// isDeclarationSpecifier() - Return true if the current token is part of a
974/// declaration specifier.
975bool Parser::isDeclarationSpecifier() const {
976 switch (Tok.getKind()) {
977 default: return false;
978 // storage-class-specifier
979 case tok::kw_typedef:
980 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +0000981 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +0000982 case tok::kw_static:
983 case tok::kw_auto:
984 case tok::kw_register:
985 case tok::kw___thread:
986
987 // type-specifiers
988 case tok::kw_short:
989 case tok::kw_long:
990 case tok::kw_signed:
991 case tok::kw_unsigned:
992 case tok::kw__Complex:
993 case tok::kw__Imaginary:
994 case tok::kw_void:
995 case tok::kw_char:
996 case tok::kw_int:
997 case tok::kw_float:
998 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000999 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001000 case tok::kw__Bool:
1001 case tok::kw__Decimal32:
1002 case tok::kw__Decimal64:
1003 case tok::kw__Decimal128:
1004
1005 // struct-or-union-specifier
1006 case tok::kw_struct:
1007 case tok::kw_union:
1008 // enum-specifier
1009 case tok::kw_enum:
1010
1011 // type-qualifier
1012 case tok::kw_const:
1013 case tok::kw_volatile:
1014 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001015
Chris Lattner4b009652007-07-25 00:24:17 +00001016 // function-specifier
1017 case tok::kw_inline:
Chris Lattnere35d2582007-08-09 16:40:21 +00001018
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001019 // GNU typeof support.
1020 case tok::kw_typeof:
1021
1022 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001023 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001024 return true;
1025
1026 // typedef-name
1027 case tok::identifier:
1028 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001029 }
1030}
1031
1032
1033/// ParseTypeQualifierListOpt
1034/// type-qualifier-list: [C99 6.7.5]
1035/// type-qualifier
1036/// [GNU] attributes
1037/// type-qualifier-list type-qualifier
1038/// [GNU] type-qualifier-list attributes
1039///
1040void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1041 while (1) {
1042 int isInvalid = false;
1043 const char *PrevSpec = 0;
1044 SourceLocation Loc = Tok.getLocation();
1045
1046 switch (Tok.getKind()) {
1047 default:
1048 // If this is not a type-qualifier token, we're done reading type
1049 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +00001050 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +00001051 return;
1052 case tok::kw_const:
1053 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1054 getLang())*2;
1055 break;
1056 case tok::kw_volatile:
1057 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1058 getLang())*2;
1059 break;
1060 case tok::kw_restrict:
1061 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1062 getLang())*2;
1063 break;
1064 case tok::kw___attribute:
1065 DS.AddAttributes(ParseAttributes());
1066 continue; // do *not* consume the next token!
1067 }
1068
1069 // If the specifier combination wasn't legal, issue a diagnostic.
1070 if (isInvalid) {
1071 assert(PrevSpec && "Method did not return previous specifier!");
1072 if (isInvalid == 1) // Error.
1073 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1074 else // extwarn.
1075 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1076 }
1077 ConsumeToken();
1078 }
1079}
1080
1081
1082/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1083///
1084void Parser::ParseDeclarator(Declarator &D) {
1085 /// This implements the 'declarator' production in the C grammar, then checks
1086 /// for well-formedness and issues diagnostics.
1087 ParseDeclaratorInternal(D);
1088
1089 // TODO: validate D.
1090
1091}
1092
1093/// ParseDeclaratorInternal
1094/// declarator: [C99 6.7.5]
1095/// pointer[opt] direct-declarator
1096/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1097/// [GNU] '&' restrict[opt] attributes[opt] declarator
1098///
1099/// pointer: [C99 6.7.5]
1100/// '*' type-qualifier-list[opt]
1101/// '*' type-qualifier-list[opt] pointer
1102///
1103void Parser::ParseDeclaratorInternal(Declarator &D) {
1104 tok::TokenKind Kind = Tok.getKind();
1105
1106 // Not a pointer or C++ reference.
1107 if (Kind != tok::star && !(Kind == tok::amp && getLang().CPlusPlus))
1108 return ParseDirectDeclarator(D);
1109
1110 // Otherwise, '*' -> pointer or '&' -> reference.
1111 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1112
1113 if (Kind == tok::star) {
1114 // Is a pointer
1115 DeclSpec DS;
1116
1117 ParseTypeQualifierListOpt(DS);
1118
1119 // Recursively parse the declarator.
1120 ParseDeclaratorInternal(D);
1121
1122 // Remember that we parsed a pointer type, and remember the type-quals.
1123 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc));
1124 } else {
1125 // Is a reference
1126 DeclSpec DS;
1127
1128 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1129 // cv-qualifiers are introduced through the use of a typedef or of a
1130 // template type argument, in which case the cv-qualifiers are ignored.
1131 //
1132 // [GNU] Retricted references are allowed.
1133 // [GNU] Attributes on references are allowed.
1134 ParseTypeQualifierListOpt(DS);
1135
1136 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1137 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1138 Diag(DS.getConstSpecLoc(),
1139 diag::err_invalid_reference_qualifier_application,
1140 "const");
1141 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1142 Diag(DS.getVolatileSpecLoc(),
1143 diag::err_invalid_reference_qualifier_application,
1144 "volatile");
1145 }
1146
1147 // Recursively parse the declarator.
1148 ParseDeclaratorInternal(D);
1149
1150 // Remember that we parsed a reference type. It doesn't have type-quals.
1151 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc));
1152 }
1153}
1154
1155/// ParseDirectDeclarator
1156/// direct-declarator: [C99 6.7.5]
1157/// identifier
1158/// '(' declarator ')'
1159/// [GNU] '(' attributes declarator ')'
1160/// [C90] direct-declarator '[' constant-expression[opt] ']'
1161/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1162/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1163/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1164/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1165/// direct-declarator '(' parameter-type-list ')'
1166/// direct-declarator '(' identifier-list[opt] ')'
1167/// [GNU] direct-declarator '(' parameter-forward-declarations
1168/// parameter-type-list[opt] ')'
1169///
1170void Parser::ParseDirectDeclarator(Declarator &D) {
1171 // Parse the first direct-declarator seen.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001172 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001173 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1174 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1175 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001176 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001177 // direct-declarator: '(' declarator ')'
1178 // direct-declarator: '(' attributes declarator ')'
1179 // Example: 'char (*X)' or 'int (*XX)(void)'
1180 ParseParenDeclarator(D);
1181 } else if (D.mayOmitIdentifier()) {
1182 // This could be something simple like "int" (in which case the declarator
1183 // portion is empty), if an abstract-declarator is allowed.
1184 D.SetIdentifier(0, Tok.getLocation());
1185 } else {
1186 // Expected identifier or '('.
1187 Diag(Tok, diag::err_expected_ident_lparen);
1188 D.SetIdentifier(0, Tok.getLocation());
1189 }
1190
1191 assert(D.isPastIdentifier() &&
1192 "Haven't past the location of the identifier yet?");
1193
1194 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001195 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001196 ParseParenDeclarator(D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001197 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001198 ParseBracketDeclarator(D);
1199 } else {
1200 break;
1201 }
1202 }
1203}
1204
1205/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
1206/// either be before the identifier (in which case these are just grouping
1207/// parens for precedence) or it may be after the identifier, in which case
1208/// these are function arguments.
1209///
1210/// This method also handles this portion of the grammar:
1211/// parameter-type-list: [C99 6.7.5]
1212/// parameter-list
1213/// parameter-list ',' '...'
1214///
1215/// parameter-list: [C99 6.7.5]
1216/// parameter-declaration
1217/// parameter-list ',' parameter-declaration
1218///
1219/// parameter-declaration: [C99 6.7.5]
1220/// declaration-specifiers declarator
1221/// [GNU] declaration-specifiers declarator attributes
1222/// declaration-specifiers abstract-declarator[opt]
1223/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1224///
1225/// identifier-list: [C99 6.7.5]
1226/// identifier
1227/// identifier-list ',' identifier
1228///
1229void Parser::ParseParenDeclarator(Declarator &D) {
1230 SourceLocation StartLoc = ConsumeParen();
1231
1232 // If we haven't past the identifier yet (or where the identifier would be
1233 // stored, if this is an abstract declarator), then this is probably just
1234 // grouping parens.
1235 if (!D.isPastIdentifier()) {
1236 // Okay, this is probably a grouping paren. However, if this could be an
1237 // abstract-declarator, then this could also be the start of function
1238 // arguments (consider 'void()').
1239 bool isGrouping;
1240
1241 if (!D.mayOmitIdentifier()) {
1242 // If this can't be an abstract-declarator, this *must* be a grouping
1243 // paren, because we haven't seen the identifier yet.
1244 isGrouping = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001245 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Chris Lattner4b009652007-07-25 00:24:17 +00001246 isDeclarationSpecifier()) { // 'int(int)' is a function.
1247 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1248 // considered to be a type, not a K&R identifier-list.
1249 isGrouping = false;
1250 } else {
1251 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1252 isGrouping = true;
1253 }
1254
1255 // If this is a grouping paren, handle:
1256 // direct-declarator: '(' declarator ')'
1257 // direct-declarator: '(' attributes declarator ')'
1258 if (isGrouping) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001259 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001260 D.AddAttributes(ParseAttributes());
1261
1262 ParseDeclaratorInternal(D);
1263 // Match the ')'.
1264 MatchRHSPunctuation(tok::r_paren, StartLoc);
1265 return;
1266 }
1267
1268 // Okay, if this wasn't a grouping paren, it must be the start of a function
1269 // argument list. Recognize that this declarator will never have an
1270 // identifier (and remember where it would have been), then fall through to
1271 // the handling of argument lists.
1272 D.SetIdentifier(0, Tok.getLocation());
1273 }
1274
1275 // Okay, this is the parameter list of a function definition, or it is an
1276 // identifier list of a K&R-style function.
1277 bool IsVariadic;
1278 bool HasPrototype;
1279 bool ErrorEmitted = false;
1280
1281 // Build up an array of information about the parsed arguments.
1282 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1283 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1284
Chris Lattner34a01ad2007-10-09 17:33:22 +00001285 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001286 // int() -> no prototype, no '...'.
1287 IsVariadic = false;
1288 HasPrototype = false;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001289 } else if (Tok.is(tok::identifier) &&
Chris Lattner4b009652007-07-25 00:24:17 +00001290 // K&R identifier lists can't have typedefs as identifiers, per
1291 // C99 6.7.5.3p11.
1292 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1293 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1294 // normal declarators, not for abstract-declarators.
1295 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
1296
1297 // If there was no identifier specified, either we are in an
1298 // abstract-declarator, or we are in a parameter declarator which was found
1299 // to be abstract. In abstract-declarators, identifier lists are not valid,
1300 // diagnose this.
1301 if (!D.getIdentifier())
1302 Diag(Tok, diag::ext_ident_list_in_param);
1303
1304 // Remember this identifier in ParamInfo.
1305 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1306 Tok.getLocation(), 0));
1307
1308 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001309 while (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001310 // Eat the comma.
1311 ConsumeToken();
1312
Chris Lattner34a01ad2007-10-09 17:33:22 +00001313 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001314 Diag(Tok, diag::err_expected_ident);
1315 ErrorEmitted = true;
1316 break;
1317 }
1318
1319 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
1320
1321 // Verify that the argument identifier has not already been mentioned.
1322 if (!ParamsSoFar.insert(ParmII)) {
1323 Diag(Tok.getLocation(), diag::err_param_redefinition,ParmII->getName());
1324 ParmII = 0;
1325 }
1326
1327 // Remember this identifier in ParamInfo.
1328 if (ParmII)
1329 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1330 Tok.getLocation(), 0));
1331
1332 // Eat the identifier.
1333 ConsumeToken();
1334 }
1335
1336 // K&R 'prototype'.
1337 IsVariadic = false;
1338 HasPrototype = false;
1339 } else {
1340 // Finally, a normal, non-empty parameter type list.
1341
1342 // Enter function-declaration scope, limiting any declarators for struct
1343 // tags to the function prototype scope.
1344 // FIXME: is this needed?
Chris Lattnera7549902007-08-26 06:24:45 +00001345 EnterScope(Scope::DeclScope);
Chris Lattner4b009652007-07-25 00:24:17 +00001346
1347 IsVariadic = false;
1348 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001349 if (Tok.is(tok::ellipsis)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001350 IsVariadic = true;
1351
1352 // Check to see if this is "void(...)" which is not allowed.
1353 if (ParamInfo.empty()) {
1354 // Otherwise, parse parameter type list. If it starts with an
1355 // ellipsis, diagnose the malformed function.
1356 Diag(Tok, diag::err_ellipsis_first_arg);
1357 IsVariadic = false; // Treat this like 'void()'.
1358 }
1359
1360 // Consume the ellipsis.
1361 ConsumeToken();
1362 break;
1363 }
1364
1365 // Parse the declaration-specifiers.
1366 DeclSpec DS;
1367 ParseDeclarationSpecifiers(DS);
1368
1369 // Parse the declarator. This is "PrototypeContext", because we must
1370 // accept either 'declarator' or 'abstract-declarator' here.
1371 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1372 ParseDeclarator(ParmDecl);
1373
1374 // Parse GNU attributes, if present.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001375 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001376 ParmDecl.AddAttributes(ParseAttributes());
1377
1378 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
1379 // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted.
1380 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1381 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
1382 Diag(DS.getStorageClassSpecLoc(),
1383 diag::err_invalid_storage_class_in_func_decl);
1384 DS.ClearStorageClassSpecs();
1385 }
1386 if (DS.isThreadSpecified()) {
1387 Diag(DS.getThreadSpecLoc(),
1388 diag::err_invalid_storage_class_in_func_decl);
1389 DS.ClearStorageClassSpecs();
1390 }
1391
1392 // Inform the actions module about the parameter declarator, so it gets
1393 // added to the current scope.
1394 Action::TypeResult ParamTy =
Steve Naroff0acc9c92007-09-15 18:49:24 +00001395 Actions.ActOnParamDeclaratorType(CurScope, ParmDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001396
1397 // Remember this parsed parameter in ParamInfo.
1398 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1399
1400 // Verify that the argument identifier has not already been mentioned.
1401 if (ParmII && !ParamsSoFar.insert(ParmII)) {
1402 Diag(ParmDecl.getIdentifierLoc(), diag::err_param_redefinition,
1403 ParmII->getName());
1404 ParmII = 0;
1405 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00001406
1407 // If no parameter was specified, verify that *something* was specified,
1408 // otherwise we have a missing type and identifier.
1409 if (!DS.hasTypeSpecifier()) {
1410 if (ParmII)
1411 Diag(ParmDecl.getIdentifierLoc(),
1412 diag::err_param_requires_type_specifier, ParmII->getName());
1413 else
1414 Diag(Tok.getLocation(), diag::err_anon_param_requires_type_specifier);
1415
1416 // Default the parameter to 'int'.
1417 const char *PrevSpec = 0;
1418 DS.SetTypeSpecType(DeclSpec::TST_int, Tok.getLocation(), PrevSpec);
1419 }
Chris Lattner4b009652007-07-25 00:24:17 +00001420
Steve Naroff91b03f72007-08-28 03:03:08 +00001421 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Nate Begeman84079d72007-11-13 22:14:47 +00001422 ParmDecl.getIdentifierLoc(), ParamTy.Val, ParmDecl.getInvalidType(),
1423 ParmDecl.getDeclSpec().getAttributes()));
1424
1425 // Ownership of DeclSpec has been handed off to ParamInfo.
1426 DS.clearAttributes();
Chris Lattner4b009652007-07-25 00:24:17 +00001427
1428 // If the next token is a comma, consume it and keep reading arguments.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001429 if (Tok.isNot(tok::comma)) break;
Chris Lattner4b009652007-07-25 00:24:17 +00001430
1431 // Consume the comma.
1432 ConsumeToken();
1433 }
1434
1435 HasPrototype = true;
1436
1437 // Leave prototype scope.
1438 ExitScope();
1439 }
1440
1441 // Remember that we parsed a function type, and remember the attributes.
1442 if (!ErrorEmitted)
1443 D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic,
1444 &ParamInfo[0], ParamInfo.size(),
1445 StartLoc));
1446
1447 // If we have the closing ')', eat it and we're done.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001448 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001449 ConsumeParen();
1450 } else {
1451 // If an error happened earlier parsing something else in the proto, don't
1452 // issue another error.
1453 if (!ErrorEmitted)
1454 Diag(Tok, diag::err_expected_rparen);
1455 SkipUntil(tok::r_paren);
1456 }
1457}
1458
1459
1460/// [C90] direct-declarator '[' constant-expression[opt] ']'
1461/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1462/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1463/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1464/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1465void Parser::ParseBracketDeclarator(Declarator &D) {
1466 SourceLocation StartLoc = ConsumeBracket();
1467
1468 // If valid, this location is the position where we read the 'static' keyword.
1469 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001470 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001471 StaticLoc = ConsumeToken();
1472
1473 // If there is a type-qualifier-list, read it now.
1474 DeclSpec DS;
1475 ParseTypeQualifierListOpt(DS);
1476
1477 // If we haven't already read 'static', check to see if there is one after the
1478 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001479 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001480 StaticLoc = ConsumeToken();
1481
1482 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1483 bool isStar = false;
1484 ExprResult NumElements(false);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001485 if (Tok.is(tok::star)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001486 // Remember the '*' token, in case we have to un-get it.
1487 Token StarTok = Tok;
1488 ConsumeToken();
1489
1490 // Check that the ']' token is present to avoid incorrectly parsing
1491 // expressions starting with '*' as [*].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001492 if (Tok.is(tok::r_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001493 if (StaticLoc.isValid())
1494 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1495 StaticLoc = SourceLocation(); // Drop the static.
1496 isStar = true;
1497 } else {
1498 // Otherwise, the * must have been some expression (such as '*ptr') that
1499 // started an assignment-expr. We already consumed the token, but now we
1500 // need to reparse it. This handles cases like 'X[*p + 4]'
1501 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
1502 }
Chris Lattner34a01ad2007-10-09 17:33:22 +00001503 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001504 // Parse the assignment-expression now.
1505 NumElements = ParseAssignmentExpression();
1506 }
1507
1508 // If there was an error parsing the assignment-expression, recover.
1509 if (NumElements.isInvalid) {
1510 // If the expression was invalid, skip it.
1511 SkipUntil(tok::r_square);
1512 return;
1513 }
1514
1515 MatchRHSPunctuation(tok::r_square, StartLoc);
1516
1517 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1518 // it was not a constant expression.
1519 if (!getLang().C99) {
1520 // TODO: check C90 array constant exprness.
1521 if (isStar || StaticLoc.isValid() ||
1522 0/*TODO: NumElts is not a C90 constantexpr */)
1523 Diag(StartLoc, diag::ext_c99_array_usage);
1524 }
1525
1526 // Remember that we parsed a pointer type, and remember the type-quals.
1527 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1528 StaticLoc.isValid(), isStar,
1529 NumElements.Val, StartLoc));
1530}
1531
Steve Naroff7cbb1462007-07-31 12:34:36 +00001532/// [GNU] typeof-specifier:
1533/// typeof ( expressions )
1534/// typeof ( type-name )
1535///
1536void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001537 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00001538 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00001539 SourceLocation StartLoc = ConsumeToken();
1540
Chris Lattner34a01ad2007-10-09 17:33:22 +00001541 if (Tok.isNot(tok::l_paren)) {
Steve Naroff14bbce82007-08-02 02:53:48 +00001542 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1543 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00001544 }
1545 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1546
1547 if (isTypeSpecifierQualifier()) {
1548 TypeTy *Ty = ParseTypeName();
1549
Steve Naroff4c255ab2007-07-31 23:56:32 +00001550 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1551
Chris Lattner34a01ad2007-10-09 17:33:22 +00001552 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001553 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001554 return;
1555 }
1556 RParenLoc = ConsumeParen();
1557 const char *PrevSpec = 0;
1558 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1559 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1560 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001561 } else { // we have an expression.
1562 ExprResult Result = ParseExpression();
Steve Naroff4c255ab2007-07-31 23:56:32 +00001563
Chris Lattner34a01ad2007-10-09 17:33:22 +00001564 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001565 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001566 return;
1567 }
1568 RParenLoc = ConsumeParen();
1569 const char *PrevSpec = 0;
1570 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1571 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1572 Result.Val))
1573 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001574 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00001575}
1576