blob: 42ef7e6c290cd1fbc921e74e79f7edbba6c4d790 [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,
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000711 AccessSpecifier AS) {
Chris Lattnera4ff4272008-03-13 06:29:04 +0000712 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000713 while (1) {
John McCall9f6e0972009-08-03 20:12:06 +0000714 bool isInvalid = false;
Chris Lattner4b009652007-07-25 00:24:17 +0000715 const char *PrevSpec = 0;
John McCall9f6e0972009-08-03 20:12:06 +0000716 unsigned DiagID = 0;
717
Chris Lattner4b009652007-07-25 00:24:17 +0000718 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000719
Chris Lattner4b009652007-07-25 00:24:17 +0000720 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000721 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000722 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000723 // If this is not a declaration specifier token, we're done reading decl
724 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +0000725 DS.Finish(Diags, PP);
Chris Lattner4b009652007-07-25 00:24:17 +0000726 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000727
728 case tok::coloncolon: // ::foo::bar
729 // Annotate C++ scope specifiers. If we get one, loop.
730 if (TryAnnotateCXXScopeToken())
731 continue;
732 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000733
734 case tok::annot_cxxscope: {
735 if (DS.hasTypeSpecifier())
736 goto DoneWithDeclSpec;
737
738 // We are looking for a qualified typename.
Douglas Gregor80b95c52009-03-25 15:40:00 +0000739 Token Next = NextToken();
740 if (Next.is(tok::annot_template_id) &&
741 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregoraabb8502009-03-31 00:43:58 +0000742 ->Kind == TNK_Type_template) {
Douglas Gregor80b95c52009-03-25 15:40:00 +0000743 // We have a qualified template-id, e.g., N::A<int>
744 CXXScopeSpec SS;
745 ParseOptionalCXXScopeSpecifier(SS);
746 assert(Tok.is(tok::annot_template_id) &&
747 "ParseOptionalCXXScopeSpecifier not working");
748 AnnotateTemplateIdTokenAsType(&SS);
749 continue;
750 }
751
752 if (Next.isNot(tok::identifier))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000753 goto DoneWithDeclSpec;
754
755 CXXScopeSpec SS;
Douglas Gregor041e9292009-03-26 23:56:24 +0000756 SS.setScopeRep(Tok.getAnnotationValue());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000757 SS.setRange(Tok.getAnnotationRange());
758
759 // If the next token is the name of the class type that the C++ scope
760 // denotes, followed by a '(', then this is a constructor declaration.
761 // We're done with the decl-specifiers.
Chris Lattner52cd7622009-04-14 22:17:06 +0000762 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000763 CurScope, &SS) &&
764 GetLookAheadToken(2).is(tok::l_paren))
765 goto DoneWithDeclSpec;
766
Douglas Gregor1075a162009-02-04 17:00:24 +0000767 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
768 Next.getLocation(), CurScope, &SS);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000769
Chris Lattner52cd7622009-04-14 22:17:06 +0000770 // If the referenced identifier is not a type, then this declspec is
771 // erroneous: We already checked about that it has no type specifier, and
772 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
773 // typename.
774 if (TypeRep == 0) {
775 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000776 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000777 goto DoneWithDeclSpec;
Chris Lattner52cd7622009-04-14 22:17:06 +0000778 }
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000779
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000780 ConsumeToken(); // The C++ scope.
781
Douglas Gregora60c62e2009-02-09 15:09:02 +0000782 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall9f6e0972009-08-03 20:12:06 +0000783 DiagID, TypeRep);
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000784 if (isInvalid)
785 break;
786
787 DS.SetRangeEnd(Tok.getLocation());
788 ConsumeToken(); // The typename.
789
790 continue;
791 }
Chris Lattnerc297b722009-01-21 19:48:37 +0000792
793 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +0000794 if (Tok.getAnnotationValue())
795 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall9f6e0972009-08-03 20:12:06 +0000796 DiagID, Tok.getAnnotationValue());
Douglas Gregord7cb0372009-04-01 21:51:26 +0000797 else
798 DS.SetTypeSpecError();
Chris Lattnerc297b722009-01-21 19:48:37 +0000799 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
800 ConsumeToken(); // The typename
801
802 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
803 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
804 // Objective-C interface. If we don't have Objective-C or a '<', this is
805 // just a normal reference to a typedef name.
806 if (!Tok.is(tok::less) || !getLang().ObjC1)
807 continue;
808
809 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000810 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnerc297b722009-01-21 19:48:37 +0000811 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremeneka0c6de32009-06-30 22:19:00 +0000812 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Chris Lattnerc297b722009-01-21 19:48:37 +0000813
814 DS.SetRangeEnd(EndProtoLoc);
815 continue;
816 }
817
Chris Lattnerfda18db2008-07-26 01:18:38 +0000818 // typedef-name
819 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000820 // In C++, check to see if this is a scope specifier like foo::bar::, if
821 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000822 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
823 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000824
Chris Lattnerfda18db2008-07-26 01:18:38 +0000825 // This identifier can only be a typedef name if we haven't already seen
826 // a type-specifier. Without this check we misparse:
827 // typedef int X; struct Y { short X; }; as 'short int'.
828 if (DS.hasTypeSpecifier())
829 goto DoneWithDeclSpec;
830
831 // It has to be available as a typedef too!
Douglas Gregor1075a162009-02-04 17:00:24 +0000832 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
833 Tok.getLocation(), CurScope);
Douglas Gregor8e458f42009-02-09 18:46:07 +0000834
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000835 // If this is not a typedef name, don't parse it as part of the declspec,
836 // it must be an implicit int or an error.
837 if (TypeRep == 0) {
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000838 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000839 goto DoneWithDeclSpec;
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000840 }
Douglas Gregor8e458f42009-02-09 18:46:07 +0000841
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000842 // C++: If the identifier is actually the name of the class type
843 // being defined and the next token is a '(', then this is a
844 // constructor declaration. We're done with the decl-specifiers
845 // and will treat this token as an identifier.
Chris Lattnercc98d8c2009-04-12 20:42:31 +0000846 if (getLang().CPlusPlus && CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000847 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
848 NextToken().getKind() == tok::l_paren)
849 goto DoneWithDeclSpec;
850
Douglas Gregora60c62e2009-02-09 15:09:02 +0000851 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall9f6e0972009-08-03 20:12:06 +0000852 DiagID, TypeRep);
Chris Lattnerfda18db2008-07-26 01:18:38 +0000853 if (isInvalid)
854 break;
855
856 DS.SetRangeEnd(Tok.getLocation());
857 ConsumeToken(); // The identifier
858
859 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
860 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
861 // Objective-C interface. If we don't have Objective-C or a '<', this is
862 // just a normal reference to a typedef name.
863 if (!Tok.is(tok::less) || !getLang().ObjC1)
864 continue;
865
866 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +0000867 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000868 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremeneka0c6de32009-06-30 22:19:00 +0000869 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000870
871 DS.SetRangeEnd(EndProtoLoc);
872
Steve Narofff7683302008-09-22 10:28:57 +0000873 // Need to support trailing type qualifiers (e.g. "id<p> const").
874 // If a type specifier follows, it will be diagnosed elsewhere.
875 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000876 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000877
878 // type-name
879 case tok::annot_template_id: {
880 TemplateIdAnnotation *TemplateId
881 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregoraabb8502009-03-31 00:43:58 +0000882 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor0c281a82009-02-25 19:37:18 +0000883 // This template-id does not refer to a type name, so we're
884 // done with the type-specifiers.
885 goto DoneWithDeclSpec;
886 }
887
888 // Turn the template-id annotation token into a type annotation
889 // token, then try again to parse it as a type-specifier.
Douglas Gregord7cb0372009-04-01 21:51:26 +0000890 AnnotateTemplateIdTokenAsType();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000891 continue;
892 }
893
Chris Lattner4b009652007-07-25 00:24:17 +0000894 // GNU attributes support.
895 case tok::kw___attribute:
896 DS.AddAttributes(ParseAttributes());
897 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000898
899 // Microsoft declspec support.
900 case tok::kw___declspec:
Eli Friedmancd231842009-06-08 07:21:15 +0000901 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000902 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000903
Steve Naroffedd04d52008-12-25 14:16:32 +0000904 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000905 case tok::kw___forceinline:
Eli Friedman891d82f2009-06-08 23:27:34 +0000906 // FIXME: Add handling here!
907 break;
908
909 case tok::kw___ptr64:
Steve Naroffad620402008-12-25 14:41:26 +0000910 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000911 case tok::kw___cdecl:
912 case tok::kw___stdcall:
913 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +0000914 DS.AddAttributes(ParseMicrosoftTypeAttributes());
915 continue;
916
Chris Lattner4b009652007-07-25 00:24:17 +0000917 // storage-class-specifier
918 case tok::kw_typedef:
John McCall9f6e0972009-08-03 20:12:06 +0000919 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
920 DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000921 break;
922 case tok::kw_extern:
923 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000924 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall9f6e0972009-08-03 20:12:06 +0000925 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
926 DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000927 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000928 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000929 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCall9f6e0972009-08-03 20:12:06 +0000930 PrevSpec, DiagID);
Steve Narofff258a0f2007-12-18 00:16:02 +0000931 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000932 case tok::kw_static:
933 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000934 Diag(Tok, diag::ext_thread_before) << "static";
John McCall9f6e0972009-08-03 20:12:06 +0000935 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
936 DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000937 break;
938 case tok::kw_auto:
Anders Carlsson4a8498c2009-06-26 18:41:36 +0000939 if (getLang().CPlusPlus0x)
John McCall9f6e0972009-08-03 20:12:06 +0000940 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
941 DiagID);
Anders Carlsson4a8498c2009-06-26 18:41:36 +0000942 else
John McCall9f6e0972009-08-03 20:12:06 +0000943 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
944 DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000945 break;
946 case tok::kw_register:
John McCall9f6e0972009-08-03 20:12:06 +0000947 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
948 DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000949 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000950 case tok::kw_mutable:
John McCall9f6e0972009-08-03 20:12:06 +0000951 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
952 DiagID);
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000953 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000954 case tok::kw___thread:
John McCall9f6e0972009-08-03 20:12:06 +0000955 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000956 break;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000957
Chris Lattner4b009652007-07-25 00:24:17 +0000958 // function-specifier
959 case tok::kw_inline:
John McCall9f6e0972009-08-03 20:12:06 +0000960 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000961 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000962 case tok::kw_virtual:
John McCall9f6e0972009-08-03 20:12:06 +0000963 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000964 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000965 case tok::kw_explicit:
John McCall9f6e0972009-08-03 20:12:06 +0000966 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000967 break;
Chris Lattnerc297b722009-01-21 19:48:37 +0000968
Anders Carlsson6c2ad5a2009-05-06 04:46:28 +0000969 // friend
970 case tok::kw_friend:
John McCall9f6e0972009-08-03 20:12:06 +0000971 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
Anders Carlsson6c2ad5a2009-05-06 04:46:28 +0000972 break;
John McCall9f6e0972009-08-03 20:12:06 +0000973
Chris Lattnerc297b722009-01-21 19:48:37 +0000974 // type-specifier
975 case tok::kw_short:
John McCall9f6e0972009-08-03 20:12:06 +0000976 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
977 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +0000978 break;
979 case tok::kw_long:
980 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall9f6e0972009-08-03 20:12:06 +0000981 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
982 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +0000983 else
John McCall9f6e0972009-08-03 20:12:06 +0000984 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
985 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +0000986 break;
987 case tok::kw_signed:
John McCall9f6e0972009-08-03 20:12:06 +0000988 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
989 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +0000990 break;
991 case tok::kw_unsigned:
John McCall9f6e0972009-08-03 20:12:06 +0000992 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
993 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +0000994 break;
995 case tok::kw__Complex:
John McCall9f6e0972009-08-03 20:12:06 +0000996 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
997 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +0000998 break;
999 case tok::kw__Imaginary:
John McCall9f6e0972009-08-03 20:12:06 +00001000 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1001 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001002 break;
1003 case tok::kw_void:
John McCall9f6e0972009-08-03 20:12:06 +00001004 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1005 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001006 break;
1007 case tok::kw_char:
John McCall9f6e0972009-08-03 20:12:06 +00001008 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1009 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001010 break;
1011 case tok::kw_int:
John McCall9f6e0972009-08-03 20:12:06 +00001012 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1013 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001014 break;
1015 case tok::kw_float:
John McCall9f6e0972009-08-03 20:12:06 +00001016 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1017 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001018 break;
1019 case tok::kw_double:
John McCall9f6e0972009-08-03 20:12:06 +00001020 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1021 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001022 break;
1023 case tok::kw_wchar_t:
John McCall9f6e0972009-08-03 20:12:06 +00001024 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1025 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001026 break;
Alisdair Meredith2bcacb62009-07-14 06:30:34 +00001027 case tok::kw_char16_t:
John McCall9f6e0972009-08-03 20:12:06 +00001028 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1029 DiagID);
Alisdair Meredith2bcacb62009-07-14 06:30:34 +00001030 break;
1031 case tok::kw_char32_t:
John McCall9f6e0972009-08-03 20:12:06 +00001032 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1033 DiagID);
Alisdair Meredith2bcacb62009-07-14 06:30:34 +00001034 break;
Chris Lattnerc297b722009-01-21 19:48:37 +00001035 case tok::kw_bool:
1036 case tok::kw__Bool:
John McCall9f6e0972009-08-03 20:12:06 +00001037 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1038 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001039 break;
1040 case tok::kw__Decimal32:
John McCall9f6e0972009-08-03 20:12:06 +00001041 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1042 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001043 break;
1044 case tok::kw__Decimal64:
John McCall9f6e0972009-08-03 20:12:06 +00001045 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1046 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001047 break;
1048 case tok::kw__Decimal128:
John McCall9f6e0972009-08-03 20:12:06 +00001049 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1050 DiagID);
Chris Lattnerc297b722009-01-21 19:48:37 +00001051 break;
1052
1053 // class-specifier:
1054 case tok::kw_class:
1055 case tok::kw_struct:
Chris Lattner197b4342009-04-12 21:49:30 +00001056 case tok::kw_union: {
1057 tok::TokenKind Kind = Tok.getKind();
1058 ConsumeToken();
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001059 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +00001060 continue;
Chris Lattner197b4342009-04-12 21:49:30 +00001061 }
Chris Lattnerc297b722009-01-21 19:48:37 +00001062
1063 // enum-specifier:
1064 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +00001065 ConsumeToken();
1066 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattnerc297b722009-01-21 19:48:37 +00001067 continue;
1068
1069 // cv-qualifier:
1070 case tok::kw_const:
John McCall9f6e0972009-08-03 20:12:06 +00001071 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1072 getLang());
Chris Lattnerc297b722009-01-21 19:48:37 +00001073 break;
1074 case tok::kw_volatile:
John McCall9f6e0972009-08-03 20:12:06 +00001075 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1076 getLang());
Chris Lattnerc297b722009-01-21 19:48:37 +00001077 break;
1078 case tok::kw_restrict:
John McCall9f6e0972009-08-03 20:12:06 +00001079 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1080 getLang());
Chris Lattnerc297b722009-01-21 19:48:37 +00001081 break;
1082
Douglas Gregord3022602009-03-27 23:10:48 +00001083 // C++ typename-specifier:
1084 case tok::kw_typename:
1085 if (TryAnnotateTypeOrScopeToken())
1086 continue;
1087 break;
1088
Chris Lattnerc297b722009-01-21 19:48:37 +00001089 // GNU typeof support.
1090 case tok::kw_typeof:
1091 ParseTypeofSpecifier(DS);
1092 continue;
1093
Anders Carlssoneed418b2009-06-24 17:47:40 +00001094 case tok::kw_decltype:
1095 ParseDecltypeSpecifier(DS);
1096 continue;
1097
Steve Naroff5f0466b2008-06-05 00:02:44 +00001098 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +00001099 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +00001100 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1101 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +00001102 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +00001103 goto DoneWithDeclSpec;
1104
1105 {
1106 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001107 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +00001108 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremeneka0c6de32009-06-30 22:19:00 +00001109 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +00001110 DS.SetRangeEnd(EndProtoLoc);
1111
Chris Lattnerf006a222008-11-18 07:48:38 +00001112 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattnerb980c732009-04-03 18:38:42 +00001113 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattnerf006a222008-11-18 07:48:38 +00001114 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +00001115 // Need to support trailing type qualifiers (e.g. "id<p> const").
1116 // If a type specifier follows, it will be diagnosed elsewhere.
1117 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +00001118 }
Chris Lattner4b009652007-07-25 00:24:17 +00001119 }
John McCall9f6e0972009-08-03 20:12:06 +00001120 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattner4b009652007-07-25 00:24:17 +00001121 if (isInvalid) {
1122 assert(PrevSpec && "Method did not return previous specifier!");
John McCall9f6e0972009-08-03 20:12:06 +00001123 assert(DiagID);
Chris Lattnerf006a222008-11-18 07:48:38 +00001124 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001125 }
Chris Lattnera4ff4272008-03-13 06:29:04 +00001126 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001127 ConsumeToken();
1128 }
1129}
Douglas Gregorb3bec712008-12-01 23:54:00 +00001130
Chris Lattnerd706dc82009-01-06 06:59:53 +00001131/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001132/// primarily follow the C++ grammar with additions for C99 and GNU,
1133/// which together subsume the C grammar. Note that the C++
1134/// type-specifier also includes the C type-qualifier (for const,
1135/// volatile, and C99 restrict). Returns true if a type-specifier was
1136/// found (and parsed), false otherwise.
1137///
1138/// type-specifier: [C++ 7.1.5]
1139/// simple-type-specifier
1140/// class-specifier
1141/// enum-specifier
1142/// elaborated-type-specifier [TODO]
1143/// cv-qualifier
1144///
1145/// cv-qualifier: [C++ 7.1.5.1]
1146/// 'const'
1147/// 'volatile'
1148/// [C99] 'restrict'
1149///
1150/// simple-type-specifier: [ C++ 7.1.5.2]
1151/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1152/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1153/// 'char'
1154/// 'wchar_t'
1155/// 'bool'
1156/// 'short'
1157/// 'int'
1158/// 'long'
1159/// 'signed'
1160/// 'unsigned'
1161/// 'float'
1162/// 'double'
1163/// 'void'
1164/// [C99] '_Bool'
1165/// [C99] '_Complex'
1166/// [C99] '_Imaginary' // Removed in TC2?
1167/// [GNU] '_Decimal32'
1168/// [GNU] '_Decimal64'
1169/// [GNU] '_Decimal128'
1170/// [GNU] typeof-specifier
1171/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1172/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlssoneed418b2009-06-24 17:47:40 +00001173/// [C++0x] 'decltype' ( expression )
John McCall9f6e0972009-08-03 20:12:06 +00001174bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnerd706dc82009-01-06 06:59:53 +00001175 const char *&PrevSpec,
John McCall9f6e0972009-08-03 20:12:06 +00001176 unsigned &DiagID,
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001177 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001178 SourceLocation Loc = Tok.getLocation();
1179
1180 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +00001181 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001182 case tok::kw_typename: // typename foo::bar
Chris Lattnerb75fde62009-01-04 23:41:41 +00001183 // Annotate typenames and C++ scope specifiers. If we get one, just
1184 // recurse to handle whatever we get.
1185 if (TryAnnotateTypeOrScopeToken())
John McCall9f6e0972009-08-03 20:12:06 +00001186 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1187 TemplateInfo);
Chris Lattnerb75fde62009-01-04 23:41:41 +00001188 // Otherwise, not a type specifier.
1189 return false;
1190 case tok::coloncolon: // ::foo::bar
1191 if (NextToken().is(tok::kw_new) || // ::new
1192 NextToken().is(tok::kw_delete)) // ::delete
1193 return false;
1194
1195 // Annotate typenames and C++ scope specifiers. If we get one, just
1196 // recurse to handle whatever we get.
1197 if (TryAnnotateTypeOrScopeToken())
John McCall9f6e0972009-08-03 20:12:06 +00001198 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1199 TemplateInfo);
Chris Lattnerb75fde62009-01-04 23:41:41 +00001200 // Otherwise, not a type specifier.
1201 return false;
1202
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001203 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +00001204 case tok::annot_typename: {
Douglas Gregord7cb0372009-04-01 21:51:26 +00001205 if (Tok.getAnnotationValue())
1206 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall9f6e0972009-08-03 20:12:06 +00001207 DiagID, Tok.getAnnotationValue());
Douglas Gregord7cb0372009-04-01 21:51:26 +00001208 else
1209 DS.SetTypeSpecError();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001210 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1211 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001212
1213 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1214 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1215 // Objective-C interface. If we don't have Objective-C or a '<', this is
1216 // just a normal reference to a typedef name.
1217 if (!Tok.is(tok::less) || !getLang().ObjC1)
1218 return true;
1219
1220 SourceLocation EndProtoLoc;
Chris Lattner5261d0c2009-03-28 19:18:32 +00001221 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001222 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremeneka0c6de32009-06-30 22:19:00 +00001223 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001224
1225 DS.SetRangeEnd(EndProtoLoc);
1226 return true;
1227 }
1228
1229 case tok::kw_short:
John McCall9f6e0972009-08-03 20:12:06 +00001230 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001231 break;
1232 case tok::kw_long:
1233 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall9f6e0972009-08-03 20:12:06 +00001234 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1235 DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001236 else
John McCall9f6e0972009-08-03 20:12:06 +00001237 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1238 DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001239 break;
1240 case tok::kw_signed:
John McCall9f6e0972009-08-03 20:12:06 +00001241 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001242 break;
1243 case tok::kw_unsigned:
John McCall9f6e0972009-08-03 20:12:06 +00001244 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1245 DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001246 break;
1247 case tok::kw__Complex:
John McCall9f6e0972009-08-03 20:12:06 +00001248 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1249 DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001250 break;
1251 case tok::kw__Imaginary:
John McCall9f6e0972009-08-03 20:12:06 +00001252 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1253 DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001254 break;
1255 case tok::kw_void:
John McCall9f6e0972009-08-03 20:12:06 +00001256 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001257 break;
1258 case tok::kw_char:
John McCall9f6e0972009-08-03 20:12:06 +00001259 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001260 break;
1261 case tok::kw_int:
John McCall9f6e0972009-08-03 20:12:06 +00001262 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001263 break;
1264 case tok::kw_float:
John McCall9f6e0972009-08-03 20:12:06 +00001265 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001266 break;
1267 case tok::kw_double:
John McCall9f6e0972009-08-03 20:12:06 +00001268 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001269 break;
1270 case tok::kw_wchar_t:
John McCall9f6e0972009-08-03 20:12:06 +00001271 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001272 break;
Alisdair Meredith2bcacb62009-07-14 06:30:34 +00001273 case tok::kw_char16_t:
John McCall9f6e0972009-08-03 20:12:06 +00001274 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredith2bcacb62009-07-14 06:30:34 +00001275 break;
1276 case tok::kw_char32_t:
John McCall9f6e0972009-08-03 20:12:06 +00001277 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredith2bcacb62009-07-14 06:30:34 +00001278 break;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001279 case tok::kw_bool:
1280 case tok::kw__Bool:
John McCall9f6e0972009-08-03 20:12:06 +00001281 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001282 break;
1283 case tok::kw__Decimal32:
John McCall9f6e0972009-08-03 20:12:06 +00001284 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1285 DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001286 break;
1287 case tok::kw__Decimal64:
John McCall9f6e0972009-08-03 20:12:06 +00001288 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1289 DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001290 break;
1291 case tok::kw__Decimal128:
John McCall9f6e0972009-08-03 20:12:06 +00001292 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1293 DiagID);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001294 break;
1295
1296 // class-specifier:
1297 case tok::kw_class:
1298 case tok::kw_struct:
Chris Lattner197b4342009-04-12 21:49:30 +00001299 case tok::kw_union: {
1300 tok::TokenKind Kind = Tok.getKind();
1301 ConsumeToken();
Douglas Gregora9db0fa2009-05-12 23:25:50 +00001302 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001303 return true;
Chris Lattner197b4342009-04-12 21:49:30 +00001304 }
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001305
1306 // enum-specifier:
1307 case tok::kw_enum:
Chris Lattner197b4342009-04-12 21:49:30 +00001308 ConsumeToken();
1309 ParseEnumSpecifier(Loc, DS);
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001310 return true;
1311
1312 // cv-qualifier:
1313 case tok::kw_const:
1314 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall9f6e0972009-08-03 20:12:06 +00001315 DiagID, getLang());
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001316 break;
1317 case tok::kw_volatile:
1318 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall9f6e0972009-08-03 20:12:06 +00001319 DiagID, getLang());
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001320 break;
1321 case tok::kw_restrict:
1322 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall9f6e0972009-08-03 20:12:06 +00001323 DiagID, getLang());
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001324 break;
1325
1326 // GNU typeof support.
1327 case tok::kw_typeof:
1328 ParseTypeofSpecifier(DS);
1329 return true;
1330
Anders Carlssoneed418b2009-06-24 17:47:40 +00001331 // C++0x decltype support.
1332 case tok::kw_decltype:
1333 ParseDecltypeSpecifier(DS);
1334 return true;
1335
Anders Carlsson7e023eb2009-06-26 23:44:14 +00001336 // C++0x auto support.
1337 case tok::kw_auto:
1338 if (!getLang().CPlusPlus0x)
1339 return false;
1340
John McCall9f6e0972009-08-03 20:12:06 +00001341 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson7e023eb2009-06-26 23:44:14 +00001342 break;
Eli Friedman891d82f2009-06-08 23:27:34 +00001343 case tok::kw___ptr64:
1344 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001345 case tok::kw___cdecl:
1346 case tok::kw___stdcall:
1347 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001348 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner5bb837e2009-01-21 19:19:26 +00001349 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +00001350
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001351 default:
1352 // Not a type-specifier; do nothing.
1353 return false;
1354 }
1355
1356 // If the specifier combination wasn't legal, issue a diagnostic.
1357 if (isInvalid) {
1358 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001359 // Pick between error or extwarn.
Chris Lattnerf006a222008-11-18 07:48:38 +00001360 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +00001361 }
1362 DS.SetRangeEnd(Tok.getLocation());
1363 ConsumeToken(); // whatever we parsed above.
1364 return true;
1365}
Chris Lattner4b009652007-07-25 00:24:17 +00001366
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001367/// ParseStructDeclaration - Parse a struct declaration without the terminating
1368/// semicolon.
1369///
Chris Lattner4b009652007-07-25 00:24:17 +00001370/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001371/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +00001372/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001373/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +00001374/// struct-declarator-list:
1375/// struct-declarator
1376/// struct-declarator-list ',' struct-declarator
1377/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1378/// struct-declarator:
1379/// declarator
1380/// [GNU] declarator attributes[opt]
1381/// declarator[opt] ':' constant-expression
1382/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1383///
Chris Lattner3dd8d392008-04-10 06:46:29 +00001384void Parser::
1385ParseStructDeclaration(DeclSpec &DS,
1386 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001387 if (Tok.is(tok::kw___extension__)) {
1388 // __extension__ silences extension warnings in the subexpression.
1389 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +00001390 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +00001391 return ParseStructDeclaration(DS, Fields);
1392 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001393
1394 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001395 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +00001396 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001397
Douglas Gregorb748fc52009-01-12 22:49:06 +00001398 // If there are no declarators, this is a free-standing declaration
1399 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001400 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +00001401 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +00001402 return;
1403 }
1404
1405 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001406 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +00001407 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +00001408 FieldDeclarator &DeclaratorInfo = Fields.back();
1409
Steve Naroffa9adf112007-08-20 22:28:22 +00001410 /// struct-declarator: declarator
1411 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +00001412 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +00001413 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +00001414
Chris Lattner34a01ad2007-10-09 17:33:22 +00001415 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +00001416 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +00001417 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001418 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +00001419 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +00001420 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001421 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +00001422 }
Sebastian Redl0c986032009-02-09 18:23:29 +00001423
Steve Naroffa9adf112007-08-20 22:28:22 +00001424 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001425 if (Tok.is(tok::kw___attribute)) {
1426 SourceLocation Loc;
1427 AttributeList *AttrList = ParseAttributes(&Loc);
1428 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1429 }
1430
Steve Naroffa9adf112007-08-20 22:28:22 +00001431 // If we don't have a comma, it is either the end of the list (a ';')
1432 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001433 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +00001434 return;
Sebastian Redl0c986032009-02-09 18:23:29 +00001435
Steve Naroffa9adf112007-08-20 22:28:22 +00001436 // Consume the comma.
1437 ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00001438
Steve Naroffa9adf112007-08-20 22:28:22 +00001439 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +00001440 Fields.push_back(FieldDeclarator(DS));
Sebastian Redl0c986032009-02-09 18:23:29 +00001441
Steve Naroffa9adf112007-08-20 22:28:22 +00001442 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001443 if (Tok.is(tok::kw___attribute)) {
1444 SourceLocation Loc;
1445 AttributeList *AttrList = ParseAttributes(&Loc);
1446 Fields.back().D.AddAttributes(AttrList, Loc);
1447 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001448 }
Steve Naroffa9adf112007-08-20 22:28:22 +00001449}
1450
1451/// ParseStructUnionBody
1452/// struct-contents:
1453/// struct-declaration-list
1454/// [EXT] empty
1455/// [GNU] "struct-declaration-list" without terminatoring ';'
1456/// struct-declaration-list:
1457/// struct-declaration
1458/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +00001459/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +00001460///
Chris Lattner4b009652007-07-25 00:24:17 +00001461void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001462 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnerc309ade2009-03-05 08:00:35 +00001463 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1464 PP.getSourceManager(),
1465 "parsing struct/union body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001466
Chris Lattner4b009652007-07-25 00:24:17 +00001467 SourceLocation LBraceLoc = ConsumeBrace();
1468
Douglas Gregorcab994d2009-01-09 22:42:13 +00001469 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001470 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1471
Chris Lattner4b009652007-07-25 00:24:17 +00001472 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1473 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +00001474 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001475 Diag(Tok, diag::ext_empty_struct_union_enum)
1476 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +00001477
Chris Lattner5261d0c2009-03-28 19:18:32 +00001478 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +00001479 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1480
Chris Lattner4b009652007-07-25 00:24:17 +00001481 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001482 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001483 // Each iteration of this loop reads one struct-declaration.
1484
1485 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001486 if (Tok.is(tok::semi)) {
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001487 Diag(Tok, diag::ext_extra_struct_semi)
1488 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +00001489 ConsumeToken();
1490 continue;
1491 }
Chris Lattner3dd8d392008-04-10 06:46:29 +00001492
1493 // Parse all the comma separated declarators.
1494 DeclSpec DS;
1495 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +00001496 if (!Tok.is(tok::at)) {
1497 ParseStructDeclaration(DS, FieldDeclarators);
1498
1499 // Convert them all to fields.
1500 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1501 FieldDeclarator &FD = FieldDeclarators[i];
1502 // Install the declarator into the current TagDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001503 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1504 DS.getSourceRange().getBegin(),
1505 FD.D, FD.BitfieldSize);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001506 FieldDecls.push_back(Field);
1507 }
1508 } else { // Handle @defs
1509 ConsumeToken();
1510 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1511 Diag(Tok, diag::err_unexpected_at);
1512 SkipUntil(tok::semi, true, true);
1513 continue;
1514 }
1515 ConsumeToken();
1516 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1517 if (!Tok.is(tok::identifier)) {
1518 Diag(Tok, diag::err_expected_ident);
1519 SkipUntil(tok::semi, true, true);
1520 continue;
1521 }
Chris Lattner5261d0c2009-03-28 19:18:32 +00001522 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001523 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1524 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001525 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1526 ConsumeToken();
1527 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1528 }
Chris Lattner4b009652007-07-25 00:24:17 +00001529
Chris Lattner34a01ad2007-10-09 17:33:22 +00001530 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001531 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001532 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001533 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001534 break;
1535 } else {
1536 Diag(Tok, diag::err_expected_semi_decl_list);
1537 // Skip to end of block or statement
1538 SkipUntil(tok::r_brace, true, true);
1539 }
1540 }
1541
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001542 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001543
Chris Lattner4b009652007-07-25 00:24:17 +00001544 AttributeList *AttrList = 0;
1545 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001546 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001547 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001548
1549 Actions.ActOnFields(CurScope,
Jay Foad9e6bef42009-05-21 09:52:38 +00001550 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbarf3944442008-10-03 02:03:53 +00001551 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001552 AttrList);
1553 StructScope.Exit();
Argiris Kirtzidiseb925642009-07-14 03:17:52 +00001554 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001555}
1556
1557
1558/// ParseEnumSpecifier
1559/// enum-specifier: [C99 6.7.2.2]
1560/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001561///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001562/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1563/// '}' attributes[opt]
1564/// 'enum' identifier
1565/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001566///
1567/// [C++] elaborated-type-specifier:
1568/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1569///
Chris Lattner197b4342009-04-12 21:49:30 +00001570void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1571 AccessSpecifier AS) {
Chris Lattner4b009652007-07-25 00:24:17 +00001572 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001573
1574 AttributeList *Attr = 0;
1575 // If attributes exist after tag, parse them.
1576 if (Tok.is(tok::kw___attribute))
1577 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001578
1579 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001580 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001581 if (Tok.isNot(tok::identifier)) {
1582 Diag(Tok, diag::err_expected_ident);
1583 if (Tok.isNot(tok::l_brace)) {
1584 // Has no name and is not a definition.
1585 // Skip the rest of this declarator, up until the comma or semicolon.
1586 SkipUntil(tok::comma, true);
1587 return;
1588 }
1589 }
1590 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001591
1592 // Must have either 'enum name' or 'enum {...}'.
1593 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1594 Diag(Tok, diag::err_expected_ident_lbrace);
1595
1596 // Skip the rest of this declarator, up until the comma or semicolon.
1597 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001598 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001599 }
1600
1601 // If an identifier is present, consume and remember it.
1602 IdentifierInfo *Name = 0;
1603 SourceLocation NameLoc;
1604 if (Tok.is(tok::identifier)) {
1605 Name = Tok.getIdentifierInfo();
1606 NameLoc = ConsumeToken();
1607 }
1608
1609 // There are three options here. If we have 'enum foo;', then this is a
1610 // forward declaration. If we have 'enum foo {...' then this is a
1611 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1612 //
1613 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1614 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1615 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1616 //
John McCall069c23a2009-07-31 02:45:11 +00001617 Action::TagUseKind TUK;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001618 if (Tok.is(tok::l_brace))
John McCall069c23a2009-07-31 02:45:11 +00001619 TUK = Action::TUK_Definition;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001620 else if (Tok.is(tok::semi))
John McCall069c23a2009-07-31 02:45:11 +00001621 TUK = Action::TUK_Declaration;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001622 else
John McCall069c23a2009-07-31 02:45:11 +00001623 TUK = Action::TUK_Reference;
Douglas Gregor71f06032009-05-28 23:31:59 +00001624 bool Owned = false;
John McCall069c23a2009-07-31 02:45:11 +00001625 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Douglas Gregor71f06032009-05-28 23:31:59 +00001626 StartLoc, SS, Name, NameLoc, Attr, AS,
Douglas Gregor95254bc2009-07-23 16:36:45 +00001627 Action::MultiTemplateParamsArg(Actions),
Douglas Gregor71f06032009-05-28 23:31:59 +00001628 Owned);
Chris Lattner4b009652007-07-25 00:24:17 +00001629
Chris Lattner34a01ad2007-10-09 17:33:22 +00001630 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001631 ParseEnumBody(StartLoc, TagDecl);
1632
1633 // TODO: semantic analysis on the declspec for enums.
1634 const char *PrevSpec = 0;
John McCall9f6e0972009-08-03 20:12:06 +00001635 unsigned DiagID;
1636 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, DiagID,
Douglas Gregor71f06032009-05-28 23:31:59 +00001637 TagDecl.getAs<void>(), Owned))
John McCall9f6e0972009-08-03 20:12:06 +00001638 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001639}
1640
1641/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1642/// enumerator-list:
1643/// enumerator
1644/// enumerator-list ',' enumerator
1645/// enumerator:
1646/// enumeration-constant
1647/// enumeration-constant '=' constant-expression
1648/// enumeration-constant:
1649/// identifier
1650///
Chris Lattner5261d0c2009-03-28 19:18:32 +00001651void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001652 // Enter the scope of the enum body and start the definition.
1653 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001654 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001655
Chris Lattner4b009652007-07-25 00:24:17 +00001656 SourceLocation LBraceLoc = ConsumeBrace();
1657
Chris Lattnerc9a92452007-08-27 17:24:30 +00001658 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001659 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001660 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001661
Chris Lattner5261d0c2009-03-28 19:18:32 +00001662 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattner4b009652007-07-25 00:24:17 +00001663
Chris Lattner5261d0c2009-03-28 19:18:32 +00001664 DeclPtrTy LastEnumConstDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001665
1666 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001667 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001668 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1669 SourceLocation IdentLoc = ConsumeToken();
1670
1671 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001672 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001673 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001674 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001675 AssignedVal = ParseConstantExpression();
1676 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001677 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001678 }
1679
1680 // Install the enumerator constant into EnumDecl.
Chris Lattner5261d0c2009-03-28 19:18:32 +00001681 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1682 LastEnumConstDecl,
1683 IdentLoc, Ident,
1684 EqualLoc,
1685 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001686 EnumConstantDecls.push_back(EnumConstDecl);
1687 LastEnumConstDecl = EnumConstDecl;
1688
Chris Lattner34a01ad2007-10-09 17:33:22 +00001689 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001690 break;
1691 SourceLocation CommaLoc = ConsumeToken();
1692
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001693 if (Tok.isNot(tok::identifier) &&
1694 !(getLang().C99 || getLang().CPlusPlus0x))
1695 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1696 << getLang().CPlusPlus
1697 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Chris Lattner4b009652007-07-25 00:24:17 +00001698 }
1699
1700 // Eat the }.
Mike Stump155750e2009-05-16 07:06:02 +00001701 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001702
Mike Stump155750e2009-05-16 07:06:02 +00001703 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
Jay Foad9e6bef42009-05-21 09:52:38 +00001704 EnumConstantDecls.data(), EnumConstantDecls.size());
Chris Lattner4b009652007-07-25 00:24:17 +00001705
Chris Lattner5261d0c2009-03-28 19:18:32 +00001706 Action::AttrTy *AttrList = 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001707 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001708 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001709 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001710
1711 EnumScope.Exit();
Argiris Kirtzidiseb925642009-07-14 03:17:52 +00001712 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001713}
1714
1715/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001716/// start of a type-qualifier-list.
1717bool Parser::isTypeQualifier() const {
1718 switch (Tok.getKind()) {
1719 default: return false;
1720 // type-qualifier
1721 case tok::kw_const:
1722 case tok::kw_volatile:
1723 case tok::kw_restrict:
1724 return true;
1725 }
1726}
1727
1728/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001729/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001730bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001731 switch (Tok.getKind()) {
1732 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001733
1734 case tok::identifier: // foo::bar
Douglas Gregord3022602009-03-27 23:10:48 +00001735 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001736 // Annotate typenames and C++ scope specifiers. If we get one, just
1737 // recurse to handle whatever we get.
1738 if (TryAnnotateTypeOrScopeToken())
1739 return isTypeSpecifierQualifier();
1740 // Otherwise, not a type specifier.
1741 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001742
Chris Lattnerb75fde62009-01-04 23:41:41 +00001743 case tok::coloncolon: // ::foo::bar
1744 if (NextToken().is(tok::kw_new) || // ::new
1745 NextToken().is(tok::kw_delete)) // ::delete
1746 return false;
1747
1748 // Annotate typenames and C++ scope specifiers. If we get one, just
1749 // recurse to handle whatever we get.
1750 if (TryAnnotateTypeOrScopeToken())
1751 return isTypeSpecifierQualifier();
1752 // Otherwise, not a type specifier.
1753 return false;
1754
Chris Lattner4b009652007-07-25 00:24:17 +00001755 // GNU attributes support.
1756 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001757 // GNU typeof support.
1758 case tok::kw_typeof:
1759
Chris Lattner4b009652007-07-25 00:24:17 +00001760 // type-specifiers
1761 case tok::kw_short:
1762 case tok::kw_long:
1763 case tok::kw_signed:
1764 case tok::kw_unsigned:
1765 case tok::kw__Complex:
1766 case tok::kw__Imaginary:
1767 case tok::kw_void:
1768 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001769 case tok::kw_wchar_t:
Alisdair Meredith2bcacb62009-07-14 06:30:34 +00001770 case tok::kw_char16_t:
1771 case tok::kw_char32_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001772 case tok::kw_int:
1773 case tok::kw_float:
1774 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001775 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001776 case tok::kw__Bool:
1777 case tok::kw__Decimal32:
1778 case tok::kw__Decimal64:
1779 case tok::kw__Decimal128:
1780
Chris Lattner2e78db32008-04-13 18:59:07 +00001781 // struct-or-union-specifier (C99) or class-specifier (C++)
1782 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001783 case tok::kw_struct:
1784 case tok::kw_union:
1785 // enum-specifier
1786 case tok::kw_enum:
1787
1788 // type-qualifier
1789 case tok::kw_const:
1790 case tok::kw_volatile:
1791 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001792
1793 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001794 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001795 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001796
1797 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1798 case tok::less:
1799 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001800
1801 case tok::kw___cdecl:
1802 case tok::kw___stdcall:
1803 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001804 case tok::kw___w64:
1805 case tok::kw___ptr64:
1806 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001807 }
1808}
1809
1810/// isDeclarationSpecifier() - Return true if the current token is part of a
1811/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001812bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001813 switch (Tok.getKind()) {
1814 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001815
1816 case tok::identifier: // foo::bar
Steve Naroff73ec9322009-03-09 21:12:44 +00001817 // Unfortunate hack to support "Class.factoryMethod" notation.
1818 if (getLang().ObjC1 && NextToken().is(tok::period))
1819 return false;
Douglas Gregord3022602009-03-27 23:10:48 +00001820 // Fall through
Steve Naroff73ec9322009-03-09 21:12:44 +00001821
Douglas Gregord3022602009-03-27 23:10:48 +00001822 case tok::kw_typename: // typename T::type
Chris Lattnerb75fde62009-01-04 23:41:41 +00001823 // Annotate typenames and C++ scope specifiers. If we get one, just
1824 // recurse to handle whatever we get.
1825 if (TryAnnotateTypeOrScopeToken())
1826 return isDeclarationSpecifier();
1827 // Otherwise, not a declaration specifier.
1828 return false;
1829 case tok::coloncolon: // ::foo::bar
1830 if (NextToken().is(tok::kw_new) || // ::new
1831 NextToken().is(tok::kw_delete)) // ::delete
1832 return false;
1833
1834 // Annotate typenames and C++ scope specifiers. If we get one, just
1835 // recurse to handle whatever we get.
1836 if (TryAnnotateTypeOrScopeToken())
1837 return isDeclarationSpecifier();
1838 // Otherwise, not a declaration specifier.
1839 return false;
1840
Chris Lattner4b009652007-07-25 00:24:17 +00001841 // storage-class-specifier
1842 case tok::kw_typedef:
1843 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001844 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001845 case tok::kw_static:
1846 case tok::kw_auto:
1847 case tok::kw_register:
1848 case tok::kw___thread:
1849
1850 // type-specifiers
1851 case tok::kw_short:
1852 case tok::kw_long:
1853 case tok::kw_signed:
1854 case tok::kw_unsigned:
1855 case tok::kw__Complex:
1856 case tok::kw__Imaginary:
1857 case tok::kw_void:
1858 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001859 case tok::kw_wchar_t:
Alisdair Meredith2bcacb62009-07-14 06:30:34 +00001860 case tok::kw_char16_t:
1861 case tok::kw_char32_t:
1862
Chris Lattner4b009652007-07-25 00:24:17 +00001863 case tok::kw_int:
1864 case tok::kw_float:
1865 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001866 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001867 case tok::kw__Bool:
1868 case tok::kw__Decimal32:
1869 case tok::kw__Decimal64:
1870 case tok::kw__Decimal128:
1871
Chris Lattner2e78db32008-04-13 18:59:07 +00001872 // struct-or-union-specifier (C99) or class-specifier (C++)
1873 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001874 case tok::kw_struct:
1875 case tok::kw_union:
1876 // enum-specifier
1877 case tok::kw_enum:
1878
1879 // type-qualifier
1880 case tok::kw_const:
1881 case tok::kw_volatile:
1882 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001883
Chris Lattner4b009652007-07-25 00:24:17 +00001884 // function-specifier
1885 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001886 case tok::kw_virtual:
1887 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001888
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001889 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001890 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001891
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001892 // GNU typeof support.
1893 case tok::kw_typeof:
1894
1895 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001896 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001897 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001898
1899 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1900 case tok::less:
1901 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001902
Steve Naroffab1a3632009-01-06 19:34:12 +00001903 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001904 case tok::kw___cdecl:
1905 case tok::kw___stdcall:
1906 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001907 case tok::kw___w64:
1908 case tok::kw___ptr64:
1909 case tok::kw___forceinline:
1910 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001911 }
1912}
1913
1914
1915/// ParseTypeQualifierListOpt
1916/// type-qualifier-list: [C99 6.7.5]
1917/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001918/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001919/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001920/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001921///
Chris Lattner460696f2008-12-18 07:02:59 +00001922void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001923 while (1) {
John McCall9f6e0972009-08-03 20:12:06 +00001924 bool isInvalid = false;
Chris Lattner4b009652007-07-25 00:24:17 +00001925 const char *PrevSpec = 0;
John McCall9f6e0972009-08-03 20:12:06 +00001926 unsigned DiagID = 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001927 SourceLocation Loc = Tok.getLocation();
1928
1929 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001930 case tok::kw_const:
John McCall9f6e0972009-08-03 20:12:06 +00001931 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
1932 getLang());
Chris Lattner4b009652007-07-25 00:24:17 +00001933 break;
1934 case tok::kw_volatile:
John McCall9f6e0972009-08-03 20:12:06 +00001935 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1936 getLang());
Chris Lattner4b009652007-07-25 00:24:17 +00001937 break;
1938 case tok::kw_restrict:
John McCall9f6e0972009-08-03 20:12:06 +00001939 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1940 getLang());
Chris Lattner4b009652007-07-25 00:24:17 +00001941 break;
Eli Friedman891d82f2009-06-08 23:27:34 +00001942 case tok::kw___w64:
Steve Naroffad620402008-12-25 14:41:26 +00001943 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001944 case tok::kw___cdecl:
1945 case tok::kw___stdcall:
1946 case tok::kw___fastcall:
Eli Friedman891d82f2009-06-08 23:27:34 +00001947 if (AttributesAllowed) {
1948 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1949 continue;
1950 }
1951 goto DoneWithTypeQuals;
Chris Lattner4b009652007-07-25 00:24:17 +00001952 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001953 if (AttributesAllowed) {
1954 DS.AddAttributes(ParseAttributes());
1955 continue; // do *not* consume the next token!
1956 }
1957 // otherwise, FALL THROUGH!
1958 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001959 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001960 // If this is not a type-qualifier token, we're done reading type
1961 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor1ba5cb32009-04-01 22:41:11 +00001962 DS.Finish(Diags, PP);
Chris Lattner460696f2008-12-18 07:02:59 +00001963 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001964 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001965
Chris Lattner4b009652007-07-25 00:24:17 +00001966 // If the specifier combination wasn't legal, issue a diagnostic.
1967 if (isInvalid) {
1968 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001969 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001970 }
1971 ConsumeToken();
1972 }
1973}
1974
1975
1976/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1977///
1978void Parser::ParseDeclarator(Declarator &D) {
1979 /// This implements the 'declarator' production in the C grammar, then checks
1980 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001981 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001982}
1983
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001984/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1985/// is parsed by the function passed to it. Pass null, and the direct-declarator
1986/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001987/// ptr-operator production.
1988///
Sebastian Redl75555032009-01-24 21:16:55 +00001989/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1990/// [C] pointer[opt] direct-declarator
1991/// [C++] direct-declarator
1992/// [C++] ptr-operator declarator
Chris Lattner4b009652007-07-25 00:24:17 +00001993///
1994/// pointer: [C99 6.7.5]
1995/// '*' type-qualifier-list[opt]
1996/// '*' type-qualifier-list[opt] pointer
1997///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001998/// ptr-operator:
1999/// '*' cv-qualifier-seq[opt]
2000/// '&'
Sebastian Redl9951dbc2009-03-15 22:02:01 +00002001/// [C++0x] '&&'
Douglas Gregor3ef6c972008-11-07 20:08:42 +00002002/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl9951dbc2009-03-15 22:02:01 +00002003/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl75555032009-01-24 21:16:55 +00002004/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002005void Parser::ParseDeclaratorInternal(Declarator &D,
2006 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00002007
Sebastian Redl75555032009-01-24 21:16:55 +00002008 // C++ member pointers start with a '::' or a nested-name.
2009 // Member pointers get special handling, since there's no place for the
2010 // scope spec in the generic path below.
Chris Lattner053dd2d2009-03-24 17:04:48 +00002011 if (getLang().CPlusPlus &&
2012 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2013 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl75555032009-01-24 21:16:55 +00002014 CXXScopeSpec SS;
2015 if (ParseOptionalCXXScopeSpecifier(SS)) {
2016 if(Tok.isNot(tok::star)) {
2017 // The scope spec really belongs to the direct-declarator.
2018 D.getCXXScopeSpec() = SS;
2019 if (DirectDeclParser)
2020 (this->*DirectDeclParser)(D);
2021 return;
2022 }
2023
2024 SourceLocation Loc = ConsumeToken();
Sebastian Redl0c986032009-02-09 18:23:29 +00002025 D.SetRangeEnd(Loc);
Sebastian Redl75555032009-01-24 21:16:55 +00002026 DeclSpec DS;
2027 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00002028 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00002029
2030 // Recurse to parse whatever is left.
2031 ParseDeclaratorInternal(D, DirectDeclParser);
2032
2033 // Sema will have to catch (syntactically invalid) pointers into global
2034 // scope. It has to catch pointers into namespace scope anyway.
2035 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002036 Loc, DS.TakeAttributes()),
2037 /* Don't replace range end. */SourceLocation());
Sebastian Redl75555032009-01-24 21:16:55 +00002038 return;
2039 }
2040 }
2041
2042 tok::TokenKind Kind = Tok.getKind();
Steve Naroff7aa54752008-08-27 16:04:49 +00002043 // Not a pointer, C++ reference, or block.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00002044 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner053dd2d2009-03-24 17:04:48 +00002045 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl4e67adb2009-03-23 00:00:23 +00002046 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattnerc14c7f02009-03-27 04:18:06 +00002047 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002048 if (DirectDeclParser)
2049 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00002050 return;
2051 }
Sebastian Redl75555032009-01-24 21:16:55 +00002052
Sebastian Redl9951dbc2009-03-15 22:02:01 +00002053 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2054 // '&&' -> rvalue reference
Sebastian Redl4e67adb2009-03-23 00:00:23 +00002055 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redl0c986032009-02-09 18:23:29 +00002056 D.SetRangeEnd(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002057
Chris Lattnerc14c7f02009-03-27 04:18:06 +00002058 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner69f01932008-02-21 01:32:26 +00002059 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00002060 DeclSpec DS;
Sebastian Redl75555032009-01-24 21:16:55 +00002061
Chris Lattner4b009652007-07-25 00:24:17 +00002062 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00002063 D.ExtendWithDeclSpec(DS);
Sebastian Redl75555032009-01-24 21:16:55 +00002064
Chris Lattner4b009652007-07-25 00:24:17 +00002065 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002066 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00002067 if (Kind == tok::star)
2068 // Remember that we parsed a pointer type, and remember the type-quals.
2069 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redl0c986032009-02-09 18:23:29 +00002070 DS.TakeAttributes()),
2071 SourceLocation());
Steve Naroff7aa54752008-08-27 16:04:49 +00002072 else
2073 // Remember that we parsed a Block type, and remember the type-quals.
2074 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump7ff82e72009-04-21 00:51:43 +00002075 Loc, DS.TakeAttributes()),
Sebastian Redl0c986032009-02-09 18:23:29 +00002076 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00002077 } else {
2078 // Is a reference
2079 DeclSpec DS;
2080
Sebastian Redl4e67adb2009-03-23 00:00:23 +00002081 // Complain about rvalue references in C++03, but then go on and build
2082 // the declarator.
2083 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2084 Diag(Loc, diag::err_rvalue_reference);
2085
Chris Lattner4b009652007-07-25 00:24:17 +00002086 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2087 // cv-qualifiers are introduced through the use of a typedef or of a
2088 // template type argument, in which case the cv-qualifiers are ignored.
2089 //
2090 // [GNU] Retricted references are allowed.
2091 // [GNU] Attributes on references are allowed.
2092 ParseTypeQualifierListOpt(DS);
Sebastian Redl0c986032009-02-09 18:23:29 +00002093 D.ExtendWithDeclSpec(DS);
Chris Lattner4b009652007-07-25 00:24:17 +00002094
2095 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2096 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2097 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00002098 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00002099 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2100 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00002101 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00002102 }
2103
2104 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002105 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00002106
Douglas Gregorb7b28a22008-11-03 15:51:28 +00002107 if (D.getNumTypeObjects() > 0) {
2108 // C++ [dcl.ref]p4: There shall be no references to references.
2109 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2110 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002111 if (const IdentifierInfo *II = D.getIdentifier())
2112 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2113 << II;
2114 else
2115 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2116 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00002117
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002118 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00002119 // can go ahead and build the (technically ill-formed)
2120 // declarator: reference collapsing will take care of it.
2121 }
2122 }
2123
Chris Lattner4b009652007-07-25 00:24:17 +00002124 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00002125 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl9951dbc2009-03-15 22:02:01 +00002126 DS.TakeAttributes(),
2127 Kind == tok::amp),
Sebastian Redl0c986032009-02-09 18:23:29 +00002128 SourceLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00002129 }
2130}
2131
2132/// ParseDirectDeclarator
2133/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00002134/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00002135/// '(' declarator ')'
2136/// [GNU] '(' attributes declarator ')'
2137/// [C90] direct-declarator '[' constant-expression[opt] ']'
2138/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2139/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2140/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2141/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2142/// direct-declarator '(' parameter-type-list ')'
2143/// direct-declarator '(' identifier-list[opt] ')'
2144/// [GNU] direct-declarator '(' parameter-forward-declarations
2145/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002146/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2147/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00002148/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00002149///
2150/// declarator-id: [C++ 8]
2151/// id-expression
2152/// '::'[opt] nested-name-specifier[opt] type-name
2153///
2154/// id-expression: [C++ 5.1]
2155/// unqualified-id
2156/// qualified-id [TODO]
2157///
2158/// unqualified-id: [C++ 5.1]
2159/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002160/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00002161/// conversion-function-id [TODO]
2162/// '~' class-name
Douglas Gregor0c281a82009-02-25 19:37:18 +00002163/// template-id
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00002164///
Chris Lattner4b009652007-07-25 00:24:17 +00002165void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002166 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002167
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002168 if (getLang().CPlusPlus) {
2169 if (D.mayHaveIdentifier()) {
Sebastian Redl75555032009-01-24 21:16:55 +00002170 // ParseDeclaratorInternal might already have parsed the scope.
2171 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2172 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002173 if (afterCXXScope) {
2174 // Change the declaration context for name lookup, until this function
2175 // is exited (and the declarator has been parsed).
2176 DeclScopeObj.EnterDeclaratorScope();
2177 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002178
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002179 if (Tok.is(tok::identifier)) {
2180 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Anders Carlssone19759d2009-04-30 22:41:11 +00002181
2182 // If this identifier is the name of the current class, it's a
2183 // constructor name.
2184 if (!D.getDeclSpec().hasTypeSpecifier() &&
2185 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
Douglas Gregorcb0a7f72009-07-06 16:40:48 +00002186 CXXScopeSpec *SS = afterCXXScope? &D.getCXXScopeSpec() : 0;
Anders Carlssone19759d2009-04-30 22:41:11 +00002187 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorcb0a7f72009-07-06 16:40:48 +00002188 Tok.getLocation(), CurScope, SS),
Anders Carlssone19759d2009-04-30 22:41:11 +00002189 Tok.getLocation());
2190 // This is a normal identifier.
2191 } else
2192 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002193 ConsumeToken();
2194 goto PastIdentifier;
Douglas Gregor0c281a82009-02-25 19:37:18 +00002195 } else if (Tok.is(tok::annot_template_id)) {
2196 TemplateIdAnnotation *TemplateId
2197 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
2198
2199 // FIXME: Could this template-id name a constructor?
2200
2201 // FIXME: This is an egregious hack, where we silently ignore
2202 // the specialization (which should be a function template
2203 // specialization name) and use the name instead. This hack
2204 // will go away when we have support for function
2205 // specializations.
2206 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
2207 TemplateId->Destroy();
2208 ConsumeToken();
2209 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00002210 } else if (Tok.is(tok::kw_operator)) {
2211 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redl0c986032009-02-09 18:23:29 +00002212 SourceLocation EndLoc;
Douglas Gregore60e5d32008-11-06 22:13:31 +00002213
Douglas Gregor853dd392008-12-26 15:00:45 +00002214 // First try the name of an overloaded operator
Sebastian Redl0c986032009-02-09 18:23:29 +00002215 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
2216 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor853dd392008-12-26 15:00:45 +00002217 } else {
2218 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redl0c986032009-02-09 18:23:29 +00002219 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
2220 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
2221 else {
Douglas Gregor853dd392008-12-26 15:00:45 +00002222 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redl0c986032009-02-09 18:23:29 +00002223 }
Douglas Gregor853dd392008-12-26 15:00:45 +00002224 }
2225 goto PastIdentifier;
2226 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002227 // This should be a C++ destructor.
2228 SourceLocation TildeLoc = ConsumeToken();
2229 if (Tok.is(tok::identifier)) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002230 // FIXME: Inaccurate.
2231 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7bbed2a2009-02-25 23:52:28 +00002232 SourceLocation EndLoc;
Douglas Gregorcb0a7f72009-07-06 16:40:48 +00002233 CXXScopeSpec *SS = afterCXXScope? &D.getCXXScopeSpec() : 0;
Fariborz Jahanian1b8fd752009-07-20 17:43:15 +00002234 TypeResult Type = ParseClassName(EndLoc, SS, true);
Douglas Gregord7cb0372009-04-01 21:51:26 +00002235 if (Type.isInvalid())
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002236 D.SetIdentifier(0, TildeLoc);
Douglas Gregord7cb0372009-04-01 21:51:26 +00002237 else
2238 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002239 } else {
Fariborz Jahanian1b8fd752009-07-20 17:43:15 +00002240 Diag(Tok, diag::err_destructor_class_name);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002241 D.SetIdentifier(0, TildeLoc);
2242 }
2243 goto PastIdentifier;
2244 }
2245
2246 // If we reached this point, token is not identifier and not '~'.
2247
2248 if (afterCXXScope) {
2249 Diag(Tok, diag::err_expected_unqualified_id);
2250 D.SetIdentifier(0, Tok.getLocation());
2251 D.setInvalidType(true);
2252 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00002253 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00002254 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002255 }
2256
2257 // If we reached this point, we are either in C/ObjC or the token didn't
2258 // satisfy any of the C++-specific checks.
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002259 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2260 assert(!getLang().CPlusPlus &&
2261 "There's a C++-specific check for tok::identifier above");
2262 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2263 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2264 ConsumeToken();
2265 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002266 // direct-declarator: '(' declarator ')'
2267 // direct-declarator: '(' attributes declarator ')'
2268 // Example: 'char (*X)' or 'int (*XX)(void)'
2269 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002270 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002271 // This could be something simple like "int" (in which case the declarator
2272 // portion is empty), if an abstract-declarator is allowed.
2273 D.SetIdentifier(0, Tok.getLocation());
2274 } else {
Douglas Gregorf03265d2009-03-06 23:28:18 +00002275 if (D.getContext() == Declarator::MemberContext)
2276 Diag(Tok, diag::err_expected_member_name_or_semi)
2277 << D.getDeclSpec().getSourceRange();
2278 else if (getLang().CPlusPlus)
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00002279 Diag(Tok, diag::err_expected_unqualified_id);
2280 else
Chris Lattnerf006a222008-11-18 07:48:38 +00002281 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00002282 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00002283 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00002284 }
2285
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00002286 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00002287 assert(D.isPastIdentifier() &&
2288 "Haven't past the location of the identifier yet?");
2289
2290 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002291 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002292 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2293 // In such a case, check if we actually have a function declarator; if it
2294 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00002295 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2296 // When not in file scope, warn for ambiguous function declarators, just
2297 // in case the author intended it as a variable definition.
2298 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2299 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2300 break;
2301 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00002302 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00002303 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00002304 ParseBracketDeclarator(D);
2305 } else {
2306 break;
2307 }
2308 }
2309}
2310
Chris Lattnera0d056d2008-04-06 05:45:57 +00002311/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2312/// only called before the identifier, so these are most likely just grouping
2313/// parens for precedence. If we find that these are actually function
2314/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2315///
2316/// direct-declarator:
2317/// '(' declarator ')'
2318/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00002319/// direct-declarator '(' parameter-type-list ')'
2320/// direct-declarator '(' identifier-list[opt] ')'
2321/// [GNU] direct-declarator '(' parameter-forward-declarations
2322/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00002323///
2324void Parser::ParseParenDeclarator(Declarator &D) {
2325 SourceLocation StartLoc = ConsumeParen();
2326 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
2327
Chris Lattner1f185292008-10-20 02:05:46 +00002328 // Eat any attributes before we look at whether this is a grouping or function
2329 // declarator paren. If this is a grouping paren, the attribute applies to
2330 // the type being built up, for example:
2331 // int (__attribute__(()) *x)(long y)
2332 // If this ends up not being a grouping paren, the attribute applies to the
2333 // first argument, for example:
2334 // int (__attribute__(()) int x)
2335 // In either case, we need to eat any attributes to be able to determine what
2336 // sort of paren this is.
2337 //
2338 AttributeList *AttrList = 0;
2339 bool RequiresArg = false;
2340 if (Tok.is(tok::kw___attribute)) {
2341 AttrList = ParseAttributes();
2342
2343 // We require that the argument list (if this is a non-grouping paren) be
2344 // present even if the attribute list was empty.
2345 RequiresArg = true;
2346 }
Steve Naroffedd04d52008-12-25 14:16:32 +00002347 // Eat any Microsoft extensions.
Eli Friedman891d82f2009-06-08 23:27:34 +00002348 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2349 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2350 Tok.is(tok::kw___ptr64)) {
2351 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2352 }
Chris Lattner1f185292008-10-20 02:05:46 +00002353
Chris Lattnera0d056d2008-04-06 05:45:57 +00002354 // If we haven't past the identifier yet (or where the identifier would be
2355 // stored, if this is an abstract declarator), then this is probably just
2356 // grouping parens. However, if this could be an abstract-declarator, then
2357 // this could also be the start of function arguments (consider 'void()').
2358 bool isGrouping;
2359
2360 if (!D.mayOmitIdentifier()) {
2361 // If this can't be an abstract-declarator, this *must* be a grouping
2362 // paren, because we haven't seen the identifier yet.
2363 isGrouping = true;
2364 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00002365 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00002366 isDeclarationSpecifier()) { // 'int(int)' is a function.
2367 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2368 // considered to be a type, not a K&R identifier-list.
2369 isGrouping = false;
2370 } else {
2371 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2372 isGrouping = true;
2373 }
2374
2375 // If this is a grouping paren, handle:
2376 // direct-declarator: '(' declarator ')'
2377 // direct-declarator: '(' attributes declarator ')'
2378 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002379 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002380 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00002381 if (AttrList)
Sebastian Redl0c986032009-02-09 18:23:29 +00002382 D.AddAttributes(AttrList, SourceLocation());
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00002383
Sebastian Redl19fec9d2008-11-21 19:14:01 +00002384 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002385 // Match the ')'.
Sebastian Redl0c986032009-02-09 18:23:29 +00002386 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00002387
2388 D.setGroupingParens(hadGroupingParens);
Sebastian Redl0c986032009-02-09 18:23:29 +00002389 D.SetRangeEnd(Loc);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002390 return;
2391 }
2392
2393 // Okay, if this wasn't a grouping paren, it must be the start of a function
2394 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00002395 // identifier (and remember where it would have been), then call into
2396 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00002397 D.SetIdentifier(0, Tok.getLocation());
2398
Chris Lattner1f185292008-10-20 02:05:46 +00002399 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00002400}
2401
2402/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2403/// declarator D up to a paren, which indicates that we are parsing function
2404/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00002405///
Chris Lattner1f185292008-10-20 02:05:46 +00002406/// If AttrList is non-null, then the caller parsed those arguments immediately
2407/// after the open paren - they should be considered to be the first argument of
2408/// a parameter. If RequiresArg is true, then the first argument of the
2409/// function is required to be present and required to not be an identifier
2410/// list.
2411///
Chris Lattner4b009652007-07-25 00:24:17 +00002412/// This method also handles this portion of the grammar:
2413/// parameter-type-list: [C99 6.7.5]
2414/// parameter-list
2415/// parameter-list ',' '...'
2416///
2417/// parameter-list: [C99 6.7.5]
2418/// parameter-declaration
2419/// parameter-list ',' parameter-declaration
2420///
2421/// parameter-declaration: [C99 6.7.5]
2422/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00002423/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002424/// [GNU] declaration-specifiers declarator attributes
Sebastian Redla8cecf62009-03-24 22:27:57 +00002425/// declaration-specifiers abstract-declarator[opt]
2426/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00002427/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00002428/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2429///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002430/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redla8cecf62009-03-24 22:27:57 +00002431/// and "exception-specification[opt]".
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002432///
Chris Lattner1f185292008-10-20 02:05:46 +00002433void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2434 AttributeList *AttrList,
2435 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00002436 // lparen is already consumed!
2437 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00002438
Chris Lattner1f185292008-10-20 02:05:46 +00002439 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002440 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00002441 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00002442 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00002443 delete AttrList;
2444 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002445
Sebastian Redl0c986032009-02-09 18:23:29 +00002446 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002447
2448 // cv-qualifier-seq[opt].
2449 DeclSpec DS;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002450 bool hasExceptionSpec = false;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002451 SourceLocation ThrowLoc;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002452 bool hasAnyExceptionSpec = false;
Sebastian Redlaaacda92009-05-29 18:02:33 +00002453 llvm::SmallVector<TypeTy*, 2> Exceptions;
2454 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002455 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00002456 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002457 if (!DS.getSourceRange().getEnd().isInvalid())
2458 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002459
2460 // Parse exception-specification[opt].
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002461 if (Tok.is(tok::kw_throw)) {
2462 hasExceptionSpec = true;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002463 ThrowLoc = Tok.getLocation();
Sebastian Redlaaacda92009-05-29 18:02:33 +00002464 ParseExceptionSpecification(Loc, Exceptions, ExceptionRanges,
2465 hasAnyExceptionSpec);
2466 assert(Exceptions.size() == ExceptionRanges.size() &&
2467 "Produced different number of exception types and ranges.");
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002468 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002469 }
2470
Chris Lattner9f7564b2008-04-06 06:57:35 +00002471 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00002472 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002473 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00002474 /*variadic*/ false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002475 SourceLocation(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002476 /*arglist*/ 0, 0,
2477 DS.getTypeQualifiers(),
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002478 hasExceptionSpec, ThrowLoc,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002479 hasAnyExceptionSpec,
Sebastian Redlaaacda92009-05-29 18:02:33 +00002480 Exceptions.data(),
2481 ExceptionRanges.data(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002482 Exceptions.size(),
Sebastian Redl0c986032009-02-09 18:23:29 +00002483 LParenLoc, D),
2484 Loc);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002485 return;
Sebastian Redlaaacda92009-05-29 18:02:33 +00002486 }
2487
Chris Lattner1f185292008-10-20 02:05:46 +00002488 // Alternatively, this parameter list may be an identifier list form for a
2489 // K&R-style function: void foo(a,b,c)
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002490 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff965f5d72009-01-30 14:23:32 +00002491 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner1f185292008-10-20 02:05:46 +00002492 // K&R identifier lists can't have typedefs as identifiers, per
2493 // C99 6.7.5.3p11.
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002494 if (RequiresArg) {
2495 Diag(Tok, diag::err_argument_required_after_attribute);
2496 delete AttrList;
2497 }
Steve Naroff3f3f3b42009-01-28 19:16:40 +00002498 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2499 // normal declarators, not for abstract-declarators.
2500 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner1f185292008-10-20 02:05:46 +00002501 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002502 }
2503
2504 // Finally, a normal, non-empty parameter type list.
2505
2506 // Build up an array of information about the parsed arguments.
2507 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00002508
2509 // Enter function-declaration scope, limiting any declarators to the
2510 // function prototype scope, including parameter declarators.
Chris Lattnerc24b8892009-03-05 00:00:31 +00002511 ParseScope PrototypeScope(this,
2512 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00002513
2514 bool IsVariadic = false;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002515 SourceLocation EllipsisLoc;
Chris Lattner9f7564b2008-04-06 06:57:35 +00002516 while (1) {
2517 if (Tok.is(tok::ellipsis)) {
2518 IsVariadic = true;
Douglas Gregor88a25f82009-02-18 07:07:28 +00002519 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002520 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002521 }
2522
Chris Lattner9f7564b2008-04-06 06:57:35 +00002523 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00002524
Chris Lattner9f7564b2008-04-06 06:57:35 +00002525 // Parse the declaration-specifiers.
2526 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00002527
2528 // If the caller parsed attributes for the first argument, add them now.
2529 if (AttrList) {
2530 DS.AddAttributes(AttrList);
2531 AttrList = 0; // Only apply the attributes to the first parameter.
2532 }
Chris Lattner9e785f52009-02-27 18:38:20 +00002533 ParseDeclarationSpecifiers(DS);
2534
Chris Lattner9f7564b2008-04-06 06:57:35 +00002535 // Parse the declarator. This is "PrototypeContext", because we must
2536 // accept either 'declarator' or 'abstract-declarator' here.
2537 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2538 ParseDeclarator(ParmDecl);
2539
2540 // Parse GNU attributes, if present.
Sebastian Redl0c986032009-02-09 18:23:29 +00002541 if (Tok.is(tok::kw___attribute)) {
2542 SourceLocation Loc;
2543 AttributeList *AttrList = ParseAttributes(&Loc);
2544 ParmDecl.AddAttributes(AttrList, Loc);
2545 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002546
Chris Lattner9f7564b2008-04-06 06:57:35 +00002547 // Remember this parsed parameter in ParamInfo.
2548 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2549
Douglas Gregor605de8d2008-12-16 21:30:33 +00002550 // DefArgToks is used when the parsing of default arguments needs
2551 // to be delayed.
2552 CachedTokens *DefArgToks = 0;
2553
Chris Lattner9f7564b2008-04-06 06:57:35 +00002554 // If no parameter was specified, verify that *something* was specified,
2555 // otherwise we have a missing type and identifier.
Chris Lattner9e785f52009-02-27 18:38:20 +00002556 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2557 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00002558 // Completely missing, emit error.
2559 Diag(DSStart, diag::err_missing_param);
2560 } else {
2561 // Otherwise, we have something. Add it and let semantic analysis try
2562 // to grok it and add the result to the ParamInfo we are building.
2563
2564 // Inform the actions module about the parameter declarator, so it gets
2565 // added to the current scope.
Chris Lattner5261d0c2009-03-28 19:18:32 +00002566 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner3e254fb2008-04-08 04:40:51 +00002567
2568 // Parse the default argument, if any. We parse the default
2569 // arguments in all dialects; the semantic analysis in
2570 // ActOnParamDefaultArgument will reject the default argument in
2571 // C.
2572 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002573 SourceLocation EqualLoc = Tok.getLocation();
2574
Chris Lattner3e254fb2008-04-08 04:40:51 +00002575 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00002576 if (D.getContext() == Declarator::MemberContext) {
2577 // If we're inside a class definition, cache the tokens
2578 // corresponding to the default argument. We'll actually parse
2579 // them when we see the end of the class definition.
2580 // FIXME: Templates will require something similar.
2581 // FIXME: Can we use a smart pointer for Toks?
2582 DefArgToks = new CachedTokens;
2583
2584 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2585 tok::semi, false)) {
2586 delete DefArgToks;
2587 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002588 Actions.ActOnParamDefaultArgumentError(Param);
2589 } else
Anders Carlssona116e6e2009-06-12 16:51:40 +00002590 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
2591 (*DefArgToks)[1].getLocation());
Chris Lattner3e254fb2008-04-08 04:40:51 +00002592 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00002593 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00002594 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00002595
2596 OwningExprResult DefArgResult(ParseAssignmentExpression());
2597 if (DefArgResult.isInvalid()) {
2598 Actions.ActOnParamDefaultArgumentError(Param);
2599 SkipUntil(tok::comma, tok::r_paren, true, true);
2600 } else {
2601 // Inform the actions module about the default argument
2602 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +00002603 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +00002604 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00002605 }
2606 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00002607
2608 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00002609 ParmDecl.getIdentifierLoc(), Param,
2610 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00002611 }
2612
2613 // If the next token is a comma, consume it and keep reading arguments.
2614 if (Tok.isNot(tok::comma)) break;
2615
2616 // Consume the comma.
2617 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00002618 }
2619
Chris Lattner9f7564b2008-04-06 06:57:35 +00002620 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00002621 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00002622
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002623 // If we have the closing ')', eat it.
Sebastian Redl0c986032009-02-09 18:23:29 +00002624 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002625
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002626 DeclSpec DS;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002627 bool hasExceptionSpec = false;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002628 SourceLocation ThrowLoc;
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002629 bool hasAnyExceptionSpec = false;
Sebastian Redlaaacda92009-05-29 18:02:33 +00002630 llvm::SmallVector<TypeTy*, 2> Exceptions;
2631 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002632 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002633 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002634 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redl0c986032009-02-09 18:23:29 +00002635 if (!DS.getSourceRange().getEnd().isInvalid())
2636 Loc = DS.getSourceRange().getEnd();
Douglas Gregor90a2c972008-11-25 03:22:00 +00002637
2638 // Parse exception-specification[opt].
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002639 if (Tok.is(tok::kw_throw)) {
2640 hasExceptionSpec = true;
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002641 ThrowLoc = Tok.getLocation();
Sebastian Redlaaacda92009-05-29 18:02:33 +00002642 ParseExceptionSpecification(Loc, Exceptions, ExceptionRanges,
2643 hasAnyExceptionSpec);
2644 assert(Exceptions.size() == ExceptionRanges.size() &&
2645 "Produced different number of exception types and ranges.");
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002646 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002647 }
2648
Chris Lattner4b009652007-07-25 00:24:17 +00002649 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002650 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002651 EllipsisLoc,
Jay Foad9e6bef42009-05-21 09:52:38 +00002652 ParamInfo.data(), ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002653 DS.getTypeQualifiers(),
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002654 hasExceptionSpec, ThrowLoc,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002655 hasAnyExceptionSpec,
Sebastian Redlaaacda92009-05-29 18:02:33 +00002656 Exceptions.data(),
2657 ExceptionRanges.data(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002658 Exceptions.size(), LParenLoc, D),
Sebastian Redl0c986032009-02-09 18:23:29 +00002659 Loc);
Chris Lattner4b009652007-07-25 00:24:17 +00002660}
2661
Chris Lattner35d9c912008-04-06 06:34:08 +00002662/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2663/// we found a K&R-style identifier list instead of a type argument list. The
2664/// current token is known to be the first identifier in the list.
2665///
2666/// identifier-list: [C99 6.7.5]
2667/// identifier
2668/// identifier-list ',' identifier
2669///
2670void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2671 Declarator &D) {
2672 // Build up an array of information about the parsed arguments.
2673 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2674 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2675
2676 // If there was no identifier specified for the declarator, either we are in
2677 // an abstract-declarator, or we are in a parameter declarator which was found
2678 // to be abstract. In abstract-declarators, identifier lists are not valid:
2679 // diagnose this.
2680 if (!D.getIdentifier())
2681 Diag(Tok, diag::ext_ident_list_in_param);
2682
2683 // Tok is known to be the first identifier in the list. Remember this
2684 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002685 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002686 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner5261d0c2009-03-28 19:18:32 +00002687 Tok.getLocation(),
2688 DeclPtrTy()));
Chris Lattner35d9c912008-04-06 06:34:08 +00002689
Chris Lattner113a56b2008-04-06 06:39:19 +00002690 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002691
2692 while (Tok.is(tok::comma)) {
2693 // Eat the comma.
2694 ConsumeToken();
2695
Chris Lattner113a56b2008-04-06 06:39:19 +00002696 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002697 if (Tok.isNot(tok::identifier)) {
2698 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002699 SkipUntil(tok::r_paren);
2700 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002701 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002702
Chris Lattner35d9c912008-04-06 06:34:08 +00002703 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002704
2705 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor1075a162009-02-04 17:00:24 +00002706 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002707 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002708
2709 // Verify that the argument identifier has not already been mentioned.
2710 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002711 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002712 } else {
2713 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002714 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner5261d0c2009-03-28 19:18:32 +00002715 Tok.getLocation(),
2716 DeclPtrTy()));
Chris Lattner113a56b2008-04-06 06:39:19 +00002717 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002718
2719 // Eat the identifier.
2720 ConsumeToken();
2721 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002722
2723 // If we have the closing ')', eat it and we're done.
2724 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2725
Chris Lattner113a56b2008-04-06 06:39:19 +00002726 // Remember that we parsed a function type, and remember the attributes. This
2727 // function type is always a K&R style function type, which is not varargs and
2728 // has no prototype.
2729 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor88a25f82009-02-18 07:07:28 +00002730 SourceLocation(),
Chris Lattner113a56b2008-04-06 06:39:19 +00002731 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002732 /*TypeQuals*/0,
Sebastian Redl9fbe9bf2009-05-31 11:47:27 +00002733 /*exception*/false,
2734 SourceLocation(), false, 0, 0, 0,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00002735 LParenLoc, D),
Sebastian Redl0c986032009-02-09 18:23:29 +00002736 RLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002737}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002738
Chris Lattner4b009652007-07-25 00:24:17 +00002739/// [C90] direct-declarator '[' constant-expression[opt] ']'
2740/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2741/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2742/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2743/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2744void Parser::ParseBracketDeclarator(Declarator &D) {
2745 SourceLocation StartLoc = ConsumeBracket();
2746
Chris Lattner1525c3a2008-12-18 07:27:21 +00002747 // C array syntax has many features, but by-far the most common is [] and [4].
2748 // This code does a fast path to handle some of the most obvious cases.
2749 if (Tok.getKind() == tok::r_square) {
Sebastian Redl0c986032009-02-09 18:23:29 +00002750 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002751 // Remember that we parsed the empty array type.
2752 OwningExprResult NumElements(Actions);
Douglas Gregor1d381132009-07-06 15:59:29 +00002753 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
2754 StartLoc, EndLoc),
Sebastian Redl0c986032009-02-09 18:23:29 +00002755 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002756 return;
2757 } else if (Tok.getKind() == tok::numeric_constant &&
2758 GetLookAheadToken(1).is(tok::r_square)) {
2759 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002760 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002761 ConsumeToken();
2762
Sebastian Redl0c986032009-02-09 18:23:29 +00002763 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002764
2765 // If there was an error parsing the assignment-expression, recover.
2766 if (ExprRes.isInvalid())
2767 ExprRes.release(); // Deallocate expr, just use [].
2768
2769 // Remember that we parsed a array type, and remember its features.
Douglas Gregor1d381132009-07-06 15:59:29 +00002770 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
2771 StartLoc, EndLoc),
Sebastian Redl0c986032009-02-09 18:23:29 +00002772 EndLoc);
Chris Lattner1525c3a2008-12-18 07:27:21 +00002773 return;
2774 }
2775
Chris Lattner4b009652007-07-25 00:24:17 +00002776 // If valid, this location is the position where we read the 'static' keyword.
2777 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002778 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002779 StaticLoc = ConsumeToken();
2780
2781 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002782 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002783 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002784 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002785
2786 // If we haven't already read 'static', check to see if there is one after the
2787 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002788 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002789 StaticLoc = ConsumeToken();
2790
2791 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2792 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002793 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002794
2795 // Handle the case where we have '[*]' as the array size. However, a leading
2796 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2797 // the the token after the star is a ']'. Since stars in arrays are
2798 // infrequent, use of lookahead is not costly here.
2799 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002800 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002801
Chris Lattner306d4df2008-12-18 06:50:14 +00002802 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002803 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002804 StaticLoc = SourceLocation(); // Drop the static.
2805 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002806 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002807 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002808 // Note, in C89, this production uses the constant-expr production instead
2809 // of assignment-expr. The only difference is that assignment-expr allows
2810 // things like '=' and '*='. Sema rejects these in C89 mode because they
2811 // are not i-c-e's, so we don't need to distinguish between the two here.
2812
Douglas Gregor98189262009-06-19 23:52:42 +00002813 // Parse the constant-expression or assignment-expression now (depending
2814 // on dialect).
2815 if (getLang().CPlusPlus)
2816 NumElements = ParseConstantExpression();
2817 else
2818 NumElements = ParseAssignmentExpression();
Chris Lattner4b009652007-07-25 00:24:17 +00002819 }
2820
2821 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002822 if (NumElements.isInvalid()) {
Chris Lattnerf3ce8572009-04-24 22:30:50 +00002823 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00002824 // If the expression was invalid, skip it.
2825 SkipUntil(tok::r_square);
2826 return;
2827 }
Sebastian Redl0c986032009-02-09 18:23:29 +00002828
2829 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2830
Chris Lattner1525c3a2008-12-18 07:27:21 +00002831 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002832 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2833 StaticLoc.isValid(), isStar,
Douglas Gregor1d381132009-07-06 15:59:29 +00002834 NumElements.release(),
2835 StartLoc, EndLoc),
Sebastian Redl0c986032009-02-09 18:23:29 +00002836 EndLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002837}
2838
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002839/// [GNU] typeof-specifier:
2840/// typeof ( expressions )
2841/// typeof ( type-name )
2842/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002843///
2844void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002845 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002846 Token OpTok = Tok;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002847 SourceLocation StartLoc = ConsumeToken();
2848
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002849 bool isCastExpr;
2850 TypeTy *CastTy;
2851 SourceRange CastRange;
2852 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
2853 isCastExpr,
2854 CastTy,
2855 CastRange);
2856
2857 if (CastRange.getEnd().isInvalid())
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002858 // FIXME: Not accurate, the range gets one token more than it should.
2859 DS.SetRangeEnd(Tok.getLocation());
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002860 else
2861 DS.SetRangeEnd(CastRange.getEnd());
2862
2863 if (isCastExpr) {
2864 if (!CastTy) {
2865 DS.SetTypeSpecError();
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002866 return;
Douglas Gregor6c0f4062009-02-18 17:45:20 +00002867 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002868
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002869 const char *PrevSpec = 0;
John McCall9f6e0972009-08-03 20:12:06 +00002870 unsigned DiagID;
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002871 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2872 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall9f6e0972009-08-03 20:12:06 +00002873 DiagID, CastTy))
2874 Diag(StartLoc, DiagID) << PrevSpec;
Argiris Kirtzidis4c90fb22009-05-22 10:22:50 +00002875 return;
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002876 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002877
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002878 // If we get here, the operand to the typeof was an expresion.
2879 if (Operand.isInvalid()) {
2880 DS.SetTypeSpecError();
Steve Naroff14bbce82007-08-02 02:53:48 +00002881 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002882 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002883
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002884 const char *PrevSpec = 0;
John McCall9f6e0972009-08-03 20:12:06 +00002885 unsigned DiagID;
Argiris Kirtzidis53f05482009-05-22 10:22:18 +00002886 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2887 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCall9f6e0972009-08-03 20:12:06 +00002888 DiagID, Operand.release()))
2889 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002890}