blob: 9dffa7730d25cbbf18cf5a39aea99258e397d563 [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.
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000280 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Steve Naroff6a0e2092007-09-12 14:07:44 +0000281
Chris Lattner4b009652007-07-25 00:24:17 +0000282 // Parse declarator '=' initializer.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000283 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000284 ConsumeToken();
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000285 ExprResult Init = ParseInitializer();
Chris Lattner4b009652007-07-25 00:24:17 +0000286 if (Init.isInvalid) {
287 SkipUntil(tok::semi);
288 return 0;
289 }
Steve Naroff6a0e2092007-09-12 14:07:44 +0000290 Actions.AddInitializerToDecl(LastDeclInGroup, Init.Val);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000291 } else if (Tok.is(tok::l_paren)) {
292 // Parse C++ direct initializer: '(' expression-list ')'
293 SourceLocation LParenLoc = ConsumeParen();
294 ExprListTy Exprs;
295 CommaLocsTy CommaLocs;
296
297 bool InvalidExpr = false;
298 if (ParseExpressionList(Exprs, CommaLocs)) {
299 SkipUntil(tok::r_paren);
300 InvalidExpr = true;
301 }
302 // Match the ')'.
303 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
304
305 if (!InvalidExpr) {
306 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
307 "Unexpected number of commas!");
308 Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc,
309 &Exprs[0], Exprs.size(),
310 &CommaLocs[0], RParenLoc);
311 }
Chris Lattner4b009652007-07-25 00:24:17 +0000312 }
313
Chris Lattner4b009652007-07-25 00:24:17 +0000314 // If we don't have a comma, it is either the end of the list (a ';') or an
315 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000316 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000317 break;
318
319 // Consume the comma.
320 ConsumeToken();
321
322 // Parse the next declarator.
323 D.clear();
324 ParseDeclarator(D);
325 }
326
Chris Lattner34a01ad2007-10-09 17:33:22 +0000327 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000328 ConsumeToken();
329 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
330 }
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000331 // If this is an ObjC2 for-each loop, this is a successful declarator
332 // parse. The syntax for these looks like:
333 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000334 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000335 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
336 }
Chris Lattner4b009652007-07-25 00:24:17 +0000337 Diag(Tok, diag::err_parse_error);
338 // Skip to end of block or statement
Chris Lattnerf491b412007-08-21 18:36:18 +0000339 SkipUntil(tok::r_brace, true, true);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000340 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000341 ConsumeToken();
342 return 0;
343}
344
345/// ParseSpecifierQualifierList
346/// specifier-qualifier-list:
347/// type-specifier specifier-qualifier-list[opt]
348/// type-qualifier specifier-qualifier-list[opt]
349/// [GNU] attributes specifier-qualifier-list[opt]
350///
351void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
352 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
353 /// parse declaration-specifiers and complain about extra stuff.
354 ParseDeclarationSpecifiers(DS);
355
356 // Validate declspec for type-name.
357 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000358 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000359 Diag(Tok, diag::err_typename_requires_specqual);
360
361 // Issue diagnostic and remove storage class if present.
362 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
363 if (DS.getStorageClassSpecLoc().isValid())
364 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
365 else
366 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
367 DS.ClearStorageClassSpecs();
368 }
369
370 // Issue diagnostic and remove function specfier if present.
371 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
372 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
373 DS.ClearFunctionSpecs();
374 }
375}
376
377/// ParseDeclarationSpecifiers
378/// declaration-specifiers: [C99 6.7]
379/// storage-class-specifier declaration-specifiers[opt]
380/// type-specifier declaration-specifiers[opt]
381/// type-qualifier declaration-specifiers[opt]
382/// [C99] function-specifier declaration-specifiers[opt]
383/// [GNU] attributes declaration-specifiers[opt]
384///
385/// storage-class-specifier: [C99 6.7.1]
386/// 'typedef'
387/// 'extern'
388/// 'static'
389/// 'auto'
390/// 'register'
391/// [GNU] '__thread'
392/// type-specifier: [C99 6.7.2]
393/// 'void'
394/// 'char'
395/// 'short'
396/// 'int'
397/// 'long'
398/// 'float'
399/// 'double'
400/// 'signed'
401/// 'unsigned'
402/// struct-or-union-specifier
403/// enum-specifier
404/// typedef-name
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000405/// [C++] 'wchar_t'
Chris Lattner4b009652007-07-25 00:24:17 +0000406/// [C++] 'bool'
407/// [C99] '_Bool'
408/// [C99] '_Complex'
409/// [C99] '_Imaginary' // Removed in TC2?
410/// [GNU] '_Decimal32'
411/// [GNU] '_Decimal64'
412/// [GNU] '_Decimal128'
Steve Naroff4c255ab2007-07-31 23:56:32 +0000413/// [GNU] typeof-specifier
Chris Lattner4b009652007-07-25 00:24:17 +0000414/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroffa8ee2262007-08-22 23:18:22 +0000415/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattner4b009652007-07-25 00:24:17 +0000416/// type-qualifier:
417/// 'const'
418/// 'volatile'
419/// [C99] 'restrict'
420/// function-specifier: [C99 6.7.4]
421/// [C99] 'inline'
422///
423void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattnera4ff4272008-03-13 06:29:04 +0000424 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000425 while (1) {
426 int isInvalid = false;
427 const char *PrevSpec = 0;
428 SourceLocation Loc = Tok.getLocation();
429
430 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000431 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000432 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000433 // If this is not a declaration specifier token, we're done reading decl
434 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000435 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000436 return;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000437
438 // typedef-name
439 case tok::identifier: {
440 // This identifier can only be a typedef name if we haven't already seen
441 // a type-specifier. Without this check we misparse:
442 // typedef int X; struct Y { short X; }; as 'short int'.
443 if (DS.hasTypeSpecifier())
444 goto DoneWithDeclSpec;
445
446 // It has to be available as a typedef too!
Argiris Kirtzidis46403632008-08-01 10:35:27 +0000447 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattnerfda18db2008-07-26 01:18:38 +0000448 if (TypeRep == 0)
449 goto DoneWithDeclSpec;
450
451 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
452 TypeRep);
453 if (isInvalid)
454 break;
455
456 DS.SetRangeEnd(Tok.getLocation());
457 ConsumeToken(); // The identifier
458
459 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
460 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
461 // Objective-C interface. If we don't have Objective-C or a '<', this is
462 // just a normal reference to a typedef name.
463 if (!Tok.is(tok::less) || !getLang().ObjC1)
464 continue;
465
466 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000467 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000468 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000469 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000470
471 DS.SetRangeEnd(EndProtoLoc);
472
Steve Narofff7683302008-09-22 10:28:57 +0000473 // Need to support trailing type qualifiers (e.g. "id<p> const").
474 // If a type specifier follows, it will be diagnosed elsewhere.
475 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000476 }
Chris Lattner4b009652007-07-25 00:24:17 +0000477 // GNU attributes support.
478 case tok::kw___attribute:
479 DS.AddAttributes(ParseAttributes());
480 continue;
481
482 // storage-class-specifier
483 case tok::kw_typedef:
484 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
485 break;
486 case tok::kw_extern:
487 if (DS.isThreadSpecified())
488 Diag(Tok, diag::ext_thread_before, "extern");
489 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
490 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000491 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000492 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
493 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000494 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000495 case tok::kw_static:
496 if (DS.isThreadSpecified())
497 Diag(Tok, diag::ext_thread_before, "static");
498 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
499 break;
500 case tok::kw_auto:
501 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
502 break;
503 case tok::kw_register:
504 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
505 break;
506 case tok::kw___thread:
507 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
508 break;
509
510 // type-specifiers
511 case tok::kw_short:
512 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
513 break;
514 case tok::kw_long:
515 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
516 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
517 else
518 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
519 break;
520 case tok::kw_signed:
521 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
522 break;
523 case tok::kw_unsigned:
524 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
525 break;
526 case tok::kw__Complex:
527 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
528 break;
529 case tok::kw__Imaginary:
530 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
531 break;
532 case tok::kw_void:
533 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
534 break;
535 case tok::kw_char:
536 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
537 break;
538 case tok::kw_int:
539 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
540 break;
541 case tok::kw_float:
542 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
543 break;
544 case tok::kw_double:
545 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
546 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000547 case tok::kw_wchar_t: // [C++ 2.11p1]
548 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
549 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000550 case tok::kw_bool: // [C++ 2.11p1]
551 case tok::kw__Bool:
552 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
553 break;
554 case tok::kw__Decimal32:
555 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
556 break;
557 case tok::kw__Decimal64:
558 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
559 break;
560 case tok::kw__Decimal128:
561 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
562 break;
Chris Lattner2e78db32008-04-13 18:59:07 +0000563
564 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000565 case tok::kw_struct:
566 case tok::kw_union:
Douglas Gregorec93f442008-04-13 21:30:24 +0000567 ParseClassSpecifier(DS);
Chris Lattner4b009652007-07-25 00:24:17 +0000568 continue;
569 case tok::kw_enum:
570 ParseEnumSpecifier(DS);
571 continue;
572
Steve Naroff7cbb1462007-07-31 12:34:36 +0000573 // GNU typeof support.
574 case tok::kw_typeof:
575 ParseTypeofSpecifier(DS);
576 continue;
577
Chris Lattner4b009652007-07-25 00:24:17 +0000578 // type-qualifier
579 case tok::kw_const:
580 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
581 getLang())*2;
582 break;
583 case tok::kw_volatile:
584 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
585 getLang())*2;
586 break;
587 case tok::kw_restrict:
588 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
589 getLang())*2;
590 break;
591
592 // function-specifier
593 case tok::kw_inline:
594 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
595 break;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000596
Steve Naroff5f0466b2008-06-05 00:02:44 +0000597 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000598 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000599 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
600 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000601 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000602 goto DoneWithDeclSpec;
603
604 {
605 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000606 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000607 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000608 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000609 DS.SetRangeEnd(EndProtoLoc);
610
Chris Lattnerb99d7492008-07-26 00:20:22 +0000611 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id,
612 SourceRange(Loc, EndProtoLoc));
Steve Narofff7683302008-09-22 10:28:57 +0000613 // Need to support trailing type qualifiers (e.g. "id<p> const").
614 // If a type specifier follows, it will be diagnosed elsewhere.
615 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000616 }
Chris Lattner4b009652007-07-25 00:24:17 +0000617 }
618 // If the specifier combination wasn't legal, issue a diagnostic.
619 if (isInvalid) {
620 assert(PrevSpec && "Method did not return previous specifier!");
621 if (isInvalid == 1) // Error.
622 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
623 else // extwarn.
624 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
625 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000626 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000627 ConsumeToken();
628 }
629}
630
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000631/// ParseStructDeclaration - Parse a struct declaration without the terminating
632/// semicolon.
633///
Chris Lattner4b009652007-07-25 00:24:17 +0000634/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000635/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000636/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000637/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000638/// struct-declarator-list:
639/// struct-declarator
640/// struct-declarator-list ',' struct-declarator
641/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
642/// struct-declarator:
643/// declarator
644/// [GNU] declarator attributes[opt]
645/// declarator[opt] ':' constant-expression
646/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
647///
Chris Lattner3dd8d392008-04-10 06:46:29 +0000648void Parser::
649ParseStructDeclaration(DeclSpec &DS,
650 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000651 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattner3dd8d392008-04-10 06:46:29 +0000652 while (Tok.is(tok::kw___extension__))
Steve Naroffa9adf112007-08-20 22:28:22 +0000653 ConsumeToken();
654
655 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000656 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +0000657 ParseSpecifierQualifierList(DS);
658 // TODO: Does specifier-qualifier list correctly check that *something* is
659 // specified?
660
661 // If there are no declarators, issue a warning.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000662 if (Tok.is(tok::semi)) {
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000663 Diag(DSStart, diag::w_no_declarators);
Steve Naroffa9adf112007-08-20 22:28:22 +0000664 return;
665 }
666
667 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000668 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000669 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +0000670 FieldDeclarator &DeclaratorInfo = Fields.back();
671
Steve Naroffa9adf112007-08-20 22:28:22 +0000672 /// struct-declarator: declarator
673 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000674 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000675 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +0000676
Chris Lattner34a01ad2007-10-09 17:33:22 +0000677 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000678 ConsumeToken();
679 ExprResult Res = ParseConstantExpression();
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000680 if (Res.isInvalid)
Steve Naroffa9adf112007-08-20 22:28:22 +0000681 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000682 else
Chris Lattner3dd8d392008-04-10 06:46:29 +0000683 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroffa9adf112007-08-20 22:28:22 +0000684 }
685
686 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000687 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000688 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000689
690 // If we don't have a comma, it is either the end of the list (a ';')
691 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000692 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000693 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000694
695 // Consume the comma.
696 ConsumeToken();
697
698 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000699 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000700
701 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000702 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000703 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000704 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000705}
706
707/// ParseStructUnionBody
708/// struct-contents:
709/// struct-declaration-list
710/// [EXT] empty
711/// [GNU] "struct-declaration-list" without terminatoring ';'
712/// struct-declaration-list:
713/// struct-declaration
714/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +0000715/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +0000716///
Chris Lattner4b009652007-07-25 00:24:17 +0000717void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
718 unsigned TagType, DeclTy *TagDecl) {
719 SourceLocation LBraceLoc = ConsumeBrace();
720
721 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
722 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +0000723 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000724 Diag(Tok, diag::ext_empty_struct_union_enum,
725 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
726
727 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000728 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
729
Chris Lattner4b009652007-07-25 00:24:17 +0000730 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000731 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000732 // Each iteration of this loop reads one struct-declaration.
733
734 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000735 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000736 Diag(Tok, diag::ext_extra_struct_semi);
737 ConsumeToken();
738 continue;
739 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000740
741 // Parse all the comma separated declarators.
742 DeclSpec DS;
743 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +0000744 if (!Tok.is(tok::at)) {
745 ParseStructDeclaration(DS, FieldDeclarators);
746
747 // Convert them all to fields.
748 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
749 FieldDeclarator &FD = FieldDeclarators[i];
750 // Install the declarator into the current TagDecl.
751 DeclTy *Field = Actions.ActOnField(CurScope,
752 DS.getSourceRange().getBegin(),
753 FD.D, FD.BitfieldSize);
754 FieldDecls.push_back(Field);
755 }
756 } else { // Handle @defs
757 ConsumeToken();
758 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
759 Diag(Tok, diag::err_unexpected_at);
760 SkipUntil(tok::semi, true, true);
761 continue;
762 }
763 ConsumeToken();
764 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
765 if (!Tok.is(tok::identifier)) {
766 Diag(Tok, diag::err_expected_ident);
767 SkipUntil(tok::semi, true, true);
768 continue;
769 }
770 llvm::SmallVector<DeclTy*, 16> Fields;
771 Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
772 Fields);
773 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
774 ConsumeToken();
775 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
776 }
Chris Lattner4b009652007-07-25 00:24:17 +0000777
Chris Lattner34a01ad2007-10-09 17:33:22 +0000778 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000779 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +0000780 } else if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000781 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
782 break;
783 } else {
784 Diag(Tok, diag::err_expected_semi_decl_list);
785 // Skip to end of block or statement
786 SkipUntil(tok::r_brace, true, true);
787 }
788 }
789
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000790 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000791
Chris Lattner4b009652007-07-25 00:24:17 +0000792 AttributeList *AttrList = 0;
793 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000794 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +0000795 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +0000796
797 Actions.ActOnFields(CurScope,
798 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
799 LBraceLoc, RBraceLoc,
800 AttrList);
Chris Lattner4b009652007-07-25 00:24:17 +0000801}
802
803
804/// ParseEnumSpecifier
805/// enum-specifier: [C99 6.7.2.2]
806/// 'enum' identifier[opt] '{' enumerator-list '}'
807/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
808/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
809/// '}' attributes[opt]
810/// 'enum' identifier
811/// [GNU] 'enum' attributes[opt] identifier
812void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000813 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000814 SourceLocation StartLoc = ConsumeToken();
815
816 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +0000817
818 AttributeList *Attr = 0;
819 // If attributes exist after tag, parse them.
820 if (Tok.is(tok::kw___attribute))
821 Attr = ParseAttributes();
822
823 // Must have either 'enum name' or 'enum {...}'.
824 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
825 Diag(Tok, diag::err_expected_ident_lbrace);
826
827 // Skip the rest of this declarator, up until the comma or semicolon.
828 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +0000829 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +0000830 }
831
832 // If an identifier is present, consume and remember it.
833 IdentifierInfo *Name = 0;
834 SourceLocation NameLoc;
835 if (Tok.is(tok::identifier)) {
836 Name = Tok.getIdentifierInfo();
837 NameLoc = ConsumeToken();
838 }
839
840 // There are three options here. If we have 'enum foo;', then this is a
841 // forward declaration. If we have 'enum foo {...' then this is a
842 // definition. Otherwise we have something like 'enum foo xyz', a reference.
843 //
844 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
845 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
846 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
847 //
848 Action::TagKind TK;
849 if (Tok.is(tok::l_brace))
850 TK = Action::TK_Definition;
851 else if (Tok.is(tok::semi))
852 TK = Action::TK_Declaration;
853 else
854 TK = Action::TK_Reference;
855 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
856 Name, NameLoc, Attr);
Chris Lattner4b009652007-07-25 00:24:17 +0000857
Chris Lattner34a01ad2007-10-09 17:33:22 +0000858 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000859 ParseEnumBody(StartLoc, TagDecl);
860
861 // TODO: semantic analysis on the declspec for enums.
862 const char *PrevSpec = 0;
863 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
864 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
865}
866
867/// ParseEnumBody - Parse a {} enclosed enumerator-list.
868/// enumerator-list:
869/// enumerator
870/// enumerator-list ',' enumerator
871/// enumerator:
872/// enumeration-constant
873/// enumeration-constant '=' constant-expression
874/// enumeration-constant:
875/// identifier
876///
877void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
878 SourceLocation LBraceLoc = ConsumeBrace();
879
Chris Lattnerc9a92452007-08-27 17:24:30 +0000880 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +0000881 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000882 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
883
884 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
885
886 DeclTy *LastEnumConstDecl = 0;
887
888 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000889 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000890 IdentifierInfo *Ident = Tok.getIdentifierInfo();
891 SourceLocation IdentLoc = ConsumeToken();
892
893 SourceLocation EqualLoc;
894 ExprTy *AssignedVal = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000895 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000896 EqualLoc = ConsumeToken();
897 ExprResult Res = ParseConstantExpression();
898 if (Res.isInvalid)
899 SkipUntil(tok::comma, tok::r_brace, true, true);
900 else
901 AssignedVal = Res.Val;
902 }
903
904 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000905 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +0000906 LastEnumConstDecl,
907 IdentLoc, Ident,
908 EqualLoc, AssignedVal);
909 EnumConstantDecls.push_back(EnumConstDecl);
910 LastEnumConstDecl = EnumConstDecl;
911
Chris Lattner34a01ad2007-10-09 17:33:22 +0000912 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000913 break;
914 SourceLocation CommaLoc = ConsumeToken();
915
Chris Lattner34a01ad2007-10-09 17:33:22 +0000916 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +0000917 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
918 }
919
920 // Eat the }.
921 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
922
Steve Naroff0acc9c92007-09-15 18:49:24 +0000923 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +0000924 EnumConstantDecls.size());
925
926 DeclTy *AttrList = 0;
927 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000928 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000929 AttrList = ParseAttributes(); // FIXME: where do they do?
930}
931
932/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +0000933/// start of a type-qualifier-list.
934bool Parser::isTypeQualifier() const {
935 switch (Tok.getKind()) {
936 default: return false;
937 // type-qualifier
938 case tok::kw_const:
939 case tok::kw_volatile:
940 case tok::kw_restrict:
941 return true;
942 }
943}
944
945/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +0000946/// start of a specifier-qualifier-list.
947bool Parser::isTypeSpecifierQualifier() const {
948 switch (Tok.getKind()) {
949 default: return false;
950 // GNU attributes support.
951 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000952 // GNU typeof support.
953 case tok::kw_typeof:
954
Chris Lattner4b009652007-07-25 00:24:17 +0000955 // type-specifiers
956 case tok::kw_short:
957 case tok::kw_long:
958 case tok::kw_signed:
959 case tok::kw_unsigned:
960 case tok::kw__Complex:
961 case tok::kw__Imaginary:
962 case tok::kw_void:
963 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000964 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +0000965 case tok::kw_int:
966 case tok::kw_float:
967 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000968 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +0000969 case tok::kw__Bool:
970 case tok::kw__Decimal32:
971 case tok::kw__Decimal64:
972 case tok::kw__Decimal128:
973
Chris Lattner2e78db32008-04-13 18:59:07 +0000974 // struct-or-union-specifier (C99) or class-specifier (C++)
975 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000976 case tok::kw_struct:
977 case tok::kw_union:
978 // enum-specifier
979 case tok::kw_enum:
980
981 // type-qualifier
982 case tok::kw_const:
983 case tok::kw_volatile:
984 case tok::kw_restrict:
985 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +0000986
987 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
988 case tok::less:
989 return getLang().ObjC1;
Chris Lattner4b009652007-07-25 00:24:17 +0000990
991 // typedef-name
992 case tok::identifier:
993 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000994 }
995}
996
997/// isDeclarationSpecifier() - Return true if the current token is part of a
998/// declaration specifier.
999bool Parser::isDeclarationSpecifier() const {
1000 switch (Tok.getKind()) {
1001 default: return false;
1002 // storage-class-specifier
1003 case tok::kw_typedef:
1004 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001005 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001006 case tok::kw_static:
1007 case tok::kw_auto:
1008 case tok::kw_register:
1009 case tok::kw___thread:
1010
1011 // type-specifiers
1012 case tok::kw_short:
1013 case tok::kw_long:
1014 case tok::kw_signed:
1015 case tok::kw_unsigned:
1016 case tok::kw__Complex:
1017 case tok::kw__Imaginary:
1018 case tok::kw_void:
1019 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001020 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001021 case tok::kw_int:
1022 case tok::kw_float:
1023 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001024 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001025 case tok::kw__Bool:
1026 case tok::kw__Decimal32:
1027 case tok::kw__Decimal64:
1028 case tok::kw__Decimal128:
1029
Chris Lattner2e78db32008-04-13 18:59:07 +00001030 // struct-or-union-specifier (C99) or class-specifier (C++)
1031 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001032 case tok::kw_struct:
1033 case tok::kw_union:
1034 // enum-specifier
1035 case tok::kw_enum:
1036
1037 // type-qualifier
1038 case tok::kw_const:
1039 case tok::kw_volatile:
1040 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001041
Chris Lattner4b009652007-07-25 00:24:17 +00001042 // function-specifier
1043 case tok::kw_inline:
Chris Lattnere35d2582007-08-09 16:40:21 +00001044
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001045 // GNU typeof support.
1046 case tok::kw_typeof:
1047
1048 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001049 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001050 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001051
1052 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1053 case tok::less:
1054 return getLang().ObjC1;
Chris Lattner4b009652007-07-25 00:24:17 +00001055
1056 // typedef-name
1057 case tok::identifier:
1058 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001059 }
1060}
1061
1062
1063/// ParseTypeQualifierListOpt
1064/// type-qualifier-list: [C99 6.7.5]
1065/// type-qualifier
1066/// [GNU] attributes
1067/// type-qualifier-list type-qualifier
1068/// [GNU] type-qualifier-list attributes
1069///
1070void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1071 while (1) {
1072 int isInvalid = false;
1073 const char *PrevSpec = 0;
1074 SourceLocation Loc = Tok.getLocation();
1075
1076 switch (Tok.getKind()) {
1077 default:
1078 // If this is not a type-qualifier token, we're done reading type
1079 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +00001080 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +00001081 return;
1082 case tok::kw_const:
1083 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1084 getLang())*2;
1085 break;
1086 case tok::kw_volatile:
1087 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1088 getLang())*2;
1089 break;
1090 case tok::kw_restrict:
1091 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1092 getLang())*2;
1093 break;
1094 case tok::kw___attribute:
1095 DS.AddAttributes(ParseAttributes());
1096 continue; // do *not* consume the next token!
1097 }
1098
1099 // If the specifier combination wasn't legal, issue a diagnostic.
1100 if (isInvalid) {
1101 assert(PrevSpec && "Method did not return previous specifier!");
1102 if (isInvalid == 1) // Error.
1103 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1104 else // extwarn.
1105 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1106 }
1107 ConsumeToken();
1108 }
1109}
1110
1111
1112/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1113///
1114void Parser::ParseDeclarator(Declarator &D) {
1115 /// This implements the 'declarator' production in the C grammar, then checks
1116 /// for well-formedness and issues diagnostics.
1117 ParseDeclaratorInternal(D);
Chris Lattner4b009652007-07-25 00:24:17 +00001118}
1119
1120/// ParseDeclaratorInternal
1121/// declarator: [C99 6.7.5]
1122/// pointer[opt] direct-declarator
1123/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1124/// [GNU] '&' restrict[opt] attributes[opt] declarator
1125///
1126/// pointer: [C99 6.7.5]
1127/// '*' type-qualifier-list[opt]
1128/// '*' type-qualifier-list[opt] pointer
1129///
1130void Parser::ParseDeclaratorInternal(Declarator &D) {
1131 tok::TokenKind Kind = Tok.getKind();
1132
Steve Naroff7aa54752008-08-27 16:04:49 +00001133 // Not a pointer, C++ reference, or block.
1134 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
1135 (Kind != tok::caret || !getLang().Blocks))
Chris Lattner4b009652007-07-25 00:24:17 +00001136 return ParseDirectDeclarator(D);
1137
Steve Naroffdc22f212008-08-28 10:07:06 +00001138 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Chris Lattner4b009652007-07-25 00:24:17 +00001139 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1140
Steve Naroffdc22f212008-08-28 10:07:06 +00001141 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner69f01932008-02-21 01:32:26 +00001142 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001143 DeclSpec DS;
1144
1145 ParseTypeQualifierListOpt(DS);
1146
1147 // Recursively parse the declarator.
1148 ParseDeclaratorInternal(D);
Steve Naroff7aa54752008-08-27 16:04:49 +00001149 if (Kind == tok::star)
1150 // Remember that we parsed a pointer type, and remember the type-quals.
1151 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1152 DS.TakeAttributes()));
1153 else
1154 // Remember that we parsed a Block type, and remember the type-quals.
1155 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1156 Loc));
Chris Lattner4b009652007-07-25 00:24:17 +00001157 } else {
1158 // Is a reference
1159 DeclSpec DS;
1160
1161 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1162 // cv-qualifiers are introduced through the use of a typedef or of a
1163 // template type argument, in which case the cv-qualifiers are ignored.
1164 //
1165 // [GNU] Retricted references are allowed.
1166 // [GNU] Attributes on references are allowed.
1167 ParseTypeQualifierListOpt(DS);
1168
1169 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1170 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1171 Diag(DS.getConstSpecLoc(),
1172 diag::err_invalid_reference_qualifier_application,
1173 "const");
1174 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1175 Diag(DS.getVolatileSpecLoc(),
1176 diag::err_invalid_reference_qualifier_application,
1177 "volatile");
1178 }
1179
1180 // Recursively parse the declarator.
1181 ParseDeclaratorInternal(D);
1182
1183 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001184 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1185 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001186 }
1187}
1188
1189/// ParseDirectDeclarator
1190/// direct-declarator: [C99 6.7.5]
1191/// identifier
1192/// '(' declarator ')'
1193/// [GNU] '(' attributes declarator ')'
1194/// [C90] direct-declarator '[' constant-expression[opt] ']'
1195/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1196/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1197/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1198/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1199/// direct-declarator '(' parameter-type-list ')'
1200/// direct-declarator '(' identifier-list[opt] ')'
1201/// [GNU] direct-declarator '(' parameter-forward-declarations
1202/// parameter-type-list[opt] ')'
1203///
1204void Parser::ParseDirectDeclarator(Declarator &D) {
1205 // Parse the first direct-declarator seen.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001206 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001207 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1208 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1209 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001210 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001211 // direct-declarator: '(' declarator ')'
1212 // direct-declarator: '(' attributes declarator ')'
1213 // Example: 'char (*X)' or 'int (*XX)(void)'
1214 ParseParenDeclarator(D);
1215 } else if (D.mayOmitIdentifier()) {
1216 // This could be something simple like "int" (in which case the declarator
1217 // portion is empty), if an abstract-declarator is allowed.
1218 D.SetIdentifier(0, Tok.getLocation());
1219 } else {
1220 // Expected identifier or '('.
1221 Diag(Tok, diag::err_expected_ident_lparen);
1222 D.SetIdentifier(0, Tok.getLocation());
1223 }
1224
1225 assert(D.isPastIdentifier() &&
1226 "Haven't past the location of the identifier yet?");
1227
1228 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001229 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidise7ae0cb2008-10-15 23:21:32 +00001230 // When not in file scope, warn for ambiguous function declarators, just
1231 // in case the author intended it as a variable definition.
Argiris Kirtzidisd7b7f032008-10-17 23:23:35 +00001232 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001233 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1234 // In such a case, check if we actually have a function declarator; if it
1235 // is not, the declarator has been fully parsed.
1236 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit() &&
Argiris Kirtzidisd7b7f032008-10-17 23:23:35 +00001237 !isCXXFunctionDeclarator(warnIfAmbiguous))
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001238 break;
Chris Lattnera0d056d2008-04-06 05:45:57 +00001239 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001240 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001241 ParseBracketDeclarator(D);
1242 } else {
1243 break;
1244 }
1245 }
1246}
1247
Chris Lattnera0d056d2008-04-06 05:45:57 +00001248/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1249/// only called before the identifier, so these are most likely just grouping
1250/// parens for precedence. If we find that these are actually function
1251/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1252///
1253/// direct-declarator:
1254/// '(' declarator ')'
1255/// [GNU] '(' attributes declarator ')'
1256///
1257void Parser::ParseParenDeclarator(Declarator &D) {
1258 SourceLocation StartLoc = ConsumeParen();
1259 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1260
1261 // If we haven't past the identifier yet (or where the identifier would be
1262 // stored, if this is an abstract declarator), then this is probably just
1263 // grouping parens. However, if this could be an abstract-declarator, then
1264 // this could also be the start of function arguments (consider 'void()').
1265 bool isGrouping;
1266
1267 if (!D.mayOmitIdentifier()) {
1268 // If this can't be an abstract-declarator, this *must* be a grouping
1269 // paren, because we haven't seen the identifier yet.
1270 isGrouping = true;
1271 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001272 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00001273 isDeclarationSpecifier()) { // 'int(int)' is a function.
1274 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1275 // considered to be a type, not a K&R identifier-list.
1276 isGrouping = false;
1277 } else {
1278 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1279 isGrouping = true;
1280 }
1281
1282 // If this is a grouping paren, handle:
1283 // direct-declarator: '(' declarator ')'
1284 // direct-declarator: '(' attributes declarator ')'
1285 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001286 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001287 D.setGroupingParens(true);
1288
Chris Lattnera0d056d2008-04-06 05:45:57 +00001289 if (Tok.is(tok::kw___attribute))
1290 D.AddAttributes(ParseAttributes());
1291
1292 ParseDeclaratorInternal(D);
1293 // Match the ')'.
1294 MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001295
1296 D.setGroupingParens(hadGroupingParens);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001297 return;
1298 }
1299
1300 // Okay, if this wasn't a grouping paren, it must be the start of a function
1301 // argument list. Recognize that this declarator will never have an
1302 // identifier (and remember where it would have been), then fall through to
1303 // the handling of argument lists.
1304 D.SetIdentifier(0, Tok.getLocation());
1305
1306 ParseFunctionDeclarator(StartLoc, D);
1307}
1308
1309/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1310/// declarator D up to a paren, which indicates that we are parsing function
1311/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001312///
1313/// This method also handles this portion of the grammar:
1314/// parameter-type-list: [C99 6.7.5]
1315/// parameter-list
1316/// parameter-list ',' '...'
1317///
1318/// parameter-list: [C99 6.7.5]
1319/// parameter-declaration
1320/// parameter-list ',' parameter-declaration
1321///
1322/// parameter-declaration: [C99 6.7.5]
1323/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00001324/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001325/// [GNU] declaration-specifiers declarator attributes
1326/// declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00001327/// [C++] declaration-specifiers abstract-declarator[opt]
1328/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001329/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1330///
Chris Lattnera0d056d2008-04-06 05:45:57 +00001331void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D) {
1332 // lparen is already consumed!
1333 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00001334
1335 // Okay, this is the parameter list of a function definition, or it is an
1336 // identifier list of a K&R-style function.
Chris Lattner4b009652007-07-25 00:24:17 +00001337
Chris Lattner34a01ad2007-10-09 17:33:22 +00001338 if (Tok.is(tok::r_paren)) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00001339 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00001340 // int() -> no prototype, no '...'.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001341 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/ false,
1342 /*variadic*/ false,
1343 /*arglist*/ 0, 0, LParenLoc));
1344
1345 ConsumeParen(); // Eat the closing ')'.
1346 return;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001347 } else if (Tok.is(tok::identifier) &&
Chris Lattner4b009652007-07-25 00:24:17 +00001348 // K&R identifier lists can't have typedefs as identifiers, per
1349 // C99 6.7.5.3p11.
1350 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1351 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1352 // normal declarators, not for abstract-declarators.
Chris Lattner35d9c912008-04-06 06:34:08 +00001353 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001354 }
1355
1356 // Finally, a normal, non-empty parameter type list.
1357
1358 // Build up an array of information about the parsed arguments.
1359 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00001360
1361 // Enter function-declaration scope, limiting any declarators to the
1362 // function prototype scope, including parameter declarators.
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00001363 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001364
1365 bool IsVariadic = false;
1366 while (1) {
1367 if (Tok.is(tok::ellipsis)) {
1368 IsVariadic = true;
Chris Lattner4b009652007-07-25 00:24:17 +00001369
Chris Lattner9f7564b2008-04-06 06:57:35 +00001370 // Check to see if this is "void(...)" which is not allowed.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001371 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00001372 // Otherwise, parse parameter type list. If it starts with an
1373 // ellipsis, diagnose the malformed function.
1374 Diag(Tok, diag::err_ellipsis_first_arg);
1375 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001376 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00001377
Chris Lattner9f7564b2008-04-06 06:57:35 +00001378 ConsumeToken(); // Consume the ellipsis.
1379 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001380 }
1381
Chris Lattner9f7564b2008-04-06 06:57:35 +00001382 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00001383
Chris Lattner9f7564b2008-04-06 06:57:35 +00001384 // Parse the declaration-specifiers.
1385 DeclSpec DS;
1386 ParseDeclarationSpecifiers(DS);
1387
1388 // Parse the declarator. This is "PrototypeContext", because we must
1389 // accept either 'declarator' or 'abstract-declarator' here.
1390 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1391 ParseDeclarator(ParmDecl);
1392
1393 // Parse GNU attributes, if present.
1394 if (Tok.is(tok::kw___attribute))
1395 ParmDecl.AddAttributes(ParseAttributes());
1396
Chris Lattner9f7564b2008-04-06 06:57:35 +00001397 // Remember this parsed parameter in ParamInfo.
1398 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1399
Chris Lattner9f7564b2008-04-06 06:57:35 +00001400 // If no parameter was specified, verify that *something* was specified,
1401 // otherwise we have a missing type and identifier.
1402 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1403 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1404 // Completely missing, emit error.
1405 Diag(DSStart, diag::err_missing_param);
1406 } else {
1407 // Otherwise, we have something. Add it and let semantic analysis try
1408 // to grok it and add the result to the ParamInfo we are building.
1409
1410 // Inform the actions module about the parameter declarator, so it gets
1411 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00001412 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1413
1414 // Parse the default argument, if any. We parse the default
1415 // arguments in all dialects; the semantic analysis in
1416 // ActOnParamDefaultArgument will reject the default argument in
1417 // C.
1418 if (Tok.is(tok::equal)) {
1419 SourceLocation EqualLoc = Tok.getLocation();
1420
1421 // Consume the '='.
1422 ConsumeToken();
1423
1424 // Parse the default argument
Chris Lattner3e254fb2008-04-08 04:40:51 +00001425 ExprResult DefArgResult = ParseAssignmentExpression();
1426 if (DefArgResult.isInvalid) {
1427 SkipUntil(tok::comma, tok::r_paren, true, true);
1428 } else {
1429 // Inform the actions module about the default argument
1430 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1431 }
1432 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001433
1434 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001435 ParmDecl.getIdentifierLoc(), Param));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001436 }
1437
1438 // If the next token is a comma, consume it and keep reading arguments.
1439 if (Tok.isNot(tok::comma)) break;
1440
1441 // Consume the comma.
1442 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00001443 }
1444
Chris Lattner9f7564b2008-04-06 06:57:35 +00001445 // Leave prototype scope.
1446 ExitScope();
1447
Chris Lattner4b009652007-07-25 00:24:17 +00001448 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001449 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1450 &ParamInfo[0], ParamInfo.size(),
1451 LParenLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00001452
1453 // If we have the closing ')', eat it and we're done.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001454 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001455}
1456
Chris Lattner35d9c912008-04-06 06:34:08 +00001457/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1458/// we found a K&R-style identifier list instead of a type argument list. The
1459/// current token is known to be the first identifier in the list.
1460///
1461/// identifier-list: [C99 6.7.5]
1462/// identifier
1463/// identifier-list ',' identifier
1464///
1465void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1466 Declarator &D) {
1467 // Build up an array of information about the parsed arguments.
1468 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1469 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1470
1471 // If there was no identifier specified for the declarator, either we are in
1472 // an abstract-declarator, or we are in a parameter declarator which was found
1473 // to be abstract. In abstract-declarators, identifier lists are not valid:
1474 // diagnose this.
1475 if (!D.getIdentifier())
1476 Diag(Tok, diag::ext_ident_list_in_param);
1477
1478 // Tok is known to be the first identifier in the list. Remember this
1479 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00001480 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00001481 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1482 Tok.getLocation(), 0));
1483
Chris Lattner113a56b2008-04-06 06:39:19 +00001484 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00001485
1486 while (Tok.is(tok::comma)) {
1487 // Eat the comma.
1488 ConsumeToken();
1489
Chris Lattner113a56b2008-04-06 06:39:19 +00001490 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00001491 if (Tok.isNot(tok::identifier)) {
1492 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00001493 SkipUntil(tok::r_paren);
1494 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00001495 }
Chris Lattneracb67d92008-04-06 06:47:48 +00001496
Chris Lattner35d9c912008-04-06 06:34:08 +00001497 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00001498
1499 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1500 if (Actions.isTypeName(*ParmII, CurScope))
1501 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner35d9c912008-04-06 06:34:08 +00001502
1503 // Verify that the argument identifier has not already been mentioned.
1504 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner113a56b2008-04-06 06:39:19 +00001505 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1506 } else {
1507 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00001508 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1509 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00001510 }
Chris Lattner35d9c912008-04-06 06:34:08 +00001511
1512 // Eat the identifier.
1513 ConsumeToken();
1514 }
1515
Chris Lattner113a56b2008-04-06 06:39:19 +00001516 // Remember that we parsed a function type, and remember the attributes. This
1517 // function type is always a K&R style function type, which is not varargs and
1518 // has no prototype.
1519 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1520 &ParamInfo[0], ParamInfo.size(),
1521 LParenLoc));
Chris Lattner35d9c912008-04-06 06:34:08 +00001522
1523 // If we have the closing ')', eat it and we're done.
Chris Lattner113a56b2008-04-06 06:39:19 +00001524 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00001525}
Chris Lattnera0d056d2008-04-06 05:45:57 +00001526
Chris Lattner4b009652007-07-25 00:24:17 +00001527/// [C90] direct-declarator '[' constant-expression[opt] ']'
1528/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1529/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1530/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1531/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1532void Parser::ParseBracketDeclarator(Declarator &D) {
1533 SourceLocation StartLoc = ConsumeBracket();
1534
1535 // If valid, this location is the position where we read the 'static' keyword.
1536 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001537 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001538 StaticLoc = ConsumeToken();
1539
1540 // If there is a type-qualifier-list, read it now.
1541 DeclSpec DS;
1542 ParseTypeQualifierListOpt(DS);
1543
1544 // If we haven't already read 'static', check to see if there is one after the
1545 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001546 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001547 StaticLoc = ConsumeToken();
1548
1549 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1550 bool isStar = false;
1551 ExprResult NumElements(false);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001552
1553 // Handle the case where we have '[*]' as the array size. However, a leading
1554 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1555 // the the token after the star is a ']'. Since stars in arrays are
1556 // infrequent, use of lookahead is not costly here.
1557 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00001558 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00001559
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001560 if (StaticLoc.isValid())
1561 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1562 StaticLoc = SourceLocation(); // Drop the static.
1563 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001564 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001565 // Parse the assignment-expression now.
1566 NumElements = ParseAssignmentExpression();
1567 }
1568
1569 // If there was an error parsing the assignment-expression, recover.
1570 if (NumElements.isInvalid) {
1571 // If the expression was invalid, skip it.
1572 SkipUntil(tok::r_square);
1573 return;
1574 }
1575
1576 MatchRHSPunctuation(tok::r_square, StartLoc);
1577
1578 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1579 // it was not a constant expression.
1580 if (!getLang().C99) {
1581 // TODO: check C90 array constant exprness.
1582 if (isStar || StaticLoc.isValid() ||
1583 0/*TODO: NumElts is not a C90 constantexpr */)
1584 Diag(StartLoc, diag::ext_c99_array_usage);
1585 }
1586
1587 // Remember that we parsed a pointer type, and remember the type-quals.
1588 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1589 StaticLoc.isValid(), isStar,
1590 NumElements.Val, StartLoc));
1591}
1592
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00001593/// [GNU] typeof-specifier:
1594/// typeof ( expressions )
1595/// typeof ( type-name )
1596/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00001597///
1598void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001599 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00001600 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00001601 SourceLocation StartLoc = ConsumeToken();
1602
Chris Lattner34a01ad2007-10-09 17:33:22 +00001603 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00001604 if (!getLang().CPlusPlus) {
1605 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1606 return;
1607 }
1608
1609 ExprResult Result = ParseCastExpression(true/*isUnaryExpression*/);
1610 if (Result.isInvalid)
1611 return;
1612
1613 const char *PrevSpec = 0;
1614 // Check for duplicate type specifiers.
1615 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1616 Result.Val))
1617 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
1618
1619 // FIXME: Not accurate, the range gets one token more than it should.
1620 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00001621 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00001622 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00001623
Steve Naroff7cbb1462007-07-31 12:34:36 +00001624 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1625
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00001626 if (isTypeIdInParens()) {
Steve Naroff7cbb1462007-07-31 12:34:36 +00001627 TypeTy *Ty = ParseTypeName();
1628
Steve Naroff4c255ab2007-07-31 23:56:32 +00001629 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1630
Chris Lattner34a01ad2007-10-09 17:33:22 +00001631 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001632 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001633 return;
1634 }
1635 RParenLoc = ConsumeParen();
1636 const char *PrevSpec = 0;
1637 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1638 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1639 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001640 } else { // we have an expression.
1641 ExprResult Result = ParseExpression();
Steve Naroff4c255ab2007-07-31 23:56:32 +00001642
Chris Lattner34a01ad2007-10-09 17:33:22 +00001643 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001644 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001645 return;
1646 }
1647 RParenLoc = ConsumeParen();
1648 const char *PrevSpec = 0;
1649 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1650 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1651 Result.Val))
1652 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001653 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00001654 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001655}
1656
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00001657