blob: 145359a77383eb2d9fc0298a98942238c08fe43c [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 Lattnerad9ac942007-01-23 01:14:52 +000016#include "llvm/ADT/SmallSet.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000017using namespace llvm;
18using 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) ))
120 SmallVector<ExprTy*, 8> ArgExprs;
121 bool ArgExprsOk = true;
122
123 // now parse the non-empty comma separated list of expressions
124 while (1) {
125 ExprResult ArgExpr = ParseAssignmentExpression();
126 if (ArgExpr.isInvalid) {
127 ArgExprsOk = false;
128 SkipUntil(tok::r_paren);
129 break;
130 } else {
131 ArgExprs.push_back(ArgExpr.Val);
132 }
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) ))
152 SmallVector<ExprTy*, 8> ArgExprs;
153 bool ArgExprsOk = true;
154
155 // now parse the list of expressions
156 while (1) {
157 ExprResult ArgExpr = ParseAssignmentExpression();
158 if (ArgExpr.isInvalid) {
159 ArgExprsOk = false;
160 SkipUntil(tok::r_paren);
161 break;
162 } else {
163 ArgExprs.push_back(ArgExpr.Val);
164 }
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 Lattner302b4be2006-11-19 02:31:38 +0000194Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000195 // Parse the common declaration-specifiers piece.
196 DeclSpec DS;
197 ParseDeclarationSpecifiers(DS);
198
Chris Lattner0e894622006-08-13 19:58:17 +0000199 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
200 // declaration-specifiers init-declarator-list[opt] ';'
201 if (Tok.getKind() == tok::semi) {
Chris Lattner0e894622006-08-13 19:58:17 +0000202 ConsumeToken();
Chris Lattner200bdc32006-11-19 02:43:37 +0000203 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Chris Lattner0e894622006-08-13 19:58:17 +0000204 }
205
Chris Lattner53361ac2006-08-10 05:19:57 +0000206 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
207 ParseDeclarator(DeclaratorInfo);
208
Chris Lattner302b4be2006-11-19 02:31:38 +0000209 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner53361ac2006-08-10 05:19:57 +0000210}
211
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000212/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
213/// parsing 'declaration-specifiers declarator'. This method is split out this
214/// way to handle the ambiguity between top-level function-definitions and
215/// declarations.
216///
217/// declaration: [C99 6.7]
218/// declaration-specifiers init-declarator-list[opt] ';' [TODO]
219/// [!C99] init-declarator-list ';' [TODO]
220/// [OMP] threadprivate-directive [TODO]
221///
222/// init-declarator-list: [C99 6.7]
223/// init-declarator
224/// init-declarator-list ',' init-declarator
225/// init-declarator: [C99 6.7]
226/// declarator
227/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000228/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
229/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000230///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000231Parser::DeclTy *Parser::
232ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
233
234 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
235 // that they can be chained properly if the actions want this.
236 Parser::DeclTy *LastDeclInGroup = 0;
237
Chris Lattner53361ac2006-08-10 05:19:57 +0000238 // At this point, we know that it is not a function definition. Parse the
239 // rest of the init-declarator-list.
240 while (1) {
Chris Lattner6d7e6342006-08-15 03:41:14 +0000241 // If a simple-asm-expr is present, parse it.
242 if (Tok.getKind() == tok::kw_asm)
243 ParseSimpleAsm();
244
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000245 // If attributes are present, parse them.
246 if (Tok.getKind() == tok::kw___attribute)
Steve Naroffb8371e12007-06-09 03:39:29 +0000247 D.AddAttribute(ParseAttributes());
Chris Lattner6d7e6342006-08-15 03:41:14 +0000248
Chris Lattner53361ac2006-08-10 05:19:57 +0000249 // Parse declarator '=' initializer.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000250 ExprResult Init;
Chris Lattner53361ac2006-08-10 05:19:57 +0000251 if (Tok.getKind() == tok::equal) {
252 ConsumeToken();
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000253 Init = ParseInitializer();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000254 if (Init.isInvalid) {
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000255 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000256 return 0;
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000257 }
Chris Lattner53361ac2006-08-10 05:19:57 +0000258 }
259
Chris Lattner697e5d62006-11-09 06:32:27 +0000260 // Inform the current actions module that we just parsed this declarator.
Chris Lattner289ab7b2006-11-08 06:54:53 +0000261 // FIXME: pass asm & attributes.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000262 LastDeclInGroup = Actions.ParseDeclarator(CurScope, D, Init.Val,
263 LastDeclInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +0000264
265 // If we don't have a comma, it is either the end of the list (a ';') or an
266 // error, bail out.
267 if (Tok.getKind() != tok::comma)
268 break;
269
270 // Consume the comma.
271 ConsumeToken();
272
273 // Parse the next declarator.
274 D.clear();
275 ParseDeclarator(D);
276 }
277
278 if (Tok.getKind() == tok::semi) {
279 ConsumeToken();
Chris Lattner776fac82007-06-09 00:53:06 +0000280 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +0000281 }
Chris Lattner776fac82007-06-09 00:53:06 +0000282
283 Diag(Tok, diag::err_parse_error);
284 // Skip to end of block or statement
285 SkipUntil(tok::r_brace, true);
286 if (Tok.getKind() == tok::semi)
287 ConsumeToken();
288 return 0;
Chris Lattner53361ac2006-08-10 05:19:57 +0000289}
290
Chris Lattner1890ac82006-08-13 01:16:23 +0000291/// ParseSpecifierQualifierList
292/// specifier-qualifier-list:
293/// type-specifier specifier-qualifier-list[opt]
294/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000295/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000296///
297void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
298 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
299 /// parse declaration-specifiers and complain about extra stuff.
300 SourceLocation Loc = Tok.getLocation();
301 ParseDeclarationSpecifiers(DS);
302
303 // Validate declspec for type-name.
304 unsigned Specs = DS.getParsedSpecifiers();
305 if (Specs == DeclSpec::PQ_None)
306 Diag(Tok, diag::err_typename_requires_specqual);
307
Chris Lattner1b22eed2006-11-28 05:12:07 +0000308 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000309 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000310 if (DS.getStorageClassSpecLoc().isValid())
311 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
312 else
313 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000314 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000315 }
Chris Lattner1b22eed2006-11-28 05:12:07 +0000316
317 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000318 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000319 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000320 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000321 }
322}
Chris Lattner53361ac2006-08-10 05:19:57 +0000323
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000324/// ParseDeclarationSpecifiers
325/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000326/// storage-class-specifier declaration-specifiers[opt]
327/// type-specifier declaration-specifiers[opt]
328/// type-qualifier declaration-specifiers[opt]
329/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000330/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000331///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000332/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000333/// 'typedef'
334/// 'extern'
335/// 'static'
336/// 'auto'
337/// 'register'
338/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000339/// type-specifier: [C99 6.7.2]
340/// 'void'
341/// 'char'
342/// 'short'
343/// 'int'
344/// 'long'
345/// 'float'
346/// 'double'
347/// 'signed'
348/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000349/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000350/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000351/// typedef-name
Bill Wendling4073ed52007-02-13 01:51:42 +0000352/// [C++] 'bool'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000353/// [C99] '_Bool'
354/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000355/// [C99] '_Imaginary' // Removed in TC2?
356/// [GNU] '_Decimal32'
357/// [GNU] '_Decimal64'
358/// [GNU] '_Decimal128'
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000359/// [GNU] typeof-specifier [TODO]
Chris Lattner3b561a32006-08-13 00:12:11 +0000360/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000361/// [OBJC] typedef-name objc-protocol-refs [TODO]
362/// [OBJC] objc-protocol-refs [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000363/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000364/// 'const'
365/// 'volatile'
366/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000367/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000368/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000369///
370void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000371 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000372 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000373 const char *PrevSpec = 0;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000374 SourceLocation Loc = Tok.getLocation();
375
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000376 switch (Tok.getKind()) {
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000377 // typedef-name
378 case tok::identifier:
379 // This identifier can only be a typedef name if we haven't already seen
Chris Lattner5646b3e2006-08-15 05:12:01 +0000380 // a type-specifier. Without this check we misparse:
381 // typedef int X; struct Y { short X; }; as 'short int'.
Chris Lattnerf055d432006-11-28 04:28:12 +0000382 if (!DS.hasTypeSpecifier()) {
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000383 // It has to be available as a typedef too!
384 if (void *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(),
385 CurScope)) {
Chris Lattnerb20e8942006-11-28 05:30:29 +0000386 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000387 TypeRep);
Chris Lattneredc9e392006-12-02 06:21:46 +0000388 break;
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000389 }
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000390 }
391 // FALL THROUGH.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000392 default:
393 // If this is not a declaration specifier token, we're done reading decl
394 // specifiers. First verify that DeclSpec's are consistent.
Chris Lattnerb20e8942006-11-28 05:30:29 +0000395 DS.Finish(Diags, getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000396 return;
Chris Lattnere37e2332006-08-15 04:50:22 +0000397
398 // GNU attributes support.
399 case tok::kw___attribute:
Steve Naroffb8371e12007-06-09 03:39:29 +0000400 DS.AddAttribute(ParseAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +0000401 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000402
403 // storage-class-specifier
404 case tok::kw_typedef:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000405 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000406 break;
407 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000408 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000409 Diag(Tok, diag::ext_thread_before, "extern");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000410 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000411 break;
412 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000413 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000414 Diag(Tok, diag::ext_thread_before, "static");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000415 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000416 break;
417 case tok::kw_auto:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000418 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000419 break;
420 case tok::kw_register:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000421 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000422 break;
423 case tok::kw___thread:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000424 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000425 break;
426
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000427 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000428 case tok::kw_short:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000429 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000430 break;
431 case tok::kw_long:
Chris Lattner353f5742006-11-28 04:50:12 +0000432 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
Chris Lattnerb20e8942006-11-28 05:30:29 +0000433 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
Chris Lattner353f5742006-11-28 04:50:12 +0000434 else
Chris Lattnerb20e8942006-11-28 05:30:29 +0000435 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000436 break;
437 case tok::kw_signed:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000438 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000439 break;
440 case tok::kw_unsigned:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000441 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000442 break;
443 case tok::kw__Complex:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000444 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000445 break;
446 case tok::kw__Imaginary:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000447 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000448 break;
449 case tok::kw_void:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000450 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000451 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000452 case tok::kw_char:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000453 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000454 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000455 case tok::kw_int:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000456 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000457 break;
458 case tok::kw_float:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000459 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000460 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000461 case tok::kw_double:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000462 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000463 break;
Bill Wendling4073ed52007-02-13 01:51:42 +0000464 case tok::kw_bool: // [C++ 2.11p1]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000465 case tok::kw__Bool:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000466 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000467 break;
468 case tok::kw__Decimal32:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000469 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000470 break;
471 case tok::kw__Decimal64:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000472 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000473 break;
474 case tok::kw__Decimal128:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000475 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000476 break;
477
Chris Lattner1890ac82006-08-13 01:16:23 +0000478 case tok::kw_struct:
479 case tok::kw_union:
480 ParseStructUnionSpecifier(DS);
481 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000482 case tok::kw_enum:
483 ParseEnumSpecifier(DS);
484 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000485
486 // type-qualifier
487 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000488 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
489 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000490 break;
491 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000492 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
493 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000494 break;
495 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000496 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
497 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000498 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000499
500 // function-specifier
501 case tok::kw_inline:
Chris Lattner1b22eed2006-11-28 05:12:07 +0000502 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000503 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000504 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000505 // If the specifier combination wasn't legal, issue a diagnostic.
506 if (isInvalid) {
507 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000508 if (isInvalid == 1) // Error.
509 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
510 else // extwarn.
511 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000512 }
513 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000514 }
515}
516
Chris Lattnerffbc2712007-01-25 06:05:38 +0000517/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
518/// the first token has already been read and has been turned into an instance
519/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
520/// otherwise it returns false and fills in Decl.
521bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
Steve Naroffb8371e12007-06-09 03:39:29 +0000522 AttributeList *Attr = 0;
Chris Lattnere37e2332006-08-15 04:50:22 +0000523 // If attributes exist after tag, parse them.
524 if (Tok.getKind() == tok::kw___attribute)
Steve Naroffb8371e12007-06-09 03:39:29 +0000525 Attr = ParseAttributes();
Chris Lattnerffbc2712007-01-25 06:05:38 +0000526
Chris Lattner1890ac82006-08-13 01:16:23 +0000527 // Must have either 'struct name' or 'struct {...}'.
528 if (Tok.getKind() != tok::identifier &&
529 Tok.getKind() != tok::l_brace) {
530 Diag(Tok, diag::err_expected_ident_lbrace);
Chris Lattner8c6519a2007-01-22 07:41:36 +0000531 // TODO: better error recovery here.
Chris Lattnerffbc2712007-01-25 06:05:38 +0000532 return true;
Chris Lattner1890ac82006-08-13 01:16:23 +0000533 }
534
Chris Lattner8c6519a2007-01-22 07:41:36 +0000535 // If an identifier is present, consume and remember it.
536 IdentifierInfo *Name = 0;
537 SourceLocation NameLoc;
538 if (Tok.getKind() == tok::identifier) {
539 Name = Tok.getIdentifierInfo();
540 NameLoc = ConsumeToken();
541 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000542
Chris Lattner8c6519a2007-01-22 07:41:36 +0000543 // There are three options here. If we have 'struct foo;', then this is a
544 // forward declaration. If we have 'struct foo {...' then this is a
Chris Lattner7b9ace62007-01-23 20:11:08 +0000545 // definition. Otherwise we have something like 'struct foo xyz', a reference.
Chris Lattner8799cf22007-01-23 01:57:16 +0000546 //
547 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
548 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
549 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
550 //
Chris Lattner7b9ace62007-01-23 20:11:08 +0000551 Action::TagKind TK;
552 if (Tok.getKind() == tok::l_brace)
553 TK = Action::TK_Definition;
554 else if (Tok.getKind() == tok::semi)
555 TK = Action::TK_Declaration;
556 else
557 TK = Action::TK_Reference;
Steve Naroffb8371e12007-06-09 03:39:29 +0000558 Decl = Actions.ParseTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Chris Lattnerffbc2712007-01-25 06:05:38 +0000559 return false;
560}
561
562
563/// ParseStructUnionSpecifier
564/// struct-or-union-specifier: [C99 6.7.2.1]
565/// struct-or-union identifier[opt] '{' struct-contents '}'
566/// struct-or-union identifier
567/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
568/// '}' attributes[opt]
569/// [GNU] struct-or-union attributes[opt] identifier
570/// struct-or-union:
571/// 'struct'
572/// 'union'
573///
574void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
575 assert((Tok.getKind() == tok::kw_struct ||
576 Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
577 DeclSpec::TST TagType =
578 Tok.getKind() == tok::kw_union ? DeclSpec::TST_union : DeclSpec::TST_struct;
579 SourceLocation StartLoc = ConsumeToken();
580
581 // Parse the tag portion of this.
582 DeclTy *TagDecl;
583 if (ParseTag(TagDecl, TagType, StartLoc))
584 return;
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000585
Chris Lattner90a26b02007-01-23 04:38:16 +0000586 // If there is a body, parse it and inform the actions module.
587 if (Tok.getKind() == tok::l_brace)
Chris Lattner1300fb92007-01-23 23:42:53 +0000588 ParseStructUnionBody(StartLoc, TagType, TagDecl);
Chris Lattnerda72c822006-08-13 22:16:42 +0000589
590 const char *PrevSpec = 0;
Chris Lattnerb9d572a2007-01-23 04:58:34 +0000591 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000592 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000593}
594
595
Chris Lattner90a26b02007-01-23 04:38:16 +0000596/// ParseStructUnionBody
597/// struct-contents:
598/// struct-declaration-list
599/// [EXT] empty
600/// [GNU] "struct-declaration-list" without terminatoring ';' [TODO]
601/// struct-declaration-list:
602/// struct-declaration
603/// struct-declaration-list struct-declaration
604/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
605/// struct-declaration:
606/// specifier-qualifier-list struct-declarator-list ';'
607/// [GNU] __extension__ struct-declaration [TODO]
608/// [GNU] specifier-qualifier-list ';' [TODO]
609/// struct-declarator-list:
610/// struct-declarator
611/// struct-declarator-list ',' struct-declarator
612/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
613/// struct-declarator:
614/// declarator
615/// [GNU] declarator attributes[opt]
616/// declarator[opt] ':' constant-expression
617/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
618///
Chris Lattner1300fb92007-01-23 23:42:53 +0000619void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
620 unsigned TagType, DeclTy *TagDecl) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000621 SourceLocation LBraceLoc = ConsumeBrace();
622
Chris Lattner7b9ace62007-01-23 20:11:08 +0000623 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
624 // C++.
Chris Lattner90a26b02007-01-23 04:38:16 +0000625 if (Tok.getKind() == tok::r_brace)
626 Diag(Tok, diag::ext_empty_struct_union_enum,
627 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Chris Lattner7b9ace62007-01-23 20:11:08 +0000628
Chris Lattner1300fb92007-01-23 23:42:53 +0000629 SmallVector<DeclTy*, 32> FieldDecls;
630
Chris Lattner7b9ace62007-01-23 20:11:08 +0000631 // While we still have something to read, read the declarations in the struct.
Chris Lattner90a26b02007-01-23 04:38:16 +0000632 while (Tok.getKind() != tok::r_brace &&
633 Tok.getKind() != tok::eof) {
634 // Each iteration of this loop reads one struct-declaration.
635
636 // Parse the common specifier-qualifiers-list piece.
637 DeclSpec DS;
638 SourceLocation SpecQualLoc = Tok.getLocation();
639 ParseSpecifierQualifierList(DS);
640 // TODO: Does specifier-qualifier list correctly check that *something* is
641 // specified?
642
Chris Lattner90a26b02007-01-23 04:38:16 +0000643 // If there are no declarators, issue a warning.
644 if (Tok.getKind() == tok::semi) {
645 Diag(SpecQualLoc, diag::w_no_declarators);
Chris Lattner7b9ace62007-01-23 20:11:08 +0000646 ConsumeToken();
647 continue;
648 }
649
650 // Read struct-declarators until we find the semicolon.
651 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
652
653 while (1) {
654 /// struct-declarator: declarator
655 /// struct-declarator: declarator[opt] ':' constant-expression
656 if (Tok.getKind() != tok::colon)
657 ParseDeclarator(DeclaratorInfo);
658
659 ExprTy *BitfieldSize = 0;
660 if (Tok.getKind() == tok::colon) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000661 ConsumeToken();
Chris Lattner7b9ace62007-01-23 20:11:08 +0000662 ExprResult Res = ParseConstantExpression();
663 if (Res.isInvalid) {
664 SkipUntil(tok::semi, true, true);
665 } else {
666 BitfieldSize = Res.Val;
667 }
Chris Lattner90a26b02007-01-23 04:38:16 +0000668 }
Chris Lattner7b9ace62007-01-23 20:11:08 +0000669
670 // If attributes exist after the declarator, parse them.
671 if (Tok.getKind() == tok::kw___attribute)
Steve Naroffb8371e12007-06-09 03:39:29 +0000672 DeclaratorInfo.AddAttribute(ParseAttributes());
Chris Lattner7b9ace62007-01-23 20:11:08 +0000673
Chris Lattner367b0192007-01-23 22:29:13 +0000674 // Install the declarator into the current TagDecl.
Chris Lattner1300fb92007-01-23 23:42:53 +0000675 DeclTy *Field = Actions.ParseField(CurScope, TagDecl, SpecQualLoc,
676 DeclaratorInfo, BitfieldSize);
677 FieldDecls.push_back(Field);
Chris Lattner7b9ace62007-01-23 20:11:08 +0000678
679 // If we don't have a comma, it is either the end of the list (a ';')
680 // or an error, bail out.
681 if (Tok.getKind() != tok::comma)
682 break;
683
684 // Consume the comma.
685 ConsumeToken();
686
687 // Parse the next declarator.
688 DeclaratorInfo.clear();
689
690 // Attributes are only allowed on the second declarator.
691 if (Tok.getKind() == tok::kw___attribute)
Steve Naroffb8371e12007-06-09 03:39:29 +0000692 DeclaratorInfo.AddAttribute(ParseAttributes());
Chris Lattner90a26b02007-01-23 04:38:16 +0000693 }
694
695 if (Tok.getKind() == tok::semi) {
696 ConsumeToken();
697 } else {
698 Diag(Tok, diag::err_expected_semi_decl_list);
699 // Skip to end of block or statement
700 SkipUntil(tok::r_brace, true, true);
701 }
702 }
703
704 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
705
Chris Lattnerc1915e22007-01-25 07:29:02 +0000706 Actions.ParseRecordBody(RecordLoc, TagDecl, &FieldDecls[0],FieldDecls.size());
707
Steve Naroffb8371e12007-06-09 03:39:29 +0000708 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +0000709 // If attributes exist after struct contents, parse them.
710 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000711 AttrList = ParseAttributes(); // FIXME: where should I put them?
Chris Lattner90a26b02007-01-23 04:38:16 +0000712}
713
714
Chris Lattner3b561a32006-08-13 00:12:11 +0000715/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000716/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000717/// 'enum' identifier[opt] '{' enumerator-list '}'
718/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000719/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
720/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000721/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000722/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000723void Parser::ParseEnumSpecifier(DeclSpec &DS) {
724 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattnerb20e8942006-11-28 05:30:29 +0000725 SourceLocation StartLoc = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000726
Chris Lattnerffbc2712007-01-25 06:05:38 +0000727 // Parse the tag portion of this.
728 DeclTy *TagDecl;
729 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
Chris Lattner3b561a32006-08-13 00:12:11 +0000730 return;
Chris Lattner3b561a32006-08-13 00:12:11 +0000731
Chris Lattnerc1915e22007-01-25 07:29:02 +0000732 if (Tok.getKind() == tok::l_brace)
733 ParseEnumBody(StartLoc, TagDecl);
734
Chris Lattner3b561a32006-08-13 00:12:11 +0000735 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000736 const char *PrevSpec = 0;
Chris Lattnerffbc2712007-01-25 06:05:38 +0000737 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000738 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000739}
740
Chris Lattnerc1915e22007-01-25 07:29:02 +0000741/// ParseEnumBody - Parse a {} enclosed enumerator-list.
742/// enumerator-list:
743/// enumerator
744/// enumerator-list ',' enumerator
745/// enumerator:
746/// enumeration-constant
747/// enumeration-constant '=' constant-expression
748/// enumeration-constant:
749/// identifier
750///
751void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
752 SourceLocation LBraceLoc = ConsumeBrace();
753
754 if (Tok.getKind() == tok::r_brace)
755 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
756
757 SmallVector<DeclTy*, 32> EnumConstantDecls;
758
759 // Parse the enumerator-list.
760 while (Tok.getKind() == tok::identifier) {
761 IdentifierInfo *Ident = Tok.getIdentifierInfo();
762 SourceLocation IdentLoc = ConsumeToken();
763
764 SourceLocation EqualLoc;
765 ExprTy *AssignedVal = 0;
766 if (Tok.getKind() == tok::equal) {
767 EqualLoc = ConsumeToken();
768 ExprResult Res = ParseConstantExpression();
769 if (Res.isInvalid)
Chris Lattnerda6c2ce2007-04-27 19:13:15 +0000770 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000771 else
772 AssignedVal = Res.Val;
773 }
774
775 // Install the enumerator constant into EnumDecl.
776 DeclTy *ConstDecl = Actions.ParseEnumConstant(CurScope, EnumDecl,
777 IdentLoc, Ident,
778 EqualLoc, AssignedVal);
779 EnumConstantDecls.push_back(ConstDecl);
780
781 if (Tok.getKind() != tok::comma)
782 break;
783 SourceLocation CommaLoc = ConsumeToken();
784
785 if (Tok.getKind() != tok::identifier && !getLang().C99)
786 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
787 }
788
789 // Eat the }.
790 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
791
792 Actions.ParseEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
793 EnumConstantDecls.size());
794
Steve Naroff0f2fe172007-06-01 17:11:19 +0000795 DeclTy *AttrList = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000796 // If attributes exist after the identifier list, parse them.
797 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000798 AttrList = ParseAttributes(); // FIXME: where do they do?
Chris Lattnerc1915e22007-01-25 07:29:02 +0000799}
Chris Lattner3b561a32006-08-13 00:12:11 +0000800
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000801/// isTypeSpecifierQualifier - Return true if the current token could be the
802/// start of a specifier-qualifier-list.
803bool Parser::isTypeSpecifierQualifier() const {
804 switch (Tok.getKind()) {
805 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000806 // GNU attributes support.
807 case tok::kw___attribute:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000808 // type-specifiers
809 case tok::kw_short:
810 case tok::kw_long:
811 case tok::kw_signed:
812 case tok::kw_unsigned:
813 case tok::kw__Complex:
814 case tok::kw__Imaginary:
815 case tok::kw_void:
816 case tok::kw_char:
817 case tok::kw_int:
818 case tok::kw_float:
819 case tok::kw_double:
820 case tok::kw__Bool:
821 case tok::kw__Decimal32:
822 case tok::kw__Decimal64:
823 case tok::kw__Decimal128:
824
825 // struct-or-union-specifier
826 case tok::kw_struct:
827 case tok::kw_union:
828 // enum-specifier
829 case tok::kw_enum:
830
831 // type-qualifier
832 case tok::kw_const:
833 case tok::kw_volatile:
834 case tok::kw_restrict:
835 return true;
836
837 // typedef-name
838 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000839 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000840
841 // TODO: Attributes.
842 }
843}
844
Chris Lattneracd58a32006-08-06 17:24:14 +0000845/// isDeclarationSpecifier() - Return true if the current token is part of a
846/// declaration specifier.
847bool Parser::isDeclarationSpecifier() const {
848 switch (Tok.getKind()) {
849 default: return false;
850 // storage-class-specifier
851 case tok::kw_typedef:
852 case tok::kw_extern:
853 case tok::kw_static:
854 case tok::kw_auto:
855 case tok::kw_register:
856 case tok::kw___thread:
857
858 // type-specifiers
859 case tok::kw_short:
860 case tok::kw_long:
861 case tok::kw_signed:
862 case tok::kw_unsigned:
863 case tok::kw__Complex:
864 case tok::kw__Imaginary:
865 case tok::kw_void:
866 case tok::kw_char:
867 case tok::kw_int:
868 case tok::kw_float:
869 case tok::kw_double:
870 case tok::kw__Bool:
871 case tok::kw__Decimal32:
872 case tok::kw__Decimal64:
873 case tok::kw__Decimal128:
874
875 // struct-or-union-specifier
876 case tok::kw_struct:
877 case tok::kw_union:
878 // enum-specifier
879 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000880
Chris Lattneracd58a32006-08-06 17:24:14 +0000881 // type-qualifier
882 case tok::kw_const:
883 case tok::kw_volatile:
884 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000885
Chris Lattneracd58a32006-08-06 17:24:14 +0000886 // function-specifier
887 case tok::kw_inline:
888 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000889
Chris Lattneracd58a32006-08-06 17:24:14 +0000890 // typedef-name
891 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000892 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +0000893 // TODO: Attributes.
894 }
895}
896
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000897
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000898/// ParseTypeQualifierListOpt
899/// type-qualifier-list: [C99 6.7.5]
900/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000901/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000902/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000903/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000904///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000905void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000906 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000907 int isInvalid = false;
908 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +0000909 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000910
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000911 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000912 default:
Chris Lattnere37e2332006-08-15 04:50:22 +0000913 // If this is not a type-qualifier token, we're done reading type
914 // qualifiers. First verify that DeclSpec's are consistent.
Chris Lattnerb20e8942006-11-28 05:30:29 +0000915 DS.Finish(Diags, getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000916 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000917 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000918 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
919 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000920 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000921 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000922 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
923 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000924 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000925 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000926 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
927 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000928 break;
Chris Lattnere37e2332006-08-15 04:50:22 +0000929 case tok::kw___attribute:
Steve Naroffb8371e12007-06-09 03:39:29 +0000930 DS.AddAttribute(ParseAttributes());
Steve Naroff98d153c2007-06-06 23:19:11 +0000931 continue; // do *not* consume the next token!
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000932 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000933
934 // If the specifier combination wasn't legal, issue a diagnostic.
935 if (isInvalid) {
936 assert(PrevSpec && "Method did not return previous specifier!");
937 if (isInvalid == 1) // Error.
938 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
939 else // extwarn.
940 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
941 }
942 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000943 }
944}
945
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000946
947/// ParseDeclarator - Parse and verify a newly-initialized declarator.
948///
949void Parser::ParseDeclarator(Declarator &D) {
950 /// This implements the 'declarator' production in the C grammar, then checks
951 /// for well-formedness and issues diagnostics.
952 ParseDeclaratorInternal(D);
953
Chris Lattner9fab3b92006-08-12 18:25:42 +0000954 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000955
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000956}
957
958/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000959/// declarator: [C99 6.7.5]
960/// pointer[opt] direct-declarator
Bill Wendling93efb222007-06-02 23:28:54 +0000961/// [C++] '&' declarator [C++ 8p4, dcl.decl]
962/// [GNU] '&' restrict[opt] attributes[opt] declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +0000963///
964/// pointer: [C99 6.7.5]
965/// '*' type-qualifier-list[opt]
966/// '*' type-qualifier-list[opt] pointer
967///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000968void Parser::ParseDeclaratorInternal(Declarator &D) {
Bill Wendling3708c182007-05-27 10:15:43 +0000969 tok::TokenKind Kind = Tok.getKind();
970
971 // Not a pointer or C++ reference.
972 if (Kind != tok::star && !(Kind == tok::amp && getLang().CPlusPlus))
Chris Lattner6c7416c2006-08-07 00:19:33 +0000973 return ParseDirectDeclarator(D);
974
Bill Wendling3708c182007-05-27 10:15:43 +0000975 // Otherwise, '*' -> pointer or '&' -> reference.
976 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
977
978 if (Kind == tok::star) {
979 // Is a pointer
980 DeclSpec DS;
Steve Naroff98d153c2007-06-06 23:19:11 +0000981
Bill Wendling3708c182007-05-27 10:15:43 +0000982 ParseTypeQualifierListOpt(DS);
Chris Lattner6c7416c2006-08-07 00:19:33 +0000983
Bill Wendling3708c182007-05-27 10:15:43 +0000984 // Recursively parse the declarator.
985 ParseDeclaratorInternal(D);
Chris Lattner9dfdb3c2006-11-13 07:38:09 +0000986
Bill Wendling3708c182007-05-27 10:15:43 +0000987 // Remember that we parsed a pointer type, and remember the type-quals.
988 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc));
989 } else {
990 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +0000991 DeclSpec DS;
992
993 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
994 // cv-qualifiers are introduced through the use of a typedef or of a
995 // template type argument, in which case the cv-qualifiers are ignored.
996 //
997 // [GNU] Retricted references are allowed.
998 // [GNU] Attributes on references are allowed.
999 ParseTypeQualifierListOpt(DS);
1000
1001 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1002 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1003 Diag(DS.getConstSpecLoc(),
1004 diag::err_invalid_reference_qualifier_application,
1005 "const");
1006 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1007 Diag(DS.getVolatileSpecLoc(),
1008 diag::err_invalid_reference_qualifier_application,
1009 "volatile");
1010 }
Bill Wendling3708c182007-05-27 10:15:43 +00001011
1012 // Recursively parse the declarator.
1013 ParseDeclaratorInternal(D);
1014
1015 // Remember that we parsed a reference type. It doesn't have type-quals.
Bill Wendling93efb222007-06-02 23:28:54 +00001016 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc));
Bill Wendling3708c182007-05-27 10:15:43 +00001017 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001018}
1019
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001020/// ParseDirectDeclarator
1021/// direct-declarator: [C99 6.7.5]
1022/// identifier
1023/// '(' declarator ')'
1024/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00001025/// [C90] direct-declarator '[' constant-expression[opt] ']'
1026/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1027/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1028/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1029/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001030/// direct-declarator '(' parameter-type-list ')'
1031/// direct-declarator '(' identifier-list[opt] ')'
1032/// [GNU] direct-declarator '(' parameter-forward-declarations
1033/// parameter-type-list[opt] ')'
1034///
Chris Lattneracd58a32006-08-06 17:24:14 +00001035void Parser::ParseDirectDeclarator(Declarator &D) {
1036 // Parse the first direct-declarator seen.
1037 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
1038 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1039 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1040 ConsumeToken();
1041 } else if (Tok.getKind() == tok::l_paren) {
1042 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001043 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001044 // Example: 'char (*X)' or 'int (*XX)(void)'
1045 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001046 } else if (D.mayOmitIdentifier()) {
1047 // This could be something simple like "int" (in which case the declarator
1048 // portion is empty), if an abstract-declarator is allowed.
1049 D.SetIdentifier(0, Tok.getLocation());
1050 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +00001051 // Expected identifier or '('.
1052 Diag(Tok, diag::err_expected_ident_lparen);
1053 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +00001054 }
1055
1056 assert(D.isPastIdentifier() &&
1057 "Haven't past the location of the identifier yet?");
1058
1059 while (1) {
1060 if (Tok.getKind() == tok::l_paren) {
1061 ParseParenDeclarator(D);
1062 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001063 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001064 } else {
1065 break;
1066 }
1067 }
1068}
1069
1070/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
1071/// either be before the identifier (in which case these are just grouping
1072/// parens for precedence) or it may be after the identifier, in which case
1073/// these are function arguments.
1074///
1075/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001076/// parameter-type-list: [C99 6.7.5]
1077/// parameter-list
1078/// parameter-list ',' '...'
1079///
1080/// parameter-list: [C99 6.7.5]
1081/// parameter-declaration
1082/// parameter-list ',' parameter-declaration
1083///
1084/// parameter-declaration: [C99 6.7.5]
1085/// declaration-specifiers declarator
Chris Lattnere37e2332006-08-15 04:50:22 +00001086/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001087/// declaration-specifiers abstract-declarator[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001088/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001089///
1090/// identifier-list: [C99 6.7.5]
1091/// identifier
1092/// identifier-list ',' identifier
1093///
Chris Lattneracd58a32006-08-06 17:24:14 +00001094void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001095 SourceLocation StartLoc = ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001096
Chris Lattneracd58a32006-08-06 17:24:14 +00001097 // If we haven't past the identifier yet (or where the identifier would be
1098 // stored, if this is an abstract declarator), then this is probably just
1099 // grouping parens.
1100 if (!D.isPastIdentifier()) {
1101 // Okay, this is probably a grouping paren. However, if this could be an
1102 // abstract-declarator, then this could also be the start of function
1103 // arguments (consider 'void()').
1104 bool isGrouping;
1105
1106 if (!D.mayOmitIdentifier()) {
1107 // If this can't be an abstract-declarator, this *must* be a grouping
1108 // paren, because we haven't seen the identifier yet.
1109 isGrouping = true;
1110 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
1111 isDeclarationSpecifier()) { // 'int(int)' is a function.
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001112 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1113 // considered to be a type, not a K&R identifier-list.
Chris Lattneracd58a32006-08-06 17:24:14 +00001114 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001115 } else {
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001116 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +00001117 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001118 }
Chris Lattneracd58a32006-08-06 17:24:14 +00001119
1120 // If this is a grouping paren, handle:
1121 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001122 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001123 if (isGrouping) {
Chris Lattnere37e2332006-08-15 04:50:22 +00001124 if (Tok.getKind() == tok::kw___attribute)
Steve Naroffb8371e12007-06-09 03:39:29 +00001125 D.AddAttribute(ParseAttributes());
Chris Lattnere37e2332006-08-15 04:50:22 +00001126
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001127 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +00001128 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +00001129 MatchRHSPunctuation(tok::r_paren, StartLoc);
Chris Lattneracd58a32006-08-06 17:24:14 +00001130 return;
1131 }
1132
1133 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +00001134 // argument list. Recognize that this declarator will never have an
1135 // identifier (and remember where it would have been), then fall through to
1136 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +00001137 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001138 }
1139
Chris Lattneracd58a32006-08-06 17:24:14 +00001140 // Okay, this is the parameter list of a function definition, or it is an
1141 // identifier list of a K&R-style function.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001142 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +00001143 bool HasPrototype;
Chris Lattner14776b92006-08-06 22:27:40 +00001144 bool ErrorEmitted = false;
1145
Chris Lattneredc9e392006-12-02 06:21:46 +00001146 // Build up an array of information about the parsed arguments.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001147 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattnerad9ac942007-01-23 01:14:52 +00001148 SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Chris Lattneredc9e392006-12-02 06:21:46 +00001149
Chris Lattneracd58a32006-08-06 17:24:14 +00001150 if (Tok.getKind() == tok::r_paren) {
1151 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001152 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001153 HasPrototype = false;
1154 } else if (Tok.getKind() == tok::identifier &&
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001155 // K&R identifier lists can't have typedefs as identifiers, per
1156 // C99 6.7.5.3p11.
Steve Naroffb419d3a2006-10-27 23:18:49 +00001157 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001158 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1159 // normal declarators, not for abstract-declarators.
1160 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
1161
1162 // If there was no identifier specified, either we are in an
1163 // abstract-declarator, or we are in a parameter declarator which was found
1164 // to be abstract. In abstract-declarators, identifier lists are not valid,
1165 // diagnose this.
1166 if (!D.getIdentifier())
1167 Diag(Tok, diag::ext_ident_list_in_param);
Chris Lattneredc9e392006-12-02 06:21:46 +00001168
Chris Lattnercbc426d2006-12-02 06:43:02 +00001169 // Remember this identifier in ParamInfo.
1170 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1171 Tok.getLocation(), 0));
1172
Chris Lattneracd58a32006-08-06 17:24:14 +00001173 ConsumeToken();
1174 while (Tok.getKind() == tok::comma) {
1175 // Eat the comma.
1176 ConsumeToken();
1177
Chris Lattnercbc426d2006-12-02 06:43:02 +00001178 if (Tok.getKind() != tok::identifier) {
1179 Diag(Tok, diag::err_expected_ident);
Chris Lattner14776b92006-08-06 22:27:40 +00001180 ErrorEmitted = true;
1181 break;
1182 }
Chris Lattnercbc426d2006-12-02 06:43:02 +00001183
Chris Lattner969ca152006-12-03 06:29:03 +00001184 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
1185
1186 // Verify that the argument identifier has not already been mentioned.
Chris Lattnerbaf33662007-01-27 02:14:08 +00001187 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerad9ac942007-01-23 01:14:52 +00001188 Diag(Tok.getLocation(), diag::err_param_redefinition,ParmII->getName());
1189 ParmII = 0;
1190 }
Chris Lattner969ca152006-12-03 06:29:03 +00001191
Chris Lattnercbc426d2006-12-02 06:43:02 +00001192 // Remember this identifier in ParamInfo.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001193 if (ParmII)
1194 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1195 Tok.getLocation(), 0));
Chris Lattnercbc426d2006-12-02 06:43:02 +00001196
1197 // Eat the identifier.
1198 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00001199 }
1200
Chris Lattneracd58a32006-08-06 17:24:14 +00001201 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001202 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001203 HasPrototype = false;
1204 } else {
Chris Lattner43e956c2006-11-28 04:05:37 +00001205 // Finally, a normal, non-empty parameter type list.
1206
Chris Lattnercbc426d2006-12-02 06:43:02 +00001207 // Enter function-declaration scope, limiting any declarators for struct
1208 // tags to the function prototype scope.
1209 // FIXME: is this needed?
Chris Lattner43e956c2006-11-28 04:05:37 +00001210 EnterScope(0);
1211
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001212 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001213 while (1) {
1214 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001215 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +00001216
1217 // Check to see if this is "void(...)" which is not allowed.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001218 if (ParamInfo.empty()) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001219 // Otherwise, parse parameter type list. If it starts with an
1220 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +00001221 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001222 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +00001223 }
1224
1225 // Consume the ellipsis.
1226 ConsumeToken();
1227 break;
1228 }
1229
Chris Lattneracd58a32006-08-06 17:24:14 +00001230 // Parse the declaration-specifiers.
1231 DeclSpec DS;
1232 ParseDeclarationSpecifiers(DS);
1233
1234 // Parse the declarator. This is "PrototypeContext", because we must
1235 // accept either 'declarator' or 'abstract-declarator' here.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001236 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1237 ParseDeclarator(ParmDecl);
Chris Lattneracd58a32006-08-06 17:24:14 +00001238
Chris Lattnere37e2332006-08-15 04:50:22 +00001239 // Parse GNU attributes, if present.
1240 if (Tok.getKind() == tok::kw___attribute)
Steve Naroffb8371e12007-06-09 03:39:29 +00001241 ParmDecl.AddAttribute(ParseAttributes());
Chris Lattnere37e2332006-08-15 04:50:22 +00001242
Chris Lattner43e956c2006-11-28 04:05:37 +00001243 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001244 // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted.
1245 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1246 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
Chris Lattner4d8f8732006-11-28 05:05:08 +00001247 Diag(DS.getStorageClassSpecLoc(),
Chris Lattner43e956c2006-11-28 04:05:37 +00001248 diag::err_invalid_storage_class_in_func_decl);
Chris Lattner353f5742006-11-28 04:50:12 +00001249 DS.ClearStorageClassSpecs();
Chris Lattner43e956c2006-11-28 04:05:37 +00001250 }
Chris Lattner4d8f8732006-11-28 05:05:08 +00001251 if (DS.isThreadSpecified()) {
1252 Diag(DS.getThreadSpecLoc(),
1253 diag::err_invalid_storage_class_in_func_decl);
1254 DS.ClearStorageClassSpecs();
1255 }
Chris Lattner43e956c2006-11-28 04:05:37 +00001256
1257 // Inform the actions module about the parameter declarator, so it gets
1258 // added to the current scope.
Chris Lattner216d8652006-12-02 06:47:41 +00001259 Action::TypeResult ParamTy =
1260 Actions.ParseParamDeclaratorType(CurScope, ParmDecl);
Chris Lattnercbc426d2006-12-02 06:43:02 +00001261
1262 // Remember this parsed parameter in ParamInfo.
Chris Lattner969ca152006-12-03 06:29:03 +00001263 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1264
1265 // Verify that the argument identifier has not already been mentioned.
Chris Lattnerbaf33662007-01-27 02:14:08 +00001266 if (ParmII && !ParamsSoFar.insert(ParmII)) {
Chris Lattnerad9ac942007-01-23 01:14:52 +00001267 Diag(ParmDecl.getIdentifierLoc(), diag::err_param_redefinition,
1268 ParmII->getName());
1269 ParmII = 0;
Chris Lattner969ca152006-12-03 06:29:03 +00001270 }
1271
1272 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnercbc426d2006-12-02 06:43:02 +00001273 ParmDecl.getIdentifierLoc(),
1274 ParamTy.Val));
Chris Lattneracd58a32006-08-06 17:24:14 +00001275
1276 // If the next token is a comma, consume it and keep reading arguments.
1277 if (Tok.getKind() != tok::comma) break;
1278
1279 // Consume the comma.
1280 ConsumeToken();
1281 }
1282
1283 HasPrototype = true;
Chris Lattner43e956c2006-11-28 04:05:37 +00001284
1285 // Leave prototype scope.
1286 ExitScope();
Chris Lattneracd58a32006-08-06 17:24:14 +00001287 }
1288
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001289 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerd2e97c12006-12-03 02:03:33 +00001290 if (!ErrorEmitted)
1291 D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic,
1292 &ParamInfo[0], ParamInfo.size(),
1293 StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001294
Chris Lattner14776b92006-08-06 22:27:40 +00001295 // If we have the closing ')', eat it and we're done.
1296 if (Tok.getKind() == tok::r_paren) {
1297 ConsumeParen();
1298 } else {
1299 // If an error happened earlier parsing something else in the proto, don't
1300 // issue another error.
1301 if (!ErrorEmitted)
1302 Diag(Tok, diag::err_expected_rparen);
1303 SkipUntil(tok::r_paren);
1304 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001305}
Chris Lattneracd58a32006-08-06 17:24:14 +00001306
Chris Lattnere8074e62006-08-06 18:30:15 +00001307
1308/// [C90] direct-declarator '[' constant-expression[opt] ']'
1309/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1310/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1311/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1312/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1313void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001314 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001315
1316 // If valid, this location is the position where we read the 'static' keyword.
1317 SourceLocation StaticLoc;
Chris Lattneraf635312006-10-16 06:06:51 +00001318 if (Tok.getKind() == tok::kw_static)
1319 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001320
1321 // If there is a type-qualifier-list, read it now.
1322 DeclSpec DS;
1323 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001324
1325 // If we haven't already read 'static', check to see if there is one after the
1326 // type-qualifier-list.
Chris Lattneraf635312006-10-16 06:06:51 +00001327 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static)
1328 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001329
1330 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001331 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001332 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +00001333 if (Tok.getKind() == tok::star) {
1334 // Remember the '*' token, in case we have to un-get it.
1335 LexerToken StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +00001336 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +00001337
1338 // Check that the ']' token is present to avoid incorrectly parsing
1339 // expressions starting with '*' as [*].
1340 if (Tok.getKind() == tok::r_square) {
1341 if (StaticLoc.isValid())
1342 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1343 StaticLoc = SourceLocation(); // Drop the static.
1344 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +00001345 } else {
1346 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +00001347 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +00001348 // need to reparse it. This handles cases like 'X[*p + 4]'
1349 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +00001350 }
Chris Lattner9fab3b92006-08-12 18:25:42 +00001351 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001352 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001353 NumElements = ParseAssignmentExpression();
1354 }
1355
1356 // If there was an error parsing the assignment-expression, recover.
1357 if (NumElements.isInvalid) {
1358 // If the expression was invalid, skip it.
1359 SkipUntil(tok::r_square);
1360 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001361 }
1362
Chris Lattner04f80192006-08-15 04:55:54 +00001363 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001364
Chris Lattnere8074e62006-08-06 18:30:15 +00001365 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1366 // it was not a constant expression.
1367 if (!getLang().C99) {
1368 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001369 if (isStar || StaticLoc.isValid() ||
1370 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001371 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001372 }
Bill Wendling93efb222007-06-02 23:28:54 +00001373
Chris Lattner6c7416c2006-08-07 00:19:33 +00001374 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001375 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1376 StaticLoc.isValid(), isStar,
1377 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001378}
1379