blob: c40e7496592643cd9db44499f239f2f3d4be39b2 [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 clang;
18
19//===----------------------------------------------------------------------===//
20// C99 6.7: Declarations.
21//===----------------------------------------------------------------------===//
22
Chris Lattnerf5fbd792006-08-10 23:56:11 +000023/// ParseTypeName
24/// type-name: [C99 6.7.6]
25/// specifier-qualifier-list abstract-declarator[opt]
Chris Lattnere550a4e2006-08-24 06:37:51 +000026Parser::TypeTy *Parser::ParseTypeName() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000027 // Parse the common declaration-specifiers piece.
28 DeclSpec DS;
Chris Lattner1890ac82006-08-13 01:16:23 +000029 ParseSpecifierQualifierList(DS);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000030
31 // Parse the abstract-declarator, if present.
32 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
33 ParseDeclarator(DeclaratorInfo);
Chris Lattnere550a4e2006-08-24 06:37:51 +000034
Chris Lattner558cb292006-11-19 01:31:06 +000035 return Actions.ParseTypeName(CurScope, DeclaratorInfo).Val;
Chris Lattnerf5fbd792006-08-10 23:56:11 +000036}
37
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000038/// ParseAttributes - Parse a non-empty attributes list.
39///
40/// [GNU] attributes:
41/// attribute
42/// attributes attribute
43///
44/// [GNU] attribute:
45/// '__attribute__' '(' '(' attribute-list ')' ')'
46///
47/// [GNU] attribute-list:
48/// attrib
49/// attribute_list ',' attrib
50///
51/// [GNU] attrib:
52/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000053/// attrib-name
54/// attrib-name '(' identifier ')'
55/// attrib-name '(' identifier ',' nonempty-expr-list ')'
56/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000057///
Steve Naroff0f2fe172007-06-01 17:11:19 +000058/// [GNU] attrib-name:
59/// identifier
60/// typespec
61/// typequal
62/// storageclass
63///
64/// FIXME: The GCC grammar/code for this construct implies we need two
65/// token lookahead. Comment from gcc: "If they start with an identifier
66/// which is followed by a comma or close parenthesis, then the arguments
67/// start with that identifier; otherwise they are an expression list."
68///
69/// At the moment, I am not doing 2 token lookahead. I am also unaware of
70/// any attributes that don't work (based on my limited testing). Most
71/// attributes are very simple in practice. Until we find a bug, I don't see
72/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000073
Steve Naroffb8371e12007-06-09 03:39:29 +000074AttributeList *Parser::ParseAttributes() {
Steve Naroff0f2fe172007-06-01 17:11:19 +000075 assert(Tok.getKind() == tok::kw___attribute && "Not an attribute list!");
76
Steve Naroffb8371e12007-06-09 03:39:29 +000077 AttributeList *CurrAttr = 0;
Steve Naroff0f2fe172007-06-01 17:11:19 +000078
79 while (Tok.getKind() == tok::kw___attribute) {
80 ConsumeToken();
81 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
82 "attribute")) {
83 SkipUntil(tok::r_paren, true); // skip until ) or ;
84 return CurrAttr;
85 }
86 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
87 SkipUntil(tok::r_paren, true); // skip until ) or ;
88 return CurrAttr;
89 }
90 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
91 while (Tok.getKind() == tok::identifier || isDeclarationSpecifier() ||
92 Tok.getKind() == tok::comma) {
93
94 if (Tok.getKind() == tok::comma) {
95 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
96 ConsumeToken();
97 continue;
98 }
99 // we have an identifier or declaration specifier (const, int, etc.)
100 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
101 SourceLocation AttrNameLoc = ConsumeToken();
Steve Naroff0f2fe172007-06-01 17:11:19 +0000102
103 // check if we have a "paramterized" attribute
104 if (Tok.getKind() == tok::l_paren) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000105 ConsumeParen(); // ignore the left paren loc for now
Steve Naroff0f2fe172007-06-01 17:11:19 +0000106
107 if (Tok.getKind() == tok::identifier) {
108 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
109 SourceLocation ParmLoc = ConsumeToken();
110
111 if (Tok.getKind() == tok::r_paren) {
112 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000113 ConsumeParen(); // ignore the right paren loc for now
114 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
115 ParmName, ParmLoc, 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000116 } else if (Tok.getKind() == tok::comma) {
117 ConsumeToken();
118 // __attribute__(( format(printf, 1, 2) ))
Chris Lattner23b7eb62007-06-15 23:05:46 +0000119 llvm::SmallVector<ExprTy*, 8> ArgExprs;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000120 bool ArgExprsOk = true;
121
122 // now parse the non-empty comma separated list of expressions
123 while (1) {
124 ExprResult ArgExpr = ParseAssignmentExpression();
125 if (ArgExpr.isInvalid) {
126 ArgExprsOk = false;
127 SkipUntil(tok::r_paren);
128 break;
129 } else {
130 ArgExprs.push_back(ArgExpr.Val);
131 }
132 if (Tok.getKind() != tok::comma)
133 break;
134 ConsumeToken(); // Eat the comma, move to the next argument
135 }
136 if (ArgExprsOk && Tok.getKind() == tok::r_paren) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000137 ConsumeParen(); // ignore the right paren loc for now
138 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
139 ParmLoc, &ArgExprs[0], ArgExprs.size(), CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000140 }
141 }
142 } else { // not an identifier
143 // parse a possibly empty comma separated list of expressions
144 if (Tok.getKind() == tok::r_paren) {
145 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000146 ConsumeParen(); // ignore the right paren loc for now
147 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
148 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000149 } else {
150 // __attribute__(( aligned(16) ))
Chris Lattner23b7eb62007-06-15 23:05:46 +0000151 llvm::SmallVector<ExprTy*, 8> ArgExprs;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000152 bool ArgExprsOk = true;
153
154 // now parse the list of expressions
155 while (1) {
156 ExprResult ArgExpr = ParseAssignmentExpression();
157 if (ArgExpr.isInvalid) {
158 ArgExprsOk = false;
159 SkipUntil(tok::r_paren);
160 break;
161 } else {
162 ArgExprs.push_back(ArgExpr.Val);
163 }
164 if (Tok.getKind() != tok::comma)
165 break;
166 ConsumeToken(); // Eat the comma, move to the next argument
167 }
168 // Match the ')'.
169 if (ArgExprsOk && Tok.getKind() == tok::r_paren) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000170 ConsumeParen(); // ignore the right paren loc for now
171 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
172 SourceLocation(), &ArgExprs[0], ArgExprs.size(),
173 CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000174 }
175 }
176 }
177 } else {
Steve Naroffb8371e12007-06-09 03:39:29 +0000178 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
179 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000180 }
181 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000182 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
183 SkipUntil(tok::r_paren, false);
184 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
185 SkipUntil(tok::r_paren, false);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000186 }
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
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000244 // If attributes are present, parse them.
245 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000246 D.AddAttributes(ParseAttributes());
Chris Lattner6d7e6342006-08-15 03:41:14 +0000247
Chris Lattner53361ac2006-08-10 05:19:57 +0000248 // Parse declarator '=' initializer.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000249 ExprResult Init;
Chris Lattner53361ac2006-08-10 05:19:57 +0000250 if (Tok.getKind() == tok::equal) {
251 ConsumeToken();
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000252 Init = ParseInitializer();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000253 if (Init.isInvalid) {
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000254 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000255 return 0;
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000256 }
Chris Lattner53361ac2006-08-10 05:19:57 +0000257 }
258
Chris Lattner697e5d62006-11-09 06:32:27 +0000259 // Inform the current actions module that we just parsed this declarator.
Chris Lattner289ab7b2006-11-08 06:54:53 +0000260 // FIXME: pass asm & attributes.
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000261 LastDeclInGroup = Actions.ParseDeclarator(CurScope, D, Init.Val,
262 LastDeclInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +0000263
264 // If we don't have a comma, it is either the end of the list (a ';') or an
265 // error, bail out.
266 if (Tok.getKind() != tok::comma)
267 break;
268
269 // Consume the comma.
270 ConsumeToken();
271
272 // Parse the next declarator.
273 D.clear();
274 ParseDeclarator(D);
275 }
276
277 if (Tok.getKind() == tok::semi) {
278 ConsumeToken();
Chris Lattner776fac82007-06-09 00:53:06 +0000279 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +0000280 }
Chris Lattner776fac82007-06-09 00:53:06 +0000281
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();
287 return 0;
Chris Lattner53361ac2006-08-10 05:19:57 +0000288}
289
Chris Lattner1890ac82006-08-13 01:16:23 +0000290/// ParseSpecifierQualifierList
291/// specifier-qualifier-list:
292/// type-specifier specifier-qualifier-list[opt]
293/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000294/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000295///
296void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
297 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
298 /// parse declaration-specifiers and complain about extra stuff.
299 SourceLocation Loc = Tok.getLocation();
300 ParseDeclarationSpecifiers(DS);
301
302 // Validate declspec for type-name.
303 unsigned Specs = DS.getParsedSpecifiers();
304 if (Specs == DeclSpec::PQ_None)
305 Diag(Tok, diag::err_typename_requires_specqual);
306
Chris Lattner1b22eed2006-11-28 05:12:07 +0000307 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000308 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000309 if (DS.getStorageClassSpecLoc().isValid())
310 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
311 else
312 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000313 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000314 }
Chris Lattner1b22eed2006-11-28 05:12:07 +0000315
316 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000317 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000318 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000319 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000320 }
321}
Chris Lattner53361ac2006-08-10 05:19:57 +0000322
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000323/// ParseDeclarationSpecifiers
324/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000325/// storage-class-specifier declaration-specifiers[opt]
326/// type-specifier declaration-specifiers[opt]
327/// type-qualifier declaration-specifiers[opt]
328/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000329/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000330///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000331/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000332/// 'typedef'
333/// 'extern'
334/// 'static'
335/// 'auto'
336/// 'register'
337/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000338/// type-specifier: [C99 6.7.2]
339/// 'void'
340/// 'char'
341/// 'short'
342/// 'int'
343/// 'long'
344/// 'float'
345/// 'double'
346/// 'signed'
347/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000348/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000349/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000350/// typedef-name
Bill Wendling4073ed52007-02-13 01:51:42 +0000351/// [C++] 'bool'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000352/// [C99] '_Bool'
353/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000354/// [C99] '_Imaginary' // Removed in TC2?
355/// [GNU] '_Decimal32'
356/// [GNU] '_Decimal64'
357/// [GNU] '_Decimal128'
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000358/// [GNU] typeof-specifier [TODO]
Chris Lattner3b561a32006-08-13 00:12:11 +0000359/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000360/// [OBJC] typedef-name objc-protocol-refs [TODO]
361/// [OBJC] objc-protocol-refs [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000362/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000363/// 'const'
364/// 'volatile'
365/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000366/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000367/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000368///
369void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000370 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000371 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000372 const char *PrevSpec = 0;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000373 SourceLocation Loc = Tok.getLocation();
374
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000375 switch (Tok.getKind()) {
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000376 // typedef-name
377 case tok::identifier:
378 // This identifier can only be a typedef name if we haven't already seen
Chris Lattner5646b3e2006-08-15 05:12:01 +0000379 // a type-specifier. Without this check we misparse:
380 // typedef int X; struct Y { short X; }; as 'short int'.
Chris Lattnerf055d432006-11-28 04:28:12 +0000381 if (!DS.hasTypeSpecifier()) {
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000382 // It has to be available as a typedef too!
383 if (void *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(),
384 CurScope)) {
Chris Lattnerb20e8942006-11-28 05:30:29 +0000385 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000386 TypeRep);
Chris Lattneredc9e392006-12-02 06:21:46 +0000387 break;
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000388 }
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000389 }
390 // FALL THROUGH.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000391 default:
392 // If this is not a declaration specifier token, we're done reading decl
393 // specifiers. First verify that DeclSpec's are consistent.
Chris Lattnerb20e8942006-11-28 05:30:29 +0000394 DS.Finish(Diags, getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000395 return;
Chris Lattnere37e2332006-08-15 04:50:22 +0000396
397 // GNU attributes support.
398 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000399 DS.AddAttributes(ParseAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +0000400 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000401
402 // storage-class-specifier
403 case tok::kw_typedef:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000404 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000405 break;
406 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000407 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000408 Diag(Tok, diag::ext_thread_before, "extern");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000409 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000410 break;
411 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000412 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000413 Diag(Tok, diag::ext_thread_before, "static");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000414 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000415 break;
416 case tok::kw_auto:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000417 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000418 break;
419 case tok::kw_register:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000420 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000421 break;
422 case tok::kw___thread:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000423 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000424 break;
425
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000426 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000427 case tok::kw_short:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000428 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000429 break;
430 case tok::kw_long:
Chris Lattner353f5742006-11-28 04:50:12 +0000431 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
Chris Lattnerb20e8942006-11-28 05:30:29 +0000432 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
Chris Lattner353f5742006-11-28 04:50:12 +0000433 else
Chris Lattnerb20e8942006-11-28 05:30:29 +0000434 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000435 break;
436 case tok::kw_signed:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000437 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000438 break;
439 case tok::kw_unsigned:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000440 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000441 break;
442 case tok::kw__Complex:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000443 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000444 break;
445 case tok::kw__Imaginary:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000446 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000447 break;
448 case tok::kw_void:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000449 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000450 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000451 case tok::kw_char:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000452 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000453 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000454 case tok::kw_int:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000455 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000456 break;
457 case tok::kw_float:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000458 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000459 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000460 case tok::kw_double:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000461 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000462 break;
Bill Wendling4073ed52007-02-13 01:51:42 +0000463 case tok::kw_bool: // [C++ 2.11p1]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000464 case tok::kw__Bool:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000465 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000466 break;
467 case tok::kw__Decimal32:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000468 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000469 break;
470 case tok::kw__Decimal64:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000471 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000472 break;
473 case tok::kw__Decimal128:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000474 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000475 break;
476
Chris Lattner1890ac82006-08-13 01:16:23 +0000477 case tok::kw_struct:
478 case tok::kw_union:
479 ParseStructUnionSpecifier(DS);
480 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000481 case tok::kw_enum:
482 ParseEnumSpecifier(DS);
483 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000484
485 // type-qualifier
486 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000487 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
488 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000489 break;
490 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000491 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
492 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000493 break;
494 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000495 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
496 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000497 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000498
499 // function-specifier
500 case tok::kw_inline:
Chris Lattner1b22eed2006-11-28 05:12:07 +0000501 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000502 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000503 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000504 // If the specifier combination wasn't legal, issue a diagnostic.
505 if (isInvalid) {
506 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000507 if (isInvalid == 1) // Error.
508 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
509 else // extwarn.
510 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000511 }
512 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000513 }
514}
515
Chris Lattnerffbc2712007-01-25 06:05:38 +0000516/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
517/// the first token has already been read and has been turned into an instance
518/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
519/// otherwise it returns false and fills in Decl.
520bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
Steve Naroffb8371e12007-06-09 03:39:29 +0000521 AttributeList *Attr = 0;
Chris Lattnere37e2332006-08-15 04:50:22 +0000522 // If attributes exist after tag, parse them.
523 if (Tok.getKind() == tok::kw___attribute)
Steve Naroffb8371e12007-06-09 03:39:29 +0000524 Attr = ParseAttributes();
Chris Lattnerffbc2712007-01-25 06:05:38 +0000525
Chris Lattner1890ac82006-08-13 01:16:23 +0000526 // Must have either 'struct name' or 'struct {...}'.
527 if (Tok.getKind() != tok::identifier &&
528 Tok.getKind() != tok::l_brace) {
529 Diag(Tok, diag::err_expected_ident_lbrace);
Chris Lattner8c6519a2007-01-22 07:41:36 +0000530 // TODO: better error recovery here.
Chris Lattnerffbc2712007-01-25 06:05:38 +0000531 return true;
Chris Lattner1890ac82006-08-13 01:16:23 +0000532 }
533
Chris Lattner8c6519a2007-01-22 07:41:36 +0000534 // If an identifier is present, consume and remember it.
535 IdentifierInfo *Name = 0;
536 SourceLocation NameLoc;
537 if (Tok.getKind() == tok::identifier) {
538 Name = Tok.getIdentifierInfo();
539 NameLoc = ConsumeToken();
540 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000541
Chris Lattner8c6519a2007-01-22 07:41:36 +0000542 // There are three options here. If we have 'struct foo;', then this is a
543 // forward declaration. If we have 'struct foo {...' then this is a
Chris Lattner7b9ace62007-01-23 20:11:08 +0000544 // definition. Otherwise we have something like 'struct foo xyz', a reference.
Chris Lattner8799cf22007-01-23 01:57:16 +0000545 //
546 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
547 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
548 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
549 //
Chris Lattner7b9ace62007-01-23 20:11:08 +0000550 Action::TagKind TK;
551 if (Tok.getKind() == tok::l_brace)
552 TK = Action::TK_Definition;
553 else if (Tok.getKind() == tok::semi)
554 TK = Action::TK_Declaration;
555 else
556 TK = Action::TK_Reference;
Steve Naroffb8371e12007-06-09 03:39:29 +0000557 Decl = Actions.ParseTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Chris Lattnerffbc2712007-01-25 06:05:38 +0000558 return false;
559}
560
561
562/// ParseStructUnionSpecifier
563/// struct-or-union-specifier: [C99 6.7.2.1]
564/// struct-or-union identifier[opt] '{' struct-contents '}'
565/// struct-or-union identifier
566/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
567/// '}' attributes[opt]
568/// [GNU] struct-or-union attributes[opt] identifier
569/// struct-or-union:
570/// 'struct'
571/// 'union'
572///
573void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
574 assert((Tok.getKind() == tok::kw_struct ||
575 Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
576 DeclSpec::TST TagType =
577 Tok.getKind() == tok::kw_union ? DeclSpec::TST_union : DeclSpec::TST_struct;
578 SourceLocation StartLoc = ConsumeToken();
579
580 // Parse the tag portion of this.
581 DeclTy *TagDecl;
582 if (ParseTag(TagDecl, TagType, StartLoc))
583 return;
Chris Lattnerbf0b7982007-01-23 04:27:41 +0000584
Chris Lattner90a26b02007-01-23 04:38:16 +0000585 // If there is a body, parse it and inform the actions module.
586 if (Tok.getKind() == tok::l_brace)
Chris Lattner1300fb92007-01-23 23:42:53 +0000587 ParseStructUnionBody(StartLoc, TagType, TagDecl);
Chris Lattnerda72c822006-08-13 22:16:42 +0000588
589 const char *PrevSpec = 0;
Chris Lattnerb9d572a2007-01-23 04:58:34 +0000590 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000591 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner1890ac82006-08-13 01:16:23 +0000592}
593
594
Chris Lattner90a26b02007-01-23 04:38:16 +0000595/// ParseStructUnionBody
596/// struct-contents:
597/// struct-declaration-list
598/// [EXT] empty
Chris Lattner736ed5d2007-06-09 05:59:07 +0000599/// [GNU] "struct-declaration-list" without terminatoring ';'
Chris Lattner90a26b02007-01-23 04:38:16 +0000600/// struct-declaration-list:
601/// struct-declaration
602/// struct-declaration-list struct-declaration
603/// [OBC] '@' 'defs' '(' class-name ')' [TODO]
604/// struct-declaration:
605/// specifier-qualifier-list struct-declarator-list ';'
Chris Lattner736ed5d2007-06-09 05:59:07 +0000606/// [GNU] __extension__ struct-declaration
607/// [GNU] specifier-qualifier-list ';'
Chris Lattner90a26b02007-01-23 04:38:16 +0000608/// struct-declarator-list:
609/// struct-declarator
610/// struct-declarator-list ',' struct-declarator
611/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
612/// struct-declarator:
613/// declarator
614/// [GNU] declarator attributes[opt]
615/// declarator[opt] ':' constant-expression
616/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
617///
Chris Lattner1300fb92007-01-23 23:42:53 +0000618void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
619 unsigned TagType, DeclTy *TagDecl) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000620 SourceLocation LBraceLoc = ConsumeBrace();
621
Chris Lattner7b9ace62007-01-23 20:11:08 +0000622 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
623 // C++.
Chris Lattner90a26b02007-01-23 04:38:16 +0000624 if (Tok.getKind() == tok::r_brace)
625 Diag(Tok, diag::ext_empty_struct_union_enum,
626 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Chris Lattner7b9ace62007-01-23 20:11:08 +0000627
Chris Lattner23b7eb62007-06-15 23:05:46 +0000628 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner1300fb92007-01-23 23:42:53 +0000629
Chris Lattner7b9ace62007-01-23 20:11:08 +0000630 // While we still have something to read, read the declarations in the struct.
Chris Lattner90a26b02007-01-23 04:38:16 +0000631 while (Tok.getKind() != tok::r_brace &&
632 Tok.getKind() != tok::eof) {
633 // Each iteration of this loop reads one struct-declaration.
634
Chris Lattner736ed5d2007-06-09 05:59:07 +0000635 // Check for extraneous top-level semicolon.
Chris Lattner36e46a22007-06-09 05:49:55 +0000636 if (Tok.getKind() == tok::semi) {
637 Diag(Tok, diag::ext_extra_struct_semi);
638 ConsumeToken();
639 continue;
640 }
Chris Lattner736ed5d2007-06-09 05:59:07 +0000641
642 // FIXME: When __extension__ is specified, disable extension diagnostics.
643 if (Tok.getKind() == tok::kw___extension__)
644 ConsumeToken();
Chris Lattner36e46a22007-06-09 05:49:55 +0000645
Chris Lattner90a26b02007-01-23 04:38:16 +0000646 // Parse the common specifier-qualifiers-list piece.
647 DeclSpec DS;
648 SourceLocation SpecQualLoc = Tok.getLocation();
649 ParseSpecifierQualifierList(DS);
650 // TODO: Does specifier-qualifier list correctly check that *something* is
651 // specified?
652
Chris Lattner90a26b02007-01-23 04:38:16 +0000653 // If there are no declarators, issue a warning.
654 if (Tok.getKind() == tok::semi) {
655 Diag(SpecQualLoc, diag::w_no_declarators);
Chris Lattner7b9ace62007-01-23 20:11:08 +0000656 ConsumeToken();
657 continue;
658 }
659
660 // Read struct-declarators until we find the semicolon.
661 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
662
663 while (1) {
664 /// struct-declarator: declarator
665 /// struct-declarator: declarator[opt] ':' constant-expression
666 if (Tok.getKind() != tok::colon)
667 ParseDeclarator(DeclaratorInfo);
668
669 ExprTy *BitfieldSize = 0;
670 if (Tok.getKind() == tok::colon) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000671 ConsumeToken();
Chris Lattner7b9ace62007-01-23 20:11:08 +0000672 ExprResult Res = ParseConstantExpression();
673 if (Res.isInvalid) {
674 SkipUntil(tok::semi, true, true);
675 } else {
676 BitfieldSize = Res.Val;
677 }
Chris Lattner90a26b02007-01-23 04:38:16 +0000678 }
Chris Lattner7b9ace62007-01-23 20:11:08 +0000679
680 // If attributes exist after the declarator, parse them.
681 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000682 DeclaratorInfo.AddAttributes(ParseAttributes());
Chris Lattner7b9ace62007-01-23 20:11:08 +0000683
Chris Lattner367b0192007-01-23 22:29:13 +0000684 // Install the declarator into the current TagDecl.
Chris Lattner1300fb92007-01-23 23:42:53 +0000685 DeclTy *Field = Actions.ParseField(CurScope, TagDecl, SpecQualLoc,
686 DeclaratorInfo, BitfieldSize);
687 FieldDecls.push_back(Field);
Chris Lattner7b9ace62007-01-23 20:11:08 +0000688
689 // If we don't have a comma, it is either the end of the list (a ';')
690 // or an error, bail out.
691 if (Tok.getKind() != tok::comma)
692 break;
693
694 // Consume the comma.
695 ConsumeToken();
696
697 // Parse the next declarator.
698 DeclaratorInfo.clear();
699
700 // Attributes are only allowed on the second declarator.
701 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000702 DeclaratorInfo.AddAttributes(ParseAttributes());
Chris Lattner90a26b02007-01-23 04:38:16 +0000703 }
704
705 if (Tok.getKind() == tok::semi) {
706 ConsumeToken();
Chris Lattner0c7e82d2007-06-09 05:54:40 +0000707 } else if (Tok.getKind() == tok::r_brace) {
708 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
709 break;
Chris Lattner90a26b02007-01-23 04:38:16 +0000710 } else {
711 Diag(Tok, diag::err_expected_semi_decl_list);
712 // Skip to end of block or statement
713 SkipUntil(tok::r_brace, true, true);
714 }
715 }
716
717 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
718
Chris Lattnerc1915e22007-01-25 07:29:02 +0000719 Actions.ParseRecordBody(RecordLoc, TagDecl, &FieldDecls[0],FieldDecls.size());
720
Steve Naroffb8371e12007-06-09 03:39:29 +0000721 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +0000722 // If attributes exist after struct contents, parse them.
723 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000724 AttrList = ParseAttributes(); // FIXME: where should I put them?
Chris Lattner90a26b02007-01-23 04:38:16 +0000725}
726
727
Chris Lattner3b561a32006-08-13 00:12:11 +0000728/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000729/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000730/// 'enum' identifier[opt] '{' enumerator-list '}'
731/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000732/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
733/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000734/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000735/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000736void Parser::ParseEnumSpecifier(DeclSpec &DS) {
737 assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
Chris Lattnerb20e8942006-11-28 05:30:29 +0000738 SourceLocation StartLoc = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000739
Chris Lattnerffbc2712007-01-25 06:05:38 +0000740 // Parse the tag portion of this.
741 DeclTy *TagDecl;
742 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
Chris Lattner3b561a32006-08-13 00:12:11 +0000743 return;
Chris Lattner3b561a32006-08-13 00:12:11 +0000744
Chris Lattnerc1915e22007-01-25 07:29:02 +0000745 if (Tok.getKind() == tok::l_brace)
746 ParseEnumBody(StartLoc, TagDecl);
747
Chris Lattner3b561a32006-08-13 00:12:11 +0000748 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000749 const char *PrevSpec = 0;
Chris Lattnerffbc2712007-01-25 06:05:38 +0000750 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000751 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000752}
753
Chris Lattnerc1915e22007-01-25 07:29:02 +0000754/// ParseEnumBody - Parse a {} enclosed enumerator-list.
755/// enumerator-list:
756/// enumerator
757/// enumerator-list ',' enumerator
758/// enumerator:
759/// enumeration-constant
760/// enumeration-constant '=' constant-expression
761/// enumeration-constant:
762/// identifier
763///
764void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
765 SourceLocation LBraceLoc = ConsumeBrace();
766
767 if (Tok.getKind() == tok::r_brace)
768 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
769
Chris Lattner23b7eb62007-06-15 23:05:46 +0000770 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000771
Chris Lattner4ef40012007-06-11 01:28:17 +0000772 DeclTy *LastEnumConstDecl = 0;
773
Chris Lattnerc1915e22007-01-25 07:29:02 +0000774 // Parse the enumerator-list.
775 while (Tok.getKind() == tok::identifier) {
776 IdentifierInfo *Ident = Tok.getIdentifierInfo();
777 SourceLocation IdentLoc = ConsumeToken();
778
779 SourceLocation EqualLoc;
780 ExprTy *AssignedVal = 0;
781 if (Tok.getKind() == tok::equal) {
782 EqualLoc = ConsumeToken();
783 ExprResult Res = ParseConstantExpression();
784 if (Res.isInvalid)
Chris Lattnerda6c2ce2007-04-27 19:13:15 +0000785 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000786 else
787 AssignedVal = Res.Val;
788 }
789
790 // Install the enumerator constant into EnumDecl.
Chris Lattner4ef40012007-06-11 01:28:17 +0000791 DeclTy *EnumConstDecl = Actions.ParseEnumConstant(CurScope, EnumDecl,
792 LastEnumConstDecl,
793 IdentLoc, Ident,
794 EqualLoc, AssignedVal);
795 EnumConstantDecls.push_back(EnumConstDecl);
796 LastEnumConstDecl = EnumConstDecl;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000797
798 if (Tok.getKind() != tok::comma)
799 break;
800 SourceLocation CommaLoc = ConsumeToken();
801
802 if (Tok.getKind() != tok::identifier && !getLang().C99)
803 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
804 }
805
806 // Eat the }.
807 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
808
809 Actions.ParseEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
810 EnumConstantDecls.size());
811
Steve Naroff0f2fe172007-06-01 17:11:19 +0000812 DeclTy *AttrList = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000813 // If attributes exist after the identifier list, parse them.
814 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f2fe172007-06-01 17:11:19 +0000815 AttrList = ParseAttributes(); // FIXME: where do they do?
Chris Lattnerc1915e22007-01-25 07:29:02 +0000816}
Chris Lattner3b561a32006-08-13 00:12:11 +0000817
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000818/// isTypeSpecifierQualifier - Return true if the current token could be the
819/// start of a specifier-qualifier-list.
820bool Parser::isTypeSpecifierQualifier() const {
821 switch (Tok.getKind()) {
822 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000823 // GNU attributes support.
824 case tok::kw___attribute:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000825 // type-specifiers
826 case tok::kw_short:
827 case tok::kw_long:
828 case tok::kw_signed:
829 case tok::kw_unsigned:
830 case tok::kw__Complex:
831 case tok::kw__Imaginary:
832 case tok::kw_void:
833 case tok::kw_char:
834 case tok::kw_int:
835 case tok::kw_float:
836 case tok::kw_double:
837 case tok::kw__Bool:
838 case tok::kw__Decimal32:
839 case tok::kw__Decimal64:
840 case tok::kw__Decimal128:
841
842 // struct-or-union-specifier
843 case tok::kw_struct:
844 case tok::kw_union:
845 // enum-specifier
846 case tok::kw_enum:
847
848 // type-qualifier
849 case tok::kw_const:
850 case tok::kw_volatile:
851 case tok::kw_restrict:
852 return true;
853
854 // typedef-name
855 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000856 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000857
858 // TODO: Attributes.
859 }
860}
861
Chris Lattneracd58a32006-08-06 17:24:14 +0000862/// isDeclarationSpecifier() - Return true if the current token is part of a
863/// declaration specifier.
864bool Parser::isDeclarationSpecifier() const {
865 switch (Tok.getKind()) {
866 default: return false;
867 // storage-class-specifier
868 case tok::kw_typedef:
869 case tok::kw_extern:
870 case tok::kw_static:
871 case tok::kw_auto:
872 case tok::kw_register:
873 case tok::kw___thread:
874
875 // type-specifiers
876 case tok::kw_short:
877 case tok::kw_long:
878 case tok::kw_signed:
879 case tok::kw_unsigned:
880 case tok::kw__Complex:
881 case tok::kw__Imaginary:
882 case tok::kw_void:
883 case tok::kw_char:
884 case tok::kw_int:
885 case tok::kw_float:
886 case tok::kw_double:
887 case tok::kw__Bool:
888 case tok::kw__Decimal32:
889 case tok::kw__Decimal64:
890 case tok::kw__Decimal128:
891
892 // struct-or-union-specifier
893 case tok::kw_struct:
894 case tok::kw_union:
895 // enum-specifier
896 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000897
Chris Lattneracd58a32006-08-06 17:24:14 +0000898 // type-qualifier
899 case tok::kw_const:
900 case tok::kw_volatile:
901 case tok::kw_restrict:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000902
Chris Lattneracd58a32006-08-06 17:24:14 +0000903 // function-specifier
904 case tok::kw_inline:
905 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000906
Chris Lattneracd58a32006-08-06 17:24:14 +0000907 // typedef-name
908 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000909 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +0000910 // TODO: Attributes.
911 }
912}
913
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000914
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000915/// ParseTypeQualifierListOpt
916/// type-qualifier-list: [C99 6.7.5]
917/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000918/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000919/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000920/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000921///
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000922void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000923 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000924 int isInvalid = false;
925 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +0000926 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000927
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000928 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000929 default:
Chris Lattnere37e2332006-08-15 04:50:22 +0000930 // If this is not a type-qualifier token, we're done reading type
931 // qualifiers. First verify that DeclSpec's are consistent.
Chris Lattnerb20e8942006-11-28 05:30:29 +0000932 DS.Finish(Diags, getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000933 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000934 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000935 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
936 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000937 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000938 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000939 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
940 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000941 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000942 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000943 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
944 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000945 break;
Chris Lattnere37e2332006-08-15 04:50:22 +0000946 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000947 DS.AddAttributes(ParseAttributes());
Steve Naroff98d153c2007-06-06 23:19:11 +0000948 continue; // do *not* consume the next token!
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000949 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +0000950
951 // If the specifier combination wasn't legal, issue a diagnostic.
952 if (isInvalid) {
953 assert(PrevSpec && "Method did not return previous specifier!");
954 if (isInvalid == 1) // Error.
955 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
956 else // extwarn.
957 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
958 }
959 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000960 }
961}
962
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000963
964/// ParseDeclarator - Parse and verify a newly-initialized declarator.
965///
966void Parser::ParseDeclarator(Declarator &D) {
967 /// This implements the 'declarator' production in the C grammar, then checks
968 /// for well-formedness and issues diagnostics.
969 ParseDeclaratorInternal(D);
970
Chris Lattner9fab3b92006-08-12 18:25:42 +0000971 // TODO: validate D.
Chris Lattnerbf320c82006-08-07 05:05:30 +0000972
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000973}
974
975/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +0000976/// declarator: [C99 6.7.5]
977/// pointer[opt] direct-declarator
Bill Wendling93efb222007-06-02 23:28:54 +0000978/// [C++] '&' declarator [C++ 8p4, dcl.decl]
979/// [GNU] '&' restrict[opt] attributes[opt] declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +0000980///
981/// pointer: [C99 6.7.5]
982/// '*' type-qualifier-list[opt]
983/// '*' type-qualifier-list[opt] pointer
984///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +0000985void Parser::ParseDeclaratorInternal(Declarator &D) {
Bill Wendling3708c182007-05-27 10:15:43 +0000986 tok::TokenKind Kind = Tok.getKind();
987
988 // Not a pointer or C++ reference.
989 if (Kind != tok::star && !(Kind == tok::amp && getLang().CPlusPlus))
Chris Lattner6c7416c2006-08-07 00:19:33 +0000990 return ParseDirectDeclarator(D);
991
Bill Wendling3708c182007-05-27 10:15:43 +0000992 // Otherwise, '*' -> pointer or '&' -> reference.
993 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
994
995 if (Kind == tok::star) {
996 // Is a pointer
997 DeclSpec DS;
Steve Naroff98d153c2007-06-06 23:19:11 +0000998
Bill Wendling3708c182007-05-27 10:15:43 +0000999 ParseTypeQualifierListOpt(DS);
Chris Lattner6c7416c2006-08-07 00:19:33 +00001000
Bill Wendling3708c182007-05-27 10:15:43 +00001001 // Recursively parse the declarator.
1002 ParseDeclaratorInternal(D);
Chris Lattner9dfdb3c2006-11-13 07:38:09 +00001003
Bill Wendling3708c182007-05-27 10:15:43 +00001004 // Remember that we parsed a pointer type, and remember the type-quals.
1005 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc));
1006 } else {
1007 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00001008 DeclSpec DS;
1009
1010 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1011 // cv-qualifiers are introduced through the use of a typedef or of a
1012 // template type argument, in which case the cv-qualifiers are ignored.
1013 //
1014 // [GNU] Retricted references are allowed.
1015 // [GNU] Attributes on references are allowed.
1016 ParseTypeQualifierListOpt(DS);
1017
1018 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1019 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1020 Diag(DS.getConstSpecLoc(),
1021 diag::err_invalid_reference_qualifier_application,
1022 "const");
1023 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1024 Diag(DS.getVolatileSpecLoc(),
1025 diag::err_invalid_reference_qualifier_application,
1026 "volatile");
1027 }
Bill Wendling3708c182007-05-27 10:15:43 +00001028
1029 // Recursively parse the declarator.
1030 ParseDeclaratorInternal(D);
1031
1032 // Remember that we parsed a reference type. It doesn't have type-quals.
Bill Wendling93efb222007-06-02 23:28:54 +00001033 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc));
Bill Wendling3708c182007-05-27 10:15:43 +00001034 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001035}
1036
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001037/// ParseDirectDeclarator
1038/// direct-declarator: [C99 6.7.5]
1039/// identifier
1040/// '(' declarator ')'
1041/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00001042/// [C90] direct-declarator '[' constant-expression[opt] ']'
1043/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1044/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1045/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1046/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001047/// direct-declarator '(' parameter-type-list ')'
1048/// direct-declarator '(' identifier-list[opt] ')'
1049/// [GNU] direct-declarator '(' parameter-forward-declarations
1050/// parameter-type-list[opt] ')'
1051///
Chris Lattneracd58a32006-08-06 17:24:14 +00001052void Parser::ParseDirectDeclarator(Declarator &D) {
1053 // Parse the first direct-declarator seen.
1054 if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
1055 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1056 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1057 ConsumeToken();
1058 } else if (Tok.getKind() == tok::l_paren) {
1059 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001060 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001061 // Example: 'char (*X)' or 'int (*XX)(void)'
1062 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001063 } else if (D.mayOmitIdentifier()) {
1064 // This could be something simple like "int" (in which case the declarator
1065 // portion is empty), if an abstract-declarator is allowed.
1066 D.SetIdentifier(0, Tok.getLocation());
1067 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +00001068 // Expected identifier or '('.
1069 Diag(Tok, diag::err_expected_ident_lparen);
1070 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +00001071 }
1072
1073 assert(D.isPastIdentifier() &&
1074 "Haven't past the location of the identifier yet?");
1075
1076 while (1) {
1077 if (Tok.getKind() == tok::l_paren) {
1078 ParseParenDeclarator(D);
1079 } else if (Tok.getKind() == tok::l_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001080 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001081 } else {
1082 break;
1083 }
1084 }
1085}
1086
1087/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
1088/// either be before the identifier (in which case these are just grouping
1089/// parens for precedence) or it may be after the identifier, in which case
1090/// these are function arguments.
1091///
1092/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001093/// parameter-type-list: [C99 6.7.5]
1094/// parameter-list
1095/// parameter-list ',' '...'
1096///
1097/// parameter-list: [C99 6.7.5]
1098/// parameter-declaration
1099/// parameter-list ',' parameter-declaration
1100///
1101/// parameter-declaration: [C99 6.7.5]
1102/// declaration-specifiers declarator
Chris Lattnere37e2332006-08-15 04:50:22 +00001103/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001104/// declaration-specifiers abstract-declarator[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001105/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001106///
1107/// identifier-list: [C99 6.7.5]
1108/// identifier
1109/// identifier-list ',' identifier
1110///
Chris Lattneracd58a32006-08-06 17:24:14 +00001111void Parser::ParseParenDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001112 SourceLocation StartLoc = ConsumeParen();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001113
Chris Lattneracd58a32006-08-06 17:24:14 +00001114 // If we haven't past the identifier yet (or where the identifier would be
1115 // stored, if this is an abstract declarator), then this is probably just
1116 // grouping parens.
1117 if (!D.isPastIdentifier()) {
1118 // Okay, this is probably a grouping paren. However, if this could be an
1119 // abstract-declarator, then this could also be the start of function
1120 // arguments (consider 'void()').
1121 bool isGrouping;
1122
1123 if (!D.mayOmitIdentifier()) {
1124 // If this can't be an abstract-declarator, this *must* be a grouping
1125 // paren, because we haven't seen the identifier yet.
1126 isGrouping = true;
1127 } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
1128 isDeclarationSpecifier()) { // 'int(int)' is a function.
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001129 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1130 // considered to be a type, not a K&R identifier-list.
Chris Lattneracd58a32006-08-06 17:24:14 +00001131 isGrouping = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001132 } else {
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001133 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
Chris Lattneracd58a32006-08-06 17:24:14 +00001134 isGrouping = true;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001135 }
Chris Lattneracd58a32006-08-06 17:24:14 +00001136
1137 // If this is a grouping paren, handle:
1138 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001139 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001140 if (isGrouping) {
Chris Lattnere37e2332006-08-15 04:50:22 +00001141 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f05a7a2007-06-09 23:38:17 +00001142 D.AddAttributes(ParseAttributes());
Chris Lattnere37e2332006-08-15 04:50:22 +00001143
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001144 ParseDeclaratorInternal(D);
Chris Lattner4564bc12006-08-10 23:14:52 +00001145 // Match the ')'.
Chris Lattner04f80192006-08-15 04:55:54 +00001146 MatchRHSPunctuation(tok::r_paren, StartLoc);
Chris Lattneracd58a32006-08-06 17:24:14 +00001147 return;
1148 }
1149
1150 // Okay, if this wasn't a grouping paren, it must be the start of a function
Chris Lattnera3507222006-08-07 00:33:37 +00001151 // argument list. Recognize that this declarator will never have an
1152 // identifier (and remember where it would have been), then fall through to
1153 // the handling of argument lists.
Chris Lattneracd58a32006-08-06 17:24:14 +00001154 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001155 }
1156
Chris Lattneracd58a32006-08-06 17:24:14 +00001157 // Okay, this is the parameter list of a function definition, or it is an
1158 // identifier list of a K&R-style function.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001159 bool IsVariadic;
Chris Lattneracd58a32006-08-06 17:24:14 +00001160 bool HasPrototype;
Chris Lattner14776b92006-08-06 22:27:40 +00001161 bool ErrorEmitted = false;
1162
Chris Lattneredc9e392006-12-02 06:21:46 +00001163 // Build up an array of information about the parsed arguments.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001164 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1165 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Chris Lattneredc9e392006-12-02 06:21:46 +00001166
Chris Lattneracd58a32006-08-06 17:24:14 +00001167 if (Tok.getKind() == tok::r_paren) {
1168 // int() -> no prototype, no '...'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001169 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001170 HasPrototype = false;
1171 } else if (Tok.getKind() == tok::identifier &&
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001172 // K&R identifier lists can't have typedefs as identifiers, per
1173 // C99 6.7.5.3p11.
Steve Naroffb419d3a2006-10-27 23:18:49 +00001174 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001175 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1176 // normal declarators, not for abstract-declarators.
1177 assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
1178
1179 // If there was no identifier specified, either we are in an
1180 // abstract-declarator, or we are in a parameter declarator which was found
1181 // to be abstract. In abstract-declarators, identifier lists are not valid,
1182 // diagnose this.
1183 if (!D.getIdentifier())
1184 Diag(Tok, diag::ext_ident_list_in_param);
Chris Lattneredc9e392006-12-02 06:21:46 +00001185
Chris Lattnercbc426d2006-12-02 06:43:02 +00001186 // Remember this identifier in ParamInfo.
1187 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1188 Tok.getLocation(), 0));
1189
Chris Lattneracd58a32006-08-06 17:24:14 +00001190 ConsumeToken();
1191 while (Tok.getKind() == tok::comma) {
1192 // Eat the comma.
1193 ConsumeToken();
1194
Chris Lattnercbc426d2006-12-02 06:43:02 +00001195 if (Tok.getKind() != tok::identifier) {
1196 Diag(Tok, diag::err_expected_ident);
Chris Lattner14776b92006-08-06 22:27:40 +00001197 ErrorEmitted = true;
1198 break;
1199 }
Chris Lattnercbc426d2006-12-02 06:43:02 +00001200
Chris Lattner969ca152006-12-03 06:29:03 +00001201 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
1202
1203 // Verify that the argument identifier has not already been mentioned.
Chris Lattnerbaf33662007-01-27 02:14:08 +00001204 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerad9ac942007-01-23 01:14:52 +00001205 Diag(Tok.getLocation(), diag::err_param_redefinition,ParmII->getName());
1206 ParmII = 0;
1207 }
Chris Lattner969ca152006-12-03 06:29:03 +00001208
Chris Lattnercbc426d2006-12-02 06:43:02 +00001209 // Remember this identifier in ParamInfo.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001210 if (ParmII)
1211 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1212 Tok.getLocation(), 0));
Chris Lattnercbc426d2006-12-02 06:43:02 +00001213
1214 // Eat the identifier.
1215 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00001216 }
1217
Chris Lattneracd58a32006-08-06 17:24:14 +00001218 // K&R 'prototype'.
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001219 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001220 HasPrototype = false;
1221 } else {
Chris Lattner43e956c2006-11-28 04:05:37 +00001222 // Finally, a normal, non-empty parameter type list.
1223
Chris Lattnercbc426d2006-12-02 06:43:02 +00001224 // Enter function-declaration scope, limiting any declarators for struct
1225 // tags to the function prototype scope.
1226 // FIXME: is this needed?
Chris Lattner43e956c2006-11-28 04:05:37 +00001227 EnterScope(0);
1228
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001229 IsVariadic = false;
Chris Lattneracd58a32006-08-06 17:24:14 +00001230 while (1) {
1231 if (Tok.getKind() == tok::ellipsis) {
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001232 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +00001233
1234 // Check to see if this is "void(...)" which is not allowed.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001235 if (ParamInfo.empty()) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001236 // Otherwise, parse parameter type list. If it starts with an
1237 // ellipsis, diagnose the malformed function.
Chris Lattneracd58a32006-08-06 17:24:14 +00001238 Diag(Tok, diag::err_ellipsis_first_arg);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001239 IsVariadic = false; // Treat this like 'void()'.
Chris Lattneracd58a32006-08-06 17:24:14 +00001240 }
1241
1242 // Consume the ellipsis.
1243 ConsumeToken();
1244 break;
1245 }
1246
Chris Lattneracd58a32006-08-06 17:24:14 +00001247 // Parse the declaration-specifiers.
1248 DeclSpec DS;
1249 ParseDeclarationSpecifiers(DS);
1250
1251 // Parse the declarator. This is "PrototypeContext", because we must
1252 // accept either 'declarator' or 'abstract-declarator' here.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001253 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1254 ParseDeclarator(ParmDecl);
Chris Lattneracd58a32006-08-06 17:24:14 +00001255
Chris Lattnere37e2332006-08-15 04:50:22 +00001256 // Parse GNU attributes, if present.
1257 if (Tok.getKind() == tok::kw___attribute)
Steve Naroff0f05a7a2007-06-09 23:38:17 +00001258 ParmDecl.AddAttributes(ParseAttributes());
Chris Lattnere37e2332006-08-15 04:50:22 +00001259
Chris Lattner43e956c2006-11-28 04:05:37 +00001260 // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001261 // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted.
1262 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1263 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
Chris Lattner4d8f8732006-11-28 05:05:08 +00001264 Diag(DS.getStorageClassSpecLoc(),
Chris Lattner43e956c2006-11-28 04:05:37 +00001265 diag::err_invalid_storage_class_in_func_decl);
Chris Lattner353f5742006-11-28 04:50:12 +00001266 DS.ClearStorageClassSpecs();
Chris Lattner43e956c2006-11-28 04:05:37 +00001267 }
Chris Lattner4d8f8732006-11-28 05:05:08 +00001268 if (DS.isThreadSpecified()) {
1269 Diag(DS.getThreadSpecLoc(),
1270 diag::err_invalid_storage_class_in_func_decl);
1271 DS.ClearStorageClassSpecs();
1272 }
Chris Lattner43e956c2006-11-28 04:05:37 +00001273
1274 // Inform the actions module about the parameter declarator, so it gets
1275 // added to the current scope.
Chris Lattner216d8652006-12-02 06:47:41 +00001276 Action::TypeResult ParamTy =
1277 Actions.ParseParamDeclaratorType(CurScope, ParmDecl);
Chris Lattnercbc426d2006-12-02 06:43:02 +00001278
1279 // Remember this parsed parameter in ParamInfo.
Chris Lattner969ca152006-12-03 06:29:03 +00001280 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1281
1282 // Verify that the argument identifier has not already been mentioned.
Chris Lattnerbaf33662007-01-27 02:14:08 +00001283 if (ParmII && !ParamsSoFar.insert(ParmII)) {
Chris Lattnerad9ac942007-01-23 01:14:52 +00001284 Diag(ParmDecl.getIdentifierLoc(), diag::err_param_redefinition,
1285 ParmII->getName());
1286 ParmII = 0;
Chris Lattner969ca152006-12-03 06:29:03 +00001287 }
1288
1289 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnercbc426d2006-12-02 06:43:02 +00001290 ParmDecl.getIdentifierLoc(),
1291 ParamTy.Val));
Chris Lattneracd58a32006-08-06 17:24:14 +00001292
1293 // If the next token is a comma, consume it and keep reading arguments.
1294 if (Tok.getKind() != tok::comma) break;
1295
1296 // Consume the comma.
1297 ConsumeToken();
1298 }
1299
1300 HasPrototype = true;
Chris Lattner43e956c2006-11-28 04:05:37 +00001301
1302 // Leave prototype scope.
1303 ExitScope();
Chris Lattneracd58a32006-08-06 17:24:14 +00001304 }
1305
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001306 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerd2e97c12006-12-03 02:03:33 +00001307 if (!ErrorEmitted)
1308 D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic,
1309 &ParamInfo[0], ParamInfo.size(),
1310 StartLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001311
Chris Lattner14776b92006-08-06 22:27:40 +00001312 // If we have the closing ')', eat it and we're done.
1313 if (Tok.getKind() == tok::r_paren) {
1314 ConsumeParen();
1315 } else {
1316 // If an error happened earlier parsing something else in the proto, don't
1317 // issue another error.
1318 if (!ErrorEmitted)
1319 Diag(Tok, diag::err_expected_rparen);
1320 SkipUntil(tok::r_paren);
1321 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001322}
Chris Lattneracd58a32006-08-06 17:24:14 +00001323
Chris Lattnere8074e62006-08-06 18:30:15 +00001324
1325/// [C90] direct-declarator '[' constant-expression[opt] ']'
1326/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1327/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1328/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1329/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1330void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001331 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001332
1333 // If valid, this location is the position where we read the 'static' keyword.
1334 SourceLocation StaticLoc;
Chris Lattneraf635312006-10-16 06:06:51 +00001335 if (Tok.getKind() == tok::kw_static)
1336 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001337
1338 // If there is a type-qualifier-list, read it now.
1339 DeclSpec DS;
1340 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001341
1342 // If we haven't already read 'static', check to see if there is one after the
1343 // type-qualifier-list.
Chris Lattneraf635312006-10-16 06:06:51 +00001344 if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static)
1345 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001346
1347 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001348 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001349 ExprResult NumElements(false);
Chris Lattner1906f802006-08-06 19:14:46 +00001350 if (Tok.getKind() == tok::star) {
1351 // Remember the '*' token, in case we have to un-get it.
Chris Lattner146762e2007-07-20 16:59:19 +00001352 Token StarTok = Tok;
Chris Lattnere8074e62006-08-06 18:30:15 +00001353 ConsumeToken();
Chris Lattner1906f802006-08-06 19:14:46 +00001354
1355 // Check that the ']' token is present to avoid incorrectly parsing
1356 // expressions starting with '*' as [*].
1357 if (Tok.getKind() == tok::r_square) {
1358 if (StaticLoc.isValid())
1359 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1360 StaticLoc = SourceLocation(); // Drop the static.
1361 isStar = true;
Chris Lattner1906f802006-08-06 19:14:46 +00001362 } else {
1363 // Otherwise, the * must have been some expression (such as '*ptr') that
Chris Lattner9fab3b92006-08-12 18:25:42 +00001364 // started an assignment-expr. We already consumed the token, but now we
Chris Lattner62591722006-08-12 18:40:58 +00001365 // need to reparse it. This handles cases like 'X[*p + 4]'
1366 NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok);
Chris Lattner1906f802006-08-06 19:14:46 +00001367 }
Chris Lattner9fab3b92006-08-12 18:25:42 +00001368 } else if (Tok.getKind() != tok::r_square) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001369 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001370 NumElements = ParseAssignmentExpression();
1371 }
1372
1373 // If there was an error parsing the assignment-expression, recover.
1374 if (NumElements.isInvalid) {
1375 // If the expression was invalid, skip it.
1376 SkipUntil(tok::r_square);
1377 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001378 }
1379
Chris Lattner04f80192006-08-15 04:55:54 +00001380 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001381
Chris Lattnere8074e62006-08-06 18:30:15 +00001382 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1383 // it was not a constant expression.
1384 if (!getLang().C99) {
1385 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001386 if (isStar || StaticLoc.isValid() ||
1387 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001388 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001389 }
Bill Wendling93efb222007-06-02 23:28:54 +00001390
Chris Lattner6c7416c2006-08-07 00:19:33 +00001391 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001392 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1393 StaticLoc.isValid(), isStar,
1394 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001395}
1396