blob: 7d1a9bb4b653d5280960b2f658ce104832eec041 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Daniel Dunbarcc7b1602008-08-11 03:45:03 +000015#include "clang/Basic/Diagnostic.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/Parse/DeclSpec.h"
Chris Lattnera7549902007-08-26 06:24:45 +000017#include "clang/Parse/Scope.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "llvm/ADT/SmallSet.h"
19using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// C99 6.7: Declarations.
23//===----------------------------------------------------------------------===//
24
25/// ParseTypeName
26/// type-name: [C99 6.7.6]
27/// specifier-qualifier-list abstract-declarator[opt]
28Parser::TypeTy *Parser::ParseTypeName() {
29 // Parse the common declaration-specifiers piece.
30 DeclSpec DS;
31 ParseSpecifierQualifierList(DS);
32
33 // Parse the abstract-declarator, if present.
34 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
35 ParseDeclarator(DeclaratorInfo);
36
Steve Naroff0acc9c92007-09-15 18:49:24 +000037 return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
Chris Lattner4b009652007-07-25 00:24:17 +000038}
39
40/// ParseAttributes - Parse a non-empty attributes list.
41///
42/// [GNU] attributes:
43/// attribute
44/// attributes attribute
45///
46/// [GNU] attribute:
47/// '__attribute__' '(' '(' attribute-list ')' ')'
48///
49/// [GNU] attribute-list:
50/// attrib
51/// attribute_list ',' attrib
52///
53/// [GNU] attrib:
54/// empty
55/// attrib-name
56/// attrib-name '(' identifier ')'
57/// attrib-name '(' identifier ',' nonempty-expr-list ')'
58/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
59///
60/// [GNU] attrib-name:
61/// identifier
62/// typespec
63/// typequal
64/// storageclass
65///
66/// FIXME: The GCC grammar/code for this construct implies we need two
67/// token lookahead. Comment from gcc: "If they start with an identifier
68/// which is followed by a comma or close parenthesis, then the arguments
69/// start with that identifier; otherwise they are an expression list."
70///
71/// At the moment, I am not doing 2 token lookahead. I am also unaware of
72/// any attributes that don't work (based on my limited testing). Most
73/// attributes are very simple in practice. Until we find a bug, I don't see
74/// a pressing need to implement the 2 token lookahead.
75
76AttributeList *Parser::ParseAttributes() {
Chris Lattner34a01ad2007-10-09 17:33:22 +000077 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Chris Lattner4b009652007-07-25 00:24:17 +000078
79 AttributeList *CurrAttr = 0;
80
Chris Lattner34a01ad2007-10-09 17:33:22 +000081 while (Tok.is(tok::kw___attribute)) {
Chris Lattner4b009652007-07-25 00:24:17 +000082 ConsumeToken();
83 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
84 "attribute")) {
85 SkipUntil(tok::r_paren, true); // skip until ) or ;
86 return CurrAttr;
87 }
88 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
89 SkipUntil(tok::r_paren, true); // skip until ) or ;
90 return CurrAttr;
91 }
92 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner34a01ad2007-10-09 17:33:22 +000093 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
94 Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +000095
Chris Lattner34a01ad2007-10-09 17:33:22 +000096 if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +000097 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
98 ConsumeToken();
99 continue;
100 }
101 // we have an identifier or declaration specifier (const, int, etc.)
102 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
103 SourceLocation AttrNameLoc = ConsumeToken();
104
105 // check if we have a "paramterized" attribute
Chris Lattner34a01ad2007-10-09 17:33:22 +0000106 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000107 ConsumeParen(); // ignore the left paren loc for now
108
Chris Lattner34a01ad2007-10-09 17:33:22 +0000109 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000110 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
111 SourceLocation ParmLoc = ConsumeToken();
112
Chris Lattner34a01ad2007-10-09 17:33:22 +0000113 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000114 // __attribute__(( mode(byte) ))
115 ConsumeParen(); // ignore the right paren loc for now
116 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
117 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000118 } else if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000119 ConsumeToken();
120 // __attribute__(( format(printf, 1, 2) ))
121 llvm::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 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000134 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000135 break;
136 ConsumeToken(); // Eat the comma, move to the next argument
137 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000138 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000139 ConsumeParen(); // ignore the right paren loc for now
140 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
141 ParmLoc, &ArgExprs[0], ArgExprs.size(), CurrAttr);
142 }
143 }
144 } else { // not an identifier
145 // parse a possibly empty comma separated list of expressions
Chris Lattner34a01ad2007-10-09 17:33:22 +0000146 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000147 // __attribute__(( nonnull() ))
148 ConsumeParen(); // ignore the right paren loc for now
149 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
150 0, SourceLocation(), 0, 0, CurrAttr);
151 } else {
152 // __attribute__(( aligned(16) ))
153 llvm::SmallVector<ExprTy*, 8> ArgExprs;
154 bool ArgExprsOk = true;
155
156 // now parse the list of expressions
157 while (1) {
158 ExprResult ArgExpr = ParseAssignmentExpression();
159 if (ArgExpr.isInvalid) {
160 ArgExprsOk = false;
161 SkipUntil(tok::r_paren);
162 break;
163 } else {
164 ArgExprs.push_back(ArgExpr.Val);
165 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000166 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000167 break;
168 ConsumeToken(); // Eat the comma, move to the next argument
169 }
170 // Match the ')'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000171 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000172 ConsumeParen(); // ignore the right paren loc for now
173 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
174 SourceLocation(), &ArgExprs[0], ArgExprs.size(),
175 CurrAttr);
176 }
177 }
178 }
179 } else {
180 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
181 0, SourceLocation(), 0, 0, CurrAttr);
182 }
183 }
184 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
185 SkipUntil(tok::r_paren, false);
186 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
187 SkipUntil(tok::r_paren, false);
188 }
189 return CurrAttr;
190}
191
192/// ParseDeclaration - Parse a full 'declaration', which consists of
193/// declaration-specifiers, some number of declarators, and a semicolon.
194/// 'Context' should be a Declarator::TheContext value.
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000195///
196/// declaration: [C99 6.7]
197/// block-declaration ->
198/// simple-declaration
199/// others [FIXME]
200/// [C++] namespace-definition
201/// others... [FIXME]
202///
Chris Lattner4b009652007-07-25 00:24:17 +0000203Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000204 switch (Tok.getKind()) {
205 case tok::kw_namespace:
206 return ParseNamespace(Context);
207 default:
208 return ParseSimpleDeclaration(Context);
209 }
210}
211
212/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
213/// declaration-specifiers init-declarator-list[opt] ';'
214///[C90/C++]init-declarator-list ';' [TODO]
215/// [OMP] threadprivate-directive [TODO]
216Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Chris Lattner4b009652007-07-25 00:24:17 +0000217 // Parse the common declaration-specifiers piece.
218 DeclSpec DS;
219 ParseDeclarationSpecifiers(DS);
220
221 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
222 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000223 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000224 ConsumeToken();
225 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
226 }
227
228 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
229 ParseDeclarator(DeclaratorInfo);
230
231 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
232}
233
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000234
Chris Lattner4b009652007-07-25 00:24:17 +0000235/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
236/// parsing 'declaration-specifiers declarator'. This method is split out this
237/// way to handle the ambiguity between top-level function-definitions and
238/// declarations.
239///
Chris Lattner4b009652007-07-25 00:24:17 +0000240/// init-declarator-list: [C99 6.7]
241/// init-declarator
242/// init-declarator-list ',' init-declarator
243/// init-declarator: [C99 6.7]
244/// declarator
245/// declarator '=' initializer
246/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
247/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000248/// [C++] declarator initializer[opt]
249///
250/// [C++] initializer:
251/// [C++] '=' initializer-clause
252/// [C++] '(' expression-list ')'
Chris Lattner4b009652007-07-25 00:24:17 +0000253///
254Parser::DeclTy *Parser::
255ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
256
257 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
258 // that they can be chained properly if the actions want this.
259 Parser::DeclTy *LastDeclInGroup = 0;
260
261 // At this point, we know that it is not a function definition. Parse the
262 // rest of the init-declarator-list.
263 while (1) {
264 // If a simple-asm-expr is present, parse it.
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000265 if (Tok.is(tok::kw_asm)) {
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000266 ExprResult AsmLabel = ParseSimpleAsm();
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000267 if (AsmLabel.isInvalid) {
268 SkipUntil(tok::semi);
269 return 0;
270 }
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000271
272 D.setAsmLabel(AsmLabel.Val);
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000273 }
Chris Lattner4b009652007-07-25 00:24:17 +0000274
275 // If attributes are present, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000276 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000277 D.AddAttributes(ParseAttributes());
Steve Naroff6a0e2092007-09-12 14:07:44 +0000278
279 // Inform the current actions module that we just parsed this declarator.
280 // FIXME: pass asm & attributes.
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000281 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Steve Naroff6a0e2092007-09-12 14:07:44 +0000282
Chris Lattner4b009652007-07-25 00:24:17 +0000283 // Parse declarator '=' initializer.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000284 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000285 ConsumeToken();
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000286 ExprResult Init = ParseInitializer();
Chris Lattner4b009652007-07-25 00:24:17 +0000287 if (Init.isInvalid) {
288 SkipUntil(tok::semi);
289 return 0;
290 }
Steve Naroff6a0e2092007-09-12 14:07:44 +0000291 Actions.AddInitializerToDecl(LastDeclInGroup, Init.Val);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000292 } else if (Tok.is(tok::l_paren)) {
293 // Parse C++ direct initializer: '(' expression-list ')'
294 SourceLocation LParenLoc = ConsumeParen();
295 ExprListTy Exprs;
296 CommaLocsTy CommaLocs;
297
298 bool InvalidExpr = false;
299 if (ParseExpressionList(Exprs, CommaLocs)) {
300 SkipUntil(tok::r_paren);
301 InvalidExpr = true;
302 }
303 // Match the ')'.
304 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
305
306 if (!InvalidExpr) {
307 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
308 "Unexpected number of commas!");
309 Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc,
310 &Exprs[0], Exprs.size(),
311 &CommaLocs[0], RParenLoc);
312 }
Chris Lattner4b009652007-07-25 00:24:17 +0000313 }
314
Chris Lattner4b009652007-07-25 00:24:17 +0000315 // If we don't have a comma, it is either the end of the list (a ';') or an
316 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000317 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000318 break;
319
320 // Consume the comma.
321 ConsumeToken();
322
323 // Parse the next declarator.
324 D.clear();
325 ParseDeclarator(D);
326 }
327
Chris Lattner34a01ad2007-10-09 17:33:22 +0000328 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000329 ConsumeToken();
330 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
331 }
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000332 // If this is an ObjC2 for-each loop, this is a successful declarator
333 // parse. The syntax for these looks like:
334 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000335 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000336 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
337 }
Chris Lattner4b009652007-07-25 00:24:17 +0000338 Diag(Tok, diag::err_parse_error);
339 // Skip to end of block or statement
Chris Lattnerf491b412007-08-21 18:36:18 +0000340 SkipUntil(tok::r_brace, true, true);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000341 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000342 ConsumeToken();
343 return 0;
344}
345
346/// ParseSpecifierQualifierList
347/// specifier-qualifier-list:
348/// type-specifier specifier-qualifier-list[opt]
349/// type-qualifier specifier-qualifier-list[opt]
350/// [GNU] attributes specifier-qualifier-list[opt]
351///
352void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
353 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
354 /// parse declaration-specifiers and complain about extra stuff.
355 ParseDeclarationSpecifiers(DS);
356
357 // Validate declspec for type-name.
358 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000359 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000360 Diag(Tok, diag::err_typename_requires_specqual);
361
362 // Issue diagnostic and remove storage class if present.
363 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
364 if (DS.getStorageClassSpecLoc().isValid())
365 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
366 else
367 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
368 DS.ClearStorageClassSpecs();
369 }
370
371 // Issue diagnostic and remove function specfier if present.
372 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
373 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
374 DS.ClearFunctionSpecs();
375 }
376}
377
378/// ParseDeclarationSpecifiers
379/// declaration-specifiers: [C99 6.7]
380/// storage-class-specifier declaration-specifiers[opt]
381/// type-specifier declaration-specifiers[opt]
382/// type-qualifier declaration-specifiers[opt]
383/// [C99] function-specifier declaration-specifiers[opt]
384/// [GNU] attributes declaration-specifiers[opt]
385///
386/// storage-class-specifier: [C99 6.7.1]
387/// 'typedef'
388/// 'extern'
389/// 'static'
390/// 'auto'
391/// 'register'
392/// [GNU] '__thread'
393/// type-specifier: [C99 6.7.2]
394/// 'void'
395/// 'char'
396/// 'short'
397/// 'int'
398/// 'long'
399/// 'float'
400/// 'double'
401/// 'signed'
402/// 'unsigned'
403/// struct-or-union-specifier
404/// enum-specifier
405/// typedef-name
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000406/// [C++] 'wchar_t'
Chris Lattner4b009652007-07-25 00:24:17 +0000407/// [C++] 'bool'
408/// [C99] '_Bool'
409/// [C99] '_Complex'
410/// [C99] '_Imaginary' // Removed in TC2?
411/// [GNU] '_Decimal32'
412/// [GNU] '_Decimal64'
413/// [GNU] '_Decimal128'
Steve Naroff4c255ab2007-07-31 23:56:32 +0000414/// [GNU] typeof-specifier
Chris Lattner4b009652007-07-25 00:24:17 +0000415/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroffa8ee2262007-08-22 23:18:22 +0000416/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattner4b009652007-07-25 00:24:17 +0000417/// type-qualifier:
418/// 'const'
419/// 'volatile'
420/// [C99] 'restrict'
421/// function-specifier: [C99 6.7.4]
422/// [C99] 'inline'
423///
424void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattnera4ff4272008-03-13 06:29:04 +0000425 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000426 while (1) {
427 int isInvalid = false;
428 const char *PrevSpec = 0;
429 SourceLocation Loc = Tok.getLocation();
430
431 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000432 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000433 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000434 // If this is not a declaration specifier token, we're done reading decl
435 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000436 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000437 return;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000438
439 // typedef-name
440 case tok::identifier: {
441 // This identifier can only be a typedef name if we haven't already seen
442 // a type-specifier. Without this check we misparse:
443 // typedef int X; struct Y { short X; }; as 'short int'.
444 if (DS.hasTypeSpecifier())
445 goto DoneWithDeclSpec;
446
447 // It has to be available as a typedef too!
Argiris Kirtzidis46403632008-08-01 10:35:27 +0000448 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattnerfda18db2008-07-26 01:18:38 +0000449 if (TypeRep == 0)
450 goto DoneWithDeclSpec;
451
452 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
453 TypeRep);
454 if (isInvalid)
455 break;
456
457 DS.SetRangeEnd(Tok.getLocation());
458 ConsumeToken(); // The identifier
459
460 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
461 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
462 // Objective-C interface. If we don't have Objective-C or a '<', this is
463 // just a normal reference to a typedef name.
464 if (!Tok.is(tok::less) || !getLang().ObjC1)
465 continue;
466
467 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000468 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000469 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000470 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000471
472 DS.SetRangeEnd(EndProtoLoc);
473
Steve Narofff7683302008-09-22 10:28:57 +0000474 // Need to support trailing type qualifiers (e.g. "id<p> const").
475 // If a type specifier follows, it will be diagnosed elsewhere.
476 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000477 }
Chris Lattner4b009652007-07-25 00:24:17 +0000478 // GNU attributes support.
479 case tok::kw___attribute:
480 DS.AddAttributes(ParseAttributes());
481 continue;
482
483 // storage-class-specifier
484 case tok::kw_typedef:
485 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
486 break;
487 case tok::kw_extern:
488 if (DS.isThreadSpecified())
489 Diag(Tok, diag::ext_thread_before, "extern");
490 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
491 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000492 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000493 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
494 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000495 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000496 case tok::kw_static:
497 if (DS.isThreadSpecified())
498 Diag(Tok, diag::ext_thread_before, "static");
499 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
500 break;
501 case tok::kw_auto:
502 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
503 break;
504 case tok::kw_register:
505 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
506 break;
507 case tok::kw___thread:
508 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
509 break;
510
511 // type-specifiers
512 case tok::kw_short:
513 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
514 break;
515 case tok::kw_long:
516 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
517 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
518 else
519 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
520 break;
521 case tok::kw_signed:
522 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
523 break;
524 case tok::kw_unsigned:
525 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
526 break;
527 case tok::kw__Complex:
528 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
529 break;
530 case tok::kw__Imaginary:
531 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
532 break;
533 case tok::kw_void:
534 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
535 break;
536 case tok::kw_char:
537 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
538 break;
539 case tok::kw_int:
540 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
541 break;
542 case tok::kw_float:
543 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
544 break;
545 case tok::kw_double:
546 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
547 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000548 case tok::kw_wchar_t: // [C++ 2.11p1]
549 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
550 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000551 case tok::kw_bool: // [C++ 2.11p1]
552 case tok::kw__Bool:
553 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
554 break;
555 case tok::kw__Decimal32:
556 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
557 break;
558 case tok::kw__Decimal64:
559 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
560 break;
561 case tok::kw__Decimal128:
562 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
563 break;
Chris Lattner2e78db32008-04-13 18:59:07 +0000564
565 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000566 case tok::kw_struct:
567 case tok::kw_union:
Douglas Gregorec93f442008-04-13 21:30:24 +0000568 ParseClassSpecifier(DS);
Chris Lattner4b009652007-07-25 00:24:17 +0000569 continue;
570 case tok::kw_enum:
571 ParseEnumSpecifier(DS);
572 continue;
573
Steve Naroff7cbb1462007-07-31 12:34:36 +0000574 // GNU typeof support.
575 case tok::kw_typeof:
576 ParseTypeofSpecifier(DS);
577 continue;
578
Chris Lattner4b009652007-07-25 00:24:17 +0000579 // type-qualifier
580 case tok::kw_const:
581 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
582 getLang())*2;
583 break;
584 case tok::kw_volatile:
585 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
586 getLang())*2;
587 break;
588 case tok::kw_restrict:
589 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
590 getLang())*2;
591 break;
592
593 // function-specifier
594 case tok::kw_inline:
595 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
596 break;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000597
Steve Naroff5f0466b2008-06-05 00:02:44 +0000598 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000599 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000600 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
601 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000602 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000603 goto DoneWithDeclSpec;
604
605 {
606 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000607 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000608 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000609 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000610 DS.SetRangeEnd(EndProtoLoc);
611
Chris Lattnerb99d7492008-07-26 00:20:22 +0000612 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id,
613 SourceRange(Loc, EndProtoLoc));
Steve Narofff7683302008-09-22 10:28:57 +0000614 // Need to support trailing type qualifiers (e.g. "id<p> const").
615 // If a type specifier follows, it will be diagnosed elsewhere.
616 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000617 }
Chris Lattner4b009652007-07-25 00:24:17 +0000618 }
619 // If the specifier combination wasn't legal, issue a diagnostic.
620 if (isInvalid) {
621 assert(PrevSpec && "Method did not return previous specifier!");
622 if (isInvalid == 1) // Error.
623 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
624 else // extwarn.
625 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
626 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000627 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000628 ConsumeToken();
629 }
630}
631
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000632/// ParseStructDeclaration - Parse a struct declaration without the terminating
633/// semicolon.
634///
Chris Lattner4b009652007-07-25 00:24:17 +0000635/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000636/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000637/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000638/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000639/// struct-declarator-list:
640/// struct-declarator
641/// struct-declarator-list ',' struct-declarator
642/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
643/// struct-declarator:
644/// declarator
645/// [GNU] declarator attributes[opt]
646/// declarator[opt] ':' constant-expression
647/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
648///
Chris Lattner3dd8d392008-04-10 06:46:29 +0000649void Parser::
650ParseStructDeclaration(DeclSpec &DS,
651 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000652 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattner3dd8d392008-04-10 06:46:29 +0000653 while (Tok.is(tok::kw___extension__))
Steve Naroffa9adf112007-08-20 22:28:22 +0000654 ConsumeToken();
655
656 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000657 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +0000658 ParseSpecifierQualifierList(DS);
659 // TODO: Does specifier-qualifier list correctly check that *something* is
660 // specified?
661
662 // If there are no declarators, issue a warning.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000663 if (Tok.is(tok::semi)) {
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000664 Diag(DSStart, diag::w_no_declarators);
Steve Naroffa9adf112007-08-20 22:28:22 +0000665 return;
666 }
667
668 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000669 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000670 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +0000671 FieldDeclarator &DeclaratorInfo = Fields.back();
672
Steve Naroffa9adf112007-08-20 22:28:22 +0000673 /// struct-declarator: declarator
674 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000675 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000676 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +0000677
Chris Lattner34a01ad2007-10-09 17:33:22 +0000678 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000679 ConsumeToken();
680 ExprResult Res = ParseConstantExpression();
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000681 if (Res.isInvalid)
Steve Naroffa9adf112007-08-20 22:28:22 +0000682 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000683 else
Chris Lattner3dd8d392008-04-10 06:46:29 +0000684 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroffa9adf112007-08-20 22:28:22 +0000685 }
686
687 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000688 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000689 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000690
691 // If we don't have a comma, it is either the end of the list (a ';')
692 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000693 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000694 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000695
696 // Consume the comma.
697 ConsumeToken();
698
699 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000700 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000701
702 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000703 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000704 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000705 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000706}
707
708/// ParseStructUnionBody
709/// struct-contents:
710/// struct-declaration-list
711/// [EXT] empty
712/// [GNU] "struct-declaration-list" without terminatoring ';'
713/// struct-declaration-list:
714/// struct-declaration
715/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +0000716/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +0000717///
Chris Lattner4b009652007-07-25 00:24:17 +0000718void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
719 unsigned TagType, DeclTy *TagDecl) {
720 SourceLocation LBraceLoc = ConsumeBrace();
721
722 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
723 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +0000724 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000725 Diag(Tok, diag::ext_empty_struct_union_enum,
726 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
727
728 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000729 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
730
Chris Lattner4b009652007-07-25 00:24:17 +0000731 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000732 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000733 // Each iteration of this loop reads one struct-declaration.
734
735 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000736 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000737 Diag(Tok, diag::ext_extra_struct_semi);
738 ConsumeToken();
739 continue;
740 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000741
742 // Parse all the comma separated declarators.
743 DeclSpec DS;
744 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +0000745 if (!Tok.is(tok::at)) {
746 ParseStructDeclaration(DS, FieldDeclarators);
747
748 // Convert them all to fields.
749 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
750 FieldDeclarator &FD = FieldDeclarators[i];
751 // Install the declarator into the current TagDecl.
752 DeclTy *Field = Actions.ActOnField(CurScope,
753 DS.getSourceRange().getBegin(),
754 FD.D, FD.BitfieldSize);
755 FieldDecls.push_back(Field);
756 }
757 } else { // Handle @defs
758 ConsumeToken();
759 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
760 Diag(Tok, diag::err_unexpected_at);
761 SkipUntil(tok::semi, true, true);
762 continue;
763 }
764 ConsumeToken();
765 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
766 if (!Tok.is(tok::identifier)) {
767 Diag(Tok, diag::err_expected_ident);
768 SkipUntil(tok::semi, true, true);
769 continue;
770 }
771 llvm::SmallVector<DeclTy*, 16> Fields;
772 Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
773 Fields);
774 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
775 ConsumeToken();
776 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
777 }
Chris Lattner4b009652007-07-25 00:24:17 +0000778
Chris Lattner34a01ad2007-10-09 17:33:22 +0000779 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000780 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +0000781 } else if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000782 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
783 break;
784 } else {
785 Diag(Tok, diag::err_expected_semi_decl_list);
786 // Skip to end of block or statement
787 SkipUntil(tok::r_brace, true, true);
788 }
789 }
790
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000791 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000792
Chris Lattner4b009652007-07-25 00:24:17 +0000793 AttributeList *AttrList = 0;
794 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000795 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +0000796 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +0000797
798 Actions.ActOnFields(CurScope,
799 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
800 LBraceLoc, RBraceLoc,
801 AttrList);
Chris Lattner4b009652007-07-25 00:24:17 +0000802}
803
804
805/// ParseEnumSpecifier
806/// enum-specifier: [C99 6.7.2.2]
807/// 'enum' identifier[opt] '{' enumerator-list '}'
808/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
809/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
810/// '}' attributes[opt]
811/// 'enum' identifier
812/// [GNU] 'enum' attributes[opt] identifier
813void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000814 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000815 SourceLocation StartLoc = ConsumeToken();
816
817 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +0000818
819 AttributeList *Attr = 0;
820 // If attributes exist after tag, parse them.
821 if (Tok.is(tok::kw___attribute))
822 Attr = ParseAttributes();
823
824 // Must have either 'enum name' or 'enum {...}'.
825 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
826 Diag(Tok, diag::err_expected_ident_lbrace);
827
828 // Skip the rest of this declarator, up until the comma or semicolon.
829 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +0000830 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +0000831 }
832
833 // If an identifier is present, consume and remember it.
834 IdentifierInfo *Name = 0;
835 SourceLocation NameLoc;
836 if (Tok.is(tok::identifier)) {
837 Name = Tok.getIdentifierInfo();
838 NameLoc = ConsumeToken();
839 }
840
841 // There are three options here. If we have 'enum foo;', then this is a
842 // forward declaration. If we have 'enum foo {...' then this is a
843 // definition. Otherwise we have something like 'enum foo xyz', a reference.
844 //
845 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
846 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
847 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
848 //
849 Action::TagKind TK;
850 if (Tok.is(tok::l_brace))
851 TK = Action::TK_Definition;
852 else if (Tok.is(tok::semi))
853 TK = Action::TK_Declaration;
854 else
855 TK = Action::TK_Reference;
856 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
857 Name, NameLoc, Attr);
Chris Lattner4b009652007-07-25 00:24:17 +0000858
Chris Lattner34a01ad2007-10-09 17:33:22 +0000859 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000860 ParseEnumBody(StartLoc, TagDecl);
861
862 // TODO: semantic analysis on the declspec for enums.
863 const char *PrevSpec = 0;
864 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
865 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
866}
867
868/// ParseEnumBody - Parse a {} enclosed enumerator-list.
869/// enumerator-list:
870/// enumerator
871/// enumerator-list ',' enumerator
872/// enumerator:
873/// enumeration-constant
874/// enumeration-constant '=' constant-expression
875/// enumeration-constant:
876/// identifier
877///
878void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
879 SourceLocation LBraceLoc = ConsumeBrace();
880
Chris Lattnerc9a92452007-08-27 17:24:30 +0000881 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +0000882 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000883 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
884
885 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
886
887 DeclTy *LastEnumConstDecl = 0;
888
889 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000890 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000891 IdentifierInfo *Ident = Tok.getIdentifierInfo();
892 SourceLocation IdentLoc = ConsumeToken();
893
894 SourceLocation EqualLoc;
895 ExprTy *AssignedVal = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000896 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000897 EqualLoc = ConsumeToken();
898 ExprResult Res = ParseConstantExpression();
899 if (Res.isInvalid)
900 SkipUntil(tok::comma, tok::r_brace, true, true);
901 else
902 AssignedVal = Res.Val;
903 }
904
905 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000906 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +0000907 LastEnumConstDecl,
908 IdentLoc, Ident,
909 EqualLoc, AssignedVal);
910 EnumConstantDecls.push_back(EnumConstDecl);
911 LastEnumConstDecl = EnumConstDecl;
912
Chris Lattner34a01ad2007-10-09 17:33:22 +0000913 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000914 break;
915 SourceLocation CommaLoc = ConsumeToken();
916
Chris Lattner34a01ad2007-10-09 17:33:22 +0000917 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +0000918 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
919 }
920
921 // Eat the }.
922 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
923
Steve Naroff0acc9c92007-09-15 18:49:24 +0000924 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +0000925 EnumConstantDecls.size());
926
927 DeclTy *AttrList = 0;
928 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000929 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000930 AttrList = ParseAttributes(); // FIXME: where do they do?
931}
932
933/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +0000934/// start of a type-qualifier-list.
935bool Parser::isTypeQualifier() const {
936 switch (Tok.getKind()) {
937 default: return false;
938 // type-qualifier
939 case tok::kw_const:
940 case tok::kw_volatile:
941 case tok::kw_restrict:
942 return true;
943 }
944}
945
946/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +0000947/// start of a specifier-qualifier-list.
948bool Parser::isTypeSpecifierQualifier() const {
949 switch (Tok.getKind()) {
950 default: return false;
951 // GNU attributes support.
952 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000953 // GNU typeof support.
954 case tok::kw_typeof:
Steve Naroff5f0466b2008-06-05 00:02:44 +0000955 // GNU bizarre protocol extension. FIXME: make an extension?
956 case tok::less:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000957
Chris Lattner4b009652007-07-25 00:24:17 +0000958 // type-specifiers
959 case tok::kw_short:
960 case tok::kw_long:
961 case tok::kw_signed:
962 case tok::kw_unsigned:
963 case tok::kw__Complex:
964 case tok::kw__Imaginary:
965 case tok::kw_void:
966 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000967 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +0000968 case tok::kw_int:
969 case tok::kw_float:
970 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000971 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +0000972 case tok::kw__Bool:
973 case tok::kw__Decimal32:
974 case tok::kw__Decimal64:
975 case tok::kw__Decimal128:
976
Chris Lattner2e78db32008-04-13 18:59:07 +0000977 // struct-or-union-specifier (C99) or class-specifier (C++)
978 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000979 case tok::kw_struct:
980 case tok::kw_union:
981 // enum-specifier
982 case tok::kw_enum:
983
984 // type-qualifier
985 case tok::kw_const:
986 case tok::kw_volatile:
987 case tok::kw_restrict:
988 return true;
989
990 // typedef-name
991 case tok::identifier:
992 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000993 }
994}
995
996/// isDeclarationSpecifier() - Return true if the current token is part of a
997/// declaration specifier.
998bool Parser::isDeclarationSpecifier() const {
999 switch (Tok.getKind()) {
1000 default: return false;
1001 // storage-class-specifier
1002 case tok::kw_typedef:
1003 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001004 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001005 case tok::kw_static:
1006 case tok::kw_auto:
1007 case tok::kw_register:
1008 case tok::kw___thread:
1009
1010 // type-specifiers
1011 case tok::kw_short:
1012 case tok::kw_long:
1013 case tok::kw_signed:
1014 case tok::kw_unsigned:
1015 case tok::kw__Complex:
1016 case tok::kw__Imaginary:
1017 case tok::kw_void:
1018 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001019 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001020 case tok::kw_int:
1021 case tok::kw_float:
1022 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001023 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001024 case tok::kw__Bool:
1025 case tok::kw__Decimal32:
1026 case tok::kw__Decimal64:
1027 case tok::kw__Decimal128:
1028
Chris Lattner2e78db32008-04-13 18:59:07 +00001029 // struct-or-union-specifier (C99) or class-specifier (C++)
1030 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001031 case tok::kw_struct:
1032 case tok::kw_union:
1033 // enum-specifier
1034 case tok::kw_enum:
1035
1036 // type-qualifier
1037 case tok::kw_const:
1038 case tok::kw_volatile:
1039 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001040
Chris Lattner4b009652007-07-25 00:24:17 +00001041 // function-specifier
1042 case tok::kw_inline:
Chris Lattnere35d2582007-08-09 16:40:21 +00001043
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001044 // GNU typeof support.
1045 case tok::kw_typeof:
1046
1047 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001048 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001049 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001050
1051 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1052 case tok::less:
1053 return getLang().ObjC1;
Chris Lattner4b009652007-07-25 00:24:17 +00001054
1055 // typedef-name
1056 case tok::identifier:
1057 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001058 }
1059}
1060
1061
1062/// ParseTypeQualifierListOpt
1063/// type-qualifier-list: [C99 6.7.5]
1064/// type-qualifier
1065/// [GNU] attributes
1066/// type-qualifier-list type-qualifier
1067/// [GNU] type-qualifier-list attributes
1068///
1069void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1070 while (1) {
1071 int isInvalid = false;
1072 const char *PrevSpec = 0;
1073 SourceLocation Loc = Tok.getLocation();
1074
1075 switch (Tok.getKind()) {
1076 default:
1077 // If this is not a type-qualifier token, we're done reading type
1078 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +00001079 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +00001080 return;
1081 case tok::kw_const:
1082 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1083 getLang())*2;
1084 break;
1085 case tok::kw_volatile:
1086 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1087 getLang())*2;
1088 break;
1089 case tok::kw_restrict:
1090 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1091 getLang())*2;
1092 break;
1093 case tok::kw___attribute:
1094 DS.AddAttributes(ParseAttributes());
1095 continue; // do *not* consume the next token!
1096 }
1097
1098 // If the specifier combination wasn't legal, issue a diagnostic.
1099 if (isInvalid) {
1100 assert(PrevSpec && "Method did not return previous specifier!");
1101 if (isInvalid == 1) // Error.
1102 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1103 else // extwarn.
1104 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1105 }
1106 ConsumeToken();
1107 }
1108}
1109
1110
1111/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1112///
1113void Parser::ParseDeclarator(Declarator &D) {
1114 /// This implements the 'declarator' production in the C grammar, then checks
1115 /// for well-formedness and issues diagnostics.
1116 ParseDeclaratorInternal(D);
Chris Lattner4b009652007-07-25 00:24:17 +00001117}
1118
1119/// ParseDeclaratorInternal
1120/// declarator: [C99 6.7.5]
1121/// pointer[opt] direct-declarator
1122/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1123/// [GNU] '&' restrict[opt] attributes[opt] declarator
1124///
1125/// pointer: [C99 6.7.5]
1126/// '*' type-qualifier-list[opt]
1127/// '*' type-qualifier-list[opt] pointer
1128///
1129void Parser::ParseDeclaratorInternal(Declarator &D) {
1130 tok::TokenKind Kind = Tok.getKind();
1131
Steve Naroff7aa54752008-08-27 16:04:49 +00001132 // Not a pointer, C++ reference, or block.
1133 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
1134 (Kind != tok::caret || !getLang().Blocks))
Chris Lattner4b009652007-07-25 00:24:17 +00001135 return ParseDirectDeclarator(D);
1136
Steve Naroffdc22f212008-08-28 10:07:06 +00001137 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Chris Lattner4b009652007-07-25 00:24:17 +00001138 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1139
Steve Naroffdc22f212008-08-28 10:07:06 +00001140 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner69f01932008-02-21 01:32:26 +00001141 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001142 DeclSpec DS;
1143
1144 ParseTypeQualifierListOpt(DS);
1145
1146 // Recursively parse the declarator.
1147 ParseDeclaratorInternal(D);
Steve Naroff7aa54752008-08-27 16:04:49 +00001148 if (Kind == tok::star)
1149 // Remember that we parsed a pointer type, and remember the type-quals.
1150 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1151 DS.TakeAttributes()));
1152 else
1153 // Remember that we parsed a Block type, and remember the type-quals.
1154 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1155 Loc));
Chris Lattner4b009652007-07-25 00:24:17 +00001156 } else {
1157 // Is a reference
1158 DeclSpec DS;
1159
1160 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1161 // cv-qualifiers are introduced through the use of a typedef or of a
1162 // template type argument, in which case the cv-qualifiers are ignored.
1163 //
1164 // [GNU] Retricted references are allowed.
1165 // [GNU] Attributes on references are allowed.
1166 ParseTypeQualifierListOpt(DS);
1167
1168 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1169 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1170 Diag(DS.getConstSpecLoc(),
1171 diag::err_invalid_reference_qualifier_application,
1172 "const");
1173 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1174 Diag(DS.getVolatileSpecLoc(),
1175 diag::err_invalid_reference_qualifier_application,
1176 "volatile");
1177 }
1178
1179 // Recursively parse the declarator.
1180 ParseDeclaratorInternal(D);
1181
1182 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001183 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1184 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001185 }
1186}
1187
1188/// ParseDirectDeclarator
1189/// direct-declarator: [C99 6.7.5]
1190/// identifier
1191/// '(' declarator ')'
1192/// [GNU] '(' attributes declarator ')'
1193/// [C90] direct-declarator '[' constant-expression[opt] ']'
1194/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1195/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1196/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1197/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1198/// direct-declarator '(' parameter-type-list ')'
1199/// direct-declarator '(' identifier-list[opt] ')'
1200/// [GNU] direct-declarator '(' parameter-forward-declarations
1201/// parameter-type-list[opt] ')'
1202///
1203void Parser::ParseDirectDeclarator(Declarator &D) {
1204 // Parse the first direct-declarator seen.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001205 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001206 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1207 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1208 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001209 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001210 // direct-declarator: '(' declarator ')'
1211 // direct-declarator: '(' attributes declarator ')'
1212 // Example: 'char (*X)' or 'int (*XX)(void)'
1213 ParseParenDeclarator(D);
1214 } else if (D.mayOmitIdentifier()) {
1215 // This could be something simple like "int" (in which case the declarator
1216 // portion is empty), if an abstract-declarator is allowed.
1217 D.SetIdentifier(0, Tok.getLocation());
1218 } else {
1219 // Expected identifier or '('.
1220 Diag(Tok, diag::err_expected_ident_lparen);
1221 D.SetIdentifier(0, Tok.getLocation());
1222 }
1223
1224 assert(D.isPastIdentifier() &&
1225 "Haven't past the location of the identifier yet?");
1226
1227 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001228 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001229 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1230 // In such a case, check if we actually have a function declarator; if it
1231 // is not, the declarator has been fully parsed.
1232 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit() &&
1233 !isCXXFunctionDeclarator())
1234 break;
Chris Lattnera0d056d2008-04-06 05:45:57 +00001235 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001236 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001237 ParseBracketDeclarator(D);
1238 } else {
1239 break;
1240 }
1241 }
1242}
1243
Chris Lattnera0d056d2008-04-06 05:45:57 +00001244/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1245/// only called before the identifier, so these are most likely just grouping
1246/// parens for precedence. If we find that these are actually function
1247/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1248///
1249/// direct-declarator:
1250/// '(' declarator ')'
1251/// [GNU] '(' attributes declarator ')'
1252///
1253void Parser::ParseParenDeclarator(Declarator &D) {
1254 SourceLocation StartLoc = ConsumeParen();
1255 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1256
1257 // If we haven't past the identifier yet (or where the identifier would be
1258 // stored, if this is an abstract declarator), then this is probably just
1259 // grouping parens. However, if this could be an abstract-declarator, then
1260 // this could also be the start of function arguments (consider 'void()').
1261 bool isGrouping;
1262
1263 if (!D.mayOmitIdentifier()) {
1264 // If this can't be an abstract-declarator, this *must* be a grouping
1265 // paren, because we haven't seen the identifier yet.
1266 isGrouping = true;
1267 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001268 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00001269 isDeclarationSpecifier()) { // 'int(int)' is a function.
1270 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1271 // considered to be a type, not a K&R identifier-list.
1272 isGrouping = false;
1273 } else {
1274 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1275 isGrouping = true;
1276 }
1277
1278 // If this is a grouping paren, handle:
1279 // direct-declarator: '(' declarator ')'
1280 // direct-declarator: '(' attributes declarator ')'
1281 if (isGrouping) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001282 D.setGroupingParens(true);
1283
Chris Lattnera0d056d2008-04-06 05:45:57 +00001284 if (Tok.is(tok::kw___attribute))
1285 D.AddAttributes(ParseAttributes());
1286
1287 ParseDeclaratorInternal(D);
1288 // Match the ')'.
1289 MatchRHSPunctuation(tok::r_paren, StartLoc);
1290 return;
1291 }
1292
1293 // Okay, if this wasn't a grouping paren, it must be the start of a function
1294 // argument list. Recognize that this declarator will never have an
1295 // identifier (and remember where it would have been), then fall through to
1296 // the handling of argument lists.
1297 D.SetIdentifier(0, Tok.getLocation());
1298
1299 ParseFunctionDeclarator(StartLoc, D);
1300}
1301
1302/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1303/// declarator D up to a paren, which indicates that we are parsing function
1304/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001305///
1306/// This method also handles this portion of the grammar:
1307/// parameter-type-list: [C99 6.7.5]
1308/// parameter-list
1309/// parameter-list ',' '...'
1310///
1311/// parameter-list: [C99 6.7.5]
1312/// parameter-declaration
1313/// parameter-list ',' parameter-declaration
1314///
1315/// parameter-declaration: [C99 6.7.5]
1316/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00001317/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001318/// [GNU] declaration-specifiers declarator attributes
1319/// declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00001320/// [C++] declaration-specifiers abstract-declarator[opt]
1321/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001322/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1323///
Chris Lattnera0d056d2008-04-06 05:45:57 +00001324void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D) {
1325 // lparen is already consumed!
1326 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00001327
1328 // Okay, this is the parameter list of a function definition, or it is an
1329 // identifier list of a K&R-style function.
Chris Lattner4b009652007-07-25 00:24:17 +00001330
Chris Lattner34a01ad2007-10-09 17:33:22 +00001331 if (Tok.is(tok::r_paren)) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00001332 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00001333 // int() -> no prototype, no '...'.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001334 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/ false,
1335 /*variadic*/ false,
1336 /*arglist*/ 0, 0, LParenLoc));
1337
1338 ConsumeParen(); // Eat the closing ')'.
1339 return;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001340 } else if (Tok.is(tok::identifier) &&
Chris Lattner4b009652007-07-25 00:24:17 +00001341 // K&R identifier lists can't have typedefs as identifiers, per
1342 // C99 6.7.5.3p11.
1343 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1344 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1345 // normal declarators, not for abstract-declarators.
Chris Lattner35d9c912008-04-06 06:34:08 +00001346 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001347 }
1348
1349 // Finally, a normal, non-empty parameter type list.
1350
1351 // Build up an array of information about the parsed arguments.
1352 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00001353
1354 // Enter function-declaration scope, limiting any declarators to the
1355 // function prototype scope, including parameter declarators.
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00001356 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001357
1358 bool IsVariadic = false;
1359 while (1) {
1360 if (Tok.is(tok::ellipsis)) {
1361 IsVariadic = true;
Chris Lattner4b009652007-07-25 00:24:17 +00001362
Chris Lattner9f7564b2008-04-06 06:57:35 +00001363 // Check to see if this is "void(...)" which is not allowed.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001364 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00001365 // Otherwise, parse parameter type list. If it starts with an
1366 // ellipsis, diagnose the malformed function.
1367 Diag(Tok, diag::err_ellipsis_first_arg);
1368 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001369 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00001370
Chris Lattner9f7564b2008-04-06 06:57:35 +00001371 ConsumeToken(); // Consume the ellipsis.
1372 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001373 }
1374
Chris Lattner9f7564b2008-04-06 06:57:35 +00001375 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00001376
Chris Lattner9f7564b2008-04-06 06:57:35 +00001377 // Parse the declaration-specifiers.
1378 DeclSpec DS;
1379 ParseDeclarationSpecifiers(DS);
1380
1381 // Parse the declarator. This is "PrototypeContext", because we must
1382 // accept either 'declarator' or 'abstract-declarator' here.
1383 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1384 ParseDeclarator(ParmDecl);
1385
1386 // Parse GNU attributes, if present.
1387 if (Tok.is(tok::kw___attribute))
1388 ParmDecl.AddAttributes(ParseAttributes());
1389
Chris Lattner9f7564b2008-04-06 06:57:35 +00001390 // Remember this parsed parameter in ParamInfo.
1391 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1392
Chris Lattner9f7564b2008-04-06 06:57:35 +00001393 // If no parameter was specified, verify that *something* was specified,
1394 // otherwise we have a missing type and identifier.
1395 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1396 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1397 // Completely missing, emit error.
1398 Diag(DSStart, diag::err_missing_param);
1399 } else {
1400 // Otherwise, we have something. Add it and let semantic analysis try
1401 // to grok it and add the result to the ParamInfo we are building.
1402
1403 // Inform the actions module about the parameter declarator, so it gets
1404 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00001405 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1406
1407 // Parse the default argument, if any. We parse the default
1408 // arguments in all dialects; the semantic analysis in
1409 // ActOnParamDefaultArgument will reject the default argument in
1410 // C.
1411 if (Tok.is(tok::equal)) {
1412 SourceLocation EqualLoc = Tok.getLocation();
1413
1414 // Consume the '='.
1415 ConsumeToken();
1416
1417 // Parse the default argument
Chris Lattner3e254fb2008-04-08 04:40:51 +00001418 ExprResult DefArgResult = ParseAssignmentExpression();
1419 if (DefArgResult.isInvalid) {
1420 SkipUntil(tok::comma, tok::r_paren, true, true);
1421 } else {
1422 // Inform the actions module about the default argument
1423 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1424 }
1425 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001426
1427 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001428 ParmDecl.getIdentifierLoc(), Param));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001429 }
1430
1431 // If the next token is a comma, consume it and keep reading arguments.
1432 if (Tok.isNot(tok::comma)) break;
1433
1434 // Consume the comma.
1435 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00001436 }
1437
Chris Lattner9f7564b2008-04-06 06:57:35 +00001438 // Leave prototype scope.
1439 ExitScope();
1440
Chris Lattner4b009652007-07-25 00:24:17 +00001441 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001442 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1443 &ParamInfo[0], ParamInfo.size(),
1444 LParenLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00001445
1446 // If we have the closing ')', eat it and we're done.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001447 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001448}
1449
Chris Lattner35d9c912008-04-06 06:34:08 +00001450/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1451/// we found a K&R-style identifier list instead of a type argument list. The
1452/// current token is known to be the first identifier in the list.
1453///
1454/// identifier-list: [C99 6.7.5]
1455/// identifier
1456/// identifier-list ',' identifier
1457///
1458void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1459 Declarator &D) {
1460 // Build up an array of information about the parsed arguments.
1461 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1462 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1463
1464 // If there was no identifier specified for the declarator, either we are in
1465 // an abstract-declarator, or we are in a parameter declarator which was found
1466 // to be abstract. In abstract-declarators, identifier lists are not valid:
1467 // diagnose this.
1468 if (!D.getIdentifier())
1469 Diag(Tok, diag::ext_ident_list_in_param);
1470
1471 // Tok is known to be the first identifier in the list. Remember this
1472 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00001473 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00001474 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1475 Tok.getLocation(), 0));
1476
Chris Lattner113a56b2008-04-06 06:39:19 +00001477 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00001478
1479 while (Tok.is(tok::comma)) {
1480 // Eat the comma.
1481 ConsumeToken();
1482
Chris Lattner113a56b2008-04-06 06:39:19 +00001483 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00001484 if (Tok.isNot(tok::identifier)) {
1485 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00001486 SkipUntil(tok::r_paren);
1487 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00001488 }
Chris Lattneracb67d92008-04-06 06:47:48 +00001489
Chris Lattner35d9c912008-04-06 06:34:08 +00001490 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00001491
1492 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1493 if (Actions.isTypeName(*ParmII, CurScope))
1494 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner35d9c912008-04-06 06:34:08 +00001495
1496 // Verify that the argument identifier has not already been mentioned.
1497 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner113a56b2008-04-06 06:39:19 +00001498 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1499 } else {
1500 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00001501 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1502 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00001503 }
Chris Lattner35d9c912008-04-06 06:34:08 +00001504
1505 // Eat the identifier.
1506 ConsumeToken();
1507 }
1508
Chris Lattner113a56b2008-04-06 06:39:19 +00001509 // Remember that we parsed a function type, and remember the attributes. This
1510 // function type is always a K&R style function type, which is not varargs and
1511 // has no prototype.
1512 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1513 &ParamInfo[0], ParamInfo.size(),
1514 LParenLoc));
Chris Lattner35d9c912008-04-06 06:34:08 +00001515
1516 // If we have the closing ')', eat it and we're done.
Chris Lattner113a56b2008-04-06 06:39:19 +00001517 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00001518}
Chris Lattnera0d056d2008-04-06 05:45:57 +00001519
Chris Lattner4b009652007-07-25 00:24:17 +00001520/// [C90] direct-declarator '[' constant-expression[opt] ']'
1521/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1522/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1523/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1524/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1525void Parser::ParseBracketDeclarator(Declarator &D) {
1526 SourceLocation StartLoc = ConsumeBracket();
1527
1528 // If valid, this location is the position where we read the 'static' keyword.
1529 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001530 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001531 StaticLoc = ConsumeToken();
1532
1533 // If there is a type-qualifier-list, read it now.
1534 DeclSpec DS;
1535 ParseTypeQualifierListOpt(DS);
1536
1537 // If we haven't already read 'static', check to see if there is one after the
1538 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001539 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001540 StaticLoc = ConsumeToken();
1541
1542 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1543 bool isStar = false;
1544 ExprResult NumElements(false);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001545
1546 // Handle the case where we have '[*]' as the array size. However, a leading
1547 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1548 // the the token after the star is a ']'. Since stars in arrays are
1549 // infrequent, use of lookahead is not costly here.
1550 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00001551 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00001552
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001553 if (StaticLoc.isValid())
1554 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1555 StaticLoc = SourceLocation(); // Drop the static.
1556 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001557 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001558 // Parse the assignment-expression now.
1559 NumElements = ParseAssignmentExpression();
1560 }
1561
1562 // If there was an error parsing the assignment-expression, recover.
1563 if (NumElements.isInvalid) {
1564 // If the expression was invalid, skip it.
1565 SkipUntil(tok::r_square);
1566 return;
1567 }
1568
1569 MatchRHSPunctuation(tok::r_square, StartLoc);
1570
1571 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1572 // it was not a constant expression.
1573 if (!getLang().C99) {
1574 // TODO: check C90 array constant exprness.
1575 if (isStar || StaticLoc.isValid() ||
1576 0/*TODO: NumElts is not a C90 constantexpr */)
1577 Diag(StartLoc, diag::ext_c99_array_usage);
1578 }
1579
1580 // Remember that we parsed a pointer type, and remember the type-quals.
1581 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1582 StaticLoc.isValid(), isStar,
1583 NumElements.Val, StartLoc));
1584}
1585
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00001586/// [GNU] typeof-specifier:
1587/// typeof ( expressions )
1588/// typeof ( type-name )
1589/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00001590///
1591void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001592 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00001593 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00001594 SourceLocation StartLoc = ConsumeToken();
1595
Chris Lattner34a01ad2007-10-09 17:33:22 +00001596 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00001597 if (!getLang().CPlusPlus) {
1598 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1599 return;
1600 }
1601
1602 ExprResult Result = ParseCastExpression(true/*isUnaryExpression*/);
1603 if (Result.isInvalid)
1604 return;
1605
1606 const char *PrevSpec = 0;
1607 // Check for duplicate type specifiers.
1608 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1609 Result.Val))
1610 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
1611
1612 // FIXME: Not accurate, the range gets one token more than it should.
1613 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00001614 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00001615 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00001616
Steve Naroff7cbb1462007-07-31 12:34:36 +00001617 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1618
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00001619 if (isTypeIdInParens()) {
Steve Naroff7cbb1462007-07-31 12:34:36 +00001620 TypeTy *Ty = ParseTypeName();
1621
Steve Naroff4c255ab2007-07-31 23:56:32 +00001622 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1623
Chris Lattner34a01ad2007-10-09 17:33:22 +00001624 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001625 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001626 return;
1627 }
1628 RParenLoc = ConsumeParen();
1629 const char *PrevSpec = 0;
1630 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1631 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1632 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001633 } else { // we have an expression.
1634 ExprResult Result = ParseExpression();
Steve Naroff4c255ab2007-07-31 23:56:32 +00001635
Chris Lattner34a01ad2007-10-09 17:33:22 +00001636 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001637 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001638 return;
1639 }
1640 RParenLoc = ConsumeParen();
1641 const char *PrevSpec = 0;
1642 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1643 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1644 Result.Val))
1645 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001646 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00001647 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001648}
1649
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00001650