blob: 9e1dd6e34bb45e3bd6e87512e795d431905ec3e9 [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 }
184 SkipUntil(tok::r_paren, false);
185 SkipUntil(tok::r_paren, false);
186 }
187 return CurrAttr;
188}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000189
Chris Lattner53361ac2006-08-10 05:19:57 +0000190/// ParseDeclaration - Parse a full 'declaration', which consists of
191/// declaration-specifiers, some number of declarators, and a semicolon.
192/// 'Context' should be a Declarator::TheContext value.
Chris Lattner302b4be2006-11-19 02:31:38 +0000193Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000194 // Parse the common declaration-specifiers piece.
195 DeclSpec DS;
196 ParseDeclarationSpecifiers(DS);
197
Chris Lattner0e894622006-08-13 19:58:17 +0000198 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
199 // declaration-specifiers init-declarator-list[opt] ';'
200 if (Tok.getKind() == tok::semi) {
Chris Lattner0e894622006-08-13 19:58:17 +0000201 ConsumeToken();
Chris Lattner200bdc32006-11-19 02:43:37 +0000202 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Chris Lattner0e894622006-08-13 19:58:17 +0000203 }
204
Chris Lattner53361ac2006-08-10 05:19:57 +0000205 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
206 ParseDeclarator(DeclaratorInfo);
207
Chris Lattner302b4be2006-11-19 02:31:38 +0000208 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner53361ac2006-08-10 05:19:57 +0000209}
210
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000211/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
212/// parsing 'declaration-specifiers declarator'. This method is split out this
213/// way to handle the ambiguity between top-level function-definitions and
214/// declarations.
215///
216/// declaration: [C99 6.7]
217/// declaration-specifiers init-declarator-list[opt] ';' [TODO]
218/// [!C99] init-declarator-list ';' [TODO]
219/// [OMP] threadprivate-directive [TODO]
220///
221/// init-declarator-list: [C99 6.7]
222/// init-declarator
223/// init-declarator-list ',' init-declarator
224/// init-declarator: [C99 6.7]
225/// declarator
226/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000227/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
228/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000229///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000230Parser::DeclTy *Parser::
231ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
232
233 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
234 // that they can be chained properly if the actions want this.
235 Parser::DeclTy *LastDeclInGroup = 0;
236
Chris Lattner53361ac2006-08-10 05:19:57 +0000237 // At this point, we know that it is not a function definition. Parse the
238 // rest of the init-declarator-list.
239 while (1) {
Chris Lattner6d7e6342006-08-15 03:41:14 +0000240 // If a simple-asm-expr is present, parse it.
241 if (Tok.getKind() == tok::kw_asm)
242 ParseSimpleAsm();
243
Steve Naroff0f2fe172007-06-01 17:11:19 +0000244 DeclTy *AttrList = 0;
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000245 // If attributes are present, parse them.
246 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000247 AttrList = 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 Lattner2dacc3f2006-10-16 00:33:54 +0000280 return LastDeclInGroup;
Chris Lattner53361ac2006-08-10 05:19:57 +0000281 } else {
282 Diag(Tok, diag::err_parse_error);
283 // Skip to end of block or statement
284 SkipUntil(tok::r_brace, true);
285 if (Tok.getKind() == tok::semi)
286 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000287 return 0;
Chris Lattner53361ac2006-08-10 05:19:57 +0000288 }
289}
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 Naroff45552922007-06-01 21:56:17 +0000400 DS.SetAttributeList(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 Naroff0f2fe172007-06-01 17:11:19 +0000522 DeclTy *AttrList = 0;
Chris Lattnere37e2332006-08-15 04:50:22 +0000523 // If attributes exist after tag, parse them.
524 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000525 AttrList = 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;
Chris Lattnerffbc2712007-01-25 06:05:38 +0000558 Decl = Actions.ParseTag(CurScope, TagType, TK, StartLoc, Name, NameLoc);
559 return false;
560}
561
562
563/// ParseStructUnionSpecifier
564/// struct-or-union-specifier: [C99 6.7.2.1]
565/// struct-or-union identifier[opt] '{' struct-contents '}'
566/// struct-or-union identifier
567/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
568/// '}' attributes[opt]
569/// [GNU] struct-or-union attributes[opt] identifier
570/// struct-or-union:
571/// 'struct'
572/// 'union'
573///
574void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
575 assert((Tok.getKind() == tok::kw_struct ||
576 Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
577 DeclSpec::TST TagType =
578 Tok.getKind() == tok::kw_union ? DeclSpec::TST_union : DeclSpec::TST_struct;
579 SourceLocation StartLoc = ConsumeToken();
580
581 // Parse the tag portion of this.
582 DeclTy *TagDecl;
583 if (ParseTag(TagDecl, TagType, StartLoc))
584 return;
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000585
Chris Lattner90a26b02007-01-23 04:38:16 +0000586 // If there is a body, parse it and inform the actions module.
587 if (Tok.getKind() == tok::l_brace)
Chris Lattner1300fb92007-01-23 23:42:53 +0000588 ParseStructUnionBody(StartLoc, TagType, TagDecl);
Chris Lattnerda72c822006-08-13 22:16:42 +0000589
590 const char *PrevSpec = 0;
Chris Lattnerb9d572a2007-01-23 04:58:34 +0000591 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000592 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000593}
594
595
Chris Lattner90a26b02007-01-23 04:38:16 +0000596/// ParseStructUnionBody
597/// struct-contents:
598/// struct-declaration-list
599/// [EXT] empty
600/// [GNU] "struct-declaration-list" without terminatoring ';' [TODO]
601/// struct-declaration-list:
602/// struct-declaration
603/// struct-declaration-list struct-declaration
604/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
605/// struct-declaration:
606/// specifier-qualifier-list struct-declarator-list ';'
607/// [GNU] __extension__ struct-declaration [TODO]
608/// [GNU] specifier-qualifier-list ';' [TODO]
609/// struct-declarator-list:
610/// struct-declarator
611/// struct-declarator-list ',' struct-declarator
612/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
613/// struct-declarator:
614/// declarator
615/// [GNU] declarator attributes[opt]
616/// declarator[opt] ':' constant-expression
617/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
618///
Chris Lattner1300fb92007-01-23 23:42:53 +0000619void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
620 unsigned TagType, DeclTy *TagDecl) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000621 SourceLocation LBraceLoc = ConsumeBrace();
622
Chris Lattner7b9ace62007-01-23 20:11:08 +0000623 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
624 // C++.
Chris Lattner90a26b02007-01-23 04:38:16 +0000625 if (Tok.getKind() == tok::r_brace)
626 Diag(Tok, diag::ext_empty_struct_union_enum,
627 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Chris Lattner7b9ace62007-01-23 20:11:08 +0000628
Chris Lattner1300fb92007-01-23 23:42:53 +0000629 SmallVector<DeclTy*, 32> FieldDecls;
630
Chris Lattner7b9ace62007-01-23 20:11:08 +0000631 // While we still have something to read, read the declarations in the struct.
Chris Lattner90a26b02007-01-23 04:38:16 +0000632 while (Tok.getKind() != tok::r_brace &&
633 Tok.getKind() != tok::eof) {
634 // Each iteration of this loop reads one struct-declaration.
635
636 // Parse the common specifier-qualifiers-list piece.
637 DeclSpec DS;
638 SourceLocation SpecQualLoc = Tok.getLocation();
639 ParseSpecifierQualifierList(DS);
640 // TODO: Does specifier-qualifier list correctly check that *something* is
641 // specified?
642
Chris Lattner90a26b02007-01-23 04:38:16 +0000643 // If there are no declarators, issue a warning.
644 if (Tok.getKind() == tok::semi) {
645 Diag(SpecQualLoc, diag::w_no_declarators);
Chris Lattner7b9ace62007-01-23 20:11:08 +0000646 ConsumeToken();
647 continue;
648 }
649
650 // Read struct-declarators until we find the semicolon.
651 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
652
653 while (1) {
654 /// struct-declarator: declarator
655 /// struct-declarator: declarator[opt] ':' constant-expression
656 if (Tok.getKind() != tok::colon)
657 ParseDeclarator(DeclaratorInfo);
658
659 ExprTy *BitfieldSize = 0;
660 if (Tok.getKind() == tok::colon) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000661 ConsumeToken();
Chris Lattner7b9ace62007-01-23 20:11:08 +0000662 ExprResult Res = ParseConstantExpression();
663 if (Res.isInvalid) {
664 SkipUntil(tok::semi, true, true);
665 } else {
666 BitfieldSize = Res.Val;
667 }
Chris Lattner90a26b02007-01-23 04:38:16 +0000668 }
Chris Lattner7b9ace62007-01-23 20:11:08 +0000669
Steve Naroff0f2fe172007-06-01 17:11:19 +0000670 DeclTy *AttrList = 0;
Chris Lattner7b9ace62007-01-23 20:11:08 +0000671 // If attributes exist after the declarator, parse them.
672 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000673 AttrList = ParseAttributes();
Chris Lattner7b9ace62007-01-23 20:11:08 +0000674
Chris Lattner367b0192007-01-23 22:29:13 +0000675 // Install the declarator into the current TagDecl.
Chris Lattner1300fb92007-01-23 23:42:53 +0000676 DeclTy *Field = Actions.ParseField(CurScope, TagDecl, SpecQualLoc,
677 DeclaratorInfo, BitfieldSize);
678 FieldDecls.push_back(Field);
Chris Lattner7b9ace62007-01-23 20:11:08 +0000679
680 // If we don't have a comma, it is either the end of the list (a ';')
681 // or an error, bail out.
682 if (Tok.getKind() != tok::comma)
683 break;
684
685 // Consume the comma.
686 ConsumeToken();
687
688 // Parse the next declarator.
689 DeclaratorInfo.clear();
690
691 // Attributes are only allowed on the second declarator.
692 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000693 AttrList = ParseAttributes();
Chris Lattner90a26b02007-01-23 04:38:16 +0000694 }
695
696 if (Tok.getKind() == tok::semi) {
697 ConsumeToken();
698 } else {
699 Diag(Tok, diag::err_expected_semi_decl_list);
700 // Skip to end of block or statement
701 SkipUntil(tok::r_brace, true, true);
702 }
703 }
704
705 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
706
Chris Lattnerc1915e22007-01-25 07:29:02 +0000707 Actions.ParseRecordBody(RecordLoc, TagDecl, &FieldDecls[0],FieldDecls.size());
708
Steve Naroff0f2fe172007-06-01 17:11:19 +0000709 DeclTy *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +0000710 // If attributes exist after struct contents, parse them.
711 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000712 AttrList = ParseAttributes(); // FIXME: where should I put them?
Chris Lattner90a26b02007-01-23 04:38:16 +0000713}
714
715
Chris Lattner3b561a32006-08-13 00:12:11 +0000716/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000717/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000718/// 'enum' identifier[opt] '{' enumerator-list '}'
719/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000720/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
721/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000722/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000723/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000724void Parser::ParseEnumSpecifier(DeclSpec &DS) {
725 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattnerb20e8942006-11-28 05:30:29 +0000726 SourceLocation StartLoc = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000727
Chris Lattnerffbc2712007-01-25 06:05:38 +0000728 // Parse the tag portion of this.
729 DeclTy *TagDecl;
730 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
Chris Lattner3b561a32006-08-13 00:12:11 +0000731 return;
Chris Lattner3b561a32006-08-13 00:12:11 +0000732
Chris Lattnerc1915e22007-01-25 07:29:02 +0000733 if (Tok.getKind() == tok::l_brace)
734 ParseEnumBody(StartLoc, TagDecl);
735
Chris Lattner3b561a32006-08-13 00:12:11 +0000736 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000737 const char *PrevSpec = 0;
Chris Lattnerffbc2712007-01-25 06:05:38 +0000738 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000739 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000740}
741
Chris Lattnerc1915e22007-01-25 07:29:02 +0000742/// ParseEnumBody - Parse a {} enclosed enumerator-list.
743/// enumerator-list:
744/// enumerator
745/// enumerator-list ',' enumerator
746/// enumerator:
747/// enumeration-constant
748/// enumeration-constant '=' constant-expression
749/// enumeration-constant:
750/// identifier
751///
752void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
753 SourceLocation LBraceLoc = ConsumeBrace();
754
755 if (Tok.getKind() == tok::r_brace)
756 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
757
758 SmallVector<DeclTy*, 32> EnumConstantDecls;
759
760 // Parse the enumerator-list.
761 while (Tok.getKind() == tok::identifier) {
762 IdentifierInfo *Ident = Tok.getIdentifierInfo();
763 SourceLocation IdentLoc = ConsumeToken();
764
765 SourceLocation EqualLoc;
766 ExprTy *AssignedVal = 0;
767 if (Tok.getKind() == tok::equal) {
768 EqualLoc = ConsumeToken();
769 ExprResult Res = ParseConstantExpression();
770 if (Res.isInvalid)
Chris Lattnerda6c2ce2007-04-27 19:13:15 +0000771 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000772 else
773 AssignedVal = Res.Val;
774 }
775
776 // Install the enumerator constant into EnumDecl.
777 DeclTy *ConstDecl = Actions.ParseEnumConstant(CurScope, EnumDecl,
778 IdentLoc, Ident,
779 EqualLoc, AssignedVal);
780 EnumConstantDecls.push_back(ConstDecl);
781
782 if (Tok.getKind() != tok::comma)
783 break;
784 SourceLocation CommaLoc = ConsumeToken();
785
786 if (Tok.getKind() != tok::identifier && !getLang().C99)
787 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
788 }
789
790 // Eat the }.
791 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
792
793 Actions.ParseEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
794 EnumConstantDecls.size());
795
Steve Naroff0f2fe172007-06-01 17:11:19 +0000796 DeclTy *AttrList = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000797 // If attributes exist after the identifier list, parse them.
798 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000799 AttrList = ParseAttributes(); // FIXME: where do they do?
Chris Lattnerc1915e22007-01-25 07:29:02 +0000800}
Chris Lattner3b561a32006-08-13 00:12:11 +0000801
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000802/// isTypeSpecifierQualifier - Return true if the current token could be the
803/// start of a specifier-qualifier-list.
804bool Parser::isTypeSpecifierQualifier() const {
805 switch (Tok.getKind()) {
806 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000807 // GNU attributes support.
808 case tok::kw___attribute:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000809 // type-specifiers
810 case tok::kw_short:
811 case tok::kw_long:
812 case tok::kw_signed:
813 case tok::kw_unsigned:
814 case tok::kw__Complex:
815 case tok::kw__Imaginary:
816 case tok::kw_void:
817 case tok::kw_char:
818 case tok::kw_int:
819 case tok::kw_float:
820 case tok::kw_double:
821 case tok::kw__Bool:
822 case tok::kw__Decimal32:
823 case tok::kw__Decimal64:
824 case tok::kw__Decimal128:
825
826 // struct-or-union-specifier
827 case tok::kw_struct:
828 case tok::kw_union:
829 // enum-specifier
830 case tok::kw_enum:
831
832 // type-qualifier
833 case tok::kw_const:
834 case tok::kw_volatile:
835 case tok::kw_restrict:
836 return true;
837
838 // typedef-name
839 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000840 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000841
842 // TODO: Attributes.
843 }
844}
845
Chris Lattneracd58a32006-08-06 17:24:14 +0000846/// isDeclarationSpecifier() - Return true if the current token is part of a
847/// declaration specifier.
848bool Parser::isDeclarationSpecifier() const {
849 switch (Tok.getKind()) {
850 default: return false;
851 // storage-class-specifier
852 case tok::kw_typedef:
853 case tok::kw_extern:
854 case tok::kw_static:
855 case tok::kw_auto:
856 case tok::kw_register:
857 case tok::kw___thread:
858
859 // type-specifiers
860 case tok::kw_short:
861 case tok::kw_long:
862 case tok::kw_signed:
863 case tok::kw_unsigned:
864 case tok::kw__Complex:
865 case tok::kw__Imaginary:
866 case tok::kw_void:
867 case tok::kw_char:
868 case tok::kw_int:
869 case tok::kw_float:
870 case tok::kw_double:
871 case tok::kw__Bool:
872 case tok::kw__Decimal32:
873 case tok::kw__Decimal64:
874 case tok::kw__Decimal128:
875
876 // struct-or-union-specifier
877 case tok::kw_struct:
878 case tok::kw_union:
879 // enum-specifier
880 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000881
Chris Lattneracd58a32006-08-06 17:24:14 +0000882 // type-qualifier
883 case tok::kw_const:
884 case tok::kw_volatile:
885 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000886
Chris Lattneracd58a32006-08-06 17:24:14 +0000887 // function-specifier
888 case tok::kw_inline:
889 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000890
Chris Lattneracd58a32006-08-06 17:24:14 +0000891 // typedef-name
892 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000893 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +0000894 // TODO: Attributes.
895 }
896}
897
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000898
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000899/// ParseTypeQualifierListOpt
900/// type-qualifier-list: [C99 6.7.5]
901/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000902/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000903/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000904/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000905///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000906void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000907 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000908 int isInvalid = false;
909 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +0000910 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000911
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000912 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000913 default:
Chris Lattnere37e2332006-08-15 04:50:22 +0000914 // If this is not a type-qualifier token, we're done reading type
915 // qualifiers. First verify that DeclSpec's are consistent.
Chris Lattnerb20e8942006-11-28 05:30:29 +0000916 DS.Finish(Diags, getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000917 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000918 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000919 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
920 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000921 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000922 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000923 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
924 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000925 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000926 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000927 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
928 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000929 break;
Chris Lattnere37e2332006-08-15 04:50:22 +0000930
931 case tok::kw___attribute:
932 ParseAttributes();
933 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000934 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000935
936 // If the specifier combination wasn't legal, issue a diagnostic.
937 if (isInvalid) {
938 assert(PrevSpec && "Method did not return previous specifier!");
939 if (isInvalid == 1) // Error.
940 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
941 else // extwarn.
942 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
943 }
944 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000945 }
946}
947
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000948
949/// ParseDeclarator - Parse and verify a newly-initialized declarator.
950///
951void Parser::ParseDeclarator(Declarator &D) {
952 /// This implements the 'declarator' production in the C grammar, then checks
953 /// for well-formedness and issues diagnostics.
954 ParseDeclaratorInternal(D);
955
Chris Lattner9fab3b92006-08-12 18:25:42 +0000956 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000957
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000958}
959
960/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000961/// declarator: [C99 6.7.5]
962/// pointer[opt] direct-declarator
Bill Wendling3708c182007-05-27 10:15:43 +0000963/// [C++] reference direct-declarator [C++ 8p4, dcl.decl]
Chris Lattner6c7416c2006-08-07 00:19:33 +0000964///
965/// pointer: [C99 6.7.5]
966/// '*' type-qualifier-list[opt]
967/// '*' type-qualifier-list[opt] pointer
968///
Bill Wendling3708c182007-05-27 10:15:43 +0000969/// reference: [C++ 8p4, dcl.decl]
970/// [C++] '&' declarator
971///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000972void Parser::ParseDeclaratorInternal(Declarator &D) {
Bill Wendling3708c182007-05-27 10:15:43 +0000973 tok::TokenKind Kind = Tok.getKind();
974
975 // Not a pointer or C++ reference.
976 if (Kind != tok::star && !(Kind == tok::amp && getLang().CPlusPlus))
Chris Lattner6c7416c2006-08-07 00:19:33 +0000977 return ParseDirectDeclarator(D);
978
Bill Wendling3708c182007-05-27 10:15:43 +0000979 // Otherwise, '*' -> pointer or '&' -> reference.
980 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
981
982 if (Kind == tok::star) {
983 // Is a pointer
984 DeclSpec DS;
985 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
994
995 // Recursively parse the declarator.
996 ParseDeclaratorInternal(D);
997
998 // Remember that we parsed a reference type. It doesn't have type-quals.
999 D.AddTypeInfo(DeclaratorChunk::getReference(Loc));
1000 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001001}
1002
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001003/// ParseDirectDeclarator
1004/// direct-declarator: [C99 6.7.5]
1005/// identifier
1006/// '(' declarator ')'
1007/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00001008/// [C90] direct-declarator '[' constant-expression[opt] ']'
1009/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1010/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1011/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1012/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001013/// direct-declarator '(' parameter-type-list ')'
1014/// direct-declarator '(' identifier-list[opt] ')'
1015/// [GNU] direct-declarator '(' parameter-forward-declarations
1016/// parameter-type-list[opt] ')'
1017///
Chris Lattneracd58a32006-08-06 17:24:14 +00001018void Parser::ParseDirectDeclarator(Declarator &D) {
1019 // Parse the first direct-declarator seen.
1020 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
1021 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1022 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1023 ConsumeToken();
1024 } else if (Tok.getKind() == tok::l_paren) {
1025 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001026 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001027 // Example: 'char (*X)' or 'int (*XX)(void)'
1028 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001029 } else if (D.mayOmitIdentifier()) {
1030 // This could be something simple like "int" (in which case the declarator
1031 // portion is empty), if an abstract-declarator is allowed.
1032 D.SetIdentifier(0, Tok.getLocation());
1033 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +00001034 // Expected identifier or '('.
1035 Diag(Tok, diag::err_expected_ident_lparen);
1036 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +00001037 }
1038
1039 assert(D.isPastIdentifier() &&
1040 "Haven't past the location of the identifier yet?");
1041
1042 while (1) {
1043 if (Tok.getKind() == tok::l_paren) {
1044 ParseParenDeclarator(D);
1045 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001046 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001047 } else {
1048 break;
1049 }
1050 }
1051}
1052
1053/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
1054/// either be before the identifier (in which case these are just grouping
1055/// parens for precedence) or it may be after the identifier, in which case
1056/// these are function arguments.
1057///
1058/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001059/// parameter-type-list: [C99 6.7.5]
1060/// parameter-list
1061/// parameter-list ',' '...'
1062///
1063/// parameter-list: [C99 6.7.5]
1064/// parameter-declaration
1065/// parameter-list ',' parameter-declaration
1066///
1067/// parameter-declaration: [C99 6.7.5]
1068/// declaration-specifiers declarator
Chris Lattnere37e2332006-08-15 04:50:22 +00001069/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001070/// declaration-specifiers abstract-declarator[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001071/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001072///
1073/// identifier-list: [C99 6.7.5]
1074/// identifier
1075/// identifier-list ',' identifier
1076///
Chris Lattneracd58a32006-08-06 17:24:14 +00001077void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001078 SourceLocation StartLoc = ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001079
Chris Lattneracd58a32006-08-06 17:24:14 +00001080 // If we haven't past the identifier yet (or where the identifier would be
1081 // stored, if this is an abstract declarator), then this is probably just
1082 // grouping parens.
1083 if (!D.isPastIdentifier()) {
1084 // Okay, this is probably a grouping paren. However, if this could be an
1085 // abstract-declarator, then this could also be the start of function
1086 // arguments (consider 'void()').
1087 bool isGrouping;
1088
1089 if (!D.mayOmitIdentifier()) {
1090 // If this can't be an abstract-declarator, this *must* be a grouping
1091 // paren, because we haven't seen the identifier yet.
1092 isGrouping = true;
1093 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
1094 isDeclarationSpecifier()) { // 'int(int)' is a function.
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001095 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1096 // considered to be a type, not a K&R identifier-list.
Chris Lattneracd58a32006-08-06 17:24:14 +00001097 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001098 } else {
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001099 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +00001100 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001101 }
Chris Lattneracd58a32006-08-06 17:24:14 +00001102
1103 // If this is a grouping paren, handle:
1104 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001105 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001106 if (isGrouping) {
Steve Naroff0f2fe172007-06-01 17:11:19 +00001107 DeclTy *AttrList = 0;
Chris Lattnere37e2332006-08-15 04:50:22 +00001108 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +00001109 AttrList = ParseAttributes();
Chris Lattnere37e2332006-08-15 04:50:22 +00001110
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001111 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +00001112 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +00001113 MatchRHSPunctuation(tok::r_paren, StartLoc);
Chris Lattneracd58a32006-08-06 17:24:14 +00001114 return;
1115 }
1116
1117 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +00001118 // argument list. Recognize that this declarator will never have an
1119 // identifier (and remember where it would have been), then fall through to
1120 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +00001121 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001122 }
1123
Chris Lattneracd58a32006-08-06 17:24:14 +00001124 // Okay, this is the parameter list of a function definition, or it is an
1125 // identifier list of a K&R-style function.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001126 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +00001127 bool HasPrototype;
Chris Lattner14776b92006-08-06 22:27:40 +00001128 bool ErrorEmitted = false;
1129
Chris Lattneredc9e392006-12-02 06:21:46 +00001130 // Build up an array of information about the parsed arguments.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001131 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattnerad9ac942007-01-23 01:14:52 +00001132 SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Chris Lattneredc9e392006-12-02 06:21:46 +00001133
Chris Lattneracd58a32006-08-06 17:24:14 +00001134 if (Tok.getKind() == tok::r_paren) {
1135 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001136 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001137 HasPrototype = false;
1138 } else if (Tok.getKind() == tok::identifier &&
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001139 // K&R identifier lists can't have typedefs as identifiers, per
1140 // C99 6.7.5.3p11.
Steve Naroffb419d3a2006-10-27 23:18:49 +00001141 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001142 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1143 // normal declarators, not for abstract-declarators.
1144 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
1145
1146 // If there was no identifier specified, either we are in an
1147 // abstract-declarator, or we are in a parameter declarator which was found
1148 // to be abstract. In abstract-declarators, identifier lists are not valid,
1149 // diagnose this.
1150 if (!D.getIdentifier())
1151 Diag(Tok, diag::ext_ident_list_in_param);
Chris Lattneredc9e392006-12-02 06:21:46 +00001152
Chris Lattnercbc426d2006-12-02 06:43:02 +00001153 // Remember this identifier in ParamInfo.
1154 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1155 Tok.getLocation(), 0));
1156
Chris Lattneracd58a32006-08-06 17:24:14 +00001157 ConsumeToken();
1158 while (Tok.getKind() == tok::comma) {
1159 // Eat the comma.
1160 ConsumeToken();
1161
Chris Lattnercbc426d2006-12-02 06:43:02 +00001162 if (Tok.getKind() != tok::identifier) {
1163 Diag(Tok, diag::err_expected_ident);
Chris Lattner14776b92006-08-06 22:27:40 +00001164 ErrorEmitted = true;
1165 break;
1166 }
Chris Lattnercbc426d2006-12-02 06:43:02 +00001167
Chris Lattner969ca152006-12-03 06:29:03 +00001168 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
1169
1170 // Verify that the argument identifier has not already been mentioned.
Chris Lattnerbaf33662007-01-27 02:14:08 +00001171 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerad9ac942007-01-23 01:14:52 +00001172 Diag(Tok.getLocation(), diag::err_param_redefinition,ParmII->getName());
1173 ParmII = 0;
1174 }
Chris Lattner969ca152006-12-03 06:29:03 +00001175
Chris Lattnercbc426d2006-12-02 06:43:02 +00001176 // Remember this identifier in ParamInfo.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001177 if (ParmII)
1178 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1179 Tok.getLocation(), 0));
Chris Lattnercbc426d2006-12-02 06:43:02 +00001180
1181 // Eat the identifier.
1182 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00001183 }
1184
Chris Lattneracd58a32006-08-06 17:24:14 +00001185 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001186 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001187 HasPrototype = false;
1188 } else {
Chris Lattner43e956c2006-11-28 04:05:37 +00001189 // Finally, a normal, non-empty parameter type list.
1190
Chris Lattnercbc426d2006-12-02 06:43:02 +00001191 // Enter function-declaration scope, limiting any declarators for struct
1192 // tags to the function prototype scope.
1193 // FIXME: is this needed?
Chris Lattner43e956c2006-11-28 04:05:37 +00001194 EnterScope(0);
1195
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001196 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001197 while (1) {
1198 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001199 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +00001200
1201 // Check to see if this is "void(...)" which is not allowed.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001202 if (ParamInfo.empty()) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001203 // Otherwise, parse parameter type list. If it starts with an
1204 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +00001205 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001206 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +00001207 }
1208
1209 // Consume the ellipsis.
1210 ConsumeToken();
1211 break;
1212 }
1213
Chris Lattneracd58a32006-08-06 17:24:14 +00001214 // Parse the declaration-specifiers.
1215 DeclSpec DS;
1216 ParseDeclarationSpecifiers(DS);
1217
1218 // Parse the declarator. This is "PrototypeContext", because we must
1219 // accept either 'declarator' or 'abstract-declarator' here.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001220 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1221 ParseDeclarator(ParmDecl);
Chris Lattneracd58a32006-08-06 17:24:14 +00001222
Chris Lattnere37e2332006-08-15 04:50:22 +00001223 // Parse GNU attributes, if present.
Steve Naroff0f2fe172007-06-01 17:11:19 +00001224 DeclTy *AttrList = 0;
Chris Lattnere37e2332006-08-15 04:50:22 +00001225 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +00001226 AttrList = ParseAttributes();
Chris Lattnere37e2332006-08-15 04:50:22 +00001227
Chris Lattner43e956c2006-11-28 04:05:37 +00001228 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001229 // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted.
1230 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1231 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
Chris Lattner4d8f8732006-11-28 05:05:08 +00001232 Diag(DS.getStorageClassSpecLoc(),
Chris Lattner43e956c2006-11-28 04:05:37 +00001233 diag::err_invalid_storage_class_in_func_decl);
Chris Lattner353f5742006-11-28 04:50:12 +00001234 DS.ClearStorageClassSpecs();
Chris Lattner43e956c2006-11-28 04:05:37 +00001235 }
Chris Lattner4d8f8732006-11-28 05:05:08 +00001236 if (DS.isThreadSpecified()) {
1237 Diag(DS.getThreadSpecLoc(),
1238 diag::err_invalid_storage_class_in_func_decl);
1239 DS.ClearStorageClassSpecs();
1240 }
Chris Lattner43e956c2006-11-28 04:05:37 +00001241
1242 // Inform the actions module about the parameter declarator, so it gets
1243 // added to the current scope.
Chris Lattner216d8652006-12-02 06:47:41 +00001244 Action::TypeResult ParamTy =
1245 Actions.ParseParamDeclaratorType(CurScope, ParmDecl);
Chris Lattnercbc426d2006-12-02 06:43:02 +00001246
1247 // Remember this parsed parameter in ParamInfo.
Chris Lattner969ca152006-12-03 06:29:03 +00001248 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1249
1250 // Verify that the argument identifier has not already been mentioned.
Chris Lattnerbaf33662007-01-27 02:14:08 +00001251 if (ParmII && !ParamsSoFar.insert(ParmII)) {
Chris Lattnerad9ac942007-01-23 01:14:52 +00001252 Diag(ParmDecl.getIdentifierLoc(), diag::err_param_redefinition,
1253 ParmII->getName());
1254 ParmII = 0;
Chris Lattner969ca152006-12-03 06:29:03 +00001255 }
1256
1257 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnercbc426d2006-12-02 06:43:02 +00001258 ParmDecl.getIdentifierLoc(),
1259 ParamTy.Val));
Chris Lattneracd58a32006-08-06 17:24:14 +00001260
1261 // If the next token is a comma, consume it and keep reading arguments.
1262 if (Tok.getKind() != tok::comma) break;
1263
1264 // Consume the comma.
1265 ConsumeToken();
1266 }
1267
1268 HasPrototype = true;
Chris Lattner43e956c2006-11-28 04:05:37 +00001269
1270 // Leave prototype scope.
1271 ExitScope();
Chris Lattneracd58a32006-08-06 17:24:14 +00001272 }
1273
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001274 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerd2e97c12006-12-03 02:03:33 +00001275 if (!ErrorEmitted)
1276 D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic,
1277 &ParamInfo[0], ParamInfo.size(),
1278 StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001279
Chris Lattner14776b92006-08-06 22:27:40 +00001280 // If we have the closing ')', eat it and we're done.
1281 if (Tok.getKind() == tok::r_paren) {
1282 ConsumeParen();
1283 } else {
1284 // If an error happened earlier parsing something else in the proto, don't
1285 // issue another error.
1286 if (!ErrorEmitted)
1287 Diag(Tok, diag::err_expected_rparen);
1288 SkipUntil(tok::r_paren);
1289 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001290}
Chris Lattneracd58a32006-08-06 17:24:14 +00001291
Chris Lattnere8074e62006-08-06 18:30:15 +00001292
1293/// [C90] direct-declarator '[' constant-expression[opt] ']'
1294/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1295/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1296/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1297/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1298void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001299 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001300
1301 // If valid, this location is the position where we read the 'static' keyword.
1302 SourceLocation StaticLoc;
Chris Lattneraf635312006-10-16 06:06:51 +00001303 if (Tok.getKind() == tok::kw_static)
1304 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001305
1306 // If there is a type-qualifier-list, read it now.
1307 DeclSpec DS;
1308 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001309
1310 // If we haven't already read 'static', check to see if there is one after the
1311 // type-qualifier-list.
Chris Lattneraf635312006-10-16 06:06:51 +00001312 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static)
1313 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001314
1315 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001316 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001317 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +00001318 if (Tok.getKind() == tok::star) {
1319 // Remember the '*' token, in case we have to un-get it.
1320 LexerToken StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +00001321 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +00001322
1323 // Check that the ']' token is present to avoid incorrectly parsing
1324 // expressions starting with '*' as [*].
1325 if (Tok.getKind() == tok::r_square) {
1326 if (StaticLoc.isValid())
1327 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1328 StaticLoc = SourceLocation(); // Drop the static.
1329 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +00001330 } else {
1331 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +00001332 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +00001333 // need to reparse it. This handles cases like 'X[*p + 4]'
1334 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +00001335 }
Chris Lattner9fab3b92006-08-12 18:25:42 +00001336 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001337 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001338 NumElements = ParseAssignmentExpression();
1339 }
1340
1341 // If there was an error parsing the assignment-expression, recover.
1342 if (NumElements.isInvalid) {
1343 // If the expression was invalid, skip it.
1344 SkipUntil(tok::r_square);
1345 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001346 }
1347
Chris Lattner04f80192006-08-15 04:55:54 +00001348 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001349
Chris Lattnere8074e62006-08-06 18:30:15 +00001350 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1351 // it was not a constant expression.
1352 if (!getLang().C99) {
1353 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001354 if (isStar || StaticLoc.isValid() ||
1355 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001356 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001357 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001358
1359 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001360 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1361 StaticLoc.isValid(), isStar,
1362 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001363}
1364