blob: 6a617128ad575c863b8ad7b92098a27007210d31 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner288e86ff12006-11-11 23:03:42 +000015#include "clang/Parse/DeclSpec.h"
Chris Lattner1a76a3c2007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000017#include "llvm/ADT/SmallSet.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000018using namespace clang;
19
20//===----------------------------------------------------------------------===//
21// C99 6.7: Declarations.
22//===----------------------------------------------------------------------===//
23
Chris Lattnerf5fbd792006-08-10 23:56:11 +000024/// ParseTypeName
25/// type-name: [C99 6.7.6]
26/// specifier-qualifier-list abstract-declarator[opt]
Chris Lattnere550a4e2006-08-24 06:37:51 +000027Parser::TypeTy *Parser::ParseTypeName() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000028 // Parse the common declaration-specifiers piece.
29 DeclSpec DS;
Chris Lattner1890ac82006-08-13 01:16:23 +000030 ParseSpecifierQualifierList(DS);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000031
32 // Parse the abstract-declarator, if present.
33 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
34 ParseDeclarator(DeclaratorInfo);
Chris Lattnere550a4e2006-08-24 06:37:51 +000035
Chris Lattner558cb292006-11-19 01:31:06 +000036 return Actions.ParseTypeName(CurScope, DeclaratorInfo).Val;
Chris Lattnerf5fbd792006-08-10 23:56:11 +000037}
38
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000039/// ParseAttributes - Parse a non-empty attributes list.
40///
41/// [GNU] attributes:
42/// attribute
43/// attributes attribute
44///
45/// [GNU] attribute:
46/// '__attribute__' '(' '(' attribute-list ')' ')'
47///
48/// [GNU] attribute-list:
49/// attrib
50/// attribute_list ',' attrib
51///
52/// [GNU] attrib:
53/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000054/// attrib-name
55/// attrib-name '(' identifier ')'
56/// attrib-name '(' identifier ',' nonempty-expr-list ')'
57/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000058///
Steve Naroff0f2fe172007-06-01 17:11:19 +000059/// [GNU] attrib-name:
60/// identifier
61/// typespec
62/// typequal
63/// storageclass
64///
65/// FIXME: The GCC grammar/code for this construct implies we need two
66/// token lookahead. Comment from gcc: "If they start with an identifier
67/// which is followed by a comma or close parenthesis, then the arguments
68/// start with that identifier; otherwise they are an expression list."
69///
70/// At the moment, I am not doing 2 token lookahead. I am also unaware of
71/// any attributes that don't work (based on my limited testing). Most
72/// attributes are very simple in practice. Until we find a bug, I don't see
73/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000074
Steve Naroffb8371e12007-06-09 03:39:29 +000075AttributeList *Parser::ParseAttributes() {
Steve Naroff0f2fe172007-06-01 17:11:19 +000076 assert(Tok.getKind() == tok::kw___attribute && "Not an attribute list!");
77
Steve Naroffb8371e12007-06-09 03:39:29 +000078 AttributeList *CurrAttr = 0;
Steve Naroff0f2fe172007-06-01 17:11:19 +000079
80 while (Tok.getKind() == tok::kw___attribute) {
81 ConsumeToken();
82 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
83 "attribute")) {
84 SkipUntil(tok::r_paren, true); // skip until ) or ;
85 return CurrAttr;
86 }
87 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
88 SkipUntil(tok::r_paren, true); // skip until ) or ;
89 return CurrAttr;
90 }
91 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
92 while (Tok.getKind() == tok::identifier || isDeclarationSpecifier() ||
93 Tok.getKind() == tok::comma) {
94
95 if (Tok.getKind() == tok::comma) {
96 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
97 ConsumeToken();
98 continue;
99 }
100 // we have an identifier or declaration specifier (const, int, etc.)
101 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
102 SourceLocation AttrNameLoc = ConsumeToken();
Steve Naroff0f2fe172007-06-01 17:11:19 +0000103
104 // check if we have a "paramterized" attribute
105 if (Tok.getKind() == tok::l_paren) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000106 ConsumeParen(); // ignore the left paren loc for now
Steve Naroff0f2fe172007-06-01 17:11:19 +0000107
108 if (Tok.getKind() == tok::identifier) {
109 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
110 SourceLocation ParmLoc = ConsumeToken();
111
112 if (Tok.getKind() == tok::r_paren) {
113 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000114 ConsumeParen(); // ignore the right paren loc for now
115 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
116 ParmName, ParmLoc, 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000117 } else if (Tok.getKind() == tok::comma) {
118 ConsumeToken();
119 // __attribute__(( format(printf, 1, 2) ))
Chris Lattner23b7eb62007-06-15 23:05:46 +0000120 llvm::SmallVector<ExprTy*, 8> ArgExprs;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000121 bool ArgExprsOk = true;
122
123 // now parse the non-empty comma separated list of expressions
124 while (1) {
125 ExprResult ArgExpr = ParseAssignmentExpression();
126 if (ArgExpr.isInvalid) {
127 ArgExprsOk = false;
128 SkipUntil(tok::r_paren);
129 break;
130 } else {
131 ArgExprs.push_back(ArgExpr.Val);
132 }
133 if (Tok.getKind() != tok::comma)
134 break;
135 ConsumeToken(); // Eat the comma, move to the next argument
136 }
137 if (ArgExprsOk && Tok.getKind() == tok::r_paren) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000138 ConsumeParen(); // ignore the right paren loc for now
139 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
140 ParmLoc, &ArgExprs[0], ArgExprs.size(), CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000141 }
142 }
143 } else { // not an identifier
144 // parse a possibly empty comma separated list of expressions
145 if (Tok.getKind() == tok::r_paren) {
146 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000147 ConsumeParen(); // ignore the right paren loc for now
148 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
149 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000150 } else {
151 // __attribute__(( aligned(16) ))
Chris Lattner23b7eb62007-06-15 23:05:46 +0000152 llvm::SmallVector<ExprTy*, 8> ArgExprs;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000153 bool ArgExprsOk = true;
154
155 // now parse the list of expressions
156 while (1) {
157 ExprResult ArgExpr = ParseAssignmentExpression();
158 if (ArgExpr.isInvalid) {
159 ArgExprsOk = false;
160 SkipUntil(tok::r_paren);
161 break;
162 } else {
163 ArgExprs.push_back(ArgExpr.Val);
164 }
165 if (Tok.getKind() != tok::comma)
166 break;
167 ConsumeToken(); // Eat the comma, move to the next argument
168 }
169 // Match the ')'.
170 if (ArgExprsOk && Tok.getKind() == tok::r_paren) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000171 ConsumeParen(); // ignore the right paren loc for now
172 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
173 SourceLocation(), &ArgExprs[0], ArgExprs.size(),
174 CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000175 }
176 }
177 }
178 } else {
Steve Naroffb8371e12007-06-09 03:39:29 +0000179 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
180 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000181 }
182 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000183 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
184 SkipUntil(tok::r_paren, false);
185 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
186 SkipUntil(tok::r_paren, false);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000187 }
188 return CurrAttr;
189}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000190
Chris Lattner53361ac2006-08-10 05:19:57 +0000191/// ParseDeclaration - Parse a full 'declaration', which consists of
192/// declaration-specifiers, some number of declarators, and a semicolon.
193/// 'Context' should be a Declarator::TheContext value.
Chris Lattnera5235172007-08-25 06:57:03 +0000194///
195/// declaration: [C99 6.7]
196/// block-declaration ->
197/// simple-declaration
198/// others [FIXME]
199/// [C++] namespace-definition
200/// others... [FIXME]
201///
Chris Lattner302b4be2006-11-19 02:31:38 +0000202Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattnera5235172007-08-25 06:57:03 +0000203 switch (Tok.getKind()) {
204 case tok::kw_namespace:
205 return ParseNamespace(Context);
206 default:
207 return ParseSimpleDeclaration(Context);
208 }
209}
210
211/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
212/// declaration-specifiers init-declarator-list[opt] ';'
213///[C90/C++]init-declarator-list ';' [TODO]
214/// [OMP] threadprivate-directive [TODO]
215Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000216 // Parse the common declaration-specifiers piece.
217 DeclSpec DS;
218 ParseDeclarationSpecifiers(DS);
219
Chris Lattner0e894622006-08-13 19:58:17 +0000220 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
221 // declaration-specifiers init-declarator-list[opt] ';'
222 if (Tok.getKind() == tok::semi) {
Chris Lattner0e894622006-08-13 19:58:17 +0000223 ConsumeToken();
Chris Lattner200bdc32006-11-19 02:43:37 +0000224 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Chris Lattner0e894622006-08-13 19:58:17 +0000225 }
226
Chris Lattner53361ac2006-08-10 05:19:57 +0000227 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
228 ParseDeclarator(DeclaratorInfo);
229
Chris Lattner302b4be2006-11-19 02:31:38 +0000230 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner53361ac2006-08-10 05:19:57 +0000231}
232
Chris Lattnera5235172007-08-25 06:57:03 +0000233
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000234/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
235/// parsing 'declaration-specifiers declarator'. This method is split out this
236/// way to handle the ambiguity between top-level function-definitions and
237/// declarations.
238///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000239/// init-declarator-list: [C99 6.7]
240/// init-declarator
241/// init-declarator-list ',' init-declarator
242/// init-declarator: [C99 6.7]
243/// declarator
244/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000245/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
246/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000247///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000248Parser::DeclTy *Parser::
249ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
250
251 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
252 // that they can be chained properly if the actions want this.
253 Parser::DeclTy *LastDeclInGroup = 0;
254
Chris Lattner53361ac2006-08-10 05:19:57 +0000255 // At this point, we know that it is not a function definition. Parse the
256 // rest of the init-declarator-list.
257 while (1) {
Chris Lattner6d7e6342006-08-15 03:41:14 +0000258 // If a simple-asm-expr is present, parse it.
259 if (Tok.getKind() == tok::kw_asm)
260 ParseSimpleAsm();
261
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000262 // If attributes are present, parse them.
263 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000264 D.AddAttributes(ParseAttributes());
Chris Lattner6d7e6342006-08-15 03:41:14 +0000265
Chris Lattner53361ac2006-08-10 05:19:57 +0000266 // Parse declarator '=' initializer.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000267 ExprResult Init;
Chris Lattner53361ac2006-08-10 05:19:57 +0000268 if (Tok.getKind() == tok::equal) {
269 ConsumeToken();
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000270 Init = ParseInitializer();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000271 if (Init.isInvalid) {
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000272 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000273 return 0;
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000274 }
Chris Lattner53361ac2006-08-10 05:19:57 +0000275 }
276
Chris Lattner697e5d62006-11-09 06:32:27 +0000277 // Inform the current actions module that we just parsed this declarator.
Chris Lattner289ab7b2006-11-08 06:54:53 +0000278 // FIXME: pass asm & attributes.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000279 LastDeclInGroup = Actions.ParseDeclarator(CurScope, D, Init.Val,
280 LastDeclInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +0000281
282 // If we don't have a comma, it is either the end of the list (a ';') or an
283 // error, bail out.
284 if (Tok.getKind() != tok::comma)
285 break;
286
287 // Consume the comma.
288 ConsumeToken();
289
290 // Parse the next declarator.
291 D.clear();
292 ParseDeclarator(D);
293 }
294
295 if (Tok.getKind() == tok::semi) {
296 ConsumeToken();
Chris Lattner776fac82007-06-09 00:53:06 +0000297 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +0000298 }
Chris Lattner776fac82007-06-09 00:53:06 +0000299
300 Diag(Tok, diag::err_parse_error);
301 // Skip to end of block or statement
Chris Lattner43ba2512007-08-21 18:36:18 +0000302 SkipUntil(tok::r_brace, true, true);
Chris Lattner776fac82007-06-09 00:53:06 +0000303 if (Tok.getKind() == tok::semi)
304 ConsumeToken();
305 return 0;
Chris Lattner53361ac2006-08-10 05:19:57 +0000306}
307
Chris Lattner1890ac82006-08-13 01:16:23 +0000308/// ParseSpecifierQualifierList
309/// specifier-qualifier-list:
310/// type-specifier specifier-qualifier-list[opt]
311/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000312/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000313///
314void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
315 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
316 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000317 ParseDeclarationSpecifiers(DS);
318
319 // Validate declspec for type-name.
320 unsigned Specs = DS.getParsedSpecifiers();
321 if (Specs == DeclSpec::PQ_None)
322 Diag(Tok, diag::err_typename_requires_specqual);
323
Chris Lattner1b22eed2006-11-28 05:12:07 +0000324 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000325 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000326 if (DS.getStorageClassSpecLoc().isValid())
327 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
328 else
329 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000330 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000331 }
Chris Lattner1b22eed2006-11-28 05:12:07 +0000332
333 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000334 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000335 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000336 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000337 }
338}
Chris Lattner53361ac2006-08-10 05:19:57 +0000339
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000340/// ParseDeclarationSpecifiers
341/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000342/// storage-class-specifier declaration-specifiers[opt]
343/// type-specifier declaration-specifiers[opt]
344/// type-qualifier declaration-specifiers[opt]
345/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000346/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000347///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000348/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000349/// 'typedef'
350/// 'extern'
351/// 'static'
352/// 'auto'
353/// 'register'
354/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000355/// type-specifier: [C99 6.7.2]
356/// 'void'
357/// 'char'
358/// 'short'
359/// 'int'
360/// 'long'
361/// 'float'
362/// 'double'
363/// 'signed'
364/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000365/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000366/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000367/// typedef-name
Bill Wendling4073ed52007-02-13 01:51:42 +0000368/// [C++] 'bool'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000369/// [C99] '_Bool'
370/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000371/// [C99] '_Imaginary' // Removed in TC2?
372/// [GNU] '_Decimal32'
373/// [GNU] '_Decimal64'
374/// [GNU] '_Decimal128'
Steve Naroff872da802007-07-31 23:56:32 +0000375/// [GNU] typeof-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000376/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroff7e901fd2007-08-22 23:18:22 +0000377/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000378/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000379/// 'const'
380/// 'volatile'
381/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000382/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000383/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000384///
385void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattner02c04392007-07-25 00:24:17 +0000386 DS.Range.setBegin(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000387 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000388 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000389 const char *PrevSpec = 0;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000390 SourceLocation Loc = Tok.getLocation();
391
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000392 switch (Tok.getKind()) {
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000393 // typedef-name
394 case tok::identifier:
395 // This identifier can only be a typedef name if we haven't already seen
Chris Lattner5646b3e2006-08-15 05:12:01 +0000396 // a type-specifier. Without this check we misparse:
397 // typedef int X; struct Y { short X; }; as 'short int'.
Chris Lattnerf055d432006-11-28 04:28:12 +0000398 if (!DS.hasTypeSpecifier()) {
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000399 // It has to be available as a typedef too!
400 if (void *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(),
401 CurScope)) {
Chris Lattnerb20e8942006-11-28 05:30:29 +0000402 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000403 TypeRep);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000404 if (isInvalid)
405 break;
406 else { // FIXME: restrict this to "id" and ObjC classnames.
407 DS.Range.setEnd(Tok.getLocation());
408 ConsumeToken(); // The identifier
409 if (Tok.getKind() == tok::less)
410 ParseObjCProtocolReferences();
411 continue;
412 }
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000413 }
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000414 }
415 // FALL THROUGH.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000416 default:
417 // If this is not a declaration specifier token, we're done reading decl
418 // specifiers. First verify that DeclSpec's are consistent.
Chris Lattnerb20e8942006-11-28 05:30:29 +0000419 DS.Finish(Diags, getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000420 return;
Chris Lattnere37e2332006-08-15 04:50:22 +0000421
422 // GNU attributes support.
423 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000424 DS.AddAttributes(ParseAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +0000425 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000426
427 // storage-class-specifier
428 case tok::kw_typedef:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000429 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000430 break;
431 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000432 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000433 Diag(Tok, diag::ext_thread_before, "extern");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000434 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000435 break;
436 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000437 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000438 Diag(Tok, diag::ext_thread_before, "static");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000439 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000440 break;
441 case tok::kw_auto:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000442 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000443 break;
444 case tok::kw_register:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000445 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000446 break;
447 case tok::kw___thread:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000448 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000449 break;
450
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000451 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000452 case tok::kw_short:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000453 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000454 break;
455 case tok::kw_long:
Chris Lattner353f5742006-11-28 04:50:12 +0000456 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
Chris Lattnerb20e8942006-11-28 05:30:29 +0000457 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
Chris Lattner353f5742006-11-28 04:50:12 +0000458 else
Chris Lattnerb20e8942006-11-28 05:30:29 +0000459 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000460 break;
461 case tok::kw_signed:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000462 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000463 break;
464 case tok::kw_unsigned:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000465 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000466 break;
467 case tok::kw__Complex:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000468 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000469 break;
470 case tok::kw__Imaginary:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000471 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000472 break;
473 case tok::kw_void:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000474 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000475 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000476 case tok::kw_char:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000477 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000478 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000479 case tok::kw_int:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000480 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000481 break;
482 case tok::kw_float:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000483 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000484 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000485 case tok::kw_double:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000486 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000487 break;
Bill Wendling4073ed52007-02-13 01:51:42 +0000488 case tok::kw_bool: // [C++ 2.11p1]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000489 case tok::kw__Bool:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000490 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000491 break;
492 case tok::kw__Decimal32:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000493 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000494 break;
495 case tok::kw__Decimal64:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000496 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000497 break;
498 case tok::kw__Decimal128:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000499 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000500 break;
501
Chris Lattner1890ac82006-08-13 01:16:23 +0000502 case tok::kw_struct:
503 case tok::kw_union:
504 ParseStructUnionSpecifier(DS);
505 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000506 case tok::kw_enum:
507 ParseEnumSpecifier(DS);
508 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000509
Steve Naroffad373bd2007-07-31 12:34:36 +0000510 // GNU typeof support.
511 case tok::kw_typeof:
512 ParseTypeofSpecifier(DS);
513 continue;
514
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000515 // type-qualifier
516 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000517 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
518 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000519 break;
520 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000521 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
522 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000523 break;
524 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000525 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
526 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000527 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000528
529 // function-specifier
530 case tok::kw_inline:
Chris Lattner1b22eed2006-11-28 05:12:07 +0000531 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000532 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000533 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000534 // If the specifier combination wasn't legal, issue a diagnostic.
535 if (isInvalid) {
536 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000537 if (isInvalid == 1) // Error.
538 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
539 else // extwarn.
540 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000541 }
Chris Lattner02c04392007-07-25 00:24:17 +0000542 DS.Range.setEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000543 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000544 }
545}
546
Chris Lattnerffbc2712007-01-25 06:05:38 +0000547/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
548/// the first token has already been read and has been turned into an instance
549/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
550/// otherwise it returns false and fills in Decl.
551bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
Steve Naroffb8371e12007-06-09 03:39:29 +0000552 AttributeList *Attr = 0;
Chris Lattnere37e2332006-08-15 04:50:22 +0000553 // If attributes exist after tag, parse them.
554 if (Tok.getKind() == tok::kw___attribute)
Steve Naroffb8371e12007-06-09 03:39:29 +0000555 Attr = ParseAttributes();
Chris Lattnerffbc2712007-01-25 06:05:38 +0000556
Chris Lattner1890ac82006-08-13 01:16:23 +0000557 // Must have either 'struct name' or 'struct {...}'.
558 if (Tok.getKind() != tok::identifier &&
559 Tok.getKind() != tok::l_brace) {
560 Diag(Tok, diag::err_expected_ident_lbrace);
Chris Lattner02c04392007-07-25 00:24:17 +0000561
562 // Skip the rest of this declarator, up until the comma or semicolon.
563 SkipUntil(tok::comma, true);
Chris Lattnerffbc2712007-01-25 06:05:38 +0000564 return true;
Chris Lattner1890ac82006-08-13 01:16:23 +0000565 }
566
Chris Lattner8c6519a2007-01-22 07:41:36 +0000567 // If an identifier is present, consume and remember it.
568 IdentifierInfo *Name = 0;
569 SourceLocation NameLoc;
570 if (Tok.getKind() == tok::identifier) {
571 Name = Tok.getIdentifierInfo();
572 NameLoc = ConsumeToken();
573 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000574
Chris Lattner8c6519a2007-01-22 07:41:36 +0000575 // There are three options here. If we have 'struct foo;', then this is a
576 // forward declaration. If we have 'struct foo {...' then this is a
Chris Lattner7b9ace62007-01-23 20:11:08 +0000577 // definition. Otherwise we have something like 'struct foo xyz', a reference.
Chris Lattner8799cf22007-01-23 01:57:16 +0000578 //
579 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
580 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
581 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
582 //
Chris Lattner7b9ace62007-01-23 20:11:08 +0000583 Action::TagKind TK;
584 if (Tok.getKind() == tok::l_brace)
585 TK = Action::TK_Definition;
586 else if (Tok.getKind() == tok::semi)
587 TK = Action::TK_Declaration;
588 else
589 TK = Action::TK_Reference;
Steve Naroffb8371e12007-06-09 03:39:29 +0000590 Decl = Actions.ParseTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Chris Lattnerffbc2712007-01-25 06:05:38 +0000591 return false;
592}
593
594
595/// ParseStructUnionSpecifier
596/// struct-or-union-specifier: [C99 6.7.2.1]
597/// struct-or-union identifier[opt] '{' struct-contents '}'
598/// struct-or-union identifier
599/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
600/// '}' attributes[opt]
601/// [GNU] struct-or-union attributes[opt] identifier
602/// struct-or-union:
603/// 'struct'
604/// 'union'
605///
606void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
607 assert((Tok.getKind() == tok::kw_struct ||
608 Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
609 DeclSpec::TST TagType =
610 Tok.getKind() == tok::kw_union ? DeclSpec::TST_union : DeclSpec::TST_struct;
611 SourceLocation StartLoc = ConsumeToken();
612
613 // Parse the tag portion of this.
614 DeclTy *TagDecl;
615 if (ParseTag(TagDecl, TagType, StartLoc))
616 return;
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000617
Chris Lattner90a26b02007-01-23 04:38:16 +0000618 // If there is a body, parse it and inform the actions module.
619 if (Tok.getKind() == tok::l_brace)
Chris Lattner1300fb92007-01-23 23:42:53 +0000620 ParseStructUnionBody(StartLoc, TagType, TagDecl);
Chris Lattnerda72c822006-08-13 22:16:42 +0000621
622 const char *PrevSpec = 0;
Chris Lattnerb9d572a2007-01-23 04:58:34 +0000623 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000624 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000625}
626
Steve Naroff97170802007-08-20 22:28:22 +0000627/// ParseStructDeclaration
Chris Lattner90a26b02007-01-23 04:38:16 +0000628/// struct-declaration:
629/// specifier-qualifier-list struct-declarator-list ';'
Chris Lattner736ed5d2007-06-09 05:59:07 +0000630/// [GNU] __extension__ struct-declaration
631/// [GNU] specifier-qualifier-list ';'
Chris Lattner90a26b02007-01-23 04:38:16 +0000632/// struct-declarator-list:
633/// struct-declarator
634/// struct-declarator-list ',' struct-declarator
635/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
636/// struct-declarator:
637/// declarator
638/// [GNU] declarator attributes[opt]
639/// declarator[opt] ':' constant-expression
640/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
641///
Steve Naroff97170802007-08-20 22:28:22 +0000642void Parser::ParseStructDeclaration(DeclTy *TagDecl,
643 llvm::SmallVector<DeclTy*, 32> &FieldDecls) {
644 // FIXME: When __extension__ is specified, disable extension diagnostics.
645 if (Tok.getKind() == tok::kw___extension__)
646 ConsumeToken();
647
648 // Parse the common specifier-qualifiers-list piece.
649 DeclSpec DS;
650 SourceLocation SpecQualLoc = Tok.getLocation();
651 ParseSpecifierQualifierList(DS);
652 // TODO: Does specifier-qualifier list correctly check that *something* is
653 // specified?
654
655 // If there are no declarators, issue a warning.
656 if (Tok.getKind() == tok::semi) {
657 Diag(SpecQualLoc, diag::w_no_declarators);
658 ConsumeToken();
659 return;
660 }
661
662 // Read struct-declarators until we find the semicolon.
663 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
664
665 while (1) {
666 /// struct-declarator: declarator
667 /// struct-declarator: declarator[opt] ':' constant-expression
668 if (Tok.getKind() != tok::colon)
669 ParseDeclarator(DeclaratorInfo);
670
671 ExprTy *BitfieldSize = 0;
672 if (Tok.getKind() == tok::colon) {
673 ConsumeToken();
674 ExprResult Res = ParseConstantExpression();
675 if (Res.isInvalid) {
676 SkipUntil(tok::semi, true, true);
677 } else {
678 BitfieldSize = Res.Val;
679 }
680 }
681
682 // If attributes exist after the declarator, parse them.
683 if (Tok.getKind() == tok::kw___attribute)
684 DeclaratorInfo.AddAttributes(ParseAttributes());
685
686 // Install the declarator into the current TagDecl.
687 DeclTy *Field = Actions.ParseField(CurScope, TagDecl, SpecQualLoc,
688 DeclaratorInfo, BitfieldSize);
689 FieldDecls.push_back(Field);
690
691 // If we don't have a comma, it is either the end of the list (a ';')
692 // or an error, bail out.
693 if (Tok.getKind() != tok::comma)
694 break;
695
696 // Consume the comma.
697 ConsumeToken();
698
699 // Parse the next declarator.
700 DeclaratorInfo.clear();
701
702 // Attributes are only allowed on the second declarator.
703 if (Tok.getKind() == tok::kw___attribute)
704 DeclaratorInfo.AddAttributes(ParseAttributes());
705 }
706 return;
707}
708
709/// ParseStructUnionBody
710/// struct-contents:
711/// struct-declaration-list
712/// [EXT] empty
713/// [GNU] "struct-declaration-list" without terminatoring ';'
714/// struct-declaration-list:
715/// struct-declaration
716/// struct-declaration-list struct-declaration
717/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
718///
Chris Lattner1300fb92007-01-23 23:42:53 +0000719void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
720 unsigned TagType, DeclTy *TagDecl) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000721 SourceLocation LBraceLoc = ConsumeBrace();
722
Chris Lattner7b9ace62007-01-23 20:11:08 +0000723 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
724 // C++.
Chris Lattner90a26b02007-01-23 04:38:16 +0000725 if (Tok.getKind() == tok::r_brace)
726 Diag(Tok, diag::ext_empty_struct_union_enum,
727 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Chris Lattner7b9ace62007-01-23 20:11:08 +0000728
Chris Lattner23b7eb62007-06-15 23:05:46 +0000729 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner1300fb92007-01-23 23:42:53 +0000730
Chris Lattner7b9ace62007-01-23 20:11:08 +0000731 // While we still have something to read, read the declarations in the struct.
Chris Lattner90a26b02007-01-23 04:38:16 +0000732 while (Tok.getKind() != tok::r_brace &&
733 Tok.getKind() != tok::eof) {
734 // Each iteration of this loop reads one struct-declaration.
735
Chris Lattner736ed5d2007-06-09 05:59:07 +0000736 // Check for extraneous top-level semicolon.
Chris Lattner36e46a22007-06-09 05:49:55 +0000737 if (Tok.getKind() == tok::semi) {
738 Diag(Tok, diag::ext_extra_struct_semi);
739 ConsumeToken();
740 continue;
741 }
Steve Naroff97170802007-08-20 22:28:22 +0000742 ParseStructDeclaration(TagDecl, FieldDecls);
Chris Lattner736ed5d2007-06-09 05:59:07 +0000743
Chris Lattner90a26b02007-01-23 04:38:16 +0000744 if (Tok.getKind() == tok::semi) {
745 ConsumeToken();
Chris Lattner0c7e82d2007-06-09 05:54:40 +0000746 } else if (Tok.getKind() == tok::r_brace) {
747 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
748 break;
Chris Lattner90a26b02007-01-23 04:38:16 +0000749 } else {
750 Diag(Tok, diag::err_expected_semi_decl_list);
751 // Skip to end of block or statement
752 SkipUntil(tok::r_brace, true, true);
753 }
754 }
755
756 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
757
Chris Lattnerc1915e22007-01-25 07:29:02 +0000758 Actions.ParseRecordBody(RecordLoc, TagDecl, &FieldDecls[0],FieldDecls.size());
759
Steve Naroffb8371e12007-06-09 03:39:29 +0000760 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +0000761 // If attributes exist after struct contents, parse them.
762 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000763 AttrList = ParseAttributes(); // FIXME: where should I put them?
Chris Lattner90a26b02007-01-23 04:38:16 +0000764}
765
766
Chris Lattner3b561a32006-08-13 00:12:11 +0000767/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000768/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000769/// 'enum' identifier[opt] '{' enumerator-list '}'
770/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000771/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
772/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000773/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000774/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000775void Parser::ParseEnumSpecifier(DeclSpec &DS) {
776 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattnerb20e8942006-11-28 05:30:29 +0000777 SourceLocation StartLoc = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000778
Chris Lattnerffbc2712007-01-25 06:05:38 +0000779 // Parse the tag portion of this.
780 DeclTy *TagDecl;
781 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
Chris Lattner3b561a32006-08-13 00:12:11 +0000782 return;
Chris Lattner3b561a32006-08-13 00:12:11 +0000783
Chris Lattnerc1915e22007-01-25 07:29:02 +0000784 if (Tok.getKind() == tok::l_brace)
785 ParseEnumBody(StartLoc, TagDecl);
786
Chris Lattner3b561a32006-08-13 00:12:11 +0000787 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000788 const char *PrevSpec = 0;
Chris Lattnerffbc2712007-01-25 06:05:38 +0000789 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000790 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000791}
792
Chris Lattnerc1915e22007-01-25 07:29:02 +0000793/// ParseEnumBody - Parse a {} enclosed enumerator-list.
794/// enumerator-list:
795/// enumerator
796/// enumerator-list ',' enumerator
797/// enumerator:
798/// enumeration-constant
799/// enumeration-constant '=' constant-expression
800/// enumeration-constant:
801/// identifier
802///
803void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
804 SourceLocation LBraceLoc = ConsumeBrace();
805
806 if (Tok.getKind() == tok::r_brace)
807 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
808
Chris Lattner23b7eb62007-06-15 23:05:46 +0000809 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000810
Chris Lattner4ef40012007-06-11 01:28:17 +0000811 DeclTy *LastEnumConstDecl = 0;
812
Chris Lattnerc1915e22007-01-25 07:29:02 +0000813 // Parse the enumerator-list.
814 while (Tok.getKind() == tok::identifier) {
815 IdentifierInfo *Ident = Tok.getIdentifierInfo();
816 SourceLocation IdentLoc = ConsumeToken();
817
818 SourceLocation EqualLoc;
819 ExprTy *AssignedVal = 0;
820 if (Tok.getKind() == tok::equal) {
821 EqualLoc = ConsumeToken();
822 ExprResult Res = ParseConstantExpression();
823 if (Res.isInvalid)
Chris Lattnerda6c2ce2007-04-27 19:13:15 +0000824 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000825 else
826 AssignedVal = Res.Val;
827 }
828
829 // Install the enumerator constant into EnumDecl.
Chris Lattner4ef40012007-06-11 01:28:17 +0000830 DeclTy *EnumConstDecl = Actions.ParseEnumConstant(CurScope, EnumDecl,
831 LastEnumConstDecl,
832 IdentLoc, Ident,
833 EqualLoc, AssignedVal);
834 EnumConstantDecls.push_back(EnumConstDecl);
835 LastEnumConstDecl = EnumConstDecl;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000836
837 if (Tok.getKind() != tok::comma)
838 break;
839 SourceLocation CommaLoc = ConsumeToken();
840
841 if (Tok.getKind() != tok::identifier && !getLang().C99)
842 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
843 }
844
845 // Eat the }.
846 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
847
848 Actions.ParseEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
849 EnumConstantDecls.size());
850
Steve Naroff0f2fe172007-06-01 17:11:19 +0000851 DeclTy *AttrList = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000852 // If attributes exist after the identifier list, parse them.
853 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000854 AttrList = ParseAttributes(); // FIXME: where do they do?
Chris Lattnerc1915e22007-01-25 07:29:02 +0000855}
Chris Lattner3b561a32006-08-13 00:12:11 +0000856
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000857/// isTypeSpecifierQualifier - Return true if the current token could be the
858/// start of a specifier-qualifier-list.
859bool Parser::isTypeSpecifierQualifier() const {
860 switch (Tok.getKind()) {
861 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000862 // GNU attributes support.
863 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +0000864 // GNU typeof support.
865 case tok::kw_typeof:
866
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000867 // type-specifiers
868 case tok::kw_short:
869 case tok::kw_long:
870 case tok::kw_signed:
871 case tok::kw_unsigned:
872 case tok::kw__Complex:
873 case tok::kw__Imaginary:
874 case tok::kw_void:
875 case tok::kw_char:
876 case tok::kw_int:
877 case tok::kw_float:
878 case tok::kw_double:
879 case tok::kw__Bool:
880 case tok::kw__Decimal32:
881 case tok::kw__Decimal64:
882 case tok::kw__Decimal128:
883
884 // struct-or-union-specifier
885 case tok::kw_struct:
886 case tok::kw_union:
887 // enum-specifier
888 case tok::kw_enum:
889
890 // type-qualifier
891 case tok::kw_const:
892 case tok::kw_volatile:
893 case tok::kw_restrict:
894 return true;
895
896 // typedef-name
897 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000898 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000899 }
900}
901
Chris Lattneracd58a32006-08-06 17:24:14 +0000902/// isDeclarationSpecifier() - Return true if the current token is part of a
903/// declaration specifier.
904bool Parser::isDeclarationSpecifier() const {
905 switch (Tok.getKind()) {
906 default: return false;
907 // storage-class-specifier
908 case tok::kw_typedef:
909 case tok::kw_extern:
910 case tok::kw_static:
911 case tok::kw_auto:
912 case tok::kw_register:
913 case tok::kw___thread:
914
915 // type-specifiers
916 case tok::kw_short:
917 case tok::kw_long:
918 case tok::kw_signed:
919 case tok::kw_unsigned:
920 case tok::kw__Complex:
921 case tok::kw__Imaginary:
922 case tok::kw_void:
923 case tok::kw_char:
924 case tok::kw_int:
925 case tok::kw_float:
926 case tok::kw_double:
927 case tok::kw__Bool:
928 case tok::kw__Decimal32:
929 case tok::kw__Decimal64:
930 case tok::kw__Decimal128:
931
932 // struct-or-union-specifier
933 case tok::kw_struct:
934 case tok::kw_union:
935 // enum-specifier
936 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000937
Chris Lattneracd58a32006-08-06 17:24:14 +0000938 // type-qualifier
939 case tok::kw_const:
940 case tok::kw_volatile:
941 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +0000942
Chris Lattneracd58a32006-08-06 17:24:14 +0000943 // function-specifier
944 case tok::kw_inline:
Chris Lattner7b20dc72007-08-09 16:40:21 +0000945
Chris Lattner599e47e2007-08-09 17:01:07 +0000946 // GNU typeof support.
947 case tok::kw_typeof:
948
949 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +0000950 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +0000951 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000952
Chris Lattneracd58a32006-08-06 17:24:14 +0000953 // typedef-name
954 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000955 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +0000956 }
957}
958
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000959
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000960/// ParseTypeQualifierListOpt
961/// type-qualifier-list: [C99 6.7.5]
962/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000963/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000964/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000965/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000966///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000967void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000968 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000969 int isInvalid = false;
970 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +0000971 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000972
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000973 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000974 default:
Chris Lattnere37e2332006-08-15 04:50:22 +0000975 // If this is not a type-qualifier token, we're done reading type
976 // qualifiers. First verify that DeclSpec's are consistent.
Chris Lattnerb20e8942006-11-28 05:30:29 +0000977 DS.Finish(Diags, getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000978 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000979 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000980 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
981 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000982 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000983 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000984 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
985 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000986 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000987 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000988 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
989 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000990 break;
Chris Lattnere37e2332006-08-15 04:50:22 +0000991 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000992 DS.AddAttributes(ParseAttributes());
Steve Naroff98d153c2007-06-06 23:19:11 +0000993 continue; // do *not* consume the next token!
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000994 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000995
996 // If the specifier combination wasn't legal, issue a diagnostic.
997 if (isInvalid) {
998 assert(PrevSpec && "Method did not return previous specifier!");
999 if (isInvalid == 1) // Error.
1000 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1001 else // extwarn.
1002 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1003 }
1004 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001005 }
1006}
1007
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001008
1009/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1010///
1011void Parser::ParseDeclarator(Declarator &D) {
1012 /// This implements the 'declarator' production in the C grammar, then checks
1013 /// for well-formedness and issues diagnostics.
1014 ParseDeclaratorInternal(D);
1015
Chris Lattner9fab3b92006-08-12 18:25:42 +00001016 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +00001017
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001018}
1019
1020/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +00001021/// declarator: [C99 6.7.5]
1022/// pointer[opt] direct-declarator
Bill Wendling93efb222007-06-02 23:28:54 +00001023/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1024/// [GNU] '&' restrict[opt] attributes[opt] declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00001025///
1026/// pointer: [C99 6.7.5]
1027/// '*' type-qualifier-list[opt]
1028/// '*' type-qualifier-list[opt] pointer
1029///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001030void Parser::ParseDeclaratorInternal(Declarator &D) {
Bill Wendling3708c182007-05-27 10:15:43 +00001031 tok::TokenKind Kind = Tok.getKind();
1032
1033 // Not a pointer or C++ reference.
1034 if (Kind != tok::star && !(Kind == tok::amp && getLang().CPlusPlus))
Chris Lattner6c7416c2006-08-07 00:19:33 +00001035 return ParseDirectDeclarator(D);
1036
Bill Wendling3708c182007-05-27 10:15:43 +00001037 // Otherwise, '*' -> pointer or '&' -> reference.
1038 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1039
1040 if (Kind == tok::star) {
1041 // Is a pointer
1042 DeclSpec DS;
Steve Naroff98d153c2007-06-06 23:19:11 +00001043
Bill Wendling3708c182007-05-27 10:15:43 +00001044 ParseTypeQualifierListOpt(DS);
Chris Lattner6c7416c2006-08-07 00:19:33 +00001045
Bill Wendling3708c182007-05-27 10:15:43 +00001046 // Recursively parse the declarator.
1047 ParseDeclaratorInternal(D);
Chris Lattner9dfdb3c2006-11-13 07:38:09 +00001048
Bill Wendling3708c182007-05-27 10:15:43 +00001049 // Remember that we parsed a pointer type, and remember the type-quals.
1050 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc));
1051 } else {
1052 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00001053 DeclSpec DS;
1054
1055 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1056 // cv-qualifiers are introduced through the use of a typedef or of a
1057 // template type argument, in which case the cv-qualifiers are ignored.
1058 //
1059 // [GNU] Retricted references are allowed.
1060 // [GNU] Attributes on references are allowed.
1061 ParseTypeQualifierListOpt(DS);
1062
1063 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1064 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1065 Diag(DS.getConstSpecLoc(),
1066 diag::err_invalid_reference_qualifier_application,
1067 "const");
1068 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1069 Diag(DS.getVolatileSpecLoc(),
1070 diag::err_invalid_reference_qualifier_application,
1071 "volatile");
1072 }
Bill Wendling3708c182007-05-27 10:15:43 +00001073
1074 // Recursively parse the declarator.
1075 ParseDeclaratorInternal(D);
1076
1077 // Remember that we parsed a reference type. It doesn't have type-quals.
Bill Wendling93efb222007-06-02 23:28:54 +00001078 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc));
Bill Wendling3708c182007-05-27 10:15:43 +00001079 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001080}
1081
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001082/// ParseDirectDeclarator
1083/// direct-declarator: [C99 6.7.5]
1084/// identifier
1085/// '(' declarator ')'
1086/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00001087/// [C90] direct-declarator '[' constant-expression[opt] ']'
1088/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1089/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1090/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1091/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001092/// direct-declarator '(' parameter-type-list ')'
1093/// direct-declarator '(' identifier-list[opt] ')'
1094/// [GNU] direct-declarator '(' parameter-forward-declarations
1095/// parameter-type-list[opt] ')'
1096///
Chris Lattneracd58a32006-08-06 17:24:14 +00001097void Parser::ParseDirectDeclarator(Declarator &D) {
1098 // Parse the first direct-declarator seen.
1099 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
1100 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1101 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1102 ConsumeToken();
1103 } else if (Tok.getKind() == tok::l_paren) {
1104 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001105 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001106 // Example: 'char (*X)' or 'int (*XX)(void)'
1107 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001108 } else if (D.mayOmitIdentifier()) {
1109 // This could be something simple like "int" (in which case the declarator
1110 // portion is empty), if an abstract-declarator is allowed.
1111 D.SetIdentifier(0, Tok.getLocation());
1112 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +00001113 // Expected identifier or '('.
1114 Diag(Tok, diag::err_expected_ident_lparen);
1115 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +00001116 }
1117
1118 assert(D.isPastIdentifier() &&
1119 "Haven't past the location of the identifier yet?");
1120
1121 while (1) {
1122 if (Tok.getKind() == tok::l_paren) {
1123 ParseParenDeclarator(D);
1124 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001125 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001126 } else {
1127 break;
1128 }
1129 }
1130}
1131
1132/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
1133/// either be before the identifier (in which case these are just grouping
1134/// parens for precedence) or it may be after the identifier, in which case
1135/// these are function arguments.
1136///
1137/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001138/// parameter-type-list: [C99 6.7.5]
1139/// parameter-list
1140/// parameter-list ',' '...'
1141///
1142/// parameter-list: [C99 6.7.5]
1143/// parameter-declaration
1144/// parameter-list ',' parameter-declaration
1145///
1146/// parameter-declaration: [C99 6.7.5]
1147/// declaration-specifiers declarator
Chris Lattnere37e2332006-08-15 04:50:22 +00001148/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001149/// declaration-specifiers abstract-declarator[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001150/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001151///
1152/// identifier-list: [C99 6.7.5]
1153/// identifier
1154/// identifier-list ',' identifier
1155///
Chris Lattneracd58a32006-08-06 17:24:14 +00001156void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001157 SourceLocation StartLoc = ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001158
Chris Lattneracd58a32006-08-06 17:24:14 +00001159 // If we haven't past the identifier yet (or where the identifier would be
1160 // stored, if this is an abstract declarator), then this is probably just
1161 // grouping parens.
1162 if (!D.isPastIdentifier()) {
1163 // Okay, this is probably a grouping paren. However, if this could be an
1164 // abstract-declarator, then this could also be the start of function
1165 // arguments (consider 'void()').
1166 bool isGrouping;
1167
1168 if (!D.mayOmitIdentifier()) {
1169 // If this can't be an abstract-declarator, this *must* be a grouping
1170 // paren, because we haven't seen the identifier yet.
1171 isGrouping = true;
1172 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
1173 isDeclarationSpecifier()) { // 'int(int)' is a function.
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001174 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1175 // considered to be a type, not a K&R identifier-list.
Chris Lattneracd58a32006-08-06 17:24:14 +00001176 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001177 } else {
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001178 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +00001179 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001180 }
Chris Lattneracd58a32006-08-06 17:24:14 +00001181
1182 // If this is a grouping paren, handle:
1183 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001184 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001185 if (isGrouping) {
Chris Lattnere37e2332006-08-15 04:50:22 +00001186 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f05a7a2007-06-09 23:38:17 +00001187 D.AddAttributes(ParseAttributes());
Chris Lattnere37e2332006-08-15 04:50:22 +00001188
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001189 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +00001190 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +00001191 MatchRHSPunctuation(tok::r_paren, StartLoc);
Chris Lattneracd58a32006-08-06 17:24:14 +00001192 return;
1193 }
1194
1195 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +00001196 // argument list. Recognize that this declarator will never have an
1197 // identifier (and remember where it would have been), then fall through to
1198 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +00001199 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001200 }
1201
Chris Lattneracd58a32006-08-06 17:24:14 +00001202 // Okay, this is the parameter list of a function definition, or it is an
1203 // identifier list of a K&R-style function.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001204 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +00001205 bool HasPrototype;
Chris Lattner14776b92006-08-06 22:27:40 +00001206 bool ErrorEmitted = false;
1207
Chris Lattneredc9e392006-12-02 06:21:46 +00001208 // Build up an array of information about the parsed arguments.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001209 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1210 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Chris Lattneredc9e392006-12-02 06:21:46 +00001211
Chris Lattneracd58a32006-08-06 17:24:14 +00001212 if (Tok.getKind() == tok::r_paren) {
1213 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001214 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001215 HasPrototype = false;
1216 } else if (Tok.getKind() == tok::identifier &&
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001217 // K&R identifier lists can't have typedefs as identifiers, per
1218 // C99 6.7.5.3p11.
Steve Naroffb419d3a2006-10-27 23:18:49 +00001219 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001220 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1221 // normal declarators, not for abstract-declarators.
1222 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
1223
1224 // If there was no identifier specified, either we are in an
1225 // abstract-declarator, or we are in a parameter declarator which was found
1226 // to be abstract. In abstract-declarators, identifier lists are not valid,
1227 // diagnose this.
1228 if (!D.getIdentifier())
1229 Diag(Tok, diag::ext_ident_list_in_param);
Chris Lattneredc9e392006-12-02 06:21:46 +00001230
Chris Lattnercbc426d2006-12-02 06:43:02 +00001231 // Remember this identifier in ParamInfo.
1232 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1233 Tok.getLocation(), 0));
1234
Chris Lattneracd58a32006-08-06 17:24:14 +00001235 ConsumeToken();
1236 while (Tok.getKind() == tok::comma) {
1237 // Eat the comma.
1238 ConsumeToken();
1239
Chris Lattnercbc426d2006-12-02 06:43:02 +00001240 if (Tok.getKind() != tok::identifier) {
1241 Diag(Tok, diag::err_expected_ident);
Chris Lattner14776b92006-08-06 22:27:40 +00001242 ErrorEmitted = true;
1243 break;
1244 }
Chris Lattnercbc426d2006-12-02 06:43:02 +00001245
Chris Lattner969ca152006-12-03 06:29:03 +00001246 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
1247
1248 // Verify that the argument identifier has not already been mentioned.
Chris Lattnerbaf33662007-01-27 02:14:08 +00001249 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerad9ac942007-01-23 01:14:52 +00001250 Diag(Tok.getLocation(), diag::err_param_redefinition,ParmII->getName());
1251 ParmII = 0;
1252 }
Chris Lattner969ca152006-12-03 06:29:03 +00001253
Chris Lattnercbc426d2006-12-02 06:43:02 +00001254 // Remember this identifier in ParamInfo.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001255 if (ParmII)
1256 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1257 Tok.getLocation(), 0));
Chris Lattnercbc426d2006-12-02 06:43:02 +00001258
1259 // Eat the identifier.
1260 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00001261 }
1262
Chris Lattneracd58a32006-08-06 17:24:14 +00001263 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001264 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001265 HasPrototype = false;
1266 } else {
Chris Lattner43e956c2006-11-28 04:05:37 +00001267 // Finally, a normal, non-empty parameter type list.
1268
Chris Lattnercbc426d2006-12-02 06:43:02 +00001269 // Enter function-declaration scope, limiting any declarators for struct
1270 // tags to the function prototype scope.
1271 // FIXME: is this needed?
Chris Lattner1a76a3c2007-08-26 06:24:45 +00001272 EnterScope(Scope::DeclScope);
Chris Lattner43e956c2006-11-28 04:05:37 +00001273
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001274 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001275 while (1) {
1276 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001277 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +00001278
1279 // Check to see if this is "void(...)" which is not allowed.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001280 if (ParamInfo.empty()) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001281 // Otherwise, parse parameter type list. If it starts with an
1282 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +00001283 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001284 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +00001285 }
1286
1287 // Consume the ellipsis.
1288 ConsumeToken();
1289 break;
1290 }
1291
Chris Lattneracd58a32006-08-06 17:24:14 +00001292 // Parse the declaration-specifiers.
1293 DeclSpec DS;
1294 ParseDeclarationSpecifiers(DS);
1295
1296 // Parse the declarator. This is "PrototypeContext", because we must
1297 // accept either 'declarator' or 'abstract-declarator' here.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001298 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1299 ParseDeclarator(ParmDecl);
Chris Lattneracd58a32006-08-06 17:24:14 +00001300
Chris Lattnere37e2332006-08-15 04:50:22 +00001301 // Parse GNU attributes, if present.
1302 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f05a7a2007-06-09 23:38:17 +00001303 ParmDecl.AddAttributes(ParseAttributes());
Chris Lattnere37e2332006-08-15 04:50:22 +00001304
Chris Lattner43e956c2006-11-28 04:05:37 +00001305 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001306 // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted.
1307 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1308 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
Chris Lattner4d8f8732006-11-28 05:05:08 +00001309 Diag(DS.getStorageClassSpecLoc(),
Chris Lattner43e956c2006-11-28 04:05:37 +00001310 diag::err_invalid_storage_class_in_func_decl);
Chris Lattner353f5742006-11-28 04:50:12 +00001311 DS.ClearStorageClassSpecs();
Chris Lattner43e956c2006-11-28 04:05:37 +00001312 }
Chris Lattner4d8f8732006-11-28 05:05:08 +00001313 if (DS.isThreadSpecified()) {
1314 Diag(DS.getThreadSpecLoc(),
1315 diag::err_invalid_storage_class_in_func_decl);
1316 DS.ClearStorageClassSpecs();
1317 }
Chris Lattner43e956c2006-11-28 04:05:37 +00001318
1319 // Inform the actions module about the parameter declarator, so it gets
1320 // added to the current scope.
Chris Lattner216d8652006-12-02 06:47:41 +00001321 Action::TypeResult ParamTy =
1322 Actions.ParseParamDeclaratorType(CurScope, ParmDecl);
Chris Lattnercbc426d2006-12-02 06:43:02 +00001323
1324 // Remember this parsed parameter in ParamInfo.
Chris Lattner969ca152006-12-03 06:29:03 +00001325 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1326
1327 // Verify that the argument identifier has not already been mentioned.
Chris Lattnerbaf33662007-01-27 02:14:08 +00001328 if (ParmII && !ParamsSoFar.insert(ParmII)) {
Chris Lattnerad9ac942007-01-23 01:14:52 +00001329 Diag(ParmDecl.getIdentifierLoc(), diag::err_param_redefinition,
1330 ParmII->getName());
1331 ParmII = 0;
Chris Lattner969ca152006-12-03 06:29:03 +00001332 }
1333
1334 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnercbc426d2006-12-02 06:43:02 +00001335 ParmDecl.getIdentifierLoc(),
1336 ParamTy.Val));
Chris Lattneracd58a32006-08-06 17:24:14 +00001337
1338 // If the next token is a comma, consume it and keep reading arguments.
1339 if (Tok.getKind() != tok::comma) break;
1340
1341 // Consume the comma.
1342 ConsumeToken();
1343 }
1344
1345 HasPrototype = true;
Chris Lattner43e956c2006-11-28 04:05:37 +00001346
1347 // Leave prototype scope.
1348 ExitScope();
Chris Lattneracd58a32006-08-06 17:24:14 +00001349 }
1350
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001351 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerd2e97c12006-12-03 02:03:33 +00001352 if (!ErrorEmitted)
1353 D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic,
1354 &ParamInfo[0], ParamInfo.size(),
1355 StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001356
Chris Lattner14776b92006-08-06 22:27:40 +00001357 // If we have the closing ')', eat it and we're done.
1358 if (Tok.getKind() == tok::r_paren) {
1359 ConsumeParen();
1360 } else {
1361 // If an error happened earlier parsing something else in the proto, don't
1362 // issue another error.
1363 if (!ErrorEmitted)
1364 Diag(Tok, diag::err_expected_rparen);
1365 SkipUntil(tok::r_paren);
1366 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001367}
Chris Lattneracd58a32006-08-06 17:24:14 +00001368
Chris Lattnere8074e62006-08-06 18:30:15 +00001369
1370/// [C90] direct-declarator '[' constant-expression[opt] ']'
1371/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1372/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1373/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1374/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1375void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001376 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001377
1378 // If valid, this location is the position where we read the 'static' keyword.
1379 SourceLocation StaticLoc;
Chris Lattneraf635312006-10-16 06:06:51 +00001380 if (Tok.getKind() == tok::kw_static)
1381 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001382
1383 // If there is a type-qualifier-list, read it now.
1384 DeclSpec DS;
1385 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001386
1387 // If we haven't already read 'static', check to see if there is one after the
1388 // type-qualifier-list.
Chris Lattneraf635312006-10-16 06:06:51 +00001389 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static)
1390 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001391
1392 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001393 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001394 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +00001395 if (Tok.getKind() == tok::star) {
1396 // Remember the '*' token, in case we have to un-get it.
Chris Lattner146762e2007-07-20 16:59:19 +00001397 Token StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +00001398 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +00001399
1400 // Check that the ']' token is present to avoid incorrectly parsing
1401 // expressions starting with '*' as [*].
1402 if (Tok.getKind() == tok::r_square) {
1403 if (StaticLoc.isValid())
1404 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1405 StaticLoc = SourceLocation(); // Drop the static.
1406 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +00001407 } else {
1408 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +00001409 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +00001410 // need to reparse it. This handles cases like 'X[*p + 4]'
1411 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +00001412 }
Chris Lattner9fab3b92006-08-12 18:25:42 +00001413 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001414 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001415 NumElements = ParseAssignmentExpression();
1416 }
1417
1418 // If there was an error parsing the assignment-expression, recover.
1419 if (NumElements.isInvalid) {
1420 // If the expression was invalid, skip it.
1421 SkipUntil(tok::r_square);
1422 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001423 }
1424
Chris Lattner04f80192006-08-15 04:55:54 +00001425 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001426
Chris Lattnere8074e62006-08-06 18:30:15 +00001427 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1428 // it was not a constant expression.
1429 if (!getLang().C99) {
1430 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001431 if (isStar || StaticLoc.isValid() ||
1432 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001433 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001434 }
Bill Wendling93efb222007-06-02 23:28:54 +00001435
Chris Lattner6c7416c2006-08-07 00:19:33 +00001436 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001437 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1438 StaticLoc.isValid(), isStar,
1439 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001440}
1441
Steve Naroffad373bd2007-07-31 12:34:36 +00001442/// [GNU] typeof-specifier:
1443/// typeof ( expressions )
1444/// typeof ( type-name )
1445///
1446void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
1447 assert(Tok.getKind() == tok::kw_typeof && "Not a typeof specifier");
Steve Naroff4bd2f712007-08-02 02:53:48 +00001448 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffad373bd2007-07-31 12:34:36 +00001449 SourceLocation StartLoc = ConsumeToken();
1450
1451 if (Tok.getKind() != tok::l_paren) {
Steve Naroff4bd2f712007-08-02 02:53:48 +00001452 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1453 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00001454 }
1455 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1456
1457 if (isTypeSpecifierQualifier()) {
1458 TypeTy *Ty = ParseTypeName();
1459
Steve Naroff872da802007-07-31 23:56:32 +00001460 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1461
Steve Naroff4bd2f712007-08-02 02:53:48 +00001462 if (Tok.getKind() != tok::r_paren) {
Steve Naroff872da802007-07-31 23:56:32 +00001463 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001464 return;
1465 }
1466 RParenLoc = ConsumeParen();
1467 const char *PrevSpec = 0;
1468 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1469 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1470 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001471 } else { // we have an expression.
1472 ExprResult Result = ParseExpression();
Steve Naroff872da802007-07-31 23:56:32 +00001473
Steve Naroff4bd2f712007-08-02 02:53:48 +00001474 if (Result.isInvalid || Tok.getKind() != tok::r_paren) {
Steve Naroff872da802007-07-31 23:56:32 +00001475 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001476 return;
1477 }
1478 RParenLoc = ConsumeParen();
1479 const char *PrevSpec = 0;
1480 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1481 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1482 Result.Val))
1483 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001484 }
Steve Naroffad373bd2007-07-31 12:34:36 +00001485}
1486