blob: 76fca9e885f2f2bd9ce83092bc29cadcb5da6248 [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
248///
249Parser::DeclTy *Parser::
250ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
251
252 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
253 // that they can be chained properly if the actions want this.
254 Parser::DeclTy *LastDeclInGroup = 0;
255
256 // At this point, we know that it is not a function definition. Parse the
257 // rest of the init-declarator-list.
258 while (1) {
259 // If a simple-asm-expr is present, parse it.
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000260 if (Tok.is(tok::kw_asm)) {
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000261 ExprResult AsmLabel = ParseSimpleAsm();
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000262 if (AsmLabel.isInvalid) {
263 SkipUntil(tok::semi);
264 return 0;
265 }
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000266
267 D.setAsmLabel(AsmLabel.Val);
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000268 }
Chris Lattner4b009652007-07-25 00:24:17 +0000269
270 // If attributes are present, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000271 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000272 D.AddAttributes(ParseAttributes());
Steve Naroff6a0e2092007-09-12 14:07:44 +0000273
274 // Inform the current actions module that we just parsed this declarator.
275 // FIXME: pass asm & attributes.
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000276 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Steve Naroff6a0e2092007-09-12 14:07:44 +0000277
Chris Lattner4b009652007-07-25 00:24:17 +0000278 // Parse declarator '=' initializer.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000279 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000280 ConsumeToken();
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000281 ExprResult Init = ParseInitializer();
Chris Lattner4b009652007-07-25 00:24:17 +0000282 if (Init.isInvalid) {
283 SkipUntil(tok::semi);
284 return 0;
285 }
Steve Naroff6a0e2092007-09-12 14:07:44 +0000286 Actions.AddInitializerToDecl(LastDeclInGroup, Init.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000287 }
288
Chris Lattner4b009652007-07-25 00:24:17 +0000289 // If we don't have a comma, it is either the end of the list (a ';') or an
290 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000291 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000292 break;
293
294 // Consume the comma.
295 ConsumeToken();
296
297 // Parse the next declarator.
298 D.clear();
299 ParseDeclarator(D);
300 }
301
Chris Lattner34a01ad2007-10-09 17:33:22 +0000302 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000303 ConsumeToken();
304 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
305 }
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000306 // If this is an ObjC2 for-each loop, this is a successful declarator
307 // parse. The syntax for these looks like:
308 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000309 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000310 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
311 }
Chris Lattner4b009652007-07-25 00:24:17 +0000312 Diag(Tok, diag::err_parse_error);
313 // Skip to end of block or statement
Chris Lattnerf491b412007-08-21 18:36:18 +0000314 SkipUntil(tok::r_brace, true, true);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000315 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000316 ConsumeToken();
317 return 0;
318}
319
320/// ParseSpecifierQualifierList
321/// specifier-qualifier-list:
322/// type-specifier specifier-qualifier-list[opt]
323/// type-qualifier specifier-qualifier-list[opt]
324/// [GNU] attributes specifier-qualifier-list[opt]
325///
326void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
327 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
328 /// parse declaration-specifiers and complain about extra stuff.
329 ParseDeclarationSpecifiers(DS);
330
331 // Validate declspec for type-name.
332 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000333 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000334 Diag(Tok, diag::err_typename_requires_specqual);
335
336 // Issue diagnostic and remove storage class if present.
337 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
338 if (DS.getStorageClassSpecLoc().isValid())
339 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
340 else
341 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
342 DS.ClearStorageClassSpecs();
343 }
344
345 // Issue diagnostic and remove function specfier if present.
346 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
347 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
348 DS.ClearFunctionSpecs();
349 }
350}
351
352/// ParseDeclarationSpecifiers
353/// declaration-specifiers: [C99 6.7]
354/// storage-class-specifier declaration-specifiers[opt]
355/// type-specifier declaration-specifiers[opt]
356/// type-qualifier declaration-specifiers[opt]
357/// [C99] function-specifier declaration-specifiers[opt]
358/// [GNU] attributes declaration-specifiers[opt]
359///
360/// storage-class-specifier: [C99 6.7.1]
361/// 'typedef'
362/// 'extern'
363/// 'static'
364/// 'auto'
365/// 'register'
366/// [GNU] '__thread'
367/// type-specifier: [C99 6.7.2]
368/// 'void'
369/// 'char'
370/// 'short'
371/// 'int'
372/// 'long'
373/// 'float'
374/// 'double'
375/// 'signed'
376/// 'unsigned'
377/// struct-or-union-specifier
378/// enum-specifier
379/// typedef-name
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000380/// [C++] 'wchar_t'
Chris Lattner4b009652007-07-25 00:24:17 +0000381/// [C++] 'bool'
382/// [C99] '_Bool'
383/// [C99] '_Complex'
384/// [C99] '_Imaginary' // Removed in TC2?
385/// [GNU] '_Decimal32'
386/// [GNU] '_Decimal64'
387/// [GNU] '_Decimal128'
Steve Naroff4c255ab2007-07-31 23:56:32 +0000388/// [GNU] typeof-specifier
Chris Lattner4b009652007-07-25 00:24:17 +0000389/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroffa8ee2262007-08-22 23:18:22 +0000390/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattner4b009652007-07-25 00:24:17 +0000391/// type-qualifier:
392/// 'const'
393/// 'volatile'
394/// [C99] 'restrict'
395/// function-specifier: [C99 6.7.4]
396/// [C99] 'inline'
397///
398void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattnera4ff4272008-03-13 06:29:04 +0000399 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000400 while (1) {
401 int isInvalid = false;
402 const char *PrevSpec = 0;
403 SourceLocation Loc = Tok.getLocation();
404
405 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000406 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000407 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000408 // If this is not a declaration specifier token, we're done reading decl
409 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000410 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000411 return;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000412
413 // typedef-name
414 case tok::identifier: {
415 // This identifier can only be a typedef name if we haven't already seen
416 // a type-specifier. Without this check we misparse:
417 // typedef int X; struct Y { short X; }; as 'short int'.
418 if (DS.hasTypeSpecifier())
419 goto DoneWithDeclSpec;
420
421 // It has to be available as a typedef too!
Argiris Kirtzidis46403632008-08-01 10:35:27 +0000422 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattnerfda18db2008-07-26 01:18:38 +0000423 if (TypeRep == 0)
424 goto DoneWithDeclSpec;
425
426 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
427 TypeRep);
428 if (isInvalid)
429 break;
430
431 DS.SetRangeEnd(Tok.getLocation());
432 ConsumeToken(); // The identifier
433
434 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
435 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
436 // Objective-C interface. If we don't have Objective-C or a '<', this is
437 // just a normal reference to a typedef name.
438 if (!Tok.is(tok::less) || !getLang().ObjC1)
439 continue;
440
441 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000442 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000443 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000444 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000445
446 DS.SetRangeEnd(EndProtoLoc);
447
448 // Do not allow any other declspecs after the protocol qualifier list
449 // "<foo,bar>short" is not allowed.
450 goto DoneWithDeclSpec;
451 }
Chris Lattner4b009652007-07-25 00:24:17 +0000452 // GNU attributes support.
453 case tok::kw___attribute:
454 DS.AddAttributes(ParseAttributes());
455 continue;
456
457 // storage-class-specifier
458 case tok::kw_typedef:
459 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
460 break;
461 case tok::kw_extern:
462 if (DS.isThreadSpecified())
463 Diag(Tok, diag::ext_thread_before, "extern");
464 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
465 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000466 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000467 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
468 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000469 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000470 case tok::kw_static:
471 if (DS.isThreadSpecified())
472 Diag(Tok, diag::ext_thread_before, "static");
473 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
474 break;
475 case tok::kw_auto:
476 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
477 break;
478 case tok::kw_register:
479 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
480 break;
481 case tok::kw___thread:
482 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
483 break;
484
485 // type-specifiers
486 case tok::kw_short:
487 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
488 break;
489 case tok::kw_long:
490 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
491 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
492 else
493 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
494 break;
495 case tok::kw_signed:
496 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
497 break;
498 case tok::kw_unsigned:
499 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
500 break;
501 case tok::kw__Complex:
502 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
503 break;
504 case tok::kw__Imaginary:
505 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
506 break;
507 case tok::kw_void:
508 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
509 break;
510 case tok::kw_char:
511 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
512 break;
513 case tok::kw_int:
514 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
515 break;
516 case tok::kw_float:
517 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
518 break;
519 case tok::kw_double:
520 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
521 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000522 case tok::kw_wchar_t: // [C++ 2.11p1]
523 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
524 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000525 case tok::kw_bool: // [C++ 2.11p1]
526 case tok::kw__Bool:
527 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
528 break;
529 case tok::kw__Decimal32:
530 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
531 break;
532 case tok::kw__Decimal64:
533 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
534 break;
535 case tok::kw__Decimal128:
536 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
537 break;
Chris Lattner2e78db32008-04-13 18:59:07 +0000538
539 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000540 case tok::kw_struct:
541 case tok::kw_union:
Douglas Gregorec93f442008-04-13 21:30:24 +0000542 ParseClassSpecifier(DS);
Chris Lattner4b009652007-07-25 00:24:17 +0000543 continue;
544 case tok::kw_enum:
545 ParseEnumSpecifier(DS);
546 continue;
547
Steve Naroff7cbb1462007-07-31 12:34:36 +0000548 // GNU typeof support.
549 case tok::kw_typeof:
550 ParseTypeofSpecifier(DS);
551 continue;
552
Chris Lattner4b009652007-07-25 00:24:17 +0000553 // type-qualifier
554 case tok::kw_const:
555 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
556 getLang())*2;
557 break;
558 case tok::kw_volatile:
559 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
560 getLang())*2;
561 break;
562 case tok::kw_restrict:
563 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
564 getLang())*2;
565 break;
566
567 // function-specifier
568 case tok::kw_inline:
569 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
570 break;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000571
Steve Naroff5f0466b2008-06-05 00:02:44 +0000572 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000573 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000574 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
575 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000576 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000577 goto DoneWithDeclSpec;
578
579 {
580 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000581 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000582 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000583 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000584 DS.SetRangeEnd(EndProtoLoc);
585
Chris Lattnerb99d7492008-07-26 00:20:22 +0000586 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id,
587 SourceRange(Loc, EndProtoLoc));
Chris Lattnerfda18db2008-07-26 01:18:38 +0000588 // Do not allow any other declspecs after the protocol qualifier list
589 // "<foo,bar>short" is not allowed.
590 goto DoneWithDeclSpec;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000591 }
Chris Lattner4b009652007-07-25 00:24:17 +0000592 }
593 // If the specifier combination wasn't legal, issue a diagnostic.
594 if (isInvalid) {
595 assert(PrevSpec && "Method did not return previous specifier!");
596 if (isInvalid == 1) // Error.
597 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
598 else // extwarn.
599 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
600 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000601 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000602 ConsumeToken();
603 }
604}
605
606/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
607/// the first token has already been read and has been turned into an instance
608/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
609/// otherwise it returns false and fills in Decl.
610bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
611 AttributeList *Attr = 0;
612 // If attributes exist after tag, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000613 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000614 Attr = ParseAttributes();
615
616 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000617 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000618 Diag(Tok, diag::err_expected_ident_lbrace);
619
620 // Skip the rest of this declarator, up until the comma or semicolon.
621 SkipUntil(tok::comma, true);
622 return true;
623 }
624
625 // If an identifier is present, consume and remember it.
626 IdentifierInfo *Name = 0;
627 SourceLocation NameLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000628 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000629 Name = Tok.getIdentifierInfo();
630 NameLoc = ConsumeToken();
631 }
632
633 // There are three options here. If we have 'struct foo;', then this is a
634 // forward declaration. If we have 'struct foo {...' then this is a
635 // definition. Otherwise we have something like 'struct foo xyz', a reference.
636 //
637 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
638 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
639 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
640 //
641 Action::TagKind TK;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000642 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000643 TK = Action::TK_Definition;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000644 else if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000645 TK = Action::TK_Declaration;
646 else
647 TK = Action::TK_Reference;
Steve Naroff0acc9c92007-09-15 18:49:24 +0000648 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Chris Lattner4b009652007-07-25 00:24:17 +0000649 return false;
650}
651
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000652/// ParseStructDeclaration - Parse a struct declaration without the terminating
653/// semicolon.
654///
Chris Lattner4b009652007-07-25 00:24:17 +0000655/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000656/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000657/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000658/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000659/// struct-declarator-list:
660/// struct-declarator
661/// struct-declarator-list ',' struct-declarator
662/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
663/// struct-declarator:
664/// declarator
665/// [GNU] declarator attributes[opt]
666/// declarator[opt] ':' constant-expression
667/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
668///
Chris Lattner3dd8d392008-04-10 06:46:29 +0000669void Parser::
670ParseStructDeclaration(DeclSpec &DS,
671 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000672 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattner3dd8d392008-04-10 06:46:29 +0000673 while (Tok.is(tok::kw___extension__))
Steve Naroffa9adf112007-08-20 22:28:22 +0000674 ConsumeToken();
675
676 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000677 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +0000678 ParseSpecifierQualifierList(DS);
679 // TODO: Does specifier-qualifier list correctly check that *something* is
680 // specified?
681
682 // If there are no declarators, issue a warning.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000683 if (Tok.is(tok::semi)) {
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000684 Diag(DSStart, diag::w_no_declarators);
Steve Naroffa9adf112007-08-20 22:28:22 +0000685 return;
686 }
687
688 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000689 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000690 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +0000691 FieldDeclarator &DeclaratorInfo = Fields.back();
692
Steve Naroffa9adf112007-08-20 22:28:22 +0000693 /// struct-declarator: declarator
694 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000695 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000696 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +0000697
Chris Lattner34a01ad2007-10-09 17:33:22 +0000698 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000699 ConsumeToken();
700 ExprResult Res = ParseConstantExpression();
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000701 if (Res.isInvalid)
Steve Naroffa9adf112007-08-20 22:28:22 +0000702 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000703 else
Chris Lattner3dd8d392008-04-10 06:46:29 +0000704 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroffa9adf112007-08-20 22:28:22 +0000705 }
706
707 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000708 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000709 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000710
711 // If we don't have a comma, it is either the end of the list (a ';')
712 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000713 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000714 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000715
716 // Consume the comma.
717 ConsumeToken();
718
719 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000720 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000721
722 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000723 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000724 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000725 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000726}
727
728/// ParseStructUnionBody
729/// struct-contents:
730/// struct-declaration-list
731/// [EXT] empty
732/// [GNU] "struct-declaration-list" without terminatoring ';'
733/// struct-declaration-list:
734/// struct-declaration
735/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +0000736/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +0000737///
Chris Lattner4b009652007-07-25 00:24:17 +0000738void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
739 unsigned TagType, DeclTy *TagDecl) {
740 SourceLocation LBraceLoc = ConsumeBrace();
741
742 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
743 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +0000744 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000745 Diag(Tok, diag::ext_empty_struct_union_enum,
746 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
747
748 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000749 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
750
Chris Lattner4b009652007-07-25 00:24:17 +0000751 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000752 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000753 // Each iteration of this loop reads one struct-declaration.
754
755 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000756 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000757 Diag(Tok, diag::ext_extra_struct_semi);
758 ConsumeToken();
759 continue;
760 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000761
762 // Parse all the comma separated declarators.
763 DeclSpec DS;
764 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +0000765 if (!Tok.is(tok::at)) {
766 ParseStructDeclaration(DS, FieldDeclarators);
767
768 // Convert them all to fields.
769 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
770 FieldDeclarator &FD = FieldDeclarators[i];
771 // Install the declarator into the current TagDecl.
772 DeclTy *Field = Actions.ActOnField(CurScope,
773 DS.getSourceRange().getBegin(),
774 FD.D, FD.BitfieldSize);
775 FieldDecls.push_back(Field);
776 }
777 } else { // Handle @defs
778 ConsumeToken();
779 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
780 Diag(Tok, diag::err_unexpected_at);
781 SkipUntil(tok::semi, true, true);
782 continue;
783 }
784 ConsumeToken();
785 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
786 if (!Tok.is(tok::identifier)) {
787 Diag(Tok, diag::err_expected_ident);
788 SkipUntil(tok::semi, true, true);
789 continue;
790 }
791 llvm::SmallVector<DeclTy*, 16> Fields;
792 Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
793 Fields);
794 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
795 ConsumeToken();
796 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
797 }
Chris Lattner4b009652007-07-25 00:24:17 +0000798
Chris Lattner34a01ad2007-10-09 17:33:22 +0000799 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000800 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +0000801 } else if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000802 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
803 break;
804 } else {
805 Diag(Tok, diag::err_expected_semi_decl_list);
806 // Skip to end of block or statement
807 SkipUntil(tok::r_brace, true, true);
808 }
809 }
810
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000811 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000812
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000813 Actions.ActOnFields(CurScope,
Chris Lattner43b885f2008-02-25 21:04:36 +0000814 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000815 LBraceLoc, RBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000816
817 AttributeList *AttrList = 0;
818 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000819 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000820 AttrList = ParseAttributes(); // FIXME: where should I put them?
821}
822
823
824/// ParseEnumSpecifier
825/// enum-specifier: [C99 6.7.2.2]
826/// 'enum' identifier[opt] '{' enumerator-list '}'
827/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
828/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
829/// '}' attributes[opt]
830/// 'enum' identifier
831/// [GNU] 'enum' attributes[opt] identifier
832void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000833 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000834 SourceLocation StartLoc = ConsumeToken();
835
836 // Parse the tag portion of this.
837 DeclTy *TagDecl;
838 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
839 return;
840
Chris Lattner34a01ad2007-10-09 17:33:22 +0000841 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000842 ParseEnumBody(StartLoc, TagDecl);
843
844 // TODO: semantic analysis on the declspec for enums.
845 const char *PrevSpec = 0;
846 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
847 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
848}
849
850/// ParseEnumBody - Parse a {} enclosed enumerator-list.
851/// enumerator-list:
852/// enumerator
853/// enumerator-list ',' enumerator
854/// enumerator:
855/// enumeration-constant
856/// enumeration-constant '=' constant-expression
857/// enumeration-constant:
858/// identifier
859///
860void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
861 SourceLocation LBraceLoc = ConsumeBrace();
862
Chris Lattnerc9a92452007-08-27 17:24:30 +0000863 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +0000864 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000865 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
866
867 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
868
869 DeclTy *LastEnumConstDecl = 0;
870
871 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000872 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000873 IdentifierInfo *Ident = Tok.getIdentifierInfo();
874 SourceLocation IdentLoc = ConsumeToken();
875
876 SourceLocation EqualLoc;
877 ExprTy *AssignedVal = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000878 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000879 EqualLoc = ConsumeToken();
880 ExprResult Res = ParseConstantExpression();
881 if (Res.isInvalid)
882 SkipUntil(tok::comma, tok::r_brace, true, true);
883 else
884 AssignedVal = Res.Val;
885 }
886
887 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000888 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +0000889 LastEnumConstDecl,
890 IdentLoc, Ident,
891 EqualLoc, AssignedVal);
892 EnumConstantDecls.push_back(EnumConstDecl);
893 LastEnumConstDecl = EnumConstDecl;
894
Chris Lattner34a01ad2007-10-09 17:33:22 +0000895 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000896 break;
897 SourceLocation CommaLoc = ConsumeToken();
898
Chris Lattner34a01ad2007-10-09 17:33:22 +0000899 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +0000900 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
901 }
902
903 // Eat the }.
904 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
905
Steve Naroff0acc9c92007-09-15 18:49:24 +0000906 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +0000907 EnumConstantDecls.size());
908
909 DeclTy *AttrList = 0;
910 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000911 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000912 AttrList = ParseAttributes(); // FIXME: where do they do?
913}
914
915/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +0000916/// start of a type-qualifier-list.
917bool Parser::isTypeQualifier() const {
918 switch (Tok.getKind()) {
919 default: return false;
920 // type-qualifier
921 case tok::kw_const:
922 case tok::kw_volatile:
923 case tok::kw_restrict:
924 return true;
925 }
926}
927
928/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +0000929/// start of a specifier-qualifier-list.
930bool Parser::isTypeSpecifierQualifier() const {
931 switch (Tok.getKind()) {
932 default: return false;
933 // GNU attributes support.
934 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000935 // GNU typeof support.
936 case tok::kw_typeof:
Steve Naroff5f0466b2008-06-05 00:02:44 +0000937 // GNU bizarre protocol extension. FIXME: make an extension?
938 case tok::less:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000939
Chris Lattner4b009652007-07-25 00:24:17 +0000940 // type-specifiers
941 case tok::kw_short:
942 case tok::kw_long:
943 case tok::kw_signed:
944 case tok::kw_unsigned:
945 case tok::kw__Complex:
946 case tok::kw__Imaginary:
947 case tok::kw_void:
948 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000949 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +0000950 case tok::kw_int:
951 case tok::kw_float:
952 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000953 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +0000954 case tok::kw__Bool:
955 case tok::kw__Decimal32:
956 case tok::kw__Decimal64:
957 case tok::kw__Decimal128:
958
Chris Lattner2e78db32008-04-13 18:59:07 +0000959 // struct-or-union-specifier (C99) or class-specifier (C++)
960 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000961 case tok::kw_struct:
962 case tok::kw_union:
963 // enum-specifier
964 case tok::kw_enum:
965
966 // type-qualifier
967 case tok::kw_const:
968 case tok::kw_volatile:
969 case tok::kw_restrict:
970 return true;
971
972 // typedef-name
973 case tok::identifier:
974 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000975 }
976}
977
978/// isDeclarationSpecifier() - Return true if the current token is part of a
979/// declaration specifier.
980bool Parser::isDeclarationSpecifier() const {
981 switch (Tok.getKind()) {
982 default: return false;
983 // storage-class-specifier
984 case tok::kw_typedef:
985 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +0000986 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +0000987 case tok::kw_static:
988 case tok::kw_auto:
989 case tok::kw_register:
990 case tok::kw___thread:
991
992 // type-specifiers
993 case tok::kw_short:
994 case tok::kw_long:
995 case tok::kw_signed:
996 case tok::kw_unsigned:
997 case tok::kw__Complex:
998 case tok::kw__Imaginary:
999 case tok::kw_void:
1000 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001001 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001002 case tok::kw_int:
1003 case tok::kw_float:
1004 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001005 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001006 case tok::kw__Bool:
1007 case tok::kw__Decimal32:
1008 case tok::kw__Decimal64:
1009 case tok::kw__Decimal128:
1010
Chris Lattner2e78db32008-04-13 18:59:07 +00001011 // struct-or-union-specifier (C99) or class-specifier (C++)
1012 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001013 case tok::kw_struct:
1014 case tok::kw_union:
1015 // enum-specifier
1016 case tok::kw_enum:
1017
1018 // type-qualifier
1019 case tok::kw_const:
1020 case tok::kw_volatile:
1021 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001022
Chris Lattner4b009652007-07-25 00:24:17 +00001023 // function-specifier
1024 case tok::kw_inline:
Chris Lattnere35d2582007-08-09 16:40:21 +00001025
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001026 // GNU typeof support.
1027 case tok::kw_typeof:
1028
1029 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001030 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001031 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001032
1033 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1034 case tok::less:
1035 return getLang().ObjC1;
Chris Lattner4b009652007-07-25 00:24:17 +00001036
1037 // typedef-name
1038 case tok::identifier:
1039 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001040 }
1041}
1042
1043
1044/// ParseTypeQualifierListOpt
1045/// type-qualifier-list: [C99 6.7.5]
1046/// type-qualifier
1047/// [GNU] attributes
1048/// type-qualifier-list type-qualifier
1049/// [GNU] type-qualifier-list attributes
1050///
1051void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1052 while (1) {
1053 int isInvalid = false;
1054 const char *PrevSpec = 0;
1055 SourceLocation Loc = Tok.getLocation();
1056
1057 switch (Tok.getKind()) {
1058 default:
1059 // If this is not a type-qualifier token, we're done reading type
1060 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +00001061 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +00001062 return;
1063 case tok::kw_const:
1064 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1065 getLang())*2;
1066 break;
1067 case tok::kw_volatile:
1068 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1069 getLang())*2;
1070 break;
1071 case tok::kw_restrict:
1072 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1073 getLang())*2;
1074 break;
1075 case tok::kw___attribute:
1076 DS.AddAttributes(ParseAttributes());
1077 continue; // do *not* consume the next token!
1078 }
1079
1080 // If the specifier combination wasn't legal, issue a diagnostic.
1081 if (isInvalid) {
1082 assert(PrevSpec && "Method did not return previous specifier!");
1083 if (isInvalid == 1) // Error.
1084 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1085 else // extwarn.
1086 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1087 }
1088 ConsumeToken();
1089 }
1090}
1091
1092
1093/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1094///
1095void Parser::ParseDeclarator(Declarator &D) {
1096 /// This implements the 'declarator' production in the C grammar, then checks
1097 /// for well-formedness and issues diagnostics.
1098 ParseDeclaratorInternal(D);
Chris Lattner4b009652007-07-25 00:24:17 +00001099}
1100
1101/// ParseDeclaratorInternal
1102/// declarator: [C99 6.7.5]
1103/// pointer[opt] direct-declarator
1104/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1105/// [GNU] '&' restrict[opt] attributes[opt] declarator
1106///
1107/// pointer: [C99 6.7.5]
1108/// '*' type-qualifier-list[opt]
1109/// '*' type-qualifier-list[opt] pointer
1110///
1111void Parser::ParseDeclaratorInternal(Declarator &D) {
1112 tok::TokenKind Kind = Tok.getKind();
1113
Steve Naroff7aa54752008-08-27 16:04:49 +00001114 // Not a pointer, C++ reference, or block.
1115 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
1116 (Kind != tok::caret || !getLang().Blocks))
Chris Lattner4b009652007-07-25 00:24:17 +00001117 return ParseDirectDeclarator(D);
1118
1119 // Otherwise, '*' -> pointer or '&' -> reference.
1120 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1121
Steve Naroff7aa54752008-08-27 16:04:49 +00001122 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner69f01932008-02-21 01:32:26 +00001123 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001124 DeclSpec DS;
1125
1126 ParseTypeQualifierListOpt(DS);
1127
1128 // Recursively parse the declarator.
1129 ParseDeclaratorInternal(D);
Steve Naroff7aa54752008-08-27 16:04:49 +00001130 if (Kind == tok::star)
1131 // Remember that we parsed a pointer type, and remember the type-quals.
1132 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1133 DS.TakeAttributes()));
1134 else
1135 // Remember that we parsed a Block type, and remember the type-quals.
1136 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1137 Loc));
Chris Lattner4b009652007-07-25 00:24:17 +00001138 } else {
1139 // Is a reference
1140 DeclSpec DS;
1141
1142 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1143 // cv-qualifiers are introduced through the use of a typedef or of a
1144 // template type argument, in which case the cv-qualifiers are ignored.
1145 //
1146 // [GNU] Retricted references are allowed.
1147 // [GNU] Attributes on references are allowed.
1148 ParseTypeQualifierListOpt(DS);
1149
1150 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1151 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1152 Diag(DS.getConstSpecLoc(),
1153 diag::err_invalid_reference_qualifier_application,
1154 "const");
1155 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1156 Diag(DS.getVolatileSpecLoc(),
1157 diag::err_invalid_reference_qualifier_application,
1158 "volatile");
1159 }
1160
1161 // Recursively parse the declarator.
1162 ParseDeclaratorInternal(D);
1163
1164 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001165 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1166 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001167 }
1168}
1169
1170/// ParseDirectDeclarator
1171/// direct-declarator: [C99 6.7.5]
1172/// identifier
1173/// '(' declarator ')'
1174/// [GNU] '(' attributes declarator ')'
1175/// [C90] direct-declarator '[' constant-expression[opt] ']'
1176/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1177/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1178/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1179/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1180/// direct-declarator '(' parameter-type-list ')'
1181/// direct-declarator '(' identifier-list[opt] ')'
1182/// [GNU] direct-declarator '(' parameter-forward-declarations
1183/// parameter-type-list[opt] ')'
1184///
1185void Parser::ParseDirectDeclarator(Declarator &D) {
1186 // Parse the first direct-declarator seen.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001187 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001188 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1189 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1190 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001191 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001192 // direct-declarator: '(' declarator ')'
1193 // direct-declarator: '(' attributes declarator ')'
1194 // Example: 'char (*X)' or 'int (*XX)(void)'
1195 ParseParenDeclarator(D);
1196 } else if (D.mayOmitIdentifier()) {
1197 // This could be something simple like "int" (in which case the declarator
1198 // portion is empty), if an abstract-declarator is allowed.
1199 D.SetIdentifier(0, Tok.getLocation());
1200 } else {
1201 // Expected identifier or '('.
1202 Diag(Tok, diag::err_expected_ident_lparen);
1203 D.SetIdentifier(0, Tok.getLocation());
1204 }
1205
1206 assert(D.isPastIdentifier() &&
1207 "Haven't past the location of the identifier yet?");
1208
1209 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001210 if (Tok.is(tok::l_paren)) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00001211 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001212 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001213 ParseBracketDeclarator(D);
1214 } else {
1215 break;
1216 }
1217 }
1218}
1219
Chris Lattnera0d056d2008-04-06 05:45:57 +00001220/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1221/// only called before the identifier, so these are most likely just grouping
1222/// parens for precedence. If we find that these are actually function
1223/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1224///
1225/// direct-declarator:
1226/// '(' declarator ')'
1227/// [GNU] '(' attributes declarator ')'
1228///
1229void Parser::ParseParenDeclarator(Declarator &D) {
1230 SourceLocation StartLoc = ConsumeParen();
1231 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1232
1233 // If we haven't past the identifier yet (or where the identifier would be
1234 // stored, if this is an abstract declarator), then this is probably just
1235 // grouping parens. However, if this could be an abstract-declarator, then
1236 // this could also be the start of function arguments (consider 'void()').
1237 bool isGrouping;
1238
1239 if (!D.mayOmitIdentifier()) {
1240 // If this can't be an abstract-declarator, this *must* be a grouping
1241 // paren, because we haven't seen the identifier yet.
1242 isGrouping = true;
1243 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
1244 isDeclarationSpecifier()) { // 'int(int)' is a function.
1245 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1246 // considered to be a type, not a K&R identifier-list.
1247 isGrouping = false;
1248 } else {
1249 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1250 isGrouping = true;
1251 }
1252
1253 // If this is a grouping paren, handle:
1254 // direct-declarator: '(' declarator ')'
1255 // direct-declarator: '(' attributes declarator ')'
1256 if (isGrouping) {
1257 if (Tok.is(tok::kw___attribute))
1258 D.AddAttributes(ParseAttributes());
1259
1260 ParseDeclaratorInternal(D);
1261 // Match the ')'.
1262 MatchRHSPunctuation(tok::r_paren, StartLoc);
1263 return;
1264 }
1265
1266 // Okay, if this wasn't a grouping paren, it must be the start of a function
1267 // argument list. Recognize that this declarator will never have an
1268 // identifier (and remember where it would have been), then fall through to
1269 // the handling of argument lists.
1270 D.SetIdentifier(0, Tok.getLocation());
1271
1272 ParseFunctionDeclarator(StartLoc, D);
1273}
1274
1275/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1276/// declarator D up to a paren, which indicates that we are parsing function
1277/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001278///
1279/// This method also handles this portion of the grammar:
1280/// parameter-type-list: [C99 6.7.5]
1281/// parameter-list
1282/// parameter-list ',' '...'
1283///
1284/// parameter-list: [C99 6.7.5]
1285/// parameter-declaration
1286/// parameter-list ',' parameter-declaration
1287///
1288/// parameter-declaration: [C99 6.7.5]
1289/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00001290/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001291/// [GNU] declaration-specifiers declarator attributes
1292/// declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00001293/// [C++] declaration-specifiers abstract-declarator[opt]
1294/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001295/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1296///
Chris Lattnera0d056d2008-04-06 05:45:57 +00001297void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D) {
1298 // lparen is already consumed!
1299 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00001300
1301 // Okay, this is the parameter list of a function definition, or it is an
1302 // identifier list of a K&R-style function.
Chris Lattner4b009652007-07-25 00:24:17 +00001303
Chris Lattner34a01ad2007-10-09 17:33:22 +00001304 if (Tok.is(tok::r_paren)) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00001305 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00001306 // int() -> no prototype, no '...'.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001307 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/ false,
1308 /*variadic*/ false,
1309 /*arglist*/ 0, 0, LParenLoc));
1310
1311 ConsumeParen(); // Eat the closing ')'.
1312 return;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001313 } else if (Tok.is(tok::identifier) &&
Chris Lattner4b009652007-07-25 00:24:17 +00001314 // K&R identifier lists can't have typedefs as identifiers, per
1315 // C99 6.7.5.3p11.
1316 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1317 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1318 // normal declarators, not for abstract-declarators.
Chris Lattner35d9c912008-04-06 06:34:08 +00001319 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001320 }
1321
1322 // Finally, a normal, non-empty parameter type list.
1323
1324 // Build up an array of information about the parsed arguments.
1325 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00001326
1327 // Enter function-declaration scope, limiting any declarators to the
1328 // function prototype scope, including parameter declarators.
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00001329 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001330
1331 bool IsVariadic = false;
1332 while (1) {
1333 if (Tok.is(tok::ellipsis)) {
1334 IsVariadic = true;
Chris Lattner4b009652007-07-25 00:24:17 +00001335
Chris Lattner9f7564b2008-04-06 06:57:35 +00001336 // Check to see if this is "void(...)" which is not allowed.
1337 if (ParamInfo.empty()) {
1338 // Otherwise, parse parameter type list. If it starts with an
1339 // ellipsis, diagnose the malformed function.
1340 Diag(Tok, diag::err_ellipsis_first_arg);
1341 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001342 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00001343
Chris Lattner9f7564b2008-04-06 06:57:35 +00001344 ConsumeToken(); // Consume the ellipsis.
1345 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001346 }
1347
Chris Lattner9f7564b2008-04-06 06:57:35 +00001348 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00001349
Chris Lattner9f7564b2008-04-06 06:57:35 +00001350 // Parse the declaration-specifiers.
1351 DeclSpec DS;
1352 ParseDeclarationSpecifiers(DS);
1353
1354 // Parse the declarator. This is "PrototypeContext", because we must
1355 // accept either 'declarator' or 'abstract-declarator' here.
1356 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1357 ParseDeclarator(ParmDecl);
1358
1359 // Parse GNU attributes, if present.
1360 if (Tok.is(tok::kw___attribute))
1361 ParmDecl.AddAttributes(ParseAttributes());
1362
Chris Lattner9f7564b2008-04-06 06:57:35 +00001363 // Remember this parsed parameter in ParamInfo.
1364 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1365
Chris Lattner9f7564b2008-04-06 06:57:35 +00001366 // If no parameter was specified, verify that *something* was specified,
1367 // otherwise we have a missing type and identifier.
1368 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1369 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1370 // Completely missing, emit error.
1371 Diag(DSStart, diag::err_missing_param);
1372 } else {
1373 // Otherwise, we have something. Add it and let semantic analysis try
1374 // to grok it and add the result to the ParamInfo we are building.
1375
1376 // Inform the actions module about the parameter declarator, so it gets
1377 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00001378 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1379
1380 // Parse the default argument, if any. We parse the default
1381 // arguments in all dialects; the semantic analysis in
1382 // ActOnParamDefaultArgument will reject the default argument in
1383 // C.
1384 if (Tok.is(tok::equal)) {
1385 SourceLocation EqualLoc = Tok.getLocation();
1386
1387 // Consume the '='.
1388 ConsumeToken();
1389
1390 // Parse the default argument
Chris Lattner3e254fb2008-04-08 04:40:51 +00001391 ExprResult DefArgResult = ParseAssignmentExpression();
1392 if (DefArgResult.isInvalid) {
1393 SkipUntil(tok::comma, tok::r_paren, true, true);
1394 } else {
1395 // Inform the actions module about the default argument
1396 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1397 }
1398 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001399
1400 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001401 ParmDecl.getIdentifierLoc(), Param));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001402 }
1403
1404 // If the next token is a comma, consume it and keep reading arguments.
1405 if (Tok.isNot(tok::comma)) break;
1406
1407 // Consume the comma.
1408 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00001409 }
1410
Chris Lattner9f7564b2008-04-06 06:57:35 +00001411 // Leave prototype scope.
1412 ExitScope();
1413
Chris Lattner4b009652007-07-25 00:24:17 +00001414 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001415 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1416 &ParamInfo[0], ParamInfo.size(),
1417 LParenLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00001418
1419 // If we have the closing ')', eat it and we're done.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001420 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001421}
1422
Chris Lattner35d9c912008-04-06 06:34:08 +00001423/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1424/// we found a K&R-style identifier list instead of a type argument list. The
1425/// current token is known to be the first identifier in the list.
1426///
1427/// identifier-list: [C99 6.7.5]
1428/// identifier
1429/// identifier-list ',' identifier
1430///
1431void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1432 Declarator &D) {
1433 // Build up an array of information about the parsed arguments.
1434 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1435 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1436
1437 // If there was no identifier specified for the declarator, either we are in
1438 // an abstract-declarator, or we are in a parameter declarator which was found
1439 // to be abstract. In abstract-declarators, identifier lists are not valid:
1440 // diagnose this.
1441 if (!D.getIdentifier())
1442 Diag(Tok, diag::ext_ident_list_in_param);
1443
1444 // Tok is known to be the first identifier in the list. Remember this
1445 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00001446 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00001447 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1448 Tok.getLocation(), 0));
1449
Chris Lattner113a56b2008-04-06 06:39:19 +00001450 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00001451
1452 while (Tok.is(tok::comma)) {
1453 // Eat the comma.
1454 ConsumeToken();
1455
Chris Lattner113a56b2008-04-06 06:39:19 +00001456 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00001457 if (Tok.isNot(tok::identifier)) {
1458 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00001459 SkipUntil(tok::r_paren);
1460 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00001461 }
Chris Lattneracb67d92008-04-06 06:47:48 +00001462
Chris Lattner35d9c912008-04-06 06:34:08 +00001463 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00001464
1465 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1466 if (Actions.isTypeName(*ParmII, CurScope))
1467 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner35d9c912008-04-06 06:34:08 +00001468
1469 // Verify that the argument identifier has not already been mentioned.
1470 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner113a56b2008-04-06 06:39:19 +00001471 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1472 } else {
1473 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00001474 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1475 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00001476 }
Chris Lattner35d9c912008-04-06 06:34:08 +00001477
1478 // Eat the identifier.
1479 ConsumeToken();
1480 }
1481
Chris Lattner113a56b2008-04-06 06:39:19 +00001482 // Remember that we parsed a function type, and remember the attributes. This
1483 // function type is always a K&R style function type, which is not varargs and
1484 // has no prototype.
1485 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1486 &ParamInfo[0], ParamInfo.size(),
1487 LParenLoc));
Chris Lattner35d9c912008-04-06 06:34:08 +00001488
1489 // If we have the closing ')', eat it and we're done.
Chris Lattner113a56b2008-04-06 06:39:19 +00001490 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00001491}
Chris Lattnera0d056d2008-04-06 05:45:57 +00001492
Chris Lattner4b009652007-07-25 00:24:17 +00001493/// [C90] direct-declarator '[' constant-expression[opt] ']'
1494/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1495/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1496/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1497/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1498void Parser::ParseBracketDeclarator(Declarator &D) {
1499 SourceLocation StartLoc = ConsumeBracket();
1500
1501 // If valid, this location is the position where we read the 'static' keyword.
1502 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001503 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001504 StaticLoc = ConsumeToken();
1505
1506 // If there is a type-qualifier-list, read it now.
1507 DeclSpec DS;
1508 ParseTypeQualifierListOpt(DS);
1509
1510 // If we haven't already read 'static', check to see if there is one after the
1511 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001512 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001513 StaticLoc = ConsumeToken();
1514
1515 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1516 bool isStar = false;
1517 ExprResult NumElements(false);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001518
1519 // Handle the case where we have '[*]' as the array size. However, a leading
1520 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1521 // the the token after the star is a ']'. Since stars in arrays are
1522 // infrequent, use of lookahead is not costly here.
1523 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00001524 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00001525
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001526 if (StaticLoc.isValid())
1527 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1528 StaticLoc = SourceLocation(); // Drop the static.
1529 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001530 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001531 // Parse the assignment-expression now.
1532 NumElements = ParseAssignmentExpression();
1533 }
1534
1535 // If there was an error parsing the assignment-expression, recover.
1536 if (NumElements.isInvalid) {
1537 // If the expression was invalid, skip it.
1538 SkipUntil(tok::r_square);
1539 return;
1540 }
1541
1542 MatchRHSPunctuation(tok::r_square, StartLoc);
1543
1544 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1545 // it was not a constant expression.
1546 if (!getLang().C99) {
1547 // TODO: check C90 array constant exprness.
1548 if (isStar || StaticLoc.isValid() ||
1549 0/*TODO: NumElts is not a C90 constantexpr */)
1550 Diag(StartLoc, diag::ext_c99_array_usage);
1551 }
1552
1553 // Remember that we parsed a pointer type, and remember the type-quals.
1554 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1555 StaticLoc.isValid(), isStar,
1556 NumElements.Val, StartLoc));
1557}
1558
Steve Naroff7cbb1462007-07-31 12:34:36 +00001559/// [GNU] typeof-specifier:
1560/// typeof ( expressions )
1561/// typeof ( type-name )
1562///
1563void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001564 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00001565 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00001566 SourceLocation StartLoc = ConsumeToken();
1567
Chris Lattner34a01ad2007-10-09 17:33:22 +00001568 if (Tok.isNot(tok::l_paren)) {
Steve Naroff14bbce82007-08-02 02:53:48 +00001569 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1570 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00001571 }
1572 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1573
1574 if (isTypeSpecifierQualifier()) {
1575 TypeTy *Ty = ParseTypeName();
1576
Steve Naroff4c255ab2007-07-31 23:56:32 +00001577 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1578
Chris Lattner34a01ad2007-10-09 17:33:22 +00001579 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001580 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001581 return;
1582 }
1583 RParenLoc = ConsumeParen();
1584 const char *PrevSpec = 0;
1585 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1586 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1587 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001588 } else { // we have an expression.
1589 ExprResult Result = ParseExpression();
Steve Naroff4c255ab2007-07-31 23:56:32 +00001590
Chris Lattner34a01ad2007-10-09 17:33:22 +00001591 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001592 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001593 return;
1594 }
1595 RParenLoc = ConsumeParen();
1596 const char *PrevSpec = 0;
1597 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1598 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1599 Result.Val))
1600 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001601 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00001602 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001603}
1604
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00001605