blob: 27df0c0795d4b2377d75ad44fcd41d8dc4a2888a [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 Smith649c7b062014-01-08 00:56:48 +00002059 if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00002060 Diag(Tok, diag::err_expected_type);
2061 DS.SetTypeSpecError();
2062 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
2063 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00002064 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00002065 if (!DS.hasTypeSpecifier())
2066 DS.SetTypeSpecError();
2067 }
Mike Stump11289f42009-09-09 15:08:12 +00002068
Chris Lattner1b22eed2006-11-28 05:12:07 +00002069 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002070 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00002071 if (DS.getStorageClassSpecLoc().isValid())
2072 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
2073 else
Richard Smithb4a9e862013-04-12 22:46:28 +00002074 Diag(DS.getThreadStorageClassSpecLoc(),
2075 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00002076 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002077 }
Mike Stump11289f42009-09-09 15:08:12 +00002078
Chris Lattner1b22eed2006-11-28 05:12:07 +00002079 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002080 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00002081 if (DS.isInlineSpecified())
2082 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
2083 if (DS.isVirtualSpecified())
2084 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
2085 if (DS.isExplicitSpecified())
2086 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00002087 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002088 }
Richard Smithc5b05522012-03-12 07:56:15 +00002089
2090 // Issue diagnostic and remove constexpr specfier if present.
2091 if (DS.isConstexprSpecified()) {
2092 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
2093 DS.ClearConstexprSpec();
2094 }
Chris Lattner1890ac82006-08-13 01:16:23 +00002095}
Chris Lattner53361ac2006-08-10 05:19:57 +00002096
Chris Lattner6cc055a2009-04-12 20:42:31 +00002097/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2098/// specified token is valid after the identifier in a declarator which
2099/// immediately follows the declspec. For example, these things are valid:
2100///
2101/// int x [ 4]; // direct-declarator
2102/// int x ( int y); // direct-declarator
2103/// int(int x ) // direct-declarator
2104/// int x ; // simple-declaration
2105/// int x = 17; // init-declarator-list
2106/// int x , y; // init-declarator-list
2107/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002108/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002109/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002110///
2111/// This is not, because 'x' does not immediately follow the declspec (though
2112/// ')' happens to be valid anyway).
2113/// int (x)
2114///
2115static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2116 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2117 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00002118 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002119}
2120
Chris Lattner20a0c612009-04-14 21:34:55 +00002121
2122/// ParseImplicitInt - This method is called when we have an non-typename
2123/// identifier in a declspec (which normally terminates the decl spec) when
2124/// the declspec has no type specifier. In this case, the declspec is either
2125/// malformed or is "implicit int" (in K&R and C89).
2126///
2127/// This method handles diagnosing this prettily and returns false if the
2128/// declspec is done being processed. If it recovers and thinks there may be
2129/// other pieces of declspec after it, it returns true.
2130///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002131bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002132 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00002133 AccessSpecifier AS, DeclSpecContext DSC,
2134 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002135 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002136
Chris Lattner20a0c612009-04-14 21:34:55 +00002137 SourceLocation Loc = Tok.getLocation();
2138 // If we see an identifier that is not a type name, we normally would
2139 // parse it as the identifer being declared. However, when a typename
2140 // is typo'd or the definition is not included, this will incorrectly
2141 // parse the typename as the identifier name and fall over misparsing
2142 // later parts of the diagnostic.
2143 //
2144 // As such, we try to do some look-ahead in cases where this would
2145 // otherwise be an "implicit-int" case to see if this is invalid. For
2146 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2147 // an identifier with implicit int, we'd get a parse error because the
2148 // next token is obviously invalid for a type. Parse these as a case
2149 // with an invalid type specifier.
2150 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002151
Chris Lattner20a0c612009-04-14 21:34:55 +00002152 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002153 // error, do lookahead to try to do better recovery. This never applies
2154 // within a type specifier. Outside of C++, we allow this even if the
2155 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002156 // implicit int as an extension in C99 and C11.
Richard Smith649c7b062014-01-08 00:56:48 +00002157 if (!isTypeSpecifier(DSC) && !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002158 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002159 // If this token is valid for implicit int, e.g. "static x = 4", then
2160 // we just avoid eating the identifier, so it will be parsed as the
2161 // identifier in the declarator.
2162 return false;
2163 }
Mike Stump11289f42009-09-09 15:08:12 +00002164
Richard Smitha952ebb2012-05-15 21:01:51 +00002165 if (getLangOpts().CPlusPlus &&
2166 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2167 // Don't require a type specifier if we have the 'auto' storage class
2168 // specifier in C++98 -- we'll promote it to a type specifier.
Richard Smithfb8b7b92013-10-15 00:00:26 +00002169 if (SS)
2170 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
Richard Smitha952ebb2012-05-15 21:01:51 +00002171 return false;
2172 }
2173
Chris Lattner20a0c612009-04-14 21:34:55 +00002174 // Otherwise, if we don't consume this token, we are going to emit an
2175 // error anyway. Try to recover from various common problems. Check
2176 // to see if this was a reference to a tag name without a tag specified.
2177 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002178 //
2179 // C++ doesn't need this, and isTagName doesn't take SS.
2180 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002181 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002182 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002183
Douglas Gregor0be31a22010-07-02 17:43:08 +00002184 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002185 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002186 case DeclSpec::TST_enum:
2187 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2188 case DeclSpec::TST_union:
2189 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2190 case DeclSpec::TST_struct:
2191 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002192 case DeclSpec::TST_interface:
2193 TagName="__interface"; FixitTagName = "__interface ";
2194 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002195 case DeclSpec::TST_class:
2196 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002197 }
Mike Stump11289f42009-09-09 15:08:12 +00002198
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002199 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002200 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2201 LookupResult R(Actions, TokenName, SourceLocation(),
2202 Sema::LookupOrdinaryName);
2203
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002204 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002205 << TokenName << TagName << getLangOpts().CPlusPlus
2206 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2207
2208 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2209 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2210 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002211 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002212 << TokenName << TagName;
2213 }
Mike Stump11289f42009-09-09 15:08:12 +00002214
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002215 // Parse this as a tag as if the missing tag were present.
2216 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00002217 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002218 else
Richard Smithc5b05522012-03-12 07:56:15 +00002219 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00002220 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002221 return true;
2222 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002223 }
Mike Stump11289f42009-09-09 15:08:12 +00002224
Richard Smithfe904f02012-05-15 21:29:55 +00002225 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002226 // being declared (with a missing type).
Richard Smith649c7b062014-01-08 00:56:48 +00002227 if (!isTypeSpecifier(DSC) &&
Richard Smithfe904f02012-05-15 21:29:55 +00002228 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002229 // Look ahead to the next token to try to figure out what this declaration
2230 // was supposed to be.
2231 switch (NextToken().getKind()) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002232 case tok::l_paren: {
2233 // static x(4); // 'x' is not a type
2234 // x(int n); // 'x' is not a type
2235 // x (*p)[]; // 'x' is a type
2236 //
2237 // Since we're in an error case (or the rare 'implicit int in C++' MS
2238 // extension), we can afford to perform a tentative parse to determine
2239 // which case we're in.
2240 TentativeParsingAction PA(*this);
2241 ConsumeToken();
2242 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2243 PA.Revert();
Richard Smithfb8b7b92013-10-15 00:00:26 +00002244
2245 if (TPR != TPResult::False()) {
2246 // The identifier is followed by a parenthesized declarator.
2247 // It's supposed to be a type.
2248 break;
2249 }
2250
2251 // If we're in a context where we could be declaring a constructor,
2252 // check whether this is a constructor declaration with a bogus name.
2253 if (DSC == DSC_class || (DSC == DSC_top_level && SS)) {
2254 IdentifierInfo *II = Tok.getIdentifierInfo();
2255 if (Actions.isCurrentClassNameTypo(II, SS)) {
2256 Diag(Loc, diag::err_constructor_bad_name)
2257 << Tok.getIdentifierInfo() << II
2258 << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
2259 Tok.setIdentifierInfo(II);
2260 }
2261 }
2262 // Fall through.
Richard Smitha952ebb2012-05-15 21:01:51 +00002263 }
Richard Smithfb8b7b92013-10-15 00:00:26 +00002264 case tok::comma:
2265 case tok::equal:
2266 case tok::kw_asm:
2267 case tok::l_brace:
2268 case tok::l_square:
2269 case tok::semi:
2270 // This looks like a variable or function declaration. The type is
2271 // probably missing. We're done parsing decl-specifiers.
2272 if (SS)
2273 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2274 return false;
Richard Smitha952ebb2012-05-15 21:01:51 +00002275
2276 default:
2277 // This is probably supposed to be a type. This includes cases like:
2278 // int f(itn);
2279 // struct S { unsinged : 4; };
2280 break;
2281 }
2282 }
2283
Chad Rosierc1183952012-06-26 22:30:43 +00002284 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00002285 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002286 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002287 IdentifierInfo *II = Tok.getIdentifierInfo();
2288 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00002289 // The action emitted a diagnostic, so we don't have to.
2290 if (T) {
2291 // The action has suggested that the type T could be used. Set that as
2292 // the type in the declaration specifiers, consume the would-be type
2293 // name token, and we're done.
2294 const char *PrevSpec;
2295 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00002296 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00002297 DS.SetRangeEnd(Tok.getLocation());
2298 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002299 // There may be other declaration specifiers after this.
2300 return true;
2301 } else if (II != Tok.getIdentifierInfo()) {
2302 // If no type was suggested, the correction is to a keyword
2303 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002304 // There may be other declaration specifiers after this.
2305 return true;
2306 }
Chad Rosierc1183952012-06-26 22:30:43 +00002307
Douglas Gregor15e56022009-10-13 23:27:22 +00002308 // Fall through; the action had no suggestion for us.
2309 } else {
2310 // The action did not emit a diagnostic, so emit one now.
2311 SourceRange R;
2312 if (SS) R = SS->getRange();
2313 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2314 }
Mike Stump11289f42009-09-09 15:08:12 +00002315
Douglas Gregor15e56022009-10-13 23:27:22 +00002316 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002317 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002318 DS.SetRangeEnd(Tok.getLocation());
2319 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002320
Chris Lattner20a0c612009-04-14 21:34:55 +00002321 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2322 // avoid rippling error messages on subsequent uses of the same type,
2323 // could be useful if #include was forgotten.
2324 return false;
2325}
2326
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002327/// \brief Determine the declaration specifier context from the declarator
2328/// context.
2329///
2330/// \param Context the declarator context, which is one of the
2331/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002332Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002333Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2334 if (Context == Declarator::MemberContext)
2335 return DSC_class;
2336 if (Context == Declarator::FileContext)
2337 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002338 if (Context == Declarator::TrailingReturnContext)
2339 return DSC_trailing;
Richard Smith649c7b062014-01-08 00:56:48 +00002340 if (Context == Declarator::AliasDeclContext ||
2341 Context == Declarator::AliasTemplateContext)
2342 return DSC_alias_declaration;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002343 return DSC_normal;
2344}
2345
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002346/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2347///
2348/// FIXME: Simply returns an alignof() expression if the argument is a
2349/// type. Ideally, the type should be propagated directly into Sema.
2350///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002351/// [C11] type-id
2352/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002353/// [C++0x] type-id ...[opt]
2354/// [C++0x] assignment-expression ...[opt]
2355ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2356 SourceLocation &EllipsisLoc) {
2357 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002358 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002359 SourceLocation TypeLoc = Tok.getLocation();
2360 ParsedType Ty = ParseTypeName().get();
2361 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002362 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2363 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002364 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002365 ER = ParseConstantExpression();
2366
Alp Toker8fbec672013-12-17 23:29:36 +00002367 if (getLangOpts().CPlusPlus11)
2368 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002369
2370 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002371}
2372
2373/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2374/// attribute to Attrs.
2375///
2376/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002377/// [C11] '_Alignas' '(' type-id ')'
2378/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002379/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2380/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002381void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002382 SourceLocation *EndLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002383 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2384 "Not an alignment-specifier!");
2385
Richard Smithd11c7a12013-01-29 01:48:07 +00002386 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2387 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002388
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002389 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00002390 if (T.expectAndConsume())
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002391 return;
2392
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002393 SourceLocation EllipsisLoc;
2394 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002395 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002396 T.skipToEnd();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002397 return;
2398 }
2399
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002400 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002401 if (EndLoc)
2402 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002403
Aaron Ballman00e99962013-08-31 01:11:41 +00002404 ArgsVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002405 ArgExprs.push_back(ArgExpr.release());
Aaron Ballman00e99962013-08-31 01:11:41 +00002406 Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1,
2407 AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002408}
2409
Richard Smith404dfb42013-11-19 22:47:36 +00002410/// Determine whether we're looking at something that might be a declarator
2411/// in a simple-declaration. If it can't possibly be a declarator, maybe
2412/// diagnose a missing semicolon after a prior tag definition in the decl
2413/// specifier.
2414///
2415/// \return \c true if an error occurred and this can't be any kind of
2416/// declaration.
2417bool
2418Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
2419 DeclSpecContext DSContext,
2420 LateParsedAttrList *LateAttrs) {
2421 assert(DS.hasTagDefinition() && "shouldn't call this");
2422
2423 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Richard Smith404dfb42013-11-19 22:47:36 +00002424
2425 if (getLangOpts().CPlusPlus &&
2426 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2427 Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) &&
2428 TryAnnotateCXXScopeToken(EnteringContext)) {
2429 SkipMalformedDecl();
2430 return true;
2431 }
2432
Richard Smith698875a2013-11-20 23:40:57 +00002433 bool HasScope = Tok.is(tok::annot_cxxscope);
2434 // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
2435 Token AfterScope = HasScope ? NextToken() : Tok;
2436
Richard Smith404dfb42013-11-19 22:47:36 +00002437 // Determine whether the following tokens could possibly be a
2438 // declarator.
Richard Smith698875a2013-11-20 23:40:57 +00002439 bool MightBeDeclarator = true;
2440 if (Tok.is(tok::kw_typename) || Tok.is(tok::annot_typename)) {
2441 // A declarator-id can't start with 'typename'.
2442 MightBeDeclarator = false;
2443 } else if (AfterScope.is(tok::annot_template_id)) {
2444 // If we have a type expressed as a template-id, this cannot be a
2445 // declarator-id (such a type cannot be redeclared in a simple-declaration).
2446 TemplateIdAnnotation *Annot =
2447 static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
2448 if (Annot->Kind == TNK_Type_template)
2449 MightBeDeclarator = false;
2450 } else if (AfterScope.is(tok::identifier)) {
2451 const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken();
2452
Richard Smith404dfb42013-11-19 22:47:36 +00002453 // These tokens cannot come after the declarator-id in a
2454 // simple-declaration, and are likely to come after a type-specifier.
Richard Smith698875a2013-11-20 23:40:57 +00002455 if (Next.is(tok::star) || Next.is(tok::amp) || Next.is(tok::ampamp) ||
2456 Next.is(tok::identifier) || Next.is(tok::annot_cxxscope) ||
2457 Next.is(tok::coloncolon)) {
2458 // Missing a semicolon.
2459 MightBeDeclarator = false;
2460 } else if (HasScope) {
2461 // If the declarator-id has a scope specifier, it must redeclare a
2462 // previously-declared entity. If that's a type (and this is not a
2463 // typedef), that's an error.
2464 CXXScopeSpec SS;
2465 Actions.RestoreNestedNameSpecifierAnnotation(
2466 Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
2467 IdentifierInfo *Name = AfterScope.getIdentifierInfo();
2468 Sema::NameClassification Classification = Actions.ClassifyName(
2469 getCurScope(), SS, Name, AfterScope.getLocation(), Next,
2470 /*IsAddressOfOperand*/false);
2471 switch (Classification.getKind()) {
2472 case Sema::NC_Error:
2473 SkipMalformedDecl();
2474 return true;
Richard Smith404dfb42013-11-19 22:47:36 +00002475
Richard Smith698875a2013-11-20 23:40:57 +00002476 case Sema::NC_Keyword:
2477 case Sema::NC_NestedNameSpecifier:
2478 llvm_unreachable("typo correction and nested name specifiers not "
2479 "possible here");
Richard Smith404dfb42013-11-19 22:47:36 +00002480
Richard Smith698875a2013-11-20 23:40:57 +00002481 case Sema::NC_Type:
2482 case Sema::NC_TypeTemplate:
2483 // Not a previously-declared non-type entity.
2484 MightBeDeclarator = false;
2485 break;
Richard Smith404dfb42013-11-19 22:47:36 +00002486
Richard Smith698875a2013-11-20 23:40:57 +00002487 case Sema::NC_Unknown:
2488 case Sema::NC_Expression:
2489 case Sema::NC_VarTemplate:
2490 case Sema::NC_FunctionTemplate:
2491 // Might be a redeclaration of a prior entity.
2492 break;
2493 }
Richard Smith404dfb42013-11-19 22:47:36 +00002494 }
Richard Smith404dfb42013-11-19 22:47:36 +00002495 }
2496
Richard Smith698875a2013-11-20 23:40:57 +00002497 if (MightBeDeclarator)
Richard Smith404dfb42013-11-19 22:47:36 +00002498 return false;
2499
2500 Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()),
Alp Toker383d2c42014-01-01 03:08:43 +00002501 diag::err_expected_after)
2502 << DeclSpec::getSpecifierName(DS.getTypeSpecType()) << tok::semi;
Richard Smith404dfb42013-11-19 22:47:36 +00002503
2504 // Try to recover from the typo, by dropping the tag definition and parsing
2505 // the problematic tokens as a type.
2506 //
2507 // FIXME: Split the DeclSpec into pieces for the standalone
2508 // declaration and pieces for the following declaration, instead
2509 // of assuming that all the other pieces attach to new declaration,
2510 // and call ParsedFreeStandingDeclSpec as appropriate.
2511 DS.ClearTypeSpecType();
2512 ParsedTemplateInfo NotATemplate;
2513 ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
2514 return false;
2515}
2516
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002517/// ParseDeclarationSpecifiers
2518/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002519/// storage-class-specifier declaration-specifiers[opt]
2520/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002521/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002522/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002523/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002524/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002525///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002526/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002527/// 'typedef'
2528/// 'extern'
2529/// 'static'
2530/// 'auto'
2531/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002532/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00002533/// [C++11] 'thread_local'
2534/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002535/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002536/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002537/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002538/// [C++] 'virtual'
2539/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002540/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002541/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002542/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002543
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002544///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002545void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002546 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002547 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002548 DeclSpecContext DSContext,
2549 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002550 if (DS.getSourceRange().isInvalid()) {
2551 DS.SetRangeStart(Tok.getLocation());
2552 DS.SetRangeEnd(Tok.getLocation());
2553 }
Chad Rosierc1183952012-06-26 22:30:43 +00002554
Douglas Gregordf593fb2011-11-07 17:33:42 +00002555 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002556 bool AttrsLastTime = false;
2557 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002558 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002559 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002560 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002561 unsigned DiagID = 0;
2562
Chris Lattner4d8f8732006-11-28 05:05:08 +00002563 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002564
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002565 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002566 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002567 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002568 if (!AttrsLastTime)
2569 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002570 else {
2571 // Reject C++11 attributes that appertain to decl specifiers as
2572 // we don't support any C++11 attributes that appertain to decl
2573 // specifiers. This also conforms to what g++ 4.8 is doing.
2574 ProhibitCXX11Attributes(attrs);
2575
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002576 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002577 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002578
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002579 // If this is not a declaration specifier token, we're done reading decl
2580 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002581 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002582 return;
Mike Stump11289f42009-09-09 15:08:12 +00002583
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002584 case tok::l_square:
2585 case tok::kw_alignas:
Richard Smith4cabd042013-02-22 09:15:49 +00002586 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002587 goto DoneWithDeclSpec;
2588
2589 ProhibitAttributes(attrs);
2590 // FIXME: It would be good to recover by accepting the attributes,
2591 // but attempting to do that now would cause serious
2592 // madness in terms of diagnostics.
2593 attrs.clear();
2594 attrs.Range = SourceRange();
2595
2596 ParseCXX11Attributes(attrs);
2597 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002598 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002599
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002600 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002601 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002602 if (DS.hasTypeSpecifier()) {
2603 bool AllowNonIdentifiers
2604 = (getCurScope()->getFlags() & (Scope::ControlScope |
2605 Scope::BlockScope |
2606 Scope::TemplateParamScope |
2607 Scope::FunctionPrototypeScope |
2608 Scope::AtCatchScope)) == 0;
2609 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002610 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002611 (DSContext == DSC_class && DS.isFriendSpecified());
2612
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002613 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002614 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002615 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002616 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002617 }
2618
Douglas Gregor80039242011-02-15 20:33:25 +00002619 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2620 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2621 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002622 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002623 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002624 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002625 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002626 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002627 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002628
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002629 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002630 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002631 }
2632
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002633 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002634 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002635 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002636 if (!DS.hasTypeSpecifier())
2637 DS.SetTypeSpecError();
2638 goto DoneWithDeclSpec;
2639 }
John McCall8bc2a702010-03-01 18:20:46 +00002640 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2641 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002642 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002643
2644 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002645 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002646 goto DoneWithDeclSpec;
2647
John McCall9dab4e62009-12-12 11:40:51 +00002648 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002649 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2650 Tok.getAnnotationRange(),
2651 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002652
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002653 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002654 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002655 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002656 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002657 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002658 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002659
2660 // C++ [class.qual]p2:
2661 // In a lookup in which the constructor is an acceptable lookup
2662 // result and the nested-name-specifier nominates a class C:
2663 //
2664 // - if the name specified after the
2665 // nested-name-specifier, when looked up in C, is the
2666 // injected-class-name of C (Clause 9), or
2667 //
2668 // - if the name specified after the nested-name-specifier
2669 // is the same as the identifier or the
2670 // simple-template-id's template-name in the last
2671 // component of the nested-name-specifier,
2672 //
2673 // the name is instead considered to name the constructor of
2674 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002675 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002676 // Thus, if the template-name is actually the constructor
2677 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002678 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002679 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002680 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00002681 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002682 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002683 if (isConstructorDeclarator()) {
2684 // The user meant this to be an out-of-line constructor
2685 // definition, but template arguments are not allowed
2686 // there. Just allow this as a constructor; we'll
2687 // complain about it later.
2688 goto DoneWithDeclSpec;
2689 }
2690
2691 // The user meant this to name a type, but it actually names
2692 // a constructor with some extraneous template
2693 // arguments. Complain, then parse it as a type as the user
2694 // intended.
2695 Diag(TemplateId->TemplateNameLoc,
2696 diag::err_out_of_line_template_id_names_constructor)
2697 << TemplateId->Name;
2698 }
2699
John McCall9dab4e62009-12-12 11:40:51 +00002700 DS.getTypeSpecScope() = SS;
2701 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002702 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002703 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002704 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002705 continue;
2706 }
2707
Douglas Gregorc5790df2009-09-28 07:26:33 +00002708 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002709 DS.getTypeSpecScope() = SS;
2710 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002711 if (Tok.getAnnotationValue()) {
2712 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002713 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002714 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002715 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002716 if (isInvalid)
2717 break;
John McCallba7bf592010-08-24 05:47:05 +00002718 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002719 else
2720 DS.SetTypeSpecError();
2721 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2722 ConsumeToken(); // The typename
2723 }
2724
Douglas Gregor167fa622009-03-25 15:40:00 +00002725 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002726 goto DoneWithDeclSpec;
2727
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002728 // If we're in a context where the identifier could be a class name,
2729 // check whether this is a constructor declaration.
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002730 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002731 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002732 &SS)) {
2733 if (isConstructorDeclarator())
2734 goto DoneWithDeclSpec;
2735
2736 // As noted in C++ [class.qual]p2 (cited above), when the name
2737 // of the class is qualified in a context where it could name
2738 // a constructor, its a constructor name. However, we've
2739 // looked at the declarator, and the user probably meant this
2740 // to be a type. Complain that it isn't supposed to be treated
2741 // as a type, then proceed to parse it as a type.
2742 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2743 << Next.getIdentifierInfo();
2744 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002745
John McCallba7bf592010-08-24 05:47:05 +00002746 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2747 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002748 getCurScope(), &SS,
2749 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002750 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002751 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002752
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002753 // If the referenced identifier is not a type, then this declspec is
2754 // erroneous: We already checked about that it has no type specifier, and
2755 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002756 // typename.
David Blaikie7d170102013-05-15 07:37:26 +00002757 if (!TypeRep) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002758 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002759 ParsedAttributesWithRange Attrs(AttrFactory);
2760 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2761 if (!Attrs.empty()) {
2762 AttrsLastTime = true;
2763 attrs.takeAllFrom(Attrs);
2764 }
2765 continue;
2766 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002767 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002768 }
Mike Stump11289f42009-09-09 15:08:12 +00002769
John McCall9dab4e62009-12-12 11:40:51 +00002770 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002771 ConsumeToken(); // The C++ scope.
2772
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002773 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002774 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002775 if (isInvalid)
2776 break;
Mike Stump11289f42009-09-09 15:08:12 +00002777
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002778 DS.SetRangeEnd(Tok.getLocation());
2779 ConsumeToken(); // The typename.
2780
2781 continue;
2782 }
Mike Stump11289f42009-09-09 15:08:12 +00002783
Chris Lattnere387d9e2009-01-21 19:48:37 +00002784 case tok::annot_typename: {
Richard Smith404dfb42013-11-19 22:47:36 +00002785 // If we've previously seen a tag definition, we were almost surely
2786 // missing a semicolon after it.
2787 if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
2788 goto DoneWithDeclSpec;
2789
John McCallba7bf592010-08-24 05:47:05 +00002790 if (Tok.getAnnotationValue()) {
2791 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002792 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002793 DiagID, T);
2794 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002795 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002796
Chris Lattner005fc1b2010-04-05 18:18:31 +00002797 if (isInvalid)
2798 break;
2799
Chris Lattnere387d9e2009-01-21 19:48:37 +00002800 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2801 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002802
Chris Lattnere387d9e2009-01-21 19:48:37 +00002803 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2804 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002805 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002806 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002807 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002808
Chris Lattnere387d9e2009-01-21 19:48:37 +00002809 continue;
2810 }
Mike Stump11289f42009-09-09 15:08:12 +00002811
Douglas Gregor06873092011-04-28 15:48:45 +00002812 case tok::kw___is_signed:
2813 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2814 // typically treats it as a trait. If we see __is_signed as it appears
2815 // in libstdc++, e.g.,
2816 //
2817 // static const bool __is_signed;
2818 //
2819 // then treat __is_signed as an identifier rather than as a keyword.
2820 if (DS.getTypeSpecType() == TST_bool &&
2821 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
Alp Toker47642d22013-12-03 06:13:01 +00002822 DS.getStorageClassSpec() == DeclSpec::SCS_static)
2823 TryKeywordIdentFallback(true);
Douglas Gregor06873092011-04-28 15:48:45 +00002824
2825 // We're done with the declaration-specifiers.
2826 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002827
Chris Lattner16fac4f2008-07-26 01:18:38 +00002828 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002829 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002830 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002831 // In C++, check to see if this is a scope specifier like foo::bar::, if
2832 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002833 if (getLangOpts().CPlusPlus) {
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002834 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002835 if (!DS.hasTypeSpecifier())
2836 DS.SetTypeSpecError();
2837 goto DoneWithDeclSpec;
2838 }
2839 if (!Tok.is(tok::identifier))
2840 continue;
2841 }
Mike Stump11289f42009-09-09 15:08:12 +00002842
Chris Lattner16fac4f2008-07-26 01:18:38 +00002843 // This identifier can only be a typedef name if we haven't already seen
2844 // a type-specifier. Without this check we misparse:
2845 // typedef int X; struct Y { short X; }; as 'short int'.
2846 if (DS.hasTypeSpecifier())
2847 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002848
John Thompson22334602010-02-05 00:12:22 +00002849 // Check for need to substitute AltiVec keyword tokens.
2850 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2851 break;
2852
Richard Smith3092a3b2012-05-09 18:56:43 +00002853 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2854 // allow the use of a typedef name as a type specifier.
2855 if (DS.isTypeAltiVecVector())
2856 goto DoneWithDeclSpec;
2857
John McCallba7bf592010-08-24 05:47:05 +00002858 ParsedType TypeRep =
2859 Actions.getTypeName(*Tok.getIdentifierInfo(),
2860 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002861
Chris Lattner6cc055a2009-04-12 20:42:31 +00002862 // If this is not a typedef name, don't parse it as part of the declspec,
2863 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002864 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002865 ParsedAttributesWithRange Attrs(AttrFactory);
2866 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2867 if (!Attrs.empty()) {
2868 AttrsLastTime = true;
2869 attrs.takeAllFrom(Attrs);
2870 }
2871 continue;
2872 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002873 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002874 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002875
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002876 // If we're in a context where the identifier could be a class name,
2877 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002878 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002879 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002880 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002881 goto DoneWithDeclSpec;
2882
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002883 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002884 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002885 if (isInvalid)
2886 break;
Mike Stump11289f42009-09-09 15:08:12 +00002887
Chris Lattner16fac4f2008-07-26 01:18:38 +00002888 DS.SetRangeEnd(Tok.getLocation());
2889 ConsumeToken(); // The identifier
2890
2891 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2892 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002893 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002894 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002895 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002896
Steve Naroffcd5e7822008-09-22 10:28:57 +00002897 // Need to support trailing type qualifiers (e.g. "id<p> const").
2898 // If a type specifier follows, it will be diagnosed elsewhere.
2899 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002900 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002901
2902 // type-name
2903 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002904 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002905 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002906 // This template-id does not refer to a type name, so we're
2907 // done with the type-specifiers.
2908 goto DoneWithDeclSpec;
2909 }
2910
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002911 // If we're in a context where the template-id could be a
2912 // constructor name or specialization, check whether this is a
2913 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002914 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002915 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002916 isConstructorDeclarator())
2917 goto DoneWithDeclSpec;
2918
Douglas Gregor7f741122009-02-25 19:37:18 +00002919 // Turn the template-id annotation token into a type annotation
2920 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002921 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002922 continue;
2923 }
2924
Chris Lattnere37e2332006-08-15 04:50:22 +00002925 // GNU attributes support.
2926 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002927 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002928 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002929
2930 // Microsoft declspec support.
2931 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002932 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002933 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002934
Steve Naroff44ac7772008-12-25 14:16:32 +00002935 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002936 case tok::kw___forceinline: {
Serge Pavlov750db652013-11-13 06:57:53 +00002937 isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002938 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002939 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002940 // FIXME: This does not work correctly if it is set to be a declspec
2941 // attribute, and a GNU attribute is simply incorrect.
Aaron Ballman00e99962013-08-31 01:11:41 +00002942 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
2943 AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002944 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002945 }
Eli Friedman53339e02009-06-08 23:27:34 +00002946
Aaron Ballman317a77f2013-05-22 23:25:32 +00002947 case tok::kw___sptr:
2948 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00002949 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002950 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002951 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002952 case tok::kw___cdecl:
2953 case tok::kw___stdcall:
2954 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002955 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002956 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002957 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002958 continue;
2959
Dawn Perchik335e16b2010-09-03 01:29:35 +00002960 // Borland single token adornments.
2961 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002962 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002963 continue;
2964
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002965 // OpenCL single token adornments.
2966 case tok::kw___kernel:
2967 ParseOpenCLAttributes(DS.getAttributes());
2968 continue;
2969
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002970 // storage-class-specifier
2971 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002972 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2973 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002974 break;
2975 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00002976 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002977 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002978 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2979 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002980 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002981 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002982 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2983 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002984 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002985 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00002986 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002987 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002988 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2989 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002990 break;
2991 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002992 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002993 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002994 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2995 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002996 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002997 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002998 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002999 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003000 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
3001 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00003002 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003003 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
3004 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003005 break;
3006 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003007 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
3008 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003009 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00003010 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003011 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
3012 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00003013 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003014 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00003015 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
3016 PrevSpec, DiagID);
3017 break;
3018 case tok::kw_thread_local:
3019 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
3020 PrevSpec, DiagID);
3021 break;
3022 case tok::kw__Thread_local:
3023 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
3024 Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003025 break;
Mike Stump11289f42009-09-09 15:08:12 +00003026
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003027 // function-specifier
3028 case tok::kw_inline:
Serge Pavlov750db652013-11-13 06:57:53 +00003029 isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003030 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00003031 case tok::kw_virtual:
Serge Pavlov750db652013-11-13 06:57:53 +00003032 isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00003033 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00003034 case tok::kw_explicit:
Serge Pavlov750db652013-11-13 06:57:53 +00003035 isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00003036 break;
Richard Smith0015f092013-01-17 22:16:11 +00003037 case tok::kw__Noreturn:
3038 if (!getLangOpts().C11)
3039 Diag(Loc, diag::ext_c11_noreturn);
Serge Pavlov750db652013-11-13 06:57:53 +00003040 isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
Richard Smith0015f092013-01-17 22:16:11 +00003041 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003042
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003043 // alignment-specifier
3044 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003045 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00003046 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003047 ParseAlignmentSpecifier(DS.getAttributes());
3048 continue;
3049
Anders Carlssoncd8db412009-05-06 04:46:28 +00003050 // friend
3051 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00003052 if (DSContext == DSC_class)
3053 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
3054 else {
3055 PrevSpec = ""; // not actually used by the diagnostic
3056 DiagID = diag::err_friend_invalid_in_context;
3057 isInvalid = true;
3058 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00003059 break;
Mike Stump11289f42009-09-09 15:08:12 +00003060
Douglas Gregor26701a42011-09-09 02:06:17 +00003061 // Modules
3062 case tok::kw___module_private__:
3063 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
3064 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003065
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00003066 // constexpr
3067 case tok::kw_constexpr:
3068 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
3069 break;
3070
Chris Lattnere387d9e2009-01-21 19:48:37 +00003071 // type-specifier
3072 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00003073 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
3074 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003075 break;
3076 case tok::kw_long:
3077 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00003078 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
3079 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003080 else
John McCall49bfce42009-08-03 20:12:06 +00003081 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
3082 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003083 break;
Francois Pichet84133e42011-04-28 01:59:37 +00003084 case tok::kw___int64:
3085 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
3086 DiagID);
3087 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003088 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00003089 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
3090 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003091 break;
3092 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00003093 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
3094 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003095 break;
3096 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00003097 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
3098 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003099 break;
3100 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00003101 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
3102 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003103 break;
3104 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00003105 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
3106 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003107 break;
3108 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00003109 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
3110 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003111 break;
3112 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00003113 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
3114 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003115 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00003116 case tok::kw___int128:
3117 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
3118 DiagID);
3119 break;
3120 case tok::kw_half:
3121 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
3122 DiagID);
3123 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003124 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00003125 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
3126 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003127 break;
3128 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00003129 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
3130 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003131 break;
3132 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00003133 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
3134 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003135 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003136 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00003137 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
3138 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003139 break;
3140 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00003141 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
3142 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003143 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003144 case tok::kw_bool:
3145 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003146 if (Tok.is(tok::kw_bool) &&
3147 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
3148 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
3149 PrevSpec = ""; // Not used by the diagnostic.
3150 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003151 // For better error recovery.
3152 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003153 isInvalid = true;
3154 } else {
3155 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
3156 DiagID);
3157 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003158 break;
3159 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00003160 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
3161 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003162 break;
3163 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00003164 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
3165 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003166 break;
3167 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00003168 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
3169 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003170 break;
John Thompson22334602010-02-05 00:12:22 +00003171 case tok::kw___vector:
3172 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3173 break;
3174 case tok::kw___pixel:
3175 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3176 break;
John McCall39439732011-04-09 22:50:59 +00003177 case tok::kw___unknown_anytype:
3178 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
3179 PrevSpec, DiagID);
3180 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003181
3182 // class-specifier:
3183 case tok::kw_class:
3184 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003185 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003186 case tok::kw_union: {
3187 tok::TokenKind Kind = Tok.getKind();
3188 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00003189
3190 // These are attributes following class specifiers.
3191 // To produce better diagnostic, we parse them when
3192 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00003193 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00003194 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00003195 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003196
3197 // If there are attributes following class specifier,
3198 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00003199 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00003200 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00003201 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003202 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003203 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003204 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003205
3206 // enum-specifier:
3207 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003208 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003209 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003210 continue;
3211
3212 // cv-qualifier:
3213 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003214 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003215 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003216 break;
3217 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003218 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003219 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003220 break;
3221 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003222 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003223 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003224 break;
3225
Douglas Gregor333489b2009-03-27 23:10:48 +00003226 // C++ typename-specifier:
3227 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003228 if (TryAnnotateTypeOrScopeToken()) {
3229 DS.SetTypeSpecError();
3230 goto DoneWithDeclSpec;
3231 }
3232 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003233 continue;
3234 break;
3235
Chris Lattnere387d9e2009-01-21 19:48:37 +00003236 // GNU typeof support.
3237 case tok::kw_typeof:
3238 ParseTypeofSpecifier(DS);
3239 continue;
3240
David Blaikie15a430a2011-12-04 05:04:18 +00003241 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003242 ParseDecltypeSpecifier(DS);
3243 continue;
3244
Alexis Hunt4a257072011-05-19 05:37:45 +00003245 case tok::kw___underlying_type:
3246 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003247 continue;
3248
3249 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003250 // C11 6.7.2.4/4:
3251 // If the _Atomic keyword is immediately followed by a left parenthesis,
3252 // it is interpreted as a type specifier (with a type name), not as a
3253 // type qualifier.
3254 if (NextToken().is(tok::l_paren)) {
3255 ParseAtomicSpecifier(DS);
3256 continue;
3257 }
3258 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3259 getLangOpts());
3260 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003261
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003262 // OpenCL qualifiers:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003263 case tok::kw___private:
3264 case tok::kw___global:
3265 case tok::kw___local:
3266 case tok::kw___constant:
3267 case tok::kw___read_only:
3268 case tok::kw___write_only:
3269 case tok::kw___read_write:
3270 ParseOpenCLQualifiers(DS);
3271 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003272
Steve Naroffcfdf6162008-06-05 00:02:44 +00003273 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003274 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003275 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3276 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003277 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00003278 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003279
Douglas Gregor3a001f42010-11-19 17:10:50 +00003280 if (!ParseObjCProtocolQualifiers(DS))
3281 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3282 << FixItHint::CreateInsertion(Loc, "id")
3283 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00003284
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003285 // Need to support trailing type qualifiers (e.g. "id<p> const").
3286 // If a type specifier follows, it will be diagnosed elsewhere.
3287 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003288 }
John McCall49bfce42009-08-03 20:12:06 +00003289 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003290 if (isInvalid) {
3291 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003292 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003293
Douglas Gregora05f5ab2010-08-23 14:34:43 +00003294 if (DiagID == diag::ext_duplicate_declspec)
3295 Diag(Tok, DiagID)
3296 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3297 else
3298 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003299 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003300
Chris Lattner2e232092008-03-13 06:29:04 +00003301 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003302 if (DiagID != diag::err_bool_redeclaration)
3303 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003304
3305 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003306 }
3307}
Douglas Gregoreb31f392008-12-01 23:54:00 +00003308
Chris Lattner70ae4912007-10-29 04:42:53 +00003309/// ParseStructDeclaration - Parse a struct declaration without the terminating
3310/// semicolon.
3311///
Chris Lattner90a26b02007-01-23 04:38:16 +00003312/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00003313/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00003314/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00003315/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00003316/// struct-declarator-list:
3317/// struct-declarator
3318/// struct-declarator-list ',' struct-declarator
3319/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3320/// struct-declarator:
3321/// declarator
3322/// [GNU] declarator attributes[opt]
3323/// declarator[opt] ':' constant-expression
3324/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3325///
Chris Lattnera12405b2008-04-10 06:46:29 +00003326void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003327ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00003328
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003329 if (Tok.is(tok::kw___extension__)) {
3330 // __extension__ silences extension warnings in the subexpression.
3331 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00003332 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003333 return ParseStructDeclaration(DS, Fields);
3334 }
Mike Stump11289f42009-09-09 15:08:12 +00003335
Steve Naroff97170802007-08-20 22:28:22 +00003336 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00003337 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003338
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00003339 // If there are no declarators, this is a free-standing declaration
3340 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00003341 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003342 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3343 DS);
3344 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00003345 return;
3346 }
3347
3348 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00003349 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00003350 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00003351 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003352 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00003353 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00003354
Bill Wendling44426052012-12-20 19:22:21 +00003355 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00003356 if (!FirstDeclarator)
3357 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00003358
Steve Naroff97170802007-08-20 22:28:22 +00003359 /// struct-declarator: declarator
3360 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003361 if (Tok.isNot(tok::colon)) {
3362 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3363 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00003364 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003365 }
Mike Stump11289f42009-09-09 15:08:12 +00003366
Alp Toker8fbec672013-12-17 23:29:36 +00003367 if (TryConsumeToken(tok::colon)) {
John McCalldadc5752010-08-24 06:29:42 +00003368 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003369 if (Res.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00003370 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner32295d32008-04-10 06:15:14 +00003371 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00003372 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00003373 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003374
Steve Naroff97170802007-08-20 22:28:22 +00003375 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003376 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003377
John McCallcfefb6d2009-11-03 02:38:08 +00003378 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00003379 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00003380
Steve Naroff97170802007-08-20 22:28:22 +00003381 // If we don't have a comma, it is either the end of the list (a ';')
3382 // or an error, bail out.
Alp Toker8fbec672013-12-17 23:29:36 +00003383 if (!TryConsumeToken(tok::comma, CommaLoc))
Chris Lattner70ae4912007-10-29 04:42:53 +00003384 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003385
John McCallcfefb6d2009-11-03 02:38:08 +00003386 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003387 }
Steve Naroff97170802007-08-20 22:28:22 +00003388}
3389
3390/// ParseStructUnionBody
3391/// struct-contents:
3392/// struct-declaration-list
3393/// [EXT] empty
3394/// [GNU] "struct-declaration-list" without terminatoring ';'
3395/// struct-declaration-list:
3396/// struct-declaration
3397/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003398/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003399///
Chris Lattner1300fb92007-01-23 23:42:53 +00003400void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003401 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003402 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3403 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00003404 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00003405
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003406 BalancedDelimiterTracker T(*this, tok::l_brace);
3407 if (T.consumeOpen())
3408 return;
Mike Stump11289f42009-09-09 15:08:12 +00003409
Douglas Gregor658b9552009-01-09 22:42:13 +00003410 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003411 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003412
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003413 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003414
Chris Lattner7b9ace62007-01-23 20:11:08 +00003415 // While we still have something to read, read the declarations in the struct.
Richard Smith34f30512013-11-23 04:06:09 +00003416 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003417 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003418
Chris Lattner736ed5d2007-06-09 05:59:07 +00003419 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003420 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003421 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003422 continue;
3423 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003424
Andy Gibbsc804e082013-04-03 09:46:04 +00003425 // Parse _Static_assert declaration.
3426 if (Tok.is(tok::kw__Static_assert)) {
3427 SourceLocation DeclEnd;
3428 ParseStaticAssertDeclaration(DeclEnd);
3429 continue;
3430 }
3431
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00003432 if (Tok.is(tok::annot_pragma_pack)) {
3433 HandlePragmaPack();
3434 continue;
3435 }
3436
3437 if (Tok.is(tok::annot_pragma_align)) {
3438 HandlePragmaAlign();
3439 continue;
3440 }
3441
John McCallcfefb6d2009-11-03 02:38:08 +00003442 if (!Tok.is(tok::at)) {
3443 struct CFieldCallback : FieldCallback {
3444 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003445 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003446 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003447
John McCall48871652010-08-21 09:40:31 +00003448 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003449 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003450 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3451
Eli Friedman934dbbf2012-08-08 23:53:27 +00003452 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003453 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003454 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003455 FD.D.getDeclSpec().getSourceRange().getBegin(),
3456 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003457 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003458 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003459 }
John McCallcfefb6d2009-11-03 02:38:08 +00003460 } Callback(*this, TagDecl, FieldDecls);
3461
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003462 // Parse all the comma separated declarators.
3463 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003464 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003465 } else { // Handle @defs
3466 ConsumeToken();
3467 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3468 Diag(Tok, diag::err_unexpected_at);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003469 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003470 continue;
3471 }
3472 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00003473 ExpectAndConsume(tok::l_paren);
Chris Lattner535b8302008-06-21 19:39:06 +00003474 if (!Tok.is(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00003475 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003476 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003477 continue;
3478 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003479 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003480 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003481 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003482 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3483 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00003484 ExpectAndConsume(tok::r_paren);
Mike Stump11289f42009-09-09 15:08:12 +00003485 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003486
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003487 if (TryConsumeToken(tok::semi))
3488 continue;
3489
3490 if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003491 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003492 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003493 }
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003494
3495 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3496 // Skip to end of block or statement to avoid ext-warning on extra ';'.
3497 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
3498 // If we stopped at a ';', eat it.
3499 TryConsumeToken(tok::semi);
Chris Lattner90a26b02007-01-23 04:38:16 +00003500 }
Mike Stump11289f42009-09-09 15:08:12 +00003501
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003502 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003503
John McCall084e83d2011-03-24 11:26:52 +00003504 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003505 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003506 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003507
Douglas Gregor0be31a22010-07-02 17:43:08 +00003508 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003509 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003510 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003511 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003512 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003513 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3514 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003515}
3516
Chris Lattner3b561a32006-08-13 00:12:11 +00003517/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003518/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003519/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003520///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003521/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3522/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003523/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3524/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003525/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003526/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003527///
Richard Smith7d137e32012-03-23 03:33:32 +00003528/// [C++11] enum-head '{' enumerator-list[opt] '}'
3529/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003530///
Richard Smith7d137e32012-03-23 03:33:32 +00003531/// enum-head: [C++11]
3532/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3533/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3534/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003535///
Richard Smith7d137e32012-03-23 03:33:32 +00003536/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003537/// 'enum'
3538/// 'enum' 'class'
3539/// 'enum' 'struct'
3540///
Richard Smith7d137e32012-03-23 03:33:32 +00003541/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003542/// ':' type-specifier-seq
3543///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003544/// [C++] elaborated-type-specifier:
3545/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3546///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003547void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003548 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003549 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003550 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003551 if (Tok.is(tok::code_completion)) {
3552 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003553 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003554 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003555 }
John McCallcb432fa2011-07-06 05:58:41 +00003556
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003557 // If attributes exist after tag, parse them.
3558 ParsedAttributesWithRange attrs(AttrFactory);
3559 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003560 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003561
3562 // If declspecs exist after tag, parse them.
3563 while (Tok.is(tok::kw___declspec))
3564 ParseMicrosoftDeclSpec(attrs);
3565
Richard Smith0f8ee222012-01-10 01:33:14 +00003566 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003567 bool IsScopedUsingClassTag = false;
3568
John McCallbeae29a2012-06-23 22:30:04 +00003569 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieud0d87b52013-04-23 02:47:36 +00003570 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3571 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3572 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003573 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003574 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003575
Bill Wendling44426052012-12-20 19:22:21 +00003576 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003577 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003578 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003579
3580 // They are allowed afterwards, though.
3581 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003582 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003583 while (Tok.is(tok::kw___declspec))
3584 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003585 }
Richard Smith7d137e32012-03-23 03:33:32 +00003586
John McCall6347b682012-05-07 06:16:58 +00003587 // C++11 [temp.explicit]p12:
3588 // The usual access controls do not apply to names used to specify
3589 // explicit instantiations.
3590 // We extend this to also cover explicit specializations. Note that
3591 // we don't suppress if this turns out to be an elaborated type
3592 // specifier.
3593 bool shouldDelayDiagsInTag =
3594 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3595 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3596 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003597
Richard Smithbfdb1082012-03-12 08:56:40 +00003598 // Enum definitions should not be parsed in a trailing-return-type.
3599 bool AllowDeclaration = DSC != DSC_trailing;
3600
3601 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003602 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003603 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003604
Abramo Bagnarad7548482010-05-19 21:37:53 +00003605 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003606 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003607 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3608 // if a fixed underlying type is allowed.
3609 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003610
3611 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith1d4b2e12013-04-01 21:43:41 +00003612 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00003613 return;
3614
3615 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00003616 Diag(Tok, diag::err_expected) << tok::identifier;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003617 if (Tok.isNot(tok::l_brace)) {
3618 // Has no name and is not a definition.
3619 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003620 SkipUntil(tok::comma, StopAtSemi);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003621 return;
3622 }
3623 }
3624 }
Mike Stump11289f42009-09-09 15:08:12 +00003625
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003626 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003627 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003628 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Alp Tokerec543272013-12-24 09:48:30 +00003629 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
Mike Stump11289f42009-09-09 15:08:12 +00003630
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003631 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003632 SkipUntil(tok::comma, StopAtSemi);
Chris Lattner3b561a32006-08-13 00:12:11 +00003633 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003634 }
Mike Stump11289f42009-09-09 15:08:12 +00003635
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003636 // If an identifier is present, consume and remember it.
3637 IdentifierInfo *Name = 0;
3638 SourceLocation NameLoc;
3639 if (Tok.is(tok::identifier)) {
3640 Name = Tok.getIdentifierInfo();
3641 NameLoc = ConsumeToken();
3642 }
Mike Stump11289f42009-09-09 15:08:12 +00003643
Richard Smith0f8ee222012-01-10 01:33:14 +00003644 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003645 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3646 // declaration of a scoped enumeration.
3647 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003648 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003649 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003650 }
3651
John McCall6347b682012-05-07 06:16:58 +00003652 // Okay, end the suppression area. We'll decide whether to emit the
3653 // diagnostics in a second.
3654 if (shouldDelayDiagsInTag)
3655 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003656
Douglas Gregor0bf31402010-10-08 23:50:27 +00003657 TypeResult BaseType;
3658
Douglas Gregord1f69f62010-12-01 17:42:47 +00003659 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003660 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003661 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003662 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003663 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003664 // If we're in class scope, this can either be an enum declaration with
3665 // an underlying type, or a declaration of a bitfield member. We try to
3666 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003667 // (integer literal, sizeof); if it's still ambiguous, we then consider
3668 // anything that's a simple-type-specifier followed by '(' as an
3669 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003670 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003671 EnterExpressionEvaluationContext Unevaluated(Actions,
3672 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003673 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003674 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003675 // bit-field. This is the common case.
3676 if (TPR == TPResult::True())
3677 PossibleBitfield = true;
3678 // If the next token starts a type-specifier-seq, it may be either a
3679 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003680 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003681 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003682 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003683 GetLookAheadToken(2).getKind() == tok::semi) {
3684 // Consume the ':'.
3685 ConsumeToken();
3686 } else {
3687 // We have the start of a type-specifier-seq, so we have to perform
3688 // tentative parsing to determine whether we have an expression or a
3689 // type.
3690 TentativeParsingAction TPA(*this);
3691
3692 // Consume the ':'.
3693 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003694
3695 // If we see a type specifier followed by an open-brace, we have an
3696 // ambiguity between an underlying type and a C++11 braced
3697 // function-style cast. Resolve this by always treating it as an
3698 // underlying type.
3699 // FIXME: The standard is not entirely clear on how to disambiguate in
3700 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003701 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003702 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003703 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003704 // We'll parse this as a bitfield later.
3705 PossibleBitfield = true;
3706 TPA.Revert();
3707 } else {
3708 // We have a type-specifier-seq.
3709 TPA.Commit();
3710 }
3711 }
3712 } else {
3713 // Consume the ':'.
3714 ConsumeToken();
3715 }
3716
3717 if (!PossibleBitfield) {
3718 SourceRange Range;
3719 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003720
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003721 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003722 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003723 } else if (!getLangOpts().ObjC2) {
3724 if (getLangOpts().CPlusPlus)
3725 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3726 else
3727 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3728 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003729 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003730 }
3731
Richard Smith0f8ee222012-01-10 01:33:14 +00003732 // There are four options here. If we have 'friend enum foo;' then this is a
3733 // friend declaration, and cannot have an accompanying definition. If we have
3734 // 'enum foo;', then this is a forward declaration. If we have
3735 // 'enum foo {...' then this is a definition. Otherwise we have something
3736 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003737 //
3738 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3739 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3740 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3741 //
John McCallfaf5fb42010-08-26 23:41:50 +00003742 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003743 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003744 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003745 } else if (Tok.is(tok::l_brace)) {
3746 if (DS.isFriendSpecified()) {
3747 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3748 << SourceRange(DS.getFriendSpecLoc());
3749 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003750 SkipUntil(tok::r_brace, StopAtSemi);
John McCall6347b682012-05-07 06:16:58 +00003751 TUK = Sema::TUK_Friend;
3752 } else {
3753 TUK = Sema::TUK_Definition;
3754 }
Richard Smith649c7b062014-01-08 00:56:48 +00003755 } else if (!isTypeSpecifier(DSC) &&
Richard Smith369b9f92012-06-25 21:37:02 +00003756 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003757 (Tok.isAtStartOfLine() &&
3758 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003759 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3760 if (Tok.isNot(tok::semi)) {
3761 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00003762 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Richard Smith369b9f92012-06-25 21:37:02 +00003763 PP.EnterToken(Tok);
3764 Tok.setKind(tok::semi);
3765 }
John McCall6347b682012-05-07 06:16:58 +00003766 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003767 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003768 }
3769
3770 // If this is an elaborated type specifier, and we delayed
3771 // diagnostics before, just merge them into the current pool.
3772 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3773 diagsFromTag.redelay();
3774 }
Richard Smith7d137e32012-03-23 03:33:32 +00003775
3776 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003777 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003778 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003779 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003780 // Skip the rest of this declarator, up until the comma or semicolon.
3781 Diag(Tok, diag::err_enum_template);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003782 SkipUntil(tok::comma, StopAtSemi);
Richard Smith7d137e32012-03-23 03:33:32 +00003783 return;
3784 }
3785
3786 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3787 // Enumerations can't be explicitly instantiated.
3788 DS.SetTypeSpecError();
3789 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3790 return;
3791 }
3792
3793 assert(TemplateInfo.TemplateParams && "no template parameters");
3794 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3795 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003796 }
Chad Rosierc1183952012-06-26 22:30:43 +00003797
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003798 if (TUK == Sema::TUK_Reference)
3799 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003800
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003801 if (!Name && TUK != Sema::TUK_Definition) {
3802 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003803
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003804 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003805 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003806 return;
3807 }
Richard Smith7d137e32012-03-23 03:33:32 +00003808
Douglas Gregord6ab8742009-05-28 23:31:59 +00003809 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003810 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003811 const char *PrevSpec = 0;
3812 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003813 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003814 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003815 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003816 Owned, IsDependent, ScopedEnumKWLoc,
Richard Smith649c7b062014-01-08 00:56:48 +00003817 IsScopedUsingClassTag, BaseType,
3818 DSC == DSC_type_specifier);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003819
Douglas Gregorba41d012010-04-24 16:38:41 +00003820 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003821 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003822 // dependent tag.
3823 if (!Name) {
3824 DS.SetTypeSpecError();
3825 Diag(Tok, diag::err_expected_type_name_after_typename);
3826 return;
3827 }
Chad Rosierc1183952012-06-26 22:30:43 +00003828
Douglas Gregor0be31a22010-07-02 17:43:08 +00003829 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003830 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003831 NameLoc);
3832 if (Type.isInvalid()) {
3833 DS.SetTypeSpecError();
3834 return;
3835 }
Chad Rosierc1183952012-06-26 22:30:43 +00003836
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003837 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3838 NameLoc.isValid() ? NameLoc : StartLoc,
3839 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003840 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003841
Douglas Gregorba41d012010-04-24 16:38:41 +00003842 return;
3843 }
Mike Stump11289f42009-09-09 15:08:12 +00003844
John McCall48871652010-08-21 09:40:31 +00003845 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003846 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003847 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003848 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003849 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003850 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregorba41d012010-04-24 16:38:41 +00003851 }
Chad Rosierc1183952012-06-26 22:30:43 +00003852
Douglas Gregorba41d012010-04-24 16:38:41 +00003853 DS.SetTypeSpecError();
3854 return;
3855 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003856
Richard Smith369b9f92012-06-25 21:37:02 +00003857 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003858 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003859
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003860 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3861 NameLoc.isValid() ? NameLoc : StartLoc,
3862 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003863 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003864}
3865
Chris Lattnerc1915e22007-01-25 07:29:02 +00003866/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3867/// enumerator-list:
3868/// enumerator
3869/// enumerator-list ',' enumerator
3870/// enumerator:
3871/// enumeration-constant
3872/// enumeration-constant '=' constant-expression
3873/// enumeration-constant:
3874/// identifier
3875///
John McCall48871652010-08-21 09:40:31 +00003876void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003877 // Enter the scope of the enum body and start the definition.
3878 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003879 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003880
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003881 BalancedDelimiterTracker T(*this, tok::l_brace);
3882 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003883
Chris Lattner37256fb2007-08-27 17:24:30 +00003884 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003885 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003886 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003887
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003888 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003889
John McCall48871652010-08-21 09:40:31 +00003890 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003891
Chris Lattnerc1915e22007-01-25 07:29:02 +00003892 // Parse the enumerator-list.
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003893 while (Tok.isNot(tok::r_brace)) {
3894 // Parse enumerator. If failed, try skipping till the start of the next
3895 // enumerator definition.
3896 if (Tok.isNot(tok::identifier)) {
3897 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
3898 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) &&
3899 TryConsumeToken(tok::comma))
3900 continue;
3901 break;
3902 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003903 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3904 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003905
John McCall811a0f52010-10-22 23:36:17 +00003906 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003907 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003908 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003909 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003910 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003911
Chris Lattnerc1915e22007-01-25 07:29:02 +00003912 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003913 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003914 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003915
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003916 if (TryConsumeToken(tok::equal, EqualLoc)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003917 AssignedVal = ParseConstantExpression();
3918 if (AssignedVal.isInvalid())
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003919 SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003920 }
Mike Stump11289f42009-09-09 15:08:12 +00003921
Chris Lattnerc1915e22007-01-25 07:29:02 +00003922 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003923 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3924 LastEnumConstDecl,
3925 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003926 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003927 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003928 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003929
Chris Lattner4ef40012007-06-11 01:28:17 +00003930 EnumConstantDecls.push_back(EnumConstDecl);
3931 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003932
Douglas Gregorce66d022010-09-07 14:51:08 +00003933 if (Tok.is(tok::identifier)) {
3934 // We're missing a comma between enumerators.
3935 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003936 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003937 << FixItHint::CreateInsertion(Loc, ", ");
3938 continue;
3939 }
Chad Rosierc1183952012-06-26 22:30:43 +00003940
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003941 // Emumerator definition must be finished, only comma or r_brace are
3942 // allowed here.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003943 SourceLocation CommaLoc;
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003944 if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) {
3945 if (EqualLoc.isValid())
3946 Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace
3947 << tok::comma;
3948 else
3949 Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator);
3950 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) {
3951 if (TryConsumeToken(tok::comma, CommaLoc))
3952 continue;
3953 } else {
3954 break;
3955 }
3956 }
Mike Stump11289f42009-09-09 15:08:12 +00003957
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003958 // If comma is followed by r_brace, emit appropriate warning.
3959 if (Tok.is(tok::r_brace) && CommaLoc.isValid()) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003960 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003961 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3962 diag::ext_enumerator_list_comma_cxx :
3963 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003964 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003965 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003966 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3967 << FixItHint::CreateRemoval(CommaLoc);
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003968 break;
Richard Smith5d164bc2011-10-15 05:09:34 +00003969 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003970 }
Mike Stump11289f42009-09-09 15:08:12 +00003971
Chris Lattnerc1915e22007-01-25 07:29:02 +00003972 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003973 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003974
Chris Lattnerc1915e22007-01-25 07:29:02 +00003975 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003976 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003977 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003978
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003979 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenkoe5fde992013-04-27 20:23:52 +00003980 EnumDecl, EnumConstantDecls,
3981 getCurScope(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003982 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003983
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003984 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003985 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3986 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003987
3988 // The next token must be valid after an enum definition. If not, a ';'
3989 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003990 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3991 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Alp Toker383d2c42014-01-01 03:08:43 +00003992 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Richard Smith369b9f92012-06-25 21:37:02 +00003993 // Push this token back into the preprocessor and change our current token
3994 // to ';' so that the rest of the code recovers as though there were an
3995 // ';' after the definition.
3996 PP.EnterToken(Tok);
3997 Tok.setKind(tok::semi);
3998 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003999}
Chris Lattner3b561a32006-08-13 00:12:11 +00004000
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004001/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004002/// start of a type-qualifier-list.
4003bool Parser::isTypeQualifier() const {
4004 switch (Tok.getKind()) {
4005 default: return false;
Alp Tokerde50ff32013-12-17 18:17:46 +00004006 // type-qualifier
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004007 case tok::kw_const:
4008 case tok::kw_volatile:
4009 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004010 case tok::kw___private:
4011 case tok::kw___local:
4012 case tok::kw___global:
4013 case tok::kw___constant:
4014 case tok::kw___read_only:
4015 case tok::kw___read_write:
4016 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004017 return true;
4018 }
4019}
4020
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004021/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
4022/// is definitely a type-specifier. Return false if it isn't part of a type
4023/// specifier or if we're not sure.
4024bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
4025 switch (Tok.getKind()) {
4026 default: return false;
4027 // type-specifiers
4028 case tok::kw_short:
4029 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004030 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004031 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004032 case tok::kw_signed:
4033 case tok::kw_unsigned:
4034 case tok::kw__Complex:
4035 case tok::kw__Imaginary:
4036 case tok::kw_void:
4037 case tok::kw_char:
4038 case tok::kw_wchar_t:
4039 case tok::kw_char16_t:
4040 case tok::kw_char32_t:
4041 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004042 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004043 case tok::kw_float:
4044 case tok::kw_double:
4045 case tok::kw_bool:
4046 case tok::kw__Bool:
4047 case tok::kw__Decimal32:
4048 case tok::kw__Decimal64:
4049 case tok::kw__Decimal128:
4050 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00004051
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004052 // struct-or-union-specifier (C99) or class-specifier (C++)
4053 case tok::kw_class:
4054 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004055 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004056 case tok::kw_union:
4057 // enum-specifier
4058 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00004059
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004060 // typedef-name
4061 case tok::annot_typename:
4062 return true;
4063 }
4064}
4065
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004066/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004067/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004068bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004069 switch (Tok.getKind()) {
4070 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004071
Chris Lattner020bab92009-01-04 23:41:41 +00004072 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00004073 if (TryAltiVecVectorToken())
4074 return true;
4075 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00004076 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004077 // Annotate typenames and C++ scope specifiers. If we get one, just
4078 // recurse to handle whatever we get.
4079 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004080 return true;
4081 if (Tok.is(tok::identifier))
4082 return false;
4083 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00004084
Chris Lattner020bab92009-01-04 23:41:41 +00004085 case tok::coloncolon: // ::foo::bar
4086 if (NextToken().is(tok::kw_new) || // ::new
4087 NextToken().is(tok::kw_delete)) // ::delete
4088 return false;
4089
Chris Lattner020bab92009-01-04 23:41:41 +00004090 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004091 return true;
4092 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00004093
Chris Lattnere37e2332006-08-15 04:50:22 +00004094 // GNU attributes support.
4095 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00004096 // GNU typeof support.
4097 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004098
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004099 // type-specifiers
4100 case tok::kw_short:
4101 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004102 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004103 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004104 case tok::kw_signed:
4105 case tok::kw_unsigned:
4106 case tok::kw__Complex:
4107 case tok::kw__Imaginary:
4108 case tok::kw_void:
4109 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004110 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004111 case tok::kw_char16_t:
4112 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004113 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004114 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004115 case tok::kw_float:
4116 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004117 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004118 case tok::kw__Bool:
4119 case tok::kw__Decimal32:
4120 case tok::kw__Decimal64:
4121 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004122 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004123
Chris Lattner861a2262008-04-13 18:59:07 +00004124 // struct-or-union-specifier (C99) or class-specifier (C++)
4125 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004126 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004127 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004128 case tok::kw_union:
4129 // enum-specifier
4130 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004131
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004132 // type-qualifier
4133 case tok::kw_const:
4134 case tok::kw_volatile:
4135 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004136
John McCallea0a39e2012-11-14 00:49:39 +00004137 // Debugger support.
4138 case tok::kw___unknown_anytype:
4139
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004140 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00004141 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004142 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004143
Chris Lattner409bf7d2008-10-20 00:25:30 +00004144 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4145 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004146 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004147
Steve Naroff44ac7772008-12-25 14:16:32 +00004148 case tok::kw___cdecl:
4149 case tok::kw___stdcall:
4150 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004151 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004152 case tok::kw___w64:
4153 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004154 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004155 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004156 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004157
4158 case tok::kw___private:
4159 case tok::kw___local:
4160 case tok::kw___global:
4161 case tok::kw___constant:
4162 case tok::kw___read_only:
4163 case tok::kw___read_write:
4164 case tok::kw___write_only:
4165
Eli Friedman53339e02009-06-08 23:27:34 +00004166 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004167
Richard Smith8e1ac332013-03-28 01:55:44 +00004168 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004169 case tok::kw__Atomic:
4170 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004171 }
4172}
4173
Chris Lattneracd58a32006-08-06 17:24:14 +00004174/// isDeclarationSpecifier() - Return true if the current token is part of a
4175/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004176///
4177/// \param DisambiguatingWithExpression True to indicate that the purpose of
4178/// this check is to disambiguate between an expression and a declaration.
4179bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004180 switch (Tok.getKind()) {
4181 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004182
Chris Lattner020bab92009-01-04 23:41:41 +00004183 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00004184 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004185 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004186 return false;
John Thompson22334602010-02-05 00:12:22 +00004187 if (TryAltiVecVectorToken())
4188 return true;
4189 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00004190 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004191 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004192 // Annotate typenames and C++ scope specifiers. If we get one, just
4193 // recurse to handle whatever we get.
4194 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004195 return true;
4196 if (Tok.is(tok::identifier))
4197 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004198
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004199 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004200 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004201 // expression is permitted, then this is probably a class message send
4202 // missing the initial '['. In this case, we won't consider this to be
4203 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004204 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004205 isStartOfObjCClassMessageMissingOpenBracket())
4206 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004207
John McCall1f476a12010-02-26 08:45:28 +00004208 return isDeclarationSpecifier();
4209
Chris Lattner020bab92009-01-04 23:41:41 +00004210 case tok::coloncolon: // ::foo::bar
4211 if (NextToken().is(tok::kw_new) || // ::new
4212 NextToken().is(tok::kw_delete)) // ::delete
4213 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004214
Chris Lattner020bab92009-01-04 23:41:41 +00004215 // Annotate typenames and C++ scope specifiers. If we get one, just
4216 // recurse to handle whatever we get.
4217 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004218 return true;
4219 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004220
Chris Lattneracd58a32006-08-06 17:24:14 +00004221 // storage-class-specifier
4222 case tok::kw_typedef:
4223 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004224 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004225 case tok::kw_static:
4226 case tok::kw_auto:
4227 case tok::kw_register:
4228 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004229 case tok::kw_thread_local:
4230 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004231
Douglas Gregor26701a42011-09-09 02:06:17 +00004232 // Modules
4233 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00004234
John McCallea0a39e2012-11-14 00:49:39 +00004235 // Debugger support
4236 case tok::kw___unknown_anytype:
4237
Chris Lattneracd58a32006-08-06 17:24:14 +00004238 // type-specifiers
4239 case tok::kw_short:
4240 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004241 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004242 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00004243 case tok::kw_signed:
4244 case tok::kw_unsigned:
4245 case tok::kw__Complex:
4246 case tok::kw__Imaginary:
4247 case tok::kw_void:
4248 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004249 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004250 case tok::kw_char16_t:
4251 case tok::kw_char32_t:
4252
Chris Lattneracd58a32006-08-06 17:24:14 +00004253 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004254 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00004255 case tok::kw_float:
4256 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004257 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00004258 case tok::kw__Bool:
4259 case tok::kw__Decimal32:
4260 case tok::kw__Decimal64:
4261 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004262 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004263
Chris Lattner861a2262008-04-13 18:59:07 +00004264 // struct-or-union-specifier (C99) or class-specifier (C++)
4265 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00004266 case tok::kw_struct:
4267 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00004268 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00004269 // enum-specifier
4270 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004271
Chris Lattneracd58a32006-08-06 17:24:14 +00004272 // type-qualifier
4273 case tok::kw_const:
4274 case tok::kw_volatile:
4275 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00004276
Chris Lattneracd58a32006-08-06 17:24:14 +00004277 // function-specifier
4278 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00004279 case tok::kw_virtual:
4280 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00004281 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00004282
Richard Smith1dba27c2013-01-29 09:02:09 +00004283 // alignment-specifier
4284 case tok::kw__Alignas:
4285
Richard Smithd16fe122012-10-25 00:00:53 +00004286 // friend keyword.
4287 case tok::kw_friend:
4288
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00004289 // static_assert-declaration
4290 case tok::kw__Static_assert:
4291
Chris Lattner599e47e2007-08-09 17:01:07 +00004292 // GNU typeof support.
4293 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004294
Chris Lattner599e47e2007-08-09 17:01:07 +00004295 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00004296 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00004297
Richard Smithd16fe122012-10-25 00:00:53 +00004298 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00004299 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00004300 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00004301
Richard Smith8e1ac332013-03-28 01:55:44 +00004302 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004303 case tok::kw__Atomic:
4304 return true;
4305
Chris Lattner8b2ec162008-07-26 03:38:44 +00004306 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4307 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004308 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004309
Douglas Gregor19b7acf2011-04-27 05:41:15 +00004310 // typedef-name
4311 case tok::annot_typename:
4312 return !DisambiguatingWithExpression ||
4313 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00004314
Steve Narofff192fab2009-01-06 19:34:12 +00004315 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00004316 case tok::kw___cdecl:
4317 case tok::kw___stdcall:
4318 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004319 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00004320 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004321 case tok::kw___sptr:
4322 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004323 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004324 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00004325 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004326 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004327 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004328
4329 case tok::kw___private:
4330 case tok::kw___local:
4331 case tok::kw___global:
4332 case tok::kw___constant:
4333 case tok::kw___read_only:
4334 case tok::kw___read_write:
4335 case tok::kw___write_only:
4336
Eli Friedman53339e02009-06-08 23:27:34 +00004337 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00004338 }
4339}
4340
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004341bool Parser::isConstructorDeclarator() {
4342 TentativeParsingAction TPA(*this);
4343
4344 // Parse the C++ scope specifier.
4345 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00004346 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004347 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00004348 TPA.Revert();
4349 return false;
4350 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004351
4352 // Parse the constructor name.
4353 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4354 // We already know that we have a constructor name; just consume
4355 // the token.
4356 ConsumeToken();
4357 } else {
4358 TPA.Revert();
4359 return false;
4360 }
4361
Richard Smith43f340f2012-03-27 23:05:05 +00004362 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004363 if (Tok.isNot(tok::l_paren)) {
4364 TPA.Revert();
4365 return false;
4366 }
4367 ConsumeParen();
4368
Richard Smith43f340f2012-03-27 23:05:05 +00004369 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4370 // that we have a constructor.
4371 if (Tok.is(tok::r_paren) ||
4372 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004373 TPA.Revert();
4374 return true;
4375 }
4376
Richard Smithf2163662013-09-06 00:12:20 +00004377 // A C++11 attribute here signals that we have a constructor, and is an
4378 // attribute on the first constructor parameter.
4379 if (getLangOpts().CPlusPlus11 &&
4380 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
4381 /*OuterMightBeMessageSend*/ true)) {
4382 TPA.Revert();
4383 return true;
4384 }
4385
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004386 // If we need to, enter the specified scope.
4387 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004388 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004389 DeclScopeObj.EnterDeclaratorScope();
4390
Francois Pichet79f3a872011-01-31 04:54:32 +00004391 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00004392 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004393 MaybeParseMicrosoftAttributes(Attrs);
4394
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004395 // Check whether the next token(s) are part of a declaration
4396 // specifier, in which case we have the start of a parameter and,
4397 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004398 bool IsConstructor = false;
4399 if (isDeclarationSpecifier())
4400 IsConstructor = true;
4401 else if (Tok.is(tok::identifier) ||
4402 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4403 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4404 // This might be a parenthesized member name, but is more likely to
4405 // be a constructor declaration with an invalid argument type. Keep
4406 // looking.
4407 if (Tok.is(tok::annot_cxxscope))
4408 ConsumeToken();
4409 ConsumeToken();
4410
4411 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004412 // which must have one of the following syntactic forms (see the
4413 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004414 switch (Tok.getKind()) {
4415 case tok::l_paren:
4416 // C(X ( int));
4417 case tok::l_square:
4418 // C(X [ 5]);
4419 // C(X [ [attribute]]);
4420 case tok::coloncolon:
4421 // C(X :: Y);
4422 // C(X :: *p);
4423 case tok::r_paren:
4424 // C(X )
4425 // Assume this isn't a constructor, rather than assuming it's a
4426 // constructor with an unnamed parameter of an ill-formed type.
4427 break;
4428
4429 default:
4430 IsConstructor = true;
4431 break;
4432 }
4433 }
4434
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004435 TPA.Revert();
4436 return IsConstructor;
4437}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004438
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004439/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004440/// type-qualifier-list: [C99 6.7.5]
4441/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004442/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004443/// [ only if VendorAttributesAllowed=true ]
4444/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004445/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004446/// [ only if VendorAttributesAllowed=true ]
4447/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004448/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004449/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004450///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004451void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4452 bool VendorAttributesAllowed,
Richard Smith8e1ac332013-03-28 01:55:44 +00004453 bool CXX11AttributesAllowed,
Alp Toker62c5b572013-11-26 01:30:10 +00004454 bool AtomicAllowed,
4455 bool IdentifierRequired) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004456 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004457 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004458 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004459 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004460 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004461 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004462
4463 SourceLocation EndLoc;
4464
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004465 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004466 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004467 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004468 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004469 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004470
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004471 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004472 case tok::code_completion:
4473 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004474 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004475
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004476 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004477 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004478 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004479 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004480 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004481 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004482 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004483 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004484 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004485 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004486 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004487 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00004488 case tok::kw__Atomic:
4489 if (!AtomicAllowed)
4490 goto DoneWithTypeQuals;
4491 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4492 getLangOpts());
4493 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004494
4495 // OpenCL qualifiers:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004496 case tok::kw___private:
4497 case tok::kw___global:
4498 case tok::kw___local:
4499 case tok::kw___constant:
4500 case tok::kw___read_only:
4501 case tok::kw___write_only:
4502 case tok::kw___read_write:
4503 ParseOpenCLQualifiers(DS);
4504 break;
4505
Aaron Ballman317a77f2013-05-22 23:25:32 +00004506 case tok::kw___uptr:
Alp Toker62c5b572013-11-26 01:30:10 +00004507 // GNU libc headers in C mode use '__uptr' as an identifer which conflicts
4508 // with the MS modifier keyword.
4509 if (VendorAttributesAllowed && !getLangOpts().CPlusPlus &&
Alp Toker47642d22013-12-03 06:13:01 +00004510 IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) {
4511 if (TryKeywordIdentFallback(false))
4512 continue;
Alp Toker62c5b572013-11-26 01:30:10 +00004513 }
4514 case tok::kw___sptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004515 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004516 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004517 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004518 case tok::kw___cdecl:
4519 case tok::kw___stdcall:
4520 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004521 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004522 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004523 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004524 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004525 continue;
4526 }
4527 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004528 case tok::kw___pascal:
4529 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004530 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004531 continue;
4532 }
4533 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004534 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004535 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004536 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004537 continue; // do *not* consume the next token!
4538 }
4539 // otherwise, FALL THROUGH!
4540 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004541 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004542 // If this is not a type-qualifier token, we're done reading type
4543 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004544 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004545 if (EndLoc.isValid())
4546 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004547 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004548 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004549
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004550 // If the specifier combination wasn't legal, issue a diagnostic.
4551 if (isInvalid) {
4552 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004553 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004554 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004555 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004556 }
4557}
4558
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004559
4560/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4561///
4562void Parser::ParseDeclarator(Declarator &D) {
4563 /// This implements the 'declarator' production in the C grammar, then checks
4564 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004565 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004566}
4567
Richard Smith0efa75c2012-03-29 01:16:42 +00004568static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4569 if (Kind == tok::star || Kind == tok::caret)
4570 return true;
4571
4572 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4573 if (!Lang.CPlusPlus)
4574 return false;
4575
4576 return Kind == tok::amp || Kind == tok::ampamp;
4577}
4578
Sebastian Redlbd150f42008-11-21 19:14:01 +00004579/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4580/// is parsed by the function passed to it. Pass null, and the direct-declarator
4581/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004582/// ptr-operator production.
4583///
Richard Smith09f76ee2011-10-19 21:33:05 +00004584/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004585/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4586/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004587///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004588/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4589/// [C] pointer[opt] direct-declarator
4590/// [C++] direct-declarator
4591/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004592///
4593/// pointer: [C99 6.7.5]
4594/// '*' type-qualifier-list[opt]
4595/// '*' type-qualifier-list[opt] pointer
4596///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004597/// ptr-operator:
4598/// '*' cv-qualifier-seq[opt]
4599/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004600/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004601/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004602/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004603/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004604void Parser::ParseDeclaratorInternal(Declarator &D,
4605 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004606 if (Diags.hasAllExtensionsSilenced())
4607 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004608
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004609 // C++ member pointers start with a '::' or a nested-name.
4610 // Member pointers get special handling, since there's no place for the
4611 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004612 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004613 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4614 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004615 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4616 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004617 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004618 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004619
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004620 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004621 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004622 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004623 if (D.mayHaveIdentifier())
4624 D.getCXXScopeSpec() = SS;
4625 else
4626 AnnotateScopeToken(SS, true);
4627
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004628 if (DirectDeclParser)
4629 (this->*DirectDeclParser)(D);
4630 return;
4631 }
4632
4633 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004634 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004635 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004636 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004637 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004638
4639 // Recurse to parse whatever is left.
4640 ParseDeclaratorInternal(D, DirectDeclParser);
4641
4642 // Sema will have to catch (syntactically invalid) pointers into global
4643 // scope. It has to catch pointers into namespace scope anyway.
4644 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004645 Loc),
4646 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004647 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004648 return;
4649 }
4650 }
4651
4652 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004653 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004654 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004655 if (DirectDeclParser)
4656 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004657 return;
4658 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004659
Sebastian Redled0f3b02009-03-15 22:02:01 +00004660 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4661 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004662 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004663 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004664
Chris Lattner9eac9312009-03-27 04:18:06 +00004665 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004666 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004667 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004668
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004669 // FIXME: GNU attributes are not allowed here in a new-type-id.
Alp Toker62c5b572013-11-26 01:30:10 +00004670 ParseTypeQualifierListOpt(DS, true, true, true, !D.mayOmitIdentifier());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004671 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004672
Bill Wendling3708c182007-05-27 10:15:43 +00004673 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004674 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004675 if (Kind == tok::star)
4676 // Remember that we parsed a pointer type, and remember the type-quals.
4677 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004678 DS.getConstSpecLoc(),
4679 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004680 DS.getRestrictSpecLoc()),
4681 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004682 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004683 else
4684 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004685 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004686 Loc),
4687 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004688 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004689 } else {
4690 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004691 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004692
Sebastian Redl3b27be62009-03-23 00:00:23 +00004693 // Complain about rvalue references in C++03, but then go on and build
4694 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004695 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004696 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004697 diag::warn_cxx98_compat_rvalue_reference :
4698 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004699
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004700 // GNU-style and C++11 attributes are allowed here, as is restrict.
4701 ParseTypeQualifierListOpt(DS);
4702 D.ExtendWithDeclSpec(DS);
4703
Bill Wendling93efb222007-06-02 23:28:54 +00004704 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4705 // cv-qualifiers are introduced through the use of a typedef or of a
4706 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004707 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4708 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4709 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004710 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004711 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4712 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004713 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00004714 // 'restrict' is permitted as an extension.
4715 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4716 Diag(DS.getAtomicSpecLoc(),
4717 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00004718 }
Bill Wendling3708c182007-05-27 10:15:43 +00004719
4720 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004721 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004722
Douglas Gregor66583c52008-11-03 15:51:28 +00004723 if (D.getNumTypeObjects() > 0) {
4724 // C++ [dcl.ref]p4: There shall be no references to references.
4725 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4726 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004727 if (const IdentifierInfo *II = D.getIdentifier())
4728 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4729 << II;
4730 else
4731 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4732 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004733
Sebastian Redlbd150f42008-11-21 19:14:01 +00004734 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004735 // can go ahead and build the (technically ill-formed)
4736 // declarator: reference collapsing will take care of it.
4737 }
4738 }
4739
Richard Smith8e1ac332013-03-28 01:55:44 +00004740 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00004741 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004742 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004743 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004744 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004745 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004746}
4747
Richard Smith0efa75c2012-03-29 01:16:42 +00004748static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4749 SourceLocation EllipsisLoc) {
4750 if (EllipsisLoc.isValid()) {
4751 FixItHint Insertion;
4752 if (!D.getEllipsisLoc().isValid()) {
4753 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4754 D.setEllipsisLoc(EllipsisLoc);
4755 }
4756 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4757 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4758 }
4759}
4760
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004761/// ParseDirectDeclarator
4762/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004763/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004764/// '(' declarator ')'
4765/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004766/// [C90] direct-declarator '[' constant-expression[opt] ']'
4767/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4768/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4769/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4770/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004771/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4772/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004773/// direct-declarator '(' parameter-type-list ')'
4774/// direct-declarator '(' identifier-list[opt] ')'
4775/// [GNU] direct-declarator '(' parameter-forward-declarations
4776/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004777/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4778/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004779/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4780/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4781/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004782/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004783/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004784///
4785/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004786/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004787/// '::'[opt] nested-name-specifier[opt] type-name
4788///
4789/// id-expression: [C++ 5.1]
4790/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004791/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004792///
4793/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004794/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004795/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004796/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004797/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004798/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004799///
Richard Smith1453e312012-03-27 01:42:32 +00004800/// Note, any additional constructs added here may need corresponding changes
4801/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004802void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004803 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004804
David Blaikiebbafb8a2012-03-11 07:00:24 +00004805 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004806 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004807 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004808 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4809 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004810 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004811 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004812 }
4813
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004814 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004815 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004816 // Change the declaration context for name lookup, until this function
4817 // is exited (and the declarator has been parsed).
4818 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004819 }
4820
Douglas Gregor27b4c162010-12-23 22:44:42 +00004821 // C++0x [dcl.fct]p14:
4822 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004823 // of a parameter-declaration-clause without a preceding comma. In
4824 // this case, the ellipsis is parsed as part of the
4825 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004826 // parameter pack that has not been expanded; otherwise, it is parsed
4827 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004828 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004829 !((D.getContext() == Declarator::PrototypeContext ||
Faisal Vali2b391ab2013-09-26 19:54:12 +00004830 D.getContext() == Declarator::LambdaExprParameterContext ||
Douglas Gregor27b4c162010-12-23 22:44:42 +00004831 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004832 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00004833 !D.hasGroupingParens() &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004834 !Actions.containsUnexpandedParameterPacks(D))) {
4835 SourceLocation EllipsisLoc = ConsumeToken();
4836 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4837 // The ellipsis was put in the wrong place. Recover, and explain to
4838 // the user what they should have done.
4839 ParseDeclarator(D);
4840 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4841 return;
4842 } else
4843 D.setEllipsisLoc(EllipsisLoc);
4844
4845 // The ellipsis can't be followed by a parenthesized declarator. We
4846 // check for that in ParseParenDeclarator, after we have disambiguated
4847 // the l_paren token.
4848 }
4849
Douglas Gregor7861a802009-11-03 01:35:08 +00004850 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4851 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4852 // We found something that indicates the start of an unqualified-id.
4853 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004854 bool AllowConstructorName;
4855 if (D.getDeclSpec().hasTypeSpecifier())
4856 AllowConstructorName = false;
4857 else if (D.getCXXScopeSpec().isSet())
4858 AllowConstructorName =
4859 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004860 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004861 else
4862 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4863
Abramo Bagnara7945c982012-01-27 09:46:47 +00004864 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004865 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4866 /*EnteringContext=*/true,
4867 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004868 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004869 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004870 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004871 D.getName()) ||
4872 // Once we're past the identifier, if the scope was bad, mark the
4873 // whole declarator bad.
4874 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004875 D.SetIdentifier(0, Tok.getLocation());
4876 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004877 } else {
4878 // Parsed the unqualified-id; update range information and move along.
4879 if (D.getSourceRange().getBegin().isInvalid())
4880 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4881 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004882 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004883 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004884 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004885 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004886 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004887 "There's a C++-specific check for tok::identifier above");
4888 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4889 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4890 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004891 goto PastIdentifier;
Richard Smith9ce302e2013-07-11 05:10:21 +00004892 } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
Richard Smithf39720b2013-10-13 22:12:28 +00004893 // A virt-specifier isn't treated as an identifier if it appears after a
4894 // trailing-return-type.
4895 if (D.getContext() != Declarator::TrailingReturnContext ||
4896 !isCXX11VirtSpecifier(Tok)) {
4897 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
4898 << FixItHint::CreateRemoval(Tok.getLocation());
4899 D.SetIdentifier(0, Tok.getLocation());
4900 ConsumeToken();
4901 goto PastIdentifier;
4902 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004903 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004904
Douglas Gregor7861a802009-11-03 01:35:08 +00004905 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004906 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004907 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004908 // Example: 'char (*X)' or 'int (*XX)(void)'
4909 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004910
4911 // If the declarator was parenthesized, we entered the declarator
4912 // scope when parsing the parenthesized declarator, then exited
4913 // the scope already. Re-enter the scope, if we need to.
4914 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004915 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004916 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004917 if (!D.isInvalidType() &&
4918 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004919 // Change the declaration context for name lookup, until this function
4920 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004921 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004922 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004923 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004924 // This could be something simple like "int" (in which case the declarator
4925 // portion is empty), if an abstract-declarator is allowed.
4926 D.SetIdentifier(0, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00004927
4928 // The grammar for abstract-pack-declarator does not allow grouping parens.
4929 // FIXME: Revisit this once core issue 1488 is resolved.
4930 if (D.hasEllipsis() && D.hasGroupingParens())
4931 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4932 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00004933 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004934 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004935 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004936 if (D.getContext() == Declarator::MemberContext)
4937 Diag(Tok, diag::err_expected_member_name_or_semi)
4938 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004939 else if (getLangOpts().CPlusPlus) {
4940 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4941 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
Richard Trieu2f586962013-09-05 02:31:33 +00004942 else {
4943 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
4944 if (Tok.isAtStartOfLine() && Loc.isValid())
4945 Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
4946 << getLangOpts().CPlusPlus;
4947 else
4948 Diag(Tok, diag::err_expected_unqualified_id)
4949 << getLangOpts().CPlusPlus;
4950 }
Richard Trieu9c672672013-01-26 02:31:38 +00004951 } else
Alp Tokerec543272013-12-24 09:48:30 +00004952 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_paren;
Chris Lattnereec40f92006-08-06 21:55:29 +00004953 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004954 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004955 }
Mike Stump11289f42009-09-09 15:08:12 +00004956
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004957 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004958 assert(D.isPastIdentifier() &&
4959 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004960
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004961 // Don't parse attributes unless we have parsed an unparenthesized name.
4962 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004963 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004964
Chris Lattneracd58a32006-08-06 17:24:14 +00004965 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004966 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004967 // Enter function-declaration scope, limiting any declarators to the
4968 // function prototype scope, including parameter declarators.
4969 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004970 Scope::FunctionPrototypeScope|Scope::DeclScope|
4971 (D.isFunctionDeclaratorAFunctionDeclaration()
4972 ? Scope::FunctionDeclarationScope : 0));
4973
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004974 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4975 // In such a case, check if we actually have a function declarator; if it
4976 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004977 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004978 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4979 // The name of the declarator, if any, is tentatively declared within
4980 // a possible direct initializer.
4981 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4982 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4983 TentativelyDeclaredIdentifiers.pop_back();
4984 if (!IsFunctionDecl)
4985 break;
4986 }
John McCall084e83d2011-03-24 11:26:52 +00004987 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004988 BalancedDelimiterTracker T(*this, tok::l_paren);
4989 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004990 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004991 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004992 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004993 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004994 } else {
4995 break;
4996 }
4997 }
Chad Rosierc1183952012-06-26 22:30:43 +00004998}
Chris Lattneracd58a32006-08-06 17:24:14 +00004999
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005000/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
5001/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00005002/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005003/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
5004///
5005/// direct-declarator:
5006/// '(' declarator ')'
5007/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005008/// direct-declarator '(' parameter-type-list ')'
5009/// direct-declarator '(' identifier-list[opt] ')'
5010/// [GNU] direct-declarator '(' parameter-forward-declarations
5011/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005012///
5013void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005014 BalancedDelimiterTracker T(*this, tok::l_paren);
5015 T.consumeOpen();
5016
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005017 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00005018
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005019 // Eat any attributes before we look at whether this is a grouping or function
5020 // declarator paren. If this is a grouping paren, the attribute applies to
5021 // the type being built up, for example:
5022 // int (__attribute__(()) *x)(long y)
5023 // If this ends up not being a grouping paren, the attribute applies to the
5024 // first argument, for example:
5025 // int (__attribute__(()) int x)
5026 // In either case, we need to eat any attributes to be able to determine what
5027 // sort of paren this is.
5028 //
John McCall084e83d2011-03-24 11:26:52 +00005029 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005030 bool RequiresArg = false;
5031 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00005032 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005033
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005034 // We require that the argument list (if this is a non-grouping paren) be
5035 // present even if the attribute list was empty.
5036 RequiresArg = true;
5037 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00005038
Steve Naroff44ac7772008-12-25 14:16:32 +00005039 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00005040 ParseMicrosoftTypeAttributes(attrs);
5041
Dawn Perchik335e16b2010-09-03 01:29:35 +00005042 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00005043 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00005044 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005045
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005046 // If we haven't past the identifier yet (or where the identifier would be
5047 // stored, if this is an abstract declarator), then this is probably just
5048 // grouping parens. However, if this could be an abstract-declarator, then
5049 // this could also be the start of function arguments (consider 'void()').
5050 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00005051
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005052 if (!D.mayOmitIdentifier()) {
5053 // If this can't be an abstract-declarator, this *must* be a grouping
5054 // paren, because we haven't seen the identifier yet.
5055 isGrouping = true;
5056 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00005057 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
5058 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00005059 isDeclarationSpecifier() || // 'int(int)' is a function.
5060 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005061 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
5062 // considered to be a type, not a K&R identifier-list.
5063 isGrouping = false;
5064 } else {
5065 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
5066 isGrouping = true;
5067 }
Mike Stump11289f42009-09-09 15:08:12 +00005068
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005069 // If this is a grouping paren, handle:
5070 // direct-declarator: '(' declarator ')'
5071 // direct-declarator: '(' attributes declarator ')'
5072 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00005073 SourceLocation EllipsisLoc = D.getEllipsisLoc();
5074 D.setEllipsisLoc(SourceLocation());
5075
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005076 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00005077 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00005078 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005079 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005080 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00005081 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005082 T.getCloseLocation()),
5083 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005084
5085 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00005086
5087 // An ellipsis cannot be placed outside parentheses.
5088 if (EllipsisLoc.isValid())
5089 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
5090
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005091 return;
5092 }
Mike Stump11289f42009-09-09 15:08:12 +00005093
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005094 // Okay, if this wasn't a grouping paren, it must be the start of a function
5095 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005096 // identifier (and remember where it would have been), then call into
5097 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005098 D.SetIdentifier(0, Tok.getLocation());
5099
David Blaikie15a430a2011-12-04 05:04:18 +00005100 // Enter function-declaration scope, limiting any declarators to the
5101 // function prototype scope, including parameter declarators.
5102 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00005103 Scope::FunctionPrototypeScope | Scope::DeclScope |
5104 (D.isFunctionDeclaratorAFunctionDeclaration()
5105 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00005106 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00005107 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005108}
5109
5110/// ParseFunctionDeclarator - We are after the identifier and have parsed the
5111/// declarator D up to a paren, which indicates that we are parsing function
5112/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00005113///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005114/// If FirstArgAttrs is non-null, then the caller parsed those arguments
5115/// immediately after the open paren - they should be considered to be the
5116/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005117///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005118/// If RequiresArg is true, then the first argument of the function is required
5119/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005120///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005121/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
5122/// (C++11) ref-qualifier[opt], exception-specification[opt],
5123/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
5124///
5125/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00005126/// dynamic-exception-specification
5127/// noexcept-specification
5128///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005129void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005130 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005131 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00005132 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005133 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00005134 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00005135 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00005136 // lparen is already consumed!
5137 assert(D.isPastIdentifier() && "Should not call before identifier!");
5138
5139 // This should be true when the function has typed arguments.
5140 // Otherwise, it is treated as a K&R-style function.
5141 bool HasProto = false;
5142 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005143 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005144 // Remember where we see an ellipsis, if any.
5145 SourceLocation EllipsisLoc;
5146
5147 DeclSpec DS(AttrFactory);
5148 bool RefQualifierIsLValueRef = true;
5149 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00005150 SourceLocation ConstQualifierLoc;
5151 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005152 ExceptionSpecificationType ESpecType = EST_None;
5153 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005154 SmallVector<ParsedType, 2> DynamicExceptions;
5155 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005156 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005157 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00005158 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005159
James Molloy6f8780b2012-02-29 10:24:19 +00005160 Actions.ActOnStartFunctionDeclarator();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005161 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
5162 EndLoc is the end location for the function declarator.
5163 They differ for trailing return types. */
5164 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005165 SourceLocation LParenLoc, RParenLoc;
5166 LParenLoc = Tracker.getOpenLocation();
5167 StartLoc = LParenLoc;
5168
Douglas Gregor9e66af42011-07-05 16:44:18 +00005169 if (isFunctionDeclaratorIdentifierList()) {
5170 if (RequiresArg)
5171 Diag(Tok, diag::err_argument_required_after_attribute);
5172
5173 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5174
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005175 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005176 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005177 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005178 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005179 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005180 if (Tok.isNot(tok::r_paren))
Faisal Vali2b391ab2013-09-26 19:54:12 +00005181 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
5182 EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005183 else if (RequiresArg)
5184 Diag(Tok, diag::err_argument_required_after_attribute);
5185
David Blaikiebbafb8a2012-03-11 07:00:24 +00005186 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005187
5188 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005189 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005190 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005191 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005192 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005193
David Blaikiebbafb8a2012-03-11 07:00:24 +00005194 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005195 // FIXME: Accept these components in any order, and produce fixits to
5196 // correct the order if the user gets it wrong. Ideally we should deal
5197 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005198
5199 // Parse cv-qualifier-seq[opt].
Richard Smith8e1ac332013-03-28 01:55:44 +00005200 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
5201 /*CXX11AttributesAllowed*/ false,
5202 /*AtomicAllowed*/ false);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005203 if (!DS.getSourceRange().getEnd().isInvalid()) {
5204 EndLoc = DS.getSourceRange().getEnd();
5205 ConstQualifierLoc = DS.getConstSpecLoc();
5206 VolatileQualifierLoc = DS.getVolatileSpecLoc();
5207 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005208
5209 // Parse ref-qualifier[opt].
5210 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005211 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005212 diag::warn_cxx98_compat_ref_qualifier :
5213 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005214
Douglas Gregor9e66af42011-07-05 16:44:18 +00005215 RefQualifierIsLValueRef = Tok.is(tok::amp);
5216 RefQualifierLoc = ConsumeToken();
5217 EndLoc = RefQualifierLoc;
5218 }
5219
Douglas Gregor3024f072012-04-16 07:05:22 +00005220 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00005221 // If a declaration declares a member function or member function
5222 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00005223 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00005224 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00005225 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00005226 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00005227 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005228 getLangOpts().CPlusPlus11 &&
Richard Smithad1bbb92013-03-15 00:41:52 +00005229 (D.getContext() == Declarator::MemberContext
5230 ? !D.getDeclSpec().isFriendSpecified()
5231 : D.getContext() == Declarator::FileContext &&
5232 D.getCXXScopeSpec().isValid() &&
5233 Actions.CurContext->isRecord());
Douglas Gregor3024f072012-04-16 07:05:22 +00005234 Sema::CXXThisScopeRAII ThisScope(Actions,
5235 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00005236 DS.getTypeQualifiers() |
Richard Smith034185c2013-04-21 01:08:50 +00005237 (D.getDeclSpec().isConstexprSpecified() &&
5238 !getLangOpts().CPlusPlus1y
Richard Smith01141a92013-01-14 01:55:13 +00005239 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00005240 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00005241
Douglas Gregor9e66af42011-07-05 16:44:18 +00005242 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00005243 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00005244 DynamicExceptions,
5245 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00005246 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005247 if (ESpecType != EST_None)
5248 EndLoc = ESpecRange.getEnd();
5249
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005250 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5251 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00005252 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005253
Douglas Gregor9e66af42011-07-05 16:44:18 +00005254 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005255 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005256 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00005257 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005258 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5259 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005260 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00005261 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00005262 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005263 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005264 }
5265 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005266 }
5267
5268 // Remember that we parsed a function type, and remember the attributes.
5269 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005270 IsAmbiguous,
5271 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005272 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005273 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005274 DS.getTypeQualifiers(),
5275 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00005276 RefQualifierLoc, ConstQualifierLoc,
5277 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00005278 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00005279 ESpecType, ESpecRange.getBegin(),
5280 DynamicExceptions.data(),
5281 DynamicExceptionRanges.data(),
5282 DynamicExceptions.size(),
5283 NoexceptExpr.isUsable() ?
5284 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005285 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005286 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005287 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00005288
5289 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005290}
5291
5292/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5293/// identifier list form for a K&R-style function: void foo(a,b,c)
5294///
5295/// Note that identifier-lists are only allowed for normal declarators, not for
5296/// abstract-declarators.
5297bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005298 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00005299 && Tok.is(tok::identifier)
5300 && !TryAltiVecVectorToken()
5301 // K&R identifier lists can't have typedefs as identifiers, per C99
5302 // 6.7.5.3p11.
5303 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5304 // Identifier lists follow a really simple grammar: the identifiers can
5305 // be followed *only* by a ", identifier" or ")". However, K&R
5306 // identifier lists are really rare in the brave new modern world, and
5307 // it is very common for someone to typo a type in a non-K&R style
5308 // list. If we are presented with something like: "void foo(intptr x,
5309 // float y)", we don't want to start parsing the function declarator as
5310 // though it is a K&R style declarator just because intptr is an
5311 // invalid type.
5312 //
5313 // To handle this, we check to see if the token after the first
5314 // identifier is a "," or ")". Only then do we parse it as an
5315 // identifier list.
5316 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5317}
5318
5319/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5320/// we found a K&R-style identifier list instead of a typed parameter list.
5321///
5322/// After returning, ParamInfo will hold the parsed parameters.
5323///
5324/// identifier-list: [C99 6.7.5]
5325/// identifier
5326/// identifier-list ',' identifier
5327///
5328void Parser::ParseFunctionDeclaratorIdentifierList(
5329 Declarator &D,
Craig Topper5603df42013-07-05 19:34:19 +00005330 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005331 // If there was no identifier specified for the declarator, either we are in
5332 // an abstract-declarator, or we are in a parameter declarator which was found
5333 // to be abstract. In abstract-declarators, identifier lists are not valid:
5334 // diagnose this.
5335 if (!D.getIdentifier())
5336 Diag(Tok, diag::ext_ident_list_in_param);
5337
5338 // Maintain an efficient lookup of params we have seen so far.
5339 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5340
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005341 do {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005342 // If this isn't an identifier, report the error and skip until ')'.
5343 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00005344 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00005345 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005346 // Forget we parsed anything.
5347 ParamInfo.clear();
5348 return;
5349 }
5350
5351 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5352
5353 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5354 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5355 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5356
5357 // Verify that the argument identifier has not already been mentioned.
5358 if (!ParamsSoFar.insert(ParmII)) {
5359 Diag(Tok, diag::err_param_redefinition) << ParmII;
5360 } else {
5361 // Remember this identifier in ParamInfo.
5362 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5363 Tok.getLocation(),
5364 0));
5365 }
5366
5367 // Eat the identifier.
5368 ConsumeToken();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005369 // The list continues if we see a comma.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005370 } while (TryConsumeToken(tok::comma));
Douglas Gregor9e66af42011-07-05 16:44:18 +00005371}
5372
5373/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5374/// after the opening parenthesis. This function will not parse a K&R-style
5375/// identifier list.
5376///
Richard Smith2620cd92012-04-11 04:01:28 +00005377/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5378/// caller parsed those arguments immediately after the open paren - they should
5379/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005380///
5381/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5382/// be the location of the ellipsis, if any was parsed.
5383///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005384/// parameter-type-list: [C99 6.7.5]
5385/// parameter-list
5386/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005387/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005388///
5389/// parameter-list: [C99 6.7.5]
5390/// parameter-declaration
5391/// parameter-list ',' parameter-declaration
5392///
5393/// parameter-declaration: [C99 6.7.5]
5394/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005395/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00005396/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00005397/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00005398/// declaration-specifiers abstract-declarator[opt]
5399/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00005400/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00005401/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00005402/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005403///
Douglas Gregor9e66af42011-07-05 16:44:18 +00005404void Parser::ParseParameterDeclarationClause(
5405 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00005406 ParsedAttributes &FirstArgAttrs,
Craig Topper5603df42013-07-05 19:34:19 +00005407 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005408 SourceLocation &EllipsisLoc) {
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005409 do {
5410 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5411 // before deciding this was a parameter-declaration-clause.
5412 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
Chris Lattner371ed4e2008-04-06 06:57:35 +00005413 break;
Mike Stump11289f42009-09-09 15:08:12 +00005414
Chris Lattner371ed4e2008-04-06 06:57:35 +00005415 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00005416 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00005417 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005418
Richard Smith2620cd92012-04-11 04:01:28 +00005419 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00005420 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00005421
John McCall53fa7142010-12-24 02:08:15 +00005422 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00005423 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00005424
5425 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005426
5427 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00005428 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005429 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00005430 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5431 // too much hassle.
5432 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00005433
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005434 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005435
Faisal Vali2b391ab2013-09-26 19:54:12 +00005436
5437 // Parse the declarator. This is "PrototypeContext" or
5438 // "LambdaExprParameterContext", because we must accept either
5439 // 'declarator' or 'abstract-declarator' here.
5440 Declarator ParmDeclarator(DS,
5441 D.getContext() == Declarator::LambdaExprContext ?
5442 Declarator::LambdaExprParameterContext :
5443 Declarator::PrototypeContext);
5444 ParseDeclarator(ParmDeclarator);
Chris Lattner371ed4e2008-04-06 06:57:35 +00005445
5446 // Parse GNU attributes, if present.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005447 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump11289f42009-09-09 15:08:12 +00005448
Chris Lattner371ed4e2008-04-06 06:57:35 +00005449 // Remember this parsed parameter in ParamInfo.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005450 IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005451
Douglas Gregor4d87df52008-12-16 21:30:33 +00005452 // DefArgToks is used when the parsing of default arguments needs
5453 // to be delayed.
5454 CachedTokens *DefArgToks = 0;
5455
Chris Lattner371ed4e2008-04-06 06:57:35 +00005456 // If no parameter was specified, verify that *something* was specified,
5457 // otherwise we have a missing type and identifier.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005458 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 &&
5459 ParmDeclarator.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005460 // Completely missing, emit error.
5461 Diag(DSStart, diag::err_missing_param);
5462 } else {
5463 // Otherwise, we have something. Add it and let semantic analysis try
5464 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005465
Chris Lattner371ed4e2008-04-06 06:57:35 +00005466 // Inform the actions module about the parameter declarator, so it gets
5467 // added to the current scope.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005468 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(),
5469 ParmDeclarator);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005470 // Parse the default argument, if any. We parse the default
5471 // arguments in all dialects; the semantic analysis in
5472 // ActOnParamDefaultArgument will reject the default argument in
5473 // C.
5474 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005475 SourceLocation EqualLoc = Tok.getLocation();
5476
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005477 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005478 if (D.getContext() == Declarator::MemberContext) {
5479 // If we're inside a class definition, cache the tokens
5480 // corresponding to the default argument. We'll actually parse
5481 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005482 // FIXME: Can we use a smart pointer for Toks?
5483 DefArgToks = new CachedTokens;
5484
Richard Smith1fff95c2013-09-12 23:28:08 +00005485 if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005486 delete DefArgToks;
5487 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005488 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005489 } else {
5490 // Mark the end of the default argument so that we know when to
5491 // stop when we parse it later on.
5492 Token DefArgEnd;
5493 DefArgEnd.startToken();
5494 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5495 DefArgEnd.setLocation(Tok.getLocation());
5496 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005497 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005498 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005499 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005500 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005501 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005502 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005503
Chad Rosierc1183952012-06-26 22:30:43 +00005504 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005505 // used.
5506 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005507 Sema::PotentiallyEvaluatedIfUsed,
5508 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005509
Sebastian Redldb63af22012-03-14 15:54:00 +00005510 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005511 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005512 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005513 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005514 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005515 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005516 if (DefArgResult.isInvalid()) {
5517 Actions.ActOnParamDefaultArgumentError(Param);
Alexey Bataevee6507d2013-11-18 08:17:37 +00005518 SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor4d87df52008-12-16 21:30:33 +00005519 } else {
5520 // Inform the actions module about the default argument
5521 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005522 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005523 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005524 }
5525 }
Mike Stump11289f42009-09-09 15:08:12 +00005526
5527 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Faisal Vali2b391ab2013-09-26 19:54:12 +00005528 ParmDeclarator.getIdentifierLoc(),
5529 Param, DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005530 }
5531
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005532 if (TryConsumeToken(tok::ellipsis, EllipsisLoc) &&
5533 !getLangOpts().CPlusPlus) {
5534 // We have ellipsis without a preceding ',', which is ill-formed
5535 // in C. Complain and provide the fix.
5536 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
5537 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005538 break;
5539 }
Mike Stump11289f42009-09-09 15:08:12 +00005540
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005541 // If the next token is a comma, consume it and keep reading arguments.
5542 } while (TryConsumeToken(tok::comma));
Chris Lattner6c940e62008-04-06 06:34:08 +00005543}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005544
Chris Lattnere8074e62006-08-06 18:30:15 +00005545/// [C90] direct-declarator '[' constant-expression[opt] ']'
5546/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5547/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5548/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5549/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005550/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5551/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005552void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005553 if (CheckProhibitedCXX11Attribute())
5554 return;
5555
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005556 BalancedDelimiterTracker T(*this, tok::l_square);
5557 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005558
Chris Lattner84a11622008-12-18 07:27:21 +00005559 // C array syntax has many features, but by-far the most common is [] and [4].
5560 // This code does a fast path to handle some of the most obvious cases.
5561 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005562 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005563 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005564 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005565
Chris Lattner84a11622008-12-18 07:27:21 +00005566 // Remember that we parsed the empty array type.
John McCall084e83d2011-03-24 11:26:52 +00005567 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005568 T.getOpenLocation(),
5569 T.getCloseLocation()),
5570 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005571 return;
5572 } else if (Tok.getKind() == tok::numeric_constant &&
5573 GetLookAheadToken(1).is(tok::r_square)) {
5574 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005575 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005576 ConsumeToken();
5577
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005578 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005579 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005580 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005581
Chris Lattner84a11622008-12-18 07:27:21 +00005582 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005583 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005584 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005585 T.getOpenLocation(),
5586 T.getCloseLocation()),
5587 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005588 return;
5589 }
Mike Stump11289f42009-09-09 15:08:12 +00005590
Chris Lattnere8074e62006-08-06 18:30:15 +00005591 // If valid, this location is the position where we read the 'static' keyword.
5592 SourceLocation StaticLoc;
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005593 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00005594
Chris Lattnere8074e62006-08-06 18:30:15 +00005595 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005596 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005597 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005598 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005599
Chris Lattnere8074e62006-08-06 18:30:15 +00005600 // If we haven't already read 'static', check to see if there is one after the
5601 // type-qualifier-list.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005602 if (!StaticLoc.isValid())
5603 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00005604
Chris Lattnere8074e62006-08-06 18:30:15 +00005605 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005606 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005607 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005608
Chris Lattner521ff2b2008-04-06 05:26:30 +00005609 // Handle the case where we have '[*]' as the array size. However, a leading
5610 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005611 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005612 // infrequent, use of lookahead is not costly here.
5613 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005614 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005615
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005616 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005617 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005618 StaticLoc = SourceLocation(); // Drop the static.
5619 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005620 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005621 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005622 // Note, in C89, this production uses the constant-expr production instead
5623 // of assignment-expr. The only difference is that assignment-expr allows
5624 // things like '=' and '*='. Sema rejects these in C89 mode because they
5625 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005626
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005627 // Parse the constant-expression or assignment-expression now (depending
5628 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005629 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005630 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005631 } else {
5632 EnterExpressionEvaluationContext Unevaluated(Actions,
5633 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005634 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005635 }
Chris Lattner62591722006-08-12 18:40:58 +00005636 }
Mike Stump11289f42009-09-09 15:08:12 +00005637
Chris Lattner62591722006-08-12 18:40:58 +00005638 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005639 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005640 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005641 // If the expression was invalid, skip it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00005642 SkipUntil(tok::r_square, StopAtSemi);
Chris Lattner62591722006-08-12 18:40:58 +00005643 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005644 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005645
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005646 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005647
John McCall084e83d2011-03-24 11:26:52 +00005648 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005649 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005650
Chris Lattner84a11622008-12-18 07:27:21 +00005651 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005652 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005653 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005654 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005655 T.getOpenLocation(),
5656 T.getCloseLocation()),
5657 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005658}
5659
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005660/// [GNU] typeof-specifier:
5661/// typeof ( expressions )
5662/// typeof ( type-name )
5663/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005664///
5665void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005666 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005667 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005668 SourceLocation StartLoc = ConsumeToken();
5669
John McCalle8595032010-01-13 20:03:27 +00005670 const bool hasParens = Tok.is(tok::l_paren);
5671
Eli Friedman15681d62012-09-26 04:34:21 +00005672 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5673 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005674
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005675 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005676 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005677 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005678 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5679 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005680 if (hasParens)
5681 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005682
5683 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005684 // FIXME: Not accurate, the range gets one token more than it should.
5685 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005686 else
5687 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005688
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005689 if (isCastExpr) {
5690 if (!CastTy) {
5691 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005692 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005693 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005694
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005695 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005696 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005697 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5698 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005699 DiagID, CastTy))
5700 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005701 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005702 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005703
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005704 // If we get here, the operand to the typeof was an expresion.
5705 if (Operand.isInvalid()) {
5706 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005707 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005708 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005709
Eli Friedmane0afc982012-01-21 01:01:51 +00005710 // We might need to transform the operand if it is potentially evaluated.
5711 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5712 if (Operand.isInvalid()) {
5713 DS.SetTypeSpecError();
5714 return;
5715 }
5716
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005717 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005718 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005719 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5720 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005721 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005722 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005723}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005724
Benjamin Kramere56f3932011-12-23 17:00:35 +00005725/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005726/// _Atomic ( type-name )
5727///
5728void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00005729 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5730 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00005731
5732 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005733 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00005734 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005735 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00005736
5737 TypeResult Result = ParseTypeName();
5738 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00005739 SkipUntil(tok::r_paren, StopAtSemi);
Eli Friedman0dfb8892011-10-06 23:00:33 +00005740 return;
5741 }
5742
5743 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005744 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005745
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005746 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005747 return;
5748
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005749 DS.setTypeofParensRange(T.getRange());
5750 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005751
5752 const char *PrevSpec = 0;
5753 unsigned DiagID;
5754 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5755 DiagID, Result.release()))
5756 Diag(StartLoc, DiagID) << PrevSpec;
5757}
5758
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005759
5760/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5761/// from TryAltiVecVectorToken.
5762bool Parser::TryAltiVecVectorTokenOutOfLine() {
5763 Token Next = NextToken();
5764 switch (Next.getKind()) {
5765 default: return false;
5766 case tok::kw_short:
5767 case tok::kw_long:
5768 case tok::kw_signed:
5769 case tok::kw_unsigned:
5770 case tok::kw_void:
5771 case tok::kw_char:
5772 case tok::kw_int:
5773 case tok::kw_float:
5774 case tok::kw_double:
5775 case tok::kw_bool:
5776 case tok::kw___pixel:
5777 Tok.setKind(tok::kw___vector);
5778 return true;
5779 case tok::identifier:
5780 if (Next.getIdentifierInfo() == Ident_pixel) {
5781 Tok.setKind(tok::kw___vector);
5782 return true;
5783 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005784 if (Next.getIdentifierInfo() == Ident_bool) {
5785 Tok.setKind(tok::kw___vector);
5786 return true;
5787 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005788 return false;
5789 }
5790}
5791
5792bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5793 const char *&PrevSpec, unsigned &DiagID,
5794 bool &isInvalid) {
5795 if (Tok.getIdentifierInfo() == Ident_vector) {
5796 Token Next = NextToken();
5797 switch (Next.getKind()) {
5798 case tok::kw_short:
5799 case tok::kw_long:
5800 case tok::kw_signed:
5801 case tok::kw_unsigned:
5802 case tok::kw_void:
5803 case tok::kw_char:
5804 case tok::kw_int:
5805 case tok::kw_float:
5806 case tok::kw_double:
5807 case tok::kw_bool:
5808 case tok::kw___pixel:
5809 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5810 return true;
5811 case tok::identifier:
5812 if (Next.getIdentifierInfo() == Ident_pixel) {
5813 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5814 return true;
5815 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005816 if (Next.getIdentifierInfo() == Ident_bool) {
5817 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5818 return true;
5819 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005820 break;
5821 default:
5822 break;
5823 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005824 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005825 DS.isTypeAltiVecVector()) {
5826 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5827 return true;
Bill Schmidt99a084b2013-07-03 20:54:09 +00005828 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5829 DS.isTypeAltiVecVector()) {
5830 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID);
5831 return true;
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005832 }
5833 return false;
5834}