blob: 277355cd40d6012f5c4403441304b49c363a2bce [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")) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000127 SkipUntil(tok::r_paren, StopAtSemi); // 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, "(")) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000131 SkipUntil(tok::r_paren, StopAtSemi); // 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") ))
Alp Toker094e5212014-01-05 03:27:11 +0000135 while (true) {
136 // Allow empty/non-empty attributes. ((__vector_size__(16),,,,))
137 if (TryConsumeToken(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000138 continue;
Alp Toker094e5212014-01-05 03:27:11 +0000139
140 // Expect an identifier or declaration specifier (const, int, etc.)
141 if (Tok.isNot(tok::identifier) && !isDeclarationSpecifier())
142 break;
143
Steve Naroff0f2fe172007-06-01 17:11:19 +0000144 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
145 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000146
Alp Toker094e5212014-01-05 03:27:11 +0000147 if (Tok.isNot(tok::l_paren)) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000148 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
149 AttributeList::AS_GNU);
Alp Toker094e5212014-01-05 03:27:11 +0000150 continue;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000151 }
Alp Toker094e5212014-01-05 03:27:11 +0000152
153 // Handle "parameterized" attributes
154 if (!LateAttrs || !isAttributeLateParsed(*AttrName)) {
155 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc, 0,
156 SourceLocation(), AttributeList::AS_GNU);
157 continue;
158 }
159
160 // Handle attributes with arguments that require late parsing.
161 LateParsedAttribute *LA =
162 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
163 LateAttrs->push_back(LA);
164
165 // Attributes in a class are parsed at the end of the class, along
166 // with other late-parsed declarations.
167 if (!ClassStack.empty() && !LateAttrs->parseSoon())
168 getCurrentClass().LateParsedDeclarations.push_back(LA);
169
170 // consume everything up to and including the matching right parens
171 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
172
173 Token Eof;
174 Eof.startToken();
175 Eof.setLocation(Tok.getLocation());
176 LA->Toks.push_back(Eof);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000177 }
Alp Toker094e5212014-01-05 03:27:11 +0000178
Alp Toker383d2c42014-01-01 03:08:43 +0000179 if (ExpectAndConsume(tok::r_paren))
Alexey Bataevee6507d2013-11-18 08:17:37 +0000180 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000181 SourceLocation Loc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000182 if (ExpectAndConsume(tok::r_paren))
Alexey Bataevee6507d2013-11-18 08:17:37 +0000183 SkipUntil(tok::r_paren, StopAtSemi);
John McCall53fa7142010-12-24 02:08:15 +0000184 if (endLoc)
185 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000186 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000187}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000188
Aaron Ballman4768b312013-11-04 12:55:56 +0000189/// \brief Normalizes an attribute name by dropping prefixed and suffixed __.
190static StringRef normalizeAttrName(StringRef Name) {
Richard Smith66e71682013-10-24 01:07:54 +0000191 if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__"))
192 Name = Name.drop_front(2).drop_back(2);
Aaron Ballman4768b312013-11-04 12:55:56 +0000193 return Name;
194}
195
196/// \brief Determine whether the given attribute has an identifier argument.
197static bool attributeHasIdentifierArg(const IdentifierInfo &II) {
198 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Richard Smith66e71682013-10-24 01:07:54 +0000199#include "clang/Parse/AttrIdentifierArg.inc"
Douglas Gregord2472d42013-05-02 23:25:32 +0000200 .Default(false);
201}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000202
Aaron Ballman4768b312013-11-04 12:55:56 +0000203/// \brief Determine whether the given attribute parses a type argument.
204static bool attributeIsTypeArgAttr(const IdentifierInfo &II) {
205 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
206#include "clang/Parse/AttrTypeArg.inc"
207 .Default(false);
208}
209
Richard Smithfeefaf52013-09-03 18:01:40 +0000210IdentifierLoc *Parser::ParseIdentifierLoc() {
211 assert(Tok.is(tok::identifier) && "expected an identifier");
212 IdentifierLoc *IL = IdentifierLoc::create(Actions.Context,
213 Tok.getLocation(),
214 Tok.getIdentifierInfo());
215 ConsumeToken();
216 return IL;
217}
218
Richard Smithb1f9a282013-10-31 01:56:18 +0000219void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName,
220 SourceLocation AttrNameLoc,
221 ParsedAttributes &Attrs,
222 SourceLocation *EndLoc) {
223 BalancedDelimiterTracker Parens(*this, tok::l_paren);
224 Parens.consumeOpen();
225
226 TypeResult T;
227 if (Tok.isNot(tok::r_paren))
228 T = ParseTypeName();
229
230 if (Parens.consumeClose())
231 return;
232
233 if (T.isInvalid())
234 return;
235
236 if (T.isUsable())
237 Attrs.addNewTypeAttr(&AttrName,
238 SourceRange(AttrNameLoc, Parens.getCloseLocation()), 0,
239 AttrNameLoc, T.get(), AttributeList::AS_GNU);
240 else
241 Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()),
242 0, AttrNameLoc, 0, 0, AttributeList::AS_GNU);
243}
244
Michael Han23214e52012-10-03 01:56:22 +0000245/// Parse the arguments to a parameterized GNU attribute or
246/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000247void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
248 SourceLocation AttrNameLoc,
249 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000250 SourceLocation *EndLoc,
251 IdentifierInfo *ScopeName,
252 SourceLocation ScopeLoc,
253 AttributeList::Syntax Syntax) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000254
255 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
256
Richard Smith66e71682013-10-24 01:07:54 +0000257 AttributeList::Kind AttrKind =
Richard Smithb1f9a282013-10-31 01:56:18 +0000258 AttributeList::getKind(AttrName, ScopeName, Syntax);
Richard Smith66e71682013-10-24 01:07:54 +0000259
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000260 // Availability attributes have their own grammar.
Richard Smithb1f9a282013-10-31 01:56:18 +0000261 // FIXME: All these cases fail to pass in the syntax and scope, and might be
262 // written as C++11 gnu:: attributes.
Richard Smith66e71682013-10-24 01:07:54 +0000263 if (AttrKind == AttributeList::AT_Availability) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000264 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
265 return;
266 }
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000267
268 if (AttrKind == AttributeList::AT_ObjCBridgeRelated) {
269 ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
270 return;
271 }
272
Richard Smithb1f9a282013-10-31 01:56:18 +0000273 // Thread safety attributes are parsed in an unevaluated context.
274 // FIXME: Share the bulk of the parsing code here and just pull out
275 // the unevaluated context.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000276 if (IsThreadSafetyAttribute(AttrName->getName())) {
277 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
278 return;
279 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000280 // Type safety attributes have their own grammar.
Richard Smith66e71682013-10-24 01:07:54 +0000281 if (AttrKind == AttributeList::AT_TypeTagForDatatype) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000282 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
283 return;
284 }
Aaron Ballman4768b312013-11-04 12:55:56 +0000285 // Some attributes expect solely a type parameter.
286 if (attributeIsTypeArgAttr(*AttrName)) {
Richard Smithb1f9a282013-10-31 01:56:18 +0000287 ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc);
288 return;
289 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000290
Richard Smith66e71682013-10-24 01:07:54 +0000291 // Ignore the left paren location for now.
292 ConsumeParen();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000293
Aaron Ballman00e99962013-08-31 01:11:41 +0000294 ArgsVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000295
Richard Smithb1f9a282013-10-31 01:56:18 +0000296 if (Tok.is(tok::identifier)) {
Richard Smith66e71682013-10-24 01:07:54 +0000297 // If this attribute wants an 'identifier' argument, make it so.
Richard Smithb1f9a282013-10-31 01:56:18 +0000298 bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName);
Richard Smith66e71682013-10-24 01:07:54 +0000299
300 // If we don't know how to parse this attribute, but this is the only
301 // token in this argument, assume it's meant to be an identifier.
Aaron Ballman66037472013-12-04 15:32:26 +0000302 if (AttrKind == AttributeList::UnknownAttribute ||
303 AttrKind == AttributeList::IgnoredAttribute) {
Richard Smith66e71682013-10-24 01:07:54 +0000304 const Token &Next = NextToken();
Richard Smithb1f9a282013-10-31 01:56:18 +0000305 IsIdentifierArg = Next.is(tok::r_paren) || Next.is(tok::comma);
Richard Smith66e71682013-10-24 01:07:54 +0000306 }
Richard Smithb12bf692011-10-17 21:20:17 +0000307
Richard Smithb1f9a282013-10-31 01:56:18 +0000308 if (IsIdentifierArg)
309 ArgExprs.push_back(ParseIdentifierLoc());
Richard Smithb12bf692011-10-17 21:20:17 +0000310 }
311
Richard Smithb1f9a282013-10-31 01:56:18 +0000312 if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) {
Richard Smithb12bf692011-10-17 21:20:17 +0000313 // Eat the comma.
Aaron Ballman00e99962013-08-31 01:11:41 +0000314 if (!ArgExprs.empty())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000315 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000316
Richard Smithb12bf692011-10-17 21:20:17 +0000317 // Parse the non-empty comma-separated list of expressions.
Alp Toker8fbec672013-12-17 23:29:36 +0000318 do {
Richard Smithb12bf692011-10-17 21:20:17 +0000319 ExprResult ArgExpr(ParseAssignmentExpression());
320 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000321 SkipUntil(tok::r_paren, StopAtSemi);
Richard Smithb12bf692011-10-17 21:20:17 +0000322 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000323 }
Richard Smithb12bf692011-10-17 21:20:17 +0000324 ArgExprs.push_back(ArgExpr.release());
Alp Toker8fbec672013-12-17 23:29:36 +0000325 // Eat the comma, move to the next argument
326 } while (TryConsumeToken(tok::comma));
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000327 }
Richard Smithb12bf692011-10-17 21:20:17 +0000328
329 SourceLocation RParen = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000330 if (!ExpectAndConsume(tok::r_paren)) {
Michael Han360d2252012-10-04 16:42:52 +0000331 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithb1f9a282013-10-31 01:56:18 +0000332 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
333 ArgExprs.data(), ArgExprs.size(), Syntax);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000334 }
335}
336
Chad Rosierc1183952012-06-26 22:30:43 +0000337/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000338/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000339void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000340 SourceLocation AttrNameLoc,
341 ParsedAttributes &Attrs)
342{
343 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000344 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000345 AttrName->getNameStart(), tok::r_paren))
346 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000347
Aaron Ballman478faed2012-06-19 22:09:27 +0000348 ExprResult ArgExpr(ParseConstantExpression());
349 if (ArgExpr.isInvalid()) {
350 T.skipToEnd();
351 return;
352 }
Aaron Ballman00e99962013-08-31 01:11:41 +0000353 ArgsUnion ExprList = ArgExpr.take();
354 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1,
355 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000356
357 T.consumeClose();
358}
359
Chad Rosierc1183952012-06-26 22:30:43 +0000360/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000361/// arguments.
362bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
363 return llvm::StringSwitch<bool>(Ident->getName())
364 .Case("dllimport", true)
365 .Case("dllexport", true)
366 .Case("noreturn", true)
367 .Case("nothrow", true)
368 .Case("noinline", true)
369 .Case("naked", true)
370 .Case("appdomain", true)
371 .Case("process", true)
372 .Case("jitintrinsic", true)
373 .Case("noalias", true)
374 .Case("restrict", true)
375 .Case("novtable", true)
376 .Case("selectany", true)
377 .Case("thread", true)
Aaron Ballman444eb6e2013-05-04 16:58:37 +0000378 .Case("safebuffers", true )
Aaron Ballman478faed2012-06-19 22:09:27 +0000379 .Default(false);
380}
381
Chad Rosierc1183952012-06-26 22:30:43 +0000382/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000383/// parameters). Will return false if we properly handled the declspec, or
384/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000385void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000386 SourceLocation Loc,
387 ParsedAttributes &Attrs) {
388 // Try to handle the easy case first -- these declspecs all take a single
389 // parameter as their argument.
390 if (llvm::StringSwitch<bool>(Ident->getName())
391 .Case("uuid", true)
392 .Case("align", true)
393 .Case("allocate", true)
394 .Default(false)) {
395 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
396 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000397 // The deprecated declspec has an optional single argument, so we will
398 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000399 // not.
400 if (Tok.getKind() == tok::l_paren)
401 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
402 else
Aaron Ballman00e99962013-08-31 01:11:41 +0000403 Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000404 } else if (Ident->getName() == "property") {
405 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000406 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000407 // must be named get or put.
John McCall5e77d762013-04-16 07:28:30 +0000408 if (Tok.isNot(tok::l_paren)) {
409 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
410 << Ident->getNameStart();
Aaron Ballman478faed2012-06-19 22:09:27 +0000411 return;
John McCall5e77d762013-04-16 07:28:30 +0000412 }
413 BalancedDelimiterTracker T(*this, tok::l_paren);
414 T.expectAndConsume(diag::err_expected_lparen_after,
415 Ident->getNameStart(), tok::r_paren);
416
417 enum AccessorKind {
418 AK_Invalid = -1,
419 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
420 };
421 IdentifierInfo *AccessorNames[] = { 0, 0 };
422 bool HasInvalidAccessor = false;
423
424 // Parse the accessor specifications.
425 while (true) {
426 // Stop if this doesn't look like an accessor spec.
427 if (!Tok.is(tok::identifier)) {
428 // If the user wrote a completely empty list, use a special diagnostic.
429 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
430 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
431 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
432 break;
433 }
434
435 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
436 break;
437 }
438
439 AccessorKind Kind;
440 SourceLocation KindLoc = Tok.getLocation();
441 StringRef KindStr = Tok.getIdentifierInfo()->getName();
442 if (KindStr == "get") {
443 Kind = AK_Get;
444 } else if (KindStr == "put") {
445 Kind = AK_Put;
446
447 // Recover from the common mistake of using 'set' instead of 'put'.
448 } else if (KindStr == "set") {
449 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
450 << FixItHint::CreateReplacement(KindLoc, "put");
451 Kind = AK_Put;
452
453 // Handle the mistake of forgetting the accessor kind by skipping
454 // this accessor.
455 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
456 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
457 ConsumeToken();
458 HasInvalidAccessor = true;
459 goto next_property_accessor;
460
461 // Otherwise, complain about the unknown accessor kind.
462 } else {
463 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
464 HasInvalidAccessor = true;
465 Kind = AK_Invalid;
466
467 // Try to keep parsing unless it doesn't look like an accessor spec.
468 if (!NextToken().is(tok::equal)) break;
469 }
470
471 // Consume the identifier.
472 ConsumeToken();
473
474 // Consume the '='.
Alp Toker8fbec672013-12-17 23:29:36 +0000475 if (!TryConsumeToken(tok::equal)) {
John McCall5e77d762013-04-16 07:28:30 +0000476 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
477 << KindStr;
478 break;
479 }
480
481 // Expect the method name.
482 if (!Tok.is(tok::identifier)) {
483 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
484 break;
485 }
486
487 if (Kind == AK_Invalid) {
488 // Just drop invalid accessors.
489 } else if (AccessorNames[Kind] != NULL) {
490 // Complain about the repeated accessor, ignore it, and keep parsing.
491 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
492 } else {
493 AccessorNames[Kind] = Tok.getIdentifierInfo();
494 }
495 ConsumeToken();
496
497 next_property_accessor:
498 // Keep processing accessors until we run out.
Alp Toker094e5212014-01-05 03:27:11 +0000499 if (TryConsumeToken(tok::comma))
John McCall5e77d762013-04-16 07:28:30 +0000500 continue;
501
502 // If we run into the ')', stop without consuming it.
Alp Toker094e5212014-01-05 03:27:11 +0000503 if (Tok.is(tok::r_paren))
John McCall5e77d762013-04-16 07:28:30 +0000504 break;
Alp Toker094e5212014-01-05 03:27:11 +0000505
506 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
507 break;
John McCall5e77d762013-04-16 07:28:30 +0000508 }
509
510 // Only add the property attribute if it was well-formed.
511 if (!HasInvalidAccessor) {
Aaron Ballman00e99962013-08-31 01:11:41 +0000512 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(),
John McCall5e77d762013-04-16 07:28:30 +0000513 AccessorNames[AK_Get], AccessorNames[AK_Put],
514 AttributeList::AS_Declspec);
515 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000516 T.skipToEnd();
517 } else {
518 // We don't recognize this as a valid declspec, but instead of creating the
519 // attribute and allowing sema to warn about it, we will warn here instead.
520 // This is because some attributes have multiple spellings, but we need to
521 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000522 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000523 // both locations.
524 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
525
526 // If there's an open paren, we should eat the open and close parens under
527 // the assumption that this unknown declspec has parameters.
528 BalancedDelimiterTracker T(*this, tok::l_paren);
529 if (!T.consumeOpen())
530 T.skipToEnd();
531 }
532}
533
Eli Friedman06de2b52009-06-08 07:21:15 +0000534/// [MS] decl-specifier:
535/// __declspec ( extended-decl-modifier-seq )
536///
537/// [MS] extended-decl-modifier-seq:
538/// extended-decl-modifier[opt]
539/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000540void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000541 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000542
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000543 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000544 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000545 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000546 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000547 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000548
Chad Rosierc1183952012-06-26 22:30:43 +0000549 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000550 // you can specify multiple attributes per declspec.
551 while (Tok.getKind() != tok::r_paren) {
552 // We expect either a well-known identifier or a generic string. Anything
553 // else is a malformed declspec.
554 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000555 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000556 Tok.getKind() != tok::kw_restrict) {
557 Diag(Tok, diag::err_ms_declspec_type);
558 T.skipToEnd();
559 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000560 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000561
562 IdentifierInfo *AttrName;
563 SourceLocation AttrNameLoc;
564 if (IsString) {
565 SmallString<8> StrBuffer;
566 bool Invalid = false;
567 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
568 if (Invalid) {
569 T.skipToEnd();
570 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000571 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000572 AttrName = PP.getIdentifierInfo(Str);
573 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000574 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000575 AttrName = Tok.getIdentifierInfo();
576 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000577 }
Chad Rosierc1183952012-06-26 22:30:43 +0000578
Aaron Ballman478faed2012-06-19 22:09:27 +0000579 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000580 // If we have a generic string, we will allow it because there is no
581 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000582 // (for instance, SAL declspecs in older versions of MSVC).
583 //
Chad Rosierc1183952012-06-26 22:30:43 +0000584 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000585 // arguments and can be turned into an attribute directly.
Aaron Ballman00e99962013-08-31 01:11:41 +0000586 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
587 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000588 else
589 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000590 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000591 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000592}
593
John McCall53fa7142010-12-24 02:08:15 +0000594void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000595 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000596 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000597 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000598 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Aaron Ballman317a77f2013-05-22 23:25:32 +0000599 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) ||
600 Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000601 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
602 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000603 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
604 AttributeList::AS_Keyword);
Eli Friedman53339e02009-06-08 23:27:34 +0000605 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000606}
607
John McCall53fa7142010-12-24 02:08:15 +0000608void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000609 // Treat these like attributes
610 while (Tok.is(tok::kw___pascal)) {
611 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
612 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000613 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
614 AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000615 }
John McCall53fa7142010-12-24 02:08:15 +0000616}
617
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000618void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
619 // Treat these like attributes
620 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000621 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000622 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +0000623 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
624 AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000625 }
626}
627
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000628void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000629 // FIXME: The mapping from attribute spelling to semantics should be
630 // performed in Sema, not here.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000631 SourceLocation Loc = Tok.getLocation();
632 switch(Tok.getKind()) {
633 // OpenCL qualifiers:
634 case tok::kw___private:
John McCall084e83d2011-03-24 11:26:52 +0000635 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000636 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000637 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000638 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000639
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000640 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000641 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000642 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000643 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000644 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000645
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000646 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000647 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000648 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000649 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000650 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000651
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000652 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000653 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000654 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000655 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000656 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000657
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000658 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000659 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000660 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000661 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000662 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000663
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000664 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000665 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000666 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000667 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000668 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000669
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000670 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000671 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000672 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000673 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000674 break;
675 default: break;
676 }
677}
678
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000679/// \brief Parse a version number.
680///
681/// version:
682/// simple-integer
683/// simple-integer ',' simple-integer
684/// simple-integer ',' simple-integer ',' simple-integer
685VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
686 Range = Tok.getLocation();
687
688 if (!Tok.is(tok::numeric_constant)) {
689 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000690 SkipUntil(tok::comma, tok::r_paren,
691 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000692 return VersionTuple();
693 }
694
695 // Parse the major (and possibly minor and subminor) versions, which
696 // are stored in the numeric constant. We utilize a quirk of the
697 // lexer, which is that it handles something like 1.2.3 as a single
698 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000699 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000700 Buffer.resize(Tok.getLength()+1);
701 const char *ThisTokBegin = &Buffer[0];
702
703 // Get the spelling of the token, which eliminates trigraphs, etc.
704 bool Invalid = false;
705 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
706 if (Invalid)
707 return VersionTuple();
708
709 // Parse the major version.
710 unsigned AfterMajor = 0;
711 unsigned Major = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000712 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000713 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
714 ++AfterMajor;
715 }
716
717 if (AfterMajor == 0) {
718 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000719 SkipUntil(tok::comma, tok::r_paren,
720 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000721 return VersionTuple();
722 }
723
724 if (AfterMajor == ActualLength) {
725 ConsumeToken();
726
727 // We only had a single version component.
728 if (Major == 0) {
729 Diag(Tok, diag::err_zero_version);
730 return VersionTuple();
731 }
732
733 return VersionTuple(Major);
734 }
735
736 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
737 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000738 SkipUntil(tok::comma, tok::r_paren,
739 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000740 return VersionTuple();
741 }
742
743 // Parse the minor version.
744 unsigned AfterMinor = AfterMajor + 1;
745 unsigned Minor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000746 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000747 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
748 ++AfterMinor;
749 }
750
751 if (AfterMinor == ActualLength) {
752 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000753
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000754 // We had major.minor.
755 if (Major == 0 && Minor == 0) {
756 Diag(Tok, diag::err_zero_version);
757 return VersionTuple();
758 }
759
Chad Rosierc1183952012-06-26 22:30:43 +0000760 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000761 }
762
763 // If what follows is not a '.', we have a problem.
764 if (ThisTokBegin[AfterMinor] != '.') {
765 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000766 SkipUntil(tok::comma, tok::r_paren,
767 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Chad Rosierc1183952012-06-26 22:30:43 +0000768 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000769 }
770
771 // Parse the subminor version.
772 unsigned AfterSubminor = AfterMinor + 1;
773 unsigned Subminor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000774 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000775 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
776 ++AfterSubminor;
777 }
778
779 if (AfterSubminor != ActualLength) {
780 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000781 SkipUntil(tok::comma, tok::r_paren,
782 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000783 return VersionTuple();
784 }
785 ConsumeToken();
786 return VersionTuple(Major, Minor, Subminor);
787}
788
789/// \brief Parse the contents of the "availability" attribute.
790///
791/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000792/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000793///
794/// platform:
795/// identifier
796///
797/// version-arg-list:
798/// version-arg
799/// version-arg ',' version-arg-list
800///
801/// version-arg:
802/// 'introduced' '=' version
803/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000804/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000805/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000806/// opt-message:
807/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000808void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
809 SourceLocation AvailabilityLoc,
810 ParsedAttributes &attrs,
811 SourceLocation *endLoc) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000812 enum { Introduced, Deprecated, Obsoleted, Unknown };
813 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000814 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000815
816 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000817 BalancedDelimiterTracker T(*this, tok::l_paren);
818 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000819 Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000820 return;
821 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000822
823 // Parse the platform name,
824 if (Tok.isNot(tok::identifier)) {
825 Diag(Tok, diag::err_availability_expected_platform);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000826 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000827 return;
828 }
Richard Smithfeefaf52013-09-03 18:01:40 +0000829 IdentifierLoc *Platform = ParseIdentifierLoc();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000830
831 // Parse the ',' following the platform name.
Alp Toker383d2c42014-01-01 03:08:43 +0000832 if (ExpectAndConsume(tok::comma)) {
833 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000834 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000835 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000836
837 // If we haven't grabbed the pointers for the identifiers
838 // "introduced", "deprecated", and "obsoleted", do so now.
839 if (!Ident_introduced) {
840 Ident_introduced = PP.getIdentifierInfo("introduced");
841 Ident_deprecated = PP.getIdentifierInfo("deprecated");
842 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000843 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000844 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000845 }
846
847 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000848 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000849 do {
850 if (Tok.isNot(tok::identifier)) {
851 Diag(Tok, diag::err_availability_expected_change);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000852 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000853 return;
854 }
855 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
856 SourceLocation KeywordLoc = ConsumeToken();
857
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000858 if (Keyword == Ident_unavailable) {
859 if (UnavailableLoc.isValid()) {
860 Diag(KeywordLoc, diag::err_availability_redundant)
861 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000862 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000863 UnavailableLoc = KeywordLoc;
864
Alp Toker383d2c42014-01-01 03:08:43 +0000865 if (TryConsumeToken(tok::comma))
866 continue;
867 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000868 }
869
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000870 if (Tok.isNot(tok::equal)) {
Alp Tokerec543272013-12-24 09:48:30 +0000871 Diag(Tok, diag::err_expected_after) << Keyword << tok::equal;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000872 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000873 return;
874 }
875 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000876 if (Keyword == Ident_message) {
Benjamin Kramera9dfa922013-09-13 17:31:48 +0000877 if (Tok.isNot(tok::string_literal)) { // Also reject wide string literals.
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000878 Diag(Tok, diag::err_expected_string_literal)
879 << /*Source='availability attribute'*/2;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000880 SkipUntil(tok::r_paren, StopAtSemi);
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000881 return;
882 }
883 MessageExpr = ParseStringLiteralExpression();
884 break;
885 }
Chad Rosierc1183952012-06-26 22:30:43 +0000886
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000887 SourceRange VersionRange;
888 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000889
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000890 if (Version.empty()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000891 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000892 return;
893 }
894
895 unsigned Index;
896 if (Keyword == Ident_introduced)
897 Index = Introduced;
898 else if (Keyword == Ident_deprecated)
899 Index = Deprecated;
900 else if (Keyword == Ident_obsoleted)
901 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000902 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000903 Index = Unknown;
904
905 if (Index < Unknown) {
906 if (!Changes[Index].KeywordLoc.isInvalid()) {
907 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000908 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000909 << SourceRange(Changes[Index].KeywordLoc,
910 Changes[Index].VersionRange.getEnd());
911 }
912
913 Changes[Index].KeywordLoc = KeywordLoc;
914 Changes[Index].Version = Version;
915 Changes[Index].VersionRange = VersionRange;
916 } else {
917 Diag(KeywordLoc, diag::err_availability_unknown_change)
918 << Keyword << VersionRange;
919 }
920
921 if (Tok.isNot(tok::comma))
922 break;
923
924 ConsumeToken();
925 } while (true);
926
927 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000928 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000929 return;
930
931 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000932 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000933
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000934 // The 'unavailable' availability cannot be combined with any other
935 // availability changes. Make sure that hasn't happened.
936 if (UnavailableLoc.isValid()) {
937 bool Complained = false;
938 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
939 if (Changes[Index].KeywordLoc.isValid()) {
940 if (!Complained) {
941 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
942 << SourceRange(Changes[Index].KeywordLoc,
943 Changes[Index].VersionRange.getEnd());
944 Complained = true;
945 }
946
947 // Clear out the availability.
948 Changes[Index] = AvailabilityChange();
949 }
950 }
951 }
952
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000953 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000954 attrs.addNew(&Availability,
955 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000956 0, AvailabilityLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +0000957 Platform,
John McCall084e83d2011-03-24 11:26:52 +0000958 Changes[Introduced],
959 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000960 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000961 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000962 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000963}
964
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000965/// \brief Parse the contents of the "objc_bridge_related" attribute.
966/// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')'
967/// related_class:
968/// Identifier
969///
970/// opt-class_method:
971/// Identifier: | <empty>
972///
973/// opt-instance_method:
974/// Identifier | <empty>
975///
976void Parser::ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated,
977 SourceLocation ObjCBridgeRelatedLoc,
978 ParsedAttributes &attrs,
979 SourceLocation *endLoc) {
980 // Opening '('.
981 BalancedDelimiterTracker T(*this, tok::l_paren);
982 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000983 Diag(Tok, diag::err_expected) << tok::l_paren;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000984 return;
985 }
986
987 // Parse the related class name.
988 if (Tok.isNot(tok::identifier)) {
989 Diag(Tok, diag::err_objcbridge_related_expected_related_class);
990 SkipUntil(tok::r_paren, StopAtSemi);
991 return;
992 }
993 IdentifierLoc *RelatedClass = ParseIdentifierLoc();
Alp Toker8fbec672013-12-17 23:29:36 +0000994 if (!TryConsumeToken(tok::comma)) {
Alp Tokerec543272013-12-24 09:48:30 +0000995 Diag(Tok, diag::err_expected) << tok::comma;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000996 SkipUntil(tok::r_paren, StopAtSemi);
997 return;
998 }
Alp Toker8fbec672013-12-17 23:29:36 +0000999
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001000 // Parse optional class method name.
1001 IdentifierLoc *ClassMethod = 0;
1002 if (Tok.is(tok::identifier)) {
1003 ClassMethod = ParseIdentifierLoc();
Alp Toker8fbec672013-12-17 23:29:36 +00001004 if (!TryConsumeToken(tok::colon)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001005 Diag(Tok, diag::err_objcbridge_related_selector_name);
1006 SkipUntil(tok::r_paren, StopAtSemi);
1007 return;
1008 }
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001009 }
Alp Toker8fbec672013-12-17 23:29:36 +00001010 if (!TryConsumeToken(tok::comma)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001011 if (Tok.is(tok::colon))
1012 Diag(Tok, diag::err_objcbridge_related_selector_name);
1013 else
Alp Tokerec543272013-12-24 09:48:30 +00001014 Diag(Tok, diag::err_expected) << tok::comma;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001015 SkipUntil(tok::r_paren, StopAtSemi);
1016 return;
1017 }
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001018
1019 // Parse optional instance method name.
1020 IdentifierLoc *InstanceMethod = 0;
1021 if (Tok.is(tok::identifier))
1022 InstanceMethod = ParseIdentifierLoc();
1023 else if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001024 Diag(Tok, diag::err_expected) << tok::r_paren;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001025 SkipUntil(tok::r_paren, StopAtSemi);
1026 return;
1027 }
1028
1029 // Closing ')'.
1030 if (T.consumeClose())
1031 return;
1032
1033 if (endLoc)
1034 *endLoc = T.getCloseLocation();
1035
1036 // Record this attribute
1037 attrs.addNew(&ObjCBridgeRelated,
1038 SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()),
1039 0, ObjCBridgeRelatedLoc,
1040 RelatedClass,
1041 ClassMethod,
1042 InstanceMethod,
1043 AttributeList::AS_GNU);
1044
1045}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001046
Bill Wendling44426052012-12-20 19:22:21 +00001047// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001048// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
1049
1050void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
1051
1052void Parser::LateParsedClass::ParseLexedAttributes() {
1053 Self->ParseLexedAttributes(*Class);
1054}
1055
1056void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001057 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001058}
1059
1060/// Wrapper class which calls ParseLexedAttribute, after setting up the
1061/// scope appropriately.
1062void Parser::ParseLexedAttributes(ParsingClass &Class) {
1063 // Deal with templates
1064 // FIXME: Test cases to make sure this does the right thing for templates.
1065 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
1066 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
1067 HasTemplateScope);
1068 if (HasTemplateScope)
1069 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
1070
Douglas Gregor3024f072012-04-16 07:05:22 +00001071 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001072 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +00001073 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001074 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
1075 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
1076
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001077 // Enter the scope of nested classes
1078 if (!AlreadyHasClassScope)
1079 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
1080 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +00001081 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +00001082 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
1083 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
1084 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001085 }
Chad Rosierc1183952012-06-26 22:30:43 +00001086
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001087 if (!AlreadyHasClassScope)
1088 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1089 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001090}
1091
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001092
1093/// \brief Parse all attributes in LAs, and attach them to Decl D.
1094void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1095 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001096 assert(LAs.parseSoon() &&
1097 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001098 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +00001099 if (D)
1100 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001101 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +00001102 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001103 }
1104 LAs.clear();
1105}
1106
1107
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001108/// \brief Finish parsing an attribute for which parsing was delayed.
1109/// This will be called at the end of parsing a class declaration
1110/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +00001111/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001112/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001113void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1114 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001115 // Save the current token position.
1116 SourceLocation OrigLoc = Tok.getLocation();
1117
1118 // Append the current token at the end of the new token stream so that it
1119 // doesn't get lost.
1120 LA.Toks.push_back(Tok);
1121 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1122 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00001123 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001124
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001125 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smith10876ef2013-01-17 01:30:42 +00001126 // FIXME: Do not warn on C++11 attributes, once we start supporting
1127 // them here.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001128 Diag(Tok, diag::warn_attribute_on_function_definition)
Aaron Ballman6d80b3c2014-01-02 18:10:17 +00001129 << &LA.AttrName;
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001130 }
1131
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001132 ParsedAttributes Attrs(AttrFactory);
1133 SourceLocation endLoc;
1134
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001135 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001136 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001137 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1138 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001139
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001140 // Allow 'this' within late-parsed attributes.
Richard Smithc3d2ebb2013-06-07 02:33:37 +00001141 Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0,
1142 ND && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001143
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001144 if (LA.Decls.size() == 1) {
1145 // If the Decl is templatized, add template parameters to scope.
1146 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1147 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1148 if (HasTemplateScope)
1149 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001150
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001151 // If the Decl is on a function, add function parameters to the scope.
1152 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1153 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1154 if (HasFunScope)
1155 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001156
Michael Han23214e52012-10-03 01:56:22 +00001157 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001158 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001159
1160 if (HasFunScope) {
1161 Actions.ActOnExitFunctionContext();
1162 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1163 }
1164 if (HasTemplateScope) {
1165 TempScope.Exit();
1166 }
1167 } else {
1168 // If there are multiple decls, then the decl cannot be within the
1169 // function scope.
Michael Han23214e52012-10-03 01:56:22 +00001170 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +00001171 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001172 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +00001173 } else {
1174 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001175 }
1176
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001177 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
1178 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
1179 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001180
1181 if (Tok.getLocation() != OrigLoc) {
1182 // Due to a parsing error, we either went over the cached tokens or
1183 // there are still cached tokens left, so we skip the leftover tokens.
1184 // Since this is an uncommon situation that should be avoided, use the
1185 // expensive isBeforeInTranslationUnit call.
1186 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1187 OrigLoc))
1188 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001189 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001190 }
1191}
1192
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001193/// \brief Wrapper around a case statement checking if AttrName is
1194/// one of the thread safety attributes
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001195bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001196 return llvm::StringSwitch<bool>(AttrName)
1197 .Case("guarded_by", true)
1198 .Case("guarded_var", true)
1199 .Case("pt_guarded_by", true)
1200 .Case("pt_guarded_var", true)
1201 .Case("lockable", true)
1202 .Case("scoped_lockable", true)
1203 .Case("no_thread_safety_analysis", true)
1204 .Case("acquired_after", true)
1205 .Case("acquired_before", true)
1206 .Case("exclusive_lock_function", true)
1207 .Case("shared_lock_function", true)
1208 .Case("exclusive_trylock_function", true)
1209 .Case("shared_trylock_function", true)
1210 .Case("unlock_function", true)
1211 .Case("lock_returned", true)
1212 .Case("locks_excluded", true)
1213 .Case("exclusive_locks_required", true)
1214 .Case("shared_locks_required", true)
1215 .Default(false);
1216}
1217
1218/// \brief Parse the contents of thread safety attributes. These
1219/// should always be parsed as an expression list.
1220///
1221/// We need to special case the parsing due to the fact that if the first token
1222/// of the first argument is an identifier, the main parse loop will store
1223/// that token as a "parameter" and the rest of
1224/// the arguments will be added to a list of "arguments". However,
1225/// subsequent tokens in the first argument are lost. We instead parse each
1226/// argument as an expression and add all arguments to the list of "arguments".
1227/// In future, we will take advantage of this special case to also
1228/// deal with some argument scoping issues here (for example, referring to a
1229/// function parameter in the attribute on that function).
1230void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1231 SourceLocation AttrNameLoc,
1232 ParsedAttributes &Attrs,
1233 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001234 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001235
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001236 BalancedDelimiterTracker T(*this, tok::l_paren);
1237 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +00001238
Aaron Ballman00e99962013-08-31 01:11:41 +00001239 ArgsVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001240 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +00001241
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001242 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001243 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinseb849c62013-02-07 19:01:07 +00001244 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001245 ExprResult ArgExpr(ParseAssignmentExpression());
1246 if (ArgExpr.isInvalid()) {
1247 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001248 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001249 break;
1250 } else {
1251 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001252 }
Alp Toker8fbec672013-12-17 23:29:36 +00001253 // Eat the comma, move to the next argument
1254 if (!TryConsumeToken(tok::comma))
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001255 break;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001256 }
1257 // Match the ')'.
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001258 if (ArgExprsOk && !T.consumeClose()) {
Aaron Ballman00e99962013-08-31 01:11:41 +00001259 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, ArgExprs.data(),
1260 ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001261 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001262 if (EndLoc)
1263 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001264}
1265
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001266void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1267 SourceLocation AttrNameLoc,
1268 ParsedAttributes &Attrs,
1269 SourceLocation *EndLoc) {
1270 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1271
1272 BalancedDelimiterTracker T(*this, tok::l_paren);
1273 T.consumeOpen();
1274
1275 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001276 Diag(Tok, diag::err_expected) << tok::identifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001277 T.skipToEnd();
1278 return;
1279 }
Richard Smithfeefaf52013-09-03 18:01:40 +00001280 IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001281
Alp Toker094e5212014-01-05 03:27:11 +00001282 if (ExpectAndConsume(tok::comma)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001283 T.skipToEnd();
1284 return;
1285 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001286
1287 SourceRange MatchingCTypeRange;
1288 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1289 if (MatchingCType.isInvalid()) {
1290 T.skipToEnd();
1291 return;
1292 }
1293
1294 bool LayoutCompatible = false;
1295 bool MustBeNull = false;
Alp Toker8fbec672013-12-17 23:29:36 +00001296 while (TryConsumeToken(tok::comma)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001297 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001298 Diag(Tok, diag::err_expected) << tok::identifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001299 T.skipToEnd();
1300 return;
1301 }
1302 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1303 if (Flag->isStr("layout_compatible"))
1304 LayoutCompatible = true;
1305 else if (Flag->isStr("must_be_null"))
1306 MustBeNull = true;
1307 else {
1308 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1309 T.skipToEnd();
1310 return;
1311 }
1312 ConsumeToken(); // consume flag
1313 }
1314
1315 if (!T.consumeClose()) {
1316 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +00001317 ArgumentKind, MatchingCType.release(),
1318 LayoutCompatible, MustBeNull,
1319 AttributeList::AS_GNU);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001320 }
1321
1322 if (EndLoc)
1323 *EndLoc = T.getCloseLocation();
1324}
1325
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001326/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1327/// of a C++11 attribute-specifier in a location where an attribute is not
1328/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1329/// situation.
1330///
1331/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1332/// this doesn't appear to actually be an attribute-specifier, and the caller
1333/// should try to parse it.
1334bool Parser::DiagnoseProhibitedCXX11Attribute() {
1335 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1336
1337 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1338 case CAK_NotAttributeSpecifier:
1339 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1340 return false;
1341
1342 case CAK_InvalidAttributeSpecifier:
1343 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1344 return false;
1345
1346 case CAK_AttributeSpecifier:
1347 // Parse and discard the attributes.
1348 SourceLocation BeginLoc = ConsumeBracket();
1349 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001350 SkipUntil(tok::r_square);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001351 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1352 SourceLocation EndLoc = ConsumeBracket();
1353 Diag(BeginLoc, diag::err_attributes_not_allowed)
1354 << SourceRange(BeginLoc, EndLoc);
1355 return true;
1356 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001357 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001358}
1359
Richard Smith98155ad2013-02-20 01:17:14 +00001360/// \brief We have found the opening square brackets of a C++11
1361/// attribute-specifier in a location where an attribute is not permitted, but
1362/// we know where the attributes ought to be written. Parse them anyway, and
1363/// provide a fixit moving them to the right place.
Richard Smith4c96e992013-02-19 23:47:15 +00001364void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1365 SourceLocation CorrectLocation) {
1366 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1367 Tok.is(tok::kw_alignas));
1368
1369 // Consume the attributes.
1370 SourceLocation Loc = Tok.getLocation();
1371 ParseCXX11Attributes(Attrs);
1372 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1373
1374 Diag(Loc, diag::err_attributes_not_allowed)
1375 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1376 << FixItHint::CreateRemoval(AttrRange);
1377}
1378
John McCall53fa7142010-12-24 02:08:15 +00001379void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1380 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1381 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001382}
1383
Michael Han64536a62012-11-06 19:34:54 +00001384void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1385 AttributeList *AttrList = attrs.getList();
1386 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001387 if (AttrList->isCXX11Attribute()) {
Richard Smith810ad3e2013-01-29 10:02:16 +00001388 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Han64536a62012-11-06 19:34:54 +00001389 << AttrList->getName();
1390 AttrList->setInvalid();
1391 }
1392 AttrList = AttrList->getNext();
1393 }
1394}
1395
Chris Lattner53361ac2006-08-10 05:19:57 +00001396/// ParseDeclaration - Parse a full 'declaration', which consists of
1397/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001398/// 'Context' should be a Declarator::TheContext value. This returns the
1399/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001400///
1401/// declaration: [C99 6.7]
1402/// block-declaration ->
1403/// simple-declaration
1404/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001405/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001406/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001407/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001408/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001409/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001410/// others... [FIXME]
1411///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001412Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1413 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001414 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001415 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001416 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001417 // Must temporarily exit the objective-c container scope for
1418 // parsing c none objective-c decls.
1419 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001420
John McCall48871652010-08-21 09:40:31 +00001421 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001422 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001423 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001424 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001425 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001426 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001427 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001428 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001429 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001430 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001431 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001432 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001433 SourceLocation InlineLoc = ConsumeToken();
1434 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1435 break;
1436 }
Chad Rosierc1183952012-06-26 22:30:43 +00001437 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001438 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001439 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001440 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001441 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001442 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001443 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001444 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001445 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001446 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001447 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001448 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001449 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001450 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001451 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001452 default:
John McCall53fa7142010-12-24 02:08:15 +00001453 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001454 }
Chad Rosierc1183952012-06-26 22:30:43 +00001455
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001456 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001457 // single decl, convert it now. Alias declarations can also declare a type;
1458 // include that too if it is present.
1459 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001460}
1461
1462/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1463/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001464/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1465/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001466///[C90/C++]init-declarator-list ';' [TODO]
1467/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001468///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001469/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001470/// attribute-specifier-seq[opt] type-specifier-seq declarator
1471///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001472/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001473/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001474///
1475/// If FRI is non-null, we might be parsing a for-range-declaration instead
1476/// of a simple-declaration. If we find that we are, we also parse the
1477/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001478Parser::DeclGroupPtrTy
1479Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1480 SourceLocation &DeclEnd,
Richard Smith2386c8b2013-02-22 09:06:26 +00001481 ParsedAttributesWithRange &Attrs,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001482 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001483 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001484 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001485
Richard Smith404dfb42013-11-19 22:47:36 +00001486 DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
1487 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext);
1488
1489 // If we had a free-standing type definition with a missing semicolon, we
1490 // may get this far before the problem becomes obvious.
1491 if (DS.hasTagDefinition() &&
1492 DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext))
1493 return DeclGroupPtrTy();
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001494
Chris Lattner0e894622006-08-13 19:58:17 +00001495 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1496 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001497 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001498 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001499 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001500 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001501 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001502 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001503 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001504 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001505 }
Chad Rosierc1183952012-06-26 22:30:43 +00001506
Richard Smith2386c8b2013-02-22 09:06:26 +00001507 DS.takeAttributesFrom(Attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00001508 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001509}
Mike Stump11289f42009-09-09 15:08:12 +00001510
Richard Smith09f76ee2011-10-19 21:33:05 +00001511/// Returns true if this might be the start of a declarator, or a common typo
1512/// for a declarator.
1513bool Parser::MightBeDeclarator(unsigned Context) {
1514 switch (Tok.getKind()) {
1515 case tok::annot_cxxscope:
1516 case tok::annot_template_id:
1517 case tok::caret:
1518 case tok::code_completion:
1519 case tok::coloncolon:
1520 case tok::ellipsis:
1521 case tok::kw___attribute:
1522 case tok::kw_operator:
1523 case tok::l_paren:
1524 case tok::star:
1525 return true;
1526
1527 case tok::amp:
1528 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001529 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001530
Richard Smithc8a79032012-01-09 22:31:44 +00001531 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001532 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001533 NextToken().is(tok::l_square);
1534
1535 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001536 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001537
Richard Smith09f76ee2011-10-19 21:33:05 +00001538 case tok::identifier:
1539 switch (NextToken().getKind()) {
1540 case tok::code_completion:
1541 case tok::coloncolon:
1542 case tok::comma:
1543 case tok::equal:
1544 case tok::equalequal: // Might be a typo for '='.
1545 case tok::kw_alignas:
1546 case tok::kw_asm:
1547 case tok::kw___attribute:
1548 case tok::l_brace:
1549 case tok::l_paren:
1550 case tok::l_square:
1551 case tok::less:
1552 case tok::r_brace:
1553 case tok::r_paren:
1554 case tok::r_square:
1555 case tok::semi:
1556 return true;
1557
1558 case tok::colon:
1559 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001560 // and in block scope it's probably a label. Inside a class definition,
1561 // this is a bit-field.
1562 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001563 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001564
1565 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001566 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001567
1568 default:
1569 return false;
1570 }
1571
1572 default:
1573 return false;
1574 }
1575}
1576
Richard Smithb8caac82012-04-11 20:59:20 +00001577/// Skip until we reach something which seems like a sensible place to pick
1578/// up parsing after a malformed declaration. This will sometimes stop sooner
1579/// than SkipUntil(tok::r_brace) would, but will never stop later.
1580void Parser::SkipMalformedDecl() {
1581 while (true) {
1582 switch (Tok.getKind()) {
1583 case tok::l_brace:
1584 // Skip until matching }, then stop. We've probably skipped over
1585 // a malformed class or function definition or similar.
1586 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001587 SkipUntil(tok::r_brace);
Richard Smithb8caac82012-04-11 20:59:20 +00001588 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1589 // This declaration isn't over yet. Keep skipping.
1590 continue;
1591 }
Alp Toker8fbec672013-12-17 23:29:36 +00001592 TryConsumeToken(tok::semi);
Richard Smithb8caac82012-04-11 20:59:20 +00001593 return;
1594
1595 case tok::l_square:
1596 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001597 SkipUntil(tok::r_square);
Richard Smithb8caac82012-04-11 20:59:20 +00001598 continue;
1599
1600 case tok::l_paren:
1601 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001602 SkipUntil(tok::r_paren);
Richard Smithb8caac82012-04-11 20:59:20 +00001603 continue;
1604
1605 case tok::r_brace:
1606 return;
1607
1608 case tok::semi:
1609 ConsumeToken();
1610 return;
1611
1612 case tok::kw_inline:
1613 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001614 // a good place to pick back up parsing, except in an Objective-C
1615 // @interface context.
1616 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1617 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001618 return;
1619 break;
1620
1621 case tok::kw_namespace:
1622 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001623 // place to pick back up parsing, except in an Objective-C
1624 // @interface context.
1625 if (Tok.isAtStartOfLine() &&
1626 (!ParsingInObjCContainer || CurParsedObjCImpl))
1627 return;
1628 break;
1629
1630 case tok::at:
1631 // @end is very much like } in Objective-C contexts.
1632 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1633 ParsingInObjCContainer)
1634 return;
1635 break;
1636
1637 case tok::minus:
1638 case tok::plus:
1639 // - and + probably start new method declarations in Objective-C contexts.
1640 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001641 return;
1642 break;
1643
1644 case tok::eof:
Richard Smith34f30512013-11-23 04:06:09 +00001645 case tok::annot_module_begin:
1646 case tok::annot_module_end:
1647 case tok::annot_module_include:
Richard Smithb8caac82012-04-11 20:59:20 +00001648 return;
1649
1650 default:
1651 break;
1652 }
1653
1654 ConsumeAnyToken();
1655 }
1656}
1657
John McCalld5a36322009-11-03 19:26:08 +00001658/// ParseDeclGroup - Having concluded that this is either a function
1659/// definition or a group of object declarations, actually parse the
1660/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001661Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1662 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001663 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001664 SourceLocation *DeclEnd,
1665 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001666 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001667 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001668 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001669
John McCalld5a36322009-11-03 19:26:08 +00001670 // Bail out if the first declarator didn't seem well-formed.
1671 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001672 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001673 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001674 }
Mike Stump11289f42009-09-09 15:08:12 +00001675
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001676 // Save late-parsed attributes for now; they need to be parsed in the
1677 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001678 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1679 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001680 if (D.isFunctionDeclarator())
1681 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1682
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001683 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00001684 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001685 // Look at the next token to make sure that this isn't a function
1686 // declaration. We have to check this because __attribute__ might be the
1687 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001688 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001689
Douglas Gregor012efe22013-04-16 16:01:32 +00001690 if (AllowFunctionDefinitions) {
1691 if (isStartOfFunctionDefinition(D)) {
1692 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1693 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00001694
Douglas Gregor012efe22013-04-16 16:01:32 +00001695 // Recover by treating the 'typedef' as spurious.
1696 DS.ClearStorageClassSpecs();
1697 }
1698
1699 Decl *TheDecl =
1700 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1701 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00001702 }
1703
Douglas Gregor012efe22013-04-16 16:01:32 +00001704 if (isDeclarationSpecifier()) {
1705 // If there is an invalid declaration specifier right after the function
1706 // prototype, then we must be in a missing semicolon case where this isn't
1707 // actually a body. Just fall through into the code that handles it as a
1708 // prototype, and let the top-level code handle the erroneous declspec
1709 // where it would otherwise expect a comma or semicolon.
1710 } else {
1711 Diag(Tok, diag::err_expected_fn_body);
1712 SkipUntil(tok::semi);
1713 return DeclGroupPtrTy();
1714 }
John McCalld5a36322009-11-03 19:26:08 +00001715 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00001716 if (Tok.is(tok::l_brace)) {
1717 Diag(Tok, diag::err_function_definition_not_allowed);
Serge Pavlov1de51512013-12-09 05:25:47 +00001718 SkipMalformedDecl();
1719 return DeclGroupPtrTy();
Douglas Gregor012efe22013-04-16 16:01:32 +00001720 }
John McCalld5a36322009-11-03 19:26:08 +00001721 }
1722 }
1723
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001724 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001725 return DeclGroupPtrTy();
1726
1727 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1728 // must parse and analyze the for-range-initializer before the declaration is
1729 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001730 //
1731 // Handle the Objective-C for-in loop variable similarly, although we
1732 // don't need to parse the container in advance.
1733 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1734 bool IsForRangeLoop = false;
Alp Toker8fbec672013-12-17 23:29:36 +00001735 if (TryConsumeToken(tok::colon, FRI->ColonLoc)) {
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001736 IsForRangeLoop = true;
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001737 if (Tok.is(tok::l_brace))
1738 FRI->RangeExpr = ParseBraceInitializer();
1739 else
1740 FRI->RangeExpr = ParseExpression();
1741 }
1742
Richard Smith02e85f32011-04-14 22:09:26 +00001743 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001744 if (IsForRangeLoop)
1745 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001746 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001747 D.complete(ThisDecl);
Rafael Espindolaab417692013-07-09 12:05:01 +00001748 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001749 }
1750
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001751 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001752 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001753 if (LateParsedAttrs.size() > 0)
1754 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001755 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001756 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001757 DeclsInGroup.push_back(FirstDecl);
1758
Richard Smith09f76ee2011-10-19 21:33:05 +00001759 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001760
John McCalld5a36322009-11-03 19:26:08 +00001761 // If we don't have a comma, it is either the end of the list (a ';') or an
1762 // error, bail out.
Alp Toker8fbec672013-12-17 23:29:36 +00001763 SourceLocation CommaLoc;
1764 while (TryConsumeToken(tok::comma, CommaLoc)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001765 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1766 // This comma was followed by a line-break and something which can't be
1767 // the start of a declarator. The comma was probably a typo for a
1768 // semicolon.
1769 Diag(CommaLoc, diag::err_expected_semi_declaration)
1770 << FixItHint::CreateReplacement(CommaLoc, ";");
1771 ExpectSemi = false;
1772 break;
1773 }
John McCalld5a36322009-11-03 19:26:08 +00001774
1775 // Parse the next declarator.
1776 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001777 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001778
1779 // Accept attributes in an init-declarator. In the first declarator in a
1780 // declaration, these would be part of the declspec. In subsequent
1781 // declarators, they become part of the declarator itself, so that they
1782 // don't apply to declarators after *this* one. Examples:
1783 // short __attribute__((common)) var; -> declspec
1784 // short var __attribute__((common)); -> declarator
1785 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001786 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001787
1788 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001789 if (!D.isInvalidType()) {
1790 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1791 D.complete(ThisDecl);
1792 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001793 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001794 }
John McCalld5a36322009-11-03 19:26:08 +00001795 }
1796
1797 if (DeclEnd)
1798 *DeclEnd = Tok.getLocation();
1799
Richard Smith09f76ee2011-10-19 21:33:05 +00001800 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001801 ExpectAndConsumeSemi(Context == Declarator::FileContext
1802 ? diag::err_invalid_token_after_toplevel_declarator
1803 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001804 // Okay, there was no semicolon and one was expected. If we see a
1805 // declaration specifier, just assume it was missing and continue parsing.
1806 // Otherwise things are very confused and we skip to recover.
1807 if (!isDeclarationSpecifier()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001808 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Alp Toker8fbec672013-12-17 23:29:36 +00001809 TryConsumeToken(tok::semi);
Chris Lattner13901342010-07-11 22:42:07 +00001810 }
John McCalld5a36322009-11-03 19:26:08 +00001811 }
1812
Rafael Espindolaab417692013-07-09 12:05:01 +00001813 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +00001814}
1815
Richard Smith02e85f32011-04-14 22:09:26 +00001816/// Parse an optional simple-asm-expr and attributes, and attach them to a
1817/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001818bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001819 // If a simple-asm-expr is present, parse it.
1820 if (Tok.is(tok::kw_asm)) {
1821 SourceLocation Loc;
1822 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1823 if (AsmLabel.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001824 SkipUntil(tok::semi, StopBeforeMatch);
Richard Smith02e85f32011-04-14 22:09:26 +00001825 return true;
1826 }
1827
1828 D.setAsmLabel(AsmLabel.release());
1829 D.SetRangeEnd(Loc);
1830 }
1831
1832 MaybeParseGNUAttributes(D);
1833 return false;
1834}
1835
Douglas Gregor23996282009-05-12 21:31:51 +00001836/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1837/// declarator'. This method parses the remainder of the declaration
1838/// (including any attributes or initializer, among other things) and
1839/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001840///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001841/// init-declarator: [C99 6.7]
1842/// declarator
1843/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001844/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1845/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001846/// [C++] declarator initializer[opt]
1847///
1848/// [C++] initializer:
1849/// [C++] '=' initializer-clause
1850/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001851/// [C++0x] '=' 'default' [TODO]
1852/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001853/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001854///
1855/// According to the standard grammar, =default and =delete are function
1856/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001857///
John McCall48871652010-08-21 09:40:31 +00001858Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001859 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001860 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001861 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001862
Richard Smith02e85f32011-04-14 22:09:26 +00001863 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1864}
Mike Stump11289f42009-09-09 15:08:12 +00001865
Richard Smith02e85f32011-04-14 22:09:26 +00001866Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1867 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001868 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001869 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001870 switch (TemplateInfo.Kind) {
1871 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001872 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001873 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001874
Douglas Gregor450f00842009-09-25 18:43:00 +00001875 case ParsedTemplateInfo::Template:
Larisse Voufo39a1e502013-08-06 01:03:05 +00001876 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001877 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001878 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001879 D);
Larisse Voufo833b05a2013-08-06 07:33:00 +00001880 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufo39a1e502013-08-06 01:03:05 +00001881 // Re-direct this decl to refer to the templated decl so that we can
1882 // initialize it.
1883 ThisDecl = VT->getTemplatedDecl();
1884 break;
1885 }
1886 case ParsedTemplateInfo::ExplicitInstantiation: {
1887 if (Tok.is(tok::semi)) {
1888 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
1889 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
1890 if (ThisRes.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001891 SkipUntil(tok::semi, StopBeforeMatch);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001892 return 0;
1893 }
1894 ThisDecl = ThisRes.get();
1895 } else {
1896 // FIXME: This check should be for a variable template instantiation only.
1897
1898 // Check that this is a valid instantiation
1899 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
1900 // If the declarator-id is not a template-id, issue a diagnostic and
1901 // recover by ignoring the 'template' keyword.
1902 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1903 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1904 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1905 } else {
1906 SourceLocation LAngleLoc =
1907 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1908 Diag(D.getIdentifierLoc(),
1909 diag::err_explicit_instantiation_with_definition)
1910 << SourceRange(TemplateInfo.TemplateLoc)
1911 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1912
1913 // Recover as if it were an explicit specialization.
1914 TemplateParameterLists FakedParamLists;
1915 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1916 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1917 LAngleLoc));
1918
1919 ThisDecl =
1920 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
1921 }
1922 }
Douglas Gregor450f00842009-09-25 18:43:00 +00001923 break;
1924 }
1925 }
Mike Stump11289f42009-09-09 15:08:12 +00001926
Richard Smith74aeef52013-04-26 16:15:35 +00001927 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith30482bc2011-02-20 03:19:35 +00001928
Douglas Gregor23996282009-05-12 21:31:51 +00001929 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001930 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001931 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001932 ConsumeToken();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001933
Anders Carlsson991285e2010-09-24 21:25:25 +00001934 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001935 if (D.isFunctionDeclarator())
1936 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1937 << 1 /* delete */;
1938 else
1939 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001940 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001941 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001942 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1943 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001944 else
1945 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001946 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001947 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001948 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001949 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001950 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001951
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001952 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001953 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001954 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001955 cutOffParsing();
1956 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001957 }
Chad Rosierc1183952012-06-26 22:30:43 +00001958
John McCalldadc5752010-08-24 06:29:42 +00001959 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001960
David Blaikiebbafb8a2012-03-11 07:00:24 +00001961 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001962 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001963 ExitScope();
1964 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001965
Douglas Gregor23996282009-05-12 21:31:51 +00001966 if (Init.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001967 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor604c3022010-03-01 18:27:54 +00001968 Actions.ActOnInitializerError(ThisDecl);
1969 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001970 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1971 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001972 }
1973 } else if (Tok.is(tok::l_paren)) {
1974 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001975 BalancedDelimiterTracker T(*this, tok::l_paren);
1976 T.consumeOpen();
1977
Benjamin Kramerf0623432012-08-23 22:51:59 +00001978 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001979 CommaLocsTy CommaLocs;
1980
David Blaikiebbafb8a2012-03-11 07:00:24 +00001981 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001982 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001983 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001984 }
1985
Douglas Gregor23996282009-05-12 21:31:51 +00001986 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001987 Actions.ActOnInitializerError(ThisDecl);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001988 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor613bf102009-12-22 17:47:17 +00001989
David Blaikiebbafb8a2012-03-11 07:00:24 +00001990 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001991 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001992 ExitScope();
1993 }
Douglas Gregor23996282009-05-12 21:31:51 +00001994 } else {
1995 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001996 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001997
1998 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1999 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00002000
David Blaikiebbafb8a2012-03-11 07:00:24 +00002001 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002002 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00002003 ExitScope();
2004 }
2005
Sebastian Redla9351792012-02-11 23:51:47 +00002006 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
2007 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002008 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00002009 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
2010 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00002011 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002012 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00002013 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00002014 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00002015 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2016
Sebastian Redl3da34892011-06-05 12:23:16 +00002017 if (D.getCXXScopeSpec().isSet()) {
2018 EnterScope(0);
2019 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
2020 }
2021
2022 ExprResult Init(ParseBraceInitializer());
2023
2024 if (D.getCXXScopeSpec().isSet()) {
2025 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
2026 ExitScope();
2027 }
2028
2029 if (Init.isInvalid()) {
2030 Actions.ActOnInitializerError(ThisDecl);
2031 } else
2032 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
2033 /*DirectInit=*/true, TypeContainsAuto);
2034
Douglas Gregor23996282009-05-12 21:31:51 +00002035 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00002036 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00002037 }
2038
Richard Smithb2bc2e62011-02-21 20:05:19 +00002039 Actions.FinalizeDeclaration(ThisDecl);
2040
Douglas Gregor23996282009-05-12 21:31:51 +00002041 return ThisDecl;
2042}
2043
Chris Lattner1890ac82006-08-13 01:16:23 +00002044/// ParseSpecifierQualifierList
2045/// specifier-qualifier-list:
2046/// type-specifier specifier-qualifier-list[opt]
2047/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002048/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00002049///
Richard Smithc5b05522012-03-12 07:56:15 +00002050void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
2051 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00002052 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
2053 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002054 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00002055 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00002056
Chris Lattner1890ac82006-08-13 01:16:23 +00002057 // Validate declspec for type-name.
2058 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00002059 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
2060 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00002061 Diag(Tok, diag::err_expected_type);
2062 DS.SetTypeSpecError();
2063 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
2064 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00002065 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00002066 if (!DS.hasTypeSpecifier())
2067 DS.SetTypeSpecError();
2068 }
Mike Stump11289f42009-09-09 15:08:12 +00002069
Chris Lattner1b22eed2006-11-28 05:12:07 +00002070 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002071 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00002072 if (DS.getStorageClassSpecLoc().isValid())
2073 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
2074 else
Richard Smithb4a9e862013-04-12 22:46:28 +00002075 Diag(DS.getThreadStorageClassSpecLoc(),
2076 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00002077 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002078 }
Mike Stump11289f42009-09-09 15:08:12 +00002079
Chris Lattner1b22eed2006-11-28 05:12:07 +00002080 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002081 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00002082 if (DS.isInlineSpecified())
2083 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
2084 if (DS.isVirtualSpecified())
2085 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
2086 if (DS.isExplicitSpecified())
2087 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00002088 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002089 }
Richard Smithc5b05522012-03-12 07:56:15 +00002090
2091 // Issue diagnostic and remove constexpr specfier if present.
2092 if (DS.isConstexprSpecified()) {
2093 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
2094 DS.ClearConstexprSpec();
2095 }
Chris Lattner1890ac82006-08-13 01:16:23 +00002096}
Chris Lattner53361ac2006-08-10 05:19:57 +00002097
Chris Lattner6cc055a2009-04-12 20:42:31 +00002098/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2099/// specified token is valid after the identifier in a declarator which
2100/// immediately follows the declspec. For example, these things are valid:
2101///
2102/// int x [ 4]; // direct-declarator
2103/// int x ( int y); // direct-declarator
2104/// int(int x ) // direct-declarator
2105/// int x ; // simple-declaration
2106/// int x = 17; // init-declarator-list
2107/// int x , y; // init-declarator-list
2108/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002109/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002110/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002111///
2112/// This is not, because 'x' does not immediately follow the declspec (though
2113/// ')' happens to be valid anyway).
2114/// int (x)
2115///
2116static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2117 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2118 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00002119 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002120}
2121
Chris Lattner20a0c612009-04-14 21:34:55 +00002122
2123/// ParseImplicitInt - This method is called when we have an non-typename
2124/// identifier in a declspec (which normally terminates the decl spec) when
2125/// the declspec has no type specifier. In this case, the declspec is either
2126/// malformed or is "implicit int" (in K&R and C89).
2127///
2128/// This method handles diagnosing this prettily and returns false if the
2129/// declspec is done being processed. If it recovers and thinks there may be
2130/// other pieces of declspec after it, it returns true.
2131///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002132bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002133 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00002134 AccessSpecifier AS, DeclSpecContext DSC,
2135 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002136 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002137
Chris Lattner20a0c612009-04-14 21:34:55 +00002138 SourceLocation Loc = Tok.getLocation();
2139 // If we see an identifier that is not a type name, we normally would
2140 // parse it as the identifer being declared. However, when a typename
2141 // is typo'd or the definition is not included, this will incorrectly
2142 // parse the typename as the identifier name and fall over misparsing
2143 // later parts of the diagnostic.
2144 //
2145 // As such, we try to do some look-ahead in cases where this would
2146 // otherwise be an "implicit-int" case to see if this is invalid. For
2147 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2148 // an identifier with implicit int, we'd get a parse error because the
2149 // next token is obviously invalid for a type. Parse these as a case
2150 // with an invalid type specifier.
2151 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002152
Chris Lattner20a0c612009-04-14 21:34:55 +00002153 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002154 // error, do lookahead to try to do better recovery. This never applies
2155 // within a type specifier. Outside of C++, we allow this even if the
2156 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002157 // implicit int as an extension in C99 and C11.
Richard Smith2f07ad52012-05-09 20:55:26 +00002158 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith3b870382013-04-30 22:43:51 +00002159 !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002160 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002161 // If this token is valid for implicit int, e.g. "static x = 4", then
2162 // we just avoid eating the identifier, so it will be parsed as the
2163 // identifier in the declarator.
2164 return false;
2165 }
Mike Stump11289f42009-09-09 15:08:12 +00002166
Richard Smitha952ebb2012-05-15 21:01:51 +00002167 if (getLangOpts().CPlusPlus &&
2168 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2169 // Don't require a type specifier if we have the 'auto' storage class
2170 // specifier in C++98 -- we'll promote it to a type specifier.
Richard Smithfb8b7b92013-10-15 00:00:26 +00002171 if (SS)
2172 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
Richard Smitha952ebb2012-05-15 21:01:51 +00002173 return false;
2174 }
2175
Chris Lattner20a0c612009-04-14 21:34:55 +00002176 // Otherwise, if we don't consume this token, we are going to emit an
2177 // error anyway. Try to recover from various common problems. Check
2178 // to see if this was a reference to a tag name without a tag specified.
2179 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002180 //
2181 // C++ doesn't need this, and isTagName doesn't take SS.
2182 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002183 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002184 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002185
Douglas Gregor0be31a22010-07-02 17:43:08 +00002186 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002187 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002188 case DeclSpec::TST_enum:
2189 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2190 case DeclSpec::TST_union:
2191 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2192 case DeclSpec::TST_struct:
2193 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002194 case DeclSpec::TST_interface:
2195 TagName="__interface"; FixitTagName = "__interface ";
2196 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002197 case DeclSpec::TST_class:
2198 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002199 }
Mike Stump11289f42009-09-09 15:08:12 +00002200
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002201 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002202 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2203 LookupResult R(Actions, TokenName, SourceLocation(),
2204 Sema::LookupOrdinaryName);
2205
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002206 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002207 << TokenName << TagName << getLangOpts().CPlusPlus
2208 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2209
2210 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2211 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2212 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002213 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002214 << TokenName << TagName;
2215 }
Mike Stump11289f42009-09-09 15:08:12 +00002216
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002217 // Parse this as a tag as if the missing tag were present.
2218 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00002219 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002220 else
Richard Smithc5b05522012-03-12 07:56:15 +00002221 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00002222 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002223 return true;
2224 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002225 }
Mike Stump11289f42009-09-09 15:08:12 +00002226
Richard Smithfe904f02012-05-15 21:29:55 +00002227 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002228 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00002229 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
2230 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002231 // Look ahead to the next token to try to figure out what this declaration
2232 // was supposed to be.
2233 switch (NextToken().getKind()) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002234 case tok::l_paren: {
2235 // static x(4); // 'x' is not a type
2236 // x(int n); // 'x' is not a type
2237 // x (*p)[]; // 'x' is a type
2238 //
2239 // Since we're in an error case (or the rare 'implicit int in C++' MS
2240 // extension), we can afford to perform a tentative parse to determine
2241 // which case we're in.
2242 TentativeParsingAction PA(*this);
2243 ConsumeToken();
2244 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2245 PA.Revert();
Richard Smithfb8b7b92013-10-15 00:00:26 +00002246
2247 if (TPR != TPResult::False()) {
2248 // The identifier is followed by a parenthesized declarator.
2249 // It's supposed to be a type.
2250 break;
2251 }
2252
2253 // If we're in a context where we could be declaring a constructor,
2254 // check whether this is a constructor declaration with a bogus name.
2255 if (DSC == DSC_class || (DSC == DSC_top_level && SS)) {
2256 IdentifierInfo *II = Tok.getIdentifierInfo();
2257 if (Actions.isCurrentClassNameTypo(II, SS)) {
2258 Diag(Loc, diag::err_constructor_bad_name)
2259 << Tok.getIdentifierInfo() << II
2260 << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
2261 Tok.setIdentifierInfo(II);
2262 }
2263 }
2264 // Fall through.
Richard Smitha952ebb2012-05-15 21:01:51 +00002265 }
Richard Smithfb8b7b92013-10-15 00:00:26 +00002266 case tok::comma:
2267 case tok::equal:
2268 case tok::kw_asm:
2269 case tok::l_brace:
2270 case tok::l_square:
2271 case tok::semi:
2272 // This looks like a variable or function declaration. The type is
2273 // probably missing. We're done parsing decl-specifiers.
2274 if (SS)
2275 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2276 return false;
Richard Smitha952ebb2012-05-15 21:01:51 +00002277
2278 default:
2279 // This is probably supposed to be a type. This includes cases like:
2280 // int f(itn);
2281 // struct S { unsinged : 4; };
2282 break;
2283 }
2284 }
2285
Chad Rosierc1183952012-06-26 22:30:43 +00002286 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00002287 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002288 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002289 IdentifierInfo *II = Tok.getIdentifierInfo();
2290 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00002291 // The action emitted a diagnostic, so we don't have to.
2292 if (T) {
2293 // The action has suggested that the type T could be used. Set that as
2294 // the type in the declaration specifiers, consume the would-be type
2295 // name token, and we're done.
2296 const char *PrevSpec;
2297 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00002298 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00002299 DS.SetRangeEnd(Tok.getLocation());
2300 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002301 // There may be other declaration specifiers after this.
2302 return true;
2303 } else if (II != Tok.getIdentifierInfo()) {
2304 // If no type was suggested, the correction is to a keyword
2305 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002306 // There may be other declaration specifiers after this.
2307 return true;
2308 }
Chad Rosierc1183952012-06-26 22:30:43 +00002309
Douglas Gregor15e56022009-10-13 23:27:22 +00002310 // Fall through; the action had no suggestion for us.
2311 } else {
2312 // The action did not emit a diagnostic, so emit one now.
2313 SourceRange R;
2314 if (SS) R = SS->getRange();
2315 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2316 }
Mike Stump11289f42009-09-09 15:08:12 +00002317
Douglas Gregor15e56022009-10-13 23:27:22 +00002318 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002319 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002320 DS.SetRangeEnd(Tok.getLocation());
2321 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002322
Chris Lattner20a0c612009-04-14 21:34:55 +00002323 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2324 // avoid rippling error messages on subsequent uses of the same type,
2325 // could be useful if #include was forgotten.
2326 return false;
2327}
2328
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002329/// \brief Determine the declaration specifier context from the declarator
2330/// context.
2331///
2332/// \param Context the declarator context, which is one of the
2333/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002334Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002335Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2336 if (Context == Declarator::MemberContext)
2337 return DSC_class;
2338 if (Context == Declarator::FileContext)
2339 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002340 if (Context == Declarator::TrailingReturnContext)
2341 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002342 return DSC_normal;
2343}
2344
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002345/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2346///
2347/// FIXME: Simply returns an alignof() expression if the argument is a
2348/// type. Ideally, the type should be propagated directly into Sema.
2349///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002350/// [C11] type-id
2351/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002352/// [C++0x] type-id ...[opt]
2353/// [C++0x] assignment-expression ...[opt]
2354ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2355 SourceLocation &EllipsisLoc) {
2356 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002357 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002358 SourceLocation TypeLoc = Tok.getLocation();
2359 ParsedType Ty = ParseTypeName().get();
2360 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002361 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2362 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002363 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002364 ER = ParseConstantExpression();
2365
Alp Toker8fbec672013-12-17 23:29:36 +00002366 if (getLangOpts().CPlusPlus11)
2367 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002368
2369 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002370}
2371
2372/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2373/// attribute to Attrs.
2374///
2375/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002376/// [C11] '_Alignas' '(' type-id ')'
2377/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002378/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2379/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002380void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002381 SourceLocation *EndLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002382 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2383 "Not an alignment-specifier!");
2384
Richard Smithd11c7a12013-01-29 01:48:07 +00002385 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2386 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002387
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002388 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00002389 if (T.expectAndConsume())
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002390 return;
2391
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002392 SourceLocation EllipsisLoc;
2393 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002394 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002395 T.skipToEnd();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002396 return;
2397 }
2398
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002399 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002400 if (EndLoc)
2401 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002402
Aaron Ballman00e99962013-08-31 01:11:41 +00002403 ArgsVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002404 ArgExprs.push_back(ArgExpr.release());
Aaron Ballman00e99962013-08-31 01:11:41 +00002405 Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1,
2406 AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002407}
2408
Richard Smith404dfb42013-11-19 22:47:36 +00002409/// Determine whether we're looking at something that might be a declarator
2410/// in a simple-declaration. If it can't possibly be a declarator, maybe
2411/// diagnose a missing semicolon after a prior tag definition in the decl
2412/// specifier.
2413///
2414/// \return \c true if an error occurred and this can't be any kind of
2415/// declaration.
2416bool
2417Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
2418 DeclSpecContext DSContext,
2419 LateParsedAttrList *LateAttrs) {
2420 assert(DS.hasTagDefinition() && "shouldn't call this");
2421
2422 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Richard Smith404dfb42013-11-19 22:47:36 +00002423
2424 if (getLangOpts().CPlusPlus &&
2425 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2426 Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) &&
2427 TryAnnotateCXXScopeToken(EnteringContext)) {
2428 SkipMalformedDecl();
2429 return true;
2430 }
2431
Richard Smith698875a2013-11-20 23:40:57 +00002432 bool HasScope = Tok.is(tok::annot_cxxscope);
2433 // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
2434 Token AfterScope = HasScope ? NextToken() : Tok;
2435
Richard Smith404dfb42013-11-19 22:47:36 +00002436 // Determine whether the following tokens could possibly be a
2437 // declarator.
Richard Smith698875a2013-11-20 23:40:57 +00002438 bool MightBeDeclarator = true;
2439 if (Tok.is(tok::kw_typename) || Tok.is(tok::annot_typename)) {
2440 // A declarator-id can't start with 'typename'.
2441 MightBeDeclarator = false;
2442 } else if (AfterScope.is(tok::annot_template_id)) {
2443 // If we have a type expressed as a template-id, this cannot be a
2444 // declarator-id (such a type cannot be redeclared in a simple-declaration).
2445 TemplateIdAnnotation *Annot =
2446 static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
2447 if (Annot->Kind == TNK_Type_template)
2448 MightBeDeclarator = false;
2449 } else if (AfterScope.is(tok::identifier)) {
2450 const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken();
2451
Richard Smith404dfb42013-11-19 22:47:36 +00002452 // These tokens cannot come after the declarator-id in a
2453 // simple-declaration, and are likely to come after a type-specifier.
Richard Smith698875a2013-11-20 23:40:57 +00002454 if (Next.is(tok::star) || Next.is(tok::amp) || Next.is(tok::ampamp) ||
2455 Next.is(tok::identifier) || Next.is(tok::annot_cxxscope) ||
2456 Next.is(tok::coloncolon)) {
2457 // Missing a semicolon.
2458 MightBeDeclarator = false;
2459 } else if (HasScope) {
2460 // If the declarator-id has a scope specifier, it must redeclare a
2461 // previously-declared entity. If that's a type (and this is not a
2462 // typedef), that's an error.
2463 CXXScopeSpec SS;
2464 Actions.RestoreNestedNameSpecifierAnnotation(
2465 Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
2466 IdentifierInfo *Name = AfterScope.getIdentifierInfo();
2467 Sema::NameClassification Classification = Actions.ClassifyName(
2468 getCurScope(), SS, Name, AfterScope.getLocation(), Next,
2469 /*IsAddressOfOperand*/false);
2470 switch (Classification.getKind()) {
2471 case Sema::NC_Error:
2472 SkipMalformedDecl();
2473 return true;
Richard Smith404dfb42013-11-19 22:47:36 +00002474
Richard Smith698875a2013-11-20 23:40:57 +00002475 case Sema::NC_Keyword:
2476 case Sema::NC_NestedNameSpecifier:
2477 llvm_unreachable("typo correction and nested name specifiers not "
2478 "possible here");
Richard Smith404dfb42013-11-19 22:47:36 +00002479
Richard Smith698875a2013-11-20 23:40:57 +00002480 case Sema::NC_Type:
2481 case Sema::NC_TypeTemplate:
2482 // Not a previously-declared non-type entity.
2483 MightBeDeclarator = false;
2484 break;
Richard Smith404dfb42013-11-19 22:47:36 +00002485
Richard Smith698875a2013-11-20 23:40:57 +00002486 case Sema::NC_Unknown:
2487 case Sema::NC_Expression:
2488 case Sema::NC_VarTemplate:
2489 case Sema::NC_FunctionTemplate:
2490 // Might be a redeclaration of a prior entity.
2491 break;
2492 }
Richard Smith404dfb42013-11-19 22:47:36 +00002493 }
Richard Smith404dfb42013-11-19 22:47:36 +00002494 }
2495
Richard Smith698875a2013-11-20 23:40:57 +00002496 if (MightBeDeclarator)
Richard Smith404dfb42013-11-19 22:47:36 +00002497 return false;
2498
2499 Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()),
Alp Toker383d2c42014-01-01 03:08:43 +00002500 diag::err_expected_after)
2501 << DeclSpec::getSpecifierName(DS.getTypeSpecType()) << tok::semi;
Richard Smith404dfb42013-11-19 22:47:36 +00002502
2503 // Try to recover from the typo, by dropping the tag definition and parsing
2504 // the problematic tokens as a type.
2505 //
2506 // FIXME: Split the DeclSpec into pieces for the standalone
2507 // declaration and pieces for the following declaration, instead
2508 // of assuming that all the other pieces attach to new declaration,
2509 // and call ParsedFreeStandingDeclSpec as appropriate.
2510 DS.ClearTypeSpecType();
2511 ParsedTemplateInfo NotATemplate;
2512 ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
2513 return false;
2514}
2515
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002516/// ParseDeclarationSpecifiers
2517/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002518/// storage-class-specifier declaration-specifiers[opt]
2519/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002520/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002521/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002522/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002523/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002524///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002525/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002526/// 'typedef'
2527/// 'extern'
2528/// 'static'
2529/// 'auto'
2530/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002531/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00002532/// [C++11] 'thread_local'
2533/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002534/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002535/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002536/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002537/// [C++] 'virtual'
2538/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002539/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002540/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002541/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002542
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002543///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002544void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002545 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002546 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002547 DeclSpecContext DSContext,
2548 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002549 if (DS.getSourceRange().isInvalid()) {
2550 DS.SetRangeStart(Tok.getLocation());
2551 DS.SetRangeEnd(Tok.getLocation());
2552 }
Chad Rosierc1183952012-06-26 22:30:43 +00002553
Douglas Gregordf593fb2011-11-07 17:33:42 +00002554 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002555 bool AttrsLastTime = false;
2556 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002557 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002558 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002559 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002560 unsigned DiagID = 0;
2561
Chris Lattner4d8f8732006-11-28 05:05:08 +00002562 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002563
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002564 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002565 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002566 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002567 if (!AttrsLastTime)
2568 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002569 else {
2570 // Reject C++11 attributes that appertain to decl specifiers as
2571 // we don't support any C++11 attributes that appertain to decl
2572 // specifiers. This also conforms to what g++ 4.8 is doing.
2573 ProhibitCXX11Attributes(attrs);
2574
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002575 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002576 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002577
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002578 // If this is not a declaration specifier token, we're done reading decl
2579 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002580 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002581 return;
Mike Stump11289f42009-09-09 15:08:12 +00002582
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002583 case tok::l_square:
2584 case tok::kw_alignas:
Richard Smith4cabd042013-02-22 09:15:49 +00002585 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002586 goto DoneWithDeclSpec;
2587
2588 ProhibitAttributes(attrs);
2589 // FIXME: It would be good to recover by accepting the attributes,
2590 // but attempting to do that now would cause serious
2591 // madness in terms of diagnostics.
2592 attrs.clear();
2593 attrs.Range = SourceRange();
2594
2595 ParseCXX11Attributes(attrs);
2596 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002597 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002598
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002599 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002600 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002601 if (DS.hasTypeSpecifier()) {
2602 bool AllowNonIdentifiers
2603 = (getCurScope()->getFlags() & (Scope::ControlScope |
2604 Scope::BlockScope |
2605 Scope::TemplateParamScope |
2606 Scope::FunctionPrototypeScope |
2607 Scope::AtCatchScope)) == 0;
2608 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002609 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002610 (DSContext == DSC_class && DS.isFriendSpecified());
2611
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002612 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002613 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002614 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002615 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002616 }
2617
Douglas Gregor80039242011-02-15 20:33:25 +00002618 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2619 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2620 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002621 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002622 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002623 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002624 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002625 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002626 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002627
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002628 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002629 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002630 }
2631
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002632 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002633 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002634 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002635 if (!DS.hasTypeSpecifier())
2636 DS.SetTypeSpecError();
2637 goto DoneWithDeclSpec;
2638 }
John McCall8bc2a702010-03-01 18:20:46 +00002639 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2640 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002641 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002642
2643 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002644 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002645 goto DoneWithDeclSpec;
2646
John McCall9dab4e62009-12-12 11:40:51 +00002647 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002648 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2649 Tok.getAnnotationRange(),
2650 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002651
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002652 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002653 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002654 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002655 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002656 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002657 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002658
2659 // C++ [class.qual]p2:
2660 // In a lookup in which the constructor is an acceptable lookup
2661 // result and the nested-name-specifier nominates a class C:
2662 //
2663 // - if the name specified after the
2664 // nested-name-specifier, when looked up in C, is the
2665 // injected-class-name of C (Clause 9), or
2666 //
2667 // - if the name specified after the nested-name-specifier
2668 // is the same as the identifier or the
2669 // simple-template-id's template-name in the last
2670 // component of the nested-name-specifier,
2671 //
2672 // the name is instead considered to name the constructor of
2673 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002674 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002675 // Thus, if the template-name is actually the constructor
2676 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002677 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002678 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002679 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00002680 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002681 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002682 if (isConstructorDeclarator()) {
2683 // The user meant this to be an out-of-line constructor
2684 // definition, but template arguments are not allowed
2685 // there. Just allow this as a constructor; we'll
2686 // complain about it later.
2687 goto DoneWithDeclSpec;
2688 }
2689
2690 // The user meant this to name a type, but it actually names
2691 // a constructor with some extraneous template
2692 // arguments. Complain, then parse it as a type as the user
2693 // intended.
2694 Diag(TemplateId->TemplateNameLoc,
2695 diag::err_out_of_line_template_id_names_constructor)
2696 << TemplateId->Name;
2697 }
2698
John McCall9dab4e62009-12-12 11:40:51 +00002699 DS.getTypeSpecScope() = SS;
2700 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002701 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002702 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002703 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002704 continue;
2705 }
2706
Douglas Gregorc5790df2009-09-28 07:26:33 +00002707 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002708 DS.getTypeSpecScope() = SS;
2709 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002710 if (Tok.getAnnotationValue()) {
2711 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002712 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002713 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002714 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002715 if (isInvalid)
2716 break;
John McCallba7bf592010-08-24 05:47:05 +00002717 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002718 else
2719 DS.SetTypeSpecError();
2720 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2721 ConsumeToken(); // The typename
2722 }
2723
Douglas Gregor167fa622009-03-25 15:40:00 +00002724 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002725 goto DoneWithDeclSpec;
2726
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002727 // If we're in a context where the identifier could be a class name,
2728 // check whether this is a constructor declaration.
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002729 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002730 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002731 &SS)) {
2732 if (isConstructorDeclarator())
2733 goto DoneWithDeclSpec;
2734
2735 // As noted in C++ [class.qual]p2 (cited above), when the name
2736 // of the class is qualified in a context where it could name
2737 // a constructor, its a constructor name. However, we've
2738 // looked at the declarator, and the user probably meant this
2739 // to be a type. Complain that it isn't supposed to be treated
2740 // as a type, then proceed to parse it as a type.
2741 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2742 << Next.getIdentifierInfo();
2743 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002744
John McCallba7bf592010-08-24 05:47:05 +00002745 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2746 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002747 getCurScope(), &SS,
2748 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002749 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002750 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002751
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002752 // If the referenced identifier is not a type, then this declspec is
2753 // erroneous: We already checked about that it has no type specifier, and
2754 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002755 // typename.
David Blaikie7d170102013-05-15 07:37:26 +00002756 if (!TypeRep) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002757 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002758 ParsedAttributesWithRange Attrs(AttrFactory);
2759 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2760 if (!Attrs.empty()) {
2761 AttrsLastTime = true;
2762 attrs.takeAllFrom(Attrs);
2763 }
2764 continue;
2765 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002766 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002767 }
Mike Stump11289f42009-09-09 15:08:12 +00002768
John McCall9dab4e62009-12-12 11:40:51 +00002769 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002770 ConsumeToken(); // The C++ scope.
2771
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002772 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002773 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002774 if (isInvalid)
2775 break;
Mike Stump11289f42009-09-09 15:08:12 +00002776
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002777 DS.SetRangeEnd(Tok.getLocation());
2778 ConsumeToken(); // The typename.
2779
2780 continue;
2781 }
Mike Stump11289f42009-09-09 15:08:12 +00002782
Chris Lattnere387d9e2009-01-21 19:48:37 +00002783 case tok::annot_typename: {
Richard Smith404dfb42013-11-19 22:47:36 +00002784 // If we've previously seen a tag definition, we were almost surely
2785 // missing a semicolon after it.
2786 if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
2787 goto DoneWithDeclSpec;
2788
John McCallba7bf592010-08-24 05:47:05 +00002789 if (Tok.getAnnotationValue()) {
2790 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002791 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002792 DiagID, T);
2793 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002794 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002795
Chris Lattner005fc1b2010-04-05 18:18:31 +00002796 if (isInvalid)
2797 break;
2798
Chris Lattnere387d9e2009-01-21 19:48:37 +00002799 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2800 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002801
Chris Lattnere387d9e2009-01-21 19:48:37 +00002802 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2803 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002804 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002805 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002806 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002807
Chris Lattnere387d9e2009-01-21 19:48:37 +00002808 continue;
2809 }
Mike Stump11289f42009-09-09 15:08:12 +00002810
Douglas Gregor06873092011-04-28 15:48:45 +00002811 case tok::kw___is_signed:
2812 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2813 // typically treats it as a trait. If we see __is_signed as it appears
2814 // in libstdc++, e.g.,
2815 //
2816 // static const bool __is_signed;
2817 //
2818 // then treat __is_signed as an identifier rather than as a keyword.
2819 if (DS.getTypeSpecType() == TST_bool &&
2820 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
Alp Toker47642d22013-12-03 06:13:01 +00002821 DS.getStorageClassSpec() == DeclSpec::SCS_static)
2822 TryKeywordIdentFallback(true);
Douglas Gregor06873092011-04-28 15:48:45 +00002823
2824 // We're done with the declaration-specifiers.
2825 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002826
Chris Lattner16fac4f2008-07-26 01:18:38 +00002827 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002828 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002829 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002830 // In C++, check to see if this is a scope specifier like foo::bar::, if
2831 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002832 if (getLangOpts().CPlusPlus) {
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002833 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002834 if (!DS.hasTypeSpecifier())
2835 DS.SetTypeSpecError();
2836 goto DoneWithDeclSpec;
2837 }
2838 if (!Tok.is(tok::identifier))
2839 continue;
2840 }
Mike Stump11289f42009-09-09 15:08:12 +00002841
Chris Lattner16fac4f2008-07-26 01:18:38 +00002842 // This identifier can only be a typedef name if we haven't already seen
2843 // a type-specifier. Without this check we misparse:
2844 // typedef int X; struct Y { short X; }; as 'short int'.
2845 if (DS.hasTypeSpecifier())
2846 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002847
John Thompson22334602010-02-05 00:12:22 +00002848 // Check for need to substitute AltiVec keyword tokens.
2849 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2850 break;
2851
Richard Smith3092a3b2012-05-09 18:56:43 +00002852 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2853 // allow the use of a typedef name as a type specifier.
2854 if (DS.isTypeAltiVecVector())
2855 goto DoneWithDeclSpec;
2856
John McCallba7bf592010-08-24 05:47:05 +00002857 ParsedType TypeRep =
2858 Actions.getTypeName(*Tok.getIdentifierInfo(),
2859 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002860
Chris Lattner6cc055a2009-04-12 20:42:31 +00002861 // If this is not a typedef name, don't parse it as part of the declspec,
2862 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002863 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002864 ParsedAttributesWithRange Attrs(AttrFactory);
2865 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2866 if (!Attrs.empty()) {
2867 AttrsLastTime = true;
2868 attrs.takeAllFrom(Attrs);
2869 }
2870 continue;
2871 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002872 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002873 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002874
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002875 // If we're in a context where the identifier could be a class name,
2876 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002877 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002878 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002879 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002880 goto DoneWithDeclSpec;
2881
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002882 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002883 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002884 if (isInvalid)
2885 break;
Mike Stump11289f42009-09-09 15:08:12 +00002886
Chris Lattner16fac4f2008-07-26 01:18:38 +00002887 DS.SetRangeEnd(Tok.getLocation());
2888 ConsumeToken(); // The identifier
2889
2890 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2891 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002892 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002893 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002894 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002895
Steve Naroffcd5e7822008-09-22 10:28:57 +00002896 // Need to support trailing type qualifiers (e.g. "id<p> const").
2897 // If a type specifier follows, it will be diagnosed elsewhere.
2898 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002899 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002900
2901 // type-name
2902 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002903 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002904 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002905 // This template-id does not refer to a type name, so we're
2906 // done with the type-specifiers.
2907 goto DoneWithDeclSpec;
2908 }
2909
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002910 // If we're in a context where the template-id could be a
2911 // constructor name or specialization, check whether this is a
2912 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002913 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002914 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002915 isConstructorDeclarator())
2916 goto DoneWithDeclSpec;
2917
Douglas Gregor7f741122009-02-25 19:37:18 +00002918 // Turn the template-id annotation token into a type annotation
2919 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002920 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002921 continue;
2922 }
2923
Chris Lattnere37e2332006-08-15 04:50:22 +00002924 // GNU attributes support.
2925 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002926 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002927 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002928
2929 // Microsoft declspec support.
2930 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002931 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002932 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002933
Steve Naroff44ac7772008-12-25 14:16:32 +00002934 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002935 case tok::kw___forceinline: {
Serge Pavlov750db652013-11-13 06:57:53 +00002936 isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002937 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002938 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002939 // FIXME: This does not work correctly if it is set to be a declspec
2940 // attribute, and a GNU attribute is simply incorrect.
Aaron Ballman00e99962013-08-31 01:11:41 +00002941 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
2942 AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002943 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002944 }
Eli Friedman53339e02009-06-08 23:27:34 +00002945
Aaron Ballman317a77f2013-05-22 23:25:32 +00002946 case tok::kw___sptr:
2947 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00002948 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002949 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002950 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002951 case tok::kw___cdecl:
2952 case tok::kw___stdcall:
2953 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002954 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002955 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002956 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002957 continue;
2958
Dawn Perchik335e16b2010-09-03 01:29:35 +00002959 // Borland single token adornments.
2960 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002961 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002962 continue;
2963
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002964 // OpenCL single token adornments.
2965 case tok::kw___kernel:
2966 ParseOpenCLAttributes(DS.getAttributes());
2967 continue;
2968
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002969 // storage-class-specifier
2970 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002971 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2972 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002973 break;
2974 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00002975 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002976 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002977 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2978 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002979 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002980 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002981 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2982 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002983 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002984 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00002985 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002986 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002987 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2988 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002989 break;
2990 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002991 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002992 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002993 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2994 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002995 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002996 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002997 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002998 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002999 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
3000 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00003001 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003002 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
3003 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003004 break;
3005 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003006 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
3007 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003008 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00003009 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003010 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
3011 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00003012 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003013 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00003014 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
3015 PrevSpec, DiagID);
3016 break;
3017 case tok::kw_thread_local:
3018 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
3019 PrevSpec, DiagID);
3020 break;
3021 case tok::kw__Thread_local:
3022 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
3023 Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003024 break;
Mike Stump11289f42009-09-09 15:08:12 +00003025
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003026 // function-specifier
3027 case tok::kw_inline:
Serge Pavlov750db652013-11-13 06:57:53 +00003028 isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003029 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00003030 case tok::kw_virtual:
Serge Pavlov750db652013-11-13 06:57:53 +00003031 isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00003032 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00003033 case tok::kw_explicit:
Serge Pavlov750db652013-11-13 06:57:53 +00003034 isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00003035 break;
Richard Smith0015f092013-01-17 22:16:11 +00003036 case tok::kw__Noreturn:
3037 if (!getLangOpts().C11)
3038 Diag(Loc, diag::ext_c11_noreturn);
Serge Pavlov750db652013-11-13 06:57:53 +00003039 isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
Richard Smith0015f092013-01-17 22:16:11 +00003040 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003041
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003042 // alignment-specifier
3043 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003044 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00003045 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003046 ParseAlignmentSpecifier(DS.getAttributes());
3047 continue;
3048
Anders Carlssoncd8db412009-05-06 04:46:28 +00003049 // friend
3050 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00003051 if (DSContext == DSC_class)
3052 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
3053 else {
3054 PrevSpec = ""; // not actually used by the diagnostic
3055 DiagID = diag::err_friend_invalid_in_context;
3056 isInvalid = true;
3057 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00003058 break;
Mike Stump11289f42009-09-09 15:08:12 +00003059
Douglas Gregor26701a42011-09-09 02:06:17 +00003060 // Modules
3061 case tok::kw___module_private__:
3062 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
3063 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003064
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00003065 // constexpr
3066 case tok::kw_constexpr:
3067 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
3068 break;
3069
Chris Lattnere387d9e2009-01-21 19:48:37 +00003070 // type-specifier
3071 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00003072 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
3073 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003074 break;
3075 case tok::kw_long:
3076 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00003077 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
3078 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003079 else
John McCall49bfce42009-08-03 20:12:06 +00003080 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
3081 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003082 break;
Francois Pichet84133e42011-04-28 01:59:37 +00003083 case tok::kw___int64:
3084 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
3085 DiagID);
3086 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003087 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00003088 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
3089 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003090 break;
3091 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00003092 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
3093 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003094 break;
3095 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00003096 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
3097 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003098 break;
3099 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00003100 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
3101 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003102 break;
3103 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00003104 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
3105 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003106 break;
3107 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00003108 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
3109 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003110 break;
3111 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00003112 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
3113 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003114 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00003115 case tok::kw___int128:
3116 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
3117 DiagID);
3118 break;
3119 case tok::kw_half:
3120 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
3121 DiagID);
3122 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003123 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00003124 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
3125 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003126 break;
3127 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00003128 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
3129 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003130 break;
3131 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00003132 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
3133 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003134 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003135 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00003136 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
3137 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003138 break;
3139 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00003140 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
3141 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003142 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003143 case tok::kw_bool:
3144 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003145 if (Tok.is(tok::kw_bool) &&
3146 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
3147 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
3148 PrevSpec = ""; // Not used by the diagnostic.
3149 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003150 // For better error recovery.
3151 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003152 isInvalid = true;
3153 } else {
3154 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
3155 DiagID);
3156 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003157 break;
3158 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00003159 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
3160 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003161 break;
3162 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00003163 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
3164 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003165 break;
3166 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00003167 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
3168 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003169 break;
John Thompson22334602010-02-05 00:12:22 +00003170 case tok::kw___vector:
3171 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3172 break;
3173 case tok::kw___pixel:
3174 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3175 break;
John McCall39439732011-04-09 22:50:59 +00003176 case tok::kw___unknown_anytype:
3177 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
3178 PrevSpec, DiagID);
3179 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003180
3181 // class-specifier:
3182 case tok::kw_class:
3183 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003184 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003185 case tok::kw_union: {
3186 tok::TokenKind Kind = Tok.getKind();
3187 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00003188
3189 // These are attributes following class specifiers.
3190 // To produce better diagnostic, we parse them when
3191 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00003192 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00003193 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00003194 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003195
3196 // If there are attributes following class specifier,
3197 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00003198 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00003199 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00003200 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003201 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003202 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003203 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003204
3205 // enum-specifier:
3206 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003207 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003208 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003209 continue;
3210
3211 // cv-qualifier:
3212 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003213 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003214 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003215 break;
3216 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003217 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003218 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003219 break;
3220 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003221 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003222 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003223 break;
3224
Douglas Gregor333489b2009-03-27 23:10:48 +00003225 // C++ typename-specifier:
3226 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003227 if (TryAnnotateTypeOrScopeToken()) {
3228 DS.SetTypeSpecError();
3229 goto DoneWithDeclSpec;
3230 }
3231 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003232 continue;
3233 break;
3234
Chris Lattnere387d9e2009-01-21 19:48:37 +00003235 // GNU typeof support.
3236 case tok::kw_typeof:
3237 ParseTypeofSpecifier(DS);
3238 continue;
3239
David Blaikie15a430a2011-12-04 05:04:18 +00003240 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003241 ParseDecltypeSpecifier(DS);
3242 continue;
3243
Alexis Hunt4a257072011-05-19 05:37:45 +00003244 case tok::kw___underlying_type:
3245 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003246 continue;
3247
3248 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003249 // C11 6.7.2.4/4:
3250 // If the _Atomic keyword is immediately followed by a left parenthesis,
3251 // it is interpreted as a type specifier (with a type name), not as a
3252 // type qualifier.
3253 if (NextToken().is(tok::l_paren)) {
3254 ParseAtomicSpecifier(DS);
3255 continue;
3256 }
3257 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3258 getLangOpts());
3259 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003260
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003261 // OpenCL qualifiers:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003262 case tok::kw___private:
3263 case tok::kw___global:
3264 case tok::kw___local:
3265 case tok::kw___constant:
3266 case tok::kw___read_only:
3267 case tok::kw___write_only:
3268 case tok::kw___read_write:
3269 ParseOpenCLQualifiers(DS);
3270 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003271
Steve Naroffcfdf6162008-06-05 00:02:44 +00003272 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003273 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003274 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3275 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003276 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00003277 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003278
Douglas Gregor3a001f42010-11-19 17:10:50 +00003279 if (!ParseObjCProtocolQualifiers(DS))
3280 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3281 << FixItHint::CreateInsertion(Loc, "id")
3282 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00003283
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003284 // Need to support trailing type qualifiers (e.g. "id<p> const").
3285 // If a type specifier follows, it will be diagnosed elsewhere.
3286 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003287 }
John McCall49bfce42009-08-03 20:12:06 +00003288 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003289 if (isInvalid) {
3290 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003291 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003292
Douglas Gregora05f5ab2010-08-23 14:34:43 +00003293 if (DiagID == diag::ext_duplicate_declspec)
3294 Diag(Tok, DiagID)
3295 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3296 else
3297 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003298 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003299
Chris Lattner2e232092008-03-13 06:29:04 +00003300 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003301 if (DiagID != diag::err_bool_redeclaration)
3302 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003303
3304 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003305 }
3306}
Douglas Gregoreb31f392008-12-01 23:54:00 +00003307
Chris Lattner70ae4912007-10-29 04:42:53 +00003308/// ParseStructDeclaration - Parse a struct declaration without the terminating
3309/// semicolon.
3310///
Chris Lattner90a26b02007-01-23 04:38:16 +00003311/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00003312/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00003313/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00003314/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00003315/// struct-declarator-list:
3316/// struct-declarator
3317/// struct-declarator-list ',' struct-declarator
3318/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3319/// struct-declarator:
3320/// declarator
3321/// [GNU] declarator attributes[opt]
3322/// declarator[opt] ':' constant-expression
3323/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3324///
Chris Lattnera12405b2008-04-10 06:46:29 +00003325void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003326ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00003327
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003328 if (Tok.is(tok::kw___extension__)) {
3329 // __extension__ silences extension warnings in the subexpression.
3330 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00003331 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003332 return ParseStructDeclaration(DS, Fields);
3333 }
Mike Stump11289f42009-09-09 15:08:12 +00003334
Steve Naroff97170802007-08-20 22:28:22 +00003335 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00003336 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003337
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00003338 // If there are no declarators, this is a free-standing declaration
3339 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00003340 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003341 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3342 DS);
3343 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00003344 return;
3345 }
3346
3347 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00003348 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00003349 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00003350 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003351 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00003352 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00003353
Bill Wendling44426052012-12-20 19:22:21 +00003354 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00003355 if (!FirstDeclarator)
3356 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00003357
Steve Naroff97170802007-08-20 22:28:22 +00003358 /// struct-declarator: declarator
3359 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003360 if (Tok.isNot(tok::colon)) {
3361 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3362 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00003363 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003364 }
Mike Stump11289f42009-09-09 15:08:12 +00003365
Alp Toker8fbec672013-12-17 23:29:36 +00003366 if (TryConsumeToken(tok::colon)) {
John McCalldadc5752010-08-24 06:29:42 +00003367 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003368 if (Res.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00003369 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner32295d32008-04-10 06:15:14 +00003370 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00003371 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00003372 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003373
Steve Naroff97170802007-08-20 22:28:22 +00003374 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003375 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003376
John McCallcfefb6d2009-11-03 02:38:08 +00003377 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00003378 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00003379
Steve Naroff97170802007-08-20 22:28:22 +00003380 // If we don't have a comma, it is either the end of the list (a ';')
3381 // or an error, bail out.
Alp Toker8fbec672013-12-17 23:29:36 +00003382 if (!TryConsumeToken(tok::comma, CommaLoc))
Chris Lattner70ae4912007-10-29 04:42:53 +00003383 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003384
John McCallcfefb6d2009-11-03 02:38:08 +00003385 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003386 }
Steve Naroff97170802007-08-20 22:28:22 +00003387}
3388
3389/// ParseStructUnionBody
3390/// struct-contents:
3391/// struct-declaration-list
3392/// [EXT] empty
3393/// [GNU] "struct-declaration-list" without terminatoring ';'
3394/// struct-declaration-list:
3395/// struct-declaration
3396/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003397/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003398///
Chris Lattner1300fb92007-01-23 23:42:53 +00003399void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003400 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003401 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3402 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00003403 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00003404
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003405 BalancedDelimiterTracker T(*this, tok::l_brace);
3406 if (T.consumeOpen())
3407 return;
Mike Stump11289f42009-09-09 15:08:12 +00003408
Douglas Gregor658b9552009-01-09 22:42:13 +00003409 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003410 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003411
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003412 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003413
Chris Lattner7b9ace62007-01-23 20:11:08 +00003414 // While we still have something to read, read the declarations in the struct.
Richard Smith34f30512013-11-23 04:06:09 +00003415 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003416 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003417
Chris Lattner736ed5d2007-06-09 05:59:07 +00003418 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003419 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003420 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003421 continue;
3422 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003423
Andy Gibbsc804e082013-04-03 09:46:04 +00003424 // Parse _Static_assert declaration.
3425 if (Tok.is(tok::kw__Static_assert)) {
3426 SourceLocation DeclEnd;
3427 ParseStaticAssertDeclaration(DeclEnd);
3428 continue;
3429 }
3430
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00003431 if (Tok.is(tok::annot_pragma_pack)) {
3432 HandlePragmaPack();
3433 continue;
3434 }
3435
3436 if (Tok.is(tok::annot_pragma_align)) {
3437 HandlePragmaAlign();
3438 continue;
3439 }
3440
John McCallcfefb6d2009-11-03 02:38:08 +00003441 if (!Tok.is(tok::at)) {
3442 struct CFieldCallback : FieldCallback {
3443 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003444 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003445 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003446
John McCall48871652010-08-21 09:40:31 +00003447 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003448 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003449 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3450
Eli Friedman934dbbf2012-08-08 23:53:27 +00003451 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003452 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003453 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003454 FD.D.getDeclSpec().getSourceRange().getBegin(),
3455 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003456 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003457 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003458 }
John McCallcfefb6d2009-11-03 02:38:08 +00003459 } Callback(*this, TagDecl, FieldDecls);
3460
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003461 // Parse all the comma separated declarators.
3462 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003463 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003464 } else { // Handle @defs
3465 ConsumeToken();
3466 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3467 Diag(Tok, diag::err_unexpected_at);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003468 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003469 continue;
3470 }
3471 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00003472 ExpectAndConsume(tok::l_paren);
Chris Lattner535b8302008-06-21 19:39:06 +00003473 if (!Tok.is(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00003474 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003475 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003476 continue;
3477 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003478 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003479 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003480 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003481 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3482 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00003483 ExpectAndConsume(tok::r_paren);
Mike Stump11289f42009-09-09 15:08:12 +00003484 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003485
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003486 if (TryConsumeToken(tok::semi))
3487 continue;
3488
3489 if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003490 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003491 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003492 }
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003493
3494 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3495 // Skip to end of block or statement to avoid ext-warning on extra ';'.
3496 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
3497 // If we stopped at a ';', eat it.
3498 TryConsumeToken(tok::semi);
Chris Lattner90a26b02007-01-23 04:38:16 +00003499 }
Mike Stump11289f42009-09-09 15:08:12 +00003500
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003501 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003502
John McCall084e83d2011-03-24 11:26:52 +00003503 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003504 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003505 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003506
Douglas Gregor0be31a22010-07-02 17:43:08 +00003507 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003508 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003509 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003510 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003511 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003512 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3513 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003514}
3515
Chris Lattner3b561a32006-08-13 00:12:11 +00003516/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003517/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003518/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003519///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003520/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3521/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003522/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3523/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003524/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003525/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003526///
Richard Smith7d137e32012-03-23 03:33:32 +00003527/// [C++11] enum-head '{' enumerator-list[opt] '}'
3528/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003529///
Richard Smith7d137e32012-03-23 03:33:32 +00003530/// enum-head: [C++11]
3531/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3532/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3533/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003534///
Richard Smith7d137e32012-03-23 03:33:32 +00003535/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003536/// 'enum'
3537/// 'enum' 'class'
3538/// 'enum' 'struct'
3539///
Richard Smith7d137e32012-03-23 03:33:32 +00003540/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003541/// ':' type-specifier-seq
3542///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003543/// [C++] elaborated-type-specifier:
3544/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3545///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003546void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003547 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003548 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003549 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003550 if (Tok.is(tok::code_completion)) {
3551 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003552 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003553 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003554 }
John McCallcb432fa2011-07-06 05:58:41 +00003555
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003556 // If attributes exist after tag, parse them.
3557 ParsedAttributesWithRange attrs(AttrFactory);
3558 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003559 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003560
3561 // If declspecs exist after tag, parse them.
3562 while (Tok.is(tok::kw___declspec))
3563 ParseMicrosoftDeclSpec(attrs);
3564
Richard Smith0f8ee222012-01-10 01:33:14 +00003565 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003566 bool IsScopedUsingClassTag = false;
3567
John McCallbeae29a2012-06-23 22:30:04 +00003568 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieud0d87b52013-04-23 02:47:36 +00003569 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3570 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3571 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003572 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003573 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003574
Bill Wendling44426052012-12-20 19:22:21 +00003575 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003576 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003577 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003578
3579 // They are allowed afterwards, though.
3580 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003581 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003582 while (Tok.is(tok::kw___declspec))
3583 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003584 }
Richard Smith7d137e32012-03-23 03:33:32 +00003585
John McCall6347b682012-05-07 06:16:58 +00003586 // C++11 [temp.explicit]p12:
3587 // The usual access controls do not apply to names used to specify
3588 // explicit instantiations.
3589 // We extend this to also cover explicit specializations. Note that
3590 // we don't suppress if this turns out to be an elaborated type
3591 // specifier.
3592 bool shouldDelayDiagsInTag =
3593 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3594 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3595 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003596
Richard Smithbfdb1082012-03-12 08:56:40 +00003597 // Enum definitions should not be parsed in a trailing-return-type.
3598 bool AllowDeclaration = DSC != DSC_trailing;
3599
3600 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003601 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003602 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003603
Abramo Bagnarad7548482010-05-19 21:37:53 +00003604 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003605 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003606 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3607 // if a fixed underlying type is allowed.
3608 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003609
3610 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith1d4b2e12013-04-01 21:43:41 +00003611 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00003612 return;
3613
3614 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00003615 Diag(Tok, diag::err_expected) << tok::identifier;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003616 if (Tok.isNot(tok::l_brace)) {
3617 // Has no name and is not a definition.
3618 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003619 SkipUntil(tok::comma, StopAtSemi);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003620 return;
3621 }
3622 }
3623 }
Mike Stump11289f42009-09-09 15:08:12 +00003624
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003625 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003626 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003627 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Alp Tokerec543272013-12-24 09:48:30 +00003628 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
Mike Stump11289f42009-09-09 15:08:12 +00003629
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003630 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003631 SkipUntil(tok::comma, StopAtSemi);
Chris Lattner3b561a32006-08-13 00:12:11 +00003632 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003633 }
Mike Stump11289f42009-09-09 15:08:12 +00003634
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003635 // If an identifier is present, consume and remember it.
3636 IdentifierInfo *Name = 0;
3637 SourceLocation NameLoc;
3638 if (Tok.is(tok::identifier)) {
3639 Name = Tok.getIdentifierInfo();
3640 NameLoc = ConsumeToken();
3641 }
Mike Stump11289f42009-09-09 15:08:12 +00003642
Richard Smith0f8ee222012-01-10 01:33:14 +00003643 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003644 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3645 // declaration of a scoped enumeration.
3646 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003647 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003648 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003649 }
3650
John McCall6347b682012-05-07 06:16:58 +00003651 // Okay, end the suppression area. We'll decide whether to emit the
3652 // diagnostics in a second.
3653 if (shouldDelayDiagsInTag)
3654 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003655
Douglas Gregor0bf31402010-10-08 23:50:27 +00003656 TypeResult BaseType;
3657
Douglas Gregord1f69f62010-12-01 17:42:47 +00003658 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003659 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003660 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003661 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003662 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003663 // If we're in class scope, this can either be an enum declaration with
3664 // an underlying type, or a declaration of a bitfield member. We try to
3665 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003666 // (integer literal, sizeof); if it's still ambiguous, we then consider
3667 // anything that's a simple-type-specifier followed by '(' as an
3668 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003669 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003670 EnterExpressionEvaluationContext Unevaluated(Actions,
3671 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003672 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003673 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003674 // bit-field. This is the common case.
3675 if (TPR == TPResult::True())
3676 PossibleBitfield = true;
3677 // If the next token starts a type-specifier-seq, it may be either a
3678 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003679 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003680 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003681 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003682 GetLookAheadToken(2).getKind() == tok::semi) {
3683 // Consume the ':'.
3684 ConsumeToken();
3685 } else {
3686 // We have the start of a type-specifier-seq, so we have to perform
3687 // tentative parsing to determine whether we have an expression or a
3688 // type.
3689 TentativeParsingAction TPA(*this);
3690
3691 // Consume the ':'.
3692 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003693
3694 // If we see a type specifier followed by an open-brace, we have an
3695 // ambiguity between an underlying type and a C++11 braced
3696 // function-style cast. Resolve this by always treating it as an
3697 // underlying type.
3698 // FIXME: The standard is not entirely clear on how to disambiguate in
3699 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003700 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003701 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003702 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003703 // We'll parse this as a bitfield later.
3704 PossibleBitfield = true;
3705 TPA.Revert();
3706 } else {
3707 // We have a type-specifier-seq.
3708 TPA.Commit();
3709 }
3710 }
3711 } else {
3712 // Consume the ':'.
3713 ConsumeToken();
3714 }
3715
3716 if (!PossibleBitfield) {
3717 SourceRange Range;
3718 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003719
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003720 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003721 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003722 } else if (!getLangOpts().ObjC2) {
3723 if (getLangOpts().CPlusPlus)
3724 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3725 else
3726 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3727 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003728 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003729 }
3730
Richard Smith0f8ee222012-01-10 01:33:14 +00003731 // There are four options here. If we have 'friend enum foo;' then this is a
3732 // friend declaration, and cannot have an accompanying definition. If we have
3733 // 'enum foo;', then this is a forward declaration. If we have
3734 // 'enum foo {...' then this is a definition. Otherwise we have something
3735 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003736 //
3737 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3738 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3739 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3740 //
John McCallfaf5fb42010-08-26 23:41:50 +00003741 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003742 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003743 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003744 } else if (Tok.is(tok::l_brace)) {
3745 if (DS.isFriendSpecified()) {
3746 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3747 << SourceRange(DS.getFriendSpecLoc());
3748 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003749 SkipUntil(tok::r_brace, StopAtSemi);
John McCall6347b682012-05-07 06:16:58 +00003750 TUK = Sema::TUK_Friend;
3751 } else {
3752 TUK = Sema::TUK_Definition;
3753 }
Richard Smith369b9f92012-06-25 21:37:02 +00003754 } else if (DSC != DSC_type_specifier &&
3755 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003756 (Tok.isAtStartOfLine() &&
3757 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003758 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3759 if (Tok.isNot(tok::semi)) {
3760 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00003761 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Richard Smith369b9f92012-06-25 21:37:02 +00003762 PP.EnterToken(Tok);
3763 Tok.setKind(tok::semi);
3764 }
John McCall6347b682012-05-07 06:16:58 +00003765 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003766 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003767 }
3768
3769 // If this is an elaborated type specifier, and we delayed
3770 // diagnostics before, just merge them into the current pool.
3771 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3772 diagsFromTag.redelay();
3773 }
Richard Smith7d137e32012-03-23 03:33:32 +00003774
3775 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003776 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003777 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003778 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003779 // Skip the rest of this declarator, up until the comma or semicolon.
3780 Diag(Tok, diag::err_enum_template);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003781 SkipUntil(tok::comma, StopAtSemi);
Richard Smith7d137e32012-03-23 03:33:32 +00003782 return;
3783 }
3784
3785 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3786 // Enumerations can't be explicitly instantiated.
3787 DS.SetTypeSpecError();
3788 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3789 return;
3790 }
3791
3792 assert(TemplateInfo.TemplateParams && "no template parameters");
3793 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3794 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003795 }
Chad Rosierc1183952012-06-26 22:30:43 +00003796
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003797 if (TUK == Sema::TUK_Reference)
3798 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003799
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003800 if (!Name && TUK != Sema::TUK_Definition) {
3801 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003802
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003803 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003804 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003805 return;
3806 }
Richard Smith7d137e32012-03-23 03:33:32 +00003807
Douglas Gregord6ab8742009-05-28 23:31:59 +00003808 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003809 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003810 const char *PrevSpec = 0;
3811 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003812 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003813 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003814 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003815 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003816 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003817
Douglas Gregorba41d012010-04-24 16:38:41 +00003818 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003819 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003820 // dependent tag.
3821 if (!Name) {
3822 DS.SetTypeSpecError();
3823 Diag(Tok, diag::err_expected_type_name_after_typename);
3824 return;
3825 }
Chad Rosierc1183952012-06-26 22:30:43 +00003826
Douglas Gregor0be31a22010-07-02 17:43:08 +00003827 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003828 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003829 NameLoc);
3830 if (Type.isInvalid()) {
3831 DS.SetTypeSpecError();
3832 return;
3833 }
Chad Rosierc1183952012-06-26 22:30:43 +00003834
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003835 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3836 NameLoc.isValid() ? NameLoc : StartLoc,
3837 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003838 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003839
Douglas Gregorba41d012010-04-24 16:38:41 +00003840 return;
3841 }
Mike Stump11289f42009-09-09 15:08:12 +00003842
John McCall48871652010-08-21 09:40:31 +00003843 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003844 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003845 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003846 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003847 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003848 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregorba41d012010-04-24 16:38:41 +00003849 }
Chad Rosierc1183952012-06-26 22:30:43 +00003850
Douglas Gregorba41d012010-04-24 16:38:41 +00003851 DS.SetTypeSpecError();
3852 return;
3853 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003854
Richard Smith369b9f92012-06-25 21:37:02 +00003855 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003856 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003857
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003858 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3859 NameLoc.isValid() ? NameLoc : StartLoc,
3860 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003861 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003862}
3863
Chris Lattnerc1915e22007-01-25 07:29:02 +00003864/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3865/// enumerator-list:
3866/// enumerator
3867/// enumerator-list ',' enumerator
3868/// enumerator:
3869/// enumeration-constant
3870/// enumeration-constant '=' constant-expression
3871/// enumeration-constant:
3872/// identifier
3873///
John McCall48871652010-08-21 09:40:31 +00003874void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003875 // Enter the scope of the enum body and start the definition.
3876 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003877 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003878
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003879 BalancedDelimiterTracker T(*this, tok::l_brace);
3880 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003881
Chris Lattner37256fb2007-08-27 17:24:30 +00003882 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003883 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003884 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003885
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003886 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003887
John McCall48871652010-08-21 09:40:31 +00003888 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003889
Chris Lattnerc1915e22007-01-25 07:29:02 +00003890 // Parse the enumerator-list.
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003891 while (Tok.isNot(tok::r_brace)) {
3892 // Parse enumerator. If failed, try skipping till the start of the next
3893 // enumerator definition.
3894 if (Tok.isNot(tok::identifier)) {
3895 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
3896 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) &&
3897 TryConsumeToken(tok::comma))
3898 continue;
3899 break;
3900 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003901 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3902 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003903
John McCall811a0f52010-10-22 23:36:17 +00003904 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003905 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003906 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003907 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003908 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003909
Chris Lattnerc1915e22007-01-25 07:29:02 +00003910 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003911 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003912 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003913
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003914 if (TryConsumeToken(tok::equal, EqualLoc)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003915 AssignedVal = ParseConstantExpression();
3916 if (AssignedVal.isInvalid())
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003917 SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003918 }
Mike Stump11289f42009-09-09 15:08:12 +00003919
Chris Lattnerc1915e22007-01-25 07:29:02 +00003920 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003921 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3922 LastEnumConstDecl,
3923 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003924 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003925 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003926 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003927
Chris Lattner4ef40012007-06-11 01:28:17 +00003928 EnumConstantDecls.push_back(EnumConstDecl);
3929 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003930
Douglas Gregorce66d022010-09-07 14:51:08 +00003931 if (Tok.is(tok::identifier)) {
3932 // We're missing a comma between enumerators.
3933 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003934 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003935 << FixItHint::CreateInsertion(Loc, ", ");
3936 continue;
3937 }
Chad Rosierc1183952012-06-26 22:30:43 +00003938
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003939 // Emumerator definition must be finished, only comma or r_brace are
3940 // allowed here.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003941 SourceLocation CommaLoc;
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003942 if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) {
3943 if (EqualLoc.isValid())
3944 Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace
3945 << tok::comma;
3946 else
3947 Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator);
3948 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) {
3949 if (TryConsumeToken(tok::comma, CommaLoc))
3950 continue;
3951 } else {
3952 break;
3953 }
3954 }
Mike Stump11289f42009-09-09 15:08:12 +00003955
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003956 // If comma is followed by r_brace, emit appropriate warning.
3957 if (Tok.is(tok::r_brace) && CommaLoc.isValid()) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003958 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003959 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3960 diag::ext_enumerator_list_comma_cxx :
3961 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003962 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003963 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003964 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3965 << FixItHint::CreateRemoval(CommaLoc);
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003966 break;
Richard Smith5d164bc2011-10-15 05:09:34 +00003967 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003968 }
Mike Stump11289f42009-09-09 15:08:12 +00003969
Chris Lattnerc1915e22007-01-25 07:29:02 +00003970 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003971 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003972
Chris Lattnerc1915e22007-01-25 07:29:02 +00003973 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003974 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003975 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003976
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003977 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenkoe5fde992013-04-27 20:23:52 +00003978 EnumDecl, EnumConstantDecls,
3979 getCurScope(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003980 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003981
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003982 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003983 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3984 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003985
3986 // The next token must be valid after an enum definition. If not, a ';'
3987 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003988 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3989 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Alp Toker383d2c42014-01-01 03:08:43 +00003990 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Richard Smith369b9f92012-06-25 21:37:02 +00003991 // Push this token back into the preprocessor and change our current token
3992 // to ';' so that the rest of the code recovers as though there were an
3993 // ';' after the definition.
3994 PP.EnterToken(Tok);
3995 Tok.setKind(tok::semi);
3996 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003997}
Chris Lattner3b561a32006-08-13 00:12:11 +00003998
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003999/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004000/// start of a type-qualifier-list.
4001bool Parser::isTypeQualifier() const {
4002 switch (Tok.getKind()) {
4003 default: return false;
Alp Tokerde50ff32013-12-17 18:17:46 +00004004 // type-qualifier
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004005 case tok::kw_const:
4006 case tok::kw_volatile:
4007 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004008 case tok::kw___private:
4009 case tok::kw___local:
4010 case tok::kw___global:
4011 case tok::kw___constant:
4012 case tok::kw___read_only:
4013 case tok::kw___read_write:
4014 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004015 return true;
4016 }
4017}
4018
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004019/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
4020/// is definitely a type-specifier. Return false if it isn't part of a type
4021/// specifier or if we're not sure.
4022bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
4023 switch (Tok.getKind()) {
4024 default: return false;
4025 // type-specifiers
4026 case tok::kw_short:
4027 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004028 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004029 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004030 case tok::kw_signed:
4031 case tok::kw_unsigned:
4032 case tok::kw__Complex:
4033 case tok::kw__Imaginary:
4034 case tok::kw_void:
4035 case tok::kw_char:
4036 case tok::kw_wchar_t:
4037 case tok::kw_char16_t:
4038 case tok::kw_char32_t:
4039 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004040 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004041 case tok::kw_float:
4042 case tok::kw_double:
4043 case tok::kw_bool:
4044 case tok::kw__Bool:
4045 case tok::kw__Decimal32:
4046 case tok::kw__Decimal64:
4047 case tok::kw__Decimal128:
4048 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00004049
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004050 // struct-or-union-specifier (C99) or class-specifier (C++)
4051 case tok::kw_class:
4052 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004053 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004054 case tok::kw_union:
4055 // enum-specifier
4056 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00004057
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004058 // typedef-name
4059 case tok::annot_typename:
4060 return true;
4061 }
4062}
4063
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004064/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004065/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004066bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004067 switch (Tok.getKind()) {
4068 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004069
Chris Lattner020bab92009-01-04 23:41:41 +00004070 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00004071 if (TryAltiVecVectorToken())
4072 return true;
4073 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00004074 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004075 // Annotate typenames and C++ scope specifiers. If we get one, just
4076 // recurse to handle whatever we get.
4077 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004078 return true;
4079 if (Tok.is(tok::identifier))
4080 return false;
4081 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00004082
Chris Lattner020bab92009-01-04 23:41:41 +00004083 case tok::coloncolon: // ::foo::bar
4084 if (NextToken().is(tok::kw_new) || // ::new
4085 NextToken().is(tok::kw_delete)) // ::delete
4086 return false;
4087
Chris Lattner020bab92009-01-04 23:41:41 +00004088 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004089 return true;
4090 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00004091
Chris Lattnere37e2332006-08-15 04:50:22 +00004092 // GNU attributes support.
4093 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00004094 // GNU typeof support.
4095 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004096
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004097 // type-specifiers
4098 case tok::kw_short:
4099 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004100 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004101 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004102 case tok::kw_signed:
4103 case tok::kw_unsigned:
4104 case tok::kw__Complex:
4105 case tok::kw__Imaginary:
4106 case tok::kw_void:
4107 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004108 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004109 case tok::kw_char16_t:
4110 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004111 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004112 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004113 case tok::kw_float:
4114 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004115 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004116 case tok::kw__Bool:
4117 case tok::kw__Decimal32:
4118 case tok::kw__Decimal64:
4119 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004120 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004121
Chris Lattner861a2262008-04-13 18:59:07 +00004122 // struct-or-union-specifier (C99) or class-specifier (C++)
4123 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004124 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004125 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004126 case tok::kw_union:
4127 // enum-specifier
4128 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004129
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004130 // type-qualifier
4131 case tok::kw_const:
4132 case tok::kw_volatile:
4133 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004134
John McCallea0a39e2012-11-14 00:49:39 +00004135 // Debugger support.
4136 case tok::kw___unknown_anytype:
4137
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004138 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00004139 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004140 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004141
Chris Lattner409bf7d2008-10-20 00:25:30 +00004142 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4143 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004144 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004145
Steve Naroff44ac7772008-12-25 14:16:32 +00004146 case tok::kw___cdecl:
4147 case tok::kw___stdcall:
4148 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004149 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004150 case tok::kw___w64:
4151 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004152 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004153 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004154 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004155
4156 case tok::kw___private:
4157 case tok::kw___local:
4158 case tok::kw___global:
4159 case tok::kw___constant:
4160 case tok::kw___read_only:
4161 case tok::kw___read_write:
4162 case tok::kw___write_only:
4163
Eli Friedman53339e02009-06-08 23:27:34 +00004164 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004165
Richard Smith8e1ac332013-03-28 01:55:44 +00004166 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004167 case tok::kw__Atomic:
4168 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004169 }
4170}
4171
Chris Lattneracd58a32006-08-06 17:24:14 +00004172/// isDeclarationSpecifier() - Return true if the current token is part of a
4173/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004174///
4175/// \param DisambiguatingWithExpression True to indicate that the purpose of
4176/// this check is to disambiguate between an expression and a declaration.
4177bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004178 switch (Tok.getKind()) {
4179 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004180
Chris Lattner020bab92009-01-04 23:41:41 +00004181 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00004182 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004183 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004184 return false;
John Thompson22334602010-02-05 00:12:22 +00004185 if (TryAltiVecVectorToken())
4186 return true;
4187 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00004188 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004189 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004190 // Annotate typenames and C++ scope specifiers. If we get one, just
4191 // recurse to handle whatever we get.
4192 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004193 return true;
4194 if (Tok.is(tok::identifier))
4195 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004196
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004197 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004198 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004199 // expression is permitted, then this is probably a class message send
4200 // missing the initial '['. In this case, we won't consider this to be
4201 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004202 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004203 isStartOfObjCClassMessageMissingOpenBracket())
4204 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004205
John McCall1f476a12010-02-26 08:45:28 +00004206 return isDeclarationSpecifier();
4207
Chris Lattner020bab92009-01-04 23:41:41 +00004208 case tok::coloncolon: // ::foo::bar
4209 if (NextToken().is(tok::kw_new) || // ::new
4210 NextToken().is(tok::kw_delete)) // ::delete
4211 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004212
Chris Lattner020bab92009-01-04 23:41:41 +00004213 // Annotate typenames and C++ scope specifiers. If we get one, just
4214 // recurse to handle whatever we get.
4215 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004216 return true;
4217 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004218
Chris Lattneracd58a32006-08-06 17:24:14 +00004219 // storage-class-specifier
4220 case tok::kw_typedef:
4221 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004222 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004223 case tok::kw_static:
4224 case tok::kw_auto:
4225 case tok::kw_register:
4226 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004227 case tok::kw_thread_local:
4228 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004229
Douglas Gregor26701a42011-09-09 02:06:17 +00004230 // Modules
4231 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00004232
John McCallea0a39e2012-11-14 00:49:39 +00004233 // Debugger support
4234 case tok::kw___unknown_anytype:
4235
Chris Lattneracd58a32006-08-06 17:24:14 +00004236 // type-specifiers
4237 case tok::kw_short:
4238 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004239 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004240 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00004241 case tok::kw_signed:
4242 case tok::kw_unsigned:
4243 case tok::kw__Complex:
4244 case tok::kw__Imaginary:
4245 case tok::kw_void:
4246 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004247 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004248 case tok::kw_char16_t:
4249 case tok::kw_char32_t:
4250
Chris Lattneracd58a32006-08-06 17:24:14 +00004251 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004252 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00004253 case tok::kw_float:
4254 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004255 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00004256 case tok::kw__Bool:
4257 case tok::kw__Decimal32:
4258 case tok::kw__Decimal64:
4259 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004260 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004261
Chris Lattner861a2262008-04-13 18:59:07 +00004262 // struct-or-union-specifier (C99) or class-specifier (C++)
4263 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00004264 case tok::kw_struct:
4265 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00004266 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00004267 // enum-specifier
4268 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004269
Chris Lattneracd58a32006-08-06 17:24:14 +00004270 // type-qualifier
4271 case tok::kw_const:
4272 case tok::kw_volatile:
4273 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00004274
Chris Lattneracd58a32006-08-06 17:24:14 +00004275 // function-specifier
4276 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00004277 case tok::kw_virtual:
4278 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00004279 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00004280
Richard Smith1dba27c2013-01-29 09:02:09 +00004281 // alignment-specifier
4282 case tok::kw__Alignas:
4283
Richard Smithd16fe122012-10-25 00:00:53 +00004284 // friend keyword.
4285 case tok::kw_friend:
4286
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00004287 // static_assert-declaration
4288 case tok::kw__Static_assert:
4289
Chris Lattner599e47e2007-08-09 17:01:07 +00004290 // GNU typeof support.
4291 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004292
Chris Lattner599e47e2007-08-09 17:01:07 +00004293 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00004294 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00004295
Richard Smithd16fe122012-10-25 00:00:53 +00004296 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00004297 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00004298 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00004299
Richard Smith8e1ac332013-03-28 01:55:44 +00004300 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004301 case tok::kw__Atomic:
4302 return true;
4303
Chris Lattner8b2ec162008-07-26 03:38:44 +00004304 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4305 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004306 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004307
Douglas Gregor19b7acf2011-04-27 05:41:15 +00004308 // typedef-name
4309 case tok::annot_typename:
4310 return !DisambiguatingWithExpression ||
4311 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00004312
Steve Narofff192fab2009-01-06 19:34:12 +00004313 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00004314 case tok::kw___cdecl:
4315 case tok::kw___stdcall:
4316 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004317 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004318 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004319 case tok::kw___sptr:
4320 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004321 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004322 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00004323 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004324 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004325 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004326
4327 case tok::kw___private:
4328 case tok::kw___local:
4329 case tok::kw___global:
4330 case tok::kw___constant:
4331 case tok::kw___read_only:
4332 case tok::kw___read_write:
4333 case tok::kw___write_only:
4334
Eli Friedman53339e02009-06-08 23:27:34 +00004335 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00004336 }
4337}
4338
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004339bool Parser::isConstructorDeclarator() {
4340 TentativeParsingAction TPA(*this);
4341
4342 // Parse the C++ scope specifier.
4343 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00004344 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004345 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00004346 TPA.Revert();
4347 return false;
4348 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004349
4350 // Parse the constructor name.
4351 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4352 // We already know that we have a constructor name; just consume
4353 // the token.
4354 ConsumeToken();
4355 } else {
4356 TPA.Revert();
4357 return false;
4358 }
4359
Richard Smith43f340f2012-03-27 23:05:05 +00004360 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004361 if (Tok.isNot(tok::l_paren)) {
4362 TPA.Revert();
4363 return false;
4364 }
4365 ConsumeParen();
4366
Richard Smith43f340f2012-03-27 23:05:05 +00004367 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4368 // that we have a constructor.
4369 if (Tok.is(tok::r_paren) ||
4370 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004371 TPA.Revert();
4372 return true;
4373 }
4374
Richard Smithf2163662013-09-06 00:12:20 +00004375 // A C++11 attribute here signals that we have a constructor, and is an
4376 // attribute on the first constructor parameter.
4377 if (getLangOpts().CPlusPlus11 &&
4378 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
4379 /*OuterMightBeMessageSend*/ true)) {
4380 TPA.Revert();
4381 return true;
4382 }
4383
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004384 // If we need to, enter the specified scope.
4385 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004386 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004387 DeclScopeObj.EnterDeclaratorScope();
4388
Francois Pichet79f3a872011-01-31 04:54:32 +00004389 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00004390 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004391 MaybeParseMicrosoftAttributes(Attrs);
4392
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004393 // Check whether the next token(s) are part of a declaration
4394 // specifier, in which case we have the start of a parameter and,
4395 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004396 bool IsConstructor = false;
4397 if (isDeclarationSpecifier())
4398 IsConstructor = true;
4399 else if (Tok.is(tok::identifier) ||
4400 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4401 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4402 // This might be a parenthesized member name, but is more likely to
4403 // be a constructor declaration with an invalid argument type. Keep
4404 // looking.
4405 if (Tok.is(tok::annot_cxxscope))
4406 ConsumeToken();
4407 ConsumeToken();
4408
4409 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004410 // which must have one of the following syntactic forms (see the
4411 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004412 switch (Tok.getKind()) {
4413 case tok::l_paren:
4414 // C(X ( int));
4415 case tok::l_square:
4416 // C(X [ 5]);
4417 // C(X [ [attribute]]);
4418 case tok::coloncolon:
4419 // C(X :: Y);
4420 // C(X :: *p);
4421 case tok::r_paren:
4422 // C(X )
4423 // Assume this isn't a constructor, rather than assuming it's a
4424 // constructor with an unnamed parameter of an ill-formed type.
4425 break;
4426
4427 default:
4428 IsConstructor = true;
4429 break;
4430 }
4431 }
4432
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004433 TPA.Revert();
4434 return IsConstructor;
4435}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004436
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004437/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004438/// type-qualifier-list: [C99 6.7.5]
4439/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004440/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004441/// [ only if VendorAttributesAllowed=true ]
4442/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004443/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004444/// [ only if VendorAttributesAllowed=true ]
4445/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004446/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004447/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004448///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004449void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4450 bool VendorAttributesAllowed,
Richard Smith8e1ac332013-03-28 01:55:44 +00004451 bool CXX11AttributesAllowed,
Alp Toker62c5b572013-11-26 01:30:10 +00004452 bool AtomicAllowed,
4453 bool IdentifierRequired) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004454 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004455 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004456 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004457 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004458 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004459 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004460
4461 SourceLocation EndLoc;
4462
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004463 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004464 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004465 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004466 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004467 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004468
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004469 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004470 case tok::code_completion:
4471 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004472 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004473
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004474 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004475 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004476 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004477 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004478 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004479 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004480 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004481 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004482 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004483 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004484 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004485 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00004486 case tok::kw__Atomic:
4487 if (!AtomicAllowed)
4488 goto DoneWithTypeQuals;
4489 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4490 getLangOpts());
4491 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004492
4493 // OpenCL qualifiers:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004494 case tok::kw___private:
4495 case tok::kw___global:
4496 case tok::kw___local:
4497 case tok::kw___constant:
4498 case tok::kw___read_only:
4499 case tok::kw___write_only:
4500 case tok::kw___read_write:
4501 ParseOpenCLQualifiers(DS);
4502 break;
4503
Aaron Ballman317a77f2013-05-22 23:25:32 +00004504 case tok::kw___uptr:
Alp Toker62c5b572013-11-26 01:30:10 +00004505 // GNU libc headers in C mode use '__uptr' as an identifer which conflicts
4506 // with the MS modifier keyword.
4507 if (VendorAttributesAllowed && !getLangOpts().CPlusPlus &&
Alp Toker47642d22013-12-03 06:13:01 +00004508 IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) {
4509 if (TryKeywordIdentFallback(false))
4510 continue;
Alp Toker62c5b572013-11-26 01:30:10 +00004511 }
4512 case tok::kw___sptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004513 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004514 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004515 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004516 case tok::kw___cdecl:
4517 case tok::kw___stdcall:
4518 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004519 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004520 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004521 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004522 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004523 continue;
4524 }
4525 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004526 case tok::kw___pascal:
4527 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004528 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004529 continue;
4530 }
4531 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004532 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004533 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004534 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004535 continue; // do *not* consume the next token!
4536 }
4537 // otherwise, FALL THROUGH!
4538 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004539 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004540 // If this is not a type-qualifier token, we're done reading type
4541 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004542 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004543 if (EndLoc.isValid())
4544 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004545 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004546 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004547
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004548 // If the specifier combination wasn't legal, issue a diagnostic.
4549 if (isInvalid) {
4550 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004551 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004552 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004553 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004554 }
4555}
4556
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004557
4558/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4559///
4560void Parser::ParseDeclarator(Declarator &D) {
4561 /// This implements the 'declarator' production in the C grammar, then checks
4562 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004563 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004564}
4565
Richard Smith0efa75c2012-03-29 01:16:42 +00004566static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4567 if (Kind == tok::star || Kind == tok::caret)
4568 return true;
4569
4570 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4571 if (!Lang.CPlusPlus)
4572 return false;
4573
4574 return Kind == tok::amp || Kind == tok::ampamp;
4575}
4576
Sebastian Redlbd150f42008-11-21 19:14:01 +00004577/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4578/// is parsed by the function passed to it. Pass null, and the direct-declarator
4579/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004580/// ptr-operator production.
4581///
Richard Smith09f76ee2011-10-19 21:33:05 +00004582/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004583/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4584/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004585///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004586/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4587/// [C] pointer[opt] direct-declarator
4588/// [C++] direct-declarator
4589/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004590///
4591/// pointer: [C99 6.7.5]
4592/// '*' type-qualifier-list[opt]
4593/// '*' type-qualifier-list[opt] pointer
4594///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004595/// ptr-operator:
4596/// '*' cv-qualifier-seq[opt]
4597/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004598/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004599/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004600/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004601/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004602void Parser::ParseDeclaratorInternal(Declarator &D,
4603 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004604 if (Diags.hasAllExtensionsSilenced())
4605 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004606
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004607 // C++ member pointers start with a '::' or a nested-name.
4608 // Member pointers get special handling, since there's no place for the
4609 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004610 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004611 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4612 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004613 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4614 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004615 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004616 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004617
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004618 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004619 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004620 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004621 if (D.mayHaveIdentifier())
4622 D.getCXXScopeSpec() = SS;
4623 else
4624 AnnotateScopeToken(SS, true);
4625
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004626 if (DirectDeclParser)
4627 (this->*DirectDeclParser)(D);
4628 return;
4629 }
4630
4631 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004632 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004633 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004634 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004635 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004636
4637 // Recurse to parse whatever is left.
4638 ParseDeclaratorInternal(D, DirectDeclParser);
4639
4640 // Sema will have to catch (syntactically invalid) pointers into global
4641 // scope. It has to catch pointers into namespace scope anyway.
4642 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004643 Loc),
4644 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004645 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004646 return;
4647 }
4648 }
4649
4650 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004651 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004652 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004653 if (DirectDeclParser)
4654 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004655 return;
4656 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004657
Sebastian Redled0f3b02009-03-15 22:02:01 +00004658 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4659 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004660 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004661 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004662
Chris Lattner9eac9312009-03-27 04:18:06 +00004663 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004664 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004665 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004666
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004667 // FIXME: GNU attributes are not allowed here in a new-type-id.
Alp Toker62c5b572013-11-26 01:30:10 +00004668 ParseTypeQualifierListOpt(DS, true, true, true, !D.mayOmitIdentifier());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004669 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004670
Bill Wendling3708c182007-05-27 10:15:43 +00004671 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004672 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004673 if (Kind == tok::star)
4674 // Remember that we parsed a pointer type, and remember the type-quals.
4675 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004676 DS.getConstSpecLoc(),
4677 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004678 DS.getRestrictSpecLoc()),
4679 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004680 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004681 else
4682 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004683 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004684 Loc),
4685 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004686 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004687 } else {
4688 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004689 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004690
Sebastian Redl3b27be62009-03-23 00:00:23 +00004691 // Complain about rvalue references in C++03, but then go on and build
4692 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004693 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004694 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004695 diag::warn_cxx98_compat_rvalue_reference :
4696 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004697
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004698 // GNU-style and C++11 attributes are allowed here, as is restrict.
4699 ParseTypeQualifierListOpt(DS);
4700 D.ExtendWithDeclSpec(DS);
4701
Bill Wendling93efb222007-06-02 23:28:54 +00004702 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4703 // cv-qualifiers are introduced through the use of a typedef or of a
4704 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004705 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4706 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4707 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004708 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004709 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4710 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004711 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00004712 // 'restrict' is permitted as an extension.
4713 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4714 Diag(DS.getAtomicSpecLoc(),
4715 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00004716 }
Bill Wendling3708c182007-05-27 10:15:43 +00004717
4718 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004719 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004720
Douglas Gregor66583c52008-11-03 15:51:28 +00004721 if (D.getNumTypeObjects() > 0) {
4722 // C++ [dcl.ref]p4: There shall be no references to references.
4723 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4724 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004725 if (const IdentifierInfo *II = D.getIdentifier())
4726 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4727 << II;
4728 else
4729 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4730 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004731
Sebastian Redlbd150f42008-11-21 19:14:01 +00004732 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004733 // can go ahead and build the (technically ill-formed)
4734 // declarator: reference collapsing will take care of it.
4735 }
4736 }
4737
Richard Smith8e1ac332013-03-28 01:55:44 +00004738 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00004739 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004740 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004741 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004742 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004743 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004744}
4745
Richard Smith0efa75c2012-03-29 01:16:42 +00004746static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4747 SourceLocation EllipsisLoc) {
4748 if (EllipsisLoc.isValid()) {
4749 FixItHint Insertion;
4750 if (!D.getEllipsisLoc().isValid()) {
4751 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4752 D.setEllipsisLoc(EllipsisLoc);
4753 }
4754 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4755 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4756 }
4757}
4758
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004759/// ParseDirectDeclarator
4760/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004761/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004762/// '(' declarator ')'
4763/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004764/// [C90] direct-declarator '[' constant-expression[opt] ']'
4765/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4766/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4767/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4768/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004769/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4770/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004771/// direct-declarator '(' parameter-type-list ')'
4772/// direct-declarator '(' identifier-list[opt] ')'
4773/// [GNU] direct-declarator '(' parameter-forward-declarations
4774/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004775/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4776/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004777/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4778/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4779/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004780/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004781/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004782///
4783/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004784/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004785/// '::'[opt] nested-name-specifier[opt] type-name
4786///
4787/// id-expression: [C++ 5.1]
4788/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004789/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004790///
4791/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004792/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004793/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004794/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004795/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004796/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004797///
Richard Smith1453e312012-03-27 01:42:32 +00004798/// Note, any additional constructs added here may need corresponding changes
4799/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004800void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004801 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004802
David Blaikiebbafb8a2012-03-11 07:00:24 +00004803 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004804 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004805 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004806 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4807 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004808 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004809 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004810 }
4811
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004812 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004813 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004814 // Change the declaration context for name lookup, until this function
4815 // is exited (and the declarator has been parsed).
4816 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004817 }
4818
Douglas Gregor27b4c162010-12-23 22:44:42 +00004819 // C++0x [dcl.fct]p14:
4820 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004821 // of a parameter-declaration-clause without a preceding comma. In
4822 // this case, the ellipsis is parsed as part of the
4823 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004824 // parameter pack that has not been expanded; otherwise, it is parsed
4825 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004826 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004827 !((D.getContext() == Declarator::PrototypeContext ||
Faisal Vali2b391ab2013-09-26 19:54:12 +00004828 D.getContext() == Declarator::LambdaExprParameterContext ||
Douglas Gregor27b4c162010-12-23 22:44:42 +00004829 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004830 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00004831 !D.hasGroupingParens() &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004832 !Actions.containsUnexpandedParameterPacks(D))) {
4833 SourceLocation EllipsisLoc = ConsumeToken();
4834 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4835 // The ellipsis was put in the wrong place. Recover, and explain to
4836 // the user what they should have done.
4837 ParseDeclarator(D);
4838 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4839 return;
4840 } else
4841 D.setEllipsisLoc(EllipsisLoc);
4842
4843 // The ellipsis can't be followed by a parenthesized declarator. We
4844 // check for that in ParseParenDeclarator, after we have disambiguated
4845 // the l_paren token.
4846 }
4847
Douglas Gregor7861a802009-11-03 01:35:08 +00004848 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4849 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4850 // We found something that indicates the start of an unqualified-id.
4851 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004852 bool AllowConstructorName;
4853 if (D.getDeclSpec().hasTypeSpecifier())
4854 AllowConstructorName = false;
4855 else if (D.getCXXScopeSpec().isSet())
4856 AllowConstructorName =
4857 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004858 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004859 else
4860 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4861
Abramo Bagnara7945c982012-01-27 09:46:47 +00004862 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004863 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4864 /*EnteringContext=*/true,
4865 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004866 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004867 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004868 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004869 D.getName()) ||
4870 // Once we're past the identifier, if the scope was bad, mark the
4871 // whole declarator bad.
4872 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004873 D.SetIdentifier(0, Tok.getLocation());
4874 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004875 } else {
4876 // Parsed the unqualified-id; update range information and move along.
4877 if (D.getSourceRange().getBegin().isInvalid())
4878 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4879 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004880 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004881 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004882 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004883 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004884 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004885 "There's a C++-specific check for tok::identifier above");
4886 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4887 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4888 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004889 goto PastIdentifier;
Richard Smith9ce302e2013-07-11 05:10:21 +00004890 } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
Richard Smithf39720b2013-10-13 22:12:28 +00004891 // A virt-specifier isn't treated as an identifier if it appears after a
4892 // trailing-return-type.
4893 if (D.getContext() != Declarator::TrailingReturnContext ||
4894 !isCXX11VirtSpecifier(Tok)) {
4895 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
4896 << FixItHint::CreateRemoval(Tok.getLocation());
4897 D.SetIdentifier(0, Tok.getLocation());
4898 ConsumeToken();
4899 goto PastIdentifier;
4900 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004901 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004902
Douglas Gregor7861a802009-11-03 01:35:08 +00004903 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004904 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004905 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004906 // Example: 'char (*X)' or 'int (*XX)(void)'
4907 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004908
4909 // If the declarator was parenthesized, we entered the declarator
4910 // scope when parsing the parenthesized declarator, then exited
4911 // the scope already. Re-enter the scope, if we need to.
4912 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004913 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004914 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004915 if (!D.isInvalidType() &&
4916 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004917 // Change the declaration context for name lookup, until this function
4918 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004919 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004920 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004921 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004922 // This could be something simple like "int" (in which case the declarator
4923 // portion is empty), if an abstract-declarator is allowed.
4924 D.SetIdentifier(0, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00004925
4926 // The grammar for abstract-pack-declarator does not allow grouping parens.
4927 // FIXME: Revisit this once core issue 1488 is resolved.
4928 if (D.hasEllipsis() && D.hasGroupingParens())
4929 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4930 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00004931 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004932 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004933 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004934 if (D.getContext() == Declarator::MemberContext)
4935 Diag(Tok, diag::err_expected_member_name_or_semi)
4936 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004937 else if (getLangOpts().CPlusPlus) {
4938 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4939 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
Richard Trieu2f586962013-09-05 02:31:33 +00004940 else {
4941 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
4942 if (Tok.isAtStartOfLine() && Loc.isValid())
4943 Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
4944 << getLangOpts().CPlusPlus;
4945 else
4946 Diag(Tok, diag::err_expected_unqualified_id)
4947 << getLangOpts().CPlusPlus;
4948 }
Richard Trieu9c672672013-01-26 02:31:38 +00004949 } else
Alp Tokerec543272013-12-24 09:48:30 +00004950 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_paren;
Chris Lattnereec40f92006-08-06 21:55:29 +00004951 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004952 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004953 }
Mike Stump11289f42009-09-09 15:08:12 +00004954
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004955 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004956 assert(D.isPastIdentifier() &&
4957 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004958
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004959 // Don't parse attributes unless we have parsed an unparenthesized name.
4960 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004961 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004962
Chris Lattneracd58a32006-08-06 17:24:14 +00004963 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004964 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004965 // Enter function-declaration scope, limiting any declarators to the
4966 // function prototype scope, including parameter declarators.
4967 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004968 Scope::FunctionPrototypeScope|Scope::DeclScope|
4969 (D.isFunctionDeclaratorAFunctionDeclaration()
4970 ? Scope::FunctionDeclarationScope : 0));
4971
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004972 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4973 // In such a case, check if we actually have a function declarator; if it
4974 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004975 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004976 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4977 // The name of the declarator, if any, is tentatively declared within
4978 // a possible direct initializer.
4979 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4980 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4981 TentativelyDeclaredIdentifiers.pop_back();
4982 if (!IsFunctionDecl)
4983 break;
4984 }
John McCall084e83d2011-03-24 11:26:52 +00004985 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004986 BalancedDelimiterTracker T(*this, tok::l_paren);
4987 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004988 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004989 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004990 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004991 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004992 } else {
4993 break;
4994 }
4995 }
Chad Rosierc1183952012-06-26 22:30:43 +00004996}
Chris Lattneracd58a32006-08-06 17:24:14 +00004997
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004998/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4999/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00005000/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005001/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
5002///
5003/// direct-declarator:
5004/// '(' declarator ')'
5005/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005006/// direct-declarator '(' parameter-type-list ')'
5007/// direct-declarator '(' identifier-list[opt] ')'
5008/// [GNU] direct-declarator '(' parameter-forward-declarations
5009/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005010///
5011void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005012 BalancedDelimiterTracker T(*this, tok::l_paren);
5013 T.consumeOpen();
5014
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005015 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00005016
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005017 // Eat any attributes before we look at whether this is a grouping or function
5018 // declarator paren. If this is a grouping paren, the attribute applies to
5019 // the type being built up, for example:
5020 // int (__attribute__(()) *x)(long y)
5021 // If this ends up not being a grouping paren, the attribute applies to the
5022 // first argument, for example:
5023 // int (__attribute__(()) int x)
5024 // In either case, we need to eat any attributes to be able to determine what
5025 // sort of paren this is.
5026 //
John McCall084e83d2011-03-24 11:26:52 +00005027 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005028 bool RequiresArg = false;
5029 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00005030 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005031
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005032 // We require that the argument list (if this is a non-grouping paren) be
5033 // present even if the attribute list was empty.
5034 RequiresArg = true;
5035 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00005036
Steve Naroff44ac7772008-12-25 14:16:32 +00005037 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00005038 ParseMicrosoftTypeAttributes(attrs);
5039
Dawn Perchik335e16b2010-09-03 01:29:35 +00005040 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00005041 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00005042 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005043
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005044 // If we haven't past the identifier yet (or where the identifier would be
5045 // stored, if this is an abstract declarator), then this is probably just
5046 // grouping parens. However, if this could be an abstract-declarator, then
5047 // this could also be the start of function arguments (consider 'void()').
5048 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00005049
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005050 if (!D.mayOmitIdentifier()) {
5051 // If this can't be an abstract-declarator, this *must* be a grouping
5052 // paren, because we haven't seen the identifier yet.
5053 isGrouping = true;
5054 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00005055 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
5056 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00005057 isDeclarationSpecifier() || // 'int(int)' is a function.
5058 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005059 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
5060 // considered to be a type, not a K&R identifier-list.
5061 isGrouping = false;
5062 } else {
5063 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
5064 isGrouping = true;
5065 }
Mike Stump11289f42009-09-09 15:08:12 +00005066
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005067 // If this is a grouping paren, handle:
5068 // direct-declarator: '(' declarator ')'
5069 // direct-declarator: '(' attributes declarator ')'
5070 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00005071 SourceLocation EllipsisLoc = D.getEllipsisLoc();
5072 D.setEllipsisLoc(SourceLocation());
5073
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005074 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00005075 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00005076 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005077 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005078 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00005079 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005080 T.getCloseLocation()),
5081 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005082
5083 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00005084
5085 // An ellipsis cannot be placed outside parentheses.
5086 if (EllipsisLoc.isValid())
5087 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
5088
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005089 return;
5090 }
Mike Stump11289f42009-09-09 15:08:12 +00005091
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005092 // Okay, if this wasn't a grouping paren, it must be the start of a function
5093 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005094 // identifier (and remember where it would have been), then call into
5095 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005096 D.SetIdentifier(0, Tok.getLocation());
5097
David Blaikie15a430a2011-12-04 05:04:18 +00005098 // Enter function-declaration scope, limiting any declarators to the
5099 // function prototype scope, including parameter declarators.
5100 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00005101 Scope::FunctionPrototypeScope | Scope::DeclScope |
5102 (D.isFunctionDeclaratorAFunctionDeclaration()
5103 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00005104 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00005105 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005106}
5107
5108/// ParseFunctionDeclarator - We are after the identifier and have parsed the
5109/// declarator D up to a paren, which indicates that we are parsing function
5110/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00005111///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005112/// If FirstArgAttrs is non-null, then the caller parsed those arguments
5113/// immediately after the open paren - they should be considered to be the
5114/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005115///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005116/// If RequiresArg is true, then the first argument of the function is required
5117/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005118///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005119/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
5120/// (C++11) ref-qualifier[opt], exception-specification[opt],
5121/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
5122///
5123/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00005124/// dynamic-exception-specification
5125/// noexcept-specification
5126///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005127void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005128 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005129 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00005130 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005131 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00005132 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00005133 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00005134 // lparen is already consumed!
5135 assert(D.isPastIdentifier() && "Should not call before identifier!");
5136
5137 // This should be true when the function has typed arguments.
5138 // Otherwise, it is treated as a K&R-style function.
5139 bool HasProto = false;
5140 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005141 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005142 // Remember where we see an ellipsis, if any.
5143 SourceLocation EllipsisLoc;
5144
5145 DeclSpec DS(AttrFactory);
5146 bool RefQualifierIsLValueRef = true;
5147 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00005148 SourceLocation ConstQualifierLoc;
5149 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005150 ExceptionSpecificationType ESpecType = EST_None;
5151 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005152 SmallVector<ParsedType, 2> DynamicExceptions;
5153 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005154 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005155 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00005156 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005157
James Molloy6f8780b2012-02-29 10:24:19 +00005158 Actions.ActOnStartFunctionDeclarator();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005159 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
5160 EndLoc is the end location for the function declarator.
5161 They differ for trailing return types. */
5162 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005163 SourceLocation LParenLoc, RParenLoc;
5164 LParenLoc = Tracker.getOpenLocation();
5165 StartLoc = LParenLoc;
5166
Douglas Gregor9e66af42011-07-05 16:44:18 +00005167 if (isFunctionDeclaratorIdentifierList()) {
5168 if (RequiresArg)
5169 Diag(Tok, diag::err_argument_required_after_attribute);
5170
5171 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5172
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005173 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005174 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005175 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005176 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005177 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005178 if (Tok.isNot(tok::r_paren))
Faisal Vali2b391ab2013-09-26 19:54:12 +00005179 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
5180 EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005181 else if (RequiresArg)
5182 Diag(Tok, diag::err_argument_required_after_attribute);
5183
David Blaikiebbafb8a2012-03-11 07:00:24 +00005184 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005185
5186 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005187 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005188 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005189 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005190 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005191
David Blaikiebbafb8a2012-03-11 07:00:24 +00005192 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005193 // FIXME: Accept these components in any order, and produce fixits to
5194 // correct the order if the user gets it wrong. Ideally we should deal
5195 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005196
5197 // Parse cv-qualifier-seq[opt].
Richard Smith8e1ac332013-03-28 01:55:44 +00005198 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
5199 /*CXX11AttributesAllowed*/ false,
5200 /*AtomicAllowed*/ false);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005201 if (!DS.getSourceRange().getEnd().isInvalid()) {
5202 EndLoc = DS.getSourceRange().getEnd();
5203 ConstQualifierLoc = DS.getConstSpecLoc();
5204 VolatileQualifierLoc = DS.getVolatileSpecLoc();
5205 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005206
5207 // Parse ref-qualifier[opt].
5208 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005209 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005210 diag::warn_cxx98_compat_ref_qualifier :
5211 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005212
Douglas Gregor9e66af42011-07-05 16:44:18 +00005213 RefQualifierIsLValueRef = Tok.is(tok::amp);
5214 RefQualifierLoc = ConsumeToken();
5215 EndLoc = RefQualifierLoc;
5216 }
5217
Douglas Gregor3024f072012-04-16 07:05:22 +00005218 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00005219 // If a declaration declares a member function or member function
5220 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00005221 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00005222 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00005223 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00005224 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00005225 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005226 getLangOpts().CPlusPlus11 &&
Richard Smithad1bbb92013-03-15 00:41:52 +00005227 (D.getContext() == Declarator::MemberContext
5228 ? !D.getDeclSpec().isFriendSpecified()
5229 : D.getContext() == Declarator::FileContext &&
5230 D.getCXXScopeSpec().isValid() &&
5231 Actions.CurContext->isRecord());
Douglas Gregor3024f072012-04-16 07:05:22 +00005232 Sema::CXXThisScopeRAII ThisScope(Actions,
5233 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00005234 DS.getTypeQualifiers() |
Richard Smith034185c2013-04-21 01:08:50 +00005235 (D.getDeclSpec().isConstexprSpecified() &&
5236 !getLangOpts().CPlusPlus1y
Richard Smith01141a92013-01-14 01:55:13 +00005237 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00005238 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00005239
Douglas Gregor9e66af42011-07-05 16:44:18 +00005240 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00005241 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00005242 DynamicExceptions,
5243 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00005244 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005245 if (ESpecType != EST_None)
5246 EndLoc = ESpecRange.getEnd();
5247
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005248 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5249 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00005250 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005251
Douglas Gregor9e66af42011-07-05 16:44:18 +00005252 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005253 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005254 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00005255 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005256 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5257 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005258 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00005259 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00005260 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005261 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005262 }
5263 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005264 }
5265
5266 // Remember that we parsed a function type, and remember the attributes.
5267 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005268 IsAmbiguous,
5269 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005270 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005271 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005272 DS.getTypeQualifiers(),
5273 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00005274 RefQualifierLoc, ConstQualifierLoc,
5275 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00005276 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00005277 ESpecType, ESpecRange.getBegin(),
5278 DynamicExceptions.data(),
5279 DynamicExceptionRanges.data(),
5280 DynamicExceptions.size(),
5281 NoexceptExpr.isUsable() ?
5282 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005283 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005284 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005285 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00005286
5287 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005288}
5289
5290/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5291/// identifier list form for a K&R-style function: void foo(a,b,c)
5292///
5293/// Note that identifier-lists are only allowed for normal declarators, not for
5294/// abstract-declarators.
5295bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005296 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00005297 && Tok.is(tok::identifier)
5298 && !TryAltiVecVectorToken()
5299 // K&R identifier lists can't have typedefs as identifiers, per C99
5300 // 6.7.5.3p11.
5301 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5302 // Identifier lists follow a really simple grammar: the identifiers can
5303 // be followed *only* by a ", identifier" or ")". However, K&R
5304 // identifier lists are really rare in the brave new modern world, and
5305 // it is very common for someone to typo a type in a non-K&R style
5306 // list. If we are presented with something like: "void foo(intptr x,
5307 // float y)", we don't want to start parsing the function declarator as
5308 // though it is a K&R style declarator just because intptr is an
5309 // invalid type.
5310 //
5311 // To handle this, we check to see if the token after the first
5312 // identifier is a "," or ")". Only then do we parse it as an
5313 // identifier list.
5314 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5315}
5316
5317/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5318/// we found a K&R-style identifier list instead of a typed parameter list.
5319///
5320/// After returning, ParamInfo will hold the parsed parameters.
5321///
5322/// identifier-list: [C99 6.7.5]
5323/// identifier
5324/// identifier-list ',' identifier
5325///
5326void Parser::ParseFunctionDeclaratorIdentifierList(
5327 Declarator &D,
Craig Topper5603df42013-07-05 19:34:19 +00005328 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005329 // If there was no identifier specified for the declarator, either we are in
5330 // an abstract-declarator, or we are in a parameter declarator which was found
5331 // to be abstract. In abstract-declarators, identifier lists are not valid:
5332 // diagnose this.
5333 if (!D.getIdentifier())
5334 Diag(Tok, diag::ext_ident_list_in_param);
5335
5336 // Maintain an efficient lookup of params we have seen so far.
5337 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5338
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005339 do {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005340 // If this isn't an identifier, report the error and skip until ')'.
5341 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00005342 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00005343 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005344 // Forget we parsed anything.
5345 ParamInfo.clear();
5346 return;
5347 }
5348
5349 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5350
5351 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5352 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5353 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5354
5355 // Verify that the argument identifier has not already been mentioned.
5356 if (!ParamsSoFar.insert(ParmII)) {
5357 Diag(Tok, diag::err_param_redefinition) << ParmII;
5358 } else {
5359 // Remember this identifier in ParamInfo.
5360 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5361 Tok.getLocation(),
5362 0));
5363 }
5364
5365 // Eat the identifier.
5366 ConsumeToken();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005367 // The list continues if we see a comma.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005368 } while (TryConsumeToken(tok::comma));
Douglas Gregor9e66af42011-07-05 16:44:18 +00005369}
5370
5371/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5372/// after the opening parenthesis. This function will not parse a K&R-style
5373/// identifier list.
5374///
Richard Smith2620cd92012-04-11 04:01:28 +00005375/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5376/// caller parsed those arguments immediately after the open paren - they should
5377/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005378///
5379/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5380/// be the location of the ellipsis, if any was parsed.
5381///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005382/// parameter-type-list: [C99 6.7.5]
5383/// parameter-list
5384/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005385/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005386///
5387/// parameter-list: [C99 6.7.5]
5388/// parameter-declaration
5389/// parameter-list ',' parameter-declaration
5390///
5391/// parameter-declaration: [C99 6.7.5]
5392/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005393/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00005394/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00005395/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00005396/// declaration-specifiers abstract-declarator[opt]
5397/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00005398/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00005399/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00005400/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005401///
Douglas Gregor9e66af42011-07-05 16:44:18 +00005402void Parser::ParseParameterDeclarationClause(
5403 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00005404 ParsedAttributes &FirstArgAttrs,
Craig Topper5603df42013-07-05 19:34:19 +00005405 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005406 SourceLocation &EllipsisLoc) {
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005407 do {
5408 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5409 // before deciding this was a parameter-declaration-clause.
5410 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
Chris Lattner371ed4e2008-04-06 06:57:35 +00005411 break;
Mike Stump11289f42009-09-09 15:08:12 +00005412
Chris Lattner371ed4e2008-04-06 06:57:35 +00005413 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00005414 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00005415 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005416
Richard Smith2620cd92012-04-11 04:01:28 +00005417 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00005418 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00005419
John McCall53fa7142010-12-24 02:08:15 +00005420 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00005421 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00005422
5423 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005424
5425 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00005426 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005427 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00005428 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5429 // too much hassle.
5430 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00005431
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005432 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005433
Faisal Vali2b391ab2013-09-26 19:54:12 +00005434
5435 // Parse the declarator. This is "PrototypeContext" or
5436 // "LambdaExprParameterContext", because we must accept either
5437 // 'declarator' or 'abstract-declarator' here.
5438 Declarator ParmDeclarator(DS,
5439 D.getContext() == Declarator::LambdaExprContext ?
5440 Declarator::LambdaExprParameterContext :
5441 Declarator::PrototypeContext);
5442 ParseDeclarator(ParmDeclarator);
Chris Lattner371ed4e2008-04-06 06:57:35 +00005443
5444 // Parse GNU attributes, if present.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005445 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump11289f42009-09-09 15:08:12 +00005446
Chris Lattner371ed4e2008-04-06 06:57:35 +00005447 // Remember this parsed parameter in ParamInfo.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005448 IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005449
Douglas Gregor4d87df52008-12-16 21:30:33 +00005450 // DefArgToks is used when the parsing of default arguments needs
5451 // to be delayed.
5452 CachedTokens *DefArgToks = 0;
5453
Chris Lattner371ed4e2008-04-06 06:57:35 +00005454 // If no parameter was specified, verify that *something* was specified,
5455 // otherwise we have a missing type and identifier.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005456 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 &&
5457 ParmDeclarator.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005458 // Completely missing, emit error.
5459 Diag(DSStart, diag::err_missing_param);
5460 } else {
5461 // Otherwise, we have something. Add it and let semantic analysis try
5462 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005463
Chris Lattner371ed4e2008-04-06 06:57:35 +00005464 // Inform the actions module about the parameter declarator, so it gets
5465 // added to the current scope.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005466 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(),
5467 ParmDeclarator);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005468 // Parse the default argument, if any. We parse the default
5469 // arguments in all dialects; the semantic analysis in
5470 // ActOnParamDefaultArgument will reject the default argument in
5471 // C.
5472 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005473 SourceLocation EqualLoc = Tok.getLocation();
5474
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005475 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005476 if (D.getContext() == Declarator::MemberContext) {
5477 // If we're inside a class definition, cache the tokens
5478 // corresponding to the default argument. We'll actually parse
5479 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005480 // FIXME: Can we use a smart pointer for Toks?
5481 DefArgToks = new CachedTokens;
5482
Richard Smith1fff95c2013-09-12 23:28:08 +00005483 if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005484 delete DefArgToks;
5485 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005486 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005487 } else {
5488 // Mark the end of the default argument so that we know when to
5489 // stop when we parse it later on.
5490 Token DefArgEnd;
5491 DefArgEnd.startToken();
5492 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5493 DefArgEnd.setLocation(Tok.getLocation());
5494 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005495 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005496 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005497 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005498 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005499 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005500 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005501
Chad Rosierc1183952012-06-26 22:30:43 +00005502 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005503 // used.
5504 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005505 Sema::PotentiallyEvaluatedIfUsed,
5506 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005507
Sebastian Redldb63af22012-03-14 15:54:00 +00005508 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005509 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005510 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005511 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005512 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005513 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005514 if (DefArgResult.isInvalid()) {
5515 Actions.ActOnParamDefaultArgumentError(Param);
Alexey Bataevee6507d2013-11-18 08:17:37 +00005516 SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor4d87df52008-12-16 21:30:33 +00005517 } else {
5518 // Inform the actions module about the default argument
5519 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005520 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005521 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005522 }
5523 }
Mike Stump11289f42009-09-09 15:08:12 +00005524
5525 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Faisal Vali2b391ab2013-09-26 19:54:12 +00005526 ParmDeclarator.getIdentifierLoc(),
5527 Param, DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005528 }
5529
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005530 if (TryConsumeToken(tok::ellipsis, EllipsisLoc) &&
5531 !getLangOpts().CPlusPlus) {
5532 // We have ellipsis without a preceding ',', which is ill-formed
5533 // in C. Complain and provide the fix.
5534 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
5535 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005536 break;
5537 }
Mike Stump11289f42009-09-09 15:08:12 +00005538
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005539 // If the next token is a comma, consume it and keep reading arguments.
5540 } while (TryConsumeToken(tok::comma));
Chris Lattner6c940e62008-04-06 06:34:08 +00005541}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005542
Chris Lattnere8074e62006-08-06 18:30:15 +00005543/// [C90] direct-declarator '[' constant-expression[opt] ']'
5544/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5545/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5546/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5547/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005548/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5549/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005550void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005551 if (CheckProhibitedCXX11Attribute())
5552 return;
5553
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005554 BalancedDelimiterTracker T(*this, tok::l_square);
5555 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005556
Chris Lattner84a11622008-12-18 07:27:21 +00005557 // C array syntax has many features, but by-far the most common is [] and [4].
5558 // This code does a fast path to handle some of the most obvious cases.
5559 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005560 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005561 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005562 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005563
Chris Lattner84a11622008-12-18 07:27:21 +00005564 // Remember that we parsed the empty array type.
John McCall084e83d2011-03-24 11:26:52 +00005565 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005566 T.getOpenLocation(),
5567 T.getCloseLocation()),
5568 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005569 return;
5570 } else if (Tok.getKind() == tok::numeric_constant &&
5571 GetLookAheadToken(1).is(tok::r_square)) {
5572 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005573 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005574 ConsumeToken();
5575
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005576 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005577 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005578 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005579
Chris Lattner84a11622008-12-18 07:27:21 +00005580 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005581 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005582 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005583 T.getOpenLocation(),
5584 T.getCloseLocation()),
5585 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005586 return;
5587 }
Mike Stump11289f42009-09-09 15:08:12 +00005588
Chris Lattnere8074e62006-08-06 18:30:15 +00005589 // If valid, this location is the position where we read the 'static' keyword.
5590 SourceLocation StaticLoc;
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005591 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00005592
Chris Lattnere8074e62006-08-06 18:30:15 +00005593 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005594 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005595 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005596 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005597
Chris Lattnere8074e62006-08-06 18:30:15 +00005598 // If we haven't already read 'static', check to see if there is one after the
5599 // type-qualifier-list.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005600 if (!StaticLoc.isValid())
5601 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00005602
Chris Lattnere8074e62006-08-06 18:30:15 +00005603 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005604 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005605 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005606
Chris Lattner521ff2b2008-04-06 05:26:30 +00005607 // Handle the case where we have '[*]' as the array size. However, a leading
5608 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005609 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005610 // infrequent, use of lookahead is not costly here.
5611 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005612 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005613
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005614 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005615 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005616 StaticLoc = SourceLocation(); // Drop the static.
5617 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005618 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005619 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005620 // Note, in C89, this production uses the constant-expr production instead
5621 // of assignment-expr. The only difference is that assignment-expr allows
5622 // things like '=' and '*='. Sema rejects these in C89 mode because they
5623 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005624
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005625 // Parse the constant-expression or assignment-expression now (depending
5626 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005627 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005628 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005629 } else {
5630 EnterExpressionEvaluationContext Unevaluated(Actions,
5631 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005632 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005633 }
Chris Lattner62591722006-08-12 18:40:58 +00005634 }
Mike Stump11289f42009-09-09 15:08:12 +00005635
Chris Lattner62591722006-08-12 18:40:58 +00005636 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005637 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005638 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005639 // If the expression was invalid, skip it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00005640 SkipUntil(tok::r_square, StopAtSemi);
Chris Lattner62591722006-08-12 18:40:58 +00005641 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005642 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005643
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005644 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005645
John McCall084e83d2011-03-24 11:26:52 +00005646 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005647 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005648
Chris Lattner84a11622008-12-18 07:27:21 +00005649 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005650 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005651 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005652 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005653 T.getOpenLocation(),
5654 T.getCloseLocation()),
5655 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005656}
5657
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005658/// [GNU] typeof-specifier:
5659/// typeof ( expressions )
5660/// typeof ( type-name )
5661/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005662///
5663void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005664 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005665 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005666 SourceLocation StartLoc = ConsumeToken();
5667
John McCalle8595032010-01-13 20:03:27 +00005668 const bool hasParens = Tok.is(tok::l_paren);
5669
Eli Friedman15681d62012-09-26 04:34:21 +00005670 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5671 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005672
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005673 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005674 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005675 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005676 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5677 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005678 if (hasParens)
5679 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005680
5681 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005682 // FIXME: Not accurate, the range gets one token more than it should.
5683 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005684 else
5685 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005686
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005687 if (isCastExpr) {
5688 if (!CastTy) {
5689 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005690 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005691 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005692
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005693 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005694 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005695 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5696 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005697 DiagID, CastTy))
5698 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005699 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005700 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005701
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005702 // If we get here, the operand to the typeof was an expresion.
5703 if (Operand.isInvalid()) {
5704 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005705 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005706 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005707
Eli Friedmane0afc982012-01-21 01:01:51 +00005708 // We might need to transform the operand if it is potentially evaluated.
5709 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5710 if (Operand.isInvalid()) {
5711 DS.SetTypeSpecError();
5712 return;
5713 }
5714
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005715 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005716 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005717 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5718 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005719 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005720 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005721}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005722
Benjamin Kramere56f3932011-12-23 17:00:35 +00005723/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005724/// _Atomic ( type-name )
5725///
5726void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00005727 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5728 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00005729
5730 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005731 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00005732 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005733 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00005734
5735 TypeResult Result = ParseTypeName();
5736 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00005737 SkipUntil(tok::r_paren, StopAtSemi);
Eli Friedman0dfb8892011-10-06 23:00:33 +00005738 return;
5739 }
5740
5741 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005742 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005743
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005744 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005745 return;
5746
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005747 DS.setTypeofParensRange(T.getRange());
5748 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005749
5750 const char *PrevSpec = 0;
5751 unsigned DiagID;
5752 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5753 DiagID, Result.release()))
5754 Diag(StartLoc, DiagID) << PrevSpec;
5755}
5756
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005757
5758/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5759/// from TryAltiVecVectorToken.
5760bool Parser::TryAltiVecVectorTokenOutOfLine() {
5761 Token Next = NextToken();
5762 switch (Next.getKind()) {
5763 default: return false;
5764 case tok::kw_short:
5765 case tok::kw_long:
5766 case tok::kw_signed:
5767 case tok::kw_unsigned:
5768 case tok::kw_void:
5769 case tok::kw_char:
5770 case tok::kw_int:
5771 case tok::kw_float:
5772 case tok::kw_double:
5773 case tok::kw_bool:
5774 case tok::kw___pixel:
5775 Tok.setKind(tok::kw___vector);
5776 return true;
5777 case tok::identifier:
5778 if (Next.getIdentifierInfo() == Ident_pixel) {
5779 Tok.setKind(tok::kw___vector);
5780 return true;
5781 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005782 if (Next.getIdentifierInfo() == Ident_bool) {
5783 Tok.setKind(tok::kw___vector);
5784 return true;
5785 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005786 return false;
5787 }
5788}
5789
5790bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5791 const char *&PrevSpec, unsigned &DiagID,
5792 bool &isInvalid) {
5793 if (Tok.getIdentifierInfo() == Ident_vector) {
5794 Token Next = NextToken();
5795 switch (Next.getKind()) {
5796 case tok::kw_short:
5797 case tok::kw_long:
5798 case tok::kw_signed:
5799 case tok::kw_unsigned:
5800 case tok::kw_void:
5801 case tok::kw_char:
5802 case tok::kw_int:
5803 case tok::kw_float:
5804 case tok::kw_double:
5805 case tok::kw_bool:
5806 case tok::kw___pixel:
5807 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5808 return true;
5809 case tok::identifier:
5810 if (Next.getIdentifierInfo() == Ident_pixel) {
5811 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5812 return true;
5813 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005814 if (Next.getIdentifierInfo() == Ident_bool) {
5815 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5816 return true;
5817 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005818 break;
5819 default:
5820 break;
5821 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005822 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005823 DS.isTypeAltiVecVector()) {
5824 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5825 return true;
Bill Schmidt99a084b2013-07-03 20:54:09 +00005826 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5827 DS.isTypeAltiVecVector()) {
5828 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID);
5829 return true;
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005830 }
5831 return false;
5832}