blob: 5b4473ea4de5dfa5d8ca8655d3251b4dd98a7467 [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
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000379/// [C++] 'wchar_t'
Chris Lattner4b009652007-07-25 00:24:17 +0000380/// [C++] 'bool'
381/// [C99] '_Bool'
382/// [C99] '_Complex'
383/// [C99] '_Imaginary' // Removed in TC2?
384/// [GNU] '_Decimal32'
385/// [GNU] '_Decimal64'
386/// [GNU] '_Decimal128'
Steve Naroff4c255ab2007-07-31 23:56:32 +0000387/// [GNU] typeof-specifier
Chris Lattner4b009652007-07-25 00:24:17 +0000388/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroffa8ee2262007-08-22 23:18:22 +0000389/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattner4b009652007-07-25 00:24:17 +0000390/// type-qualifier:
391/// 'const'
392/// 'volatile'
393/// [C99] 'restrict'
394/// function-specifier: [C99 6.7.4]
395/// [C99] 'inline'
396///
397void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattnera4ff4272008-03-13 06:29:04 +0000398 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000399 while (1) {
400 int isInvalid = false;
401 const char *PrevSpec = 0;
402 SourceLocation Loc = Tok.getLocation();
403
404 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000405 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000406 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000407 // If this is not a declaration specifier token, we're done reading decl
408 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000409 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000410 return;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000411
412 // typedef-name
413 case tok::identifier: {
414 // This identifier can only be a typedef name if we haven't already seen
415 // a type-specifier. Without this check we misparse:
416 // typedef int X; struct Y { short X; }; as 'short int'.
417 if (DS.hasTypeSpecifier())
418 goto DoneWithDeclSpec;
419
420 // It has to be available as a typedef too!
Argiris Kirtzidis46403632008-08-01 10:35:27 +0000421 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattnerfda18db2008-07-26 01:18:38 +0000422 if (TypeRep == 0)
423 goto DoneWithDeclSpec;
424
425 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
426 TypeRep);
427 if (isInvalid)
428 break;
429
430 DS.SetRangeEnd(Tok.getLocation());
431 ConsumeToken(); // The identifier
432
433 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
434 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
435 // Objective-C interface. If we don't have Objective-C or a '<', this is
436 // just a normal reference to a typedef name.
437 if (!Tok.is(tok::less) || !getLang().ObjC1)
438 continue;
439
440 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000441 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000442 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000443 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000444
445 DS.SetRangeEnd(EndProtoLoc);
446
447 // Do not allow any other declspecs after the protocol qualifier list
448 // "<foo,bar>short" is not allowed.
449 goto DoneWithDeclSpec;
450 }
Chris Lattner4b009652007-07-25 00:24:17 +0000451 // GNU attributes support.
452 case tok::kw___attribute:
453 DS.AddAttributes(ParseAttributes());
454 continue;
455
456 // storage-class-specifier
457 case tok::kw_typedef:
458 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
459 break;
460 case tok::kw_extern:
461 if (DS.isThreadSpecified())
462 Diag(Tok, diag::ext_thread_before, "extern");
463 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
464 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000465 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000466 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
467 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000468 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000469 case tok::kw_static:
470 if (DS.isThreadSpecified())
471 Diag(Tok, diag::ext_thread_before, "static");
472 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
473 break;
474 case tok::kw_auto:
475 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
476 break;
477 case tok::kw_register:
478 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
479 break;
480 case tok::kw___thread:
481 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
482 break;
483
484 // type-specifiers
485 case tok::kw_short:
486 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
487 break;
488 case tok::kw_long:
489 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
490 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
491 else
492 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
493 break;
494 case tok::kw_signed:
495 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
496 break;
497 case tok::kw_unsigned:
498 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
499 break;
500 case tok::kw__Complex:
501 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
502 break;
503 case tok::kw__Imaginary:
504 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
505 break;
506 case tok::kw_void:
507 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
508 break;
509 case tok::kw_char:
510 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
511 break;
512 case tok::kw_int:
513 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
514 break;
515 case tok::kw_float:
516 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
517 break;
518 case tok::kw_double:
519 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
520 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000521 case tok::kw_wchar_t: // [C++ 2.11p1]
522 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
523 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000524 case tok::kw_bool: // [C++ 2.11p1]
525 case tok::kw__Bool:
526 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
527 break;
528 case tok::kw__Decimal32:
529 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
530 break;
531 case tok::kw__Decimal64:
532 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
533 break;
534 case tok::kw__Decimal128:
535 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
536 break;
Chris Lattner2e78db32008-04-13 18:59:07 +0000537
538 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000539 case tok::kw_struct:
540 case tok::kw_union:
Douglas Gregorec93f442008-04-13 21:30:24 +0000541 ParseClassSpecifier(DS);
Chris Lattner4b009652007-07-25 00:24:17 +0000542 continue;
543 case tok::kw_enum:
544 ParseEnumSpecifier(DS);
545 continue;
546
Steve Naroff7cbb1462007-07-31 12:34:36 +0000547 // GNU typeof support.
548 case tok::kw_typeof:
549 ParseTypeofSpecifier(DS);
550 continue;
551
Chris Lattner4b009652007-07-25 00:24:17 +0000552 // type-qualifier
553 case tok::kw_const:
554 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
555 getLang())*2;
556 break;
557 case tok::kw_volatile:
558 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
559 getLang())*2;
560 break;
561 case tok::kw_restrict:
562 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
563 getLang())*2;
564 break;
565
566 // function-specifier
567 case tok::kw_inline:
568 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
569 break;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000570
Steve Naroff5f0466b2008-06-05 00:02:44 +0000571 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000572 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000573 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
574 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000575 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000576 goto DoneWithDeclSpec;
577
578 {
579 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000580 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000581 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000582 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000583 DS.SetRangeEnd(EndProtoLoc);
584
Chris Lattnerb99d7492008-07-26 00:20:22 +0000585 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id,
586 SourceRange(Loc, EndProtoLoc));
Chris Lattnerfda18db2008-07-26 01:18:38 +0000587 // Do not allow any other declspecs after the protocol qualifier list
588 // "<foo,bar>short" is not allowed.
589 goto DoneWithDeclSpec;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000590 }
Chris Lattner4b009652007-07-25 00:24:17 +0000591 }
592 // If the specifier combination wasn't legal, issue a diagnostic.
593 if (isInvalid) {
594 assert(PrevSpec && "Method did not return previous specifier!");
595 if (isInvalid == 1) // Error.
596 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
597 else // extwarn.
598 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
599 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000600 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000601 ConsumeToken();
602 }
603}
604
605/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
606/// the first token has already been read and has been turned into an instance
607/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
608/// otherwise it returns false and fills in Decl.
609bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
610 AttributeList *Attr = 0;
611 // If attributes exist after tag, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000612 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000613 Attr = ParseAttributes();
614
615 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000616 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000617 Diag(Tok, diag::err_expected_ident_lbrace);
618
619 // Skip the rest of this declarator, up until the comma or semicolon.
620 SkipUntil(tok::comma, true);
621 return true;
622 }
623
624 // If an identifier is present, consume and remember it.
625 IdentifierInfo *Name = 0;
626 SourceLocation NameLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000627 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000628 Name = Tok.getIdentifierInfo();
629 NameLoc = ConsumeToken();
630 }
631
632 // There are three options here. If we have 'struct foo;', then this is a
633 // forward declaration. If we have 'struct foo {...' then this is a
634 // definition. Otherwise we have something like 'struct foo xyz', a reference.
635 //
636 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
637 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
638 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
639 //
640 Action::TagKind TK;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000641 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000642 TK = Action::TK_Definition;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000643 else if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000644 TK = Action::TK_Declaration;
645 else
646 TK = Action::TK_Reference;
Steve Naroff0acc9c92007-09-15 18:49:24 +0000647 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Chris Lattner4b009652007-07-25 00:24:17 +0000648 return false;
649}
650
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000651/// ParseStructDeclaration - Parse a struct declaration without the terminating
652/// semicolon.
653///
Chris Lattner4b009652007-07-25 00:24:17 +0000654/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000655/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000656/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000657/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000658/// struct-declarator-list:
659/// struct-declarator
660/// struct-declarator-list ',' struct-declarator
661/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
662/// struct-declarator:
663/// declarator
664/// [GNU] declarator attributes[opt]
665/// declarator[opt] ':' constant-expression
666/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
667///
Chris Lattner3dd8d392008-04-10 06:46:29 +0000668void Parser::
669ParseStructDeclaration(DeclSpec &DS,
670 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000671 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattner3dd8d392008-04-10 06:46:29 +0000672 while (Tok.is(tok::kw___extension__))
Steve Naroffa9adf112007-08-20 22:28:22 +0000673 ConsumeToken();
674
675 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000676 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +0000677 ParseSpecifierQualifierList(DS);
678 // TODO: Does specifier-qualifier list correctly check that *something* is
679 // specified?
680
681 // If there are no declarators, issue a warning.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000682 if (Tok.is(tok::semi)) {
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000683 Diag(DSStart, diag::w_no_declarators);
Steve Naroffa9adf112007-08-20 22:28:22 +0000684 return;
685 }
686
687 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000688 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000689 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +0000690 FieldDeclarator &DeclaratorInfo = Fields.back();
691
Steve Naroffa9adf112007-08-20 22:28:22 +0000692 /// struct-declarator: declarator
693 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000694 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000695 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +0000696
Chris Lattner34a01ad2007-10-09 17:33:22 +0000697 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000698 ConsumeToken();
699 ExprResult Res = ParseConstantExpression();
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000700 if (Res.isInvalid)
Steve Naroffa9adf112007-08-20 22:28:22 +0000701 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000702 else
Chris Lattner3dd8d392008-04-10 06:46:29 +0000703 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroffa9adf112007-08-20 22:28:22 +0000704 }
705
706 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000707 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000708 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000709
710 // If we don't have a comma, it is either the end of the list (a ';')
711 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000712 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000713 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000714
715 // Consume the comma.
716 ConsumeToken();
717
718 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000719 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000720
721 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000722 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000723 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000724 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000725}
726
727/// ParseStructUnionBody
728/// struct-contents:
729/// struct-declaration-list
730/// [EXT] empty
731/// [GNU] "struct-declaration-list" without terminatoring ';'
732/// struct-declaration-list:
733/// struct-declaration
734/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +0000735/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +0000736///
Chris Lattner4b009652007-07-25 00:24:17 +0000737void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
738 unsigned TagType, DeclTy *TagDecl) {
739 SourceLocation LBraceLoc = ConsumeBrace();
740
741 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
742 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +0000743 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000744 Diag(Tok, diag::ext_empty_struct_union_enum,
745 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
746
747 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000748 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
749
Chris Lattner4b009652007-07-25 00:24:17 +0000750 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000751 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000752 // Each iteration of this loop reads one struct-declaration.
753
754 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000755 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000756 Diag(Tok, diag::ext_extra_struct_semi);
757 ConsumeToken();
758 continue;
759 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000760
761 // Parse all the comma separated declarators.
762 DeclSpec DS;
763 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +0000764 if (!Tok.is(tok::at)) {
765 ParseStructDeclaration(DS, FieldDeclarators);
766
767 // Convert them all to fields.
768 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
769 FieldDeclarator &FD = FieldDeclarators[i];
770 // Install the declarator into the current TagDecl.
771 DeclTy *Field = Actions.ActOnField(CurScope,
772 DS.getSourceRange().getBegin(),
773 FD.D, FD.BitfieldSize);
774 FieldDecls.push_back(Field);
775 }
776 } else { // Handle @defs
777 ConsumeToken();
778 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
779 Diag(Tok, diag::err_unexpected_at);
780 SkipUntil(tok::semi, true, true);
781 continue;
782 }
783 ConsumeToken();
784 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
785 if (!Tok.is(tok::identifier)) {
786 Diag(Tok, diag::err_expected_ident);
787 SkipUntil(tok::semi, true, true);
788 continue;
789 }
790 llvm::SmallVector<DeclTy*, 16> Fields;
791 Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
792 Fields);
793 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
794 ConsumeToken();
795 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
796 }
Chris Lattner4b009652007-07-25 00:24:17 +0000797
Chris Lattner34a01ad2007-10-09 17:33:22 +0000798 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000799 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +0000800 } else if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000801 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
802 break;
803 } else {
804 Diag(Tok, diag::err_expected_semi_decl_list);
805 // Skip to end of block or statement
806 SkipUntil(tok::r_brace, true, true);
807 }
808 }
809
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000810 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000811
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000812 Actions.ActOnFields(CurScope,
Chris Lattner43b885f2008-02-25 21:04:36 +0000813 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000814 LBraceLoc, RBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000815
816 AttributeList *AttrList = 0;
817 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000818 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000819 AttrList = ParseAttributes(); // FIXME: where should I put them?
820}
821
822
823/// ParseEnumSpecifier
824/// enum-specifier: [C99 6.7.2.2]
825/// 'enum' identifier[opt] '{' enumerator-list '}'
826/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
827/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
828/// '}' attributes[opt]
829/// 'enum' identifier
830/// [GNU] 'enum' attributes[opt] identifier
831void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000832 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000833 SourceLocation StartLoc = ConsumeToken();
834
835 // Parse the tag portion of this.
836 DeclTy *TagDecl;
837 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
838 return;
839
Chris Lattner34a01ad2007-10-09 17:33:22 +0000840 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000841 ParseEnumBody(StartLoc, TagDecl);
842
843 // TODO: semantic analysis on the declspec for enums.
844 const char *PrevSpec = 0;
845 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
846 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
847}
848
849/// ParseEnumBody - Parse a {} enclosed enumerator-list.
850/// enumerator-list:
851/// enumerator
852/// enumerator-list ',' enumerator
853/// enumerator:
854/// enumeration-constant
855/// enumeration-constant '=' constant-expression
856/// enumeration-constant:
857/// identifier
858///
859void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
860 SourceLocation LBraceLoc = ConsumeBrace();
861
Chris Lattnerc9a92452007-08-27 17:24:30 +0000862 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +0000863 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000864 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
865
866 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
867
868 DeclTy *LastEnumConstDecl = 0;
869
870 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000871 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000872 IdentifierInfo *Ident = Tok.getIdentifierInfo();
873 SourceLocation IdentLoc = ConsumeToken();
874
875 SourceLocation EqualLoc;
876 ExprTy *AssignedVal = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000877 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000878 EqualLoc = ConsumeToken();
879 ExprResult Res = ParseConstantExpression();
880 if (Res.isInvalid)
881 SkipUntil(tok::comma, tok::r_brace, true, true);
882 else
883 AssignedVal = Res.Val;
884 }
885
886 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000887 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +0000888 LastEnumConstDecl,
889 IdentLoc, Ident,
890 EqualLoc, AssignedVal);
891 EnumConstantDecls.push_back(EnumConstDecl);
892 LastEnumConstDecl = EnumConstDecl;
893
Chris Lattner34a01ad2007-10-09 17:33:22 +0000894 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000895 break;
896 SourceLocation CommaLoc = ConsumeToken();
897
Chris Lattner34a01ad2007-10-09 17:33:22 +0000898 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +0000899 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
900 }
901
902 // Eat the }.
903 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
904
Steve Naroff0acc9c92007-09-15 18:49:24 +0000905 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +0000906 EnumConstantDecls.size());
907
908 DeclTy *AttrList = 0;
909 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000910 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000911 AttrList = ParseAttributes(); // FIXME: where do they do?
912}
913
914/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +0000915/// start of a type-qualifier-list.
916bool Parser::isTypeQualifier() const {
917 switch (Tok.getKind()) {
918 default: return false;
919 // type-qualifier
920 case tok::kw_const:
921 case tok::kw_volatile:
922 case tok::kw_restrict:
923 return true;
924 }
925}
926
927/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +0000928/// start of a specifier-qualifier-list.
929bool Parser::isTypeSpecifierQualifier() const {
930 switch (Tok.getKind()) {
931 default: return false;
932 // GNU attributes support.
933 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000934 // GNU typeof support.
935 case tok::kw_typeof:
Steve Naroff5f0466b2008-06-05 00:02:44 +0000936 // GNU bizarre protocol extension. FIXME: make an extension?
937 case tok::less:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000938
Chris Lattner4b009652007-07-25 00:24:17 +0000939 // type-specifiers
940 case tok::kw_short:
941 case tok::kw_long:
942 case tok::kw_signed:
943 case tok::kw_unsigned:
944 case tok::kw__Complex:
945 case tok::kw__Imaginary:
946 case tok::kw_void:
947 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000948 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +0000949 case tok::kw_int:
950 case tok::kw_float:
951 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000952 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +0000953 case tok::kw__Bool:
954 case tok::kw__Decimal32:
955 case tok::kw__Decimal64:
956 case tok::kw__Decimal128:
957
Chris Lattner2e78db32008-04-13 18:59:07 +0000958 // struct-or-union-specifier (C99) or class-specifier (C++)
959 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000960 case tok::kw_struct:
961 case tok::kw_union:
962 // enum-specifier
963 case tok::kw_enum:
964
965 // type-qualifier
966 case tok::kw_const:
967 case tok::kw_volatile:
968 case tok::kw_restrict:
969 return true;
970
971 // typedef-name
972 case tok::identifier:
973 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000974 }
975}
976
977/// isDeclarationSpecifier() - Return true if the current token is part of a
978/// declaration specifier.
979bool Parser::isDeclarationSpecifier() const {
980 switch (Tok.getKind()) {
981 default: return false;
982 // storage-class-specifier
983 case tok::kw_typedef:
984 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +0000985 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +0000986 case tok::kw_static:
987 case tok::kw_auto:
988 case tok::kw_register:
989 case tok::kw___thread:
990
991 // type-specifiers
992 case tok::kw_short:
993 case tok::kw_long:
994 case tok::kw_signed:
995 case tok::kw_unsigned:
996 case tok::kw__Complex:
997 case tok::kw__Imaginary:
998 case tok::kw_void:
999 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001000 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001001 case tok::kw_int:
1002 case tok::kw_float:
1003 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001004 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001005 case tok::kw__Bool:
1006 case tok::kw__Decimal32:
1007 case tok::kw__Decimal64:
1008 case tok::kw__Decimal128:
1009
Chris Lattner2e78db32008-04-13 18:59:07 +00001010 // struct-or-union-specifier (C99) or class-specifier (C++)
1011 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001012 case tok::kw_struct:
1013 case tok::kw_union:
1014 // enum-specifier
1015 case tok::kw_enum:
1016
1017 // type-qualifier
1018 case tok::kw_const:
1019 case tok::kw_volatile:
1020 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001021
Chris Lattner4b009652007-07-25 00:24:17 +00001022 // function-specifier
1023 case tok::kw_inline:
Chris Lattnere35d2582007-08-09 16:40:21 +00001024
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001025 // GNU typeof support.
1026 case tok::kw_typeof:
1027
1028 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001029 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001030 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001031
1032 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1033 case tok::less:
1034 return getLang().ObjC1;
Chris Lattner4b009652007-07-25 00:24:17 +00001035
1036 // typedef-name
1037 case tok::identifier:
1038 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001039 }
1040}
1041
1042
1043/// ParseTypeQualifierListOpt
1044/// type-qualifier-list: [C99 6.7.5]
1045/// type-qualifier
1046/// [GNU] attributes
1047/// type-qualifier-list type-qualifier
1048/// [GNU] type-qualifier-list attributes
1049///
1050void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1051 while (1) {
1052 int isInvalid = false;
1053 const char *PrevSpec = 0;
1054 SourceLocation Loc = Tok.getLocation();
1055
1056 switch (Tok.getKind()) {
1057 default:
1058 // If this is not a type-qualifier token, we're done reading type
1059 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +00001060 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +00001061 return;
1062 case tok::kw_const:
1063 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1064 getLang())*2;
1065 break;
1066 case tok::kw_volatile:
1067 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1068 getLang())*2;
1069 break;
1070 case tok::kw_restrict:
1071 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1072 getLang())*2;
1073 break;
1074 case tok::kw___attribute:
1075 DS.AddAttributes(ParseAttributes());
1076 continue; // do *not* consume the next token!
1077 }
1078
1079 // If the specifier combination wasn't legal, issue a diagnostic.
1080 if (isInvalid) {
1081 assert(PrevSpec && "Method did not return previous specifier!");
1082 if (isInvalid == 1) // Error.
1083 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1084 else // extwarn.
1085 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1086 }
1087 ConsumeToken();
1088 }
1089}
1090
1091
1092/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1093///
1094void Parser::ParseDeclarator(Declarator &D) {
1095 /// This implements the 'declarator' production in the C grammar, then checks
1096 /// for well-formedness and issues diagnostics.
1097 ParseDeclaratorInternal(D);
Chris Lattner4b009652007-07-25 00:24:17 +00001098}
1099
1100/// ParseDeclaratorInternal
1101/// declarator: [C99 6.7.5]
1102/// pointer[opt] direct-declarator
1103/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1104/// [GNU] '&' restrict[opt] attributes[opt] declarator
1105///
1106/// pointer: [C99 6.7.5]
1107/// '*' type-qualifier-list[opt]
1108/// '*' type-qualifier-list[opt] pointer
1109///
1110void Parser::ParseDeclaratorInternal(Declarator &D) {
1111 tok::TokenKind Kind = Tok.getKind();
1112
1113 // Not a pointer or C++ reference.
Chris Lattner69f01932008-02-21 01:32:26 +00001114 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus))
Chris Lattner4b009652007-07-25 00:24:17 +00001115 return ParseDirectDeclarator(D);
1116
1117 // Otherwise, '*' -> pointer or '&' -> reference.
1118 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1119
1120 if (Kind == tok::star) {
Chris Lattner69f01932008-02-21 01:32:26 +00001121 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001122 DeclSpec DS;
1123
1124 ParseTypeQualifierListOpt(DS);
1125
1126 // Recursively parse the declarator.
1127 ParseDeclaratorInternal(D);
1128
1129 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001130 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1131 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001132 } else {
1133 // Is a reference
1134 DeclSpec DS;
1135
1136 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1137 // cv-qualifiers are introduced through the use of a typedef or of a
1138 // template type argument, in which case the cv-qualifiers are ignored.
1139 //
1140 // [GNU] Retricted references are allowed.
1141 // [GNU] Attributes on references are allowed.
1142 ParseTypeQualifierListOpt(DS);
1143
1144 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1145 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1146 Diag(DS.getConstSpecLoc(),
1147 diag::err_invalid_reference_qualifier_application,
1148 "const");
1149 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1150 Diag(DS.getVolatileSpecLoc(),
1151 diag::err_invalid_reference_qualifier_application,
1152 "volatile");
1153 }
1154
1155 // Recursively parse the declarator.
1156 ParseDeclaratorInternal(D);
1157
1158 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001159 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1160 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001161 }
1162}
1163
1164/// ParseDirectDeclarator
1165/// direct-declarator: [C99 6.7.5]
1166/// identifier
1167/// '(' declarator ')'
1168/// [GNU] '(' attributes declarator ')'
1169/// [C90] direct-declarator '[' constant-expression[opt] ']'
1170/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1171/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1172/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1173/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1174/// direct-declarator '(' parameter-type-list ')'
1175/// direct-declarator '(' identifier-list[opt] ')'
1176/// [GNU] direct-declarator '(' parameter-forward-declarations
1177/// parameter-type-list[opt] ')'
1178///
1179void Parser::ParseDirectDeclarator(Declarator &D) {
1180 // Parse the first direct-declarator seen.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001181 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001182 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1183 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1184 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001185 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001186 // direct-declarator: '(' declarator ')'
1187 // direct-declarator: '(' attributes declarator ')'
1188 // Example: 'char (*X)' or 'int (*XX)(void)'
1189 ParseParenDeclarator(D);
1190 } else if (D.mayOmitIdentifier()) {
1191 // This could be something simple like "int" (in which case the declarator
1192 // portion is empty), if an abstract-declarator is allowed.
1193 D.SetIdentifier(0, Tok.getLocation());
1194 } else {
1195 // Expected identifier or '('.
1196 Diag(Tok, diag::err_expected_ident_lparen);
1197 D.SetIdentifier(0, Tok.getLocation());
1198 }
1199
1200 assert(D.isPastIdentifier() &&
1201 "Haven't past the location of the identifier yet?");
1202
1203 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001204 if (Tok.is(tok::l_paren)) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00001205 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001206 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001207 ParseBracketDeclarator(D);
1208 } else {
1209 break;
1210 }
1211 }
1212}
1213
Chris Lattnera0d056d2008-04-06 05:45:57 +00001214/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1215/// only called before the identifier, so these are most likely just grouping
1216/// parens for precedence. If we find that these are actually function
1217/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1218///
1219/// direct-declarator:
1220/// '(' declarator ')'
1221/// [GNU] '(' attributes declarator ')'
1222///
1223void Parser::ParseParenDeclarator(Declarator &D) {
1224 SourceLocation StartLoc = ConsumeParen();
1225 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1226
1227 // If we haven't past the identifier yet (or where the identifier would be
1228 // stored, if this is an abstract declarator), then this is probably just
1229 // grouping parens. However, if this could be an abstract-declarator, then
1230 // this could also be the start of function arguments (consider 'void()').
1231 bool isGrouping;
1232
1233 if (!D.mayOmitIdentifier()) {
1234 // If this can't be an abstract-declarator, this *must* be a grouping
1235 // paren, because we haven't seen the identifier yet.
1236 isGrouping = true;
1237 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
1238 isDeclarationSpecifier()) { // 'int(int)' is a function.
1239 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1240 // considered to be a type, not a K&R identifier-list.
1241 isGrouping = false;
1242 } else {
1243 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1244 isGrouping = true;
1245 }
1246
1247 // If this is a grouping paren, handle:
1248 // direct-declarator: '(' declarator ')'
1249 // direct-declarator: '(' attributes declarator ')'
1250 if (isGrouping) {
1251 if (Tok.is(tok::kw___attribute))
1252 D.AddAttributes(ParseAttributes());
1253
1254 ParseDeclaratorInternal(D);
1255 // Match the ')'.
1256 MatchRHSPunctuation(tok::r_paren, StartLoc);
1257 return;
1258 }
1259
1260 // Okay, if this wasn't a grouping paren, it must be the start of a function
1261 // argument list. Recognize that this declarator will never have an
1262 // identifier (and remember where it would have been), then fall through to
1263 // the handling of argument lists.
1264 D.SetIdentifier(0, Tok.getLocation());
1265
1266 ParseFunctionDeclarator(StartLoc, D);
1267}
1268
1269/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1270/// declarator D up to a paren, which indicates that we are parsing function
1271/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001272///
1273/// This method also handles this portion of the grammar:
1274/// parameter-type-list: [C99 6.7.5]
1275/// parameter-list
1276/// parameter-list ',' '...'
1277///
1278/// parameter-list: [C99 6.7.5]
1279/// parameter-declaration
1280/// parameter-list ',' parameter-declaration
1281///
1282/// parameter-declaration: [C99 6.7.5]
1283/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00001284/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001285/// [GNU] declaration-specifiers declarator attributes
1286/// declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00001287/// [C++] declaration-specifiers abstract-declarator[opt]
1288/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001289/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1290///
Chris Lattnera0d056d2008-04-06 05:45:57 +00001291void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D) {
1292 // lparen is already consumed!
1293 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00001294
1295 // Okay, this is the parameter list of a function definition, or it is an
1296 // identifier list of a K&R-style function.
Chris Lattner4b009652007-07-25 00:24:17 +00001297
Chris Lattner34a01ad2007-10-09 17:33:22 +00001298 if (Tok.is(tok::r_paren)) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00001299 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00001300 // int() -> no prototype, no '...'.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001301 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/ false,
1302 /*variadic*/ false,
1303 /*arglist*/ 0, 0, LParenLoc));
1304
1305 ConsumeParen(); // Eat the closing ')'.
1306 return;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001307 } else if (Tok.is(tok::identifier) &&
Chris Lattner4b009652007-07-25 00:24:17 +00001308 // K&R identifier lists can't have typedefs as identifiers, per
1309 // C99 6.7.5.3p11.
1310 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1311 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1312 // normal declarators, not for abstract-declarators.
Chris Lattner35d9c912008-04-06 06:34:08 +00001313 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001314 }
1315
1316 // Finally, a normal, non-empty parameter type list.
1317
1318 // Build up an array of information about the parsed arguments.
1319 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00001320
1321 // Enter function-declaration scope, limiting any declarators to the
1322 // function prototype scope, including parameter declarators.
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00001323 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001324
1325 bool IsVariadic = false;
1326 while (1) {
1327 if (Tok.is(tok::ellipsis)) {
1328 IsVariadic = true;
Chris Lattner4b009652007-07-25 00:24:17 +00001329
Chris Lattner9f7564b2008-04-06 06:57:35 +00001330 // Check to see if this is "void(...)" which is not allowed.
1331 if (ParamInfo.empty()) {
1332 // Otherwise, parse parameter type list. If it starts with an
1333 // ellipsis, diagnose the malformed function.
1334 Diag(Tok, diag::err_ellipsis_first_arg);
1335 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001336 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00001337
Chris Lattner9f7564b2008-04-06 06:57:35 +00001338 ConsumeToken(); // Consume the ellipsis.
1339 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001340 }
1341
Chris Lattner9f7564b2008-04-06 06:57:35 +00001342 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00001343
Chris Lattner9f7564b2008-04-06 06:57:35 +00001344 // Parse the declaration-specifiers.
1345 DeclSpec DS;
1346 ParseDeclarationSpecifiers(DS);
1347
1348 // Parse the declarator. This is "PrototypeContext", because we must
1349 // accept either 'declarator' or 'abstract-declarator' here.
1350 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1351 ParseDeclarator(ParmDecl);
1352
1353 // Parse GNU attributes, if present.
1354 if (Tok.is(tok::kw___attribute))
1355 ParmDecl.AddAttributes(ParseAttributes());
1356
Chris Lattner9f7564b2008-04-06 06:57:35 +00001357 // Remember this parsed parameter in ParamInfo.
1358 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1359
Chris Lattner9f7564b2008-04-06 06:57:35 +00001360 // If no parameter was specified, verify that *something* was specified,
1361 // otherwise we have a missing type and identifier.
1362 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1363 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1364 // Completely missing, emit error.
1365 Diag(DSStart, diag::err_missing_param);
1366 } else {
1367 // Otherwise, we have something. Add it and let semantic analysis try
1368 // to grok it and add the result to the ParamInfo we are building.
1369
1370 // Inform the actions module about the parameter declarator, so it gets
1371 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00001372 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1373
1374 // Parse the default argument, if any. We parse the default
1375 // arguments in all dialects; the semantic analysis in
1376 // ActOnParamDefaultArgument will reject the default argument in
1377 // C.
1378 if (Tok.is(tok::equal)) {
1379 SourceLocation EqualLoc = Tok.getLocation();
1380
1381 // Consume the '='.
1382 ConsumeToken();
1383
1384 // Parse the default argument
Chris Lattner3e254fb2008-04-08 04:40:51 +00001385 ExprResult DefArgResult = ParseAssignmentExpression();
1386 if (DefArgResult.isInvalid) {
1387 SkipUntil(tok::comma, tok::r_paren, true, true);
1388 } else {
1389 // Inform the actions module about the default argument
1390 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1391 }
1392 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001393
1394 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001395 ParmDecl.getIdentifierLoc(), Param));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001396 }
1397
1398 // If the next token is a comma, consume it and keep reading arguments.
1399 if (Tok.isNot(tok::comma)) break;
1400
1401 // Consume the comma.
1402 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00001403 }
1404
Chris Lattner9f7564b2008-04-06 06:57:35 +00001405 // Leave prototype scope.
1406 ExitScope();
1407
Chris Lattner4b009652007-07-25 00:24:17 +00001408 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001409 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1410 &ParamInfo[0], ParamInfo.size(),
1411 LParenLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00001412
1413 // If we have the closing ')', eat it and we're done.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001414 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001415}
1416
Chris Lattner35d9c912008-04-06 06:34:08 +00001417/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1418/// we found a K&R-style identifier list instead of a type argument list. The
1419/// current token is known to be the first identifier in the list.
1420///
1421/// identifier-list: [C99 6.7.5]
1422/// identifier
1423/// identifier-list ',' identifier
1424///
1425void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1426 Declarator &D) {
1427 // Build up an array of information about the parsed arguments.
1428 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1429 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1430
1431 // If there was no identifier specified for the declarator, either we are in
1432 // an abstract-declarator, or we are in a parameter declarator which was found
1433 // to be abstract. In abstract-declarators, identifier lists are not valid:
1434 // diagnose this.
1435 if (!D.getIdentifier())
1436 Diag(Tok, diag::ext_ident_list_in_param);
1437
1438 // Tok is known to be the first identifier in the list. Remember this
1439 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00001440 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00001441 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1442 Tok.getLocation(), 0));
1443
Chris Lattner113a56b2008-04-06 06:39:19 +00001444 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00001445
1446 while (Tok.is(tok::comma)) {
1447 // Eat the comma.
1448 ConsumeToken();
1449
Chris Lattner113a56b2008-04-06 06:39:19 +00001450 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00001451 if (Tok.isNot(tok::identifier)) {
1452 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00001453 SkipUntil(tok::r_paren);
1454 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00001455 }
Chris Lattneracb67d92008-04-06 06:47:48 +00001456
Chris Lattner35d9c912008-04-06 06:34:08 +00001457 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00001458
1459 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1460 if (Actions.isTypeName(*ParmII, CurScope))
1461 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner35d9c912008-04-06 06:34:08 +00001462
1463 // Verify that the argument identifier has not already been mentioned.
1464 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner113a56b2008-04-06 06:39:19 +00001465 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1466 } else {
1467 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00001468 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1469 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00001470 }
Chris Lattner35d9c912008-04-06 06:34:08 +00001471
1472 // Eat the identifier.
1473 ConsumeToken();
1474 }
1475
Chris Lattner113a56b2008-04-06 06:39:19 +00001476 // Remember that we parsed a function type, and remember the attributes. This
1477 // function type is always a K&R style function type, which is not varargs and
1478 // has no prototype.
1479 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1480 &ParamInfo[0], ParamInfo.size(),
1481 LParenLoc));
Chris Lattner35d9c912008-04-06 06:34:08 +00001482
1483 // If we have the closing ')', eat it and we're done.
Chris Lattner113a56b2008-04-06 06:39:19 +00001484 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00001485}
Chris Lattnera0d056d2008-04-06 05:45:57 +00001486
Chris Lattner4b009652007-07-25 00:24:17 +00001487/// [C90] direct-declarator '[' constant-expression[opt] ']'
1488/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1489/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1490/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1491/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1492void Parser::ParseBracketDeclarator(Declarator &D) {
1493 SourceLocation StartLoc = ConsumeBracket();
1494
1495 // If valid, this location is the position where we read the 'static' keyword.
1496 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001497 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001498 StaticLoc = ConsumeToken();
1499
1500 // If there is a type-qualifier-list, read it now.
1501 DeclSpec DS;
1502 ParseTypeQualifierListOpt(DS);
1503
1504 // If we haven't already read 'static', check to see if there is one after the
1505 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001506 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001507 StaticLoc = ConsumeToken();
1508
1509 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1510 bool isStar = false;
1511 ExprResult NumElements(false);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001512
1513 // Handle the case where we have '[*]' as the array size. However, a leading
1514 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1515 // the the token after the star is a ']'. Since stars in arrays are
1516 // infrequent, use of lookahead is not costly here.
1517 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00001518 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00001519
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001520 if (StaticLoc.isValid())
1521 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1522 StaticLoc = SourceLocation(); // Drop the static.
1523 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001524 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001525 // Parse the assignment-expression now.
1526 NumElements = ParseAssignmentExpression();
1527 }
1528
1529 // If there was an error parsing the assignment-expression, recover.
1530 if (NumElements.isInvalid) {
1531 // If the expression was invalid, skip it.
1532 SkipUntil(tok::r_square);
1533 return;
1534 }
1535
1536 MatchRHSPunctuation(tok::r_square, StartLoc);
1537
1538 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1539 // it was not a constant expression.
1540 if (!getLang().C99) {
1541 // TODO: check C90 array constant exprness.
1542 if (isStar || StaticLoc.isValid() ||
1543 0/*TODO: NumElts is not a C90 constantexpr */)
1544 Diag(StartLoc, diag::ext_c99_array_usage);
1545 }
1546
1547 // Remember that we parsed a pointer type, and remember the type-quals.
1548 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1549 StaticLoc.isValid(), isStar,
1550 NumElements.Val, StartLoc));
1551}
1552
Steve Naroff7cbb1462007-07-31 12:34:36 +00001553/// [GNU] typeof-specifier:
1554/// typeof ( expressions )
1555/// typeof ( type-name )
1556///
1557void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001558 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00001559 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00001560 SourceLocation StartLoc = ConsumeToken();
1561
Chris Lattner34a01ad2007-10-09 17:33:22 +00001562 if (Tok.isNot(tok::l_paren)) {
Steve Naroff14bbce82007-08-02 02:53:48 +00001563 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1564 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00001565 }
1566 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1567
1568 if (isTypeSpecifierQualifier()) {
1569 TypeTy *Ty = ParseTypeName();
1570
Steve Naroff4c255ab2007-07-31 23:56:32 +00001571 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1572
Chris Lattner34a01ad2007-10-09 17:33:22 +00001573 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001574 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001575 return;
1576 }
1577 RParenLoc = ConsumeParen();
1578 const char *PrevSpec = 0;
1579 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1580 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1581 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001582 } else { // we have an expression.
1583 ExprResult Result = ParseExpression();
Steve Naroff4c255ab2007-07-31 23:56:32 +00001584
Chris Lattner34a01ad2007-10-09 17:33:22 +00001585 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001586 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001587 return;
1588 }
1589 RParenLoc = ConsumeParen();
1590 const char *PrevSpec = 0;
1591 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1592 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1593 Result.Val))
1594 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001595 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00001596}
1597
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00001598