blob: 914a5acd09c130cfff08518a2478d9d86a39b52e [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"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/Scope.h"
18#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000019#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/ADT/SmallSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000022#include "llvm/ADT/SmallString.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000023#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// C99 6.7: Declarations.
28//===----------------------------------------------------------------------===//
29
30/// ParseTypeName
31/// type-name: [C99 6.7.6]
32/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000033///
34/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000035TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000036 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000037 AccessSpecifier AS,
38 Decl **OwnedType) {
Reid Spencer5f016e22007-07-11 17:01:13 +000039 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000040 DeclSpec DS(AttrFactory);
Richard Smithc89edf52011-07-01 19:46:12 +000041 ParseSpecifierQualifierList(DS, AS);
42 if (OwnedType)
43 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000044
Reid Spencer5f016e22007-07-11 17:01:13 +000045 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000046 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000047 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000048 if (Range)
49 *Range = DeclaratorInfo.getSourceRange();
50
Chris Lattnereaaebc72009-04-25 08:06:05 +000051 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000052 return true;
53
Douglas Gregor23c94db2010-07-02 17:43:08 +000054 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000055}
56
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000057
58/// isAttributeLateParsed - Return true if the attribute has arguments that
59/// require late parsing.
60static bool isAttributeLateParsed(const IdentifierInfo &II) {
61 return llvm::StringSwitch<bool>(II.getName())
62#include "clang/Parse/AttrLateParsed.inc"
63 .Default(false);
64}
65
66
Sean Huntbbd37c62009-11-21 08:43:09 +000067/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000068///
69/// [GNU] attributes:
70/// attribute
71/// attributes attribute
72///
73/// [GNU] attribute:
74/// '__attribute__' '(' '(' attribute-list ')' ')'
75///
76/// [GNU] attribute-list:
77/// attrib
78/// attribute_list ',' attrib
79///
80/// [GNU] attrib:
81/// empty
82/// attrib-name
83/// attrib-name '(' identifier ')'
84/// attrib-name '(' identifier ',' nonempty-expr-list ')'
85/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
86///
87/// [GNU] attrib-name:
88/// identifier
89/// typespec
90/// typequal
91/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000092///
Reid Spencer5f016e22007-07-11 17:01:13 +000093/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000094/// token lookahead. Comment from gcc: "If they start with an identifier
95/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000096/// start with that identifier; otherwise they are an expression list."
97///
Richard Smithfe0a0fb2011-10-17 21:20:17 +000098/// GCC does not require the ',' between attribs in an attribute-list.
99///
Reid Spencer5f016e22007-07-11 17:01:13 +0000100/// At the moment, I am not doing 2 token lookahead. I am also unaware of
101/// any attributes that don't work (based on my limited testing). Most
102/// attributes are very simple in practice. Until we find a bug, I don't see
103/// a pressing need to implement the 2 token lookahead.
104
John McCall7f040a92010-12-24 02:08:15 +0000105void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000106 SourceLocation *endLoc,
107 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000108 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Chris Lattner04d66662007-10-09 17:33:22 +0000110 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 ConsumeToken();
112 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
113 "attribute")) {
114 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000115 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 }
117 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
118 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000119 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 }
121 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000122 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
123 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000124 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
126 ConsumeToken();
127 continue;
128 }
129 // we have an identifier or declaration specifier (const, int, etc.)
130 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
131 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000133 if (Tok.is(tok::l_paren)) {
134 // handle "parameterized" attributes
135 if (LateAttrs && !ClassStack.empty() &&
136 isAttributeLateParsed(*AttrName)) {
137 // Delayed parsing is only available for attributes that occur
138 // in certain locations within a class scope.
139 LateParsedAttribute *LA =
140 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
141 LateAttrs->push_back(LA);
142 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000144 // consume everything up to and including the matching right parens
145 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000147 Token Eof;
148 Eof.startToken();
149 Eof.setLocation(Tok.getLocation());
150 LA->Toks.push_back(Eof);
151 } else {
152 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 }
154 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000155 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
156 0, SourceLocation(), 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 }
158 }
159 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000161 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000162 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
163 SkipUntil(tok::r_paren, false);
164 }
John McCall7f040a92010-12-24 02:08:15 +0000165 if (endLoc)
166 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000168}
169
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000170
171/// Parse the arguments to a parameterized GNU attribute
172void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
173 SourceLocation AttrNameLoc,
174 ParsedAttributes &Attrs,
175 SourceLocation *EndLoc) {
176
177 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
178
179 // Availability attributes have their own grammar.
180 if (AttrName->isStr("availability")) {
181 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
182 return;
183 }
184 // Thread safety attributes fit into the FIXME case above, so we
185 // just parse the arguments as a list of expressions
186 if (IsThreadSafetyAttribute(AttrName->getName())) {
187 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
188 return;
189 }
190
191 ConsumeParen(); // ignore the left paren loc for now
192
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000193 IdentifierInfo *ParmName = 0;
194 SourceLocation ParmLoc;
195 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000196
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000197 switch (Tok.getKind()) {
198 case tok::kw_char:
199 case tok::kw_wchar_t:
200 case tok::kw_char16_t:
201 case tok::kw_char32_t:
202 case tok::kw_bool:
203 case tok::kw_short:
204 case tok::kw_int:
205 case tok::kw_long:
206 case tok::kw___int64:
207 case tok::kw_signed:
208 case tok::kw_unsigned:
209 case tok::kw_float:
210 case tok::kw_double:
211 case tok::kw_void:
212 case tok::kw_typeof:
213 // __attribute__(( vec_type_hint(char) ))
214 // FIXME: Don't just discard the builtin type token.
215 ConsumeToken();
216 BuiltinType = true;
217 break;
218
219 case tok::identifier:
220 ParmName = Tok.getIdentifierInfo();
221 ParmLoc = ConsumeToken();
222 break;
223
224 default:
225 break;
226 }
227
228 ExprVector ArgExprs(Actions);
229
230 if (!BuiltinType &&
231 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
232 // Eat the comma.
233 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000234 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000235
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000236 // Parse the non-empty comma-separated list of expressions.
237 while (1) {
238 ExprResult ArgExpr(ParseAssignmentExpression());
239 if (ArgExpr.isInvalid()) {
240 SkipUntil(tok::r_paren);
241 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000242 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000243 ArgExprs.push_back(ArgExpr.release());
244 if (Tok.isNot(tok::comma))
245 break;
246 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000247 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000248 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000249 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
250 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
251 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000252 while (Tok.is(tok::identifier)) {
253 ConsumeToken();
254 if (Tok.is(tok::greater))
255 break;
256 if (Tok.is(tok::comma)) {
257 ConsumeToken();
258 continue;
259 }
260 }
261 if (Tok.isNot(tok::greater))
262 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000263 SkipUntil(tok::r_paren, false, true); // skip until ')'
264 }
265 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000266
267 SourceLocation RParen = Tok.getLocation();
268 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
269 AttributeList *attr =
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000270 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000271 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
272 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
273 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000274 }
275}
276
277
Eli Friedmana23b4852009-06-08 07:21:15 +0000278/// ParseMicrosoftDeclSpec - Parse an __declspec construct
279///
280/// [MS] decl-specifier:
281/// __declspec ( extended-decl-modifier-seq )
282///
283/// [MS] extended-decl-modifier-seq:
284/// extended-decl-modifier[opt]
285/// extended-decl-modifier extended-decl-modifier-seq
286
John McCall7f040a92010-12-24 02:08:15 +0000287void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000288 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000289
Steve Narofff59e17e2008-12-24 20:59:21 +0000290 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000291 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
292 "declspec")) {
293 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000294 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000295 }
Francois Pichet373197b2011-05-07 19:04:49 +0000296
Eli Friedman290eeb02009-06-08 23:27:34 +0000297 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000298 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
299 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet373197b2011-05-07 19:04:49 +0000300
301 // FIXME: Remove this when we have proper __declspec(property()) support.
302 // Just skip everything inside property().
303 if (AttrName->getName() == "property") {
304 ConsumeParen();
305 SkipUntil(tok::r_paren);
306 }
Eli Friedmana23b4852009-06-08 07:21:15 +0000307 if (Tok.is(tok::l_paren)) {
308 ConsumeParen();
309 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
310 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000311 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000312 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000313 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000314 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
315 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000316 }
317 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
318 SkipUntil(tok::r_paren, false);
319 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000320 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
321 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000322 }
323 }
324 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
325 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000326 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000327}
328
John McCall7f040a92010-12-24 02:08:15 +0000329void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000330 // Treat these like attributes
331 // FIXME: Allow Sema to distinguish between these and real attributes!
332 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000333 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000334 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000335 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000336 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000337 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
338 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet58fd97a2011-08-25 00:36:46 +0000339 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
340 Tok.is(tok::kw___ptr32))
Eli Friedman290eeb02009-06-08 23:27:34 +0000341 // FIXME: Support these properly!
342 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000343 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
344 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000345 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000346}
347
John McCall7f040a92010-12-24 02:08:15 +0000348void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000349 // Treat these like attributes
350 while (Tok.is(tok::kw___pascal)) {
351 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
352 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000353 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
354 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000355 }
John McCall7f040a92010-12-24 02:08:15 +0000356}
357
Peter Collingbournef315fa82011-02-14 01:42:53 +0000358void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
359 // Treat these like attributes
360 while (Tok.is(tok::kw___kernel)) {
361 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000362 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
363 AttrNameLoc, 0, AttrNameLoc, 0,
364 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000365 }
366}
367
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000368void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
369 SourceLocation Loc = Tok.getLocation();
370 switch(Tok.getKind()) {
371 // OpenCL qualifiers:
372 case tok::kw___private:
373 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000374 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000375 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000376 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000377 break;
378
379 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000380 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000381 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000382 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000383 break;
384
385 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000386 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000387 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000388 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000389 break;
390
391 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000392 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000393 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000394 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000395 break;
396
397 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000398 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000399 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000400 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000401 break;
402
403 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000404 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000405 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000406 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000407 break;
408
409 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000410 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000411 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000412 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000413 break;
414 default: break;
415 }
416}
417
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000418/// \brief Parse a version number.
419///
420/// version:
421/// simple-integer
422/// simple-integer ',' simple-integer
423/// simple-integer ',' simple-integer ',' simple-integer
424VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
425 Range = Tok.getLocation();
426
427 if (!Tok.is(tok::numeric_constant)) {
428 Diag(Tok, diag::err_expected_version);
429 SkipUntil(tok::comma, tok::r_paren, true, true, true);
430 return VersionTuple();
431 }
432
433 // Parse the major (and possibly minor and subminor) versions, which
434 // are stored in the numeric constant. We utilize a quirk of the
435 // lexer, which is that it handles something like 1.2.3 as a single
436 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000437 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000438 Buffer.resize(Tok.getLength()+1);
439 const char *ThisTokBegin = &Buffer[0];
440
441 // Get the spelling of the token, which eliminates trigraphs, etc.
442 bool Invalid = false;
443 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
444 if (Invalid)
445 return VersionTuple();
446
447 // Parse the major version.
448 unsigned AfterMajor = 0;
449 unsigned Major = 0;
450 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
451 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
452 ++AfterMajor;
453 }
454
455 if (AfterMajor == 0) {
456 Diag(Tok, diag::err_expected_version);
457 SkipUntil(tok::comma, tok::r_paren, true, true, true);
458 return VersionTuple();
459 }
460
461 if (AfterMajor == ActualLength) {
462 ConsumeToken();
463
464 // We only had a single version component.
465 if (Major == 0) {
466 Diag(Tok, diag::err_zero_version);
467 return VersionTuple();
468 }
469
470 return VersionTuple(Major);
471 }
472
473 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
474 Diag(Tok, diag::err_expected_version);
475 SkipUntil(tok::comma, tok::r_paren, true, true, true);
476 return VersionTuple();
477 }
478
479 // Parse the minor version.
480 unsigned AfterMinor = AfterMajor + 1;
481 unsigned Minor = 0;
482 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
483 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
484 ++AfterMinor;
485 }
486
487 if (AfterMinor == ActualLength) {
488 ConsumeToken();
489
490 // We had major.minor.
491 if (Major == 0 && Minor == 0) {
492 Diag(Tok, diag::err_zero_version);
493 return VersionTuple();
494 }
495
496 return VersionTuple(Major, Minor);
497 }
498
499 // If what follows is not a '.', we have a problem.
500 if (ThisTokBegin[AfterMinor] != '.') {
501 Diag(Tok, diag::err_expected_version);
502 SkipUntil(tok::comma, tok::r_paren, true, true, true);
503 return VersionTuple();
504 }
505
506 // Parse the subminor version.
507 unsigned AfterSubminor = AfterMinor + 1;
508 unsigned Subminor = 0;
509 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
510 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
511 ++AfterSubminor;
512 }
513
514 if (AfterSubminor != ActualLength) {
515 Diag(Tok, diag::err_expected_version);
516 SkipUntil(tok::comma, tok::r_paren, true, true, true);
517 return VersionTuple();
518 }
519 ConsumeToken();
520 return VersionTuple(Major, Minor, Subminor);
521}
522
523/// \brief Parse the contents of the "availability" attribute.
524///
525/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000526/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000527///
528/// platform:
529/// identifier
530///
531/// version-arg-list:
532/// version-arg
533/// version-arg ',' version-arg-list
534///
535/// version-arg:
536/// 'introduced' '=' version
537/// 'deprecated' '=' version
538/// 'removed' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000539/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000540/// opt-message:
541/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000542void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
543 SourceLocation AvailabilityLoc,
544 ParsedAttributes &attrs,
545 SourceLocation *endLoc) {
546 SourceLocation PlatformLoc;
547 IdentifierInfo *Platform = 0;
548
549 enum { Introduced, Deprecated, Obsoleted, Unknown };
550 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000551 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000552
553 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000554 BalancedDelimiterTracker T(*this, tok::l_paren);
555 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000556 Diag(Tok, diag::err_expected_lparen);
557 return;
558 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000559
560 // Parse the platform name,
561 if (Tok.isNot(tok::identifier)) {
562 Diag(Tok, diag::err_availability_expected_platform);
563 SkipUntil(tok::r_paren);
564 return;
565 }
566 Platform = Tok.getIdentifierInfo();
567 PlatformLoc = ConsumeToken();
568
569 // Parse the ',' following the platform name.
570 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
571 return;
572
573 // If we haven't grabbed the pointers for the identifiers
574 // "introduced", "deprecated", and "obsoleted", do so now.
575 if (!Ident_introduced) {
576 Ident_introduced = PP.getIdentifierInfo("introduced");
577 Ident_deprecated = PP.getIdentifierInfo("deprecated");
578 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000579 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000580 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000581 }
582
583 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000584 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000585 do {
586 if (Tok.isNot(tok::identifier)) {
587 Diag(Tok, diag::err_availability_expected_change);
588 SkipUntil(tok::r_paren);
589 return;
590 }
591 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
592 SourceLocation KeywordLoc = ConsumeToken();
593
Douglas Gregorb53e4172011-03-26 03:35:55 +0000594 if (Keyword == Ident_unavailable) {
595 if (UnavailableLoc.isValid()) {
596 Diag(KeywordLoc, diag::err_availability_redundant)
597 << Keyword << SourceRange(UnavailableLoc);
598 }
599 UnavailableLoc = KeywordLoc;
600
601 if (Tok.isNot(tok::comma))
602 break;
603
604 ConsumeToken();
605 continue;
606 }
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000607
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000608 if (Tok.isNot(tok::equal)) {
609 Diag(Tok, diag::err_expected_equal_after)
610 << Keyword;
611 SkipUntil(tok::r_paren);
612 return;
613 }
614 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000615 if (Keyword == Ident_message) {
616 if (!isTokenStringLiteral()) {
617 Diag(Tok, diag::err_expected_string_literal);
618 SkipUntil(tok::r_paren);
619 return;
620 }
621 MessageExpr = ParseStringLiteralExpression();
622 break;
623 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000624
625 SourceRange VersionRange;
626 VersionTuple Version = ParseVersionTuple(VersionRange);
627
628 if (Version.empty()) {
629 SkipUntil(tok::r_paren);
630 return;
631 }
632
633 unsigned Index;
634 if (Keyword == Ident_introduced)
635 Index = Introduced;
636 else if (Keyword == Ident_deprecated)
637 Index = Deprecated;
638 else if (Keyword == Ident_obsoleted)
639 Index = Obsoleted;
640 else
641 Index = Unknown;
642
643 if (Index < Unknown) {
644 if (!Changes[Index].KeywordLoc.isInvalid()) {
645 Diag(KeywordLoc, diag::err_availability_redundant)
646 << Keyword
647 << SourceRange(Changes[Index].KeywordLoc,
648 Changes[Index].VersionRange.getEnd());
649 }
650
651 Changes[Index].KeywordLoc = KeywordLoc;
652 Changes[Index].Version = Version;
653 Changes[Index].VersionRange = VersionRange;
654 } else {
655 Diag(KeywordLoc, diag::err_availability_unknown_change)
656 << Keyword << VersionRange;
657 }
658
659 if (Tok.isNot(tok::comma))
660 break;
661
662 ConsumeToken();
663 } while (true);
664
665 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000666 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000667 return;
668
669 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000670 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000671
Douglas Gregorb53e4172011-03-26 03:35:55 +0000672 // The 'unavailable' availability cannot be combined with any other
673 // availability changes. Make sure that hasn't happened.
674 if (UnavailableLoc.isValid()) {
675 bool Complained = false;
676 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
677 if (Changes[Index].KeywordLoc.isValid()) {
678 if (!Complained) {
679 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
680 << SourceRange(Changes[Index].KeywordLoc,
681 Changes[Index].VersionRange.getEnd());
682 Complained = true;
683 }
684
685 // Clear out the availability.
686 Changes[Index] = AvailabilityChange();
687 }
688 }
689 }
690
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000691 // Record this attribute
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000692 attrs.addNew(&Availability,
693 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000694 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000695 Platform, PlatformLoc,
696 Changes[Introduced],
697 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000698 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000699 UnavailableLoc, MessageExpr.take(),
700 false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000701}
702
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000703
704// Late Parsed Attributes:
705// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
706
707void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
708
709void Parser::LateParsedClass::ParseLexedAttributes() {
710 Self->ParseLexedAttributes(*Class);
711}
712
713void Parser::LateParsedAttribute::ParseLexedAttributes() {
714 Self->ParseLexedAttribute(*this);
715}
716
717/// Wrapper class which calls ParseLexedAttribute, after setting up the
718/// scope appropriately.
719void Parser::ParseLexedAttributes(ParsingClass &Class) {
720 // Deal with templates
721 // FIXME: Test cases to make sure this does the right thing for templates.
722 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
723 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
724 HasTemplateScope);
725 if (HasTemplateScope)
726 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
727
728 // Set or update the scope flags to include Scope::ThisScope.
729 bool AlreadyHasClassScope = Class.TopLevelClass;
730 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
731 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
732 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
733
734 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) {
735 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
736 }
737}
738
739/// \brief Finish parsing an attribute for which parsing was delayed.
740/// This will be called at the end of parsing a class declaration
741/// for each LateParsedAttribute. We consume the saved tokens and
742/// create an attribute with the arguments filled in. We add this
743/// to the Attribute list for the decl.
744void Parser::ParseLexedAttribute(LateParsedAttribute &LA) {
745 // Save the current token position.
746 SourceLocation OrigLoc = Tok.getLocation();
747
748 // Append the current token at the end of the new token stream so that it
749 // doesn't get lost.
750 LA.Toks.push_back(Tok);
751 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
752 // Consume the previously pushed token.
753 ConsumeAnyToken();
754
755 ParsedAttributes Attrs(AttrFactory);
756 SourceLocation endLoc;
757
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000758 // If the Decl is templatized, add template parameters to scope.
759 bool HasTemplateScope = LA.D && LA.D->isTemplateDecl();
760 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
761 if (HasTemplateScope)
762 Actions.ActOnReenterTemplateScope(Actions.CurScope, LA.D);
763
764 // If the Decl is on a function, add function parameters to the scope.
765 bool HasFunctionScope = LA.D && LA.D->isFunctionOrFunctionTemplate();
766 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
767 if (HasFunctionScope)
768 Actions.ActOnReenterFunctionContext(Actions.CurScope, LA.D);
769
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000770 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
771
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000772 if (HasFunctionScope) {
773 Actions.ActOnExitFunctionContext();
774 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
775 }
776 if (HasTemplateScope) {
777 TempScope.Exit();
778 }
779
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000780 // Late parsed attributes must be attached to Decls by hand. If the
781 // LA.D is not set, then this was not done properly.
782 assert(LA.D && "No decl attached to late parsed attribute");
783 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.D, Attrs);
784
785 if (Tok.getLocation() != OrigLoc) {
786 // Due to a parsing error, we either went over the cached tokens or
787 // there are still cached tokens left, so we skip the leftover tokens.
788 // Since this is an uncommon situation that should be avoided, use the
789 // expensive isBeforeInTranslationUnit call.
790 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
791 OrigLoc))
792 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
793 ConsumeAnyToken();
794 }
795}
796
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000797/// \brief Wrapper around a case statement checking if AttrName is
798/// one of the thread safety attributes
799bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
800 return llvm::StringSwitch<bool>(AttrName)
801 .Case("guarded_by", true)
802 .Case("guarded_var", true)
803 .Case("pt_guarded_by", true)
804 .Case("pt_guarded_var", true)
805 .Case("lockable", true)
806 .Case("scoped_lockable", true)
807 .Case("no_thread_safety_analysis", true)
808 .Case("acquired_after", true)
809 .Case("acquired_before", true)
810 .Case("exclusive_lock_function", true)
811 .Case("shared_lock_function", true)
812 .Case("exclusive_trylock_function", true)
813 .Case("shared_trylock_function", true)
814 .Case("unlock_function", true)
815 .Case("lock_returned", true)
816 .Case("locks_excluded", true)
817 .Case("exclusive_locks_required", true)
818 .Case("shared_locks_required", true)
819 .Default(false);
820}
821
822/// \brief Parse the contents of thread safety attributes. These
823/// should always be parsed as an expression list.
824///
825/// We need to special case the parsing due to the fact that if the first token
826/// of the first argument is an identifier, the main parse loop will store
827/// that token as a "parameter" and the rest of
828/// the arguments will be added to a list of "arguments". However,
829/// subsequent tokens in the first argument are lost. We instead parse each
830/// argument as an expression and add all arguments to the list of "arguments".
831/// In future, we will take advantage of this special case to also
832/// deal with some argument scoping issues here (for example, referring to a
833/// function parameter in the attribute on that function).
834void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
835 SourceLocation AttrNameLoc,
836 ParsedAttributes &Attrs,
837 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000838 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000839
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000840 BalancedDelimiterTracker T(*this, tok::l_paren);
841 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000842
843 ExprVector ArgExprs(Actions);
844 bool ArgExprsOk = true;
845
846 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +0000847 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000848 ExprResult ArgExpr(ParseAssignmentExpression());
849 if (ArgExpr.isInvalid()) {
850 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000851 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000852 break;
853 } else {
854 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000855 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000856 if (Tok.isNot(tok::comma))
857 break;
858 ConsumeToken(); // Eat the comma, move to the next argument
859 }
860 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000861 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000862 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
863 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000864 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000865 if (EndLoc)
866 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000867}
868
John McCall7f040a92010-12-24 02:08:15 +0000869void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
870 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
871 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000872}
873
Reid Spencer5f016e22007-07-11 17:01:13 +0000874/// ParseDeclaration - Parse a full 'declaration', which consists of
875/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000876/// 'Context' should be a Declarator::TheContext value. This returns the
877/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000878///
879/// declaration: [C99 6.7]
880/// block-declaration ->
881/// simple-declaration
882/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000883/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000884/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000885/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000886/// [C++] using-declaration
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000887/// [C++0x/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000888/// others... [FIXME]
889///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000890Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
891 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000892 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000893 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000894 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000895 // Must temporarily exit the objective-c container scope for
896 // parsing c none objective-c decls.
897 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000898
John McCalld226f652010-08-21 09:40:31 +0000899 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000900 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000901 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000902 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000903 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000904 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000905 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000906 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000907 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000908 // Could be the start of an inline namespace. Allowed as an ext in C++03.
909 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000910 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000911 SourceLocation InlineLoc = ConsumeToken();
912 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
913 break;
914 }
John McCall7f040a92010-12-24 02:08:15 +0000915 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000916 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000917 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000918 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000919 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000920 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000921 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000922 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000923 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000924 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000925 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000926 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000927 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000928 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000929 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000930 default:
John McCall7f040a92010-12-24 02:08:15 +0000931 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000932 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000933
Chris Lattner682bf922009-03-29 16:50:03 +0000934 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +0000935 // single decl, convert it now. Alias declarations can also declare a type;
936 // include that too if it is present.
937 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000938}
939
940/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
941/// declaration-specifiers init-declarator-list[opt] ';'
942///[C90/C++]init-declarator-list ';' [TODO]
943/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000944///
Richard Smithad762fc2011-04-14 22:09:26 +0000945/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
946/// attribute-specifier-seq[opt] type-specifier-seq declarator
947///
Chris Lattnercd147752009-03-29 17:27:48 +0000948/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000949/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000950///
951/// If FRI is non-null, we might be parsing a for-range-declaration instead
952/// of a simple-declaration. If we find that we are, we also parse the
953/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000954Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
955 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000956 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000957 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000958 bool RequireSemi,
959 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000960 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000961 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000962 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000963
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000964 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000965 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +0000966
Reid Spencer5f016e22007-07-11 17:01:13 +0000967 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
968 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000969 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000970 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000971 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000972 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000973 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000974 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000975 }
Douglas Gregor312eadb2011-04-24 05:37:28 +0000976
977 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +0000978}
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Richard Smith0706df42011-10-19 21:33:05 +0000980/// Returns true if this might be the start of a declarator, or a common typo
981/// for a declarator.
982bool Parser::MightBeDeclarator(unsigned Context) {
983 switch (Tok.getKind()) {
984 case tok::annot_cxxscope:
985 case tok::annot_template_id:
986 case tok::caret:
987 case tok::code_completion:
988 case tok::coloncolon:
989 case tok::ellipsis:
990 case tok::kw___attribute:
991 case tok::kw_operator:
992 case tok::l_paren:
993 case tok::star:
994 return true;
995
996 case tok::amp:
997 case tok::ampamp:
Richard Smith0706df42011-10-19 21:33:05 +0000998 return getLang().CPlusPlus;
999
Richard Smith1c94c162012-01-09 22:31:44 +00001000 case tok::l_square: // Might be an attribute on an unnamed bit-field.
1001 return Context == Declarator::MemberContext && getLang().CPlusPlus0x &&
1002 NextToken().is(tok::l_square);
1003
1004 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
1005 return Context == Declarator::MemberContext || getLang().CPlusPlus;
1006
Richard Smith0706df42011-10-19 21:33:05 +00001007 case tok::identifier:
1008 switch (NextToken().getKind()) {
1009 case tok::code_completion:
1010 case tok::coloncolon:
1011 case tok::comma:
1012 case tok::equal:
1013 case tok::equalequal: // Might be a typo for '='.
1014 case tok::kw_alignas:
1015 case tok::kw_asm:
1016 case tok::kw___attribute:
1017 case tok::l_brace:
1018 case tok::l_paren:
1019 case tok::l_square:
1020 case tok::less:
1021 case tok::r_brace:
1022 case tok::r_paren:
1023 case tok::r_square:
1024 case tok::semi:
1025 return true;
1026
1027 case tok::colon:
1028 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001029 // and in block scope it's probably a label. Inside a class definition,
1030 // this is a bit-field.
1031 return Context == Declarator::MemberContext ||
1032 (getLang().CPlusPlus && Context == Declarator::FileContext);
1033
1034 case tok::identifier: // Possible virt-specifier.
1035 return getLang().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001036
1037 default:
1038 return false;
1039 }
1040
1041 default:
1042 return false;
1043 }
1044}
1045
John McCalld8ac0572009-11-03 19:26:08 +00001046/// ParseDeclGroup - Having concluded that this is either a function
1047/// definition or a group of object declarations, actually parse the
1048/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001049Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1050 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001051 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001052 SourceLocation *DeclEnd,
1053 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001054 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001055 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001056 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001057
John McCalld8ac0572009-11-03 19:26:08 +00001058 // Bail out if the first declarator didn't seem well-formed.
1059 if (!D.hasName() && !D.mayOmitIdentifier()) {
1060 // Skip until ; or }.
1061 SkipUntil(tok::r_brace, true, true);
1062 if (Tok.is(tok::semi))
1063 ConsumeToken();
1064 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001065 }
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Chris Lattnerc82daef2010-07-11 22:24:20 +00001067 // Check to see if we have a function *definition* which must have a body.
1068 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1069 // Look at the next token to make sure that this isn't a function
1070 // declaration. We have to check this because __attribute__ might be the
1071 // start of a function definition in GCC-extended K&R C.
1072 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001073
Chris Lattner004659a2010-07-11 22:42:07 +00001074 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001075 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1076 Diag(Tok, diag::err_function_declared_typedef);
1077
1078 // Recover by treating the 'typedef' as spurious.
1079 DS.ClearStorageClassSpecs();
1080 }
1081
John McCalld226f652010-08-21 09:40:31 +00001082 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +00001083 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001084 }
1085
1086 if (isDeclarationSpecifier()) {
1087 // If there is an invalid declaration specifier right after the function
1088 // prototype, then we must be in a missing semicolon case where this isn't
1089 // actually a body. Just fall through into the code that handles it as a
1090 // prototype, and let the top-level code handle the erroneous declspec
1091 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001092 } else {
1093 Diag(Tok, diag::err_expected_fn_body);
1094 SkipUntil(tok::semi);
1095 return DeclGroupPtrTy();
1096 }
1097 }
1098
Richard Smithad762fc2011-04-14 22:09:26 +00001099 if (ParseAttributesAfterDeclarator(D))
1100 return DeclGroupPtrTy();
1101
1102 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1103 // must parse and analyze the for-range-initializer before the declaration is
1104 // analyzed.
1105 if (FRI && Tok.is(tok::colon)) {
1106 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001107 if (Tok.is(tok::l_brace))
1108 FRI->RangeExpr = ParseBraceInitializer();
1109 else
1110 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001111 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1112 Actions.ActOnCXXForRangeDecl(ThisDecl);
1113 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001114 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001115 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1116 }
1117
Chris Lattner5f9e2722011-07-23 10:55:15 +00001118 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001119 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall54abf7d2009-11-04 02:18:39 +00001120 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001121 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001122 DeclsInGroup.push_back(FirstDecl);
1123
Richard Smith0706df42011-10-19 21:33:05 +00001124 bool ExpectSemi = Context != Declarator::ForContext;
1125
John McCalld8ac0572009-11-03 19:26:08 +00001126 // If we don't have a comma, it is either the end of the list (a ';') or an
1127 // error, bail out.
1128 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001129 SourceLocation CommaLoc = ConsumeToken();
1130
1131 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1132 // This comma was followed by a line-break and something which can't be
1133 // the start of a declarator. The comma was probably a typo for a
1134 // semicolon.
1135 Diag(CommaLoc, diag::err_expected_semi_declaration)
1136 << FixItHint::CreateReplacement(CommaLoc, ";");
1137 ExpectSemi = false;
1138 break;
1139 }
John McCalld8ac0572009-11-03 19:26:08 +00001140
1141 // Parse the next declarator.
1142 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001143 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001144
1145 // Accept attributes in an init-declarator. In the first declarator in a
1146 // declaration, these would be part of the declspec. In subsequent
1147 // declarators, they become part of the declarator itself, so that they
1148 // don't apply to declarators after *this* one. Examples:
1149 // short __attribute__((common)) var; -> declspec
1150 // short var __attribute__((common)); -> declarator
1151 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001152 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001153
1154 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001155 if (!D.isInvalidType()) {
1156 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1157 D.complete(ThisDecl);
1158 if (ThisDecl)
1159 DeclsInGroup.push_back(ThisDecl);
1160 }
John McCalld8ac0572009-11-03 19:26:08 +00001161 }
1162
1163 if (DeclEnd)
1164 *DeclEnd = Tok.getLocation();
1165
Richard Smith0706df42011-10-19 21:33:05 +00001166 if (ExpectSemi &&
John McCalld8ac0572009-11-03 19:26:08 +00001167 ExpectAndConsume(tok::semi,
1168 Context == Declarator::FileContext
1169 ? diag::err_invalid_token_after_toplevel_declarator
1170 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001171 // Okay, there was no semicolon and one was expected. If we see a
1172 // declaration specifier, just assume it was missing and continue parsing.
1173 // Otherwise things are very confused and we skip to recover.
1174 if (!isDeclarationSpecifier()) {
1175 SkipUntil(tok::r_brace, true, true);
1176 if (Tok.is(tok::semi))
1177 ConsumeToken();
1178 }
John McCalld8ac0572009-11-03 19:26:08 +00001179 }
1180
Douglas Gregor23c94db2010-07-02 17:43:08 +00001181 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001182 DeclsInGroup.data(),
1183 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001184}
1185
Richard Smithad762fc2011-04-14 22:09:26 +00001186/// Parse an optional simple-asm-expr and attributes, and attach them to a
1187/// declarator. Returns true on an error.
1188bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
1189 // If a simple-asm-expr is present, parse it.
1190 if (Tok.is(tok::kw_asm)) {
1191 SourceLocation Loc;
1192 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1193 if (AsmLabel.isInvalid()) {
1194 SkipUntil(tok::semi, true, true);
1195 return true;
1196 }
1197
1198 D.setAsmLabel(AsmLabel.release());
1199 D.SetRangeEnd(Loc);
1200 }
1201
1202 MaybeParseGNUAttributes(D);
1203 return false;
1204}
1205
Douglas Gregor1426e532009-05-12 21:31:51 +00001206/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1207/// declarator'. This method parses the remainder of the declaration
1208/// (including any attributes or initializer, among other things) and
1209/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001210///
Reid Spencer5f016e22007-07-11 17:01:13 +00001211/// init-declarator: [C99 6.7]
1212/// declarator
1213/// declarator '=' initializer
1214/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1215/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001216/// [C++] declarator initializer[opt]
1217///
1218/// [C++] initializer:
1219/// [C++] '=' initializer-clause
1220/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001221/// [C++0x] '=' 'default' [TODO]
1222/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001223/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001224///
1225/// According to the standard grammar, =default and =delete are function
1226/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001227///
John McCalld226f652010-08-21 09:40:31 +00001228Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001229 const ParsedTemplateInfo &TemplateInfo) {
Richard Smithad762fc2011-04-14 22:09:26 +00001230 if (ParseAttributesAfterDeclarator(D))
1231 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001232
Richard Smithad762fc2011-04-14 22:09:26 +00001233 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1234}
Mike Stump1eb44332009-09-09 15:08:12 +00001235
Richard Smithad762fc2011-04-14 22:09:26 +00001236Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1237 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001238 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001239 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001240 switch (TemplateInfo.Kind) {
1241 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001242 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001243 break;
1244
1245 case ParsedTemplateInfo::Template:
1246 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001247 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001248 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001249 TemplateInfo.TemplateParams->data(),
1250 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001251 D);
1252 break;
1253
1254 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001255 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001256 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001257 TemplateInfo.ExternLoc,
1258 TemplateInfo.TemplateLoc,
1259 D);
1260 if (ThisRes.isInvalid()) {
1261 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001262 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001263 }
1264
1265 ThisDecl = ThisRes.get();
1266 break;
1267 }
1268 }
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Richard Smith34b41d92011-02-20 03:19:35 +00001270 bool TypeContainsAuto =
1271 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1272
Douglas Gregor1426e532009-05-12 21:31:51 +00001273 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001274 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001275 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001276 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001277 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001278 if (D.isFunctionDeclarator())
1279 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1280 << 1 /* delete */;
1281 else
1282 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001283 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001284 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001285 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1286 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001287 else
1288 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001289 } else {
John McCall731ad842009-12-19 09:28:58 +00001290 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1291 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001292 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001293 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001294
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001295 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001296 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001297 cutOffParsing();
1298 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001299 }
1300
John McCall60d7b3a2010-08-24 06:29:42 +00001301 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001302
John McCall731ad842009-12-19 09:28:58 +00001303 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001304 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001305 ExitScope();
1306 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001307
Douglas Gregor1426e532009-05-12 21:31:51 +00001308 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001309 SkipUntil(tok::comma, true, true);
1310 Actions.ActOnInitializerError(ThisDecl);
1311 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001312 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1313 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001314 }
1315 } else if (Tok.is(tok::l_paren)) {
1316 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001317 BalancedDelimiterTracker T(*this, tok::l_paren);
1318 T.consumeOpen();
1319
Douglas Gregor1426e532009-05-12 21:31:51 +00001320 ExprVector Exprs(Actions);
1321 CommaLocsTy CommaLocs;
1322
Douglas Gregorb4debae2009-12-22 17:47:17 +00001323 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1324 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001325 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001326 }
1327
Douglas Gregor1426e532009-05-12 21:31:51 +00001328 if (ParseExpressionList(Exprs, CommaLocs)) {
1329 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001330
1331 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001332 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001333 ExitScope();
1334 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001335 } else {
1336 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001337 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001338
1339 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1340 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001341
1342 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001343 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001344 ExitScope();
1345 }
1346
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001347 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1348 T.getCloseLocation(),
1349 move_arg(Exprs));
1350 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1351 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001352 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001353 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1354 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001355 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1356
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001357 if (D.getCXXScopeSpec().isSet()) {
1358 EnterScope(0);
1359 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1360 }
1361
1362 ExprResult Init(ParseBraceInitializer());
1363
1364 if (D.getCXXScopeSpec().isSet()) {
1365 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1366 ExitScope();
1367 }
1368
1369 if (Init.isInvalid()) {
1370 Actions.ActOnInitializerError(ThisDecl);
1371 } else
1372 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1373 /*DirectInit=*/true, TypeContainsAuto);
1374
Douglas Gregor1426e532009-05-12 21:31:51 +00001375 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001376 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001377 }
1378
Richard Smith483b9f32011-02-21 20:05:19 +00001379 Actions.FinalizeDeclaration(ThisDecl);
1380
Douglas Gregor1426e532009-05-12 21:31:51 +00001381 return ThisDecl;
1382}
1383
Reid Spencer5f016e22007-07-11 17:01:13 +00001384/// ParseSpecifierQualifierList
1385/// specifier-qualifier-list:
1386/// type-specifier specifier-qualifier-list[opt]
1387/// type-qualifier specifier-qualifier-list[opt]
1388/// [GNU] attributes specifier-qualifier-list[opt]
1389///
Richard Smithc89edf52011-07-01 19:46:12 +00001390void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001391 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1392 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001393 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc89edf52011-07-01 19:46:12 +00001394 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001395
Reid Spencer5f016e22007-07-11 17:01:13 +00001396 // Validate declspec for type-name.
1397 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001398 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001399 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001400 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001401
Reid Spencer5f016e22007-07-11 17:01:13 +00001402 // Issue diagnostic and remove storage class if present.
1403 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1404 if (DS.getStorageClassSpecLoc().isValid())
1405 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1406 else
1407 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1408 DS.ClearStorageClassSpecs();
1409 }
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Reid Spencer5f016e22007-07-11 17:01:13 +00001411 // Issue diagnostic and remove function specfier if present.
1412 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001413 if (DS.isInlineSpecified())
1414 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1415 if (DS.isVirtualSpecified())
1416 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1417 if (DS.isExplicitSpecified())
1418 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001419 DS.ClearFunctionSpecs();
1420 }
1421}
1422
Chris Lattnerc199ab32009-04-12 20:42:31 +00001423/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1424/// specified token is valid after the identifier in a declarator which
1425/// immediately follows the declspec. For example, these things are valid:
1426///
1427/// int x [ 4]; // direct-declarator
1428/// int x ( int y); // direct-declarator
1429/// int(int x ) // direct-declarator
1430/// int x ; // simple-declaration
1431/// int x = 17; // init-declarator-list
1432/// int x , y; // init-declarator-list
1433/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001434/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001435/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001436///
1437/// This is not, because 'x' does not immediately follow the declspec (though
1438/// ')' happens to be valid anyway).
1439/// int (x)
1440///
1441static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1442 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1443 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001444 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001445}
1446
Chris Lattnere40c2952009-04-14 21:34:55 +00001447
1448/// ParseImplicitInt - This method is called when we have an non-typename
1449/// identifier in a declspec (which normally terminates the decl spec) when
1450/// the declspec has no type specifier. In this case, the declspec is either
1451/// malformed or is "implicit int" (in K&R and C89).
1452///
1453/// This method handles diagnosing this prettily and returns false if the
1454/// declspec is done being processed. If it recovers and thinks there may be
1455/// other pieces of declspec after it, it returns true.
1456///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001457bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001458 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001459 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001460 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001461
Chris Lattnere40c2952009-04-14 21:34:55 +00001462 SourceLocation Loc = Tok.getLocation();
1463 // If we see an identifier that is not a type name, we normally would
1464 // parse it as the identifer being declared. However, when a typename
1465 // is typo'd or the definition is not included, this will incorrectly
1466 // parse the typename as the identifier name and fall over misparsing
1467 // later parts of the diagnostic.
1468 //
1469 // As such, we try to do some look-ahead in cases where this would
1470 // otherwise be an "implicit-int" case to see if this is invalid. For
1471 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1472 // an identifier with implicit int, we'd get a parse error because the
1473 // next token is obviously invalid for a type. Parse these as a case
1474 // with an invalid type specifier.
1475 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001476
Chris Lattnere40c2952009-04-14 21:34:55 +00001477 // Since we know that this either implicit int (which is rare) or an
1478 // error, we'd do lookahead to try to do better recovery.
1479 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1480 // If this token is valid for implicit int, e.g. "static x = 4", then
1481 // we just avoid eating the identifier, so it will be parsed as the
1482 // identifier in the declarator.
1483 return false;
1484 }
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Chris Lattnere40c2952009-04-14 21:34:55 +00001486 // Otherwise, if we don't consume this token, we are going to emit an
1487 // error anyway. Try to recover from various common problems. Check
1488 // to see if this was a reference to a tag name without a tag specified.
1489 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001490 //
1491 // C++ doesn't need this, and isTagName doesn't take SS.
1492 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001493 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001494 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001495
Douglas Gregor23c94db2010-07-02 17:43:08 +00001496 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001497 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001498 case DeclSpec::TST_enum:
1499 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1500 case DeclSpec::TST_union:
1501 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1502 case DeclSpec::TST_struct:
1503 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1504 case DeclSpec::TST_class:
1505 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001506 }
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Chris Lattnerf4382f52009-04-14 22:17:06 +00001508 if (TagName) {
1509 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001510 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001511 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Chris Lattnerf4382f52009-04-14 22:17:06 +00001513 // Parse this as a tag as if the missing tag were present.
1514 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001515 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001516 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001517 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001518 return true;
1519 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001520 }
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Douglas Gregora786fdb2009-10-13 23:27:22 +00001522 // This is almost certainly an invalid type name. Let the action emit a
1523 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001524 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001525 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001526 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001527 // The action emitted a diagnostic, so we don't have to.
1528 if (T) {
1529 // The action has suggested that the type T could be used. Set that as
1530 // the type in the declaration specifiers, consume the would-be type
1531 // name token, and we're done.
1532 const char *PrevSpec;
1533 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001534 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001535 DS.SetRangeEnd(Tok.getLocation());
1536 ConsumeToken();
1537
1538 // There may be other declaration specifiers after this.
1539 return true;
1540 }
1541
1542 // Fall through; the action had no suggestion for us.
1543 } else {
1544 // The action did not emit a diagnostic, so emit one now.
1545 SourceRange R;
1546 if (SS) R = SS->getRange();
1547 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1548 }
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Douglas Gregora786fdb2009-10-13 23:27:22 +00001550 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001551 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001552 unsigned DiagID;
1553 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001554 DS.SetRangeEnd(Tok.getLocation());
1555 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Chris Lattnere40c2952009-04-14 21:34:55 +00001557 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1558 // avoid rippling error messages on subsequent uses of the same type,
1559 // could be useful if #include was forgotten.
1560 return false;
1561}
1562
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001563/// \brief Determine the declaration specifier context from the declarator
1564/// context.
1565///
1566/// \param Context the declarator context, which is one of the
1567/// Declarator::TheContext enumerator values.
1568Parser::DeclSpecContext
1569Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1570 if (Context == Declarator::MemberContext)
1571 return DSC_class;
1572 if (Context == Declarator::FileContext)
1573 return DSC_top_level;
1574 return DSC_normal;
1575}
1576
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001577/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1578///
1579/// FIXME: Simply returns an alignof() expression if the argument is a
1580/// type. Ideally, the type should be propagated directly into Sema.
1581///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001582/// [C11] type-id
1583/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001584/// [C++0x] type-id ...[opt]
1585/// [C++0x] assignment-expression ...[opt]
1586ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1587 SourceLocation &EllipsisLoc) {
1588 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001589 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001590 SourceLocation TypeLoc = Tok.getLocation();
1591 ParsedType Ty = ParseTypeName().get();
1592 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001593 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1594 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001595 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001596 ER = ParseConstantExpression();
1597
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001598 if (getLang().CPlusPlus0x && Tok.is(tok::ellipsis))
1599 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001600
1601 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001602}
1603
1604/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1605/// attribute to Attrs.
1606///
1607/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001608/// [C11] '_Alignas' '(' type-id ')'
1609/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001610/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1611/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001612void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1613 SourceLocation *endLoc) {
1614 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1615 "Not an alignment-specifier!");
1616
1617 SourceLocation KWLoc = Tok.getLocation();
1618 ConsumeToken();
1619
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001620 BalancedDelimiterTracker T(*this, tok::l_paren);
1621 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001622 return;
1623
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001624 SourceLocation EllipsisLoc;
1625 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001626 if (ArgExpr.isInvalid()) {
1627 SkipUntil(tok::r_paren);
1628 return;
1629 }
1630
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001631 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001632 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001633 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001634
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001635 // FIXME: Handle pack-expansions here.
1636 if (EllipsisLoc.isValid()) {
1637 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1638 return;
1639 }
1640
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001641 ExprVector ArgExprs(Actions);
1642 ArgExprs.push_back(ArgExpr.release());
1643 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001644 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001645}
1646
Reid Spencer5f016e22007-07-11 17:01:13 +00001647/// ParseDeclarationSpecifiers
1648/// declaration-specifiers: [C99 6.7]
1649/// storage-class-specifier declaration-specifiers[opt]
1650/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001651/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001652/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001653/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001654/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001655///
1656/// storage-class-specifier: [C99 6.7.1]
1657/// 'typedef'
1658/// 'extern'
1659/// 'static'
1660/// 'auto'
1661/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001662/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001663/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001664/// function-specifier: [C99 6.7.4]
1665/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001666/// [C++] 'virtual'
1667/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001668/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001669/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001670/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001671
Reid Spencer5f016e22007-07-11 17:01:13 +00001672///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001673void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001674 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001675 AccessSpecifier AS,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001676 DeclSpecContext DSContext) {
1677 if (DS.getSourceRange().isInvalid()) {
1678 DS.SetRangeStart(Tok.getLocation());
1679 DS.SetRangeEnd(Tok.getLocation());
1680 }
1681
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001682 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001683 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001684 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001685 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001686 unsigned DiagID = 0;
1687
Reid Spencer5f016e22007-07-11 17:01:13 +00001688 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001689
Reid Spencer5f016e22007-07-11 17:01:13 +00001690 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001691 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001692 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001693 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1694 MaybeParseCXX0XAttributes(DS.getAttributes());
1695
Reid Spencer5f016e22007-07-11 17:01:13 +00001696 // If this is not a declaration specifier token, we're done reading decl
1697 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001698 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001699 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001700
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001701 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001702 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001703 if (DS.hasTypeSpecifier()) {
1704 bool AllowNonIdentifiers
1705 = (getCurScope()->getFlags() & (Scope::ControlScope |
1706 Scope::BlockScope |
1707 Scope::TemplateParamScope |
1708 Scope::FunctionPrototypeScope |
1709 Scope::AtCatchScope)) == 0;
1710 bool AllowNestedNameSpecifiers
1711 = DSContext == DSC_top_level ||
1712 (DSContext == DSC_class && DS.isFriendSpecified());
1713
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001714 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1715 AllowNonIdentifiers,
1716 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001717 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001718 }
1719
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001720 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1721 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1722 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001723 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1724 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001725 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001726 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001727 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00001728 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001729
1730 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001731 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001732 }
1733
Chris Lattner5e02c472009-01-05 00:07:25 +00001734 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001735 // C++ scope specifier. Annotate and loop, or bail out on error.
1736 if (TryAnnotateCXXScopeToken(true)) {
1737 if (!DS.hasTypeSpecifier())
1738 DS.SetTypeSpecError();
1739 goto DoneWithDeclSpec;
1740 }
John McCall2e0a7152010-03-01 18:20:46 +00001741 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1742 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001743 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001744
1745 case tok::annot_cxxscope: {
1746 if (DS.hasTypeSpecifier())
1747 goto DoneWithDeclSpec;
1748
John McCallaa87d332009-12-12 11:40:51 +00001749 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001750 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1751 Tok.getAnnotationRange(),
1752 SS);
John McCallaa87d332009-12-12 11:40:51 +00001753
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001754 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001755 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001756 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001757 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001758 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001759 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001760
1761 // C++ [class.qual]p2:
1762 // In a lookup in which the constructor is an acceptable lookup
1763 // result and the nested-name-specifier nominates a class C:
1764 //
1765 // - if the name specified after the
1766 // nested-name-specifier, when looked up in C, is the
1767 // injected-class-name of C (Clause 9), or
1768 //
1769 // - if the name specified after the nested-name-specifier
1770 // is the same as the identifier or the
1771 // simple-template-id's template-name in the last
1772 // component of the nested-name-specifier,
1773 //
1774 // the name is instead considered to name the constructor of
1775 // class C.
1776 //
1777 // Thus, if the template-name is actually the constructor
1778 // name, then the code is ill-formed; this interpretation is
1779 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001780 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001781 if ((DSContext == DSC_top_level ||
1782 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1783 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001784 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001785 if (isConstructorDeclarator()) {
1786 // The user meant this to be an out-of-line constructor
1787 // definition, but template arguments are not allowed
1788 // there. Just allow this as a constructor; we'll
1789 // complain about it later.
1790 goto DoneWithDeclSpec;
1791 }
1792
1793 // The user meant this to name a type, but it actually names
1794 // a constructor with some extraneous template
1795 // arguments. Complain, then parse it as a type as the user
1796 // intended.
1797 Diag(TemplateId->TemplateNameLoc,
1798 diag::err_out_of_line_template_id_names_constructor)
1799 << TemplateId->Name;
1800 }
1801
John McCallaa87d332009-12-12 11:40:51 +00001802 DS.getTypeSpecScope() = SS;
1803 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001804 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001805 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001806 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001807 continue;
1808 }
1809
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001810 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001811 DS.getTypeSpecScope() = SS;
1812 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001813 if (Tok.getAnnotationValue()) {
1814 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001815 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1816 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001817 PrevSpec, DiagID, T);
1818 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001819 else
1820 DS.SetTypeSpecError();
1821 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1822 ConsumeToken(); // The typename
1823 }
1824
Douglas Gregor9135c722009-03-25 15:40:00 +00001825 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001826 goto DoneWithDeclSpec;
1827
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001828 // If we're in a context where the identifier could be a class name,
1829 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001830 if ((DSContext == DSC_top_level ||
1831 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001832 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001833 &SS)) {
1834 if (isConstructorDeclarator())
1835 goto DoneWithDeclSpec;
1836
1837 // As noted in C++ [class.qual]p2 (cited above), when the name
1838 // of the class is qualified in a context where it could name
1839 // a constructor, its a constructor name. However, we've
1840 // looked at the declarator, and the user probably meant this
1841 // to be a type. Complain that it isn't supposed to be treated
1842 // as a type, then proceed to parse it as a type.
1843 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1844 << Next.getIdentifierInfo();
1845 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001846
John McCallb3d87482010-08-24 05:47:05 +00001847 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1848 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001849 getCurScope(), &SS,
1850 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001851 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00001852 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001853
Chris Lattnerf4382f52009-04-14 22:17:06 +00001854 // If the referenced identifier is not a type, then this declspec is
1855 // erroneous: We already checked about that it has no type specifier, and
1856 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001857 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001858 if (TypeRep == 0) {
1859 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001860 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001861 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001862 }
Mike Stump1eb44332009-09-09 15:08:12 +00001863
John McCallaa87d332009-12-12 11:40:51 +00001864 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001865 ConsumeToken(); // The C++ scope.
1866
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001867 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001868 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001869 if (isInvalid)
1870 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001871
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001872 DS.SetRangeEnd(Tok.getLocation());
1873 ConsumeToken(); // The typename.
1874
1875 continue;
1876 }
Mike Stump1eb44332009-09-09 15:08:12 +00001877
Chris Lattner80d0c892009-01-21 19:48:37 +00001878 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001879 if (Tok.getAnnotationValue()) {
1880 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001881 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001882 DiagID, T);
1883 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001884 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001885
1886 if (isInvalid)
1887 break;
1888
Chris Lattner80d0c892009-01-21 19:48:37 +00001889 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1890 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001891
Chris Lattner80d0c892009-01-21 19:48:37 +00001892 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1893 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001894 // Objective-C interface.
1895 if (Tok.is(tok::less) && getLang().ObjC1)
1896 ParseObjCProtocolQualifiers(DS);
1897
Chris Lattner80d0c892009-01-21 19:48:37 +00001898 continue;
1899 }
Mike Stump1eb44332009-09-09 15:08:12 +00001900
Douglas Gregorbfad9152011-04-28 15:48:45 +00001901 case tok::kw___is_signed:
1902 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1903 // typically treats it as a trait. If we see __is_signed as it appears
1904 // in libstdc++, e.g.,
1905 //
1906 // static const bool __is_signed;
1907 //
1908 // then treat __is_signed as an identifier rather than as a keyword.
1909 if (DS.getTypeSpecType() == TST_bool &&
1910 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1911 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1912 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1913 Tok.setKind(tok::identifier);
1914 }
1915
1916 // We're done with the declaration-specifiers.
1917 goto DoneWithDeclSpec;
1918
Chris Lattner3bd934a2008-07-26 01:18:38 +00001919 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00001920 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001921 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001922 // In C++, check to see if this is a scope specifier like foo::bar::, if
1923 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001924 if (getLang().CPlusPlus) {
1925 if (TryAnnotateCXXScopeToken(true)) {
1926 if (!DS.hasTypeSpecifier())
1927 DS.SetTypeSpecError();
1928 goto DoneWithDeclSpec;
1929 }
1930 if (!Tok.is(tok::identifier))
1931 continue;
1932 }
Mike Stump1eb44332009-09-09 15:08:12 +00001933
Chris Lattner3bd934a2008-07-26 01:18:38 +00001934 // This identifier can only be a typedef name if we haven't already seen
1935 // a type-specifier. Without this check we misparse:
1936 // typedef int X; struct Y { short X; }; as 'short int'.
1937 if (DS.hasTypeSpecifier())
1938 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001939
John Thompson82287d12010-02-05 00:12:22 +00001940 // Check for need to substitute AltiVec keyword tokens.
1941 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1942 break;
1943
Chris Lattner3bd934a2008-07-26 01:18:38 +00001944 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001945 ParsedType TypeRep =
1946 Actions.getTypeName(*Tok.getIdentifierInfo(),
1947 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001948
Chris Lattnerc199ab32009-04-12 20:42:31 +00001949 // If this is not a typedef name, don't parse it as part of the declspec,
1950 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001951 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001952 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001953 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001954 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001955
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001956 // If we're in a context where the identifier could be a class name,
1957 // check whether this is a constructor declaration.
1958 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001959 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001960 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001961 goto DoneWithDeclSpec;
1962
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001963 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001964 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001965 if (isInvalid)
1966 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001967
Chris Lattner3bd934a2008-07-26 01:18:38 +00001968 DS.SetRangeEnd(Tok.getLocation());
1969 ConsumeToken(); // The identifier
1970
1971 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1972 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001973 // Objective-C interface.
1974 if (Tok.is(tok::less) && getLang().ObjC1)
1975 ParseObjCProtocolQualifiers(DS);
1976
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001977 // Need to support trailing type qualifiers (e.g. "id<p> const").
1978 // If a type specifier follows, it will be diagnosed elsewhere.
1979 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001980 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001981
1982 // type-name
1983 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001984 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001985 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001986 // This template-id does not refer to a type name, so we're
1987 // done with the type-specifiers.
1988 goto DoneWithDeclSpec;
1989 }
1990
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001991 // If we're in a context where the template-id could be a
1992 // constructor name or specialization, check whether this is a
1993 // constructor declaration.
1994 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001995 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001996 isConstructorDeclarator())
1997 goto DoneWithDeclSpec;
1998
Douglas Gregor39a8de12009-02-25 19:37:18 +00001999 // Turn the template-id annotation token into a type annotation
2000 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002001 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002002 continue;
2003 }
2004
Reid Spencer5f016e22007-07-11 17:01:13 +00002005 // GNU attributes support.
2006 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00002007 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00002008 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002009
2010 // Microsoft declspec support.
2011 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002012 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002013 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002014
Steve Naroff239f0732008-12-25 14:16:32 +00002015 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002016 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00002017 // FIXME: Add handling here!
2018 break;
2019
2020 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002021 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002022 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002023 case tok::kw___cdecl:
2024 case tok::kw___stdcall:
2025 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002026 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002027 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002028 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002029 continue;
2030
Dawn Perchik52fc3142010-09-03 01:29:35 +00002031 // Borland single token adornments.
2032 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002033 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002034 continue;
2035
Peter Collingbournef315fa82011-02-14 01:42:53 +00002036 // OpenCL single token adornments.
2037 case tok::kw___kernel:
2038 ParseOpenCLAttributes(DS.getAttributes());
2039 continue;
2040
Reid Spencer5f016e22007-07-11 17:01:13 +00002041 // storage-class-specifier
2042 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002043 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2044 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002045 break;
2046 case tok::kw_extern:
2047 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002048 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002049 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2050 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002051 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002052 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002053 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2054 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002055 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002056 case tok::kw_static:
2057 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002058 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002059 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2060 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002061 break;
2062 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00002063 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002064 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002065 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2066 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002067 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002068 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002069 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002070 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002071 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2072 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002073 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002074 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2075 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002076 break;
2077 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002078 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2079 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002080 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002081 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002082 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2083 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002084 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002085 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002086 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002087 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002088
Reid Spencer5f016e22007-07-11 17:01:13 +00002089 // function-specifier
2090 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002091 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002092 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002093 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002094 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002095 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002096 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002097 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002098 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002099
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002100 // alignment-specifier
2101 case tok::kw__Alignas:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002102 if (!getLang().C11)
2103 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002104 ParseAlignmentSpecifier(DS.getAttributes());
2105 continue;
2106
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002107 // friend
2108 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002109 if (DSContext == DSC_class)
2110 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2111 else {
2112 PrevSpec = ""; // not actually used by the diagnostic
2113 DiagID = diag::err_friend_invalid_in_context;
2114 isInvalid = true;
2115 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002116 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002117
Douglas Gregor8d267c52011-09-09 02:06:17 +00002118 // Modules
2119 case tok::kw___module_private__:
2120 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2121 break;
2122
Sebastian Redl2ac67232009-11-05 15:47:02 +00002123 // constexpr
2124 case tok::kw_constexpr:
2125 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2126 break;
2127
Chris Lattner80d0c892009-01-21 19:48:37 +00002128 // type-specifier
2129 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002130 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2131 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002132 break;
2133 case tok::kw_long:
2134 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002135 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2136 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002137 else
John McCallfec54012009-08-03 20:12:06 +00002138 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2139 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002140 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002141 case tok::kw___int64:
2142 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2143 DiagID);
2144 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002145 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002146 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2147 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002148 break;
2149 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002150 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2151 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002152 break;
2153 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002154 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2155 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002156 break;
2157 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002158 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2159 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002160 break;
2161 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002162 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2163 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002164 break;
2165 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002166 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2167 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002168 break;
2169 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002170 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2171 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002172 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002173 case tok::kw_half:
2174 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2175 DiagID);
2176 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002177 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002178 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2179 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002180 break;
2181 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002182 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2183 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002184 break;
2185 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002186 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2187 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002188 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002189 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002190 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2191 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002192 break;
2193 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002194 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2195 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002196 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002197 case tok::kw_bool:
2198 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002199 if (Tok.is(tok::kw_bool) &&
2200 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2201 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2202 PrevSpec = ""; // Not used by the diagnostic.
2203 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002204 // For better error recovery.
2205 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002206 isInvalid = true;
2207 } else {
2208 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2209 DiagID);
2210 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002211 break;
2212 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002213 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2214 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002215 break;
2216 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002217 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2218 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002219 break;
2220 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002221 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2222 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002223 break;
John Thompson82287d12010-02-05 00:12:22 +00002224 case tok::kw___vector:
2225 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2226 break;
2227 case tok::kw___pixel:
2228 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2229 break;
John McCalla5fc4722011-04-09 22:50:59 +00002230 case tok::kw___unknown_anytype:
2231 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2232 PrevSpec, DiagID);
2233 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002234
2235 // class-specifier:
2236 case tok::kw_class:
2237 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002238 case tok::kw_union: {
2239 tok::TokenKind Kind = Tok.getKind();
2240 ConsumeToken();
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002241 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, EnteringContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002242 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002243 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002244
2245 // enum-specifier:
2246 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002247 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002248 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00002249 continue;
2250
2251 // cv-qualifier:
2252 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002253 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
2254 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002255 break;
2256 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002257 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2258 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002259 break;
2260 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002261 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2262 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002263 break;
2264
Douglas Gregord57959a2009-03-27 23:10:48 +00002265 // C++ typename-specifier:
2266 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002267 if (TryAnnotateTypeOrScopeToken()) {
2268 DS.SetTypeSpecError();
2269 goto DoneWithDeclSpec;
2270 }
2271 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002272 continue;
2273 break;
2274
Chris Lattner80d0c892009-01-21 19:48:37 +00002275 // GNU typeof support.
2276 case tok::kw_typeof:
2277 ParseTypeofSpecifier(DS);
2278 continue;
2279
David Blaikie42d6d0c2011-12-04 05:04:18 +00002280 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002281 ParseDecltypeSpecifier(DS);
2282 continue;
2283
Sean Huntdb5d44b2011-05-19 05:37:45 +00002284 case tok::kw___underlying_type:
2285 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002286 continue;
2287
2288 case tok::kw__Atomic:
2289 ParseAtomicSpecifier(DS);
2290 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002291
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002292 // OpenCL qualifiers:
2293 case tok::kw_private:
2294 if (!getLang().OpenCL)
2295 goto DoneWithDeclSpec;
2296 case tok::kw___private:
2297 case tok::kw___global:
2298 case tok::kw___local:
2299 case tok::kw___constant:
2300 case tok::kw___read_only:
2301 case tok::kw___write_only:
2302 case tok::kw___read_write:
2303 ParseOpenCLQualifiers(DS);
2304 break;
2305
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002306 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002307 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002308 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2309 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00002310 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002311 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002312
Douglas Gregor46f936e2010-11-19 17:10:50 +00002313 if (!ParseObjCProtocolQualifiers(DS))
2314 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2315 << FixItHint::CreateInsertion(Loc, "id")
2316 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002317
2318 // Need to support trailing type qualifiers (e.g. "id<p> const").
2319 // If a type specifier follows, it will be diagnosed elsewhere.
2320 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002321 }
John McCallfec54012009-08-03 20:12:06 +00002322 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002323 if (isInvalid) {
2324 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002325 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002326
2327 if (DiagID == diag::ext_duplicate_declspec)
2328 Diag(Tok, DiagID)
2329 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2330 else
2331 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002332 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002333
Chris Lattner81c018d2008-03-13 06:29:04 +00002334 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002335 if (DiagID != diag::err_bool_redeclaration)
2336 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002337 }
2338}
Douglas Gregoradcac882008-12-01 23:54:00 +00002339
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002340/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00002341/// primarily follow the C++ grammar with additions for C99 and GNU,
2342/// which together subsume the C grammar. Note that the C++
2343/// type-specifier also includes the C type-qualifier (for const,
2344/// volatile, and C99 restrict). Returns true if a type-specifier was
2345/// found (and parsed), false otherwise.
2346///
2347/// type-specifier: [C++ 7.1.5]
2348/// simple-type-specifier
2349/// class-specifier
2350/// enum-specifier
2351/// elaborated-type-specifier [TODO]
2352/// cv-qualifier
2353///
2354/// cv-qualifier: [C++ 7.1.5.1]
2355/// 'const'
2356/// 'volatile'
2357/// [C99] 'restrict'
2358///
2359/// simple-type-specifier: [ C++ 7.1.5.2]
2360/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2361/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2362/// 'char'
2363/// 'wchar_t'
2364/// 'bool'
2365/// 'short'
2366/// 'int'
2367/// 'long'
2368/// 'signed'
2369/// 'unsigned'
2370/// 'float'
2371/// 'double'
2372/// 'void'
2373/// [C99] '_Bool'
2374/// [C99] '_Complex'
2375/// [C99] '_Imaginary' // Removed in TC2?
2376/// [GNU] '_Decimal32'
2377/// [GNU] '_Decimal64'
2378/// [GNU] '_Decimal128'
2379/// [GNU] typeof-specifier
2380/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2381/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002382/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00002383/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00002384bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002385 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002386 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002387 const ParsedTemplateInfo &TemplateInfo,
2388 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00002389 SourceLocation Loc = Tok.getLocation();
2390
2391 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00002392 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00002393 // If we already have a type specifier, this identifier is not a type.
2394 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2395 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2396 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2397 return false;
John Thompson82287d12010-02-05 00:12:22 +00002398 // Check for need to substitute AltiVec keyword tokens.
2399 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2400 break;
2401 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00002402 case tok::kw_decltype:
Douglas Gregord57959a2009-03-27 23:10:48 +00002403 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00002404 // Annotate typenames and C++ scope specifiers. If we get one, just
2405 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002406 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2407 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002408 return true;
2409 if (Tok.is(tok::identifier))
2410 return false;
2411 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2412 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00002413 case tok::coloncolon: // ::foo::bar
2414 if (NextToken().is(tok::kw_new) || // ::new
2415 NextToken().is(tok::kw_delete)) // ::delete
2416 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002417
Chris Lattner166a8fc2009-01-04 23:41:41 +00002418 // Annotate typenames and C++ scope specifiers. If we get one, just
2419 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002420 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2421 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002422 return true;
2423 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2424 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00002425
Douglas Gregor12e083c2008-11-07 15:42:26 +00002426 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00002427 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002428 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00002429 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2430 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002431 DiagID, T);
2432 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002433 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002434 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2435 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002436
Douglas Gregor12e083c2008-11-07 15:42:26 +00002437 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2438 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2439 // Objective-C interface. If we don't have Objective-C or a '<', this is
2440 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002441 if (Tok.is(tok::less) && getLang().ObjC1)
2442 ParseObjCProtocolQualifiers(DS);
2443
Douglas Gregor12e083c2008-11-07 15:42:26 +00002444 return true;
2445 }
2446
2447 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002448 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002449 break;
2450 case tok::kw_long:
2451 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002452 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2453 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002454 else
John McCallfec54012009-08-03 20:12:06 +00002455 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2456 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002457 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002458 case tok::kw___int64:
2459 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2460 DiagID);
2461 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002462 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002463 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002464 break;
2465 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002466 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2467 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002468 break;
2469 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002470 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2471 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002472 break;
2473 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002474 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2475 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002476 break;
2477 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002478 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002479 break;
2480 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002481 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002482 break;
2483 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002484 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002485 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002486 case tok::kw_half:
2487 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
2488 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002489 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002490 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002491 break;
2492 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002493 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002494 break;
2495 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002496 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002497 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002498 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002499 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002500 break;
2501 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002502 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002503 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002504 case tok::kw_bool:
2505 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002506 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002507 break;
2508 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002509 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2510 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002511 break;
2512 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002513 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2514 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002515 break;
2516 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002517 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2518 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002519 break;
John Thompson82287d12010-02-05 00:12:22 +00002520 case tok::kw___vector:
2521 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2522 break;
2523 case tok::kw___pixel:
2524 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2525 break;
2526
Douglas Gregor12e083c2008-11-07 15:42:26 +00002527 // class-specifier:
2528 case tok::kw_class:
2529 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002530 case tok::kw_union: {
2531 tok::TokenKind Kind = Tok.getKind();
2532 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002533 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002534 /*EnteringContext=*/false,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002535 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002536 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002537 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002538
2539 // enum-specifier:
2540 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002541 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002542 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002543 return true;
2544
2545 // cv-qualifier:
2546 case tok::kw_const:
2547 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002548 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002549 break;
2550 case tok::kw_volatile:
2551 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002552 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002553 break;
2554 case tok::kw_restrict:
2555 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002556 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002557 break;
2558
2559 // GNU typeof support.
2560 case tok::kw_typeof:
2561 ParseTypeofSpecifier(DS);
2562 return true;
2563
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002564 // C++0x decltype support.
David Blaikie42d6d0c2011-12-04 05:04:18 +00002565 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002566 ParseDecltypeSpecifier(DS);
2567 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002568
Sean Huntdb5d44b2011-05-19 05:37:45 +00002569 // C++0x type traits support.
2570 case tok::kw___underlying_type:
2571 ParseUnderlyingTypeSpecifier(DS);
2572 return true;
2573
Eli Friedmanb001de72011-10-06 23:00:33 +00002574 case tok::kw__Atomic:
2575 ParseAtomicSpecifier(DS);
2576 return true;
2577
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002578 // OpenCL qualifiers:
2579 case tok::kw_private:
2580 if (!getLang().OpenCL)
2581 return false;
2582 case tok::kw___private:
2583 case tok::kw___global:
2584 case tok::kw___local:
2585 case tok::kw___constant:
2586 case tok::kw___read_only:
2587 case tok::kw___write_only:
2588 case tok::kw___read_write:
2589 ParseOpenCLQualifiers(DS);
2590 break;
2591
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002592 // C++0x auto support.
2593 case tok::kw_auto:
Richard Smith87e96eb2011-09-04 20:24:20 +00002594 // This is only called in situations where a storage-class specifier is
2595 // illegal, so we can assume an auto type specifier was intended even in
2596 // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate
2597 // extension diagnostic.
2598 if (!getLang().CPlusPlus)
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002599 return false;
2600
John McCallfec54012009-08-03 20:12:06 +00002601 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002602 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002603
Eli Friedman290eeb02009-06-08 23:27:34 +00002604 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002605 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00002606 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002607 case tok::kw___cdecl:
2608 case tok::kw___stdcall:
2609 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002610 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002611 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002612 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002613 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002614
Dawn Perchik52fc3142010-09-03 01:29:35 +00002615 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002616 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002617 return true;
2618
Douglas Gregor12e083c2008-11-07 15:42:26 +00002619 default:
2620 // Not a type-specifier; do nothing.
2621 return false;
2622 }
2623
2624 // If the specifier combination wasn't legal, issue a diagnostic.
2625 if (isInvalid) {
2626 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002627 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002628 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002629 }
2630 DS.SetRangeEnd(Tok.getLocation());
2631 ConsumeToken(); // whatever we parsed above.
2632 return true;
2633}
Reid Spencer5f016e22007-07-11 17:01:13 +00002634
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002635/// ParseStructDeclaration - Parse a struct declaration without the terminating
2636/// semicolon.
2637///
Reid Spencer5f016e22007-07-11 17:01:13 +00002638/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002639/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002640/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002641/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002642/// struct-declarator-list:
2643/// struct-declarator
2644/// struct-declarator-list ',' struct-declarator
2645/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2646/// struct-declarator:
2647/// declarator
2648/// [GNU] declarator attributes[opt]
2649/// declarator[opt] ':' constant-expression
2650/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2651///
Chris Lattnere1359422008-04-10 06:46:29 +00002652void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002653ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002654
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002655 if (Tok.is(tok::kw___extension__)) {
2656 // __extension__ silences extension warnings in the subexpression.
2657 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002658 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002659 return ParseStructDeclaration(DS, Fields);
2660 }
Mike Stump1eb44332009-09-09 15:08:12 +00002661
Steve Naroff28a7ca82007-08-20 22:28:22 +00002662 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002663 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002664
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002665 // If there are no declarators, this is a free-standing declaration
2666 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002667 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002668 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002669 return;
2670 }
2671
2672 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002673 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002674 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002675 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002676 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002677 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002678 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002679
2680 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002681 if (!FirstDeclarator)
2682 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002683
Steve Naroff28a7ca82007-08-20 22:28:22 +00002684 /// struct-declarator: declarator
2685 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002686 if (Tok.isNot(tok::colon)) {
2687 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2688 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002689 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002690 }
Mike Stump1eb44332009-09-09 15:08:12 +00002691
Chris Lattner04d66662007-10-09 17:33:22 +00002692 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002693 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002694 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002695 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002696 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002697 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002698 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002699 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002700
Steve Naroff28a7ca82007-08-20 22:28:22 +00002701 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002702 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002703
John McCallbdd563e2009-11-03 02:38:08 +00002704 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002705 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002706 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002707
Steve Naroff28a7ca82007-08-20 22:28:22 +00002708 // If we don't have a comma, it is either the end of the list (a ';')
2709 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002710 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002711 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002712
Steve Naroff28a7ca82007-08-20 22:28:22 +00002713 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002714 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002715
John McCallbdd563e2009-11-03 02:38:08 +00002716 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002717 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002718}
2719
2720/// ParseStructUnionBody
2721/// struct-contents:
2722/// struct-declaration-list
2723/// [EXT] empty
2724/// [GNU] "struct-declaration-list" without terminatoring ';'
2725/// struct-declaration-list:
2726/// struct-declaration
2727/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002728/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002729///
Reid Spencer5f016e22007-07-11 17:01:13 +00002730void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002731 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002732 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2733 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002734
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002735 BalancedDelimiterTracker T(*this, tok::l_brace);
2736 if (T.consumeOpen())
2737 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002738
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002739 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002740 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002741
Reid Spencer5f016e22007-07-11 17:01:13 +00002742 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2743 // C++.
Richard Smithd7c56e12011-12-29 21:57:33 +00002744 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) {
2745 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2746 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2747 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002748
Chris Lattner5f9e2722011-07-23 10:55:15 +00002749 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002750
Reid Spencer5f016e22007-07-11 17:01:13 +00002751 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002752 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002753 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002754
Reid Spencer5f016e22007-07-11 17:01:13 +00002755 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002756 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002757 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002758 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002759 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002760 ConsumeToken();
2761 continue;
2762 }
Chris Lattnere1359422008-04-10 06:46:29 +00002763
2764 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002765 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002766
John McCallbdd563e2009-11-03 02:38:08 +00002767 if (!Tok.is(tok::at)) {
2768 struct CFieldCallback : FieldCallback {
2769 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002770 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002771 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002772
John McCalld226f652010-08-21 09:40:31 +00002773 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002774 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002775 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2776
John McCalld226f652010-08-21 09:40:31 +00002777 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002778 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002779 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002780 FD.D.getDeclSpec().getSourceRange().getBegin(),
2781 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002782 FieldDecls.push_back(Field);
2783 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002784 }
John McCallbdd563e2009-11-03 02:38:08 +00002785 } Callback(*this, TagDecl, FieldDecls);
2786
2787 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002788 } else { // Handle @defs
2789 ConsumeToken();
2790 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2791 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002792 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002793 continue;
2794 }
2795 ConsumeToken();
2796 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2797 if (!Tok.is(tok::identifier)) {
2798 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002799 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002800 continue;
2801 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002802 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002803 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002804 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002805 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2806 ConsumeToken();
2807 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002808 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002809
Chris Lattner04d66662007-10-09 17:33:22 +00002810 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002811 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002812 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002813 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002814 break;
2815 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002816 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2817 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002818 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002819 // If we stopped at a ';', eat it.
2820 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002821 }
2822 }
Mike Stump1eb44332009-09-09 15:08:12 +00002823
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002824 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002825
John McCall0b7e6782011-03-24 11:26:52 +00002826 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002827 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002828 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002829
Douglas Gregor23c94db2010-07-02 17:43:08 +00002830 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002831 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002832 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002833 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002834 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002835 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2836 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002837}
2838
Reid Spencer5f016e22007-07-11 17:01:13 +00002839/// ParseEnumSpecifier
2840/// enum-specifier: [C99 6.7.2.2]
2841/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002842///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002843/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2844/// '}' attributes[opt]
2845/// 'enum' identifier
2846/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002847///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002848/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2849/// [C++0x] enum-head '{' enumerator-list ',' '}'
2850///
2851/// enum-head: [C++0x]
2852/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2853/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2854///
2855/// enum-key: [C++0x]
2856/// 'enum'
2857/// 'enum' 'class'
2858/// 'enum' 'struct'
2859///
2860/// enum-base: [C++0x]
2861/// ':' type-specifier-seq
2862///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002863/// [C++] elaborated-type-specifier:
2864/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2865///
Chris Lattner4c97d762009-04-12 21:49:30 +00002866void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002867 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002868 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002869 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002870 if (Tok.is(tok::code_completion)) {
2871 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002872 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002873 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002874 }
John McCall57c13002011-07-06 05:58:41 +00002875
Richard Smithbdad7a22012-01-10 01:33:14 +00002876 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002877 bool IsScopedUsingClassTag = false;
2878
2879 if (getLang().CPlusPlus0x &&
2880 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002881 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002882 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002883 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002884 }
Douglas Gregor374929f2009-09-18 15:37:17 +00002885
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002886 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002887 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002888 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002889
Douglas Gregor5471bc82011-09-08 17:18:35 +00002890 bool AllowFixedUnderlyingType
Francois Pichet62ec1f22011-09-17 17:15:52 +00002891 = getLang().CPlusPlus0x || getLang().MicrosoftExt || getLang().ObjC2;
John McCall57c13002011-07-06 05:58:41 +00002892
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002893 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002894 if (getLang().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002895 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2896 // if a fixed underlying type is allowed.
2897 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2898
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002899 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2900 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002901 return;
2902
2903 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002904 Diag(Tok, diag::err_expected_ident);
2905 if (Tok.isNot(tok::l_brace)) {
2906 // Has no name and is not a definition.
2907 // Skip the rest of this declarator, up until the comma or semicolon.
2908 SkipUntil(tok::comma, true);
2909 return;
2910 }
2911 }
2912 }
Mike Stump1eb44332009-09-09 15:08:12 +00002913
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002914 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002915 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2916 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002917 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002918
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002919 // Skip the rest of this declarator, up until the comma or semicolon.
2920 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002921 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002922 }
Mike Stump1eb44332009-09-09 15:08:12 +00002923
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002924 // If an identifier is present, consume and remember it.
2925 IdentifierInfo *Name = 0;
2926 SourceLocation NameLoc;
2927 if (Tok.is(tok::identifier)) {
2928 Name = Tok.getIdentifierInfo();
2929 NameLoc = ConsumeToken();
2930 }
Mike Stump1eb44332009-09-09 15:08:12 +00002931
Richard Smithbdad7a22012-01-10 01:33:14 +00002932 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002933 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2934 // declaration of a scoped enumeration.
2935 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00002936 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002937 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002938 }
2939
2940 TypeResult BaseType;
2941
Douglas Gregora61b3e72010-12-01 17:42:47 +00002942 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002943 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002944 bool PossibleBitfield = false;
2945 if (getCurScope()->getFlags() & Scope::ClassScope) {
2946 // If we're in class scope, this can either be an enum declaration with
2947 // an underlying type, or a declaration of a bitfield member. We try to
2948 // use a simple disambiguation scheme first to catch the common cases
2949 // (integer literal, sizeof); if it's still ambiguous, we then consider
2950 // anything that's a simple-type-specifier followed by '(' as an
2951 // expression. This suffices because function types are not valid
2952 // underlying types anyway.
2953 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2954 // If the next token starts an expression, we know we're parsing a
2955 // bit-field. This is the common case.
2956 if (TPR == TPResult::True())
2957 PossibleBitfield = true;
2958 // If the next token starts a type-specifier-seq, it may be either a
2959 // a fixed underlying type or the start of a function-style cast in C++;
2960 // lookahead one more token to see if it's obvious that we have a
2961 // fixed underlying type.
2962 else if (TPR == TPResult::False() &&
2963 GetLookAheadToken(2).getKind() == tok::semi) {
2964 // Consume the ':'.
2965 ConsumeToken();
2966 } else {
2967 // We have the start of a type-specifier-seq, so we have to perform
2968 // tentative parsing to determine whether we have an expression or a
2969 // type.
2970 TentativeParsingAction TPA(*this);
2971
2972 // Consume the ':'.
2973 ConsumeToken();
2974
Douglas Gregor86f208c2011-02-22 20:32:04 +00002975 if ((getLang().CPlusPlus &&
2976 isCXXDeclarationSpecifier() != TPResult::True()) ||
2977 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002978 // We'll parse this as a bitfield later.
2979 PossibleBitfield = true;
2980 TPA.Revert();
2981 } else {
2982 // We have a type-specifier-seq.
2983 TPA.Commit();
2984 }
2985 }
2986 } else {
2987 // Consume the ':'.
2988 ConsumeToken();
2989 }
2990
2991 if (!PossibleBitfield) {
2992 SourceRange Range;
2993 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002994
Douglas Gregor5471bc82011-09-08 17:18:35 +00002995 if (!getLang().CPlusPlus0x && !getLang().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002996 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2997 << Range;
Richard Smith7fe62082011-10-15 05:09:34 +00002998 if (getLang().CPlusPlus0x)
2999 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003000 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003001 }
3002
Richard Smithbdad7a22012-01-10 01:33:14 +00003003 // There are four options here. If we have 'friend enum foo;' then this is a
3004 // friend declaration, and cannot have an accompanying definition. If we have
3005 // 'enum foo;', then this is a forward declaration. If we have
3006 // 'enum foo {...' then this is a definition. Otherwise we have something
3007 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003008 //
3009 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3010 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3011 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3012 //
John McCallf312b1e2010-08-26 23:41:50 +00003013 Sema::TagUseKind TUK;
Richard Smithbdad7a22012-01-10 01:33:14 +00003014 if (DS.isFriendSpecified())
3015 TUK = Sema::TUK_Friend;
3016 else if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00003017 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003018 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00003019 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003020 else
John McCallf312b1e2010-08-26 23:41:50 +00003021 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003022
3023 // enums cannot be templates, although they can be referenced from a
3024 // template.
3025 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003026 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003027 Diag(Tok, diag::err_enum_template);
3028
3029 // Skip the rest of this declarator, up until the comma or semicolon.
3030 SkipUntil(tok::comma, true);
3031 return;
3032 }
3033
Douglas Gregorb9075602011-02-22 02:55:24 +00003034 if (!Name && TUK != Sema::TUK_Definition) {
3035 Diag(Tok, diag::err_enumerator_unnamed_no_def);
3036
3037 // Skip the rest of this declarator, up until the comma or semicolon.
3038 SkipUntil(tok::comma, true);
3039 return;
3040 }
3041
Douglas Gregor402abb52009-05-28 23:31:59 +00003042 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003043 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003044 const char *PrevSpec = 0;
3045 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003046 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003047 StartLoc, SS, Name, NameLoc, attrs.getList(),
Douglas Gregore7612302011-09-09 19:05:14 +00003048 AS, DS.getModulePrivateSpecLoc(),
John McCallf312b1e2010-08-26 23:41:50 +00003049 MultiTemplateParamsArg(Actions),
Richard Smithbdad7a22012-01-10 01:33:14 +00003050 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003051 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003052
Douglas Gregor48c89f42010-04-24 16:38:41 +00003053 if (IsDependent) {
3054 // This enum has a dependent nested-name-specifier. Handle it as a
3055 // dependent tag.
3056 if (!Name) {
3057 DS.SetTypeSpecError();
3058 Diag(Tok, diag::err_expected_type_name_after_typename);
3059 return;
3060 }
3061
Douglas Gregor23c94db2010-07-02 17:43:08 +00003062 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003063 TUK, SS, Name, StartLoc,
3064 NameLoc);
3065 if (Type.isInvalid()) {
3066 DS.SetTypeSpecError();
3067 return;
3068 }
3069
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003070 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3071 NameLoc.isValid() ? NameLoc : StartLoc,
3072 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003073 Diag(StartLoc, DiagID) << PrevSpec;
3074
3075 return;
3076 }
Mike Stump1eb44332009-09-09 15:08:12 +00003077
John McCalld226f652010-08-21 09:40:31 +00003078 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003079 // The action failed to produce an enumeration tag. If this is a
3080 // definition, consume the entire definition.
3081 if (Tok.is(tok::l_brace)) {
3082 ConsumeBrace();
3083 SkipUntil(tok::r_brace);
3084 }
3085
3086 DS.SetTypeSpecError();
3087 return;
3088 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003089
3090 if (Tok.is(tok::l_brace)) {
3091 if (TUK == Sema::TUK_Friend)
3092 Diag(Tok, diag::err_friend_decl_defines_type)
3093 << SourceRange(DS.getFriendSpecLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +00003094 ParseEnumBody(StartLoc, TagDecl);
Richard Smithbdad7a22012-01-10 01:33:14 +00003095 }
Mike Stump1eb44332009-09-09 15:08:12 +00003096
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003097 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3098 NameLoc.isValid() ? NameLoc : StartLoc,
3099 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003100 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003101}
3102
3103/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3104/// enumerator-list:
3105/// enumerator
3106/// enumerator-list ',' enumerator
3107/// enumerator:
3108/// enumeration-constant
3109/// enumeration-constant '=' constant-expression
3110/// enumeration-constant:
3111/// identifier
3112///
John McCalld226f652010-08-21 09:40:31 +00003113void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003114 // Enter the scope of the enum body and start the definition.
3115 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003116 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003117
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003118 BalancedDelimiterTracker T(*this, tok::l_brace);
3119 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003120
Chris Lattner7946dd32007-08-27 17:24:30 +00003121 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00003122 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003123 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003124
Chris Lattner5f9e2722011-07-23 10:55:15 +00003125 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003126
John McCalld226f652010-08-21 09:40:31 +00003127 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003128
Reid Spencer5f016e22007-07-11 17:01:13 +00003129 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003130 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003131 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3132 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003133
John McCall5b629aa2010-10-22 23:36:17 +00003134 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003135 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003136 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003137
Reid Spencer5f016e22007-07-11 17:01:13 +00003138 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003139 ExprResult AssignedVal;
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003140 ParsingDeclRAIIObject PD(*this);
3141
Chris Lattner04d66662007-10-09 17:33:22 +00003142 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003143 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003144 AssignedVal = ParseConstantExpression();
3145 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003146 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003147 }
Mike Stump1eb44332009-09-09 15:08:12 +00003148
Reid Spencer5f016e22007-07-11 17:01:13 +00003149 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003150 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3151 LastEnumConstDecl,
3152 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003153 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003154 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003155 PD.complete(EnumConstDecl);
3156
Reid Spencer5f016e22007-07-11 17:01:13 +00003157 EnumConstantDecls.push_back(EnumConstDecl);
3158 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003159
Douglas Gregor751f6922010-09-07 14:51:08 +00003160 if (Tok.is(tok::identifier)) {
3161 // We're missing a comma between enumerators.
3162 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3163 Diag(Loc, diag::err_enumerator_list_missing_comma)
3164 << FixItHint::CreateInsertion(Loc, ", ");
3165 continue;
3166 }
3167
Chris Lattner04d66662007-10-09 17:33:22 +00003168 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003169 break;
3170 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003171
Richard Smith7fe62082011-10-15 05:09:34 +00003172 if (Tok.isNot(tok::identifier)) {
3173 if (!getLang().C99 && !getLang().CPlusPlus0x)
3174 Diag(CommaLoc, diag::ext_enumerator_list_comma)
3175 << getLang().CPlusPlus
3176 << FixItHint::CreateRemoval(CommaLoc);
3177 else if (getLang().CPlusPlus0x)
3178 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3179 << FixItHint::CreateRemoval(CommaLoc);
3180 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003181 }
Mike Stump1eb44332009-09-09 15:08:12 +00003182
Reid Spencer5f016e22007-07-11 17:01:13 +00003183 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003184 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003185
Reid Spencer5f016e22007-07-11 17:01:13 +00003186 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003187 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003188 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003189
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003190 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3191 EnumDecl, EnumConstantDecls.data(),
3192 EnumConstantDecls.size(), getCurScope(),
3193 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003194
Douglas Gregor72de6672009-01-08 20:45:30 +00003195 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003196 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3197 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003198}
3199
3200/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003201/// start of a type-qualifier-list.
3202bool Parser::isTypeQualifier() const {
3203 switch (Tok.getKind()) {
3204 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003205
3206 // type-qualifier only in OpenCL
3207 case tok::kw_private:
3208 return getLang().OpenCL;
3209
Steve Naroff5f8aa692008-02-11 23:15:56 +00003210 // type-qualifier
3211 case tok::kw_const:
3212 case tok::kw_volatile:
3213 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003214 case tok::kw___private:
3215 case tok::kw___local:
3216 case tok::kw___global:
3217 case tok::kw___constant:
3218 case tok::kw___read_only:
3219 case tok::kw___read_write:
3220 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003221 return true;
3222 }
3223}
3224
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003225/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3226/// is definitely a type-specifier. Return false if it isn't part of a type
3227/// specifier or if we're not sure.
3228bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3229 switch (Tok.getKind()) {
3230 default: return false;
3231 // type-specifiers
3232 case tok::kw_short:
3233 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003234 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003235 case tok::kw_signed:
3236 case tok::kw_unsigned:
3237 case tok::kw__Complex:
3238 case tok::kw__Imaginary:
3239 case tok::kw_void:
3240 case tok::kw_char:
3241 case tok::kw_wchar_t:
3242 case tok::kw_char16_t:
3243 case tok::kw_char32_t:
3244 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003245 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003246 case tok::kw_float:
3247 case tok::kw_double:
3248 case tok::kw_bool:
3249 case tok::kw__Bool:
3250 case tok::kw__Decimal32:
3251 case tok::kw__Decimal64:
3252 case tok::kw__Decimal128:
3253 case tok::kw___vector:
3254
3255 // struct-or-union-specifier (C99) or class-specifier (C++)
3256 case tok::kw_class:
3257 case tok::kw_struct:
3258 case tok::kw_union:
3259 // enum-specifier
3260 case tok::kw_enum:
3261
3262 // typedef-name
3263 case tok::annot_typename:
3264 return true;
3265 }
3266}
3267
Steve Naroff5f8aa692008-02-11 23:15:56 +00003268/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003269/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003270bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003271 switch (Tok.getKind()) {
3272 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003273
Chris Lattner166a8fc2009-01-04 23:41:41 +00003274 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003275 if (TryAltiVecVectorToken())
3276 return true;
3277 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003278 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003279 // Annotate typenames and C++ scope specifiers. If we get one, just
3280 // recurse to handle whatever we get.
3281 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003282 return true;
3283 if (Tok.is(tok::identifier))
3284 return false;
3285 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003286
Chris Lattner166a8fc2009-01-04 23:41:41 +00003287 case tok::coloncolon: // ::foo::bar
3288 if (NextToken().is(tok::kw_new) || // ::new
3289 NextToken().is(tok::kw_delete)) // ::delete
3290 return false;
3291
Chris Lattner166a8fc2009-01-04 23:41:41 +00003292 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003293 return true;
3294 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003295
Reid Spencer5f016e22007-07-11 17:01:13 +00003296 // GNU attributes support.
3297 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003298 // GNU typeof support.
3299 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003300
Reid Spencer5f016e22007-07-11 17:01:13 +00003301 // type-specifiers
3302 case tok::kw_short:
3303 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003304 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003305 case tok::kw_signed:
3306 case tok::kw_unsigned:
3307 case tok::kw__Complex:
3308 case tok::kw__Imaginary:
3309 case tok::kw_void:
3310 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003311 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003312 case tok::kw_char16_t:
3313 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003314 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003315 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003316 case tok::kw_float:
3317 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003318 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003319 case tok::kw__Bool:
3320 case tok::kw__Decimal32:
3321 case tok::kw__Decimal64:
3322 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003323 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003324
Chris Lattner99dc9142008-04-13 18:59:07 +00003325 // struct-or-union-specifier (C99) or class-specifier (C++)
3326 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003327 case tok::kw_struct:
3328 case tok::kw_union:
3329 // enum-specifier
3330 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003331
Reid Spencer5f016e22007-07-11 17:01:13 +00003332 // type-qualifier
3333 case tok::kw_const:
3334 case tok::kw_volatile:
3335 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003336
3337 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003338 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003339 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003340
Chris Lattner7c186be2008-10-20 00:25:30 +00003341 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3342 case tok::less:
3343 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003344
Steve Naroff239f0732008-12-25 14:16:32 +00003345 case tok::kw___cdecl:
3346 case tok::kw___stdcall:
3347 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003348 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003349 case tok::kw___w64:
3350 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003351 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003352 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003353 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003354
3355 case tok::kw___private:
3356 case tok::kw___local:
3357 case tok::kw___global:
3358 case tok::kw___constant:
3359 case tok::kw___read_only:
3360 case tok::kw___read_write:
3361 case tok::kw___write_only:
3362
Eli Friedman290eeb02009-06-08 23:27:34 +00003363 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003364
3365 case tok::kw_private:
3366 return getLang().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003367
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003368 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003369 case tok::kw__Atomic:
3370 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003371 }
3372}
3373
3374/// isDeclarationSpecifier() - Return true if the current token is part of a
3375/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003376///
3377/// \param DisambiguatingWithExpression True to indicate that the purpose of
3378/// this check is to disambiguate between an expression and a declaration.
3379bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003380 switch (Tok.getKind()) {
3381 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003382
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003383 case tok::kw_private:
3384 return getLang().OpenCL;
3385
Chris Lattner166a8fc2009-01-04 23:41:41 +00003386 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003387 // Unfortunate hack to support "Class.factoryMethod" notation.
3388 if (getLang().ObjC1 && NextToken().is(tok::period))
3389 return false;
John Thompson82287d12010-02-05 00:12:22 +00003390 if (TryAltiVecVectorToken())
3391 return true;
3392 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003393 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003394 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003395 // Annotate typenames and C++ scope specifiers. If we get one, just
3396 // recurse to handle whatever we get.
3397 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003398 return true;
3399 if (Tok.is(tok::identifier))
3400 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003401
3402 // If we're in Objective-C and we have an Objective-C class type followed
3403 // by an identifier and then either ':' or ']', in a place where an
3404 // expression is permitted, then this is probably a class message send
3405 // missing the initial '['. In this case, we won't consider this to be
3406 // the start of a declaration.
3407 if (DisambiguatingWithExpression &&
3408 isStartOfObjCClassMessageMissingOpenBracket())
3409 return false;
3410
John McCall9ba61662010-02-26 08:45:28 +00003411 return isDeclarationSpecifier();
3412
Chris Lattner166a8fc2009-01-04 23:41:41 +00003413 case tok::coloncolon: // ::foo::bar
3414 if (NextToken().is(tok::kw_new) || // ::new
3415 NextToken().is(tok::kw_delete)) // ::delete
3416 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003417
Chris Lattner166a8fc2009-01-04 23:41:41 +00003418 // Annotate typenames and C++ scope specifiers. If we get one, just
3419 // recurse to handle whatever we get.
3420 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003421 return true;
3422 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003423
Reid Spencer5f016e22007-07-11 17:01:13 +00003424 // storage-class-specifier
3425 case tok::kw_typedef:
3426 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003427 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003428 case tok::kw_static:
3429 case tok::kw_auto:
3430 case tok::kw_register:
3431 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003432
Douglas Gregor8d267c52011-09-09 02:06:17 +00003433 // Modules
3434 case tok::kw___module_private__:
3435
Reid Spencer5f016e22007-07-11 17:01:13 +00003436 // type-specifiers
3437 case tok::kw_short:
3438 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003439 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003440 case tok::kw_signed:
3441 case tok::kw_unsigned:
3442 case tok::kw__Complex:
3443 case tok::kw__Imaginary:
3444 case tok::kw_void:
3445 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003446 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003447 case tok::kw_char16_t:
3448 case tok::kw_char32_t:
3449
Reid Spencer5f016e22007-07-11 17:01:13 +00003450 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003451 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003452 case tok::kw_float:
3453 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003454 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003455 case tok::kw__Bool:
3456 case tok::kw__Decimal32:
3457 case tok::kw__Decimal64:
3458 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003459 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003460
Chris Lattner99dc9142008-04-13 18:59:07 +00003461 // struct-or-union-specifier (C99) or class-specifier (C++)
3462 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003463 case tok::kw_struct:
3464 case tok::kw_union:
3465 // enum-specifier
3466 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003467
Reid Spencer5f016e22007-07-11 17:01:13 +00003468 // type-qualifier
3469 case tok::kw_const:
3470 case tok::kw_volatile:
3471 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003472
Reid Spencer5f016e22007-07-11 17:01:13 +00003473 // function-specifier
3474 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003475 case tok::kw_virtual:
3476 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003477
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003478 // static_assert-declaration
3479 case tok::kw__Static_assert:
3480
Chris Lattner1ef08762007-08-09 17:01:07 +00003481 // GNU typeof support.
3482 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003483
Chris Lattner1ef08762007-08-09 17:01:07 +00003484 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003485 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003486 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003487
Francois Pichete3d49b42011-06-19 08:02:06 +00003488 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003489 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003490 return true;
3491
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003492 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003493 case tok::kw__Atomic:
3494 return true;
3495
Chris Lattnerf3948c42008-07-26 03:38:44 +00003496 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3497 case tok::less:
3498 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003499
Douglas Gregord9d75e52011-04-27 05:41:15 +00003500 // typedef-name
3501 case tok::annot_typename:
3502 return !DisambiguatingWithExpression ||
3503 !isStartOfObjCClassMessageMissingOpenBracket();
3504
Steve Naroff47f52092009-01-06 19:34:12 +00003505 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003506 case tok::kw___cdecl:
3507 case tok::kw___stdcall:
3508 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003509 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003510 case tok::kw___w64:
3511 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003512 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003513 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003514 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003515 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003516
3517 case tok::kw___private:
3518 case tok::kw___local:
3519 case tok::kw___global:
3520 case tok::kw___constant:
3521 case tok::kw___read_only:
3522 case tok::kw___read_write:
3523 case tok::kw___write_only:
3524
Eli Friedman290eeb02009-06-08 23:27:34 +00003525 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003526 }
3527}
3528
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003529bool Parser::isConstructorDeclarator() {
3530 TentativeParsingAction TPA(*this);
3531
3532 // Parse the C++ scope specifier.
3533 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003534 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3535 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003536 TPA.Revert();
3537 return false;
3538 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003539
3540 // Parse the constructor name.
3541 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3542 // We already know that we have a constructor name; just consume
3543 // the token.
3544 ConsumeToken();
3545 } else {
3546 TPA.Revert();
3547 return false;
3548 }
3549
3550 // Current class name must be followed by a left parentheses.
3551 if (Tok.isNot(tok::l_paren)) {
3552 TPA.Revert();
3553 return false;
3554 }
3555 ConsumeParen();
3556
3557 // A right parentheses or ellipsis signals that we have a constructor.
3558 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3559 TPA.Revert();
3560 return true;
3561 }
3562
3563 // If we need to, enter the specified scope.
3564 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003565 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003566 DeclScopeObj.EnterDeclaratorScope();
3567
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003568 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003569 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003570 MaybeParseMicrosoftAttributes(Attrs);
3571
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003572 // Check whether the next token(s) are part of a declaration
3573 // specifier, in which case we have the start of a parameter and,
3574 // therefore, we know that this is a constructor.
3575 bool IsConstructor = isDeclarationSpecifier();
3576 TPA.Revert();
3577 return IsConstructor;
3578}
Reid Spencer5f016e22007-07-11 17:01:13 +00003579
3580/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003581/// type-qualifier-list: [C99 6.7.5]
3582/// type-qualifier
3583/// [vendor] attributes
3584/// [ only if VendorAttributesAllowed=true ]
3585/// type-qualifier-list type-qualifier
3586/// [vendor] type-qualifier-list attributes
3587/// [ only if VendorAttributesAllowed=true ]
3588/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3589/// [ only if CXX0XAttributesAllowed=true ]
3590/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003591///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003592void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3593 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003594 bool CXX0XAttributesAllowed) {
3595 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3596 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003597 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003598 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003599 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003600 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003601 else
3602 Diag(Loc, diag::err_attributes_not_allowed);
3603 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003604
3605 SourceLocation EndLoc;
3606
Reid Spencer5f016e22007-07-11 17:01:13 +00003607 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003608 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003609 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003610 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003611 SourceLocation Loc = Tok.getLocation();
3612
3613 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003614 case tok::code_completion:
3615 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003616 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003617
Reid Spencer5f016e22007-07-11 17:01:13 +00003618 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003619 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3620 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003621 break;
3622 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003623 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3624 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003625 break;
3626 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003627 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3628 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003629 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003630
3631 // OpenCL qualifiers:
3632 case tok::kw_private:
3633 if (!getLang().OpenCL)
3634 goto DoneWithTypeQuals;
3635 case tok::kw___private:
3636 case tok::kw___global:
3637 case tok::kw___local:
3638 case tok::kw___constant:
3639 case tok::kw___read_only:
3640 case tok::kw___write_only:
3641 case tok::kw___read_write:
3642 ParseOpenCLQualifiers(DS);
3643 break;
3644
Eli Friedman290eeb02009-06-08 23:27:34 +00003645 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003646 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003647 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003648 case tok::kw___cdecl:
3649 case tok::kw___stdcall:
3650 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003651 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003652 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003653 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003654 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003655 continue;
3656 }
3657 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003658 case tok::kw___pascal:
3659 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003660 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003661 continue;
3662 }
3663 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003664 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003665 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003666 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003667 continue; // do *not* consume the next token!
3668 }
3669 // otherwise, FALL THROUGH!
3670 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003671 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003672 // If this is not a type-qualifier token, we're done reading type
3673 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003674 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003675 if (EndLoc.isValid())
3676 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003677 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003678 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003679
Reid Spencer5f016e22007-07-11 17:01:13 +00003680 // If the specifier combination wasn't legal, issue a diagnostic.
3681 if (isInvalid) {
3682 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003683 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003684 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003685 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003686 }
3687}
3688
3689
3690/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3691///
3692void Parser::ParseDeclarator(Declarator &D) {
3693 /// This implements the 'declarator' production in the C grammar, then checks
3694 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003695 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003696}
3697
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003698/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3699/// is parsed by the function passed to it. Pass null, and the direct-declarator
3700/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003701/// ptr-operator production.
3702///
Richard Smith0706df42011-10-19 21:33:05 +00003703/// If the grammar of this construct is extended, matching changes must also be
3704/// made to TryParseDeclarator and MightBeDeclarator.
3705///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003706/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3707/// [C] pointer[opt] direct-declarator
3708/// [C++] direct-declarator
3709/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003710///
3711/// pointer: [C99 6.7.5]
3712/// '*' type-qualifier-list[opt]
3713/// '*' type-qualifier-list[opt] pointer
3714///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003715/// ptr-operator:
3716/// '*' cv-qualifier-seq[opt]
3717/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003718/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003719/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003720/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003721/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003722void Parser::ParseDeclaratorInternal(Declarator &D,
3723 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003724 if (Diags.hasAllExtensionsSilenced())
3725 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003726
Sebastian Redlf30208a2009-01-24 21:16:55 +00003727 // C++ member pointers start with a '::' or a nested-name.
3728 // Member pointers get special handling, since there's no place for the
3729 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003730 if (getLang().CPlusPlus &&
3731 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3732 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003733 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3734 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003735 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003736 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003737
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003738 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003739 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003740 // The scope spec really belongs to the direct-declarator.
3741 D.getCXXScopeSpec() = SS;
3742 if (DirectDeclParser)
3743 (this->*DirectDeclParser)(D);
3744 return;
3745 }
3746
3747 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003748 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003749 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003750 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003751 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003752
3753 // Recurse to parse whatever is left.
3754 ParseDeclaratorInternal(D, DirectDeclParser);
3755
3756 // Sema will have to catch (syntactically invalid) pointers into global
3757 // scope. It has to catch pointers into namespace scope anyway.
3758 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003759 Loc),
3760 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003761 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003762 return;
3763 }
3764 }
3765
3766 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003767 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003768 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003769 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003770 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003771 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003772 if (DirectDeclParser)
3773 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003774 return;
3775 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003776
Sebastian Redl05532f22009-03-15 22:02:01 +00003777 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3778 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003779 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003780 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003781
Chris Lattner9af55002009-03-27 04:18:06 +00003782 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003783 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003784 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003785
Reid Spencer5f016e22007-07-11 17:01:13 +00003786 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003787 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003788
Reid Spencer5f016e22007-07-11 17:01:13 +00003789 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003790 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003791 if (Kind == tok::star)
3792 // Remember that we parsed a pointer type, and remember the type-quals.
3793 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003794 DS.getConstSpecLoc(),
3795 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003796 DS.getRestrictSpecLoc()),
3797 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003798 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003799 else
3800 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003801 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003802 Loc),
3803 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003804 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003805 } else {
3806 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003807 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003808
Sebastian Redl743de1f2009-03-23 00:00:23 +00003809 // Complain about rvalue references in C++03, but then go on and build
3810 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003811 if (Kind == tok::ampamp)
3812 Diag(Loc, getLang().CPlusPlus0x ?
3813 diag::warn_cxx98_compat_rvalue_reference :
3814 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003815
Reid Spencer5f016e22007-07-11 17:01:13 +00003816 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3817 // cv-qualifiers are introduced through the use of a typedef or of a
3818 // template type argument, in which case the cv-qualifiers are ignored.
3819 //
3820 // [GNU] Retricted references are allowed.
3821 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003822 // [C++0x] Attributes on references are not allowed.
3823 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003824 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003825
3826 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3827 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3828 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003829 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003830 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3831 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003832 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003833 }
3834
3835 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003836 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003837
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003838 if (D.getNumTypeObjects() > 0) {
3839 // C++ [dcl.ref]p4: There shall be no references to references.
3840 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3841 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003842 if (const IdentifierInfo *II = D.getIdentifier())
3843 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3844 << II;
3845 else
3846 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3847 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003848
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003849 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003850 // can go ahead and build the (technically ill-formed)
3851 // declarator: reference collapsing will take care of it.
3852 }
3853 }
3854
Reid Spencer5f016e22007-07-11 17:01:13 +00003855 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003856 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003857 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003858 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003859 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003860 }
3861}
3862
3863/// ParseDirectDeclarator
3864/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003865/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003866/// '(' declarator ')'
3867/// [GNU] '(' attributes declarator ')'
3868/// [C90] direct-declarator '[' constant-expression[opt] ']'
3869/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3870/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3871/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3872/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3873/// direct-declarator '(' parameter-type-list ')'
3874/// direct-declarator '(' identifier-list[opt] ')'
3875/// [GNU] direct-declarator '(' parameter-forward-declarations
3876/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003877/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3878/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003879/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003880///
3881/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003882/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003883/// '::'[opt] nested-name-specifier[opt] type-name
3884///
3885/// id-expression: [C++ 5.1]
3886/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003887/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003888///
3889/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003890/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003891/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003892/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003893/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003894/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003895///
Reid Spencer5f016e22007-07-11 17:01:13 +00003896void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003897 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003898
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003899 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3900 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003901 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003902 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3903 D.getContext() == Declarator::MemberContext;
3904 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3905 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003906 }
3907
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003908 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003909 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003910 // Change the declaration context for name lookup, until this function
3911 // is exited (and the declarator has been parsed).
3912 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003913 }
3914
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003915 // C++0x [dcl.fct]p14:
3916 // There is a syntactic ambiguity when an ellipsis occurs at the end
3917 // of a parameter-declaration-clause without a preceding comma. In
3918 // this case, the ellipsis is parsed as part of the
3919 // abstract-declarator if the type of the parameter names a template
3920 // parameter pack that has not been expanded; otherwise, it is parsed
3921 // as part of the parameter-declaration-clause.
3922 if (Tok.is(tok::ellipsis) &&
3923 !((D.getContext() == Declarator::PrototypeContext ||
3924 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003925 NextToken().is(tok::r_paren) &&
3926 !Actions.containsUnexpandedParameterPacks(D)))
3927 D.setEllipsisLoc(ConsumeToken());
3928
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003929 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3930 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3931 // We found something that indicates the start of an unqualified-id.
3932 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003933 bool AllowConstructorName;
3934 if (D.getDeclSpec().hasTypeSpecifier())
3935 AllowConstructorName = false;
3936 else if (D.getCXXScopeSpec().isSet())
3937 AllowConstructorName =
3938 (D.getContext() == Declarator::FileContext ||
3939 (D.getContext() == Declarator::MemberContext &&
3940 D.getDeclSpec().isFriendSpecified()));
3941 else
3942 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3943
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003944 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003945 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3946 /*EnteringContext=*/true,
3947 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003948 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003949 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003950 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003951 D.getName()) ||
3952 // Once we're past the identifier, if the scope was bad, mark the
3953 // whole declarator bad.
3954 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003955 D.SetIdentifier(0, Tok.getLocation());
3956 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003957 } else {
3958 // Parsed the unqualified-id; update range information and move along.
3959 if (D.getSourceRange().getBegin().isInvalid())
3960 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3961 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003962 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003963 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003964 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003965 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003966 assert(!getLang().CPlusPlus &&
3967 "There's a C++-specific check for tok::identifier above");
3968 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3969 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3970 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003971 goto PastIdentifier;
3972 }
3973
3974 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003975 // direct-declarator: '(' declarator ')'
3976 // direct-declarator: '(' attributes declarator ')'
3977 // Example: 'char (*X)' or 'int (*XX)(void)'
3978 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003979
3980 // If the declarator was parenthesized, we entered the declarator
3981 // scope when parsing the parenthesized declarator, then exited
3982 // the scope already. Re-enter the scope, if we need to.
3983 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003984 // If there was an error parsing parenthesized declarator, declarator
3985 // scope may have been enterred before. Don't do it again.
3986 if (!D.isInvalidType() &&
3987 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003988 // Change the declaration context for name lookup, until this function
3989 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003990 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003991 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003992 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003993 // This could be something simple like "int" (in which case the declarator
3994 // portion is empty), if an abstract-declarator is allowed.
3995 D.SetIdentifier(0, Tok.getLocation());
3996 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003997 if (D.getContext() == Declarator::MemberContext)
3998 Diag(Tok, diag::err_expected_member_name_or_semi)
3999 << D.getDeclSpec().getSourceRange();
4000 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00004001 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004002 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004003 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004004 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004005 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004006 }
Mike Stump1eb44332009-09-09 15:08:12 +00004007
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004008 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004009 assert(D.isPastIdentifier() &&
4010 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004011
Sean Huntbbd37c62009-11-21 08:43:09 +00004012 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00004013 if (D.getIdentifier())
4014 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004015
Reid Spencer5f016e22007-07-11 17:01:13 +00004016 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004017 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004018 // Enter function-declaration scope, limiting any declarators to the
4019 // function prototype scope, including parameter declarators.
4020 ParseScope PrototypeScope(this,
4021 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004022 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4023 // In such a case, check if we actually have a function declarator; if it
4024 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00004025 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4026 // When not in file scope, warn for ambiguous function declarators, just
4027 // in case the author intended it as a variable definition.
4028 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4029 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4030 break;
4031 }
John McCall0b7e6782011-03-24 11:26:52 +00004032 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004033 BalancedDelimiterTracker T(*this, tok::l_paren);
4034 T.consumeOpen();
4035 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004036 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004037 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004038 ParseBracketDeclarator(D);
4039 } else {
4040 break;
4041 }
4042 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00004043}
Reid Spencer5f016e22007-07-11 17:01:13 +00004044
Chris Lattneref4715c2008-04-06 05:45:57 +00004045/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4046/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004047/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004048/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4049///
4050/// direct-declarator:
4051/// '(' declarator ')'
4052/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004053/// direct-declarator '(' parameter-type-list ')'
4054/// direct-declarator '(' identifier-list[opt] ')'
4055/// [GNU] direct-declarator '(' parameter-forward-declarations
4056/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004057///
4058void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004059 BalancedDelimiterTracker T(*this, tok::l_paren);
4060 T.consumeOpen();
4061
Chris Lattneref4715c2008-04-06 05:45:57 +00004062 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004063
Chris Lattner7399ee02008-10-20 02:05:46 +00004064 // Eat any attributes before we look at whether this is a grouping or function
4065 // declarator paren. If this is a grouping paren, the attribute applies to
4066 // the type being built up, for example:
4067 // int (__attribute__(()) *x)(long y)
4068 // If this ends up not being a grouping paren, the attribute applies to the
4069 // first argument, for example:
4070 // int (__attribute__(()) int x)
4071 // In either case, we need to eat any attributes to be able to determine what
4072 // sort of paren this is.
4073 //
John McCall0b7e6782011-03-24 11:26:52 +00004074 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004075 bool RequiresArg = false;
4076 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004077 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004078
Chris Lattner7399ee02008-10-20 02:05:46 +00004079 // We require that the argument list (if this is a non-grouping paren) be
4080 // present even if the attribute list was empty.
4081 RequiresArg = true;
4082 }
Steve Naroff239f0732008-12-25 14:16:32 +00004083 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004084 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004085 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004086 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004087 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004088 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004089 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004090 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004091 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004092 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004093
Chris Lattneref4715c2008-04-06 05:45:57 +00004094 // If we haven't past the identifier yet (or where the identifier would be
4095 // stored, if this is an abstract declarator), then this is probably just
4096 // grouping parens. However, if this could be an abstract-declarator, then
4097 // this could also be the start of function arguments (consider 'void()').
4098 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004099
Chris Lattneref4715c2008-04-06 05:45:57 +00004100 if (!D.mayOmitIdentifier()) {
4101 // If this can't be an abstract-declarator, this *must* be a grouping
4102 // paren, because we haven't seen the identifier yet.
4103 isGrouping = true;
4104 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00004105 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00004106 isDeclarationSpecifier()) { // 'int(int)' is a function.
4107 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4108 // considered to be a type, not a K&R identifier-list.
4109 isGrouping = false;
4110 } else {
4111 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4112 isGrouping = true;
4113 }
Mike Stump1eb44332009-09-09 15:08:12 +00004114
Chris Lattneref4715c2008-04-06 05:45:57 +00004115 // If this is a grouping paren, handle:
4116 // direct-declarator: '(' declarator ')'
4117 // direct-declarator: '(' attributes declarator ')'
4118 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004119 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004120 D.setGroupingParens(true);
4121
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004122 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004123 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004124 T.consumeClose();
4125 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4126 T.getCloseLocation()),
4127 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004128
4129 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00004130 return;
4131 }
Mike Stump1eb44332009-09-09 15:08:12 +00004132
Chris Lattneref4715c2008-04-06 05:45:57 +00004133 // Okay, if this wasn't a grouping paren, it must be the start of a function
4134 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004135 // identifier (and remember where it would have been), then call into
4136 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004137 D.SetIdentifier(0, Tok.getLocation());
4138
David Blaikie42d6d0c2011-12-04 05:04:18 +00004139 // Enter function-declaration scope, limiting any declarators to the
4140 // function prototype scope, including parameter declarators.
4141 ParseScope PrototypeScope(this,
4142 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004143 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004144 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004145}
4146
4147/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4148/// declarator D up to a paren, which indicates that we are parsing function
4149/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004150///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004151/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00004152/// after the open paren - they should be considered to be the first argument of
4153/// a parameter. If RequiresArg is true, then the first argument of the
4154/// function is required to be present and required to not be an identifier
4155/// list.
4156///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004157/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
4158/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
4159/// (C++0x) trailing-return-type[opt].
4160///
4161/// [C++0x] exception-specification:
4162/// dynamic-exception-specification
4163/// noexcept-specification
4164///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004165void Parser::ParseFunctionDeclarator(Declarator &D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004166 ParsedAttributes &attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004167 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004168 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004169 assert(getCurScope()->isFunctionPrototypeScope() &&
4170 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004171 // lparen is already consumed!
4172 assert(D.isPastIdentifier() && "Should not call before identifier!");
4173
4174 // This should be true when the function has typed arguments.
4175 // Otherwise, it is treated as a K&R-style function.
4176 bool HasProto = false;
4177 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004178 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004179 // Remember where we see an ellipsis, if any.
4180 SourceLocation EllipsisLoc;
4181
4182 DeclSpec DS(AttrFactory);
4183 bool RefQualifierIsLValueRef = true;
4184 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004185 SourceLocation ConstQualifierLoc;
4186 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004187 ExceptionSpecificationType ESpecType = EST_None;
4188 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004189 SmallVector<ParsedType, 2> DynamicExceptions;
4190 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004191 ExprResult NoexceptExpr;
4192 ParsedType TrailingReturnType;
4193
4194 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004195 if (isFunctionDeclaratorIdentifierList()) {
4196 if (RequiresArg)
4197 Diag(Tok, diag::err_argument_required_after_attribute);
4198
4199 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4200
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004201 Tracker.consumeClose();
4202 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004203 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004204 if (Tok.isNot(tok::r_paren))
4205 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
4206 else if (RequiresArg)
4207 Diag(Tok, diag::err_argument_required_after_attribute);
4208
4209 HasProto = ParamInfo.size() || getLang().CPlusPlus;
4210
4211 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004212 Tracker.consumeClose();
4213 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004214
4215 if (getLang().CPlusPlus) {
4216 MaybeParseCXX0XAttributes(attrs);
4217
4218 // Parse cv-qualifier-seq[opt].
4219 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor43f51032011-10-19 06:04:55 +00004220 if (!DS.getSourceRange().getEnd().isInvalid()) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004221 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor43f51032011-10-19 06:04:55 +00004222 ConstQualifierLoc = DS.getConstSpecLoc();
4223 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4224 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004225
4226 // Parse ref-qualifier[opt].
4227 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004228 Diag(Tok, getLang().CPlusPlus0x ?
4229 diag::warn_cxx98_compat_ref_qualifier :
4230 diag::ext_ref_qualifier);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004231
4232 RefQualifierIsLValueRef = Tok.is(tok::amp);
4233 RefQualifierLoc = ConsumeToken();
4234 EndLoc = RefQualifierLoc;
4235 }
4236
4237 // Parse exception-specification[opt].
4238 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4239 DynamicExceptions,
4240 DynamicExceptionRanges,
4241 NoexceptExpr);
4242 if (ESpecType != EST_None)
4243 EndLoc = ESpecRange.getEnd();
4244
4245 // Parse trailing-return-type[opt].
4246 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004247 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004248 SourceRange Range;
4249 TrailingReturnType = ParseTrailingReturnType(Range).get();
4250 if (Range.getEnd().isValid())
4251 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004252 }
4253 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004254 }
4255
4256 // Remember that we parsed a function type, and remember the attributes.
4257 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4258 /*isVariadic=*/EllipsisLoc.isValid(),
4259 EllipsisLoc,
4260 ParamInfo.data(), ParamInfo.size(),
4261 DS.getTypeQualifiers(),
4262 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004263 RefQualifierLoc, ConstQualifierLoc,
4264 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004265 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004266 ESpecType, ESpecRange.getBegin(),
4267 DynamicExceptions.data(),
4268 DynamicExceptionRanges.data(),
4269 DynamicExceptions.size(),
4270 NoexceptExpr.isUsable() ?
4271 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004272 Tracker.getOpenLocation(),
4273 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004274 TrailingReturnType),
4275 attrs, EndLoc);
4276}
4277
4278/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4279/// identifier list form for a K&R-style function: void foo(a,b,c)
4280///
4281/// Note that identifier-lists are only allowed for normal declarators, not for
4282/// abstract-declarators.
4283bool Parser::isFunctionDeclaratorIdentifierList() {
4284 return !getLang().CPlusPlus
4285 && Tok.is(tok::identifier)
4286 && !TryAltiVecVectorToken()
4287 // K&R identifier lists can't have typedefs as identifiers, per C99
4288 // 6.7.5.3p11.
4289 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4290 // Identifier lists follow a really simple grammar: the identifiers can
4291 // be followed *only* by a ", identifier" or ")". However, K&R
4292 // identifier lists are really rare in the brave new modern world, and
4293 // it is very common for someone to typo a type in a non-K&R style
4294 // list. If we are presented with something like: "void foo(intptr x,
4295 // float y)", we don't want to start parsing the function declarator as
4296 // though it is a K&R style declarator just because intptr is an
4297 // invalid type.
4298 //
4299 // To handle this, we check to see if the token after the first
4300 // identifier is a "," or ")". Only then do we parse it as an
4301 // identifier list.
4302 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4303}
4304
4305/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4306/// we found a K&R-style identifier list instead of a typed parameter list.
4307///
4308/// After returning, ParamInfo will hold the parsed parameters.
4309///
4310/// identifier-list: [C99 6.7.5]
4311/// identifier
4312/// identifier-list ',' identifier
4313///
4314void Parser::ParseFunctionDeclaratorIdentifierList(
4315 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004316 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004317 // If there was no identifier specified for the declarator, either we are in
4318 // an abstract-declarator, or we are in a parameter declarator which was found
4319 // to be abstract. In abstract-declarators, identifier lists are not valid:
4320 // diagnose this.
4321 if (!D.getIdentifier())
4322 Diag(Tok, diag::ext_ident_list_in_param);
4323
4324 // Maintain an efficient lookup of params we have seen so far.
4325 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4326
4327 while (1) {
4328 // If this isn't an identifier, report the error and skip until ')'.
4329 if (Tok.isNot(tok::identifier)) {
4330 Diag(Tok, diag::err_expected_ident);
4331 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4332 // Forget we parsed anything.
4333 ParamInfo.clear();
4334 return;
4335 }
4336
4337 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4338
4339 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4340 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4341 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4342
4343 // Verify that the argument identifier has not already been mentioned.
4344 if (!ParamsSoFar.insert(ParmII)) {
4345 Diag(Tok, diag::err_param_redefinition) << ParmII;
4346 } else {
4347 // Remember this identifier in ParamInfo.
4348 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4349 Tok.getLocation(),
4350 0));
4351 }
4352
4353 // Eat the identifier.
4354 ConsumeToken();
4355
4356 // The list continues if we see a comma.
4357 if (Tok.isNot(tok::comma))
4358 break;
4359 ConsumeToken();
4360 }
4361}
4362
4363/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4364/// after the opening parenthesis. This function will not parse a K&R-style
4365/// identifier list.
4366///
4367/// D is the declarator being parsed. If attrs is non-null, then the caller
4368/// parsed those arguments immediately after the open paren - they should be
4369/// considered to be the first argument of a parameter.
4370///
4371/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4372/// be the location of the ellipsis, if any was parsed.
4373///
Reid Spencer5f016e22007-07-11 17:01:13 +00004374/// parameter-type-list: [C99 6.7.5]
4375/// parameter-list
4376/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004377/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004378///
4379/// parameter-list: [C99 6.7.5]
4380/// parameter-declaration
4381/// parameter-list ',' parameter-declaration
4382///
4383/// parameter-declaration: [C99 6.7.5]
4384/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004385/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004386/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004387/// declaration-specifiers abstract-declarator[opt]
4388/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004389/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004390/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
4391///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004392void Parser::ParseParameterDeclarationClause(
4393 Declarator &D,
4394 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004395 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004396 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004397
Chris Lattnerf97409f2008-04-06 06:57:35 +00004398 while (1) {
4399 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00004400 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004401 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004402 }
Mike Stump1eb44332009-09-09 15:08:12 +00004403
Chris Lattnerf97409f2008-04-06 06:57:35 +00004404 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004405 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004406 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004407
John McCall7f040a92010-12-24 02:08:15 +00004408 // Skip any Microsoft attributes before a param.
Francois Pichet62ec1f22011-09-17 17:15:52 +00004409 if (getLang().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004410 ParseMicrosoftAttributes(DS.getAttributes());
4411
4412 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004413
4414 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004415 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004416 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4417 // attributes lost? Should they even be allowed?
4418 // FIXME: If we can leave the attributes in the token stream somehow, we can
4419 // get rid of a parameter (attrs) and this statement. It might be too much
4420 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004421 DS.takeAttributesFrom(attrs);
4422
Chris Lattnere64c5492009-02-27 18:38:20 +00004423 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004424
Chris Lattnerf97409f2008-04-06 06:57:35 +00004425 // Parse the declarator. This is "PrototypeContext", because we must
4426 // accept either 'declarator' or 'abstract-declarator' here.
4427 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4428 ParseDeclarator(ParmDecl);
4429
4430 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004431 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004432
Chris Lattnerf97409f2008-04-06 06:57:35 +00004433 // Remember this parsed parameter in ParamInfo.
4434 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004435
Douglas Gregor72b505b2008-12-16 21:30:33 +00004436 // DefArgToks is used when the parsing of default arguments needs
4437 // to be delayed.
4438 CachedTokens *DefArgToks = 0;
4439
Chris Lattnerf97409f2008-04-06 06:57:35 +00004440 // If no parameter was specified, verify that *something* was specified,
4441 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004442 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4443 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004444 // Completely missing, emit error.
4445 Diag(DSStart, diag::err_missing_param);
4446 } else {
4447 // Otherwise, we have something. Add it and let semantic analysis try
4448 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004449
Chris Lattnerf97409f2008-04-06 06:57:35 +00004450 // Inform the actions module about the parameter declarator, so it gets
4451 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004452 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004453
4454 // Parse the default argument, if any. We parse the default
4455 // arguments in all dialects; the semantic analysis in
4456 // ActOnParamDefaultArgument will reject the default argument in
4457 // C.
4458 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004459 SourceLocation EqualLoc = Tok.getLocation();
4460
Chris Lattner04421082008-04-08 04:40:51 +00004461 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004462 if (D.getContext() == Declarator::MemberContext) {
4463 // If we're inside a class definition, cache the tokens
4464 // corresponding to the default argument. We'll actually parse
4465 // them when we see the end of the class definition.
4466 // FIXME: Templates will require something similar.
4467 // FIXME: Can we use a smart pointer for Toks?
4468 DefArgToks = new CachedTokens;
4469
Mike Stump1eb44332009-09-09 15:08:12 +00004470 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004471 /*StopAtSemi=*/true,
4472 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004473 delete DefArgToks;
4474 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004475 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004476 } else {
4477 // Mark the end of the default argument so that we know when to
4478 // stop when we parse it later on.
4479 Token DefArgEnd;
4480 DefArgEnd.startToken();
4481 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4482 DefArgEnd.setLocation(Tok.getLocation());
4483 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004484 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004485 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004486 }
Chris Lattner04421082008-04-08 04:40:51 +00004487 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004488 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004489 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004490
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004491 // The argument isn't actually potentially evaluated unless it is
4492 // used.
4493 EnterExpressionEvaluationContext Eval(Actions,
4494 Sema::PotentiallyEvaluatedIfUsed);
4495
John McCall60d7b3a2010-08-24 06:29:42 +00004496 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004497 if (DefArgResult.isInvalid()) {
4498 Actions.ActOnParamDefaultArgumentError(Param);
4499 SkipUntil(tok::comma, tok::r_paren, true, true);
4500 } else {
4501 // Inform the actions module about the default argument
4502 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004503 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004504 }
Chris Lattner04421082008-04-08 04:40:51 +00004505 }
4506 }
Mike Stump1eb44332009-09-09 15:08:12 +00004507
4508 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4509 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004510 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004511 }
4512
4513 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004514 if (Tok.isNot(tok::comma)) {
4515 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004516 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4517
4518 if (!getLang().CPlusPlus) {
4519 // We have ellipsis without a preceding ',', which is ill-formed
4520 // in C. Complain and provide the fix.
4521 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004522 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004523 }
4524 }
4525
4526 break;
4527 }
Mike Stump1eb44332009-09-09 15:08:12 +00004528
Chris Lattnerf97409f2008-04-06 06:57:35 +00004529 // Consume the comma.
4530 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004531 }
Mike Stump1eb44332009-09-09 15:08:12 +00004532
Chris Lattner66d28652008-04-06 06:34:08 +00004533}
Chris Lattneref4715c2008-04-06 05:45:57 +00004534
Reid Spencer5f016e22007-07-11 17:01:13 +00004535/// [C90] direct-declarator '[' constant-expression[opt] ']'
4536/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4537/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4538/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4539/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4540void Parser::ParseBracketDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004541 BalancedDelimiterTracker T(*this, tok::l_square);
4542 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004543
Chris Lattner378c7e42008-12-18 07:27:21 +00004544 // C array syntax has many features, but by-far the most common is [] and [4].
4545 // This code does a fast path to handle some of the most obvious cases.
4546 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004547 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004548 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004549 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004550
Chris Lattner378c7e42008-12-18 07:27:21 +00004551 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004552 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004553 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004554 T.getOpenLocation(),
4555 T.getCloseLocation()),
4556 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004557 return;
4558 } else if (Tok.getKind() == tok::numeric_constant &&
4559 GetLookAheadToken(1).is(tok::r_square)) {
4560 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004561 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004562 ConsumeToken();
4563
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004564 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004565 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004566 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004567
Chris Lattner378c7e42008-12-18 07:27:21 +00004568 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004569 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004570 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004571 T.getOpenLocation(),
4572 T.getCloseLocation()),
4573 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004574 return;
4575 }
Mike Stump1eb44332009-09-09 15:08:12 +00004576
Reid Spencer5f016e22007-07-11 17:01:13 +00004577 // If valid, this location is the position where we read the 'static' keyword.
4578 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004579 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004580 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004581
Reid Spencer5f016e22007-07-11 17:01:13 +00004582 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004583 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004584 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004585 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004586
Reid Spencer5f016e22007-07-11 17:01:13 +00004587 // If we haven't already read 'static', check to see if there is one after the
4588 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004589 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004590 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004591
Reid Spencer5f016e22007-07-11 17:01:13 +00004592 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4593 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004594 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004595
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004596 // Handle the case where we have '[*]' as the array size. However, a leading
4597 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4598 // the the token after the star is a ']'. Since stars in arrays are
4599 // infrequent, use of lookahead is not costly here.
4600 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004601 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004602
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004603 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004604 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004605 StaticLoc = SourceLocation(); // Drop the static.
4606 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004607 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004608 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004609 // Note, in C89, this production uses the constant-expr production instead
4610 // of assignment-expr. The only difference is that assignment-expr allows
4611 // things like '=' and '*='. Sema rejects these in C89 mode because they
4612 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004613
Douglas Gregore0762c92009-06-19 23:52:42 +00004614 // Parse the constant-expression or assignment-expression now (depending
4615 // on dialect).
Eli Friedman71b8fb52012-01-21 01:01:51 +00004616 if (getLang().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004617 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004618 } else {
4619 EnterExpressionEvaluationContext Unevaluated(Actions,
4620 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004621 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004622 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004623 }
Mike Stump1eb44332009-09-09 15:08:12 +00004624
Reid Spencer5f016e22007-07-11 17:01:13 +00004625 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004626 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004627 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004628 // If the expression was invalid, skip it.
4629 SkipUntil(tok::r_square);
4630 return;
4631 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004632
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004633 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004634
John McCall0b7e6782011-03-24 11:26:52 +00004635 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004636 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004637
Chris Lattner378c7e42008-12-18 07:27:21 +00004638 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004639 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004640 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004641 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004642 T.getOpenLocation(),
4643 T.getCloseLocation()),
4644 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004645}
4646
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004647/// [GNU] typeof-specifier:
4648/// typeof ( expressions )
4649/// typeof ( type-name )
4650/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004651///
4652void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004653 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004654 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004655 SourceLocation StartLoc = ConsumeToken();
4656
John McCallcfb708c2010-01-13 20:03:27 +00004657 const bool hasParens = Tok.is(tok::l_paren);
4658
Eli Friedman71b8fb52012-01-21 01:01:51 +00004659 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4660
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004661 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004662 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004663 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004664 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4665 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004666 if (hasParens)
4667 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004668
4669 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004670 // FIXME: Not accurate, the range gets one token more than it should.
4671 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004672 else
4673 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004674
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004675 if (isCastExpr) {
4676 if (!CastTy) {
4677 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004678 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004679 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004680
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004681 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004682 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004683 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4684 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004685 DiagID, CastTy))
4686 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004687 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004688 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004689
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004690 // If we get here, the operand to the typeof was an expresion.
4691 if (Operand.isInvalid()) {
4692 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004693 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004694 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004695
Eli Friedman71b8fb52012-01-21 01:01:51 +00004696 // We might need to transform the operand if it is potentially evaluated.
4697 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4698 if (Operand.isInvalid()) {
4699 DS.SetTypeSpecError();
4700 return;
4701 }
4702
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004703 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004704 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004705 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4706 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004707 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004708 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004709}
Chris Lattner1b492422010-02-28 18:33:55 +00004710
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004711/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004712/// _Atomic ( type-name )
4713///
4714void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4715 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4716
4717 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004718 BalancedDelimiterTracker T(*this, tok::l_paren);
4719 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004720 SkipUntil(tok::r_paren);
4721 return;
4722 }
4723
4724 TypeResult Result = ParseTypeName();
4725 if (Result.isInvalid()) {
4726 SkipUntil(tok::r_paren);
4727 return;
4728 }
4729
4730 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004731 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004732
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004733 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004734 return;
4735
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004736 DS.setTypeofParensRange(T.getRange());
4737 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004738
4739 const char *PrevSpec = 0;
4740 unsigned DiagID;
4741 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4742 DiagID, Result.release()))
4743 Diag(StartLoc, DiagID) << PrevSpec;
4744}
4745
Chris Lattner1b492422010-02-28 18:33:55 +00004746
4747/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4748/// from TryAltiVecVectorToken.
4749bool Parser::TryAltiVecVectorTokenOutOfLine() {
4750 Token Next = NextToken();
4751 switch (Next.getKind()) {
4752 default: return false;
4753 case tok::kw_short:
4754 case tok::kw_long:
4755 case tok::kw_signed:
4756 case tok::kw_unsigned:
4757 case tok::kw_void:
4758 case tok::kw_char:
4759 case tok::kw_int:
4760 case tok::kw_float:
4761 case tok::kw_double:
4762 case tok::kw_bool:
4763 case tok::kw___pixel:
4764 Tok.setKind(tok::kw___vector);
4765 return true;
4766 case tok::identifier:
4767 if (Next.getIdentifierInfo() == Ident_pixel) {
4768 Tok.setKind(tok::kw___vector);
4769 return true;
4770 }
4771 return false;
4772 }
4773}
4774
4775bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4776 const char *&PrevSpec, unsigned &DiagID,
4777 bool &isInvalid) {
4778 if (Tok.getIdentifierInfo() == Ident_vector) {
4779 Token Next = NextToken();
4780 switch (Next.getKind()) {
4781 case tok::kw_short:
4782 case tok::kw_long:
4783 case tok::kw_signed:
4784 case tok::kw_unsigned:
4785 case tok::kw_void:
4786 case tok::kw_char:
4787 case tok::kw_int:
4788 case tok::kw_float:
4789 case tok::kw_double:
4790 case tok::kw_bool:
4791 case tok::kw___pixel:
4792 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4793 return true;
4794 case tok::identifier:
4795 if (Next.getIdentifierInfo() == Ident_pixel) {
4796 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4797 return true;
4798 }
4799 break;
4800 default:
4801 break;
4802 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004803 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004804 DS.isTypeAltiVecVector()) {
4805 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4806 return true;
4807 }
4808 return false;
4809}