blob: 1f8d4738919b352e09d038114578228aaf88d23e [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner31e05722007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Sebastian Redla55e52c2008-11-25 22:21:31 +000018#include "AstGuard.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl4c5d3202008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Douglas Gregor809070a2009-02-18 17:45:20 +000031Action::TypeResult Parser::ParseTypeName() {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Gregor809070a2009-02-18 17:45:20 +000040 if (DeclaratorInfo.getInvalidType())
41 return true;
42
43 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000044}
45
46/// ParseAttributes - Parse a non-empty attributes list.
47///
48/// [GNU] attributes:
49/// attribute
50/// attributes attribute
51///
52/// [GNU] attribute:
53/// '__attribute__' '(' '(' attribute-list ')' ')'
54///
55/// [GNU] attribute-list:
56/// attrib
57/// attribute_list ',' attrib
58///
59/// [GNU] attrib:
60/// empty
61/// attrib-name
62/// attrib-name '(' identifier ')'
63/// attrib-name '(' identifier ',' nonempty-expr-list ')'
64/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
65///
66/// [GNU] attrib-name:
67/// identifier
68/// typespec
69/// typequal
70/// storageclass
71///
72/// FIXME: The GCC grammar/code for this construct implies we need two
73/// token lookahead. Comment from gcc: "If they start with an identifier
74/// which is followed by a comma or close parenthesis, then the arguments
75/// start with that identifier; otherwise they are an expression list."
76///
77/// At the moment, I am not doing 2 token lookahead. I am also unaware of
78/// any attributes that don't work (based on my limited testing). Most
79/// attributes are very simple in practice. Until we find a bug, I don't see
80/// a pressing need to implement the 2 token lookahead.
81
Sebastian Redlab197ba2009-02-09 18:23:29 +000082AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000083 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Reid Spencer5f016e22007-07-11 17:01:13 +000084
85 AttributeList *CurrAttr = 0;
86
Chris Lattner04d66662007-10-09 17:33:22 +000087 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000088 ConsumeToken();
89 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
90 "attribute")) {
91 SkipUntil(tok::r_paren, true); // skip until ) or ;
92 return CurrAttr;
93 }
94 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
95 SkipUntil(tok::r_paren, true); // skip until ) or ;
96 return CurrAttr;
97 }
98 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +000099 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
100 Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000101
Chris Lattner04d66662007-10-09 17:33:22 +0000102 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
104 ConsumeToken();
105 continue;
106 }
107 // we have an identifier or declaration specifier (const, int, etc.)
108 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
109 SourceLocation AttrNameLoc = ConsumeToken();
110
111 // check if we have a "paramterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000112 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 ConsumeParen(); // ignore the left paren loc for now
114
Chris Lattner04d66662007-10-09 17:33:22 +0000115 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
117 SourceLocation ParmLoc = ConsumeToken();
118
Chris Lattner04d66662007-10-09 17:33:22 +0000119 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 // __attribute__(( mode(byte) ))
121 ConsumeParen(); // ignore the right paren loc for now
122 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
123 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner04d66662007-10-09 17:33:22 +0000124 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 ConsumeToken();
126 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000127 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 bool ArgExprsOk = true;
129
130 // now parse the non-empty comma separated list of expressions
131 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000132 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000133 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 ArgExprsOk = false;
135 SkipUntil(tok::r_paren);
136 break;
137 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000138 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 }
Chris Lattner04d66662007-10-09 17:33:22 +0000140 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 break;
142 ConsumeToken(); // Eat the comma, move to the next argument
143 }
Chris Lattner04d66662007-10-09 17:33:22 +0000144 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 ConsumeParen(); // ignore the right paren loc for now
146 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000147 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 }
149 }
150 } else { // not an identifier
151 // parse a possibly empty comma separated list of expressions
Chris Lattner04d66662007-10-09 17:33:22 +0000152 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 // __attribute__(( nonnull() ))
154 ConsumeParen(); // ignore the right paren loc for now
155 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
156 0, SourceLocation(), 0, 0, CurrAttr);
157 } else {
158 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000159 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 bool ArgExprsOk = true;
161
162 // now parse the list of expressions
163 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000164 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000165 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 ArgExprsOk = false;
167 SkipUntil(tok::r_paren);
168 break;
169 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000170 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 }
Chris Lattner04d66662007-10-09 17:33:22 +0000172 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 break;
174 ConsumeToken(); // Eat the comma, move to the next argument
175 }
176 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000177 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000179 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
180 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 CurrAttr);
182 }
183 }
184 }
185 } else {
186 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
187 0, SourceLocation(), 0, 0, CurrAttr);
188 }
189 }
190 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 SkipUntil(tok::r_paren, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000192 SourceLocation Loc = Tok.getLocation();;
193 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
194 SkipUntil(tok::r_paren, false);
195 }
196 if (EndLoc)
197 *EndLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 }
199 return CurrAttr;
200}
201
Steve Narofff59e17e2008-12-24 20:59:21 +0000202/// FuzzyParseMicrosoftDeclSpec. When -fms-extensions is enabled, this
203/// routine is called to skip/ignore tokens that comprise the MS declspec.
204void Parser::FuzzyParseMicrosoftDeclSpec() {
205 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
206 ConsumeToken();
207 if (Tok.is(tok::l_paren)) {
208 unsigned short savedParenCount = ParenCount;
209 do {
210 ConsumeAnyToken();
211 } while (ParenCount > savedParenCount && Tok.isNot(tok::eof));
212 }
213 return;
214}
215
Reid Spencer5f016e22007-07-11 17:01:13 +0000216/// ParseDeclaration - Parse a full 'declaration', which consists of
217/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000218/// 'Context' should be a Declarator::TheContext value. This returns the
219/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000220///
221/// declaration: [C99 6.7]
222/// block-declaration ->
223/// simple-declaration
224/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000225/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000226/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000227/// [C++] using-directive
228/// [C++] using-declaration [TODO]
Sebastian Redl50de12f2009-03-24 22:27:57 +0000229/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000230/// others... [FIXME]
231///
Chris Lattner97144fc2009-04-02 04:16:50 +0000232Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
233 SourceLocation &DeclEnd) {
Chris Lattner682bf922009-03-29 16:50:03 +0000234 DeclPtrTy SingleDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000235 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000236 case tok::kw_export:
237 case tok::kw_template:
Chris Lattner97144fc2009-04-02 04:16:50 +0000238 SingleDecl = ParseTemplateDeclarationOrSpecialization(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000239 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000240 case tok::kw_namespace:
Chris Lattner97144fc2009-04-02 04:16:50 +0000241 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000242 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000243 case tok::kw_using:
Chris Lattner97144fc2009-04-02 04:16:50 +0000244 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000245 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000246 case tok::kw_static_assert:
Chris Lattner97144fc2009-04-02 04:16:50 +0000247 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000248 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000249 default:
Chris Lattner97144fc2009-04-02 04:16:50 +0000250 return ParseSimpleDeclaration(Context, DeclEnd);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000251 }
Chris Lattner682bf922009-03-29 16:50:03 +0000252
253 // This routine returns a DeclGroup, if the thing we parsed only contains a
254 // single decl, convert it now.
255 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000256}
257
258/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
259/// declaration-specifiers init-declarator-list[opt] ';'
260///[C90/C++]init-declarator-list ';' [TODO]
261/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000262///
263/// If RequireSemi is false, this does not check for a ';' at the end of the
264/// declaration.
265Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000266 SourceLocation &DeclEnd,
Chris Lattnercd147752009-03-29 17:27:48 +0000267 bool RequireSemi) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000268 // Parse the common declaration-specifiers piece.
269 DeclSpec DS;
270 ParseDeclarationSpecifiers(DS);
271
272 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
273 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000274 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000275 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000276 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
277 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 }
279
280 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
281 ParseDeclarator(DeclaratorInfo);
282
Chris Lattner23c4b182009-03-29 17:18:04 +0000283 DeclGroupPtrTy DG =
284 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattnercd147752009-03-29 17:27:48 +0000285
Chris Lattner97144fc2009-04-02 04:16:50 +0000286 DeclEnd = Tok.getLocation();
287
Chris Lattnercd147752009-03-29 17:27:48 +0000288 // If the client wants to check what comes after the declaration, just return
289 // immediately without checking anything!
290 if (!RequireSemi) return DG;
Chris Lattner23c4b182009-03-29 17:18:04 +0000291
292 if (Tok.is(tok::semi)) {
293 ConsumeToken();
Chris Lattner23c4b182009-03-29 17:18:04 +0000294 return DG;
295 }
296
Chris Lattner23c4b182009-03-29 17:18:04 +0000297 Diag(Tok, diag::err_expected_semi_declation);
298 // Skip to end of block or statement
299 SkipUntil(tok::r_brace, true, true);
300 if (Tok.is(tok::semi))
301 ConsumeToken();
302 return DG;
Reid Spencer5f016e22007-07-11 17:01:13 +0000303}
304
Chris Lattner8f08cb72007-08-25 06:57:03 +0000305
Reid Spencer5f016e22007-07-11 17:01:13 +0000306/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
307/// parsing 'declaration-specifiers declarator'. This method is split out this
308/// way to handle the ambiguity between top-level function-definitions and
309/// declarations.
310///
Reid Spencer5f016e22007-07-11 17:01:13 +0000311/// init-declarator-list: [C99 6.7]
312/// init-declarator
313/// init-declarator-list ',' init-declarator
314/// init-declarator: [C99 6.7]
315/// declarator
316/// declarator '=' initializer
317/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
318/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000319/// [C++] declarator initializer[opt]
320///
321/// [C++] initializer:
322/// [C++] '=' initializer-clause
323/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000324/// [C++0x] '=' 'default' [TODO]
325/// [C++0x] '=' 'delete'
326///
327/// According to the standard grammar, =default and =delete are function
328/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000329///
Chris Lattner682bf922009-03-29 16:50:03 +0000330Parser::DeclGroupPtrTy Parser::
Reid Spencer5f016e22007-07-11 17:01:13 +0000331ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattner682bf922009-03-29 16:50:03 +0000332 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
333 // that we parse together here.
334 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Reid Spencer5f016e22007-07-11 17:01:13 +0000335
336 // At this point, we know that it is not a function definition. Parse the
337 // rest of the init-declarator-list.
338 while (1) {
339 // If a simple-asm-expr is present, parse it.
Daniel Dunbara80f8742008-08-05 01:35:17 +0000340 if (Tok.is(tok::kw_asm)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +0000341 SourceLocation Loc;
342 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000343 if (AsmLabel.isInvalid()) {
Chris Lattner23c4b182009-03-29 17:18:04 +0000344 SkipUntil(tok::semi, true, true);
Chris Lattner682bf922009-03-29 16:50:03 +0000345 return DeclGroupPtrTy();
Daniel Dunbara80f8742008-08-05 01:35:17 +0000346 }
Sebastian Redlab197ba2009-02-09 18:23:29 +0000347
Sebastian Redleffa8d12008-12-10 00:02:53 +0000348 D.setAsmLabel(AsmLabel.release());
Sebastian Redlab197ba2009-02-09 18:23:29 +0000349 D.SetRangeEnd(Loc);
Daniel Dunbara80f8742008-08-05 01:35:17 +0000350 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000351
352 // If attributes are present, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +0000353 if (Tok.is(tok::kw___attribute)) {
354 SourceLocation Loc;
355 AttributeList *AttrList = ParseAttributes(&Loc);
356 D.AddAttributes(AttrList, Loc);
357 }
Steve Naroffbb204692007-09-12 14:07:44 +0000358
359 // Inform the current actions module that we just parsed this declarator.
Chris Lattner682bf922009-03-29 16:50:03 +0000360 DeclPtrTy ThisDecl = Actions.ActOnDeclarator(CurScope, D);
361 DeclsInGroup.push_back(ThisDecl);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000362
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 // Parse declarator '=' initializer.
Chris Lattner04d66662007-10-09 17:33:22 +0000364 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 ConsumeToken();
Sebastian Redl50de12f2009-03-24 22:27:57 +0000366 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
367 SourceLocation DelLoc = ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000368 Actions.SetDeclDeleted(ThisDecl, DelLoc);
Sebastian Redl50de12f2009-03-24 22:27:57 +0000369 } else {
370 OwningExprResult Init(ParseInitializer());
371 if (Init.isInvalid()) {
Chris Lattner23c4b182009-03-29 17:18:04 +0000372 SkipUntil(tok::semi, true, true);
Chris Lattner682bf922009-03-29 16:50:03 +0000373 return DeclGroupPtrTy();
Sebastian Redl50de12f2009-03-24 22:27:57 +0000374 }
Chris Lattner682bf922009-03-29 16:50:03 +0000375 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Reid Spencer5f016e22007-07-11 17:01:13 +0000376 }
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000377 } else if (Tok.is(tok::l_paren)) {
378 // Parse C++ direct initializer: '(' expression-list ')'
379 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redla55e52c2008-11-25 22:21:31 +0000380 ExprVector Exprs(Actions);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000381 CommaLocsTy CommaLocs;
382
383 bool InvalidExpr = false;
384 if (ParseExpressionList(Exprs, CommaLocs)) {
385 SkipUntil(tok::r_paren);
386 InvalidExpr = true;
387 }
388 // Match the ')'.
389 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
390
391 if (!InvalidExpr) {
392 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
393 "Unexpected number of commas!");
Chris Lattner682bf922009-03-29 16:50:03 +0000394 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000395 move_arg(Exprs),
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000396 &CommaLocs[0], RParenLoc);
397 }
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000398 } else {
Chris Lattner682bf922009-03-29 16:50:03 +0000399 Actions.ActOnUninitializedDecl(ThisDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 }
401
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 // If we don't have a comma, it is either the end of the list (a ';') or an
403 // error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000404 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000405 break;
406
407 // Consume the comma.
408 ConsumeToken();
409
410 // Parse the next declarator.
411 D.clear();
Chris Lattneraab740a2008-10-20 04:57:38 +0000412
413 // Accept attributes in an init-declarator. In the first declarator in a
414 // declaration, these would be part of the declspec. In subsequent
415 // declarators, they become part of the declarator itself, so that they
416 // don't apply to declarators after *this* one. Examples:
417 // short __attribute__((common)) var; -> declspec
418 // short var __attribute__((common)); -> declarator
419 // short x, __attribute__((common)) var; -> declarator
Sebastian Redlab197ba2009-02-09 18:23:29 +0000420 if (Tok.is(tok::kw___attribute)) {
421 SourceLocation Loc;
422 AttributeList *AttrList = ParseAttributes(&Loc);
423 D.AddAttributes(AttrList, Loc);
424 }
Chris Lattneraab740a2008-10-20 04:57:38 +0000425
Reid Spencer5f016e22007-07-11 17:01:13 +0000426 ParseDeclarator(D);
427 }
428
Chris Lattner23c4b182009-03-29 17:18:04 +0000429 return Actions.FinalizeDeclaratorGroup(CurScope, &DeclsInGroup[0],
430 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000431}
432
433/// ParseSpecifierQualifierList
434/// specifier-qualifier-list:
435/// type-specifier specifier-qualifier-list[opt]
436/// type-qualifier specifier-qualifier-list[opt]
437/// [GNU] attributes specifier-qualifier-list[opt]
438///
439void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
440 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
441 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000442 ParseDeclarationSpecifiers(DS);
443
444 // Validate declspec for type-name.
445 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000446 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Reid Spencer5f016e22007-07-11 17:01:13 +0000447 Diag(Tok, diag::err_typename_requires_specqual);
448
449 // Issue diagnostic and remove storage class if present.
450 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
451 if (DS.getStorageClassSpecLoc().isValid())
452 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
453 else
454 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
455 DS.ClearStorageClassSpecs();
456 }
457
458 // Issue diagnostic and remove function specfier if present.
459 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000460 if (DS.isInlineSpecified())
461 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
462 if (DS.isVirtualSpecified())
463 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
464 if (DS.isExplicitSpecified())
465 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000466 DS.ClearFunctionSpecs();
467 }
468}
469
470/// ParseDeclarationSpecifiers
471/// declaration-specifiers: [C99 6.7]
472/// storage-class-specifier declaration-specifiers[opt]
473/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000474/// [C99] function-specifier declaration-specifiers[opt]
475/// [GNU] attributes declaration-specifiers[opt]
476///
477/// storage-class-specifier: [C99 6.7.1]
478/// 'typedef'
479/// 'extern'
480/// 'static'
481/// 'auto'
482/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000483/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000484/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000485/// function-specifier: [C99 6.7.4]
486/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000487/// [C++] 'virtual'
488/// [C++] 'explicit'
Reid Spencer5f016e22007-07-11 17:01:13 +0000489///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000490void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000491 TemplateParameterLists *TemplateParams,
492 AccessSpecifier AS){
Chris Lattner81c018d2008-03-13 06:29:04 +0000493 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000494 while (1) {
495 int isInvalid = false;
496 const char *PrevSpec = 0;
497 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000498
Reid Spencer5f016e22007-07-11 17:01:13 +0000499 switch (Tok.getKind()) {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000500 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000501 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000502 // If this is not a declaration specifier token, we're done reading decl
503 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000504 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000505 return;
Chris Lattner5e02c472009-01-05 00:07:25 +0000506
507 case tok::coloncolon: // ::foo::bar
508 // Annotate C++ scope specifiers. If we get one, loop.
509 if (TryAnnotateCXXScopeToken())
510 continue;
511 goto DoneWithDeclSpec;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000512
513 case tok::annot_cxxscope: {
514 if (DS.hasTypeSpecifier())
515 goto DoneWithDeclSpec;
516
517 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000518 Token Next = NextToken();
519 if (Next.is(tok::annot_template_id) &&
520 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000521 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000522 // We have a qualified template-id, e.g., N::A<int>
523 CXXScopeSpec SS;
524 ParseOptionalCXXScopeSpecifier(SS);
525 assert(Tok.is(tok::annot_template_id) &&
526 "ParseOptionalCXXScopeSpecifier not working");
527 AnnotateTemplateIdTokenAsType(&SS);
528 continue;
529 }
530
531 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000532 goto DoneWithDeclSpec;
533
534 CXXScopeSpec SS;
Douglas Gregor35073692009-03-26 23:56:24 +0000535 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000536 SS.setRange(Tok.getAnnotationRange());
537
538 // If the next token is the name of the class type that the C++ scope
539 // denotes, followed by a '(', then this is a constructor declaration.
540 // We're done with the decl-specifiers.
541 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
542 CurScope, &SS) &&
543 GetLookAheadToken(2).is(tok::l_paren))
544 goto DoneWithDeclSpec;
545
Douglas Gregorb696ea32009-02-04 17:00:24 +0000546 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
547 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000548
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000549 if (TypeRep == 0)
550 goto DoneWithDeclSpec;
Douglas Gregore4e5b052009-03-19 00:18:19 +0000551
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000552 ConsumeToken(); // The C++ scope.
553
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000554 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000555 TypeRep);
556 if (isInvalid)
557 break;
558
559 DS.SetRangeEnd(Tok.getLocation());
560 ConsumeToken(); // The typename.
561
562 continue;
563 }
Chris Lattner80d0c892009-01-21 19:48:37 +0000564
565 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000566 if (Tok.getAnnotationValue())
567 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
568 Tok.getAnnotationValue());
569 else
570 DS.SetTypeSpecError();
Chris Lattner80d0c892009-01-21 19:48:37 +0000571 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
572 ConsumeToken(); // The typename
573
574 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
575 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
576 // Objective-C interface. If we don't have Objective-C or a '<', this is
577 // just a normal reference to a typedef name.
578 if (!Tok.is(tok::less) || !getLang().ObjC1)
579 continue;
580
581 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000582 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner80d0c892009-01-21 19:48:37 +0000583 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
584 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
585
586 DS.SetRangeEnd(EndProtoLoc);
587 continue;
588 }
589
Chris Lattner3bd934a2008-07-26 01:18:38 +0000590 // typedef-name
591 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +0000592 // In C++, check to see if this is a scope specifier like foo::bar::, if
593 // so handle it as such. This is important for ctor parsing.
Chris Lattner837acd02009-01-21 19:19:26 +0000594 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
595 continue;
Chris Lattner5e02c472009-01-05 00:07:25 +0000596
Chris Lattner3bd934a2008-07-26 01:18:38 +0000597 // This identifier can only be a typedef name if we haven't already seen
598 // a type-specifier. Without this check we misparse:
599 // typedef int X; struct Y { short X; }; as 'short int'.
600 if (DS.hasTypeSpecifier())
601 goto DoneWithDeclSpec;
602
603 // It has to be available as a typedef too!
Douglas Gregorb696ea32009-02-04 17:00:24 +0000604 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
605 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000606
Chris Lattner3bd934a2008-07-26 01:18:38 +0000607 if (TypeRep == 0)
608 goto DoneWithDeclSpec;
Douglas Gregor55f6b142009-02-09 18:46:07 +0000609
Douglas Gregorb48fe382008-10-31 09:07:45 +0000610 // C++: If the identifier is actually the name of the class type
611 // being defined and the next token is a '(', then this is a
612 // constructor declaration. We're done with the decl-specifiers
613 // and will treat this token as an identifier.
614 if (getLang().CPlusPlus &&
Douglas Gregor3218c4b2009-01-09 22:42:13 +0000615 CurScope->isClassScope() &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000616 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
617 NextToken().getKind() == tok::l_paren)
618 goto DoneWithDeclSpec;
619
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000620 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattner3bd934a2008-07-26 01:18:38 +0000621 TypeRep);
622 if (isInvalid)
623 break;
624
625 DS.SetRangeEnd(Tok.getLocation());
626 ConsumeToken(); // The identifier
627
628 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
629 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
630 // Objective-C interface. If we don't have Objective-C or a '<', this is
631 // just a normal reference to a typedef name.
632 if (!Tok.is(tok::less) || !getLang().ObjC1)
633 continue;
634
635 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000636 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000637 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000638 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000639
640 DS.SetRangeEnd(EndProtoLoc);
641
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000642 // Need to support trailing type qualifiers (e.g. "id<p> const").
643 // If a type specifier follows, it will be diagnosed elsewhere.
644 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000645 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000646
647 // type-name
648 case tok::annot_template_id: {
649 TemplateIdAnnotation *TemplateId
650 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000651 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000652 // This template-id does not refer to a type name, so we're
653 // done with the type-specifiers.
654 goto DoneWithDeclSpec;
655 }
656
657 // Turn the template-id annotation token into a type annotation
658 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +0000659 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000660 continue;
661 }
662
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 // GNU attributes support.
664 case tok::kw___attribute:
665 DS.AddAttributes(ParseAttributes());
666 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +0000667
668 // Microsoft declspec support.
669 case tok::kw___declspec:
670 if (!PP.getLangOptions().Microsoft)
671 goto DoneWithDeclSpec;
672 FuzzyParseMicrosoftDeclSpec();
673 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000674
Steve Naroff239f0732008-12-25 14:16:32 +0000675 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000676 case tok::kw___forceinline:
677 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +0000678 case tok::kw___cdecl:
679 case tok::kw___stdcall:
680 case tok::kw___fastcall:
681 if (!PP.getLangOptions().Microsoft)
682 goto DoneWithDeclSpec;
683 // Just ignore it.
684 break;
685
Reid Spencer5f016e22007-07-11 17:01:13 +0000686 // storage-class-specifier
687 case tok::kw_typedef:
688 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
689 break;
690 case tok::kw_extern:
691 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000692 Diag(Tok, diag::ext_thread_before) << "extern";
Reid Spencer5f016e22007-07-11 17:01:13 +0000693 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
694 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000695 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000696 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
697 PrevSpec);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000698 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000699 case tok::kw_static:
700 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000701 Diag(Tok, diag::ext_thread_before) << "static";
Reid Spencer5f016e22007-07-11 17:01:13 +0000702 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
703 break;
704 case tok::kw_auto:
705 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
706 break;
707 case tok::kw_register:
708 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
709 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000710 case tok::kw_mutable:
711 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
712 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000713 case tok::kw___thread:
714 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
715 break;
716
Reid Spencer5f016e22007-07-11 17:01:13 +0000717 continue;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000718
Reid Spencer5f016e22007-07-11 17:01:13 +0000719 // function-specifier
720 case tok::kw_inline:
721 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
722 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000723 case tok::kw_virtual:
724 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
725 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000726 case tok::kw_explicit:
727 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
728 break;
Chris Lattner80d0c892009-01-21 19:48:37 +0000729
730 // type-specifier
731 case tok::kw_short:
732 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
733 break;
734 case tok::kw_long:
735 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
736 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
737 else
738 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
739 break;
740 case tok::kw_signed:
741 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
742 break;
743 case tok::kw_unsigned:
744 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
745 break;
746 case tok::kw__Complex:
747 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
748 break;
749 case tok::kw__Imaginary:
750 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
751 break;
752 case tok::kw_void:
753 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
754 break;
755 case tok::kw_char:
756 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
757 break;
758 case tok::kw_int:
759 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
760 break;
761 case tok::kw_float:
762 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
763 break;
764 case tok::kw_double:
765 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
766 break;
767 case tok::kw_wchar_t:
768 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
769 break;
770 case tok::kw_bool:
771 case tok::kw__Bool:
772 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
773 break;
774 case tok::kw__Decimal32:
775 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
776 break;
777 case tok::kw__Decimal64:
778 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
779 break;
780 case tok::kw__Decimal128:
781 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
782 break;
783
784 // class-specifier:
785 case tok::kw_class:
786 case tok::kw_struct:
787 case tok::kw_union:
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000788 ParseClassSpecifier(DS, TemplateParams, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +0000789 continue;
790
791 // enum-specifier:
792 case tok::kw_enum:
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000793 ParseEnumSpecifier(DS, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +0000794 continue;
795
796 // cv-qualifier:
797 case tok::kw_const:
798 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
799 break;
800 case tok::kw_volatile:
801 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
802 getLang())*2;
803 break;
804 case tok::kw_restrict:
805 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
806 getLang())*2;
807 break;
808
Douglas Gregord57959a2009-03-27 23:10:48 +0000809 // C++ typename-specifier:
810 case tok::kw_typename:
811 if (TryAnnotateTypeOrScopeToken())
812 continue;
813 break;
814
Chris Lattner80d0c892009-01-21 19:48:37 +0000815 // GNU typeof support.
816 case tok::kw_typeof:
817 ParseTypeofSpecifier(DS);
818 continue;
819
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000820 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +0000821 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +0000822 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
823 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +0000824 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +0000825 goto DoneWithDeclSpec;
826
827 {
828 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000829 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000830 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000831 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000832 DS.SetRangeEnd(EndProtoLoc);
833
Chris Lattner1ab3b962008-11-18 07:48:38 +0000834 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
835 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000836 // Need to support trailing type qualifiers (e.g. "id<p> const").
837 // If a type specifier follows, it will be diagnosed elsewhere.
838 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000839 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000840 }
841 // If the specifier combination wasn't legal, issue a diagnostic.
842 if (isInvalid) {
843 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +0000844 // Pick between error or extwarn.
845 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
846 : diag::ext_duplicate_declspec;
847 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000848 }
Chris Lattner81c018d2008-03-13 06:29:04 +0000849 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000850 ConsumeToken();
851 }
852}
Douglas Gregoradcac882008-12-01 23:54:00 +0000853
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000854/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +0000855/// primarily follow the C++ grammar with additions for C99 and GNU,
856/// which together subsume the C grammar. Note that the C++
857/// type-specifier also includes the C type-qualifier (for const,
858/// volatile, and C99 restrict). Returns true if a type-specifier was
859/// found (and parsed), false otherwise.
860///
861/// type-specifier: [C++ 7.1.5]
862/// simple-type-specifier
863/// class-specifier
864/// enum-specifier
865/// elaborated-type-specifier [TODO]
866/// cv-qualifier
867///
868/// cv-qualifier: [C++ 7.1.5.1]
869/// 'const'
870/// 'volatile'
871/// [C99] 'restrict'
872///
873/// simple-type-specifier: [ C++ 7.1.5.2]
874/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
875/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
876/// 'char'
877/// 'wchar_t'
878/// 'bool'
879/// 'short'
880/// 'int'
881/// 'long'
882/// 'signed'
883/// 'unsigned'
884/// 'float'
885/// 'double'
886/// 'void'
887/// [C99] '_Bool'
888/// [C99] '_Complex'
889/// [C99] '_Imaginary' // Removed in TC2?
890/// [GNU] '_Decimal32'
891/// [GNU] '_Decimal64'
892/// [GNU] '_Decimal128'
893/// [GNU] typeof-specifier
894/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
895/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000896bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
897 const char *&PrevSpec,
898 TemplateParameterLists *TemplateParams){
Douglas Gregor12e083c2008-11-07 15:42:26 +0000899 SourceLocation Loc = Tok.getLocation();
900
901 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +0000902 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +0000903 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +0000904 // Annotate typenames and C++ scope specifiers. If we get one, just
905 // recurse to handle whatever we get.
906 if (TryAnnotateTypeOrScopeToken())
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000907 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattner166a8fc2009-01-04 23:41:41 +0000908 // Otherwise, not a type specifier.
909 return false;
910 case tok::coloncolon: // ::foo::bar
911 if (NextToken().is(tok::kw_new) || // ::new
912 NextToken().is(tok::kw_delete)) // ::delete
913 return false;
914
915 // Annotate typenames and C++ scope specifiers. If we get one, just
916 // recurse to handle whatever we get.
917 if (TryAnnotateTypeOrScopeToken())
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000918 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattner166a8fc2009-01-04 23:41:41 +0000919 // Otherwise, not a type specifier.
920 return false;
921
Douglas Gregor12e083c2008-11-07 15:42:26 +0000922 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +0000923 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000924 if (Tok.getAnnotationValue())
925 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
926 Tok.getAnnotationValue());
927 else
928 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000929 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
930 ConsumeToken(); // The typename
Douglas Gregor12e083c2008-11-07 15:42:26 +0000931
932 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
933 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
934 // Objective-C interface. If we don't have Objective-C or a '<', this is
935 // just a normal reference to a typedef name.
936 if (!Tok.is(tok::less) || !getLang().ObjC1)
937 return true;
938
939 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000940 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000941 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
942 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
943
944 DS.SetRangeEnd(EndProtoLoc);
945 return true;
946 }
947
948 case tok::kw_short:
949 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
950 break;
951 case tok::kw_long:
952 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
953 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
954 else
955 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
956 break;
957 case tok::kw_signed:
958 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
959 break;
960 case tok::kw_unsigned:
961 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
962 break;
963 case tok::kw__Complex:
964 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
965 break;
966 case tok::kw__Imaginary:
967 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
968 break;
969 case tok::kw_void:
970 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
971 break;
972 case tok::kw_char:
973 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
974 break;
975 case tok::kw_int:
976 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
977 break;
978 case tok::kw_float:
979 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
980 break;
981 case tok::kw_double:
982 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
983 break;
984 case tok::kw_wchar_t:
985 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
986 break;
987 case tok::kw_bool:
988 case tok::kw__Bool:
989 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
990 break;
991 case tok::kw__Decimal32:
992 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
993 break;
994 case tok::kw__Decimal64:
995 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
996 break;
997 case tok::kw__Decimal128:
998 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
999 break;
1000
1001 // class-specifier:
1002 case tok::kw_class:
1003 case tok::kw_struct:
1004 case tok::kw_union:
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001005 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001006 return true;
1007
1008 // enum-specifier:
1009 case tok::kw_enum:
1010 ParseEnumSpecifier(DS);
1011 return true;
1012
1013 // cv-qualifier:
1014 case tok::kw_const:
1015 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1016 getLang())*2;
1017 break;
1018 case tok::kw_volatile:
1019 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1020 getLang())*2;
1021 break;
1022 case tok::kw_restrict:
1023 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1024 getLang())*2;
1025 break;
1026
1027 // GNU typeof support.
1028 case tok::kw_typeof:
1029 ParseTypeofSpecifier(DS);
1030 return true;
1031
Steve Naroff239f0732008-12-25 14:16:32 +00001032 case tok::kw___cdecl:
1033 case tok::kw___stdcall:
1034 case tok::kw___fastcall:
Chris Lattner837acd02009-01-21 19:19:26 +00001035 if (!PP.getLangOptions().Microsoft) return false;
1036 ConsumeToken();
1037 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001038
Douglas Gregor12e083c2008-11-07 15:42:26 +00001039 default:
1040 // Not a type-specifier; do nothing.
1041 return false;
1042 }
1043
1044 // If the specifier combination wasn't legal, issue a diagnostic.
1045 if (isInvalid) {
1046 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001047 // Pick between error or extwarn.
1048 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1049 : diag::ext_duplicate_declspec;
1050 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001051 }
1052 DS.SetRangeEnd(Tok.getLocation());
1053 ConsumeToken(); // whatever we parsed above.
1054 return true;
1055}
Reid Spencer5f016e22007-07-11 17:01:13 +00001056
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001057/// ParseStructDeclaration - Parse a struct declaration without the terminating
1058/// semicolon.
1059///
Reid Spencer5f016e22007-07-11 17:01:13 +00001060/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001061/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001062/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001063/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001064/// struct-declarator-list:
1065/// struct-declarator
1066/// struct-declarator-list ',' struct-declarator
1067/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1068/// struct-declarator:
1069/// declarator
1070/// [GNU] declarator attributes[opt]
1071/// declarator[opt] ':' constant-expression
1072/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1073///
Chris Lattnere1359422008-04-10 06:46:29 +00001074void Parser::
1075ParseStructDeclaration(DeclSpec &DS,
1076 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001077 if (Tok.is(tok::kw___extension__)) {
1078 // __extension__ silences extension warnings in the subexpression.
1079 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001080 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001081 return ParseStructDeclaration(DS, Fields);
1082 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001083
1084 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001085 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001086 ParseSpecifierQualifierList(DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001087
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001088 // If there are no declarators, this is a free-standing declaration
1089 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001090 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001091 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001092 return;
1093 }
1094
1095 // Read struct-declarators until we find the semicolon.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001096 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +00001097 while (1) {
Chris Lattnere1359422008-04-10 06:46:29 +00001098 FieldDeclarator &DeclaratorInfo = Fields.back();
1099
Steve Naroff28a7ca82007-08-20 22:28:22 +00001100 /// struct-declarator: declarator
1101 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +00001102 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +00001103 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001104
Chris Lattner04d66662007-10-09 17:33:22 +00001105 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001106 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001107 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001108 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001109 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001110 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001111 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001112 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001113
Steve Naroff28a7ca82007-08-20 22:28:22 +00001114 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001115 if (Tok.is(tok::kw___attribute)) {
1116 SourceLocation Loc;
1117 AttributeList *AttrList = ParseAttributes(&Loc);
1118 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1119 }
1120
Steve Naroff28a7ca82007-08-20 22:28:22 +00001121 // If we don't have a comma, it is either the end of the list (a ';')
1122 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001123 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001124 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001125
Steve Naroff28a7ca82007-08-20 22:28:22 +00001126 // Consume the comma.
1127 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001128
Steve Naroff28a7ca82007-08-20 22:28:22 +00001129 // Parse the next declarator.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001130 Fields.push_back(FieldDeclarator(DS));
Sebastian Redlab197ba2009-02-09 18:23:29 +00001131
Steve Naroff28a7ca82007-08-20 22:28:22 +00001132 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001133 if (Tok.is(tok::kw___attribute)) {
1134 SourceLocation Loc;
1135 AttributeList *AttrList = ParseAttributes(&Loc);
1136 Fields.back().D.AddAttributes(AttrList, Loc);
1137 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001138 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001139}
1140
1141/// ParseStructUnionBody
1142/// struct-contents:
1143/// struct-declaration-list
1144/// [EXT] empty
1145/// [GNU] "struct-declaration-list" without terminatoring ';'
1146/// struct-declaration-list:
1147/// struct-declaration
1148/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001149/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001150///
Reid Spencer5f016e22007-07-11 17:01:13 +00001151void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001152 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001153 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1154 PP.getSourceManager(),
1155 "parsing struct/union body");
Chris Lattner27b7f102009-03-05 02:25:03 +00001156
Reid Spencer5f016e22007-07-11 17:01:13 +00001157 SourceLocation LBraceLoc = ConsumeBrace();
1158
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001159 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001160 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1161
Reid Spencer5f016e22007-07-11 17:01:13 +00001162 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1163 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001164 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001165 Diag(Tok, diag::ext_empty_struct_union_enum)
1166 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001167
Chris Lattnerb28317a2009-03-28 19:18:32 +00001168 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001169 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1170
Reid Spencer5f016e22007-07-11 17:01:13 +00001171 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001172 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001173 // Each iteration of this loop reads one struct-declaration.
1174
1175 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001176 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001177 Diag(Tok, diag::ext_extra_struct_semi)
1178 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001179 ConsumeToken();
1180 continue;
1181 }
Chris Lattnere1359422008-04-10 06:46:29 +00001182
1183 // Parse all the comma separated declarators.
1184 DeclSpec DS;
1185 FieldDeclarators.clear();
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001186 if (!Tok.is(tok::at)) {
1187 ParseStructDeclaration(DS, FieldDeclarators);
1188
1189 // Convert them all to fields.
1190 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1191 FieldDeclarator &FD = FieldDeclarators[i];
1192 // Install the declarator into the current TagDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001193 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1194 DS.getSourceRange().getBegin(),
1195 FD.D, FD.BitfieldSize);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001196 FieldDecls.push_back(Field);
1197 }
1198 } else { // Handle @defs
1199 ConsumeToken();
1200 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1201 Diag(Tok, diag::err_unexpected_at);
1202 SkipUntil(tok::semi, true, true);
1203 continue;
1204 }
1205 ConsumeToken();
1206 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1207 if (!Tok.is(tok::identifier)) {
1208 Diag(Tok, diag::err_expected_ident);
1209 SkipUntil(tok::semi, true, true);
1210 continue;
1211 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001212 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor44b43212008-12-11 16:49:14 +00001213 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1214 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001215 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1216 ConsumeToken();
1217 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1218 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001219
Chris Lattner04d66662007-10-09 17:33:22 +00001220 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001221 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001222 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001223 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001224 break;
1225 } else {
1226 Diag(Tok, diag::err_expected_semi_decl_list);
1227 // Skip to end of block or statement
1228 SkipUntil(tok::r_brace, true, true);
1229 }
1230 }
1231
Steve Naroff60fccee2007-10-29 21:38:07 +00001232 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001233
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 AttributeList *AttrList = 0;
1235 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001236 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +00001237 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001238
1239 Actions.ActOnFields(CurScope,
1240 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1241 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001242 AttrList);
1243 StructScope.Exit();
1244 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001245}
1246
1247
1248/// ParseEnumSpecifier
1249/// enum-specifier: [C99 6.7.2.2]
1250/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001251///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001252/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1253/// '}' attributes[opt]
1254/// 'enum' identifier
1255/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001256///
1257/// [C++] elaborated-type-specifier:
1258/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1259///
Douglas Gregor06c0fec2009-03-25 22:00:53 +00001260void Parser::ParseEnumSpecifier(DeclSpec &DS, AccessSpecifier AS) {
Chris Lattner04d66662007-10-09 17:33:22 +00001261 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Reid Spencer5f016e22007-07-11 17:01:13 +00001262 SourceLocation StartLoc = ConsumeToken();
1263
1264 // Parse the tag portion of this.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001265
1266 AttributeList *Attr = 0;
1267 // If attributes exist after tag, parse them.
1268 if (Tok.is(tok::kw___attribute))
1269 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001270
1271 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001272 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001273 if (Tok.isNot(tok::identifier)) {
1274 Diag(Tok, diag::err_expected_ident);
1275 if (Tok.isNot(tok::l_brace)) {
1276 // Has no name and is not a definition.
1277 // Skip the rest of this declarator, up until the comma or semicolon.
1278 SkipUntil(tok::comma, true);
1279 return;
1280 }
1281 }
1282 }
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001283
1284 // Must have either 'enum name' or 'enum {...}'.
1285 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1286 Diag(Tok, diag::err_expected_ident_lbrace);
1287
1288 // Skip the rest of this declarator, up until the comma or semicolon.
1289 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001290 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001291 }
1292
1293 // If an identifier is present, consume and remember it.
1294 IdentifierInfo *Name = 0;
1295 SourceLocation NameLoc;
1296 if (Tok.is(tok::identifier)) {
1297 Name = Tok.getIdentifierInfo();
1298 NameLoc = ConsumeToken();
1299 }
1300
1301 // There are three options here. If we have 'enum foo;', then this is a
1302 // forward declaration. If we have 'enum foo {...' then this is a
1303 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1304 //
1305 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1306 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1307 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1308 //
1309 Action::TagKind TK;
1310 if (Tok.is(tok::l_brace))
1311 TK = Action::TK_Definition;
1312 else if (Tok.is(tok::semi))
1313 TK = Action::TK_Declaration;
1314 else
1315 TK = Action::TK_Reference;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001316 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
1317 StartLoc, SS, Name, NameLoc, Attr, AS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001318
Chris Lattner04d66662007-10-09 17:33:22 +00001319 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001320 ParseEnumBody(StartLoc, TagDecl);
1321
1322 // TODO: semantic analysis on the declspec for enums.
1323 const char *PrevSpec = 0;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001324 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
1325 TagDecl.getAs<void>()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001326 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001327}
1328
1329/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1330/// enumerator-list:
1331/// enumerator
1332/// enumerator-list ',' enumerator
1333/// enumerator:
1334/// enumeration-constant
1335/// enumeration-constant '=' constant-expression
1336/// enumeration-constant:
1337/// identifier
1338///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001339void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001340 // Enter the scope of the enum body and start the definition.
1341 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001342 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001343
Reid Spencer5f016e22007-07-11 17:01:13 +00001344 SourceLocation LBraceLoc = ConsumeBrace();
1345
Chris Lattner7946dd32007-08-27 17:24:30 +00001346 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001347 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001348 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Reid Spencer5f016e22007-07-11 17:01:13 +00001349
Chris Lattnerb28317a2009-03-28 19:18:32 +00001350 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00001351
Chris Lattnerb28317a2009-03-28 19:18:32 +00001352 DeclPtrTy LastEnumConstDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001353
1354 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001355 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001356 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1357 SourceLocation IdentLoc = ConsumeToken();
1358
1359 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001360 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001361 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001362 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001363 AssignedVal = ParseConstantExpression();
1364 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001365 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001366 }
1367
1368 // Install the enumerator constant into EnumDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001369 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1370 LastEnumConstDecl,
1371 IdentLoc, Ident,
1372 EqualLoc,
1373 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001374 EnumConstantDecls.push_back(EnumConstDecl);
1375 LastEnumConstDecl = EnumConstDecl;
1376
Chris Lattner04d66662007-10-09 17:33:22 +00001377 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001378 break;
1379 SourceLocation CommaLoc = ConsumeToken();
1380
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001381 if (Tok.isNot(tok::identifier) &&
1382 !(getLang().C99 || getLang().CPlusPlus0x))
1383 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1384 << getLang().CPlusPlus
1385 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Reid Spencer5f016e22007-07-11 17:01:13 +00001386 }
1387
1388 // Eat the }.
1389 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1390
Steve Naroff08d92e42007-09-15 18:49:24 +00001391 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Reid Spencer5f016e22007-07-11 17:01:13 +00001392 EnumConstantDecls.size());
1393
Chris Lattnerb28317a2009-03-28 19:18:32 +00001394 Action::AttrTy *AttrList = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001395 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001396 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001397 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00001398
1399 EnumScope.Exit();
1400 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001401}
1402
1403/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001404/// start of a type-qualifier-list.
1405bool Parser::isTypeQualifier() const {
1406 switch (Tok.getKind()) {
1407 default: return false;
1408 // type-qualifier
1409 case tok::kw_const:
1410 case tok::kw_volatile:
1411 case tok::kw_restrict:
1412 return true;
1413 }
1414}
1415
1416/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001417/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001418bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001419 switch (Tok.getKind()) {
1420 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001421
1422 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001423 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001424 // Annotate typenames and C++ scope specifiers. If we get one, just
1425 // recurse to handle whatever we get.
1426 if (TryAnnotateTypeOrScopeToken())
1427 return isTypeSpecifierQualifier();
1428 // Otherwise, not a type specifier.
1429 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001430
Chris Lattner166a8fc2009-01-04 23:41:41 +00001431 case tok::coloncolon: // ::foo::bar
1432 if (NextToken().is(tok::kw_new) || // ::new
1433 NextToken().is(tok::kw_delete)) // ::delete
1434 return false;
1435
1436 // Annotate typenames and C++ scope specifiers. If we get one, just
1437 // recurse to handle whatever we get.
1438 if (TryAnnotateTypeOrScopeToken())
1439 return isTypeSpecifierQualifier();
1440 // Otherwise, not a type specifier.
1441 return false;
1442
Reid Spencer5f016e22007-07-11 17:01:13 +00001443 // GNU attributes support.
1444 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001445 // GNU typeof support.
1446 case tok::kw_typeof:
1447
Reid Spencer5f016e22007-07-11 17:01:13 +00001448 // type-specifiers
1449 case tok::kw_short:
1450 case tok::kw_long:
1451 case tok::kw_signed:
1452 case tok::kw_unsigned:
1453 case tok::kw__Complex:
1454 case tok::kw__Imaginary:
1455 case tok::kw_void:
1456 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001457 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001458 case tok::kw_int:
1459 case tok::kw_float:
1460 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001461 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001462 case tok::kw__Bool:
1463 case tok::kw__Decimal32:
1464 case tok::kw__Decimal64:
1465 case tok::kw__Decimal128:
1466
Chris Lattner99dc9142008-04-13 18:59:07 +00001467 // struct-or-union-specifier (C99) or class-specifier (C++)
1468 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001469 case tok::kw_struct:
1470 case tok::kw_union:
1471 // enum-specifier
1472 case tok::kw_enum:
1473
1474 // type-qualifier
1475 case tok::kw_const:
1476 case tok::kw_volatile:
1477 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001478
1479 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001480 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001481 return true;
Chris Lattner7c186be2008-10-20 00:25:30 +00001482
1483 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1484 case tok::less:
1485 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001486
1487 case tok::kw___cdecl:
1488 case tok::kw___stdcall:
1489 case tok::kw___fastcall:
1490 return PP.getLangOptions().Microsoft;
Reid Spencer5f016e22007-07-11 17:01:13 +00001491 }
1492}
1493
1494/// isDeclarationSpecifier() - Return true if the current token is part of a
1495/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001496bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001497 switch (Tok.getKind()) {
1498 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001499
1500 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00001501 // Unfortunate hack to support "Class.factoryMethod" notation.
1502 if (getLang().ObjC1 && NextToken().is(tok::period))
1503 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001504 // Fall through
Steve Naroff61f72cb2009-03-09 21:12:44 +00001505
Douglas Gregord57959a2009-03-27 23:10:48 +00001506 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001507 // Annotate typenames and C++ scope specifiers. If we get one, just
1508 // recurse to handle whatever we get.
1509 if (TryAnnotateTypeOrScopeToken())
1510 return isDeclarationSpecifier();
1511 // Otherwise, not a declaration specifier.
1512 return false;
1513 case tok::coloncolon: // ::foo::bar
1514 if (NextToken().is(tok::kw_new) || // ::new
1515 NextToken().is(tok::kw_delete)) // ::delete
1516 return false;
1517
1518 // Annotate typenames and C++ scope specifiers. If we get one, just
1519 // recurse to handle whatever we get.
1520 if (TryAnnotateTypeOrScopeToken())
1521 return isDeclarationSpecifier();
1522 // Otherwise, not a declaration specifier.
1523 return false;
1524
Reid Spencer5f016e22007-07-11 17:01:13 +00001525 // storage-class-specifier
1526 case tok::kw_typedef:
1527 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001528 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001529 case tok::kw_static:
1530 case tok::kw_auto:
1531 case tok::kw_register:
1532 case tok::kw___thread:
1533
1534 // type-specifiers
1535 case tok::kw_short:
1536 case tok::kw_long:
1537 case tok::kw_signed:
1538 case tok::kw_unsigned:
1539 case tok::kw__Complex:
1540 case tok::kw__Imaginary:
1541 case tok::kw_void:
1542 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001543 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001544 case tok::kw_int:
1545 case tok::kw_float:
1546 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001547 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001548 case tok::kw__Bool:
1549 case tok::kw__Decimal32:
1550 case tok::kw__Decimal64:
1551 case tok::kw__Decimal128:
1552
Chris Lattner99dc9142008-04-13 18:59:07 +00001553 // struct-or-union-specifier (C99) or class-specifier (C++)
1554 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001555 case tok::kw_struct:
1556 case tok::kw_union:
1557 // enum-specifier
1558 case tok::kw_enum:
1559
1560 // type-qualifier
1561 case tok::kw_const:
1562 case tok::kw_volatile:
1563 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001564
Reid Spencer5f016e22007-07-11 17:01:13 +00001565 // function-specifier
1566 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00001567 case tok::kw_virtual:
1568 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001569
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001570 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001571 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001572
Chris Lattner1ef08762007-08-09 17:01:07 +00001573 // GNU typeof support.
1574 case tok::kw_typeof:
1575
1576 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001577 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00001578 return true;
Chris Lattnerf3948c42008-07-26 03:38:44 +00001579
1580 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1581 case tok::less:
1582 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001583
Steve Naroff47f52092009-01-06 19:34:12 +00001584 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00001585 case tok::kw___cdecl:
1586 case tok::kw___stdcall:
1587 case tok::kw___fastcall:
1588 return PP.getLangOptions().Microsoft;
Reid Spencer5f016e22007-07-11 17:01:13 +00001589 }
1590}
1591
1592
1593/// ParseTypeQualifierListOpt
1594/// type-qualifier-list: [C99 6.7.5]
1595/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001596/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001597/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001598/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001599///
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001600void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001601 while (1) {
1602 int isInvalid = false;
1603 const char *PrevSpec = 0;
1604 SourceLocation Loc = Tok.getLocation();
1605
1606 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001607 case tok::kw_const:
1608 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1609 getLang())*2;
1610 break;
1611 case tok::kw_volatile:
1612 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1613 getLang())*2;
1614 break;
1615 case tok::kw_restrict:
1616 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1617 getLang())*2;
1618 break;
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001619 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00001620 case tok::kw___cdecl:
1621 case tok::kw___stdcall:
1622 case tok::kw___fastcall:
1623 if (!PP.getLangOptions().Microsoft)
1624 goto DoneWithTypeQuals;
1625 // Just ignore it.
1626 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001627 case tok::kw___attribute:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001628 if (AttributesAllowed) {
1629 DS.AddAttributes(ParseAttributes());
1630 continue; // do *not* consume the next token!
1631 }
1632 // otherwise, FALL THROUGH!
1633 default:
Steve Naroff239f0732008-12-25 14:16:32 +00001634 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001635 // If this is not a type-qualifier token, we're done reading type
1636 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001637 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001638 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001639 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00001640
Reid Spencer5f016e22007-07-11 17:01:13 +00001641 // If the specifier combination wasn't legal, issue a diagnostic.
1642 if (isInvalid) {
1643 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001644 // Pick between error or extwarn.
1645 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1646 : diag::ext_duplicate_declspec;
1647 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001648 }
1649 ConsumeToken();
1650 }
1651}
1652
1653
1654/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1655///
1656void Parser::ParseDeclarator(Declarator &D) {
1657 /// This implements the 'declarator' production in the C grammar, then checks
1658 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001659 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00001660}
1661
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001662/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1663/// is parsed by the function passed to it. Pass null, and the direct-declarator
1664/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001665/// ptr-operator production.
1666///
Sebastian Redlf30208a2009-01-24 21:16:55 +00001667/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1668/// [C] pointer[opt] direct-declarator
1669/// [C++] direct-declarator
1670/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00001671///
1672/// pointer: [C99 6.7.5]
1673/// '*' type-qualifier-list[opt]
1674/// '*' type-qualifier-list[opt] pointer
1675///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001676/// ptr-operator:
1677/// '*' cv-qualifier-seq[opt]
1678/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00001679/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001680/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00001681/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00001682/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001683void Parser::ParseDeclaratorInternal(Declarator &D,
1684 DirectDeclParseFunction DirectDeclParser) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001685
Sebastian Redlf30208a2009-01-24 21:16:55 +00001686 // C++ member pointers start with a '::' or a nested-name.
1687 // Member pointers get special handling, since there's no place for the
1688 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00001689 if (getLang().CPlusPlus &&
1690 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1691 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001692 CXXScopeSpec SS;
1693 if (ParseOptionalCXXScopeSpecifier(SS)) {
1694 if(Tok.isNot(tok::star)) {
1695 // The scope spec really belongs to the direct-declarator.
1696 D.getCXXScopeSpec() = SS;
1697 if (DirectDeclParser)
1698 (this->*DirectDeclParser)(D);
1699 return;
1700 }
1701
1702 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001703 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001704 DeclSpec DS;
1705 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001706 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001707
1708 // Recurse to parse whatever is left.
1709 ParseDeclaratorInternal(D, DirectDeclParser);
1710
1711 // Sema will have to catch (syntactically invalid) pointers into global
1712 // scope. It has to catch pointers into namespace scope anyway.
1713 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001714 Loc, DS.TakeAttributes()),
1715 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00001716 return;
1717 }
1718 }
1719
1720 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00001721 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00001722 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00001723 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00001724 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00001725 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001726 if (DirectDeclParser)
1727 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001728 return;
1729 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001730
Sebastian Redl05532f22009-03-15 22:02:01 +00001731 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1732 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00001733 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001734 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001735
Chris Lattner9af55002009-03-27 04:18:06 +00001736 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00001737 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00001738 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001739
Reid Spencer5f016e22007-07-11 17:01:13 +00001740 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001741 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001742
Reid Spencer5f016e22007-07-11 17:01:13 +00001743 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001744 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00001745 if (Kind == tok::star)
1746 // Remember that we parsed a pointer type, and remember the type-quals.
1747 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00001748 DS.TakeAttributes()),
1749 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00001750 else
1751 // Remember that we parsed a Block type, and remember the type-quals.
1752 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001753 Loc),
1754 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001755 } else {
1756 // Is a reference
1757 DeclSpec DS;
1758
Sebastian Redl743de1f2009-03-23 00:00:23 +00001759 // Complain about rvalue references in C++03, but then go on and build
1760 // the declarator.
1761 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1762 Diag(Loc, diag::err_rvalue_reference);
1763
Reid Spencer5f016e22007-07-11 17:01:13 +00001764 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1765 // cv-qualifiers are introduced through the use of a typedef or of a
1766 // template type argument, in which case the cv-qualifiers are ignored.
1767 //
1768 // [GNU] Retricted references are allowed.
1769 // [GNU] Attributes on references are allowed.
1770 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001771 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00001772
1773 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1774 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1775 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001776 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001777 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1778 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001779 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00001780 }
1781
1782 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001783 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00001784
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001785 if (D.getNumTypeObjects() > 0) {
1786 // C++ [dcl.ref]p4: There shall be no references to references.
1787 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1788 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00001789 if (const IdentifierInfo *II = D.getIdentifier())
1790 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1791 << II;
1792 else
1793 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1794 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001795
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001796 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001797 // can go ahead and build the (technically ill-formed)
1798 // declarator: reference collapsing will take care of it.
1799 }
1800 }
1801
Reid Spencer5f016e22007-07-11 17:01:13 +00001802 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001803 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00001804 DS.TakeAttributes(),
1805 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00001806 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001807 }
1808}
1809
1810/// ParseDirectDeclarator
1811/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00001812/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00001813/// '(' declarator ')'
1814/// [GNU] '(' attributes declarator ')'
1815/// [C90] direct-declarator '[' constant-expression[opt] ']'
1816/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1817/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1818/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1819/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1820/// direct-declarator '(' parameter-type-list ')'
1821/// direct-declarator '(' identifier-list[opt] ')'
1822/// [GNU] direct-declarator '(' parameter-forward-declarations
1823/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001824/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1825/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00001826/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001827///
1828/// declarator-id: [C++ 8]
1829/// id-expression
1830/// '::'[opt] nested-name-specifier[opt] type-name
1831///
1832/// id-expression: [C++ 5.1]
1833/// unqualified-id
1834/// qualified-id [TODO]
1835///
1836/// unqualified-id: [C++ 5.1]
1837/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001838/// operator-function-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001839/// conversion-function-id [TODO]
1840/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00001841/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001842///
Reid Spencer5f016e22007-07-11 17:01:13 +00001843void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001844 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001845
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001846 if (getLang().CPlusPlus) {
1847 if (D.mayHaveIdentifier()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001848 // ParseDeclaratorInternal might already have parsed the scope.
1849 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1850 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001851 if (afterCXXScope) {
1852 // Change the declaration context for name lookup, until this function
1853 // is exited (and the declarator has been parsed).
1854 DeclScopeObj.EnterDeclaratorScope();
1855 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001856
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001857 if (Tok.is(tok::identifier)) {
1858 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001859
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001860 // If this identifier is the name of the current class, it's a
1861 // constructor name.
Douglas Gregor39a8de12009-02-25 19:37:18 +00001862 if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)){
Steve Naroffb43a50f2009-01-28 19:39:02 +00001863 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorb696ea32009-02-04 17:00:24 +00001864 Tok.getLocation(), CurScope),
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001865 Tok.getLocation());
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001866 // This is a normal identifier.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001867 } else
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001868 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1869 ConsumeToken();
1870 goto PastIdentifier;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001871 } else if (Tok.is(tok::annot_template_id)) {
1872 TemplateIdAnnotation *TemplateId
1873 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1874
1875 // FIXME: Could this template-id name a constructor?
1876
1877 // FIXME: This is an egregious hack, where we silently ignore
1878 // the specialization (which should be a function template
1879 // specialization name) and use the name instead. This hack
1880 // will go away when we have support for function
1881 // specializations.
1882 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
1883 TemplateId->Destroy();
1884 ConsumeToken();
1885 goto PastIdentifier;
Douglas Gregor70316a02008-12-26 15:00:45 +00001886 } else if (Tok.is(tok::kw_operator)) {
1887 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001888 SourceLocation EndLoc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001889
Douglas Gregor70316a02008-12-26 15:00:45 +00001890 // First try the name of an overloaded operator
Sebastian Redlab197ba2009-02-09 18:23:29 +00001891 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
1892 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor70316a02008-12-26 15:00:45 +00001893 } else {
1894 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redlab197ba2009-02-09 18:23:29 +00001895 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
1896 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
1897 else {
Douglas Gregor70316a02008-12-26 15:00:45 +00001898 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redlab197ba2009-02-09 18:23:29 +00001899 }
Douglas Gregor70316a02008-12-26 15:00:45 +00001900 }
1901 goto PastIdentifier;
1902 } else if (Tok.is(tok::tilde)) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001903 // This should be a C++ destructor.
1904 SourceLocation TildeLoc = ConsumeToken();
1905 if (Tok.is(tok::identifier)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00001906 // FIXME: Inaccurate.
1907 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7f43d672009-02-25 23:52:28 +00001908 SourceLocation EndLoc;
Douglas Gregor31a19b62009-04-01 21:51:26 +00001909 TypeResult Type = ParseClassName(EndLoc);
1910 if (Type.isInvalid())
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001911 D.SetIdentifier(0, TildeLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001912 else
1913 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001914 } else {
1915 Diag(Tok, diag::err_expected_class_name);
1916 D.SetIdentifier(0, TildeLoc);
1917 }
1918 goto PastIdentifier;
1919 }
1920
1921 // If we reached this point, token is not identifier and not '~'.
1922
1923 if (afterCXXScope) {
1924 Diag(Tok, diag::err_expected_unqualified_id);
1925 D.SetIdentifier(0, Tok.getLocation());
1926 D.setInvalidType(true);
1927 goto PastIdentifier;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001928 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001929 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001930 }
1931
1932 // If we reached this point, we are either in C/ObjC or the token didn't
1933 // satisfy any of the C++-specific checks.
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001934 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1935 assert(!getLang().CPlusPlus &&
1936 "There's a C++-specific check for tok::identifier above");
1937 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1938 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1939 ConsumeToken();
1940 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001941 // direct-declarator: '(' declarator ')'
1942 // direct-declarator: '(' attributes declarator ')'
1943 // Example: 'char (*X)' or 'int (*XX)(void)'
1944 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001945 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001946 // This could be something simple like "int" (in which case the declarator
1947 // portion is empty), if an abstract-declarator is allowed.
1948 D.SetIdentifier(0, Tok.getLocation());
1949 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00001950 if (D.getContext() == Declarator::MemberContext)
1951 Diag(Tok, diag::err_expected_member_name_or_semi)
1952 << D.getDeclSpec().getSourceRange();
1953 else if (getLang().CPlusPlus)
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001954 Diag(Tok, diag::err_expected_unqualified_id);
1955 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00001956 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00001957 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00001958 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001959 }
1960
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00001961 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00001962 assert(D.isPastIdentifier() &&
1963 "Haven't past the location of the identifier yet?");
1964
1965 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00001966 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001967 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1968 // In such a case, check if we actually have a function declarator; if it
1969 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00001970 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1971 // When not in file scope, warn for ambiguous function declarators, just
1972 // in case the author intended it as a variable definition.
1973 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1974 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1975 break;
1976 }
Chris Lattneref4715c2008-04-06 05:45:57 +00001977 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00001978 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001979 ParseBracketDeclarator(D);
1980 } else {
1981 break;
1982 }
1983 }
1984}
1985
Chris Lattneref4715c2008-04-06 05:45:57 +00001986/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1987/// only called before the identifier, so these are most likely just grouping
1988/// parens for precedence. If we find that these are actually function
1989/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1990///
1991/// direct-declarator:
1992/// '(' declarator ')'
1993/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00001994/// direct-declarator '(' parameter-type-list ')'
1995/// direct-declarator '(' identifier-list[opt] ')'
1996/// [GNU] direct-declarator '(' parameter-forward-declarations
1997/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00001998///
1999void Parser::ParseParenDeclarator(Declarator &D) {
2000 SourceLocation StartLoc = ConsumeParen();
2001 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
2002
Chris Lattner7399ee02008-10-20 02:05:46 +00002003 // Eat any attributes before we look at whether this is a grouping or function
2004 // declarator paren. If this is a grouping paren, the attribute applies to
2005 // the type being built up, for example:
2006 // int (__attribute__(()) *x)(long y)
2007 // If this ends up not being a grouping paren, the attribute applies to the
2008 // first argument, for example:
2009 // int (__attribute__(()) int x)
2010 // In either case, we need to eat any attributes to be able to determine what
2011 // sort of paren this is.
2012 //
2013 AttributeList *AttrList = 0;
2014 bool RequiresArg = false;
2015 if (Tok.is(tok::kw___attribute)) {
2016 AttrList = ParseAttributes();
2017
2018 // We require that the argument list (if this is a non-grouping paren) be
2019 // present even if the attribute list was empty.
2020 RequiresArg = true;
2021 }
Steve Naroff239f0732008-12-25 14:16:32 +00002022 // Eat any Microsoft extensions.
Douglas Gregor5a2f5d32009-01-10 00:48:18 +00002023 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2024 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroff239f0732008-12-25 14:16:32 +00002025 ConsumeToken();
Chris Lattner7399ee02008-10-20 02:05:46 +00002026
Chris Lattneref4715c2008-04-06 05:45:57 +00002027 // If we haven't past the identifier yet (or where the identifier would be
2028 // stored, if this is an abstract declarator), then this is probably just
2029 // grouping parens. However, if this could be an abstract-declarator, then
2030 // this could also be the start of function arguments (consider 'void()').
2031 bool isGrouping;
2032
2033 if (!D.mayOmitIdentifier()) {
2034 // If this can't be an abstract-declarator, this *must* be a grouping
2035 // paren, because we haven't seen the identifier yet.
2036 isGrouping = true;
2037 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002038 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002039 isDeclarationSpecifier()) { // 'int(int)' is a function.
2040 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2041 // considered to be a type, not a K&R identifier-list.
2042 isGrouping = false;
2043 } else {
2044 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2045 isGrouping = true;
2046 }
2047
2048 // If this is a grouping paren, handle:
2049 // direct-declarator: '(' declarator ')'
2050 // direct-declarator: '(' attributes declarator ')'
2051 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002052 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002053 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002054 if (AttrList)
Sebastian Redlab197ba2009-02-09 18:23:29 +00002055 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002056
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002057 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002058 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002059 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002060
2061 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002062 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002063 return;
2064 }
2065
2066 // Okay, if this wasn't a grouping paren, it must be the start of a function
2067 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002068 // identifier (and remember where it would have been), then call into
2069 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002070 D.SetIdentifier(0, Tok.getLocation());
2071
Chris Lattner7399ee02008-10-20 02:05:46 +00002072 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002073}
2074
2075/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2076/// declarator D up to a paren, which indicates that we are parsing function
2077/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002078///
Chris Lattner7399ee02008-10-20 02:05:46 +00002079/// If AttrList is non-null, then the caller parsed those arguments immediately
2080/// after the open paren - they should be considered to be the first argument of
2081/// a parameter. If RequiresArg is true, then the first argument of the
2082/// function is required to be present and required to not be an identifier
2083/// list.
2084///
Reid Spencer5f016e22007-07-11 17:01:13 +00002085/// This method also handles this portion of the grammar:
2086/// parameter-type-list: [C99 6.7.5]
2087/// parameter-list
2088/// parameter-list ',' '...'
2089///
2090/// parameter-list: [C99 6.7.5]
2091/// parameter-declaration
2092/// parameter-list ',' parameter-declaration
2093///
2094/// parameter-declaration: [C99 6.7.5]
2095/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002096/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002097/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002098/// declaration-specifiers abstract-declarator[opt]
2099/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002100/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002101/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2102///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002103/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002104/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002105///
Chris Lattner7399ee02008-10-20 02:05:46 +00002106void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2107 AttributeList *AttrList,
2108 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002109 // lparen is already consumed!
2110 assert(D.isPastIdentifier() && "Should not call before identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00002111
Chris Lattner7399ee02008-10-20 02:05:46 +00002112 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002113 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002114 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002115 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002116 delete AttrList;
2117 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002118
Sebastian Redlab197ba2009-02-09 18:23:29 +00002119 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002120
2121 // cv-qualifier-seq[opt].
2122 DeclSpec DS;
2123 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002124 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002125 if (!DS.getSourceRange().getEnd().isInvalid())
2126 Loc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002127
2128 // Parse exception-specification[opt].
2129 if (Tok.is(tok::kw_throw))
Sebastian Redlab197ba2009-02-09 18:23:29 +00002130 ParseExceptionSpecification(Loc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002131 }
2132
Chris Lattnerf97409f2008-04-06 06:57:35 +00002133 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002134 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002135 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002136 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002137 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002138 /*arglist*/ 0, 0,
2139 DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002140 LParenLoc, D),
2141 Loc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002142 return;
Chris Lattner7399ee02008-10-20 02:05:46 +00002143 }
2144
2145 // Alternatively, this parameter list may be an identifier list form for a
2146 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00002147 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00002148 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002149 // K&R identifier lists can't have typedefs as identifiers, per
2150 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002151 if (RequiresArg) {
2152 Diag(Tok, diag::err_argument_required_after_attribute);
2153 delete AttrList;
2154 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002155 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2156 // normal declarators, not for abstract-declarators.
2157 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002158 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002159 }
2160
2161 // Finally, a normal, non-empty parameter type list.
2162
2163 // Build up an array of information about the parsed arguments.
2164 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002165
2166 // Enter function-declaration scope, limiting any declarators to the
2167 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002168 ParseScope PrototypeScope(this,
2169 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002170
2171 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002172 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002173 while (1) {
2174 if (Tok.is(tok::ellipsis)) {
2175 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002176 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002177 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002178 }
2179
Chris Lattnerf97409f2008-04-06 06:57:35 +00002180 SourceLocation DSStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00002181
Chris Lattnerf97409f2008-04-06 06:57:35 +00002182 // Parse the declaration-specifiers.
2183 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002184
2185 // If the caller parsed attributes for the first argument, add them now.
2186 if (AttrList) {
2187 DS.AddAttributes(AttrList);
2188 AttrList = 0; // Only apply the attributes to the first parameter.
2189 }
Chris Lattnere64c5492009-02-27 18:38:20 +00002190 ParseDeclarationSpecifiers(DS);
2191
Chris Lattnerf97409f2008-04-06 06:57:35 +00002192 // Parse the declarator. This is "PrototypeContext", because we must
2193 // accept either 'declarator' or 'abstract-declarator' here.
2194 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2195 ParseDeclarator(ParmDecl);
2196
2197 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002198 if (Tok.is(tok::kw___attribute)) {
2199 SourceLocation Loc;
2200 AttributeList *AttrList = ParseAttributes(&Loc);
2201 ParmDecl.AddAttributes(AttrList, Loc);
2202 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002203
Chris Lattnerf97409f2008-04-06 06:57:35 +00002204 // Remember this parsed parameter in ParamInfo.
2205 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2206
Douglas Gregor72b505b2008-12-16 21:30:33 +00002207 // DefArgToks is used when the parsing of default arguments needs
2208 // to be delayed.
2209 CachedTokens *DefArgToks = 0;
2210
Chris Lattnerf97409f2008-04-06 06:57:35 +00002211 // If no parameter was specified, verify that *something* was specified,
2212 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00002213 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2214 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002215 // Completely missing, emit error.
2216 Diag(DSStart, diag::err_missing_param);
2217 } else {
2218 // Otherwise, we have something. Add it and let semantic analysis try
2219 // to grok it and add the result to the ParamInfo we are building.
2220
2221 // Inform the actions module about the parameter declarator, so it gets
2222 // added to the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002223 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00002224
2225 // Parse the default argument, if any. We parse the default
2226 // arguments in all dialects; the semantic analysis in
2227 // ActOnParamDefaultArgument will reject the default argument in
2228 // C.
2229 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002230 SourceLocation EqualLoc = Tok.getLocation();
2231
Chris Lattner04421082008-04-08 04:40:51 +00002232 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002233 if (D.getContext() == Declarator::MemberContext) {
2234 // If we're inside a class definition, cache the tokens
2235 // corresponding to the default argument. We'll actually parse
2236 // them when we see the end of the class definition.
2237 // FIXME: Templates will require something similar.
2238 // FIXME: Can we use a smart pointer for Toks?
2239 DefArgToks = new CachedTokens;
2240
2241 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2242 tok::semi, false)) {
2243 delete DefArgToks;
2244 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002245 Actions.ActOnParamDefaultArgumentError(Param);
2246 } else
2247 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner04421082008-04-08 04:40:51 +00002248 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002249 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002250 ConsumeToken();
Douglas Gregor72b505b2008-12-16 21:30:33 +00002251
2252 OwningExprResult DefArgResult(ParseAssignmentExpression());
2253 if (DefArgResult.isInvalid()) {
2254 Actions.ActOnParamDefaultArgumentError(Param);
2255 SkipUntil(tok::comma, tok::r_paren, true, true);
2256 } else {
2257 // Inform the actions module about the default argument
2258 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002259 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00002260 }
Chris Lattner04421082008-04-08 04:40:51 +00002261 }
2262 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002263
2264 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002265 ParmDecl.getIdentifierLoc(), Param,
2266 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002267 }
2268
2269 // If the next token is a comma, consume it and keep reading arguments.
2270 if (Tok.isNot(tok::comma)) break;
2271
2272 // Consume the comma.
2273 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002274 }
2275
Chris Lattnerf97409f2008-04-06 06:57:35 +00002276 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002277 PrototypeScope.Exit();
Chris Lattnerf97409f2008-04-06 06:57:35 +00002278
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002279 // If we have the closing ')', eat it.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002280 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002281
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002282 DeclSpec DS;
2283 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002284 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002285 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002286 if (!DS.getSourceRange().getEnd().isInvalid())
2287 Loc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002288
2289 // Parse exception-specification[opt].
2290 if (Tok.is(tok::kw_throw))
Sebastian Redlab197ba2009-02-09 18:23:29 +00002291 ParseExceptionSpecification(Loc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002292 }
2293
Reid Spencer5f016e22007-07-11 17:01:13 +00002294 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002295 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002296 EllipsisLoc,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002297 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002298 DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002299 LParenLoc, D),
2300 Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002301}
2302
Chris Lattner66d28652008-04-06 06:34:08 +00002303/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2304/// we found a K&R-style identifier list instead of a type argument list. The
2305/// current token is known to be the first identifier in the list.
2306///
2307/// identifier-list: [C99 6.7.5]
2308/// identifier
2309/// identifier-list ',' identifier
2310///
2311void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2312 Declarator &D) {
2313 // Build up an array of information about the parsed arguments.
2314 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2315 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2316
2317 // If there was no identifier specified for the declarator, either we are in
2318 // an abstract-declarator, or we are in a parameter declarator which was found
2319 // to be abstract. In abstract-declarators, identifier lists are not valid:
2320 // diagnose this.
2321 if (!D.getIdentifier())
2322 Diag(Tok, diag::ext_ident_list_in_param);
2323
2324 // Tok is known to be the first identifier in the list. Remember this
2325 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002326 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002327 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattnerb28317a2009-03-28 19:18:32 +00002328 Tok.getLocation(),
2329 DeclPtrTy()));
Chris Lattner66d28652008-04-06 06:34:08 +00002330
Chris Lattner50c64772008-04-06 06:39:19 +00002331 ConsumeToken(); // eat the first identifier.
Chris Lattner66d28652008-04-06 06:34:08 +00002332
2333 while (Tok.is(tok::comma)) {
2334 // Eat the comma.
2335 ConsumeToken();
2336
Chris Lattner50c64772008-04-06 06:39:19 +00002337 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002338 if (Tok.isNot(tok::identifier)) {
2339 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002340 SkipUntil(tok::r_paren);
2341 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002342 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002343
Chris Lattner66d28652008-04-06 06:34:08 +00002344 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002345
2346 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00002347 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002348 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner66d28652008-04-06 06:34:08 +00002349
2350 // Verify that the argument identifier has not already been mentioned.
2351 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002352 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002353 } else {
2354 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002355 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002356 Tok.getLocation(),
2357 DeclPtrTy()));
Chris Lattner50c64772008-04-06 06:39:19 +00002358 }
Chris Lattner66d28652008-04-06 06:34:08 +00002359
2360 // Eat the identifier.
2361 ConsumeToken();
2362 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002363
2364 // If we have the closing ')', eat it and we're done.
2365 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2366
Chris Lattner50c64772008-04-06 06:39:19 +00002367 // Remember that we parsed a function type, and remember the attributes. This
2368 // function type is always a K&R style function type, which is not varargs and
2369 // has no prototype.
2370 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002371 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00002372 &ParamInfo[0], ParamInfo.size(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002373 /*TypeQuals*/0, LParenLoc, D),
2374 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002375}
Chris Lattneref4715c2008-04-06 05:45:57 +00002376
Reid Spencer5f016e22007-07-11 17:01:13 +00002377/// [C90] direct-declarator '[' constant-expression[opt] ']'
2378/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2379/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2380/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2381/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2382void Parser::ParseBracketDeclarator(Declarator &D) {
2383 SourceLocation StartLoc = ConsumeBracket();
2384
Chris Lattner378c7e42008-12-18 07:27:21 +00002385 // C array syntax has many features, but by-far the most common is [] and [4].
2386 // This code does a fast path to handle some of the most obvious cases.
2387 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002388 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002389 // Remember that we parsed the empty array type.
2390 OwningExprResult NumElements(Actions);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002391 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2392 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002393 return;
2394 } else if (Tok.getKind() == tok::numeric_constant &&
2395 GetLookAheadToken(1).is(tok::r_square)) {
2396 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002397 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002398 ConsumeToken();
2399
Sebastian Redlab197ba2009-02-09 18:23:29 +00002400 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002401
2402 // If there was an error parsing the assignment-expression, recover.
2403 if (ExprRes.isInvalid())
2404 ExprRes.release(); // Deallocate expr, just use [].
2405
2406 // Remember that we parsed a array type, and remember its features.
2407 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002408 ExprRes.release(), StartLoc),
2409 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002410 return;
2411 }
2412
Reid Spencer5f016e22007-07-11 17:01:13 +00002413 // If valid, this location is the position where we read the 'static' keyword.
2414 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002415 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002416 StaticLoc = ConsumeToken();
2417
2418 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002419 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002420 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002421 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Reid Spencer5f016e22007-07-11 17:01:13 +00002422
2423 // If we haven't already read 'static', check to see if there is one after the
2424 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002425 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002426 StaticLoc = ConsumeToken();
2427
2428 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2429 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002430 OwningExprResult NumElements(Actions);
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002431
2432 // Handle the case where we have '[*]' as the array size. However, a leading
2433 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2434 // the the token after the star is a ']'. Since stars in arrays are
2435 // infrequent, use of lookahead is not costly here.
2436 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002437 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002438
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002439 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002440 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002441 StaticLoc = SourceLocation(); // Drop the static.
2442 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002443 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002444 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002445 // Note, in C89, this production uses the constant-expr production instead
2446 // of assignment-expr. The only difference is that assignment-expr allows
2447 // things like '=' and '*='. Sema rejects these in C89 mode because they
2448 // are not i-c-e's, so we don't need to distinguish between the two here.
2449
Reid Spencer5f016e22007-07-11 17:01:13 +00002450 // Parse the assignment-expression now.
2451 NumElements = ParseAssignmentExpression();
2452 }
2453
2454 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002455 if (NumElements.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002456 // If the expression was invalid, skip it.
2457 SkipUntil(tok::r_square);
2458 return;
2459 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002460
2461 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2462
Chris Lattner378c7e42008-12-18 07:27:21 +00002463 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00002464 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2465 StaticLoc.isValid(), isStar,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002466 NumElements.release(), StartLoc),
2467 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002468}
2469
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002470/// [GNU] typeof-specifier:
2471/// typeof ( expressions )
2472/// typeof ( type-name )
2473/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00002474///
2475void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00002476 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002477 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffd1861fd2007-07-31 12:34:36 +00002478 SourceLocation StartLoc = ConsumeToken();
2479
Chris Lattner04d66662007-10-09 17:33:22 +00002480 if (Tok.isNot(tok::l_paren)) {
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002481 if (!getLang().CPlusPlus) {
Chris Lattner08631c52008-11-23 21:45:46 +00002482 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002483 return;
2484 }
2485
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002486 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor809070a2009-02-18 17:45:20 +00002487 if (Result.isInvalid()) {
2488 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002489 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00002490 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002491
2492 const char *PrevSpec = 0;
2493 // Check for duplicate type specifiers.
2494 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002495 Result.release()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002496 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002497
2498 // FIXME: Not accurate, the range gets one token more than it should.
2499 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002500 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002501 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002502
Steve Naroffd1861fd2007-07-31 12:34:36 +00002503 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2504
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00002505 if (isTypeIdInParens()) {
Douglas Gregor809070a2009-02-18 17:45:20 +00002506 Action::TypeResult Ty = ParseTypeName();
Steve Naroffd1861fd2007-07-31 12:34:36 +00002507
Douglas Gregor809070a2009-02-18 17:45:20 +00002508 assert((Ty.isInvalid() || Ty.get()) &&
2509 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002510
Chris Lattner04d66662007-10-09 17:33:22 +00002511 if (Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002512 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002513 return;
2514 }
2515 RParenLoc = ConsumeParen();
Douglas Gregor809070a2009-02-18 17:45:20 +00002516
2517 if (Ty.isInvalid())
2518 DS.SetTypeSpecError();
2519 else {
2520 const char *PrevSpec = 0;
2521 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2522 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2523 Ty.get()))
2524 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2525 }
Steve Naroffd1861fd2007-07-31 12:34:36 +00002526 } else { // we have an expression.
Sebastian Redl2f7ece72008-12-11 21:36:32 +00002527 OwningExprResult Result(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002528
2529 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00002530 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor809070a2009-02-18 17:45:20 +00002531 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002532 return;
2533 }
2534 RParenLoc = ConsumeParen();
2535 const char *PrevSpec = 0;
2536 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2537 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redleffa8d12008-12-10 00:02:53 +00002538 Result.release()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002539 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002540 }
Argyrios Kyrtzidis0919f9e2008-08-16 10:21:33 +00002541 DS.SetRangeEnd(RParenLoc);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002542}
2543
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00002544