blob: 029d9b5aaec7e99b94b83c6ee8e16577a7a1be9f [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner31e05722007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl4c5d3202008-11-21 19:14:01 +000028///
29/// Called type-id in C++.
Sebastian Redlef65f062009-05-29 18:02:33 +000030Action::TypeResult Parser::ParseTypeName(SourceRange *Range) {
Reid Spencer5f016e22007-07-11 17:01:13 +000031 // Parse the common declaration-specifiers piece.
32 DeclSpec DS;
33 ParseSpecifierQualifierList(DS);
Sebastian Redlef65f062009-05-29 18:02:33 +000034
Reid Spencer5f016e22007-07-11 17:01:13 +000035 // Parse the abstract-declarator, if present.
36 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
37 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000038 if (Range)
39 *Range = DeclaratorInfo.getSourceRange();
40
Chris Lattnereaaebc72009-04-25 08:06:05 +000041 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000042 return true;
43
44 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redlab197ba2009-02-09 18:23:29 +000083AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000084 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Reid Spencer5f016e22007-07-11 17:01:13 +000085
86 AttributeList *CurrAttr = 0;
87
Chris Lattner04d66662007-10-09 17:33:22 +000088 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000100 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
101 Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000102
Chris Lattner04d66662007-10-09 17:33:22 +0000103 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000113 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 ConsumeParen(); // ignore the left paren loc for now
115
Chris Lattner04d66662007-10-09 17:33:22 +0000116 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
118 SourceLocation ParmLoc = ConsumeToken();
119
Chris Lattner04d66662007-10-09 17:33:22 +0000120 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000125 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 ConsumeToken();
127 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000128 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 bool ArgExprsOk = true;
130
131 // now parse the non-empty comma separated list of expressions
132 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000133 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000134 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 ArgExprsOk = false;
136 SkipUntil(tok::r_paren);
137 break;
138 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000139 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 }
Chris Lattner04d66662007-10-09 17:33:22 +0000141 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 break;
143 ConsumeToken(); // Eat the comma, move to the next argument
144 }
Chris Lattner04d66662007-10-09 17:33:22 +0000145 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 ConsumeParen(); // ignore the right paren loc for now
147 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000148 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 }
150 }
151 } else { // not an identifier
Nate Begeman6f3d8382009-06-26 06:32:41 +0000152 switch (Tok.getKind()) {
153 case tok::r_paren:
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 // parse a possibly empty comma separated list of expressions
Reid Spencer5f016e22007-07-11 17:01:13 +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 Begeman6f3d8382009-06-26 06:32:41 +0000159 break;
160 case tok::kw_char:
161 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000162 case tok::kw_char16_t:
163 case tok::kw_char32_t:
Nate Begeman6f3d8382009-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:
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000184 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 bool ArgExprsOk = true;
186
187 // now parse the list of expressions
188 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000189 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000190 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 ArgExprsOk = false;
192 SkipUntil(tok::r_paren);
193 break;
194 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000195 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 }
Chris Lattner04d66662007-10-09 17:33:22 +0000197 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 break;
199 ConsumeToken(); // Eat the comma, move to the next argument
200 }
201 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000202 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000204 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
205 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 CurrAttr);
207 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000208 break;
Reid Spencer5f016e22007-07-11 17:01:13 +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))
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 SkipUntil(tok::r_paren, false);
Sebastian Redlab197ba2009-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 }
225 return CurrAttr;
226}
227
Eli Friedmana23b4852009-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 Friedman290eeb02009-06-08 23:27:34 +0000237AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000238 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000239
Steve Narofff59e17e2008-12-24 20:59:21 +0000240 ConsumeToken();
Eli Friedmana23b4852009-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 Friedman290eeb02009-06-08 23:27:34 +0000246 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-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 Friedman290eeb02009-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 Narofff59e17e2008-12-24 20:59:21 +0000287}
288
Reid Spencer5f016e22007-07-11 17:01:13 +0000289/// ParseDeclaration - Parse a full 'declaration', which consists of
290/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000291/// 'Context' should be a Declarator::TheContext value. This returns the
292/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000293///
294/// declaration: [C99 6.7]
295/// block-declaration ->
296/// simple-declaration
297/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000298/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000299/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000300/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000301/// [C++] using-declaration
Sebastian Redl50de12f2009-03-24 22:27:57 +0000302/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000303/// others... [FIXME]
304///
Chris Lattner97144fc2009-04-02 04:16:50 +0000305Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
306 SourceLocation &DeclEnd) {
Chris Lattner682bf922009-03-29 16:50:03 +0000307 DeclPtrTy SingleDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000308 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000309 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000310 case tok::kw_export:
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000311 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000312 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000313 case tok::kw_namespace:
Chris Lattner97144fc2009-04-02 04:16:50 +0000314 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000315 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000316 case tok::kw_using:
Chris Lattner97144fc2009-04-02 04:16:50 +0000317 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000318 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000319 case tok::kw_static_assert:
Chris Lattner97144fc2009-04-02 04:16:50 +0000320 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000321 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000322 default:
Chris Lattner97144fc2009-04-02 04:16:50 +0000323 return ParseSimpleDeclaration(Context, DeclEnd);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000324 }
Chris Lattner682bf922009-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 Lattner8f08cb72007-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 Lattnercd147752009-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 Lattner97144fc2009-04-02 04:16:50 +0000339 SourceLocation &DeclEnd,
Chris Lattnercd147752009-03-29 17:27:48 +0000340 bool RequireSemi) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000347 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000349 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
350 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 }
352
353 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
354 ParseDeclarator(DeclaratorInfo);
355
Chris Lattner23c4b182009-03-29 17:18:04 +0000356 DeclGroupPtrTy DG =
357 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattnercd147752009-03-29 17:27:48 +0000358
Chris Lattner97144fc2009-04-02 04:16:50 +0000359 DeclEnd = Tok.getLocation();
360
Chris Lattnercd147752009-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 Lattner23c4b182009-03-29 17:18:04 +0000364
365 if (Tok.is(tok::semi)) {
366 ConsumeToken();
Chris Lattner23c4b182009-03-29 17:18:04 +0000367 return DG;
368 }
369
John McCall5c15fe12009-07-31 02:20:35 +0000370 Diag(Tok, diag::err_expected_semi_declaration);
Chris Lattner23c4b182009-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000376}
377
Douglas Gregor1426e532009-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.
Reid Spencer5f016e22007-07-11 17:01:13 +0000382///
Reid Spencer5f016e22007-07-11 17:01:13 +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
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000388/// [C++] declarator initializer[opt]
389///
390/// [C++] initializer:
391/// [C++] '=' initializer-clause
392/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-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.
Reid Spencer5f016e22007-07-11 17:01:13 +0000398///
Douglas Gregore542c862009-06-23 23:11:28 +0000399Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
400 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-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 Gregore542c862009-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 Gregor1426e532009-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 {
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000437 if (getLang().CPlusPlus)
438 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
439
Douglas Gregor1426e532009-05-12 21:31:51 +0000440 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000441
442 if (getLang().CPlusPlus)
443 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
444
Douglas Gregor1426e532009-05-12 21:31:51 +0000445 if (Init.isInvalid()) {
446 SkipUntil(tok::semi, true, true);
447 return DeclPtrTy();
448 }
Anders Carlssonf5dcd382009-05-30 21:37:25 +0000449 Actions.AddInitializerToDecl(ThisDecl, Actions.FullExpr(Init));
Douglas Gregor1426e532009-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 Foadbeaaccd2009-05-21 09:52:38 +0000467 CommaLocs.data(), RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000468 }
469 } else {
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000470 bool TypeContainsUndeducedAuto =
471 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
472 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-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 Lattner682bf922009-03-29 16:50:03 +0000490Parser::DeclGroupPtrTy Parser::
Reid Spencer5f016e22007-07-11 17:01:13 +0000491ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattner682bf922009-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;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Gregor1426e532009-05-12 21:31:51 +0000499 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
500 if (ThisDecl.get())
501 DeclsInGroup.push_back(ThisDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000502
Reid Spencer5f016e22007-07-11 17:01:13 +0000503 // If we don't have a comma, it is either the end of the list (a ';') or an
504 // error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000505 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000506 break;
507
508 // Consume the comma.
509 ConsumeToken();
510
511 // Parse the next declarator.
512 D.clear();
Chris Lattneraab740a2008-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 Redlab197ba2009-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 Lattneraab740a2008-10-20 04:57:38 +0000526
Reid Spencer5f016e22007-07-11 17:01:13 +0000527 ParseDeclarator(D);
528 }
529
Eli Friedmanc1dc6532009-05-29 01:49:24 +0000530 return Actions.FinalizeDeclaratorGroup(CurScope, D.getDeclSpec(),
531 DeclsInGroup.data(),
Chris Lattner23c4b182009-03-29 17:18:04 +0000532 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +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.
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 ParseDeclarationSpecifiers(DS);
545
546 // Validate declspec for type-name.
547 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000548 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
549 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +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 Gregorb48fe382008-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);
Reid Spencer5f016e22007-07-11 17:01:13 +0000569 DS.ClearFunctionSpecs();
570 }
571}
572
Chris Lattnerc199ab32009-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 Lattnerb6645dd2009-04-14 21:16:09 +0000584/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000585/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-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 Lattnerb6645dd2009-04-14 21:16:09 +0000594 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000595}
596
Chris Lattnere40c2952009-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 Lattnerf4382f52009-04-14 22:17:06 +0000607bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000608 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000609 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000610 assert(Tok.is(tok::identifier) && "should have identifier");
611
Chris Lattnere40c2952009-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 Lattnerf4382f52009-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 Lattnere40c2952009-04-14 21:34:55 +0000645
Chris Lattnere40c2952009-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 Lattnere40c2952009-04-14 21:34:55 +0000653
Chris Lattnerf4382f52009-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 Gregor4d9a16f2009-05-12 23:25:50 +0000663 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000664 return true;
665 }
Chris Lattnere40c2952009-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 Lattnerf4382f52009-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 Lattnere40c2952009-04-14 21:34:55 +0000675 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000676 unsigned DiagID;
677 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-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
Reid Spencer5f016e22007-07-11 17:01:13 +0000687/// ParseDeclarationSpecifiers
688/// declaration-specifiers: [C99 6.7]
689/// storage-class-specifier declaration-specifiers[opt]
690/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl669d5d72008-11-14 23:42:31 +0000700/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000701/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000702/// function-specifier: [C99 6.7.4]
703/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000704/// [C++] 'virtual'
705/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000706/// 'friend': [C++ dcl.friend]
707
Reid Spencer5f016e22007-07-11 17:01:13 +0000708///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000709void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000710 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000711 AccessSpecifier AS,
712 DeclSpecContext DSContext) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000713 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000714 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000715 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000717 unsigned DiagID = 0;
718
Reid Spencer5f016e22007-07-11 17:01:13 +0000719 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000720
Reid Spencer5f016e22007-07-11 17:01:13 +0000721 switch (Tok.getKind()) {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000722 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000723 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 // If this is not a declaration specifier token, we're done reading decl
725 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000726 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 return;
Chris Lattner5e02c472009-01-05 00:07:25 +0000728
729 case tok::coloncolon: // ::foo::bar
730 // Annotate C++ scope specifiers. If we get one, loop.
731 if (TryAnnotateCXXScopeToken())
732 continue;
733 goto DoneWithDeclSpec;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000734
735 case tok::annot_cxxscope: {
736 if (DS.hasTypeSpecifier())
737 goto DoneWithDeclSpec;
738
739 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000740 Token Next = NextToken();
741 if (Next.is(tok::annot_template_id) &&
742 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000743 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000744 // We have a qualified template-id, e.g., N::A<int>
745 CXXScopeSpec SS;
746 ParseOptionalCXXScopeSpecifier(SS);
747 assert(Tok.is(tok::annot_template_id) &&
748 "ParseOptionalCXXScopeSpecifier not working");
749 AnnotateTemplateIdTokenAsType(&SS);
750 continue;
751 }
752
753 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000754 goto DoneWithDeclSpec;
755
756 CXXScopeSpec SS;
Douglas Gregor35073692009-03-26 23:56:24 +0000757 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000758 SS.setRange(Tok.getAnnotationRange());
759
760 // If the next token is the name of the class type that the C++ scope
761 // denotes, followed by a '(', then this is a constructor declaration.
762 // We're done with the decl-specifiers.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000763 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000764 CurScope, &SS) &&
765 GetLookAheadToken(2).is(tok::l_paren))
766 goto DoneWithDeclSpec;
767
Douglas Gregorb696ea32009-02-04 17:00:24 +0000768 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
769 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000770
Chris Lattnerf4382f52009-04-14 22:17:06 +0000771 // If the referenced identifier is not a type, then this declspec is
772 // erroneous: We already checked about that it has no type specifier, and
773 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
774 // typename.
775 if (TypeRep == 0) {
776 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000777 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000778 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +0000779 }
Douglas Gregore4e5b052009-03-19 00:18:19 +0000780
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000781 ConsumeToken(); // The C++ scope.
782
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000783 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000784 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000785 if (isInvalid)
786 break;
787
788 DS.SetRangeEnd(Tok.getLocation());
789 ConsumeToken(); // The typename.
790
791 continue;
792 }
Chris Lattner80d0c892009-01-21 19:48:37 +0000793
794 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000795 if (Tok.getAnnotationValue())
796 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000797 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +0000798 else
799 DS.SetTypeSpecError();
Chris Lattner80d0c892009-01-21 19:48:37 +0000800 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
801 ConsumeToken(); // The typename
802
803 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
804 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
805 // Objective-C interface. If we don't have Objective-C or a '<', this is
806 // just a normal reference to a typedef name.
807 if (!Tok.is(tok::less) || !getLang().ObjC1)
808 continue;
809
810 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000811 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner80d0c892009-01-21 19:48:37 +0000812 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek1bc5bbf2009-06-30 22:19:00 +0000813 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Chris Lattner80d0c892009-01-21 19:48:37 +0000814
815 DS.SetRangeEnd(EndProtoLoc);
816 continue;
817 }
818
Chris Lattner3bd934a2008-07-26 01:18:38 +0000819 // typedef-name
820 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +0000821 // In C++, check to see if this is a scope specifier like foo::bar::, if
822 // so handle it as such. This is important for ctor parsing.
Chris Lattner837acd02009-01-21 19:19:26 +0000823 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
824 continue;
Chris Lattner5e02c472009-01-05 00:07:25 +0000825
Chris Lattner3bd934a2008-07-26 01:18:38 +0000826 // This identifier can only be a typedef name if we haven't already seen
827 // a type-specifier. Without this check we misparse:
828 // typedef int X; struct Y { short X; }; as 'short int'.
829 if (DS.hasTypeSpecifier())
830 goto DoneWithDeclSpec;
831
832 // It has to be available as a typedef too!
Douglas Gregorb696ea32009-02-04 17:00:24 +0000833 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
834 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000835
Chris Lattnerc199ab32009-04-12 20:42:31 +0000836 // If this is not a typedef name, don't parse it as part of the declspec,
837 // it must be an implicit int or an error.
838 if (TypeRep == 0) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000839 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000840 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +0000841 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000842
Douglas Gregorb48fe382008-10-31 09:07:45 +0000843 // C++: If the identifier is actually the name of the class type
844 // being defined and the next token is a '(', then this is a
845 // constructor declaration. We're done with the decl-specifiers
846 // and will treat this token as an identifier.
Chris Lattnerc199ab32009-04-12 20:42:31 +0000847 if (getLang().CPlusPlus && CurScope->isClassScope() &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000848 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
849 NextToken().getKind() == tok::l_paren)
850 goto DoneWithDeclSpec;
851
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000852 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000853 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +0000854 if (isInvalid)
855 break;
856
857 DS.SetRangeEnd(Tok.getLocation());
858 ConsumeToken(); // The identifier
859
860 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
861 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
862 // Objective-C interface. If we don't have Objective-C or a '<', this is
863 // just a normal reference to a typedef name.
864 if (!Tok.is(tok::less) || !getLang().ObjC1)
865 continue;
866
867 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000868 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000869 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek1bc5bbf2009-06-30 22:19:00 +0000870 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000871
872 DS.SetRangeEnd(EndProtoLoc);
873
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000874 // Need to support trailing type qualifiers (e.g. "id<p> const").
875 // If a type specifier follows, it will be diagnosed elsewhere.
876 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000877 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000878
879 // type-name
880 case tok::annot_template_id: {
881 TemplateIdAnnotation *TemplateId
882 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000883 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000884 // This template-id does not refer to a type name, so we're
885 // done with the type-specifiers.
886 goto DoneWithDeclSpec;
887 }
888
889 // Turn the template-id annotation token into a type annotation
890 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +0000891 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000892 continue;
893 }
894
Reid Spencer5f016e22007-07-11 17:01:13 +0000895 // GNU attributes support.
896 case tok::kw___attribute:
897 DS.AddAttributes(ParseAttributes());
898 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +0000899
900 // Microsoft declspec support.
901 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +0000902 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +0000903 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000904
Steve Naroff239f0732008-12-25 14:16:32 +0000905 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000906 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +0000907 // FIXME: Add handling here!
908 break;
909
910 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000911 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +0000912 case tok::kw___cdecl:
913 case tok::kw___stdcall:
914 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +0000915 DS.AddAttributes(ParseMicrosoftTypeAttributes());
916 continue;
917
Reid Spencer5f016e22007-07-11 17:01:13 +0000918 // storage-class-specifier
919 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +0000920 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
921 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000922 break;
923 case tok::kw_extern:
924 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000925 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +0000926 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
927 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000928 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000929 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000930 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +0000931 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000932 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000933 case tok::kw_static:
934 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000935 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +0000936 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
937 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000938 break;
939 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +0000940 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +0000941 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
942 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +0000943 else
John McCallfec54012009-08-03 20:12:06 +0000944 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
945 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000946 break;
947 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +0000948 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
949 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000950 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000951 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +0000952 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
953 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +0000954 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000955 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +0000956 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000957 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000958
Reid Spencer5f016e22007-07-11 17:01:13 +0000959 // function-specifier
960 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +0000961 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000963 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +0000964 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +0000965 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000966 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +0000967 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +0000968 break;
Chris Lattner80d0c892009-01-21 19:48:37 +0000969
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000970 // friend
971 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +0000972 if (DSContext == DSC_class)
973 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
974 else {
975 PrevSpec = ""; // not actually used by the diagnostic
976 DiagID = diag::err_friend_invalid_in_context;
977 isInvalid = true;
978 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000979 break;
John McCallfec54012009-08-03 20:12:06 +0000980
Chris Lattner80d0c892009-01-21 19:48:37 +0000981 // type-specifier
982 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000983 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
984 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +0000985 break;
986 case tok::kw_long:
987 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +0000988 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
989 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +0000990 else
John McCallfec54012009-08-03 20:12:06 +0000991 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
992 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +0000993 break;
994 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000995 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
996 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +0000997 break;
998 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +0000999 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1000 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001001 break;
1002 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001003 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1004 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001005 break;
1006 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001007 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1008 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001009 break;
1010 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001011 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1012 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001013 break;
1014 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001015 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1016 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001017 break;
1018 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001019 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1020 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001021 break;
1022 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001023 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1024 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001025 break;
1026 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001027 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1028 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001029 break;
1030 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001031 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1032 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001033 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001034 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001035 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1036 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001037 break;
1038 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001039 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1040 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001041 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001042 case tok::kw_bool:
1043 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001044 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1045 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001046 break;
1047 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001048 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1049 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001050 break;
1051 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001052 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1053 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001054 break;
1055 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001056 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1057 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001058 break;
1059
1060 // class-specifier:
1061 case tok::kw_class:
1062 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001063 case tok::kw_union: {
1064 tok::TokenKind Kind = Tok.getKind();
1065 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001066 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001067 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001068 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001069
1070 // enum-specifier:
1071 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001072 ConsumeToken();
1073 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001074 continue;
1075
1076 // cv-qualifier:
1077 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001078 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1079 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001080 break;
1081 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001082 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1083 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001084 break;
1085 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001086 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1087 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001088 break;
1089
Douglas Gregord57959a2009-03-27 23:10:48 +00001090 // C++ typename-specifier:
1091 case tok::kw_typename:
1092 if (TryAnnotateTypeOrScopeToken())
1093 continue;
1094 break;
1095
Chris Lattner80d0c892009-01-21 19:48:37 +00001096 // GNU typeof support.
1097 case tok::kw_typeof:
1098 ParseTypeofSpecifier(DS);
1099 continue;
1100
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001101 case tok::kw_decltype:
1102 ParseDecltypeSpecifier(DS);
1103 continue;
1104
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001105 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001106 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001107 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1108 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001109 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001110 goto DoneWithDeclSpec;
1111
1112 {
1113 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001114 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +00001115 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek1bc5bbf2009-06-30 22:19:00 +00001116 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +00001117 DS.SetRangeEnd(EndProtoLoc);
1118
Chris Lattner1ab3b962008-11-18 07:48:38 +00001119 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner75e36062009-04-03 18:38:42 +00001120 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001121 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001122 // Need to support trailing type qualifiers (e.g. "id<p> const").
1123 // If a type specifier follows, it will be diagnosed elsewhere.
1124 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001125 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001126 }
John McCallfec54012009-08-03 20:12:06 +00001127 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001128 if (isInvalid) {
1129 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001130 assert(DiagID);
Chris Lattner1ab3b962008-11-18 07:48:38 +00001131 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001132 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001133 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 ConsumeToken();
1135 }
1136}
Douglas Gregoradcac882008-12-01 23:54:00 +00001137
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001138/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001139/// primarily follow the C++ grammar with additions for C99 and GNU,
1140/// which together subsume the C grammar. Note that the C++
1141/// type-specifier also includes the C type-qualifier (for const,
1142/// volatile, and C99 restrict). Returns true if a type-specifier was
1143/// found (and parsed), false otherwise.
1144///
1145/// type-specifier: [C++ 7.1.5]
1146/// simple-type-specifier
1147/// class-specifier
1148/// enum-specifier
1149/// elaborated-type-specifier [TODO]
1150/// cv-qualifier
1151///
1152/// cv-qualifier: [C++ 7.1.5.1]
1153/// 'const'
1154/// 'volatile'
1155/// [C99] 'restrict'
1156///
1157/// simple-type-specifier: [ C++ 7.1.5.2]
1158/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1159/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1160/// 'char'
1161/// 'wchar_t'
1162/// 'bool'
1163/// 'short'
1164/// 'int'
1165/// 'long'
1166/// 'signed'
1167/// 'unsigned'
1168/// 'float'
1169/// 'double'
1170/// 'void'
1171/// [C99] '_Bool'
1172/// [C99] '_Complex'
1173/// [C99] '_Imaginary' // Removed in TC2?
1174/// [GNU] '_Decimal32'
1175/// [GNU] '_Decimal64'
1176/// [GNU] '_Decimal128'
1177/// [GNU] typeof-specifier
1178/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1179/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001180/// [C++0x] 'decltype' ( expression )
John McCallfec54012009-08-03 20:12:06 +00001181bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001182 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001183 unsigned &DiagID,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001184 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001185 SourceLocation Loc = Tok.getLocation();
1186
1187 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001188 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001189 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001190 // Annotate typenames and C++ scope specifiers. If we get one, just
1191 // recurse to handle whatever we get.
1192 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001193 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1194 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001195 // Otherwise, not a type specifier.
1196 return false;
1197 case tok::coloncolon: // ::foo::bar
1198 if (NextToken().is(tok::kw_new) || // ::new
1199 NextToken().is(tok::kw_delete)) // ::delete
1200 return false;
1201
1202 // Annotate typenames and C++ scope specifiers. If we get one, just
1203 // recurse to handle whatever we get.
1204 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001205 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1206 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001207 // Otherwise, not a type specifier.
1208 return false;
1209
Douglas Gregor12e083c2008-11-07 15:42:26 +00001210 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001211 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001212 if (Tok.getAnnotationValue())
1213 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001214 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001215 else
1216 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001217 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1218 ConsumeToken(); // The typename
Douglas Gregor12e083c2008-11-07 15:42:26 +00001219
1220 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1221 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1222 // Objective-C interface. If we don't have Objective-C or a '<', this is
1223 // just a normal reference to a typedef name.
1224 if (!Tok.is(tok::less) || !getLang().ObjC1)
1225 return true;
1226
1227 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001228 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001229 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek1bc5bbf2009-06-30 22:19:00 +00001230 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001231
1232 DS.SetRangeEnd(EndProtoLoc);
1233 return true;
1234 }
1235
1236 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001237 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001238 break;
1239 case tok::kw_long:
1240 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001241 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1242 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001243 else
John McCallfec54012009-08-03 20:12:06 +00001244 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1245 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001246 break;
1247 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001248 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001249 break;
1250 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001251 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1252 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001253 break;
1254 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001255 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1256 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001257 break;
1258 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001259 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1260 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001261 break;
1262 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001263 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001264 break;
1265 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001266 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001267 break;
1268 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001269 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001270 break;
1271 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001272 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001273 break;
1274 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001275 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001276 break;
1277 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001278 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001279 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001280 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001281 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001282 break;
1283 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001284 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001285 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001286 case tok::kw_bool:
1287 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001288 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001289 break;
1290 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001291 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1292 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001293 break;
1294 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001295 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1296 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001297 break;
1298 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001299 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1300 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001301 break;
1302
1303 // class-specifier:
1304 case tok::kw_class:
1305 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001306 case tok::kw_union: {
1307 tok::TokenKind Kind = Tok.getKind();
1308 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001309 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001310 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001311 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001312
1313 // enum-specifier:
1314 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001315 ConsumeToken();
1316 ParseEnumSpecifier(Loc, DS);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001317 return true;
1318
1319 // cv-qualifier:
1320 case tok::kw_const:
1321 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001322 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001323 break;
1324 case tok::kw_volatile:
1325 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001326 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001327 break;
1328 case tok::kw_restrict:
1329 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001330 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001331 break;
1332
1333 // GNU typeof support.
1334 case tok::kw_typeof:
1335 ParseTypeofSpecifier(DS);
1336 return true;
1337
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001338 // C++0x decltype support.
1339 case tok::kw_decltype:
1340 ParseDecltypeSpecifier(DS);
1341 return true;
1342
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001343 // C++0x auto support.
1344 case tok::kw_auto:
1345 if (!getLang().CPlusPlus0x)
1346 return false;
1347
John McCallfec54012009-08-03 20:12:06 +00001348 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001349 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001350 case tok::kw___ptr64:
1351 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001352 case tok::kw___cdecl:
1353 case tok::kw___stdcall:
1354 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001355 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001356 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001357
Douglas Gregor12e083c2008-11-07 15:42:26 +00001358 default:
1359 // Not a type-specifier; do nothing.
1360 return false;
1361 }
1362
1363 // If the specifier combination wasn't legal, issue a diagnostic.
1364 if (isInvalid) {
1365 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001366 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001367 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001368 }
1369 DS.SetRangeEnd(Tok.getLocation());
1370 ConsumeToken(); // whatever we parsed above.
1371 return true;
1372}
Reid Spencer5f016e22007-07-11 17:01:13 +00001373
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001374/// ParseStructDeclaration - Parse a struct declaration without the terminating
1375/// semicolon.
1376///
Reid Spencer5f016e22007-07-11 17:01:13 +00001377/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001378/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001379/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001380/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001381/// struct-declarator-list:
1382/// struct-declarator
1383/// struct-declarator-list ',' struct-declarator
1384/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1385/// struct-declarator:
1386/// declarator
1387/// [GNU] declarator attributes[opt]
1388/// declarator[opt] ':' constant-expression
1389/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1390///
Chris Lattnere1359422008-04-10 06:46:29 +00001391void Parser::
1392ParseStructDeclaration(DeclSpec &DS,
1393 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001394 if (Tok.is(tok::kw___extension__)) {
1395 // __extension__ silences extension warnings in the subexpression.
1396 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001397 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001398 return ParseStructDeclaration(DS, Fields);
1399 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001400
1401 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001402 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001403 ParseSpecifierQualifierList(DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001404
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001405 // If there are no declarators, this is a free-standing declaration
1406 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001407 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001408 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001409 return;
1410 }
1411
1412 // Read struct-declarators until we find the semicolon.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001413 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +00001414 while (1) {
Chris Lattnere1359422008-04-10 06:46:29 +00001415 FieldDeclarator &DeclaratorInfo = Fields.back();
1416
Steve Naroff28a7ca82007-08-20 22:28:22 +00001417 /// struct-declarator: declarator
1418 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +00001419 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +00001420 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001421
Chris Lattner04d66662007-10-09 17:33:22 +00001422 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001423 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001424 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001425 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001426 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001427 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001428 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001429 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001430
Steve Naroff28a7ca82007-08-20 22:28:22 +00001431 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001432 if (Tok.is(tok::kw___attribute)) {
1433 SourceLocation Loc;
1434 AttributeList *AttrList = ParseAttributes(&Loc);
1435 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1436 }
1437
Steve Naroff28a7ca82007-08-20 22:28:22 +00001438 // If we don't have a comma, it is either the end of the list (a ';')
1439 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001440 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001441 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001442
Steve Naroff28a7ca82007-08-20 22:28:22 +00001443 // Consume the comma.
1444 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001445
Steve Naroff28a7ca82007-08-20 22:28:22 +00001446 // Parse the next declarator.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001447 Fields.push_back(FieldDeclarator(DS));
Sebastian Redlab197ba2009-02-09 18:23:29 +00001448
Steve Naroff28a7ca82007-08-20 22:28:22 +00001449 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001450 if (Tok.is(tok::kw___attribute)) {
1451 SourceLocation Loc;
1452 AttributeList *AttrList = ParseAttributes(&Loc);
1453 Fields.back().D.AddAttributes(AttrList, Loc);
1454 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001455 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001456}
1457
1458/// ParseStructUnionBody
1459/// struct-contents:
1460/// struct-declaration-list
1461/// [EXT] empty
1462/// [GNU] "struct-declaration-list" without terminatoring ';'
1463/// struct-declaration-list:
1464/// struct-declaration
1465/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001466/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001467///
Reid Spencer5f016e22007-07-11 17:01:13 +00001468void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001469 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001470 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1471 PP.getSourceManager(),
1472 "parsing struct/union body");
Chris Lattner27b7f102009-03-05 02:25:03 +00001473
Reid Spencer5f016e22007-07-11 17:01:13 +00001474 SourceLocation LBraceLoc = ConsumeBrace();
1475
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001476 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001477 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1478
Reid Spencer5f016e22007-07-11 17:01:13 +00001479 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1480 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001481 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001482 Diag(Tok, diag::ext_empty_struct_union_enum)
1483 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001484
Chris Lattnerb28317a2009-03-28 19:18:32 +00001485 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001486 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1487
Reid Spencer5f016e22007-07-11 17:01:13 +00001488 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001489 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001490 // Each iteration of this loop reads one struct-declaration.
1491
1492 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001493 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001494 Diag(Tok, diag::ext_extra_struct_semi)
1495 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001496 ConsumeToken();
1497 continue;
1498 }
Chris Lattnere1359422008-04-10 06:46:29 +00001499
1500 // Parse all the comma separated declarators.
1501 DeclSpec DS;
1502 FieldDeclarators.clear();
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001503 if (!Tok.is(tok::at)) {
1504 ParseStructDeclaration(DS, FieldDeclarators);
1505
1506 // Convert them all to fields.
1507 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1508 FieldDeclarator &FD = FieldDeclarators[i];
1509 // Install the declarator into the current TagDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001510 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1511 DS.getSourceRange().getBegin(),
1512 FD.D, FD.BitfieldSize);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001513 FieldDecls.push_back(Field);
1514 }
1515 } else { // Handle @defs
1516 ConsumeToken();
1517 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1518 Diag(Tok, diag::err_unexpected_at);
1519 SkipUntil(tok::semi, true, true);
1520 continue;
1521 }
1522 ConsumeToken();
1523 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1524 if (!Tok.is(tok::identifier)) {
1525 Diag(Tok, diag::err_expected_ident);
1526 SkipUntil(tok::semi, true, true);
1527 continue;
1528 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001529 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor44b43212008-12-11 16:49:14 +00001530 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1531 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001532 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1533 ConsumeToken();
1534 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1535 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001536
Chris Lattner04d66662007-10-09 17:33:22 +00001537 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001538 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001539 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001540 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001541 break;
1542 } else {
1543 Diag(Tok, diag::err_expected_semi_decl_list);
1544 // Skip to end of block or statement
1545 SkipUntil(tok::r_brace, true, true);
1546 }
1547 }
1548
Steve Naroff60fccee2007-10-29 21:38:07 +00001549 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001550
Reid Spencer5f016e22007-07-11 17:01:13 +00001551 AttributeList *AttrList = 0;
1552 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001553 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +00001554 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001555
1556 Actions.ActOnFields(CurScope,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001557 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001558 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001559 AttrList);
1560 StructScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001561 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001562}
1563
1564
1565/// ParseEnumSpecifier
1566/// enum-specifier: [C99 6.7.2.2]
1567/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001568///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001569/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1570/// '}' attributes[opt]
1571/// 'enum' identifier
1572/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001573///
1574/// [C++] elaborated-type-specifier:
1575/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1576///
Chris Lattner4c97d762009-04-12 21:49:30 +00001577void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1578 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001579 // Parse the tag portion of this.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001580
1581 AttributeList *Attr = 0;
1582 // If attributes exist after tag, parse them.
1583 if (Tok.is(tok::kw___attribute))
1584 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001585
1586 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001587 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001588 if (Tok.isNot(tok::identifier)) {
1589 Diag(Tok, diag::err_expected_ident);
1590 if (Tok.isNot(tok::l_brace)) {
1591 // Has no name and is not a definition.
1592 // Skip the rest of this declarator, up until the comma or semicolon.
1593 SkipUntil(tok::comma, true);
1594 return;
1595 }
1596 }
1597 }
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001598
1599 // Must have either 'enum name' or 'enum {...}'.
1600 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1601 Diag(Tok, diag::err_expected_ident_lbrace);
1602
1603 // Skip the rest of this declarator, up until the comma or semicolon.
1604 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001605 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001606 }
1607
1608 // If an identifier is present, consume and remember it.
1609 IdentifierInfo *Name = 0;
1610 SourceLocation NameLoc;
1611 if (Tok.is(tok::identifier)) {
1612 Name = Tok.getIdentifierInfo();
1613 NameLoc = ConsumeToken();
1614 }
1615
1616 // There are three options here. If we have 'enum foo;', then this is a
1617 // forward declaration. If we have 'enum foo {...' then this is a
1618 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1619 //
1620 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1621 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1622 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1623 //
John McCall0f434ec2009-07-31 02:45:11 +00001624 Action::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001625 if (Tok.is(tok::l_brace))
John McCall0f434ec2009-07-31 02:45:11 +00001626 TUK = Action::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001627 else if (Tok.is(tok::semi))
John McCall0f434ec2009-07-31 02:45:11 +00001628 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001629 else
John McCall0f434ec2009-07-31 02:45:11 +00001630 TUK = Action::TUK_Reference;
Douglas Gregor402abb52009-05-28 23:31:59 +00001631 bool Owned = false;
John McCall0f434ec2009-07-31 02:45:11 +00001632 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Douglas Gregor402abb52009-05-28 23:31:59 +00001633 StartLoc, SS, Name, NameLoc, Attr, AS,
Douglas Gregorbd1099e2009-07-23 16:36:45 +00001634 Action::MultiTemplateParamsArg(Actions),
Douglas Gregor402abb52009-05-28 23:31:59 +00001635 Owned);
Reid Spencer5f016e22007-07-11 17:01:13 +00001636
Chris Lattner04d66662007-10-09 17:33:22 +00001637 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001638 ParseEnumBody(StartLoc, TagDecl);
1639
1640 // TODO: semantic analysis on the declspec for enums.
1641 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001642 unsigned DiagID;
1643 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, DiagID,
Douglas Gregor402abb52009-05-28 23:31:59 +00001644 TagDecl.getAs<void>(), Owned))
John McCallfec54012009-08-03 20:12:06 +00001645 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001646}
1647
1648/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1649/// enumerator-list:
1650/// enumerator
1651/// enumerator-list ',' enumerator
1652/// enumerator:
1653/// enumeration-constant
1654/// enumeration-constant '=' constant-expression
1655/// enumeration-constant:
1656/// identifier
1657///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001658void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001659 // Enter the scope of the enum body and start the definition.
1660 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001661 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001662
Reid Spencer5f016e22007-07-11 17:01:13 +00001663 SourceLocation LBraceLoc = ConsumeBrace();
1664
Chris Lattner7946dd32007-08-27 17:24:30 +00001665 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001666 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001667 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Reid Spencer5f016e22007-07-11 17:01:13 +00001668
Chris Lattnerb28317a2009-03-28 19:18:32 +00001669 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00001670
Chris Lattnerb28317a2009-03-28 19:18:32 +00001671 DeclPtrTy LastEnumConstDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001672
1673 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001674 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001675 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1676 SourceLocation IdentLoc = ConsumeToken();
1677
1678 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001679 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001680 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001681 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001682 AssignedVal = ParseConstantExpression();
1683 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001684 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001685 }
1686
1687 // Install the enumerator constant into EnumDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001688 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1689 LastEnumConstDecl,
1690 IdentLoc, Ident,
1691 EqualLoc,
1692 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001693 EnumConstantDecls.push_back(EnumConstDecl);
1694 LastEnumConstDecl = EnumConstDecl;
1695
Chris Lattner04d66662007-10-09 17:33:22 +00001696 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001697 break;
1698 SourceLocation CommaLoc = ConsumeToken();
1699
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001700 if (Tok.isNot(tok::identifier) &&
1701 !(getLang().C99 || getLang().CPlusPlus0x))
1702 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1703 << getLang().CPlusPlus
1704 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Reid Spencer5f016e22007-07-11 17:01:13 +00001705 }
1706
1707 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00001708 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001709
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001710 AttributeList *Attr = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001711 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001712 if (Tok.is(tok::kw___attribute))
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001713 Attr = ParseAttributes();
Douglas Gregor72de6672009-01-08 20:45:30 +00001714
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001715 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
1716 EnumConstantDecls.data(), EnumConstantDecls.size(),
1717 CurScope, Attr);
1718
Douglas Gregor72de6672009-01-08 20:45:30 +00001719 EnumScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001720 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001721}
1722
1723/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001724/// start of a type-qualifier-list.
1725bool Parser::isTypeQualifier() const {
1726 switch (Tok.getKind()) {
1727 default: return false;
1728 // type-qualifier
1729 case tok::kw_const:
1730 case tok::kw_volatile:
1731 case tok::kw_restrict:
1732 return true;
1733 }
1734}
1735
1736/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001737/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001738bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001739 switch (Tok.getKind()) {
1740 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001741
1742 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001743 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001744 // Annotate typenames and C++ scope specifiers. If we get one, just
1745 // recurse to handle whatever we get.
1746 if (TryAnnotateTypeOrScopeToken())
1747 return isTypeSpecifierQualifier();
1748 // Otherwise, not a type specifier.
1749 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001750
Chris Lattner166a8fc2009-01-04 23:41:41 +00001751 case tok::coloncolon: // ::foo::bar
1752 if (NextToken().is(tok::kw_new) || // ::new
1753 NextToken().is(tok::kw_delete)) // ::delete
1754 return false;
1755
1756 // Annotate typenames and C++ scope specifiers. If we get one, just
1757 // recurse to handle whatever we get.
1758 if (TryAnnotateTypeOrScopeToken())
1759 return isTypeSpecifierQualifier();
1760 // Otherwise, not a type specifier.
1761 return false;
1762
Reid Spencer5f016e22007-07-11 17:01:13 +00001763 // GNU attributes support.
1764 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001765 // GNU typeof support.
1766 case tok::kw_typeof:
1767
Reid Spencer5f016e22007-07-11 17:01:13 +00001768 // type-specifiers
1769 case tok::kw_short:
1770 case tok::kw_long:
1771 case tok::kw_signed:
1772 case tok::kw_unsigned:
1773 case tok::kw__Complex:
1774 case tok::kw__Imaginary:
1775 case tok::kw_void:
1776 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001777 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001778 case tok::kw_char16_t:
1779 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001780 case tok::kw_int:
1781 case tok::kw_float:
1782 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001783 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001784 case tok::kw__Bool:
1785 case tok::kw__Decimal32:
1786 case tok::kw__Decimal64:
1787 case tok::kw__Decimal128:
1788
Chris Lattner99dc9142008-04-13 18:59:07 +00001789 // struct-or-union-specifier (C99) or class-specifier (C++)
1790 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001791 case tok::kw_struct:
1792 case tok::kw_union:
1793 // enum-specifier
1794 case tok::kw_enum:
1795
1796 // type-qualifier
1797 case tok::kw_const:
1798 case tok::kw_volatile:
1799 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001800
1801 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001802 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001803 return true;
Chris Lattner7c186be2008-10-20 00:25:30 +00001804
1805 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1806 case tok::less:
1807 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001808
1809 case tok::kw___cdecl:
1810 case tok::kw___stdcall:
1811 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001812 case tok::kw___w64:
1813 case tok::kw___ptr64:
1814 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001815 }
1816}
1817
1818/// isDeclarationSpecifier() - Return true if the current token is part of a
1819/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001820bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001821 switch (Tok.getKind()) {
1822 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001823
1824 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00001825 // Unfortunate hack to support "Class.factoryMethod" notation.
1826 if (getLang().ObjC1 && NextToken().is(tok::period))
1827 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001828 // Fall through
Steve Naroff61f72cb2009-03-09 21:12:44 +00001829
Douglas Gregord57959a2009-03-27 23:10:48 +00001830 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001831 // Annotate typenames and C++ scope specifiers. If we get one, just
1832 // recurse to handle whatever we get.
1833 if (TryAnnotateTypeOrScopeToken())
1834 return isDeclarationSpecifier();
1835 // Otherwise, not a declaration specifier.
1836 return false;
1837 case tok::coloncolon: // ::foo::bar
1838 if (NextToken().is(tok::kw_new) || // ::new
1839 NextToken().is(tok::kw_delete)) // ::delete
1840 return false;
1841
1842 // Annotate typenames and C++ scope specifiers. If we get one, just
1843 // recurse to handle whatever we get.
1844 if (TryAnnotateTypeOrScopeToken())
1845 return isDeclarationSpecifier();
1846 // Otherwise, not a declaration specifier.
1847 return false;
1848
Reid Spencer5f016e22007-07-11 17:01:13 +00001849 // storage-class-specifier
1850 case tok::kw_typedef:
1851 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001852 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001853 case tok::kw_static:
1854 case tok::kw_auto:
1855 case tok::kw_register:
1856 case tok::kw___thread:
1857
1858 // type-specifiers
1859 case tok::kw_short:
1860 case tok::kw_long:
1861 case tok::kw_signed:
1862 case tok::kw_unsigned:
1863 case tok::kw__Complex:
1864 case tok::kw__Imaginary:
1865 case tok::kw_void:
1866 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001867 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001868 case tok::kw_char16_t:
1869 case tok::kw_char32_t:
1870
Reid Spencer5f016e22007-07-11 17:01:13 +00001871 case tok::kw_int:
1872 case tok::kw_float:
1873 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001874 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001875 case tok::kw__Bool:
1876 case tok::kw__Decimal32:
1877 case tok::kw__Decimal64:
1878 case tok::kw__Decimal128:
1879
Chris Lattner99dc9142008-04-13 18:59:07 +00001880 // struct-or-union-specifier (C99) or class-specifier (C++)
1881 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001882 case tok::kw_struct:
1883 case tok::kw_union:
1884 // enum-specifier
1885 case tok::kw_enum:
1886
1887 // type-qualifier
1888 case tok::kw_const:
1889 case tok::kw_volatile:
1890 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001891
Reid Spencer5f016e22007-07-11 17:01:13 +00001892 // function-specifier
1893 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00001894 case tok::kw_virtual:
1895 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001896
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001897 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001898 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001899
Chris Lattner1ef08762007-08-09 17:01:07 +00001900 // GNU typeof support.
1901 case tok::kw_typeof:
1902
1903 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001904 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00001905 return true;
Chris Lattnerf3948c42008-07-26 03:38:44 +00001906
1907 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1908 case tok::less:
1909 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001910
Steve Naroff47f52092009-01-06 19:34:12 +00001911 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00001912 case tok::kw___cdecl:
1913 case tok::kw___stdcall:
1914 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001915 case tok::kw___w64:
1916 case tok::kw___ptr64:
1917 case tok::kw___forceinline:
1918 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001919 }
1920}
1921
1922
1923/// ParseTypeQualifierListOpt
1924/// type-qualifier-list: [C99 6.7.5]
1925/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001926/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001927/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001928/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001929///
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001930void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001931 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001932 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001933 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001934 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001935 SourceLocation Loc = Tok.getLocation();
1936
1937 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001938 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001939 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
1940 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001941 break;
1942 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001943 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1944 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001945 break;
1946 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001947 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1948 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001949 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001950 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001951 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00001952 case tok::kw___cdecl:
1953 case tok::kw___stdcall:
1954 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001955 if (AttributesAllowed) {
1956 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1957 continue;
1958 }
1959 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00001960 case tok::kw___attribute:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001961 if (AttributesAllowed) {
1962 DS.AddAttributes(ParseAttributes());
1963 continue; // do *not* consume the next token!
1964 }
1965 // otherwise, FALL THROUGH!
1966 default:
Steve Naroff239f0732008-12-25 14:16:32 +00001967 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001968 // If this is not a type-qualifier token, we're done reading type
1969 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001970 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001971 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001972 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00001973
Reid Spencer5f016e22007-07-11 17:01:13 +00001974 // If the specifier combination wasn't legal, issue a diagnostic.
1975 if (isInvalid) {
1976 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001977 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001978 }
1979 ConsumeToken();
1980 }
1981}
1982
1983
1984/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1985///
1986void Parser::ParseDeclarator(Declarator &D) {
1987 /// This implements the 'declarator' production in the C grammar, then checks
1988 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001989 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00001990}
1991
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001992/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1993/// is parsed by the function passed to it. Pass null, and the direct-declarator
1994/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001995/// ptr-operator production.
1996///
Sebastian Redlf30208a2009-01-24 21:16:55 +00001997/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1998/// [C] pointer[opt] direct-declarator
1999/// [C++] direct-declarator
2000/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002001///
2002/// pointer: [C99 6.7.5]
2003/// '*' type-qualifier-list[opt]
2004/// '*' type-qualifier-list[opt] pointer
2005///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002006/// ptr-operator:
2007/// '*' cv-qualifier-seq[opt]
2008/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002009/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002010/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002011/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002012/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002013void Parser::ParseDeclaratorInternal(Declarator &D,
2014 DirectDeclParseFunction DirectDeclParser) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002015
Sebastian Redlf30208a2009-01-24 21:16:55 +00002016 // C++ member pointers start with a '::' or a nested-name.
2017 // Member pointers get special handling, since there's no place for the
2018 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002019 if (getLang().CPlusPlus &&
2020 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2021 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002022 CXXScopeSpec SS;
2023 if (ParseOptionalCXXScopeSpecifier(SS)) {
2024 if(Tok.isNot(tok::star)) {
2025 // The scope spec really belongs to the direct-declarator.
2026 D.getCXXScopeSpec() = SS;
2027 if (DirectDeclParser)
2028 (this->*DirectDeclParser)(D);
2029 return;
2030 }
2031
2032 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002033 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002034 DeclSpec DS;
2035 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002036 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002037
2038 // Recurse to parse whatever is left.
2039 ParseDeclaratorInternal(D, DirectDeclParser);
2040
2041 // Sema will have to catch (syntactically invalid) pointers into global
2042 // scope. It has to catch pointers into namespace scope anyway.
2043 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002044 Loc, DS.TakeAttributes()),
2045 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002046 return;
2047 }
2048 }
2049
2050 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002051 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002052 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002053 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002054 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002055 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002056 if (DirectDeclParser)
2057 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002058 return;
2059 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002060
Sebastian Redl05532f22009-03-15 22:02:01 +00002061 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2062 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002063 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002064 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002065
Chris Lattner9af55002009-03-27 04:18:06 +00002066 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002067 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002068 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002069
Reid Spencer5f016e22007-07-11 17:01:13 +00002070 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002071 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002072
Reid Spencer5f016e22007-07-11 17:01:13 +00002073 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002074 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002075 if (Kind == tok::star)
2076 // Remember that we parsed a pointer type, and remember the type-quals.
2077 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002078 DS.TakeAttributes()),
2079 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002080 else
2081 // Remember that we parsed a Block type, and remember the type-quals.
2082 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002083 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002084 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002085 } else {
2086 // Is a reference
2087 DeclSpec DS;
2088
Sebastian Redl743de1f2009-03-23 00:00:23 +00002089 // Complain about rvalue references in C++03, but then go on and build
2090 // the declarator.
2091 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2092 Diag(Loc, diag::err_rvalue_reference);
2093
Reid Spencer5f016e22007-07-11 17:01:13 +00002094 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2095 // cv-qualifiers are introduced through the use of a typedef or of a
2096 // template type argument, in which case the cv-qualifiers are ignored.
2097 //
2098 // [GNU] Retricted references are allowed.
2099 // [GNU] Attributes on references are allowed.
2100 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002101 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002102
2103 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2104 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2105 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002106 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002107 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2108 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002109 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002110 }
2111
2112 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002113 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002114
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002115 if (D.getNumTypeObjects() > 0) {
2116 // C++ [dcl.ref]p4: There shall be no references to references.
2117 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2118 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002119 if (const IdentifierInfo *II = D.getIdentifier())
2120 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2121 << II;
2122 else
2123 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2124 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002125
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002126 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002127 // can go ahead and build the (technically ill-formed)
2128 // declarator: reference collapsing will take care of it.
2129 }
2130 }
2131
Reid Spencer5f016e22007-07-11 17:01:13 +00002132 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002133 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002134 DS.TakeAttributes(),
2135 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002136 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002137 }
2138}
2139
2140/// ParseDirectDeclarator
2141/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002142/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002143/// '(' declarator ')'
2144/// [GNU] '(' attributes declarator ')'
2145/// [C90] direct-declarator '[' constant-expression[opt] ']'
2146/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2147/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2148/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2149/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2150/// direct-declarator '(' parameter-type-list ')'
2151/// direct-declarator '(' identifier-list[opt] ')'
2152/// [GNU] direct-declarator '(' parameter-forward-declarations
2153/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002154/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2155/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002156/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002157///
2158/// declarator-id: [C++ 8]
2159/// id-expression
2160/// '::'[opt] nested-name-specifier[opt] type-name
2161///
2162/// id-expression: [C++ 5.1]
2163/// unqualified-id
2164/// qualified-id [TODO]
2165///
2166/// unqualified-id: [C++ 5.1]
2167/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002168/// operator-function-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002169/// conversion-function-id [TODO]
2170/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002171/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002172///
Reid Spencer5f016e22007-07-11 17:01:13 +00002173void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002174 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002175
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002176 if (getLang().CPlusPlus) {
2177 if (D.mayHaveIdentifier()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002178 // ParseDeclaratorInternal might already have parsed the scope.
2179 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2180 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002181 if (afterCXXScope) {
2182 // Change the declaration context for name lookup, until this function
2183 // is exited (and the declarator has been parsed).
2184 DeclScopeObj.EnterDeclaratorScope();
2185 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002186
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002187 if (Tok.is(tok::identifier)) {
2188 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Anders Carlsson4649cac2009-04-30 22:41:11 +00002189
2190 // If this identifier is the name of the current class, it's a
2191 // constructor name.
2192 if (!D.getDeclSpec().hasTypeSpecifier() &&
2193 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
Douglas Gregor675431d2009-07-06 16:40:48 +00002194 CXXScopeSpec *SS = afterCXXScope? &D.getCXXScopeSpec() : 0;
Anders Carlsson4649cac2009-04-30 22:41:11 +00002195 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor675431d2009-07-06 16:40:48 +00002196 Tok.getLocation(), CurScope, SS),
Anders Carlsson4649cac2009-04-30 22:41:11 +00002197 Tok.getLocation());
2198 // This is a normal identifier.
2199 } else
2200 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002201 ConsumeToken();
2202 goto PastIdentifier;
Douglas Gregor39a8de12009-02-25 19:37:18 +00002203 } else if (Tok.is(tok::annot_template_id)) {
2204 TemplateIdAnnotation *TemplateId
2205 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
2206
2207 // FIXME: Could this template-id name a constructor?
2208
2209 // FIXME: This is an egregious hack, where we silently ignore
2210 // the specialization (which should be a function template
2211 // specialization name) and use the name instead. This hack
2212 // will go away when we have support for function
2213 // specializations.
2214 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
2215 TemplateId->Destroy();
2216 ConsumeToken();
2217 goto PastIdentifier;
Douglas Gregor70316a02008-12-26 15:00:45 +00002218 } else if (Tok.is(tok::kw_operator)) {
2219 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002220 SourceLocation EndLoc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002221
Douglas Gregor70316a02008-12-26 15:00:45 +00002222 // First try the name of an overloaded operator
Sebastian Redlab197ba2009-02-09 18:23:29 +00002223 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
2224 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor70316a02008-12-26 15:00:45 +00002225 } else {
2226 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redlab197ba2009-02-09 18:23:29 +00002227 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
2228 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
2229 else {
Douglas Gregor70316a02008-12-26 15:00:45 +00002230 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002231 }
Douglas Gregor70316a02008-12-26 15:00:45 +00002232 }
2233 goto PastIdentifier;
2234 } else if (Tok.is(tok::tilde)) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002235 // This should be a C++ destructor.
2236 SourceLocation TildeLoc = ConsumeToken();
2237 if (Tok.is(tok::identifier)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002238 // FIXME: Inaccurate.
2239 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7f43d672009-02-25 23:52:28 +00002240 SourceLocation EndLoc;
Douglas Gregor675431d2009-07-06 16:40:48 +00002241 CXXScopeSpec *SS = afterCXXScope? &D.getCXXScopeSpec() : 0;
Fariborz Jahaniand33c8682009-07-20 17:43:15 +00002242 TypeResult Type = ParseClassName(EndLoc, SS, true);
Douglas Gregor31a19b62009-04-01 21:51:26 +00002243 if (Type.isInvalid())
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002244 D.SetIdentifier(0, TildeLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00002245 else
2246 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002247 } else {
Fariborz Jahaniand33c8682009-07-20 17:43:15 +00002248 Diag(Tok, diag::err_destructor_class_name);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002249 D.SetIdentifier(0, TildeLoc);
2250 }
2251 goto PastIdentifier;
2252 }
2253
2254 // If we reached this point, token is not identifier and not '~'.
2255
2256 if (afterCXXScope) {
2257 Diag(Tok, diag::err_expected_unqualified_id);
2258 D.SetIdentifier(0, Tok.getLocation());
2259 D.setInvalidType(true);
2260 goto PastIdentifier;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002261 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002262 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002263 }
2264
2265 // If we reached this point, we are either in C/ObjC or the token didn't
2266 // satisfy any of the C++-specific checks.
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002267 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2268 assert(!getLang().CPlusPlus &&
2269 "There's a C++-specific check for tok::identifier above");
2270 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2271 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2272 ConsumeToken();
2273 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002274 // direct-declarator: '(' declarator ')'
2275 // direct-declarator: '(' attributes declarator ')'
2276 // Example: 'char (*X)' or 'int (*XX)(void)'
2277 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002278 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002279 // This could be something simple like "int" (in which case the declarator
2280 // portion is empty), if an abstract-declarator is allowed.
2281 D.SetIdentifier(0, Tok.getLocation());
2282 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002283 if (D.getContext() == Declarator::MemberContext)
2284 Diag(Tok, diag::err_expected_member_name_or_semi)
2285 << D.getDeclSpec().getSourceRange();
2286 else if (getLang().CPlusPlus)
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002287 Diag(Tok, diag::err_expected_unqualified_id);
2288 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002289 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002290 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002291 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002292 }
2293
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002294 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002295 assert(D.isPastIdentifier() &&
2296 "Haven't past the location of the identifier yet?");
2297
2298 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002299 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002300 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2301 // In such a case, check if we actually have a function declarator; if it
2302 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002303 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2304 // When not in file scope, warn for ambiguous function declarators, just
2305 // in case the author intended it as a variable definition.
2306 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2307 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2308 break;
2309 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002310 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002311 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002312 ParseBracketDeclarator(D);
2313 } else {
2314 break;
2315 }
2316 }
2317}
2318
Chris Lattneref4715c2008-04-06 05:45:57 +00002319/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2320/// only called before the identifier, so these are most likely just grouping
2321/// parens for precedence. If we find that these are actually function
2322/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2323///
2324/// direct-declarator:
2325/// '(' declarator ')'
2326/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002327/// direct-declarator '(' parameter-type-list ')'
2328/// direct-declarator '(' identifier-list[opt] ')'
2329/// [GNU] direct-declarator '(' parameter-forward-declarations
2330/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002331///
2332void Parser::ParseParenDeclarator(Declarator &D) {
2333 SourceLocation StartLoc = ConsumeParen();
2334 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
2335
Chris Lattner7399ee02008-10-20 02:05:46 +00002336 // Eat any attributes before we look at whether this is a grouping or function
2337 // declarator paren. If this is a grouping paren, the attribute applies to
2338 // the type being built up, for example:
2339 // int (__attribute__(()) *x)(long y)
2340 // If this ends up not being a grouping paren, the attribute applies to the
2341 // first argument, for example:
2342 // int (__attribute__(()) int x)
2343 // In either case, we need to eat any attributes to be able to determine what
2344 // sort of paren this is.
2345 //
2346 AttributeList *AttrList = 0;
2347 bool RequiresArg = false;
2348 if (Tok.is(tok::kw___attribute)) {
2349 AttrList = ParseAttributes();
2350
2351 // We require that the argument list (if this is a non-grouping paren) be
2352 // present even if the attribute list was empty.
2353 RequiresArg = true;
2354 }
Steve Naroff239f0732008-12-25 14:16:32 +00002355 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002356 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2357 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2358 Tok.is(tok::kw___ptr64)) {
2359 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2360 }
Chris Lattner7399ee02008-10-20 02:05:46 +00002361
Chris Lattneref4715c2008-04-06 05:45:57 +00002362 // If we haven't past the identifier yet (or where the identifier would be
2363 // stored, if this is an abstract declarator), then this is probably just
2364 // grouping parens. However, if this could be an abstract-declarator, then
2365 // this could also be the start of function arguments (consider 'void()').
2366 bool isGrouping;
2367
2368 if (!D.mayOmitIdentifier()) {
2369 // If this can't be an abstract-declarator, this *must* be a grouping
2370 // paren, because we haven't seen the identifier yet.
2371 isGrouping = true;
2372 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002373 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002374 isDeclarationSpecifier()) { // 'int(int)' is a function.
2375 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2376 // considered to be a type, not a K&R identifier-list.
2377 isGrouping = false;
2378 } else {
2379 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2380 isGrouping = true;
2381 }
2382
2383 // If this is a grouping paren, handle:
2384 // direct-declarator: '(' declarator ')'
2385 // direct-declarator: '(' attributes declarator ')'
2386 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002387 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002388 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002389 if (AttrList)
Sebastian Redlab197ba2009-02-09 18:23:29 +00002390 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002391
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002392 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002393 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002394 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002395
2396 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002397 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002398 return;
2399 }
2400
2401 // Okay, if this wasn't a grouping paren, it must be the start of a function
2402 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002403 // identifier (and remember where it would have been), then call into
2404 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002405 D.SetIdentifier(0, Tok.getLocation());
2406
Chris Lattner7399ee02008-10-20 02:05:46 +00002407 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002408}
2409
2410/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2411/// declarator D up to a paren, which indicates that we are parsing function
2412/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002413///
Chris Lattner7399ee02008-10-20 02:05:46 +00002414/// If AttrList is non-null, then the caller parsed those arguments immediately
2415/// after the open paren - they should be considered to be the first argument of
2416/// a parameter. If RequiresArg is true, then the first argument of the
2417/// function is required to be present and required to not be an identifier
2418/// list.
2419///
Reid Spencer5f016e22007-07-11 17:01:13 +00002420/// This method also handles this portion of the grammar:
2421/// parameter-type-list: [C99 6.7.5]
2422/// parameter-list
2423/// parameter-list ',' '...'
2424///
2425/// parameter-list: [C99 6.7.5]
2426/// parameter-declaration
2427/// parameter-list ',' parameter-declaration
2428///
2429/// parameter-declaration: [C99 6.7.5]
2430/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002431/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002432/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002433/// declaration-specifiers abstract-declarator[opt]
2434/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002435/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002436/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2437///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002438/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002439/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002440///
Chris Lattner7399ee02008-10-20 02:05:46 +00002441void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2442 AttributeList *AttrList,
2443 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002444 // lparen is already consumed!
2445 assert(D.isPastIdentifier() && "Should not call before identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00002446
Chris Lattner7399ee02008-10-20 02:05:46 +00002447 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002448 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002449 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002450 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002451 delete AttrList;
2452 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002453
Sebastian Redlab197ba2009-02-09 18:23:29 +00002454 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002455
2456 // cv-qualifier-seq[opt].
2457 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002458 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002459 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002460 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002461 llvm::SmallVector<TypeTy*, 2> Exceptions;
2462 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002463 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002464 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002465 if (!DS.getSourceRange().getEnd().isInvalid())
2466 Loc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002467
2468 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002469 if (Tok.is(tok::kw_throw)) {
2470 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002471 ThrowLoc = Tok.getLocation();
Sebastian Redlef65f062009-05-29 18:02:33 +00002472 ParseExceptionSpecification(Loc, Exceptions, ExceptionRanges,
2473 hasAnyExceptionSpec);
2474 assert(Exceptions.size() == ExceptionRanges.size() &&
2475 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002476 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002477 }
2478
Chris Lattnerf97409f2008-04-06 06:57:35 +00002479 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002480 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002481 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002482 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002483 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002484 /*arglist*/ 0, 0,
2485 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002486 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002487 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002488 Exceptions.data(),
2489 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002490 Exceptions.size(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002491 LParenLoc, D),
2492 Loc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002493 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00002494 }
2495
Chris Lattner7399ee02008-10-20 02:05:46 +00002496 // Alternatively, this parameter list may be an identifier list form for a
2497 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00002498 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00002499 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002500 // K&R identifier lists can't have typedefs as identifiers, per
2501 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002502 if (RequiresArg) {
2503 Diag(Tok, diag::err_argument_required_after_attribute);
2504 delete AttrList;
2505 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002506 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2507 // normal declarators, not for abstract-declarators.
2508 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002509 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002510 }
2511
2512 // Finally, a normal, non-empty parameter type list.
2513
2514 // Build up an array of information about the parsed arguments.
2515 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002516
2517 // Enter function-declaration scope, limiting any declarators to the
2518 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002519 ParseScope PrototypeScope(this,
2520 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002521
2522 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002523 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002524 while (1) {
2525 if (Tok.is(tok::ellipsis)) {
2526 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002527 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002528 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002529 }
2530
Chris Lattnerf97409f2008-04-06 06:57:35 +00002531 SourceLocation DSStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00002532
Chris Lattnerf97409f2008-04-06 06:57:35 +00002533 // Parse the declaration-specifiers.
2534 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002535
2536 // If the caller parsed attributes for the first argument, add them now.
2537 if (AttrList) {
2538 DS.AddAttributes(AttrList);
2539 AttrList = 0; // Only apply the attributes to the first parameter.
2540 }
Chris Lattnere64c5492009-02-27 18:38:20 +00002541 ParseDeclarationSpecifiers(DS);
2542
Chris Lattnerf97409f2008-04-06 06:57:35 +00002543 // Parse the declarator. This is "PrototypeContext", because we must
2544 // accept either 'declarator' or 'abstract-declarator' here.
2545 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2546 ParseDeclarator(ParmDecl);
2547
2548 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002549 if (Tok.is(tok::kw___attribute)) {
2550 SourceLocation Loc;
2551 AttributeList *AttrList = ParseAttributes(&Loc);
2552 ParmDecl.AddAttributes(AttrList, Loc);
2553 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002554
Chris Lattnerf97409f2008-04-06 06:57:35 +00002555 // Remember this parsed parameter in ParamInfo.
2556 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2557
Douglas Gregor72b505b2008-12-16 21:30:33 +00002558 // DefArgToks is used when the parsing of default arguments needs
2559 // to be delayed.
2560 CachedTokens *DefArgToks = 0;
2561
Chris Lattnerf97409f2008-04-06 06:57:35 +00002562 // If no parameter was specified, verify that *something* was specified,
2563 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00002564 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2565 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002566 // Completely missing, emit error.
2567 Diag(DSStart, diag::err_missing_param);
2568 } else {
2569 // Otherwise, we have something. Add it and let semantic analysis try
2570 // to grok it and add the result to the ParamInfo we are building.
2571
2572 // Inform the actions module about the parameter declarator, so it gets
2573 // added to the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002574 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00002575
2576 // Parse the default argument, if any. We parse the default
2577 // arguments in all dialects; the semantic analysis in
2578 // ActOnParamDefaultArgument will reject the default argument in
2579 // C.
2580 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002581 SourceLocation EqualLoc = Tok.getLocation();
2582
Chris Lattner04421082008-04-08 04:40:51 +00002583 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002584 if (D.getContext() == Declarator::MemberContext) {
2585 // If we're inside a class definition, cache the tokens
2586 // corresponding to the default argument. We'll actually parse
2587 // them when we see the end of the class definition.
2588 // FIXME: Templates will require something similar.
2589 // FIXME: Can we use a smart pointer for Toks?
2590 DefArgToks = new CachedTokens;
2591
2592 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2593 tok::semi, false)) {
2594 delete DefArgToks;
2595 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002596 Actions.ActOnParamDefaultArgumentError(Param);
2597 } else
Anders Carlsson5e300d12009-06-12 16:51:40 +00002598 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
2599 (*DefArgToks)[1].getLocation());
Chris Lattner04421082008-04-08 04:40:51 +00002600 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002601 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002602 ConsumeToken();
Douglas Gregor72b505b2008-12-16 21:30:33 +00002603
2604 OwningExprResult DefArgResult(ParseAssignmentExpression());
2605 if (DefArgResult.isInvalid()) {
2606 Actions.ActOnParamDefaultArgumentError(Param);
2607 SkipUntil(tok::comma, tok::r_paren, true, true);
2608 } else {
2609 // Inform the actions module about the default argument
2610 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002611 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00002612 }
Chris Lattner04421082008-04-08 04:40:51 +00002613 }
2614 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002615
2616 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002617 ParmDecl.getIdentifierLoc(), Param,
2618 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002619 }
2620
2621 // If the next token is a comma, consume it and keep reading arguments.
2622 if (Tok.isNot(tok::comma)) break;
2623
2624 // Consume the comma.
2625 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002626 }
2627
Chris Lattnerf97409f2008-04-06 06:57:35 +00002628 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002629 PrototypeScope.Exit();
Chris Lattnerf97409f2008-04-06 06:57:35 +00002630
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002631 // If we have the closing ')', eat it.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002632 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002633
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002634 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002635 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002636 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002637 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002638 llvm::SmallVector<TypeTy*, 2> Exceptions;
2639 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002640 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002641 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002642 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002643 if (!DS.getSourceRange().getEnd().isInvalid())
2644 Loc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002645
2646 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002647 if (Tok.is(tok::kw_throw)) {
2648 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002649 ThrowLoc = Tok.getLocation();
Sebastian Redlef65f062009-05-29 18:02:33 +00002650 ParseExceptionSpecification(Loc, Exceptions, ExceptionRanges,
2651 hasAnyExceptionSpec);
2652 assert(Exceptions.size() == ExceptionRanges.size() &&
2653 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002654 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002655 }
2656
Reid Spencer5f016e22007-07-11 17:01:13 +00002657 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002658 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002659 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00002660 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002661 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002662 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002663 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002664 Exceptions.data(),
2665 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002666 Exceptions.size(), LParenLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002667 Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002668}
2669
Chris Lattner66d28652008-04-06 06:34:08 +00002670/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2671/// we found a K&R-style identifier list instead of a type argument list. The
2672/// current token is known to be the first identifier in the list.
2673///
2674/// identifier-list: [C99 6.7.5]
2675/// identifier
2676/// identifier-list ',' identifier
2677///
2678void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2679 Declarator &D) {
2680 // Build up an array of information about the parsed arguments.
2681 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2682 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2683
2684 // If there was no identifier specified for the declarator, either we are in
2685 // an abstract-declarator, or we are in a parameter declarator which was found
2686 // to be abstract. In abstract-declarators, identifier lists are not valid:
2687 // diagnose this.
2688 if (!D.getIdentifier())
2689 Diag(Tok, diag::ext_ident_list_in_param);
2690
2691 // Tok is known to be the first identifier in the list. Remember this
2692 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002693 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002694 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattnerb28317a2009-03-28 19:18:32 +00002695 Tok.getLocation(),
2696 DeclPtrTy()));
Chris Lattner66d28652008-04-06 06:34:08 +00002697
Chris Lattner50c64772008-04-06 06:39:19 +00002698 ConsumeToken(); // eat the first identifier.
Chris Lattner66d28652008-04-06 06:34:08 +00002699
2700 while (Tok.is(tok::comma)) {
2701 // Eat the comma.
2702 ConsumeToken();
2703
Chris Lattner50c64772008-04-06 06:39:19 +00002704 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002705 if (Tok.isNot(tok::identifier)) {
2706 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002707 SkipUntil(tok::r_paren);
2708 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002709 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002710
Chris Lattner66d28652008-04-06 06:34:08 +00002711 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002712
2713 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00002714 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002715 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner66d28652008-04-06 06:34:08 +00002716
2717 // Verify that the argument identifier has not already been mentioned.
2718 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002719 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002720 } else {
2721 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002722 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002723 Tok.getLocation(),
2724 DeclPtrTy()));
Chris Lattner50c64772008-04-06 06:39:19 +00002725 }
Chris Lattner66d28652008-04-06 06:34:08 +00002726
2727 // Eat the identifier.
2728 ConsumeToken();
2729 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002730
2731 // If we have the closing ')', eat it and we're done.
2732 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2733
Chris Lattner50c64772008-04-06 06:39:19 +00002734 // Remember that we parsed a function type, and remember the attributes. This
2735 // function type is always a K&R style function type, which is not varargs and
2736 // has no prototype.
2737 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002738 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00002739 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002740 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00002741 /*exception*/false,
2742 SourceLocation(), false, 0, 0, 0,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002743 LParenLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002744 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002745}
Chris Lattneref4715c2008-04-06 05:45:57 +00002746
Reid Spencer5f016e22007-07-11 17:01:13 +00002747/// [C90] direct-declarator '[' constant-expression[opt] ']'
2748/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2749/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2750/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2751/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2752void Parser::ParseBracketDeclarator(Declarator &D) {
2753 SourceLocation StartLoc = ConsumeBracket();
2754
Chris Lattner378c7e42008-12-18 07:27:21 +00002755 // C array syntax has many features, but by-far the most common is [] and [4].
2756 // This code does a fast path to handle some of the most obvious cases.
2757 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002758 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002759 // Remember that we parsed the empty array type.
2760 OwningExprResult NumElements(Actions);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002761 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
2762 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002763 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002764 return;
2765 } else if (Tok.getKind() == tok::numeric_constant &&
2766 GetLookAheadToken(1).is(tok::r_square)) {
2767 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002768 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002769 ConsumeToken();
2770
Sebastian Redlab197ba2009-02-09 18:23:29 +00002771 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002772
2773 // If there was an error parsing the assignment-expression, recover.
2774 if (ExprRes.isInvalid())
2775 ExprRes.release(); // Deallocate expr, just use [].
2776
2777 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002778 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
2779 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002780 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002781 return;
2782 }
2783
Reid Spencer5f016e22007-07-11 17:01:13 +00002784 // If valid, this location is the position where we read the 'static' keyword.
2785 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002786 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002787 StaticLoc = ConsumeToken();
2788
2789 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002790 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002791 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002792 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Reid Spencer5f016e22007-07-11 17:01:13 +00002793
2794 // If we haven't already read 'static', check to see if there is one after the
2795 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002796 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002797 StaticLoc = ConsumeToken();
2798
2799 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2800 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002801 OwningExprResult NumElements(Actions);
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002802
2803 // Handle the case where we have '[*]' as the array size. However, a leading
2804 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2805 // the the token after the star is a ']'. Since stars in arrays are
2806 // infrequent, use of lookahead is not costly here.
2807 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002808 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002809
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002810 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002811 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002812 StaticLoc = SourceLocation(); // Drop the static.
2813 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002814 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002815 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002816 // Note, in C89, this production uses the constant-expr production instead
2817 // of assignment-expr. The only difference is that assignment-expr allows
2818 // things like '=' and '*='. Sema rejects these in C89 mode because they
2819 // are not i-c-e's, so we don't need to distinguish between the two here.
2820
Douglas Gregore0762c92009-06-19 23:52:42 +00002821 // Parse the constant-expression or assignment-expression now (depending
2822 // on dialect).
2823 if (getLang().CPlusPlus)
2824 NumElements = ParseConstantExpression();
2825 else
2826 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00002827 }
2828
2829 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002830 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00002831 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002832 // If the expression was invalid, skip it.
2833 SkipUntil(tok::r_square);
2834 return;
2835 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002836
2837 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2838
Chris Lattner378c7e42008-12-18 07:27:21 +00002839 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00002840 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2841 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002842 NumElements.release(),
2843 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002844 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002845}
2846
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002847/// [GNU] typeof-specifier:
2848/// typeof ( expressions )
2849/// typeof ( type-name )
2850/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00002851///
2852void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00002853 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002854 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002855 SourceLocation StartLoc = ConsumeToken();
2856
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002857 bool isCastExpr;
2858 TypeTy *CastTy;
2859 SourceRange CastRange;
2860 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
2861 isCastExpr,
2862 CastTy,
2863 CastRange);
2864
2865 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002866 // FIXME: Not accurate, the range gets one token more than it should.
2867 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002868 else
2869 DS.SetRangeEnd(CastRange.getEnd());
2870
2871 if (isCastExpr) {
2872 if (!CastTy) {
2873 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002874 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00002875 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002876
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002877 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002878 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002879 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2880 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002881 DiagID, CastTy))
2882 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002883 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002884 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002885
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002886 // If we get here, the operand to the typeof was an expresion.
2887 if (Operand.isInvalid()) {
2888 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002889 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002890 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002891
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002892 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002893 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002894 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2895 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002896 DiagID, Operand.release()))
2897 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002898}