blob: 86d390442acd2aafbbe1a0e92b0e933afae862af [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
Chris Lattner36e46a22007-06-09 05:49:55 +0000636 if (Tok.getKind() == tok::semi) {
637 Diag(Tok, diag::ext_extra_struct_semi);
638 ConsumeToken();
639 continue;
640 }
641
Chris Lattner90a26b02007-01-23 04:38:16 +0000642 // Parse the common specifier-qualifiers-list piece.
643 DeclSpec DS;
644 SourceLocation SpecQualLoc = Tok.getLocation();
645 ParseSpecifierQualifierList(DS);
646 // TODO: Does specifier-qualifier list correctly check that *something* is
647 // specified?
648
Chris Lattner90a26b02007-01-23 04:38:16 +0000649 // If there are no declarators, issue a warning.
650 if (Tok.getKind() == tok::semi) {
651 Diag(SpecQualLoc, diag::w_no_declarators);
Chris Lattner7b9ace62007-01-23 20:11:08 +0000652 ConsumeToken();
653 continue;
654 }
655
656 // Read struct-declarators until we find the semicolon.
657 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
658
659 while (1) {
660 /// struct-declarator: declarator
661 /// struct-declarator: declarator[opt] ':' constant-expression
662 if (Tok.getKind() != tok::colon)
663 ParseDeclarator(DeclaratorInfo);
664
665 ExprTy *BitfieldSize = 0;
666 if (Tok.getKind() == tok::colon) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000667 ConsumeToken();
Chris Lattner7b9ace62007-01-23 20:11:08 +0000668 ExprResult Res = ParseConstantExpression();
669 if (Res.isInvalid) {
670 SkipUntil(tok::semi, true, true);
671 } else {
672 BitfieldSize = Res.Val;
673 }
Chris Lattner90a26b02007-01-23 04:38:16 +0000674 }
Chris Lattner7b9ace62007-01-23 20:11:08 +0000675
676 // If attributes exist after the declarator, parse them.
677 if (Tok.getKind() == tok::kw___attribute)
Steve Naroffb8371e12007-06-09 03:39:29 +0000678 DeclaratorInfo.AddAttribute(ParseAttributes());
Chris Lattner7b9ace62007-01-23 20:11:08 +0000679
Chris Lattner367b0192007-01-23 22:29:13 +0000680 // Install the declarator into the current TagDecl.
Chris Lattner1300fb92007-01-23 23:42:53 +0000681 DeclTy *Field = Actions.ParseField(CurScope, TagDecl, SpecQualLoc,
682 DeclaratorInfo, BitfieldSize);
683 FieldDecls.push_back(Field);
Chris Lattner7b9ace62007-01-23 20:11:08 +0000684
685 // If we don't have a comma, it is either the end of the list (a ';')
686 // or an error, bail out.
687 if (Tok.getKind() != tok::comma)
688 break;
689
690 // Consume the comma.
691 ConsumeToken();
692
693 // Parse the next declarator.
694 DeclaratorInfo.clear();
695
696 // Attributes are only allowed on the second declarator.
697 if (Tok.getKind() == tok::kw___attribute)
Steve Naroffb8371e12007-06-09 03:39:29 +0000698 DeclaratorInfo.AddAttribute(ParseAttributes());
Chris Lattner90a26b02007-01-23 04:38:16 +0000699 }
700
701 if (Tok.getKind() == tok::semi) {
702 ConsumeToken();
703 } else {
704 Diag(Tok, diag::err_expected_semi_decl_list);
705 // Skip to end of block or statement
706 SkipUntil(tok::r_brace, true, true);
707 }
708 }
709
710 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
711
Chris Lattnerc1915e22007-01-25 07:29:02 +0000712 Actions.ParseRecordBody(RecordLoc, TagDecl, &FieldDecls[0],FieldDecls.size());
713
Steve Naroffb8371e12007-06-09 03:39:29 +0000714 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +0000715 // If attributes exist after struct contents, parse them.
716 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000717 AttrList = ParseAttributes(); // FIXME: where should I put them?
Chris Lattner90a26b02007-01-23 04:38:16 +0000718}
719
720
Chris Lattner3b561a32006-08-13 00:12:11 +0000721/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000722/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000723/// 'enum' identifier[opt] '{' enumerator-list '}'
724/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000725/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
726/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000727/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000728/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000729void Parser::ParseEnumSpecifier(DeclSpec &DS) {
730 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattnerb20e8942006-11-28 05:30:29 +0000731 SourceLocation StartLoc = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000732
Chris Lattnerffbc2712007-01-25 06:05:38 +0000733 // Parse the tag portion of this.
734 DeclTy *TagDecl;
735 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
Chris Lattner3b561a32006-08-13 00:12:11 +0000736 return;
Chris Lattner3b561a32006-08-13 00:12:11 +0000737
Chris Lattnerc1915e22007-01-25 07:29:02 +0000738 if (Tok.getKind() == tok::l_brace)
739 ParseEnumBody(StartLoc, TagDecl);
740
Chris Lattner3b561a32006-08-13 00:12:11 +0000741 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000742 const char *PrevSpec = 0;
Chris Lattnerffbc2712007-01-25 06:05:38 +0000743 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000744 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000745}
746
Chris Lattnerc1915e22007-01-25 07:29:02 +0000747/// ParseEnumBody - Parse a {} enclosed enumerator-list.
748/// enumerator-list:
749/// enumerator
750/// enumerator-list ',' enumerator
751/// enumerator:
752/// enumeration-constant
753/// enumeration-constant '=' constant-expression
754/// enumeration-constant:
755/// identifier
756///
757void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
758 SourceLocation LBraceLoc = ConsumeBrace();
759
760 if (Tok.getKind() == tok::r_brace)
761 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
762
763 SmallVector<DeclTy*, 32> EnumConstantDecls;
764
765 // Parse the enumerator-list.
766 while (Tok.getKind() == tok::identifier) {
767 IdentifierInfo *Ident = Tok.getIdentifierInfo();
768 SourceLocation IdentLoc = ConsumeToken();
769
770 SourceLocation EqualLoc;
771 ExprTy *AssignedVal = 0;
772 if (Tok.getKind() == tok::equal) {
773 EqualLoc = ConsumeToken();
774 ExprResult Res = ParseConstantExpression();
775 if (Res.isInvalid)
Chris Lattnerda6c2ce2007-04-27 19:13:15 +0000776 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000777 else
778 AssignedVal = Res.Val;
779 }
780
781 // Install the enumerator constant into EnumDecl.
782 DeclTy *ConstDecl = Actions.ParseEnumConstant(CurScope, EnumDecl,
783 IdentLoc, Ident,
784 EqualLoc, AssignedVal);
785 EnumConstantDecls.push_back(ConstDecl);
786
787 if (Tok.getKind() != tok::comma)
788 break;
789 SourceLocation CommaLoc = ConsumeToken();
790
791 if (Tok.getKind() != tok::identifier && !getLang().C99)
792 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
793 }
794
795 // Eat the }.
796 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
797
798 Actions.ParseEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
799 EnumConstantDecls.size());
800
Steve Naroff0f2fe172007-06-01 17:11:19 +0000801 DeclTy *AttrList = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000802 // If attributes exist after the identifier list, parse them.
803 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000804 AttrList = ParseAttributes(); // FIXME: where do they do?
Chris Lattnerc1915e22007-01-25 07:29:02 +0000805}
Chris Lattner3b561a32006-08-13 00:12:11 +0000806
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000807/// isTypeSpecifierQualifier - Return true if the current token could be the
808/// start of a specifier-qualifier-list.
809bool Parser::isTypeSpecifierQualifier() const {
810 switch (Tok.getKind()) {
811 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000812 // GNU attributes support.
813 case tok::kw___attribute:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000814 // type-specifiers
815 case tok::kw_short:
816 case tok::kw_long:
817 case tok::kw_signed:
818 case tok::kw_unsigned:
819 case tok::kw__Complex:
820 case tok::kw__Imaginary:
821 case tok::kw_void:
822 case tok::kw_char:
823 case tok::kw_int:
824 case tok::kw_float:
825 case tok::kw_double:
826 case tok::kw__Bool:
827 case tok::kw__Decimal32:
828 case tok::kw__Decimal64:
829 case tok::kw__Decimal128:
830
831 // struct-or-union-specifier
832 case tok::kw_struct:
833 case tok::kw_union:
834 // enum-specifier
835 case tok::kw_enum:
836
837 // type-qualifier
838 case tok::kw_const:
839 case tok::kw_volatile:
840 case tok::kw_restrict:
841 return true;
842
843 // typedef-name
844 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000845 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000846
847 // TODO: Attributes.
848 }
849}
850
Chris Lattneracd58a32006-08-06 17:24:14 +0000851/// isDeclarationSpecifier() - Return true if the current token is part of a
852/// declaration specifier.
853bool Parser::isDeclarationSpecifier() const {
854 switch (Tok.getKind()) {
855 default: return false;
856 // storage-class-specifier
857 case tok::kw_typedef:
858 case tok::kw_extern:
859 case tok::kw_static:
860 case tok::kw_auto:
861 case tok::kw_register:
862 case tok::kw___thread:
863
864 // type-specifiers
865 case tok::kw_short:
866 case tok::kw_long:
867 case tok::kw_signed:
868 case tok::kw_unsigned:
869 case tok::kw__Complex:
870 case tok::kw__Imaginary:
871 case tok::kw_void:
872 case tok::kw_char:
873 case tok::kw_int:
874 case tok::kw_float:
875 case tok::kw_double:
876 case tok::kw__Bool:
877 case tok::kw__Decimal32:
878 case tok::kw__Decimal64:
879 case tok::kw__Decimal128:
880
881 // struct-or-union-specifier
882 case tok::kw_struct:
883 case tok::kw_union:
884 // enum-specifier
885 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000886
Chris Lattneracd58a32006-08-06 17:24:14 +0000887 // type-qualifier
888 case tok::kw_const:
889 case tok::kw_volatile:
890 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000891
Chris Lattneracd58a32006-08-06 17:24:14 +0000892 // function-specifier
893 case tok::kw_inline:
894 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000895
Chris Lattneracd58a32006-08-06 17:24:14 +0000896 // typedef-name
897 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000898 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +0000899 // TODO: Attributes.
900 }
901}
902
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000903
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000904/// ParseTypeQualifierListOpt
905/// type-qualifier-list: [C99 6.7.5]
906/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000907/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000908/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000909/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000910///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000911void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000912 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000913 int isInvalid = false;
914 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +0000915 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000916
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000917 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000918 default:
Chris Lattnere37e2332006-08-15 04:50:22 +0000919 // If this is not a type-qualifier token, we're done reading type
920 // qualifiers. First verify that DeclSpec's are consistent.
Chris Lattnerb20e8942006-11-28 05:30:29 +0000921 DS.Finish(Diags, getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000922 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000923 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000924 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
925 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000926 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000927 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000928 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
929 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000930 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000931 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000932 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
933 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000934 break;
Chris Lattnere37e2332006-08-15 04:50:22 +0000935 case tok::kw___attribute:
Steve Naroffb8371e12007-06-09 03:39:29 +0000936 DS.AddAttribute(ParseAttributes());
Steve Naroff98d153c2007-06-06 23:19:11 +0000937 continue; // do *not* consume the next token!
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000938 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000939
940 // If the specifier combination wasn't legal, issue a diagnostic.
941 if (isInvalid) {
942 assert(PrevSpec && "Method did not return previous specifier!");
943 if (isInvalid == 1) // Error.
944 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
945 else // extwarn.
946 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
947 }
948 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000949 }
950}
951
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000952
953/// ParseDeclarator - Parse and verify a newly-initialized declarator.
954///
955void Parser::ParseDeclarator(Declarator &D) {
956 /// This implements the 'declarator' production in the C grammar, then checks
957 /// for well-formedness and issues diagnostics.
958 ParseDeclaratorInternal(D);
959
Chris Lattner9fab3b92006-08-12 18:25:42 +0000960 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000961
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000962}
963
964/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000965/// declarator: [C99 6.7.5]
966/// pointer[opt] direct-declarator
Bill Wendling93efb222007-06-02 23:28:54 +0000967/// [C++] '&' declarator [C++ 8p4, dcl.decl]
968/// [GNU] '&' restrict[opt] attributes[opt] declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +0000969///
970/// pointer: [C99 6.7.5]
971/// '*' type-qualifier-list[opt]
972/// '*' type-qualifier-list[opt] pointer
973///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000974void Parser::ParseDeclaratorInternal(Declarator &D) {
Bill Wendling3708c182007-05-27 10:15:43 +0000975 tok::TokenKind Kind = Tok.getKind();
976
977 // Not a pointer or C++ reference.
978 if (Kind != tok::star && !(Kind == tok::amp && getLang().CPlusPlus))
Chris Lattner6c7416c2006-08-07 00:19:33 +0000979 return ParseDirectDeclarator(D);
980
Bill Wendling3708c182007-05-27 10:15:43 +0000981 // Otherwise, '*' -> pointer or '&' -> reference.
982 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
983
984 if (Kind == tok::star) {
985 // Is a pointer
986 DeclSpec DS;
Steve Naroff98d153c2007-06-06 23:19:11 +0000987
Bill Wendling3708c182007-05-27 10:15:43 +0000988 ParseTypeQualifierListOpt(DS);
Chris Lattner6c7416c2006-08-07 00:19:33 +0000989
Bill Wendling3708c182007-05-27 10:15:43 +0000990 // Recursively parse the declarator.
991 ParseDeclaratorInternal(D);
Chris Lattner9dfdb3c2006-11-13 07:38:09 +0000992
Bill Wendling3708c182007-05-27 10:15:43 +0000993 // Remember that we parsed a pointer type, and remember the type-quals.
994 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc));
995 } else {
996 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +0000997 DeclSpec DS;
998
999 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1000 // cv-qualifiers are introduced through the use of a typedef or of a
1001 // template type argument, in which case the cv-qualifiers are ignored.
1002 //
1003 // [GNU] Retricted references are allowed.
1004 // [GNU] Attributes on references are allowed.
1005 ParseTypeQualifierListOpt(DS);
1006
1007 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1008 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1009 Diag(DS.getConstSpecLoc(),
1010 diag::err_invalid_reference_qualifier_application,
1011 "const");
1012 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1013 Diag(DS.getVolatileSpecLoc(),
1014 diag::err_invalid_reference_qualifier_application,
1015 "volatile");
1016 }
Bill Wendling3708c182007-05-27 10:15:43 +00001017
1018 // Recursively parse the declarator.
1019 ParseDeclaratorInternal(D);
1020
1021 // Remember that we parsed a reference type. It doesn't have type-quals.
Bill Wendling93efb222007-06-02 23:28:54 +00001022 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc));
Bill Wendling3708c182007-05-27 10:15:43 +00001023 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001024}
1025
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001026/// ParseDirectDeclarator
1027/// direct-declarator: [C99 6.7.5]
1028/// identifier
1029/// '(' declarator ')'
1030/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00001031/// [C90] direct-declarator '[' constant-expression[opt] ']'
1032/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1033/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1034/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1035/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001036/// direct-declarator '(' parameter-type-list ')'
1037/// direct-declarator '(' identifier-list[opt] ')'
1038/// [GNU] direct-declarator '(' parameter-forward-declarations
1039/// parameter-type-list[opt] ')'
1040///
Chris Lattneracd58a32006-08-06 17:24:14 +00001041void Parser::ParseDirectDeclarator(Declarator &D) {
1042 // Parse the first direct-declarator seen.
1043 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
1044 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1045 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1046 ConsumeToken();
1047 } else if (Tok.getKind() == tok::l_paren) {
1048 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001049 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001050 // Example: 'char (*X)' or 'int (*XX)(void)'
1051 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001052 } else if (D.mayOmitIdentifier()) {
1053 // This could be something simple like "int" (in which case the declarator
1054 // portion is empty), if an abstract-declarator is allowed.
1055 D.SetIdentifier(0, Tok.getLocation());
1056 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +00001057 // Expected identifier or '('.
1058 Diag(Tok, diag::err_expected_ident_lparen);
1059 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +00001060 }
1061
1062 assert(D.isPastIdentifier() &&
1063 "Haven't past the location of the identifier yet?");
1064
1065 while (1) {
1066 if (Tok.getKind() == tok::l_paren) {
1067 ParseParenDeclarator(D);
1068 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001069 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001070 } else {
1071 break;
1072 }
1073 }
1074}
1075
1076/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
1077/// either be before the identifier (in which case these are just grouping
1078/// parens for precedence) or it may be after the identifier, in which case
1079/// these are function arguments.
1080///
1081/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001082/// parameter-type-list: [C99 6.7.5]
1083/// parameter-list
1084/// parameter-list ',' '...'
1085///
1086/// parameter-list: [C99 6.7.5]
1087/// parameter-declaration
1088/// parameter-list ',' parameter-declaration
1089///
1090/// parameter-declaration: [C99 6.7.5]
1091/// declaration-specifiers declarator
Chris Lattnere37e2332006-08-15 04:50:22 +00001092/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001093/// declaration-specifiers abstract-declarator[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001094/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001095///
1096/// identifier-list: [C99 6.7.5]
1097/// identifier
1098/// identifier-list ',' identifier
1099///
Chris Lattneracd58a32006-08-06 17:24:14 +00001100void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001101 SourceLocation StartLoc = ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001102
Chris Lattneracd58a32006-08-06 17:24:14 +00001103 // If we haven't past the identifier yet (or where the identifier would be
1104 // stored, if this is an abstract declarator), then this is probably just
1105 // grouping parens.
1106 if (!D.isPastIdentifier()) {
1107 // Okay, this is probably a grouping paren. However, if this could be an
1108 // abstract-declarator, then this could also be the start of function
1109 // arguments (consider 'void()').
1110 bool isGrouping;
1111
1112 if (!D.mayOmitIdentifier()) {
1113 // If this can't be an abstract-declarator, this *must* be a grouping
1114 // paren, because we haven't seen the identifier yet.
1115 isGrouping = true;
1116 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
1117 isDeclarationSpecifier()) { // 'int(int)' is a function.
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001118 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1119 // considered to be a type, not a K&R identifier-list.
Chris Lattneracd58a32006-08-06 17:24:14 +00001120 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001121 } else {
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001122 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +00001123 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001124 }
Chris Lattneracd58a32006-08-06 17:24:14 +00001125
1126 // If this is a grouping paren, handle:
1127 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001128 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001129 if (isGrouping) {
Chris Lattnere37e2332006-08-15 04:50:22 +00001130 if (Tok.getKind() == tok::kw___attribute)
Steve Naroffb8371e12007-06-09 03:39:29 +00001131 D.AddAttribute(ParseAttributes());
Chris Lattnere37e2332006-08-15 04:50:22 +00001132
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001133 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +00001134 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +00001135 MatchRHSPunctuation(tok::r_paren, StartLoc);
Chris Lattneracd58a32006-08-06 17:24:14 +00001136 return;
1137 }
1138
1139 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +00001140 // argument list. Recognize that this declarator will never have an
1141 // identifier (and remember where it would have been), then fall through to
1142 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +00001143 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001144 }
1145
Chris Lattneracd58a32006-08-06 17:24:14 +00001146 // Okay, this is the parameter list of a function definition, or it is an
1147 // identifier list of a K&R-style function.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001148 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +00001149 bool HasPrototype;
Chris Lattner14776b92006-08-06 22:27:40 +00001150 bool ErrorEmitted = false;
1151
Chris Lattneredc9e392006-12-02 06:21:46 +00001152 // Build up an array of information about the parsed arguments.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001153 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattnerad9ac942007-01-23 01:14:52 +00001154 SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Chris Lattneredc9e392006-12-02 06:21:46 +00001155
Chris Lattneracd58a32006-08-06 17:24:14 +00001156 if (Tok.getKind() == tok::r_paren) {
1157 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001158 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001159 HasPrototype = false;
1160 } else if (Tok.getKind() == tok::identifier &&
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001161 // K&R identifier lists can't have typedefs as identifiers, per
1162 // C99 6.7.5.3p11.
Steve Naroffb419d3a2006-10-27 23:18:49 +00001163 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001164 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1165 // normal declarators, not for abstract-declarators.
1166 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
1167
1168 // If there was no identifier specified, either we are in an
1169 // abstract-declarator, or we are in a parameter declarator which was found
1170 // to be abstract. In abstract-declarators, identifier lists are not valid,
1171 // diagnose this.
1172 if (!D.getIdentifier())
1173 Diag(Tok, diag::ext_ident_list_in_param);
Chris Lattneredc9e392006-12-02 06:21:46 +00001174
Chris Lattnercbc426d2006-12-02 06:43:02 +00001175 // Remember this identifier in ParamInfo.
1176 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1177 Tok.getLocation(), 0));
1178
Chris Lattneracd58a32006-08-06 17:24:14 +00001179 ConsumeToken();
1180 while (Tok.getKind() == tok::comma) {
1181 // Eat the comma.
1182 ConsumeToken();
1183
Chris Lattnercbc426d2006-12-02 06:43:02 +00001184 if (Tok.getKind() != tok::identifier) {
1185 Diag(Tok, diag::err_expected_ident);
Chris Lattner14776b92006-08-06 22:27:40 +00001186 ErrorEmitted = true;
1187 break;
1188 }
Chris Lattnercbc426d2006-12-02 06:43:02 +00001189
Chris Lattner969ca152006-12-03 06:29:03 +00001190 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
1191
1192 // Verify that the argument identifier has not already been mentioned.
Chris Lattnerbaf33662007-01-27 02:14:08 +00001193 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerad9ac942007-01-23 01:14:52 +00001194 Diag(Tok.getLocation(), diag::err_param_redefinition,ParmII->getName());
1195 ParmII = 0;
1196 }
Chris Lattner969ca152006-12-03 06:29:03 +00001197
Chris Lattnercbc426d2006-12-02 06:43:02 +00001198 // Remember this identifier in ParamInfo.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001199 if (ParmII)
1200 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1201 Tok.getLocation(), 0));
Chris Lattnercbc426d2006-12-02 06:43:02 +00001202
1203 // Eat the identifier.
1204 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00001205 }
1206
Chris Lattneracd58a32006-08-06 17:24:14 +00001207 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001208 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001209 HasPrototype = false;
1210 } else {
Chris Lattner43e956c2006-11-28 04:05:37 +00001211 // Finally, a normal, non-empty parameter type list.
1212
Chris Lattnercbc426d2006-12-02 06:43:02 +00001213 // Enter function-declaration scope, limiting any declarators for struct
1214 // tags to the function prototype scope.
1215 // FIXME: is this needed?
Chris Lattner43e956c2006-11-28 04:05:37 +00001216 EnterScope(0);
1217
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001218 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001219 while (1) {
1220 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001221 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +00001222
1223 // Check to see if this is "void(...)" which is not allowed.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001224 if (ParamInfo.empty()) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001225 // Otherwise, parse parameter type list. If it starts with an
1226 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +00001227 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001228 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +00001229 }
1230
1231 // Consume the ellipsis.
1232 ConsumeToken();
1233 break;
1234 }
1235
Chris Lattneracd58a32006-08-06 17:24:14 +00001236 // Parse the declaration-specifiers.
1237 DeclSpec DS;
1238 ParseDeclarationSpecifiers(DS);
1239
1240 // Parse the declarator. This is "PrototypeContext", because we must
1241 // accept either 'declarator' or 'abstract-declarator' here.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001242 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1243 ParseDeclarator(ParmDecl);
Chris Lattneracd58a32006-08-06 17:24:14 +00001244
Chris Lattnere37e2332006-08-15 04:50:22 +00001245 // Parse GNU attributes, if present.
1246 if (Tok.getKind() == tok::kw___attribute)
Steve Naroffb8371e12007-06-09 03:39:29 +00001247 ParmDecl.AddAttribute(ParseAttributes());
Chris Lattnere37e2332006-08-15 04:50:22 +00001248
Chris Lattner43e956c2006-11-28 04:05:37 +00001249 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001250 // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted.
1251 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1252 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
Chris Lattner4d8f8732006-11-28 05:05:08 +00001253 Diag(DS.getStorageClassSpecLoc(),
Chris Lattner43e956c2006-11-28 04:05:37 +00001254 diag::err_invalid_storage_class_in_func_decl);
Chris Lattner353f5742006-11-28 04:50:12 +00001255 DS.ClearStorageClassSpecs();
Chris Lattner43e956c2006-11-28 04:05:37 +00001256 }
Chris Lattner4d8f8732006-11-28 05:05:08 +00001257 if (DS.isThreadSpecified()) {
1258 Diag(DS.getThreadSpecLoc(),
1259 diag::err_invalid_storage_class_in_func_decl);
1260 DS.ClearStorageClassSpecs();
1261 }
Chris Lattner43e956c2006-11-28 04:05:37 +00001262
1263 // Inform the actions module about the parameter declarator, so it gets
1264 // added to the current scope.
Chris Lattner216d8652006-12-02 06:47:41 +00001265 Action::TypeResult ParamTy =
1266 Actions.ParseParamDeclaratorType(CurScope, ParmDecl);
Chris Lattnercbc426d2006-12-02 06:43:02 +00001267
1268 // Remember this parsed parameter in ParamInfo.
Chris Lattner969ca152006-12-03 06:29:03 +00001269 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1270
1271 // Verify that the argument identifier has not already been mentioned.
Chris Lattnerbaf33662007-01-27 02:14:08 +00001272 if (ParmII && !ParamsSoFar.insert(ParmII)) {
Chris Lattnerad9ac942007-01-23 01:14:52 +00001273 Diag(ParmDecl.getIdentifierLoc(), diag::err_param_redefinition,
1274 ParmII->getName());
1275 ParmII = 0;
Chris Lattner969ca152006-12-03 06:29:03 +00001276 }
1277
1278 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnercbc426d2006-12-02 06:43:02 +00001279 ParmDecl.getIdentifierLoc(),
1280 ParamTy.Val));
Chris Lattneracd58a32006-08-06 17:24:14 +00001281
1282 // If the next token is a comma, consume it and keep reading arguments.
1283 if (Tok.getKind() != tok::comma) break;
1284
1285 // Consume the comma.
1286 ConsumeToken();
1287 }
1288
1289 HasPrototype = true;
Chris Lattner43e956c2006-11-28 04:05:37 +00001290
1291 // Leave prototype scope.
1292 ExitScope();
Chris Lattneracd58a32006-08-06 17:24:14 +00001293 }
1294
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001295 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerd2e97c12006-12-03 02:03:33 +00001296 if (!ErrorEmitted)
1297 D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic,
1298 &ParamInfo[0], ParamInfo.size(),
1299 StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001300
Chris Lattner14776b92006-08-06 22:27:40 +00001301 // If we have the closing ')', eat it and we're done.
1302 if (Tok.getKind() == tok::r_paren) {
1303 ConsumeParen();
1304 } else {
1305 // If an error happened earlier parsing something else in the proto, don't
1306 // issue another error.
1307 if (!ErrorEmitted)
1308 Diag(Tok, diag::err_expected_rparen);
1309 SkipUntil(tok::r_paren);
1310 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001311}
Chris Lattneracd58a32006-08-06 17:24:14 +00001312
Chris Lattnere8074e62006-08-06 18:30:15 +00001313
1314/// [C90] direct-declarator '[' constant-expression[opt] ']'
1315/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1316/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1317/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1318/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1319void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001320 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001321
1322 // If valid, this location is the position where we read the 'static' keyword.
1323 SourceLocation StaticLoc;
Chris Lattneraf635312006-10-16 06:06:51 +00001324 if (Tok.getKind() == tok::kw_static)
1325 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001326
1327 // If there is a type-qualifier-list, read it now.
1328 DeclSpec DS;
1329 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001330
1331 // If we haven't already read 'static', check to see if there is one after the
1332 // type-qualifier-list.
Chris Lattneraf635312006-10-16 06:06:51 +00001333 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static)
1334 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001335
1336 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001337 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001338 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +00001339 if (Tok.getKind() == tok::star) {
1340 // Remember the '*' token, in case we have to un-get it.
1341 LexerToken StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +00001342 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +00001343
1344 // Check that the ']' token is present to avoid incorrectly parsing
1345 // expressions starting with '*' as [*].
1346 if (Tok.getKind() == tok::r_square) {
1347 if (StaticLoc.isValid())
1348 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1349 StaticLoc = SourceLocation(); // Drop the static.
1350 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +00001351 } else {
1352 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +00001353 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +00001354 // need to reparse it. This handles cases like 'X[*p + 4]'
1355 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +00001356 }
Chris Lattner9fab3b92006-08-12 18:25:42 +00001357 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001358 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001359 NumElements = ParseAssignmentExpression();
1360 }
1361
1362 // If there was an error parsing the assignment-expression, recover.
1363 if (NumElements.isInvalid) {
1364 // If the expression was invalid, skip it.
1365 SkipUntil(tok::r_square);
1366 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001367 }
1368
Chris Lattner04f80192006-08-15 04:55:54 +00001369 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001370
Chris Lattnere8074e62006-08-06 18:30:15 +00001371 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1372 // it was not a constant expression.
1373 if (!getLang().C99) {
1374 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001375 if (isStar || StaticLoc.isValid() ||
1376 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001377 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001378 }
Bill Wendling93efb222007-06-02 23:28:54 +00001379
Chris Lattner6c7416c2006-08-07 00:19:33 +00001380 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001381 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1382 StaticLoc.isValid(), isStar,
1383 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001384}
1385