blob: 27b4919b3426de59bae8da624534e44bf4ded4b9 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Larisse Voufo725de3e2013-06-21 00:08:46 +000016#include "clang/AST/DeclTemplate.h"
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000017#include "clang/Basic/AddressSpaces.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000018#include "clang/Basic/CharInfo.h"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000019#include "clang/Basic/OpenCL.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrain031643e2012-04-26 23:36:17 +000021#include "clang/Sema/Lookup.h"
John McCall8b0666c2010-08-20 18:27:03 +000022#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000023#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/Sema/Scope.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000025#include "llvm/ADT/SmallSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000026#include "llvm/ADT/SmallString.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000027#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000028using namespace clang;
29
30//===----------------------------------------------------------------------===//
31// C99 6.7: Declarations.
32//===----------------------------------------------------------------------===//
33
Chris Lattnerf5fbd792006-08-10 23:56:11 +000034/// ParseTypeName
35/// type-name: [C99 6.7.6]
36/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000037///
38/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000039TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCall31168b02011-06-15 23:02:42 +000040 Declarator::TheContext Context,
Richard Smithcd1c0552011-07-01 19:46:12 +000041 AccessSpecifier AS,
Richard Smith54ecd982013-02-20 19:22:51 +000042 Decl **OwnedType,
43 ParsedAttributes *Attrs) {
Richard Smith62dad822012-03-15 01:02:11 +000044 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smith2f07ad52012-05-09 20:55:26 +000045 if (DSC == DSC_normal)
46 DSC = DSC_type_specifier;
Richard Smithbfdb1082012-03-12 08:56:40 +000047
Chris Lattnerf5fbd792006-08-10 23:56:11 +000048 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000049 DeclSpec DS(AttrFactory);
Richard Smith54ecd982013-02-20 19:22:51 +000050 if (Attrs)
51 DS.addAttributes(Attrs->getList());
Richard Smithbfdb1082012-03-12 08:56:40 +000052 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithcd1c0552011-07-01 19:46:12 +000053 if (OwnedType)
54 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redld6434562009-05-29 18:02:33 +000055
Chris Lattnerf5fbd792006-08-10 23:56:11 +000056 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000057 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000058 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000059 if (Range)
60 *Range = DeclaratorInfo.getSourceRange();
61
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000062 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000063 return true;
64
Douglas Gregor0be31a22010-07-02 17:43:08 +000065 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000066}
67
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000068
69/// isAttributeLateParsed - Return true if the attribute has arguments that
70/// require late parsing.
71static bool isAttributeLateParsed(const IdentifierInfo &II) {
72 return llvm::StringSwitch<bool>(II.getName())
73#include "clang/Parse/AttrLateParsed.inc"
74 .Default(false);
75}
76
Alexis Hunt96d5c762009-11-21 08:43:09 +000077/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000078///
79/// [GNU] attributes:
80/// attribute
81/// attributes attribute
82///
83/// [GNU] attribute:
84/// '__attribute__' '(' '(' attribute-list ')' ')'
85///
86/// [GNU] attribute-list:
87/// attrib
88/// attribute_list ',' attrib
89///
90/// [GNU] attrib:
91/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000092/// attrib-name
93/// attrib-name '(' identifier ')'
94/// attrib-name '(' identifier ',' nonempty-expr-list ')'
95/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000096///
Steve Naroff0f2fe172007-06-01 17:11:19 +000097/// [GNU] attrib-name:
98/// identifier
99/// typespec
100/// typequal
101/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +0000102///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000103/// Whether an attribute takes an 'identifier' is determined by the
104/// attrib-name. GCC's behavior here is not worth imitating:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000105///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000106/// * In C mode, if the attribute argument list starts with an identifier
107/// followed by a ',' or an ')', and the identifier doesn't resolve to
108/// a type, it is parsed as an identifier. If the attribute actually
109/// wanted an expression, it's out of luck (but it turns out that no
110/// attributes work that way, because C constant expressions are very
111/// limited).
112/// * In C++ mode, if the attribute argument list starts with an identifier,
113/// and the attribute *wants* an identifier, it is parsed as an identifier.
114/// At block scope, any additional tokens between the identifier and the
115/// ',' or ')' are ignored, otherwise they produce a parse error.
Richard Smithb12bf692011-10-17 21:20:17 +0000116///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000117/// We follow the C++ model, but don't allow junk after the identifier.
John McCall53fa7142010-12-24 02:08:15 +0000118void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000119 SourceLocation *endLoc,
120 LateParsedAttrList *LateAttrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000121 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000122
Chris Lattner76c72282007-10-09 17:33:22 +0000123 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000124 ConsumeToken();
125 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
126 "attribute")) {
127 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000128 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000129 }
130 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
131 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000132 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000133 }
134 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000135 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
136 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000137 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000138 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
139 ConsumeToken();
140 continue;
141 }
142 // we have an identifier or declaration specifier (const, int, etc.)
143 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
144 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000145
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000146 if (Tok.is(tok::l_paren)) {
147 // handle "parameterized" attributes
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000148 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000149 LateParsedAttribute *LA =
150 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
151 LateAttrs->push_back(LA);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000152
Bill Wendling44426052012-12-20 19:22:21 +0000153 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000154 // with other late-parsed declarations.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +0000155 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000156 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump11289f42009-09-09 15:08:12 +0000157
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000158 // consume everything up to and including the matching right parens
159 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000160
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000161 Token Eof;
162 Eof.startToken();
163 Eof.setLocation(Tok.getLocation());
164 LA->Toks.push_back(Eof);
165 } else {
Michael Han23214e52012-10-03 01:56:22 +0000166 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000167 0, SourceLocation(), AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000168 }
169 } else {
Aaron Ballman00e99962013-08-31 01:11:41 +0000170 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
171 AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000172 }
173 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000174 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Richard Smith66e71682013-10-24 01:07:54 +0000175 SkipUntil(tok::r_paren);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000176 SourceLocation Loc = Tok.getLocation();
Richard Smith66e71682013-10-24 01:07:54 +0000177 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
178 SkipUntil(tok::r_paren);
John McCall53fa7142010-12-24 02:08:15 +0000179 if (endLoc)
180 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000181 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000182}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000183
Richard Smith66e71682013-10-24 01:07:54 +0000184/// \brief Determine whether the given attribute has an identifier argument.
185static bool attributeHasIdentifierArg(const IdentifierInfo &II) {
186 StringRef Name = II.getName();
187 if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__"))
188 Name = Name.drop_front(2).drop_back(2);
189 return llvm::StringSwitch<bool>(Name)
190#include "clang/Parse/AttrIdentifierArg.inc"
Douglas Gregord2472d42013-05-02 23:25:32 +0000191 .Default(false);
192}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000193
Richard Smithfeefaf52013-09-03 18:01:40 +0000194IdentifierLoc *Parser::ParseIdentifierLoc() {
195 assert(Tok.is(tok::identifier) && "expected an identifier");
196 IdentifierLoc *IL = IdentifierLoc::create(Actions.Context,
197 Tok.getLocation(),
198 Tok.getIdentifierInfo());
199 ConsumeToken();
200 return IL;
201}
202
Richard Smithb1f9a282013-10-31 01:56:18 +0000203void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName,
204 SourceLocation AttrNameLoc,
205 ParsedAttributes &Attrs,
206 SourceLocation *EndLoc) {
207 BalancedDelimiterTracker Parens(*this, tok::l_paren);
208 Parens.consumeOpen();
209
210 TypeResult T;
211 if (Tok.isNot(tok::r_paren))
212 T = ParseTypeName();
213
214 if (Parens.consumeClose())
215 return;
216
217 if (T.isInvalid())
218 return;
219
220 if (T.isUsable())
221 Attrs.addNewTypeAttr(&AttrName,
222 SourceRange(AttrNameLoc, Parens.getCloseLocation()), 0,
223 AttrNameLoc, T.get(), AttributeList::AS_GNU);
224 else
225 Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()),
226 0, AttrNameLoc, 0, 0, AttributeList::AS_GNU);
227}
228
Michael Han23214e52012-10-03 01:56:22 +0000229/// Parse the arguments to a parameterized GNU attribute or
230/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000231void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
232 SourceLocation AttrNameLoc,
233 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000234 SourceLocation *EndLoc,
235 IdentifierInfo *ScopeName,
236 SourceLocation ScopeLoc,
237 AttributeList::Syntax Syntax) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000238
239 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
240
Richard Smith66e71682013-10-24 01:07:54 +0000241 AttributeList::Kind AttrKind =
Richard Smithb1f9a282013-10-31 01:56:18 +0000242 AttributeList::getKind(AttrName, ScopeName, Syntax);
Richard Smith66e71682013-10-24 01:07:54 +0000243
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000244 // Availability attributes have their own grammar.
Richard Smithb1f9a282013-10-31 01:56:18 +0000245 // FIXME: All these cases fail to pass in the syntax and scope, and might be
246 // written as C++11 gnu:: attributes.
Richard Smith66e71682013-10-24 01:07:54 +0000247 if (AttrKind == AttributeList::AT_Availability) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000248 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
249 return;
250 }
Richard Smithb1f9a282013-10-31 01:56:18 +0000251 // Thread safety attributes are parsed in an unevaluated context.
252 // FIXME: Share the bulk of the parsing code here and just pull out
253 // the unevaluated context.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000254 if (IsThreadSafetyAttribute(AttrName->getName())) {
255 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
256 return;
257 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000258 // Type safety attributes have their own grammar.
Richard Smith66e71682013-10-24 01:07:54 +0000259 if (AttrKind == AttributeList::AT_TypeTagForDatatype) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000260 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
261 return;
262 }
Richard Smithb1f9a282013-10-31 01:56:18 +0000263 // __attribute__((vec_type_hint)) and iboutletcollection expect a type arg.
264 if (AttrKind == AttributeList::AT_VecTypeHint ||
265 AttrKind == AttributeList::AT_IBOutletCollection) {
266 ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc);
267 return;
268 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000269
Richard Smith66e71682013-10-24 01:07:54 +0000270 // Ignore the left paren location for now.
271 ConsumeParen();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000272
Aaron Ballman00e99962013-08-31 01:11:41 +0000273 ArgsVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000274
Richard Smithb1f9a282013-10-31 01:56:18 +0000275 if (Tok.is(tok::identifier)) {
Richard Smith66e71682013-10-24 01:07:54 +0000276 // If this attribute wants an 'identifier' argument, make it so.
Richard Smithb1f9a282013-10-31 01:56:18 +0000277 bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName);
Richard Smith66e71682013-10-24 01:07:54 +0000278
279 // If we don't know how to parse this attribute, but this is the only
280 // token in this argument, assume it's meant to be an identifier.
281 if (AttrKind == AttributeList::UnknownAttribute) {
282 const Token &Next = NextToken();
Richard Smithb1f9a282013-10-31 01:56:18 +0000283 IsIdentifierArg = Next.is(tok::r_paren) || Next.is(tok::comma);
Richard Smith66e71682013-10-24 01:07:54 +0000284 }
Richard Smithb12bf692011-10-17 21:20:17 +0000285
Richard Smithb1f9a282013-10-31 01:56:18 +0000286 if (IsIdentifierArg)
287 ArgExprs.push_back(ParseIdentifierLoc());
Richard Smithb12bf692011-10-17 21:20:17 +0000288 }
289
Richard Smithb1f9a282013-10-31 01:56:18 +0000290 if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) {
Richard Smithb12bf692011-10-17 21:20:17 +0000291 // Eat the comma.
Aaron Ballman00e99962013-08-31 01:11:41 +0000292 if (!ArgExprs.empty())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000293 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000294
Richard Smithb12bf692011-10-17 21:20:17 +0000295 // Parse the non-empty comma-separated list of expressions.
296 while (1) {
297 ExprResult ArgExpr(ParseAssignmentExpression());
298 if (ArgExpr.isInvalid()) {
299 SkipUntil(tok::r_paren);
300 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000301 }
Richard Smithb12bf692011-10-17 21:20:17 +0000302 ArgExprs.push_back(ArgExpr.release());
303 if (Tok.isNot(tok::comma))
304 break;
305 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000306 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000307 }
Richard Smithb12bf692011-10-17 21:20:17 +0000308
309 SourceLocation RParen = Tok.getLocation();
Richard Smithb1f9a282013-10-31 01:56:18 +0000310 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
Michael Han360d2252012-10-04 16:42:52 +0000311 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithb1f9a282013-10-31 01:56:18 +0000312 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
313 ArgExprs.data(), ArgExprs.size(), Syntax);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000314 }
315}
316
Chad Rosierc1183952012-06-26 22:30:43 +0000317/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000318/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000319void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000320 SourceLocation AttrNameLoc,
321 ParsedAttributes &Attrs)
322{
323 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000324 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000325 AttrName->getNameStart(), tok::r_paren))
326 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000327
Aaron Ballman478faed2012-06-19 22:09:27 +0000328 ExprResult ArgExpr(ParseConstantExpression());
329 if (ArgExpr.isInvalid()) {
330 T.skipToEnd();
331 return;
332 }
Aaron Ballman00e99962013-08-31 01:11:41 +0000333 ArgsUnion ExprList = ArgExpr.take();
334 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1,
335 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000336
337 T.consumeClose();
338}
339
Chad Rosierc1183952012-06-26 22:30:43 +0000340/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000341/// arguments.
342bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
343 return llvm::StringSwitch<bool>(Ident->getName())
344 .Case("dllimport", true)
345 .Case("dllexport", true)
346 .Case("noreturn", true)
347 .Case("nothrow", true)
348 .Case("noinline", true)
349 .Case("naked", true)
350 .Case("appdomain", true)
351 .Case("process", true)
352 .Case("jitintrinsic", true)
353 .Case("noalias", true)
354 .Case("restrict", true)
355 .Case("novtable", true)
356 .Case("selectany", true)
357 .Case("thread", true)
Aaron Ballman444eb6e2013-05-04 16:58:37 +0000358 .Case("safebuffers", true )
Aaron Ballman478faed2012-06-19 22:09:27 +0000359 .Default(false);
360}
361
Chad Rosierc1183952012-06-26 22:30:43 +0000362/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000363/// parameters). Will return false if we properly handled the declspec, or
364/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000365void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000366 SourceLocation Loc,
367 ParsedAttributes &Attrs) {
368 // Try to handle the easy case first -- these declspecs all take a single
369 // parameter as their argument.
370 if (llvm::StringSwitch<bool>(Ident->getName())
371 .Case("uuid", true)
372 .Case("align", true)
373 .Case("allocate", true)
374 .Default(false)) {
375 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
376 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000377 // The deprecated declspec has an optional single argument, so we will
378 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000379 // not.
380 if (Tok.getKind() == tok::l_paren)
381 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
382 else
Aaron Ballman00e99962013-08-31 01:11:41 +0000383 Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000384 } else if (Ident->getName() == "property") {
385 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000386 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000387 // must be named get or put.
John McCall5e77d762013-04-16 07:28:30 +0000388 if (Tok.isNot(tok::l_paren)) {
389 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
390 << Ident->getNameStart();
Aaron Ballman478faed2012-06-19 22:09:27 +0000391 return;
John McCall5e77d762013-04-16 07:28:30 +0000392 }
393 BalancedDelimiterTracker T(*this, tok::l_paren);
394 T.expectAndConsume(diag::err_expected_lparen_after,
395 Ident->getNameStart(), tok::r_paren);
396
397 enum AccessorKind {
398 AK_Invalid = -1,
399 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
400 };
401 IdentifierInfo *AccessorNames[] = { 0, 0 };
402 bool HasInvalidAccessor = false;
403
404 // Parse the accessor specifications.
405 while (true) {
406 // Stop if this doesn't look like an accessor spec.
407 if (!Tok.is(tok::identifier)) {
408 // If the user wrote a completely empty list, use a special diagnostic.
409 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
410 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
411 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
412 break;
413 }
414
415 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
416 break;
417 }
418
419 AccessorKind Kind;
420 SourceLocation KindLoc = Tok.getLocation();
421 StringRef KindStr = Tok.getIdentifierInfo()->getName();
422 if (KindStr == "get") {
423 Kind = AK_Get;
424 } else if (KindStr == "put") {
425 Kind = AK_Put;
426
427 // Recover from the common mistake of using 'set' instead of 'put'.
428 } else if (KindStr == "set") {
429 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
430 << FixItHint::CreateReplacement(KindLoc, "put");
431 Kind = AK_Put;
432
433 // Handle the mistake of forgetting the accessor kind by skipping
434 // this accessor.
435 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
436 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
437 ConsumeToken();
438 HasInvalidAccessor = true;
439 goto next_property_accessor;
440
441 // Otherwise, complain about the unknown accessor kind.
442 } else {
443 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
444 HasInvalidAccessor = true;
445 Kind = AK_Invalid;
446
447 // Try to keep parsing unless it doesn't look like an accessor spec.
448 if (!NextToken().is(tok::equal)) break;
449 }
450
451 // Consume the identifier.
452 ConsumeToken();
453
454 // Consume the '='.
455 if (Tok.is(tok::equal)) {
456 ConsumeToken();
457 } else {
458 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
459 << KindStr;
460 break;
461 }
462
463 // Expect the method name.
464 if (!Tok.is(tok::identifier)) {
465 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
466 break;
467 }
468
469 if (Kind == AK_Invalid) {
470 // Just drop invalid accessors.
471 } else if (AccessorNames[Kind] != NULL) {
472 // Complain about the repeated accessor, ignore it, and keep parsing.
473 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
474 } else {
475 AccessorNames[Kind] = Tok.getIdentifierInfo();
476 }
477 ConsumeToken();
478
479 next_property_accessor:
480 // Keep processing accessors until we run out.
481 if (Tok.is(tok::comma)) {
482 ConsumeAnyToken();
483 continue;
484
485 // If we run into the ')', stop without consuming it.
486 } else if (Tok.is(tok::r_paren)) {
487 break;
488 } else {
489 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
490 break;
491 }
492 }
493
494 // Only add the property attribute if it was well-formed.
495 if (!HasInvalidAccessor) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000496 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(),
John McCall5e77d762013-04-16 07:28:30 +0000497 AccessorNames[AK_Get], AccessorNames[AK_Put],
498 AttributeList::AS_Declspec);
499 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000500 T.skipToEnd();
501 } else {
502 // We don't recognize this as a valid declspec, but instead of creating the
503 // attribute and allowing sema to warn about it, we will warn here instead.
504 // This is because some attributes have multiple spellings, but we need to
505 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000506 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000507 // both locations.
508 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
509
510 // If there's an open paren, we should eat the open and close parens under
511 // the assumption that this unknown declspec has parameters.
512 BalancedDelimiterTracker T(*this, tok::l_paren);
513 if (!T.consumeOpen())
514 T.skipToEnd();
515 }
516}
517
Eli Friedman06de2b52009-06-08 07:21:15 +0000518/// [MS] decl-specifier:
519/// __declspec ( extended-decl-modifier-seq )
520///
521/// [MS] extended-decl-modifier-seq:
522/// extended-decl-modifier[opt]
523/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000524void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000525 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000526
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000527 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000528 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000529 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000530 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000531 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000532
Chad Rosierc1183952012-06-26 22:30:43 +0000533 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000534 // you can specify multiple attributes per declspec.
535 while (Tok.getKind() != tok::r_paren) {
536 // We expect either a well-known identifier or a generic string. Anything
537 // else is a malformed declspec.
538 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000539 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000540 Tok.getKind() != tok::kw_restrict) {
541 Diag(Tok, diag::err_ms_declspec_type);
542 T.skipToEnd();
543 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000544 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000545
546 IdentifierInfo *AttrName;
547 SourceLocation AttrNameLoc;
548 if (IsString) {
549 SmallString<8> StrBuffer;
550 bool Invalid = false;
551 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
552 if (Invalid) {
553 T.skipToEnd();
554 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000555 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000556 AttrName = PP.getIdentifierInfo(Str);
557 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000558 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000559 AttrName = Tok.getIdentifierInfo();
560 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000561 }
Chad Rosierc1183952012-06-26 22:30:43 +0000562
Aaron Ballman478faed2012-06-19 22:09:27 +0000563 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000564 // If we have a generic string, we will allow it because there is no
565 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000566 // (for instance, SAL declspecs in older versions of MSVC).
567 //
Chad Rosierc1183952012-06-26 22:30:43 +0000568 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000569 // arguments and can be turned into an attribute directly.
Aaron Ballman00e99962013-08-31 01:11:41 +0000570 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
571 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000572 else
573 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000574 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000575 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000576}
577
John McCall53fa7142010-12-24 02:08:15 +0000578void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000579 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000580 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000581 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000582 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Aaron Ballman317a77f2013-05-22 23:25:32 +0000583 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) ||
584 Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000585 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
586 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000587 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
588 AttributeList::AS_Keyword);
Eli Friedman53339e02009-06-08 23:27:34 +0000589 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000590}
591
John McCall53fa7142010-12-24 02:08:15 +0000592void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000593 // Treat these like attributes
594 while (Tok.is(tok::kw___pascal)) {
595 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
596 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000597 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
598 AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000599 }
John McCall53fa7142010-12-24 02:08:15 +0000600}
601
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000602void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
603 // Treat these like attributes
604 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000605 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000606 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000607 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
608 AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000609 }
610}
611
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000612void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000613 // FIXME: The mapping from attribute spelling to semantics should be
614 // performed in Sema, not here.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000615 SourceLocation Loc = Tok.getLocation();
616 switch(Tok.getKind()) {
617 // OpenCL qualifiers:
618 case tok::kw___private:
Chad Rosierc1183952012-06-26 22:30:43 +0000619 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000620 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000621 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000622 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000623 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000624
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000625 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000626 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000627 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000628 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000629 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000630
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000631 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000632 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000633 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000634 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000635 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000636
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000637 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000638 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000639 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000640 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000641 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000642
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000643 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000644 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000645 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000646 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000647 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000648
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000649 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000650 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000651 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000652 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000653 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000654
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000655 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000656 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000657 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000658 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000659 break;
660 default: break;
661 }
662}
663
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000664/// \brief Parse a version number.
665///
666/// version:
667/// simple-integer
668/// simple-integer ',' simple-integer
669/// simple-integer ',' simple-integer ',' simple-integer
670VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
671 Range = Tok.getLocation();
672
673 if (!Tok.is(tok::numeric_constant)) {
674 Diag(Tok, diag::err_expected_version);
675 SkipUntil(tok::comma, tok::r_paren, true, true, true);
676 return VersionTuple();
677 }
678
679 // Parse the major (and possibly minor and subminor) versions, which
680 // are stored in the numeric constant. We utilize a quirk of the
681 // lexer, which is that it handles something like 1.2.3 as a single
682 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000683 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000684 Buffer.resize(Tok.getLength()+1);
685 const char *ThisTokBegin = &Buffer[0];
686
687 // Get the spelling of the token, which eliminates trigraphs, etc.
688 bool Invalid = false;
689 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
690 if (Invalid)
691 return VersionTuple();
692
693 // Parse the major version.
694 unsigned AfterMajor = 0;
695 unsigned Major = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000696 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000697 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
698 ++AfterMajor;
699 }
700
701 if (AfterMajor == 0) {
702 Diag(Tok, diag::err_expected_version);
703 SkipUntil(tok::comma, tok::r_paren, true, true, true);
704 return VersionTuple();
705 }
706
707 if (AfterMajor == ActualLength) {
708 ConsumeToken();
709
710 // We only had a single version component.
711 if (Major == 0) {
712 Diag(Tok, diag::err_zero_version);
713 return VersionTuple();
714 }
715
716 return VersionTuple(Major);
717 }
718
719 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
720 Diag(Tok, diag::err_expected_version);
721 SkipUntil(tok::comma, tok::r_paren, true, true, true);
722 return VersionTuple();
723 }
724
725 // Parse the minor version.
726 unsigned AfterMinor = AfterMajor + 1;
727 unsigned Minor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000728 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000729 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
730 ++AfterMinor;
731 }
732
733 if (AfterMinor == ActualLength) {
734 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000735
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000736 // We had major.minor.
737 if (Major == 0 && Minor == 0) {
738 Diag(Tok, diag::err_zero_version);
739 return VersionTuple();
740 }
741
Chad Rosierc1183952012-06-26 22:30:43 +0000742 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000743 }
744
745 // If what follows is not a '.', we have a problem.
746 if (ThisTokBegin[AfterMinor] != '.') {
747 Diag(Tok, diag::err_expected_version);
748 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosierc1183952012-06-26 22:30:43 +0000749 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000750 }
751
752 // Parse the subminor version.
753 unsigned AfterSubminor = AfterMinor + 1;
754 unsigned Subminor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000755 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000756 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
757 ++AfterSubminor;
758 }
759
760 if (AfterSubminor != ActualLength) {
761 Diag(Tok, diag::err_expected_version);
762 SkipUntil(tok::comma, tok::r_paren, true, true, true);
763 return VersionTuple();
764 }
765 ConsumeToken();
766 return VersionTuple(Major, Minor, Subminor);
767}
768
769/// \brief Parse the contents of the "availability" attribute.
770///
771/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000772/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000773///
774/// platform:
775/// identifier
776///
777/// version-arg-list:
778/// version-arg
779/// version-arg ',' version-arg-list
780///
781/// version-arg:
782/// 'introduced' '=' version
783/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000784/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000785/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000786/// opt-message:
787/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000788void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
789 SourceLocation AvailabilityLoc,
790 ParsedAttributes &attrs,
791 SourceLocation *endLoc) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000792 enum { Introduced, Deprecated, Obsoleted, Unknown };
793 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000794 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000795
796 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000797 BalancedDelimiterTracker T(*this, tok::l_paren);
798 if (T.consumeOpen()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000799 Diag(Tok, diag::err_expected_lparen);
800 return;
801 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000802
803 // Parse the platform name,
804 if (Tok.isNot(tok::identifier)) {
805 Diag(Tok, diag::err_availability_expected_platform);
806 SkipUntil(tok::r_paren);
807 return;
808 }
Richard Smithfeefaf52013-09-03 18:01:40 +0000809 IdentifierLoc *Platform = ParseIdentifierLoc();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000810
811 // Parse the ',' following the platform name.
812 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
813 return;
814
815 // If we haven't grabbed the pointers for the identifiers
816 // "introduced", "deprecated", and "obsoleted", do so now.
817 if (!Ident_introduced) {
818 Ident_introduced = PP.getIdentifierInfo("introduced");
819 Ident_deprecated = PP.getIdentifierInfo("deprecated");
820 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000821 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000822 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000823 }
824
825 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000826 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000827 do {
828 if (Tok.isNot(tok::identifier)) {
829 Diag(Tok, diag::err_availability_expected_change);
830 SkipUntil(tok::r_paren);
831 return;
832 }
833 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
834 SourceLocation KeywordLoc = ConsumeToken();
835
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000836 if (Keyword == Ident_unavailable) {
837 if (UnavailableLoc.isValid()) {
838 Diag(KeywordLoc, diag::err_availability_redundant)
839 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000840 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000841 UnavailableLoc = KeywordLoc;
842
843 if (Tok.isNot(tok::comma))
844 break;
845
846 ConsumeToken();
847 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000848 }
849
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000850 if (Tok.isNot(tok::equal)) {
851 Diag(Tok, diag::err_expected_equal_after)
852 << Keyword;
853 SkipUntil(tok::r_paren);
854 return;
855 }
856 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000857 if (Keyword == Ident_message) {
Benjamin Kramera9dfa922013-09-13 17:31:48 +0000858 if (Tok.isNot(tok::string_literal)) { // Also reject wide string literals.
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000859 Diag(Tok, diag::err_expected_string_literal)
860 << /*Source='availability attribute'*/2;
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000861 SkipUntil(tok::r_paren);
862 return;
863 }
864 MessageExpr = ParseStringLiteralExpression();
865 break;
866 }
Chad Rosierc1183952012-06-26 22:30:43 +0000867
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000868 SourceRange VersionRange;
869 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000870
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000871 if (Version.empty()) {
872 SkipUntil(tok::r_paren);
873 return;
874 }
875
876 unsigned Index;
877 if (Keyword == Ident_introduced)
878 Index = Introduced;
879 else if (Keyword == Ident_deprecated)
880 Index = Deprecated;
881 else if (Keyword == Ident_obsoleted)
882 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000883 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000884 Index = Unknown;
885
886 if (Index < Unknown) {
887 if (!Changes[Index].KeywordLoc.isInvalid()) {
888 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000889 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000890 << SourceRange(Changes[Index].KeywordLoc,
891 Changes[Index].VersionRange.getEnd());
892 }
893
894 Changes[Index].KeywordLoc = KeywordLoc;
895 Changes[Index].Version = Version;
896 Changes[Index].VersionRange = VersionRange;
897 } else {
898 Diag(KeywordLoc, diag::err_availability_unknown_change)
899 << Keyword << VersionRange;
900 }
901
902 if (Tok.isNot(tok::comma))
903 break;
904
905 ConsumeToken();
906 } while (true);
907
908 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000909 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000910 return;
911
912 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000913 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000914
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000915 // The 'unavailable' availability cannot be combined with any other
916 // availability changes. Make sure that hasn't happened.
917 if (UnavailableLoc.isValid()) {
918 bool Complained = false;
919 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
920 if (Changes[Index].KeywordLoc.isValid()) {
921 if (!Complained) {
922 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
923 << SourceRange(Changes[Index].KeywordLoc,
924 Changes[Index].VersionRange.getEnd());
925 Complained = true;
926 }
927
928 // Clear out the availability.
929 Changes[Index] = AvailabilityChange();
930 }
931 }
932 }
933
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000934 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000935 attrs.addNew(&Availability,
936 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000937 0, AvailabilityLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +0000938 Platform,
John McCall084e83d2011-03-24 11:26:52 +0000939 Changes[Introduced],
940 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000941 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000942 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000943 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000944}
945
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000946
Bill Wendling44426052012-12-20 19:22:21 +0000947// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000948// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
949
950void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
951
952void Parser::LateParsedClass::ParseLexedAttributes() {
953 Self->ParseLexedAttributes(*Class);
954}
955
956void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000957 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000958}
959
960/// Wrapper class which calls ParseLexedAttribute, after setting up the
961/// scope appropriately.
962void Parser::ParseLexedAttributes(ParsingClass &Class) {
963 // Deal with templates
964 // FIXME: Test cases to make sure this does the right thing for templates.
965 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
966 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
967 HasTemplateScope);
968 if (HasTemplateScope)
969 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
970
Douglas Gregor3024f072012-04-16 07:05:22 +0000971 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000972 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +0000973 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000974 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
975 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
976
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000977 // Enter the scope of nested classes
978 if (!AlreadyHasClassScope)
979 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
980 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +0000981 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +0000982 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
983 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
984 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000985 }
Chad Rosierc1183952012-06-26 22:30:43 +0000986
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000987 if (!AlreadyHasClassScope)
988 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
989 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000990}
991
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000992
993/// \brief Parse all attributes in LAs, and attach them to Decl D.
994void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
995 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +0000996 assert(LAs.parseSoon() &&
997 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000998 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +0000999 if (D)
1000 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001001 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +00001002 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001003 }
1004 LAs.clear();
1005}
1006
1007
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001008/// \brief Finish parsing an attribute for which parsing was delayed.
1009/// This will be called at the end of parsing a class declaration
1010/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +00001011/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001012/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001013void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1014 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001015 // Save the current token position.
1016 SourceLocation OrigLoc = Tok.getLocation();
1017
1018 // Append the current token at the end of the new token stream so that it
1019 // doesn't get lost.
1020 LA.Toks.push_back(Tok);
1021 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1022 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00001023 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001024
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001025 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smith10876ef2013-01-17 01:30:42 +00001026 // FIXME: Do not warn on C++11 attributes, once we start supporting
1027 // them here.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001028 Diag(Tok, diag::warn_attribute_on_function_definition)
1029 << LA.AttrName.getName();
1030 }
1031
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001032 ParsedAttributes Attrs(AttrFactory);
1033 SourceLocation endLoc;
1034
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001035 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001036 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001037 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1038 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001039
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001040 // Allow 'this' within late-parsed attributes.
Richard Smithc3d2ebb2013-06-07 02:33:37 +00001041 Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0,
1042 ND && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001043
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001044 if (LA.Decls.size() == 1) {
1045 // If the Decl is templatized, add template parameters to scope.
1046 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1047 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1048 if (HasTemplateScope)
1049 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001050
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001051 // If the Decl is on a function, add function parameters to the scope.
1052 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1053 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1054 if (HasFunScope)
1055 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001056
Michael Han23214e52012-10-03 01:56:22 +00001057 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001058 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001059
1060 if (HasFunScope) {
1061 Actions.ActOnExitFunctionContext();
1062 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1063 }
1064 if (HasTemplateScope) {
1065 TempScope.Exit();
1066 }
1067 } else {
1068 // If there are multiple decls, then the decl cannot be within the
1069 // function scope.
Michael Han23214e52012-10-03 01:56:22 +00001070 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001071 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001072 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +00001073 } else {
1074 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001075 }
1076
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001077 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
1078 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
1079 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001080
1081 if (Tok.getLocation() != OrigLoc) {
1082 // Due to a parsing error, we either went over the cached tokens or
1083 // there are still cached tokens left, so we skip the leftover tokens.
1084 // Since this is an uncommon situation that should be avoided, use the
1085 // expensive isBeforeInTranslationUnit call.
1086 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1087 OrigLoc))
1088 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001089 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001090 }
1091}
1092
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001093/// \brief Wrapper around a case statement checking if AttrName is
1094/// one of the thread safety attributes
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001095bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001096 return llvm::StringSwitch<bool>(AttrName)
1097 .Case("guarded_by", true)
1098 .Case("guarded_var", true)
1099 .Case("pt_guarded_by", true)
1100 .Case("pt_guarded_var", true)
1101 .Case("lockable", true)
1102 .Case("scoped_lockable", true)
1103 .Case("no_thread_safety_analysis", true)
1104 .Case("acquired_after", true)
1105 .Case("acquired_before", true)
1106 .Case("exclusive_lock_function", true)
1107 .Case("shared_lock_function", true)
1108 .Case("exclusive_trylock_function", true)
1109 .Case("shared_trylock_function", true)
1110 .Case("unlock_function", true)
1111 .Case("lock_returned", true)
1112 .Case("locks_excluded", true)
1113 .Case("exclusive_locks_required", true)
1114 .Case("shared_locks_required", true)
1115 .Default(false);
1116}
1117
1118/// \brief Parse the contents of thread safety attributes. These
1119/// should always be parsed as an expression list.
1120///
1121/// We need to special case the parsing due to the fact that if the first token
1122/// of the first argument is an identifier, the main parse loop will store
1123/// that token as a "parameter" and the rest of
1124/// the arguments will be added to a list of "arguments". However,
1125/// subsequent tokens in the first argument are lost. We instead parse each
1126/// argument as an expression and add all arguments to the list of "arguments".
1127/// In future, we will take advantage of this special case to also
1128/// deal with some argument scoping issues here (for example, referring to a
1129/// function parameter in the attribute on that function).
1130void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1131 SourceLocation AttrNameLoc,
1132 ParsedAttributes &Attrs,
1133 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001134 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001135
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001136 BalancedDelimiterTracker T(*this, tok::l_paren);
1137 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +00001138
Aaron Ballman00e99962013-08-31 01:11:41 +00001139 ArgsVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001140 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +00001141
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001142 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001143 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinseb849c62013-02-07 19:01:07 +00001144 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001145 ExprResult ArgExpr(ParseAssignmentExpression());
1146 if (ArgExpr.isInvalid()) {
1147 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001148 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001149 break;
1150 } else {
1151 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001152 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001153 if (Tok.isNot(tok::comma))
1154 break;
1155 ConsumeToken(); // Eat the comma, move to the next argument
1156 }
1157 // Match the ')'.
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001158 if (ArgExprsOk && !T.consumeClose()) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001159 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, ArgExprs.data(),
1160 ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001161 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001162 if (EndLoc)
1163 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001164}
1165
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001166void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1167 SourceLocation AttrNameLoc,
1168 ParsedAttributes &Attrs,
1169 SourceLocation *EndLoc) {
1170 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1171
1172 BalancedDelimiterTracker T(*this, tok::l_paren);
1173 T.consumeOpen();
1174
1175 if (Tok.isNot(tok::identifier)) {
1176 Diag(Tok, diag::err_expected_ident);
1177 T.skipToEnd();
1178 return;
1179 }
Richard Smithfeefaf52013-09-03 18:01:40 +00001180 IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001181
1182 if (Tok.isNot(tok::comma)) {
1183 Diag(Tok, diag::err_expected_comma);
1184 T.skipToEnd();
1185 return;
1186 }
1187 ConsumeToken();
1188
1189 SourceRange MatchingCTypeRange;
1190 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1191 if (MatchingCType.isInvalid()) {
1192 T.skipToEnd();
1193 return;
1194 }
1195
1196 bool LayoutCompatible = false;
1197 bool MustBeNull = false;
1198 while (Tok.is(tok::comma)) {
1199 ConsumeToken();
1200 if (Tok.isNot(tok::identifier)) {
1201 Diag(Tok, diag::err_expected_ident);
1202 T.skipToEnd();
1203 return;
1204 }
1205 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1206 if (Flag->isStr("layout_compatible"))
1207 LayoutCompatible = true;
1208 else if (Flag->isStr("must_be_null"))
1209 MustBeNull = true;
1210 else {
1211 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1212 T.skipToEnd();
1213 return;
1214 }
1215 ConsumeToken(); // consume flag
1216 }
1217
1218 if (!T.consumeClose()) {
1219 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +00001220 ArgumentKind, MatchingCType.release(),
1221 LayoutCompatible, MustBeNull,
1222 AttributeList::AS_GNU);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001223 }
1224
1225 if (EndLoc)
1226 *EndLoc = T.getCloseLocation();
1227}
1228
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001229/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1230/// of a C++11 attribute-specifier in a location where an attribute is not
1231/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1232/// situation.
1233///
1234/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1235/// this doesn't appear to actually be an attribute-specifier, and the caller
1236/// should try to parse it.
1237bool Parser::DiagnoseProhibitedCXX11Attribute() {
1238 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1239
1240 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1241 case CAK_NotAttributeSpecifier:
1242 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1243 return false;
1244
1245 case CAK_InvalidAttributeSpecifier:
1246 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1247 return false;
1248
1249 case CAK_AttributeSpecifier:
1250 // Parse and discard the attributes.
1251 SourceLocation BeginLoc = ConsumeBracket();
1252 ConsumeBracket();
1253 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1254 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1255 SourceLocation EndLoc = ConsumeBracket();
1256 Diag(BeginLoc, diag::err_attributes_not_allowed)
1257 << SourceRange(BeginLoc, EndLoc);
1258 return true;
1259 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001260 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001261}
1262
Richard Smith98155ad2013-02-20 01:17:14 +00001263/// \brief We have found the opening square brackets of a C++11
1264/// attribute-specifier in a location where an attribute is not permitted, but
1265/// we know where the attributes ought to be written. Parse them anyway, and
1266/// provide a fixit moving them to the right place.
Richard Smith4c96e992013-02-19 23:47:15 +00001267void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1268 SourceLocation CorrectLocation) {
1269 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1270 Tok.is(tok::kw_alignas));
1271
1272 // Consume the attributes.
1273 SourceLocation Loc = Tok.getLocation();
1274 ParseCXX11Attributes(Attrs);
1275 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1276
1277 Diag(Loc, diag::err_attributes_not_allowed)
1278 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1279 << FixItHint::CreateRemoval(AttrRange);
1280}
1281
John McCall53fa7142010-12-24 02:08:15 +00001282void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1283 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1284 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001285}
1286
Michael Han64536a62012-11-06 19:34:54 +00001287void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1288 AttributeList *AttrList = attrs.getList();
1289 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001290 if (AttrList->isCXX11Attribute()) {
Richard Smith810ad3e2013-01-29 10:02:16 +00001291 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Han64536a62012-11-06 19:34:54 +00001292 << AttrList->getName();
1293 AttrList->setInvalid();
1294 }
1295 AttrList = AttrList->getNext();
1296 }
1297}
1298
Chris Lattner53361ac2006-08-10 05:19:57 +00001299/// ParseDeclaration - Parse a full 'declaration', which consists of
1300/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001301/// 'Context' should be a Declarator::TheContext value. This returns the
1302/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001303///
1304/// declaration: [C99 6.7]
1305/// block-declaration ->
1306/// simple-declaration
1307/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001308/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001309/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001310/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001311/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001312/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001313/// others... [FIXME]
1314///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001315Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1316 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001317 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001318 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001319 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001320 // Must temporarily exit the objective-c container scope for
1321 // parsing c none objective-c decls.
1322 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001323
John McCall48871652010-08-21 09:40:31 +00001324 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001325 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001326 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001327 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001328 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001329 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001330 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001331 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001332 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001333 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001334 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001335 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001336 SourceLocation InlineLoc = ConsumeToken();
1337 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1338 break;
1339 }
Chad Rosierc1183952012-06-26 22:30:43 +00001340 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001341 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001342 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001343 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001344 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001345 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001346 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001347 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001348 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001349 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001350 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001351 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001352 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001353 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001354 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001355 default:
John McCall53fa7142010-12-24 02:08:15 +00001356 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001357 }
Chad Rosierc1183952012-06-26 22:30:43 +00001358
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001359 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001360 // single decl, convert it now. Alias declarations can also declare a type;
1361 // include that too if it is present.
1362 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001363}
1364
1365/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1366/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001367/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1368/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001369///[C90/C++]init-declarator-list ';' [TODO]
1370/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001371///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001372/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001373/// attribute-specifier-seq[opt] type-specifier-seq declarator
1374///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001375/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001376/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001377///
1378/// If FRI is non-null, we might be parsing a for-range-declaration instead
1379/// of a simple-declaration. If we find that we are, we also parse the
1380/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001381Parser::DeclGroupPtrTy
1382Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1383 SourceLocation &DeclEnd,
Richard Smith2386c8b2013-02-22 09:06:26 +00001384 ParsedAttributesWithRange &Attrs,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001385 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001386 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001387 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001388
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001389 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +00001390 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001391
Chris Lattner0e894622006-08-13 19:58:17 +00001392 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1393 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001394 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001395 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001396 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001397 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001398 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001399 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001400 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001401 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001402 }
Chad Rosierc1183952012-06-26 22:30:43 +00001403
Richard Smith2386c8b2013-02-22 09:06:26 +00001404 DS.takeAttributesFrom(Attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00001405 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001406}
Mike Stump11289f42009-09-09 15:08:12 +00001407
Richard Smith09f76ee2011-10-19 21:33:05 +00001408/// Returns true if this might be the start of a declarator, or a common typo
1409/// for a declarator.
1410bool Parser::MightBeDeclarator(unsigned Context) {
1411 switch (Tok.getKind()) {
1412 case tok::annot_cxxscope:
1413 case tok::annot_template_id:
1414 case tok::caret:
1415 case tok::code_completion:
1416 case tok::coloncolon:
1417 case tok::ellipsis:
1418 case tok::kw___attribute:
1419 case tok::kw_operator:
1420 case tok::l_paren:
1421 case tok::star:
1422 return true;
1423
1424 case tok::amp:
1425 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001426 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001427
Richard Smithc8a79032012-01-09 22:31:44 +00001428 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001429 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001430 NextToken().is(tok::l_square);
1431
1432 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001433 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001434
Richard Smith09f76ee2011-10-19 21:33:05 +00001435 case tok::identifier:
1436 switch (NextToken().getKind()) {
1437 case tok::code_completion:
1438 case tok::coloncolon:
1439 case tok::comma:
1440 case tok::equal:
1441 case tok::equalequal: // Might be a typo for '='.
1442 case tok::kw_alignas:
1443 case tok::kw_asm:
1444 case tok::kw___attribute:
1445 case tok::l_brace:
1446 case tok::l_paren:
1447 case tok::l_square:
1448 case tok::less:
1449 case tok::r_brace:
1450 case tok::r_paren:
1451 case tok::r_square:
1452 case tok::semi:
1453 return true;
1454
1455 case tok::colon:
1456 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001457 // and in block scope it's probably a label. Inside a class definition,
1458 // this is a bit-field.
1459 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001460 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001461
1462 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001463 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001464
1465 default:
1466 return false;
1467 }
1468
1469 default:
1470 return false;
1471 }
1472}
1473
Richard Smithb8caac82012-04-11 20:59:20 +00001474/// Skip until we reach something which seems like a sensible place to pick
1475/// up parsing after a malformed declaration. This will sometimes stop sooner
1476/// than SkipUntil(tok::r_brace) would, but will never stop later.
1477void Parser::SkipMalformedDecl() {
1478 while (true) {
1479 switch (Tok.getKind()) {
1480 case tok::l_brace:
1481 // Skip until matching }, then stop. We've probably skipped over
1482 // a malformed class or function definition or similar.
1483 ConsumeBrace();
1484 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1485 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1486 // This declaration isn't over yet. Keep skipping.
1487 continue;
1488 }
1489 if (Tok.is(tok::semi))
1490 ConsumeToken();
1491 return;
1492
1493 case tok::l_square:
1494 ConsumeBracket();
1495 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1496 continue;
1497
1498 case tok::l_paren:
1499 ConsumeParen();
1500 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1501 continue;
1502
1503 case tok::r_brace:
1504 return;
1505
1506 case tok::semi:
1507 ConsumeToken();
1508 return;
1509
1510 case tok::kw_inline:
1511 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001512 // a good place to pick back up parsing, except in an Objective-C
1513 // @interface context.
1514 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1515 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001516 return;
1517 break;
1518
1519 case tok::kw_namespace:
1520 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001521 // place to pick back up parsing, except in an Objective-C
1522 // @interface context.
1523 if (Tok.isAtStartOfLine() &&
1524 (!ParsingInObjCContainer || CurParsedObjCImpl))
1525 return;
1526 break;
1527
1528 case tok::at:
1529 // @end is very much like } in Objective-C contexts.
1530 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1531 ParsingInObjCContainer)
1532 return;
1533 break;
1534
1535 case tok::minus:
1536 case tok::plus:
1537 // - and + probably start new method declarations in Objective-C contexts.
1538 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001539 return;
1540 break;
1541
1542 case tok::eof:
1543 return;
1544
1545 default:
1546 break;
1547 }
1548
1549 ConsumeAnyToken();
1550 }
1551}
1552
John McCalld5a36322009-11-03 19:26:08 +00001553/// ParseDeclGroup - Having concluded that this is either a function
1554/// definition or a group of object declarations, actually parse the
1555/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001556Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1557 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001558 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001559 SourceLocation *DeclEnd,
1560 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001561 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001562 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001563 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001564
John McCalld5a36322009-11-03 19:26:08 +00001565 // Bail out if the first declarator didn't seem well-formed.
1566 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001567 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001568 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001569 }
Mike Stump11289f42009-09-09 15:08:12 +00001570
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001571 // Save late-parsed attributes for now; they need to be parsed in the
1572 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001573 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1574 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001575 if (D.isFunctionDeclarator())
1576 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1577
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001578 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00001579 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001580 // Look at the next token to make sure that this isn't a function
1581 // declaration. We have to check this because __attribute__ might be the
1582 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001583 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001584
Douglas Gregor012efe22013-04-16 16:01:32 +00001585 if (AllowFunctionDefinitions) {
1586 if (isStartOfFunctionDefinition(D)) {
1587 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1588 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00001589
Douglas Gregor012efe22013-04-16 16:01:32 +00001590 // Recover by treating the 'typedef' as spurious.
1591 DS.ClearStorageClassSpecs();
1592 }
1593
1594 Decl *TheDecl =
1595 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1596 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00001597 }
1598
Douglas Gregor012efe22013-04-16 16:01:32 +00001599 if (isDeclarationSpecifier()) {
1600 // If there is an invalid declaration specifier right after the function
1601 // prototype, then we must be in a missing semicolon case where this isn't
1602 // actually a body. Just fall through into the code that handles it as a
1603 // prototype, and let the top-level code handle the erroneous declspec
1604 // where it would otherwise expect a comma or semicolon.
1605 } else {
1606 Diag(Tok, diag::err_expected_fn_body);
1607 SkipUntil(tok::semi);
1608 return DeclGroupPtrTy();
1609 }
John McCalld5a36322009-11-03 19:26:08 +00001610 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00001611 if (Tok.is(tok::l_brace)) {
1612 Diag(Tok, diag::err_function_definition_not_allowed);
1613 SkipUntil(tok::r_brace, true, true);
1614 }
John McCalld5a36322009-11-03 19:26:08 +00001615 }
1616 }
1617
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001618 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001619 return DeclGroupPtrTy();
1620
1621 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1622 // must parse and analyze the for-range-initializer before the declaration is
1623 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001624 //
1625 // Handle the Objective-C for-in loop variable similarly, although we
1626 // don't need to parse the container in advance.
1627 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1628 bool IsForRangeLoop = false;
1629 if (Tok.is(tok::colon)) {
1630 IsForRangeLoop = true;
1631 FRI->ColonLoc = ConsumeToken();
1632 if (Tok.is(tok::l_brace))
1633 FRI->RangeExpr = ParseBraceInitializer();
1634 else
1635 FRI->RangeExpr = ParseExpression();
1636 }
1637
Richard Smith02e85f32011-04-14 22:09:26 +00001638 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001639 if (IsForRangeLoop)
1640 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001641 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001642 D.complete(ThisDecl);
Rafael Espindolaab417692013-07-09 12:05:01 +00001643 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001644 }
1645
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001646 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001647 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001648 if (LateParsedAttrs.size() > 0)
1649 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001650 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001651 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001652 DeclsInGroup.push_back(FirstDecl);
1653
Richard Smith09f76ee2011-10-19 21:33:05 +00001654 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001655
John McCalld5a36322009-11-03 19:26:08 +00001656 // If we don't have a comma, it is either the end of the list (a ';') or an
1657 // error, bail out.
1658 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001659 SourceLocation CommaLoc = ConsumeToken();
1660
1661 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1662 // This comma was followed by a line-break and something which can't be
1663 // the start of a declarator. The comma was probably a typo for a
1664 // semicolon.
1665 Diag(CommaLoc, diag::err_expected_semi_declaration)
1666 << FixItHint::CreateReplacement(CommaLoc, ";");
1667 ExpectSemi = false;
1668 break;
1669 }
John McCalld5a36322009-11-03 19:26:08 +00001670
1671 // Parse the next declarator.
1672 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001673 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001674
1675 // Accept attributes in an init-declarator. In the first declarator in a
1676 // declaration, these would be part of the declspec. In subsequent
1677 // declarators, they become part of the declarator itself, so that they
1678 // don't apply to declarators after *this* one. Examples:
1679 // short __attribute__((common)) var; -> declspec
1680 // short var __attribute__((common)); -> declarator
1681 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001682 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001683
1684 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001685 if (!D.isInvalidType()) {
1686 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1687 D.complete(ThisDecl);
1688 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001689 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001690 }
John McCalld5a36322009-11-03 19:26:08 +00001691 }
1692
1693 if (DeclEnd)
1694 *DeclEnd = Tok.getLocation();
1695
Richard Smith09f76ee2011-10-19 21:33:05 +00001696 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001697 ExpectAndConsumeSemi(Context == Declarator::FileContext
1698 ? diag::err_invalid_token_after_toplevel_declarator
1699 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001700 // Okay, there was no semicolon and one was expected. If we see a
1701 // declaration specifier, just assume it was missing and continue parsing.
1702 // Otherwise things are very confused and we skip to recover.
1703 if (!isDeclarationSpecifier()) {
1704 SkipUntil(tok::r_brace, true, true);
1705 if (Tok.is(tok::semi))
1706 ConsumeToken();
1707 }
John McCalld5a36322009-11-03 19:26:08 +00001708 }
1709
Rafael Espindolaab417692013-07-09 12:05:01 +00001710 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +00001711}
1712
Richard Smith02e85f32011-04-14 22:09:26 +00001713/// Parse an optional simple-asm-expr and attributes, and attach them to a
1714/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001715bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001716 // If a simple-asm-expr is present, parse it.
1717 if (Tok.is(tok::kw_asm)) {
1718 SourceLocation Loc;
1719 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1720 if (AsmLabel.isInvalid()) {
1721 SkipUntil(tok::semi, true, true);
1722 return true;
1723 }
1724
1725 D.setAsmLabel(AsmLabel.release());
1726 D.SetRangeEnd(Loc);
1727 }
1728
1729 MaybeParseGNUAttributes(D);
1730 return false;
1731}
1732
Douglas Gregor23996282009-05-12 21:31:51 +00001733/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1734/// declarator'. This method parses the remainder of the declaration
1735/// (including any attributes or initializer, among other things) and
1736/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001737///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001738/// init-declarator: [C99 6.7]
1739/// declarator
1740/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001741/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1742/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001743/// [C++] declarator initializer[opt]
1744///
1745/// [C++] initializer:
1746/// [C++] '=' initializer-clause
1747/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001748/// [C++0x] '=' 'default' [TODO]
1749/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001750/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001751///
1752/// According to the standard grammar, =default and =delete are function
1753/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001754///
John McCall48871652010-08-21 09:40:31 +00001755Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001756 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001757 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001758 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001759
Richard Smith02e85f32011-04-14 22:09:26 +00001760 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1761}
Mike Stump11289f42009-09-09 15:08:12 +00001762
Richard Smith02e85f32011-04-14 22:09:26 +00001763Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1764 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001765 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001766 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001767 switch (TemplateInfo.Kind) {
1768 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001769 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001770 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001771
Douglas Gregor450f00842009-09-25 18:43:00 +00001772 case ParsedTemplateInfo::Template:
Larisse Voufo39a1e502013-08-06 01:03:05 +00001773 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001774 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001775 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001776 D);
Larisse Voufo833b05a2013-08-06 07:33:00 +00001777 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufo39a1e502013-08-06 01:03:05 +00001778 // Re-direct this decl to refer to the templated decl so that we can
1779 // initialize it.
1780 ThisDecl = VT->getTemplatedDecl();
1781 break;
1782 }
1783 case ParsedTemplateInfo::ExplicitInstantiation: {
1784 if (Tok.is(tok::semi)) {
1785 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
1786 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
1787 if (ThisRes.isInvalid()) {
1788 SkipUntil(tok::semi, true, true);
1789 return 0;
1790 }
1791 ThisDecl = ThisRes.get();
1792 } else {
1793 // FIXME: This check should be for a variable template instantiation only.
1794
1795 // Check that this is a valid instantiation
1796 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
1797 // If the declarator-id is not a template-id, issue a diagnostic and
1798 // recover by ignoring the 'template' keyword.
1799 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1800 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1801 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1802 } else {
1803 SourceLocation LAngleLoc =
1804 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1805 Diag(D.getIdentifierLoc(),
1806 diag::err_explicit_instantiation_with_definition)
1807 << SourceRange(TemplateInfo.TemplateLoc)
1808 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1809
1810 // Recover as if it were an explicit specialization.
1811 TemplateParameterLists FakedParamLists;
1812 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1813 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1814 LAngleLoc));
1815
1816 ThisDecl =
1817 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
1818 }
1819 }
Douglas Gregor450f00842009-09-25 18:43:00 +00001820 break;
1821 }
1822 }
Mike Stump11289f42009-09-09 15:08:12 +00001823
Richard Smith74aeef52013-04-26 16:15:35 +00001824 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith30482bc2011-02-20 03:19:35 +00001825
Douglas Gregor23996282009-05-12 21:31:51 +00001826 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001827 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001828 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001829 ConsumeToken();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001830
Anders Carlsson991285e2010-09-24 21:25:25 +00001831 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001832 if (D.isFunctionDeclarator())
1833 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1834 << 1 /* delete */;
1835 else
1836 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001837 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001838 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001839 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1840 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001841 else
1842 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001843 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001844 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001845 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001846 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001847 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001848
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001849 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001850 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001851 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001852 cutOffParsing();
1853 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001854 }
Chad Rosierc1183952012-06-26 22:30:43 +00001855
John McCalldadc5752010-08-24 06:29:42 +00001856 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001857
David Blaikiebbafb8a2012-03-11 07:00:24 +00001858 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001859 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001860 ExitScope();
1861 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001862
Douglas Gregor23996282009-05-12 21:31:51 +00001863 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001864 SkipUntil(tok::comma, true, true);
1865 Actions.ActOnInitializerError(ThisDecl);
1866 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001867 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1868 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001869 }
1870 } else if (Tok.is(tok::l_paren)) {
1871 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001872 BalancedDelimiterTracker T(*this, tok::l_paren);
1873 T.consumeOpen();
1874
Benjamin Kramerf0623432012-08-23 22:51:59 +00001875 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001876 CommaLocsTy CommaLocs;
1877
David Blaikiebbafb8a2012-03-11 07:00:24 +00001878 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001879 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001880 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001881 }
1882
Douglas Gregor23996282009-05-12 21:31:51 +00001883 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001884 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor23996282009-05-12 21:31:51 +00001885 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001886
David Blaikiebbafb8a2012-03-11 07:00:24 +00001887 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001888 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001889 ExitScope();
1890 }
Douglas Gregor23996282009-05-12 21:31:51 +00001891 } else {
1892 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001893 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001894
1895 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1896 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001897
David Blaikiebbafb8a2012-03-11 07:00:24 +00001898 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001899 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001900 ExitScope();
1901 }
1902
Sebastian Redla9351792012-02-11 23:51:47 +00001903 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1904 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001905 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001906 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1907 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001908 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001909 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001910 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001911 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001912 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1913
Sebastian Redl3da34892011-06-05 12:23:16 +00001914 if (D.getCXXScopeSpec().isSet()) {
1915 EnterScope(0);
1916 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1917 }
1918
1919 ExprResult Init(ParseBraceInitializer());
1920
1921 if (D.getCXXScopeSpec().isSet()) {
1922 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1923 ExitScope();
1924 }
1925
1926 if (Init.isInvalid()) {
1927 Actions.ActOnInitializerError(ThisDecl);
1928 } else
1929 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1930 /*DirectInit=*/true, TypeContainsAuto);
1931
Douglas Gregor23996282009-05-12 21:31:51 +00001932 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001933 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001934 }
1935
Richard Smithb2bc2e62011-02-21 20:05:19 +00001936 Actions.FinalizeDeclaration(ThisDecl);
1937
Douglas Gregor23996282009-05-12 21:31:51 +00001938 return ThisDecl;
1939}
1940
Chris Lattner1890ac82006-08-13 01:16:23 +00001941/// ParseSpecifierQualifierList
1942/// specifier-qualifier-list:
1943/// type-specifier specifier-qualifier-list[opt]
1944/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001945/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001946///
Richard Smithc5b05522012-03-12 07:56:15 +00001947void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1948 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001949 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1950 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001951 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001952 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001953
Chris Lattner1890ac82006-08-13 01:16:23 +00001954 // Validate declspec for type-name.
1955 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00001956 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1957 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001958 Diag(Tok, diag::err_expected_type);
1959 DS.SetTypeSpecError();
1960 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1961 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001962 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001963 if (!DS.hasTypeSpecifier())
1964 DS.SetTypeSpecError();
1965 }
Mike Stump11289f42009-09-09 15:08:12 +00001966
Chris Lattner1b22eed2006-11-28 05:12:07 +00001967 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001968 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001969 if (DS.getStorageClassSpecLoc().isValid())
1970 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1971 else
Richard Smithb4a9e862013-04-12 22:46:28 +00001972 Diag(DS.getThreadStorageClassSpecLoc(),
1973 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001974 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001975 }
Mike Stump11289f42009-09-09 15:08:12 +00001976
Chris Lattner1b22eed2006-11-28 05:12:07 +00001977 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001978 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001979 if (DS.isInlineSpecified())
1980 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1981 if (DS.isVirtualSpecified())
1982 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1983 if (DS.isExplicitSpecified())
1984 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001985 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001986 }
Richard Smithc5b05522012-03-12 07:56:15 +00001987
1988 // Issue diagnostic and remove constexpr specfier if present.
1989 if (DS.isConstexprSpecified()) {
1990 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1991 DS.ClearConstexprSpec();
1992 }
Chris Lattner1890ac82006-08-13 01:16:23 +00001993}
Chris Lattner53361ac2006-08-10 05:19:57 +00001994
Chris Lattner6cc055a2009-04-12 20:42:31 +00001995/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1996/// specified token is valid after the identifier in a declarator which
1997/// immediately follows the declspec. For example, these things are valid:
1998///
1999/// int x [ 4]; // direct-declarator
2000/// int x ( int y); // direct-declarator
2001/// int(int x ) // direct-declarator
2002/// int x ; // simple-declaration
2003/// int x = 17; // init-declarator-list
2004/// int x , y; // init-declarator-list
2005/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002006/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002007/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002008///
2009/// This is not, because 'x' does not immediately follow the declspec (though
2010/// ')' happens to be valid anyway).
2011/// int (x)
2012///
2013static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2014 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2015 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00002016 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002017}
2018
Chris Lattner20a0c612009-04-14 21:34:55 +00002019
2020/// ParseImplicitInt - This method is called when we have an non-typename
2021/// identifier in a declspec (which normally terminates the decl spec) when
2022/// the declspec has no type specifier. In this case, the declspec is either
2023/// malformed or is "implicit int" (in K&R and C89).
2024///
2025/// This method handles diagnosing this prettily and returns false if the
2026/// declspec is done being processed. If it recovers and thinks there may be
2027/// other pieces of declspec after it, it returns true.
2028///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002029bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002030 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00002031 AccessSpecifier AS, DeclSpecContext DSC,
2032 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002033 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002034
Chris Lattner20a0c612009-04-14 21:34:55 +00002035 SourceLocation Loc = Tok.getLocation();
2036 // If we see an identifier that is not a type name, we normally would
2037 // parse it as the identifer being declared. However, when a typename
2038 // is typo'd or the definition is not included, this will incorrectly
2039 // parse the typename as the identifier name and fall over misparsing
2040 // later parts of the diagnostic.
2041 //
2042 // As such, we try to do some look-ahead in cases where this would
2043 // otherwise be an "implicit-int" case to see if this is invalid. For
2044 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2045 // an identifier with implicit int, we'd get a parse error because the
2046 // next token is obviously invalid for a type. Parse these as a case
2047 // with an invalid type specifier.
2048 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002049
Chris Lattner20a0c612009-04-14 21:34:55 +00002050 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002051 // error, do lookahead to try to do better recovery. This never applies
2052 // within a type specifier. Outside of C++, we allow this even if the
2053 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002054 // implicit int as an extension in C99 and C11.
Richard Smith2f07ad52012-05-09 20:55:26 +00002055 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith3b870382013-04-30 22:43:51 +00002056 !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002057 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002058 // If this token is valid for implicit int, e.g. "static x = 4", then
2059 // we just avoid eating the identifier, so it will be parsed as the
2060 // identifier in the declarator.
2061 return false;
2062 }
Mike Stump11289f42009-09-09 15:08:12 +00002063
Richard Smitha952ebb2012-05-15 21:01:51 +00002064 if (getLangOpts().CPlusPlus &&
2065 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2066 // Don't require a type specifier if we have the 'auto' storage class
2067 // specifier in C++98 -- we'll promote it to a type specifier.
Richard Smithfb8b7b92013-10-15 00:00:26 +00002068 if (SS)
2069 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
Richard Smitha952ebb2012-05-15 21:01:51 +00002070 return false;
2071 }
2072
Chris Lattner20a0c612009-04-14 21:34:55 +00002073 // Otherwise, if we don't consume this token, we are going to emit an
2074 // error anyway. Try to recover from various common problems. Check
2075 // to see if this was a reference to a tag name without a tag specified.
2076 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002077 //
2078 // C++ doesn't need this, and isTagName doesn't take SS.
2079 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002080 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002081 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002082
Douglas Gregor0be31a22010-07-02 17:43:08 +00002083 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002084 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002085 case DeclSpec::TST_enum:
2086 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2087 case DeclSpec::TST_union:
2088 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2089 case DeclSpec::TST_struct:
2090 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002091 case DeclSpec::TST_interface:
2092 TagName="__interface"; FixitTagName = "__interface ";
2093 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002094 case DeclSpec::TST_class:
2095 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002096 }
Mike Stump11289f42009-09-09 15:08:12 +00002097
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002098 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002099 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2100 LookupResult R(Actions, TokenName, SourceLocation(),
2101 Sema::LookupOrdinaryName);
2102
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002103 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002104 << TokenName << TagName << getLangOpts().CPlusPlus
2105 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2106
2107 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2108 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2109 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002110 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002111 << TokenName << TagName;
2112 }
Mike Stump11289f42009-09-09 15:08:12 +00002113
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002114 // Parse this as a tag as if the missing tag were present.
2115 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00002116 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002117 else
Richard Smithc5b05522012-03-12 07:56:15 +00002118 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00002119 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002120 return true;
2121 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002122 }
Mike Stump11289f42009-09-09 15:08:12 +00002123
Richard Smithfe904f02012-05-15 21:29:55 +00002124 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002125 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00002126 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
2127 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002128 // Look ahead to the next token to try to figure out what this declaration
2129 // was supposed to be.
2130 switch (NextToken().getKind()) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002131 case tok::l_paren: {
2132 // static x(4); // 'x' is not a type
2133 // x(int n); // 'x' is not a type
2134 // x (*p)[]; // 'x' is a type
2135 //
2136 // Since we're in an error case (or the rare 'implicit int in C++' MS
2137 // extension), we can afford to perform a tentative parse to determine
2138 // which case we're in.
2139 TentativeParsingAction PA(*this);
2140 ConsumeToken();
2141 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2142 PA.Revert();
Richard Smithfb8b7b92013-10-15 00:00:26 +00002143
2144 if (TPR != TPResult::False()) {
2145 // The identifier is followed by a parenthesized declarator.
2146 // It's supposed to be a type.
2147 break;
2148 }
2149
2150 // If we're in a context where we could be declaring a constructor,
2151 // check whether this is a constructor declaration with a bogus name.
2152 if (DSC == DSC_class || (DSC == DSC_top_level && SS)) {
2153 IdentifierInfo *II = Tok.getIdentifierInfo();
2154 if (Actions.isCurrentClassNameTypo(II, SS)) {
2155 Diag(Loc, diag::err_constructor_bad_name)
2156 << Tok.getIdentifierInfo() << II
2157 << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
2158 Tok.setIdentifierInfo(II);
2159 }
2160 }
2161 // Fall through.
Richard Smitha952ebb2012-05-15 21:01:51 +00002162 }
Richard Smithfb8b7b92013-10-15 00:00:26 +00002163 case tok::comma:
2164 case tok::equal:
2165 case tok::kw_asm:
2166 case tok::l_brace:
2167 case tok::l_square:
2168 case tok::semi:
2169 // This looks like a variable or function declaration. The type is
2170 // probably missing. We're done parsing decl-specifiers.
2171 if (SS)
2172 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2173 return false;
Richard Smitha952ebb2012-05-15 21:01:51 +00002174
2175 default:
2176 // This is probably supposed to be a type. This includes cases like:
2177 // int f(itn);
2178 // struct S { unsinged : 4; };
2179 break;
2180 }
2181 }
2182
Chad Rosierc1183952012-06-26 22:30:43 +00002183 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00002184 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002185 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002186 IdentifierInfo *II = Tok.getIdentifierInfo();
2187 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00002188 // The action emitted a diagnostic, so we don't have to.
2189 if (T) {
2190 // The action has suggested that the type T could be used. Set that as
2191 // the type in the declaration specifiers, consume the would-be type
2192 // name token, and we're done.
2193 const char *PrevSpec;
2194 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00002195 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00002196 DS.SetRangeEnd(Tok.getLocation());
2197 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002198 // There may be other declaration specifiers after this.
2199 return true;
2200 } else if (II != Tok.getIdentifierInfo()) {
2201 // If no type was suggested, the correction is to a keyword
2202 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002203 // There may be other declaration specifiers after this.
2204 return true;
2205 }
Chad Rosierc1183952012-06-26 22:30:43 +00002206
Douglas Gregor15e56022009-10-13 23:27:22 +00002207 // Fall through; the action had no suggestion for us.
2208 } else {
2209 // The action did not emit a diagnostic, so emit one now.
2210 SourceRange R;
2211 if (SS) R = SS->getRange();
2212 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2213 }
Mike Stump11289f42009-09-09 15:08:12 +00002214
Douglas Gregor15e56022009-10-13 23:27:22 +00002215 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002216 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002217 DS.SetRangeEnd(Tok.getLocation());
2218 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002219
Chris Lattner20a0c612009-04-14 21:34:55 +00002220 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2221 // avoid rippling error messages on subsequent uses of the same type,
2222 // could be useful if #include was forgotten.
2223 return false;
2224}
2225
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002226/// \brief Determine the declaration specifier context from the declarator
2227/// context.
2228///
2229/// \param Context the declarator context, which is one of the
2230/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002231Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002232Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2233 if (Context == Declarator::MemberContext)
2234 return DSC_class;
2235 if (Context == Declarator::FileContext)
2236 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002237 if (Context == Declarator::TrailingReturnContext)
2238 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002239 return DSC_normal;
2240}
2241
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002242/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2243///
2244/// FIXME: Simply returns an alignof() expression if the argument is a
2245/// type. Ideally, the type should be propagated directly into Sema.
2246///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002247/// [C11] type-id
2248/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002249/// [C++0x] type-id ...[opt]
2250/// [C++0x] assignment-expression ...[opt]
2251ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2252 SourceLocation &EllipsisLoc) {
2253 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002254 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002255 SourceLocation TypeLoc = Tok.getLocation();
2256 ParsedType Ty = ParseTypeName().get();
2257 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002258 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2259 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002260 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002261 ER = ParseConstantExpression();
2262
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002263 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00002264 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002265
2266 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002267}
2268
2269/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2270/// attribute to Attrs.
2271///
2272/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002273/// [C11] '_Alignas' '(' type-id ')'
2274/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002275/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2276/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002277void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002278 SourceLocation *EndLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002279 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2280 "Not an alignment-specifier!");
2281
Richard Smithd11c7a12013-01-29 01:48:07 +00002282 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2283 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002284
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002285 BalancedDelimiterTracker T(*this, tok::l_paren);
2286 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002287 return;
2288
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002289 SourceLocation EllipsisLoc;
2290 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002291 if (ArgExpr.isInvalid()) {
2292 SkipUntil(tok::r_paren);
2293 return;
2294 }
2295
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002296 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002297 if (EndLoc)
2298 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002299
Aaron Ballman00e99962013-08-31 01:11:41 +00002300 ArgsVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002301 ArgExprs.push_back(ArgExpr.release());
Aaron Ballman00e99962013-08-31 01:11:41 +00002302 Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1,
2303 AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002304}
2305
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002306/// ParseDeclarationSpecifiers
2307/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002308/// storage-class-specifier declaration-specifiers[opt]
2309/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002310/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002311/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002312/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002313/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002314///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002315/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002316/// 'typedef'
2317/// 'extern'
2318/// 'static'
2319/// 'auto'
2320/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002321/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00002322/// [C++11] 'thread_local'
2323/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002324/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002325/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002326/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002327/// [C++] 'virtual'
2328/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002329/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002330/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002331/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002332
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002333///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002334void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002335 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002336 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002337 DeclSpecContext DSContext,
2338 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002339 if (DS.getSourceRange().isInvalid()) {
2340 DS.SetRangeStart(Tok.getLocation());
2341 DS.SetRangeEnd(Tok.getLocation());
2342 }
Chad Rosierc1183952012-06-26 22:30:43 +00002343
Douglas Gregordf593fb2011-11-07 17:33:42 +00002344 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002345 bool AttrsLastTime = false;
2346 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002347 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002348 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002349 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002350 unsigned DiagID = 0;
2351
Chris Lattner4d8f8732006-11-28 05:05:08 +00002352 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002353
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002354 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002355 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002356 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002357 if (!AttrsLastTime)
2358 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002359 else {
2360 // Reject C++11 attributes that appertain to decl specifiers as
2361 // we don't support any C++11 attributes that appertain to decl
2362 // specifiers. This also conforms to what g++ 4.8 is doing.
2363 ProhibitCXX11Attributes(attrs);
2364
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002365 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002366 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002367
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002368 // If this is not a declaration specifier token, we're done reading decl
2369 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002370 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002371 return;
Mike Stump11289f42009-09-09 15:08:12 +00002372
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002373 case tok::l_square:
2374 case tok::kw_alignas:
Richard Smith4cabd042013-02-22 09:15:49 +00002375 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002376 goto DoneWithDeclSpec;
2377
2378 ProhibitAttributes(attrs);
2379 // FIXME: It would be good to recover by accepting the attributes,
2380 // but attempting to do that now would cause serious
2381 // madness in terms of diagnostics.
2382 attrs.clear();
2383 attrs.Range = SourceRange();
2384
2385 ParseCXX11Attributes(attrs);
2386 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002387 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002388
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002389 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002390 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002391 if (DS.hasTypeSpecifier()) {
2392 bool AllowNonIdentifiers
2393 = (getCurScope()->getFlags() & (Scope::ControlScope |
2394 Scope::BlockScope |
2395 Scope::TemplateParamScope |
2396 Scope::FunctionPrototypeScope |
2397 Scope::AtCatchScope)) == 0;
2398 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002399 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002400 (DSContext == DSC_class && DS.isFriendSpecified());
2401
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002402 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002403 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002404 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002405 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002406 }
2407
Douglas Gregor80039242011-02-15 20:33:25 +00002408 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2409 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2410 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002411 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002412 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002413 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002414 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002415 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002416 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002417
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002418 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002419 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002420 }
2421
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002422 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002423 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002424 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002425 if (!DS.hasTypeSpecifier())
2426 DS.SetTypeSpecError();
2427 goto DoneWithDeclSpec;
2428 }
John McCall8bc2a702010-03-01 18:20:46 +00002429 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2430 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002431 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002432
2433 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002434 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002435 goto DoneWithDeclSpec;
2436
John McCall9dab4e62009-12-12 11:40:51 +00002437 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002438 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2439 Tok.getAnnotationRange(),
2440 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002441
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002442 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002443 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002444 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002445 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002446 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002447 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002448
2449 // C++ [class.qual]p2:
2450 // In a lookup in which the constructor is an acceptable lookup
2451 // result and the nested-name-specifier nominates a class C:
2452 //
2453 // - if the name specified after the
2454 // nested-name-specifier, when looked up in C, is the
2455 // injected-class-name of C (Clause 9), or
2456 //
2457 // - if the name specified after the nested-name-specifier
2458 // is the same as the identifier or the
2459 // simple-template-id's template-name in the last
2460 // component of the nested-name-specifier,
2461 //
2462 // the name is instead considered to name the constructor of
2463 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002464 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002465 // Thus, if the template-name is actually the constructor
2466 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002467 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002468 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002469 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00002470 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002471 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002472 if (isConstructorDeclarator()) {
2473 // The user meant this to be an out-of-line constructor
2474 // definition, but template arguments are not allowed
2475 // there. Just allow this as a constructor; we'll
2476 // complain about it later.
2477 goto DoneWithDeclSpec;
2478 }
2479
2480 // The user meant this to name a type, but it actually names
2481 // a constructor with some extraneous template
2482 // arguments. Complain, then parse it as a type as the user
2483 // intended.
2484 Diag(TemplateId->TemplateNameLoc,
2485 diag::err_out_of_line_template_id_names_constructor)
2486 << TemplateId->Name;
2487 }
2488
John McCall9dab4e62009-12-12 11:40:51 +00002489 DS.getTypeSpecScope() = SS;
2490 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002491 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002492 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002493 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002494 continue;
2495 }
2496
Douglas Gregorc5790df2009-09-28 07:26:33 +00002497 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002498 DS.getTypeSpecScope() = SS;
2499 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002500 if (Tok.getAnnotationValue()) {
2501 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002502 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002503 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002504 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002505 if (isInvalid)
2506 break;
John McCallba7bf592010-08-24 05:47:05 +00002507 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002508 else
2509 DS.SetTypeSpecError();
2510 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2511 ConsumeToken(); // The typename
2512 }
2513
Douglas Gregor167fa622009-03-25 15:40:00 +00002514 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002515 goto DoneWithDeclSpec;
2516
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002517 // If we're in a context where the identifier could be a class name,
2518 // check whether this is a constructor declaration.
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002519 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002520 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002521 &SS)) {
2522 if (isConstructorDeclarator())
2523 goto DoneWithDeclSpec;
2524
2525 // As noted in C++ [class.qual]p2 (cited above), when the name
2526 // of the class is qualified in a context where it could name
2527 // a constructor, its a constructor name. However, we've
2528 // looked at the declarator, and the user probably meant this
2529 // to be a type. Complain that it isn't supposed to be treated
2530 // as a type, then proceed to parse it as a type.
2531 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2532 << Next.getIdentifierInfo();
2533 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002534
John McCallba7bf592010-08-24 05:47:05 +00002535 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2536 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002537 getCurScope(), &SS,
2538 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002539 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002540 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002541
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002542 // If the referenced identifier is not a type, then this declspec is
2543 // erroneous: We already checked about that it has no type specifier, and
2544 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002545 // typename.
David Blaikie7d170102013-05-15 07:37:26 +00002546 if (!TypeRep) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002547 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002548 ParsedAttributesWithRange Attrs(AttrFactory);
2549 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2550 if (!Attrs.empty()) {
2551 AttrsLastTime = true;
2552 attrs.takeAllFrom(Attrs);
2553 }
2554 continue;
2555 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002556 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002557 }
Mike Stump11289f42009-09-09 15:08:12 +00002558
John McCall9dab4e62009-12-12 11:40:51 +00002559 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002560 ConsumeToken(); // The C++ scope.
2561
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002562 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002563 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002564 if (isInvalid)
2565 break;
Mike Stump11289f42009-09-09 15:08:12 +00002566
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002567 DS.SetRangeEnd(Tok.getLocation());
2568 ConsumeToken(); // The typename.
2569
2570 continue;
2571 }
Mike Stump11289f42009-09-09 15:08:12 +00002572
Chris Lattnere387d9e2009-01-21 19:48:37 +00002573 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002574 if (Tok.getAnnotationValue()) {
2575 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002576 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002577 DiagID, T);
2578 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002579 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002580
Chris Lattner005fc1b2010-04-05 18:18:31 +00002581 if (isInvalid)
2582 break;
2583
Chris Lattnere387d9e2009-01-21 19:48:37 +00002584 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2585 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002586
Chris Lattnere387d9e2009-01-21 19:48:37 +00002587 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2588 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002589 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002590 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002591 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002592
Chris Lattnere387d9e2009-01-21 19:48:37 +00002593 continue;
2594 }
Mike Stump11289f42009-09-09 15:08:12 +00002595
Douglas Gregor06873092011-04-28 15:48:45 +00002596 case tok::kw___is_signed:
2597 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2598 // typically treats it as a trait. If we see __is_signed as it appears
2599 // in libstdc++, e.g.,
2600 //
2601 // static const bool __is_signed;
2602 //
2603 // then treat __is_signed as an identifier rather than as a keyword.
2604 if (DS.getTypeSpecType() == TST_bool &&
2605 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2606 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2607 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2608 Tok.setKind(tok::identifier);
2609 }
2610
2611 // We're done with the declaration-specifiers.
2612 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002613
Chris Lattner16fac4f2008-07-26 01:18:38 +00002614 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002615 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002616 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002617 // In C++, check to see if this is a scope specifier like foo::bar::, if
2618 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002619 if (getLangOpts().CPlusPlus) {
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002620 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002621 if (!DS.hasTypeSpecifier())
2622 DS.SetTypeSpecError();
2623 goto DoneWithDeclSpec;
2624 }
2625 if (!Tok.is(tok::identifier))
2626 continue;
2627 }
Mike Stump11289f42009-09-09 15:08:12 +00002628
Chris Lattner16fac4f2008-07-26 01:18:38 +00002629 // This identifier can only be a typedef name if we haven't already seen
2630 // a type-specifier. Without this check we misparse:
2631 // typedef int X; struct Y { short X; }; as 'short int'.
2632 if (DS.hasTypeSpecifier())
2633 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002634
John Thompson22334602010-02-05 00:12:22 +00002635 // Check for need to substitute AltiVec keyword tokens.
2636 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2637 break;
2638
Richard Smith3092a3b2012-05-09 18:56:43 +00002639 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2640 // allow the use of a typedef name as a type specifier.
2641 if (DS.isTypeAltiVecVector())
2642 goto DoneWithDeclSpec;
2643
John McCallba7bf592010-08-24 05:47:05 +00002644 ParsedType TypeRep =
2645 Actions.getTypeName(*Tok.getIdentifierInfo(),
2646 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002647
Chris Lattner6cc055a2009-04-12 20:42:31 +00002648 // If this is not a typedef name, don't parse it as part of the declspec,
2649 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002650 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002651 ParsedAttributesWithRange Attrs(AttrFactory);
2652 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2653 if (!Attrs.empty()) {
2654 AttrsLastTime = true;
2655 attrs.takeAllFrom(Attrs);
2656 }
2657 continue;
2658 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002659 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002660 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002661
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002662 // If we're in a context where the identifier could be a class name,
2663 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002664 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002665 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002666 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002667 goto DoneWithDeclSpec;
2668
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002669 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002670 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002671 if (isInvalid)
2672 break;
Mike Stump11289f42009-09-09 15:08:12 +00002673
Chris Lattner16fac4f2008-07-26 01:18:38 +00002674 DS.SetRangeEnd(Tok.getLocation());
2675 ConsumeToken(); // The identifier
2676
2677 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2678 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002679 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002680 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002681 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002682
Steve Naroffcd5e7822008-09-22 10:28:57 +00002683 // Need to support trailing type qualifiers (e.g. "id<p> const").
2684 // If a type specifier follows, it will be diagnosed elsewhere.
2685 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002686 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002687
2688 // type-name
2689 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002690 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002691 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002692 // This template-id does not refer to a type name, so we're
2693 // done with the type-specifiers.
2694 goto DoneWithDeclSpec;
2695 }
2696
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002697 // If we're in a context where the template-id could be a
2698 // constructor name or specialization, check whether this is a
2699 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002700 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002701 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002702 isConstructorDeclarator())
2703 goto DoneWithDeclSpec;
2704
Douglas Gregor7f741122009-02-25 19:37:18 +00002705 // Turn the template-id annotation token into a type annotation
2706 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002707 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002708 continue;
2709 }
2710
Chris Lattnere37e2332006-08-15 04:50:22 +00002711 // GNU attributes support.
2712 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002713 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002714 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002715
2716 // Microsoft declspec support.
2717 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002718 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002719 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002720
Steve Naroff44ac7772008-12-25 14:16:32 +00002721 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002722 case tok::kw___forceinline: {
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002723 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002724 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002725 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002726 // FIXME: This does not work correctly if it is set to be a declspec
2727 // attribute, and a GNU attribute is simply incorrect.
Aaron Ballman00e99962013-08-31 01:11:41 +00002728 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
2729 AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002730 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002731 }
Eli Friedman53339e02009-06-08 23:27:34 +00002732
Aaron Ballman317a77f2013-05-22 23:25:32 +00002733 case tok::kw___sptr:
2734 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00002735 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002736 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002737 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002738 case tok::kw___cdecl:
2739 case tok::kw___stdcall:
2740 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002741 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002742 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002743 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002744 continue;
2745
Dawn Perchik335e16b2010-09-03 01:29:35 +00002746 // Borland single token adornments.
2747 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002748 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002749 continue;
2750
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002751 // OpenCL single token adornments.
2752 case tok::kw___kernel:
2753 ParseOpenCLAttributes(DS.getAttributes());
2754 continue;
2755
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002756 // storage-class-specifier
2757 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002758 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2759 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002760 break;
2761 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00002762 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002763 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002764 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2765 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002766 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002767 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002768 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2769 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002770 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002771 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00002772 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002773 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002774 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2775 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002776 break;
2777 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002778 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002779 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002780 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2781 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002782 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002783 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002784 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002785 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002786 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2787 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002788 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002789 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2790 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002791 break;
2792 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002793 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2794 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002795 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002796 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002797 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2798 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002799 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002800 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00002801 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2802 PrevSpec, DiagID);
2803 break;
2804 case tok::kw_thread_local:
2805 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2806 PrevSpec, DiagID);
2807 break;
2808 case tok::kw__Thread_local:
2809 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2810 Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002811 break;
Mike Stump11289f42009-09-09 15:08:12 +00002812
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002813 // function-specifier
2814 case tok::kw_inline:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002815 isInvalid = DS.setFunctionSpecInline(Loc);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002816 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002817 case tok::kw_virtual:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002818 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002819 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002820 case tok::kw_explicit:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002821 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002822 break;
Richard Smith0015f092013-01-17 22:16:11 +00002823 case tok::kw__Noreturn:
2824 if (!getLangOpts().C11)
2825 Diag(Loc, diag::ext_c11_noreturn);
2826 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2827 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002828
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002829 // alignment-specifier
2830 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002831 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002832 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002833 ParseAlignmentSpecifier(DS.getAttributes());
2834 continue;
2835
Anders Carlssoncd8db412009-05-06 04:46:28 +00002836 // friend
2837 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002838 if (DSContext == DSC_class)
2839 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2840 else {
2841 PrevSpec = ""; // not actually used by the diagnostic
2842 DiagID = diag::err_friend_invalid_in_context;
2843 isInvalid = true;
2844 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002845 break;
Mike Stump11289f42009-09-09 15:08:12 +00002846
Douglas Gregor26701a42011-09-09 02:06:17 +00002847 // Modules
2848 case tok::kw___module_private__:
2849 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2850 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002851
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002852 // constexpr
2853 case tok::kw_constexpr:
2854 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2855 break;
2856
Chris Lattnere387d9e2009-01-21 19:48:37 +00002857 // type-specifier
2858 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002859 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2860 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002861 break;
2862 case tok::kw_long:
2863 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002864 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2865 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002866 else
John McCall49bfce42009-08-03 20:12:06 +00002867 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2868 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002869 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002870 case tok::kw___int64:
2871 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2872 DiagID);
2873 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002874 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002875 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2876 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002877 break;
2878 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002879 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2880 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002881 break;
2882 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002883 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2884 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002885 break;
2886 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002887 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2888 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002889 break;
2890 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002891 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2892 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002893 break;
2894 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002895 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2896 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002897 break;
2898 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002899 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2900 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002901 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00002902 case tok::kw___int128:
2903 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2904 DiagID);
2905 break;
2906 case tok::kw_half:
2907 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2908 DiagID);
2909 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002910 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002911 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2912 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002913 break;
2914 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002915 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2916 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002917 break;
2918 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002919 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2920 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002921 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002922 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002923 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2924 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002925 break;
2926 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002927 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2928 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002929 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002930 case tok::kw_bool:
2931 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002932 if (Tok.is(tok::kw_bool) &&
2933 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2934 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2935 PrevSpec = ""; // Not used by the diagnostic.
2936 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002937 // For better error recovery.
2938 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002939 isInvalid = true;
2940 } else {
2941 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2942 DiagID);
2943 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002944 break;
2945 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002946 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2947 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002948 break;
2949 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002950 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2951 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002952 break;
2953 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002954 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2955 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002956 break;
John Thompson22334602010-02-05 00:12:22 +00002957 case tok::kw___vector:
2958 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2959 break;
2960 case tok::kw___pixel:
2961 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2962 break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00002963 case tok::kw_image1d_t:
2964 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2965 PrevSpec, DiagID);
2966 break;
2967 case tok::kw_image1d_array_t:
2968 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2969 PrevSpec, DiagID);
2970 break;
2971 case tok::kw_image1d_buffer_t:
2972 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2973 PrevSpec, DiagID);
2974 break;
2975 case tok::kw_image2d_t:
2976 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2977 PrevSpec, DiagID);
2978 break;
2979 case tok::kw_image2d_array_t:
2980 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2981 PrevSpec, DiagID);
2982 break;
2983 case tok::kw_image3d_t:
2984 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2985 PrevSpec, DiagID);
2986 break;
Guy Benyei61054192013-02-07 10:55:47 +00002987 case tok::kw_sampler_t:
2988 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
2989 PrevSpec, DiagID);
2990 break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00002991 case tok::kw_event_t:
2992 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
2993 PrevSpec, DiagID);
2994 break;
John McCall39439732011-04-09 22:50:59 +00002995 case tok::kw___unknown_anytype:
2996 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2997 PrevSpec, DiagID);
2998 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002999
3000 // class-specifier:
3001 case tok::kw_class:
3002 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003003 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003004 case tok::kw_union: {
3005 tok::TokenKind Kind = Tok.getKind();
3006 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00003007
3008 // These are attributes following class specifiers.
3009 // To produce better diagnostic, we parse them when
3010 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00003011 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00003012 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00003013 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003014
3015 // If there are attributes following class specifier,
3016 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00003017 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00003018 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00003019 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003020 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003021 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003022 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003023
3024 // enum-specifier:
3025 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003026 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003027 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003028 continue;
3029
3030 // cv-qualifier:
3031 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003032 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003033 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003034 break;
3035 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003036 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003037 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003038 break;
3039 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003040 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003041 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003042 break;
3043
Douglas Gregor333489b2009-03-27 23:10:48 +00003044 // C++ typename-specifier:
3045 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003046 if (TryAnnotateTypeOrScopeToken()) {
3047 DS.SetTypeSpecError();
3048 goto DoneWithDeclSpec;
3049 }
3050 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003051 continue;
3052 break;
3053
Chris Lattnere387d9e2009-01-21 19:48:37 +00003054 // GNU typeof support.
3055 case tok::kw_typeof:
3056 ParseTypeofSpecifier(DS);
3057 continue;
3058
David Blaikie15a430a2011-12-04 05:04:18 +00003059 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003060 ParseDecltypeSpecifier(DS);
3061 continue;
3062
Alexis Hunt4a257072011-05-19 05:37:45 +00003063 case tok::kw___underlying_type:
3064 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003065 continue;
3066
3067 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003068 // C11 6.7.2.4/4:
3069 // If the _Atomic keyword is immediately followed by a left parenthesis,
3070 // it is interpreted as a type specifier (with a type name), not as a
3071 // type qualifier.
3072 if (NextToken().is(tok::l_paren)) {
3073 ParseAtomicSpecifier(DS);
3074 continue;
3075 }
3076 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3077 getLangOpts());
3078 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003079
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003080 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00003081 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003082 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003083 goto DoneWithDeclSpec;
3084 case tok::kw___private:
3085 case tok::kw___global:
3086 case tok::kw___local:
3087 case tok::kw___constant:
3088 case tok::kw___read_only:
3089 case tok::kw___write_only:
3090 case tok::kw___read_write:
3091 ParseOpenCLQualifiers(DS);
3092 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003093
Steve Naroffcfdf6162008-06-05 00:02:44 +00003094 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003095 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003096 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3097 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003098 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00003099 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003100
Douglas Gregor3a001f42010-11-19 17:10:50 +00003101 if (!ParseObjCProtocolQualifiers(DS))
3102 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3103 << FixItHint::CreateInsertion(Loc, "id")
3104 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00003105
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003106 // Need to support trailing type qualifiers (e.g. "id<p> const").
3107 // If a type specifier follows, it will be diagnosed elsewhere.
3108 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003109 }
John McCall49bfce42009-08-03 20:12:06 +00003110 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003111 if (isInvalid) {
3112 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003113 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003114
Douglas Gregora05f5ab2010-08-23 14:34:43 +00003115 if (DiagID == diag::ext_duplicate_declspec)
3116 Diag(Tok, DiagID)
3117 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3118 else
3119 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003120 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003121
Chris Lattner2e232092008-03-13 06:29:04 +00003122 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003123 if (DiagID != diag::err_bool_redeclaration)
3124 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003125
3126 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003127 }
3128}
Douglas Gregoreb31f392008-12-01 23:54:00 +00003129
Chris Lattner70ae4912007-10-29 04:42:53 +00003130/// ParseStructDeclaration - Parse a struct declaration without the terminating
3131/// semicolon.
3132///
Chris Lattner90a26b02007-01-23 04:38:16 +00003133/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00003134/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00003135/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00003136/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00003137/// struct-declarator-list:
3138/// struct-declarator
3139/// struct-declarator-list ',' struct-declarator
3140/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3141/// struct-declarator:
3142/// declarator
3143/// [GNU] declarator attributes[opt]
3144/// declarator[opt] ':' constant-expression
3145/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3146///
Chris Lattnera12405b2008-04-10 06:46:29 +00003147void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003148ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00003149
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003150 if (Tok.is(tok::kw___extension__)) {
3151 // __extension__ silences extension warnings in the subexpression.
3152 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00003153 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003154 return ParseStructDeclaration(DS, Fields);
3155 }
Mike Stump11289f42009-09-09 15:08:12 +00003156
Steve Naroff97170802007-08-20 22:28:22 +00003157 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00003158 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003159
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00003160 // If there are no declarators, this is a free-standing declaration
3161 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00003162 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003163 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3164 DS);
3165 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00003166 return;
3167 }
3168
3169 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00003170 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00003171 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00003172 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003173 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00003174 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00003175
Bill Wendling44426052012-12-20 19:22:21 +00003176 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00003177 if (!FirstDeclarator)
3178 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00003179
Steve Naroff97170802007-08-20 22:28:22 +00003180 /// struct-declarator: declarator
3181 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003182 if (Tok.isNot(tok::colon)) {
3183 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3184 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00003185 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003186 }
Mike Stump11289f42009-09-09 15:08:12 +00003187
Chris Lattner76c72282007-10-09 17:33:22 +00003188 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00003189 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00003190 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003191 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00003192 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00003193 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00003194 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00003195 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003196
Steve Naroff97170802007-08-20 22:28:22 +00003197 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003198 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003199
John McCallcfefb6d2009-11-03 02:38:08 +00003200 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00003201 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00003202
Steve Naroff97170802007-08-20 22:28:22 +00003203 // If we don't have a comma, it is either the end of the list (a ';')
3204 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00003205 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00003206 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003207
Steve Naroff97170802007-08-20 22:28:22 +00003208 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00003209 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003210
John McCallcfefb6d2009-11-03 02:38:08 +00003211 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003212 }
Steve Naroff97170802007-08-20 22:28:22 +00003213}
3214
3215/// ParseStructUnionBody
3216/// struct-contents:
3217/// struct-declaration-list
3218/// [EXT] empty
3219/// [GNU] "struct-declaration-list" without terminatoring ';'
3220/// struct-declaration-list:
3221/// struct-declaration
3222/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003223/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003224///
Chris Lattner1300fb92007-01-23 23:42:53 +00003225void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003226 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003227 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3228 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00003229 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00003230
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003231 BalancedDelimiterTracker T(*this, tok::l_brace);
3232 if (T.consumeOpen())
3233 return;
Mike Stump11289f42009-09-09 15:08:12 +00003234
Douglas Gregor658b9552009-01-09 22:42:13 +00003235 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003236 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003237
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003238 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003239
Chris Lattner7b9ace62007-01-23 20:11:08 +00003240 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00003241 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003242 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003243
Chris Lattner736ed5d2007-06-09 05:59:07 +00003244 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003245 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003246 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003247 continue;
3248 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003249
Andy Gibbsc804e082013-04-03 09:46:04 +00003250 // Parse _Static_assert declaration.
3251 if (Tok.is(tok::kw__Static_assert)) {
3252 SourceLocation DeclEnd;
3253 ParseStaticAssertDeclaration(DeclEnd);
3254 continue;
3255 }
3256
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00003257 if (Tok.is(tok::annot_pragma_pack)) {
3258 HandlePragmaPack();
3259 continue;
3260 }
3261
3262 if (Tok.is(tok::annot_pragma_align)) {
3263 HandlePragmaAlign();
3264 continue;
3265 }
3266
John McCallcfefb6d2009-11-03 02:38:08 +00003267 if (!Tok.is(tok::at)) {
3268 struct CFieldCallback : FieldCallback {
3269 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003270 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003271 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003272
John McCall48871652010-08-21 09:40:31 +00003273 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003274 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003275 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3276
Eli Friedman934dbbf2012-08-08 23:53:27 +00003277 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003278 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003279 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003280 FD.D.getDeclSpec().getSourceRange().getBegin(),
3281 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003282 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003283 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003284 }
John McCallcfefb6d2009-11-03 02:38:08 +00003285 } Callback(*this, TagDecl, FieldDecls);
3286
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003287 // Parse all the comma separated declarators.
3288 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003289 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003290 } else { // Handle @defs
3291 ConsumeToken();
3292 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3293 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00003294 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003295 continue;
3296 }
3297 ConsumeToken();
3298 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3299 if (!Tok.is(tok::identifier)) {
3300 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00003301 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003302 continue;
3303 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003304 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003305 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003306 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003307 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3308 ConsumeToken();
3309 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00003310 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003311
Chris Lattner76c72282007-10-09 17:33:22 +00003312 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003313 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00003314 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003315 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003316 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003317 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00003318 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3319 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00003320 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00003321 // If we stopped at a ';', eat it.
3322 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00003323 }
3324 }
Mike Stump11289f42009-09-09 15:08:12 +00003325
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003326 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003327
John McCall084e83d2011-03-24 11:26:52 +00003328 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003329 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003330 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003331
Douglas Gregor0be31a22010-07-02 17:43:08 +00003332 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003333 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003334 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003335 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003336 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003337 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3338 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003339}
3340
Chris Lattner3b561a32006-08-13 00:12:11 +00003341/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003342/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003343/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003344///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003345/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3346/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003347/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3348/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003349/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003350/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003351///
Richard Smith7d137e32012-03-23 03:33:32 +00003352/// [C++11] enum-head '{' enumerator-list[opt] '}'
3353/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003354///
Richard Smith7d137e32012-03-23 03:33:32 +00003355/// enum-head: [C++11]
3356/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3357/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3358/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003359///
Richard Smith7d137e32012-03-23 03:33:32 +00003360/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003361/// 'enum'
3362/// 'enum' 'class'
3363/// 'enum' 'struct'
3364///
Richard Smith7d137e32012-03-23 03:33:32 +00003365/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003366/// ':' type-specifier-seq
3367///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003368/// [C++] elaborated-type-specifier:
3369/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3370///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003371void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003372 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003373 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003374 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003375 if (Tok.is(tok::code_completion)) {
3376 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003377 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003378 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003379 }
John McCallcb432fa2011-07-06 05:58:41 +00003380
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003381 // If attributes exist after tag, parse them.
3382 ParsedAttributesWithRange attrs(AttrFactory);
3383 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003384 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003385
3386 // If declspecs exist after tag, parse them.
3387 while (Tok.is(tok::kw___declspec))
3388 ParseMicrosoftDeclSpec(attrs);
3389
Richard Smith0f8ee222012-01-10 01:33:14 +00003390 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003391 bool IsScopedUsingClassTag = false;
3392
John McCallbeae29a2012-06-23 22:30:04 +00003393 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieud0d87b52013-04-23 02:47:36 +00003394 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3395 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3396 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003397 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003398 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003399
Bill Wendling44426052012-12-20 19:22:21 +00003400 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003401 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003402 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003403
3404 // They are allowed afterwards, though.
3405 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003406 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003407 while (Tok.is(tok::kw___declspec))
3408 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003409 }
Richard Smith7d137e32012-03-23 03:33:32 +00003410
John McCall6347b682012-05-07 06:16:58 +00003411 // C++11 [temp.explicit]p12:
3412 // The usual access controls do not apply to names used to specify
3413 // explicit instantiations.
3414 // We extend this to also cover explicit specializations. Note that
3415 // we don't suppress if this turns out to be an elaborated type
3416 // specifier.
3417 bool shouldDelayDiagsInTag =
3418 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3419 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3420 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003421
Richard Smithbfdb1082012-03-12 08:56:40 +00003422 // Enum definitions should not be parsed in a trailing-return-type.
3423 bool AllowDeclaration = DSC != DSC_trailing;
3424
3425 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003426 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003427 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003428
Abramo Bagnarad7548482010-05-19 21:37:53 +00003429 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003430 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003431 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3432 // if a fixed underlying type is allowed.
3433 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003434
3435 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith1d4b2e12013-04-01 21:43:41 +00003436 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00003437 return;
3438
3439 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003440 Diag(Tok, diag::err_expected_ident);
3441 if (Tok.isNot(tok::l_brace)) {
3442 // Has no name and is not a definition.
3443 // Skip the rest of this declarator, up until the comma or semicolon.
3444 SkipUntil(tok::comma, true);
3445 return;
3446 }
3447 }
3448 }
Mike Stump11289f42009-09-09 15:08:12 +00003449
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003450 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003451 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003452 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003453 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003454
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003455 // Skip the rest of this declarator, up until the comma or semicolon.
3456 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00003457 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003458 }
Mike Stump11289f42009-09-09 15:08:12 +00003459
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003460 // If an identifier is present, consume and remember it.
3461 IdentifierInfo *Name = 0;
3462 SourceLocation NameLoc;
3463 if (Tok.is(tok::identifier)) {
3464 Name = Tok.getIdentifierInfo();
3465 NameLoc = ConsumeToken();
3466 }
Mike Stump11289f42009-09-09 15:08:12 +00003467
Richard Smith0f8ee222012-01-10 01:33:14 +00003468 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003469 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3470 // declaration of a scoped enumeration.
3471 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003472 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003473 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003474 }
3475
John McCall6347b682012-05-07 06:16:58 +00003476 // Okay, end the suppression area. We'll decide whether to emit the
3477 // diagnostics in a second.
3478 if (shouldDelayDiagsInTag)
3479 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003480
Douglas Gregor0bf31402010-10-08 23:50:27 +00003481 TypeResult BaseType;
3482
Douglas Gregord1f69f62010-12-01 17:42:47 +00003483 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003484 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003485 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003486 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003487 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003488 // If we're in class scope, this can either be an enum declaration with
3489 // an underlying type, or a declaration of a bitfield member. We try to
3490 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003491 // (integer literal, sizeof); if it's still ambiguous, we then consider
3492 // anything that's a simple-type-specifier followed by '(' as an
3493 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003494 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003495 EnterExpressionEvaluationContext Unevaluated(Actions,
3496 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003497 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003498 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003499 // bit-field. This is the common case.
3500 if (TPR == TPResult::True())
3501 PossibleBitfield = true;
3502 // If the next token starts a type-specifier-seq, it may be either a
3503 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003504 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003505 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003506 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003507 GetLookAheadToken(2).getKind() == tok::semi) {
3508 // Consume the ':'.
3509 ConsumeToken();
3510 } else {
3511 // We have the start of a type-specifier-seq, so we have to perform
3512 // tentative parsing to determine whether we have an expression or a
3513 // type.
3514 TentativeParsingAction TPA(*this);
3515
3516 // Consume the ':'.
3517 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003518
3519 // If we see a type specifier followed by an open-brace, we have an
3520 // ambiguity between an underlying type and a C++11 braced
3521 // function-style cast. Resolve this by always treating it as an
3522 // underlying type.
3523 // FIXME: The standard is not entirely clear on how to disambiguate in
3524 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003525 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003526 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003527 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003528 // We'll parse this as a bitfield later.
3529 PossibleBitfield = true;
3530 TPA.Revert();
3531 } else {
3532 // We have a type-specifier-seq.
3533 TPA.Commit();
3534 }
3535 }
3536 } else {
3537 // Consume the ':'.
3538 ConsumeToken();
3539 }
3540
3541 if (!PossibleBitfield) {
3542 SourceRange Range;
3543 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003544
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003545 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003546 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003547 } else if (!getLangOpts().ObjC2) {
3548 if (getLangOpts().CPlusPlus)
3549 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3550 else
3551 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3552 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003553 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003554 }
3555
Richard Smith0f8ee222012-01-10 01:33:14 +00003556 // There are four options here. If we have 'friend enum foo;' then this is a
3557 // friend declaration, and cannot have an accompanying definition. If we have
3558 // 'enum foo;', then this is a forward declaration. If we have
3559 // 'enum foo {...' then this is a definition. Otherwise we have something
3560 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003561 //
3562 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3563 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3564 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3565 //
John McCallfaf5fb42010-08-26 23:41:50 +00003566 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003567 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003568 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003569 } else if (Tok.is(tok::l_brace)) {
3570 if (DS.isFriendSpecified()) {
3571 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3572 << SourceRange(DS.getFriendSpecLoc());
3573 ConsumeBrace();
3574 SkipUntil(tok::r_brace);
3575 TUK = Sema::TUK_Friend;
3576 } else {
3577 TUK = Sema::TUK_Definition;
3578 }
Richard Smith369b9f92012-06-25 21:37:02 +00003579 } else if (DSC != DSC_type_specifier &&
3580 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003581 (Tok.isAtStartOfLine() &&
3582 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003583 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3584 if (Tok.isNot(tok::semi)) {
3585 // A semicolon was missing after this declaration. Diagnose and recover.
3586 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3587 "enum");
3588 PP.EnterToken(Tok);
3589 Tok.setKind(tok::semi);
3590 }
John McCall6347b682012-05-07 06:16:58 +00003591 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003592 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003593 }
3594
3595 // If this is an elaborated type specifier, and we delayed
3596 // diagnostics before, just merge them into the current pool.
3597 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3598 diagsFromTag.redelay();
3599 }
Richard Smith7d137e32012-03-23 03:33:32 +00003600
3601 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003602 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003603 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003604 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003605 // Skip the rest of this declarator, up until the comma or semicolon.
3606 Diag(Tok, diag::err_enum_template);
3607 SkipUntil(tok::comma, true);
3608 return;
3609 }
3610
3611 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3612 // Enumerations can't be explicitly instantiated.
3613 DS.SetTypeSpecError();
3614 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3615 return;
3616 }
3617
3618 assert(TemplateInfo.TemplateParams && "no template parameters");
3619 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3620 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003621 }
Chad Rosierc1183952012-06-26 22:30:43 +00003622
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003623 if (TUK == Sema::TUK_Reference)
3624 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003625
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003626 if (!Name && TUK != Sema::TUK_Definition) {
3627 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003628
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003629 // Skip the rest of this declarator, up until the comma or semicolon.
3630 SkipUntil(tok::comma, true);
3631 return;
3632 }
Richard Smith7d137e32012-03-23 03:33:32 +00003633
Douglas Gregord6ab8742009-05-28 23:31:59 +00003634 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003635 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003636 const char *PrevSpec = 0;
3637 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003638 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003639 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003640 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003641 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003642 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003643
Douglas Gregorba41d012010-04-24 16:38:41 +00003644 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003645 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003646 // dependent tag.
3647 if (!Name) {
3648 DS.SetTypeSpecError();
3649 Diag(Tok, diag::err_expected_type_name_after_typename);
3650 return;
3651 }
Chad Rosierc1183952012-06-26 22:30:43 +00003652
Douglas Gregor0be31a22010-07-02 17:43:08 +00003653 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003654 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003655 NameLoc);
3656 if (Type.isInvalid()) {
3657 DS.SetTypeSpecError();
3658 return;
3659 }
Chad Rosierc1183952012-06-26 22:30:43 +00003660
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003661 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3662 NameLoc.isValid() ? NameLoc : StartLoc,
3663 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003664 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003665
Douglas Gregorba41d012010-04-24 16:38:41 +00003666 return;
3667 }
Mike Stump11289f42009-09-09 15:08:12 +00003668
John McCall48871652010-08-21 09:40:31 +00003669 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003670 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003671 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003672 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003673 ConsumeBrace();
3674 SkipUntil(tok::r_brace);
3675 }
Chad Rosierc1183952012-06-26 22:30:43 +00003676
Douglas Gregorba41d012010-04-24 16:38:41 +00003677 DS.SetTypeSpecError();
3678 return;
3679 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003680
Richard Smith369b9f92012-06-25 21:37:02 +00003681 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003682 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003683
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003684 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3685 NameLoc.isValid() ? NameLoc : StartLoc,
3686 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003687 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003688}
3689
Chris Lattnerc1915e22007-01-25 07:29:02 +00003690/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3691/// enumerator-list:
3692/// enumerator
3693/// enumerator-list ',' enumerator
3694/// enumerator:
3695/// enumeration-constant
3696/// enumeration-constant '=' constant-expression
3697/// enumeration-constant:
3698/// identifier
3699///
John McCall48871652010-08-21 09:40:31 +00003700void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003701 // Enter the scope of the enum body and start the definition.
3702 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003703 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003704
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003705 BalancedDelimiterTracker T(*this, tok::l_brace);
3706 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003707
Chris Lattner37256fb2007-08-27 17:24:30 +00003708 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003709 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003710 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003711
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003712 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003713
John McCall48871652010-08-21 09:40:31 +00003714 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003715
Chris Lattnerc1915e22007-01-25 07:29:02 +00003716 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003717 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003718 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3719 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003720
John McCall811a0f52010-10-22 23:36:17 +00003721 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003722 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003723 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003724 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003725 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003726
Chris Lattnerc1915e22007-01-25 07:29:02 +00003727 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003728 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003729 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003730
Chris Lattner76c72282007-10-09 17:33:22 +00003731 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003732 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003733 AssignedVal = ParseConstantExpression();
3734 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00003735 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003736 }
Mike Stump11289f42009-09-09 15:08:12 +00003737
Chris Lattnerc1915e22007-01-25 07:29:02 +00003738 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003739 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3740 LastEnumConstDecl,
3741 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003742 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003743 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003744 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003745
Chris Lattner4ef40012007-06-11 01:28:17 +00003746 EnumConstantDecls.push_back(EnumConstDecl);
3747 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003748
Douglas Gregorce66d022010-09-07 14:51:08 +00003749 if (Tok.is(tok::identifier)) {
3750 // We're missing a comma between enumerators.
3751 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003752 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003753 << FixItHint::CreateInsertion(Loc, ", ");
3754 continue;
3755 }
Chad Rosierc1183952012-06-26 22:30:43 +00003756
Chris Lattner76c72282007-10-09 17:33:22 +00003757 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003758 break;
3759 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003760
Richard Smith5d164bc2011-10-15 05:09:34 +00003761 if (Tok.isNot(tok::identifier)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003762 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003763 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3764 diag::ext_enumerator_list_comma_cxx :
3765 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003766 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003767 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003768 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3769 << FixItHint::CreateRemoval(CommaLoc);
3770 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003771 }
Mike Stump11289f42009-09-09 15:08:12 +00003772
Chris Lattnerc1915e22007-01-25 07:29:02 +00003773 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003774 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003775
Chris Lattnerc1915e22007-01-25 07:29:02 +00003776 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003777 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003778 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003779
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003780 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenkoe5fde992013-04-27 20:23:52 +00003781 EnumDecl, EnumConstantDecls,
3782 getCurScope(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003783 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003784
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003785 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003786 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3787 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003788
3789 // The next token must be valid after an enum definition. If not, a ';'
3790 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003791 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3792 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00003793 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3794 // Push this token back into the preprocessor and change our current token
3795 // to ';' so that the rest of the code recovers as though there were an
3796 // ';' after the definition.
3797 PP.EnterToken(Tok);
3798 Tok.setKind(tok::semi);
3799 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003800}
Chris Lattner3b561a32006-08-13 00:12:11 +00003801
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003802/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003803/// start of a type-qualifier-list.
3804bool Parser::isTypeQualifier() const {
3805 switch (Tok.getKind()) {
3806 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003807
3808 // type-qualifier only in OpenCL
3809 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003810 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003811
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003812 // type-qualifier
3813 case tok::kw_const:
3814 case tok::kw_volatile:
3815 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003816 case tok::kw___private:
3817 case tok::kw___local:
3818 case tok::kw___global:
3819 case tok::kw___constant:
3820 case tok::kw___read_only:
3821 case tok::kw___read_write:
3822 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003823 return true;
3824 }
3825}
3826
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003827/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3828/// is definitely a type-specifier. Return false if it isn't part of a type
3829/// specifier or if we're not sure.
3830bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3831 switch (Tok.getKind()) {
3832 default: return false;
3833 // type-specifiers
3834 case tok::kw_short:
3835 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003836 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003837 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003838 case tok::kw_signed:
3839 case tok::kw_unsigned:
3840 case tok::kw__Complex:
3841 case tok::kw__Imaginary:
3842 case tok::kw_void:
3843 case tok::kw_char:
3844 case tok::kw_wchar_t:
3845 case tok::kw_char16_t:
3846 case tok::kw_char32_t:
3847 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003848 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003849 case tok::kw_float:
3850 case tok::kw_double:
3851 case tok::kw_bool:
3852 case tok::kw__Bool:
3853 case tok::kw__Decimal32:
3854 case tok::kw__Decimal64:
3855 case tok::kw__Decimal128:
3856 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003857
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003858 // OpenCL specific types:
3859 case tok::kw_image1d_t:
3860 case tok::kw_image1d_array_t:
3861 case tok::kw_image1d_buffer_t:
3862 case tok::kw_image2d_t:
3863 case tok::kw_image2d_array_t:
3864 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003865 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003866 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003867
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003868 // struct-or-union-specifier (C99) or class-specifier (C++)
3869 case tok::kw_class:
3870 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003871 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003872 case tok::kw_union:
3873 // enum-specifier
3874 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00003875
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003876 // typedef-name
3877 case tok::annot_typename:
3878 return true;
3879 }
3880}
3881
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003882/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003883/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003884bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003885 switch (Tok.getKind()) {
3886 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003887
Chris Lattner020bab92009-01-04 23:41:41 +00003888 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003889 if (TryAltiVecVectorToken())
3890 return true;
3891 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003892 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003893 // Annotate typenames and C++ scope specifiers. If we get one, just
3894 // recurse to handle whatever we get.
3895 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003896 return true;
3897 if (Tok.is(tok::identifier))
3898 return false;
3899 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003900
Chris Lattner020bab92009-01-04 23:41:41 +00003901 case tok::coloncolon: // ::foo::bar
3902 if (NextToken().is(tok::kw_new) || // ::new
3903 NextToken().is(tok::kw_delete)) // ::delete
3904 return false;
3905
Chris Lattner020bab92009-01-04 23:41:41 +00003906 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003907 return true;
3908 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003909
Chris Lattnere37e2332006-08-15 04:50:22 +00003910 // GNU attributes support.
3911 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003912 // GNU typeof support.
3913 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003914
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003915 // type-specifiers
3916 case tok::kw_short:
3917 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003918 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003919 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003920 case tok::kw_signed:
3921 case tok::kw_unsigned:
3922 case tok::kw__Complex:
3923 case tok::kw__Imaginary:
3924 case tok::kw_void:
3925 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003926 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003927 case tok::kw_char16_t:
3928 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003929 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003930 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003931 case tok::kw_float:
3932 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003933 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003934 case tok::kw__Bool:
3935 case tok::kw__Decimal32:
3936 case tok::kw__Decimal64:
3937 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003938 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003939
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003940 // OpenCL specific types:
3941 case tok::kw_image1d_t:
3942 case tok::kw_image1d_array_t:
3943 case tok::kw_image1d_buffer_t:
3944 case tok::kw_image2d_t:
3945 case tok::kw_image2d_array_t:
3946 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003947 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003948 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003949
Chris Lattner861a2262008-04-13 18:59:07 +00003950 // struct-or-union-specifier (C99) or class-specifier (C++)
3951 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003952 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003953 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003954 case tok::kw_union:
3955 // enum-specifier
3956 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003957
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003958 // type-qualifier
3959 case tok::kw_const:
3960 case tok::kw_volatile:
3961 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003962
John McCallea0a39e2012-11-14 00:49:39 +00003963 // Debugger support.
3964 case tok::kw___unknown_anytype:
3965
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003966 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00003967 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003968 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003969
Chris Lattner409bf7d2008-10-20 00:25:30 +00003970 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3971 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003972 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003973
Steve Naroff44ac7772008-12-25 14:16:32 +00003974 case tok::kw___cdecl:
3975 case tok::kw___stdcall:
3976 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003977 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003978 case tok::kw___w64:
3979 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003980 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003981 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003982 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003983
3984 case tok::kw___private:
3985 case tok::kw___local:
3986 case tok::kw___global:
3987 case tok::kw___constant:
3988 case tok::kw___read_only:
3989 case tok::kw___read_write:
3990 case tok::kw___write_only:
3991
Eli Friedman53339e02009-06-08 23:27:34 +00003992 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003993
3994 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003995 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00003996
Richard Smith8e1ac332013-03-28 01:55:44 +00003997 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00003998 case tok::kw__Atomic:
3999 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004000 }
4001}
4002
Chris Lattneracd58a32006-08-06 17:24:14 +00004003/// isDeclarationSpecifier() - Return true if the current token is part of a
4004/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004005///
4006/// \param DisambiguatingWithExpression True to indicate that the purpose of
4007/// this check is to disambiguate between an expression and a declaration.
4008bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004009 switch (Tok.getKind()) {
4010 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004011
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004012 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004013 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004014
Chris Lattner020bab92009-01-04 23:41:41 +00004015 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00004016 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004017 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004018 return false;
John Thompson22334602010-02-05 00:12:22 +00004019 if (TryAltiVecVectorToken())
4020 return true;
4021 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00004022 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004023 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004024 // Annotate typenames and C++ scope specifiers. If we get one, just
4025 // recurse to handle whatever we get.
4026 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004027 return true;
4028 if (Tok.is(tok::identifier))
4029 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004030
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004031 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004032 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004033 // expression is permitted, then this is probably a class message send
4034 // missing the initial '['. In this case, we won't consider this to be
4035 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004036 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004037 isStartOfObjCClassMessageMissingOpenBracket())
4038 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004039
John McCall1f476a12010-02-26 08:45:28 +00004040 return isDeclarationSpecifier();
4041
Chris Lattner020bab92009-01-04 23:41:41 +00004042 case tok::coloncolon: // ::foo::bar
4043 if (NextToken().is(tok::kw_new) || // ::new
4044 NextToken().is(tok::kw_delete)) // ::delete
4045 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004046
Chris Lattner020bab92009-01-04 23:41:41 +00004047 // Annotate typenames and C++ scope specifiers. If we get one, just
4048 // recurse to handle whatever we get.
4049 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004050 return true;
4051 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004052
Chris Lattneracd58a32006-08-06 17:24:14 +00004053 // storage-class-specifier
4054 case tok::kw_typedef:
4055 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004056 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004057 case tok::kw_static:
4058 case tok::kw_auto:
4059 case tok::kw_register:
4060 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004061 case tok::kw_thread_local:
4062 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004063
Douglas Gregor26701a42011-09-09 02:06:17 +00004064 // Modules
4065 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00004066
John McCallea0a39e2012-11-14 00:49:39 +00004067 // Debugger support
4068 case tok::kw___unknown_anytype:
4069
Chris Lattneracd58a32006-08-06 17:24:14 +00004070 // type-specifiers
4071 case tok::kw_short:
4072 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004073 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004074 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00004075 case tok::kw_signed:
4076 case tok::kw_unsigned:
4077 case tok::kw__Complex:
4078 case tok::kw__Imaginary:
4079 case tok::kw_void:
4080 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004081 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004082 case tok::kw_char16_t:
4083 case tok::kw_char32_t:
4084
Chris Lattneracd58a32006-08-06 17:24:14 +00004085 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004086 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00004087 case tok::kw_float:
4088 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004089 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00004090 case tok::kw__Bool:
4091 case tok::kw__Decimal32:
4092 case tok::kw__Decimal64:
4093 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004094 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004095
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004096 // OpenCL specific types:
4097 case tok::kw_image1d_t:
4098 case tok::kw_image1d_array_t:
4099 case tok::kw_image1d_buffer_t:
4100 case tok::kw_image2d_t:
4101 case tok::kw_image2d_array_t:
4102 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00004103 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00004104 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00004105
Chris Lattner861a2262008-04-13 18:59:07 +00004106 // struct-or-union-specifier (C99) or class-specifier (C++)
4107 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00004108 case tok::kw_struct:
4109 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00004110 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00004111 // enum-specifier
4112 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004113
Chris Lattneracd58a32006-08-06 17:24:14 +00004114 // type-qualifier
4115 case tok::kw_const:
4116 case tok::kw_volatile:
4117 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00004118
Chris Lattneracd58a32006-08-06 17:24:14 +00004119 // function-specifier
4120 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00004121 case tok::kw_virtual:
4122 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00004123 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00004124
Richard Smith1dba27c2013-01-29 09:02:09 +00004125 // alignment-specifier
4126 case tok::kw__Alignas:
4127
Richard Smithd16fe122012-10-25 00:00:53 +00004128 // friend keyword.
4129 case tok::kw_friend:
4130
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00004131 // static_assert-declaration
4132 case tok::kw__Static_assert:
4133
Chris Lattner599e47e2007-08-09 17:01:07 +00004134 // GNU typeof support.
4135 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004136
Chris Lattner599e47e2007-08-09 17:01:07 +00004137 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00004138 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00004139
Richard Smithd16fe122012-10-25 00:00:53 +00004140 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00004141 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00004142 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00004143
Richard Smith8e1ac332013-03-28 01:55:44 +00004144 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004145 case tok::kw__Atomic:
4146 return true;
4147
Chris Lattner8b2ec162008-07-26 03:38:44 +00004148 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4149 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004150 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004151
Douglas Gregor19b7acf2011-04-27 05:41:15 +00004152 // typedef-name
4153 case tok::annot_typename:
4154 return !DisambiguatingWithExpression ||
4155 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00004156
Steve Narofff192fab2009-01-06 19:34:12 +00004157 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00004158 case tok::kw___cdecl:
4159 case tok::kw___stdcall:
4160 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004161 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004162 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004163 case tok::kw___sptr:
4164 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004165 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004166 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00004167 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004168 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004169 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004170
4171 case tok::kw___private:
4172 case tok::kw___local:
4173 case tok::kw___global:
4174 case tok::kw___constant:
4175 case tok::kw___read_only:
4176 case tok::kw___read_write:
4177 case tok::kw___write_only:
4178
Eli Friedman53339e02009-06-08 23:27:34 +00004179 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00004180 }
4181}
4182
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004183bool Parser::isConstructorDeclarator() {
4184 TentativeParsingAction TPA(*this);
4185
4186 // Parse the C++ scope specifier.
4187 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00004188 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004189 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00004190 TPA.Revert();
4191 return false;
4192 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004193
4194 // Parse the constructor name.
4195 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4196 // We already know that we have a constructor name; just consume
4197 // the token.
4198 ConsumeToken();
4199 } else {
4200 TPA.Revert();
4201 return false;
4202 }
4203
Richard Smith43f340f2012-03-27 23:05:05 +00004204 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004205 if (Tok.isNot(tok::l_paren)) {
4206 TPA.Revert();
4207 return false;
4208 }
4209 ConsumeParen();
4210
Richard Smith43f340f2012-03-27 23:05:05 +00004211 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4212 // that we have a constructor.
4213 if (Tok.is(tok::r_paren) ||
4214 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004215 TPA.Revert();
4216 return true;
4217 }
4218
Richard Smithf2163662013-09-06 00:12:20 +00004219 // A C++11 attribute here signals that we have a constructor, and is an
4220 // attribute on the first constructor parameter.
4221 if (getLangOpts().CPlusPlus11 &&
4222 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
4223 /*OuterMightBeMessageSend*/ true)) {
4224 TPA.Revert();
4225 return true;
4226 }
4227
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004228 // If we need to, enter the specified scope.
4229 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004230 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004231 DeclScopeObj.EnterDeclaratorScope();
4232
Francois Pichet79f3a872011-01-31 04:54:32 +00004233 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00004234 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004235 MaybeParseMicrosoftAttributes(Attrs);
4236
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004237 // Check whether the next token(s) are part of a declaration
4238 // specifier, in which case we have the start of a parameter and,
4239 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004240 bool IsConstructor = false;
4241 if (isDeclarationSpecifier())
4242 IsConstructor = true;
4243 else if (Tok.is(tok::identifier) ||
4244 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4245 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4246 // This might be a parenthesized member name, but is more likely to
4247 // be a constructor declaration with an invalid argument type. Keep
4248 // looking.
4249 if (Tok.is(tok::annot_cxxscope))
4250 ConsumeToken();
4251 ConsumeToken();
4252
4253 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004254 // which must have one of the following syntactic forms (see the
4255 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004256 switch (Tok.getKind()) {
4257 case tok::l_paren:
4258 // C(X ( int));
4259 case tok::l_square:
4260 // C(X [ 5]);
4261 // C(X [ [attribute]]);
4262 case tok::coloncolon:
4263 // C(X :: Y);
4264 // C(X :: *p);
4265 case tok::r_paren:
4266 // C(X )
4267 // Assume this isn't a constructor, rather than assuming it's a
4268 // constructor with an unnamed parameter of an ill-formed type.
4269 break;
4270
4271 default:
4272 IsConstructor = true;
4273 break;
4274 }
4275 }
4276
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004277 TPA.Revert();
4278 return IsConstructor;
4279}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004280
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004281/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004282/// type-qualifier-list: [C99 6.7.5]
4283/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004284/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004285/// [ only if VendorAttributesAllowed=true ]
4286/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004287/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004288/// [ only if VendorAttributesAllowed=true ]
4289/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004290/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004291/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004292///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004293void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4294 bool VendorAttributesAllowed,
Richard Smith8e1ac332013-03-28 01:55:44 +00004295 bool CXX11AttributesAllowed,
4296 bool AtomicAllowed) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004297 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004298 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004299 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004300 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004301 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004302 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004303
4304 SourceLocation EndLoc;
4305
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004306 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004307 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004308 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004309 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004310 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004311
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004312 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004313 case tok::code_completion:
4314 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004315 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004316
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004317 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004318 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004319 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004320 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004321 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004322 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004323 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004324 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004325 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004326 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004327 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004328 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00004329 case tok::kw__Atomic:
4330 if (!AtomicAllowed)
4331 goto DoneWithTypeQuals;
4332 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4333 getLangOpts());
4334 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004335
4336 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00004337 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004338 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004339 goto DoneWithTypeQuals;
4340 case tok::kw___private:
4341 case tok::kw___global:
4342 case tok::kw___local:
4343 case tok::kw___constant:
4344 case tok::kw___read_only:
4345 case tok::kw___write_only:
4346 case tok::kw___read_write:
4347 ParseOpenCLQualifiers(DS);
4348 break;
4349
Aaron Ballman317a77f2013-05-22 23:25:32 +00004350 case tok::kw___sptr:
4351 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004352 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004353 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004354 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004355 case tok::kw___cdecl:
4356 case tok::kw___stdcall:
4357 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004358 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004359 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004360 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004361 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004362 continue;
4363 }
4364 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004365 case tok::kw___pascal:
4366 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004367 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004368 continue;
4369 }
4370 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004371 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004372 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004373 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004374 continue; // do *not* consume the next token!
4375 }
4376 // otherwise, FALL THROUGH!
4377 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004378 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004379 // If this is not a type-qualifier token, we're done reading type
4380 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004381 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004382 if (EndLoc.isValid())
4383 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004384 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004385 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004386
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004387 // If the specifier combination wasn't legal, issue a diagnostic.
4388 if (isInvalid) {
4389 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004390 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004391 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004392 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004393 }
4394}
4395
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004396
4397/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4398///
4399void Parser::ParseDeclarator(Declarator &D) {
4400 /// This implements the 'declarator' production in the C grammar, then checks
4401 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004402 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004403}
4404
Richard Smith0efa75c2012-03-29 01:16:42 +00004405static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4406 if (Kind == tok::star || Kind == tok::caret)
4407 return true;
4408
4409 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4410 if (!Lang.CPlusPlus)
4411 return false;
4412
4413 return Kind == tok::amp || Kind == tok::ampamp;
4414}
4415
Sebastian Redlbd150f42008-11-21 19:14:01 +00004416/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4417/// is parsed by the function passed to it. Pass null, and the direct-declarator
4418/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004419/// ptr-operator production.
4420///
Richard Smith09f76ee2011-10-19 21:33:05 +00004421/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004422/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4423/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004424///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004425/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4426/// [C] pointer[opt] direct-declarator
4427/// [C++] direct-declarator
4428/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004429///
4430/// pointer: [C99 6.7.5]
4431/// '*' type-qualifier-list[opt]
4432/// '*' type-qualifier-list[opt] pointer
4433///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004434/// ptr-operator:
4435/// '*' cv-qualifier-seq[opt]
4436/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004437/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004438/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004439/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004440/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004441void Parser::ParseDeclaratorInternal(Declarator &D,
4442 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004443 if (Diags.hasAllExtensionsSilenced())
4444 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004445
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004446 // C++ member pointers start with a '::' or a nested-name.
4447 // Member pointers get special handling, since there's no place for the
4448 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004449 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004450 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4451 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004452 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4453 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004454 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004455 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004456
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004457 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004458 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004459 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004460 if (D.mayHaveIdentifier())
4461 D.getCXXScopeSpec() = SS;
4462 else
4463 AnnotateScopeToken(SS, true);
4464
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004465 if (DirectDeclParser)
4466 (this->*DirectDeclParser)(D);
4467 return;
4468 }
4469
4470 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004471 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004472 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004473 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004474 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004475
4476 // Recurse to parse whatever is left.
4477 ParseDeclaratorInternal(D, DirectDeclParser);
4478
4479 // Sema will have to catch (syntactically invalid) pointers into global
4480 // scope. It has to catch pointers into namespace scope anyway.
4481 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004482 Loc),
4483 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004484 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004485 return;
4486 }
4487 }
4488
4489 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004490 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004491 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004492 if (DirectDeclParser)
4493 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004494 return;
4495 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004496
Sebastian Redled0f3b02009-03-15 22:02:01 +00004497 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4498 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004499 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004500 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004501
Chris Lattner9eac9312009-03-27 04:18:06 +00004502 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004503 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004504 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004505
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004506 // FIXME: GNU attributes are not allowed here in a new-type-id.
Bill Wendling3708c182007-05-27 10:15:43 +00004507 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004508 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004509
Bill Wendling3708c182007-05-27 10:15:43 +00004510 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004511 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004512 if (Kind == tok::star)
4513 // Remember that we parsed a pointer type, and remember the type-quals.
4514 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004515 DS.getConstSpecLoc(),
4516 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004517 DS.getRestrictSpecLoc()),
4518 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004519 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004520 else
4521 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004522 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004523 Loc),
4524 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004525 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004526 } else {
4527 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004528 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004529
Sebastian Redl3b27be62009-03-23 00:00:23 +00004530 // Complain about rvalue references in C++03, but then go on and build
4531 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004532 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004533 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004534 diag::warn_cxx98_compat_rvalue_reference :
4535 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004536
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004537 // GNU-style and C++11 attributes are allowed here, as is restrict.
4538 ParseTypeQualifierListOpt(DS);
4539 D.ExtendWithDeclSpec(DS);
4540
Bill Wendling93efb222007-06-02 23:28:54 +00004541 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4542 // cv-qualifiers are introduced through the use of a typedef or of a
4543 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004544 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4545 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4546 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004547 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004548 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4549 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004550 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00004551 // 'restrict' is permitted as an extension.
4552 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4553 Diag(DS.getAtomicSpecLoc(),
4554 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00004555 }
Bill Wendling3708c182007-05-27 10:15:43 +00004556
4557 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004558 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004559
Douglas Gregor66583c52008-11-03 15:51:28 +00004560 if (D.getNumTypeObjects() > 0) {
4561 // C++ [dcl.ref]p4: There shall be no references to references.
4562 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4563 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004564 if (const IdentifierInfo *II = D.getIdentifier())
4565 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4566 << II;
4567 else
4568 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4569 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004570
Sebastian Redlbd150f42008-11-21 19:14:01 +00004571 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004572 // can go ahead and build the (technically ill-formed)
4573 // declarator: reference collapsing will take care of it.
4574 }
4575 }
4576
Richard Smith8e1ac332013-03-28 01:55:44 +00004577 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00004578 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004579 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004580 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004581 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004582 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004583}
4584
Richard Smith0efa75c2012-03-29 01:16:42 +00004585static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4586 SourceLocation EllipsisLoc) {
4587 if (EllipsisLoc.isValid()) {
4588 FixItHint Insertion;
4589 if (!D.getEllipsisLoc().isValid()) {
4590 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4591 D.setEllipsisLoc(EllipsisLoc);
4592 }
4593 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4594 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4595 }
4596}
4597
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004598/// ParseDirectDeclarator
4599/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004600/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004601/// '(' declarator ')'
4602/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004603/// [C90] direct-declarator '[' constant-expression[opt] ']'
4604/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4605/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4606/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4607/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004608/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4609/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004610/// direct-declarator '(' parameter-type-list ')'
4611/// direct-declarator '(' identifier-list[opt] ')'
4612/// [GNU] direct-declarator '(' parameter-forward-declarations
4613/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004614/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4615/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004616/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4617/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4618/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004619/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004620/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004621///
4622/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004623/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004624/// '::'[opt] nested-name-specifier[opt] type-name
4625///
4626/// id-expression: [C++ 5.1]
4627/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004628/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004629///
4630/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004631/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004632/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004633/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004634/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004635/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004636///
Richard Smith1453e312012-03-27 01:42:32 +00004637/// Note, any additional constructs added here may need corresponding changes
4638/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004639void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004640 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004641
David Blaikiebbafb8a2012-03-11 07:00:24 +00004642 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004643 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004644 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004645 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4646 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004647 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004648 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004649 }
4650
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004651 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004652 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004653 // Change the declaration context for name lookup, until this function
4654 // is exited (and the declarator has been parsed).
4655 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004656 }
4657
Douglas Gregor27b4c162010-12-23 22:44:42 +00004658 // C++0x [dcl.fct]p14:
4659 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004660 // of a parameter-declaration-clause without a preceding comma. In
4661 // this case, the ellipsis is parsed as part of the
4662 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004663 // parameter pack that has not been expanded; otherwise, it is parsed
4664 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004665 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004666 !((D.getContext() == Declarator::PrototypeContext ||
Faisal Vali2b391ab2013-09-26 19:54:12 +00004667 D.getContext() == Declarator::LambdaExprParameterContext ||
Douglas Gregor27b4c162010-12-23 22:44:42 +00004668 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004669 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00004670 !D.hasGroupingParens() &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004671 !Actions.containsUnexpandedParameterPacks(D))) {
4672 SourceLocation EllipsisLoc = ConsumeToken();
4673 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4674 // The ellipsis was put in the wrong place. Recover, and explain to
4675 // the user what they should have done.
4676 ParseDeclarator(D);
4677 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4678 return;
4679 } else
4680 D.setEllipsisLoc(EllipsisLoc);
4681
4682 // The ellipsis can't be followed by a parenthesized declarator. We
4683 // check for that in ParseParenDeclarator, after we have disambiguated
4684 // the l_paren token.
4685 }
4686
Douglas Gregor7861a802009-11-03 01:35:08 +00004687 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4688 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4689 // We found something that indicates the start of an unqualified-id.
4690 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004691 bool AllowConstructorName;
4692 if (D.getDeclSpec().hasTypeSpecifier())
4693 AllowConstructorName = false;
4694 else if (D.getCXXScopeSpec().isSet())
4695 AllowConstructorName =
4696 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004697 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004698 else
4699 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4700
Abramo Bagnara7945c982012-01-27 09:46:47 +00004701 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004702 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4703 /*EnteringContext=*/true,
4704 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004705 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004706 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004707 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004708 D.getName()) ||
4709 // Once we're past the identifier, if the scope was bad, mark the
4710 // whole declarator bad.
4711 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004712 D.SetIdentifier(0, Tok.getLocation());
4713 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004714 } else {
4715 // Parsed the unqualified-id; update range information and move along.
4716 if (D.getSourceRange().getBegin().isInvalid())
4717 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4718 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004719 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004720 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004721 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004722 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004723 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004724 "There's a C++-specific check for tok::identifier above");
4725 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4726 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4727 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004728 goto PastIdentifier;
Richard Smith9ce302e2013-07-11 05:10:21 +00004729 } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
Richard Smithf39720b2013-10-13 22:12:28 +00004730 // A virt-specifier isn't treated as an identifier if it appears after a
4731 // trailing-return-type.
4732 if (D.getContext() != Declarator::TrailingReturnContext ||
4733 !isCXX11VirtSpecifier(Tok)) {
4734 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
4735 << FixItHint::CreateRemoval(Tok.getLocation());
4736 D.SetIdentifier(0, Tok.getLocation());
4737 ConsumeToken();
4738 goto PastIdentifier;
4739 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004740 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004741
Douglas Gregor7861a802009-11-03 01:35:08 +00004742 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004743 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004744 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004745 // Example: 'char (*X)' or 'int (*XX)(void)'
4746 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004747
4748 // If the declarator was parenthesized, we entered the declarator
4749 // scope when parsing the parenthesized declarator, then exited
4750 // the scope already. Re-enter the scope, if we need to.
4751 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004752 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004753 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004754 if (!D.isInvalidType() &&
4755 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004756 // Change the declaration context for name lookup, until this function
4757 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004758 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004759 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004760 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004761 // This could be something simple like "int" (in which case the declarator
4762 // portion is empty), if an abstract-declarator is allowed.
4763 D.SetIdentifier(0, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00004764
4765 // The grammar for abstract-pack-declarator does not allow grouping parens.
4766 // FIXME: Revisit this once core issue 1488 is resolved.
4767 if (D.hasEllipsis() && D.hasGroupingParens())
4768 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4769 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00004770 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004771 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004772 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004773 if (D.getContext() == Declarator::MemberContext)
4774 Diag(Tok, diag::err_expected_member_name_or_semi)
4775 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004776 else if (getLangOpts().CPlusPlus) {
4777 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4778 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
Richard Trieu2f586962013-09-05 02:31:33 +00004779 else {
4780 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
4781 if (Tok.isAtStartOfLine() && Loc.isValid())
4782 Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
4783 << getLangOpts().CPlusPlus;
4784 else
4785 Diag(Tok, diag::err_expected_unqualified_id)
4786 << getLangOpts().CPlusPlus;
4787 }
Richard Trieu9c672672013-01-26 02:31:38 +00004788 } else
Chris Lattner6d29c102008-11-18 07:48:38 +00004789 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004790 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004791 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004792 }
Mike Stump11289f42009-09-09 15:08:12 +00004793
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004794 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004795 assert(D.isPastIdentifier() &&
4796 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004797
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004798 // Don't parse attributes unless we have parsed an unparenthesized name.
4799 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004800 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004801
Chris Lattneracd58a32006-08-06 17:24:14 +00004802 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004803 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004804 // Enter function-declaration scope, limiting any declarators to the
4805 // function prototype scope, including parameter declarators.
4806 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004807 Scope::FunctionPrototypeScope|Scope::DeclScope|
4808 (D.isFunctionDeclaratorAFunctionDeclaration()
4809 ? Scope::FunctionDeclarationScope : 0));
4810
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004811 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4812 // In such a case, check if we actually have a function declarator; if it
4813 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004814 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004815 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4816 // The name of the declarator, if any, is tentatively declared within
4817 // a possible direct initializer.
4818 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4819 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4820 TentativelyDeclaredIdentifiers.pop_back();
4821 if (!IsFunctionDecl)
4822 break;
4823 }
John McCall084e83d2011-03-24 11:26:52 +00004824 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004825 BalancedDelimiterTracker T(*this, tok::l_paren);
4826 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004827 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004828 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004829 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004830 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004831 } else {
4832 break;
4833 }
4834 }
Chad Rosierc1183952012-06-26 22:30:43 +00004835}
Chris Lattneracd58a32006-08-06 17:24:14 +00004836
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004837/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4838/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004839/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004840/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4841///
4842/// direct-declarator:
4843/// '(' declarator ')'
4844/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004845/// direct-declarator '(' parameter-type-list ')'
4846/// direct-declarator '(' identifier-list[opt] ')'
4847/// [GNU] direct-declarator '(' parameter-forward-declarations
4848/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004849///
4850void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004851 BalancedDelimiterTracker T(*this, tok::l_paren);
4852 T.consumeOpen();
4853
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004854 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004855
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004856 // Eat any attributes before we look at whether this is a grouping or function
4857 // declarator paren. If this is a grouping paren, the attribute applies to
4858 // the type being built up, for example:
4859 // int (__attribute__(()) *x)(long y)
4860 // If this ends up not being a grouping paren, the attribute applies to the
4861 // first argument, for example:
4862 // int (__attribute__(()) int x)
4863 // In either case, we need to eat any attributes to be able to determine what
4864 // sort of paren this is.
4865 //
John McCall084e83d2011-03-24 11:26:52 +00004866 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004867 bool RequiresArg = false;
4868 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004869 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004870
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004871 // We require that the argument list (if this is a non-grouping paren) be
4872 // present even if the attribute list was empty.
4873 RequiresArg = true;
4874 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00004875
Steve Naroff44ac7772008-12-25 14:16:32 +00004876 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00004877 ParseMicrosoftTypeAttributes(attrs);
4878
Dawn Perchik335e16b2010-09-03 01:29:35 +00004879 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004880 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004881 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004882
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004883 // If we haven't past the identifier yet (or where the identifier would be
4884 // stored, if this is an abstract declarator), then this is probably just
4885 // grouping parens. However, if this could be an abstract-declarator, then
4886 // this could also be the start of function arguments (consider 'void()').
4887 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004888
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004889 if (!D.mayOmitIdentifier()) {
4890 // If this can't be an abstract-declarator, this *must* be a grouping
4891 // paren, because we haven't seen the identifier yet.
4892 isGrouping = true;
4893 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00004894 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4895 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00004896 isDeclarationSpecifier() || // 'int(int)' is a function.
4897 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004898 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4899 // considered to be a type, not a K&R identifier-list.
4900 isGrouping = false;
4901 } else {
4902 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4903 isGrouping = true;
4904 }
Mike Stump11289f42009-09-09 15:08:12 +00004905
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004906 // If this is a grouping paren, handle:
4907 // direct-declarator: '(' declarator ')'
4908 // direct-declarator: '(' attributes declarator ')'
4909 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00004910 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4911 D.setEllipsisLoc(SourceLocation());
4912
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004913 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004914 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00004915 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004916 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004917 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00004918 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004919 T.getCloseLocation()),
4920 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004921
4922 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00004923
4924 // An ellipsis cannot be placed outside parentheses.
4925 if (EllipsisLoc.isValid())
4926 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4927
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004928 return;
4929 }
Mike Stump11289f42009-09-09 15:08:12 +00004930
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004931 // Okay, if this wasn't a grouping paren, it must be the start of a function
4932 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004933 // identifier (and remember where it would have been), then call into
4934 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004935 D.SetIdentifier(0, Tok.getLocation());
4936
David Blaikie15a430a2011-12-04 05:04:18 +00004937 // Enter function-declaration scope, limiting any declarators to the
4938 // function prototype scope, including parameter declarators.
4939 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004940 Scope::FunctionPrototypeScope | Scope::DeclScope |
4941 (D.isFunctionDeclaratorAFunctionDeclaration()
4942 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00004943 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00004944 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004945}
4946
4947/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4948/// declarator D up to a paren, which indicates that we are parsing function
4949/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00004950///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004951/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4952/// immediately after the open paren - they should be considered to be the
4953/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004954///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004955/// If RequiresArg is true, then the first argument of the function is required
4956/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004957///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004958/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4959/// (C++11) ref-qualifier[opt], exception-specification[opt],
4960/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4961///
4962/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00004963/// dynamic-exception-specification
4964/// noexcept-specification
4965///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004966void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004967 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004968 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00004969 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004970 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00004971 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00004972 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00004973 // lparen is already consumed!
4974 assert(D.isPastIdentifier() && "Should not call before identifier!");
4975
4976 // This should be true when the function has typed arguments.
4977 // Otherwise, it is treated as a K&R-style function.
4978 bool HasProto = false;
4979 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004980 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004981 // Remember where we see an ellipsis, if any.
4982 SourceLocation EllipsisLoc;
4983
4984 DeclSpec DS(AttrFactory);
4985 bool RefQualifierIsLValueRef = true;
4986 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00004987 SourceLocation ConstQualifierLoc;
4988 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004989 ExceptionSpecificationType ESpecType = EST_None;
4990 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004991 SmallVector<ParsedType, 2> DynamicExceptions;
4992 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004993 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004994 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00004995 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004996
James Molloy6f8780b2012-02-29 10:24:19 +00004997 Actions.ActOnStartFunctionDeclarator();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004998 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4999 EndLoc is the end location for the function declarator.
5000 They differ for trailing return types. */
5001 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005002 SourceLocation LParenLoc, RParenLoc;
5003 LParenLoc = Tracker.getOpenLocation();
5004 StartLoc = LParenLoc;
5005
Douglas Gregor9e66af42011-07-05 16:44:18 +00005006 if (isFunctionDeclaratorIdentifierList()) {
5007 if (RequiresArg)
5008 Diag(Tok, diag::err_argument_required_after_attribute);
5009
5010 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5011
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005012 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005013 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005014 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005015 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005016 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005017 if (Tok.isNot(tok::r_paren))
Faisal Vali2b391ab2013-09-26 19:54:12 +00005018 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
5019 EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005020 else if (RequiresArg)
5021 Diag(Tok, diag::err_argument_required_after_attribute);
5022
David Blaikiebbafb8a2012-03-11 07:00:24 +00005023 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005024
5025 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005026 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005027 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005028 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005029 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005030
David Blaikiebbafb8a2012-03-11 07:00:24 +00005031 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005032 // FIXME: Accept these components in any order, and produce fixits to
5033 // correct the order if the user gets it wrong. Ideally we should deal
5034 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005035
5036 // Parse cv-qualifier-seq[opt].
Richard Smith8e1ac332013-03-28 01:55:44 +00005037 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
5038 /*CXX11AttributesAllowed*/ false,
5039 /*AtomicAllowed*/ false);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005040 if (!DS.getSourceRange().getEnd().isInvalid()) {
5041 EndLoc = DS.getSourceRange().getEnd();
5042 ConstQualifierLoc = DS.getConstSpecLoc();
5043 VolatileQualifierLoc = DS.getVolatileSpecLoc();
5044 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005045
5046 // Parse ref-qualifier[opt].
5047 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005048 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005049 diag::warn_cxx98_compat_ref_qualifier :
5050 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005051
Douglas Gregor9e66af42011-07-05 16:44:18 +00005052 RefQualifierIsLValueRef = Tok.is(tok::amp);
5053 RefQualifierLoc = ConsumeToken();
5054 EndLoc = RefQualifierLoc;
5055 }
5056
Douglas Gregor3024f072012-04-16 07:05:22 +00005057 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00005058 // If a declaration declares a member function or member function
5059 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00005060 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00005061 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00005062 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00005063 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00005064 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005065 getLangOpts().CPlusPlus11 &&
Richard Smithad1bbb92013-03-15 00:41:52 +00005066 (D.getContext() == Declarator::MemberContext
5067 ? !D.getDeclSpec().isFriendSpecified()
5068 : D.getContext() == Declarator::FileContext &&
5069 D.getCXXScopeSpec().isValid() &&
5070 Actions.CurContext->isRecord());
Douglas Gregor3024f072012-04-16 07:05:22 +00005071 Sema::CXXThisScopeRAII ThisScope(Actions,
5072 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00005073 DS.getTypeQualifiers() |
Richard Smith034185c2013-04-21 01:08:50 +00005074 (D.getDeclSpec().isConstexprSpecified() &&
5075 !getLangOpts().CPlusPlus1y
Richard Smith01141a92013-01-14 01:55:13 +00005076 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00005077 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00005078
Douglas Gregor9e66af42011-07-05 16:44:18 +00005079 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00005080 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00005081 DynamicExceptions,
5082 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00005083 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005084 if (ESpecType != EST_None)
5085 EndLoc = ESpecRange.getEnd();
5086
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005087 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5088 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00005089 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005090
Douglas Gregor9e66af42011-07-05 16:44:18 +00005091 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005092 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005093 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00005094 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005095 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5096 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005097 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00005098 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00005099 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005100 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005101 }
5102 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005103 }
5104
5105 // Remember that we parsed a function type, and remember the attributes.
5106 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005107 IsAmbiguous,
5108 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005109 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005110 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005111 DS.getTypeQualifiers(),
5112 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00005113 RefQualifierLoc, ConstQualifierLoc,
5114 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00005115 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00005116 ESpecType, ESpecRange.getBegin(),
5117 DynamicExceptions.data(),
5118 DynamicExceptionRanges.data(),
5119 DynamicExceptions.size(),
5120 NoexceptExpr.isUsable() ?
5121 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005122 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005123 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005124 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00005125
5126 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005127}
5128
5129/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5130/// identifier list form for a K&R-style function: void foo(a,b,c)
5131///
5132/// Note that identifier-lists are only allowed for normal declarators, not for
5133/// abstract-declarators.
5134bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005135 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00005136 && Tok.is(tok::identifier)
5137 && !TryAltiVecVectorToken()
5138 // K&R identifier lists can't have typedefs as identifiers, per C99
5139 // 6.7.5.3p11.
5140 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5141 // Identifier lists follow a really simple grammar: the identifiers can
5142 // be followed *only* by a ", identifier" or ")". However, K&R
5143 // identifier lists are really rare in the brave new modern world, and
5144 // it is very common for someone to typo a type in a non-K&R style
5145 // list. If we are presented with something like: "void foo(intptr x,
5146 // float y)", we don't want to start parsing the function declarator as
5147 // though it is a K&R style declarator just because intptr is an
5148 // invalid type.
5149 //
5150 // To handle this, we check to see if the token after the first
5151 // identifier is a "," or ")". Only then do we parse it as an
5152 // identifier list.
5153 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5154}
5155
5156/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5157/// we found a K&R-style identifier list instead of a typed parameter list.
5158///
5159/// After returning, ParamInfo will hold the parsed parameters.
5160///
5161/// identifier-list: [C99 6.7.5]
5162/// identifier
5163/// identifier-list ',' identifier
5164///
5165void Parser::ParseFunctionDeclaratorIdentifierList(
5166 Declarator &D,
Craig Topper5603df42013-07-05 19:34:19 +00005167 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005168 // If there was no identifier specified for the declarator, either we are in
5169 // an abstract-declarator, or we are in a parameter declarator which was found
5170 // to be abstract. In abstract-declarators, identifier lists are not valid:
5171 // diagnose this.
5172 if (!D.getIdentifier())
5173 Diag(Tok, diag::ext_ident_list_in_param);
5174
5175 // Maintain an efficient lookup of params we have seen so far.
5176 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5177
5178 while (1) {
5179 // If this isn't an identifier, report the error and skip until ')'.
5180 if (Tok.isNot(tok::identifier)) {
5181 Diag(Tok, diag::err_expected_ident);
5182 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
5183 // Forget we parsed anything.
5184 ParamInfo.clear();
5185 return;
5186 }
5187
5188 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5189
5190 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5191 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5192 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5193
5194 // Verify that the argument identifier has not already been mentioned.
5195 if (!ParamsSoFar.insert(ParmII)) {
5196 Diag(Tok, diag::err_param_redefinition) << ParmII;
5197 } else {
5198 // Remember this identifier in ParamInfo.
5199 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5200 Tok.getLocation(),
5201 0));
5202 }
5203
5204 // Eat the identifier.
5205 ConsumeToken();
5206
5207 // The list continues if we see a comma.
5208 if (Tok.isNot(tok::comma))
5209 break;
5210 ConsumeToken();
5211 }
5212}
5213
5214/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5215/// after the opening parenthesis. This function will not parse a K&R-style
5216/// identifier list.
5217///
Richard Smith2620cd92012-04-11 04:01:28 +00005218/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5219/// caller parsed those arguments immediately after the open paren - they should
5220/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005221///
5222/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5223/// be the location of the ellipsis, if any was parsed.
5224///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005225/// parameter-type-list: [C99 6.7.5]
5226/// parameter-list
5227/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005228/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005229///
5230/// parameter-list: [C99 6.7.5]
5231/// parameter-declaration
5232/// parameter-list ',' parameter-declaration
5233///
5234/// parameter-declaration: [C99 6.7.5]
5235/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005236/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00005237/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00005238/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00005239/// declaration-specifiers abstract-declarator[opt]
5240/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00005241/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00005242/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00005243/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005244///
Douglas Gregor9e66af42011-07-05 16:44:18 +00005245void Parser::ParseParameterDeclarationClause(
5246 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00005247 ParsedAttributes &FirstArgAttrs,
Craig Topper5603df42013-07-05 19:34:19 +00005248 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005249 SourceLocation &EllipsisLoc) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005250 while (1) {
5251 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00005252 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5253 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00005254 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00005255 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00005256 }
Mike Stump11289f42009-09-09 15:08:12 +00005257
Chris Lattner371ed4e2008-04-06 06:57:35 +00005258 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00005259 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00005260 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005261
Richard Smith2620cd92012-04-11 04:01:28 +00005262 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00005263 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00005264
John McCall53fa7142010-12-24 02:08:15 +00005265 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00005266 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00005267
5268 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005269
5270 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00005271 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005272 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00005273 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5274 // too much hassle.
5275 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00005276
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005277 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005278
Faisal Vali2b391ab2013-09-26 19:54:12 +00005279
5280 // Parse the declarator. This is "PrototypeContext" or
5281 // "LambdaExprParameterContext", because we must accept either
5282 // 'declarator' or 'abstract-declarator' here.
5283 Declarator ParmDeclarator(DS,
5284 D.getContext() == Declarator::LambdaExprContext ?
5285 Declarator::LambdaExprParameterContext :
5286 Declarator::PrototypeContext);
5287 ParseDeclarator(ParmDeclarator);
Chris Lattner371ed4e2008-04-06 06:57:35 +00005288
5289 // Parse GNU attributes, if present.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005290 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump11289f42009-09-09 15:08:12 +00005291
Chris Lattner371ed4e2008-04-06 06:57:35 +00005292 // Remember this parsed parameter in ParamInfo.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005293 IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005294
Douglas Gregor4d87df52008-12-16 21:30:33 +00005295 // DefArgToks is used when the parsing of default arguments needs
5296 // to be delayed.
5297 CachedTokens *DefArgToks = 0;
5298
Chris Lattner371ed4e2008-04-06 06:57:35 +00005299 // If no parameter was specified, verify that *something* was specified,
5300 // otherwise we have a missing type and identifier.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005301 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 &&
5302 ParmDeclarator.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005303 // Completely missing, emit error.
5304 Diag(DSStart, diag::err_missing_param);
5305 } else {
5306 // Otherwise, we have something. Add it and let semantic analysis try
5307 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005308
Chris Lattner371ed4e2008-04-06 06:57:35 +00005309 // Inform the actions module about the parameter declarator, so it gets
5310 // added to the current scope.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005311 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(),
5312 ParmDeclarator);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005313 // Parse the default argument, if any. We parse the default
5314 // arguments in all dialects; the semantic analysis in
5315 // ActOnParamDefaultArgument will reject the default argument in
5316 // C.
5317 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005318 SourceLocation EqualLoc = Tok.getLocation();
5319
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005320 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005321 if (D.getContext() == Declarator::MemberContext) {
5322 // If we're inside a class definition, cache the tokens
5323 // corresponding to the default argument. We'll actually parse
5324 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005325 // FIXME: Can we use a smart pointer for Toks?
5326 DefArgToks = new CachedTokens;
5327
Richard Smith1fff95c2013-09-12 23:28:08 +00005328 if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005329 delete DefArgToks;
5330 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005331 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005332 } else {
5333 // Mark the end of the default argument so that we know when to
5334 // stop when we parse it later on.
5335 Token DefArgEnd;
5336 DefArgEnd.startToken();
5337 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5338 DefArgEnd.setLocation(Tok.getLocation());
5339 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005340 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005341 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005342 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005343 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005344 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005345 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005346
Chad Rosierc1183952012-06-26 22:30:43 +00005347 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005348 // used.
5349 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005350 Sema::PotentiallyEvaluatedIfUsed,
5351 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005352
Sebastian Redldb63af22012-03-14 15:54:00 +00005353 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005354 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005355 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005356 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005357 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005358 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005359 if (DefArgResult.isInvalid()) {
5360 Actions.ActOnParamDefaultArgumentError(Param);
5361 SkipUntil(tok::comma, tok::r_paren, true, true);
5362 } else {
5363 // Inform the actions module about the default argument
5364 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005365 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005366 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005367 }
5368 }
Mike Stump11289f42009-09-09 15:08:12 +00005369
5370 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Faisal Vali2b391ab2013-09-26 19:54:12 +00005371 ParmDeclarator.getIdentifierLoc(),
5372 Param, DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005373 }
5374
5375 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005376 if (Tok.isNot(tok::comma)) {
5377 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005378 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00005379
David Blaikiebbafb8a2012-03-11 07:00:24 +00005380 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005381 // We have ellipsis without a preceding ',', which is ill-formed
5382 // in C. Complain and provide the fix.
5383 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00005384 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005385 }
5386 }
Chad Rosierc1183952012-06-26 22:30:43 +00005387
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005388 break;
5389 }
Mike Stump11289f42009-09-09 15:08:12 +00005390
Chris Lattner371ed4e2008-04-06 06:57:35 +00005391 // Consume the comma.
5392 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00005393 }
Mike Stump11289f42009-09-09 15:08:12 +00005394
Chris Lattner6c940e62008-04-06 06:34:08 +00005395}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005396
Chris Lattnere8074e62006-08-06 18:30:15 +00005397/// [C90] direct-declarator '[' constant-expression[opt] ']'
5398/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5399/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5400/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5401/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005402/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5403/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005404void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005405 if (CheckProhibitedCXX11Attribute())
5406 return;
5407
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005408 BalancedDelimiterTracker T(*this, tok::l_square);
5409 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005410
Chris Lattner84a11622008-12-18 07:27:21 +00005411 // C array syntax has many features, but by-far the most common is [] and [4].
5412 // This code does a fast path to handle some of the most obvious cases.
5413 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005414 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005415 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005416 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005417
Chris Lattner84a11622008-12-18 07:27:21 +00005418 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00005419 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00005420 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005421 T.getOpenLocation(),
5422 T.getCloseLocation()),
5423 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005424 return;
5425 } else if (Tok.getKind() == tok::numeric_constant &&
5426 GetLookAheadToken(1).is(tok::r_square)) {
5427 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005428 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005429 ConsumeToken();
5430
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005431 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005432 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005433 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005434
Chris Lattner84a11622008-12-18 07:27:21 +00005435 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005436 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005437 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005438 T.getOpenLocation(),
5439 T.getCloseLocation()),
5440 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005441 return;
5442 }
Mike Stump11289f42009-09-09 15:08:12 +00005443
Chris Lattnere8074e62006-08-06 18:30:15 +00005444 // If valid, this location is the position where we read the 'static' keyword.
5445 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00005446 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005447 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005448
Chris Lattnere8074e62006-08-06 18:30:15 +00005449 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005450 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005451 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005452 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005453
Chris Lattnere8074e62006-08-06 18:30:15 +00005454 // If we haven't already read 'static', check to see if there is one after the
5455 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00005456 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005457 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005458
Chris Lattnere8074e62006-08-06 18:30:15 +00005459 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005460 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005461 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005462
Chris Lattner521ff2b2008-04-06 05:26:30 +00005463 // Handle the case where we have '[*]' as the array size. However, a leading
5464 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005465 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005466 // infrequent, use of lookahead is not costly here.
5467 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005468 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005469
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005470 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005471 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005472 StaticLoc = SourceLocation(); // Drop the static.
5473 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005474 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005475 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005476 // Note, in C89, this production uses the constant-expr production instead
5477 // of assignment-expr. The only difference is that assignment-expr allows
5478 // things like '=' and '*='. Sema rejects these in C89 mode because they
5479 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005480
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005481 // Parse the constant-expression or assignment-expression now (depending
5482 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005483 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005484 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005485 } else {
5486 EnterExpressionEvaluationContext Unevaluated(Actions,
5487 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005488 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005489 }
Chris Lattner62591722006-08-12 18:40:58 +00005490 }
Mike Stump11289f42009-09-09 15:08:12 +00005491
Chris Lattner62591722006-08-12 18:40:58 +00005492 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005493 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005494 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005495 // If the expression was invalid, skip it.
5496 SkipUntil(tok::r_square);
5497 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005498 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005499
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005500 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005501
John McCall084e83d2011-03-24 11:26:52 +00005502 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005503 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005504
Chris Lattner84a11622008-12-18 07:27:21 +00005505 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005506 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005507 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005508 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005509 T.getOpenLocation(),
5510 T.getCloseLocation()),
5511 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005512}
5513
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005514/// [GNU] typeof-specifier:
5515/// typeof ( expressions )
5516/// typeof ( type-name )
5517/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005518///
5519void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005520 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005521 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005522 SourceLocation StartLoc = ConsumeToken();
5523
John McCalle8595032010-01-13 20:03:27 +00005524 const bool hasParens = Tok.is(tok::l_paren);
5525
Eli Friedman15681d62012-09-26 04:34:21 +00005526 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5527 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005528
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005529 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005530 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005531 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005532 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5533 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005534 if (hasParens)
5535 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005536
5537 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005538 // FIXME: Not accurate, the range gets one token more than it should.
5539 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005540 else
5541 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005542
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005543 if (isCastExpr) {
5544 if (!CastTy) {
5545 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005546 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005547 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005548
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005549 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005550 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005551 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5552 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005553 DiagID, CastTy))
5554 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005555 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005556 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005557
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005558 // If we get here, the operand to the typeof was an expresion.
5559 if (Operand.isInvalid()) {
5560 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005561 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005562 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005563
Eli Friedmane0afc982012-01-21 01:01:51 +00005564 // We might need to transform the operand if it is potentially evaluated.
5565 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5566 if (Operand.isInvalid()) {
5567 DS.SetTypeSpecError();
5568 return;
5569 }
5570
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005571 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005572 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005573 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5574 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005575 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005576 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005577}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005578
Benjamin Kramere56f3932011-12-23 17:00:35 +00005579/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005580/// _Atomic ( type-name )
5581///
5582void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00005583 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5584 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00005585
5586 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005587 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00005588 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005589 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00005590
5591 TypeResult Result = ParseTypeName();
5592 if (Result.isInvalid()) {
5593 SkipUntil(tok::r_paren);
5594 return;
5595 }
5596
5597 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005598 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005599
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005600 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005601 return;
5602
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005603 DS.setTypeofParensRange(T.getRange());
5604 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005605
5606 const char *PrevSpec = 0;
5607 unsigned DiagID;
5608 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5609 DiagID, Result.release()))
5610 Diag(StartLoc, DiagID) << PrevSpec;
5611}
5612
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005613
5614/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5615/// from TryAltiVecVectorToken.
5616bool Parser::TryAltiVecVectorTokenOutOfLine() {
5617 Token Next = NextToken();
5618 switch (Next.getKind()) {
5619 default: return false;
5620 case tok::kw_short:
5621 case tok::kw_long:
5622 case tok::kw_signed:
5623 case tok::kw_unsigned:
5624 case tok::kw_void:
5625 case tok::kw_char:
5626 case tok::kw_int:
5627 case tok::kw_float:
5628 case tok::kw_double:
5629 case tok::kw_bool:
5630 case tok::kw___pixel:
5631 Tok.setKind(tok::kw___vector);
5632 return true;
5633 case tok::identifier:
5634 if (Next.getIdentifierInfo() == Ident_pixel) {
5635 Tok.setKind(tok::kw___vector);
5636 return true;
5637 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005638 if (Next.getIdentifierInfo() == Ident_bool) {
5639 Tok.setKind(tok::kw___vector);
5640 return true;
5641 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005642 return false;
5643 }
5644}
5645
5646bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5647 const char *&PrevSpec, unsigned &DiagID,
5648 bool &isInvalid) {
5649 if (Tok.getIdentifierInfo() == Ident_vector) {
5650 Token Next = NextToken();
5651 switch (Next.getKind()) {
5652 case tok::kw_short:
5653 case tok::kw_long:
5654 case tok::kw_signed:
5655 case tok::kw_unsigned:
5656 case tok::kw_void:
5657 case tok::kw_char:
5658 case tok::kw_int:
5659 case tok::kw_float:
5660 case tok::kw_double:
5661 case tok::kw_bool:
5662 case tok::kw___pixel:
5663 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5664 return true;
5665 case tok::identifier:
5666 if (Next.getIdentifierInfo() == Ident_pixel) {
5667 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5668 return true;
5669 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005670 if (Next.getIdentifierInfo() == Ident_bool) {
5671 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5672 return true;
5673 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005674 break;
5675 default:
5676 break;
5677 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005678 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005679 DS.isTypeAltiVecVector()) {
5680 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5681 return true;
Bill Schmidt99a084b2013-07-03 20:54:09 +00005682 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5683 DS.isTypeAltiVecVector()) {
5684 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID);
5685 return true;
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005686 }
5687 return false;
5688}