blob: eb5cedd5f40e0f65f3616f37285e206802b41751 [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"
Chris Lattner52a425b2009-01-27 18:30:58 +000015#include "clang/Basic/DiagnosticParse.h"
Chris Lattnera7549902007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerdaa5c002008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Sebastian Redl6008ac32008-11-25 22:21:31 +000018#include "AstGuard.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "llvm/ADT/SmallSet.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
26/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Sebastian Redl66df3ef2008-12-02 14:43:59 +000031Parser::TypeTy *Parser::ParseTypeName() {
Chris Lattner4b009652007-07-25 00:24:17 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
34 ParseSpecifierQualifierList(DS);
35
36 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
39
Douglas Gregor10a18fc2009-01-26 22:44:13 +000040 return Actions.ActOnTypeName(CurScope, DeclaratorInfo).get();
Chris Lattner4b009652007-07-25 00:24:17 +000041}
42
43/// ParseAttributes - Parse a non-empty attributes list.
44///
45/// [GNU] attributes:
46/// attribute
47/// attributes attribute
48///
49/// [GNU] attribute:
50/// '__attribute__' '(' '(' attribute-list ')' ')'
51///
52/// [GNU] attribute-list:
53/// attrib
54/// attribute_list ',' attrib
55///
56/// [GNU] attrib:
57/// empty
58/// attrib-name
59/// attrib-name '(' identifier ')'
60/// attrib-name '(' identifier ',' nonempty-expr-list ')'
61/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
62///
63/// [GNU] attrib-name:
64/// identifier
65/// typespec
66/// typequal
67/// storageclass
68///
69/// FIXME: The GCC grammar/code for this construct implies we need two
70/// token lookahead. Comment from gcc: "If they start with an identifier
71/// which is followed by a comma or close parenthesis, then the arguments
72/// start with that identifier; otherwise they are an expression list."
73///
74/// At the moment, I am not doing 2 token lookahead. I am also unaware of
75/// any attributes that don't work (based on my limited testing). Most
76/// attributes are very simple in practice. Until we find a bug, I don't see
77/// a pressing need to implement the 2 token lookahead.
78
79AttributeList *Parser::ParseAttributes() {
Chris Lattner34a01ad2007-10-09 17:33:22 +000080 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Chris Lattner4b009652007-07-25 00:24:17 +000081
82 AttributeList *CurrAttr = 0;
83
Chris Lattner34a01ad2007-10-09 17:33:22 +000084 while (Tok.is(tok::kw___attribute)) {
Chris Lattner4b009652007-07-25 00:24:17 +000085 ConsumeToken();
86 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
87 "attribute")) {
88 SkipUntil(tok::r_paren, true); // skip until ) or ;
89 return CurrAttr;
90 }
91 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
92 SkipUntil(tok::r_paren, true); // skip until ) or ;
93 return CurrAttr;
94 }
95 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner34a01ad2007-10-09 17:33:22 +000096 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
97 Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +000098
Chris Lattner34a01ad2007-10-09 17:33:22 +000099 if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000100 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
101 ConsumeToken();
102 continue;
103 }
104 // we have an identifier or declaration specifier (const, int, etc.)
105 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
106 SourceLocation AttrNameLoc = ConsumeToken();
107
108 // check if we have a "paramterized" attribute
Chris Lattner34a01ad2007-10-09 17:33:22 +0000109 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000110 ConsumeParen(); // ignore the left paren loc for now
111
Chris Lattner34a01ad2007-10-09 17:33:22 +0000112 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000113 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
114 SourceLocation ParmLoc = ConsumeToken();
115
Chris Lattner34a01ad2007-10-09 17:33:22 +0000116 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000117 // __attribute__(( mode(byte) ))
118 ConsumeParen(); // ignore the right paren loc for now
119 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
120 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000121 } else if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000122 ConsumeToken();
123 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000124 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000125 bool ArgExprsOk = true;
126
127 // now parse the non-empty comma separated list of expressions
128 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000129 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000130 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000131 ArgExprsOk = false;
132 SkipUntil(tok::r_paren);
133 break;
134 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000135 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000136 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000137 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000138 break;
139 ConsumeToken(); // Eat the comma, move to the next argument
140 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000141 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000142 ConsumeParen(); // ignore the right paren loc for now
143 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redl6008ac32008-11-25 22:21:31 +0000144 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Chris Lattner4b009652007-07-25 00:24:17 +0000145 }
146 }
147 } else { // not an identifier
148 // parse a possibly empty comma separated list of expressions
Chris Lattner34a01ad2007-10-09 17:33:22 +0000149 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000150 // __attribute__(( nonnull() ))
151 ConsumeParen(); // ignore the right paren loc for now
152 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
153 0, SourceLocation(), 0, 0, CurrAttr);
154 } else {
155 // __attribute__(( aligned(16) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000156 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000157 bool ArgExprsOk = true;
158
159 // now parse the list of expressions
160 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000161 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000162 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000163 ArgExprsOk = false;
164 SkipUntil(tok::r_paren);
165 break;
166 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000167 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000168 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000169 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000170 break;
171 ConsumeToken(); // Eat the comma, move to the next argument
172 }
173 // Match the ')'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000174 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000175 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl6008ac32008-11-25 22:21:31 +0000176 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
177 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Chris Lattner4b009652007-07-25 00:24:17 +0000178 CurrAttr);
179 }
180 }
181 }
182 } else {
183 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
184 0, SourceLocation(), 0, 0, CurrAttr);
185 }
186 }
187 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
188 SkipUntil(tok::r_paren, false);
189 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
190 SkipUntil(tok::r_paren, false);
191 }
192 return CurrAttr;
193}
194
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000195/// FuzzyParseMicrosoftDeclSpec. When -fms-extensions is enabled, this
196/// routine is called to skip/ignore tokens that comprise the MS declspec.
197void Parser::FuzzyParseMicrosoftDeclSpec() {
198 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
199 ConsumeToken();
200 if (Tok.is(tok::l_paren)) {
201 unsigned short savedParenCount = ParenCount;
202 do {
203 ConsumeAnyToken();
204 } while (ParenCount > savedParenCount && Tok.isNot(tok::eof));
205 }
206 return;
207}
208
Chris Lattner4b009652007-07-25 00:24:17 +0000209/// ParseDeclaration - Parse a full 'declaration', which consists of
210/// declaration-specifiers, some number of declarators, and a semicolon.
211/// 'Context' should be a Declarator::TheContext value.
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000212///
213/// declaration: [C99 6.7]
214/// block-declaration ->
215/// simple-declaration
216/// others [FIXME]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000217/// [C++] template-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000218/// [C++] namespace-definition
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000219/// [C++] using-directive
220/// [C++] using-declaration [TODO]
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000221/// others... [FIXME]
222///
Chris Lattner4b009652007-07-25 00:24:17 +0000223Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000224 switch (Tok.getKind()) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000225 case tok::kw_export:
226 case tok::kw_template:
227 return ParseTemplateDeclaration(Context);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000228 case tok::kw_namespace:
229 return ParseNamespace(Context);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000230 case tok::kw_using:
231 return ParseUsingDirectiveOrDeclaration(Context);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000232 default:
233 return ParseSimpleDeclaration(Context);
234 }
235}
236
237/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
238/// declaration-specifiers init-declarator-list[opt] ';'
239///[C90/C++]init-declarator-list ';' [TODO]
240/// [OMP] threadprivate-directive [TODO]
241Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Chris Lattner4b009652007-07-25 00:24:17 +0000242 // Parse the common declaration-specifiers piece.
243 DeclSpec DS;
244 ParseDeclarationSpecifiers(DS);
245
246 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
247 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000248 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000249 ConsumeToken();
250 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
251 }
252
253 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
254 ParseDeclarator(DeclaratorInfo);
255
256 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
257}
258
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000259
Chris Lattner4b009652007-07-25 00:24:17 +0000260/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
261/// parsing 'declaration-specifiers declarator'. This method is split out this
262/// way to handle the ambiguity between top-level function-definitions and
263/// declarations.
264///
Chris Lattner4b009652007-07-25 00:24:17 +0000265/// init-declarator-list: [C99 6.7]
266/// init-declarator
267/// init-declarator-list ',' init-declarator
268/// init-declarator: [C99 6.7]
269/// declarator
270/// declarator '=' initializer
271/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
272/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000273/// [C++] declarator initializer[opt]
274///
275/// [C++] initializer:
276/// [C++] '=' initializer-clause
277/// [C++] '(' expression-list ')'
Chris Lattner4b009652007-07-25 00:24:17 +0000278///
279Parser::DeclTy *Parser::
280ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
281
282 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
283 // that they can be chained properly if the actions want this.
284 Parser::DeclTy *LastDeclInGroup = 0;
285
286 // At this point, we know that it is not a function definition. Parse the
287 // rest of the init-declarator-list.
288 while (1) {
289 // If a simple-asm-expr is present, parse it.
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000290 if (Tok.is(tok::kw_asm)) {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000291 OwningExprResult AsmLabel(ParseSimpleAsm());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000292 if (AsmLabel.isInvalid()) {
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000293 SkipUntil(tok::semi);
294 return 0;
295 }
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000296
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000297 D.setAsmLabel(AsmLabel.release());
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000298 }
Chris Lattner4b009652007-07-25 00:24:17 +0000299
300 // If attributes are present, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000301 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000302 D.AddAttributes(ParseAttributes());
Steve Naroff6a0e2092007-09-12 14:07:44 +0000303
304 // Inform the current actions module that we just parsed this declarator.
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000305 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000306
Chris Lattner4b009652007-07-25 00:24:17 +0000307 // Parse declarator '=' initializer.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000308 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000309 ConsumeToken();
Sebastian Redl39d4f022008-12-11 22:51:44 +0000310 OwningExprResult Init(ParseInitializer());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000311 if (Init.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000312 SkipUntil(tok::semi);
313 return 0;
314 }
Sebastian Redl6619aa52009-01-18 18:03:53 +0000315 Actions.AddInitializerToDecl(LastDeclInGroup, move_arg(Init));
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000316 } else if (Tok.is(tok::l_paren)) {
317 // Parse C++ direct initializer: '(' expression-list ')'
318 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl6008ac32008-11-25 22:21:31 +0000319 ExprVector Exprs(Actions);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000320 CommaLocsTy CommaLocs;
321
322 bool InvalidExpr = false;
323 if (ParseExpressionList(Exprs, CommaLocs)) {
324 SkipUntil(tok::r_paren);
325 InvalidExpr = true;
326 }
327 // Match the ')'.
328 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
329
330 if (!InvalidExpr) {
331 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
332 "Unexpected number of commas!");
333 Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc,
Sebastian Redl6008ac32008-11-25 22:21:31 +0000334 Exprs.take(), Exprs.size(),
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000335 &CommaLocs[0], RParenLoc);
336 }
Douglas Gregor81c29152008-10-29 00:13:59 +0000337 } else {
338 Actions.ActOnUninitializedDecl(LastDeclInGroup);
Chris Lattner4b009652007-07-25 00:24:17 +0000339 }
340
Chris Lattner4b009652007-07-25 00:24:17 +0000341 // If we don't have a comma, it is either the end of the list (a ';') or an
342 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000343 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000344 break;
345
346 // Consume the comma.
347 ConsumeToken();
348
349 // Parse the next declarator.
350 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000351
352 // Accept attributes in an init-declarator. In the first declarator in a
353 // declaration, these would be part of the declspec. In subsequent
354 // declarators, they become part of the declarator itself, so that they
355 // don't apply to declarators after *this* one. Examples:
356 // short __attribute__((common)) var; -> declspec
357 // short var __attribute__((common)); -> declarator
358 // short x, __attribute__((common)) var; -> declarator
359 if (Tok.is(tok::kw___attribute))
360 D.AddAttributes(ParseAttributes());
361
Chris Lattner4b009652007-07-25 00:24:17 +0000362 ParseDeclarator(D);
363 }
364
Chris Lattner34a01ad2007-10-09 17:33:22 +0000365 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000366 ConsumeToken();
Fariborz Jahanianc1509b02009-01-17 00:00:40 +0000367 // for(is key; in keys) is error.
368 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
369 Diag(Tok, diag::err_parse_error);
370 return 0;
371 }
Chris Lattner4b009652007-07-25 00:24:17 +0000372 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
373 }
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000374 // If this is an ObjC2 for-each loop, this is a successful declarator
375 // parse. The syntax for these looks like:
376 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000377 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000378 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
379 }
Chris Lattner4b009652007-07-25 00:24:17 +0000380 Diag(Tok, diag::err_parse_error);
381 // Skip to end of block or statement
Chris Lattnerf491b412007-08-21 18:36:18 +0000382 SkipUntil(tok::r_brace, true, true);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000383 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000384 ConsumeToken();
385 return 0;
386}
387
388/// ParseSpecifierQualifierList
389/// specifier-qualifier-list:
390/// type-specifier specifier-qualifier-list[opt]
391/// type-qualifier specifier-qualifier-list[opt]
392/// [GNU] attributes specifier-qualifier-list[opt]
393///
394void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
395 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
396 /// parse declaration-specifiers and complain about extra stuff.
397 ParseDeclarationSpecifiers(DS);
398
399 // Validate declspec for type-name.
400 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000401 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000402 Diag(Tok, diag::err_typename_requires_specqual);
403
404 // Issue diagnostic and remove storage class if present.
405 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
406 if (DS.getStorageClassSpecLoc().isValid())
407 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
408 else
409 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
410 DS.ClearStorageClassSpecs();
411 }
412
413 // Issue diagnostic and remove function specfier if present.
414 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000415 if (DS.isInlineSpecified())
416 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
417 if (DS.isVirtualSpecified())
418 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
419 if (DS.isExplicitSpecified())
420 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000421 DS.ClearFunctionSpecs();
422 }
423}
424
425/// ParseDeclarationSpecifiers
426/// declaration-specifiers: [C99 6.7]
427/// storage-class-specifier declaration-specifiers[opt]
428/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000429/// [C99] function-specifier declaration-specifiers[opt]
430/// [GNU] attributes declaration-specifiers[opt]
431///
432/// storage-class-specifier: [C99 6.7.1]
433/// 'typedef'
434/// 'extern'
435/// 'static'
436/// 'auto'
437/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000438/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000439/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000440/// function-specifier: [C99 6.7.4]
441/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000442/// [C++] 'virtual'
443/// [C++] 'explicit'
Chris Lattner4b009652007-07-25 00:24:17 +0000444///
Douglas Gregor52473432008-12-24 02:52:09 +0000445void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Chris Lattner712f9a32009-01-05 00:07:25 +0000446 TemplateParameterLists *TemplateParams){
Chris Lattnera4ff4272008-03-13 06:29:04 +0000447 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000448 while (1) {
449 int isInvalid = false;
450 const char *PrevSpec = 0;
451 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000452
Chris Lattner4b009652007-07-25 00:24:17 +0000453 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000454 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000455 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000456 // If this is not a declaration specifier token, we're done reading decl
457 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000458 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000459 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000460
461 case tok::coloncolon: // ::foo::bar
462 // Annotate C++ scope specifiers. If we get one, loop.
463 if (TryAnnotateCXXScopeToken())
464 continue;
465 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000466
467 case tok::annot_cxxscope: {
468 if (DS.hasTypeSpecifier())
469 goto DoneWithDeclSpec;
470
471 // We are looking for a qualified typename.
472 if (NextToken().isNot(tok::identifier))
473 goto DoneWithDeclSpec;
474
475 CXXScopeSpec SS;
476 SS.setScopeRep(Tok.getAnnotationValue());
477 SS.setRange(Tok.getAnnotationRange());
478
479 // If the next token is the name of the class type that the C++ scope
480 // denotes, followed by a '(', then this is a constructor declaration.
481 // We're done with the decl-specifiers.
482 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
483 CurScope, &SS) &&
484 GetLookAheadToken(2).is(tok::l_paren))
485 goto DoneWithDeclSpec;
486
Steve Naroff7b36a1b2009-01-28 19:39:02 +0000487 TypeTy *TypeRep = Actions.getTypeName(*NextToken().getIdentifierInfo(),
488 CurScope, &SS);
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000489 if (TypeRep == 0)
490 goto DoneWithDeclSpec;
491
492 ConsumeToken(); // The C++ scope.
493
494 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
495 TypeRep);
496 if (isInvalid)
497 break;
498
499 DS.SetRangeEnd(Tok.getLocation());
500 ConsumeToken(); // The typename.
501
502 continue;
503 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000504
505 case tok::annot_typename: {
506 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
507 Tok.getAnnotationValue());
508 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
509 ConsumeToken(); // The typename
510
511 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
512 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
513 // Objective-C interface. If we don't have Objective-C or a '<', this is
514 // just a normal reference to a typedef name.
515 if (!Tok.is(tok::less) || !getLang().ObjC1)
516 continue;
517
518 SourceLocation EndProtoLoc;
519 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
520 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
521 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
522
523 DS.SetRangeEnd(EndProtoLoc);
524 continue;
525 }
526
Chris Lattnerfda18db2008-07-26 01:18:38 +0000527 // typedef-name
528 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000529 // In C++, check to see if this is a scope specifier like foo::bar::, if
530 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000531 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
532 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000533
Chris Lattnerfda18db2008-07-26 01:18:38 +0000534 // This identifier can only be a typedef name if we haven't already seen
535 // a type-specifier. Without this check we misparse:
536 // typedef int X; struct Y { short X; }; as 'short int'.
537 if (DS.hasTypeSpecifier())
538 goto DoneWithDeclSpec;
539
540 // It has to be available as a typedef too!
Steve Naroff7b36a1b2009-01-28 19:39:02 +0000541 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattnerfda18db2008-07-26 01:18:38 +0000542 if (TypeRep == 0)
543 goto DoneWithDeclSpec;
544
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000545 // C++: If the identifier is actually the name of the class type
546 // being defined and the next token is a '(', then this is a
547 // constructor declaration. We're done with the decl-specifiers
548 // and will treat this token as an identifier.
549 if (getLang().CPlusPlus &&
Douglas Gregorcab994d2009-01-09 22:42:13 +0000550 CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000551 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
552 NextToken().getKind() == tok::l_paren)
553 goto DoneWithDeclSpec;
554
Chris Lattnerfda18db2008-07-26 01:18:38 +0000555 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
556 TypeRep);
557 if (isInvalid)
558 break;
559
560 DS.SetRangeEnd(Tok.getLocation());
561 ConsumeToken(); // The identifier
562
563 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
564 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
565 // Objective-C interface. If we don't have Objective-C or a '<', this is
566 // just a normal reference to a typedef name.
567 if (!Tok.is(tok::less) || !getLang().ObjC1)
568 continue;
569
570 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000571 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000572 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000573 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000574
575 DS.SetRangeEnd(EndProtoLoc);
576
Steve Narofff7683302008-09-22 10:28:57 +0000577 // Need to support trailing type qualifiers (e.g. "id<p> const").
578 // If a type specifier follows, it will be diagnosed elsewhere.
579 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000580 }
Chris Lattner4b009652007-07-25 00:24:17 +0000581 // GNU attributes support.
582 case tok::kw___attribute:
583 DS.AddAttributes(ParseAttributes());
584 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000585
586 // Microsoft declspec support.
587 case tok::kw___declspec:
588 if (!PP.getLangOptions().Microsoft)
589 goto DoneWithDeclSpec;
590 FuzzyParseMicrosoftDeclSpec();
591 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000592
Steve Naroffedd04d52008-12-25 14:16:32 +0000593 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000594 case tok::kw___forceinline:
595 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000596 case tok::kw___cdecl:
597 case tok::kw___stdcall:
598 case tok::kw___fastcall:
599 if (!PP.getLangOptions().Microsoft)
600 goto DoneWithDeclSpec;
601 // Just ignore it.
602 break;
603
Chris Lattner4b009652007-07-25 00:24:17 +0000604 // storage-class-specifier
605 case tok::kw_typedef:
606 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
607 break;
608 case tok::kw_extern:
609 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000610 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000611 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
612 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000613 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000614 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
615 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000616 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000617 case tok::kw_static:
618 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000619 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000620 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
621 break;
622 case tok::kw_auto:
623 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
624 break;
625 case tok::kw_register:
626 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
627 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000628 case tok::kw_mutable:
629 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
630 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000631 case tok::kw___thread:
632 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
633 break;
634
Chris Lattner4b009652007-07-25 00:24:17 +0000635 continue;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000636
Chris Lattner4b009652007-07-25 00:24:17 +0000637 // function-specifier
638 case tok::kw_inline:
639 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
640 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000641 case tok::kw_virtual:
642 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
643 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000644 case tok::kw_explicit:
645 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
646 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000647
648 // type-specifier
649 case tok::kw_short:
650 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
651 break;
652 case tok::kw_long:
653 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
654 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
655 else
656 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
657 break;
658 case tok::kw_signed:
659 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
660 break;
661 case tok::kw_unsigned:
662 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
663 break;
664 case tok::kw__Complex:
665 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
666 break;
667 case tok::kw__Imaginary:
668 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
669 break;
670 case tok::kw_void:
671 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
672 break;
673 case tok::kw_char:
674 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
675 break;
676 case tok::kw_int:
677 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
678 break;
679 case tok::kw_float:
680 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
681 break;
682 case tok::kw_double:
683 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
684 break;
685 case tok::kw_wchar_t:
686 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
687 break;
688 case tok::kw_bool:
689 case tok::kw__Bool:
690 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
691 break;
692 case tok::kw__Decimal32:
693 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
694 break;
695 case tok::kw__Decimal64:
696 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
697 break;
698 case tok::kw__Decimal128:
699 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
700 break;
701
702 // class-specifier:
703 case tok::kw_class:
704 case tok::kw_struct:
705 case tok::kw_union:
706 ParseClassSpecifier(DS, TemplateParams);
707 continue;
708
709 // enum-specifier:
710 case tok::kw_enum:
711 ParseEnumSpecifier(DS);
712 continue;
713
714 // cv-qualifier:
715 case tok::kw_const:
716 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
717 break;
718 case tok::kw_volatile:
719 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
720 getLang())*2;
721 break;
722 case tok::kw_restrict:
723 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
724 getLang())*2;
725 break;
726
727 // GNU typeof support.
728 case tok::kw_typeof:
729 ParseTypeofSpecifier(DS);
730 continue;
731
Steve Naroff5f0466b2008-06-05 00:02:44 +0000732 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000733 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000734 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
735 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000736 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000737 goto DoneWithDeclSpec;
738
739 {
740 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000741 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000742 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000743 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000744 DS.SetRangeEnd(EndProtoLoc);
745
Chris Lattnerf006a222008-11-18 07:48:38 +0000746 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
747 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000748 // Need to support trailing type qualifiers (e.g. "id<p> const").
749 // If a type specifier follows, it will be diagnosed elsewhere.
750 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000751 }
Chris Lattner4b009652007-07-25 00:24:17 +0000752 }
753 // If the specifier combination wasn't legal, issue a diagnostic.
754 if (isInvalid) {
755 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000756 // Pick between error or extwarn.
757 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
758 : diag::ext_duplicate_declspec;
759 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000760 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000761 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000762 ConsumeToken();
763 }
764}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000765
Chris Lattnerd706dc82009-01-06 06:59:53 +0000766/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000767/// primarily follow the C++ grammar with additions for C99 and GNU,
768/// which together subsume the C grammar. Note that the C++
769/// type-specifier also includes the C type-qualifier (for const,
770/// volatile, and C99 restrict). Returns true if a type-specifier was
771/// found (and parsed), false otherwise.
772///
773/// type-specifier: [C++ 7.1.5]
774/// simple-type-specifier
775/// class-specifier
776/// enum-specifier
777/// elaborated-type-specifier [TODO]
778/// cv-qualifier
779///
780/// cv-qualifier: [C++ 7.1.5.1]
781/// 'const'
782/// 'volatile'
783/// [C99] 'restrict'
784///
785/// simple-type-specifier: [ C++ 7.1.5.2]
786/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
787/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
788/// 'char'
789/// 'wchar_t'
790/// 'bool'
791/// 'short'
792/// 'int'
793/// 'long'
794/// 'signed'
795/// 'unsigned'
796/// 'float'
797/// 'double'
798/// 'void'
799/// [C99] '_Bool'
800/// [C99] '_Complex'
801/// [C99] '_Imaginary' // Removed in TC2?
802/// [GNU] '_Decimal32'
803/// [GNU] '_Decimal64'
804/// [GNU] '_Decimal128'
805/// [GNU] typeof-specifier
806/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
807/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerd706dc82009-01-06 06:59:53 +0000808bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
809 const char *&PrevSpec,
810 TemplateParameterLists *TemplateParams){
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000811 SourceLocation Loc = Tok.getLocation();
812
813 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +0000814 case tok::identifier: // foo::bar
815 // Annotate typenames and C++ scope specifiers. If we get one, just
816 // recurse to handle whatever we get.
817 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000818 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000819 // Otherwise, not a type specifier.
820 return false;
821 case tok::coloncolon: // ::foo::bar
822 if (NextToken().is(tok::kw_new) || // ::new
823 NextToken().is(tok::kw_delete)) // ::delete
824 return false;
825
826 // Annotate typenames and C++ scope specifiers. If we get one, just
827 // recurse to handle whatever we get.
828 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000829 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000830 // Otherwise, not a type specifier.
831 return false;
832
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000833 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +0000834 case tok::annot_typename: {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000835 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000836 Tok.getAnnotationValue());
837 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
838 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000839
840 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
841 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
842 // Objective-C interface. If we don't have Objective-C or a '<', this is
843 // just a normal reference to a typedef name.
844 if (!Tok.is(tok::less) || !getLang().ObjC1)
845 return true;
846
847 SourceLocation EndProtoLoc;
848 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
849 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
850 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
851
852 DS.SetRangeEnd(EndProtoLoc);
853 return true;
854 }
855
856 case tok::kw_short:
857 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
858 break;
859 case tok::kw_long:
860 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
861 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
862 else
863 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
864 break;
865 case tok::kw_signed:
866 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
867 break;
868 case tok::kw_unsigned:
869 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
870 break;
871 case tok::kw__Complex:
872 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
873 break;
874 case tok::kw__Imaginary:
875 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
876 break;
877 case tok::kw_void:
878 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
879 break;
880 case tok::kw_char:
881 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
882 break;
883 case tok::kw_int:
884 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
885 break;
886 case tok::kw_float:
887 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
888 break;
889 case tok::kw_double:
890 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
891 break;
892 case tok::kw_wchar_t:
893 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
894 break;
895 case tok::kw_bool:
896 case tok::kw__Bool:
897 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
898 break;
899 case tok::kw__Decimal32:
900 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
901 break;
902 case tok::kw__Decimal64:
903 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
904 break;
905 case tok::kw__Decimal128:
906 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
907 break;
908
909 // class-specifier:
910 case tok::kw_class:
911 case tok::kw_struct:
912 case tok::kw_union:
Douglas Gregor52473432008-12-24 02:52:09 +0000913 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000914 return true;
915
916 // enum-specifier:
917 case tok::kw_enum:
918 ParseEnumSpecifier(DS);
919 return true;
920
921 // cv-qualifier:
922 case tok::kw_const:
923 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
924 getLang())*2;
925 break;
926 case tok::kw_volatile:
927 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
928 getLang())*2;
929 break;
930 case tok::kw_restrict:
931 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
932 getLang())*2;
933 break;
934
935 // GNU typeof support.
936 case tok::kw_typeof:
937 ParseTypeofSpecifier(DS);
938 return true;
939
Steve Naroffedd04d52008-12-25 14:16:32 +0000940 case tok::kw___cdecl:
941 case tok::kw___stdcall:
942 case tok::kw___fastcall:
Chris Lattner5bb837e2009-01-21 19:19:26 +0000943 if (!PP.getLangOptions().Microsoft) return false;
944 ConsumeToken();
945 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +0000946
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000947 default:
948 // Not a type-specifier; do nothing.
949 return false;
950 }
951
952 // If the specifier combination wasn't legal, issue a diagnostic.
953 if (isInvalid) {
954 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000955 // Pick between error or extwarn.
956 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
957 : diag::ext_duplicate_declspec;
958 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000959 }
960 DS.SetRangeEnd(Tok.getLocation());
961 ConsumeToken(); // whatever we parsed above.
962 return true;
963}
Chris Lattner4b009652007-07-25 00:24:17 +0000964
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000965/// ParseStructDeclaration - Parse a struct declaration without the terminating
966/// semicolon.
967///
Chris Lattner4b009652007-07-25 00:24:17 +0000968/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000969/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000970/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000971/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000972/// struct-declarator-list:
973/// struct-declarator
974/// struct-declarator-list ',' struct-declarator
975/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
976/// struct-declarator:
977/// declarator
978/// [GNU] declarator attributes[opt]
979/// declarator[opt] ':' constant-expression
980/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
981///
Chris Lattner3dd8d392008-04-10 06:46:29 +0000982void Parser::
983ParseStructDeclaration(DeclSpec &DS,
984 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000985 if (Tok.is(tok::kw___extension__)) {
986 // __extension__ silences extension warnings in the subexpression.
987 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +0000988 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000989 return ParseStructDeclaration(DS, Fields);
990 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000991
992 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000993 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +0000994 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +0000995
Douglas Gregorb748fc52009-01-12 22:49:06 +0000996 // If there are no declarators, this is a free-standing declaration
997 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000998 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +0000999 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001000 return;
1001 }
1002
1003 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001004 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001005 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001006 FieldDeclarator &DeclaratorInfo = Fields.back();
1007
Steve Naroffa9adf112007-08-20 22:28:22 +00001008 /// struct-declarator: declarator
1009 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001010 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001011 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001012
Chris Lattner34a01ad2007-10-09 17:33:22 +00001013 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001014 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001015 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001016 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001017 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001018 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001019 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001020 }
1021
1022 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001023 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001024 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +00001025
1026 // If we don't have a comma, it is either the end of the list (a ';')
1027 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001028 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001029 return;
Steve Naroffa9adf112007-08-20 22:28:22 +00001030
1031 // Consume the comma.
1032 ConsumeToken();
1033
1034 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001035 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001036
1037 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001038 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001039 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +00001040 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001041}
1042
1043/// ParseStructUnionBody
1044/// struct-contents:
1045/// struct-declaration-list
1046/// [EXT] empty
1047/// [GNU] "struct-declaration-list" without terminatoring ';'
1048/// struct-declaration-list:
1049/// struct-declaration
1050/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001051/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001052///
Chris Lattner4b009652007-07-25 00:24:17 +00001053void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
1054 unsigned TagType, DeclTy *TagDecl) {
1055 SourceLocation LBraceLoc = ConsumeBrace();
1056
Douglas Gregorcab994d2009-01-09 22:42:13 +00001057 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001058 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1059
Chris Lattner4b009652007-07-25 00:24:17 +00001060 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1061 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001062 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001063 Diag(Tok, diag::ext_empty_struct_union_enum)
1064 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001065
1066 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001067 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1068
Chris Lattner4b009652007-07-25 00:24:17 +00001069 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001070 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001071 // Each iteration of this loop reads one struct-declaration.
1072
1073 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001074 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001075 Diag(Tok, diag::ext_extra_struct_semi);
1076 ConsumeToken();
1077 continue;
1078 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001079
1080 // Parse all the comma separated declarators.
1081 DeclSpec DS;
1082 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001083 if (!Tok.is(tok::at)) {
1084 ParseStructDeclaration(DS, FieldDeclarators);
1085
1086 // Convert them all to fields.
1087 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1088 FieldDeclarator &FD = FieldDeclarators[i];
1089 // Install the declarator into the current TagDecl.
Douglas Gregor8acb7272008-12-11 16:49:14 +00001090 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
Chris Lattner1bf58f62008-06-21 19:39:06 +00001091 DS.getSourceRange().getBegin(),
1092 FD.D, FD.BitfieldSize);
1093 FieldDecls.push_back(Field);
1094 }
1095 } else { // Handle @defs
1096 ConsumeToken();
1097 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1098 Diag(Tok, diag::err_unexpected_at);
1099 SkipUntil(tok::semi, true, true);
1100 continue;
1101 }
1102 ConsumeToken();
1103 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1104 if (!Tok.is(tok::identifier)) {
1105 Diag(Tok, diag::err_expected_ident);
1106 SkipUntil(tok::semi, true, true);
1107 continue;
1108 }
1109 llvm::SmallVector<DeclTy*, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001110 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1111 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001112 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1113 ConsumeToken();
1114 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1115 }
Chris Lattner4b009652007-07-25 00:24:17 +00001116
Chris Lattner34a01ad2007-10-09 17:33:22 +00001117 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001118 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001119 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001120 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001121 break;
1122 } else {
1123 Diag(Tok, diag::err_expected_semi_decl_list);
1124 // Skip to end of block or statement
1125 SkipUntil(tok::r_brace, true, true);
1126 }
1127 }
1128
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001129 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001130
Chris Lattner4b009652007-07-25 00:24:17 +00001131 AttributeList *AttrList = 0;
1132 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001133 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001134 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001135
1136 Actions.ActOnFields(CurScope,
1137 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1138 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001139 AttrList);
1140 StructScope.Exit();
1141 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001142}
1143
1144
1145/// ParseEnumSpecifier
1146/// enum-specifier: [C99 6.7.2.2]
1147/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001148///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001149/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1150/// '}' attributes[opt]
1151/// 'enum' identifier
1152/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001153///
1154/// [C++] elaborated-type-specifier:
1155/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1156///
Chris Lattner4b009652007-07-25 00:24:17 +00001157void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001158 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +00001159 SourceLocation StartLoc = ConsumeToken();
1160
1161 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001162
1163 AttributeList *Attr = 0;
1164 // If attributes exist after tag, parse them.
1165 if (Tok.is(tok::kw___attribute))
1166 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001167
1168 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001169 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001170 if (Tok.isNot(tok::identifier)) {
1171 Diag(Tok, diag::err_expected_ident);
1172 if (Tok.isNot(tok::l_brace)) {
1173 // Has no name and is not a definition.
1174 // Skip the rest of this declarator, up until the comma or semicolon.
1175 SkipUntil(tok::comma, true);
1176 return;
1177 }
1178 }
1179 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001180
1181 // Must have either 'enum name' or 'enum {...}'.
1182 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1183 Diag(Tok, diag::err_expected_ident_lbrace);
1184
1185 // Skip the rest of this declarator, up until the comma or semicolon.
1186 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001187 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001188 }
1189
1190 // If an identifier is present, consume and remember it.
1191 IdentifierInfo *Name = 0;
1192 SourceLocation NameLoc;
1193 if (Tok.is(tok::identifier)) {
1194 Name = Tok.getIdentifierInfo();
1195 NameLoc = ConsumeToken();
1196 }
1197
1198 // There are three options here. If we have 'enum foo;', then this is a
1199 // forward declaration. If we have 'enum foo {...' then this is a
1200 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1201 //
1202 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1203 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1204 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1205 //
1206 Action::TagKind TK;
1207 if (Tok.is(tok::l_brace))
1208 TK = Action::TK_Definition;
1209 else if (Tok.is(tok::semi))
1210 TK = Action::TK_Declaration;
1211 else
1212 TK = Action::TK_Reference;
1213 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Douglas Gregor52473432008-12-24 02:52:09 +00001214 SS, Name, NameLoc, Attr,
1215 Action::MultiTemplateParamsArg(Actions));
Chris Lattner4b009652007-07-25 00:24:17 +00001216
Chris Lattner34a01ad2007-10-09 17:33:22 +00001217 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001218 ParseEnumBody(StartLoc, TagDecl);
1219
1220 // TODO: semantic analysis on the declspec for enums.
1221 const char *PrevSpec = 0;
1222 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerf006a222008-11-18 07:48:38 +00001223 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001224}
1225
1226/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1227/// enumerator-list:
1228/// enumerator
1229/// enumerator-list ',' enumerator
1230/// enumerator:
1231/// enumeration-constant
1232/// enumeration-constant '=' constant-expression
1233/// enumeration-constant:
1234/// identifier
1235///
1236void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001237 // Enter the scope of the enum body and start the definition.
1238 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001239 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001240
Chris Lattner4b009652007-07-25 00:24:17 +00001241 SourceLocation LBraceLoc = ConsumeBrace();
1242
Chris Lattnerc9a92452007-08-27 17:24:30 +00001243 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001244 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001245 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001246
1247 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1248
1249 DeclTy *LastEnumConstDecl = 0;
1250
1251 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001252 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001253 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1254 SourceLocation IdentLoc = ConsumeToken();
1255
1256 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001257 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001258 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001259 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001260 AssignedVal = ParseConstantExpression();
1261 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001262 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001263 }
1264
1265 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +00001266 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +00001267 LastEnumConstDecl,
1268 IdentLoc, Ident,
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001269 EqualLoc,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001270 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001271 EnumConstantDecls.push_back(EnumConstDecl);
1272 LastEnumConstDecl = EnumConstDecl;
1273
Chris Lattner34a01ad2007-10-09 17:33:22 +00001274 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001275 break;
1276 SourceLocation CommaLoc = ConsumeToken();
1277
Chris Lattner34a01ad2007-10-09 17:33:22 +00001278 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +00001279 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1280 }
1281
1282 // Eat the }.
1283 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1284
Steve Naroff0acc9c92007-09-15 18:49:24 +00001285 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001286 EnumConstantDecls.size());
1287
1288 DeclTy *AttrList = 0;
1289 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001290 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001291 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001292
1293 EnumScope.Exit();
1294 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001295}
1296
1297/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001298/// start of a type-qualifier-list.
1299bool Parser::isTypeQualifier() const {
1300 switch (Tok.getKind()) {
1301 default: return false;
1302 // type-qualifier
1303 case tok::kw_const:
1304 case tok::kw_volatile:
1305 case tok::kw_restrict:
1306 return true;
1307 }
1308}
1309
1310/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001311/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001312bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001313 switch (Tok.getKind()) {
1314 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001315
1316 case tok::identifier: // foo::bar
1317 // Annotate typenames and C++ scope specifiers. If we get one, just
1318 // recurse to handle whatever we get.
1319 if (TryAnnotateTypeOrScopeToken())
1320 return isTypeSpecifierQualifier();
1321 // Otherwise, not a type specifier.
1322 return false;
1323 case tok::coloncolon: // ::foo::bar
1324 if (NextToken().is(tok::kw_new) || // ::new
1325 NextToken().is(tok::kw_delete)) // ::delete
1326 return false;
1327
1328 // Annotate typenames and C++ scope specifiers. If we get one, just
1329 // recurse to handle whatever we get.
1330 if (TryAnnotateTypeOrScopeToken())
1331 return isTypeSpecifierQualifier();
1332 // Otherwise, not a type specifier.
1333 return false;
1334
Chris Lattner4b009652007-07-25 00:24:17 +00001335 // GNU attributes support.
1336 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001337 // GNU typeof support.
1338 case tok::kw_typeof:
1339
Chris Lattner4b009652007-07-25 00:24:17 +00001340 // type-specifiers
1341 case tok::kw_short:
1342 case tok::kw_long:
1343 case tok::kw_signed:
1344 case tok::kw_unsigned:
1345 case tok::kw__Complex:
1346 case tok::kw__Imaginary:
1347 case tok::kw_void:
1348 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001349 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001350 case tok::kw_int:
1351 case tok::kw_float:
1352 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001353 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001354 case tok::kw__Bool:
1355 case tok::kw__Decimal32:
1356 case tok::kw__Decimal64:
1357 case tok::kw__Decimal128:
1358
Chris Lattner2e78db32008-04-13 18:59:07 +00001359 // struct-or-union-specifier (C99) or class-specifier (C++)
1360 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001361 case tok::kw_struct:
1362 case tok::kw_union:
1363 // enum-specifier
1364 case tok::kw_enum:
1365
1366 // type-qualifier
1367 case tok::kw_const:
1368 case tok::kw_volatile:
1369 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001370
1371 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001372 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001373 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001374
1375 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1376 case tok::less:
1377 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001378
1379 case tok::kw___cdecl:
1380 case tok::kw___stdcall:
1381 case tok::kw___fastcall:
1382 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001383 }
1384}
1385
1386/// isDeclarationSpecifier() - Return true if the current token is part of a
1387/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001388bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001389 switch (Tok.getKind()) {
1390 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001391
1392 case tok::identifier: // foo::bar
1393 // Annotate typenames and C++ scope specifiers. If we get one, just
1394 // recurse to handle whatever we get.
1395 if (TryAnnotateTypeOrScopeToken())
1396 return isDeclarationSpecifier();
1397 // Otherwise, not a declaration specifier.
1398 return false;
1399 case tok::coloncolon: // ::foo::bar
1400 if (NextToken().is(tok::kw_new) || // ::new
1401 NextToken().is(tok::kw_delete)) // ::delete
1402 return false;
1403
1404 // Annotate typenames and C++ scope specifiers. If we get one, just
1405 // recurse to handle whatever we get.
1406 if (TryAnnotateTypeOrScopeToken())
1407 return isDeclarationSpecifier();
1408 // Otherwise, not a declaration specifier.
1409 return false;
1410
Chris Lattner4b009652007-07-25 00:24:17 +00001411 // storage-class-specifier
1412 case tok::kw_typedef:
1413 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001414 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001415 case tok::kw_static:
1416 case tok::kw_auto:
1417 case tok::kw_register:
1418 case tok::kw___thread:
1419
1420 // type-specifiers
1421 case tok::kw_short:
1422 case tok::kw_long:
1423 case tok::kw_signed:
1424 case tok::kw_unsigned:
1425 case tok::kw__Complex:
1426 case tok::kw__Imaginary:
1427 case tok::kw_void:
1428 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001429 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001430 case tok::kw_int:
1431 case tok::kw_float:
1432 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001433 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001434 case tok::kw__Bool:
1435 case tok::kw__Decimal32:
1436 case tok::kw__Decimal64:
1437 case tok::kw__Decimal128:
1438
Chris Lattner2e78db32008-04-13 18:59:07 +00001439 // struct-or-union-specifier (C99) or class-specifier (C++)
1440 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001441 case tok::kw_struct:
1442 case tok::kw_union:
1443 // enum-specifier
1444 case tok::kw_enum:
1445
1446 // type-qualifier
1447 case tok::kw_const:
1448 case tok::kw_volatile:
1449 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001450
Chris Lattner4b009652007-07-25 00:24:17 +00001451 // function-specifier
1452 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001453 case tok::kw_virtual:
1454 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001455
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001456 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001457 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001458
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001459 // GNU typeof support.
1460 case tok::kw_typeof:
1461
1462 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001463 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001464 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001465
1466 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1467 case tok::less:
1468 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001469
Steve Naroffab1a3632009-01-06 19:34:12 +00001470 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001471 case tok::kw___cdecl:
1472 case tok::kw___stdcall:
1473 case tok::kw___fastcall:
1474 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001475 }
1476}
1477
1478
1479/// ParseTypeQualifierListOpt
1480/// type-qualifier-list: [C99 6.7.5]
1481/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001482/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001483/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001484/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001485///
Chris Lattner460696f2008-12-18 07:02:59 +00001486void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001487 while (1) {
1488 int isInvalid = false;
1489 const char *PrevSpec = 0;
1490 SourceLocation Loc = Tok.getLocation();
1491
1492 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001493 case tok::kw_const:
1494 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1495 getLang())*2;
1496 break;
1497 case tok::kw_volatile:
1498 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1499 getLang())*2;
1500 break;
1501 case tok::kw_restrict:
1502 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1503 getLang())*2;
1504 break;
Steve Naroffad620402008-12-25 14:41:26 +00001505 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001506 case tok::kw___cdecl:
1507 case tok::kw___stdcall:
1508 case tok::kw___fastcall:
1509 if (!PP.getLangOptions().Microsoft)
1510 goto DoneWithTypeQuals;
1511 // Just ignore it.
1512 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001513 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001514 if (AttributesAllowed) {
1515 DS.AddAttributes(ParseAttributes());
1516 continue; // do *not* consume the next token!
1517 }
1518 // otherwise, FALL THROUGH!
1519 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001520 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001521 // If this is not a type-qualifier token, we're done reading type
1522 // qualifiers. First verify that DeclSpec's are consistent.
1523 DS.Finish(Diags, PP.getSourceManager(), getLang());
1524 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001525 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001526
Chris Lattner4b009652007-07-25 00:24:17 +00001527 // If the specifier combination wasn't legal, issue a diagnostic.
1528 if (isInvalid) {
1529 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001530 // Pick between error or extwarn.
1531 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1532 : diag::ext_duplicate_declspec;
1533 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001534 }
1535 ConsumeToken();
1536 }
1537}
1538
1539
1540/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1541///
1542void Parser::ParseDeclarator(Declarator &D) {
1543 /// This implements the 'declarator' production in the C grammar, then checks
1544 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001545 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001546}
1547
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001548/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1549/// is parsed by the function passed to it. Pass null, and the direct-declarator
1550/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001551/// ptr-operator production.
1552///
Sebastian Redl75555032009-01-24 21:16:55 +00001553/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1554/// [C] pointer[opt] direct-declarator
1555/// [C++] direct-declarator
1556/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001557///
1558/// pointer: [C99 6.7.5]
1559/// '*' type-qualifier-list[opt]
1560/// '*' type-qualifier-list[opt] pointer
1561///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001562/// ptr-operator:
1563/// '*' cv-qualifier-seq[opt]
1564/// '&'
1565/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00001566/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001567void Parser::ParseDeclaratorInternal(Declarator &D,
1568 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001569
Sebastian Redl75555032009-01-24 21:16:55 +00001570 // C++ member pointers start with a '::' or a nested-name.
1571 // Member pointers get special handling, since there's no place for the
1572 // scope spec in the generic path below.
1573 if ((Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1574 Tok.is(tok::annot_cxxscope)) && getLang().CPlusPlus) {
1575 CXXScopeSpec SS;
1576 if (ParseOptionalCXXScopeSpecifier(SS)) {
1577 if(Tok.isNot(tok::star)) {
1578 // The scope spec really belongs to the direct-declarator.
1579 D.getCXXScopeSpec() = SS;
1580 if (DirectDeclParser)
1581 (this->*DirectDeclParser)(D);
1582 return;
1583 }
1584
1585 SourceLocation Loc = ConsumeToken();
1586 DeclSpec DS;
1587 ParseTypeQualifierListOpt(DS);
1588
1589 // Recurse to parse whatever is left.
1590 ParseDeclaratorInternal(D, DirectDeclParser);
1591
1592 // Sema will have to catch (syntactically invalid) pointers into global
1593 // scope. It has to catch pointers into namespace scope anyway.
1594 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
1595 Loc,DS.TakeAttributes()));
1596 return;
1597 }
1598 }
1599
1600 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00001601 // Not a pointer, C++ reference, or block.
1602 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001603 (Kind != tok::caret || !getLang().Blocks)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001604 if (DirectDeclParser)
1605 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001606 return;
1607 }
Sebastian Redl75555032009-01-24 21:16:55 +00001608
Steve Naroffdc22f212008-08-28 10:07:06 +00001609 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Chris Lattner4b009652007-07-25 00:24:17 +00001610 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1611
Steve Naroffdc22f212008-08-28 10:07:06 +00001612 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner69f01932008-02-21 01:32:26 +00001613 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001614 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00001615
Chris Lattner4b009652007-07-25 00:24:17 +00001616 ParseTypeQualifierListOpt(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00001617
Chris Lattner4b009652007-07-25 00:24:17 +00001618 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001619 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001620 if (Kind == tok::star)
1621 // Remember that we parsed a pointer type, and remember the type-quals.
1622 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1623 DS.TakeAttributes()));
1624 else
1625 // Remember that we parsed a Block type, and remember the type-quals.
1626 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1627 Loc));
Chris Lattner4b009652007-07-25 00:24:17 +00001628 } else {
1629 // Is a reference
1630 DeclSpec DS;
1631
1632 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1633 // cv-qualifiers are introduced through the use of a typedef or of a
1634 // template type argument, in which case the cv-qualifiers are ignored.
1635 //
1636 // [GNU] Retricted references are allowed.
1637 // [GNU] Attributes on references are allowed.
1638 ParseTypeQualifierListOpt(DS);
1639
1640 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1641 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1642 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001643 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001644 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1645 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001646 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001647 }
1648
1649 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001650 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001651
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001652 if (D.getNumTypeObjects() > 0) {
1653 // C++ [dcl.ref]p4: There shall be no references to references.
1654 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1655 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001656 if (const IdentifierInfo *II = D.getIdentifier())
1657 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1658 << II;
1659 else
1660 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1661 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001662
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001663 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001664 // can go ahead and build the (technically ill-formed)
1665 // declarator: reference collapsing will take care of it.
1666 }
1667 }
1668
Chris Lattner4b009652007-07-25 00:24:17 +00001669 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001670 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1671 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001672 }
1673}
1674
1675/// ParseDirectDeclarator
1676/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001677/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001678/// '(' declarator ')'
1679/// [GNU] '(' attributes declarator ')'
1680/// [C90] direct-declarator '[' constant-expression[opt] ']'
1681/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1682/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1683/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1684/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1685/// direct-declarator '(' parameter-type-list ')'
1686/// direct-declarator '(' identifier-list[opt] ')'
1687/// [GNU] direct-declarator '(' parameter-forward-declarations
1688/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001689/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1690/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001691/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001692///
1693/// declarator-id: [C++ 8]
1694/// id-expression
1695/// '::'[opt] nested-name-specifier[opt] type-name
1696///
1697/// id-expression: [C++ 5.1]
1698/// unqualified-id
1699/// qualified-id [TODO]
1700///
1701/// unqualified-id: [C++ 5.1]
1702/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001703/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001704/// conversion-function-id [TODO]
1705/// '~' class-name
1706/// template-id [TODO]
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001707///
Chris Lattner4b009652007-07-25 00:24:17 +00001708void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001709 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001710
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001711 if (getLang().CPlusPlus) {
1712 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00001713 // ParseDeclaratorInternal might already have parsed the scope.
1714 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1715 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001716 if (afterCXXScope) {
1717 // Change the declaration context for name lookup, until this function
1718 // is exited (and the declarator has been parsed).
1719 DeclScopeObj.EnterDeclaratorScope();
1720 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001721
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001722 if (Tok.is(tok::identifier)) {
1723 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor2fa10442008-12-18 19:37:40 +00001724
1725 // If this identifier is followed by a '<', we may have a template-id.
1726 DeclTy *Template;
Douglas Gregor853dd392008-12-26 15:00:45 +00001727 if (NextToken().is(tok::less) &&
Douglas Gregor2fa10442008-12-18 19:37:40 +00001728 (Template = Actions.isTemplateName(*Tok.getIdentifierInfo(),
1729 CurScope))) {
1730 IdentifierInfo *II = Tok.getIdentifierInfo();
1731 AnnotateTemplateIdToken(Template, 0);
1732 // FIXME: Set the declarator to a template-id. How? I don't
1733 // know... for now, just use the identifier.
1734 D.SetIdentifier(II, Tok.getLocation());
1735 }
1736 // If this identifier is the name of the current class, it's a
1737 // constructor name.
Douglas Gregor853dd392008-12-26 15:00:45 +00001738 else if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope))
Steve Naroff7b36a1b2009-01-28 19:39:02 +00001739 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
1740 CurScope),
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001741 Tok.getLocation());
Douglas Gregor2fa10442008-12-18 19:37:40 +00001742 // This is a normal identifier.
1743 else
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001744 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1745 ConsumeToken();
1746 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00001747 } else if (Tok.is(tok::kw_operator)) {
1748 SourceLocation OperatorLoc = Tok.getLocation();
Douglas Gregore60e5d32008-11-06 22:13:31 +00001749
Douglas Gregor853dd392008-12-26 15:00:45 +00001750 // First try the name of an overloaded operator
1751 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId()) {
1752 D.setOverloadedOperator(Op, OperatorLoc);
1753 } else {
1754 // This must be a conversion function (C++ [class.conv.fct]).
1755 if (TypeTy *ConvType = ParseConversionFunctionId())
1756 D.setConversionFunction(ConvType, OperatorLoc);
1757 else
1758 D.SetIdentifier(0, Tok.getLocation());
1759 }
1760 goto PastIdentifier;
1761 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001762 // This should be a C++ destructor.
1763 SourceLocation TildeLoc = ConsumeToken();
1764 if (Tok.is(tok::identifier)) {
1765 if (TypeTy *Type = ParseClassName())
1766 D.setDestructor(Type, TildeLoc);
1767 else
1768 D.SetIdentifier(0, TildeLoc);
1769 } else {
1770 Diag(Tok, diag::err_expected_class_name);
1771 D.SetIdentifier(0, TildeLoc);
1772 }
1773 goto PastIdentifier;
1774 }
1775
1776 // If we reached this point, token is not identifier and not '~'.
1777
1778 if (afterCXXScope) {
1779 Diag(Tok, diag::err_expected_unqualified_id);
1780 D.SetIdentifier(0, Tok.getLocation());
1781 D.setInvalidType(true);
1782 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001783 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001784 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001785 }
1786
1787 // If we reached this point, we are either in C/ObjC or the token didn't
1788 // satisfy any of the C++-specific checks.
1789
1790 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1791 assert(!getLang().CPlusPlus &&
1792 "There's a C++-specific check for tok::identifier above");
1793 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1794 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1795 ConsumeToken();
1796 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001797 // direct-declarator: '(' declarator ')'
1798 // direct-declarator: '(' attributes declarator ')'
1799 // Example: 'char (*X)' or 'int (*XX)(void)'
1800 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001801 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001802 // This could be something simple like "int" (in which case the declarator
1803 // portion is empty), if an abstract-declarator is allowed.
1804 D.SetIdentifier(0, Tok.getLocation());
1805 } else {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001806 if (getLang().CPlusPlus)
1807 Diag(Tok, diag::err_expected_unqualified_id);
1808 else
Chris Lattnerf006a222008-11-18 07:48:38 +00001809 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00001810 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00001811 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001812 }
1813
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001814 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00001815 assert(D.isPastIdentifier() &&
1816 "Haven't past the location of the identifier yet?");
1817
1818 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001819 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001820 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1821 // In such a case, check if we actually have a function declarator; if it
1822 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001823 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1824 // When not in file scope, warn for ambiguous function declarators, just
1825 // in case the author intended it as a variable definition.
1826 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1827 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1828 break;
1829 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001830 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001831 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001832 ParseBracketDeclarator(D);
1833 } else {
1834 break;
1835 }
1836 }
1837}
1838
Chris Lattnera0d056d2008-04-06 05:45:57 +00001839/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1840/// only called before the identifier, so these are most likely just grouping
1841/// parens for precedence. If we find that these are actually function
1842/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1843///
1844/// direct-declarator:
1845/// '(' declarator ')'
1846/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001847/// direct-declarator '(' parameter-type-list ')'
1848/// direct-declarator '(' identifier-list[opt] ')'
1849/// [GNU] direct-declarator '(' parameter-forward-declarations
1850/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001851///
1852void Parser::ParseParenDeclarator(Declarator &D) {
1853 SourceLocation StartLoc = ConsumeParen();
1854 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1855
Chris Lattner1f185292008-10-20 02:05:46 +00001856 // Eat any attributes before we look at whether this is a grouping or function
1857 // declarator paren. If this is a grouping paren, the attribute applies to
1858 // the type being built up, for example:
1859 // int (__attribute__(()) *x)(long y)
1860 // If this ends up not being a grouping paren, the attribute applies to the
1861 // first argument, for example:
1862 // int (__attribute__(()) int x)
1863 // In either case, we need to eat any attributes to be able to determine what
1864 // sort of paren this is.
1865 //
1866 AttributeList *AttrList = 0;
1867 bool RequiresArg = false;
1868 if (Tok.is(tok::kw___attribute)) {
1869 AttrList = ParseAttributes();
1870
1871 // We require that the argument list (if this is a non-grouping paren) be
1872 // present even if the attribute list was empty.
1873 RequiresArg = true;
1874 }
Steve Naroffedd04d52008-12-25 14:16:32 +00001875 // Eat any Microsoft extensions.
Douglas Gregore51b7c82009-01-10 00:48:18 +00001876 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
1877 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroffedd04d52008-12-25 14:16:32 +00001878 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00001879
Chris Lattnera0d056d2008-04-06 05:45:57 +00001880 // If we haven't past the identifier yet (or where the identifier would be
1881 // stored, if this is an abstract declarator), then this is probably just
1882 // grouping parens. However, if this could be an abstract-declarator, then
1883 // this could also be the start of function arguments (consider 'void()').
1884 bool isGrouping;
1885
1886 if (!D.mayOmitIdentifier()) {
1887 // If this can't be an abstract-declarator, this *must* be a grouping
1888 // paren, because we haven't seen the identifier yet.
1889 isGrouping = true;
1890 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001891 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00001892 isDeclarationSpecifier()) { // 'int(int)' is a function.
1893 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1894 // considered to be a type, not a K&R identifier-list.
1895 isGrouping = false;
1896 } else {
1897 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1898 isGrouping = true;
1899 }
1900
1901 // If this is a grouping paren, handle:
1902 // direct-declarator: '(' declarator ')'
1903 // direct-declarator: '(' attributes declarator ')'
1904 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001905 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001906 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00001907 if (AttrList)
1908 D.AddAttributes(AttrList);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001909
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001910 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001911 // Match the ')'.
1912 MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001913
1914 D.setGroupingParens(hadGroupingParens);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001915 return;
1916 }
1917
1918 // Okay, if this wasn't a grouping paren, it must be the start of a function
1919 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00001920 // identifier (and remember where it would have been), then call into
1921 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00001922 D.SetIdentifier(0, Tok.getLocation());
1923
Chris Lattner1f185292008-10-20 02:05:46 +00001924 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001925}
1926
1927/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1928/// declarator D up to a paren, which indicates that we are parsing function
1929/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001930///
Chris Lattner1f185292008-10-20 02:05:46 +00001931/// If AttrList is non-null, then the caller parsed those arguments immediately
1932/// after the open paren - they should be considered to be the first argument of
1933/// a parameter. If RequiresArg is true, then the first argument of the
1934/// function is required to be present and required to not be an identifier
1935/// list.
1936///
Chris Lattner4b009652007-07-25 00:24:17 +00001937/// This method also handles this portion of the grammar:
1938/// parameter-type-list: [C99 6.7.5]
1939/// parameter-list
1940/// parameter-list ',' '...'
1941///
1942/// parameter-list: [C99 6.7.5]
1943/// parameter-declaration
1944/// parameter-list ',' parameter-declaration
1945///
1946/// parameter-declaration: [C99 6.7.5]
1947/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00001948/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001949/// [GNU] declaration-specifiers declarator attributes
1950/// declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00001951/// [C++] declaration-specifiers abstract-declarator[opt]
1952/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001953/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1954///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001955/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
1956/// and "exception-specification[opt]"(TODO).
1957///
Chris Lattner1f185292008-10-20 02:05:46 +00001958void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
1959 AttributeList *AttrList,
1960 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00001961 // lparen is already consumed!
1962 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00001963
Chris Lattner1f185292008-10-20 02:05:46 +00001964 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001965 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00001966 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001967 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00001968 delete AttrList;
1969 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001970
1971 ConsumeParen(); // Eat the closing ')'.
1972
1973 // cv-qualifier-seq[opt].
1974 DeclSpec DS;
1975 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00001976 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor90a2c972008-11-25 03:22:00 +00001977
1978 // Parse exception-specification[opt].
1979 if (Tok.is(tok::kw_throw))
1980 ParseExceptionSpecification();
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001981 }
1982
Chris Lattner9f7564b2008-04-06 06:57:35 +00001983 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00001984 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001985 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00001986 /*variadic*/ false,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001987 /*arglist*/ 0, 0,
1988 DS.getTypeQualifiers(),
Chris Lattnerdefaf412009-01-20 19:11:22 +00001989 LParenLoc, D));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001990 return;
Chris Lattner1f185292008-10-20 02:05:46 +00001991 }
1992
1993 // Alternatively, this parameter list may be an identifier list form for a
1994 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00001995 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
1996
Steve Naroff7b36a1b2009-01-28 19:39:02 +00001997 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(), CurScope);
Steve Naroff3f3f3b42009-01-28 19:16:40 +00001998 if (TypeRep) {
1999 // This is a typename. Replace the current token in-place with an
2000 // annotation type token.
2001 Tok.setKind(tok::annot_typename);
2002 Tok.setAnnotationValue(TypeRep);
2003 Tok.setAnnotationEndLoc(Tok.getLocation());
2004 // In case the tokens were cached, have Preprocessor replace
2005 // them with the annotation token.
2006 PP.AnnotateCachedTokens(Tok);
2007 } else {
Chris Lattner1f185292008-10-20 02:05:46 +00002008 // K&R identifier lists can't have typedefs as identifiers, per
2009 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002010 if (RequiresArg) {
2011 Diag(Tok, diag::err_argument_required_after_attribute);
2012 delete AttrList;
2013 }
2014
2015 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2016 // normal declarators, not for abstract-declarators.
2017 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002018 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002019 }
2020
2021 // Finally, a normal, non-empty parameter type list.
2022
2023 // Build up an array of information about the parsed arguments.
2024 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002025
2026 // Enter function-declaration scope, limiting any declarators to the
2027 // function prototype scope, including parameter declarators.
Douglas Gregorcab994d2009-01-09 22:42:13 +00002028 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002029
2030 bool IsVariadic = false;
2031 while (1) {
2032 if (Tok.is(tok::ellipsis)) {
2033 IsVariadic = true;
Chris Lattner4b009652007-07-25 00:24:17 +00002034
Chris Lattner9f7564b2008-04-06 06:57:35 +00002035 // Check to see if this is "void(...)" which is not allowed.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002036 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002037 // Otherwise, parse parameter type list. If it starts with an
2038 // ellipsis, diagnose the malformed function.
2039 Diag(Tok, diag::err_ellipsis_first_arg);
2040 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner4b009652007-07-25 00:24:17 +00002041 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00002042
Chris Lattner9f7564b2008-04-06 06:57:35 +00002043 ConsumeToken(); // Consume the ellipsis.
2044 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002045 }
2046
Chris Lattner9f7564b2008-04-06 06:57:35 +00002047 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002048
Chris Lattner9f7564b2008-04-06 06:57:35 +00002049 // Parse the declaration-specifiers.
2050 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002051
2052 // If the caller parsed attributes for the first argument, add them now.
2053 if (AttrList) {
2054 DS.AddAttributes(AttrList);
2055 AttrList = 0; // Only apply the attributes to the first parameter.
2056 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002057 ParseDeclarationSpecifiers(DS);
2058
2059 // Parse the declarator. This is "PrototypeContext", because we must
2060 // accept either 'declarator' or 'abstract-declarator' here.
2061 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2062 ParseDeclarator(ParmDecl);
2063
2064 // Parse GNU attributes, if present.
2065 if (Tok.is(tok::kw___attribute))
2066 ParmDecl.AddAttributes(ParseAttributes());
2067
Chris Lattner9f7564b2008-04-06 06:57:35 +00002068 // Remember this parsed parameter in ParamInfo.
2069 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2070
Douglas Gregor605de8d2008-12-16 21:30:33 +00002071 // DefArgToks is used when the parsing of default arguments needs
2072 // to be delayed.
2073 CachedTokens *DefArgToks = 0;
2074
Chris Lattner9f7564b2008-04-06 06:57:35 +00002075 // If no parameter was specified, verify that *something* was specified,
2076 // otherwise we have a missing type and identifier.
2077 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
2078 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
2079 // Completely missing, emit error.
2080 Diag(DSStart, diag::err_missing_param);
2081 } else {
2082 // Otherwise, we have something. Add it and let semantic analysis try
2083 // to grok it and add the result to the ParamInfo we are building.
2084
2085 // Inform the actions module about the parameter declarator, so it gets
2086 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00002087 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
2088
2089 // Parse the default argument, if any. We parse the default
2090 // arguments in all dialects; the semantic analysis in
2091 // ActOnParamDefaultArgument will reject the default argument in
2092 // C.
2093 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002094 SourceLocation EqualLoc = Tok.getLocation();
2095
Chris Lattner3e254fb2008-04-08 04:40:51 +00002096 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002097 if (D.getContext() == Declarator::MemberContext) {
2098 // If we're inside a class definition, cache the tokens
2099 // corresponding to the default argument. We'll actually parse
2100 // them when we see the end of the class definition.
2101 // FIXME: Templates will require something similar.
2102 // FIXME: Can we use a smart pointer for Toks?
2103 DefArgToks = new CachedTokens;
2104
2105 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2106 tok::semi, false)) {
2107 delete DefArgToks;
2108 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002109 Actions.ActOnParamDefaultArgumentError(Param);
2110 } else
2111 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002112 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002113 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002114 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002115
2116 OwningExprResult DefArgResult(ParseAssignmentExpression());
2117 if (DefArgResult.isInvalid()) {
2118 Actions.ActOnParamDefaultArgumentError(Param);
2119 SkipUntil(tok::comma, tok::r_paren, true, true);
2120 } else {
2121 // Inform the actions module about the default argument
2122 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
2123 DefArgResult.release());
2124 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002125 }
2126 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002127
2128 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002129 ParmDecl.getIdentifierLoc(), Param,
2130 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002131 }
2132
2133 // If the next token is a comma, consume it and keep reading arguments.
2134 if (Tok.isNot(tok::comma)) break;
2135
2136 // Consume the comma.
2137 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002138 }
2139
Chris Lattner9f7564b2008-04-06 06:57:35 +00002140 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002141 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002142
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002143 // If we have the closing ')', eat it.
2144 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2145
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002146 DeclSpec DS;
2147 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002148 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002149 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor90a2c972008-11-25 03:22:00 +00002150
2151 // Parse exception-specification[opt].
2152 if (Tok.is(tok::kw_throw))
2153 ParseExceptionSpecification();
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002154 }
2155
Chris Lattner4b009652007-07-25 00:24:17 +00002156 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002157 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
2158 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002159 DS.getTypeQualifiers(),
Chris Lattnerdefaf412009-01-20 19:11:22 +00002160 LParenLoc, D));
Chris Lattner4b009652007-07-25 00:24:17 +00002161}
2162
Chris Lattner35d9c912008-04-06 06:34:08 +00002163/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2164/// we found a K&R-style identifier list instead of a type argument list. The
2165/// current token is known to be the first identifier in the list.
2166///
2167/// identifier-list: [C99 6.7.5]
2168/// identifier
2169/// identifier-list ',' identifier
2170///
2171void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2172 Declarator &D) {
2173 // Build up an array of information about the parsed arguments.
2174 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2175 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2176
2177 // If there was no identifier specified for the declarator, either we are in
2178 // an abstract-declarator, or we are in a parameter declarator which was found
2179 // to be abstract. In abstract-declarators, identifier lists are not valid:
2180 // diagnose this.
2181 if (!D.getIdentifier())
2182 Diag(Tok, diag::ext_ident_list_in_param);
2183
2184 // Tok is known to be the first identifier in the list. Remember this
2185 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002186 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002187 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
2188 Tok.getLocation(), 0));
2189
Chris Lattner113a56b2008-04-06 06:39:19 +00002190 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002191
2192 while (Tok.is(tok::comma)) {
2193 // Eat the comma.
2194 ConsumeToken();
2195
Chris Lattner113a56b2008-04-06 06:39:19 +00002196 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002197 if (Tok.isNot(tok::identifier)) {
2198 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002199 SkipUntil(tok::r_paren);
2200 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002201 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002202
Chris Lattner35d9c912008-04-06 06:34:08 +00002203 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002204
2205 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Steve Naroff7b36a1b2009-01-28 19:39:02 +00002206 if (Actions.getTypeName(*ParmII, CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002207 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002208
2209 // Verify that the argument identifier has not already been mentioned.
2210 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002211 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002212 } else {
2213 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002214 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2215 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00002216 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002217
2218 // Eat the identifier.
2219 ConsumeToken();
2220 }
2221
Chris Lattner113a56b2008-04-06 06:39:19 +00002222 // Remember that we parsed a function type, and remember the attributes. This
2223 // function type is always a K&R style function type, which is not varargs and
2224 // has no prototype.
2225 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
2226 &ParamInfo[0], ParamInfo.size(),
Chris Lattnerdefaf412009-01-20 19:11:22 +00002227 /*TypeQuals*/0, LParenLoc, D));
Chris Lattner35d9c912008-04-06 06:34:08 +00002228
2229 // If we have the closing ')', eat it and we're done.
Chris Lattner113a56b2008-04-06 06:39:19 +00002230 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002231}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002232
Chris Lattner4b009652007-07-25 00:24:17 +00002233/// [C90] direct-declarator '[' constant-expression[opt] ']'
2234/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2235/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2236/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2237/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2238void Parser::ParseBracketDeclarator(Declarator &D) {
2239 SourceLocation StartLoc = ConsumeBracket();
2240
Chris Lattner1525c3a2008-12-18 07:27:21 +00002241 // C array syntax has many features, but by-far the most common is [] and [4].
2242 // This code does a fast path to handle some of the most obvious cases.
2243 if (Tok.getKind() == tok::r_square) {
2244 MatchRHSPunctuation(tok::r_square, StartLoc);
2245 // Remember that we parsed the empty array type.
2246 OwningExprResult NumElements(Actions);
2247 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc));
2248 return;
2249 } else if (Tok.getKind() == tok::numeric_constant &&
2250 GetLookAheadToken(1).is(tok::r_square)) {
2251 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002252 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002253 ConsumeToken();
2254
2255 MatchRHSPunctuation(tok::r_square, StartLoc);
2256
2257 // If there was an error parsing the assignment-expression, recover.
2258 if (ExprRes.isInvalid())
2259 ExprRes.release(); // Deallocate expr, just use [].
2260
2261 // Remember that we parsed a array type, and remember its features.
2262 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
2263 ExprRes.release(), StartLoc));
2264 return;
2265 }
2266
Chris Lattner4b009652007-07-25 00:24:17 +00002267 // If valid, this location is the position where we read the 'static' keyword.
2268 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002269 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002270 StaticLoc = ConsumeToken();
2271
2272 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002273 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002274 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002275 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002276
2277 // If we haven't already read 'static', check to see if there is one after the
2278 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002279 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002280 StaticLoc = ConsumeToken();
2281
2282 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2283 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002284 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002285
2286 // Handle the case where we have '[*]' as the array size. However, a leading
2287 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2288 // the the token after the star is a ']'. Since stars in arrays are
2289 // infrequent, use of lookahead is not costly here.
2290 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002291 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002292
Chris Lattner306d4df2008-12-18 06:50:14 +00002293 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002294 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002295 StaticLoc = SourceLocation(); // Drop the static.
2296 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002297 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002298 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002299 // Note, in C89, this production uses the constant-expr production instead
2300 // of assignment-expr. The only difference is that assignment-expr allows
2301 // things like '=' and '*='. Sema rejects these in C89 mode because they
2302 // are not i-c-e's, so we don't need to distinguish between the two here.
2303
Chris Lattner4b009652007-07-25 00:24:17 +00002304 // Parse the assignment-expression now.
2305 NumElements = ParseAssignmentExpression();
2306 }
2307
2308 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002309 if (NumElements.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002310 // If the expression was invalid, skip it.
2311 SkipUntil(tok::r_square);
2312 return;
2313 }
2314
2315 MatchRHSPunctuation(tok::r_square, StartLoc);
2316
Chris Lattner1525c3a2008-12-18 07:27:21 +00002317 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002318 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2319 StaticLoc.isValid(), isStar,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002320 NumElements.release(), StartLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00002321}
2322
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002323/// [GNU] typeof-specifier:
2324/// typeof ( expressions )
2325/// typeof ( type-name )
2326/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002327///
2328void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002329 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002330 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002331 SourceLocation StartLoc = ConsumeToken();
2332
Chris Lattner34a01ad2007-10-09 17:33:22 +00002333 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002334 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002335 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002336 return;
2337 }
2338
Sebastian Redl14ca7412008-12-11 21:36:32 +00002339 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002340 if (Result.isInvalid())
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002341 return;
2342
2343 const char *PrevSpec = 0;
2344 // Check for duplicate type specifiers.
2345 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002346 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002347 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002348
2349 // FIXME: Not accurate, the range gets one token more than it should.
2350 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002351 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002352 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002353
Steve Naroff7cbb1462007-07-31 12:34:36 +00002354 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2355
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002356 if (isTypeIdInParens()) {
Steve Naroff7cbb1462007-07-31 12:34:36 +00002357 TypeTy *Ty = ParseTypeName();
2358
Steve Naroff4c255ab2007-07-31 23:56:32 +00002359 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
2360
Chris Lattner34a01ad2007-10-09 17:33:22 +00002361 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002362 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002363 return;
2364 }
2365 RParenLoc = ConsumeParen();
2366 const char *PrevSpec = 0;
2367 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2368 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
Chris Lattnerf006a222008-11-18 07:48:38 +00002369 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002370 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002371 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002372
2373 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002374 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002375 return;
2376 }
2377 RParenLoc = ConsumeParen();
2378 const char *PrevSpec = 0;
2379 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2380 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002381 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002382 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002383 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002384 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002385}
2386
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002387