blob: 597d43fb1b2e6fbeb9a6c2667e2ab7547b61df8c [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 Carlsson9abf2ae2009-08-16 05:13:48 +0000449 Actions.AddInitializerToDecl(ThisDecl, move(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.
Douglas Gregordec06662009-08-21 18:42:58 +0000847 if (getLang().CPlusPlus &&
848 (CurScope->isClassScope() ||
849 (CurScope->isTemplateParamScope() &&
850 CurScope->getParent()->isClassScope())) &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000851 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
852 NextToken().getKind() == tok::l_paren)
853 goto DoneWithDeclSpec;
854
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000855 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000856 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +0000857 if (isInvalid)
858 break;
859
860 DS.SetRangeEnd(Tok.getLocation());
861 ConsumeToken(); // The identifier
862
863 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
864 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
865 // Objective-C interface. If we don't have Objective-C or a '<', this is
866 // just a normal reference to a typedef name.
867 if (!Tok.is(tok::less) || !getLang().ObjC1)
868 continue;
869
870 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000871 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000872 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek1bc5bbf2009-06-30 22:19:00 +0000873 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000874
875 DS.SetRangeEnd(EndProtoLoc);
876
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000877 // Need to support trailing type qualifiers (e.g. "id<p> const").
878 // If a type specifier follows, it will be diagnosed elsewhere.
879 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000880 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000881
882 // type-name
883 case tok::annot_template_id: {
884 TemplateIdAnnotation *TemplateId
885 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000886 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000887 // This template-id does not refer to a type name, so we're
888 // done with the type-specifiers.
889 goto DoneWithDeclSpec;
890 }
891
892 // Turn the template-id annotation token into a type annotation
893 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +0000894 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000895 continue;
896 }
897
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 // GNU attributes support.
899 case tok::kw___attribute:
900 DS.AddAttributes(ParseAttributes());
901 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +0000902
903 // Microsoft declspec support.
904 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +0000905 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +0000906 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000907
Steve Naroff239f0732008-12-25 14:16:32 +0000908 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000909 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +0000910 // FIXME: Add handling here!
911 break;
912
913 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000914 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +0000915 case tok::kw___cdecl:
916 case tok::kw___stdcall:
917 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +0000918 DS.AddAttributes(ParseMicrosoftTypeAttributes());
919 continue;
920
Reid Spencer5f016e22007-07-11 17:01:13 +0000921 // storage-class-specifier
922 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +0000923 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
924 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000925 break;
926 case tok::kw_extern:
927 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000928 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +0000929 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
930 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000931 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000932 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000933 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +0000934 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000935 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000936 case tok::kw_static:
937 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000938 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +0000939 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
940 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000941 break;
942 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +0000943 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +0000944 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
945 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +0000946 else
John McCallfec54012009-08-03 20:12:06 +0000947 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
948 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000949 break;
950 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +0000951 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
952 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000953 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000954 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +0000955 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
956 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +0000957 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000958 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +0000959 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000960 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000961
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 // function-specifier
963 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +0000964 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000965 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000966 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +0000967 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +0000968 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000969 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +0000970 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +0000971 break;
Chris Lattner80d0c892009-01-21 19:48:37 +0000972
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000973 // friend
974 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +0000975 if (DSContext == DSC_class)
976 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
977 else {
978 PrevSpec = ""; // not actually used by the diagnostic
979 DiagID = diag::err_friend_invalid_in_context;
980 isInvalid = true;
981 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000982 break;
John McCallfec54012009-08-03 20:12:06 +0000983
Chris Lattner80d0c892009-01-21 19:48:37 +0000984 // type-specifier
985 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000986 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
987 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +0000988 break;
989 case tok::kw_long:
990 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +0000991 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
992 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +0000993 else
John McCallfec54012009-08-03 20:12:06 +0000994 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
995 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +0000996 break;
997 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000998 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
999 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001000 break;
1001 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001002 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1003 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001004 break;
1005 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001006 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1007 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001008 break;
1009 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001010 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1011 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001012 break;
1013 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001014 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1015 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001016 break;
1017 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001018 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1019 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001020 break;
1021 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001022 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1023 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001024 break;
1025 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001026 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1027 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001028 break;
1029 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001030 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1031 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001032 break;
1033 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001034 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1035 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001036 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001037 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001038 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1039 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001040 break;
1041 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001042 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1043 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001044 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001045 case tok::kw_bool:
1046 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001047 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1048 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001049 break;
1050 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001051 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1052 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001053 break;
1054 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001055 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1056 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001057 break;
1058 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001059 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1060 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001061 break;
1062
1063 // class-specifier:
1064 case tok::kw_class:
1065 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001066 case tok::kw_union: {
1067 tok::TokenKind Kind = Tok.getKind();
1068 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001069 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001070 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001071 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001072
1073 // enum-specifier:
1074 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001075 ConsumeToken();
1076 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001077 continue;
1078
1079 // cv-qualifier:
1080 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001081 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1082 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001083 break;
1084 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001085 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1086 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001087 break;
1088 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001089 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1090 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001091 break;
1092
Douglas Gregord57959a2009-03-27 23:10:48 +00001093 // C++ typename-specifier:
1094 case tok::kw_typename:
1095 if (TryAnnotateTypeOrScopeToken())
1096 continue;
1097 break;
1098
Chris Lattner80d0c892009-01-21 19:48:37 +00001099 // GNU typeof support.
1100 case tok::kw_typeof:
1101 ParseTypeofSpecifier(DS);
1102 continue;
1103
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001104 case tok::kw_decltype:
1105 ParseDecltypeSpecifier(DS);
1106 continue;
1107
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001108 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001109 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001110 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1111 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001112 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001113 goto DoneWithDeclSpec;
1114
1115 {
1116 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001117 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +00001118 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek1bc5bbf2009-06-30 22:19:00 +00001119 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +00001120 DS.SetRangeEnd(EndProtoLoc);
1121
Chris Lattner1ab3b962008-11-18 07:48:38 +00001122 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner75e36062009-04-03 18:38:42 +00001123 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001124 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001125 // Need to support trailing type qualifiers (e.g. "id<p> const").
1126 // If a type specifier follows, it will be diagnosed elsewhere.
1127 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001128 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001129 }
John McCallfec54012009-08-03 20:12:06 +00001130 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 if (isInvalid) {
1132 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001133 assert(DiagID);
Chris Lattner1ab3b962008-11-18 07:48:38 +00001134 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001135 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001136 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001137 ConsumeToken();
1138 }
1139}
Douglas Gregoradcac882008-12-01 23:54:00 +00001140
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001141/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001142/// primarily follow the C++ grammar with additions for C99 and GNU,
1143/// which together subsume the C grammar. Note that the C++
1144/// type-specifier also includes the C type-qualifier (for const,
1145/// volatile, and C99 restrict). Returns true if a type-specifier was
1146/// found (and parsed), false otherwise.
1147///
1148/// type-specifier: [C++ 7.1.5]
1149/// simple-type-specifier
1150/// class-specifier
1151/// enum-specifier
1152/// elaborated-type-specifier [TODO]
1153/// cv-qualifier
1154///
1155/// cv-qualifier: [C++ 7.1.5.1]
1156/// 'const'
1157/// 'volatile'
1158/// [C99] 'restrict'
1159///
1160/// simple-type-specifier: [ C++ 7.1.5.2]
1161/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1162/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1163/// 'char'
1164/// 'wchar_t'
1165/// 'bool'
1166/// 'short'
1167/// 'int'
1168/// 'long'
1169/// 'signed'
1170/// 'unsigned'
1171/// 'float'
1172/// 'double'
1173/// 'void'
1174/// [C99] '_Bool'
1175/// [C99] '_Complex'
1176/// [C99] '_Imaginary' // Removed in TC2?
1177/// [GNU] '_Decimal32'
1178/// [GNU] '_Decimal64'
1179/// [GNU] '_Decimal128'
1180/// [GNU] typeof-specifier
1181/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1182/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001183/// [C++0x] 'decltype' ( expression )
John McCallfec54012009-08-03 20:12:06 +00001184bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001185 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001186 unsigned &DiagID,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001187 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001188 SourceLocation Loc = Tok.getLocation();
1189
1190 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001191 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001192 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001193 // Annotate typenames and C++ scope specifiers. If we get one, just
1194 // recurse to handle whatever we get.
1195 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001196 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1197 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001198 // Otherwise, not a type specifier.
1199 return false;
1200 case tok::coloncolon: // ::foo::bar
1201 if (NextToken().is(tok::kw_new) || // ::new
1202 NextToken().is(tok::kw_delete)) // ::delete
1203 return false;
1204
1205 // Annotate typenames and C++ scope specifiers. If we get one, just
1206 // recurse to handle whatever we get.
1207 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001208 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1209 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001210 // Otherwise, not a type specifier.
1211 return false;
1212
Douglas Gregor12e083c2008-11-07 15:42:26 +00001213 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001214 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001215 if (Tok.getAnnotationValue())
1216 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001217 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001218 else
1219 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001220 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1221 ConsumeToken(); // The typename
Douglas Gregor12e083c2008-11-07 15:42:26 +00001222
1223 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1224 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1225 // Objective-C interface. If we don't have Objective-C or a '<', this is
1226 // just a normal reference to a typedef name.
1227 if (!Tok.is(tok::less) || !getLang().ObjC1)
1228 return true;
1229
1230 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001231 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001232 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek1bc5bbf2009-06-30 22:19:00 +00001233 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001234
1235 DS.SetRangeEnd(EndProtoLoc);
1236 return true;
1237 }
1238
1239 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001240 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001241 break;
1242 case tok::kw_long:
1243 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001244 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1245 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001246 else
John McCallfec54012009-08-03 20:12:06 +00001247 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1248 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001249 break;
1250 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001251 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001252 break;
1253 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001254 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1255 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001256 break;
1257 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001258 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1259 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001260 break;
1261 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001262 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1263 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001264 break;
1265 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001266 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001267 break;
1268 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001269 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001270 break;
1271 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001272 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001273 break;
1274 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001275 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001276 break;
1277 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001278 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001279 break;
1280 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001281 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001282 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001283 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001284 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001285 break;
1286 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001287 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001288 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001289 case tok::kw_bool:
1290 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001291 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001292 break;
1293 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001294 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1295 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001296 break;
1297 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001298 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1299 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001300 break;
1301 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001302 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1303 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001304 break;
1305
1306 // class-specifier:
1307 case tok::kw_class:
1308 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001309 case tok::kw_union: {
1310 tok::TokenKind Kind = Tok.getKind();
1311 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001312 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001313 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001314 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001315
1316 // enum-specifier:
1317 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001318 ConsumeToken();
1319 ParseEnumSpecifier(Loc, DS);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001320 return true;
1321
1322 // cv-qualifier:
1323 case tok::kw_const:
1324 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001325 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001326 break;
1327 case tok::kw_volatile:
1328 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001329 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001330 break;
1331 case tok::kw_restrict:
1332 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001333 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001334 break;
1335
1336 // GNU typeof support.
1337 case tok::kw_typeof:
1338 ParseTypeofSpecifier(DS);
1339 return true;
1340
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001341 // C++0x decltype support.
1342 case tok::kw_decltype:
1343 ParseDecltypeSpecifier(DS);
1344 return true;
1345
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001346 // C++0x auto support.
1347 case tok::kw_auto:
1348 if (!getLang().CPlusPlus0x)
1349 return false;
1350
John McCallfec54012009-08-03 20:12:06 +00001351 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001352 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001353 case tok::kw___ptr64:
1354 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001355 case tok::kw___cdecl:
1356 case tok::kw___stdcall:
1357 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001358 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001359 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001360
Douglas Gregor12e083c2008-11-07 15:42:26 +00001361 default:
1362 // Not a type-specifier; do nothing.
1363 return false;
1364 }
1365
1366 // If the specifier combination wasn't legal, issue a diagnostic.
1367 if (isInvalid) {
1368 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001369 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001370 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001371 }
1372 DS.SetRangeEnd(Tok.getLocation());
1373 ConsumeToken(); // whatever we parsed above.
1374 return true;
1375}
Reid Spencer5f016e22007-07-11 17:01:13 +00001376
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001377/// ParseStructDeclaration - Parse a struct declaration without the terminating
1378/// semicolon.
1379///
Reid Spencer5f016e22007-07-11 17:01:13 +00001380/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001381/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001382/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001383/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001384/// struct-declarator-list:
1385/// struct-declarator
1386/// struct-declarator-list ',' struct-declarator
1387/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1388/// struct-declarator:
1389/// declarator
1390/// [GNU] declarator attributes[opt]
1391/// declarator[opt] ':' constant-expression
1392/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1393///
Chris Lattnere1359422008-04-10 06:46:29 +00001394void Parser::
1395ParseStructDeclaration(DeclSpec &DS,
1396 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001397 if (Tok.is(tok::kw___extension__)) {
1398 // __extension__ silences extension warnings in the subexpression.
1399 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001400 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001401 return ParseStructDeclaration(DS, Fields);
1402 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001403
1404 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001405 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001406 ParseSpecifierQualifierList(DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001407
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001408 // If there are no declarators, this is a free-standing declaration
1409 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001410 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001411 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001412 return;
1413 }
1414
1415 // Read struct-declarators until we find the semicolon.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001416 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +00001417 while (1) {
Chris Lattnere1359422008-04-10 06:46:29 +00001418 FieldDeclarator &DeclaratorInfo = Fields.back();
1419
Steve Naroff28a7ca82007-08-20 22:28:22 +00001420 /// struct-declarator: declarator
1421 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +00001422 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +00001423 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001424
Chris Lattner04d66662007-10-09 17:33:22 +00001425 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001426 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001427 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001428 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001429 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001430 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001431 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001432 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001433
Steve Naroff28a7ca82007-08-20 22:28:22 +00001434 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001435 if (Tok.is(tok::kw___attribute)) {
1436 SourceLocation Loc;
1437 AttributeList *AttrList = ParseAttributes(&Loc);
1438 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1439 }
1440
Steve Naroff28a7ca82007-08-20 22:28:22 +00001441 // If we don't have a comma, it is either the end of the list (a ';')
1442 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001443 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001444 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001445
Steve Naroff28a7ca82007-08-20 22:28:22 +00001446 // Consume the comma.
1447 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001448
Steve Naroff28a7ca82007-08-20 22:28:22 +00001449 // Parse the next declarator.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001450 Fields.push_back(FieldDeclarator(DS));
Sebastian Redlab197ba2009-02-09 18:23:29 +00001451
Steve Naroff28a7ca82007-08-20 22:28:22 +00001452 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001453 if (Tok.is(tok::kw___attribute)) {
1454 SourceLocation Loc;
1455 AttributeList *AttrList = ParseAttributes(&Loc);
1456 Fields.back().D.AddAttributes(AttrList, Loc);
1457 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001458 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001459}
1460
1461/// ParseStructUnionBody
1462/// struct-contents:
1463/// struct-declaration-list
1464/// [EXT] empty
1465/// [GNU] "struct-declaration-list" without terminatoring ';'
1466/// struct-declaration-list:
1467/// struct-declaration
1468/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001469/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001470///
Reid Spencer5f016e22007-07-11 17:01:13 +00001471void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001472 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001473 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1474 PP.getSourceManager(),
1475 "parsing struct/union body");
Chris Lattner27b7f102009-03-05 02:25:03 +00001476
Reid Spencer5f016e22007-07-11 17:01:13 +00001477 SourceLocation LBraceLoc = ConsumeBrace();
1478
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001479 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001480 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1481
Reid Spencer5f016e22007-07-11 17:01:13 +00001482 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1483 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001484 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001485 Diag(Tok, diag::ext_empty_struct_union_enum)
1486 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001487
Chris Lattnerb28317a2009-03-28 19:18:32 +00001488 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001489 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1490
Reid Spencer5f016e22007-07-11 17:01:13 +00001491 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001492 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001493 // Each iteration of this loop reads one struct-declaration.
1494
1495 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001496 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001497 Diag(Tok, diag::ext_extra_struct_semi)
1498 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001499 ConsumeToken();
1500 continue;
1501 }
Chris Lattnere1359422008-04-10 06:46:29 +00001502
1503 // Parse all the comma separated declarators.
1504 DeclSpec DS;
1505 FieldDeclarators.clear();
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001506 if (!Tok.is(tok::at)) {
1507 ParseStructDeclaration(DS, FieldDeclarators);
1508
1509 // Convert them all to fields.
1510 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1511 FieldDeclarator &FD = FieldDeclarators[i];
1512 // Install the declarator into the current TagDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001513 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1514 DS.getSourceRange().getBegin(),
1515 FD.D, FD.BitfieldSize);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001516 FieldDecls.push_back(Field);
1517 }
1518 } else { // Handle @defs
1519 ConsumeToken();
1520 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1521 Diag(Tok, diag::err_unexpected_at);
1522 SkipUntil(tok::semi, true, true);
1523 continue;
1524 }
1525 ConsumeToken();
1526 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1527 if (!Tok.is(tok::identifier)) {
1528 Diag(Tok, diag::err_expected_ident);
1529 SkipUntil(tok::semi, true, true);
1530 continue;
1531 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001532 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor44b43212008-12-11 16:49:14 +00001533 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1534 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001535 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1536 ConsumeToken();
1537 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1538 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001539
Chris Lattner04d66662007-10-09 17:33:22 +00001540 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001541 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001542 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001543 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001544 break;
1545 } else {
1546 Diag(Tok, diag::err_expected_semi_decl_list);
1547 // Skip to end of block or statement
1548 SkipUntil(tok::r_brace, true, true);
1549 }
1550 }
1551
Steve Naroff60fccee2007-10-29 21:38:07 +00001552 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001553
Reid Spencer5f016e22007-07-11 17:01:13 +00001554 AttributeList *AttrList = 0;
1555 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001556 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +00001557 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001558
1559 Actions.ActOnFields(CurScope,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001560 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001561 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001562 AttrList);
1563 StructScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001564 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001565}
1566
1567
1568/// ParseEnumSpecifier
1569/// enum-specifier: [C99 6.7.2.2]
1570/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001571///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001572/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1573/// '}' attributes[opt]
1574/// 'enum' identifier
1575/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001576///
1577/// [C++] elaborated-type-specifier:
1578/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1579///
Chris Lattner4c97d762009-04-12 21:49:30 +00001580void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1581 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001582 // Parse the tag portion of this.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001583
1584 AttributeList *Attr = 0;
1585 // If attributes exist after tag, parse them.
1586 if (Tok.is(tok::kw___attribute))
1587 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001588
1589 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001590 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001591 if (Tok.isNot(tok::identifier)) {
1592 Diag(Tok, diag::err_expected_ident);
1593 if (Tok.isNot(tok::l_brace)) {
1594 // Has no name and is not a definition.
1595 // Skip the rest of this declarator, up until the comma or semicolon.
1596 SkipUntil(tok::comma, true);
1597 return;
1598 }
1599 }
1600 }
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001601
1602 // Must have either 'enum name' or 'enum {...}'.
1603 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1604 Diag(Tok, diag::err_expected_ident_lbrace);
1605
1606 // Skip the rest of this declarator, up until the comma or semicolon.
1607 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001608 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001609 }
1610
1611 // If an identifier is present, consume and remember it.
1612 IdentifierInfo *Name = 0;
1613 SourceLocation NameLoc;
1614 if (Tok.is(tok::identifier)) {
1615 Name = Tok.getIdentifierInfo();
1616 NameLoc = ConsumeToken();
1617 }
1618
1619 // There are three options here. If we have 'enum foo;', then this is a
1620 // forward declaration. If we have 'enum foo {...' then this is a
1621 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1622 //
1623 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1624 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1625 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1626 //
John McCall0f434ec2009-07-31 02:45:11 +00001627 Action::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001628 if (Tok.is(tok::l_brace))
John McCall0f434ec2009-07-31 02:45:11 +00001629 TUK = Action::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001630 else if (Tok.is(tok::semi))
John McCall0f434ec2009-07-31 02:45:11 +00001631 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001632 else
John McCall0f434ec2009-07-31 02:45:11 +00001633 TUK = Action::TUK_Reference;
Douglas Gregor402abb52009-05-28 23:31:59 +00001634 bool Owned = false;
John McCall0f434ec2009-07-31 02:45:11 +00001635 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Douglas Gregor402abb52009-05-28 23:31:59 +00001636 StartLoc, SS, Name, NameLoc, Attr, AS,
Douglas Gregorbd1099e2009-07-23 16:36:45 +00001637 Action::MultiTemplateParamsArg(Actions),
Douglas Gregor402abb52009-05-28 23:31:59 +00001638 Owned);
Reid Spencer5f016e22007-07-11 17:01:13 +00001639
Chris Lattner04d66662007-10-09 17:33:22 +00001640 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001641 ParseEnumBody(StartLoc, TagDecl);
1642
1643 // TODO: semantic analysis on the declspec for enums.
1644 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001645 unsigned DiagID;
1646 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, DiagID,
Douglas Gregor402abb52009-05-28 23:31:59 +00001647 TagDecl.getAs<void>(), Owned))
John McCallfec54012009-08-03 20:12:06 +00001648 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001649}
1650
1651/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1652/// enumerator-list:
1653/// enumerator
1654/// enumerator-list ',' enumerator
1655/// enumerator:
1656/// enumeration-constant
1657/// enumeration-constant '=' constant-expression
1658/// enumeration-constant:
1659/// identifier
1660///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001661void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001662 // Enter the scope of the enum body and start the definition.
1663 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001664 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001665
Reid Spencer5f016e22007-07-11 17:01:13 +00001666 SourceLocation LBraceLoc = ConsumeBrace();
1667
Chris Lattner7946dd32007-08-27 17:24:30 +00001668 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001669 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001670 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Reid Spencer5f016e22007-07-11 17:01:13 +00001671
Chris Lattnerb28317a2009-03-28 19:18:32 +00001672 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00001673
Chris Lattnerb28317a2009-03-28 19:18:32 +00001674 DeclPtrTy LastEnumConstDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001675
1676 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001677 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001678 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1679 SourceLocation IdentLoc = ConsumeToken();
1680
1681 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001682 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001683 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001684 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001685 AssignedVal = ParseConstantExpression();
1686 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001687 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001688 }
1689
1690 // Install the enumerator constant into EnumDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001691 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1692 LastEnumConstDecl,
1693 IdentLoc, Ident,
1694 EqualLoc,
1695 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001696 EnumConstantDecls.push_back(EnumConstDecl);
1697 LastEnumConstDecl = EnumConstDecl;
1698
Chris Lattner04d66662007-10-09 17:33:22 +00001699 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001700 break;
1701 SourceLocation CommaLoc = ConsumeToken();
1702
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001703 if (Tok.isNot(tok::identifier) &&
1704 !(getLang().C99 || getLang().CPlusPlus0x))
1705 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1706 << getLang().CPlusPlus
1707 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Reid Spencer5f016e22007-07-11 17:01:13 +00001708 }
1709
1710 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00001711 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001712
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001713 AttributeList *Attr = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001714 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001715 if (Tok.is(tok::kw___attribute))
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001716 Attr = ParseAttributes();
Douglas Gregor72de6672009-01-08 20:45:30 +00001717
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001718 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
1719 EnumConstantDecls.data(), EnumConstantDecls.size(),
1720 CurScope, Attr);
1721
Douglas Gregor72de6672009-01-08 20:45:30 +00001722 EnumScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001723 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001724}
1725
1726/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001727/// start of a type-qualifier-list.
1728bool Parser::isTypeQualifier() const {
1729 switch (Tok.getKind()) {
1730 default: return false;
1731 // type-qualifier
1732 case tok::kw_const:
1733 case tok::kw_volatile:
1734 case tok::kw_restrict:
1735 return true;
1736 }
1737}
1738
1739/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001740/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001741bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001742 switch (Tok.getKind()) {
1743 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001744
1745 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001746 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001747 // Annotate typenames and C++ scope specifiers. If we get one, just
1748 // recurse to handle whatever we get.
1749 if (TryAnnotateTypeOrScopeToken())
1750 return isTypeSpecifierQualifier();
1751 // Otherwise, not a type specifier.
1752 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001753
Chris Lattner166a8fc2009-01-04 23:41:41 +00001754 case tok::coloncolon: // ::foo::bar
1755 if (NextToken().is(tok::kw_new) || // ::new
1756 NextToken().is(tok::kw_delete)) // ::delete
1757 return false;
1758
1759 // Annotate typenames and C++ scope specifiers. If we get one, just
1760 // recurse to handle whatever we get.
1761 if (TryAnnotateTypeOrScopeToken())
1762 return isTypeSpecifierQualifier();
1763 // Otherwise, not a type specifier.
1764 return false;
1765
Reid Spencer5f016e22007-07-11 17:01:13 +00001766 // GNU attributes support.
1767 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001768 // GNU typeof support.
1769 case tok::kw_typeof:
1770
Reid Spencer5f016e22007-07-11 17:01:13 +00001771 // type-specifiers
1772 case tok::kw_short:
1773 case tok::kw_long:
1774 case tok::kw_signed:
1775 case tok::kw_unsigned:
1776 case tok::kw__Complex:
1777 case tok::kw__Imaginary:
1778 case tok::kw_void:
1779 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001780 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001781 case tok::kw_char16_t:
1782 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001783 case tok::kw_int:
1784 case tok::kw_float:
1785 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001786 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001787 case tok::kw__Bool:
1788 case tok::kw__Decimal32:
1789 case tok::kw__Decimal64:
1790 case tok::kw__Decimal128:
1791
Chris Lattner99dc9142008-04-13 18:59:07 +00001792 // struct-or-union-specifier (C99) or class-specifier (C++)
1793 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001794 case tok::kw_struct:
1795 case tok::kw_union:
1796 // enum-specifier
1797 case tok::kw_enum:
1798
1799 // type-qualifier
1800 case tok::kw_const:
1801 case tok::kw_volatile:
1802 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001803
1804 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001805 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001806 return true;
Chris Lattner7c186be2008-10-20 00:25:30 +00001807
1808 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1809 case tok::less:
1810 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001811
1812 case tok::kw___cdecl:
1813 case tok::kw___stdcall:
1814 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001815 case tok::kw___w64:
1816 case tok::kw___ptr64:
1817 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001818 }
1819}
1820
1821/// isDeclarationSpecifier() - Return true if the current token is part of a
1822/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001823bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001824 switch (Tok.getKind()) {
1825 default: return false;
Chris Lattner166a8fc2009-01-04 23:41:41 +00001826
1827 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00001828 // Unfortunate hack to support "Class.factoryMethod" notation.
1829 if (getLang().ObjC1 && NextToken().is(tok::period))
1830 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001831 // Fall through
Steve Naroff61f72cb2009-03-09 21:12:44 +00001832
Douglas Gregord57959a2009-03-27 23:10:48 +00001833 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001834 // Annotate typenames and C++ scope specifiers. If we get one, just
1835 // recurse to handle whatever we get.
1836 if (TryAnnotateTypeOrScopeToken())
1837 return isDeclarationSpecifier();
1838 // Otherwise, not a declaration specifier.
1839 return false;
1840 case tok::coloncolon: // ::foo::bar
1841 if (NextToken().is(tok::kw_new) || // ::new
1842 NextToken().is(tok::kw_delete)) // ::delete
1843 return false;
1844
1845 // Annotate typenames and C++ scope specifiers. If we get one, just
1846 // recurse to handle whatever we get.
1847 if (TryAnnotateTypeOrScopeToken())
1848 return isDeclarationSpecifier();
1849 // Otherwise, not a declaration specifier.
1850 return false;
1851
Reid Spencer5f016e22007-07-11 17:01:13 +00001852 // storage-class-specifier
1853 case tok::kw_typedef:
1854 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001855 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001856 case tok::kw_static:
1857 case tok::kw_auto:
1858 case tok::kw_register:
1859 case tok::kw___thread:
1860
1861 // type-specifiers
1862 case tok::kw_short:
1863 case tok::kw_long:
1864 case tok::kw_signed:
1865 case tok::kw_unsigned:
1866 case tok::kw__Complex:
1867 case tok::kw__Imaginary:
1868 case tok::kw_void:
1869 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001870 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001871 case tok::kw_char16_t:
1872 case tok::kw_char32_t:
1873
Reid Spencer5f016e22007-07-11 17:01:13 +00001874 case tok::kw_int:
1875 case tok::kw_float:
1876 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001877 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001878 case tok::kw__Bool:
1879 case tok::kw__Decimal32:
1880 case tok::kw__Decimal64:
1881 case tok::kw__Decimal128:
1882
Chris Lattner99dc9142008-04-13 18:59:07 +00001883 // struct-or-union-specifier (C99) or class-specifier (C++)
1884 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001885 case tok::kw_struct:
1886 case tok::kw_union:
1887 // enum-specifier
1888 case tok::kw_enum:
1889
1890 // type-qualifier
1891 case tok::kw_const:
1892 case tok::kw_volatile:
1893 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001894
Reid Spencer5f016e22007-07-11 17:01:13 +00001895 // function-specifier
1896 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00001897 case tok::kw_virtual:
1898 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001899
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001900 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001901 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001902
Chris Lattner1ef08762007-08-09 17:01:07 +00001903 // GNU typeof support.
1904 case tok::kw_typeof:
1905
1906 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001907 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00001908 return true;
Chris Lattnerf3948c42008-07-26 03:38:44 +00001909
1910 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1911 case tok::less:
1912 return getLang().ObjC1;
Steve Naroff239f0732008-12-25 14:16:32 +00001913
Steve Naroff47f52092009-01-06 19:34:12 +00001914 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00001915 case tok::kw___cdecl:
1916 case tok::kw___stdcall:
1917 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001918 case tok::kw___w64:
1919 case tok::kw___ptr64:
1920 case tok::kw___forceinline:
1921 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001922 }
1923}
1924
1925
1926/// ParseTypeQualifierListOpt
1927/// type-qualifier-list: [C99 6.7.5]
1928/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001929/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001930/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001931/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001932///
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001933void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001934 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001935 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001936 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001937 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001938 SourceLocation Loc = Tok.getLocation();
1939
1940 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001941 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001942 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
1943 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001944 break;
1945 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001946 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1947 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001948 break;
1949 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001950 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1951 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001952 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001953 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001954 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00001955 case tok::kw___cdecl:
1956 case tok::kw___stdcall:
1957 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001958 if (AttributesAllowed) {
1959 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1960 continue;
1961 }
1962 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00001963 case tok::kw___attribute:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001964 if (AttributesAllowed) {
1965 DS.AddAttributes(ParseAttributes());
1966 continue; // do *not* consume the next token!
1967 }
1968 // otherwise, FALL THROUGH!
1969 default:
Steve Naroff239f0732008-12-25 14:16:32 +00001970 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001971 // If this is not a type-qualifier token, we're done reading type
1972 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001973 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001974 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001975 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00001976
Reid Spencer5f016e22007-07-11 17:01:13 +00001977 // If the specifier combination wasn't legal, issue a diagnostic.
1978 if (isInvalid) {
1979 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001980 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001981 }
1982 ConsumeToken();
1983 }
1984}
1985
1986
1987/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1988///
1989void Parser::ParseDeclarator(Declarator &D) {
1990 /// This implements the 'declarator' production in the C grammar, then checks
1991 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001992 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00001993}
1994
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001995/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1996/// is parsed by the function passed to it. Pass null, and the direct-declarator
1997/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001998/// ptr-operator production.
1999///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002000/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2001/// [C] pointer[opt] direct-declarator
2002/// [C++] direct-declarator
2003/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002004///
2005/// pointer: [C99 6.7.5]
2006/// '*' type-qualifier-list[opt]
2007/// '*' type-qualifier-list[opt] pointer
2008///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002009/// ptr-operator:
2010/// '*' cv-qualifier-seq[opt]
2011/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002012/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002013/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002014/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002015/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002016void Parser::ParseDeclaratorInternal(Declarator &D,
2017 DirectDeclParseFunction DirectDeclParser) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002018
Sebastian Redlf30208a2009-01-24 21:16:55 +00002019 // C++ member pointers start with a '::' or a nested-name.
2020 // Member pointers get special handling, since there's no place for the
2021 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002022 if (getLang().CPlusPlus &&
2023 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2024 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002025 CXXScopeSpec SS;
2026 if (ParseOptionalCXXScopeSpecifier(SS)) {
2027 if(Tok.isNot(tok::star)) {
2028 // The scope spec really belongs to the direct-declarator.
2029 D.getCXXScopeSpec() = SS;
2030 if (DirectDeclParser)
2031 (this->*DirectDeclParser)(D);
2032 return;
2033 }
2034
2035 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002036 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002037 DeclSpec DS;
2038 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002039 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002040
2041 // Recurse to parse whatever is left.
2042 ParseDeclaratorInternal(D, DirectDeclParser);
2043
2044 // Sema will have to catch (syntactically invalid) pointers into global
2045 // scope. It has to catch pointers into namespace scope anyway.
2046 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002047 Loc, DS.TakeAttributes()),
2048 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002049 return;
2050 }
2051 }
2052
2053 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002054 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002055 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002056 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002057 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002058 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002059 if (DirectDeclParser)
2060 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002061 return;
2062 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002063
Sebastian Redl05532f22009-03-15 22:02:01 +00002064 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2065 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002066 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002067 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002068
Chris Lattner9af55002009-03-27 04:18:06 +00002069 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002070 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002071 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002072
Reid Spencer5f016e22007-07-11 17:01:13 +00002073 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002074 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002075
Reid Spencer5f016e22007-07-11 17:01:13 +00002076 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002077 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002078 if (Kind == tok::star)
2079 // Remember that we parsed a pointer type, and remember the type-quals.
2080 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002081 DS.TakeAttributes()),
2082 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002083 else
2084 // Remember that we parsed a Block type, and remember the type-quals.
2085 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002086 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002087 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002088 } else {
2089 // Is a reference
2090 DeclSpec DS;
2091
Sebastian Redl743de1f2009-03-23 00:00:23 +00002092 // Complain about rvalue references in C++03, but then go on and build
2093 // the declarator.
2094 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2095 Diag(Loc, diag::err_rvalue_reference);
2096
Reid Spencer5f016e22007-07-11 17:01:13 +00002097 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2098 // cv-qualifiers are introduced through the use of a typedef or of a
2099 // template type argument, in which case the cv-qualifiers are ignored.
2100 //
2101 // [GNU] Retricted references are allowed.
2102 // [GNU] Attributes on references are allowed.
2103 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002104 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002105
2106 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2107 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2108 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002109 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002110 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2111 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002112 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002113 }
2114
2115 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002116 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002117
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002118 if (D.getNumTypeObjects() > 0) {
2119 // C++ [dcl.ref]p4: There shall be no references to references.
2120 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2121 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002122 if (const IdentifierInfo *II = D.getIdentifier())
2123 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2124 << II;
2125 else
2126 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2127 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002128
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002129 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002130 // can go ahead and build the (technically ill-formed)
2131 // declarator: reference collapsing will take care of it.
2132 }
2133 }
2134
Reid Spencer5f016e22007-07-11 17:01:13 +00002135 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002136 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002137 DS.TakeAttributes(),
2138 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002139 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002140 }
2141}
2142
2143/// ParseDirectDeclarator
2144/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002145/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002146/// '(' declarator ')'
2147/// [GNU] '(' attributes declarator ')'
2148/// [C90] direct-declarator '[' constant-expression[opt] ']'
2149/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2150/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2151/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2152/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2153/// direct-declarator '(' parameter-type-list ')'
2154/// direct-declarator '(' identifier-list[opt] ')'
2155/// [GNU] direct-declarator '(' parameter-forward-declarations
2156/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002157/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2158/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002159/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002160///
2161/// declarator-id: [C++ 8]
2162/// id-expression
2163/// '::'[opt] nested-name-specifier[opt] type-name
2164///
2165/// id-expression: [C++ 5.1]
2166/// unqualified-id
2167/// qualified-id [TODO]
2168///
2169/// unqualified-id: [C++ 5.1]
2170/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002171/// operator-function-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002172/// conversion-function-id [TODO]
2173/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002174/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002175///
Reid Spencer5f016e22007-07-11 17:01:13 +00002176void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002177 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002178
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002179 if (getLang().CPlusPlus) {
2180 if (D.mayHaveIdentifier()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002181 // ParseDeclaratorInternal might already have parsed the scope.
2182 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2183 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002184 if (afterCXXScope) {
2185 // Change the declaration context for name lookup, until this function
2186 // is exited (and the declarator has been parsed).
2187 DeclScopeObj.EnterDeclaratorScope();
2188 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002189
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002190 if (Tok.is(tok::identifier)) {
2191 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Anders Carlsson4649cac2009-04-30 22:41:11 +00002192
2193 // If this identifier is the name of the current class, it's a
2194 // constructor name.
2195 if (!D.getDeclSpec().hasTypeSpecifier() &&
2196 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
Douglas Gregor675431d2009-07-06 16:40:48 +00002197 CXXScopeSpec *SS = afterCXXScope? &D.getCXXScopeSpec() : 0;
Anders Carlsson4649cac2009-04-30 22:41:11 +00002198 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor675431d2009-07-06 16:40:48 +00002199 Tok.getLocation(), CurScope, SS),
Anders Carlsson4649cac2009-04-30 22:41:11 +00002200 Tok.getLocation());
2201 // This is a normal identifier.
2202 } else
2203 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002204 ConsumeToken();
2205 goto PastIdentifier;
Douglas Gregor39a8de12009-02-25 19:37:18 +00002206 } else if (Tok.is(tok::annot_template_id)) {
2207 TemplateIdAnnotation *TemplateId
2208 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
2209
2210 // FIXME: Could this template-id name a constructor?
2211
2212 // FIXME: This is an egregious hack, where we silently ignore
2213 // the specialization (which should be a function template
2214 // specialization name) and use the name instead. This hack
2215 // will go away when we have support for function
2216 // specializations.
2217 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
2218 TemplateId->Destroy();
2219 ConsumeToken();
2220 goto PastIdentifier;
Douglas Gregor70316a02008-12-26 15:00:45 +00002221 } else if (Tok.is(tok::kw_operator)) {
2222 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002223 SourceLocation EndLoc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002224
Douglas Gregor70316a02008-12-26 15:00:45 +00002225 // First try the name of an overloaded operator
Sebastian Redlab197ba2009-02-09 18:23:29 +00002226 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
2227 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor70316a02008-12-26 15:00:45 +00002228 } else {
2229 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redlab197ba2009-02-09 18:23:29 +00002230 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
2231 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
2232 else {
Douglas Gregor70316a02008-12-26 15:00:45 +00002233 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002234 }
Douglas Gregor70316a02008-12-26 15:00:45 +00002235 }
2236 goto PastIdentifier;
2237 } else if (Tok.is(tok::tilde)) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002238 // This should be a C++ destructor.
2239 SourceLocation TildeLoc = ConsumeToken();
2240 if (Tok.is(tok::identifier)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002241 // FIXME: Inaccurate.
2242 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7f43d672009-02-25 23:52:28 +00002243 SourceLocation EndLoc;
Douglas Gregor675431d2009-07-06 16:40:48 +00002244 CXXScopeSpec *SS = afterCXXScope? &D.getCXXScopeSpec() : 0;
Fariborz Jahaniand33c8682009-07-20 17:43:15 +00002245 TypeResult Type = ParseClassName(EndLoc, SS, true);
Douglas Gregor31a19b62009-04-01 21:51:26 +00002246 if (Type.isInvalid())
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002247 D.SetIdentifier(0, TildeLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00002248 else
2249 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002250 } else {
Fariborz Jahaniand33c8682009-07-20 17:43:15 +00002251 Diag(Tok, diag::err_destructor_class_name);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002252 D.SetIdentifier(0, TildeLoc);
2253 }
2254 goto PastIdentifier;
2255 }
2256
2257 // If we reached this point, token is not identifier and not '~'.
2258
2259 if (afterCXXScope) {
2260 Diag(Tok, diag::err_expected_unqualified_id);
2261 D.SetIdentifier(0, Tok.getLocation());
2262 D.setInvalidType(true);
2263 goto PastIdentifier;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002264 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002265 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002266 }
2267
2268 // If we reached this point, we are either in C/ObjC or the token didn't
2269 // satisfy any of the C++-specific checks.
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002270 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2271 assert(!getLang().CPlusPlus &&
2272 "There's a C++-specific check for tok::identifier above");
2273 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2274 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2275 ConsumeToken();
2276 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002277 // direct-declarator: '(' declarator ')'
2278 // direct-declarator: '(' attributes declarator ')'
2279 // Example: 'char (*X)' or 'int (*XX)(void)'
2280 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002281 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002282 // This could be something simple like "int" (in which case the declarator
2283 // portion is empty), if an abstract-declarator is allowed.
2284 D.SetIdentifier(0, Tok.getLocation());
2285 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002286 if (D.getContext() == Declarator::MemberContext)
2287 Diag(Tok, diag::err_expected_member_name_or_semi)
2288 << D.getDeclSpec().getSourceRange();
2289 else if (getLang().CPlusPlus)
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002290 Diag(Tok, diag::err_expected_unqualified_id);
2291 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002292 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002293 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002294 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002295 }
2296
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002297 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002298 assert(D.isPastIdentifier() &&
2299 "Haven't past the location of the identifier yet?");
2300
2301 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002302 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002303 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2304 // In such a case, check if we actually have a function declarator; if it
2305 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002306 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2307 // When not in file scope, warn for ambiguous function declarators, just
2308 // in case the author intended it as a variable definition.
2309 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2310 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2311 break;
2312 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002313 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002314 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002315 ParseBracketDeclarator(D);
2316 } else {
2317 break;
2318 }
2319 }
2320}
2321
Chris Lattneref4715c2008-04-06 05:45:57 +00002322/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2323/// only called before the identifier, so these are most likely just grouping
2324/// parens for precedence. If we find that these are actually function
2325/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2326///
2327/// direct-declarator:
2328/// '(' declarator ')'
2329/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002330/// direct-declarator '(' parameter-type-list ')'
2331/// direct-declarator '(' identifier-list[opt] ')'
2332/// [GNU] direct-declarator '(' parameter-forward-declarations
2333/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002334///
2335void Parser::ParseParenDeclarator(Declarator &D) {
2336 SourceLocation StartLoc = ConsumeParen();
2337 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
2338
Chris Lattner7399ee02008-10-20 02:05:46 +00002339 // Eat any attributes before we look at whether this is a grouping or function
2340 // declarator paren. If this is a grouping paren, the attribute applies to
2341 // the type being built up, for example:
2342 // int (__attribute__(()) *x)(long y)
2343 // If this ends up not being a grouping paren, the attribute applies to the
2344 // first argument, for example:
2345 // int (__attribute__(()) int x)
2346 // In either case, we need to eat any attributes to be able to determine what
2347 // sort of paren this is.
2348 //
2349 AttributeList *AttrList = 0;
2350 bool RequiresArg = false;
2351 if (Tok.is(tok::kw___attribute)) {
2352 AttrList = ParseAttributes();
2353
2354 // We require that the argument list (if this is a non-grouping paren) be
2355 // present even if the attribute list was empty.
2356 RequiresArg = true;
2357 }
Steve Naroff239f0732008-12-25 14:16:32 +00002358 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002359 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2360 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2361 Tok.is(tok::kw___ptr64)) {
2362 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2363 }
Chris Lattner7399ee02008-10-20 02:05:46 +00002364
Chris Lattneref4715c2008-04-06 05:45:57 +00002365 // If we haven't past the identifier yet (or where the identifier would be
2366 // stored, if this is an abstract declarator), then this is probably just
2367 // grouping parens. However, if this could be an abstract-declarator, then
2368 // this could also be the start of function arguments (consider 'void()').
2369 bool isGrouping;
2370
2371 if (!D.mayOmitIdentifier()) {
2372 // If this can't be an abstract-declarator, this *must* be a grouping
2373 // paren, because we haven't seen the identifier yet.
2374 isGrouping = true;
2375 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002376 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002377 isDeclarationSpecifier()) { // 'int(int)' is a function.
2378 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2379 // considered to be a type, not a K&R identifier-list.
2380 isGrouping = false;
2381 } else {
2382 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2383 isGrouping = true;
2384 }
2385
2386 // If this is a grouping paren, handle:
2387 // direct-declarator: '(' declarator ')'
2388 // direct-declarator: '(' attributes declarator ')'
2389 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002390 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002391 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002392 if (AttrList)
Sebastian Redlab197ba2009-02-09 18:23:29 +00002393 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002394
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002395 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002396 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002397 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002398
2399 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002400 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002401 return;
2402 }
2403
2404 // Okay, if this wasn't a grouping paren, it must be the start of a function
2405 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002406 // identifier (and remember where it would have been), then call into
2407 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002408 D.SetIdentifier(0, Tok.getLocation());
2409
Chris Lattner7399ee02008-10-20 02:05:46 +00002410 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002411}
2412
2413/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2414/// declarator D up to a paren, which indicates that we are parsing function
2415/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002416///
Chris Lattner7399ee02008-10-20 02:05:46 +00002417/// If AttrList is non-null, then the caller parsed those arguments immediately
2418/// after the open paren - they should be considered to be the first argument of
2419/// a parameter. If RequiresArg is true, then the first argument of the
2420/// function is required to be present and required to not be an identifier
2421/// list.
2422///
Reid Spencer5f016e22007-07-11 17:01:13 +00002423/// This method also handles this portion of the grammar:
2424/// parameter-type-list: [C99 6.7.5]
2425/// parameter-list
2426/// parameter-list ',' '...'
2427///
2428/// parameter-list: [C99 6.7.5]
2429/// parameter-declaration
2430/// parameter-list ',' parameter-declaration
2431///
2432/// parameter-declaration: [C99 6.7.5]
2433/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002434/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002435/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002436/// declaration-specifiers abstract-declarator[opt]
2437/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002438/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002439/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2440///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002441/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002442/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002443///
Chris Lattner7399ee02008-10-20 02:05:46 +00002444void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2445 AttributeList *AttrList,
2446 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002447 // lparen is already consumed!
2448 assert(D.isPastIdentifier() && "Should not call before identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00002449
Chris Lattner7399ee02008-10-20 02:05:46 +00002450 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002451 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002452 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002453 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002454 delete AttrList;
2455 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002456
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002457 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2458 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002459
2460 // cv-qualifier-seq[opt].
2461 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002462 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002463 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002464 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002465 llvm::SmallVector<TypeTy*, 2> Exceptions;
2466 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002467 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002468 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002469 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002470 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002471
2472 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002473 if (Tok.is(tok::kw_throw)) {
2474 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002475 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002476 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002477 hasAnyExceptionSpec);
2478 assert(Exceptions.size() == ExceptionRanges.size() &&
2479 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002480 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002481 }
2482
Chris Lattnerf97409f2008-04-06 06:57:35 +00002483 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002484 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002485 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002486 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002487 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002488 /*arglist*/ 0, 0,
2489 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002490 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002491 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002492 Exceptions.data(),
2493 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002494 Exceptions.size(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002495 LParenLoc, RParenLoc, D),
2496 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002497 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00002498 }
2499
Chris Lattner7399ee02008-10-20 02:05:46 +00002500 // Alternatively, this parameter list may be an identifier list form for a
2501 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00002502 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00002503 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002504 // K&R identifier lists can't have typedefs as identifiers, per
2505 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002506 if (RequiresArg) {
2507 Diag(Tok, diag::err_argument_required_after_attribute);
2508 delete AttrList;
2509 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002510 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2511 // normal declarators, not for abstract-declarators.
2512 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002513 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002514 }
2515
2516 // Finally, a normal, non-empty parameter type list.
2517
2518 // Build up an array of information about the parsed arguments.
2519 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002520
2521 // Enter function-declaration scope, limiting any declarators to the
2522 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002523 ParseScope PrototypeScope(this,
2524 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002525
2526 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002527 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002528 while (1) {
2529 if (Tok.is(tok::ellipsis)) {
2530 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002531 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002532 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002533 }
2534
Chris Lattnerf97409f2008-04-06 06:57:35 +00002535 SourceLocation DSStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00002536
Chris Lattnerf97409f2008-04-06 06:57:35 +00002537 // Parse the declaration-specifiers.
2538 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002539
2540 // If the caller parsed attributes for the first argument, add them now.
2541 if (AttrList) {
2542 DS.AddAttributes(AttrList);
2543 AttrList = 0; // Only apply the attributes to the first parameter.
2544 }
Chris Lattnere64c5492009-02-27 18:38:20 +00002545 ParseDeclarationSpecifiers(DS);
2546
Chris Lattnerf97409f2008-04-06 06:57:35 +00002547 // Parse the declarator. This is "PrototypeContext", because we must
2548 // accept either 'declarator' or 'abstract-declarator' here.
2549 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2550 ParseDeclarator(ParmDecl);
2551
2552 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002553 if (Tok.is(tok::kw___attribute)) {
2554 SourceLocation Loc;
2555 AttributeList *AttrList = ParseAttributes(&Loc);
2556 ParmDecl.AddAttributes(AttrList, Loc);
2557 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002558
Chris Lattnerf97409f2008-04-06 06:57:35 +00002559 // Remember this parsed parameter in ParamInfo.
2560 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2561
Douglas Gregor72b505b2008-12-16 21:30:33 +00002562 // DefArgToks is used when the parsing of default arguments needs
2563 // to be delayed.
2564 CachedTokens *DefArgToks = 0;
2565
Chris Lattnerf97409f2008-04-06 06:57:35 +00002566 // If no parameter was specified, verify that *something* was specified,
2567 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00002568 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2569 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002570 // Completely missing, emit error.
2571 Diag(DSStart, diag::err_missing_param);
2572 } else {
2573 // Otherwise, we have something. Add it and let semantic analysis try
2574 // to grok it and add the result to the ParamInfo we are building.
2575
2576 // Inform the actions module about the parameter declarator, so it gets
2577 // added to the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002578 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00002579
2580 // Parse the default argument, if any. We parse the default
2581 // arguments in all dialects; the semantic analysis in
2582 // ActOnParamDefaultArgument will reject the default argument in
2583 // C.
2584 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002585 SourceLocation EqualLoc = Tok.getLocation();
2586
Chris Lattner04421082008-04-08 04:40:51 +00002587 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002588 if (D.getContext() == Declarator::MemberContext) {
2589 // If we're inside a class definition, cache the tokens
2590 // corresponding to the default argument. We'll actually parse
2591 // them when we see the end of the class definition.
2592 // FIXME: Templates will require something similar.
2593 // FIXME: Can we use a smart pointer for Toks?
2594 DefArgToks = new CachedTokens;
2595
2596 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2597 tok::semi, false)) {
2598 delete DefArgToks;
2599 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002600 Actions.ActOnParamDefaultArgumentError(Param);
2601 } else
Anders Carlsson5e300d12009-06-12 16:51:40 +00002602 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
2603 (*DefArgToks)[1].getLocation());
Chris Lattner04421082008-04-08 04:40:51 +00002604 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002605 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002606 ConsumeToken();
Douglas Gregor72b505b2008-12-16 21:30:33 +00002607
2608 OwningExprResult DefArgResult(ParseAssignmentExpression());
2609 if (DefArgResult.isInvalid()) {
2610 Actions.ActOnParamDefaultArgumentError(Param);
2611 SkipUntil(tok::comma, tok::r_paren, true, true);
2612 } else {
2613 // Inform the actions module about the default argument
2614 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002615 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00002616 }
Chris Lattner04421082008-04-08 04:40:51 +00002617 }
2618 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002619
2620 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002621 ParmDecl.getIdentifierLoc(), Param,
2622 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002623 }
2624
2625 // If the next token is a comma, consume it and keep reading arguments.
2626 if (Tok.isNot(tok::comma)) break;
2627
2628 // Consume the comma.
2629 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002630 }
2631
Chris Lattnerf97409f2008-04-06 06:57:35 +00002632 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002633 PrototypeScope.Exit();
Chris Lattnerf97409f2008-04-06 06:57:35 +00002634
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002635 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002636 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2637 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002638
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002639 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002640 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002641 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002642 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002643 llvm::SmallVector<TypeTy*, 2> Exceptions;
2644 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002645 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002646 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002647 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002648 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002649 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002650
2651 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002652 if (Tok.is(tok::kw_throw)) {
2653 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002654 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002655 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002656 hasAnyExceptionSpec);
2657 assert(Exceptions.size() == ExceptionRanges.size() &&
2658 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002659 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002660 }
2661
Reid Spencer5f016e22007-07-11 17:01:13 +00002662 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002663 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002664 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00002665 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002666 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002667 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002668 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002669 Exceptions.data(),
2670 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002671 Exceptions.size(),
2672 LParenLoc, RParenLoc, D),
2673 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002674}
2675
Chris Lattner66d28652008-04-06 06:34:08 +00002676/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2677/// we found a K&R-style identifier list instead of a type argument list. The
2678/// current token is known to be the first identifier in the list.
2679///
2680/// identifier-list: [C99 6.7.5]
2681/// identifier
2682/// identifier-list ',' identifier
2683///
2684void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2685 Declarator &D) {
2686 // Build up an array of information about the parsed arguments.
2687 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2688 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2689
2690 // If there was no identifier specified for the declarator, either we are in
2691 // an abstract-declarator, or we are in a parameter declarator which was found
2692 // to be abstract. In abstract-declarators, identifier lists are not valid:
2693 // diagnose this.
2694 if (!D.getIdentifier())
2695 Diag(Tok, diag::ext_ident_list_in_param);
2696
2697 // Tok is known to be the first identifier in the list. Remember this
2698 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002699 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002700 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattnerb28317a2009-03-28 19:18:32 +00002701 Tok.getLocation(),
2702 DeclPtrTy()));
Chris Lattner66d28652008-04-06 06:34:08 +00002703
Chris Lattner50c64772008-04-06 06:39:19 +00002704 ConsumeToken(); // eat the first identifier.
Chris Lattner66d28652008-04-06 06:34:08 +00002705
2706 while (Tok.is(tok::comma)) {
2707 // Eat the comma.
2708 ConsumeToken();
2709
Chris Lattner50c64772008-04-06 06:39:19 +00002710 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002711 if (Tok.isNot(tok::identifier)) {
2712 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002713 SkipUntil(tok::r_paren);
2714 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002715 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002716
Chris Lattner66d28652008-04-06 06:34:08 +00002717 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002718
2719 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00002720 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002721 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner66d28652008-04-06 06:34:08 +00002722
2723 // Verify that the argument identifier has not already been mentioned.
2724 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002725 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002726 } else {
2727 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002728 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002729 Tok.getLocation(),
2730 DeclPtrTy()));
Chris Lattner50c64772008-04-06 06:39:19 +00002731 }
Chris Lattner66d28652008-04-06 06:34:08 +00002732
2733 // Eat the identifier.
2734 ConsumeToken();
2735 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002736
2737 // If we have the closing ')', eat it and we're done.
2738 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2739
Chris Lattner50c64772008-04-06 06:39:19 +00002740 // Remember that we parsed a function type, and remember the attributes. This
2741 // function type is always a K&R style function type, which is not varargs and
2742 // has no prototype.
2743 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002744 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00002745 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002746 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00002747 /*exception*/false,
2748 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002749 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002750 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002751}
Chris Lattneref4715c2008-04-06 05:45:57 +00002752
Reid Spencer5f016e22007-07-11 17:01:13 +00002753/// [C90] direct-declarator '[' constant-expression[opt] ']'
2754/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2755/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2756/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2757/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2758void Parser::ParseBracketDeclarator(Declarator &D) {
2759 SourceLocation StartLoc = ConsumeBracket();
2760
Chris Lattner378c7e42008-12-18 07:27:21 +00002761 // C array syntax has many features, but by-far the most common is [] and [4].
2762 // This code does a fast path to handle some of the most obvious cases.
2763 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002764 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002765 // Remember that we parsed the empty array type.
2766 OwningExprResult NumElements(Actions);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002767 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
2768 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002769 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002770 return;
2771 } else if (Tok.getKind() == tok::numeric_constant &&
2772 GetLookAheadToken(1).is(tok::r_square)) {
2773 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002774 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002775 ConsumeToken();
2776
Sebastian Redlab197ba2009-02-09 18:23:29 +00002777 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002778
2779 // If there was an error parsing the assignment-expression, recover.
2780 if (ExprRes.isInvalid())
2781 ExprRes.release(); // Deallocate expr, just use [].
2782
2783 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002784 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
2785 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002786 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002787 return;
2788 }
2789
Reid Spencer5f016e22007-07-11 17:01:13 +00002790 // If valid, this location is the position where we read the 'static' keyword.
2791 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002792 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002793 StaticLoc = ConsumeToken();
2794
2795 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002796 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002797 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002798 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Reid Spencer5f016e22007-07-11 17:01:13 +00002799
2800 // If we haven't already read 'static', check to see if there is one after the
2801 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002802 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002803 StaticLoc = ConsumeToken();
2804
2805 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2806 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002807 OwningExprResult NumElements(Actions);
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002808
2809 // Handle the case where we have '[*]' as the array size. However, a leading
2810 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2811 // the the token after the star is a ']'. Since stars in arrays are
2812 // infrequent, use of lookahead is not costly here.
2813 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002814 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002815
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002816 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002817 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002818 StaticLoc = SourceLocation(); // Drop the static.
2819 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002820 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002821 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002822 // Note, in C89, this production uses the constant-expr production instead
2823 // of assignment-expr. The only difference is that assignment-expr allows
2824 // things like '=' and '*='. Sema rejects these in C89 mode because they
2825 // are not i-c-e's, so we don't need to distinguish between the two here.
2826
Douglas Gregore0762c92009-06-19 23:52:42 +00002827 // Parse the constant-expression or assignment-expression now (depending
2828 // on dialect).
2829 if (getLang().CPlusPlus)
2830 NumElements = ParseConstantExpression();
2831 else
2832 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00002833 }
2834
2835 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002836 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00002837 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002838 // If the expression was invalid, skip it.
2839 SkipUntil(tok::r_square);
2840 return;
2841 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002842
2843 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2844
Chris Lattner378c7e42008-12-18 07:27:21 +00002845 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00002846 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2847 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002848 NumElements.release(),
2849 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002850 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002851}
2852
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002853/// [GNU] typeof-specifier:
2854/// typeof ( expressions )
2855/// typeof ( type-name )
2856/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00002857///
2858void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00002859 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002860 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002861 SourceLocation StartLoc = ConsumeToken();
2862
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002863 bool isCastExpr;
2864 TypeTy *CastTy;
2865 SourceRange CastRange;
2866 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
2867 isCastExpr,
2868 CastTy,
2869 CastRange);
2870
2871 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002872 // FIXME: Not accurate, the range gets one token more than it should.
2873 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002874 else
2875 DS.SetRangeEnd(CastRange.getEnd());
2876
2877 if (isCastExpr) {
2878 if (!CastTy) {
2879 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002880 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00002881 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002882
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002883 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002884 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002885 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2886 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002887 DiagID, CastTy))
2888 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002889 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002890 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002891
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002892 // If we get here, the operand to the typeof was an expresion.
2893 if (Operand.isInvalid()) {
2894 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002895 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002896 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002897
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002898 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002899 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002900 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2901 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002902 DiagID, Operand.release()))
2903 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002904}