blob: 1cf331702d99424e75a9c278fd3613ceb6dfdbda [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"
15#include "clang/Parse/DeclSpec.h"
Chris Lattnera7549902007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "llvm/ADT/SmallSet.h"
18using namespace clang;
19
20//===----------------------------------------------------------------------===//
21// C99 6.7: Declarations.
22//===----------------------------------------------------------------------===//
23
24/// ParseTypeName
25/// type-name: [C99 6.7.6]
26/// specifier-qualifier-list abstract-declarator[opt]
27Parser::TypeTy *Parser::ParseTypeName() {
28 // Parse the common declaration-specifiers piece.
29 DeclSpec DS;
30 ParseSpecifierQualifierList(DS);
31
32 // Parse the abstract-declarator, if present.
33 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
34 ParseDeclarator(DeclaratorInfo);
35
Steve Naroff0acc9c92007-09-15 18:49:24 +000036 return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
Chris Lattner4b009652007-07-25 00:24:17 +000037}
38
39/// ParseAttributes - Parse a non-empty attributes list.
40///
41/// [GNU] attributes:
42/// attribute
43/// attributes attribute
44///
45/// [GNU] attribute:
46/// '__attribute__' '(' '(' attribute-list ')' ')'
47///
48/// [GNU] attribute-list:
49/// attrib
50/// attribute_list ',' attrib
51///
52/// [GNU] attrib:
53/// empty
54/// attrib-name
55/// attrib-name '(' identifier ')'
56/// attrib-name '(' identifier ',' nonempty-expr-list ')'
57/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
58///
59/// [GNU] attrib-name:
60/// identifier
61/// typespec
62/// typequal
63/// storageclass
64///
65/// FIXME: The GCC grammar/code for this construct implies we need two
66/// token lookahead. Comment from gcc: "If they start with an identifier
67/// which is followed by a comma or close parenthesis, then the arguments
68/// start with that identifier; otherwise they are an expression list."
69///
70/// At the moment, I am not doing 2 token lookahead. I am also unaware of
71/// any attributes that don't work (based on my limited testing). Most
72/// attributes are very simple in practice. Until we find a bug, I don't see
73/// a pressing need to implement the 2 token lookahead.
74
75AttributeList *Parser::ParseAttributes() {
Chris Lattner34a01ad2007-10-09 17:33:22 +000076 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Chris Lattner4b009652007-07-25 00:24:17 +000077
78 AttributeList *CurrAttr = 0;
79
Chris Lattner34a01ad2007-10-09 17:33:22 +000080 while (Tok.is(tok::kw___attribute)) {
Chris Lattner4b009652007-07-25 00:24:17 +000081 ConsumeToken();
82 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
83 "attribute")) {
84 SkipUntil(tok::r_paren, true); // skip until ) or ;
85 return CurrAttr;
86 }
87 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
88 SkipUntil(tok::r_paren, true); // skip until ) or ;
89 return CurrAttr;
90 }
91 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner34a01ad2007-10-09 17:33:22 +000092 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
93 Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +000094
Chris Lattner34a01ad2007-10-09 17:33:22 +000095 if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +000096 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
97 ConsumeToken();
98 continue;
99 }
100 // we have an identifier or declaration specifier (const, int, etc.)
101 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
102 SourceLocation AttrNameLoc = ConsumeToken();
103
104 // check if we have a "paramterized" attribute
Chris Lattner34a01ad2007-10-09 17:33:22 +0000105 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000106 ConsumeParen(); // ignore the left paren loc for now
107
Chris Lattner34a01ad2007-10-09 17:33:22 +0000108 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000109 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
110 SourceLocation ParmLoc = ConsumeToken();
111
Chris Lattner34a01ad2007-10-09 17:33:22 +0000112 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000113 // __attribute__(( mode(byte) ))
114 ConsumeParen(); // ignore the right paren loc for now
115 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
116 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000117 } else if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000118 ConsumeToken();
119 // __attribute__(( format(printf, 1, 2) ))
120 llvm::SmallVector<ExprTy*, 8> ArgExprs;
121 bool ArgExprsOk = true;
122
123 // now parse the non-empty comma separated list of expressions
124 while (1) {
125 ExprResult ArgExpr = ParseAssignmentExpression();
126 if (ArgExpr.isInvalid) {
127 ArgExprsOk = false;
128 SkipUntil(tok::r_paren);
129 break;
130 } else {
131 ArgExprs.push_back(ArgExpr.Val);
132 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000133 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000134 break;
135 ConsumeToken(); // Eat the comma, move to the next argument
136 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000137 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000138 ConsumeParen(); // ignore the right paren loc for now
139 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
140 ParmLoc, &ArgExprs[0], ArgExprs.size(), CurrAttr);
141 }
142 }
143 } else { // not an identifier
144 // parse a possibly empty comma separated list of expressions
Chris Lattner34a01ad2007-10-09 17:33:22 +0000145 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000146 // __attribute__(( nonnull() ))
147 ConsumeParen(); // ignore the right paren loc for now
148 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
149 0, SourceLocation(), 0, 0, CurrAttr);
150 } else {
151 // __attribute__(( aligned(16) ))
152 llvm::SmallVector<ExprTy*, 8> ArgExprs;
153 bool ArgExprsOk = true;
154
155 // now parse the list of expressions
156 while (1) {
157 ExprResult ArgExpr = ParseAssignmentExpression();
158 if (ArgExpr.isInvalid) {
159 ArgExprsOk = false;
160 SkipUntil(tok::r_paren);
161 break;
162 } else {
163 ArgExprs.push_back(ArgExpr.Val);
164 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000165 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000166 break;
167 ConsumeToken(); // Eat the comma, move to the next argument
168 }
169 // Match the ')'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000170 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000171 ConsumeParen(); // ignore the right paren loc for now
172 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
173 SourceLocation(), &ArgExprs[0], ArgExprs.size(),
174 CurrAttr);
175 }
176 }
177 }
178 } else {
179 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
180 0, SourceLocation(), 0, 0, CurrAttr);
181 }
182 }
183 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
184 SkipUntil(tok::r_paren, false);
185 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
186 SkipUntil(tok::r_paren, false);
187 }
188 return CurrAttr;
189}
190
191/// ParseDeclaration - Parse a full 'declaration', which consists of
192/// declaration-specifiers, some number of declarators, and a semicolon.
193/// 'Context' should be a Declarator::TheContext value.
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000194///
195/// declaration: [C99 6.7]
196/// block-declaration ->
197/// simple-declaration
198/// others [FIXME]
199/// [C++] namespace-definition
200/// others... [FIXME]
201///
Chris Lattner4b009652007-07-25 00:24:17 +0000202Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000203 switch (Tok.getKind()) {
204 case tok::kw_namespace:
205 return ParseNamespace(Context);
206 default:
207 return ParseSimpleDeclaration(Context);
208 }
209}
210
211/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
212/// declaration-specifiers init-declarator-list[opt] ';'
213///[C90/C++]init-declarator-list ';' [TODO]
214/// [OMP] threadprivate-directive [TODO]
215Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Chris Lattner4b009652007-07-25 00:24:17 +0000216 // Parse the common declaration-specifiers piece.
217 DeclSpec DS;
218 ParseDeclarationSpecifiers(DS);
219
220 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
221 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000222 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000223 ConsumeToken();
224 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
225 }
226
227 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
228 ParseDeclarator(DeclaratorInfo);
229
230 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
231}
232
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000233
Chris Lattner4b009652007-07-25 00:24:17 +0000234/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
235/// parsing 'declaration-specifiers declarator'. This method is split out this
236/// way to handle the ambiguity between top-level function-definitions and
237/// declarations.
238///
Chris Lattner4b009652007-07-25 00:24:17 +0000239/// init-declarator-list: [C99 6.7]
240/// init-declarator
241/// init-declarator-list ',' init-declarator
242/// init-declarator: [C99 6.7]
243/// declarator
244/// declarator '=' initializer
245/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
246/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
247///
248Parser::DeclTy *Parser::
249ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
250
251 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
252 // that they can be chained properly if the actions want this.
253 Parser::DeclTy *LastDeclInGroup = 0;
254
255 // At this point, we know that it is not a function definition. Parse the
256 // rest of the init-declarator-list.
257 while (1) {
258 // If a simple-asm-expr is present, parse it.
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000259 if (Tok.is(tok::kw_asm)) {
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000260 ExprResult AsmLabel = ParseSimpleAsm();
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000261 if (AsmLabel.isInvalid) {
262 SkipUntil(tok::semi);
263 return 0;
264 }
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000265
266 D.setAsmLabel(AsmLabel.Val);
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000267 }
Chris Lattner4b009652007-07-25 00:24:17 +0000268
269 // If attributes are present, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000270 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000271 D.AddAttributes(ParseAttributes());
Steve Naroff6a0e2092007-09-12 14:07:44 +0000272
273 // Inform the current actions module that we just parsed this declarator.
274 // FIXME: pass asm & attributes.
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000275 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Steve Naroff6a0e2092007-09-12 14:07:44 +0000276
Chris Lattner4b009652007-07-25 00:24:17 +0000277 // Parse declarator '=' initializer.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000278 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000279 ConsumeToken();
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000280 ExprResult Init = ParseInitializer();
Chris Lattner4b009652007-07-25 00:24:17 +0000281 if (Init.isInvalid) {
282 SkipUntil(tok::semi);
283 return 0;
284 }
Steve Naroff6a0e2092007-09-12 14:07:44 +0000285 Actions.AddInitializerToDecl(LastDeclInGroup, Init.Val);
Chris Lattner4b009652007-07-25 00:24:17 +0000286 }
287
Chris Lattner4b009652007-07-25 00:24:17 +0000288 // If we don't have a comma, it is either the end of the list (a ';') or an
289 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000290 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000291 break;
292
293 // Consume the comma.
294 ConsumeToken();
295
296 // Parse the next declarator.
297 D.clear();
298 ParseDeclarator(D);
299 }
300
Chris Lattner34a01ad2007-10-09 17:33:22 +0000301 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000302 ConsumeToken();
303 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
304 }
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000305 // If this is an ObjC2 for-each loop, this is a successful declarator
306 // parse. The syntax for these looks like:
307 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000308 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000309 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
310 }
Chris Lattner4b009652007-07-25 00:24:17 +0000311 Diag(Tok, diag::err_parse_error);
312 // Skip to end of block or statement
Chris Lattnerf491b412007-08-21 18:36:18 +0000313 SkipUntil(tok::r_brace, true, true);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000314 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000315 ConsumeToken();
316 return 0;
317}
318
319/// ParseSpecifierQualifierList
320/// specifier-qualifier-list:
321/// type-specifier specifier-qualifier-list[opt]
322/// type-qualifier specifier-qualifier-list[opt]
323/// [GNU] attributes specifier-qualifier-list[opt]
324///
325void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
326 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
327 /// parse declaration-specifiers and complain about extra stuff.
328 ParseDeclarationSpecifiers(DS);
329
330 // Validate declspec for type-name.
331 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000332 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000333 Diag(Tok, diag::err_typename_requires_specqual);
334
335 // Issue diagnostic and remove storage class if present.
336 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
337 if (DS.getStorageClassSpecLoc().isValid())
338 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
339 else
340 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
341 DS.ClearStorageClassSpecs();
342 }
343
344 // Issue diagnostic and remove function specfier if present.
345 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
346 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
347 DS.ClearFunctionSpecs();
348 }
349}
350
351/// ParseDeclarationSpecifiers
352/// declaration-specifiers: [C99 6.7]
353/// storage-class-specifier declaration-specifiers[opt]
354/// type-specifier declaration-specifiers[opt]
355/// type-qualifier declaration-specifiers[opt]
356/// [C99] function-specifier declaration-specifiers[opt]
357/// [GNU] attributes declaration-specifiers[opt]
358///
359/// storage-class-specifier: [C99 6.7.1]
360/// 'typedef'
361/// 'extern'
362/// 'static'
363/// 'auto'
364/// 'register'
365/// [GNU] '__thread'
366/// type-specifier: [C99 6.7.2]
367/// 'void'
368/// 'char'
369/// 'short'
370/// 'int'
371/// 'long'
372/// 'float'
373/// 'double'
374/// 'signed'
375/// 'unsigned'
376/// struct-or-union-specifier
377/// enum-specifier
378/// typedef-name
379/// [C++] 'bool'
380/// [C99] '_Bool'
381/// [C99] '_Complex'
382/// [C99] '_Imaginary' // Removed in TC2?
383/// [GNU] '_Decimal32'
384/// [GNU] '_Decimal64'
385/// [GNU] '_Decimal128'
Steve Naroff4c255ab2007-07-31 23:56:32 +0000386/// [GNU] typeof-specifier
Chris Lattner4b009652007-07-25 00:24:17 +0000387/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroffa8ee2262007-08-22 23:18:22 +0000388/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattner4b009652007-07-25 00:24:17 +0000389/// type-qualifier:
390/// 'const'
391/// 'volatile'
392/// [C99] 'restrict'
393/// function-specifier: [C99 6.7.4]
394/// [C99] 'inline'
395///
396void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattnera4ff4272008-03-13 06:29:04 +0000397 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000398 while (1) {
399 int isInvalid = false;
400 const char *PrevSpec = 0;
401 SourceLocation Loc = Tok.getLocation();
402
403 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000404 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000405 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000406 // If this is not a declaration specifier token, we're done reading decl
407 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000408 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000409 return;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000410
411 // typedef-name
412 case tok::identifier: {
413 // This identifier can only be a typedef name if we haven't already seen
414 // a type-specifier. Without this check we misparse:
415 // typedef int X; struct Y { short X; }; as 'short int'.
416 if (DS.hasTypeSpecifier())
417 goto DoneWithDeclSpec;
418
419 // It has to be available as a typedef too!
Argiris Kirtzidis46403632008-08-01 10:35:27 +0000420 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattnerfda18db2008-07-26 01:18:38 +0000421 if (TypeRep == 0)
422 goto DoneWithDeclSpec;
423
424 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
425 TypeRep);
426 if (isInvalid)
427 break;
428
429 DS.SetRangeEnd(Tok.getLocation());
430 ConsumeToken(); // The identifier
431
432 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
433 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
434 // Objective-C interface. If we don't have Objective-C or a '<', this is
435 // just a normal reference to a typedef name.
436 if (!Tok.is(tok::less) || !getLang().ObjC1)
437 continue;
438
439 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000440 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000441 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000442 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000443
444 DS.SetRangeEnd(EndProtoLoc);
445
446 // Do not allow any other declspecs after the protocol qualifier list
447 // "<foo,bar>short" is not allowed.
448 goto DoneWithDeclSpec;
449 }
Chris Lattner4b009652007-07-25 00:24:17 +0000450 // GNU attributes support.
451 case tok::kw___attribute:
452 DS.AddAttributes(ParseAttributes());
453 continue;
454
455 // storage-class-specifier
456 case tok::kw_typedef:
457 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
458 break;
459 case tok::kw_extern:
460 if (DS.isThreadSpecified())
461 Diag(Tok, diag::ext_thread_before, "extern");
462 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
463 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000464 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000465 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
466 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000467 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000468 case tok::kw_static:
469 if (DS.isThreadSpecified())
470 Diag(Tok, diag::ext_thread_before, "static");
471 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
472 break;
473 case tok::kw_auto:
474 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
475 break;
476 case tok::kw_register:
477 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
478 break;
479 case tok::kw___thread:
480 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
481 break;
482
483 // type-specifiers
484 case tok::kw_short:
485 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
486 break;
487 case tok::kw_long:
488 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
489 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
490 else
491 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
492 break;
493 case tok::kw_signed:
494 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
495 break;
496 case tok::kw_unsigned:
497 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
498 break;
499 case tok::kw__Complex:
500 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
501 break;
502 case tok::kw__Imaginary:
503 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
504 break;
505 case tok::kw_void:
506 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
507 break;
508 case tok::kw_char:
509 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
510 break;
511 case tok::kw_int:
512 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
513 break;
514 case tok::kw_float:
515 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
516 break;
517 case tok::kw_double:
518 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
519 break;
520 case tok::kw_bool: // [C++ 2.11p1]
521 case tok::kw__Bool:
522 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
523 break;
524 case tok::kw__Decimal32:
525 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
526 break;
527 case tok::kw__Decimal64:
528 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
529 break;
530 case tok::kw__Decimal128:
531 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
532 break;
Chris Lattner2e78db32008-04-13 18:59:07 +0000533
534 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000535 case tok::kw_struct:
536 case tok::kw_union:
Douglas Gregorec93f442008-04-13 21:30:24 +0000537 ParseClassSpecifier(DS);
Chris Lattner4b009652007-07-25 00:24:17 +0000538 continue;
539 case tok::kw_enum:
540 ParseEnumSpecifier(DS);
541 continue;
542
Steve Naroff7cbb1462007-07-31 12:34:36 +0000543 // GNU typeof support.
544 case tok::kw_typeof:
545 ParseTypeofSpecifier(DS);
546 continue;
547
Chris Lattner4b009652007-07-25 00:24:17 +0000548 // type-qualifier
549 case tok::kw_const:
550 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
551 getLang())*2;
552 break;
553 case tok::kw_volatile:
554 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
555 getLang())*2;
556 break;
557 case tok::kw_restrict:
558 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
559 getLang())*2;
560 break;
561
562 // function-specifier
563 case tok::kw_inline:
564 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
565 break;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000566
Steve Naroff5f0466b2008-06-05 00:02:44 +0000567 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000568 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000569 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
570 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000571 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000572 goto DoneWithDeclSpec;
573
574 {
575 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000576 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000577 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000578 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000579 DS.SetRangeEnd(EndProtoLoc);
580
Chris Lattnerb99d7492008-07-26 00:20:22 +0000581 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id,
582 SourceRange(Loc, EndProtoLoc));
Chris Lattnerfda18db2008-07-26 01:18:38 +0000583 // Do not allow any other declspecs after the protocol qualifier list
584 // "<foo,bar>short" is not allowed.
585 goto DoneWithDeclSpec;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000586 }
Chris Lattner4b009652007-07-25 00:24:17 +0000587 }
588 // If the specifier combination wasn't legal, issue a diagnostic.
589 if (isInvalid) {
590 assert(PrevSpec && "Method did not return previous specifier!");
591 if (isInvalid == 1) // Error.
592 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
593 else // extwarn.
594 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
595 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000596 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000597 ConsumeToken();
598 }
599}
600
601/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
602/// the first token has already been read and has been turned into an instance
603/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
604/// otherwise it returns false and fills in Decl.
605bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
606 AttributeList *Attr = 0;
607 // If attributes exist after tag, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000608 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000609 Attr = ParseAttributes();
610
611 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000612 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000613 Diag(Tok, diag::err_expected_ident_lbrace);
614
615 // Skip the rest of this declarator, up until the comma or semicolon.
616 SkipUntil(tok::comma, true);
617 return true;
618 }
619
620 // If an identifier is present, consume and remember it.
621 IdentifierInfo *Name = 0;
622 SourceLocation NameLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000623 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000624 Name = Tok.getIdentifierInfo();
625 NameLoc = ConsumeToken();
626 }
627
628 // There are three options here. If we have 'struct foo;', then this is a
629 // forward declaration. If we have 'struct foo {...' then this is a
630 // definition. Otherwise we have something like 'struct foo xyz', a reference.
631 //
632 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
633 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
634 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
635 //
636 Action::TagKind TK;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000637 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000638 TK = Action::TK_Definition;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000639 else if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000640 TK = Action::TK_Declaration;
641 else
642 TK = Action::TK_Reference;
Steve Naroff0acc9c92007-09-15 18:49:24 +0000643 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Chris Lattner4b009652007-07-25 00:24:17 +0000644 return false;
645}
646
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000647/// ParseStructDeclaration - Parse a struct declaration without the terminating
648/// semicolon.
649///
Chris Lattner4b009652007-07-25 00:24:17 +0000650/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000651/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000652/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000653/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000654/// struct-declarator-list:
655/// struct-declarator
656/// struct-declarator-list ',' struct-declarator
657/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
658/// struct-declarator:
659/// declarator
660/// [GNU] declarator attributes[opt]
661/// declarator[opt] ':' constant-expression
662/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
663///
Chris Lattner3dd8d392008-04-10 06:46:29 +0000664void Parser::
665ParseStructDeclaration(DeclSpec &DS,
666 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000667 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattner3dd8d392008-04-10 06:46:29 +0000668 while (Tok.is(tok::kw___extension__))
Steve Naroffa9adf112007-08-20 22:28:22 +0000669 ConsumeToken();
670
671 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000672 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +0000673 ParseSpecifierQualifierList(DS);
674 // TODO: Does specifier-qualifier list correctly check that *something* is
675 // specified?
676
677 // If there are no declarators, issue a warning.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000678 if (Tok.is(tok::semi)) {
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000679 Diag(DSStart, diag::w_no_declarators);
Steve Naroffa9adf112007-08-20 22:28:22 +0000680 return;
681 }
682
683 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000684 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000685 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +0000686 FieldDeclarator &DeclaratorInfo = Fields.back();
687
Steve Naroffa9adf112007-08-20 22:28:22 +0000688 /// struct-declarator: declarator
689 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000690 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000691 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +0000692
Chris Lattner34a01ad2007-10-09 17:33:22 +0000693 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000694 ConsumeToken();
695 ExprResult Res = ParseConstantExpression();
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000696 if (Res.isInvalid)
Steve Naroffa9adf112007-08-20 22:28:22 +0000697 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000698 else
Chris Lattner3dd8d392008-04-10 06:46:29 +0000699 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroffa9adf112007-08-20 22:28:22 +0000700 }
701
702 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000703 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000704 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000705
706 // If we don't have a comma, it is either the end of the list (a ';')
707 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000708 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000709 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000710
711 // Consume the comma.
712 ConsumeToken();
713
714 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000715 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000716
717 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000718 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000719 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000720 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000721}
722
723/// ParseStructUnionBody
724/// struct-contents:
725/// struct-declaration-list
726/// [EXT] empty
727/// [GNU] "struct-declaration-list" without terminatoring ';'
728/// struct-declaration-list:
729/// struct-declaration
730/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +0000731/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +0000732///
Chris Lattner4b009652007-07-25 00:24:17 +0000733void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
734 unsigned TagType, DeclTy *TagDecl) {
735 SourceLocation LBraceLoc = ConsumeBrace();
736
737 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
738 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +0000739 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000740 Diag(Tok, diag::ext_empty_struct_union_enum,
741 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
742
743 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000744 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
745
Chris Lattner4b009652007-07-25 00:24:17 +0000746 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000747 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000748 // Each iteration of this loop reads one struct-declaration.
749
750 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000751 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000752 Diag(Tok, diag::ext_extra_struct_semi);
753 ConsumeToken();
754 continue;
755 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000756
757 // Parse all the comma separated declarators.
758 DeclSpec DS;
759 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +0000760 if (!Tok.is(tok::at)) {
761 ParseStructDeclaration(DS, FieldDeclarators);
762
763 // Convert them all to fields.
764 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
765 FieldDeclarator &FD = FieldDeclarators[i];
766 // Install the declarator into the current TagDecl.
767 DeclTy *Field = Actions.ActOnField(CurScope,
768 DS.getSourceRange().getBegin(),
769 FD.D, FD.BitfieldSize);
770 FieldDecls.push_back(Field);
771 }
772 } else { // Handle @defs
773 ConsumeToken();
774 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
775 Diag(Tok, diag::err_unexpected_at);
776 SkipUntil(tok::semi, true, true);
777 continue;
778 }
779 ConsumeToken();
780 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
781 if (!Tok.is(tok::identifier)) {
782 Diag(Tok, diag::err_expected_ident);
783 SkipUntil(tok::semi, true, true);
784 continue;
785 }
786 llvm::SmallVector<DeclTy*, 16> Fields;
787 Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
788 Fields);
789 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
790 ConsumeToken();
791 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
792 }
Chris Lattner4b009652007-07-25 00:24:17 +0000793
Chris Lattner34a01ad2007-10-09 17:33:22 +0000794 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000795 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +0000796 } else if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000797 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
798 break;
799 } else {
800 Diag(Tok, diag::err_expected_semi_decl_list);
801 // Skip to end of block or statement
802 SkipUntil(tok::r_brace, true, true);
803 }
804 }
805
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000806 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000807
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000808 Actions.ActOnFields(CurScope,
Chris Lattner43b885f2008-02-25 21:04:36 +0000809 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000810 LBraceLoc, RBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000811
812 AttributeList *AttrList = 0;
813 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000814 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000815 AttrList = ParseAttributes(); // FIXME: where should I put them?
816}
817
818
819/// ParseEnumSpecifier
820/// enum-specifier: [C99 6.7.2.2]
821/// 'enum' identifier[opt] '{' enumerator-list '}'
822/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
823/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
824/// '}' attributes[opt]
825/// 'enum' identifier
826/// [GNU] 'enum' attributes[opt] identifier
827void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000828 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000829 SourceLocation StartLoc = ConsumeToken();
830
831 // Parse the tag portion of this.
832 DeclTy *TagDecl;
833 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
834 return;
835
Chris Lattner34a01ad2007-10-09 17:33:22 +0000836 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000837 ParseEnumBody(StartLoc, TagDecl);
838
839 // TODO: semantic analysis on the declspec for enums.
840 const char *PrevSpec = 0;
841 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
842 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
843}
844
845/// ParseEnumBody - Parse a {} enclosed enumerator-list.
846/// enumerator-list:
847/// enumerator
848/// enumerator-list ',' enumerator
849/// enumerator:
850/// enumeration-constant
851/// enumeration-constant '=' constant-expression
852/// enumeration-constant:
853/// identifier
854///
855void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
856 SourceLocation LBraceLoc = ConsumeBrace();
857
Chris Lattnerc9a92452007-08-27 17:24:30 +0000858 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +0000859 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000860 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
861
862 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
863
864 DeclTy *LastEnumConstDecl = 0;
865
866 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000867 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000868 IdentifierInfo *Ident = Tok.getIdentifierInfo();
869 SourceLocation IdentLoc = ConsumeToken();
870
871 SourceLocation EqualLoc;
872 ExprTy *AssignedVal = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000873 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000874 EqualLoc = ConsumeToken();
875 ExprResult Res = ParseConstantExpression();
876 if (Res.isInvalid)
877 SkipUntil(tok::comma, tok::r_brace, true, true);
878 else
879 AssignedVal = Res.Val;
880 }
881
882 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000883 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +0000884 LastEnumConstDecl,
885 IdentLoc, Ident,
886 EqualLoc, AssignedVal);
887 EnumConstantDecls.push_back(EnumConstDecl);
888 LastEnumConstDecl = EnumConstDecl;
889
Chris Lattner34a01ad2007-10-09 17:33:22 +0000890 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000891 break;
892 SourceLocation CommaLoc = ConsumeToken();
893
Chris Lattner34a01ad2007-10-09 17:33:22 +0000894 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +0000895 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
896 }
897
898 // Eat the }.
899 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
900
Steve Naroff0acc9c92007-09-15 18:49:24 +0000901 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +0000902 EnumConstantDecls.size());
903
904 DeclTy *AttrList = 0;
905 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000906 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000907 AttrList = ParseAttributes(); // FIXME: where do they do?
908}
909
910/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +0000911/// start of a type-qualifier-list.
912bool Parser::isTypeQualifier() const {
913 switch (Tok.getKind()) {
914 default: return false;
915 // type-qualifier
916 case tok::kw_const:
917 case tok::kw_volatile:
918 case tok::kw_restrict:
919 return true;
920 }
921}
922
923/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +0000924/// start of a specifier-qualifier-list.
925bool Parser::isTypeSpecifierQualifier() const {
926 switch (Tok.getKind()) {
927 default: return false;
928 // GNU attributes support.
929 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000930 // GNU typeof support.
931 case tok::kw_typeof:
Steve Naroff5f0466b2008-06-05 00:02:44 +0000932 // GNU bizarre protocol extension. FIXME: make an extension?
933 case tok::less:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000934
Chris Lattner4b009652007-07-25 00:24:17 +0000935 // type-specifiers
936 case tok::kw_short:
937 case tok::kw_long:
938 case tok::kw_signed:
939 case tok::kw_unsigned:
940 case tok::kw__Complex:
941 case tok::kw__Imaginary:
942 case tok::kw_void:
943 case tok::kw_char:
944 case tok::kw_int:
945 case tok::kw_float:
946 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000947 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +0000948 case tok::kw__Bool:
949 case tok::kw__Decimal32:
950 case tok::kw__Decimal64:
951 case tok::kw__Decimal128:
952
Chris Lattner2e78db32008-04-13 18:59:07 +0000953 // struct-or-union-specifier (C99) or class-specifier (C++)
954 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000955 case tok::kw_struct:
956 case tok::kw_union:
957 // enum-specifier
958 case tok::kw_enum:
959
960 // type-qualifier
961 case tok::kw_const:
962 case tok::kw_volatile:
963 case tok::kw_restrict:
964 return true;
965
966 // typedef-name
967 case tok::identifier:
968 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000969 }
970}
971
972/// isDeclarationSpecifier() - Return true if the current token is part of a
973/// declaration specifier.
974bool Parser::isDeclarationSpecifier() const {
975 switch (Tok.getKind()) {
976 default: return false;
977 // storage-class-specifier
978 case tok::kw_typedef:
979 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +0000980 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +0000981 case tok::kw_static:
982 case tok::kw_auto:
983 case tok::kw_register:
984 case tok::kw___thread:
985
986 // type-specifiers
987 case tok::kw_short:
988 case tok::kw_long:
989 case tok::kw_signed:
990 case tok::kw_unsigned:
991 case tok::kw__Complex:
992 case tok::kw__Imaginary:
993 case tok::kw_void:
994 case tok::kw_char:
995 case tok::kw_int:
996 case tok::kw_float:
997 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000998 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +0000999 case tok::kw__Bool:
1000 case tok::kw__Decimal32:
1001 case tok::kw__Decimal64:
1002 case tok::kw__Decimal128:
1003
Chris Lattner2e78db32008-04-13 18:59:07 +00001004 // struct-or-union-specifier (C99) or class-specifier (C++)
1005 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001006 case tok::kw_struct:
1007 case tok::kw_union:
1008 // enum-specifier
1009 case tok::kw_enum:
1010
1011 // type-qualifier
1012 case tok::kw_const:
1013 case tok::kw_volatile:
1014 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001015
Chris Lattner4b009652007-07-25 00:24:17 +00001016 // function-specifier
1017 case tok::kw_inline:
Chris Lattnere35d2582007-08-09 16:40:21 +00001018
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001019 // GNU typeof support.
1020 case tok::kw_typeof:
1021
1022 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001023 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001024 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001025
1026 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1027 case tok::less:
1028 return getLang().ObjC1;
Chris Lattner4b009652007-07-25 00:24:17 +00001029
1030 // typedef-name
1031 case tok::identifier:
1032 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001033 }
1034}
1035
1036
1037/// ParseTypeQualifierListOpt
1038/// type-qualifier-list: [C99 6.7.5]
1039/// type-qualifier
1040/// [GNU] attributes
1041/// type-qualifier-list type-qualifier
1042/// [GNU] type-qualifier-list attributes
1043///
1044void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1045 while (1) {
1046 int isInvalid = false;
1047 const char *PrevSpec = 0;
1048 SourceLocation Loc = Tok.getLocation();
1049
1050 switch (Tok.getKind()) {
1051 default:
1052 // If this is not a type-qualifier token, we're done reading type
1053 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +00001054 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +00001055 return;
1056 case tok::kw_const:
1057 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1058 getLang())*2;
1059 break;
1060 case tok::kw_volatile:
1061 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1062 getLang())*2;
1063 break;
1064 case tok::kw_restrict:
1065 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1066 getLang())*2;
1067 break;
1068 case tok::kw___attribute:
1069 DS.AddAttributes(ParseAttributes());
1070 continue; // do *not* consume the next token!
1071 }
1072
1073 // If the specifier combination wasn't legal, issue a diagnostic.
1074 if (isInvalid) {
1075 assert(PrevSpec && "Method did not return previous specifier!");
1076 if (isInvalid == 1) // Error.
1077 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1078 else // extwarn.
1079 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1080 }
1081 ConsumeToken();
1082 }
1083}
1084
1085
1086/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1087///
1088void Parser::ParseDeclarator(Declarator &D) {
1089 /// This implements the 'declarator' production in the C grammar, then checks
1090 /// for well-formedness and issues diagnostics.
1091 ParseDeclaratorInternal(D);
Chris Lattner4b009652007-07-25 00:24:17 +00001092}
1093
1094/// ParseDeclaratorInternal
1095/// declarator: [C99 6.7.5]
1096/// pointer[opt] direct-declarator
1097/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1098/// [GNU] '&' restrict[opt] attributes[opt] declarator
1099///
1100/// pointer: [C99 6.7.5]
1101/// '*' type-qualifier-list[opt]
1102/// '*' type-qualifier-list[opt] pointer
1103///
1104void Parser::ParseDeclaratorInternal(Declarator &D) {
1105 tok::TokenKind Kind = Tok.getKind();
1106
1107 // Not a pointer or C++ reference.
Chris Lattner69f01932008-02-21 01:32:26 +00001108 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus))
Chris Lattner4b009652007-07-25 00:24:17 +00001109 return ParseDirectDeclarator(D);
1110
1111 // Otherwise, '*' -> pointer or '&' -> reference.
1112 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1113
1114 if (Kind == tok::star) {
Chris Lattner69f01932008-02-21 01:32:26 +00001115 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001116 DeclSpec DS;
1117
1118 ParseTypeQualifierListOpt(DS);
1119
1120 // Recursively parse the declarator.
1121 ParseDeclaratorInternal(D);
1122
1123 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001124 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1125 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001126 } else {
1127 // Is a reference
1128 DeclSpec DS;
1129
1130 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1131 // cv-qualifiers are introduced through the use of a typedef or of a
1132 // template type argument, in which case the cv-qualifiers are ignored.
1133 //
1134 // [GNU] Retricted references are allowed.
1135 // [GNU] Attributes on references are allowed.
1136 ParseTypeQualifierListOpt(DS);
1137
1138 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1139 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1140 Diag(DS.getConstSpecLoc(),
1141 diag::err_invalid_reference_qualifier_application,
1142 "const");
1143 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1144 Diag(DS.getVolatileSpecLoc(),
1145 diag::err_invalid_reference_qualifier_application,
1146 "volatile");
1147 }
1148
1149 // Recursively parse the declarator.
1150 ParseDeclaratorInternal(D);
1151
1152 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001153 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1154 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001155 }
1156}
1157
1158/// ParseDirectDeclarator
1159/// direct-declarator: [C99 6.7.5]
1160/// identifier
1161/// '(' declarator ')'
1162/// [GNU] '(' attributes declarator ')'
1163/// [C90] direct-declarator '[' constant-expression[opt] ']'
1164/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1165/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1166/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1167/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1168/// direct-declarator '(' parameter-type-list ')'
1169/// direct-declarator '(' identifier-list[opt] ')'
1170/// [GNU] direct-declarator '(' parameter-forward-declarations
1171/// parameter-type-list[opt] ')'
1172///
1173void Parser::ParseDirectDeclarator(Declarator &D) {
1174 // Parse the first direct-declarator seen.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001175 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001176 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1177 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1178 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001179 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001180 // direct-declarator: '(' declarator ')'
1181 // direct-declarator: '(' attributes declarator ')'
1182 // Example: 'char (*X)' or 'int (*XX)(void)'
1183 ParseParenDeclarator(D);
1184 } else if (D.mayOmitIdentifier()) {
1185 // This could be something simple like "int" (in which case the declarator
1186 // portion is empty), if an abstract-declarator is allowed.
1187 D.SetIdentifier(0, Tok.getLocation());
1188 } else {
1189 // Expected identifier or '('.
1190 Diag(Tok, diag::err_expected_ident_lparen);
1191 D.SetIdentifier(0, Tok.getLocation());
1192 }
1193
1194 assert(D.isPastIdentifier() &&
1195 "Haven't past the location of the identifier yet?");
1196
1197 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001198 if (Tok.is(tok::l_paren)) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00001199 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001200 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001201 ParseBracketDeclarator(D);
1202 } else {
1203 break;
1204 }
1205 }
1206}
1207
Chris Lattnera0d056d2008-04-06 05:45:57 +00001208/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1209/// only called before the identifier, so these are most likely just grouping
1210/// parens for precedence. If we find that these are actually function
1211/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1212///
1213/// direct-declarator:
1214/// '(' declarator ')'
1215/// [GNU] '(' attributes declarator ')'
1216///
1217void Parser::ParseParenDeclarator(Declarator &D) {
1218 SourceLocation StartLoc = ConsumeParen();
1219 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1220
1221 // If we haven't past the identifier yet (or where the identifier would be
1222 // stored, if this is an abstract declarator), then this is probably just
1223 // grouping parens. However, if this could be an abstract-declarator, then
1224 // this could also be the start of function arguments (consider 'void()').
1225 bool isGrouping;
1226
1227 if (!D.mayOmitIdentifier()) {
1228 // If this can't be an abstract-declarator, this *must* be a grouping
1229 // paren, because we haven't seen the identifier yet.
1230 isGrouping = true;
1231 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
1232 isDeclarationSpecifier()) { // 'int(int)' is a function.
1233 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1234 // considered to be a type, not a K&R identifier-list.
1235 isGrouping = false;
1236 } else {
1237 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1238 isGrouping = true;
1239 }
1240
1241 // If this is a grouping paren, handle:
1242 // direct-declarator: '(' declarator ')'
1243 // direct-declarator: '(' attributes declarator ')'
1244 if (isGrouping) {
1245 if (Tok.is(tok::kw___attribute))
1246 D.AddAttributes(ParseAttributes());
1247
1248 ParseDeclaratorInternal(D);
1249 // Match the ')'.
1250 MatchRHSPunctuation(tok::r_paren, StartLoc);
1251 return;
1252 }
1253
1254 // Okay, if this wasn't a grouping paren, it must be the start of a function
1255 // argument list. Recognize that this declarator will never have an
1256 // identifier (and remember where it would have been), then fall through to
1257 // the handling of argument lists.
1258 D.SetIdentifier(0, Tok.getLocation());
1259
1260 ParseFunctionDeclarator(StartLoc, D);
1261}
1262
1263/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1264/// declarator D up to a paren, which indicates that we are parsing function
1265/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001266///
1267/// This method also handles this portion of the grammar:
1268/// parameter-type-list: [C99 6.7.5]
1269/// parameter-list
1270/// parameter-list ',' '...'
1271///
1272/// parameter-list: [C99 6.7.5]
1273/// parameter-declaration
1274/// parameter-list ',' parameter-declaration
1275///
1276/// parameter-declaration: [C99 6.7.5]
1277/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00001278/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001279/// [GNU] declaration-specifiers declarator attributes
1280/// declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00001281/// [C++] declaration-specifiers abstract-declarator[opt]
1282/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001283/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1284///
Chris Lattnera0d056d2008-04-06 05:45:57 +00001285void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D) {
1286 // lparen is already consumed!
1287 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00001288
1289 // Okay, this is the parameter list of a function definition, or it is an
1290 // identifier list of a K&R-style function.
Chris Lattner4b009652007-07-25 00:24:17 +00001291
Chris Lattner34a01ad2007-10-09 17:33:22 +00001292 if (Tok.is(tok::r_paren)) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00001293 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00001294 // int() -> no prototype, no '...'.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001295 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/ false,
1296 /*variadic*/ false,
1297 /*arglist*/ 0, 0, LParenLoc));
1298
1299 ConsumeParen(); // Eat the closing ')'.
1300 return;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001301 } else if (Tok.is(tok::identifier) &&
Chris Lattner4b009652007-07-25 00:24:17 +00001302 // K&R identifier lists can't have typedefs as identifiers, per
1303 // C99 6.7.5.3p11.
1304 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1305 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1306 // normal declarators, not for abstract-declarators.
Chris Lattner35d9c912008-04-06 06:34:08 +00001307 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001308 }
1309
1310 // Finally, a normal, non-empty parameter type list.
1311
1312 // Build up an array of information about the parsed arguments.
1313 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00001314
1315 // Enter function-declaration scope, limiting any declarators to the
1316 // function prototype scope, including parameter declarators.
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00001317 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001318
1319 bool IsVariadic = false;
1320 while (1) {
1321 if (Tok.is(tok::ellipsis)) {
1322 IsVariadic = true;
Chris Lattner4b009652007-07-25 00:24:17 +00001323
Chris Lattner9f7564b2008-04-06 06:57:35 +00001324 // Check to see if this is "void(...)" which is not allowed.
1325 if (ParamInfo.empty()) {
1326 // Otherwise, parse parameter type list. If it starts with an
1327 // ellipsis, diagnose the malformed function.
1328 Diag(Tok, diag::err_ellipsis_first_arg);
1329 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001330 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00001331
Chris Lattner9f7564b2008-04-06 06:57:35 +00001332 ConsumeToken(); // Consume the ellipsis.
1333 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001334 }
1335
Chris Lattner9f7564b2008-04-06 06:57:35 +00001336 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00001337
Chris Lattner9f7564b2008-04-06 06:57:35 +00001338 // Parse the declaration-specifiers.
1339 DeclSpec DS;
1340 ParseDeclarationSpecifiers(DS);
1341
1342 // Parse the declarator. This is "PrototypeContext", because we must
1343 // accept either 'declarator' or 'abstract-declarator' here.
1344 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1345 ParseDeclarator(ParmDecl);
1346
1347 // Parse GNU attributes, if present.
1348 if (Tok.is(tok::kw___attribute))
1349 ParmDecl.AddAttributes(ParseAttributes());
1350
Chris Lattner9f7564b2008-04-06 06:57:35 +00001351 // Remember this parsed parameter in ParamInfo.
1352 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1353
Chris Lattner9f7564b2008-04-06 06:57:35 +00001354 // If no parameter was specified, verify that *something* was specified,
1355 // otherwise we have a missing type and identifier.
1356 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1357 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1358 // Completely missing, emit error.
1359 Diag(DSStart, diag::err_missing_param);
1360 } else {
1361 // Otherwise, we have something. Add it and let semantic analysis try
1362 // to grok it and add the result to the ParamInfo we are building.
1363
1364 // Inform the actions module about the parameter declarator, so it gets
1365 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00001366 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1367
1368 // Parse the default argument, if any. We parse the default
1369 // arguments in all dialects; the semantic analysis in
1370 // ActOnParamDefaultArgument will reject the default argument in
1371 // C.
1372 if (Tok.is(tok::equal)) {
1373 SourceLocation EqualLoc = Tok.getLocation();
1374
1375 // Consume the '='.
1376 ConsumeToken();
1377
1378 // Parse the default argument
Chris Lattner3e254fb2008-04-08 04:40:51 +00001379 ExprResult DefArgResult = ParseAssignmentExpression();
1380 if (DefArgResult.isInvalid) {
1381 SkipUntil(tok::comma, tok::r_paren, true, true);
1382 } else {
1383 // Inform the actions module about the default argument
1384 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1385 }
1386 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001387
1388 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001389 ParmDecl.getIdentifierLoc(), Param));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001390 }
1391
1392 // If the next token is a comma, consume it and keep reading arguments.
1393 if (Tok.isNot(tok::comma)) break;
1394
1395 // Consume the comma.
1396 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00001397 }
1398
Chris Lattner9f7564b2008-04-06 06:57:35 +00001399 // Leave prototype scope.
1400 ExitScope();
1401
Chris Lattner4b009652007-07-25 00:24:17 +00001402 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001403 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1404 &ParamInfo[0], ParamInfo.size(),
1405 LParenLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00001406
1407 // If we have the closing ')', eat it and we're done.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001408 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001409}
1410
Chris Lattner35d9c912008-04-06 06:34:08 +00001411/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1412/// we found a K&R-style identifier list instead of a type argument list. The
1413/// current token is known to be the first identifier in the list.
1414///
1415/// identifier-list: [C99 6.7.5]
1416/// identifier
1417/// identifier-list ',' identifier
1418///
1419void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1420 Declarator &D) {
1421 // Build up an array of information about the parsed arguments.
1422 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1423 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1424
1425 // If there was no identifier specified for the declarator, either we are in
1426 // an abstract-declarator, or we are in a parameter declarator which was found
1427 // to be abstract. In abstract-declarators, identifier lists are not valid:
1428 // diagnose this.
1429 if (!D.getIdentifier())
1430 Diag(Tok, diag::ext_ident_list_in_param);
1431
1432 // Tok is known to be the first identifier in the list. Remember this
1433 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00001434 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00001435 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1436 Tok.getLocation(), 0));
1437
Chris Lattner113a56b2008-04-06 06:39:19 +00001438 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00001439
1440 while (Tok.is(tok::comma)) {
1441 // Eat the comma.
1442 ConsumeToken();
1443
Chris Lattner113a56b2008-04-06 06:39:19 +00001444 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00001445 if (Tok.isNot(tok::identifier)) {
1446 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00001447 SkipUntil(tok::r_paren);
1448 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00001449 }
Chris Lattneracb67d92008-04-06 06:47:48 +00001450
Chris Lattner35d9c912008-04-06 06:34:08 +00001451 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00001452
1453 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1454 if (Actions.isTypeName(*ParmII, CurScope))
1455 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner35d9c912008-04-06 06:34:08 +00001456
1457 // Verify that the argument identifier has not already been mentioned.
1458 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner113a56b2008-04-06 06:39:19 +00001459 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1460 } else {
1461 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00001462 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1463 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00001464 }
Chris Lattner35d9c912008-04-06 06:34:08 +00001465
1466 // Eat the identifier.
1467 ConsumeToken();
1468 }
1469
Chris Lattner113a56b2008-04-06 06:39:19 +00001470 // Remember that we parsed a function type, and remember the attributes. This
1471 // function type is always a K&R style function type, which is not varargs and
1472 // has no prototype.
1473 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1474 &ParamInfo[0], ParamInfo.size(),
1475 LParenLoc));
Chris Lattner35d9c912008-04-06 06:34:08 +00001476
1477 // If we have the closing ')', eat it and we're done.
Chris Lattner113a56b2008-04-06 06:39:19 +00001478 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00001479}
Chris Lattnera0d056d2008-04-06 05:45:57 +00001480
Chris Lattner4b009652007-07-25 00:24:17 +00001481/// [C90] direct-declarator '[' constant-expression[opt] ']'
1482/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1483/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1484/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1485/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1486void Parser::ParseBracketDeclarator(Declarator &D) {
1487 SourceLocation StartLoc = ConsumeBracket();
1488
1489 // If valid, this location is the position where we read the 'static' keyword.
1490 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001491 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001492 StaticLoc = ConsumeToken();
1493
1494 // If there is a type-qualifier-list, read it now.
1495 DeclSpec DS;
1496 ParseTypeQualifierListOpt(DS);
1497
1498 // If we haven't already read 'static', check to see if there is one after the
1499 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001500 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001501 StaticLoc = ConsumeToken();
1502
1503 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1504 bool isStar = false;
1505 ExprResult NumElements(false);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001506
1507 // Handle the case where we have '[*]' as the array size. However, a leading
1508 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1509 // the the token after the star is a ']'. Since stars in arrays are
1510 // infrequent, use of lookahead is not costly here.
1511 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00001512 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00001513
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001514 if (StaticLoc.isValid())
1515 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1516 StaticLoc = SourceLocation(); // Drop the static.
1517 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001518 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001519 // Parse the assignment-expression now.
1520 NumElements = ParseAssignmentExpression();
1521 }
1522
1523 // If there was an error parsing the assignment-expression, recover.
1524 if (NumElements.isInvalid) {
1525 // If the expression was invalid, skip it.
1526 SkipUntil(tok::r_square);
1527 return;
1528 }
1529
1530 MatchRHSPunctuation(tok::r_square, StartLoc);
1531
1532 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1533 // it was not a constant expression.
1534 if (!getLang().C99) {
1535 // TODO: check C90 array constant exprness.
1536 if (isStar || StaticLoc.isValid() ||
1537 0/*TODO: NumElts is not a C90 constantexpr */)
1538 Diag(StartLoc, diag::ext_c99_array_usage);
1539 }
1540
1541 // Remember that we parsed a pointer type, and remember the type-quals.
1542 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1543 StaticLoc.isValid(), isStar,
1544 NumElements.Val, StartLoc));
1545}
1546
Steve Naroff7cbb1462007-07-31 12:34:36 +00001547/// [GNU] typeof-specifier:
1548/// typeof ( expressions )
1549/// typeof ( type-name )
1550///
1551void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001552 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00001553 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00001554 SourceLocation StartLoc = ConsumeToken();
1555
Chris Lattner34a01ad2007-10-09 17:33:22 +00001556 if (Tok.isNot(tok::l_paren)) {
Steve Naroff14bbce82007-08-02 02:53:48 +00001557 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1558 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00001559 }
1560 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1561
1562 if (isTypeSpecifierQualifier()) {
1563 TypeTy *Ty = ParseTypeName();
1564
Steve Naroff4c255ab2007-07-31 23:56:32 +00001565 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1566
Chris Lattner34a01ad2007-10-09 17:33:22 +00001567 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001568 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001569 return;
1570 }
1571 RParenLoc = ConsumeParen();
1572 const char *PrevSpec = 0;
1573 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1574 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1575 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001576 } else { // we have an expression.
1577 ExprResult Result = ParseExpression();
Steve Naroff4c255ab2007-07-31 23:56:32 +00001578
Chris Lattner34a01ad2007-10-09 17:33:22 +00001579 if (Result.isInvalid || 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_typeofExpr, StartLoc, PrevSpec,
1587 Result.Val))
1588 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001589 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00001590}
1591
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00001592