blob: a80f57c8ca26fca5f392477a4dbcac7be16996f6 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner545f39e2009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattnera7549902007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerdaa5c002008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "llvm/ADT/SmallSet.h"
19using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// C99 6.7: Declarations.
23//===----------------------------------------------------------------------===//
24
25/// ParseTypeName
26/// type-name: [C99 6.7.6]
27/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +000028///
29/// Called type-id in C++.
Sebastian Redlaaacda92009-05-29 18:02:33 +000030Action::TypeResult Parser::ParseTypeName(SourceRange *Range) {
Chris Lattner4b009652007-07-25 00:24:17 +000031 // Parse the common declaration-specifiers piece.
32 DeclSpec DS;
33 ParseSpecifierQualifierList(DS);
Sebastian Redlaaacda92009-05-29 18:02:33 +000034
Chris Lattner4b009652007-07-25 00:24:17 +000035 // Parse the abstract-declarator, if present.
36 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
37 ParseDeclarator(DeclaratorInfo);
Sebastian Redlaaacda92009-05-29 18:02:33 +000038 if (Range)
39 *Range = DeclaratorInfo.getSourceRange();
40
Chris Lattner34c61332009-04-25 08:06:05 +000041 if (DeclaratorInfo.isInvalidType())
Douglas Gregor6c0f4062009-02-18 17:45:20 +000042 return true;
43
44 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Chris Lattner4b009652007-07-25 00:24:17 +000045}
46
47/// ParseAttributes - Parse a non-empty attributes list.
48///
49/// [GNU] attributes:
50/// attribute
51/// attributes attribute
52///
53/// [GNU] attribute:
54/// '__attribute__' '(' '(' attribute-list ')' ')'
55///
56/// [GNU] attribute-list:
57/// attrib
58/// attribute_list ',' attrib
59///
60/// [GNU] attrib:
61/// empty
62/// attrib-name
63/// attrib-name '(' identifier ')'
64/// attrib-name '(' identifier ',' nonempty-expr-list ')'
65/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
66///
67/// [GNU] attrib-name:
68/// identifier
69/// typespec
70/// typequal
71/// storageclass
72///
73/// FIXME: The GCC grammar/code for this construct implies we need two
74/// token lookahead. Comment from gcc: "If they start with an identifier
75/// which is followed by a comma or close parenthesis, then the arguments
76/// start with that identifier; otherwise they are an expression list."
77///
78/// At the moment, I am not doing 2 token lookahead. I am also unaware of
79/// any attributes that don't work (based on my limited testing). Most
80/// attributes are very simple in practice. Until we find a bug, I don't see
81/// a pressing need to implement the 2 token lookahead.
82
Sebastian Redl0c986032009-02-09 18:23:29 +000083AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner34a01ad2007-10-09 17:33:22 +000084 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Chris Lattner4b009652007-07-25 00:24:17 +000085
86 AttributeList *CurrAttr = 0;
87
Chris Lattner34a01ad2007-10-09 17:33:22 +000088 while (Tok.is(tok::kw___attribute)) {
Chris Lattner4b009652007-07-25 00:24:17 +000089 ConsumeToken();
90 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
91 "attribute")) {
92 SkipUntil(tok::r_paren, true); // skip until ) or ;
93 return CurrAttr;
94 }
95 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
96 SkipUntil(tok::r_paren, true); // skip until ) or ;
97 return CurrAttr;
98 }
99 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner34a01ad2007-10-09 17:33:22 +0000100 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
101 Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000102
Chris Lattner34a01ad2007-10-09 17:33:22 +0000103 if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000104 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
105 ConsumeToken();
106 continue;
107 }
108 // we have an identifier or declaration specifier (const, int, etc.)
109 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
110 SourceLocation AttrNameLoc = ConsumeToken();
111
112 // check if we have a "paramterized" attribute
Chris Lattner34a01ad2007-10-09 17:33:22 +0000113 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000114 ConsumeParen(); // ignore the left paren loc for now
115
Chris Lattner34a01ad2007-10-09 17:33:22 +0000116 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000117 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
118 SourceLocation ParmLoc = ConsumeToken();
119
Chris Lattner34a01ad2007-10-09 17:33:22 +0000120 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000121 // __attribute__(( mode(byte) ))
122 ConsumeParen(); // ignore the right paren loc for now
123 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
124 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000125 } else if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000126 ConsumeToken();
127 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000128 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000129 bool ArgExprsOk = true;
130
131 // now parse the non-empty comma separated list of expressions
132 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000133 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000134 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000135 ArgExprsOk = false;
136 SkipUntil(tok::r_paren);
137 break;
138 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000139 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000140 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000141 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000142 break;
143 ConsumeToken(); // Eat the comma, move to the next argument
144 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000145 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000146 ConsumeParen(); // ignore the right paren loc for now
147 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redl6008ac32008-11-25 22:21:31 +0000148 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Chris Lattner4b009652007-07-25 00:24:17 +0000149 }
150 }
151 } else { // not an identifier
Nate Begeman60702162009-06-26 06:32:41 +0000152 switch (Tok.getKind()) {
153 case tok::r_paren:
Chris Lattner4b009652007-07-25 00:24:17 +0000154 // parse a possibly empty comma separated list of expressions
Chris Lattner4b009652007-07-25 00:24:17 +0000155 // __attribute__(( nonnull() ))
156 ConsumeParen(); // ignore the right paren loc for now
157 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
158 0, SourceLocation(), 0, 0, CurrAttr);
Nate Begeman60702162009-06-26 06:32:41 +0000159 break;
160 case tok::kw_char:
161 case tok::kw_wchar_t:
Alisdair Meredith2bcacb62009-07-14 06:30:34 +0000162 case tok::kw_char16_t:
163 case tok::kw_char32_t:
Nate Begeman60702162009-06-26 06:32:41 +0000164 case tok::kw_bool:
165 case tok::kw_short:
166 case tok::kw_int:
167 case tok::kw_long:
168 case tok::kw_signed:
169 case tok::kw_unsigned:
170 case tok::kw_float:
171 case tok::kw_double:
172 case tok::kw_void:
173 case tok::kw_typeof:
174 // If it's a builtin type name, eat it and expect a rparen
175 // __attribute__(( vec_type_hint(char) ))
176 ConsumeToken();
177 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
178 0, SourceLocation(), 0, 0, CurrAttr);
179 if (Tok.is(tok::r_paren))
180 ConsumeParen();
181 break;
182 default:
Chris Lattner4b009652007-07-25 00:24:17 +0000183 // __attribute__(( aligned(16) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000184 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000185 bool ArgExprsOk = true;
186
187 // now parse the list of expressions
188 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000189 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000190 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000191 ArgExprsOk = false;
192 SkipUntil(tok::r_paren);
193 break;
194 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000195 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000196 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000197 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000198 break;
199 ConsumeToken(); // Eat the comma, move to the next argument
200 }
201 // Match the ')'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000202 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000203 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl6008ac32008-11-25 22:21:31 +0000204 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
205 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Chris Lattner4b009652007-07-25 00:24:17 +0000206 CurrAttr);
207 }
Nate Begeman60702162009-06-26 06:32:41 +0000208 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000209 }
210 }
211 } else {
212 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
213 0, SourceLocation(), 0, 0, CurrAttr);
214 }
215 }
216 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Chris Lattner4b009652007-07-25 00:24:17 +0000217 SkipUntil(tok::r_paren, false);
Sebastian Redl0c986032009-02-09 18:23:29 +0000218 SourceLocation Loc = Tok.getLocation();;
219 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
220 SkipUntil(tok::r_paren, false);
221 }
222 if (EndLoc)
223 *EndLoc = Loc;
Chris Lattner4b009652007-07-25 00:24:17 +0000224 }
225 return CurrAttr;
226}
227
Eli Friedmancd231842009-06-08 07:21:15 +0000228/// ParseMicrosoftDeclSpec - Parse an __declspec construct
229///
230/// [MS] decl-specifier:
231/// __declspec ( extended-decl-modifier-seq )
232///
233/// [MS] extended-decl-modifier-seq:
234/// extended-decl-modifier[opt]
235/// extended-decl-modifier extended-decl-modifier-seq
236
Eli Friedman891d82f2009-06-08 23:27:34 +0000237AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000238 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmancd231842009-06-08 07:21:15 +0000239
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000240 ConsumeToken();
Eli Friedmancd231842009-06-08 07:21:15 +0000241 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
242 "declspec")) {
243 SkipUntil(tok::r_paren, true); // skip until ) or ;
244 return CurrAttr;
245 }
Eli Friedman891d82f2009-06-08 23:27:34 +0000246 while (Tok.getIdentifierInfo()) {
Eli Friedmancd231842009-06-08 07:21:15 +0000247 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
248 SourceLocation AttrNameLoc = ConsumeToken();
249 if (Tok.is(tok::l_paren)) {
250 ConsumeParen();
251 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
252 // correctly.
253 OwningExprResult ArgExpr(ParseAssignmentExpression());
254 if (!ArgExpr.isInvalid()) {
255 ExprTy* ExprList = ArgExpr.take();
256 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
257 SourceLocation(), &ExprList, 1,
258 CurrAttr, true);
259 }
260 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
261 SkipUntil(tok::r_paren, false);
262 } else {
263 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, SourceLocation(),
264 0, 0, CurrAttr, true);
265 }
266 }
267 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
268 SkipUntil(tok::r_paren, false);
Eli Friedman891d82f2009-06-08 23:27:34 +0000269 return CurrAttr;
270}
271
272AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) {
273 // Treat these like attributes
274 // FIXME: Allow Sema to distinguish between these and real attributes!
275 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
276 Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___ptr64) ||
277 Tok.is(tok::kw___w64)) {
278 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
279 SourceLocation AttrNameLoc = ConsumeToken();
280 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
281 // FIXME: Support these properly!
282 continue;
283 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
284 SourceLocation(), 0, 0, CurrAttr, true);
285 }
286 return CurrAttr;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000287}
288
Chris Lattner4b009652007-07-25 00:24:17 +0000289/// ParseDeclaration - Parse a full 'declaration', which consists of
290/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000291/// 'Context' should be a Declarator::TheContext value. This returns the
292/// location of the semicolon in DeclEnd.
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000293///
294/// declaration: [C99 6.7]
295/// block-declaration ->
296/// simple-declaration
297/// others [FIXME]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000298/// [C++] template-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000299/// [C++] namespace-definition
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000300/// [C++] using-directive
Douglas Gregorcad27f62009-06-22 23:06:13 +0000301/// [C++] using-declaration
Sebastian Redla8cecf62009-03-24 22:27:57 +0000302/// [C++0x] static_assert-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000303/// others... [FIXME]
304///
Chris Lattner9802a0a2009-04-02 04:16:50 +0000305Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
306 SourceLocation &DeclEnd) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000307 DeclPtrTy SingleDecl;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000308 switch (Tok.getKind()) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000309 case tok::kw_template:
Douglas Gregore3298aa2009-05-12 21:31:51 +0000310 case tok::kw_export:
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000311 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000312 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000313 case tok::kw_namespace:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000314 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000315 break;
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000316 case tok::kw_using:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000317 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000318 break;
Anders Carlssonab041982009-03-11 16:27:10 +0000319 case tok::kw_static_assert:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000320 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000321 break;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000322 default:
Chris Lattner9802a0a2009-04-02 04:16:50 +0000323 return ParseSimpleDeclaration(Context, DeclEnd);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000324 }
Chris Lattnera17991f2009-03-29 16:50:03 +0000325
326 // This routine returns a DeclGroup, if the thing we parsed only contains a
327 // single decl, convert it now.
328 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000329}
330
331/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
332/// declaration-specifiers init-declarator-list[opt] ';'
333///[C90/C++]init-declarator-list ';' [TODO]
334/// [OMP] threadprivate-directive [TODO]
Chris Lattnerf8016042009-03-29 17:27:48 +0000335///
336/// If RequireSemi is false, this does not check for a ';' at the end of the
337/// declaration.
338Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Chris Lattner9802a0a2009-04-02 04:16:50 +0000339 SourceLocation &DeclEnd,
Chris Lattnerf8016042009-03-29 17:27:48 +0000340 bool RequireSemi) {
Chris Lattner4b009652007-07-25 00:24:17 +0000341 // Parse the common declaration-specifiers piece.
342 DeclSpec DS;
343 ParseDeclarationSpecifiers(DS);
344
345 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
346 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000347 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000348 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000349 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
350 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000351 }
352
353 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
354 ParseDeclarator(DeclaratorInfo);
355
Chris Lattner2c41d482009-03-29 17:18:04 +0000356 DeclGroupPtrTy DG =
357 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattnerf8016042009-03-29 17:27:48 +0000358
Chris Lattner9802a0a2009-04-02 04:16:50 +0000359 DeclEnd = Tok.getLocation();
360
Chris Lattnerf8016042009-03-29 17:27:48 +0000361 // If the client wants to check what comes after the declaration, just return
362 // immediately without checking anything!
363 if (!RequireSemi) return DG;
Chris Lattner2c41d482009-03-29 17:18:04 +0000364
365 if (Tok.is(tok::semi)) {
366 ConsumeToken();
Chris Lattner2c41d482009-03-29 17:18:04 +0000367 return DG;
368 }
369
John McCallfcb32f42009-07-31 02:20:35 +0000370 Diag(Tok, diag::err_expected_semi_declaration);
Chris Lattner2c41d482009-03-29 17:18:04 +0000371 // Skip to end of block or statement
372 SkipUntil(tok::r_brace, true, true);
373 if (Tok.is(tok::semi))
374 ConsumeToken();
375 return DG;
Chris Lattner4b009652007-07-25 00:24:17 +0000376}
377
Douglas Gregore3298aa2009-05-12 21:31:51 +0000378/// \brief Parse 'declaration' after parsing 'declaration-specifiers
379/// declarator'. This method parses the remainder of the declaration
380/// (including any attributes or initializer, among other things) and
381/// finalizes the declaration.
Chris Lattner4b009652007-07-25 00:24:17 +0000382///
Chris Lattner4b009652007-07-25 00:24:17 +0000383/// init-declarator: [C99 6.7]
384/// declarator
385/// declarator '=' initializer
386/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
387/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000388/// [C++] declarator initializer[opt]
389///
390/// [C++] initializer:
391/// [C++] '=' initializer-clause
392/// [C++] '(' expression-list ')'
Sebastian Redla8cecf62009-03-24 22:27:57 +0000393/// [C++0x] '=' 'default' [TODO]
394/// [C++0x] '=' 'delete'
395///
396/// According to the standard grammar, =default and =delete are function
397/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattner4b009652007-07-25 00:24:17 +0000398///
Douglas Gregor2ae1d772009-06-23 23:11:28 +0000399Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
400 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregore3298aa2009-05-12 21:31:51 +0000401 // If a simple-asm-expr is present, parse it.
402 if (Tok.is(tok::kw_asm)) {
403 SourceLocation Loc;
404 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
405 if (AsmLabel.isInvalid()) {
406 SkipUntil(tok::semi, true, true);
407 return DeclPtrTy();
408 }
409
410 D.setAsmLabel(AsmLabel.release());
411 D.SetRangeEnd(Loc);
412 }
413
414 // If attributes are present, parse them.
415 if (Tok.is(tok::kw___attribute)) {
416 SourceLocation Loc;
417 AttributeList *AttrList = ParseAttributes(&Loc);
418 D.AddAttributes(AttrList, Loc);
419 }
420
421 // Inform the current actions module that we just parsed this declarator.
Douglas Gregor2ae1d772009-06-23 23:11:28 +0000422 DeclPtrTy ThisDecl = TemplateInfo.TemplateParams?
423 Actions.ActOnTemplateDeclarator(CurScope,
424 Action::MultiTemplateParamsArg(Actions,
425 TemplateInfo.TemplateParams->data(),
426 TemplateInfo.TemplateParams->size()),
427 D)
428 : Actions.ActOnDeclarator(CurScope, D);
Douglas Gregore3298aa2009-05-12 21:31:51 +0000429
430 // Parse declarator '=' initializer.
431 if (Tok.is(tok::equal)) {
432 ConsumeToken();
433 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
434 SourceLocation DelLoc = ConsumeToken();
435 Actions.SetDeclDeleted(ThisDecl, DelLoc);
436 } else {
Argiris Kirtzidis68370592009-06-17 22:50:06 +0000437 if (getLang().CPlusPlus)
438 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
439
Douglas Gregore3298aa2009-05-12 21:31:51 +0000440 OwningExprResult Init(ParseInitializer());
Argiris Kirtzidis68370592009-06-17 22:50:06 +0000441
442 if (getLang().CPlusPlus)
443 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
444
Douglas Gregore3298aa2009-05-12 21:31:51 +0000445 if (Init.isInvalid()) {
446 SkipUntil(tok::semi, true, true);
447 return DeclPtrTy();
448 }
Anders Carlssonf9f05b82009-05-30 21:37:25 +0000449 Actions.AddInitializerToDecl(ThisDecl, Actions.FullExpr(Init));
Douglas Gregore3298aa2009-05-12 21:31:51 +0000450 }
451 } else if (Tok.is(tok::l_paren)) {
452 // Parse C++ direct initializer: '(' expression-list ')'
453 SourceLocation LParenLoc = ConsumeParen();
454 ExprVector Exprs(Actions);
455 CommaLocsTy CommaLocs;
456
457 if (ParseExpressionList(Exprs, CommaLocs)) {
458 SkipUntil(tok::r_paren);
459 } else {
460 // Match the ')'.
461 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
462
463 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
464 "Unexpected number of commas!");
465 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
466 move_arg(Exprs),
Jay Foad9e6bef42009-05-21 09:52:38 +0000467 CommaLocs.data(), RParenLoc);
Douglas Gregore3298aa2009-05-12 21:31:51 +0000468 }
469 } else {
Anders Carlsson68acecb2009-07-11 00:34:39 +0000470 bool TypeContainsUndeducedAuto =
471 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
472 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregore3298aa2009-05-12 21:31:51 +0000473 }
474
475 return ThisDecl;
476}
477
478/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
479/// parsing 'declaration-specifiers declarator'. This method is split out this
480/// way to handle the ambiguity between top-level function-definitions and
481/// declarations.
482///
483/// init-declarator-list: [C99 6.7]
484/// init-declarator
485/// init-declarator-list ',' init-declarator
486///
487/// According to the standard grammar, =default and =delete are function
488/// definitions, but that definitely doesn't fit with the parser here.
489///
Chris Lattnera17991f2009-03-29 16:50:03 +0000490Parser::DeclGroupPtrTy Parser::
Chris Lattner4b009652007-07-25 00:24:17 +0000491ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000492 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
493 // that we parse together here.
494 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Chris Lattner4b009652007-07-25 00:24:17 +0000495
496 // At this point, we know that it is not a function definition. Parse the
497 // rest of the init-declarator-list.
498 while (1) {
Douglas Gregore3298aa2009-05-12 21:31:51 +0000499 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
500 if (ThisDecl.get())
501 DeclsInGroup.push_back(ThisDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000502
Chris Lattner4b009652007-07-25 00:24:17 +0000503 // If we don't have a comma, it is either the end of the list (a ';') or an
504 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000505 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000506 break;
507
508 // Consume the comma.
509 ConsumeToken();
510
511 // Parse the next declarator.
512 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000513
514 // Accept attributes in an init-declarator. In the first declarator in a
515 // declaration, these would be part of the declspec. In subsequent
516 // declarators, they become part of the declarator itself, so that they
517 // don't apply to declarators after *this* one. Examples:
518 // short __attribute__((common)) var; -> declspec
519 // short var __attribute__((common)); -> declarator
520 // short x, __attribute__((common)) var; -> declarator
Sebastian Redl0c986032009-02-09 18:23:29 +0000521 if (Tok.is(tok::kw___attribute)) {
522 SourceLocation Loc;
523 AttributeList *AttrList = ParseAttributes(&Loc);
524 D.AddAttributes(AttrList, Loc);
525 }
Chris Lattner926cf542008-10-20 04:57:38 +0000526
Chris Lattner4b009652007-07-25 00:24:17 +0000527 ParseDeclarator(D);
528 }
529
Eli Friedman4d57af22009-05-29 01:49:24 +0000530 return Actions.FinalizeDeclaratorGroup(CurScope, D.getDeclSpec(),
531 DeclsInGroup.data(),
Chris Lattner2c41d482009-03-29 17:18:04 +0000532 DeclsInGroup.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000533}
534
535/// ParseSpecifierQualifierList
536/// specifier-qualifier-list:
537/// type-specifier specifier-qualifier-list[opt]
538/// type-qualifier specifier-qualifier-list[opt]
539/// [GNU] attributes specifier-qualifier-list[opt]
540///
541void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
542 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
543 /// parse declaration-specifiers and complain about extra stuff.
544 ParseDeclarationSpecifiers(DS);
545
546 // Validate declspec for type-name.
547 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera52aec42009-04-14 21:16:09 +0000548 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
549 !DS.getAttributes())
Chris Lattner4b009652007-07-25 00:24:17 +0000550 Diag(Tok, diag::err_typename_requires_specqual);
551
552 // Issue diagnostic and remove storage class if present.
553 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
554 if (DS.getStorageClassSpecLoc().isValid())
555 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
556 else
557 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
558 DS.ClearStorageClassSpecs();
559 }
560
561 // Issue diagnostic and remove function specfier if present.
562 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000563 if (DS.isInlineSpecified())
564 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
565 if (DS.isVirtualSpecified())
566 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
567 if (DS.isExplicitSpecified())
568 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000569 DS.ClearFunctionSpecs();
570 }
571}
572
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000573/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
574/// specified token is valid after the identifier in a declarator which
575/// immediately follows the declspec. For example, these things are valid:
576///
577/// int x [ 4]; // direct-declarator
578/// int x ( int y); // direct-declarator
579/// int(int x ) // direct-declarator
580/// int x ; // simple-declaration
581/// int x = 17; // init-declarator-list
582/// int x , y; // init-declarator-list
583/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera52aec42009-04-14 21:16:09 +0000584/// int x : 4; // struct-declarator
Chris Lattnerca6cc362009-04-12 22:29:43 +0000585/// int x { 5}; // C++'0x unified initializers
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000586///
587/// This is not, because 'x' does not immediately follow the declspec (though
588/// ')' happens to be valid anyway).
589/// int (x)
590///
591static bool isValidAfterIdentifierInDeclarator(const Token &T) {
592 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
593 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera52aec42009-04-14 21:16:09 +0000594 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000595}
596
Chris Lattner82353c62009-04-14 21:34:55 +0000597
598/// ParseImplicitInt - This method is called when we have an non-typename
599/// identifier in a declspec (which normally terminates the decl spec) when
600/// the declspec has no type specifier. In this case, the declspec is either
601/// malformed or is "implicit int" (in K&R and C89).
602///
603/// This method handles diagnosing this prettily and returns false if the
604/// declspec is done being processed. If it recovers and thinks there may be
605/// other pieces of declspec after it, it returns true.
606///
Chris Lattner52cd7622009-04-14 22:17:06 +0000607bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000608 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner82353c62009-04-14 21:34:55 +0000609 AccessSpecifier AS) {
Chris Lattner52cd7622009-04-14 22:17:06 +0000610 assert(Tok.is(tok::identifier) && "should have identifier");
611
Chris Lattner82353c62009-04-14 21:34:55 +0000612 SourceLocation Loc = Tok.getLocation();
613 // If we see an identifier that is not a type name, we normally would
614 // parse it as the identifer being declared. However, when a typename
615 // is typo'd or the definition is not included, this will incorrectly
616 // parse the typename as the identifier name and fall over misparsing
617 // later parts of the diagnostic.
618 //
619 // As such, we try to do some look-ahead in cases where this would
620 // otherwise be an "implicit-int" case to see if this is invalid. For
621 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
622 // an identifier with implicit int, we'd get a parse error because the
623 // next token is obviously invalid for a type. Parse these as a case
624 // with an invalid type specifier.
625 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
626
627 // Since we know that this either implicit int (which is rare) or an
628 // error, we'd do lookahead to try to do better recovery.
629 if (isValidAfterIdentifierInDeclarator(NextToken())) {
630 // If this token is valid for implicit int, e.g. "static x = 4", then
631 // we just avoid eating the identifier, so it will be parsed as the
632 // identifier in the declarator.
633 return false;
634 }
635
636 // Otherwise, if we don't consume this token, we are going to emit an
637 // error anyway. Try to recover from various common problems. Check
638 // to see if this was a reference to a tag name without a tag specified.
639 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattner52cd7622009-04-14 22:17:06 +0000640 //
641 // C++ doesn't need this, and isTagName doesn't take SS.
642 if (SS == 0) {
643 const char *TagName = 0;
644 tok::TokenKind TagKind = tok::unknown;
Chris Lattner82353c62009-04-14 21:34:55 +0000645
Chris Lattner82353c62009-04-14 21:34:55 +0000646 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
647 default: break;
648 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
649 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
650 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
651 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
652 }
Chris Lattner82353c62009-04-14 21:34:55 +0000653
Chris Lattner52cd7622009-04-14 22:17:06 +0000654 if (TagName) {
655 Diag(Loc, diag::err_use_of_tag_name_without_tag)
656 << Tok.getIdentifierInfo() << TagName
657 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
658
659 // Parse this as a tag as if the missing tag were present.
660 if (TagKind == tok::kw_enum)
661 ParseEnumSpecifier(Loc, DS, AS);
662 else
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000663 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattner52cd7622009-04-14 22:17:06 +0000664 return true;
665 }
Chris Lattner82353c62009-04-14 21:34:55 +0000666 }
667
668 // Since this is almost certainly an invalid type name, emit a
669 // diagnostic that says it, eat the token, and mark the declspec as
670 // invalid.
Chris Lattner52cd7622009-04-14 22:17:06 +0000671 SourceRange R;
672 if (SS) R = SS->getRange();
673
674 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
Chris Lattner82353c62009-04-14 21:34:55 +0000675 const char *PrevSpec;
John McCall9f6e0972009-08-03 20:12:06 +0000676 unsigned DiagID;
677 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner82353c62009-04-14 21:34:55 +0000678 DS.SetRangeEnd(Tok.getLocation());
679 ConsumeToken();
680
681 // TODO: Could inject an invalid typedef decl in an enclosing scope to
682 // avoid rippling error messages on subsequent uses of the same type,
683 // could be useful if #include was forgotten.
684 return false;
685}
686
Chris Lattner4b009652007-07-25 00:24:17 +0000687/// ParseDeclarationSpecifiers
688/// declaration-specifiers: [C99 6.7]
689/// storage-class-specifier declaration-specifiers[opt]
690/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000691/// [C99] function-specifier declaration-specifiers[opt]
692/// [GNU] attributes declaration-specifiers[opt]
693///
694/// storage-class-specifier: [C99 6.7.1]
695/// 'typedef'
696/// 'extern'
697/// 'static'
698/// 'auto'
699/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000700/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000701/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000702/// function-specifier: [C99 6.7.4]
703/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000704/// [C++] 'virtual'
705/// [C++] 'explicit'
Anders Carlsson6c2ad5a2009-05-06 04:46:28 +0000706/// 'friend': [C++ dcl.friend]
707
Chris Lattner4b009652007-07-25 00:24:17 +0000708///
Douglas Gregor52473432008-12-24 02:52:09 +0000709void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000710 const ParsedTemplateInfo &TemplateInfo,
John McCall140607b2009-08-06 02:15:43 +0000711 AccessSpecifier AS,
712 DeclSpecContext DSContext) {
Chris Lattnera4ff4272008-03-13 06:29:04 +0000713 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000714 while (1) {
John McCall9f6e0972009-08-03 20:12:06 +0000715 bool isInvalid = false;
Chris Lattner4b009652007-07-25 00:24:17 +0000716 const char *PrevSpec = 0;
John McCall9f6e0972009-08-03 20:12:06 +0000717 unsigned DiagID = 0;
718
Chris Lattner4b009652007-07-25 00:24:17 +0000719 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000720
Chris Lattner4b009652007-07-25 00:24:17 +0000721 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000722 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000723 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000724 // If this is not a declaration specifier token, we're done reading decl
725 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000726 DS.Finish(Diags, PP);
Chris Lattner4b009652007-07-25 00:24:17 +0000727 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000728
729 case tok::coloncolon: // ::foo::bar
730 // Annotate C++ scope specifiers. If we get one, loop.
731 if (TryAnnotateCXXScopeToken())
732 continue;
733 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000734
735 case tok::annot_cxxscope: {
736 if (DS.hasTypeSpecifier())
737 goto DoneWithDeclSpec;
738
739 // We are looking for a qualified typename.
Douglas Gregor80b95c52009-03-25 15:40:00 +0000740 Token Next = NextToken();
741 if (Next.is(tok::annot_template_id) &&
742 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregoraabb8502009-03-31 00:43:58 +0000743 ->Kind == TNK_Type_template) {
Douglas Gregor80b95c52009-03-25 15:40:00 +0000744 // We have a qualified template-id, e.g., N::A<int>
745 CXXScopeSpec SS;
746 ParseOptionalCXXScopeSpecifier(SS);
747 assert(Tok.is(tok::annot_template_id) &&
748 "ParseOptionalCXXScopeSpecifier not working");
749 AnnotateTemplateIdTokenAsType(&SS);
750 continue;
751 }
752
753 if (Next.isNot(tok::identifier))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000754 goto DoneWithDeclSpec;
755
756 CXXScopeSpec SS;
Douglas Gregor041e9292009-03-26 23:56:24 +0000757 SS.setScopeRep(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000758 SS.setRange(Tok.getAnnotationRange());
759
760 // If the next token is the name of the class type that the C++ scope
761 // denotes, followed by a '(', then this is a constructor declaration.
762 // We're done with the decl-specifiers.
Chris Lattner52cd7622009-04-14 22:17:06 +0000763 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000764 CurScope, &SS) &&
765 GetLookAheadToken(2).is(tok::l_paren))
766 goto DoneWithDeclSpec;
767
Douglas Gregor1075a162009-02-04 17:00:24 +0000768 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
769 Next.getLocation(), CurScope, &SS);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000770
Chris Lattner52cd7622009-04-14 22:17:06 +0000771 // If the referenced identifier is not a type, then this declspec is
772 // erroneous: We already checked about that it has no type specifier, and
773 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
774 // typename.
775 if (TypeRep == 0) {
776 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000777 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000778 goto DoneWithDeclSpec;
Chris Lattner52cd7622009-04-14 22:17:06 +0000779 }
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000780
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000781 ConsumeToken(); // The C++ scope.
782
Douglas Gregora60c62e2009-02-09 15:09:02 +0000783 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall9f6e0972009-08-03 20:12:06 +0000784 DiagID, TypeRep);
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000785 if (isInvalid)
786 break;
787
788 DS.SetRangeEnd(Tok.getLocation());
789 ConsumeToken(); // The typename.
790
791 continue;
792 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000793
794 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +0000795 if (Tok.getAnnotationValue())
796 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall9f6e0972009-08-03 20:12:06 +0000797 DiagID, Tok.getAnnotationValue());
Douglas Gregord7cb0372009-04-01 21:51:26 +0000798 else
799 DS.SetTypeSpecError();
Chris Lattnerc297b722009-01-21 19:48:37 +0000800 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
801 ConsumeToken(); // The typename
802
803 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
804 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
805 // Objective-C interface. If we don't have Objective-C or a '<', this is
806 // just a normal reference to a typedef name.
807 if (!Tok.is(tok::less) || !getLang().ObjC1)
808 continue;
809
810 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000811 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnerc297b722009-01-21 19:48:37 +0000812 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremeneka0c6de32009-06-30 22:19:00 +0000813 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Chris Lattnerc297b722009-01-21 19:48:37 +0000814
815 DS.SetRangeEnd(EndProtoLoc);
816 continue;
817 }
818
Chris Lattnerfda18db2008-07-26 01:18:38 +0000819 // typedef-name
820 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000821 // In C++, check to see if this is a scope specifier like foo::bar::, if
822 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000823 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
824 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000825
Chris Lattnerfda18db2008-07-26 01:18:38 +0000826 // This identifier can only be a typedef name if we haven't already seen
827 // a type-specifier. Without this check we misparse:
828 // typedef int X; struct Y { short X; }; as 'short int'.
829 if (DS.hasTypeSpecifier())
830 goto DoneWithDeclSpec;
831
832 // It has to be available as a typedef too!
Douglas Gregor1075a162009-02-04 17:00:24 +0000833 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
834 Tok.getLocation(), CurScope);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000835
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000836 // If this is not a typedef name, don't parse it as part of the declspec,
837 // it must be an implicit int or an error.
838 if (TypeRep == 0) {
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000839 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000840 goto DoneWithDeclSpec;
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000841 }
Douglas Gregor8e458f42009-02-09 18:46:07 +0000842
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000843 // C++: If the identifier is actually the name of the class type
844 // being defined and the next token is a '(', then this is a
845 // constructor declaration. We're done with the decl-specifiers
846 // and will treat this token as an identifier.
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000847 if (getLang().CPlusPlus && CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000848 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
849 NextToken().getKind() == tok::l_paren)
850 goto DoneWithDeclSpec;
851
Douglas Gregora60c62e2009-02-09 15:09:02 +0000852 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall9f6e0972009-08-03 20:12:06 +0000853 DiagID, TypeRep);
Chris Lattnerfda18db2008-07-26 01:18:38 +0000854 if (isInvalid)
855 break;
856
857 DS.SetRangeEnd(Tok.getLocation());
858 ConsumeToken(); // The identifier
859
860 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
861 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
862 // Objective-C interface. If we don't have Objective-C or a '<', this is
863 // just a normal reference to a typedef name.
864 if (!Tok.is(tok::less) || !getLang().ObjC1)
865 continue;
866
867 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000868 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000869 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremeneka0c6de32009-06-30 22:19:00 +0000870 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000871
872 DS.SetRangeEnd(EndProtoLoc);
873
Steve Narofff7683302008-09-22 10:28:57 +0000874 // Need to support trailing type qualifiers (e.g. "id<p> const").
875 // If a type specifier follows, it will be diagnosed elsewhere.
876 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000877 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000878
879 // type-name
880 case tok::annot_template_id: {
881 TemplateIdAnnotation *TemplateId
882 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregoraabb8502009-03-31 00:43:58 +0000883 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor0c281a82009-02-25 19:37:18 +0000884 // This template-id does not refer to a type name, so we're
885 // done with the type-specifiers.
886 goto DoneWithDeclSpec;
887 }
888
889 // Turn the template-id annotation token into a type annotation
890 // token, then try again to parse it as a type-specifier.
Douglas Gregord7cb0372009-04-01 21:51:26 +0000891 AnnotateTemplateIdTokenAsType();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000892 continue;
893 }
894
Chris Lattner4b009652007-07-25 00:24:17 +0000895 // GNU attributes support.
896 case tok::kw___attribute:
897 DS.AddAttributes(ParseAttributes());
898 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000899
900 // Microsoft declspec support.
901 case tok::kw___declspec:
Eli Friedmancd231842009-06-08 07:21:15 +0000902 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000903 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000904
Steve Naroffedd04d52008-12-25 14:16:32 +0000905 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000906 case tok::kw___forceinline:
Eli Friedman891d82f2009-06-08 23:27:34 +0000907 // FIXME: Add handling here!
908 break;
909
910 case tok::kw___ptr64:
Steve Naroffad620402008-12-25 14:41:26 +0000911 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000912 case tok::kw___cdecl:
913 case tok::kw___stdcall:
914 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +0000915 DS.AddAttributes(ParseMicrosoftTypeAttributes());
916 continue;
917
Chris Lattner4b009652007-07-25 00:24:17 +0000918 // storage-class-specifier
919 case tok::kw_typedef:
John McCall9f6e0972009-08-03 20:12:06 +0000920 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
921 DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000922 break;
923 case tok::kw_extern:
924 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000925 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall9f6e0972009-08-03 20:12:06 +0000926 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
927 DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000928 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000929 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000930 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCall9f6e0972009-08-03 20:12:06 +0000931 PrevSpec, DiagID);
Steve Narofff258a0f2007-12-18 00:16:02 +0000932 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000933 case tok::kw_static:
934 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000935 Diag(Tok, diag::ext_thread_before) << "static";
John McCall9f6e0972009-08-03 20:12:06 +0000936 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
937 DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000938 break;
939 case tok::kw_auto:
Anders Carlsson4a8498c2009-06-26 18:41:36 +0000940 if (getLang().CPlusPlus0x)
John McCall9f6e0972009-08-03 20:12:06 +0000941 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
942 DiagID);
Anders Carlsson4a8498c2009-06-26 18:41:36 +0000943 else
John McCall9f6e0972009-08-03 20:12:06 +0000944 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
945 DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000946 break;
947 case tok::kw_register:
John McCall9f6e0972009-08-03 20:12:06 +0000948 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
949 DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000950 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000951 case tok::kw_mutable:
John McCall9f6e0972009-08-03 20:12:06 +0000952 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
953 DiagID);
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000954 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000955 case tok::kw___thread:
John McCall9f6e0972009-08-03 20:12:06 +0000956 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000957 break;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000958
Chris Lattner4b009652007-07-25 00:24:17 +0000959 // function-specifier
960 case tok::kw_inline:
John McCall9f6e0972009-08-03 20:12:06 +0000961 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000962 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000963 case tok::kw_virtual:
John McCall9f6e0972009-08-03 20:12:06 +0000964 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000965 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000966 case tok::kw_explicit:
John McCall9f6e0972009-08-03 20:12:06 +0000967 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000968 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000969
Anders Carlsson6c2ad5a2009-05-06 04:46:28 +0000970 // friend
971 case tok::kw_friend:
John McCall140607b2009-08-06 02:15:43 +0000972 if (DSContext == DSC_class)
973 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
974 else {
975 PrevSpec = ""; // not actually used by the diagnostic
976 DiagID = diag::err_friend_invalid_in_context;
977 isInvalid = true;
978 }
Anders Carlsson6c2ad5a2009-05-06 04:46:28 +0000979 break;
John McCall9f6e0972009-08-03 20:12:06 +0000980
Chris Lattnerc297b722009-01-21 19:48:37 +0000981 // type-specifier
982 case tok::kw_short:
John McCall9f6e0972009-08-03 20:12:06 +0000983 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
984 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +0000985 break;
986 case tok::kw_long:
987 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall9f6e0972009-08-03 20:12:06 +0000988 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
989 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +0000990 else
John McCall9f6e0972009-08-03 20:12:06 +0000991 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
992 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +0000993 break;
994 case tok::kw_signed:
John McCall9f6e0972009-08-03 20:12:06 +0000995 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
996 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +0000997 break;
998 case tok::kw_unsigned:
John McCall9f6e0972009-08-03 20:12:06 +0000999 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1000 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001001 break;
1002 case tok::kw__Complex:
John McCall9f6e0972009-08-03 20:12:06 +00001003 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1004 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001005 break;
1006 case tok::kw__Imaginary:
John McCall9f6e0972009-08-03 20:12:06 +00001007 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1008 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001009 break;
1010 case tok::kw_void:
John McCall9f6e0972009-08-03 20:12:06 +00001011 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1012 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001013 break;
1014 case tok::kw_char:
John McCall9f6e0972009-08-03 20:12:06 +00001015 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1016 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001017 break;
1018 case tok::kw_int:
John McCall9f6e0972009-08-03 20:12:06 +00001019 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1020 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001021 break;
1022 case tok::kw_float:
John McCall9f6e0972009-08-03 20:12:06 +00001023 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1024 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001025 break;
1026 case tok::kw_double:
John McCall9f6e0972009-08-03 20:12:06 +00001027 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1028 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001029 break;
1030 case tok::kw_wchar_t:
John McCall9f6e0972009-08-03 20:12:06 +00001031 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1032 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001033 break;
Alisdair Meredith2bcacb62009-07-14 06:30:34 +00001034 case tok::kw_char16_t:
John McCall9f6e0972009-08-03 20:12:06 +00001035 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1036 DiagID);
Alisdair Meredith2bcacb62009-07-14 06:30:34 +00001037 break;
1038 case tok::kw_char32_t:
John McCall9f6e0972009-08-03 20:12:06 +00001039 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1040 DiagID);
Alisdair Meredith2bcacb62009-07-14 06:30:34 +00001041 break;
Chris Lattnerc297b722009-01-21 19:48:37 +00001042 case tok::kw_bool:
1043 case tok::kw__Bool:
John McCall9f6e0972009-08-03 20:12:06 +00001044 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1045 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001046 break;
1047 case tok::kw__Decimal32:
John McCall9f6e0972009-08-03 20:12:06 +00001048 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1049 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001050 break;
1051 case tok::kw__Decimal64:
John McCall9f6e0972009-08-03 20:12:06 +00001052 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1053 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001054 break;
1055 case tok::kw__Decimal128:
John McCall9f6e0972009-08-03 20:12:06 +00001056 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1057 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001058 break;
1059
1060 // class-specifier:
1061 case tok::kw_class:
1062 case tok::kw_struct:
Chris Lattner197b4342009-04-12 21:49:30 +00001063 case tok::kw_union: {
1064 tok::TokenKind Kind = Tok.getKind();
1065 ConsumeToken();
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001066 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +00001067 continue;
Chris Lattner197b4342009-04-12 21:49:30 +00001068 }
Chris Lattnerc297b722009-01-21 19:48:37 +00001069
1070 // enum-specifier:
1071 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +00001072 ConsumeToken();
1073 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +00001074 continue;
1075
1076 // cv-qualifier:
1077 case tok::kw_const:
John McCall9f6e0972009-08-03 20:12:06 +00001078 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1079 getLang());
Chris Lattnerc297b722009-01-21 19:48:37 +00001080 break;
1081 case tok::kw_volatile:
John McCall9f6e0972009-08-03 20:12:06 +00001082 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1083 getLang());
Chris Lattnerc297b722009-01-21 19:48:37 +00001084 break;
1085 case tok::kw_restrict:
John McCall9f6e0972009-08-03 20:12:06 +00001086 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1087 getLang());
Chris Lattnerc297b722009-01-21 19:48:37 +00001088 break;
1089
Douglas Gregord3022602009-03-27 23:10:48 +00001090 // C++ typename-specifier:
1091 case tok::kw_typename:
1092 if (TryAnnotateTypeOrScopeToken())
1093 continue;
1094 break;
1095
Chris Lattnerc297b722009-01-21 19:48:37 +00001096 // GNU typeof support.
1097 case tok::kw_typeof:
1098 ParseTypeofSpecifier(DS);
1099 continue;
1100
Anders Carlssoneed418b2009-06-24 17:47:40 +00001101 case tok::kw_decltype:
1102 ParseDecltypeSpecifier(DS);
1103 continue;
1104
Steve Naroff5f0466b2008-06-05 00:02:44 +00001105 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +00001106 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +00001107 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1108 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +00001109 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +00001110 goto DoneWithDeclSpec;
1111
1112 {
1113 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001114 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +00001115 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremeneka0c6de32009-06-30 22:19:00 +00001116 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +00001117 DS.SetRangeEnd(EndProtoLoc);
1118
Chris Lattnerf006a222008-11-18 07:48:38 +00001119 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattnerb980c732009-04-03 18:38:42 +00001120 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattnerf006a222008-11-18 07:48:38 +00001121 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +00001122 // Need to support trailing type qualifiers (e.g. "id<p> const").
1123 // If a type specifier follows, it will be diagnosed elsewhere.
1124 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +00001125 }
Chris Lattner4b009652007-07-25 00:24:17 +00001126 }
John McCall9f6e0972009-08-03 20:12:06 +00001127 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattner4b009652007-07-25 00:24:17 +00001128 if (isInvalid) {
1129 assert(PrevSpec && "Method did not return previous specifier!");
John McCall9f6e0972009-08-03 20:12:06 +00001130 assert(DiagID);
Chris Lattnerf006a222008-11-18 07:48:38 +00001131 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001132 }
Chris Lattnera4ff4272008-03-13 06:29:04 +00001133 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001134 ConsumeToken();
1135 }
1136}
Douglas Gregorb3bec712008-12-01 23:54:00 +00001137
Chris Lattnerd706dc82009-01-06 06:59:53 +00001138/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001139/// primarily follow the C++ grammar with additions for C99 and GNU,
1140/// which together subsume the C grammar. Note that the C++
1141/// type-specifier also includes the C type-qualifier (for const,
1142/// volatile, and C99 restrict). Returns true if a type-specifier was
1143/// found (and parsed), false otherwise.
1144///
1145/// type-specifier: [C++ 7.1.5]
1146/// simple-type-specifier
1147/// class-specifier
1148/// enum-specifier
1149/// elaborated-type-specifier [TODO]
1150/// cv-qualifier
1151///
1152/// cv-qualifier: [C++ 7.1.5.1]
1153/// 'const'
1154/// 'volatile'
1155/// [C99] 'restrict'
1156///
1157/// simple-type-specifier: [ C++ 7.1.5.2]
1158/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1159/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1160/// 'char'
1161/// 'wchar_t'
1162/// 'bool'
1163/// 'short'
1164/// 'int'
1165/// 'long'
1166/// 'signed'
1167/// 'unsigned'
1168/// 'float'
1169/// 'double'
1170/// 'void'
1171/// [C99] '_Bool'
1172/// [C99] '_Complex'
1173/// [C99] '_Imaginary' // Removed in TC2?
1174/// [GNU] '_Decimal32'
1175/// [GNU] '_Decimal64'
1176/// [GNU] '_Decimal128'
1177/// [GNU] typeof-specifier
1178/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1179/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlssoneed418b2009-06-24 17:47:40 +00001180/// [C++0x] 'decltype' ( expression )
John McCall9f6e0972009-08-03 20:12:06 +00001181bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnerd706dc82009-01-06 06:59:53 +00001182 const char *&PrevSpec,
John McCall9f6e0972009-08-03 20:12:06 +00001183 unsigned &DiagID,
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001184 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001185 SourceLocation Loc = Tok.getLocation();
1186
1187 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +00001188 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001189 case tok::kw_typename: // typename foo::bar
Chris Lattnerb75fde62009-01-04 23:41:41 +00001190 // Annotate typenames and C++ scope specifiers. If we get one, just
1191 // recurse to handle whatever we get.
1192 if (TryAnnotateTypeOrScopeToken())
John McCall9f6e0972009-08-03 20:12:06 +00001193 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1194 TemplateInfo);
Chris Lattnerb75fde62009-01-04 23:41:41 +00001195 // Otherwise, not a type specifier.
1196 return false;
1197 case tok::coloncolon: // ::foo::bar
1198 if (NextToken().is(tok::kw_new) || // ::new
1199 NextToken().is(tok::kw_delete)) // ::delete
1200 return false;
1201
1202 // Annotate typenames and C++ scope specifiers. If we get one, just
1203 // recurse to handle whatever we get.
1204 if (TryAnnotateTypeOrScopeToken())
John McCall9f6e0972009-08-03 20:12:06 +00001205 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1206 TemplateInfo);
Chris Lattnerb75fde62009-01-04 23:41:41 +00001207 // Otherwise, not a type specifier.
1208 return false;
1209
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001210 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +00001211 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +00001212 if (Tok.getAnnotationValue())
1213 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall9f6e0972009-08-03 20:12:06 +00001214 DiagID, Tok.getAnnotationValue());
Douglas Gregord7cb0372009-04-01 21:51:26 +00001215 else
1216 DS.SetTypeSpecError();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001217 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1218 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001219
1220 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1221 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1222 // Objective-C interface. If we don't have Objective-C or a '<', this is
1223 // just a normal reference to a typedef name.
1224 if (!Tok.is(tok::less) || !getLang().ObjC1)
1225 return true;
1226
1227 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001228 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001229 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremeneka0c6de32009-06-30 22:19:00 +00001230 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001231
1232 DS.SetRangeEnd(EndProtoLoc);
1233 return true;
1234 }
1235
1236 case tok::kw_short:
John McCall9f6e0972009-08-03 20:12:06 +00001237 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001238 break;
1239 case tok::kw_long:
1240 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall9f6e0972009-08-03 20:12:06 +00001241 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1242 DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001243 else
John McCall9f6e0972009-08-03 20:12:06 +00001244 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1245 DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001246 break;
1247 case tok::kw_signed:
John McCall9f6e0972009-08-03 20:12:06 +00001248 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001249 break;
1250 case tok::kw_unsigned:
John McCall9f6e0972009-08-03 20:12:06 +00001251 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1252 DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001253 break;
1254 case tok::kw__Complex:
John McCall9f6e0972009-08-03 20:12:06 +00001255 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1256 DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001257 break;
1258 case tok::kw__Imaginary:
John McCall9f6e0972009-08-03 20:12:06 +00001259 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1260 DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001261 break;
1262 case tok::kw_void:
John McCall9f6e0972009-08-03 20:12:06 +00001263 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001264 break;
1265 case tok::kw_char:
John McCall9f6e0972009-08-03 20:12:06 +00001266 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001267 break;
1268 case tok::kw_int:
John McCall9f6e0972009-08-03 20:12:06 +00001269 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001270 break;
1271 case tok::kw_float:
John McCall9f6e0972009-08-03 20:12:06 +00001272 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001273 break;
1274 case tok::kw_double:
John McCall9f6e0972009-08-03 20:12:06 +00001275 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001276 break;
1277 case tok::kw_wchar_t:
John McCall9f6e0972009-08-03 20:12:06 +00001278 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001279 break;
Alisdair Meredith2bcacb62009-07-14 06:30:34 +00001280 case tok::kw_char16_t:
John McCall9f6e0972009-08-03 20:12:06 +00001281 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredith2bcacb62009-07-14 06:30:34 +00001282 break;
1283 case tok::kw_char32_t:
John McCall9f6e0972009-08-03 20:12:06 +00001284 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredith2bcacb62009-07-14 06:30:34 +00001285 break;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001286 case tok::kw_bool:
1287 case tok::kw__Bool:
John McCall9f6e0972009-08-03 20:12:06 +00001288 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001289 break;
1290 case tok::kw__Decimal32:
John McCall9f6e0972009-08-03 20:12:06 +00001291 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1292 DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001293 break;
1294 case tok::kw__Decimal64:
John McCall9f6e0972009-08-03 20:12:06 +00001295 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1296 DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001297 break;
1298 case tok::kw__Decimal128:
John McCall9f6e0972009-08-03 20:12:06 +00001299 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1300 DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001301 break;
1302
1303 // class-specifier:
1304 case tok::kw_class:
1305 case tok::kw_struct:
Chris Lattner197b4342009-04-12 21:49:30 +00001306 case tok::kw_union: {
1307 tok::TokenKind Kind = Tok.getKind();
1308 ConsumeToken();
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001309 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001310 return true;
Chris Lattner197b4342009-04-12 21:49:30 +00001311 }
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001312
1313 // enum-specifier:
1314 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +00001315 ConsumeToken();
1316 ParseEnumSpecifier(Loc, DS);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001317 return true;
1318
1319 // cv-qualifier:
1320 case tok::kw_const:
1321 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall9f6e0972009-08-03 20:12:06 +00001322 DiagID, getLang());
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001323 break;
1324 case tok::kw_volatile:
1325 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall9f6e0972009-08-03 20:12:06 +00001326 DiagID, getLang());
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001327 break;
1328 case tok::kw_restrict:
1329 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall9f6e0972009-08-03 20:12:06 +00001330 DiagID, getLang());
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001331 break;
1332
1333 // GNU typeof support.
1334 case tok::kw_typeof:
1335 ParseTypeofSpecifier(DS);
1336 return true;
1337
Anders Carlssoneed418b2009-06-24 17:47:40 +00001338 // C++0x decltype support.
1339 case tok::kw_decltype:
1340 ParseDecltypeSpecifier(DS);
1341 return true;
1342
Anders Carlsson7e023eb2009-06-26 23:44:14 +00001343 // C++0x auto support.
1344 case tok::kw_auto:
1345 if (!getLang().CPlusPlus0x)
1346 return false;
1347
John McCall9f6e0972009-08-03 20:12:06 +00001348 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson7e023eb2009-06-26 23:44:14 +00001349 break;
Eli Friedman891d82f2009-06-08 23:27:34 +00001350 case tok::kw___ptr64:
1351 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001352 case tok::kw___cdecl:
1353 case tok::kw___stdcall:
1354 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001355 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner5bb837e2009-01-21 19:19:26 +00001356 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +00001357
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001358 default:
1359 // Not a type-specifier; do nothing.
1360 return false;
1361 }
1362
1363 // If the specifier combination wasn't legal, issue a diagnostic.
1364 if (isInvalid) {
1365 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001366 // Pick between error or extwarn.
Chris Lattnerf006a222008-11-18 07:48:38 +00001367 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001368 }
1369 DS.SetRangeEnd(Tok.getLocation());
1370 ConsumeToken(); // whatever we parsed above.
1371 return true;
1372}
Chris Lattner4b009652007-07-25 00:24:17 +00001373
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001374/// ParseStructDeclaration - Parse a struct declaration without the terminating
1375/// semicolon.
1376///
Chris Lattner4b009652007-07-25 00:24:17 +00001377/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001378/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001379/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001380/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +00001381/// struct-declarator-list:
1382/// struct-declarator
1383/// struct-declarator-list ',' struct-declarator
1384/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1385/// struct-declarator:
1386/// declarator
1387/// [GNU] declarator attributes[opt]
1388/// declarator[opt] ':' constant-expression
1389/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1390///
Chris Lattner3dd8d392008-04-10 06:46:29 +00001391void Parser::
1392ParseStructDeclaration(DeclSpec &DS,
1393 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001394 if (Tok.is(tok::kw___extension__)) {
1395 // __extension__ silences extension warnings in the subexpression.
1396 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001397 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001398 return ParseStructDeclaration(DS, Fields);
1399 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001400
1401 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001402 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001403 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001404
Douglas Gregorb748fc52009-01-12 22:49:06 +00001405 // If there are no declarators, this is a free-standing declaration
1406 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001407 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001408 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001409 return;
1410 }
1411
1412 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001413 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001414 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001415 FieldDeclarator &DeclaratorInfo = Fields.back();
1416
Steve Naroffa9adf112007-08-20 22:28:22 +00001417 /// struct-declarator: declarator
1418 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001419 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001420 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001421
Chris Lattner34a01ad2007-10-09 17:33:22 +00001422 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001423 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001424 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001425 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001426 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001427 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001428 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001429 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001430
Steve Naroffa9adf112007-08-20 22:28:22 +00001431 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001432 if (Tok.is(tok::kw___attribute)) {
1433 SourceLocation Loc;
1434 AttributeList *AttrList = ParseAttributes(&Loc);
1435 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1436 }
1437
Steve Naroffa9adf112007-08-20 22:28:22 +00001438 // If we don't have a comma, it is either the end of the list (a ';')
1439 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001440 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001441 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001442
Steve Naroffa9adf112007-08-20 22:28:22 +00001443 // Consume the comma.
1444 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001445
Steve Naroffa9adf112007-08-20 22:28:22 +00001446 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001447 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001448
Steve Naroffa9adf112007-08-20 22:28:22 +00001449 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001450 if (Tok.is(tok::kw___attribute)) {
1451 SourceLocation Loc;
1452 AttributeList *AttrList = ParseAttributes(&Loc);
1453 Fields.back().D.AddAttributes(AttrList, Loc);
1454 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001455 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001456}
1457
1458/// ParseStructUnionBody
1459/// struct-contents:
1460/// struct-declaration-list
1461/// [EXT] empty
1462/// [GNU] "struct-declaration-list" without terminatoring ';'
1463/// struct-declaration-list:
1464/// struct-declaration
1465/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001466/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001467///
Chris Lattner4b009652007-07-25 00:24:17 +00001468void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001469 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001470 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1471 PP.getSourceManager(),
1472 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001473
Chris Lattner4b009652007-07-25 00:24:17 +00001474 SourceLocation LBraceLoc = ConsumeBrace();
1475
Douglas Gregorcab994d2009-01-09 22:42:13 +00001476 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001477 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1478
Chris Lattner4b009652007-07-25 00:24:17 +00001479 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1480 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001481 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001482 Diag(Tok, diag::ext_empty_struct_union_enum)
1483 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001484
Chris Lattner5261d0c2009-03-28 19:18:32 +00001485 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001486 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1487
Chris Lattner4b009652007-07-25 00:24:17 +00001488 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001489 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001490 // Each iteration of this loop reads one struct-declaration.
1491
1492 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001493 if (Tok.is(tok::semi)) {
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001494 Diag(Tok, diag::ext_extra_struct_semi)
1495 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +00001496 ConsumeToken();
1497 continue;
1498 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001499
1500 // Parse all the comma separated declarators.
1501 DeclSpec DS;
1502 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001503 if (!Tok.is(tok::at)) {
1504 ParseStructDeclaration(DS, FieldDeclarators);
1505
1506 // Convert them all to fields.
1507 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1508 FieldDeclarator &FD = FieldDeclarators[i];
1509 // Install the declarator into the current TagDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001510 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1511 DS.getSourceRange().getBegin(),
1512 FD.D, FD.BitfieldSize);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001513 FieldDecls.push_back(Field);
1514 }
1515 } else { // Handle @defs
1516 ConsumeToken();
1517 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1518 Diag(Tok, diag::err_unexpected_at);
1519 SkipUntil(tok::semi, true, true);
1520 continue;
1521 }
1522 ConsumeToken();
1523 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1524 if (!Tok.is(tok::identifier)) {
1525 Diag(Tok, diag::err_expected_ident);
1526 SkipUntil(tok::semi, true, true);
1527 continue;
1528 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001529 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001530 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1531 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001532 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1533 ConsumeToken();
1534 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1535 }
Chris Lattner4b009652007-07-25 00:24:17 +00001536
Chris Lattner34a01ad2007-10-09 17:33:22 +00001537 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001538 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001539 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001540 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001541 break;
1542 } else {
1543 Diag(Tok, diag::err_expected_semi_decl_list);
1544 // Skip to end of block or statement
1545 SkipUntil(tok::r_brace, true, true);
1546 }
1547 }
1548
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001549 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001550
Chris Lattner4b009652007-07-25 00:24:17 +00001551 AttributeList *AttrList = 0;
1552 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001553 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001554 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001555
1556 Actions.ActOnFields(CurScope,
Jay Foad9e6bef42009-05-21 09:52:38 +00001557 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbarf3944442008-10-03 02:03:53 +00001558 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001559 AttrList);
1560 StructScope.Exit();
Argiris Kirtzidiseb925642009-07-14 03:17:52 +00001561 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001562}
1563
1564
1565/// ParseEnumSpecifier
1566/// enum-specifier: [C99 6.7.2.2]
1567/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001568///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001569/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1570/// '}' attributes[opt]
1571/// 'enum' identifier
1572/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001573///
1574/// [C++] elaborated-type-specifier:
1575/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1576///
Chris Lattner197b4342009-04-12 21:49:30 +00001577void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1578 AccessSpecifier AS) {
Chris Lattner4b009652007-07-25 00:24:17 +00001579 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001580
1581 AttributeList *Attr = 0;
1582 // If attributes exist after tag, parse them.
1583 if (Tok.is(tok::kw___attribute))
1584 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001585
1586 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001587 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001588 if (Tok.isNot(tok::identifier)) {
1589 Diag(Tok, diag::err_expected_ident);
1590 if (Tok.isNot(tok::l_brace)) {
1591 // Has no name and is not a definition.
1592 // Skip the rest of this declarator, up until the comma or semicolon.
1593 SkipUntil(tok::comma, true);
1594 return;
1595 }
1596 }
1597 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001598
1599 // Must have either 'enum name' or 'enum {...}'.
1600 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1601 Diag(Tok, diag::err_expected_ident_lbrace);
1602
1603 // Skip the rest of this declarator, up until the comma or semicolon.
1604 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001605 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001606 }
1607
1608 // If an identifier is present, consume and remember it.
1609 IdentifierInfo *Name = 0;
1610 SourceLocation NameLoc;
1611 if (Tok.is(tok::identifier)) {
1612 Name = Tok.getIdentifierInfo();
1613 NameLoc = ConsumeToken();
1614 }
1615
1616 // There are three options here. If we have 'enum foo;', then this is a
1617 // forward declaration. If we have 'enum foo {...' then this is a
1618 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1619 //
1620 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1621 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1622 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1623 //
John McCall069c23a2009-07-31 02:45:11 +00001624 Action::TagUseKind TUK;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001625 if (Tok.is(tok::l_brace))
John McCall069c23a2009-07-31 02:45:11 +00001626 TUK = Action::TUK_Definition;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001627 else if (Tok.is(tok::semi))
John McCall069c23a2009-07-31 02:45:11 +00001628 TUK = Action::TUK_Declaration;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001629 else
John McCall069c23a2009-07-31 02:45:11 +00001630 TUK = Action::TUK_Reference;
Douglas Gregor71f06032009-05-28 23:31:59 +00001631 bool Owned = false;
John McCall069c23a2009-07-31 02:45:11 +00001632 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Douglas Gregor71f06032009-05-28 23:31:59 +00001633 StartLoc, SS, Name, NameLoc, Attr, AS,
Douglas Gregor95254bc2009-07-23 16:36:45 +00001634 Action::MultiTemplateParamsArg(Actions),
Douglas Gregor71f06032009-05-28 23:31:59 +00001635 Owned);
Chris Lattner4b009652007-07-25 00:24:17 +00001636
Chris Lattner34a01ad2007-10-09 17:33:22 +00001637 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001638 ParseEnumBody(StartLoc, TagDecl);
1639
1640 // TODO: semantic analysis on the declspec for enums.
1641 const char *PrevSpec = 0;
John McCall9f6e0972009-08-03 20:12:06 +00001642 unsigned DiagID;
1643 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, DiagID,
Douglas Gregor71f06032009-05-28 23:31:59 +00001644 TagDecl.getAs<void>(), Owned))
John McCall9f6e0972009-08-03 20:12:06 +00001645 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001646}
1647
1648/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1649/// enumerator-list:
1650/// enumerator
1651/// enumerator-list ',' enumerator
1652/// enumerator:
1653/// enumeration-constant
1654/// enumeration-constant '=' constant-expression
1655/// enumeration-constant:
1656/// identifier
1657///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001658void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001659 // Enter the scope of the enum body and start the definition.
1660 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001661 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001662
Chris Lattner4b009652007-07-25 00:24:17 +00001663 SourceLocation LBraceLoc = ConsumeBrace();
1664
Chris Lattnerc9a92452007-08-27 17:24:30 +00001665 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001666 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001667 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001668
Chris Lattner5261d0c2009-03-28 19:18:32 +00001669 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattner4b009652007-07-25 00:24:17 +00001670
Chris Lattner5261d0c2009-03-28 19:18:32 +00001671 DeclPtrTy LastEnumConstDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001672
1673 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001674 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001675 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1676 SourceLocation IdentLoc = ConsumeToken();
1677
1678 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001679 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001680 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001681 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001682 AssignedVal = ParseConstantExpression();
1683 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001684 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001685 }
1686
1687 // Install the enumerator constant into EnumDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001688 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1689 LastEnumConstDecl,
1690 IdentLoc, Ident,
1691 EqualLoc,
1692 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001693 EnumConstantDecls.push_back(EnumConstDecl);
1694 LastEnumConstDecl = EnumConstDecl;
1695
Chris Lattner34a01ad2007-10-09 17:33:22 +00001696 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001697 break;
1698 SourceLocation CommaLoc = ConsumeToken();
1699
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001700 if (Tok.isNot(tok::identifier) &&
1701 !(getLang().C99 || getLang().CPlusPlus0x))
1702 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1703 << getLang().CPlusPlus
1704 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Chris Lattner4b009652007-07-25 00:24:17 +00001705 }
1706
1707 // Eat the }.
Mike Stump155750e2009-05-16 07:06:02 +00001708 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001709
Mike Stump155750e2009-05-16 07:06:02 +00001710 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
Jay Foad9e6bef42009-05-21 09:52:38 +00001711 EnumConstantDecls.data(), EnumConstantDecls.size());
Chris Lattner4b009652007-07-25 00:24:17 +00001712
Chris Lattner5261d0c2009-03-28 19:18:32 +00001713 Action::AttrTy *AttrList = 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001714 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001715 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001716 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001717
1718 EnumScope.Exit();
Argiris Kirtzidiseb925642009-07-14 03:17:52 +00001719 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001720}
1721
1722/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001723/// start of a type-qualifier-list.
1724bool Parser::isTypeQualifier() const {
1725 switch (Tok.getKind()) {
1726 default: return false;
1727 // type-qualifier
1728 case tok::kw_const:
1729 case tok::kw_volatile:
1730 case tok::kw_restrict:
1731 return true;
1732 }
1733}
1734
1735/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001736/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001737bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001738 switch (Tok.getKind()) {
1739 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001740
1741 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001742 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001743 // Annotate typenames and C++ scope specifiers. If we get one, just
1744 // recurse to handle whatever we get.
1745 if (TryAnnotateTypeOrScopeToken())
1746 return isTypeSpecifierQualifier();
1747 // Otherwise, not a type specifier.
1748 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001749
Chris Lattnerb75fde62009-01-04 23:41:41 +00001750 case tok::coloncolon: // ::foo::bar
1751 if (NextToken().is(tok::kw_new) || // ::new
1752 NextToken().is(tok::kw_delete)) // ::delete
1753 return false;
1754
1755 // Annotate typenames and C++ scope specifiers. If we get one, just
1756 // recurse to handle whatever we get.
1757 if (TryAnnotateTypeOrScopeToken())
1758 return isTypeSpecifierQualifier();
1759 // Otherwise, not a type specifier.
1760 return false;
1761
Chris Lattner4b009652007-07-25 00:24:17 +00001762 // GNU attributes support.
1763 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001764 // GNU typeof support.
1765 case tok::kw_typeof:
1766
Chris Lattner4b009652007-07-25 00:24:17 +00001767 // type-specifiers
1768 case tok::kw_short:
1769 case tok::kw_long:
1770 case tok::kw_signed:
1771 case tok::kw_unsigned:
1772 case tok::kw__Complex:
1773 case tok::kw__Imaginary:
1774 case tok::kw_void:
1775 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001776 case tok::kw_wchar_t:
Alisdair Meredith2bcacb62009-07-14 06:30:34 +00001777 case tok::kw_char16_t:
1778 case tok::kw_char32_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001779 case tok::kw_int:
1780 case tok::kw_float:
1781 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001782 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001783 case tok::kw__Bool:
1784 case tok::kw__Decimal32:
1785 case tok::kw__Decimal64:
1786 case tok::kw__Decimal128:
1787
Chris Lattner2e78db32008-04-13 18:59:07 +00001788 // struct-or-union-specifier (C99) or class-specifier (C++)
1789 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001790 case tok::kw_struct:
1791 case tok::kw_union:
1792 // enum-specifier
1793 case tok::kw_enum:
1794
1795 // type-qualifier
1796 case tok::kw_const:
1797 case tok::kw_volatile:
1798 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001799
1800 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001801 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001802 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001803
1804 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1805 case tok::less:
1806 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001807
1808 case tok::kw___cdecl:
1809 case tok::kw___stdcall:
1810 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001811 case tok::kw___w64:
1812 case tok::kw___ptr64:
1813 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001814 }
1815}
1816
1817/// isDeclarationSpecifier() - Return true if the current token is part of a
1818/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001819bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001820 switch (Tok.getKind()) {
1821 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001822
1823 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-03-09 21:12:44 +00001824 // Unfortunate hack to support "Class.factoryMethod" notation.
1825 if (getLang().ObjC1 && NextToken().is(tok::period))
1826 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001827 // Fall through
Steve Naroff73ec9322009-03-09 21:12:44 +00001828
Douglas Gregord3022602009-03-27 23:10:48 +00001829 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001830 // Annotate typenames and C++ scope specifiers. If we get one, just
1831 // recurse to handle whatever we get.
1832 if (TryAnnotateTypeOrScopeToken())
1833 return isDeclarationSpecifier();
1834 // Otherwise, not a declaration specifier.
1835 return false;
1836 case tok::coloncolon: // ::foo::bar
1837 if (NextToken().is(tok::kw_new) || // ::new
1838 NextToken().is(tok::kw_delete)) // ::delete
1839 return false;
1840
1841 // Annotate typenames and C++ scope specifiers. If we get one, just
1842 // recurse to handle whatever we get.
1843 if (TryAnnotateTypeOrScopeToken())
1844 return isDeclarationSpecifier();
1845 // Otherwise, not a declaration specifier.
1846 return false;
1847
Chris Lattner4b009652007-07-25 00:24:17 +00001848 // storage-class-specifier
1849 case tok::kw_typedef:
1850 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001851 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001852 case tok::kw_static:
1853 case tok::kw_auto:
1854 case tok::kw_register:
1855 case tok::kw___thread:
1856
1857 // type-specifiers
1858 case tok::kw_short:
1859 case tok::kw_long:
1860 case tok::kw_signed:
1861 case tok::kw_unsigned:
1862 case tok::kw__Complex:
1863 case tok::kw__Imaginary:
1864 case tok::kw_void:
1865 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001866 case tok::kw_wchar_t:
Alisdair Meredith2bcacb62009-07-14 06:30:34 +00001867 case tok::kw_char16_t:
1868 case tok::kw_char32_t:
1869
Chris Lattner4b009652007-07-25 00:24:17 +00001870 case tok::kw_int:
1871 case tok::kw_float:
1872 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001873 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001874 case tok::kw__Bool:
1875 case tok::kw__Decimal32:
1876 case tok::kw__Decimal64:
1877 case tok::kw__Decimal128:
1878
Chris Lattner2e78db32008-04-13 18:59:07 +00001879 // struct-or-union-specifier (C99) or class-specifier (C++)
1880 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001881 case tok::kw_struct:
1882 case tok::kw_union:
1883 // enum-specifier
1884 case tok::kw_enum:
1885
1886 // type-qualifier
1887 case tok::kw_const:
1888 case tok::kw_volatile:
1889 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001890
Chris Lattner4b009652007-07-25 00:24:17 +00001891 // function-specifier
1892 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001893 case tok::kw_virtual:
1894 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001895
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001896 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001897 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001898
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001899 // GNU typeof support.
1900 case tok::kw_typeof:
1901
1902 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001903 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001904 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001905
1906 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1907 case tok::less:
1908 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001909
Steve Naroffab1a3632009-01-06 19:34:12 +00001910 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001911 case tok::kw___cdecl:
1912 case tok::kw___stdcall:
1913 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001914 case tok::kw___w64:
1915 case tok::kw___ptr64:
1916 case tok::kw___forceinline:
1917 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001918 }
1919}
1920
1921
1922/// ParseTypeQualifierListOpt
1923/// type-qualifier-list: [C99 6.7.5]
1924/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001925/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001926/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001927/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001928///
Chris Lattner460696f2008-12-18 07:02:59 +00001929void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001930 while (1) {
John McCall9f6e0972009-08-03 20:12:06 +00001931 bool isInvalid = false;
Chris Lattner4b009652007-07-25 00:24:17 +00001932 const char *PrevSpec = 0;
John McCall9f6e0972009-08-03 20:12:06 +00001933 unsigned DiagID = 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001934 SourceLocation Loc = Tok.getLocation();
1935
1936 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001937 case tok::kw_const:
John McCall9f6e0972009-08-03 20:12:06 +00001938 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
1939 getLang());
Chris Lattner4b009652007-07-25 00:24:17 +00001940 break;
1941 case tok::kw_volatile:
John McCall9f6e0972009-08-03 20:12:06 +00001942 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1943 getLang());
Chris Lattner4b009652007-07-25 00:24:17 +00001944 break;
1945 case tok::kw_restrict:
John McCall9f6e0972009-08-03 20:12:06 +00001946 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1947 getLang());
Chris Lattner4b009652007-07-25 00:24:17 +00001948 break;
Eli Friedman891d82f2009-06-08 23:27:34 +00001949 case tok::kw___w64:
Steve Naroffad620402008-12-25 14:41:26 +00001950 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001951 case tok::kw___cdecl:
1952 case tok::kw___stdcall:
1953 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001954 if (AttributesAllowed) {
1955 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1956 continue;
1957 }
1958 goto DoneWithTypeQuals;
Chris Lattner4b009652007-07-25 00:24:17 +00001959 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001960 if (AttributesAllowed) {
1961 DS.AddAttributes(ParseAttributes());
1962 continue; // do *not* consume the next token!
1963 }
1964 // otherwise, FALL THROUGH!
1965 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001966 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001967 // If this is not a type-qualifier token, we're done reading type
1968 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001969 DS.Finish(Diags, PP);
Chris Lattner460696f2008-12-18 07:02:59 +00001970 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001971 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001972
Chris Lattner4b009652007-07-25 00:24:17 +00001973 // If the specifier combination wasn't legal, issue a diagnostic.
1974 if (isInvalid) {
1975 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001976 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001977 }
1978 ConsumeToken();
1979 }
1980}
1981
1982
1983/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1984///
1985void Parser::ParseDeclarator(Declarator &D) {
1986 /// This implements the 'declarator' production in the C grammar, then checks
1987 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001988 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001989}
1990
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001991/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1992/// is parsed by the function passed to it. Pass null, and the direct-declarator
1993/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001994/// ptr-operator production.
1995///
Sebastian Redl75555032009-01-24 21:16:55 +00001996/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1997/// [C] pointer[opt] direct-declarator
1998/// [C++] direct-declarator
1999/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00002000///
2001/// pointer: [C99 6.7.5]
2002/// '*' type-qualifier-list[opt]
2003/// '*' type-qualifier-list[opt] pointer
2004///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00002005/// ptr-operator:
2006/// '*' cv-qualifier-seq[opt]
2007/// '&'
Sebastian Redl9951dbc2009-03-15 22:02:01 +00002008/// [C++0x] '&&'
Douglas Gregor3ef6c972008-11-07 20:08:42 +00002009/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl9951dbc2009-03-15 22:02:01 +00002010/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00002011/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002012void Parser::ParseDeclaratorInternal(Declarator &D,
2013 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00002014
Sebastian Redl75555032009-01-24 21:16:55 +00002015 // C++ member pointers start with a '::' or a nested-name.
2016 // Member pointers get special handling, since there's no place for the
2017 // scope spec in the generic path below.
Chris Lattner053dd2d2009-03-24 17:04:48 +00002018 if (getLang().CPlusPlus &&
2019 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2020 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl75555032009-01-24 21:16:55 +00002021 CXXScopeSpec SS;
2022 if (ParseOptionalCXXScopeSpecifier(SS)) {
2023 if(Tok.isNot(tok::star)) {
2024 // The scope spec really belongs to the direct-declarator.
2025 D.getCXXScopeSpec() = SS;
2026 if (DirectDeclParser)
2027 (this->*DirectDeclParser)(D);
2028 return;
2029 }
2030
2031 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00002032 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00002033 DeclSpec DS;
2034 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00002035 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00002036
2037 // Recurse to parse whatever is left.
2038 ParseDeclaratorInternal(D, DirectDeclParser);
2039
2040 // Sema will have to catch (syntactically invalid) pointers into global
2041 // scope. It has to catch pointers into namespace scope anyway.
2042 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002043 Loc, DS.TakeAttributes()),
2044 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00002045 return;
2046 }
2047 }
2048
2049 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00002050 // Not a pointer, C++ reference, or block.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00002051 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner053dd2d2009-03-24 17:04:48 +00002052 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl4e67adb2009-03-23 00:00:23 +00002053 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00002054 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002055 if (DirectDeclParser)
2056 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00002057 return;
2058 }
Sebastian Redl75555032009-01-24 21:16:55 +00002059
Sebastian Redl9951dbc2009-03-15 22:02:01 +00002060 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2061 // '&&' -> rvalue reference
Sebastian Redl4e67adb2009-03-23 00:00:23 +00002062 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redl0c986032009-02-09 18:23:29 +00002063 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002064
Chris Lattnerc14c7f02009-03-27 04:18:06 +00002065 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner69f01932008-02-21 01:32:26 +00002066 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00002067 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00002068
Chris Lattner4b009652007-07-25 00:24:17 +00002069 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00002070 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00002071
Chris Lattner4b009652007-07-25 00:24:17 +00002072 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002073 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00002074 if (Kind == tok::star)
2075 // Remember that we parsed a pointer type, and remember the type-quals.
2076 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00002077 DS.TakeAttributes()),
2078 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00002079 else
2080 // Remember that we parsed a Block type, and remember the type-quals.
2081 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump7ff82e72009-04-21 00:51:43 +00002082 Loc, DS.TakeAttributes()),
Sebastian Redl0c986032009-02-09 18:23:29 +00002083 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00002084 } else {
2085 // Is a reference
2086 DeclSpec DS;
2087
Sebastian Redl4e67adb2009-03-23 00:00:23 +00002088 // Complain about rvalue references in C++03, but then go on and build
2089 // the declarator.
2090 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2091 Diag(Loc, diag::err_rvalue_reference);
2092
Chris Lattner4b009652007-07-25 00:24:17 +00002093 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2094 // cv-qualifiers are introduced through the use of a typedef or of a
2095 // template type argument, in which case the cv-qualifiers are ignored.
2096 //
2097 // [GNU] Retricted references are allowed.
2098 // [GNU] Attributes on references are allowed.
2099 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00002100 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00002101
2102 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2103 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2104 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00002105 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00002106 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2107 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00002108 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00002109 }
2110
2111 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002112 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00002113
Douglas Gregorb7b28a22008-11-03 15:51:28 +00002114 if (D.getNumTypeObjects() > 0) {
2115 // C++ [dcl.ref]p4: There shall be no references to references.
2116 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2117 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002118 if (const IdentifierInfo *II = D.getIdentifier())
2119 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2120 << II;
2121 else
2122 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2123 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00002124
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002125 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00002126 // can go ahead and build the (technically ill-formed)
2127 // declarator: reference collapsing will take care of it.
2128 }
2129 }
2130
Chris Lattner4b009652007-07-25 00:24:17 +00002131 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00002132 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl9951dbc2009-03-15 22:02:01 +00002133 DS.TakeAttributes(),
2134 Kind == tok::amp),
Sebastian Redl0c986032009-02-09 18:23:29 +00002135 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00002136 }
2137}
2138
2139/// ParseDirectDeclarator
2140/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00002141/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00002142/// '(' declarator ')'
2143/// [GNU] '(' attributes declarator ')'
2144/// [C90] direct-declarator '[' constant-expression[opt] ']'
2145/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2146/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2147/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2148/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2149/// direct-declarator '(' parameter-type-list ')'
2150/// direct-declarator '(' identifier-list[opt] ')'
2151/// [GNU] direct-declarator '(' parameter-forward-declarations
2152/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002153/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2154/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00002155/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00002156///
2157/// declarator-id: [C++ 8]
2158/// id-expression
2159/// '::'[opt] nested-name-specifier[opt] type-name
2160///
2161/// id-expression: [C++ 5.1]
2162/// unqualified-id
2163/// qualified-id [TODO]
2164///
2165/// unqualified-id: [C++ 5.1]
2166/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002167/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00002168/// conversion-function-id [TODO]
2169/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00002170/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00002171///
Chris Lattner4b009652007-07-25 00:24:17 +00002172void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002173 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002174
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002175 if (getLang().CPlusPlus) {
2176 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00002177 // ParseDeclaratorInternal might already have parsed the scope.
2178 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2179 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002180 if (afterCXXScope) {
2181 // Change the declaration context for name lookup, until this function
2182 // is exited (and the declarator has been parsed).
2183 DeclScopeObj.EnterDeclaratorScope();
2184 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002185
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002186 if (Tok.is(tok::identifier)) {
2187 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Anders Carlssone19759d2009-04-30 22:41:11 +00002188
2189 // If this identifier is the name of the current class, it's a
2190 // constructor name.
2191 if (!D.getDeclSpec().hasTypeSpecifier() &&
2192 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
Douglas Gregorcb0a7f72009-07-06 16:40:48 +00002193 CXXScopeSpec *SS = afterCXXScope? &D.getCXXScopeSpec() : 0;
Anders Carlssone19759d2009-04-30 22:41:11 +00002194 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorcb0a7f72009-07-06 16:40:48 +00002195 Tok.getLocation(), CurScope, SS),
Anders Carlssone19759d2009-04-30 22:41:11 +00002196 Tok.getLocation());
2197 // This is a normal identifier.
2198 } else
2199 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002200 ConsumeToken();
2201 goto PastIdentifier;
Douglas Gregor0c281a82009-02-25 19:37:18 +00002202 } else if (Tok.is(tok::annot_template_id)) {
2203 TemplateIdAnnotation *TemplateId
2204 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
2205
2206 // FIXME: Could this template-id name a constructor?
2207
2208 // FIXME: This is an egregious hack, where we silently ignore
2209 // the specialization (which should be a function template
2210 // specialization name) and use the name instead. This hack
2211 // will go away when we have support for function
2212 // specializations.
2213 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
2214 TemplateId->Destroy();
2215 ConsumeToken();
2216 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00002217 } else if (Tok.is(tok::kw_operator)) {
2218 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00002219 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00002220
Douglas Gregor853dd392008-12-26 15:00:45 +00002221 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00002222 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
2223 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00002224 } else {
2225 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00002226 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
2227 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
2228 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00002229 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00002230 }
Douglas Gregor853dd392008-12-26 15:00:45 +00002231 }
2232 goto PastIdentifier;
2233 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002234 // This should be a C++ destructor.
2235 SourceLocation TildeLoc = ConsumeToken();
2236 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002237 // FIXME: Inaccurate.
2238 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00002239 SourceLocation EndLoc;
Douglas Gregorcb0a7f72009-07-06 16:40:48 +00002240 CXXScopeSpec *SS = afterCXXScope? &D.getCXXScopeSpec() : 0;
Fariborz Jahanian1b8fd752009-07-20 17:43:15 +00002241 TypeResult Type = ParseClassName(EndLoc, SS, true);
Douglas Gregord7cb0372009-04-01 21:51:26 +00002242 if (Type.isInvalid())
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002243 D.SetIdentifier(0, TildeLoc);
Douglas Gregord7cb0372009-04-01 21:51:26 +00002244 else
2245 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002246 } else {
Fariborz Jahanian1b8fd752009-07-20 17:43:15 +00002247 Diag(Tok, diag::err_destructor_class_name);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002248 D.SetIdentifier(0, TildeLoc);
2249 }
2250 goto PastIdentifier;
2251 }
2252
2253 // If we reached this point, token is not identifier and not '~'.
2254
2255 if (afterCXXScope) {
2256 Diag(Tok, diag::err_expected_unqualified_id);
2257 D.SetIdentifier(0, Tok.getLocation());
2258 D.setInvalidType(true);
2259 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00002260 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00002261 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002262 }
2263
2264 // If we reached this point, we are either in C/ObjC or the token didn't
2265 // satisfy any of the C++-specific checks.
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002266 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2267 assert(!getLang().CPlusPlus &&
2268 "There's a C++-specific check for tok::identifier above");
2269 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2270 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2271 ConsumeToken();
2272 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002273 // direct-declarator: '(' declarator ')'
2274 // direct-declarator: '(' attributes declarator ')'
2275 // Example: 'char (*X)' or 'int (*XX)(void)'
2276 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002277 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002278 // This could be something simple like "int" (in which case the declarator
2279 // portion is empty), if an abstract-declarator is allowed.
2280 D.SetIdentifier(0, Tok.getLocation());
2281 } else {
Douglas Gregorf03265d2009-03-06 23:28:18 +00002282 if (D.getContext() == Declarator::MemberContext)
2283 Diag(Tok, diag::err_expected_member_name_or_semi)
2284 << D.getDeclSpec().getSourceRange();
2285 else if (getLang().CPlusPlus)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002286 Diag(Tok, diag::err_expected_unqualified_id);
2287 else
Chris Lattnerf006a222008-11-18 07:48:38 +00002288 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00002289 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00002290 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00002291 }
2292
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002293 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00002294 assert(D.isPastIdentifier() &&
2295 "Haven't past the location of the identifier yet?");
2296
2297 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002298 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002299 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2300 // In such a case, check if we actually have a function declarator; if it
2301 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00002302 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2303 // When not in file scope, warn for ambiguous function declarators, just
2304 // in case the author intended it as a variable definition.
2305 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2306 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2307 break;
2308 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00002309 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00002310 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002311 ParseBracketDeclarator(D);
2312 } else {
2313 break;
2314 }
2315 }
2316}
2317
Chris Lattnera0d056d2008-04-06 05:45:57 +00002318/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2319/// only called before the identifier, so these are most likely just grouping
2320/// parens for precedence. If we find that these are actually function
2321/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2322///
2323/// direct-declarator:
2324/// '(' declarator ')'
2325/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00002326/// direct-declarator '(' parameter-type-list ')'
2327/// direct-declarator '(' identifier-list[opt] ')'
2328/// [GNU] direct-declarator '(' parameter-forward-declarations
2329/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00002330///
2331void Parser::ParseParenDeclarator(Declarator &D) {
2332 SourceLocation StartLoc = ConsumeParen();
2333 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
2334
Chris Lattner1f185292008-10-20 02:05:46 +00002335 // Eat any attributes before we look at whether this is a grouping or function
2336 // declarator paren. If this is a grouping paren, the attribute applies to
2337 // the type being built up, for example:
2338 // int (__attribute__(()) *x)(long y)
2339 // If this ends up not being a grouping paren, the attribute applies to the
2340 // first argument, for example:
2341 // int (__attribute__(()) int x)
2342 // In either case, we need to eat any attributes to be able to determine what
2343 // sort of paren this is.
2344 //
2345 AttributeList *AttrList = 0;
2346 bool RequiresArg = false;
2347 if (Tok.is(tok::kw___attribute)) {
2348 AttrList = ParseAttributes();
2349
2350 // We require that the argument list (if this is a non-grouping paren) be
2351 // present even if the attribute list was empty.
2352 RequiresArg = true;
2353 }
Steve Naroffedd04d52008-12-25 14:16:32 +00002354 // Eat any Microsoft extensions.
Eli Friedman891d82f2009-06-08 23:27:34 +00002355 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2356 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2357 Tok.is(tok::kw___ptr64)) {
2358 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2359 }
Chris Lattner1f185292008-10-20 02:05:46 +00002360
Chris Lattnera0d056d2008-04-06 05:45:57 +00002361 // If we haven't past the identifier yet (or where the identifier would be
2362 // stored, if this is an abstract declarator), then this is probably just
2363 // grouping parens. However, if this could be an abstract-declarator, then
2364 // this could also be the start of function arguments (consider 'void()').
2365 bool isGrouping;
2366
2367 if (!D.mayOmitIdentifier()) {
2368 // If this can't be an abstract-declarator, this *must* be a grouping
2369 // paren, because we haven't seen the identifier yet.
2370 isGrouping = true;
2371 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002372 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00002373 isDeclarationSpecifier()) { // 'int(int)' is a function.
2374 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2375 // considered to be a type, not a K&R identifier-list.
2376 isGrouping = false;
2377 } else {
2378 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2379 isGrouping = true;
2380 }
2381
2382 // If this is a grouping paren, handle:
2383 // direct-declarator: '(' declarator ')'
2384 // direct-declarator: '(' attributes declarator ')'
2385 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002386 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002387 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00002388 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00002389 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002390
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002391 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002392 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00002393 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002394
2395 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00002396 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002397 return;
2398 }
2399
2400 // Okay, if this wasn't a grouping paren, it must be the start of a function
2401 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00002402 // identifier (and remember where it would have been), then call into
2403 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00002404 D.SetIdentifier(0, Tok.getLocation());
2405
Chris Lattner1f185292008-10-20 02:05:46 +00002406 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002407}
2408
2409/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2410/// declarator D up to a paren, which indicates that we are parsing function
2411/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00002412///
Chris Lattner1f185292008-10-20 02:05:46 +00002413/// If AttrList is non-null, then the caller parsed those arguments immediately
2414/// after the open paren - they should be considered to be the first argument of
2415/// a parameter. If RequiresArg is true, then the first argument of the
2416/// function is required to be present and required to not be an identifier
2417/// list.
2418///
Chris Lattner4b009652007-07-25 00:24:17 +00002419/// This method also handles this portion of the grammar:
2420/// parameter-type-list: [C99 6.7.5]
2421/// parameter-list
2422/// parameter-list ',' '...'
2423///
2424/// parameter-list: [C99 6.7.5]
2425/// parameter-declaration
2426/// parameter-list ',' parameter-declaration
2427///
2428/// parameter-declaration: [C99 6.7.5]
2429/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00002430/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002431/// [GNU] declaration-specifiers declarator attributes
Sebastian Redla8cecf62009-03-24 22:27:57 +00002432/// declaration-specifiers abstract-declarator[opt]
2433/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002434/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002435/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2436///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002437/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redla8cecf62009-03-24 22:27:57 +00002438/// and "exception-specification[opt]".
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002439///
Chris Lattner1f185292008-10-20 02:05:46 +00002440void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2441 AttributeList *AttrList,
2442 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002443 // lparen is already consumed!
2444 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002445
Chris Lattner1f185292008-10-20 02:05:46 +00002446 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002447 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002448 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002449 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002450 delete AttrList;
2451 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002452
Sebastian Redl0c986032009-02-09 18:23:29 +00002453 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002454
2455 // cv-qualifier-seq[opt].
2456 DeclSpec DS;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002457 bool hasExceptionSpec = false;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002458 SourceLocation ThrowLoc;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002459 bool hasAnyExceptionSpec = false;
Sebastian Redlaaacda92009-05-29 18:02:33 +00002460 llvm::SmallVector<TypeTy*, 2> Exceptions;
2461 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002462 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002463 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002464 if (!DS.getSourceRange().getEnd().isInvalid())
2465 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002466
2467 // Parse exception-specification[opt].
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002468 if (Tok.is(tok::kw_throw)) {
2469 hasExceptionSpec = true;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002470 ThrowLoc = Tok.getLocation();
Sebastian Redlaaacda92009-05-29 18:02:33 +00002471 ParseExceptionSpecification(Loc, Exceptions, ExceptionRanges,
2472 hasAnyExceptionSpec);
2473 assert(Exceptions.size() == ExceptionRanges.size() &&
2474 "Produced different number of exception types and ranges.");
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002475 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002476 }
2477
Chris Lattner9f7564b2008-04-06 06:57:35 +00002478 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002479 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002480 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002481 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002482 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002483 /*arglist*/ 0, 0,
2484 DS.getTypeQualifiers(),
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002485 hasExceptionSpec, ThrowLoc,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002486 hasAnyExceptionSpec,
Sebastian Redlaaacda92009-05-29 18:02:33 +00002487 Exceptions.data(),
2488 ExceptionRanges.data(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002489 Exceptions.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002490 LParenLoc, D),
2491 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002492 return;
Sebastian Redlaaacda92009-05-29 18:02:33 +00002493 }
2494
Chris Lattner1f185292008-10-20 02:05:46 +00002495 // Alternatively, this parameter list may be an identifier list form for a
2496 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002497 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002498 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002499 // K&R identifier lists can't have typedefs as identifiers, per
2500 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002501 if (RequiresArg) {
2502 Diag(Tok, diag::err_argument_required_after_attribute);
2503 delete AttrList;
2504 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002505 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2506 // normal declarators, not for abstract-declarators.
2507 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002508 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002509 }
2510
2511 // Finally, a normal, non-empty parameter type list.
2512
2513 // Build up an array of information about the parsed arguments.
2514 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002515
2516 // Enter function-declaration scope, limiting any declarators to the
2517 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002518 ParseScope PrototypeScope(this,
2519 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002520
2521 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002522 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002523 while (1) {
2524 if (Tok.is(tok::ellipsis)) {
2525 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002526 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002527 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002528 }
2529
Chris Lattner9f7564b2008-04-06 06:57:35 +00002530 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002531
Chris Lattner9f7564b2008-04-06 06:57:35 +00002532 // Parse the declaration-specifiers.
2533 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002534
2535 // If the caller parsed attributes for the first argument, add them now.
2536 if (AttrList) {
2537 DS.AddAttributes(AttrList);
2538 AttrList = 0; // Only apply the attributes to the first parameter.
2539 }
Chris Lattner9e785f52009-02-27 18:38:20 +00002540 ParseDeclarationSpecifiers(DS);
2541
Chris Lattner9f7564b2008-04-06 06:57:35 +00002542 // Parse the declarator. This is "PrototypeContext", because we must
2543 // accept either 'declarator' or 'abstract-declarator' here.
2544 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2545 ParseDeclarator(ParmDecl);
2546
2547 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002548 if (Tok.is(tok::kw___attribute)) {
2549 SourceLocation Loc;
2550 AttributeList *AttrList = ParseAttributes(&Loc);
2551 ParmDecl.AddAttributes(AttrList, Loc);
2552 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002553
Chris Lattner9f7564b2008-04-06 06:57:35 +00002554 // Remember this parsed parameter in ParamInfo.
2555 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2556
Douglas Gregor605de8d2008-12-16 21:30:33 +00002557 // DefArgToks is used when the parsing of default arguments needs
2558 // to be delayed.
2559 CachedTokens *DefArgToks = 0;
2560
Chris Lattner9f7564b2008-04-06 06:57:35 +00002561 // If no parameter was specified, verify that *something* was specified,
2562 // otherwise we have a missing type and identifier.
Chris Lattner9e785f52009-02-27 18:38:20 +00002563 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2564 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002565 // Completely missing, emit error.
2566 Diag(DSStart, diag::err_missing_param);
2567 } else {
2568 // Otherwise, we have something. Add it and let semantic analysis try
2569 // to grok it and add the result to the ParamInfo we are building.
2570
2571 // Inform the actions module about the parameter declarator, so it gets
2572 // added to the current scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002573 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002574
2575 // Parse the default argument, if any. We parse the default
2576 // arguments in all dialects; the semantic analysis in
2577 // ActOnParamDefaultArgument will reject the default argument in
2578 // C.
2579 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002580 SourceLocation EqualLoc = Tok.getLocation();
2581
Chris Lattner3e254fb2008-04-08 04:40:51 +00002582 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002583 if (D.getContext() == Declarator::MemberContext) {
2584 // If we're inside a class definition, cache the tokens
2585 // corresponding to the default argument. We'll actually parse
2586 // them when we see the end of the class definition.
2587 // FIXME: Templates will require something similar.
2588 // FIXME: Can we use a smart pointer for Toks?
2589 DefArgToks = new CachedTokens;
2590
2591 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2592 tok::semi, false)) {
2593 delete DefArgToks;
2594 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002595 Actions.ActOnParamDefaultArgumentError(Param);
2596 } else
Anders Carlssona116e6e2009-06-12 16:51:40 +00002597 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
2598 (*DefArgToks)[1].getLocation());
Chris Lattner3e254fb2008-04-08 04:40:51 +00002599 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002600 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002601 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002602
2603 OwningExprResult DefArgResult(ParseAssignmentExpression());
2604 if (DefArgResult.isInvalid()) {
2605 Actions.ActOnParamDefaultArgumentError(Param);
2606 SkipUntil(tok::comma, tok::r_paren, true, true);
2607 } else {
2608 // Inform the actions module about the default argument
2609 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00002610 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +00002611 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002612 }
2613 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002614
2615 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002616 ParmDecl.getIdentifierLoc(), Param,
2617 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002618 }
2619
2620 // If the next token is a comma, consume it and keep reading arguments.
2621 if (Tok.isNot(tok::comma)) break;
2622
2623 // Consume the comma.
2624 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002625 }
2626
Chris Lattner9f7564b2008-04-06 06:57:35 +00002627 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002628 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002629
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002630 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002631 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002632
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002633 DeclSpec DS;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002634 bool hasExceptionSpec = false;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002635 SourceLocation ThrowLoc;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002636 bool hasAnyExceptionSpec = false;
Sebastian Redlaaacda92009-05-29 18:02:33 +00002637 llvm::SmallVector<TypeTy*, 2> Exceptions;
2638 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002639 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002640 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002641 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002642 if (!DS.getSourceRange().getEnd().isInvalid())
2643 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002644
2645 // Parse exception-specification[opt].
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002646 if (Tok.is(tok::kw_throw)) {
2647 hasExceptionSpec = true;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002648 ThrowLoc = Tok.getLocation();
Sebastian Redlaaacda92009-05-29 18:02:33 +00002649 ParseExceptionSpecification(Loc, Exceptions, ExceptionRanges,
2650 hasAnyExceptionSpec);
2651 assert(Exceptions.size() == ExceptionRanges.size() &&
2652 "Produced different number of exception types and ranges.");
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002653 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002654 }
2655
Chris Lattner4b009652007-07-25 00:24:17 +00002656 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002657 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002658 EllipsisLoc,
Jay Foad9e6bef42009-05-21 09:52:38 +00002659 ParamInfo.data(), ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002660 DS.getTypeQualifiers(),
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002661 hasExceptionSpec, ThrowLoc,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002662 hasAnyExceptionSpec,
Sebastian Redlaaacda92009-05-29 18:02:33 +00002663 Exceptions.data(),
2664 ExceptionRanges.data(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002665 Exceptions.size(), LParenLoc, D),
Sebastian Redl0c986032009-02-09 18:23:29 +00002666 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002667}
2668
Chris Lattner35d9c912008-04-06 06:34:08 +00002669/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2670/// we found a K&R-style identifier list instead of a type argument list. The
2671/// current token is known to be the first identifier in the list.
2672///
2673/// identifier-list: [C99 6.7.5]
2674/// identifier
2675/// identifier-list ',' identifier
2676///
2677void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2678 Declarator &D) {
2679 // Build up an array of information about the parsed arguments.
2680 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2681 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2682
2683 // If there was no identifier specified for the declarator, either we are in
2684 // an abstract-declarator, or we are in a parameter declarator which was found
2685 // to be abstract. In abstract-declarators, identifier lists are not valid:
2686 // diagnose this.
2687 if (!D.getIdentifier())
2688 Diag(Tok, diag::ext_ident_list_in_param);
2689
2690 // Tok is known to be the first identifier in the list. Remember this
2691 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002692 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002693 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner5261d0c2009-03-28 19:18:32 +00002694 Tok.getLocation(),
2695 DeclPtrTy()));
Chris Lattner35d9c912008-04-06 06:34:08 +00002696
Chris Lattner113a56b2008-04-06 06:39:19 +00002697 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002698
2699 while (Tok.is(tok::comma)) {
2700 // Eat the comma.
2701 ConsumeToken();
2702
Chris Lattner113a56b2008-04-06 06:39:19 +00002703 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002704 if (Tok.isNot(tok::identifier)) {
2705 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002706 SkipUntil(tok::r_paren);
2707 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002708 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002709
Chris Lattner35d9c912008-04-06 06:34:08 +00002710 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002711
2712 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002713 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002714 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002715
2716 // Verify that the argument identifier has not already been mentioned.
2717 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002718 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002719 } else {
2720 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002721 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002722 Tok.getLocation(),
2723 DeclPtrTy()));
Chris Lattner113a56b2008-04-06 06:39:19 +00002724 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002725
2726 // Eat the identifier.
2727 ConsumeToken();
2728 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002729
2730 // If we have the closing ')', eat it and we're done.
2731 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2732
Chris Lattner113a56b2008-04-06 06:39:19 +00002733 // Remember that we parsed a function type, and remember the attributes. This
2734 // function type is always a K&R style function type, which is not varargs and
2735 // has no prototype.
2736 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002737 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002738 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002739 /*TypeQuals*/0,
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002740 /*exception*/false,
2741 SourceLocation(), false, 0, 0, 0,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002742 LParenLoc, D),
Sebastian Redl0c986032009-02-09 18:23:29 +00002743 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002744}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002745
Chris Lattner4b009652007-07-25 00:24:17 +00002746/// [C90] direct-declarator '[' constant-expression[opt] ']'
2747/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2748/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2749/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2750/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2751void Parser::ParseBracketDeclarator(Declarator &D) {
2752 SourceLocation StartLoc = ConsumeBracket();
2753
Chris Lattner1525c3a2008-12-18 07:27:21 +00002754 // C array syntax has many features, but by-far the most common is [] and [4].
2755 // This code does a fast path to handle some of the most obvious cases.
2756 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002757 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002758 // Remember that we parsed the empty array type.
2759 OwningExprResult NumElements(Actions);
Douglas Gregor1d381132009-07-06 15:59:29 +00002760 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
2761 StartLoc, EndLoc),
Sebastian Redl0c986032009-02-09 18:23:29 +00002762 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002763 return;
2764 } else if (Tok.getKind() == tok::numeric_constant &&
2765 GetLookAheadToken(1).is(tok::r_square)) {
2766 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002767 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002768 ConsumeToken();
2769
Sebastian Redl0c986032009-02-09 18:23:29 +00002770 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002771
2772 // If there was an error parsing the assignment-expression, recover.
2773 if (ExprRes.isInvalid())
2774 ExprRes.release(); // Deallocate expr, just use [].
2775
2776 // Remember that we parsed a array type, and remember its features.
Douglas Gregor1d381132009-07-06 15:59:29 +00002777 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
2778 StartLoc, EndLoc),
Sebastian Redl0c986032009-02-09 18:23:29 +00002779 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002780 return;
2781 }
2782
Chris Lattner4b009652007-07-25 00:24:17 +00002783 // If valid, this location is the position where we read the 'static' keyword.
2784 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002785 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002786 StaticLoc = ConsumeToken();
2787
2788 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002789 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002790 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002791 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002792
2793 // If we haven't already read 'static', check to see if there is one after the
2794 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002795 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002796 StaticLoc = ConsumeToken();
2797
2798 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2799 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002800 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002801
2802 // Handle the case where we have '[*]' as the array size. However, a leading
2803 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2804 // the the token after the star is a ']'. Since stars in arrays are
2805 // infrequent, use of lookahead is not costly here.
2806 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002807 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002808
Chris Lattner306d4df2008-12-18 06:50:14 +00002809 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002810 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002811 StaticLoc = SourceLocation(); // Drop the static.
2812 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002813 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002814 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002815 // Note, in C89, this production uses the constant-expr production instead
2816 // of assignment-expr. The only difference is that assignment-expr allows
2817 // things like '=' and '*='. Sema rejects these in C89 mode because they
2818 // are not i-c-e's, so we don't need to distinguish between the two here.
2819
Douglas Gregor98189262009-06-19 23:52:42 +00002820 // Parse the constant-expression or assignment-expression now (depending
2821 // on dialect).
2822 if (getLang().CPlusPlus)
2823 NumElements = ParseConstantExpression();
2824 else
2825 NumElements = ParseAssignmentExpression();
Chris Lattner4b009652007-07-25 00:24:17 +00002826 }
2827
2828 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002829 if (NumElements.isInvalid()) {
Chris Lattnerf3ce8572009-04-24 22:30:50 +00002830 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00002831 // If the expression was invalid, skip it.
2832 SkipUntil(tok::r_square);
2833 return;
2834 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002835
2836 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2837
Chris Lattner1525c3a2008-12-18 07:27:21 +00002838 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002839 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2840 StaticLoc.isValid(), isStar,
Douglas Gregor1d381132009-07-06 15:59:29 +00002841 NumElements.release(),
2842 StartLoc, EndLoc),
Sebastian Redl0c986032009-02-09 18:23:29 +00002843 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002844}
2845
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002846/// [GNU] typeof-specifier:
2847/// typeof ( expressions )
2848/// typeof ( type-name )
2849/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002850///
2851void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002852 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002853 Token OpTok = Tok;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002854 SourceLocation StartLoc = ConsumeToken();
2855
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002856 bool isCastExpr;
2857 TypeTy *CastTy;
2858 SourceRange CastRange;
2859 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
2860 isCastExpr,
2861 CastTy,
2862 CastRange);
2863
2864 if (CastRange.getEnd().isInvalid())
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002865 // FIXME: Not accurate, the range gets one token more than it should.
2866 DS.SetRangeEnd(Tok.getLocation());
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002867 else
2868 DS.SetRangeEnd(CastRange.getEnd());
2869
2870 if (isCastExpr) {
2871 if (!CastTy) {
2872 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002873 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002874 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002875
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002876 const char *PrevSpec = 0;
John McCall9f6e0972009-08-03 20:12:06 +00002877 unsigned DiagID;
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002878 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2879 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall9f6e0972009-08-03 20:12:06 +00002880 DiagID, CastTy))
2881 Diag(StartLoc, DiagID) << PrevSpec;
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002882 return;
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002883 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002884
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002885 // If we get here, the operand to the typeof was an expresion.
2886 if (Operand.isInvalid()) {
2887 DS.SetTypeSpecError();
Steve Naroff14bbce82007-08-02 02:53:48 +00002888 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002889 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002890
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002891 const char *PrevSpec = 0;
John McCall9f6e0972009-08-03 20:12:06 +00002892 unsigned DiagID;
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002893 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2894 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCall9f6e0972009-08-03 20:12:06 +00002895 DiagID, Operand.release()))
2896 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002897}