blob: 74f7602f88b40d557409fd493e0edc46d87ab9a6 [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 Naroff0f2fe172007-06-01 17:11:19 +000075Parser::DeclTy *Parser::ParseAttributes() {
76 assert(Tok.getKind() == tok::kw___attribute && "Not an attribute list!");
77
78 DeclTy *CurrAttr = 0;
79
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();
103 SourceLocation LParenLoc, RParenLoc;
104
105 // check if we have a "paramterized" attribute
106 if (Tok.getKind() == tok::l_paren) {
107 LParenLoc = ConsumeParen();
108
109 if (Tok.getKind() == tok::identifier) {
110 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
111 SourceLocation ParmLoc = ConsumeToken();
112
113 if (Tok.getKind() == tok::r_paren) {
114 // __attribute__(( mode(byte) ))
115 RParenLoc = ConsumeParen();
116 CurrAttr = Actions.ParseAttribute(AttrName, AttrNameLoc, CurrAttr,
117 ParmName, ParmLoc, 0, 0, LParenLoc, RParenLoc);
118 } else if (Tok.getKind() == tok::comma) {
119 ConsumeToken();
120 // __attribute__(( format(printf, 1, 2) ))
121 SmallVector<ExprTy*, 8> ArgExprs;
122 bool ArgExprsOk = true;
123
124 // now parse the non-empty comma separated list of expressions
125 while (1) {
126 ExprResult ArgExpr = ParseAssignmentExpression();
127 if (ArgExpr.isInvalid) {
128 ArgExprsOk = false;
129 SkipUntil(tok::r_paren);
130 break;
131 } else {
132 ArgExprs.push_back(ArgExpr.Val);
133 }
134 if (Tok.getKind() != tok::comma)
135 break;
136 ConsumeToken(); // Eat the comma, move to the next argument
137 }
138 if (ArgExprsOk && Tok.getKind() == tok::r_paren) {
139 RParenLoc = ConsumeParen();
140 CurrAttr = Actions.ParseAttribute(AttrName, AttrNameLoc, CurrAttr,
141 ParmName, ParmLoc, &ArgExprs[0], ArgExprs.size(),
142 LParenLoc, RParenLoc);
143 }
144 }
145 } else { // not an identifier
146 // parse a possibly empty comma separated list of expressions
147 if (Tok.getKind() == tok::r_paren) {
148 // __attribute__(( nonnull() ))
149 RParenLoc = ConsumeParen();
150 CurrAttr = Actions.ParseAttribute(AttrName, AttrNameLoc, CurrAttr,
151 0, SourceLocation(), 0, 0, LParenLoc, RParenLoc);
152 } else {
153 // __attribute__(( aligned(16) ))
154 SmallVector<ExprTy*, 8> ArgExprs;
155 bool ArgExprsOk = true;
156
157 // now parse the list of expressions
158 while (1) {
159 ExprResult ArgExpr = ParseAssignmentExpression();
160 if (ArgExpr.isInvalid) {
161 ArgExprsOk = false;
162 SkipUntil(tok::r_paren);
163 break;
164 } else {
165 ArgExprs.push_back(ArgExpr.Val);
166 }
167 if (Tok.getKind() != tok::comma)
168 break;
169 ConsumeToken(); // Eat the comma, move to the next argument
170 }
171 // Match the ')'.
172 if (ArgExprsOk && Tok.getKind() == tok::r_paren) {
173 RParenLoc = ConsumeParen();
174 CurrAttr = Actions.ParseAttribute(AttrName, AttrNameLoc, CurrAttr,
175 0, SourceLocation(), &ArgExprs[0], ArgExprs.size(),
176 LParenLoc, RParenLoc);
177 }
178 }
179 }
180 } else {
181 CurrAttr = Actions.ParseAttribute(AttrName, AttrNameLoc, CurrAttr);
182 }
183 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000184 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
185 SkipUntil(tok::r_paren, false);
186 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
187 SkipUntil(tok::r_paren, false);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000188 }
189 return CurrAttr;
190}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000191
Chris Lattner53361ac2006-08-10 05:19:57 +0000192/// ParseDeclaration - Parse a full 'declaration', which consists of
193/// declaration-specifiers, some number of declarators, and a semicolon.
194/// 'Context' should be a Declarator::TheContext value.
Chris Lattner302b4be2006-11-19 02:31:38 +0000195Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000196 // Parse the common declaration-specifiers piece.
197 DeclSpec DS;
198 ParseDeclarationSpecifiers(DS);
199
Chris Lattner0e894622006-08-13 19:58:17 +0000200 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
201 // declaration-specifiers init-declarator-list[opt] ';'
202 if (Tok.getKind() == tok::semi) {
Chris Lattner0e894622006-08-13 19:58:17 +0000203 ConsumeToken();
Chris Lattner200bdc32006-11-19 02:43:37 +0000204 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Chris Lattner0e894622006-08-13 19:58:17 +0000205 }
206
Chris Lattner53361ac2006-08-10 05:19:57 +0000207 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
208 ParseDeclarator(DeclaratorInfo);
209
Chris Lattner302b4be2006-11-19 02:31:38 +0000210 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner53361ac2006-08-10 05:19:57 +0000211}
212
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000213/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
214/// parsing 'declaration-specifiers declarator'. This method is split out this
215/// way to handle the ambiguity between top-level function-definitions and
216/// declarations.
217///
218/// declaration: [C99 6.7]
219/// declaration-specifiers init-declarator-list[opt] ';' [TODO]
220/// [!C99] init-declarator-list ';' [TODO]
221/// [OMP] threadprivate-directive [TODO]
222///
223/// init-declarator-list: [C99 6.7]
224/// init-declarator
225/// init-declarator-list ',' init-declarator
226/// init-declarator: [C99 6.7]
227/// declarator
228/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000229/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
230/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000231///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000232Parser::DeclTy *Parser::
233ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
234
235 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
236 // that they can be chained properly if the actions want this.
237 Parser::DeclTy *LastDeclInGroup = 0;
238
Chris Lattner53361ac2006-08-10 05:19:57 +0000239 // At this point, we know that it is not a function definition. Parse the
240 // rest of the init-declarator-list.
241 while (1) {
Chris Lattner6d7e6342006-08-15 03:41:14 +0000242 // If a simple-asm-expr is present, parse it.
243 if (Tok.getKind() == tok::kw_asm)
244 ParseSimpleAsm();
245
Steve Naroff0f2fe172007-06-01 17:11:19 +0000246 DeclTy *AttrList = 0;
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000247 // If attributes are present, parse them.
248 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000249 AttrList = ParseAttributes();
Chris Lattner6d7e6342006-08-15 03:41:14 +0000250
Chris Lattner53361ac2006-08-10 05:19:57 +0000251 // Parse declarator '=' initializer.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000252 ExprResult Init;
Chris Lattner53361ac2006-08-10 05:19:57 +0000253 if (Tok.getKind() == tok::equal) {
254 ConsumeToken();
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000255 Init = ParseInitializer();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000256 if (Init.isInvalid) {
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000257 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000258 return 0;
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000259 }
Chris Lattner53361ac2006-08-10 05:19:57 +0000260 }
261
Chris Lattner697e5d62006-11-09 06:32:27 +0000262 // Inform the current actions module that we just parsed this declarator.
Chris Lattner289ab7b2006-11-08 06:54:53 +0000263 // FIXME: pass asm & attributes.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000264 LastDeclInGroup = Actions.ParseDeclarator(CurScope, D, Init.Val,
265 LastDeclInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +0000266
267 // If we don't have a comma, it is either the end of the list (a ';') or an
268 // error, bail out.
269 if (Tok.getKind() != tok::comma)
270 break;
271
272 // Consume the comma.
273 ConsumeToken();
274
275 // Parse the next declarator.
276 D.clear();
277 ParseDeclarator(D);
278 }
279
280 if (Tok.getKind() == tok::semi) {
281 ConsumeToken();
Chris Lattner776fac82007-06-09 00:53:06 +0000282 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +0000283 }
Chris Lattner776fac82007-06-09 00:53:06 +0000284
285 Diag(Tok, diag::err_parse_error);
286 // Skip to end of block or statement
287 SkipUntil(tok::r_brace, true);
288 if (Tok.getKind() == tok::semi)
289 ConsumeToken();
290 return 0;
Chris Lattner53361ac2006-08-10 05:19:57 +0000291}
292
Chris Lattner1890ac82006-08-13 01:16:23 +0000293/// ParseSpecifierQualifierList
294/// specifier-qualifier-list:
295/// type-specifier specifier-qualifier-list[opt]
296/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000297/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000298///
299void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
300 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
301 /// parse declaration-specifiers and complain about extra stuff.
302 SourceLocation Loc = Tok.getLocation();
303 ParseDeclarationSpecifiers(DS);
304
305 // Validate declspec for type-name.
306 unsigned Specs = DS.getParsedSpecifiers();
307 if (Specs == DeclSpec::PQ_None)
308 Diag(Tok, diag::err_typename_requires_specqual);
309
Chris Lattner1b22eed2006-11-28 05:12:07 +0000310 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000311 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000312 if (DS.getStorageClassSpecLoc().isValid())
313 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
314 else
315 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000316 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000317 }
Chris Lattner1b22eed2006-11-28 05:12:07 +0000318
319 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000320 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000321 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000322 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000323 }
324}
Chris Lattner53361ac2006-08-10 05:19:57 +0000325
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000326/// ParseDeclarationSpecifiers
327/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000328/// storage-class-specifier declaration-specifiers[opt]
329/// type-specifier declaration-specifiers[opt]
330/// type-qualifier declaration-specifiers[opt]
331/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000332/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000333///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000334/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000335/// 'typedef'
336/// 'extern'
337/// 'static'
338/// 'auto'
339/// 'register'
340/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000341/// type-specifier: [C99 6.7.2]
342/// 'void'
343/// 'char'
344/// 'short'
345/// 'int'
346/// 'long'
347/// 'float'
348/// 'double'
349/// 'signed'
350/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000351/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000352/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000353/// typedef-name
Bill Wendling4073ed52007-02-13 01:51:42 +0000354/// [C++] 'bool'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000355/// [C99] '_Bool'
356/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000357/// [C99] '_Imaginary' // Removed in TC2?
358/// [GNU] '_Decimal32'
359/// [GNU] '_Decimal64'
360/// [GNU] '_Decimal128'
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000361/// [GNU] typeof-specifier [TODO]
Chris Lattner3b561a32006-08-13 00:12:11 +0000362/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000363/// [OBJC] typedef-name objc-protocol-refs [TODO]
364/// [OBJC] objc-protocol-refs [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000365/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000366/// 'const'
367/// 'volatile'
368/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000369/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000370/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000371///
372void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000373 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000374 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000375 const char *PrevSpec = 0;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000376 SourceLocation Loc = Tok.getLocation();
377
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000378 switch (Tok.getKind()) {
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000379 // typedef-name
380 case tok::identifier:
381 // This identifier can only be a typedef name if we haven't already seen
Chris Lattner5646b3e2006-08-15 05:12:01 +0000382 // a type-specifier. Without this check we misparse:
383 // typedef int X; struct Y { short X; }; as 'short int'.
Chris Lattnerf055d432006-11-28 04:28:12 +0000384 if (!DS.hasTypeSpecifier()) {
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000385 // It has to be available as a typedef too!
386 if (void *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(),
387 CurScope)) {
Chris Lattnerb20e8942006-11-28 05:30:29 +0000388 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000389 TypeRep);
Chris Lattneredc9e392006-12-02 06:21:46 +0000390 break;
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000391 }
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000392 }
393 // FALL THROUGH.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000394 default:
395 // If this is not a declaration specifier token, we're done reading decl
396 // specifiers. First verify that DeclSpec's are consistent.
Chris Lattnerb20e8942006-11-28 05:30:29 +0000397 DS.Finish(Diags, getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000398 return;
Chris Lattnere37e2332006-08-15 04:50:22 +0000399
400 // GNU attributes support.
401 case tok::kw___attribute:
Steve Naroff45552922007-06-01 21:56:17 +0000402 DS.SetAttributeList(ParseAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +0000403 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000404
405 // storage-class-specifier
406 case tok::kw_typedef:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000407 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000408 break;
409 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000410 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000411 Diag(Tok, diag::ext_thread_before, "extern");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000412 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000413 break;
414 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000415 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000416 Diag(Tok, diag::ext_thread_before, "static");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000417 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000418 break;
419 case tok::kw_auto:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000420 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000421 break;
422 case tok::kw_register:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000423 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000424 break;
425 case tok::kw___thread:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000426 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000427 break;
428
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000429 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000430 case tok::kw_short:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000431 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000432 break;
433 case tok::kw_long:
Chris Lattner353f5742006-11-28 04:50:12 +0000434 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
Chris Lattnerb20e8942006-11-28 05:30:29 +0000435 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
Chris Lattner353f5742006-11-28 04:50:12 +0000436 else
Chris Lattnerb20e8942006-11-28 05:30:29 +0000437 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000438 break;
439 case tok::kw_signed:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000440 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000441 break;
442 case tok::kw_unsigned:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000443 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000444 break;
445 case tok::kw__Complex:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000446 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000447 break;
448 case tok::kw__Imaginary:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000449 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000450 break;
451 case tok::kw_void:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000452 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000453 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000454 case tok::kw_char:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000455 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000456 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000457 case tok::kw_int:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000458 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000459 break;
460 case tok::kw_float:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000461 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000462 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000463 case tok::kw_double:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000464 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000465 break;
Bill Wendling4073ed52007-02-13 01:51:42 +0000466 case tok::kw_bool: // [C++ 2.11p1]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000467 case tok::kw__Bool:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000468 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000469 break;
470 case tok::kw__Decimal32:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000471 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000472 break;
473 case tok::kw__Decimal64:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000474 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000475 break;
476 case tok::kw__Decimal128:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000477 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000478 break;
479
Chris Lattner1890ac82006-08-13 01:16:23 +0000480 case tok::kw_struct:
481 case tok::kw_union:
482 ParseStructUnionSpecifier(DS);
483 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000484 case tok::kw_enum:
485 ParseEnumSpecifier(DS);
486 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000487
488 // type-qualifier
489 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000490 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
491 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000492 break;
493 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000494 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
495 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000496 break;
497 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000498 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
499 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000500 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000501
502 // function-specifier
503 case tok::kw_inline:
Chris Lattner1b22eed2006-11-28 05:12:07 +0000504 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000505 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000506 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000507 // If the specifier combination wasn't legal, issue a diagnostic.
508 if (isInvalid) {
509 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000510 if (isInvalid == 1) // Error.
511 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
512 else // extwarn.
513 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000514 }
515 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000516 }
517}
518
Chris Lattnerffbc2712007-01-25 06:05:38 +0000519/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
520/// the first token has already been read and has been turned into an instance
521/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
522/// otherwise it returns false and fills in Decl.
523bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
Steve Naroff0f2fe172007-06-01 17:11:19 +0000524 DeclTy *AttrList = 0;
Chris Lattnere37e2332006-08-15 04:50:22 +0000525 // If attributes exist after tag, parse them.
526 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000527 AttrList = ParseAttributes();
Chris Lattnerffbc2712007-01-25 06:05:38 +0000528
Chris Lattner1890ac82006-08-13 01:16:23 +0000529 // Must have either 'struct name' or 'struct {...}'.
530 if (Tok.getKind() != tok::identifier &&
531 Tok.getKind() != tok::l_brace) {
532 Diag(Tok, diag::err_expected_ident_lbrace);
Chris Lattner8c6519a2007-01-22 07:41:36 +0000533 // TODO: better error recovery here.
Chris Lattnerffbc2712007-01-25 06:05:38 +0000534 return true;
Chris Lattner1890ac82006-08-13 01:16:23 +0000535 }
536
Chris Lattner8c6519a2007-01-22 07:41:36 +0000537 // If an identifier is present, consume and remember it.
538 IdentifierInfo *Name = 0;
539 SourceLocation NameLoc;
540 if (Tok.getKind() == tok::identifier) {
541 Name = Tok.getIdentifierInfo();
542 NameLoc = ConsumeToken();
543 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000544
Chris Lattner8c6519a2007-01-22 07:41:36 +0000545 // There are three options here. If we have 'struct foo;', then this is a
546 // forward declaration. If we have 'struct foo {...' then this is a
Chris Lattner7b9ace62007-01-23 20:11:08 +0000547 // definition. Otherwise we have something like 'struct foo xyz', a reference.
Chris Lattner8799cf22007-01-23 01:57:16 +0000548 //
549 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
550 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
551 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
552 //
Chris Lattner7b9ace62007-01-23 20:11:08 +0000553 Action::TagKind TK;
554 if (Tok.getKind() == tok::l_brace)
555 TK = Action::TK_Definition;
556 else if (Tok.getKind() == tok::semi)
557 TK = Action::TK_Declaration;
558 else
559 TK = Action::TK_Reference;
Chris Lattnerffbc2712007-01-25 06:05:38 +0000560 Decl = Actions.ParseTag(CurScope, TagType, TK, StartLoc, Name, NameLoc);
561 return false;
562}
563
564
565/// ParseStructUnionSpecifier
566/// struct-or-union-specifier: [C99 6.7.2.1]
567/// struct-or-union identifier[opt] '{' struct-contents '}'
568/// struct-or-union identifier
569/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
570/// '}' attributes[opt]
571/// [GNU] struct-or-union attributes[opt] identifier
572/// struct-or-union:
573/// 'struct'
574/// 'union'
575///
576void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
577 assert((Tok.getKind() == tok::kw_struct ||
578 Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
579 DeclSpec::TST TagType =
580 Tok.getKind() == tok::kw_union ? DeclSpec::TST_union : DeclSpec::TST_struct;
581 SourceLocation StartLoc = ConsumeToken();
582
583 // Parse the tag portion of this.
584 DeclTy *TagDecl;
585 if (ParseTag(TagDecl, TagType, StartLoc))
586 return;
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000587
Chris Lattner90a26b02007-01-23 04:38:16 +0000588 // If there is a body, parse it and inform the actions module.
589 if (Tok.getKind() == tok::l_brace)
Chris Lattner1300fb92007-01-23 23:42:53 +0000590 ParseStructUnionBody(StartLoc, TagType, TagDecl);
Chris Lattnerda72c822006-08-13 22:16:42 +0000591
592 const char *PrevSpec = 0;
Chris Lattnerb9d572a2007-01-23 04:58:34 +0000593 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000594 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000595}
596
597
Chris Lattner90a26b02007-01-23 04:38:16 +0000598/// ParseStructUnionBody
599/// struct-contents:
600/// struct-declaration-list
601/// [EXT] empty
602/// [GNU] "struct-declaration-list" without terminatoring ';' [TODO]
603/// struct-declaration-list:
604/// struct-declaration
605/// struct-declaration-list struct-declaration
606/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
607/// struct-declaration:
608/// specifier-qualifier-list struct-declarator-list ';'
609/// [GNU] __extension__ struct-declaration [TODO]
610/// [GNU] specifier-qualifier-list ';' [TODO]
611/// struct-declarator-list:
612/// struct-declarator
613/// struct-declarator-list ',' struct-declarator
614/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
615/// struct-declarator:
616/// declarator
617/// [GNU] declarator attributes[opt]
618/// declarator[opt] ':' constant-expression
619/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
620///
Chris Lattner1300fb92007-01-23 23:42:53 +0000621void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
622 unsigned TagType, DeclTy *TagDecl) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000623 SourceLocation LBraceLoc = ConsumeBrace();
624
Chris Lattner7b9ace62007-01-23 20:11:08 +0000625 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
626 // C++.
Chris Lattner90a26b02007-01-23 04:38:16 +0000627 if (Tok.getKind() == tok::r_brace)
628 Diag(Tok, diag::ext_empty_struct_union_enum,
629 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Chris Lattner7b9ace62007-01-23 20:11:08 +0000630
Chris Lattner1300fb92007-01-23 23:42:53 +0000631 SmallVector<DeclTy*, 32> FieldDecls;
632
Chris Lattner7b9ace62007-01-23 20:11:08 +0000633 // While we still have something to read, read the declarations in the struct.
Chris Lattner90a26b02007-01-23 04:38:16 +0000634 while (Tok.getKind() != tok::r_brace &&
635 Tok.getKind() != tok::eof) {
636 // Each iteration of this loop reads one struct-declaration.
637
638 // Parse the common specifier-qualifiers-list piece.
639 DeclSpec DS;
640 SourceLocation SpecQualLoc = Tok.getLocation();
641 ParseSpecifierQualifierList(DS);
642 // TODO: Does specifier-qualifier list correctly check that *something* is
643 // specified?
644
Chris Lattner90a26b02007-01-23 04:38:16 +0000645 // If there are no declarators, issue a warning.
646 if (Tok.getKind() == tok::semi) {
647 Diag(SpecQualLoc, diag::w_no_declarators);
Chris Lattner7b9ace62007-01-23 20:11:08 +0000648 ConsumeToken();
649 continue;
650 }
651
652 // Read struct-declarators until we find the semicolon.
653 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
654
655 while (1) {
656 /// struct-declarator: declarator
657 /// struct-declarator: declarator[opt] ':' constant-expression
658 if (Tok.getKind() != tok::colon)
659 ParseDeclarator(DeclaratorInfo);
660
661 ExprTy *BitfieldSize = 0;
662 if (Tok.getKind() == tok::colon) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000663 ConsumeToken();
Chris Lattner7b9ace62007-01-23 20:11:08 +0000664 ExprResult Res = ParseConstantExpression();
665 if (Res.isInvalid) {
666 SkipUntil(tok::semi, true, true);
667 } else {
668 BitfieldSize = Res.Val;
669 }
Chris Lattner90a26b02007-01-23 04:38:16 +0000670 }
Chris Lattner7b9ace62007-01-23 20:11:08 +0000671
Steve Naroff0f2fe172007-06-01 17:11:19 +0000672 DeclTy *AttrList = 0;
Chris Lattner7b9ace62007-01-23 20:11:08 +0000673 // If attributes exist after the declarator, parse them.
674 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000675 AttrList = ParseAttributes();
Chris Lattner7b9ace62007-01-23 20:11:08 +0000676
Chris Lattner367b0192007-01-23 22:29:13 +0000677 // Install the declarator into the current TagDecl.
Chris Lattner1300fb92007-01-23 23:42:53 +0000678 DeclTy *Field = Actions.ParseField(CurScope, TagDecl, SpecQualLoc,
679 DeclaratorInfo, BitfieldSize);
680 FieldDecls.push_back(Field);
Chris Lattner7b9ace62007-01-23 20:11:08 +0000681
682 // If we don't have a comma, it is either the end of the list (a ';')
683 // or an error, bail out.
684 if (Tok.getKind() != tok::comma)
685 break;
686
687 // Consume the comma.
688 ConsumeToken();
689
690 // Parse the next declarator.
691 DeclaratorInfo.clear();
692
693 // Attributes are only allowed on the second declarator.
694 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000695 AttrList = ParseAttributes();
Chris Lattner90a26b02007-01-23 04:38:16 +0000696 }
697
698 if (Tok.getKind() == tok::semi) {
699 ConsumeToken();
700 } else {
701 Diag(Tok, diag::err_expected_semi_decl_list);
702 // Skip to end of block or statement
703 SkipUntil(tok::r_brace, true, true);
704 }
705 }
706
707 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
708
Chris Lattnerc1915e22007-01-25 07:29:02 +0000709 Actions.ParseRecordBody(RecordLoc, TagDecl, &FieldDecls[0],FieldDecls.size());
710
Steve Naroff0f2fe172007-06-01 17:11:19 +0000711 DeclTy *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +0000712 // If attributes exist after struct contents, parse them.
713 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000714 AttrList = ParseAttributes(); // FIXME: where should I put them?
Chris Lattner90a26b02007-01-23 04:38:16 +0000715}
716
717
Chris Lattner3b561a32006-08-13 00:12:11 +0000718/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000719/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000720/// 'enum' identifier[opt] '{' enumerator-list '}'
721/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000722/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
723/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000724/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000725/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000726void Parser::ParseEnumSpecifier(DeclSpec &DS) {
727 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattnerb20e8942006-11-28 05:30:29 +0000728 SourceLocation StartLoc = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000729
Chris Lattnerffbc2712007-01-25 06:05:38 +0000730 // Parse the tag portion of this.
731 DeclTy *TagDecl;
732 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
Chris Lattner3b561a32006-08-13 00:12:11 +0000733 return;
Chris Lattner3b561a32006-08-13 00:12:11 +0000734
Chris Lattnerc1915e22007-01-25 07:29:02 +0000735 if (Tok.getKind() == tok::l_brace)
736 ParseEnumBody(StartLoc, TagDecl);
737
Chris Lattner3b561a32006-08-13 00:12:11 +0000738 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000739 const char *PrevSpec = 0;
Chris Lattnerffbc2712007-01-25 06:05:38 +0000740 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000741 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000742}
743
Chris Lattnerc1915e22007-01-25 07:29:02 +0000744/// ParseEnumBody - Parse a {} enclosed enumerator-list.
745/// enumerator-list:
746/// enumerator
747/// enumerator-list ',' enumerator
748/// enumerator:
749/// enumeration-constant
750/// enumeration-constant '=' constant-expression
751/// enumeration-constant:
752/// identifier
753///
754void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
755 SourceLocation LBraceLoc = ConsumeBrace();
756
757 if (Tok.getKind() == tok::r_brace)
758 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
759
760 SmallVector<DeclTy*, 32> EnumConstantDecls;
761
762 // Parse the enumerator-list.
763 while (Tok.getKind() == tok::identifier) {
764 IdentifierInfo *Ident = Tok.getIdentifierInfo();
765 SourceLocation IdentLoc = ConsumeToken();
766
767 SourceLocation EqualLoc;
768 ExprTy *AssignedVal = 0;
769 if (Tok.getKind() == tok::equal) {
770 EqualLoc = ConsumeToken();
771 ExprResult Res = ParseConstantExpression();
772 if (Res.isInvalid)
Chris Lattnerda6c2ce2007-04-27 19:13:15 +0000773 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000774 else
775 AssignedVal = Res.Val;
776 }
777
778 // Install the enumerator constant into EnumDecl.
779 DeclTy *ConstDecl = Actions.ParseEnumConstant(CurScope, EnumDecl,
780 IdentLoc, Ident,
781 EqualLoc, AssignedVal);
782 EnumConstantDecls.push_back(ConstDecl);
783
784 if (Tok.getKind() != tok::comma)
785 break;
786 SourceLocation CommaLoc = ConsumeToken();
787
788 if (Tok.getKind() != tok::identifier && !getLang().C99)
789 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
790 }
791
792 // Eat the }.
793 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
794
795 Actions.ParseEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
796 EnumConstantDecls.size());
797
Steve Naroff0f2fe172007-06-01 17:11:19 +0000798 DeclTy *AttrList = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000799 // If attributes exist after the identifier list, parse them.
800 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000801 AttrList = ParseAttributes(); // FIXME: where do they do?
Chris Lattnerc1915e22007-01-25 07:29:02 +0000802}
Chris Lattner3b561a32006-08-13 00:12:11 +0000803
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000804/// isTypeSpecifierQualifier - Return true if the current token could be the
805/// start of a specifier-qualifier-list.
806bool Parser::isTypeSpecifierQualifier() const {
807 switch (Tok.getKind()) {
808 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000809 // GNU attributes support.
810 case tok::kw___attribute:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000811 // type-specifiers
812 case tok::kw_short:
813 case tok::kw_long:
814 case tok::kw_signed:
815 case tok::kw_unsigned:
816 case tok::kw__Complex:
817 case tok::kw__Imaginary:
818 case tok::kw_void:
819 case tok::kw_char:
820 case tok::kw_int:
821 case tok::kw_float:
822 case tok::kw_double:
823 case tok::kw__Bool:
824 case tok::kw__Decimal32:
825 case tok::kw__Decimal64:
826 case tok::kw__Decimal128:
827
828 // struct-or-union-specifier
829 case tok::kw_struct:
830 case tok::kw_union:
831 // enum-specifier
832 case tok::kw_enum:
833
834 // type-qualifier
835 case tok::kw_const:
836 case tok::kw_volatile:
837 case tok::kw_restrict:
838 return true;
839
840 // typedef-name
841 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000842 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000843
844 // TODO: Attributes.
845 }
846}
847
Chris Lattneracd58a32006-08-06 17:24:14 +0000848/// isDeclarationSpecifier() - Return true if the current token is part of a
849/// declaration specifier.
850bool Parser::isDeclarationSpecifier() const {
851 switch (Tok.getKind()) {
852 default: return false;
853 // storage-class-specifier
854 case tok::kw_typedef:
855 case tok::kw_extern:
856 case tok::kw_static:
857 case tok::kw_auto:
858 case tok::kw_register:
859 case tok::kw___thread:
860
861 // type-specifiers
862 case tok::kw_short:
863 case tok::kw_long:
864 case tok::kw_signed:
865 case tok::kw_unsigned:
866 case tok::kw__Complex:
867 case tok::kw__Imaginary:
868 case tok::kw_void:
869 case tok::kw_char:
870 case tok::kw_int:
871 case tok::kw_float:
872 case tok::kw_double:
873 case tok::kw__Bool:
874 case tok::kw__Decimal32:
875 case tok::kw__Decimal64:
876 case tok::kw__Decimal128:
877
878 // struct-or-union-specifier
879 case tok::kw_struct:
880 case tok::kw_union:
881 // enum-specifier
882 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000883
Chris Lattneracd58a32006-08-06 17:24:14 +0000884 // type-qualifier
885 case tok::kw_const:
886 case tok::kw_volatile:
887 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000888
Chris Lattneracd58a32006-08-06 17:24:14 +0000889 // function-specifier
890 case tok::kw_inline:
891 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000892
Chris Lattneracd58a32006-08-06 17:24:14 +0000893 // typedef-name
894 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000895 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +0000896 // TODO: Attributes.
897 }
898}
899
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000900
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000901/// ParseTypeQualifierListOpt
902/// type-qualifier-list: [C99 6.7.5]
903/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000904/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000905/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000906/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000907///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000908void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000909 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000910 int isInvalid = false;
911 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +0000912 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000913
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000914 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000915 default:
Chris Lattnere37e2332006-08-15 04:50:22 +0000916 // If this is not a type-qualifier token, we're done reading type
917 // qualifiers. First verify that DeclSpec's are consistent.
Chris Lattnerb20e8942006-11-28 05:30:29 +0000918 DS.Finish(Diags, getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000919 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000920 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000921 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
922 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000923 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000924 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000925 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
926 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000927 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000928 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000929 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
930 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000931 break;
Chris Lattnere37e2332006-08-15 04:50:22 +0000932 case tok::kw___attribute:
933 ParseAttributes();
Steve Naroff98d153c2007-06-06 23:19:11 +0000934 continue; // do *not* consume the next token!
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000935 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000936
937 // If the specifier combination wasn't legal, issue a diagnostic.
938 if (isInvalid) {
939 assert(PrevSpec && "Method did not return previous specifier!");
940 if (isInvalid == 1) // Error.
941 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
942 else // extwarn.
943 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
944 }
945 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000946 }
947}
948
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000949
950/// ParseDeclarator - Parse and verify a newly-initialized declarator.
951///
952void Parser::ParseDeclarator(Declarator &D) {
953 /// This implements the 'declarator' production in the C grammar, then checks
954 /// for well-formedness and issues diagnostics.
955 ParseDeclaratorInternal(D);
956
Chris Lattner9fab3b92006-08-12 18:25:42 +0000957 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000958
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000959}
960
961/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000962/// declarator: [C99 6.7.5]
963/// pointer[opt] direct-declarator
Bill Wendling93efb222007-06-02 23:28:54 +0000964/// [C++] '&' declarator [C++ 8p4, dcl.decl]
965/// [GNU] '&' restrict[opt] attributes[opt] declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +0000966///
967/// pointer: [C99 6.7.5]
968/// '*' type-qualifier-list[opt]
969/// '*' type-qualifier-list[opt] pointer
970///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000971void Parser::ParseDeclaratorInternal(Declarator &D) {
Bill Wendling3708c182007-05-27 10:15:43 +0000972 tok::TokenKind Kind = Tok.getKind();
973
974 // Not a pointer or C++ reference.
975 if (Kind != tok::star && !(Kind == tok::amp && getLang().CPlusPlus))
Chris Lattner6c7416c2006-08-07 00:19:33 +0000976 return ParseDirectDeclarator(D);
977
Bill Wendling3708c182007-05-27 10:15:43 +0000978 // Otherwise, '*' -> pointer or '&' -> reference.
979 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
980
981 if (Kind == tok::star) {
982 // Is a pointer
983 DeclSpec DS;
Steve Naroff98d153c2007-06-06 23:19:11 +0000984
Bill Wendling3708c182007-05-27 10:15:43 +0000985 ParseTypeQualifierListOpt(DS);
Chris Lattner6c7416c2006-08-07 00:19:33 +0000986
Bill Wendling3708c182007-05-27 10:15:43 +0000987 // Recursively parse the declarator.
988 ParseDeclaratorInternal(D);
Chris Lattner9dfdb3c2006-11-13 07:38:09 +0000989
Bill Wendling3708c182007-05-27 10:15:43 +0000990 // Remember that we parsed a pointer type, and remember the type-quals.
991 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc));
992 } else {
993 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +0000994 DeclSpec DS;
995
996 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
997 // cv-qualifiers are introduced through the use of a typedef or of a
998 // template type argument, in which case the cv-qualifiers are ignored.
999 //
1000 // [GNU] Retricted references are allowed.
1001 // [GNU] Attributes on references are allowed.
1002 ParseTypeQualifierListOpt(DS);
1003
1004 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1005 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1006 Diag(DS.getConstSpecLoc(),
1007 diag::err_invalid_reference_qualifier_application,
1008 "const");
1009 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1010 Diag(DS.getVolatileSpecLoc(),
1011 diag::err_invalid_reference_qualifier_application,
1012 "volatile");
1013 }
Bill Wendling3708c182007-05-27 10:15:43 +00001014
1015 // Recursively parse the declarator.
1016 ParseDeclaratorInternal(D);
1017
1018 // Remember that we parsed a reference type. It doesn't have type-quals.
Bill Wendling93efb222007-06-02 23:28:54 +00001019 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc));
Bill Wendling3708c182007-05-27 10:15:43 +00001020 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001021}
1022
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001023/// ParseDirectDeclarator
1024/// direct-declarator: [C99 6.7.5]
1025/// identifier
1026/// '(' declarator ')'
1027/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00001028/// [C90] direct-declarator '[' constant-expression[opt] ']'
1029/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1030/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1031/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1032/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001033/// direct-declarator '(' parameter-type-list ')'
1034/// direct-declarator '(' identifier-list[opt] ')'
1035/// [GNU] direct-declarator '(' parameter-forward-declarations
1036/// parameter-type-list[opt] ')'
1037///
Chris Lattneracd58a32006-08-06 17:24:14 +00001038void Parser::ParseDirectDeclarator(Declarator &D) {
1039 // Parse the first direct-declarator seen.
1040 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
1041 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1042 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1043 ConsumeToken();
1044 } else if (Tok.getKind() == tok::l_paren) {
1045 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001046 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001047 // Example: 'char (*X)' or 'int (*XX)(void)'
1048 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001049 } else if (D.mayOmitIdentifier()) {
1050 // This could be something simple like "int" (in which case the declarator
1051 // portion is empty), if an abstract-declarator is allowed.
1052 D.SetIdentifier(0, Tok.getLocation());
1053 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +00001054 // Expected identifier or '('.
1055 Diag(Tok, diag::err_expected_ident_lparen);
1056 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +00001057 }
1058
1059 assert(D.isPastIdentifier() &&
1060 "Haven't past the location of the identifier yet?");
1061
1062 while (1) {
1063 if (Tok.getKind() == tok::l_paren) {
1064 ParseParenDeclarator(D);
1065 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001066 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001067 } else {
1068 break;
1069 }
1070 }
1071}
1072
1073/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
1074/// either be before the identifier (in which case these are just grouping
1075/// parens for precedence) or it may be after the identifier, in which case
1076/// these are function arguments.
1077///
1078/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001079/// parameter-type-list: [C99 6.7.5]
1080/// parameter-list
1081/// parameter-list ',' '...'
1082///
1083/// parameter-list: [C99 6.7.5]
1084/// parameter-declaration
1085/// parameter-list ',' parameter-declaration
1086///
1087/// parameter-declaration: [C99 6.7.5]
1088/// declaration-specifiers declarator
Chris Lattnere37e2332006-08-15 04:50:22 +00001089/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001090/// declaration-specifiers abstract-declarator[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001091/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001092///
1093/// identifier-list: [C99 6.7.5]
1094/// identifier
1095/// identifier-list ',' identifier
1096///
Chris Lattneracd58a32006-08-06 17:24:14 +00001097void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001098 SourceLocation StartLoc = ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001099
Chris Lattneracd58a32006-08-06 17:24:14 +00001100 // If we haven't past the identifier yet (or where the identifier would be
1101 // stored, if this is an abstract declarator), then this is probably just
1102 // grouping parens.
1103 if (!D.isPastIdentifier()) {
1104 // Okay, this is probably a grouping paren. However, if this could be an
1105 // abstract-declarator, then this could also be the start of function
1106 // arguments (consider 'void()').
1107 bool isGrouping;
1108
1109 if (!D.mayOmitIdentifier()) {
1110 // If this can't be an abstract-declarator, this *must* be a grouping
1111 // paren, because we haven't seen the identifier yet.
1112 isGrouping = true;
1113 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
1114 isDeclarationSpecifier()) { // 'int(int)' is a function.
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001115 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1116 // considered to be a type, not a K&R identifier-list.
Chris Lattneracd58a32006-08-06 17:24:14 +00001117 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001118 } else {
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001119 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +00001120 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001121 }
Chris Lattneracd58a32006-08-06 17:24:14 +00001122
1123 // If this is a grouping paren, handle:
1124 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001125 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001126 if (isGrouping) {
Steve Naroff0f2fe172007-06-01 17:11:19 +00001127 DeclTy *AttrList = 0;
Chris Lattnere37e2332006-08-15 04:50:22 +00001128 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +00001129 AttrList = ParseAttributes();
Chris Lattnere37e2332006-08-15 04:50:22 +00001130
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001131 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +00001132 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +00001133 MatchRHSPunctuation(tok::r_paren, StartLoc);
Chris Lattneracd58a32006-08-06 17:24:14 +00001134 return;
1135 }
1136
1137 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +00001138 // argument list. Recognize that this declarator will never have an
1139 // identifier (and remember where it would have been), then fall through to
1140 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +00001141 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001142 }
1143
Chris Lattneracd58a32006-08-06 17:24:14 +00001144 // Okay, this is the parameter list of a function definition, or it is an
1145 // identifier list of a K&R-style function.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001146 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +00001147 bool HasPrototype;
Chris Lattner14776b92006-08-06 22:27:40 +00001148 bool ErrorEmitted = false;
1149
Chris Lattneredc9e392006-12-02 06:21:46 +00001150 // Build up an array of information about the parsed arguments.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001151 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattnerad9ac942007-01-23 01:14:52 +00001152 SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Chris Lattneredc9e392006-12-02 06:21:46 +00001153
Chris Lattneracd58a32006-08-06 17:24:14 +00001154 if (Tok.getKind() == tok::r_paren) {
1155 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001156 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001157 HasPrototype = false;
1158 } else if (Tok.getKind() == tok::identifier &&
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001159 // K&R identifier lists can't have typedefs as identifiers, per
1160 // C99 6.7.5.3p11.
Steve Naroffb419d3a2006-10-27 23:18:49 +00001161 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001162 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1163 // normal declarators, not for abstract-declarators.
1164 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
1165
1166 // If there was no identifier specified, either we are in an
1167 // abstract-declarator, or we are in a parameter declarator which was found
1168 // to be abstract. In abstract-declarators, identifier lists are not valid,
1169 // diagnose this.
1170 if (!D.getIdentifier())
1171 Diag(Tok, diag::ext_ident_list_in_param);
Chris Lattneredc9e392006-12-02 06:21:46 +00001172
Chris Lattnercbc426d2006-12-02 06:43:02 +00001173 // Remember this identifier in ParamInfo.
1174 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1175 Tok.getLocation(), 0));
1176
Chris Lattneracd58a32006-08-06 17:24:14 +00001177 ConsumeToken();
1178 while (Tok.getKind() == tok::comma) {
1179 // Eat the comma.
1180 ConsumeToken();
1181
Chris Lattnercbc426d2006-12-02 06:43:02 +00001182 if (Tok.getKind() != tok::identifier) {
1183 Diag(Tok, diag::err_expected_ident);
Chris Lattner14776b92006-08-06 22:27:40 +00001184 ErrorEmitted = true;
1185 break;
1186 }
Chris Lattnercbc426d2006-12-02 06:43:02 +00001187
Chris Lattner969ca152006-12-03 06:29:03 +00001188 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
1189
1190 // Verify that the argument identifier has not already been mentioned.
Chris Lattnerbaf33662007-01-27 02:14:08 +00001191 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerad9ac942007-01-23 01:14:52 +00001192 Diag(Tok.getLocation(), diag::err_param_redefinition,ParmII->getName());
1193 ParmII = 0;
1194 }
Chris Lattner969ca152006-12-03 06:29:03 +00001195
Chris Lattnercbc426d2006-12-02 06:43:02 +00001196 // Remember this identifier in ParamInfo.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001197 if (ParmII)
1198 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1199 Tok.getLocation(), 0));
Chris Lattnercbc426d2006-12-02 06:43:02 +00001200
1201 // Eat the identifier.
1202 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00001203 }
1204
Chris Lattneracd58a32006-08-06 17:24:14 +00001205 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001206 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001207 HasPrototype = false;
1208 } else {
Chris Lattner43e956c2006-11-28 04:05:37 +00001209 // Finally, a normal, non-empty parameter type list.
1210
Chris Lattnercbc426d2006-12-02 06:43:02 +00001211 // Enter function-declaration scope, limiting any declarators for struct
1212 // tags to the function prototype scope.
1213 // FIXME: is this needed?
Chris Lattner43e956c2006-11-28 04:05:37 +00001214 EnterScope(0);
1215
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001216 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001217 while (1) {
1218 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001219 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +00001220
1221 // Check to see if this is "void(...)" which is not allowed.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001222 if (ParamInfo.empty()) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001223 // Otherwise, parse parameter type list. If it starts with an
1224 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +00001225 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001226 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +00001227 }
1228
1229 // Consume the ellipsis.
1230 ConsumeToken();
1231 break;
1232 }
1233
Chris Lattneracd58a32006-08-06 17:24:14 +00001234 // Parse the declaration-specifiers.
1235 DeclSpec DS;
1236 ParseDeclarationSpecifiers(DS);
1237
1238 // Parse the declarator. This is "PrototypeContext", because we must
1239 // accept either 'declarator' or 'abstract-declarator' here.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001240 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1241 ParseDeclarator(ParmDecl);
Chris Lattneracd58a32006-08-06 17:24:14 +00001242
Chris Lattnere37e2332006-08-15 04:50:22 +00001243 // Parse GNU attributes, if present.
Steve Naroff0f2fe172007-06-01 17:11:19 +00001244 DeclTy *AttrList = 0;
Chris Lattnere37e2332006-08-15 04:50:22 +00001245 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +00001246 AttrList = ParseAttributes();
Chris Lattnere37e2332006-08-15 04:50:22 +00001247
Chris Lattner43e956c2006-11-28 04:05:37 +00001248 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001249 // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted.
1250 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1251 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
Chris Lattner4d8f8732006-11-28 05:05:08 +00001252 Diag(DS.getStorageClassSpecLoc(),
Chris Lattner43e956c2006-11-28 04:05:37 +00001253 diag::err_invalid_storage_class_in_func_decl);
Chris Lattner353f5742006-11-28 04:50:12 +00001254 DS.ClearStorageClassSpecs();
Chris Lattner43e956c2006-11-28 04:05:37 +00001255 }
Chris Lattner4d8f8732006-11-28 05:05:08 +00001256 if (DS.isThreadSpecified()) {
1257 Diag(DS.getThreadSpecLoc(),
1258 diag::err_invalid_storage_class_in_func_decl);
1259 DS.ClearStorageClassSpecs();
1260 }
Chris Lattner43e956c2006-11-28 04:05:37 +00001261
1262 // Inform the actions module about the parameter declarator, so it gets
1263 // added to the current scope.
Chris Lattner216d8652006-12-02 06:47:41 +00001264 Action::TypeResult ParamTy =
1265 Actions.ParseParamDeclaratorType(CurScope, ParmDecl);
Chris Lattnercbc426d2006-12-02 06:43:02 +00001266
1267 // Remember this parsed parameter in ParamInfo.
Chris Lattner969ca152006-12-03 06:29:03 +00001268 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1269
1270 // Verify that the argument identifier has not already been mentioned.
Chris Lattnerbaf33662007-01-27 02:14:08 +00001271 if (ParmII && !ParamsSoFar.insert(ParmII)) {
Chris Lattnerad9ac942007-01-23 01:14:52 +00001272 Diag(ParmDecl.getIdentifierLoc(), diag::err_param_redefinition,
1273 ParmII->getName());
1274 ParmII = 0;
Chris Lattner969ca152006-12-03 06:29:03 +00001275 }
1276
1277 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnercbc426d2006-12-02 06:43:02 +00001278 ParmDecl.getIdentifierLoc(),
1279 ParamTy.Val));
Chris Lattneracd58a32006-08-06 17:24:14 +00001280
1281 // If the next token is a comma, consume it and keep reading arguments.
1282 if (Tok.getKind() != tok::comma) break;
1283
1284 // Consume the comma.
1285 ConsumeToken();
1286 }
1287
1288 HasPrototype = true;
Chris Lattner43e956c2006-11-28 04:05:37 +00001289
1290 // Leave prototype scope.
1291 ExitScope();
Chris Lattneracd58a32006-08-06 17:24:14 +00001292 }
1293
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001294 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerd2e97c12006-12-03 02:03:33 +00001295 if (!ErrorEmitted)
1296 D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic,
1297 &ParamInfo[0], ParamInfo.size(),
1298 StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001299
Chris Lattner14776b92006-08-06 22:27:40 +00001300 // If we have the closing ')', eat it and we're done.
1301 if (Tok.getKind() == tok::r_paren) {
1302 ConsumeParen();
1303 } else {
1304 // If an error happened earlier parsing something else in the proto, don't
1305 // issue another error.
1306 if (!ErrorEmitted)
1307 Diag(Tok, diag::err_expected_rparen);
1308 SkipUntil(tok::r_paren);
1309 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001310}
Chris Lattneracd58a32006-08-06 17:24:14 +00001311
Chris Lattnere8074e62006-08-06 18:30:15 +00001312
1313/// [C90] direct-declarator '[' constant-expression[opt] ']'
1314/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1315/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1316/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1317/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1318void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001319 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001320
1321 // If valid, this location is the position where we read the 'static' keyword.
1322 SourceLocation StaticLoc;
Chris Lattneraf635312006-10-16 06:06:51 +00001323 if (Tok.getKind() == tok::kw_static)
1324 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001325
1326 // If there is a type-qualifier-list, read it now.
1327 DeclSpec DS;
1328 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001329
1330 // If we haven't already read 'static', check to see if there is one after the
1331 // type-qualifier-list.
Chris Lattneraf635312006-10-16 06:06:51 +00001332 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static)
1333 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001334
1335 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001336 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001337 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +00001338 if (Tok.getKind() == tok::star) {
1339 // Remember the '*' token, in case we have to un-get it.
1340 LexerToken StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +00001341 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +00001342
1343 // Check that the ']' token is present to avoid incorrectly parsing
1344 // expressions starting with '*' as [*].
1345 if (Tok.getKind() == tok::r_square) {
1346 if (StaticLoc.isValid())
1347 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1348 StaticLoc = SourceLocation(); // Drop the static.
1349 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +00001350 } else {
1351 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +00001352 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +00001353 // need to reparse it. This handles cases like 'X[*p + 4]'
1354 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +00001355 }
Chris Lattner9fab3b92006-08-12 18:25:42 +00001356 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001357 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001358 NumElements = ParseAssignmentExpression();
1359 }
1360
1361 // If there was an error parsing the assignment-expression, recover.
1362 if (NumElements.isInvalid) {
1363 // If the expression was invalid, skip it.
1364 SkipUntil(tok::r_square);
1365 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001366 }
1367
Chris Lattner04f80192006-08-15 04:55:54 +00001368 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001369
Chris Lattnere8074e62006-08-06 18:30:15 +00001370 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1371 // it was not a constant expression.
1372 if (!getLang().C99) {
1373 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001374 if (isStar || StaticLoc.isValid() ||
1375 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001376 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001377 }
Bill Wendling93efb222007-06-02 23:28:54 +00001378
Chris Lattner6c7416c2006-08-07 00:19:33 +00001379 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001380 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1381 StaticLoc.isValid(), isStar,
1382 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001383}
1384