blob: 4ad8f5878f8074ed4229a284435a360ce7050340 [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 Naroff0f05a7a2007-06-09 23:38:17 +0000247 D.AddAttributes(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 Naroff0f05a7a2007-06-09 23:38:17 +0000400 DS.AddAttributes(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
Chris Lattner736ed5d2007-06-09 05:59:07 +0000600/// [GNU] "struct-declaration-list" without terminatoring ';'
Chris Lattner90a26b02007-01-23 04:38:16 +0000601/// 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 ';'
Chris Lattner736ed5d2007-06-09 05:59:07 +0000607/// [GNU] __extension__ struct-declaration
608/// [GNU] specifier-qualifier-list ';'
Chris Lattner90a26b02007-01-23 04:38:16 +0000609/// 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 Lattner736ed5d2007-06-09 05:59:07 +0000636 // Check for extraneous top-level semicolon.
Chris Lattner36e46a22007-06-09 05:49:55 +0000637 if (Tok.getKind() == tok::semi) {
638 Diag(Tok, diag::ext_extra_struct_semi);
639 ConsumeToken();
640 continue;
641 }
Chris Lattner736ed5d2007-06-09 05:59:07 +0000642
643 // FIXME: When __extension__ is specified, disable extension diagnostics.
644 if (Tok.getKind() == tok::kw___extension__)
645 ConsumeToken();
Chris Lattner36e46a22007-06-09 05:49:55 +0000646
Chris Lattner90a26b02007-01-23 04:38:16 +0000647 // Parse the common specifier-qualifiers-list piece.
648 DeclSpec DS;
649 SourceLocation SpecQualLoc = Tok.getLocation();
650 ParseSpecifierQualifierList(DS);
651 // TODO: Does specifier-qualifier list correctly check that *something* is
652 // specified?
653
Chris Lattner90a26b02007-01-23 04:38:16 +0000654 // If there are no declarators, issue a warning.
655 if (Tok.getKind() == tok::semi) {
656 Diag(SpecQualLoc, diag::w_no_declarators);
Chris Lattner7b9ace62007-01-23 20:11:08 +0000657 ConsumeToken();
658 continue;
659 }
660
661 // Read struct-declarators until we find the semicolon.
662 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
663
664 while (1) {
665 /// struct-declarator: declarator
666 /// struct-declarator: declarator[opt] ':' constant-expression
667 if (Tok.getKind() != tok::colon)
668 ParseDeclarator(DeclaratorInfo);
669
670 ExprTy *BitfieldSize = 0;
671 if (Tok.getKind() == tok::colon) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000672 ConsumeToken();
Chris Lattner7b9ace62007-01-23 20:11:08 +0000673 ExprResult Res = ParseConstantExpression();
674 if (Res.isInvalid) {
675 SkipUntil(tok::semi, true, true);
676 } else {
677 BitfieldSize = Res.Val;
678 }
Chris Lattner90a26b02007-01-23 04:38:16 +0000679 }
Chris Lattner7b9ace62007-01-23 20:11:08 +0000680
681 // If attributes exist after the declarator, parse them.
682 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000683 DeclaratorInfo.AddAttributes(ParseAttributes());
Chris Lattner7b9ace62007-01-23 20:11:08 +0000684
Chris Lattner367b0192007-01-23 22:29:13 +0000685 // Install the declarator into the current TagDecl.
Chris Lattner1300fb92007-01-23 23:42:53 +0000686 DeclTy *Field = Actions.ParseField(CurScope, TagDecl, SpecQualLoc,
687 DeclaratorInfo, BitfieldSize);
688 FieldDecls.push_back(Field);
Chris Lattner7b9ace62007-01-23 20:11:08 +0000689
690 // If we don't have a comma, it is either the end of the list (a ';')
691 // or an error, bail out.
692 if (Tok.getKind() != tok::comma)
693 break;
694
695 // Consume the comma.
696 ConsumeToken();
697
698 // Parse the next declarator.
699 DeclaratorInfo.clear();
700
701 // Attributes are only allowed on the second declarator.
702 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000703 DeclaratorInfo.AddAttributes(ParseAttributes());
Chris Lattner90a26b02007-01-23 04:38:16 +0000704 }
705
706 if (Tok.getKind() == tok::semi) {
707 ConsumeToken();
Chris Lattner0c7e82d2007-06-09 05:54:40 +0000708 } else if (Tok.getKind() == tok::r_brace) {
709 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
710 break;
Chris Lattner90a26b02007-01-23 04:38:16 +0000711 } else {
712 Diag(Tok, diag::err_expected_semi_decl_list);
713 // Skip to end of block or statement
714 SkipUntil(tok::r_brace, true, true);
715 }
716 }
717
718 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
719
Chris Lattnerc1915e22007-01-25 07:29:02 +0000720 Actions.ParseRecordBody(RecordLoc, TagDecl, &FieldDecls[0],FieldDecls.size());
721
Steve Naroffb8371e12007-06-09 03:39:29 +0000722 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +0000723 // If attributes exist after struct contents, parse them.
724 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000725 AttrList = ParseAttributes(); // FIXME: where should I put them?
Chris Lattner90a26b02007-01-23 04:38:16 +0000726}
727
728
Chris Lattner3b561a32006-08-13 00:12:11 +0000729/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000730/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000731/// 'enum' identifier[opt] '{' enumerator-list '}'
732/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000733/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
734/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000735/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000736/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000737void Parser::ParseEnumSpecifier(DeclSpec &DS) {
738 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattnerb20e8942006-11-28 05:30:29 +0000739 SourceLocation StartLoc = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000740
Chris Lattnerffbc2712007-01-25 06:05:38 +0000741 // Parse the tag portion of this.
742 DeclTy *TagDecl;
743 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
Chris Lattner3b561a32006-08-13 00:12:11 +0000744 return;
Chris Lattner3b561a32006-08-13 00:12:11 +0000745
Chris Lattnerc1915e22007-01-25 07:29:02 +0000746 if (Tok.getKind() == tok::l_brace)
747 ParseEnumBody(StartLoc, TagDecl);
748
Chris Lattner3b561a32006-08-13 00:12:11 +0000749 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000750 const char *PrevSpec = 0;
Chris Lattnerffbc2712007-01-25 06:05:38 +0000751 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000752 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000753}
754
Chris Lattnerc1915e22007-01-25 07:29:02 +0000755/// ParseEnumBody - Parse a {} enclosed enumerator-list.
756/// enumerator-list:
757/// enumerator
758/// enumerator-list ',' enumerator
759/// enumerator:
760/// enumeration-constant
761/// enumeration-constant '=' constant-expression
762/// enumeration-constant:
763/// identifier
764///
765void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
766 SourceLocation LBraceLoc = ConsumeBrace();
767
768 if (Tok.getKind() == tok::r_brace)
769 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
770
771 SmallVector<DeclTy*, 32> EnumConstantDecls;
772
773 // Parse the enumerator-list.
774 while (Tok.getKind() == tok::identifier) {
775 IdentifierInfo *Ident = Tok.getIdentifierInfo();
776 SourceLocation IdentLoc = ConsumeToken();
777
778 SourceLocation EqualLoc;
779 ExprTy *AssignedVal = 0;
780 if (Tok.getKind() == tok::equal) {
781 EqualLoc = ConsumeToken();
782 ExprResult Res = ParseConstantExpression();
783 if (Res.isInvalid)
Chris Lattnerda6c2ce2007-04-27 19:13:15 +0000784 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000785 else
786 AssignedVal = Res.Val;
787 }
788
789 // Install the enumerator constant into EnumDecl.
790 DeclTy *ConstDecl = Actions.ParseEnumConstant(CurScope, EnumDecl,
791 IdentLoc, Ident,
792 EqualLoc, AssignedVal);
793 EnumConstantDecls.push_back(ConstDecl);
794
795 if (Tok.getKind() != tok::comma)
796 break;
797 SourceLocation CommaLoc = ConsumeToken();
798
799 if (Tok.getKind() != tok::identifier && !getLang().C99)
800 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
801 }
802
803 // Eat the }.
804 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
805
806 Actions.ParseEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
807 EnumConstantDecls.size());
808
Steve Naroff0f2fe172007-06-01 17:11:19 +0000809 DeclTy *AttrList = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000810 // If attributes exist after the identifier list, parse them.
811 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000812 AttrList = ParseAttributes(); // FIXME: where do they do?
Chris Lattnerc1915e22007-01-25 07:29:02 +0000813}
Chris Lattner3b561a32006-08-13 00:12:11 +0000814
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000815/// isTypeSpecifierQualifier - Return true if the current token could be the
816/// start of a specifier-qualifier-list.
817bool Parser::isTypeSpecifierQualifier() const {
818 switch (Tok.getKind()) {
819 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000820 // GNU attributes support.
821 case tok::kw___attribute:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000822 // type-specifiers
823 case tok::kw_short:
824 case tok::kw_long:
825 case tok::kw_signed:
826 case tok::kw_unsigned:
827 case tok::kw__Complex:
828 case tok::kw__Imaginary:
829 case tok::kw_void:
830 case tok::kw_char:
831 case tok::kw_int:
832 case tok::kw_float:
833 case tok::kw_double:
834 case tok::kw__Bool:
835 case tok::kw__Decimal32:
836 case tok::kw__Decimal64:
837 case tok::kw__Decimal128:
838
839 // struct-or-union-specifier
840 case tok::kw_struct:
841 case tok::kw_union:
842 // enum-specifier
843 case tok::kw_enum:
844
845 // type-qualifier
846 case tok::kw_const:
847 case tok::kw_volatile:
848 case tok::kw_restrict:
849 return true;
850
851 // typedef-name
852 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000853 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000854
855 // TODO: Attributes.
856 }
857}
858
Chris Lattneracd58a32006-08-06 17:24:14 +0000859/// isDeclarationSpecifier() - Return true if the current token is part of a
860/// declaration specifier.
861bool Parser::isDeclarationSpecifier() const {
862 switch (Tok.getKind()) {
863 default: return false;
864 // storage-class-specifier
865 case tok::kw_typedef:
866 case tok::kw_extern:
867 case tok::kw_static:
868 case tok::kw_auto:
869 case tok::kw_register:
870 case tok::kw___thread:
871
872 // type-specifiers
873 case tok::kw_short:
874 case tok::kw_long:
875 case tok::kw_signed:
876 case tok::kw_unsigned:
877 case tok::kw__Complex:
878 case tok::kw__Imaginary:
879 case tok::kw_void:
880 case tok::kw_char:
881 case tok::kw_int:
882 case tok::kw_float:
883 case tok::kw_double:
884 case tok::kw__Bool:
885 case tok::kw__Decimal32:
886 case tok::kw__Decimal64:
887 case tok::kw__Decimal128:
888
889 // struct-or-union-specifier
890 case tok::kw_struct:
891 case tok::kw_union:
892 // enum-specifier
893 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000894
Chris Lattneracd58a32006-08-06 17:24:14 +0000895 // type-qualifier
896 case tok::kw_const:
897 case tok::kw_volatile:
898 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000899
Chris Lattneracd58a32006-08-06 17:24:14 +0000900 // function-specifier
901 case tok::kw_inline:
902 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000903
Chris Lattneracd58a32006-08-06 17:24:14 +0000904 // typedef-name
905 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000906 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +0000907 // TODO: Attributes.
908 }
909}
910
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000911
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000912/// ParseTypeQualifierListOpt
913/// type-qualifier-list: [C99 6.7.5]
914/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000915/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000916/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000917/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000918///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000919void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000920 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000921 int isInvalid = false;
922 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +0000923 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000924
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000925 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000926 default:
Chris Lattnere37e2332006-08-15 04:50:22 +0000927 // If this is not a type-qualifier token, we're done reading type
928 // qualifiers. First verify that DeclSpec's are consistent.
Chris Lattnerb20e8942006-11-28 05:30:29 +0000929 DS.Finish(Diags, getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000930 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000931 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000932 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
933 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000934 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000935 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000936 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
937 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000938 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000939 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000940 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
941 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000942 break;
Chris Lattnere37e2332006-08-15 04:50:22 +0000943 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000944 DS.AddAttributes(ParseAttributes());
Steve Naroff98d153c2007-06-06 23:19:11 +0000945 continue; // do *not* consume the next token!
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000946 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000947
948 // If the specifier combination wasn't legal, issue a diagnostic.
949 if (isInvalid) {
950 assert(PrevSpec && "Method did not return previous specifier!");
951 if (isInvalid == 1) // Error.
952 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
953 else // extwarn.
954 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
955 }
956 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000957 }
958}
959
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000960
961/// ParseDeclarator - Parse and verify a newly-initialized declarator.
962///
963void Parser::ParseDeclarator(Declarator &D) {
964 /// This implements the 'declarator' production in the C grammar, then checks
965 /// for well-formedness and issues diagnostics.
966 ParseDeclaratorInternal(D);
967
Chris Lattner9fab3b92006-08-12 18:25:42 +0000968 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000969
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000970}
971
972/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000973/// declarator: [C99 6.7.5]
974/// pointer[opt] direct-declarator
Bill Wendling93efb222007-06-02 23:28:54 +0000975/// [C++] '&' declarator [C++ 8p4, dcl.decl]
976/// [GNU] '&' restrict[opt] attributes[opt] declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +0000977///
978/// pointer: [C99 6.7.5]
979/// '*' type-qualifier-list[opt]
980/// '*' type-qualifier-list[opt] pointer
981///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000982void Parser::ParseDeclaratorInternal(Declarator &D) {
Bill Wendling3708c182007-05-27 10:15:43 +0000983 tok::TokenKind Kind = Tok.getKind();
984
985 // Not a pointer or C++ reference.
986 if (Kind != tok::star && !(Kind == tok::amp && getLang().CPlusPlus))
Chris Lattner6c7416c2006-08-07 00:19:33 +0000987 return ParseDirectDeclarator(D);
988
Bill Wendling3708c182007-05-27 10:15:43 +0000989 // Otherwise, '*' -> pointer or '&' -> reference.
990 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
991
992 if (Kind == tok::star) {
993 // Is a pointer
994 DeclSpec DS;
Steve Naroff98d153c2007-06-06 23:19:11 +0000995
Bill Wendling3708c182007-05-27 10:15:43 +0000996 ParseTypeQualifierListOpt(DS);
Chris Lattner6c7416c2006-08-07 00:19:33 +0000997
Bill Wendling3708c182007-05-27 10:15:43 +0000998 // Recursively parse the declarator.
999 ParseDeclaratorInternal(D);
Chris Lattner9dfdb3c2006-11-13 07:38:09 +00001000
Bill Wendling3708c182007-05-27 10:15:43 +00001001 // Remember that we parsed a pointer type, and remember the type-quals.
1002 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc));
1003 } else {
1004 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00001005 DeclSpec DS;
1006
1007 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1008 // cv-qualifiers are introduced through the use of a typedef or of a
1009 // template type argument, in which case the cv-qualifiers are ignored.
1010 //
1011 // [GNU] Retricted references are allowed.
1012 // [GNU] Attributes on references are allowed.
1013 ParseTypeQualifierListOpt(DS);
1014
1015 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1016 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1017 Diag(DS.getConstSpecLoc(),
1018 diag::err_invalid_reference_qualifier_application,
1019 "const");
1020 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1021 Diag(DS.getVolatileSpecLoc(),
1022 diag::err_invalid_reference_qualifier_application,
1023 "volatile");
1024 }
Bill Wendling3708c182007-05-27 10:15:43 +00001025
1026 // Recursively parse the declarator.
1027 ParseDeclaratorInternal(D);
1028
1029 // Remember that we parsed a reference type. It doesn't have type-quals.
Bill Wendling93efb222007-06-02 23:28:54 +00001030 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc));
Bill Wendling3708c182007-05-27 10:15:43 +00001031 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001032}
1033
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001034/// ParseDirectDeclarator
1035/// direct-declarator: [C99 6.7.5]
1036/// identifier
1037/// '(' declarator ')'
1038/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00001039/// [C90] direct-declarator '[' constant-expression[opt] ']'
1040/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1041/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1042/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1043/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001044/// direct-declarator '(' parameter-type-list ')'
1045/// direct-declarator '(' identifier-list[opt] ')'
1046/// [GNU] direct-declarator '(' parameter-forward-declarations
1047/// parameter-type-list[opt] ')'
1048///
Chris Lattneracd58a32006-08-06 17:24:14 +00001049void Parser::ParseDirectDeclarator(Declarator &D) {
1050 // Parse the first direct-declarator seen.
1051 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
1052 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1053 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1054 ConsumeToken();
1055 } else if (Tok.getKind() == tok::l_paren) {
1056 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001057 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001058 // Example: 'char (*X)' or 'int (*XX)(void)'
1059 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001060 } else if (D.mayOmitIdentifier()) {
1061 // This could be something simple like "int" (in which case the declarator
1062 // portion is empty), if an abstract-declarator is allowed.
1063 D.SetIdentifier(0, Tok.getLocation());
1064 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +00001065 // Expected identifier or '('.
1066 Diag(Tok, diag::err_expected_ident_lparen);
1067 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +00001068 }
1069
1070 assert(D.isPastIdentifier() &&
1071 "Haven't past the location of the identifier yet?");
1072
1073 while (1) {
1074 if (Tok.getKind() == tok::l_paren) {
1075 ParseParenDeclarator(D);
1076 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001077 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001078 } else {
1079 break;
1080 }
1081 }
1082}
1083
1084/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
1085/// either be before the identifier (in which case these are just grouping
1086/// parens for precedence) or it may be after the identifier, in which case
1087/// these are function arguments.
1088///
1089/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001090/// parameter-type-list: [C99 6.7.5]
1091/// parameter-list
1092/// parameter-list ',' '...'
1093///
1094/// parameter-list: [C99 6.7.5]
1095/// parameter-declaration
1096/// parameter-list ',' parameter-declaration
1097///
1098/// parameter-declaration: [C99 6.7.5]
1099/// declaration-specifiers declarator
Chris Lattnere37e2332006-08-15 04:50:22 +00001100/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001101/// declaration-specifiers abstract-declarator[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001102/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001103///
1104/// identifier-list: [C99 6.7.5]
1105/// identifier
1106/// identifier-list ',' identifier
1107///
Chris Lattneracd58a32006-08-06 17:24:14 +00001108void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001109 SourceLocation StartLoc = ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001110
Chris Lattneracd58a32006-08-06 17:24:14 +00001111 // If we haven't past the identifier yet (or where the identifier would be
1112 // stored, if this is an abstract declarator), then this is probably just
1113 // grouping parens.
1114 if (!D.isPastIdentifier()) {
1115 // Okay, this is probably a grouping paren. However, if this could be an
1116 // abstract-declarator, then this could also be the start of function
1117 // arguments (consider 'void()').
1118 bool isGrouping;
1119
1120 if (!D.mayOmitIdentifier()) {
1121 // If this can't be an abstract-declarator, this *must* be a grouping
1122 // paren, because we haven't seen the identifier yet.
1123 isGrouping = true;
1124 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
1125 isDeclarationSpecifier()) { // 'int(int)' is a function.
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001126 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1127 // considered to be a type, not a K&R identifier-list.
Chris Lattneracd58a32006-08-06 17:24:14 +00001128 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001129 } else {
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001130 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +00001131 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001132 }
Chris Lattneracd58a32006-08-06 17:24:14 +00001133
1134 // If this is a grouping paren, handle:
1135 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001136 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001137 if (isGrouping) {
Chris Lattnere37e2332006-08-15 04:50:22 +00001138 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f05a7a2007-06-09 23:38:17 +00001139 D.AddAttributes(ParseAttributes());
Chris Lattnere37e2332006-08-15 04:50:22 +00001140
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001141 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +00001142 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +00001143 MatchRHSPunctuation(tok::r_paren, StartLoc);
Chris Lattneracd58a32006-08-06 17:24:14 +00001144 return;
1145 }
1146
1147 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +00001148 // argument list. Recognize that this declarator will never have an
1149 // identifier (and remember where it would have been), then fall through to
1150 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +00001151 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001152 }
1153
Chris Lattneracd58a32006-08-06 17:24:14 +00001154 // Okay, this is the parameter list of a function definition, or it is an
1155 // identifier list of a K&R-style function.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001156 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +00001157 bool HasPrototype;
Chris Lattner14776b92006-08-06 22:27:40 +00001158 bool ErrorEmitted = false;
1159
Chris Lattneredc9e392006-12-02 06:21:46 +00001160 // Build up an array of information about the parsed arguments.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001161 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattnerad9ac942007-01-23 01:14:52 +00001162 SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Chris Lattneredc9e392006-12-02 06:21:46 +00001163
Chris Lattneracd58a32006-08-06 17:24:14 +00001164 if (Tok.getKind() == tok::r_paren) {
1165 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001166 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001167 HasPrototype = false;
1168 } else if (Tok.getKind() == tok::identifier &&
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001169 // K&R identifier lists can't have typedefs as identifiers, per
1170 // C99 6.7.5.3p11.
Steve Naroffb419d3a2006-10-27 23:18:49 +00001171 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001172 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1173 // normal declarators, not for abstract-declarators.
1174 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
1175
1176 // If there was no identifier specified, either we are in an
1177 // abstract-declarator, or we are in a parameter declarator which was found
1178 // to be abstract. In abstract-declarators, identifier lists are not valid,
1179 // diagnose this.
1180 if (!D.getIdentifier())
1181 Diag(Tok, diag::ext_ident_list_in_param);
Chris Lattneredc9e392006-12-02 06:21:46 +00001182
Chris Lattnercbc426d2006-12-02 06:43:02 +00001183 // Remember this identifier in ParamInfo.
1184 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1185 Tok.getLocation(), 0));
1186
Chris Lattneracd58a32006-08-06 17:24:14 +00001187 ConsumeToken();
1188 while (Tok.getKind() == tok::comma) {
1189 // Eat the comma.
1190 ConsumeToken();
1191
Chris Lattnercbc426d2006-12-02 06:43:02 +00001192 if (Tok.getKind() != tok::identifier) {
1193 Diag(Tok, diag::err_expected_ident);
Chris Lattner14776b92006-08-06 22:27:40 +00001194 ErrorEmitted = true;
1195 break;
1196 }
Chris Lattnercbc426d2006-12-02 06:43:02 +00001197
Chris Lattner969ca152006-12-03 06:29:03 +00001198 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
1199
1200 // Verify that the argument identifier has not already been mentioned.
Chris Lattnerbaf33662007-01-27 02:14:08 +00001201 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerad9ac942007-01-23 01:14:52 +00001202 Diag(Tok.getLocation(), diag::err_param_redefinition,ParmII->getName());
1203 ParmII = 0;
1204 }
Chris Lattner969ca152006-12-03 06:29:03 +00001205
Chris Lattnercbc426d2006-12-02 06:43:02 +00001206 // Remember this identifier in ParamInfo.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001207 if (ParmII)
1208 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1209 Tok.getLocation(), 0));
Chris Lattnercbc426d2006-12-02 06:43:02 +00001210
1211 // Eat the identifier.
1212 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00001213 }
1214
Chris Lattneracd58a32006-08-06 17:24:14 +00001215 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001216 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001217 HasPrototype = false;
1218 } else {
Chris Lattner43e956c2006-11-28 04:05:37 +00001219 // Finally, a normal, non-empty parameter type list.
1220
Chris Lattnercbc426d2006-12-02 06:43:02 +00001221 // Enter function-declaration scope, limiting any declarators for struct
1222 // tags to the function prototype scope.
1223 // FIXME: is this needed?
Chris Lattner43e956c2006-11-28 04:05:37 +00001224 EnterScope(0);
1225
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001226 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001227 while (1) {
1228 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001229 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +00001230
1231 // Check to see if this is "void(...)" which is not allowed.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001232 if (ParamInfo.empty()) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001233 // Otherwise, parse parameter type list. If it starts with an
1234 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +00001235 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001236 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +00001237 }
1238
1239 // Consume the ellipsis.
1240 ConsumeToken();
1241 break;
1242 }
1243
Chris Lattneracd58a32006-08-06 17:24:14 +00001244 // Parse the declaration-specifiers.
1245 DeclSpec DS;
1246 ParseDeclarationSpecifiers(DS);
1247
1248 // Parse the declarator. This is "PrototypeContext", because we must
1249 // accept either 'declarator' or 'abstract-declarator' here.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001250 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1251 ParseDeclarator(ParmDecl);
Chris Lattneracd58a32006-08-06 17:24:14 +00001252
Chris Lattnere37e2332006-08-15 04:50:22 +00001253 // Parse GNU attributes, if present.
1254 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f05a7a2007-06-09 23:38:17 +00001255 ParmDecl.AddAttributes(ParseAttributes());
Chris Lattnere37e2332006-08-15 04:50:22 +00001256
Chris Lattner43e956c2006-11-28 04:05:37 +00001257 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001258 // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted.
1259 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1260 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
Chris Lattner4d8f8732006-11-28 05:05:08 +00001261 Diag(DS.getStorageClassSpecLoc(),
Chris Lattner43e956c2006-11-28 04:05:37 +00001262 diag::err_invalid_storage_class_in_func_decl);
Chris Lattner353f5742006-11-28 04:50:12 +00001263 DS.ClearStorageClassSpecs();
Chris Lattner43e956c2006-11-28 04:05:37 +00001264 }
Chris Lattner4d8f8732006-11-28 05:05:08 +00001265 if (DS.isThreadSpecified()) {
1266 Diag(DS.getThreadSpecLoc(),
1267 diag::err_invalid_storage_class_in_func_decl);
1268 DS.ClearStorageClassSpecs();
1269 }
Chris Lattner43e956c2006-11-28 04:05:37 +00001270
1271 // Inform the actions module about the parameter declarator, so it gets
1272 // added to the current scope.
Chris Lattner216d8652006-12-02 06:47:41 +00001273 Action::TypeResult ParamTy =
1274 Actions.ParseParamDeclaratorType(CurScope, ParmDecl);
Chris Lattnercbc426d2006-12-02 06:43:02 +00001275
1276 // Remember this parsed parameter in ParamInfo.
Chris Lattner969ca152006-12-03 06:29:03 +00001277 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1278
1279 // Verify that the argument identifier has not already been mentioned.
Chris Lattnerbaf33662007-01-27 02:14:08 +00001280 if (ParmII && !ParamsSoFar.insert(ParmII)) {
Chris Lattnerad9ac942007-01-23 01:14:52 +00001281 Diag(ParmDecl.getIdentifierLoc(), diag::err_param_redefinition,
1282 ParmII->getName());
1283 ParmII = 0;
Chris Lattner969ca152006-12-03 06:29:03 +00001284 }
1285
1286 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnercbc426d2006-12-02 06:43:02 +00001287 ParmDecl.getIdentifierLoc(),
1288 ParamTy.Val));
Chris Lattneracd58a32006-08-06 17:24:14 +00001289
1290 // If the next token is a comma, consume it and keep reading arguments.
1291 if (Tok.getKind() != tok::comma) break;
1292
1293 // Consume the comma.
1294 ConsumeToken();
1295 }
1296
1297 HasPrototype = true;
Chris Lattner43e956c2006-11-28 04:05:37 +00001298
1299 // Leave prototype scope.
1300 ExitScope();
Chris Lattneracd58a32006-08-06 17:24:14 +00001301 }
1302
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001303 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerd2e97c12006-12-03 02:03:33 +00001304 if (!ErrorEmitted)
1305 D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic,
1306 &ParamInfo[0], ParamInfo.size(),
1307 StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001308
Chris Lattner14776b92006-08-06 22:27:40 +00001309 // If we have the closing ')', eat it and we're done.
1310 if (Tok.getKind() == tok::r_paren) {
1311 ConsumeParen();
1312 } else {
1313 // If an error happened earlier parsing something else in the proto, don't
1314 // issue another error.
1315 if (!ErrorEmitted)
1316 Diag(Tok, diag::err_expected_rparen);
1317 SkipUntil(tok::r_paren);
1318 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001319}
Chris Lattneracd58a32006-08-06 17:24:14 +00001320
Chris Lattnere8074e62006-08-06 18:30:15 +00001321
1322/// [C90] direct-declarator '[' constant-expression[opt] ']'
1323/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1324/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1325/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1326/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1327void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001328 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001329
1330 // If valid, this location is the position where we read the 'static' keyword.
1331 SourceLocation StaticLoc;
Chris Lattneraf635312006-10-16 06:06:51 +00001332 if (Tok.getKind() == tok::kw_static)
1333 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001334
1335 // If there is a type-qualifier-list, read it now.
1336 DeclSpec DS;
1337 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001338
1339 // If we haven't already read 'static', check to see if there is one after the
1340 // type-qualifier-list.
Chris Lattneraf635312006-10-16 06:06:51 +00001341 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static)
1342 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001343
1344 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001345 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001346 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +00001347 if (Tok.getKind() == tok::star) {
1348 // Remember the '*' token, in case we have to un-get it.
1349 LexerToken StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +00001350 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +00001351
1352 // Check that the ']' token is present to avoid incorrectly parsing
1353 // expressions starting with '*' as [*].
1354 if (Tok.getKind() == tok::r_square) {
1355 if (StaticLoc.isValid())
1356 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1357 StaticLoc = SourceLocation(); // Drop the static.
1358 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +00001359 } else {
1360 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +00001361 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +00001362 // need to reparse it. This handles cases like 'X[*p + 4]'
1363 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +00001364 }
Chris Lattner9fab3b92006-08-12 18:25:42 +00001365 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001366 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001367 NumElements = ParseAssignmentExpression();
1368 }
1369
1370 // If there was an error parsing the assignment-expression, recover.
1371 if (NumElements.isInvalid) {
1372 // If the expression was invalid, skip it.
1373 SkipUntil(tok::r_square);
1374 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001375 }
1376
Chris Lattner04f80192006-08-15 04:55:54 +00001377 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001378
Chris Lattnere8074e62006-08-06 18:30:15 +00001379 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1380 // it was not a constant expression.
1381 if (!getLang().C99) {
1382 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001383 if (isStar || StaticLoc.isValid() ||
1384 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001385 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001386 }
Bill Wendling93efb222007-06-02 23:28:54 +00001387
Chris Lattner6c7416c2006-08-07 00:19:33 +00001388 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001389 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1390 StaticLoc.isValid(), isStar,
1391 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001392}
1393