blob: b248d7582d8477876fc3e3e0045e993e5f164ddf [file] [log] [blame]
Hans Wennborgdcfba332015-10-06 23:40:43 +00001//===--- ParseDecl.cpp - Declaration Parsing --------------------*- C++ -*-===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Declaration portions of the Parser interfaces.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000014#include "clang/Parse/RAIIObjectsForParser.h"
Erik Verbruggen888d52a2014-01-15 09:15:43 +000015#include "clang/AST/ASTContext.h"
Chandler Carruth757fcd62014-03-04 10:05:20 +000016#include "clang/AST/DeclTemplate.h"
Jordan Rose1e879d82018-03-23 00:07:18 +000017#include "clang/AST/PrettyDeclStackTrace.h"
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000018#include "clang/Basic/AddressSpaces.h"
Aaron Ballmanfdd783a2014-03-31 18:18:43 +000019#include "clang/Basic/Attributes.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000020#include "clang/Basic/CharInfo.h"
Aaron Ballmanfdd783a2014-03-31 18:18:43 +000021#include "clang/Basic/TargetInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrain031643e2012-04-26 23:36:17 +000023#include "clang/Sema/Lookup.h"
John McCall8b0666c2010-08-20 18:27:03 +000024#include "clang/Sema/ParsedTemplate.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000025#include "clang/Sema/Scope.h"
George Burgess IVa19ea342016-11-10 20:43:52 +000026#include "llvm/ADT/Optional.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000027#include "llvm/ADT/SmallSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000028#include "llvm/ADT/SmallString.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000029#include "llvm/ADT/StringSwitch.h"
Hans Wennborgdcfba332015-10-06 23:40:43 +000030
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000031using namespace clang;
32
33//===----------------------------------------------------------------------===//
34// C99 6.7: Declarations.
35//===----------------------------------------------------------------------===//
36
Chris Lattnerf5fbd792006-08-10 23:56:11 +000037/// ParseTypeName
38/// type-name: [C99 6.7.6]
39/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000040///
41/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000042TypeResult Parser::ParseTypeName(SourceRange *Range,
Faisal Vali421b2d12017-12-29 05:41:00 +000043 DeclaratorContext Context,
Richard Smithcd1c0552011-07-01 19:46:12 +000044 AccessSpecifier AS,
Richard Smith54ecd982013-02-20 19:22:51 +000045 Decl **OwnedType,
46 ParsedAttributes *Attrs) {
Richard Smith62dad822012-03-15 01:02:11 +000047 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Faisal Vali7db85c52017-12-31 00:06:40 +000048 if (DSC == DeclSpecContext::DSC_normal)
49 DSC = DeclSpecContext::DSC_type_specifier;
Richard Smithbfdb1082012-03-12 08:56:40 +000050
Chris Lattnerf5fbd792006-08-10 23:56:11 +000051 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000052 DeclSpec DS(AttrFactory);
Richard Smith54ecd982013-02-20 19:22:51 +000053 if (Attrs)
Erich Keanec480f302018-07-12 21:09:05 +000054 DS.addAttributes(*Attrs);
Richard Smithbfdb1082012-03-12 08:56:40 +000055 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithcd1c0552011-07-01 19:46:12 +000056 if (OwnedType)
Craig Topper161e4db2014-05-21 06:02:52 +000057 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : nullptr;
Sebastian Redld6434562009-05-29 18:02:33 +000058
Chris Lattnerf5fbd792006-08-10 23:56:11 +000059 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000060 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000061 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000062 if (Range)
63 *Range = DeclaratorInfo.getSourceRange();
64
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000065 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000066 return true;
67
Douglas Gregor0be31a22010-07-02 17:43:08 +000068 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000069}
70
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000071/// Normalizes an attribute name by dropping prefixed and suffixed __.
George Burgess IV779af822017-06-30 22:33:24 +000072static StringRef normalizeAttrName(StringRef Name) {
73 if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__"))
74 return Name.drop_front(2).drop_back(2);
75 return Name;
76}
77
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000078/// isAttributeLateParsed - Return true if the attribute has arguments that
79/// require late parsing.
80static bool isAttributeLateParsed(const IdentifierInfo &II) {
Aaron Ballman35db2b32014-01-29 22:13:45 +000081#define CLANG_ATTR_LATE_PARSED_LIST
George Burgess IV779af822017-06-30 22:33:24 +000082 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Aaron Ballman35db2b32014-01-29 22:13:45 +000083#include "clang/Parse/AttrParserStringSwitches.inc"
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000084 .Default(false);
Aaron Ballman35db2b32014-01-29 22:13:45 +000085#undef CLANG_ATTR_LATE_PARSED_LIST
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000086}
87
Leonard Chanc72aaf62019-05-07 03:20:17 +000088/// Check if the a start and end source location expand to the same macro.
Benjamin Kramerdc5f8052019-08-23 19:59:23 +000089static bool FindLocsWithCommonFileID(Preprocessor &PP, SourceLocation StartLoc,
90 SourceLocation EndLoc) {
Leonard Chanc72aaf62019-05-07 03:20:17 +000091 if (!StartLoc.isMacroID() || !EndLoc.isMacroID())
92 return false;
93
94 SourceManager &SM = PP.getSourceManager();
95 if (SM.getFileID(StartLoc) != SM.getFileID(EndLoc))
96 return false;
97
98 bool AttrStartIsInMacro =
99 Lexer::isAtStartOfMacroExpansion(StartLoc, SM, PP.getLangOpts());
100 bool AttrEndIsInMacro =
101 Lexer::isAtEndOfMacroExpansion(EndLoc, SM, PP.getLangOpts());
102 return AttrStartIsInMacro && AttrEndIsInMacro;
103}
104
Alexis Hunt96d5c762009-11-21 08:43:09 +0000105/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000106///
107/// [GNU] attributes:
108/// attribute
109/// attributes attribute
110///
111/// [GNU] attribute:
112/// '__attribute__' '(' '(' attribute-list ')' ')'
113///
114/// [GNU] attribute-list:
115/// attrib
116/// attribute_list ',' attrib
117///
118/// [GNU] attrib:
119/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +0000120/// attrib-name
121/// attrib-name '(' identifier ')'
122/// attrib-name '(' identifier ',' nonempty-expr-list ')'
123/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000124///
Steve Naroff0f2fe172007-06-01 17:11:19 +0000125/// [GNU] attrib-name:
126/// identifier
127/// typespec
128/// typequal
129/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +0000130///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000131/// Whether an attribute takes an 'identifier' is determined by the
132/// attrib-name. GCC's behavior here is not worth imitating:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000133///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000134/// * In C mode, if the attribute argument list starts with an identifier
135/// followed by a ',' or an ')', and the identifier doesn't resolve to
136/// a type, it is parsed as an identifier. If the attribute actually
137/// wanted an expression, it's out of luck (but it turns out that no
138/// attributes work that way, because C constant expressions are very
139/// limited).
140/// * In C++ mode, if the attribute argument list starts with an identifier,
141/// and the attribute *wants* an identifier, it is parsed as an identifier.
142/// At block scope, any additional tokens between the identifier and the
143/// ',' or ')' are ignored, otherwise they produce a parse error.
Richard Smithb12bf692011-10-17 21:20:17 +0000144///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000145/// We follow the C++ model, but don't allow junk after the identifier.
John McCall53fa7142010-12-24 02:08:15 +0000146void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000147 SourceLocation *endLoc,
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000148 LateParsedAttrList *LateAttrs,
149 Declarator *D) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000150 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000151
Chris Lattner76c72282007-10-09 17:33:22 +0000152 while (Tok.is(tok::kw___attribute)) {
Leonard Chanc72aaf62019-05-07 03:20:17 +0000153 SourceLocation AttrTokLoc = ConsumeToken();
154 unsigned OldNumAttrs = attrs.size();
155 unsigned OldNumLateAttrs = LateAttrs ? LateAttrs->size() : 0;
156
Steve Naroff0f2fe172007-06-01 17:11:19 +0000157 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
158 "attribute")) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000159 SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000160 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000161 }
162 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000163 SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000164 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000165 }
166 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Aaron Ballman7a899092019-06-18 12:57:05 +0000167 do {
168 // Eat preceeding commas to allow __attribute__((,,,foo))
169 while (TryConsumeToken(tok::comma))
170 ;
Alp Toker094e5212014-01-05 03:27:11 +0000171
172 // Expect an identifier or declaration specifier (const, int, etc.)
David Majnemer22fe7712015-01-03 19:41:00 +0000173 if (Tok.isAnnotation())
174 break;
175 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
176 if (!AttrName)
Alp Toker094e5212014-01-05 03:27:11 +0000177 break;
178
Steve Naroff0f2fe172007-06-01 17:11:19 +0000179 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000180
Alp Toker094e5212014-01-05 03:27:11 +0000181 if (Tok.isNot(tok::l_paren)) {
Craig Topper161e4db2014-05-21 06:02:52 +0000182 attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Erich Keanee891aa92018-07-13 15:07:47 +0000183 ParsedAttr::AS_GNU);
Alp Toker094e5212014-01-05 03:27:11 +0000184 continue;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000185 }
Alp Toker094e5212014-01-05 03:27:11 +0000186
187 // Handle "parameterized" attributes
188 if (!LateAttrs || !isAttributeLateParsed(*AttrName)) {
Craig Topper161e4db2014-05-21 06:02:52 +0000189 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc, nullptr,
Erich Keanee891aa92018-07-13 15:07:47 +0000190 SourceLocation(), ParsedAttr::AS_GNU, D);
Alp Toker094e5212014-01-05 03:27:11 +0000191 continue;
192 }
193
194 // Handle attributes with arguments that require late parsing.
195 LateParsedAttribute *LA =
196 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
197 LateAttrs->push_back(LA);
198
199 // Attributes in a class are parsed at the end of the class, along
200 // with other late-parsed declarations.
201 if (!ClassStack.empty() && !LateAttrs->parseSoon())
202 getCurrentClass().LateParsedDeclarations.push_back(LA);
203
George Burgess IVc8b95372017-01-04 22:43:01 +0000204 // Be sure ConsumeAndStoreUntil doesn't see the start l_paren, since it
205 // recursively consumes balanced parens.
206 LA->Toks.push_back(Tok);
207 ConsumeParen();
208 // Consume everything up to and including the matching right parens.
209 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, /*StopAtSemi=*/true);
Alp Toker094e5212014-01-05 03:27:11 +0000210
211 Token Eof;
212 Eof.startToken();
213 Eof.setLocation(Tok.getLocation());
214 LA->Toks.push_back(Eof);
Aaron Ballman7a899092019-06-18 12:57:05 +0000215 } while (Tok.is(tok::comma));
Alp Toker094e5212014-01-05 03:27:11 +0000216
Alp Toker383d2c42014-01-01 03:08:43 +0000217 if (ExpectAndConsume(tok::r_paren))
Alexey Bataevee6507d2013-11-18 08:17:37 +0000218 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000219 SourceLocation Loc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000220 if (ExpectAndConsume(tok::r_paren))
Alexey Bataevee6507d2013-11-18 08:17:37 +0000221 SkipUntil(tok::r_paren, StopAtSemi);
John McCall53fa7142010-12-24 02:08:15 +0000222 if (endLoc)
223 *endLoc = Loc;
Leonard Chanc72aaf62019-05-07 03:20:17 +0000224
225 // If this was declared in a macro, attach the macro IdentifierInfo to the
226 // parsed attribute.
Leonard Chan69aec052019-05-12 21:50:01 +0000227 auto &SM = PP.getSourceManager();
228 if (!SM.isWrittenInBuiltinFile(SM.getSpellingLoc(AttrTokLoc)) &&
229 FindLocsWithCommonFileID(PP, AttrTokLoc, Loc)) {
Leonard Chanc72aaf62019-05-07 03:20:17 +0000230 CharSourceRange ExpansionRange = SM.getExpansionRange(AttrTokLoc);
231 StringRef FoundName =
232 Lexer::getSourceText(ExpansionRange, SM, PP.getLangOpts());
233 IdentifierInfo *MacroII = PP.getIdentifierInfo(FoundName);
234
235 for (unsigned i = OldNumAttrs; i < attrs.size(); ++i)
236 attrs[i].setMacroIdentifier(MacroII, ExpansionRange.getBegin());
237
238 if (LateAttrs) {
239 for (unsigned i = OldNumLateAttrs; i < LateAttrs->size(); ++i)
240 (*LateAttrs)[i]->MacroII = MacroII;
241 }
242 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000243 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000244}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000245
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000246/// Determine whether the given attribute has an identifier argument.
Aaron Ballman4768b312013-11-04 12:55:56 +0000247static bool attributeHasIdentifierArg(const IdentifierInfo &II) {
Aaron Ballman35db2b32014-01-29 22:13:45 +0000248#define CLANG_ATTR_IDENTIFIER_ARG_LIST
Aaron Ballman4768b312013-11-04 12:55:56 +0000249 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Aaron Ballman35db2b32014-01-29 22:13:45 +0000250#include "clang/Parse/AttrParserStringSwitches.inc"
Douglas Gregord2472d42013-05-02 23:25:32 +0000251 .Default(false);
Aaron Ballman35db2b32014-01-29 22:13:45 +0000252#undef CLANG_ATTR_IDENTIFIER_ARG_LIST
Douglas Gregord2472d42013-05-02 23:25:32 +0000253}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000254
Erich Keane3efe0022018-07-20 14:13:28 +0000255/// Determine whether the given attribute has a variadic identifier argument.
256static bool attributeHasVariadicIdentifierArg(const IdentifierInfo &II) {
257#define CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST
258 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
259#include "clang/Parse/AttrParserStringSwitches.inc"
260 .Default(false);
261#undef CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST
262}
263
Johannes Doerfertac991bb2019-01-19 05:36:54 +0000264/// Determine whether the given attribute treats kw_this as an identifier.
265static bool attributeTreatsKeywordThisAsIdentifier(const IdentifierInfo &II) {
266#define CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST
267 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
268#include "clang/Parse/AttrParserStringSwitches.inc"
269 .Default(false);
270#undef CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST
271}
272
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000273/// Determine whether the given attribute parses a type argument.
Aaron Ballman4768b312013-11-04 12:55:56 +0000274static bool attributeIsTypeArgAttr(const IdentifierInfo &II) {
Aaron Ballman35db2b32014-01-29 22:13:45 +0000275#define CLANG_ATTR_TYPE_ARG_LIST
Aaron Ballman4768b312013-11-04 12:55:56 +0000276 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Aaron Ballman35db2b32014-01-29 22:13:45 +0000277#include "clang/Parse/AttrParserStringSwitches.inc"
Aaron Ballman4768b312013-11-04 12:55:56 +0000278 .Default(false);
Aaron Ballman35db2b32014-01-29 22:13:45 +0000279#undef CLANG_ATTR_TYPE_ARG_LIST
Aaron Ballman4768b312013-11-04 12:55:56 +0000280}
281
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000282/// Determine whether the given attribute requires parsing its arguments
Aaron Ballman15b27b92014-01-09 19:39:35 +0000283/// in an unevaluated context or not.
284static bool attributeParsedArgsUnevaluated(const IdentifierInfo &II) {
Aaron Ballman35db2b32014-01-29 22:13:45 +0000285#define CLANG_ATTR_ARG_CONTEXT_LIST
Aaron Ballman15b27b92014-01-09 19:39:35 +0000286 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Aaron Ballman35db2b32014-01-29 22:13:45 +0000287#include "clang/Parse/AttrParserStringSwitches.inc"
Aaron Ballman15b27b92014-01-09 19:39:35 +0000288 .Default(false);
Aaron Ballman35db2b32014-01-29 22:13:45 +0000289#undef CLANG_ATTR_ARG_CONTEXT_LIST
Aaron Ballman15b27b92014-01-09 19:39:35 +0000290}
291
Richard Smithfeefaf52013-09-03 18:01:40 +0000292IdentifierLoc *Parser::ParseIdentifierLoc() {
293 assert(Tok.is(tok::identifier) && "expected an identifier");
294 IdentifierLoc *IL = IdentifierLoc::create(Actions.Context,
295 Tok.getLocation(),
296 Tok.getIdentifierInfo());
297 ConsumeToken();
298 return IL;
299}
300
Richard Smithb1f9a282013-10-31 01:56:18 +0000301void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName,
302 SourceLocation AttrNameLoc,
303 ParsedAttributes &Attrs,
Aaron Ballman80f1529c2014-07-16 20:21:50 +0000304 SourceLocation *EndLoc,
305 IdentifierInfo *ScopeName,
306 SourceLocation ScopeLoc,
Erich Keanee891aa92018-07-13 15:07:47 +0000307 ParsedAttr::Syntax Syntax) {
Richard Smithb1f9a282013-10-31 01:56:18 +0000308 BalancedDelimiterTracker Parens(*this, tok::l_paren);
309 Parens.consumeOpen();
310
311 TypeResult T;
312 if (Tok.isNot(tok::r_paren))
313 T = ParseTypeName();
314
315 if (Parens.consumeClose())
316 return;
317
318 if (T.isInvalid())
319 return;
320
321 if (T.isUsable())
322 Attrs.addNewTypeAttr(&AttrName,
Craig Topper161e4db2014-05-21 06:02:52 +0000323 SourceRange(AttrNameLoc, Parens.getCloseLocation()),
Aaron Ballman80f1529c2014-07-16 20:21:50 +0000324 ScopeName, ScopeLoc, T.get(), Syntax);
Richard Smithb1f9a282013-10-31 01:56:18 +0000325 else
326 Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()),
Aaron Ballman80f1529c2014-07-16 20:21:50 +0000327 ScopeName, ScopeLoc, nullptr, 0, Syntax);
Richard Smithb1f9a282013-10-31 01:56:18 +0000328}
329
Aaron Ballman35f94212014-04-14 16:03:22 +0000330unsigned Parser::ParseAttributeArgsCommon(
Aaron Ballmanb8e20392014-03-31 17:32:39 +0000331 IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
332 ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
Erich Keanee891aa92018-07-13 15:07:47 +0000333 SourceLocation ScopeLoc, ParsedAttr::Syntax Syntax) {
Aaron Ballmanb8e20392014-03-31 17:32:39 +0000334 // Ignore the left paren location for now.
335 ConsumeParen();
336
Johannes Doerfertac991bb2019-01-19 05:36:54 +0000337 bool ChangeKWThisToIdent = attributeTreatsKeywordThisAsIdentifier(*AttrName);
Matthias Gehred293cbd2019-07-25 17:50:51 +0000338 bool AttributeIsTypeArgAttr = attributeIsTypeArgAttr(*AttrName);
Johannes Doerfertac991bb2019-01-19 05:36:54 +0000339
340 // Interpret "kw_this" as an identifier if the attributed requests it.
341 if (ChangeKWThisToIdent && Tok.is(tok::kw_this))
342 Tok.setKind(tok::identifier);
343
Aaron Ballmanb8e20392014-03-31 17:32:39 +0000344 ArgsVector ArgExprs;
345 if (Tok.is(tok::identifier)) {
346 // If this attribute wants an 'identifier' argument, make it so.
Erich Keane3efe0022018-07-20 14:13:28 +0000347 bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName) ||
348 attributeHasVariadicIdentifierArg(*AttrName);
Erich Keanee891aa92018-07-13 15:07:47 +0000349 ParsedAttr::Kind AttrKind =
Erich Keane6a24e802019-09-13 17:39:31 +0000350 ParsedAttr::getParsedKind(AttrName, ScopeName, Syntax);
Aaron Ballmanb8e20392014-03-31 17:32:39 +0000351
352 // If we don't know how to parse this attribute, but this is the only
353 // token in this argument, assume it's meant to be an identifier.
Erich Keanee891aa92018-07-13 15:07:47 +0000354 if (AttrKind == ParsedAttr::UnknownAttribute ||
355 AttrKind == ParsedAttr::IgnoredAttribute) {
Aaron Ballmanb8e20392014-03-31 17:32:39 +0000356 const Token &Next = NextToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000357 IsIdentifierArg = Next.isOneOf(tok::r_paren, tok::comma);
Aaron Ballmanb8e20392014-03-31 17:32:39 +0000358 }
359
360 if (IsIdentifierArg)
361 ArgExprs.push_back(ParseIdentifierLoc());
362 }
363
Matthias Gehred293cbd2019-07-25 17:50:51 +0000364 ParsedType TheParsedType;
Aaron Ballmanb8e20392014-03-31 17:32:39 +0000365 if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) {
366 // Eat the comma.
367 if (!ArgExprs.empty())
368 ConsumeToken();
369
370 // Parse the non-empty comma-separated list of expressions.
371 do {
Johannes Doerfertac991bb2019-01-19 05:36:54 +0000372 // Interpret "kw_this" as an identifier if the attributed requests it.
373 if (ChangeKWThisToIdent && Tok.is(tok::kw_this))
374 Tok.setKind(tok::identifier);
375
Erich Keane3efe0022018-07-20 14:13:28 +0000376 ExprResult ArgExpr;
Matthias Gehred293cbd2019-07-25 17:50:51 +0000377 if (AttributeIsTypeArgAttr) {
378 TypeResult T = ParseTypeName();
379 if (T.isInvalid()) {
380 SkipUntil(tok::r_paren, StopAtSemi);
381 return 0;
382 }
383 if (T.isUsable())
384 TheParsedType = T.get();
385 break; // FIXME: Multiple type arguments are not implemented.
386 } else if (Tok.is(tok::identifier) &&
387 attributeHasVariadicIdentifierArg(*AttrName)) {
Erich Keane3efe0022018-07-20 14:13:28 +0000388 ArgExprs.push_back(ParseIdentifierLoc());
389 } else {
390 bool Uneval = attributeParsedArgsUnevaluated(*AttrName);
391 EnterExpressionEvaluationContext Unevaluated(
392 Actions,
393 Uneval ? Sema::ExpressionEvaluationContext::Unevaluated
394 : Sema::ExpressionEvaluationContext::ConstantEvaluated);
Aaron Ballmanb8e20392014-03-31 17:32:39 +0000395
Erich Keane3efe0022018-07-20 14:13:28 +0000396 ExprResult ArgExpr(
397 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression()));
398 if (ArgExpr.isInvalid()) {
399 SkipUntil(tok::r_paren, StopAtSemi);
400 return 0;
401 }
402 ArgExprs.push_back(ArgExpr.get());
Aaron Ballmanb8e20392014-03-31 17:32:39 +0000403 }
Aaron Ballmanb8e20392014-03-31 17:32:39 +0000404 // Eat the comma, move to the next argument
405 } while (TryConsumeToken(tok::comma));
406 }
407
408 SourceLocation RParen = Tok.getLocation();
409 if (!ExpectAndConsume(tok::r_paren)) {
410 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Matthias Gehred293cbd2019-07-25 17:50:51 +0000411
412 if (AttributeIsTypeArgAttr && !TheParsedType.get().isNull()) {
413 Attrs.addNewTypeAttr(AttrName, SourceRange(AttrNameLoc, RParen),
414 ScopeName, ScopeLoc, TheParsedType, Syntax);
415 } else {
416 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
417 ArgExprs.data(), ArgExprs.size(), Syntax);
418 }
Aaron Ballmanb8e20392014-03-31 17:32:39 +0000419 }
420
421 if (EndLoc)
422 *EndLoc = RParen;
Aaron Ballman35f94212014-04-14 16:03:22 +0000423
Matthias Gehred293cbd2019-07-25 17:50:51 +0000424 return static_cast<unsigned>(ArgExprs.size() + !TheParsedType.get().isNull());
Aaron Ballmanb8e20392014-03-31 17:32:39 +0000425}
426
Michael Han23214e52012-10-03 01:56:22 +0000427/// Parse the arguments to a parameterized GNU attribute or
428/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000429void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
430 SourceLocation AttrNameLoc,
431 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000432 SourceLocation *EndLoc,
433 IdentifierInfo *ScopeName,
434 SourceLocation ScopeLoc,
Erich Keanee891aa92018-07-13 15:07:47 +0000435 ParsedAttr::Syntax Syntax,
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000436 Declarator *D) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000437
438 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
439
Erich Keanee891aa92018-07-13 15:07:47 +0000440 ParsedAttr::Kind AttrKind =
Erich Keane6a24e802019-09-13 17:39:31 +0000441 ParsedAttr::getParsedKind(AttrName, ScopeName, Syntax);
Richard Smith66e71682013-10-24 01:07:54 +0000442
Erich Keanee891aa92018-07-13 15:07:47 +0000443 if (AttrKind == ParsedAttr::AT_Availability) {
Aaron Ballman80f1529c2014-07-16 20:21:50 +0000444 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
445 ScopeLoc, Syntax);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000446 return;
Erich Keanee891aa92018-07-13 15:07:47 +0000447 } else if (AttrKind == ParsedAttr::AT_ExternalSourceSymbol) {
Alex Lorenzd5d27e12017-03-01 18:06:25 +0000448 ParseExternalSourceSymbolAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
449 ScopeName, ScopeLoc, Syntax);
450 return;
Erich Keanee891aa92018-07-13 15:07:47 +0000451 } else if (AttrKind == ParsedAttr::AT_ObjCBridgeRelated) {
Aaron Ballman80f1529c2014-07-16 20:21:50 +0000452 ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
453 ScopeName, ScopeLoc, Syntax);
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000454 return;
Erich Keanee891aa92018-07-13 15:07:47 +0000455 } else if (AttrKind == ParsedAttr::AT_TypeTagForDatatype) {
Aaron Ballman80f1529c2014-07-16 20:21:50 +0000456 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
457 ScopeName, ScopeLoc, Syntax);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000458 return;
Aaron Ballman80f1529c2014-07-16 20:21:50 +0000459 } else if (attributeIsTypeArgAttr(*AttrName)) {
460 ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
461 ScopeLoc, Syntax);
Richard Smithb1f9a282013-10-31 01:56:18 +0000462 return;
463 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000464
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000465 // These may refer to the function arguments, but need to be parsed early to
466 // participate in determining whether it's a redeclaration.
George Burgess IVa19ea342016-11-10 20:43:52 +0000467 llvm::Optional<ParseScope> PrototypeScope;
Ulrich Weigandef5aa292015-07-13 14:13:01 +0000468 if (normalizeAttrName(AttrName->getName()) == "enable_if" &&
469 D && D->isFunctionDeclarator()) {
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000470 DeclaratorChunk::FunctionTypeInfo FTI = D->getFunctionTypeInfo();
George Burgess IVa19ea342016-11-10 20:43:52 +0000471 PrototypeScope.emplace(this, Scope::FunctionPrototypeScope |
472 Scope::FunctionDeclarationScope |
473 Scope::DeclScope);
Alp Tokerc5350722014-02-26 22:27:52 +0000474 for (unsigned i = 0; i != FTI.NumParams; ++i) {
475 ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param);
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000476 Actions.ActOnReenterCXXMethodParameter(getCurScope(), Param);
477 }
478 }
479
Aaron Ballmanb8e20392014-03-31 17:32:39 +0000480 ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
481 ScopeLoc, Syntax);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000482}
483
Alex Lorenzd5d27e12017-03-01 18:06:25 +0000484unsigned Parser::ParseClangAttributeArgs(
485 IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
486 ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
Erich Keanee891aa92018-07-13 15:07:47 +0000487 SourceLocation ScopeLoc, ParsedAttr::Syntax Syntax) {
Alex Lorenzd5d27e12017-03-01 18:06:25 +0000488 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
489
Erich Keanee891aa92018-07-13 15:07:47 +0000490 ParsedAttr::Kind AttrKind =
Erich Keane6a24e802019-09-13 17:39:31 +0000491 ParsedAttr::getParsedKind(AttrName, ScopeName, Syntax);
Alex Lorenzd5d27e12017-03-01 18:06:25 +0000492
Aaron Ballman38bbc162018-02-24 17:16:42 +0000493 switch (AttrKind) {
494 default:
495 return ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc,
496 ScopeName, ScopeLoc, Syntax);
Erich Keanee891aa92018-07-13 15:07:47 +0000497 case ParsedAttr::AT_ExternalSourceSymbol:
Alex Lorenzd5d27e12017-03-01 18:06:25 +0000498 ParseExternalSourceSymbolAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
499 ScopeName, ScopeLoc, Syntax);
Aaron Ballman38bbc162018-02-24 17:16:42 +0000500 break;
Erich Keanee891aa92018-07-13 15:07:47 +0000501 case ParsedAttr::AT_Availability:
Aaron Ballman38bbc162018-02-24 17:16:42 +0000502 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
503 ScopeLoc, Syntax);
504 break;
Erich Keanee891aa92018-07-13 15:07:47 +0000505 case ParsedAttr::AT_ObjCBridgeRelated:
Aaron Ballmanc248b0f2018-02-24 17:37:37 +0000506 ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
507 ScopeName, ScopeLoc, Syntax);
508 break;
Erich Keanee891aa92018-07-13 15:07:47 +0000509 case ParsedAttr::AT_TypeTagForDatatype:
Aaron Ballmana26d8ee2018-02-25 14:01:04 +0000510 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
511 ScopeName, ScopeLoc, Syntax);
512 break;
Alex Lorenzd5d27e12017-03-01 18:06:25 +0000513 }
Erich Keanec480f302018-07-12 21:09:05 +0000514 return !Attrs.empty() ? Attrs.begin()->getNumArgs() : 0;
Alex Lorenzd5d27e12017-03-01 18:06:25 +0000515}
516
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000517bool Parser::ParseMicrosoftDeclSpecArgs(IdentifierInfo *AttrName,
518 SourceLocation AttrNameLoc,
519 ParsedAttributes &Attrs) {
520 // If the attribute isn't known, we will not attempt to parse any
521 // arguments.
522 if (!hasAttribute(AttrSyntax::Declspec, nullptr, AttrName,
Bob Wilson7c730832015-07-20 22:57:31 +0000523 getTargetInfo(), getLangOpts())) {
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000524 // Eat the left paren, then skip to the ending right paren.
525 ConsumeParen();
526 SkipUntil(tok::r_paren);
527 return false;
Aaron Ballman478faed2012-06-19 22:09:27 +0000528 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000529
Aaron Ballman95d57032014-04-14 16:44:26 +0000530 SourceLocation OpenParenLoc = Tok.getLocation();
531
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000532 if (AttrName->getName() == "property") {
Aaron Ballman478faed2012-06-19 22:09:27 +0000533 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000534 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000535 // must be named get or put.
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000536
John McCall5e77d762013-04-16 07:28:30 +0000537 BalancedDelimiterTracker T(*this, tok::l_paren);
538 T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000539 AttrName->getNameStart(), tok::r_paren);
John McCall5e77d762013-04-16 07:28:30 +0000540
541 enum AccessorKind {
542 AK_Invalid = -1,
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000543 AK_Put = 0,
544 AK_Get = 1 // indices into AccessorNames
John McCall5e77d762013-04-16 07:28:30 +0000545 };
Craig Topper161e4db2014-05-21 06:02:52 +0000546 IdentifierInfo *AccessorNames[] = {nullptr, nullptr};
John McCall5e77d762013-04-16 07:28:30 +0000547 bool HasInvalidAccessor = false;
548
549 // Parse the accessor specifications.
550 while (true) {
551 // Stop if this doesn't look like an accessor spec.
552 if (!Tok.is(tok::identifier)) {
553 // If the user wrote a completely empty list, use a special diagnostic.
554 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
Craig Topper161e4db2014-05-21 06:02:52 +0000555 AccessorNames[AK_Put] == nullptr &&
556 AccessorNames[AK_Get] == nullptr) {
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000557 Diag(AttrNameLoc, diag::err_ms_property_no_getter_or_putter);
John McCall5e77d762013-04-16 07:28:30 +0000558 break;
559 }
560
561 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
562 break;
563 }
564
565 AccessorKind Kind;
566 SourceLocation KindLoc = Tok.getLocation();
567 StringRef KindStr = Tok.getIdentifierInfo()->getName();
568 if (KindStr == "get") {
569 Kind = AK_Get;
570 } else if (KindStr == "put") {
571 Kind = AK_Put;
572
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000573 // Recover from the common mistake of using 'set' instead of 'put'.
John McCall5e77d762013-04-16 07:28:30 +0000574 } else if (KindStr == "set") {
575 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000576 << FixItHint::CreateReplacement(KindLoc, "put");
John McCall5e77d762013-04-16 07:28:30 +0000577 Kind = AK_Put;
578
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000579 // Handle the mistake of forgetting the accessor kind by skipping
580 // this accessor.
John McCall5e77d762013-04-16 07:28:30 +0000581 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
582 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
583 ConsumeToken();
584 HasInvalidAccessor = true;
585 goto next_property_accessor;
586
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000587 // Otherwise, complain about the unknown accessor kind.
John McCall5e77d762013-04-16 07:28:30 +0000588 } else {
589 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
590 HasInvalidAccessor = true;
591 Kind = AK_Invalid;
592
593 // Try to keep parsing unless it doesn't look like an accessor spec.
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000594 if (!NextToken().is(tok::equal))
595 break;
John McCall5e77d762013-04-16 07:28:30 +0000596 }
597
598 // Consume the identifier.
599 ConsumeToken();
600
601 // Consume the '='.
Alp Toker8fbec672013-12-17 23:29:36 +0000602 if (!TryConsumeToken(tok::equal)) {
John McCall5e77d762013-04-16 07:28:30 +0000603 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000604 << KindStr;
John McCall5e77d762013-04-16 07:28:30 +0000605 break;
606 }
607
608 // Expect the method name.
609 if (!Tok.is(tok::identifier)) {
610 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
611 break;
612 }
613
614 if (Kind == AK_Invalid) {
615 // Just drop invalid accessors.
Craig Topper161e4db2014-05-21 06:02:52 +0000616 } else if (AccessorNames[Kind] != nullptr) {
John McCall5e77d762013-04-16 07:28:30 +0000617 // Complain about the repeated accessor, ignore it, and keep parsing.
618 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
619 } else {
620 AccessorNames[Kind] = Tok.getIdentifierInfo();
621 }
622 ConsumeToken();
623
624 next_property_accessor:
625 // Keep processing accessors until we run out.
Alp Toker094e5212014-01-05 03:27:11 +0000626 if (TryConsumeToken(tok::comma))
John McCall5e77d762013-04-16 07:28:30 +0000627 continue;
628
629 // If we run into the ')', stop without consuming it.
Alp Toker094e5212014-01-05 03:27:11 +0000630 if (Tok.is(tok::r_paren))
John McCall5e77d762013-04-16 07:28:30 +0000631 break;
Alp Toker094e5212014-01-05 03:27:11 +0000632
633 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
634 break;
John McCall5e77d762013-04-16 07:28:30 +0000635 }
636
637 // Only add the property attribute if it was well-formed.
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000638 if (!HasInvalidAccessor)
Craig Topper161e4db2014-05-21 06:02:52 +0000639 Attrs.addNewPropertyAttr(AttrName, AttrNameLoc, nullptr, SourceLocation(),
John McCall5e77d762013-04-16 07:28:30 +0000640 AccessorNames[AK_Get], AccessorNames[AK_Put],
Erich Keanee891aa92018-07-13 15:07:47 +0000641 ParsedAttr::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000642 T.skipToEnd();
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000643 return !HasInvalidAccessor;
Aaron Ballman478faed2012-06-19 22:09:27 +0000644 }
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000645
Aaron Ballman95d57032014-04-14 16:44:26 +0000646 unsigned NumArgs =
647 ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, nullptr, nullptr,
Erich Keanee891aa92018-07-13 15:07:47 +0000648 SourceLocation(), ParsedAttr::AS_Declspec);
Aaron Ballman95d57032014-04-14 16:44:26 +0000649
650 // If this attribute's args were parsed, and it was expected to have
651 // arguments but none were provided, emit a diagnostic.
Erich Keanec480f302018-07-12 21:09:05 +0000652 if (!Attrs.empty() && Attrs.begin()->getMaxArgs() && !NumArgs) {
Aaron Ballmanef5d94c2014-04-15 00:36:39 +0000653 Diag(OpenParenLoc, diag::err_attribute_requires_arguments) << AttrName;
Aaron Ballman95d57032014-04-14 16:44:26 +0000654 return false;
655 }
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000656 return true;
Aaron Ballman478faed2012-06-19 22:09:27 +0000657}
658
Eli Friedman06de2b52009-06-08 07:21:15 +0000659/// [MS] decl-specifier:
660/// __declspec ( extended-decl-modifier-seq )
661///
662/// [MS] extended-decl-modifier-seq:
663/// extended-decl-modifier[opt]
664/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman068aa512015-05-20 20:58:33 +0000665void Parser::ParseMicrosoftDeclSpecs(ParsedAttributes &Attrs,
666 SourceLocation *End) {
Saleem Abdulrasoold170c4b2015-10-04 17:51:05 +0000667 assert(getLangOpts().DeclSpecKeyword && "__declspec keyword is not enabled");
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000668 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000669
Aaron Ballman068aa512015-05-20 20:58:33 +0000670 while (Tok.is(tok::kw___declspec)) {
671 ConsumeToken();
672 BalancedDelimiterTracker T(*this, tok::l_paren);
673 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
674 tok::r_paren))
Aaron Ballman478faed2012-06-19 22:09:27 +0000675 return;
Aaron Ballman478faed2012-06-19 22:09:27 +0000676
Aaron Ballman068aa512015-05-20 20:58:33 +0000677 // An empty declspec is perfectly legal and should not warn. Additionally,
678 // you can specify multiple attributes per declspec.
679 while (Tok.isNot(tok::r_paren)) {
680 // Attribute not present.
681 if (TryConsumeToken(tok::comma))
682 continue;
683
684 // We expect either a well-known identifier or a generic string. Anything
685 // else is a malformed declspec.
686 bool IsString = Tok.getKind() == tok::string_literal;
687 if (!IsString && Tok.getKind() != tok::identifier &&
688 Tok.getKind() != tok::kw_restrict) {
689 Diag(Tok, diag::err_ms_declspec_type);
Aaron Ballman478faed2012-06-19 22:09:27 +0000690 T.skipToEnd();
691 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000692 }
Aaron Ballman068aa512015-05-20 20:58:33 +0000693
694 IdentifierInfo *AttrName;
695 SourceLocation AttrNameLoc;
696 if (IsString) {
697 SmallString<8> StrBuffer;
698 bool Invalid = false;
699 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
700 if (Invalid) {
701 T.skipToEnd();
702 return;
703 }
704 AttrName = PP.getIdentifierInfo(Str);
705 AttrNameLoc = ConsumeStringToken();
706 } else {
707 AttrName = Tok.getIdentifierInfo();
708 AttrNameLoc = ConsumeToken();
709 }
710
711 bool AttrHandled = false;
712
713 // Parse attribute arguments.
714 if (Tok.is(tok::l_paren))
715 AttrHandled = ParseMicrosoftDeclSpecArgs(AttrName, AttrNameLoc, Attrs);
716 else if (AttrName->getName() == "property")
717 // The property attribute must have an argument list.
718 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
719 << AttrName->getName();
720
721 if (!AttrHandled)
722 Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Erich Keanee891aa92018-07-13 15:07:47 +0000723 ParsedAttr::AS_Declspec);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000724 }
Aaron Ballman068aa512015-05-20 20:58:33 +0000725 T.consumeClose();
726 if (End)
727 *End = T.getCloseLocation();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000728 }
Eli Friedman53339e02009-06-08 23:27:34 +0000729}
730
John McCall53fa7142010-12-24 02:08:15 +0000731void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000732 // Treat these like attributes
Reid Klecknerd7857f02014-10-24 17:42:17 +0000733 while (true) {
734 switch (Tok.getKind()) {
735 case tok::kw___fastcall:
736 case tok::kw___stdcall:
737 case tok::kw___thiscall:
Erich Keane757d3172016-11-02 18:29:35 +0000738 case tok::kw___regcall:
Reid Klecknerd7857f02014-10-24 17:42:17 +0000739 case tok::kw___cdecl:
740 case tok::kw___vectorcall:
741 case tok::kw___ptr64:
742 case tok::kw___w64:
743 case tok::kw___ptr32:
Reid Klecknerd7857f02014-10-24 17:42:17 +0000744 case tok::kw___sptr:
745 case tok::kw___uptr: {
746 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
747 SourceLocation AttrNameLoc = ConsumeToken();
748 attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Erich Keanee891aa92018-07-13 15:07:47 +0000749 ParsedAttr::AS_Keyword);
Reid Klecknerd7857f02014-10-24 17:42:17 +0000750 break;
751 }
752 default:
753 return;
754 }
Eli Friedman53339e02009-06-08 23:27:34 +0000755 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000756}
757
Nico Rieckeaaae272014-12-04 23:31:08 +0000758void Parser::DiagnoseAndSkipExtendedMicrosoftTypeAttributes() {
759 SourceLocation StartLoc = Tok.getLocation();
760 SourceLocation EndLoc = SkipExtendedMicrosoftTypeAttributes();
761
762 if (EndLoc.isValid()) {
763 SourceRange Range(StartLoc, EndLoc);
764 Diag(StartLoc, diag::warn_microsoft_qualifiers_ignored) << Range;
765 }
766}
767
768SourceLocation Parser::SkipExtendedMicrosoftTypeAttributes() {
769 SourceLocation EndLoc;
770
771 while (true) {
772 switch (Tok.getKind()) {
773 case tok::kw_const:
774 case tok::kw_volatile:
775 case tok::kw___fastcall:
776 case tok::kw___stdcall:
777 case tok::kw___thiscall:
778 case tok::kw___cdecl:
779 case tok::kw___vectorcall:
780 case tok::kw___ptr32:
781 case tok::kw___ptr64:
782 case tok::kw___w64:
783 case tok::kw___unaligned:
784 case tok::kw___sptr:
785 case tok::kw___uptr:
786 EndLoc = ConsumeToken();
787 break;
788 default:
789 return EndLoc;
790 }
791 }
792}
793
John McCall53fa7142010-12-24 02:08:15 +0000794void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000795 // Treat these like attributes
796 while (Tok.is(tok::kw___pascal)) {
797 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
798 SourceLocation AttrNameLoc = ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +0000799 attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Erich Keanee891aa92018-07-13 15:07:47 +0000800 ParsedAttr::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000801 }
John McCall53fa7142010-12-24 02:08:15 +0000802}
803
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +0000804void Parser::ParseOpenCLKernelAttributes(ParsedAttributes &attrs) {
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000805 // Treat these like attributes
806 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000807 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000808 SourceLocation AttrNameLoc = ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +0000809 attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Erich Keanee891aa92018-07-13 15:07:47 +0000810 ParsedAttr::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000811 }
812}
813
Aaron Ballman05d76ea2014-01-14 01:29:54 +0000814void Parser::ParseOpenCLQualifiers(ParsedAttributes &Attrs) {
815 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
816 SourceLocation AttrNameLoc = Tok.getLocation();
Craig Topper161e4db2014-05-21 06:02:52 +0000817 Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Erich Keanee891aa92018-07-13 15:07:47 +0000818 ParsedAttr::AS_Keyword);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000819}
820
Douglas Gregor261a89b2015-06-19 17:51:05 +0000821void Parser::ParseNullabilityTypeSpecifiers(ParsedAttributes &attrs) {
822 // Treat these like attributes, even though they're type specifiers.
823 while (true) {
824 switch (Tok.getKind()) {
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000825 case tok::kw__Nonnull:
826 case tok::kw__Nullable:
827 case tok::kw__Null_unspecified: {
Douglas Gregor261a89b2015-06-19 17:51:05 +0000828 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
829 SourceLocation AttrNameLoc = ConsumeToken();
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000830 if (!getLangOpts().ObjC)
Douglas Gregor261a89b2015-06-19 17:51:05 +0000831 Diag(AttrNameLoc, diag::ext_nullability)
832 << AttrName;
Fangrui Song6907ce22018-07-30 19:24:48 +0000833 attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Erich Keanee891aa92018-07-13 15:07:47 +0000834 ParsedAttr::AS_Keyword);
Douglas Gregor261a89b2015-06-19 17:51:05 +0000835 break;
836 }
837 default:
838 return;
839 }
840 }
841}
842
Fariborz Jahaniandbc956d2014-10-02 16:39:45 +0000843static bool VersionNumberSeparator(const char Separator) {
844 return (Separator == '.' || Separator == '_');
845}
846
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000847/// Parse a version number.
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000848///
849/// version:
850/// simple-integer
Jan Korous23255692018-05-17 11:51:49 +0000851/// simple-integer '.' simple-integer
852/// simple-integer '_' simple-integer
853/// simple-integer '.' simple-integer '.' simple-integer
854/// simple-integer '_' simple-integer '_' simple-integer
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000855VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
Erik Pilkington29099de2016-07-16 00:35:23 +0000856 Range = SourceRange(Tok.getLocation(), Tok.getEndLoc());
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000857
858 if (!Tok.is(tok::numeric_constant)) {
859 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000860 SkipUntil(tok::comma, tok::r_paren,
861 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000862 return VersionTuple();
863 }
864
865 // Parse the major (and possibly minor and subminor) versions, which
866 // are stored in the numeric constant. We utilize a quirk of the
867 // lexer, which is that it handles something like 1.2.3 as a single
868 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000869 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000870 Buffer.resize(Tok.getLength()+1);
871 const char *ThisTokBegin = &Buffer[0];
872
873 // Get the spelling of the token, which eliminates trigraphs, etc.
874 bool Invalid = false;
875 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
876 if (Invalid)
877 return VersionTuple();
878
879 // Parse the major version.
880 unsigned AfterMajor = 0;
881 unsigned Major = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000882 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000883 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
884 ++AfterMajor;
885 }
886
887 if (AfterMajor == 0) {
888 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000889 SkipUntil(tok::comma, tok::r_paren,
890 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000891 return VersionTuple();
892 }
893
894 if (AfterMajor == ActualLength) {
895 ConsumeToken();
896
897 // We only had a single version component.
898 if (Major == 0) {
899 Diag(Tok, diag::err_zero_version);
900 return VersionTuple();
901 }
902
903 return VersionTuple(Major);
904 }
905
Fariborz Jahaniance72e632014-10-02 17:57:26 +0000906 const char AfterMajorSeparator = ThisTokBegin[AfterMajor];
907 if (!VersionNumberSeparator(AfterMajorSeparator)
Fariborz Jahaniandbc956d2014-10-02 16:39:45 +0000908 || (AfterMajor + 1 == ActualLength)) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000909 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000910 SkipUntil(tok::comma, tok::r_paren,
911 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000912 return VersionTuple();
913 }
914
915 // Parse the minor version.
916 unsigned AfterMinor = AfterMajor + 1;
917 unsigned Minor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000918 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000919 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
920 ++AfterMinor;
921 }
922
923 if (AfterMinor == ActualLength) {
924 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000925
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000926 // We had major.minor.
927 if (Major == 0 && Minor == 0) {
928 Diag(Tok, diag::err_zero_version);
929 return VersionTuple();
930 }
931
Jan Korous23255692018-05-17 11:51:49 +0000932 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000933 }
934
Fariborz Jahaniance72e632014-10-02 17:57:26 +0000935 const char AfterMinorSeparator = ThisTokBegin[AfterMinor];
Fariborz Jahaniandbc956d2014-10-02 16:39:45 +0000936 // If what follows is not a '.' or '_', we have a problem.
Fariborz Jahaniance72e632014-10-02 17:57:26 +0000937 if (!VersionNumberSeparator(AfterMinorSeparator)) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000938 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000939 SkipUntil(tok::comma, tok::r_paren,
940 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Chad Rosierc1183952012-06-26 22:30:43 +0000941 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000942 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000943
Fariborz Jahanianb6161612014-10-03 17:21:12 +0000944 // Warn if separators, be it '.' or '_', do not match.
Fariborz Jahaniance72e632014-10-02 17:57:26 +0000945 if (AfterMajorSeparator != AfterMinorSeparator)
946 Diag(Tok, diag::warn_expected_consistent_version_separator);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000947
948 // Parse the subminor version.
949 unsigned AfterSubminor = AfterMinor + 1;
950 unsigned Subminor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000951 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000952 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
953 ++AfterSubminor;
954 }
955
956 if (AfterSubminor != ActualLength) {
957 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000958 SkipUntil(tok::comma, tok::r_paren,
959 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000960 return VersionTuple();
961 }
962 ConsumeToken();
Jan Korous23255692018-05-17 11:51:49 +0000963 return VersionTuple(Major, Minor, Subminor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000964}
965
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000966/// Parse the contents of the "availability" attribute.
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000967///
968/// availability-attribute:
Manman Ren75bc6762016-03-21 17:30:55 +0000969/// 'availability' '(' platform ',' opt-strict version-arg-list,
970/// opt-replacement, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000971///
972/// platform:
973/// identifier
974///
Manman Rend8039df2016-02-22 04:47:24 +0000975/// opt-strict:
976/// 'strict' ','
Manman Renb636b902016-02-17 22:05:48 +0000977///
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000978/// version-arg-list:
979/// version-arg
980/// version-arg ',' version-arg-list
981///
982/// version-arg:
983/// 'introduced' '=' version
984/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000985/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000986/// 'unavailable'
Manman Ren75bc6762016-03-21 17:30:55 +0000987/// opt-replacement:
988/// 'replacement' '=' <string>
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000989/// opt-message:
990/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000991void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
992 SourceLocation AvailabilityLoc,
993 ParsedAttributes &attrs,
Aaron Ballman80f1529c2014-07-16 20:21:50 +0000994 SourceLocation *endLoc,
995 IdentifierInfo *ScopeName,
996 SourceLocation ScopeLoc,
Erich Keanee891aa92018-07-13 15:07:47 +0000997 ParsedAttr::Syntax Syntax) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000998 enum { Introduced, Deprecated, Obsoleted, Unknown };
999 AvailabilityChange Changes[Unknown];
Manman Ren75bc6762016-03-21 17:30:55 +00001000 ExprResult MessageExpr, ReplacementExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001001
1002 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001003 BalancedDelimiterTracker T(*this, tok::l_paren);
1004 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00001005 Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001006 return;
1007 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001008
Manman Renb636b902016-02-17 22:05:48 +00001009 // Parse the platform name.
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001010 if (Tok.isNot(tok::identifier)) {
1011 Diag(Tok, diag::err_availability_expected_platform);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001012 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001013 return;
1014 }
Richard Smithfeefaf52013-09-03 18:01:40 +00001015 IdentifierLoc *Platform = ParseIdentifierLoc();
Alex Lorenz0b1ce8b2017-08-15 14:42:01 +00001016 if (const IdentifierInfo *const Ident = Platform->Ident) {
1017 // Canonicalize platform name from "macosx" to "macos".
1018 if (Ident->getName() == "macosx")
1019 Platform->Ident = PP.getIdentifierInfo("macos");
1020 // Canonicalize platform name from "macosx_app_extension" to
1021 // "macos_app_extension".
1022 else if (Ident->getName() == "macosx_app_extension")
1023 Platform->Ident = PP.getIdentifierInfo("macos_app_extension");
1024 else
1025 Platform->Ident = PP.getIdentifierInfo(
1026 AvailabilityAttr::canonicalizePlatformName(Ident->getName()));
1027 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001028
1029 // Parse the ',' following the platform name.
Alp Toker383d2c42014-01-01 03:08:43 +00001030 if (ExpectAndConsume(tok::comma)) {
1031 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001032 return;
Alp Toker383d2c42014-01-01 03:08:43 +00001033 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001034
1035 // If we haven't grabbed the pointers for the identifiers
1036 // "introduced", "deprecated", and "obsoleted", do so now.
1037 if (!Ident_introduced) {
1038 Ident_introduced = PP.getIdentifierInfo("introduced");
1039 Ident_deprecated = PP.getIdentifierInfo("deprecated");
1040 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001041 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001042 Ident_message = PP.getIdentifierInfo("message");
Manman Rend8039df2016-02-22 04:47:24 +00001043 Ident_strict = PP.getIdentifierInfo("strict");
Manman Ren75bc6762016-03-21 17:30:55 +00001044 Ident_replacement = PP.getIdentifierInfo("replacement");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001045 }
1046
Manman Ren75bc6762016-03-21 17:30:55 +00001047 // Parse the optional "strict", the optional "replacement" and the set of
Manman Renb636b902016-02-17 22:05:48 +00001048 // introductions/deprecations/removals.
Manman Rend8039df2016-02-22 04:47:24 +00001049 SourceLocation UnavailableLoc, StrictLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001050 do {
1051 if (Tok.isNot(tok::identifier)) {
1052 Diag(Tok, diag::err_availability_expected_change);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001053 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001054 return;
1055 }
1056 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
1057 SourceLocation KeywordLoc = ConsumeToken();
1058
Manman Rend8039df2016-02-22 04:47:24 +00001059 if (Keyword == Ident_strict) {
1060 if (StrictLoc.isValid()) {
Manman Renb636b902016-02-17 22:05:48 +00001061 Diag(KeywordLoc, diag::err_availability_redundant)
Manman Rend8039df2016-02-22 04:47:24 +00001062 << Keyword << SourceRange(StrictLoc);
Manman Renb636b902016-02-17 22:05:48 +00001063 }
Manman Rend8039df2016-02-22 04:47:24 +00001064 StrictLoc = KeywordLoc;
Manman Renb636b902016-02-17 22:05:48 +00001065 continue;
1066 }
1067
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001068 if (Keyword == Ident_unavailable) {
1069 if (UnavailableLoc.isValid()) {
1070 Diag(KeywordLoc, diag::err_availability_redundant)
1071 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +00001072 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001073 UnavailableLoc = KeywordLoc;
Alp Toker97650562014-01-10 11:19:30 +00001074 continue;
Chad Rosierc1183952012-06-26 22:30:43 +00001075 }
1076
Michael Wu260e9622018-11-12 02:44:33 +00001077 if (Keyword == Ident_deprecated && Platform->Ident &&
1078 Platform->Ident->isStr("swift")) {
1079 // For swift, we deprecate for all versions.
1080 if (Changes[Deprecated].KeywordLoc.isValid()) {
1081 Diag(KeywordLoc, diag::err_availability_redundant)
1082 << Keyword
1083 << SourceRange(Changes[Deprecated].KeywordLoc);
1084 }
1085
1086 Changes[Deprecated].KeywordLoc = KeywordLoc;
1087 // Use a fake version here.
1088 Changes[Deprecated].Version = VersionTuple(1);
1089 continue;
1090 }
1091
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001092 if (Tok.isNot(tok::equal)) {
Alp Tokerec543272013-12-24 09:48:30 +00001093 Diag(Tok, diag::err_expected_after) << Keyword << tok::equal;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001094 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001095 return;
1096 }
1097 ConsumeToken();
Manman Ren75bc6762016-03-21 17:30:55 +00001098 if (Keyword == Ident_message || Keyword == Ident_replacement) {
David Majnemer2e498302014-07-18 05:43:12 +00001099 if (Tok.isNot(tok::string_literal)) {
Andy Gibbsa8df57a2012-11-17 19:16:52 +00001100 Diag(Tok, diag::err_expected_string_literal)
1101 << /*Source='availability attribute'*/2;
Alexey Bataevee6507d2013-11-18 08:17:37 +00001102 SkipUntil(tok::r_paren, StopAtSemi);
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001103 return;
1104 }
Manman Ren75bc6762016-03-21 17:30:55 +00001105 if (Keyword == Ident_message)
1106 MessageExpr = ParseStringLiteralExpression();
1107 else
1108 ReplacementExpr = ParseStringLiteralExpression();
David Majnemer2e498302014-07-18 05:43:12 +00001109 // Also reject wide string literals.
1110 if (StringLiteral *MessageStringLiteral =
1111 cast_or_null<StringLiteral>(MessageExpr.get())) {
1112 if (MessageStringLiteral->getCharByteWidth() != 1) {
1113 Diag(MessageStringLiteral->getSourceRange().getBegin(),
1114 diag::err_expected_string_literal)
1115 << /*Source='availability attribute'*/ 2;
1116 SkipUntil(tok::r_paren, StopAtSemi);
1117 return;
1118 }
1119 }
Manman Ren75bc6762016-03-21 17:30:55 +00001120 if (Keyword == Ident_message)
1121 break;
1122 else
1123 continue;
Fariborz Jahanian88d510d2011-12-10 00:28:41 +00001124 }
Chad Rosierc1183952012-06-26 22:30:43 +00001125
Fariborz Jahanian5a29e6a2014-11-05 23:58:55 +00001126 // Special handling of 'NA' only when applied to introduced or
1127 // deprecated.
1128 if ((Keyword == Ident_introduced || Keyword == Ident_deprecated) &&
1129 Tok.is(tok::identifier)) {
1130 IdentifierInfo *NA = Tok.getIdentifierInfo();
1131 if (NA->getName() == "NA") {
1132 ConsumeToken();
1133 if (Keyword == Ident_introduced)
1134 UnavailableLoc = KeywordLoc;
1135 continue;
1136 }
1137 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001138
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001139 SourceRange VersionRange;
1140 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +00001141
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001142 if (Version.empty()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001143 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001144 return;
1145 }
1146
1147 unsigned Index;
1148 if (Keyword == Ident_introduced)
1149 Index = Introduced;
1150 else if (Keyword == Ident_deprecated)
1151 Index = Deprecated;
1152 else if (Keyword == Ident_obsoleted)
1153 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +00001154 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001155 Index = Unknown;
1156
1157 if (Index < Unknown) {
1158 if (!Changes[Index].KeywordLoc.isInvalid()) {
1159 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +00001160 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001161 << SourceRange(Changes[Index].KeywordLoc,
1162 Changes[Index].VersionRange.getEnd());
1163 }
1164
1165 Changes[Index].KeywordLoc = KeywordLoc;
1166 Changes[Index].Version = Version;
1167 Changes[Index].VersionRange = VersionRange;
1168 } else {
1169 Diag(KeywordLoc, diag::err_availability_unknown_change)
1170 << Keyword << VersionRange;
1171 }
1172
Alp Toker97650562014-01-10 11:19:30 +00001173 } while (TryConsumeToken(tok::comma));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001174
1175 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001176 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001177 return;
1178
1179 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001180 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001181
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001182 // The 'unavailable' availability cannot be combined with any other
1183 // availability changes. Make sure that hasn't happened.
1184 if (UnavailableLoc.isValid()) {
1185 bool Complained = false;
1186 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
1187 if (Changes[Index].KeywordLoc.isValid()) {
1188 if (!Complained) {
1189 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
1190 << SourceRange(Changes[Index].KeywordLoc,
1191 Changes[Index].VersionRange.getEnd());
1192 Complained = true;
1193 }
1194
1195 // Clear out the availability.
1196 Changes[Index] = AvailabilityChange();
1197 }
1198 }
1199 }
1200
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001201 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +00001202 attrs.addNew(&Availability,
1203 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Aaron Ballman80f1529c2014-07-16 20:21:50 +00001204 ScopeName, ScopeLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +00001205 Platform,
John McCall084e83d2011-03-24 11:26:52 +00001206 Changes[Introduced],
1207 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +00001208 Changes[Obsoleted],
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001209 UnavailableLoc, MessageExpr.get(),
Manman Ren75bc6762016-03-21 17:30:55 +00001210 Syntax, StrictLoc, ReplacementExpr.get());
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001211}
1212
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001213/// Parse the contents of the "external_source_symbol" attribute.
Alex Lorenzd5d27e12017-03-01 18:06:25 +00001214///
1215/// external-source-symbol-attribute:
1216/// 'external_source_symbol' '(' keyword-arg-list ')'
1217///
1218/// keyword-arg-list:
1219/// keyword-arg
1220/// keyword-arg ',' keyword-arg-list
1221///
1222/// keyword-arg:
1223/// 'language' '=' <string>
1224/// 'defined_in' '=' <string>
1225/// 'generated_declaration'
1226void Parser::ParseExternalSourceSymbolAttribute(
1227 IdentifierInfo &ExternalSourceSymbol, SourceLocation Loc,
1228 ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
Erich Keanee891aa92018-07-13 15:07:47 +00001229 SourceLocation ScopeLoc, ParsedAttr::Syntax Syntax) {
Alex Lorenzd5d27e12017-03-01 18:06:25 +00001230 // Opening '('.
1231 BalancedDelimiterTracker T(*this, tok::l_paren);
1232 if (T.expectAndConsume())
1233 return;
1234
1235 // Initialize the pointers for the keyword identifiers when required.
1236 if (!Ident_language) {
1237 Ident_language = PP.getIdentifierInfo("language");
1238 Ident_defined_in = PP.getIdentifierInfo("defined_in");
1239 Ident_generated_declaration = PP.getIdentifierInfo("generated_declaration");
1240 }
1241
1242 ExprResult Language;
1243 bool HasLanguage = false;
1244 ExprResult DefinedInExpr;
1245 bool HasDefinedIn = false;
1246 IdentifierLoc *GeneratedDeclaration = nullptr;
1247
1248 // Parse the language/defined_in/generated_declaration keywords
1249 do {
1250 if (Tok.isNot(tok::identifier)) {
1251 Diag(Tok, diag::err_external_source_symbol_expected_keyword);
1252 SkipUntil(tok::r_paren, StopAtSemi);
1253 return;
1254 }
1255
1256 SourceLocation KeywordLoc = Tok.getLocation();
1257 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
1258 if (Keyword == Ident_generated_declaration) {
1259 if (GeneratedDeclaration) {
1260 Diag(Tok, diag::err_external_source_symbol_duplicate_clause) << Keyword;
1261 SkipUntil(tok::r_paren, StopAtSemi);
1262 return;
1263 }
1264 GeneratedDeclaration = ParseIdentifierLoc();
1265 continue;
1266 }
1267
1268 if (Keyword != Ident_language && Keyword != Ident_defined_in) {
1269 Diag(Tok, diag::err_external_source_symbol_expected_keyword);
1270 SkipUntil(tok::r_paren, StopAtSemi);
1271 return;
1272 }
1273
1274 ConsumeToken();
1275 if (ExpectAndConsume(tok::equal, diag::err_expected_after,
1276 Keyword->getName())) {
1277 SkipUntil(tok::r_paren, StopAtSemi);
1278 return;
1279 }
1280
1281 bool HadLanguage = HasLanguage, HadDefinedIn = HasDefinedIn;
1282 if (Keyword == Ident_language)
1283 HasLanguage = true;
1284 else
1285 HasDefinedIn = true;
1286
1287 if (Tok.isNot(tok::string_literal)) {
1288 Diag(Tok, diag::err_expected_string_literal)
1289 << /*Source='external_source_symbol attribute'*/ 3
1290 << /*language | source container*/ (Keyword != Ident_language);
1291 SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
1292 continue;
1293 }
1294 if (Keyword == Ident_language) {
1295 if (HadLanguage) {
1296 Diag(KeywordLoc, diag::err_external_source_symbol_duplicate_clause)
1297 << Keyword;
1298 ParseStringLiteralExpression();
1299 continue;
1300 }
1301 Language = ParseStringLiteralExpression();
1302 } else {
1303 assert(Keyword == Ident_defined_in && "Invalid clause keyword!");
1304 if (HadDefinedIn) {
1305 Diag(KeywordLoc, diag::err_external_source_symbol_duplicate_clause)
1306 << Keyword;
1307 ParseStringLiteralExpression();
1308 continue;
1309 }
1310 DefinedInExpr = ParseStringLiteralExpression();
1311 }
1312 } while (TryConsumeToken(tok::comma));
1313
1314 // Closing ')'.
1315 if (T.consumeClose())
1316 return;
1317 if (EndLoc)
1318 *EndLoc = T.getCloseLocation();
1319
1320 ArgsUnion Args[] = {Language.get(), DefinedInExpr.get(),
1321 GeneratedDeclaration};
1322 Attrs.addNew(&ExternalSourceSymbol, SourceRange(Loc, T.getCloseLocation()),
1323 ScopeName, ScopeLoc, Args, llvm::array_lengthof(Args), Syntax);
1324}
1325
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001326/// Parse the contents of the "objc_bridge_related" attribute.
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001327/// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')'
1328/// related_class:
1329/// Identifier
1330///
1331/// opt-class_method:
1332/// Identifier: | <empty>
1333///
1334/// opt-instance_method:
1335/// Identifier | <empty>
1336///
1337void Parser::ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated,
1338 SourceLocation ObjCBridgeRelatedLoc,
1339 ParsedAttributes &attrs,
Aaron Ballman80f1529c2014-07-16 20:21:50 +00001340 SourceLocation *endLoc,
1341 IdentifierInfo *ScopeName,
1342 SourceLocation ScopeLoc,
Erich Keanee891aa92018-07-13 15:07:47 +00001343 ParsedAttr::Syntax Syntax) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001344 // Opening '('.
1345 BalancedDelimiterTracker T(*this, tok::l_paren);
1346 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00001347 Diag(Tok, diag::err_expected) << tok::l_paren;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001348 return;
1349 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001350
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001351 // Parse the related class name.
1352 if (Tok.isNot(tok::identifier)) {
1353 Diag(Tok, diag::err_objcbridge_related_expected_related_class);
1354 SkipUntil(tok::r_paren, StopAtSemi);
1355 return;
1356 }
1357 IdentifierLoc *RelatedClass = ParseIdentifierLoc();
Alp Toker97650562014-01-10 11:19:30 +00001358 if (ExpectAndConsume(tok::comma)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001359 SkipUntil(tok::r_paren, StopAtSemi);
1360 return;
1361 }
Alp Toker8fbec672013-12-17 23:29:36 +00001362
Aaron Ballman48a533d2018-02-27 23:49:28 +00001363 // Parse class method name. It's non-optional in the sense that a trailing
1364 // comma is required, but it can be the empty string, and then we record a
1365 // nullptr.
Craig Topper161e4db2014-05-21 06:02:52 +00001366 IdentifierLoc *ClassMethod = nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001367 if (Tok.is(tok::identifier)) {
1368 ClassMethod = ParseIdentifierLoc();
Alp Toker8fbec672013-12-17 23:29:36 +00001369 if (!TryConsumeToken(tok::colon)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001370 Diag(Tok, diag::err_objcbridge_related_selector_name);
1371 SkipUntil(tok::r_paren, StopAtSemi);
1372 return;
1373 }
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001374 }
Alp Toker8fbec672013-12-17 23:29:36 +00001375 if (!TryConsumeToken(tok::comma)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001376 if (Tok.is(tok::colon))
1377 Diag(Tok, diag::err_objcbridge_related_selector_name);
1378 else
Alp Tokerec543272013-12-24 09:48:30 +00001379 Diag(Tok, diag::err_expected) << tok::comma;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001380 SkipUntil(tok::r_paren, StopAtSemi);
1381 return;
1382 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001383
Aaron Ballman48a533d2018-02-27 23:49:28 +00001384 // Parse instance method name. Also non-optional but empty string is
1385 // permitted.
Craig Topper161e4db2014-05-21 06:02:52 +00001386 IdentifierLoc *InstanceMethod = nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001387 if (Tok.is(tok::identifier))
1388 InstanceMethod = ParseIdentifierLoc();
1389 else if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001390 Diag(Tok, diag::err_expected) << tok::r_paren;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001391 SkipUntil(tok::r_paren, StopAtSemi);
1392 return;
1393 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001394
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001395 // Closing ')'.
1396 if (T.consumeClose())
1397 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001398
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001399 if (endLoc)
1400 *endLoc = T.getCloseLocation();
Fangrui Song6907ce22018-07-30 19:24:48 +00001401
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001402 // Record this attribute
1403 attrs.addNew(&ObjCBridgeRelated,
1404 SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()),
Aaron Ballman80f1529c2014-07-16 20:21:50 +00001405 ScopeName, ScopeLoc,
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001406 RelatedClass,
1407 ClassMethod,
1408 InstanceMethod,
Aaron Ballman80f1529c2014-07-16 20:21:50 +00001409 Syntax);
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001410}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001411
Bill Wendling44426052012-12-20 19:22:21 +00001412// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001413// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
1414
1415void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
1416
1417void Parser::LateParsedClass::ParseLexedAttributes() {
1418 Self->ParseLexedAttributes(*Class);
1419}
1420
1421void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001422 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001423}
1424
1425/// Wrapper class which calls ParseLexedAttribute, after setting up the
1426/// scope appropriately.
1427void Parser::ParseLexedAttributes(ParsingClass &Class) {
1428 // Deal with templates
1429 // FIXME: Test cases to make sure this does the right thing for templates.
1430 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
1431 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
1432 HasTemplateScope);
1433 if (HasTemplateScope)
1434 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
1435
Douglas Gregor3024f072012-04-16 07:05:22 +00001436 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001437 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +00001438 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001439 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
1440 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
1441
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001442 // Enter the scope of nested classes
1443 if (!AlreadyHasClassScope)
1444 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
1445 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +00001446 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +00001447 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
1448 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
1449 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001450 }
Chad Rosierc1183952012-06-26 22:30:43 +00001451
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001452 if (!AlreadyHasClassScope)
1453 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1454 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001455}
1456
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001457/// Parse all attributes in LAs, and attach them to Decl D.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001458void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1459 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001460 assert(LAs.parseSoon() &&
1461 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001462 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +00001463 if (D)
1464 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001465 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +00001466 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001467 }
1468 LAs.clear();
1469}
1470
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001471/// Finish parsing an attribute for which parsing was delayed.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001472/// This will be called at the end of parsing a class declaration
1473/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +00001474/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001475/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001476void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1477 bool EnterScope, bool OnDefinition) {
David Majnemerd5946f52015-01-13 08:35:24 +00001478 // Create a fake EOF so that attribute parsing won't go off the end of the
1479 // attribute.
1480 Token AttrEnd;
1481 AttrEnd.startToken();
1482 AttrEnd.setKind(tok::eof);
1483 AttrEnd.setLocation(Tok.getLocation());
1484 AttrEnd.setEofData(LA.Toks.data());
1485 LA.Toks.push_back(AttrEnd);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001486
1487 // Append the current token at the end of the new token stream so that it
1488 // doesn't get lost.
1489 LA.Toks.push_back(Tok);
Ilya Biryukov929af672019-05-17 09:32:05 +00001490 PP.EnterTokenStream(LA.Toks, true, /*IsReinject=*/true);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001491 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00001492 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001493
1494 ParsedAttributes Attrs(AttrFactory);
1495 SourceLocation endLoc;
1496
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001497 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001498 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001499 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1500 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001501
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001502 // Allow 'this' within late-parsed attributes.
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00001503 Sema::CXXThisScopeRAII ThisScope(Actions, RD, Qualifiers(),
Richard Smithc3d2ebb2013-06-07 02:33:37 +00001504 ND && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001505
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001506 if (LA.Decls.size() == 1) {
1507 // If the Decl is templatized, add template parameters to scope.
1508 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1509 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1510 if (HasTemplateScope)
1511 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001512
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001513 // If the Decl is on a function, add function parameters to the scope.
1514 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
Momchil Velikov57c681f2017-08-10 15:43:06 +00001515 ParseScope FnScope(
1516 this, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope,
1517 HasFunScope);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001518 if (HasFunScope)
1519 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001520
Michael Han23214e52012-10-03 01:56:22 +00001521 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Erich Keanee891aa92018-07-13 15:07:47 +00001522 nullptr, SourceLocation(), ParsedAttr::AS_GNU,
Craig Topper161e4db2014-05-21 06:02:52 +00001523 nullptr);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001524
1525 if (HasFunScope) {
1526 Actions.ActOnExitFunctionContext();
1527 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1528 }
1529 if (HasTemplateScope) {
1530 TempScope.Exit();
1531 }
1532 } else {
1533 // If there are multiple decls, then the decl cannot be within the
1534 // function scope.
Michael Han23214e52012-10-03 01:56:22 +00001535 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Erich Keanee891aa92018-07-13 15:07:47 +00001536 nullptr, SourceLocation(), ParsedAttr::AS_GNU,
Craig Topper161e4db2014-05-21 06:02:52 +00001537 nullptr);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001538 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +00001539 } else {
1540 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001541 }
1542
Erich Keanec480f302018-07-12 21:09:05 +00001543 if (OnDefinition && !Attrs.empty() && !Attrs.begin()->isCXX11Attribute() &&
1544 Attrs.begin()->isKnownToGCC())
Aaron Ballman9a99e0d2014-01-20 17:18:35 +00001545 Diag(Tok, diag::warn_attribute_on_function_definition)
1546 << &LA.AttrName;
1547
1548 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i)
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001549 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001550
David Majnemerd5946f52015-01-13 08:35:24 +00001551 // Due to a parsing error, we either went over the cached tokens or
1552 // there are still cached tokens left, so we skip the leftover tokens.
1553 while (Tok.isNot(tok::eof))
1554 ConsumeAnyToken();
1555
1556 if (Tok.is(tok::eof) && Tok.getEofData() == AttrEnd.getEofData())
1557 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001558}
1559
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001560void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1561 SourceLocation AttrNameLoc,
1562 ParsedAttributes &Attrs,
Aaron Ballman80f1529c2014-07-16 20:21:50 +00001563 SourceLocation *EndLoc,
1564 IdentifierInfo *ScopeName,
1565 SourceLocation ScopeLoc,
Erich Keanee891aa92018-07-13 15:07:47 +00001566 ParsedAttr::Syntax Syntax) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001567 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1568
1569 BalancedDelimiterTracker T(*this, tok::l_paren);
1570 T.consumeOpen();
1571
1572 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001573 Diag(Tok, diag::err_expected) << tok::identifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001574 T.skipToEnd();
1575 return;
1576 }
Richard Smithfeefaf52013-09-03 18:01:40 +00001577 IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001578
Alp Toker094e5212014-01-05 03:27:11 +00001579 if (ExpectAndConsume(tok::comma)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001580 T.skipToEnd();
1581 return;
1582 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001583
1584 SourceRange MatchingCTypeRange;
1585 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1586 if (MatchingCType.isInvalid()) {
1587 T.skipToEnd();
1588 return;
1589 }
1590
1591 bool LayoutCompatible = false;
1592 bool MustBeNull = false;
Alp Toker8fbec672013-12-17 23:29:36 +00001593 while (TryConsumeToken(tok::comma)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001594 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001595 Diag(Tok, diag::err_expected) << tok::identifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001596 T.skipToEnd();
1597 return;
1598 }
1599 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1600 if (Flag->isStr("layout_compatible"))
1601 LayoutCompatible = true;
1602 else if (Flag->isStr("must_be_null"))
1603 MustBeNull = true;
1604 else {
1605 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1606 T.skipToEnd();
1607 return;
1608 }
1609 ConsumeToken(); // consume flag
1610 }
1611
1612 if (!T.consumeClose()) {
Aaron Ballman80f1529c2014-07-16 20:21:50 +00001613 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, ScopeName, ScopeLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001614 ArgumentKind, MatchingCType.get(),
Aaron Ballman80f1529c2014-07-16 20:21:50 +00001615 LayoutCompatible, MustBeNull, Syntax);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001616 }
1617
1618 if (EndLoc)
1619 *EndLoc = T.getCloseLocation();
1620}
1621
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001622/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1623/// of a C++11 attribute-specifier in a location where an attribute is not
1624/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1625/// situation.
1626///
1627/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1628/// this doesn't appear to actually be an attribute-specifier, and the caller
1629/// should try to parse it.
1630bool Parser::DiagnoseProhibitedCXX11Attribute() {
1631 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1632
1633 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1634 case CAK_NotAttributeSpecifier:
1635 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1636 return false;
1637
1638 case CAK_InvalidAttributeSpecifier:
1639 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1640 return false;
1641
1642 case CAK_AttributeSpecifier:
1643 // Parse and discard the attributes.
1644 SourceLocation BeginLoc = ConsumeBracket();
1645 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001646 SkipUntil(tok::r_square);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001647 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1648 SourceLocation EndLoc = ConsumeBracket();
1649 Diag(BeginLoc, diag::err_attributes_not_allowed)
1650 << SourceRange(BeginLoc, EndLoc);
1651 return true;
1652 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001653 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001654}
1655
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001656/// We have found the opening square brackets of a C++11
Richard Smith98155ad2013-02-20 01:17:14 +00001657/// attribute-specifier in a location where an attribute is not permitted, but
1658/// we know where the attributes ought to be written. Parse them anyway, and
1659/// provide a fixit moving them to the right place.
Richard Smith4c96e992013-02-19 23:47:15 +00001660void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1661 SourceLocation CorrectLocation) {
1662 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1663 Tok.is(tok::kw_alignas));
1664
1665 // Consume the attributes.
1666 SourceLocation Loc = Tok.getLocation();
1667 ParseCXX11Attributes(Attrs);
1668 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
Faisal Valic5089c02017-12-25 22:23:20 +00001669 // FIXME: use err_attributes_misplaced
Richard Smith4c96e992013-02-19 23:47:15 +00001670 Diag(Loc, diag::err_attributes_not_allowed)
1671 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1672 << FixItHint::CreateRemoval(AttrRange);
1673}
1674
Erich Keanec480f302018-07-12 21:09:05 +00001675void Parser::DiagnoseProhibitedAttributes(
1676 const SourceRange &Range, const SourceLocation CorrectLocation) {
Faisal Valic5089c02017-12-25 22:23:20 +00001677 if (CorrectLocation.isValid()) {
Erich Keanec480f302018-07-12 21:09:05 +00001678 CharSourceRange AttrRange(Range, true);
Faisal Valic5089c02017-12-25 22:23:20 +00001679 Diag(CorrectLocation, diag::err_attributes_misplaced)
1680 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1681 << FixItHint::CreateRemoval(AttrRange);
1682 } else
Erich Keanec480f302018-07-12 21:09:05 +00001683 Diag(Range.getBegin(), diag::err_attributes_not_allowed) << Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001684}
1685
Richard Smith49cc1cc2016-08-18 21:59:42 +00001686void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &Attrs,
1687 unsigned DiagID) {
Erich Keanee891aa92018-07-13 15:07:47 +00001688 for (const ParsedAttr &AL : Attrs) {
Erich Keanec480f302018-07-12 21:09:05 +00001689 if (!AL.isCXX11Attribute() && !AL.isC2xAttribute())
Richard Smith49cc1cc2016-08-18 21:59:42 +00001690 continue;
Erich Keanee891aa92018-07-13 15:07:47 +00001691 if (AL.getKind() == ParsedAttr::UnknownAttribute)
Erich Keane6a24e802019-09-13 17:39:31 +00001692 Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL;
Richard Smith49cc1cc2016-08-18 21:59:42 +00001693 else {
Erich Keane6a24e802019-09-13 17:39:31 +00001694 Diag(AL.getLoc(), DiagID) << AL;
Erich Keanec480f302018-07-12 21:09:05 +00001695 AL.setInvalid();
Michael Han64536a62012-11-06 19:34:54 +00001696 }
Michael Han64536a62012-11-06 19:34:54 +00001697 }
1698}
1699
Nico Weber32a0fc72016-09-03 03:01:32 +00001700// Usually, `__attribute__((attrib)) class Foo {} var` means that attribute
1701// applies to var, not the type Foo.
David Majnemer936b4112015-04-19 07:53:29 +00001702// As an exception to the rule, __declspec(align(...)) before the
1703// class-key affects the type instead of the variable.
Nico Weber32a0fc72016-09-03 03:01:32 +00001704// Also, Microsoft-style [attributes] seem to affect the type instead of the
1705// variable.
1706// This function moves attributes that should apply to the type off DS to Attrs.
1707void Parser::stripTypeAttributesOffDeclSpec(ParsedAttributesWithRange &Attrs,
1708 DeclSpec &DS,
1709 Sema::TagUseKind TUK) {
David Majnemer936b4112015-04-19 07:53:29 +00001710 if (TUK == Sema::TUK_Reference)
1711 return;
1712
Erich Keanee891aa92018-07-13 15:07:47 +00001713 llvm::SmallVector<ParsedAttr *, 1> ToBeMoved;
David Majnemer936b4112015-04-19 07:53:29 +00001714
Erich Keanee891aa92018-07-13 15:07:47 +00001715 for (ParsedAttr &AL : DS.getAttributes()) {
1716 if ((AL.getKind() == ParsedAttr::AT_Aligned &&
Erich Keanec480f302018-07-12 21:09:05 +00001717 AL.isDeclspecAttribute()) ||
1718 AL.isMicrosoftAttribute())
1719 ToBeMoved.push_back(&AL);
David Majnemer936b4112015-04-19 07:53:29 +00001720 }
Nico Weber88f5ed92016-09-13 18:55:26 +00001721
Erich Keanee891aa92018-07-13 15:07:47 +00001722 for (ParsedAttr *AL : ToBeMoved) {
Erich Keanec480f302018-07-12 21:09:05 +00001723 DS.getAttributes().remove(AL);
1724 Attrs.addAtEnd(AL);
1725 }
David Majnemer936b4112015-04-19 07:53:29 +00001726}
1727
Chris Lattner53361ac2006-08-10 05:19:57 +00001728/// ParseDeclaration - Parse a full 'declaration', which consists of
1729/// declaration-specifiers, some number of declarators, and a semicolon.
Faisal Vali421b2d12017-12-29 05:41:00 +00001730/// 'Context' should be a DeclaratorContext value. This returns the
Chris Lattner49836b42009-04-02 04:16:50 +00001731/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001732///
1733/// declaration: [C99 6.7]
1734/// block-declaration ->
1735/// simple-declaration
1736/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001737/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001738/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001739/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001740/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001741/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001742/// others... [FIXME]
1743///
Nathan Huckleberry1e0affb2019-08-20 17:16:49 +00001744Parser::DeclGroupPtrTy
1745Parser::ParseDeclaration(DeclaratorContext Context, SourceLocation &DeclEnd,
1746 ParsedAttributesWithRange &attrs,
1747 SourceLocation *DeclSpecStart) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001748 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001749 // Must temporarily exit the objective-c container scope for
1750 // parsing c none objective-c decls.
1751 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001752
Craig Topper161e4db2014-05-21 06:02:52 +00001753 Decl *SingleDecl = nullptr;
Chris Lattnera5235172007-08-25 06:57:03 +00001754 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001755 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001756 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001757 ProhibitAttributes(attrs);
Erich Keanec480f302018-07-12 21:09:05 +00001758 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd, attrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001759 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001760 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001761 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001762 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001763 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001764 SourceLocation InlineLoc = ConsumeToken();
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00001765 return ParseNamespace(Context, DeclEnd, InlineLoc);
Sebastian Redl67667942010-08-27 23:12:46 +00001766 }
Nathan Huckleberry1e0affb2019-08-20 17:16:49 +00001767 return ParseSimpleDeclaration(Context, DeclEnd, attrs, true, nullptr,
1768 DeclSpecStart);
Chris Lattnera5235172007-08-25 06:57:03 +00001769 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001770 ProhibitAttributes(attrs);
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00001771 return ParseNamespace(Context, DeclEnd);
Douglas Gregord7c4d982008-12-30 03:27:21 +00001772 case tok::kw_using:
Richard Smith6f1daa42016-12-16 00:58:48 +00001773 return ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
1774 DeclEnd, attrs);
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001775 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001776 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001777 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001778 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001779 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001780 default:
Nathan Huckleberry1e0affb2019-08-20 17:16:49 +00001781 return ParseSimpleDeclaration(Context, DeclEnd, attrs, true, nullptr,
1782 DeclSpecStart);
Chris Lattnera5235172007-08-25 06:57:03 +00001783 }
Chad Rosierc1183952012-06-26 22:30:43 +00001784
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001785 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smith6f1daa42016-12-16 00:58:48 +00001786 // single decl, convert it now.
1787 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +00001788}
1789
1790/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1791/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001792/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1793/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001794///[C90/C++]init-declarator-list ';' [TODO]
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001795/// [OMP] threadprivate-directive
1796/// [OMP] allocate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001797///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001798/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001799/// attribute-specifier-seq[opt] type-specifier-seq declarator
1800///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001801/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001802/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001803///
1804/// If FRI is non-null, we might be parsing a for-range-declaration instead
1805/// of a simple-declaration. If we find that we are, we also parse the
1806/// for-range-initializer, and place it here.
Nathan Huckleberry1e0affb2019-08-20 17:16:49 +00001807///
1808/// DeclSpecStart is used when decl-specifiers are parsed before parsing
1809/// the Declaration. The SourceLocation for this Decl is set to
1810/// DeclSpecStart if DeclSpecStart is non-null.
1811Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(
1812 DeclaratorContext Context, SourceLocation &DeclEnd,
1813 ParsedAttributesWithRange &Attrs, bool RequireSemi, ForRangeInit *FRI,
1814 SourceLocation *DeclSpecStart) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001815 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001816 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001817
Richard Smith404dfb42013-11-19 22:47:36 +00001818 DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
Faisal Valia534f072018-04-26 00:42:40 +00001819 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext);
Richard Smith404dfb42013-11-19 22:47:36 +00001820
1821 // If we had a free-standing type definition with a missing semicolon, we
1822 // may get this far before the problem becomes obvious.
1823 if (DS.hasTagDefinition() &&
1824 DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext))
David Blaikie0403cb12016-01-15 23:43:25 +00001825 return nullptr;
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001826
Chris Lattner0e894622006-08-13 19:58:17 +00001827 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1828 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001829 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001830 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001831 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001832 if (RequireSemi) ConsumeToken();
Nico Weber7b837f52016-01-28 19:25:00 +00001833 RecordDecl *AnonRecord = nullptr;
John McCall48871652010-08-21 09:40:31 +00001834 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Nico Weber7b837f52016-01-28 19:25:00 +00001835 DS, AnonRecord);
John McCall28a6aea2009-11-04 02:18:39 +00001836 DS.complete(TheDecl);
Nico Weber7b837f52016-01-28 19:25:00 +00001837 if (AnonRecord) {
1838 Decl* decls[] = {AnonRecord, TheDecl};
Richard Smith3beb7c62017-01-12 02:27:38 +00001839 return Actions.BuildDeclaratorGroup(decls);
Nico Weber7b837f52016-01-28 19:25:00 +00001840 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001841 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001842 }
Chad Rosierc1183952012-06-26 22:30:43 +00001843
Nathan Huckleberry1e0affb2019-08-20 17:16:49 +00001844 if (DeclSpecStart)
1845 DS.SetRangeStart(*DeclSpecStart);
1846
Richard Smith2386c8b2013-02-22 09:06:26 +00001847 DS.takeAttributesFrom(Attrs);
Reid Klecknerd61a3112014-12-15 23:16:32 +00001848 return ParseDeclGroup(DS, Context, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001849}
Mike Stump11289f42009-09-09 15:08:12 +00001850
Richard Smith09f76ee2011-10-19 21:33:05 +00001851/// Returns true if this might be the start of a declarator, or a common typo
1852/// for a declarator.
Faisal Vali421b2d12017-12-29 05:41:00 +00001853bool Parser::MightBeDeclarator(DeclaratorContext Context) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001854 switch (Tok.getKind()) {
1855 case tok::annot_cxxscope:
1856 case tok::annot_template_id:
1857 case tok::caret:
1858 case tok::code_completion:
1859 case tok::coloncolon:
1860 case tok::ellipsis:
1861 case tok::kw___attribute:
1862 case tok::kw_operator:
1863 case tok::l_paren:
1864 case tok::star:
1865 return true;
1866
1867 case tok::amp:
1868 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001869 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001870
Richard Smithc8a79032012-01-09 22:31:44 +00001871 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Faisal Vali421b2d12017-12-29 05:41:00 +00001872 return Context == DeclaratorContext::MemberContext &&
1873 getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square);
Richard Smithc8a79032012-01-09 22:31:44 +00001874
1875 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
Faisal Vali421b2d12017-12-29 05:41:00 +00001876 return Context == DeclaratorContext::MemberContext ||
1877 getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001878
Richard Smith09f76ee2011-10-19 21:33:05 +00001879 case tok::identifier:
1880 switch (NextToken().getKind()) {
1881 case tok::code_completion:
1882 case tok::coloncolon:
1883 case tok::comma:
1884 case tok::equal:
1885 case tok::equalequal: // Might be a typo for '='.
1886 case tok::kw_alignas:
1887 case tok::kw_asm:
1888 case tok::kw___attribute:
1889 case tok::l_brace:
1890 case tok::l_paren:
1891 case tok::l_square:
1892 case tok::less:
1893 case tok::r_brace:
1894 case tok::r_paren:
1895 case tok::r_square:
1896 case tok::semi:
1897 return true;
1898
1899 case tok::colon:
1900 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001901 // and in block scope it's probably a label. Inside a class definition,
1902 // this is a bit-field.
Faisal Vali421b2d12017-12-29 05:41:00 +00001903 return Context == DeclaratorContext::MemberContext ||
1904 (getLangOpts().CPlusPlus &&
1905 Context == DeclaratorContext::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001906
1907 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001908 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001909
1910 default:
1911 return false;
1912 }
1913
1914 default:
1915 return false;
1916 }
1917}
1918
Richard Smithb8caac82012-04-11 20:59:20 +00001919/// Skip until we reach something which seems like a sensible place to pick
1920/// up parsing after a malformed declaration. This will sometimes stop sooner
1921/// than SkipUntil(tok::r_brace) would, but will never stop later.
1922void Parser::SkipMalformedDecl() {
1923 while (true) {
1924 switch (Tok.getKind()) {
1925 case tok::l_brace:
1926 // Skip until matching }, then stop. We've probably skipped over
1927 // a malformed class or function definition or similar.
1928 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001929 SkipUntil(tok::r_brace);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001930 if (Tok.isOneOf(tok::comma, tok::l_brace, tok::kw_try)) {
Richard Smithb8caac82012-04-11 20:59:20 +00001931 // This declaration isn't over yet. Keep skipping.
1932 continue;
1933 }
Alp Toker8fbec672013-12-17 23:29:36 +00001934 TryConsumeToken(tok::semi);
Richard Smithb8caac82012-04-11 20:59:20 +00001935 return;
1936
1937 case tok::l_square:
1938 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001939 SkipUntil(tok::r_square);
Richard Smithb8caac82012-04-11 20:59:20 +00001940 continue;
1941
1942 case tok::l_paren:
1943 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001944 SkipUntil(tok::r_paren);
Richard Smithb8caac82012-04-11 20:59:20 +00001945 continue;
1946
1947 case tok::r_brace:
1948 return;
1949
1950 case tok::semi:
1951 ConsumeToken();
1952 return;
1953
1954 case tok::kw_inline:
1955 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001956 // a good place to pick back up parsing, except in an Objective-C
1957 // @interface context.
1958 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1959 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001960 return;
1961 break;
1962
1963 case tok::kw_namespace:
1964 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001965 // place to pick back up parsing, except in an Objective-C
1966 // @interface context.
1967 if (Tok.isAtStartOfLine() &&
1968 (!ParsingInObjCContainer || CurParsedObjCImpl))
1969 return;
1970 break;
1971
1972 case tok::at:
1973 // @end is very much like } in Objective-C contexts.
1974 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1975 ParsingInObjCContainer)
1976 return;
1977 break;
1978
1979 case tok::minus:
1980 case tok::plus:
1981 // - and + probably start new method declarations in Objective-C contexts.
1982 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001983 return;
1984 break;
1985
1986 case tok::eof:
Richard Smith34f30512013-11-23 04:06:09 +00001987 case tok::annot_module_begin:
1988 case tok::annot_module_end:
1989 case tok::annot_module_include:
Richard Smithb8caac82012-04-11 20:59:20 +00001990 return;
1991
1992 default:
1993 break;
1994 }
1995
1996 ConsumeAnyToken();
1997 }
1998}
1999
John McCalld5a36322009-11-03 19:26:08 +00002000/// ParseDeclGroup - Having concluded that this is either a function
2001/// definition or a group of object declarations, actually parse the
2002/// result.
John McCall28a6aea2009-11-04 02:18:39 +00002003Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
Faisal Vali421b2d12017-12-29 05:41:00 +00002004 DeclaratorContext Context,
Richard Smith02e85f32011-04-14 22:09:26 +00002005 SourceLocation *DeclEnd,
2006 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00002007 // Parse the first declarator.
Faisal Vali421b2d12017-12-29 05:41:00 +00002008 ParsingDeclarator D(*this, DS, Context);
John McCalld5a36322009-11-03 19:26:08 +00002009 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00002010
John McCalld5a36322009-11-03 19:26:08 +00002011 // Bail out if the first declarator didn't seem well-formed.
2012 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00002013 SkipMalformedDecl();
David Blaikie0403cb12016-01-15 23:43:25 +00002014 return nullptr;
Chris Lattnerefb0f112009-03-29 17:18:04 +00002015 }
Mike Stump11289f42009-09-09 15:08:12 +00002016
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00002017 // Save late-parsed attributes for now; they need to be parsed in the
2018 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00002019 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
2020 LateParsedAttrList LateParsedAttrs(true);
Richard Smith99c464c2014-11-10 21:10:32 +00002021 if (D.isFunctionDeclarator()) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00002022 MaybeParseGNUAttributes(D, &LateParsedAttrs);
2023
Richard Smith99c464c2014-11-10 21:10:32 +00002024 // The _Noreturn keyword can't appear here, unlike the GNU noreturn
2025 // attribute. If we find the keyword here, tell the user to put it
2026 // at the start instead.
2027 if (Tok.is(tok::kw__Noreturn)) {
2028 SourceLocation Loc = ConsumeToken();
2029 const char *PrevSpec;
2030 unsigned DiagID;
2031
2032 // We can offer a fixit if it's valid to mark this function as _Noreturn
2033 // and we don't have any other declarators in this declaration.
2034 bool Fixit = !DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
2035 MaybeParseGNUAttributes(D, &LateParsedAttrs);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002036 Fixit &= Tok.isOneOf(tok::semi, tok::l_brace, tok::kw_try);
Richard Smith99c464c2014-11-10 21:10:32 +00002037
2038 Diag(Loc, diag::err_c11_noreturn_misplaced)
2039 << (Fixit ? FixItHint::CreateRemoval(Loc) : FixItHint())
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002040 << (Fixit ? FixItHint::CreateInsertion(D.getBeginLoc(), "_Noreturn ")
Richard Smith99c464c2014-11-10 21:10:32 +00002041 : FixItHint());
2042 }
2043 }
2044
Chris Lattnerdbb1e932010-07-11 22:24:20 +00002045 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00002046 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00002047 // Look at the next token to make sure that this isn't a function
2048 // declaration. We have to check this because __attribute__ might be the
2049 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00002050 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00002051
Reid Klecknerd61a3112014-12-15 23:16:32 +00002052 // Function definitions are only allowed at file scope and in C++ classes.
2053 // The C++ inline method definition case is handled elsewhere, so we only
2054 // need to handle the file scope definition case.
Faisal Vali421b2d12017-12-29 05:41:00 +00002055 if (Context == DeclaratorContext::FileContext) {
Douglas Gregor012efe22013-04-16 16:01:32 +00002056 if (isStartOfFunctionDefinition(D)) {
2057 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2058 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00002059
Douglas Gregor012efe22013-04-16 16:01:32 +00002060 // Recover by treating the 'typedef' as spurious.
2061 DS.ClearStorageClassSpecs();
2062 }
2063
2064 Decl *TheDecl =
2065 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
2066 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00002067 }
2068
Douglas Gregor012efe22013-04-16 16:01:32 +00002069 if (isDeclarationSpecifier()) {
Nico Weber6b05f382015-02-18 04:53:03 +00002070 // If there is an invalid declaration specifier right after the
2071 // function prototype, then we must be in a missing semicolon case
2072 // where this isn't actually a body. Just fall through into the code
2073 // that handles it as a prototype, and let the top-level code handle
2074 // the erroneous declspec where it would otherwise expect a comma or
2075 // semicolon.
Douglas Gregor012efe22013-04-16 16:01:32 +00002076 } else {
2077 Diag(Tok, diag::err_expected_fn_body);
2078 SkipUntil(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +00002079 return nullptr;
Douglas Gregor012efe22013-04-16 16:01:32 +00002080 }
John McCalld5a36322009-11-03 19:26:08 +00002081 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00002082 if (Tok.is(tok::l_brace)) {
2083 Diag(Tok, diag::err_function_definition_not_allowed);
Serge Pavlov1de51512013-12-09 05:25:47 +00002084 SkipMalformedDecl();
David Blaikie0403cb12016-01-15 23:43:25 +00002085 return nullptr;
Douglas Gregor012efe22013-04-16 16:01:32 +00002086 }
John McCalld5a36322009-11-03 19:26:08 +00002087 }
2088 }
2089
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00002090 if (ParseAsmAttributesAfterDeclarator(D))
David Blaikie0403cb12016-01-15 23:43:25 +00002091 return nullptr;
Richard Smith02e85f32011-04-14 22:09:26 +00002092
2093 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
2094 // must parse and analyze the for-range-initializer before the declaration is
2095 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00002096 //
2097 // Handle the Objective-C for-in loop variable similarly, although we
2098 // don't need to parse the container in advance.
2099 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
2100 bool IsForRangeLoop = false;
Alp Toker8fbec672013-12-17 23:29:36 +00002101 if (TryConsumeToken(tok::colon, FRI->ColonLoc)) {
Douglas Gregor2eb1c572013-04-08 20:52:24 +00002102 IsForRangeLoop = true;
Alexey Bataevbef93a92019-10-07 18:54:57 +00002103 if (getLangOpts().OpenMP)
2104 Actions.startOpenMPCXXRangeFor();
Douglas Gregor2eb1c572013-04-08 20:52:24 +00002105 if (Tok.is(tok::l_brace))
2106 FRI->RangeExpr = ParseBraceInitializer();
2107 else
2108 FRI->RangeExpr = ParseExpression();
2109 }
2110
Richard Smith02e85f32011-04-14 22:09:26 +00002111 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
George Karpenkovec38cf72018-03-29 00:56:24 +00002112 if (IsForRangeLoop) {
Douglas Gregor2eb1c572013-04-08 20:52:24 +00002113 Actions.ActOnCXXForRangeDecl(ThisDecl);
George Karpenkovec38cf72018-03-29 00:56:24 +00002114 } else {
2115 // Obj-C for loop
2116 if (auto *VD = dyn_cast_or_null<VarDecl>(ThisDecl))
2117 VD->setObjCForDecl(true);
2118 }
Richard Smith02e85f32011-04-14 22:09:26 +00002119 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00002120 D.complete(ThisDecl);
Rafael Espindolaab417692013-07-09 12:05:01 +00002121 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00002122 }
2123
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002124 SmallVector<Decl *, 8> DeclsInGroup;
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00002125 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(
2126 D, ParsedTemplateInfo(), FRI);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00002127 if (LateParsedAttrs.size() > 0)
2128 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00002129 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00002130 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00002131 DeclsInGroup.push_back(FirstDecl);
2132
Faisal Vali421b2d12017-12-29 05:41:00 +00002133 bool ExpectSemi = Context != DeclaratorContext::ForContext;
Fangrui Song6907ce22018-07-30 19:24:48 +00002134
John McCalld5a36322009-11-03 19:26:08 +00002135 // If we don't have a comma, it is either the end of the list (a ';') or an
2136 // error, bail out.
Alp Toker8fbec672013-12-17 23:29:36 +00002137 SourceLocation CommaLoc;
2138 while (TryConsumeToken(tok::comma, CommaLoc)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00002139 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
2140 // This comma was followed by a line-break and something which can't be
2141 // the start of a declarator. The comma was probably a typo for a
2142 // semicolon.
2143 Diag(CommaLoc, diag::err_expected_semi_declaration)
2144 << FixItHint::CreateReplacement(CommaLoc, ";");
2145 ExpectSemi = false;
2146 break;
2147 }
John McCalld5a36322009-11-03 19:26:08 +00002148
2149 // Parse the next declarator.
2150 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00002151 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00002152
2153 // Accept attributes in an init-declarator. In the first declarator in a
2154 // declaration, these would be part of the declspec. In subsequent
2155 // declarators, they become part of the declarator itself, so that they
2156 // don't apply to declarators after *this* one. Examples:
2157 // short __attribute__((common)) var; -> declspec
2158 // short var __attribute__((common)); -> declarator
2159 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00002160 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00002161
Nico Rieckeaaae272014-12-04 23:31:08 +00002162 // MSVC parses but ignores qualifiers after the comma as an extension.
2163 if (getLangOpts().MicrosoftExt)
2164 DiagnoseAndSkipExtendedMicrosoftTypeAttributes();
2165
John McCalld5a36322009-11-03 19:26:08 +00002166 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00002167 if (!D.isInvalidType()) {
2168 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
2169 D.complete(ThisDecl);
2170 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00002171 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00002172 }
John McCalld5a36322009-11-03 19:26:08 +00002173 }
2174
2175 if (DeclEnd)
2176 *DeclEnd = Tok.getLocation();
2177
Richard Smith09f76ee2011-10-19 21:33:05 +00002178 if (ExpectSemi &&
Faisal Vali421b2d12017-12-29 05:41:00 +00002179 ExpectAndConsumeSemi(Context == DeclaratorContext::FileContext
Chris Lattner02f1b612012-04-28 16:12:17 +00002180 ? diag::err_invalid_token_after_toplevel_declarator
2181 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00002182 // Okay, there was no semicolon and one was expected. If we see a
2183 // declaration specifier, just assume it was missing and continue parsing.
2184 // Otherwise things are very confused and we skip to recover.
2185 if (!isDeclarationSpecifier()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002186 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Alp Toker8fbec672013-12-17 23:29:36 +00002187 TryConsumeToken(tok::semi);
Chris Lattner13901342010-07-11 22:42:07 +00002188 }
John McCalld5a36322009-11-03 19:26:08 +00002189 }
2190
Rafael Espindolaab417692013-07-09 12:05:01 +00002191 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +00002192}
2193
Richard Smith02e85f32011-04-14 22:09:26 +00002194/// Parse an optional simple-asm-expr and attributes, and attach them to a
2195/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00002196bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00002197 // If a simple-asm-expr is present, parse it.
2198 if (Tok.is(tok::kw_asm)) {
2199 SourceLocation Loc;
2200 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
2201 if (AsmLabel.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002202 SkipUntil(tok::semi, StopBeforeMatch);
Richard Smith02e85f32011-04-14 22:09:26 +00002203 return true;
2204 }
2205
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002206 D.setAsmLabel(AsmLabel.get());
Richard Smith02e85f32011-04-14 22:09:26 +00002207 D.SetRangeEnd(Loc);
2208 }
2209
2210 MaybeParseGNUAttributes(D);
2211 return false;
2212}
2213
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002214/// Parse 'declaration' after parsing 'declaration-specifiers
Douglas Gregor23996282009-05-12 21:31:51 +00002215/// declarator'. This method parses the remainder of the declaration
2216/// (including any attributes or initializer, among other things) and
2217/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00002218///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00002219/// init-declarator: [C99 6.7]
2220/// declarator
2221/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00002222/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
2223/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002224/// [C++] declarator initializer[opt]
2225///
2226/// [C++] initializer:
2227/// [C++] '=' initializer-clause
2228/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00002229/// [C++0x] '=' 'default' [TODO]
2230/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00002231/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00002232///
2233/// According to the standard grammar, =default and =delete are function
2234/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00002235///
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00002236Decl *Parser::ParseDeclarationAfterDeclarator(
2237 Declarator &D, const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00002238 if (ParseAsmAttributesAfterDeclarator(D))
Craig Topper161e4db2014-05-21 06:02:52 +00002239 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002240
Richard Smith02e85f32011-04-14 22:09:26 +00002241 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
2242}
Mike Stump11289f42009-09-09 15:08:12 +00002243
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00002244Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(
2245 Declarator &D, const ParsedTemplateInfo &TemplateInfo, ForRangeInit *FRI) {
Richard Smithc95d2c52017-09-22 04:25:05 +00002246 // RAII type used to track whether we're inside an initializer.
2247 struct InitializerScopeRAII {
2248 Parser &P;
2249 Declarator &D;
2250 Decl *ThisDecl;
2251
2252 InitializerScopeRAII(Parser &P, Declarator &D, Decl *ThisDecl)
2253 : P(P), D(D), ThisDecl(ThisDecl) {
2254 if (ThisDecl && P.getLangOpts().CPlusPlus) {
2255 Scope *S = nullptr;
2256 if (D.getCXXScopeSpec().isSet()) {
2257 P.EnterScope(0);
2258 S = P.getCurScope();
2259 }
2260 P.Actions.ActOnCXXEnterDeclInitializer(S, ThisDecl);
2261 }
2262 }
2263 ~InitializerScopeRAII() { pop(); }
2264 void pop() {
2265 if (ThisDecl && P.getLangOpts().CPlusPlus) {
2266 Scope *S = nullptr;
2267 if (D.getCXXScopeSpec().isSet())
2268 S = P.getCurScope();
2269 P.Actions.ActOnCXXExitDeclInitializer(S, ThisDecl);
2270 if (S)
2271 P.ExitScope();
2272 }
2273 ThisDecl = nullptr;
2274 }
2275 };
2276
Douglas Gregor23996282009-05-12 21:31:51 +00002277 // Inform the current actions module that we just parsed this declarator.
Craig Topper161e4db2014-05-21 06:02:52 +00002278 Decl *ThisDecl = nullptr;
Douglas Gregor450f00842009-09-25 18:43:00 +00002279 switch (TemplateInfo.Kind) {
2280 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002281 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00002282 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002283
Douglas Gregor450f00842009-09-25 18:43:00 +00002284 case ParsedTemplateInfo::Template:
Larisse Voufo39a1e502013-08-06 01:03:05 +00002285 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002286 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002287 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00002288 D);
Larisse Voufo833b05a2013-08-06 07:33:00 +00002289 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufo39a1e502013-08-06 01:03:05 +00002290 // Re-direct this decl to refer to the templated decl so that we can
2291 // initialize it.
2292 ThisDecl = VT->getTemplatedDecl();
2293 break;
2294 }
2295 case ParsedTemplateInfo::ExplicitInstantiation: {
2296 if (Tok.is(tok::semi)) {
2297 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
2298 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
2299 if (ThisRes.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002300 SkipUntil(tok::semi, StopBeforeMatch);
Craig Topper161e4db2014-05-21 06:02:52 +00002301 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00002302 }
2303 ThisDecl = ThisRes.get();
2304 } else {
2305 // FIXME: This check should be for a variable template instantiation only.
2306
2307 // Check that this is a valid instantiation
Faisal Vali2ab8c152017-12-30 04:15:27 +00002308 if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002309 // If the declarator-id is not a template-id, issue a diagnostic and
2310 // recover by ignoring the 'template' keyword.
2311 Diag(Tok, diag::err_template_defn_explicit_instantiation)
2312 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
2313 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
2314 } else {
2315 SourceLocation LAngleLoc =
2316 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
2317 Diag(D.getIdentifierLoc(),
2318 diag::err_explicit_instantiation_with_definition)
2319 << SourceRange(TemplateInfo.TemplateLoc)
2320 << FixItHint::CreateInsertion(LAngleLoc, "<>");
2321
2322 // Recover as if it were an explicit specialization.
2323 TemplateParameterLists FakedParamLists;
2324 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
Craig Topper96225a52015-12-24 23:58:25 +00002325 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, None,
Hubert Tongf608c052016-04-29 18:05:37 +00002326 LAngleLoc, nullptr));
Larisse Voufo39a1e502013-08-06 01:03:05 +00002327
2328 ThisDecl =
2329 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
2330 }
2331 }
Douglas Gregor450f00842009-09-25 18:43:00 +00002332 break;
2333 }
2334 }
Mike Stump11289f42009-09-09 15:08:12 +00002335
Douglas Gregor23996282009-05-12 21:31:51 +00002336 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00002337 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00002338 if (isTokenEqualOrEqualTypo()) {
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00002339 SourceLocation EqualLoc = ConsumeToken();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002340
Anders Carlsson991285e2010-09-24 21:25:25 +00002341 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002342 if (D.isFunctionDeclarator())
2343 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2344 << 1 /* delete */;
2345 else
2346 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00002347 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002348 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00002349 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2350 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002351 else
2352 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00002353 } else {
Richard Smithc95d2c52017-09-22 04:25:05 +00002354 InitializerScopeRAII InitScope(*this, D, ThisDecl);
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00002355
Douglas Gregor7aa6b222010-05-30 01:49:25 +00002356 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002357 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00002358 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002359 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002360 return nullptr;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00002361 }
Chad Rosierc1183952012-06-26 22:30:43 +00002362
Ilya Biryukov4f9543b2019-01-31 20:20:32 +00002363 PreferredType.enterVariableInit(Tok.getLocation(), ThisDecl);
2364 ExprResult Init = ParseInitializer();
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00002365
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00002366 // If this is the only decl in (possibly) range based for statement,
2367 // our best guess is that the user meant ':' instead of '='.
2368 if (Tok.is(tok::r_paren) && FRI && D.isFirstDeclarator()) {
2369 Diag(EqualLoc, diag::err_single_decl_assign_in_for_range)
2370 << FixItHint::CreateReplacement(EqualLoc, ":");
2371 // We are trying to stop parser from looking for ';' in this for
2372 // statement, therefore preventing spurious errors to be issued.
2373 FRI->ColonLoc = EqualLoc;
2374 Init = ExprError();
2375 FRI->RangeExpr = Init;
2376 }
2377
Richard Smithc95d2c52017-09-22 04:25:05 +00002378 InitScope.pop();
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00002379
Douglas Gregor23996282009-05-12 21:31:51 +00002380 if (Init.isInvalid()) {
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00002381 SmallVector<tok::TokenKind, 2> StopTokens;
2382 StopTokens.push_back(tok::comma);
Faisal Vali421b2d12017-12-29 05:41:00 +00002383 if (D.getContext() == DeclaratorContext::ForContext ||
2384 D.getContext() == DeclaratorContext::InitStmtContext)
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00002385 StopTokens.push_back(tok::r_paren);
2386 SkipUntil(StopTokens, StopAtSemi | StopBeforeMatch);
Douglas Gregor604c3022010-03-01 18:27:54 +00002387 Actions.ActOnInitializerError(ThisDecl);
2388 } else
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002389 Actions.AddInitializerToDecl(ThisDecl, Init.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00002390 /*DirectInit=*/false);
Douglas Gregor23996282009-05-12 21:31:51 +00002391 }
2392 } else if (Tok.is(tok::l_paren)) {
2393 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002394 BalancedDelimiterTracker T(*this, tok::l_paren);
2395 T.consumeOpen();
2396
Benjamin Kramerf0623432012-08-23 22:51:59 +00002397 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00002398 CommaLocsTy CommaLocs;
2399
Richard Smithc95d2c52017-09-22 04:25:05 +00002400 InitializerScopeRAII InitScope(*this, D, ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00002401
Ilya Biryukovcbea95d2017-09-08 13:36:38 +00002402 auto ThisVarDecl = dyn_cast_or_null<VarDecl>(ThisDecl);
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002403 auto RunSignatureHelp = [&]() {
Ilya Biryukov832c4af2018-09-07 14:04:39 +00002404 QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
Ilya Biryukovcbea95d2017-09-08 13:36:38 +00002405 getCurScope(), ThisVarDecl->getType()->getCanonicalTypeInternal(),
Ilya Biryukov2fab2352018-08-30 13:08:03 +00002406 ThisDecl->getLocation(), Exprs, T.getOpenLocation());
Kadir Cetinkayaa32d2532018-09-10 13:46:28 +00002407 CalledSignatureHelp = true;
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002408 return PreferredType;
Ilya Biryukovcbea95d2017-09-08 13:36:38 +00002409 };
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002410 auto SetPreferredType = [&] {
2411 PreferredType.enterFunctionArgument(Tok.getLocation(), RunSignatureHelp);
2412 };
2413
2414 llvm::function_ref<void()> ExpressionStarts;
Ilya Biryukovcbea95d2017-09-08 13:36:38 +00002415 if (ThisVarDecl) {
2416 // ParseExpressionList can sometimes succeed even when ThisDecl is not
2417 // VarDecl. This is an error and it is reported in a call to
2418 // Actions.ActOnInitializerError(). However, we call
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002419 // ProduceConstructorSignatureHelp only on VarDecls.
2420 ExpressionStarts = SetPreferredType;
Ilya Biryukovcbea95d2017-09-08 13:36:38 +00002421 }
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002422 if (ParseExpressionList(Exprs, CommaLocs, ExpressionStarts)) {
Kadir Cetinkayaa32d2532018-09-10 13:46:28 +00002423 if (ThisVarDecl && PP.isCodeCompletionReached() && !CalledSignatureHelp) {
2424 Actions.ProduceConstructorSignatureHelp(
2425 getCurScope(), ThisVarDecl->getType()->getCanonicalTypeInternal(),
2426 ThisDecl->getLocation(), Exprs, T.getOpenLocation());
2427 CalledSignatureHelp = true;
2428 }
David Blaikieeae04112012-10-10 23:15:05 +00002429 Actions.ActOnInitializerError(ThisDecl);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002430 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor23996282009-05-12 21:31:51 +00002431 } else {
2432 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002433 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00002434
2435 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
2436 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00002437
Richard Smithc95d2c52017-09-22 04:25:05 +00002438 InitScope.pop();
Douglas Gregor613bf102009-12-22 17:47:17 +00002439
Sebastian Redla9351792012-02-11 23:51:47 +00002440 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
2441 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002442 Exprs);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002443 Actions.AddInitializerToDecl(ThisDecl, Initializer.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00002444 /*DirectInit=*/true);
Douglas Gregor23996282009-05-12 21:31:51 +00002445 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002446 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00002447 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00002448 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00002449 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2450
Richard Smithc95d2c52017-09-22 04:25:05 +00002451 InitializerScopeRAII InitScope(*this, D, ThisDecl);
Sebastian Redl3da34892011-06-05 12:23:16 +00002452
2453 ExprResult Init(ParseBraceInitializer());
2454
Richard Smithc95d2c52017-09-22 04:25:05 +00002455 InitScope.pop();
Sebastian Redl3da34892011-06-05 12:23:16 +00002456
2457 if (Init.isInvalid()) {
2458 Actions.ActOnInitializerError(ThisDecl);
2459 } else
Richard Smith3beb7c62017-01-12 02:27:38 +00002460 Actions.AddInitializerToDecl(ThisDecl, Init.get(), /*DirectInit=*/true);
Sebastian Redl3da34892011-06-05 12:23:16 +00002461
Douglas Gregor23996282009-05-12 21:31:51 +00002462 } else {
Richard Smith3beb7c62017-01-12 02:27:38 +00002463 Actions.ActOnUninitializedDecl(ThisDecl);
Douglas Gregor23996282009-05-12 21:31:51 +00002464 }
2465
Richard Smithb2bc2e62011-02-21 20:05:19 +00002466 Actions.FinalizeDeclaration(ThisDecl);
2467
Douglas Gregor23996282009-05-12 21:31:51 +00002468 return ThisDecl;
2469}
2470
Chris Lattner1890ac82006-08-13 01:16:23 +00002471/// ParseSpecifierQualifierList
2472/// specifier-qualifier-list:
2473/// type-specifier specifier-qualifier-list[opt]
2474/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002475/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00002476///
Richard Smithc5b05522012-03-12 07:56:15 +00002477void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
2478 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00002479 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
2480 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002481 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Faisal Valia534f072018-04-26 00:42:40 +00002482 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00002483
Chris Lattner1890ac82006-08-13 01:16:23 +00002484 // Validate declspec for type-name.
2485 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith649c7b062014-01-08 00:56:48 +00002486 if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00002487 Diag(Tok, diag::err_expected_type);
2488 DS.SetTypeSpecError();
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002489 } else if (Specs == DeclSpec::PQ_None && !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00002490 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00002491 if (!DS.hasTypeSpecifier())
2492 DS.SetTypeSpecError();
2493 }
Mike Stump11289f42009-09-09 15:08:12 +00002494
Chris Lattner1b22eed2006-11-28 05:12:07 +00002495 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002496 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00002497 if (DS.getStorageClassSpecLoc().isValid())
2498 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
2499 else
Richard Smithb4a9e862013-04-12 22:46:28 +00002500 Diag(DS.getThreadStorageClassSpecLoc(),
2501 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00002502 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002503 }
Mike Stump11289f42009-09-09 15:08:12 +00002504
Craig Topper3f13d4d22015-11-14 18:15:55 +00002505 // Issue diagnostic and remove function specifier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002506 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00002507 if (DS.isInlineSpecified())
2508 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
2509 if (DS.isVirtualSpecified())
2510 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
Richard Smith76b90272019-05-09 03:59:21 +00002511 if (DS.hasExplicitSpecifier())
Douglas Gregor61956c42008-10-31 09:07:45 +00002512 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00002513 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002514 }
Richard Smithc5b05522012-03-12 07:56:15 +00002515
Richard Smith76b90272019-05-09 03:59:21 +00002516 // Issue diagnostic and remove constexpr specifier if present.
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002517 if (DS.hasConstexprSpecifier() && DSC != DeclSpecContext::DSC_condition) {
2518 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr)
Richard Smitha6e8b682019-09-04 20:30:37 +00002519 << DS.getConstexprSpecifier();
Richard Smithc5b05522012-03-12 07:56:15 +00002520 DS.ClearConstexprSpec();
2521 }
Chris Lattner1890ac82006-08-13 01:16:23 +00002522}
Chris Lattner53361ac2006-08-10 05:19:57 +00002523
Chris Lattner6cc055a2009-04-12 20:42:31 +00002524/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2525/// specified token is valid after the identifier in a declarator which
2526/// immediately follows the declspec. For example, these things are valid:
2527///
2528/// int x [ 4]; // direct-declarator
2529/// int x ( int y); // direct-declarator
2530/// int(int x ) // direct-declarator
2531/// int x ; // simple-declaration
2532/// int x = 17; // init-declarator-list
2533/// int x , y; // init-declarator-list
2534/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002535/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002536/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002537///
2538/// This is not, because 'x' does not immediately follow the declspec (though
2539/// ')' happens to be valid anyway).
2540/// int (x)
2541///
2542static bool isValidAfterIdentifierInDeclarator(const Token &T) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002543 return T.isOneOf(tok::l_square, tok::l_paren, tok::r_paren, tok::semi,
2544 tok::comma, tok::equal, tok::kw_asm, tok::l_brace,
2545 tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002546}
2547
Chris Lattner20a0c612009-04-14 21:34:55 +00002548/// ParseImplicitInt - This method is called when we have an non-typename
2549/// identifier in a declspec (which normally terminates the decl spec) when
2550/// the declspec has no type specifier. In this case, the declspec is either
2551/// malformed or is "implicit int" (in K&R and C89).
2552///
2553/// This method handles diagnosing this prettily and returns false if the
2554/// declspec is done being processed. If it recovers and thinks there may be
2555/// other pieces of declspec after it, it returns true.
2556///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002557bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002558 const ParsedTemplateInfo &TemplateInfo,
Richard Smitha0a5d502014-05-08 22:32:00 +00002559 AccessSpecifier AS, DeclSpecContext DSC,
Michael Han9407e502012-11-26 22:54:45 +00002560 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002561 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002562
Chris Lattner20a0c612009-04-14 21:34:55 +00002563 SourceLocation Loc = Tok.getLocation();
2564 // If we see an identifier that is not a type name, we normally would
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002565 // parse it as the identifier being declared. However, when a typename
Chris Lattner20a0c612009-04-14 21:34:55 +00002566 // is typo'd or the definition is not included, this will incorrectly
2567 // parse the typename as the identifier name and fall over misparsing
2568 // later parts of the diagnostic.
2569 //
2570 // As such, we try to do some look-ahead in cases where this would
2571 // otherwise be an "implicit-int" case to see if this is invalid. For
2572 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2573 // an identifier with implicit int, we'd get a parse error because the
2574 // next token is obviously invalid for a type. Parse these as a case
2575 // with an invalid type specifier.
2576 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002577
Chris Lattner20a0c612009-04-14 21:34:55 +00002578 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002579 // error, do lookahead to try to do better recovery. This never applies
2580 // within a type specifier. Outside of C++, we allow this even if the
2581 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002582 // implicit int as an extension in C99 and C11.
Richard Smith649c7b062014-01-08 00:56:48 +00002583 if (!isTypeSpecifier(DSC) && !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002584 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002585 // If this token is valid for implicit int, e.g. "static x = 4", then
2586 // we just avoid eating the identifier, so it will be parsed as the
2587 // identifier in the declarator.
2588 return false;
2589 }
Mike Stump11289f42009-09-09 15:08:12 +00002590
Sven van Haastregte518bb42019-05-22 13:12:20 +00002591 // Early exit as Sema has a dedicated missing_actual_pipe_type diagnostic
2592 // for incomplete declarations such as `pipe p`.
2593 if (getLangOpts().OpenCLCPlusPlus && DS.isTypeSpecPipe())
2594 return false;
2595
Richard Smitha952ebb2012-05-15 21:01:51 +00002596 if (getLangOpts().CPlusPlus &&
2597 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2598 // Don't require a type specifier if we have the 'auto' storage class
2599 // specifier in C++98 -- we'll promote it to a type specifier.
Richard Smithfb8b7b92013-10-15 00:00:26 +00002600 if (SS)
2601 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
Richard Smitha952ebb2012-05-15 21:01:51 +00002602 return false;
2603 }
2604
Reid Kleckner9a96e422016-05-24 21:23:54 +00002605 if (getLangOpts().CPlusPlus && (!SS || SS->isEmpty()) &&
2606 getLangOpts().MSVCCompat) {
2607 // Lookup of an unqualified type name has failed in MSVC compatibility mode.
2608 // Give Sema a chance to recover if we are in a template with dependent base
2609 // classes.
2610 if (ParsedType T = Actions.ActOnMSVCUnknownTypeName(
2611 *Tok.getIdentifierInfo(), Tok.getLocation(),
Faisal Vali7db85c52017-12-31 00:06:40 +00002612 DSC == DeclSpecContext::DSC_template_type_arg)) {
Reid Kleckner9a96e422016-05-24 21:23:54 +00002613 const char *PrevSpec;
2614 unsigned DiagID;
2615 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
2616 Actions.getASTContext().getPrintingPolicy());
2617 DS.SetRangeEnd(Tok.getLocation());
2618 ConsumeToken();
2619 return false;
2620 }
2621 }
2622
Chris Lattner20a0c612009-04-14 21:34:55 +00002623 // Otherwise, if we don't consume this token, we are going to emit an
2624 // error anyway. Try to recover from various common problems. Check
2625 // to see if this was a reference to a tag name without a tag specified.
2626 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002627 //
2628 // C++ doesn't need this, and isTagName doesn't take SS.
Craig Topper161e4db2014-05-21 06:02:52 +00002629 if (SS == nullptr) {
2630 const char *TagName = nullptr, *FixitTagName = nullptr;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002631 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002632
Douglas Gregor0be31a22010-07-02 17:43:08 +00002633 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002634 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002635 case DeclSpec::TST_enum:
2636 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2637 case DeclSpec::TST_union:
2638 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2639 case DeclSpec::TST_struct:
2640 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002641 case DeclSpec::TST_interface:
2642 TagName="__interface"; FixitTagName = "__interface ";
2643 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002644 case DeclSpec::TST_class:
2645 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002646 }
Mike Stump11289f42009-09-09 15:08:12 +00002647
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002648 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002649 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2650 LookupResult R(Actions, TokenName, SourceLocation(),
2651 Sema::LookupOrdinaryName);
2652
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002653 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002654 << TokenName << TagName << getLangOpts().CPlusPlus
2655 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2656
2657 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2658 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2659 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002660 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002661 << TokenName << TagName;
2662 }
Mike Stump11289f42009-09-09 15:08:12 +00002663
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002664 // Parse this as a tag as if the missing tag were present.
2665 if (TagKind == tok::kw_enum)
Faisal Vali7db85c52017-12-31 00:06:40 +00002666 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS,
2667 DeclSpecContext::DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002668 else
Richard Smithc5b05522012-03-12 07:56:15 +00002669 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Faisal Vali7db85c52017-12-31 00:06:40 +00002670 /*EnteringContext*/ false,
2671 DeclSpecContext::DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002672 return true;
2673 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002674 }
Mike Stump11289f42009-09-09 15:08:12 +00002675
Richard Smithfe904f02012-05-15 21:29:55 +00002676 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002677 // being declared (with a missing type).
Faisal Vali7db85c52017-12-31 00:06:40 +00002678 if (!isTypeSpecifier(DSC) && (!SS || DSC == DeclSpecContext::DSC_top_level ||
2679 DSC == DeclSpecContext::DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002680 // Look ahead to the next token to try to figure out what this declaration
2681 // was supposed to be.
2682 switch (NextToken().getKind()) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002683 case tok::l_paren: {
2684 // static x(4); // 'x' is not a type
2685 // x(int n); // 'x' is not a type
2686 // x (*p)[]; // 'x' is a type
2687 //
Richard Smitha0a5d502014-05-08 22:32:00 +00002688 // Since we're in an error case, we can afford to perform a tentative
2689 // parse to determine which case we're in.
Richard Smitha952ebb2012-05-15 21:01:51 +00002690 TentativeParsingAction PA(*this);
2691 ConsumeToken();
2692 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2693 PA.Revert();
Richard Smithfb8b7b92013-10-15 00:00:26 +00002694
Richard Smithee390432014-05-16 01:56:53 +00002695 if (TPR != TPResult::False) {
Richard Smithfb8b7b92013-10-15 00:00:26 +00002696 // The identifier is followed by a parenthesized declarator.
2697 // It's supposed to be a type.
2698 break;
2699 }
2700
2701 // If we're in a context where we could be declaring a constructor,
2702 // check whether this is a constructor declaration with a bogus name.
Faisal Vali7db85c52017-12-31 00:06:40 +00002703 if (DSC == DeclSpecContext::DSC_class ||
2704 (DSC == DeclSpecContext::DSC_top_level && SS)) {
Richard Smithfb8b7b92013-10-15 00:00:26 +00002705 IdentifierInfo *II = Tok.getIdentifierInfo();
2706 if (Actions.isCurrentClassNameTypo(II, SS)) {
2707 Diag(Loc, diag::err_constructor_bad_name)
2708 << Tok.getIdentifierInfo() << II
2709 << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
2710 Tok.setIdentifierInfo(II);
2711 }
2712 }
2713 // Fall through.
Galina Kistanova77674252017-06-01 21:15:34 +00002714 LLVM_FALLTHROUGH;
Richard Smitha952ebb2012-05-15 21:01:51 +00002715 }
Richard Smithfb8b7b92013-10-15 00:00:26 +00002716 case tok::comma:
2717 case tok::equal:
2718 case tok::kw_asm:
2719 case tok::l_brace:
2720 case tok::l_square:
2721 case tok::semi:
2722 // This looks like a variable or function declaration. The type is
2723 // probably missing. We're done parsing decl-specifiers.
Nicolas Lesseree057172019-05-05 12:15:17 +00002724 // But only if we are not in a function prototype scope.
2725 if (getCurScope()->isFunctionPrototypeScope())
2726 break;
Richard Smithfb8b7b92013-10-15 00:00:26 +00002727 if (SS)
2728 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2729 return false;
Richard Smitha952ebb2012-05-15 21:01:51 +00002730
2731 default:
2732 // This is probably supposed to be a type. This includes cases like:
2733 // int f(itn);
2734 // struct S { unsinged : 4; };
2735 break;
2736 }
2737 }
2738
Reid Klecknerc05ca5e2014-06-19 01:23:22 +00002739 // This is almost certainly an invalid type name. Let Sema emit a diagnostic
2740 // and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002741 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002742 IdentifierInfo *II = Tok.getIdentifierInfo();
Richard Smith52f8d192017-05-10 21:32:16 +00002743 bool IsTemplateName = getLangOpts().CPlusPlus && NextToken().is(tok::less);
Reid Klecknerc05ca5e2014-06-19 01:23:22 +00002744 Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T,
Richard Smith52f8d192017-05-10 21:32:16 +00002745 IsTemplateName);
Reid Klecknerc05ca5e2014-06-19 01:23:22 +00002746 if (T) {
2747 // The action has suggested that the type T could be used. Set that as
2748 // the type in the declaration specifiers, consume the would-be type
2749 // name token, and we're done.
2750 const char *PrevSpec;
2751 unsigned DiagID;
2752 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
2753 Actions.getASTContext().getPrintingPolicy());
2754 DS.SetRangeEnd(Tok.getLocation());
2755 ConsumeToken();
2756 // There may be other declaration specifiers after this.
2757 return true;
2758 } else if (II != Tok.getIdentifierInfo()) {
2759 // If no type was suggested, the correction is to a keyword
2760 Tok.setKind(II->getTokenID());
2761 // There may be other declaration specifiers after this.
2762 return true;
Douglas Gregor15e56022009-10-13 23:27:22 +00002763 }
Mike Stump11289f42009-09-09 15:08:12 +00002764
Reid Klecknerc05ca5e2014-06-19 01:23:22 +00002765 // Otherwise, the action had no suggestion for us. Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002766 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002767 DS.SetRangeEnd(Tok.getLocation());
2768 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002769
Richard Smith52f8d192017-05-10 21:32:16 +00002770 // Eat any following template arguments.
2771 if (IsTemplateName) {
2772 SourceLocation LAngle, RAngle;
2773 TemplateArgList Args;
2774 ParseTemplateIdAfterTemplateName(true, LAngle, Args, RAngle);
2775 }
2776
Chris Lattner20a0c612009-04-14 21:34:55 +00002777 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2778 // avoid rippling error messages on subsequent uses of the same type,
2779 // could be useful if #include was forgotten.
Richard Smithb3065792019-05-07 07:36:07 +00002780 return true;
Chris Lattner20a0c612009-04-14 21:34:55 +00002781}
2782
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002783/// Determine the declaration specifier context from the declarator
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002784/// context.
2785///
2786/// \param Context the declarator context, which is one of the
Faisal Vali421b2d12017-12-29 05:41:00 +00002787/// DeclaratorContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002788Parser::DeclSpecContext
Faisal Vali421b2d12017-12-29 05:41:00 +00002789Parser::getDeclSpecContextFromDeclaratorContext(DeclaratorContext Context) {
2790 if (Context == DeclaratorContext::MemberContext)
Faisal Vali7db85c52017-12-31 00:06:40 +00002791 return DeclSpecContext::DSC_class;
Faisal Vali421b2d12017-12-29 05:41:00 +00002792 if (Context == DeclaratorContext::FileContext)
Faisal Vali7db85c52017-12-31 00:06:40 +00002793 return DeclSpecContext::DSC_top_level;
Faisal Vali421b2d12017-12-29 05:41:00 +00002794 if (Context == DeclaratorContext::TemplateParamContext)
Faisal Vali7db85c52017-12-31 00:06:40 +00002795 return DeclSpecContext::DSC_template_param;
Richard Smith77a9c602018-02-28 03:02:23 +00002796 if (Context == DeclaratorContext::TemplateArgContext ||
2797 Context == DeclaratorContext::TemplateTypeArgContext)
Faisal Vali7db85c52017-12-31 00:06:40 +00002798 return DeclSpecContext::DSC_template_type_arg;
Richard Smithe303e352018-02-02 22:24:54 +00002799 if (Context == DeclaratorContext::TrailingReturnContext ||
2800 Context == DeclaratorContext::TrailingReturnVarContext)
Faisal Vali7db85c52017-12-31 00:06:40 +00002801 return DeclSpecContext::DSC_trailing;
Faisal Vali421b2d12017-12-29 05:41:00 +00002802 if (Context == DeclaratorContext::AliasDeclContext ||
2803 Context == DeclaratorContext::AliasTemplateContext)
Faisal Vali7db85c52017-12-31 00:06:40 +00002804 return DeclSpecContext::DSC_alias_declaration;
2805 return DeclSpecContext::DSC_normal;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002806}
2807
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002808/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2809///
2810/// FIXME: Simply returns an alignof() expression if the argument is a
2811/// type. Ideally, the type should be propagated directly into Sema.
2812///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002813/// [C11] type-id
2814/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002815/// [C++0x] type-id ...[opt]
2816/// [C++0x] assignment-expression ...[opt]
2817ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2818 SourceLocation &EllipsisLoc) {
2819 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002820 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002821 SourceLocation TypeLoc = Tok.getLocation();
2822 ParsedType Ty = ParseTypeName().get();
2823 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002824 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2825 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002826 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002827 ER = ParseConstantExpression();
2828
Alp Toker8fbec672013-12-17 23:29:36 +00002829 if (getLangOpts().CPlusPlus11)
2830 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002831
2832 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002833}
2834
2835/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2836/// attribute to Attrs.
2837///
2838/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002839/// [C11] '_Alignas' '(' type-id ')'
2840/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002841/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2842/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002843void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002844 SourceLocation *EndLoc) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002845 assert(Tok.isOneOf(tok::kw_alignas, tok::kw__Alignas) &&
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002846 "Not an alignment-specifier!");
2847
Richard Smithd11c7a12013-01-29 01:48:07 +00002848 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2849 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002850
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002851 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00002852 if (T.expectAndConsume())
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002853 return;
2854
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002855 SourceLocation EllipsisLoc;
2856 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002857 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002858 T.skipToEnd();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002859 return;
2860 }
2861
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002862 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002863 if (EndLoc)
2864 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002865
Aaron Ballman00e99962013-08-31 01:11:41 +00002866 ArgsVector ArgExprs;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002867 ArgExprs.push_back(ArgExpr.get());
Craig Topper161e4db2014-05-21 06:02:52 +00002868 Attrs.addNew(KWName, KWLoc, nullptr, KWLoc, ArgExprs.data(), 1,
Erich Keanee891aa92018-07-13 15:07:47 +00002869 ParsedAttr::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002870}
2871
Richard Smith404dfb42013-11-19 22:47:36 +00002872/// Determine whether we're looking at something that might be a declarator
2873/// in a simple-declaration. If it can't possibly be a declarator, maybe
2874/// diagnose a missing semicolon after a prior tag definition in the decl
2875/// specifier.
2876///
2877/// \return \c true if an error occurred and this can't be any kind of
2878/// declaration.
2879bool
2880Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
2881 DeclSpecContext DSContext,
2882 LateParsedAttrList *LateAttrs) {
2883 assert(DS.hasTagDefinition() && "shouldn't call this");
2884
Faisal Vali7db85c52017-12-31 00:06:40 +00002885 bool EnteringContext = (DSContext == DeclSpecContext::DSC_class ||
2886 DSContext == DeclSpecContext::DSC_top_level);
Richard Smith404dfb42013-11-19 22:47:36 +00002887
2888 if (getLangOpts().CPlusPlus &&
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002889 Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype,
2890 tok::annot_template_id) &&
Richard Smith404dfb42013-11-19 22:47:36 +00002891 TryAnnotateCXXScopeToken(EnteringContext)) {
2892 SkipMalformedDecl();
2893 return true;
2894 }
2895
Richard Smith698875a2013-11-20 23:40:57 +00002896 bool HasScope = Tok.is(tok::annot_cxxscope);
2897 // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
2898 Token AfterScope = HasScope ? NextToken() : Tok;
2899
Richard Smith404dfb42013-11-19 22:47:36 +00002900 // Determine whether the following tokens could possibly be a
2901 // declarator.
Richard Smith698875a2013-11-20 23:40:57 +00002902 bool MightBeDeclarator = true;
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002903 if (Tok.isOneOf(tok::kw_typename, tok::annot_typename)) {
Richard Smith698875a2013-11-20 23:40:57 +00002904 // A declarator-id can't start with 'typename'.
2905 MightBeDeclarator = false;
2906 } else if (AfterScope.is(tok::annot_template_id)) {
2907 // If we have a type expressed as a template-id, this cannot be a
2908 // declarator-id (such a type cannot be redeclared in a simple-declaration).
2909 TemplateIdAnnotation *Annot =
2910 static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
2911 if (Annot->Kind == TNK_Type_template)
2912 MightBeDeclarator = false;
2913 } else if (AfterScope.is(tok::identifier)) {
2914 const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken();
2915
Richard Smith404dfb42013-11-19 22:47:36 +00002916 // These tokens cannot come after the declarator-id in a
2917 // simple-declaration, and are likely to come after a type-specifier.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002918 if (Next.isOneOf(tok::star, tok::amp, tok::ampamp, tok::identifier,
2919 tok::annot_cxxscope, tok::coloncolon)) {
Richard Smith698875a2013-11-20 23:40:57 +00002920 // Missing a semicolon.
2921 MightBeDeclarator = false;
2922 } else if (HasScope) {
2923 // If the declarator-id has a scope specifier, it must redeclare a
2924 // previously-declared entity. If that's a type (and this is not a
2925 // typedef), that's an error.
2926 CXXScopeSpec SS;
2927 Actions.RestoreNestedNameSpecifierAnnotation(
2928 Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
2929 IdentifierInfo *Name = AfterScope.getIdentifierInfo();
2930 Sema::NameClassification Classification = Actions.ClassifyName(
2931 getCurScope(), SS, Name, AfterScope.getLocation(), Next,
Richard Smith7e8fe672019-10-14 21:53:03 +00002932 /*CCC=*/nullptr);
Richard Smith698875a2013-11-20 23:40:57 +00002933 switch (Classification.getKind()) {
2934 case Sema::NC_Error:
2935 SkipMalformedDecl();
2936 return true;
Richard Smith404dfb42013-11-19 22:47:36 +00002937
Richard Smith698875a2013-11-20 23:40:57 +00002938 case Sema::NC_Keyword:
Richard Smith7e8fe672019-10-14 21:53:03 +00002939 llvm_unreachable("typo correction is not possible here");
Richard Smith404dfb42013-11-19 22:47:36 +00002940
Richard Smith698875a2013-11-20 23:40:57 +00002941 case Sema::NC_Type:
2942 case Sema::NC_TypeTemplate:
Richard Smith7e8fe672019-10-14 21:53:03 +00002943 case Sema::NC_UndeclaredNonType:
2944 case Sema::NC_UndeclaredTemplate:
Richard Smith698875a2013-11-20 23:40:57 +00002945 // Not a previously-declared non-type entity.
2946 MightBeDeclarator = false;
2947 break;
Richard Smith404dfb42013-11-19 22:47:36 +00002948
Richard Smith698875a2013-11-20 23:40:57 +00002949 case Sema::NC_Unknown:
Richard Smith7e8fe672019-10-14 21:53:03 +00002950 case Sema::NC_NonType:
2951 case Sema::NC_DependentNonType:
2952 case Sema::NC_ContextIndependentExpr:
Richard Smith698875a2013-11-20 23:40:57 +00002953 case Sema::NC_VarTemplate:
2954 case Sema::NC_FunctionTemplate:
2955 // Might be a redeclaration of a prior entity.
2956 break;
2957 }
Richard Smith404dfb42013-11-19 22:47:36 +00002958 }
Richard Smith404dfb42013-11-19 22:47:36 +00002959 }
2960
Richard Smith698875a2013-11-20 23:40:57 +00002961 if (MightBeDeclarator)
Richard Smith404dfb42013-11-19 22:47:36 +00002962 return false;
2963
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002964 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002965 Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getEndLoc()),
Alp Toker383d2c42014-01-01 03:08:43 +00002966 diag::err_expected_after)
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002967 << DeclSpec::getSpecifierName(DS.getTypeSpecType(), PPol) << tok::semi;
Richard Smith404dfb42013-11-19 22:47:36 +00002968
2969 // Try to recover from the typo, by dropping the tag definition and parsing
2970 // the problematic tokens as a type.
2971 //
2972 // FIXME: Split the DeclSpec into pieces for the standalone
2973 // declaration and pieces for the following declaration, instead
2974 // of assuming that all the other pieces attach to new declaration,
2975 // and call ParsedFreeStandingDeclSpec as appropriate.
2976 DS.ClearTypeSpecType();
2977 ParsedTemplateInfo NotATemplate;
Faisal Valia534f072018-04-26 00:42:40 +00002978 ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
Richard Smith404dfb42013-11-19 22:47:36 +00002979 return false;
2980}
2981
Leonard Chanab80f3c2018-06-14 14:53:51 +00002982// Choose the apprpriate diagnostic error for why fixed point types are
2983// disabled, set the previous specifier, and mark as invalid.
2984static void SetupFixedPointError(const LangOptions &LangOpts,
2985 const char *&PrevSpec, unsigned &DiagID,
2986 bool &isInvalid) {
2987 assert(!LangOpts.FixedPoint);
2988 DiagID = diag::err_fixed_point_not_enabled;
2989 PrevSpec = ""; // Not used by diagnostic
2990 isInvalid = true;
2991}
2992
Faisal Valia534f072018-04-26 00:42:40 +00002993/// ParseDeclarationSpecifiers
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002994/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002995/// storage-class-specifier declaration-specifiers[opt]
2996/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002997/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002998/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002999/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00003000/// [Clang] '__module_private__' declaration-specifiers[opt]
Douglas Gregorab209d82015-07-07 03:58:42 +00003001/// [ObjC1] '__kindof' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003002///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003003/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00003004/// 'typedef'
3005/// 'extern'
3006/// 'static'
3007/// 'auto'
3008/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00003009/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00003010/// [C++11] 'thread_local'
3011/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00003012/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003013/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00003014/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00003015/// [C++] 'virtual'
3016/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003017/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00003018/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00003019/// 'constexpr': [C++0x dcl.constexpr]
Faisal Valia534f072018-04-26 00:42:40 +00003020void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00003021 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00003022 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00003023 DeclSpecContext DSContext,
3024 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00003025 if (DS.getSourceRange().isInvalid()) {
David Majnemer24b28302014-07-26 05:41:31 +00003026 // Start the range at the current token but make the end of the range
3027 // invalid. This will make the entire range invalid unless we successfully
3028 // consume a token.
Douglas Gregor0e7dde52011-04-24 05:37:28 +00003029 DS.SetRangeStart(Tok.getLocation());
David Majnemer24b28302014-07-26 05:41:31 +00003030 DS.SetRangeEnd(SourceLocation());
Douglas Gregor0e7dde52011-04-24 05:37:28 +00003031 }
Chad Rosierc1183952012-06-26 22:30:43 +00003032
Faisal Vali7db85c52017-12-31 00:06:40 +00003033 bool EnteringContext = (DSContext == DeclSpecContext::DSC_class ||
3034 DSContext == DeclSpecContext::DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003035 bool AttrsLastTime = false;
3036 ParsedAttributesWithRange attrs(AttrFactory);
Benjamin Kramere4812142015-03-12 14:28:38 +00003037 // We use Sema's policy to get bool macros right.
Richard Smith301bc212016-05-19 01:39:10 +00003038 PrintingPolicy Policy = Actions.getPrintingPolicy();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003039 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00003040 bool isInvalid = false;
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003041 bool isStorageClass = false;
Craig Topper161e4db2014-05-21 06:02:52 +00003042 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +00003043 unsigned DiagID = 0;
3044
Richard Smithba24f352019-05-09 19:45:46 +00003045 // This value needs to be set to the location of the last token if the last
3046 // token of the specifier is already consumed.
3047 SourceLocation ConsumedEnd;
Richard Smith76b90272019-05-09 03:59:21 +00003048
David Majnemer51fd8a02015-07-22 23:46:18 +00003049 // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL
3050 // implementation for VS2013 uses _Atomic as an identifier for one of the
3051 // classes in <atomic>.
3052 //
3053 // A typedef declaration containing _Atomic<...> is among the places where
3054 // the class is used. If we are currently parsing such a declaration, treat
3055 // the token as an identifier.
3056 if (getLangOpts().MSVCCompat && Tok.is(tok::kw__Atomic) &&
3057 DS.getStorageClassSpec() == clang::DeclSpec::SCS_typedef &&
3058 !DS.hasTypeSpecifier() && GetLookAheadToken(1).is(tok::less))
3059 Tok.setKind(tok::identifier);
3060
Chris Lattner4d8f8732006-11-28 05:05:08 +00003061 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00003062
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003063 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00003064 default:
Chris Lattner0974b232008-07-26 00:20:22 +00003065 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003066 if (!AttrsLastTime)
3067 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00003068 else {
3069 // Reject C++11 attributes that appertain to decl specifiers as
3070 // we don't support any C++11 attributes that appertain to decl
3071 // specifiers. This also conforms to what g++ 4.8 is doing.
Richard Smith49cc1cc2016-08-18 21:59:42 +00003072 ProhibitCXX11Attributes(attrs, diag::err_attribute_not_type_attr);
Michael Han64536a62012-11-06 19:34:54 +00003073
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003074 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00003075 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00003076
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003077 // If this is not a declaration specifier token, we're done reading decl
3078 // specifiers. First verify that DeclSpec's are consistent.
Craig Topper25122412015-11-15 03:32:11 +00003079 DS.Finish(Actions, Policy);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003080 return;
Mike Stump11289f42009-09-09 15:08:12 +00003081
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003082 case tok::l_square:
3083 case tok::kw_alignas:
Aaron Ballman606093a2017-10-15 15:01:42 +00003084 if (!standardAttributesAllowed() || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003085 goto DoneWithDeclSpec;
3086
3087 ProhibitAttributes(attrs);
3088 // FIXME: It would be good to recover by accepting the attributes,
3089 // but attempting to do that now would cause serious
3090 // madness in terms of diagnostics.
3091 attrs.clear();
3092 attrs.Range = SourceRange();
3093
3094 ParseCXX11Attributes(attrs);
3095 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00003096 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003097
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003098 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00003099 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003100 if (DS.hasTypeSpecifier()) {
3101 bool AllowNonIdentifiers
3102 = (getCurScope()->getFlags() & (Scope::ControlScope |
3103 Scope::BlockScope |
3104 Scope::TemplateParamScope |
3105 Scope::FunctionPrototypeScope |
3106 Scope::AtCatchScope)) == 0;
3107 bool AllowNestedNameSpecifiers
Faisal Vali7db85c52017-12-31 00:06:40 +00003108 = DSContext == DeclSpecContext::DSC_top_level ||
3109 (DSContext == DeclSpecContext::DSC_class && DS.isFriendSpecified());
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003110
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00003111 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00003112 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00003113 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003114 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00003115 }
3116
Douglas Gregor80039242011-02-15 20:33:25 +00003117 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
3118 CCC = Sema::PCC_LocalDeclarationSpecifiers;
3119 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Faisal Vali7db85c52017-12-31 00:06:40 +00003120 CCC = DSContext == DeclSpecContext::DSC_class ? Sema::PCC_MemberTemplate
3121 : Sema::PCC_Template;
3122 else if (DSContext == DeclSpecContext::DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00003123 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00003124 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00003125 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00003126
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003127 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003128 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003129 }
3130
Chris Lattnerbd31aa32009-01-05 00:07:25 +00003131 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00003132 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman2a1d9a92013-08-15 23:59:20 +00003133 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00003134 if (!DS.hasTypeSpecifier())
3135 DS.SetTypeSpecError();
3136 goto DoneWithDeclSpec;
3137 }
John McCall8bc2a702010-03-01 18:20:46 +00003138 if (Tok.is(tok::coloncolon)) // ::new or ::delete
3139 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00003140 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003141
3142 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00003143 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003144 goto DoneWithDeclSpec;
3145
John McCall9dab4e62009-12-12 11:40:51 +00003146 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00003147 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
3148 Tok.getAnnotationRange(),
3149 SS);
John McCall9dab4e62009-12-12 11:40:51 +00003150
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003151 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00003152 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00003153 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00003154 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00003155 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00003156 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003157
Richard Smith74f02342017-01-19 21:00:13 +00003158 // If this would be a valid constructor declaration with template
3159 // arguments, we will reject the attempt to form an invalid type-id
3160 // referring to the injected-class-name when we annotate the token,
3161 // per C++ [class.qual]p2.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003162 //
Richard Smith74f02342017-01-19 21:00:13 +00003163 // To improve diagnostics for this case, parse the declaration as a
3164 // constructor (and reject the extra template arguments later).
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00003165 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Faisal Vali7db85c52017-12-31 00:06:40 +00003166 if ((DSContext == DeclSpecContext::DSC_top_level ||
3167 DSContext == DeclSpecContext::DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00003168 TemplateId->Name &&
Richard Smith74f02342017-01-19 21:00:13 +00003169 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS) &&
Faisal Vali7db85c52017-12-31 00:06:40 +00003170 isConstructorDeclarator(/*Unqualified*/ false)) {
Richard Smith74f02342017-01-19 21:00:13 +00003171 // The user meant this to be an out-of-line constructor
3172 // definition, but template arguments are not allowed
3173 // there. Just allow this as a constructor; we'll
3174 // complain about it later.
3175 goto DoneWithDeclSpec;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003176 }
3177
John McCall9dab4e62009-12-12 11:40:51 +00003178 DS.getTypeSpecScope() = SS;
Richard Smithaf3b3252017-05-18 19:21:48 +00003179 ConsumeAnnotationToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00003180 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00003181 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00003182 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00003183 continue;
3184 }
3185
Douglas Gregorc5790df2009-09-28 07:26:33 +00003186 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00003187 DS.getTypeSpecScope() = SS;
Richard Smithaf3b3252017-05-18 19:21:48 +00003188 ConsumeAnnotationToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00003189 if (Tok.getAnnotationValue()) {
3190 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00003191 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00003192 Tok.getAnnotationEndLoc(),
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003193 PrevSpec, DiagID, T, Policy);
Richard Smithda837032012-09-14 18:27:01 +00003194 if (isInvalid)
3195 break;
John McCallba7bf592010-08-24 05:47:05 +00003196 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00003197 else
3198 DS.SetTypeSpecError();
3199 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
Richard Smithaf3b3252017-05-18 19:21:48 +00003200 ConsumeAnnotationToken(); // The typename
Douglas Gregorc5790df2009-09-28 07:26:33 +00003201 }
3202
Douglas Gregor167fa622009-03-25 15:40:00 +00003203 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003204 goto DoneWithDeclSpec;
3205
Richard Smith74f02342017-01-19 21:00:13 +00003206 // Check whether this is a constructor declaration. If we're in a
3207 // context where the identifier could be a class name, and it has the
3208 // shape of a constructor declaration, process it as one.
Faisal Vali7db85c52017-12-31 00:06:40 +00003209 if ((DSContext == DeclSpecContext::DSC_top_level ||
3210 DSContext == DeclSpecContext::DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00003211 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Richard Smith74f02342017-01-19 21:00:13 +00003212 &SS) &&
3213 isConstructorDeclarator(/*Unqualified*/ false))
3214 goto DoneWithDeclSpec;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003215
David Blaikieefdccaa2016-01-15 23:43:34 +00003216 ParsedType TypeRep =
3217 Actions.getTypeName(*Next.getIdentifierInfo(), Next.getLocation(),
3218 getCurScope(), &SS, false, false, nullptr,
3219 /*IsCtorOrDtorName=*/false,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00003220 /*WantNontrivialTypeSourceInfo=*/true,
Richard Smith600b5262017-01-26 20:40:47 +00003221 isClassTemplateDeductionContext(DSContext));
Douglas Gregor8bf42052009-02-09 18:46:07 +00003222
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00003223 // If the referenced identifier is not a type, then this declspec is
3224 // erroneous: We already checked about that it has no type specifier, and
3225 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00003226 // typename.
David Blaikie7d170102013-05-15 07:37:26 +00003227 if (!TypeRep) {
Richard Smithaf3b3252017-05-18 19:21:48 +00003228 // Eat the scope spec so the identifier is current.
3229 ConsumeAnnotationToken();
Michael Han9407e502012-11-26 22:54:45 +00003230 ParsedAttributesWithRange Attrs(AttrFactory);
3231 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
3232 if (!Attrs.empty()) {
3233 AttrsLastTime = true;
3234 attrs.takeAllFrom(Attrs);
3235 }
3236 continue;
3237 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003238 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00003239 }
Mike Stump11289f42009-09-09 15:08:12 +00003240
John McCall9dab4e62009-12-12 11:40:51 +00003241 DS.getTypeSpecScope() = SS;
Richard Smithaf3b3252017-05-18 19:21:48 +00003242 ConsumeAnnotationToken(); // The C++ scope.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003243
Douglas Gregor9817f4a2009-02-09 15:09:02 +00003244 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003245 DiagID, TypeRep, Policy);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003246 if (isInvalid)
3247 break;
Mike Stump11289f42009-09-09 15:08:12 +00003248
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003249 DS.SetRangeEnd(Tok.getLocation());
3250 ConsumeToken(); // The typename.
3251
3252 continue;
3253 }
Mike Stump11289f42009-09-09 15:08:12 +00003254
Chris Lattnere387d9e2009-01-21 19:48:37 +00003255 case tok::annot_typename: {
Richard Smith404dfb42013-11-19 22:47:36 +00003256 // If we've previously seen a tag definition, we were almost surely
3257 // missing a semicolon after it.
3258 if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
3259 goto DoneWithDeclSpec;
3260
John McCallba7bf592010-08-24 05:47:05 +00003261 if (Tok.getAnnotationValue()) {
3262 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00003263 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003264 DiagID, T, Policy);
John McCallba7bf592010-08-24 05:47:05 +00003265 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00003266 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00003267
Chris Lattner005fc1b2010-04-05 18:18:31 +00003268 if (isInvalid)
3269 break;
3270
Chris Lattnere387d9e2009-01-21 19:48:37 +00003271 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
Richard Smithaf3b3252017-05-18 19:21:48 +00003272 ConsumeAnnotationToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00003273
Chris Lattnere387d9e2009-01-21 19:48:37 +00003274 continue;
3275 }
Mike Stump11289f42009-09-09 15:08:12 +00003276
Douglas Gregor06873092011-04-28 15:48:45 +00003277 case tok::kw___is_signed:
3278 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
3279 // typically treats it as a trait. If we see __is_signed as it appears
3280 // in libstdc++, e.g.,
3281 //
3282 // static const bool __is_signed;
3283 //
3284 // then treat __is_signed as an identifier rather than as a keyword.
Faisal Vali090da2d2018-01-01 18:23:28 +00003285 if (DS.getTypeSpecType() == TST_bool &&
Douglas Gregor06873092011-04-28 15:48:45 +00003286 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
Alp Toker47642d22013-12-03 06:13:01 +00003287 DS.getStorageClassSpec() == DeclSpec::SCS_static)
3288 TryKeywordIdentFallback(true);
Douglas Gregor06873092011-04-28 15:48:45 +00003289
3290 // We're done with the declaration-specifiers.
3291 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003292
Chris Lattner16fac4f2008-07-26 01:18:38 +00003293 // typedef-name
Nikola Smiljanic67860242014-09-26 00:28:20 +00003294 case tok::kw___super:
David Blaikie15a430a2011-12-04 05:04:18 +00003295 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003296 case tok::identifier: {
Reid Klecknerc582f012014-07-14 18:19:58 +00003297 // This identifier can only be a typedef name if we haven't already seen
3298 // a type-specifier. Without this check we misparse:
3299 // typedef int X; struct Y { short X; }; as 'short int'.
3300 if (DS.hasTypeSpecifier())
3301 goto DoneWithDeclSpec;
3302
Aaron Ballman52d0aaa2017-02-14 22:47:20 +00003303 // If the token is an identifier named "__declspec" and Microsoft
3304 // extensions are not enabled, it is likely that there will be cascading
3305 // parse errors if this really is a __declspec attribute. Attempt to
3306 // recognize that scenario and recover gracefully.
3307 if (!getLangOpts().DeclSpecKeyword && Tok.is(tok::identifier) &&
3308 Tok.getIdentifierInfo()->getName().equals("__declspec")) {
3309 Diag(Loc, diag::err_ms_attributes_not_enabled);
3310
3311 // The next token should be an open paren. If it is, eat the entire
3312 // attribute declaration and continue.
3313 if (NextToken().is(tok::l_paren)) {
3314 // Consume the __declspec identifier.
Richard Trieuba057372017-02-14 23:56:55 +00003315 ConsumeToken();
Aaron Ballman52d0aaa2017-02-14 22:47:20 +00003316
3317 // Eat the parens and everything between them.
3318 BalancedDelimiterTracker T(*this, tok::l_paren);
3319 if (T.consumeOpen()) {
3320 assert(false && "Not a left paren?");
3321 return;
3322 }
3323 T.skipToEnd();
3324 continue;
3325 }
3326 }
3327
Serge Pavlov458ea762014-07-16 05:16:52 +00003328 // In C++, check to see if this is a scope specifier like foo::bar::, if
3329 // so handle it as such. This is important for ctor parsing.
3330 if (getLangOpts().CPlusPlus) {
3331 if (TryAnnotateCXXScopeToken(EnteringContext)) {
3332 DS.SetTypeSpecError();
3333 goto DoneWithDeclSpec;
3334 }
3335 if (!Tok.is(tok::identifier))
3336 continue;
3337 }
3338
John Thompson22334602010-02-05 00:12:22 +00003339 // Check for need to substitute AltiVec keyword tokens.
3340 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
3341 break;
3342
Richard Smith3092a3b2012-05-09 18:56:43 +00003343 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
3344 // allow the use of a typedef name as a type specifier.
3345 if (DS.isTypeAltiVecVector())
3346 goto DoneWithDeclSpec;
3347
Faisal Vali7db85c52017-12-31 00:06:40 +00003348 if (DSContext == DeclSpecContext::DSC_objc_method_result &&
3349 isObjCInstancetype()) {
Douglas Gregor5c0870a2015-06-19 23:18:00 +00003350 ParsedType TypeRep = Actions.ActOnObjCInstanceType(Loc);
3351 assert(TypeRep);
3352 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
3353 DiagID, TypeRep, Policy);
3354 if (isInvalid)
3355 break;
3356
3357 DS.SetRangeEnd(Loc);
3358 ConsumeToken();
3359 continue;
3360 }
3361
Richard Smith715ee072018-06-20 21:58:20 +00003362 // If we're in a context where the identifier could be a class name,
3363 // check whether this is a constructor declaration.
3364 if (getLangOpts().CPlusPlus && DSContext == DeclSpecContext::DSC_class &&
3365 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
3366 isConstructorDeclarator(/*Unqualified*/true))
3367 goto DoneWithDeclSpec;
3368
Richard Smith600b5262017-01-26 20:40:47 +00003369 ParsedType TypeRep = Actions.getTypeName(
3370 *Tok.getIdentifierInfo(), Tok.getLocation(), getCurScope(), nullptr,
3371 false, false, nullptr, false, false,
3372 isClassTemplateDeductionContext(DSContext));
Douglas Gregor8bf42052009-02-09 18:46:07 +00003373
Chris Lattner6cc055a2009-04-12 20:42:31 +00003374 // If this is not a typedef name, don't parse it as part of the declspec,
3375 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00003376 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00003377 ParsedAttributesWithRange Attrs(AttrFactory);
Craig Topper161e4db2014-05-21 06:02:52 +00003378 if (ParseImplicitInt(DS, nullptr, TemplateInfo, AS, DSContext, Attrs)) {
Michael Han9407e502012-11-26 22:54:45 +00003379 if (!Attrs.empty()) {
3380 AttrsLastTime = true;
3381 attrs.takeAllFrom(Attrs);
3382 }
3383 continue;
3384 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00003385 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00003386 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00003387
Richard Smith35845152017-02-07 01:37:30 +00003388 // Likewise, if this is a context where the identifier could be a template
3389 // name, check whether this is a deduction guide declaration.
Aaron Ballmanc351fba2017-12-04 20:27:34 +00003390 if (getLangOpts().CPlusPlus17 &&
Faisal Vali7db85c52017-12-31 00:06:40 +00003391 (DSContext == DeclSpecContext::DSC_class ||
3392 DSContext == DeclSpecContext::DSC_top_level) &&
Richard Smith35845152017-02-07 01:37:30 +00003393 Actions.isDeductionGuideName(getCurScope(), *Tok.getIdentifierInfo(),
3394 Tok.getLocation()) &&
3395 isConstructorDeclarator(/*Unqualified*/ true,
3396 /*DeductionGuide*/ true))
3397 goto DoneWithDeclSpec;
3398
Douglas Gregor9817f4a2009-02-09 15:09:02 +00003399 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003400 DiagID, TypeRep, Policy);
Chris Lattner16fac4f2008-07-26 01:18:38 +00003401 if (isInvalid)
3402 break;
Mike Stump11289f42009-09-09 15:08:12 +00003403
Chris Lattner16fac4f2008-07-26 01:18:38 +00003404 DS.SetRangeEnd(Tok.getLocation());
3405 ConsumeToken(); // The identifier
3406
Douglas Gregore9d95f12015-07-07 03:57:35 +00003407 // Objective-C supports type arguments and protocol references
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003408 // following an Objective-C object or object pointer
3409 // type. Handle either one of them.
Erik Pilkingtonfa983902018-10-30 20:31:30 +00003410 if (Tok.is(tok::less) && getLangOpts().ObjC) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003411 SourceLocation NewEndLoc;
3412 TypeResult NewTypeRep = parseObjCTypeArgsAndProtocolQualifiers(
3413 Loc, TypeRep, /*consumeLastToken=*/true,
3414 NewEndLoc);
3415 if (NewTypeRep.isUsable()) {
3416 DS.UpdateTypeRep(NewTypeRep.get());
3417 DS.SetRangeEnd(NewEndLoc);
3418 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00003419 }
Chad Rosierc1183952012-06-26 22:30:43 +00003420
Steve Naroffcd5e7822008-09-22 10:28:57 +00003421 // Need to support trailing type qualifiers (e.g. "id<p> const").
3422 // If a type specifier follows, it will be diagnosed elsewhere.
3423 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00003424 }
Douglas Gregor7f741122009-02-25 19:37:18 +00003425
3426 // type-name
3427 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00003428 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Richard Smithb23c5e82019-05-09 03:31:27 +00003429 if (TemplateId->Kind != TNK_Type_template &&
3430 TemplateId->Kind != TNK_Undeclared_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00003431 // This template-id does not refer to a type name, so we're
3432 // done with the type-specifiers.
3433 goto DoneWithDeclSpec;
3434 }
3435
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003436 // If we're in a context where the template-id could be a
3437 // constructor name or specialization, check whether this is a
3438 // constructor declaration.
Faisal Vali7db85c52017-12-31 00:06:40 +00003439 if (getLangOpts().CPlusPlus && DSContext == DeclSpecContext::DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00003440 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Richard Smith446161b2014-03-03 21:12:53 +00003441 isConstructorDeclarator(TemplateId->SS.isEmpty()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003442 goto DoneWithDeclSpec;
3443
Douglas Gregor7f741122009-02-25 19:37:18 +00003444 // Turn the template-id annotation token into a type annotation
3445 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00003446 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00003447 continue;
3448 }
3449
Chris Lattnere37e2332006-08-15 04:50:22 +00003450 // GNU attributes support.
3451 case tok::kw___attribute:
Craig Topper161e4db2014-05-21 06:02:52 +00003452 ParseGNUAttributes(DS.getAttributes(), nullptr, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00003453 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00003454
3455 // Microsoft declspec support.
3456 case tok::kw___declspec:
Aaron Ballman068aa512015-05-20 20:58:33 +00003457 ParseMicrosoftDeclSpecs(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00003458 continue;
Mike Stump11289f42009-09-09 15:08:12 +00003459
Steve Naroff44ac7772008-12-25 14:16:32 +00003460 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00003461 case tok::kw___forceinline: {
Serge Pavlov750db652013-11-13 06:57:53 +00003462 isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00003463 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00003464 SourceLocation AttrNameLoc = Tok.getLocation();
Craig Topper161e4db2014-05-21 06:02:52 +00003465 DS.getAttributes().addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc,
Erich Keanee891aa92018-07-13 15:07:47 +00003466 nullptr, 0, ParsedAttr::AS_Keyword);
Richard Smithda837032012-09-14 18:27:01 +00003467 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00003468 }
Eli Friedman53339e02009-06-08 23:27:34 +00003469
Andrey Bokhanko45d41322016-05-11 18:38:21 +00003470 case tok::kw___unaligned:
3471 isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID,
3472 getLangOpts());
3473 break;
3474
Aaron Ballman317a77f2013-05-22 23:25:32 +00003475 case tok::kw___sptr:
3476 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00003477 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003478 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00003479 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00003480 case tok::kw___cdecl:
3481 case tok::kw___stdcall:
3482 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003483 case tok::kw___thiscall:
Erich Keane757d3172016-11-02 18:29:35 +00003484 case tok::kw___regcall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00003485 case tok::kw___vectorcall:
John McCall53fa7142010-12-24 02:08:15 +00003486 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003487 continue;
3488
Dawn Perchik335e16b2010-09-03 01:29:35 +00003489 // Borland single token adornments.
3490 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00003491 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00003492 continue;
3493
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003494 // OpenCL single token adornments.
3495 case tok::kw___kernel:
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +00003496 ParseOpenCLKernelAttributes(DS.getAttributes());
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003497 continue;
3498
Douglas Gregor261a89b2015-06-19 17:51:05 +00003499 // Nullability type specifiers.
Douglas Gregoraea7afd2015-06-24 22:02:08 +00003500 case tok::kw__Nonnull:
3501 case tok::kw__Nullable:
3502 case tok::kw__Null_unspecified:
Douglas Gregor261a89b2015-06-19 17:51:05 +00003503 ParseNullabilityTypeSpecifiers(DS.getAttributes());
3504 continue;
3505
Douglas Gregorab209d82015-07-07 03:58:42 +00003506 // Objective-C 'kindof' types.
3507 case tok::kw___kindof:
3508 DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc,
Erich Keanee891aa92018-07-13 15:07:47 +00003509 nullptr, 0, ParsedAttr::AS_Keyword);
Douglas Gregorab209d82015-07-07 03:58:42 +00003510 (void)ConsumeToken();
3511 continue;
3512
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003513 // storage-class-specifier
3514 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003515 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003516 PrevSpec, DiagID, Policy);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003517 isStorageClass = true;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003518 break;
3519 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00003520 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00003521 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003522 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003523 PrevSpec, DiagID, Policy);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003524 isStorageClass = true;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003525 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00003526 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003527 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003528 Loc, PrevSpec, DiagID, Policy);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003529 isStorageClass = true;
Steve Naroff2050b0d2007-12-18 00:16:02 +00003530 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003531 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00003532 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00003533 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003534 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003535 PrevSpec, DiagID, Policy);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003536 isStorageClass = true;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003537 break;
3538 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003539 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003540 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003541 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003542 PrevSpec, DiagID, Policy);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003543 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00003544 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003545 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00003546 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003547 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003548 DiagID, Policy);
Richard Smith58c74332011-09-04 19:54:14 +00003549 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003550 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003551 PrevSpec, DiagID, Policy);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003552 isStorageClass = true;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003553 break;
Richard Smithe301ba22015-11-11 02:02:15 +00003554 case tok::kw___auto_type:
3555 Diag(Tok, diag::ext_auto_type);
3556 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto_type, Loc, PrevSpec,
3557 DiagID, Policy);
3558 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003559 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003560 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003561 PrevSpec, DiagID, Policy);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003562 isStorageClass = true;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003563 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00003564 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003565 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003566 PrevSpec, DiagID, Policy);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003567 isStorageClass = true;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00003568 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003569 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00003570 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
3571 PrevSpec, DiagID);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003572 isStorageClass = true;
Richard Smithb4a9e862013-04-12 22:46:28 +00003573 break;
3574 case tok::kw_thread_local:
3575 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
3576 PrevSpec, DiagID);
Sven van Haastregtc4100832018-04-24 14:47:29 +00003577 isStorageClass = true;
Richard Smithb4a9e862013-04-12 22:46:28 +00003578 break;
3579 case tok::kw__Thread_local:
Aaron Ballman774bd6e2019-08-26 19:44:07 +00003580 if (!getLangOpts().C11)
3581 Diag(Tok, diag::ext_c11_feature) << Tok.getName();
Richard Smithb4a9e862013-04-12 22:46:28 +00003582 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
3583 Loc, PrevSpec, DiagID);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003584 isStorageClass = true;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003585 break;
Mike Stump11289f42009-09-09 15:08:12 +00003586
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003587 // function-specifier
3588 case tok::kw_inline:
Serge Pavlov750db652013-11-13 06:57:53 +00003589 isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003590 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00003591 case tok::kw_virtual:
Anastasia Stulova46b55fa2019-07-18 10:02:35 +00003592 // C++ for OpenCL does not allow virtual function qualifier, to avoid
3593 // function pointers restricted in OpenCL v2.0 s6.9.a.
Sven van Haastregt49ffffb2018-04-23 11:23:47 +00003594 if (getLangOpts().OpenCLCPlusPlus) {
3595 DiagID = diag::err_openclcxx_virtual_function;
3596 PrevSpec = Tok.getIdentifierInfo()->getNameStart();
3597 isInvalid = true;
3598 }
3599 else {
3600 isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
3601 }
Douglas Gregor61956c42008-10-31 09:07:45 +00003602 break;
Richard Smith76b90272019-05-09 03:59:21 +00003603 case tok::kw_explicit: {
3604 SourceLocation ExplicitLoc = Loc;
3605 SourceLocation CloseParenLoc;
3606 ExplicitSpecifier ExplicitSpec(nullptr, ExplicitSpecKind::ResolvedTrue);
Richard Smithba24f352019-05-09 19:45:46 +00003607 ConsumedEnd = ExplicitLoc;
Richard Smith76b90272019-05-09 03:59:21 +00003608 ConsumeToken(); // kw_explicit
3609 if (Tok.is(tok::l_paren)) {
3610 if (getLangOpts().CPlusPlus2a) {
3611 ExprResult ExplicitExpr(static_cast<Expr *>(nullptr));
3612 BalancedDelimiterTracker Tracker(*this, tok::l_paren);
3613 Tracker.consumeOpen();
3614 ExplicitExpr = ParseConstantExpression();
Richard Smithba24f352019-05-09 19:45:46 +00003615 ConsumedEnd = Tok.getLocation();
Richard Smith76b90272019-05-09 03:59:21 +00003616 if (ExplicitExpr.isUsable()) {
3617 CloseParenLoc = Tok.getLocation();
3618 Tracker.consumeClose();
3619 ExplicitSpec =
3620 Actions.ActOnExplicitBoolSpecifier(ExplicitExpr.get());
3621 } else
3622 Tracker.skipToEnd();
3623 } else
3624 Diag(Tok.getLocation(), diag::warn_cxx2a_compat_explicit_bool);
3625 }
3626 isInvalid = DS.setFunctionSpecExplicit(ExplicitLoc, PrevSpec, DiagID,
3627 ExplicitSpec, CloseParenLoc);
Douglas Gregor61956c42008-10-31 09:07:45 +00003628 break;
Richard Smith76b90272019-05-09 03:59:21 +00003629 }
Richard Smith0015f092013-01-17 22:16:11 +00003630 case tok::kw__Noreturn:
3631 if (!getLangOpts().C11)
Aaron Ballman1d935222019-08-27 14:41:39 +00003632 Diag(Tok, diag::ext_c11_feature) << Tok.getName();
Serge Pavlov750db652013-11-13 06:57:53 +00003633 isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
Richard Smith0015f092013-01-17 22:16:11 +00003634 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003635
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003636 // alignment-specifier
3637 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003638 if (!getLangOpts().C11)
Aaron Ballman774bd6e2019-08-26 19:44:07 +00003639 Diag(Tok, diag::ext_c11_feature) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003640 ParseAlignmentSpecifier(DS.getAttributes());
3641 continue;
3642
Anders Carlssoncd8db412009-05-06 04:46:28 +00003643 // friend
3644 case tok::kw_friend:
Faisal Vali7db85c52017-12-31 00:06:40 +00003645 if (DSContext == DeclSpecContext::DSC_class)
John McCall07e91c02009-08-06 02:15:43 +00003646 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
3647 else {
3648 PrevSpec = ""; // not actually used by the diagnostic
3649 DiagID = diag::err_friend_invalid_in_context;
3650 isInvalid = true;
3651 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00003652 break;
Mike Stump11289f42009-09-09 15:08:12 +00003653
Douglas Gregor26701a42011-09-09 02:06:17 +00003654 // Modules
3655 case tok::kw___module_private__:
3656 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
3657 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003658
Richard Smitha6e8b682019-09-04 20:30:37 +00003659 // constexpr, consteval, constinit specifiers
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00003660 case tok::kw_constexpr:
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003661 isInvalid = DS.SetConstexprSpec(CSK_constexpr, Loc, PrevSpec, DiagID);
3662 break;
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003663 case tok::kw_consteval:
3664 isInvalid = DS.SetConstexprSpec(CSK_consteval, Loc, PrevSpec, DiagID);
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00003665 break;
Richard Smitha6e8b682019-09-04 20:30:37 +00003666 case tok::kw_constinit:
3667 isInvalid = DS.SetConstexprSpec(CSK_constinit, Loc, PrevSpec, DiagID);
3668 break;
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00003669
Chris Lattnere387d9e2009-01-21 19:48:37 +00003670 // type-specifier
3671 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00003672 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003673 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003674 break;
3675 case tok::kw_long:
3676 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00003677 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003678 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003679 else
John McCall49bfce42009-08-03 20:12:06 +00003680 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003681 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003682 break;
Francois Pichet84133e42011-04-28 01:59:37 +00003683 case tok::kw___int64:
3684 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003685 DiagID, Policy);
Francois Pichet84133e42011-04-28 01:59:37 +00003686 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003687 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00003688 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
3689 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003690 break;
3691 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00003692 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
3693 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003694 break;
3695 case tok::kw__Complex:
Aaron Ballman9fac4a52019-08-27 19:15:24 +00003696 if (!getLangOpts().C99)
3697 Diag(Tok, diag::ext_c99_feature) << Tok.getName();
John McCall49bfce42009-08-03 20:12:06 +00003698 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
3699 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003700 break;
3701 case tok::kw__Imaginary:
Aaron Ballman9fac4a52019-08-27 19:15:24 +00003702 if (!getLangOpts().C99)
3703 Diag(Tok, diag::ext_c99_feature) << Tok.getName();
John McCall49bfce42009-08-03 20:12:06 +00003704 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
3705 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003706 break;
3707 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00003708 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003709 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003710 break;
3711 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00003712 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003713 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003714 break;
3715 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00003716 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003717 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003718 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00003719 case tok::kw___int128:
3720 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003721 DiagID, Policy);
Richard Smithf016bbc2012-04-04 06:24:32 +00003722 break;
3723 case tok::kw_half:
3724 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003725 DiagID, Policy);
Richard Smithf016bbc2012-04-04 06:24:32 +00003726 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003727 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00003728 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003729 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003730 break;
3731 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00003732 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003733 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003734 break;
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00003735 case tok::kw__Float16:
3736 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float16, Loc, PrevSpec,
3737 DiagID, Policy);
3738 break;
Leonard Chanf921d852018-06-04 16:07:52 +00003739 case tok::kw__Accum:
3740 if (!getLangOpts().FixedPoint) {
Leonard Chanab80f3c2018-06-14 14:53:51 +00003741 SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid);
Leonard Chanf921d852018-06-04 16:07:52 +00003742 } else {
3743 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_accum, Loc, PrevSpec,
3744 DiagID, Policy);
3745 }
3746 break;
Leonard Chanab80f3c2018-06-14 14:53:51 +00003747 case tok::kw__Fract:
3748 if (!getLangOpts().FixedPoint) {
3749 SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid);
3750 } else {
3751 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_fract, Loc, PrevSpec,
3752 DiagID, Policy);
3753 }
3754 break;
3755 case tok::kw__Sat:
3756 if (!getLangOpts().FixedPoint) {
3757 SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid);
3758 } else {
3759 isInvalid = DS.SetTypeSpecSat(Loc, PrevSpec, DiagID);
3760 }
3761 break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00003762 case tok::kw___float128:
3763 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float128, Loc, PrevSpec,
3764 DiagID, Policy);
3765 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003766 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00003767 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003768 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003769 break;
Richard Smith3a8244d2018-05-01 05:02:45 +00003770 case tok::kw_char8_t:
3771 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char8, Loc, PrevSpec,
3772 DiagID, Policy);
3773 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003774 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00003775 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003776 DiagID, Policy);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003777 break;
3778 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00003779 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003780 DiagID, Policy);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003781 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003782 case tok::kw_bool:
3783 case tok::kw__Bool:
Aaron Ballman27e66bf2019-08-27 20:33:05 +00003784 if (Tok.is(tok::kw__Bool) && !getLangOpts().C99)
3785 Diag(Tok, diag::ext_c99_feature) << Tok.getName();
3786
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003787 if (Tok.is(tok::kw_bool) &&
3788 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
3789 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
3790 PrevSpec = ""; // Not used by the diagnostic.
3791 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003792 // For better error recovery.
3793 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003794 isInvalid = true;
3795 } else {
3796 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003797 DiagID, Policy);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003798 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003799 break;
3800 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00003801 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003802 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003803 break;
3804 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00003805 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003806 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003807 break;
3808 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00003809 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003810 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003811 break;
John Thompson22334602010-02-05 00:12:22 +00003812 case tok::kw___vector:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003813 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
John Thompson22334602010-02-05 00:12:22 +00003814 break;
3815 case tok::kw___pixel:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003816 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
John Thompson22334602010-02-05 00:12:22 +00003817 break;
Bill Seurercf2c96b2015-01-12 19:35:51 +00003818 case tok::kw___bool:
3819 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy);
3820 break;
Xiuli Pan9c14e282016-01-09 12:53:17 +00003821 case tok::kw_pipe:
Sven van Haastregte518bb42019-05-22 13:12:20 +00003822 if (!getLangOpts().OpenCL || (getLangOpts().OpenCLVersion < 200 &&
3823 !getLangOpts().OpenCLCPlusPlus)) {
Xiuli Pan9c14e282016-01-09 12:53:17 +00003824 // OpenCL 2.0 defined this keyword. OpenCL 1.2 and earlier should
3825 // support the "pipe" word as identifier.
3826 Tok.getIdentifierInfo()->revertTokenIDToIdentifier();
3827 goto DoneWithDeclSpec;
3828 }
3829 isInvalid = DS.SetTypePipe(true, Loc, PrevSpec, DiagID, Policy);
3830 break;
Alexey Bader954ba212016-04-08 13:40:33 +00003831#define GENERIC_IMAGE_TYPE(ImgType, Id) \
3832 case tok::kw_##ImgType##_t: \
3833 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_##ImgType##_t, Loc, PrevSpec, \
3834 DiagID, Policy); \
3835 break;
Alexey Baderb62f1442016-04-13 08:33:41 +00003836#include "clang/Basic/OpenCLImageTypes.def"
John McCall39439732011-04-09 22:50:59 +00003837 case tok::kw___unknown_anytype:
Faisal Vali090da2d2018-01-01 18:23:28 +00003838 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003839 PrevSpec, DiagID, Policy);
John McCall39439732011-04-09 22:50:59 +00003840 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003841
3842 // class-specifier:
3843 case tok::kw_class:
3844 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003845 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003846 case tok::kw_union: {
3847 tok::TokenKind Kind = Tok.getKind();
3848 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00003849
3850 // These are attributes following class specifiers.
3851 // To produce better diagnostic, we parse them when
3852 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00003853 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00003854 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00003855 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003856
3857 // If there are attributes following class specifier,
3858 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00003859 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00003860 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00003861 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003862 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003863 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003864 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003865
3866 // enum-specifier:
3867 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003868 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003869 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003870 continue;
Faisal Valia534f072018-04-26 00:42:40 +00003871
Chris Lattnere387d9e2009-01-21 19:48:37 +00003872 // cv-qualifier:
3873 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003874 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003875 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003876 break;
3877 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003878 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003879 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003880 break;
3881 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003882 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003883 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003884 break;
3885
Douglas Gregor333489b2009-03-27 23:10:48 +00003886 // C++ typename-specifier:
3887 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003888 if (TryAnnotateTypeOrScopeToken()) {
3889 DS.SetTypeSpecError();
3890 goto DoneWithDeclSpec;
3891 }
3892 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003893 continue;
3894 break;
3895
Chris Lattnere387d9e2009-01-21 19:48:37 +00003896 // GNU typeof support.
3897 case tok::kw_typeof:
3898 ParseTypeofSpecifier(DS);
3899 continue;
3900
David Blaikie15a430a2011-12-04 05:04:18 +00003901 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003902 ParseDecltypeSpecifier(DS);
3903 continue;
3904
David Majnemer15b311c2016-06-14 03:20:28 +00003905 case tok::annot_pragma_pack:
3906 HandlePragmaPack();
3907 continue;
3908
3909 case tok::annot_pragma_ms_pragma:
3910 HandlePragmaMSPragma();
3911 continue;
3912
3913 case tok::annot_pragma_ms_vtordisp:
3914 HandlePragmaMSVtorDisp();
3915 continue;
3916
3917 case tok::annot_pragma_ms_pointers_to_members:
3918 HandlePragmaMSPointersToMembers();
3919 continue;
3920
Alexis Hunt4a257072011-05-19 05:37:45 +00003921 case tok::kw___underlying_type:
3922 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003923 continue;
3924
3925 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003926 // C11 6.7.2.4/4:
3927 // If the _Atomic keyword is immediately followed by a left parenthesis,
3928 // it is interpreted as a type specifier (with a type name), not as a
3929 // type qualifier.
Aaron Ballman5cd5d562019-09-04 21:01:57 +00003930 if (!getLangOpts().C11)
3931 Diag(Tok, diag::ext_c11_feature) << Tok.getName();
3932
Richard Smith8e1ac332013-03-28 01:55:44 +00003933 if (NextToken().is(tok::l_paren)) {
3934 ParseAtomicSpecifier(DS);
3935 continue;
3936 }
3937 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3938 getLangOpts());
3939 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003940
Sven van Haastregt2ca6ba12018-05-09 13:16:17 +00003941 // OpenCL address space qualifiers:
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003942 case tok::kw___generic:
3943 // generic address space is introduced only in OpenCL v2.0
3944 // see OpenCL C Spec v2.0 s6.5.5
Sven van Haastregt2ca6ba12018-05-09 13:16:17 +00003945 if (Actions.getLangOpts().OpenCLVersion < 200 &&
3946 !Actions.getLangOpts().OpenCLCPlusPlus) {
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003947 DiagID = diag::err_opencl_unknown_type_specifier;
3948 PrevSpec = Tok.getIdentifierInfo()->getNameStart();
3949 isInvalid = true;
3950 break;
3951 };
Galina Kistanova77674252017-06-01 21:15:34 +00003952 LLVM_FALLTHROUGH;
Anastasia Stulova948e37c2019-03-25 11:54:02 +00003953 case tok::kw_private:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003954 case tok::kw___private:
3955 case tok::kw___global:
3956 case tok::kw___local:
3957 case tok::kw___constant:
Anastasia Stulova2c4730d2019-02-15 12:07:57 +00003958 // OpenCL access qualifiers:
3959 case tok::kw___read_only:
3960 case tok::kw___write_only:
3961 case tok::kw___read_write:
Aaron Ballman05d76ea2014-01-14 01:29:54 +00003962 ParseOpenCLQualifiers(DS.getAttributes());
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003963 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003964
Steve Naroffcfdf6162008-06-05 00:02:44 +00003965 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003966 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003967 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3968 // but we support it.
Erik Pilkingtonfa983902018-10-30 20:31:30 +00003969 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC)
Chris Lattner0974b232008-07-26 00:20:22 +00003970 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003971
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003972 SourceLocation StartLoc = Tok.getLocation();
3973 SourceLocation EndLoc;
3974 TypeResult Type = parseObjCProtocolQualifierType(EndLoc);
3975 if (Type.isUsable()) {
3976 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, StartLoc,
3977 PrevSpec, DiagID, Type.get(),
3978 Actions.getASTContext().getPrintingPolicy()))
3979 Diag(StartLoc, DiagID) << PrevSpec;
Fangrui Song6907ce22018-07-30 19:24:48 +00003980
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003981 DS.SetRangeEnd(EndLoc);
3982 } else {
3983 DS.SetTypeSpecError();
3984 }
Chad Rosierc1183952012-06-26 22:30:43 +00003985
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003986 // Need to support trailing type qualifiers (e.g. "id<p> const").
3987 // If a type specifier follows, it will be diagnosed elsewhere.
3988 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003989 }
Richard Smith76b90272019-05-09 03:59:21 +00003990
Richard Smithba24f352019-05-09 19:45:46 +00003991 DS.SetRangeEnd(ConsumedEnd.isValid() ? ConsumedEnd : Tok.getLocation());
Richard Smith76b90272019-05-09 03:59:21 +00003992
John McCall49bfce42009-08-03 20:12:06 +00003993 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003994 if (isInvalid) {
3995 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003996 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003997
Nick Desaulniers150ca532018-10-03 23:09:29 +00003998 if (DiagID == diag::ext_duplicate_declspec ||
Richard Smith76b90272019-05-09 03:59:21 +00003999 DiagID == diag::ext_warn_duplicate_declspec ||
4000 DiagID == diag::err_duplicate_declspec)
4001 Diag(Loc, DiagID) << PrevSpec
4002 << FixItHint::CreateRemoval(
4003 SourceRange(Loc, DS.getEndLoc()));
Anastasia Stulova43ab9a02016-05-12 16:28:25 +00004004 else if (DiagID == diag::err_opencl_unknown_type_specifier) {
Richard Smith76b90272019-05-09 03:59:21 +00004005 Diag(Loc, DiagID) << getLangOpts().OpenCLCPlusPlus
4006 << getLangOpts().getOpenCLVersionTuple().getAsString()
4007 << PrevSpec << isStorageClass;
Anastasia Stulova43ab9a02016-05-12 16:28:25 +00004008 } else
Richard Smith76b90272019-05-09 03:59:21 +00004009 Diag(Loc, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004010 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00004011
Richard Smithba24f352019-05-09 19:45:46 +00004012 if (DiagID != diag::err_bool_redeclaration && ConsumedEnd.isInvalid())
Volodymyr Sapsai9f7b5cc2018-04-10 18:29:47 +00004013 // After an error the next token can be an annotation token.
4014 ConsumeAnyToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00004015
4016 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004017 }
4018}
Douglas Gregoreb31f392008-12-01 23:54:00 +00004019
Chris Lattner70ae4912007-10-29 04:42:53 +00004020/// ParseStructDeclaration - Parse a struct declaration without the terminating
4021/// semicolon.
4022///
Nico Weber98dd0852019-03-14 14:18:56 +00004023/// Note that a struct declaration refers to a declaration in a struct,
4024/// not to the declaration of a struct.
4025///
Chris Lattner90a26b02007-01-23 04:38:16 +00004026/// struct-declaration:
Aaron Ballman606093a2017-10-15 15:01:42 +00004027/// [C2x] attributes-specifier-seq[opt]
4028/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00004029/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00004030/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00004031/// struct-declarator-list:
4032/// struct-declarator
4033/// struct-declarator-list ',' struct-declarator
4034/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
4035/// struct-declarator:
4036/// declarator
4037/// [GNU] declarator attributes[opt]
4038/// declarator[opt] ':' constant-expression
4039/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
4040///
Benjamin Kramera39beb92014-09-03 11:06:10 +00004041void Parser::ParseStructDeclaration(
4042 ParsingDeclSpec &DS,
4043 llvm::function_ref<void(ParsingFieldDeclarator &)> FieldsCallback) {
Chad Rosierc1183952012-06-26 22:30:43 +00004044
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00004045 if (Tok.is(tok::kw___extension__)) {
4046 // __extension__ silences extension warnings in the subexpression.
4047 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00004048 ConsumeToken();
Benjamin Kramera39beb92014-09-03 11:06:10 +00004049 return ParseStructDeclaration(DS, FieldsCallback);
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00004050 }
Mike Stump11289f42009-09-09 15:08:12 +00004051
Aaron Ballman606093a2017-10-15 15:01:42 +00004052 // Parse leading attributes.
4053 ParsedAttributesWithRange Attrs(AttrFactory);
4054 MaybeParseCXX11Attributes(Attrs);
4055 DS.takeAttributesFrom(Attrs);
4056
Steve Naroff97170802007-08-20 22:28:22 +00004057 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00004058 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00004059
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00004060 // If there are no declarators, this is a free-standing declaration
4061 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00004062 if (Tok.is(tok::semi)) {
Nico Weber7b837f52016-01-28 19:25:00 +00004063 RecordDecl *AnonRecord = nullptr;
Eli Friedman89b1f2c2012-08-08 23:04:35 +00004064 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Nico Weber7b837f52016-01-28 19:25:00 +00004065 DS, AnonRecord);
4066 assert(!AnonRecord && "Did not expect anonymous struct or union here");
Eli Friedman89b1f2c2012-08-08 23:04:35 +00004067 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00004068 return;
4069 }
4070
4071 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00004072 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00004073 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00004074 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00004075 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00004076 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00004077
Bill Wendling44426052012-12-20 19:22:21 +00004078 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00004079 if (!FirstDeclarator)
4080 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00004081
Steve Naroff97170802007-08-20 22:28:22 +00004082 /// struct-declarator: declarator
4083 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00004084 if (Tok.isNot(tok::colon)) {
4085 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
4086 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00004087 ParseDeclarator(DeclaratorInfo.D);
Richard Smith3d1a94c2014-08-12 00:22:39 +00004088 } else
4089 DeclaratorInfo.D.SetIdentifier(nullptr, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00004090
Alp Toker8fbec672013-12-17 23:29:36 +00004091 if (TryConsumeToken(tok::colon)) {
John McCalldadc5752010-08-24 06:29:42 +00004092 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00004093 if (Res.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00004094 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner32295d32008-04-10 06:15:14 +00004095 else
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004096 DeclaratorInfo.BitfieldSize = Res.get();
Steve Naroff97170802007-08-20 22:28:22 +00004097 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004098
Steve Naroff97170802007-08-20 22:28:22 +00004099 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00004100 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004101
John McCallcfefb6d2009-11-03 02:38:08 +00004102 // We're done with this declarator; invoke the callback.
Benjamin Kramera39beb92014-09-03 11:06:10 +00004103 FieldsCallback(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00004104
Steve Naroff97170802007-08-20 22:28:22 +00004105 // If we don't have a comma, it is either the end of the list (a ';')
4106 // or an error, bail out.
Alp Toker8fbec672013-12-17 23:29:36 +00004107 if (!TryConsumeToken(tok::comma, CommaLoc))
Chris Lattner70ae4912007-10-29 04:42:53 +00004108 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004109
John McCallcfefb6d2009-11-03 02:38:08 +00004110 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00004111 }
Steve Naroff97170802007-08-20 22:28:22 +00004112}
4113
4114/// ParseStructUnionBody
4115/// struct-contents:
4116/// struct-declaration-list
4117/// [EXT] empty
4118/// [GNU] "struct-declaration-list" without terminatoring ';'
4119/// struct-declaration-list:
4120/// struct-declaration
4121/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00004122/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00004123///
Chris Lattner1300fb92007-01-23 23:42:53 +00004124void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Serge Pavlov0c503192019-08-01 11:46:28 +00004125 DeclSpec::TST TagType, Decl *TagDecl) {
Jordan Rose1e879d82018-03-23 00:07:18 +00004126 PrettyDeclStackTraceEntry CrashInfo(Actions.Context, TagDecl, RecordLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00004127 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00004128 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00004129
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004130 BalancedDelimiterTracker T(*this, tok::l_brace);
4131 if (T.consumeOpen())
4132 return;
Mike Stump11289f42009-09-09 15:08:12 +00004133
Douglas Gregor658b9552009-01-09 22:42:13 +00004134 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004135 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00004136
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004137 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00004138
Chris Lattner7b9ace62007-01-23 20:11:08 +00004139 // While we still have something to read, read the declarations in the struct.
Richard Smith752ada82015-11-17 23:32:01 +00004140 while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
4141 Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00004142 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00004143
Chris Lattner736ed5d2007-06-09 05:59:07 +00004144 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00004145 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00004146 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00004147 continue;
4148 }
Chris Lattnera12405b2008-04-10 06:46:29 +00004149
Andy Gibbsc804e082013-04-03 09:46:04 +00004150 // Parse _Static_assert declaration.
4151 if (Tok.is(tok::kw__Static_assert)) {
4152 SourceLocation DeclEnd;
4153 ParseStaticAssertDeclaration(DeclEnd);
4154 continue;
4155 }
4156
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00004157 if (Tok.is(tok::annot_pragma_pack)) {
4158 HandlePragmaPack();
4159 continue;
4160 }
4161
4162 if (Tok.is(tok::annot_pragma_align)) {
4163 HandlePragmaAlign();
4164 continue;
4165 }
4166
Alexey Bataev4652e4b2015-08-12 07:10:54 +00004167 if (Tok.is(tok::annot_pragma_openmp)) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00004168 // Result can be ignored, because it must be always empty.
4169 AccessSpecifier AS = AS_none;
4170 ParsedAttributesWithRange Attrs(AttrFactory);
4171 (void)ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs);
Alexey Bataev4652e4b2015-08-12 07:10:54 +00004172 continue;
4173 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00004174
Serge Pavlov037861b2019-08-04 10:08:51 +00004175 if (tok::isPragmaAnnotation(Tok.getKind())) {
4176 Diag(Tok.getLocation(), diag::err_pragma_misplaced_in_decl)
4177 << DeclSpec::getSpecifierName(
4178 TagType, Actions.getASTContext().getPrintingPolicy());
4179 ConsumeAnnotationToken();
4180 continue;
4181 }
4182
John McCallcfefb6d2009-11-03 02:38:08 +00004183 if (!Tok.is(tok::at)) {
Benjamin Kramera39beb92014-09-03 11:06:10 +00004184 auto CFieldCallback = [&](ParsingFieldDeclarator &FD) {
4185 // Install the declarator into the current TagDecl.
4186 Decl *Field =
4187 Actions.ActOnField(getCurScope(), TagDecl,
4188 FD.D.getDeclSpec().getSourceRange().getBegin(),
4189 FD.D, FD.BitfieldSize);
4190 FieldDecls.push_back(Field);
4191 FD.complete(Field);
4192 };
John McCallcfefb6d2009-11-03 02:38:08 +00004193
Eli Friedman89b1f2c2012-08-08 23:04:35 +00004194 // Parse all the comma separated declarators.
4195 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00004196 ParseStructDeclaration(DS, CFieldCallback);
Chris Lattner535b8302008-06-21 19:39:06 +00004197 } else { // Handle @defs
4198 ConsumeToken();
4199 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
4200 Diag(Tok, diag::err_unexpected_at);
Alexey Bataevee6507d2013-11-18 08:17:37 +00004201 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00004202 continue;
4203 }
4204 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00004205 ExpectAndConsume(tok::l_paren);
Chris Lattner535b8302008-06-21 19:39:06 +00004206 if (!Tok.is(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00004207 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00004208 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00004209 continue;
4210 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004211 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00004212 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00004213 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00004214 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
4215 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00004216 ExpectAndConsume(tok::r_paren);
Mike Stump11289f42009-09-09 15:08:12 +00004217 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00004218
Alp Tokera3ebe6e2013-12-17 14:12:37 +00004219 if (TryConsumeToken(tok::semi))
4220 continue;
4221
4222 if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00004223 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00004224 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00004225 }
Alp Tokera3ebe6e2013-12-17 14:12:37 +00004226
4227 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
4228 // Skip to end of block or statement to avoid ext-warning on extra ';'.
4229 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
4230 // If we stopped at a ';', eat it.
4231 TryConsumeToken(tok::semi);
Chris Lattner90a26b02007-01-23 04:38:16 +00004232 }
Mike Stump11289f42009-09-09 15:08:12 +00004233
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004234 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00004235
John McCall084e83d2011-03-24 11:26:52 +00004236 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00004237 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00004238 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00004239
Erich Keanec480f302018-07-12 21:09:05 +00004240 Actions.ActOnFields(getCurScope(), RecordLoc, TagDecl, FieldDecls,
4241 T.getOpenLocation(), T.getCloseLocation(), attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00004242 StructScope.Exit();
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00004243 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, T.getRange());
Chris Lattner90a26b02007-01-23 04:38:16 +00004244}
4245
Chris Lattner3b561a32006-08-13 00:12:11 +00004246/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00004247/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00004248/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004249///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00004250/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
4251/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00004252/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
4253/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00004254/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00004255/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004256///
Richard Smith7d137e32012-03-23 03:33:32 +00004257/// [C++11] enum-head '{' enumerator-list[opt] '}'
4258/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00004259///
Richard Smith7d137e32012-03-23 03:33:32 +00004260/// enum-head: [C++11]
4261/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
4262/// enum-key attribute-specifier-seq[opt] nested-name-specifier
4263/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00004264///
Richard Smith7d137e32012-03-23 03:33:32 +00004265/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00004266/// 'enum'
4267/// 'enum' 'class'
4268/// 'enum' 'struct'
4269///
Richard Smith7d137e32012-03-23 03:33:32 +00004270/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00004271/// ':' type-specifier-seq
4272///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004273/// [C++] elaborated-type-specifier:
4274/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
4275///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00004276void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00004277 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00004278 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00004279 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00004280 if (Tok.is(tok::code_completion)) {
4281 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00004282 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004283 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00004284 }
John McCallcb432fa2011-07-06 05:58:41 +00004285
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00004286 // If attributes exist after tag, parse them.
4287 ParsedAttributesWithRange attrs(AttrFactory);
4288 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00004289 MaybeParseCXX11Attributes(attrs);
Aaron Ballman068aa512015-05-20 20:58:33 +00004290 MaybeParseMicrosoftDeclSpecs(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00004291
Richard Smith0f8ee222012-01-10 01:33:14 +00004292 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00004293 bool IsScopedUsingClassTag = false;
4294
John McCallbeae29a2012-06-23 22:30:04 +00004295 // In C++11, recognize 'enum class' and 'enum struct'.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00004296 if (Tok.isOneOf(tok::kw_class, tok::kw_struct)) {
Richard Trieud0d87b52013-04-23 02:47:36 +00004297 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
4298 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00004299 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00004300 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00004301
Bill Wendling44426052012-12-20 19:22:21 +00004302 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00004303 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00004304 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00004305
4306 // They are allowed afterwards, though.
4307 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00004308 MaybeParseCXX11Attributes(attrs);
Aaron Ballman068aa512015-05-20 20:58:33 +00004309 MaybeParseMicrosoftDeclSpecs(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00004310 }
Richard Smith7d137e32012-03-23 03:33:32 +00004311
John McCall6347b682012-05-07 06:16:58 +00004312 // C++11 [temp.explicit]p12:
4313 // The usual access controls do not apply to names used to specify
4314 // explicit instantiations.
4315 // We extend this to also cover explicit specializations. Note that
4316 // we don't suppress if this turns out to be an elaborated type
4317 // specifier.
4318 bool shouldDelayDiagsInTag =
4319 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
4320 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
4321 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00004322
Richard Smithbfdb1082012-03-12 08:56:40 +00004323 // Enum definitions should not be parsed in a trailing-return-type.
Faisal Vali7db85c52017-12-31 00:06:40 +00004324 bool AllowDeclaration = DSC != DeclSpecContext::DSC_trailing;
Richard Smithbfdb1082012-03-12 08:56:40 +00004325
Abramo Bagnarad7548482010-05-19 21:37:53 +00004326 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004327 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00004328 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
4329 // if a fixed underlying type is allowed.
Erik Pilkington6f11db12018-09-28 20:24:58 +00004330 ColonProtectionRAIIObject X(*this, AllowDeclaration);
Chad Rosierc1183952012-06-26 22:30:43 +00004331
Nico Webercfaa4cd2015-02-15 07:26:13 +00004332 CXXScopeSpec Spec;
David Blaikieefdccaa2016-01-15 23:43:34 +00004333 if (ParseOptionalCXXScopeSpecifier(Spec, nullptr,
Richard Smith1d4b2e12013-04-01 21:43:41 +00004334 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00004335 return;
4336
Nico Webercfaa4cd2015-02-15 07:26:13 +00004337 if (Spec.isSet() && Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00004338 Diag(Tok, diag::err_expected) << tok::identifier;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004339 if (Tok.isNot(tok::l_brace)) {
4340 // Has no name and is not a definition.
4341 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00004342 SkipUntil(tok::comma, StopAtSemi);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004343 return;
4344 }
4345 }
Nico Webercfaa4cd2015-02-15 07:26:13 +00004346
4347 SS = Spec;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004348 }
Mike Stump11289f42009-09-09 15:08:12 +00004349
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00004350 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00004351 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Erik Pilkington6f11db12018-09-28 20:24:58 +00004352 !(AllowDeclaration && Tok.is(tok::colon))) {
Alp Tokerec543272013-12-24 09:48:30 +00004353 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
Mike Stump11289f42009-09-09 15:08:12 +00004354
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00004355 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00004356 SkipUntil(tok::comma, StopAtSemi);
Chris Lattner3b561a32006-08-13 00:12:11 +00004357 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00004358 }
Mike Stump11289f42009-09-09 15:08:12 +00004359
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00004360 // If an identifier is present, consume and remember it.
Craig Topper161e4db2014-05-21 06:02:52 +00004361 IdentifierInfo *Name = nullptr;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00004362 SourceLocation NameLoc;
4363 if (Tok.is(tok::identifier)) {
4364 Name = Tok.getIdentifierInfo();
4365 NameLoc = ConsumeToken();
4366 }
Mike Stump11289f42009-09-09 15:08:12 +00004367
Richard Smith0f8ee222012-01-10 01:33:14 +00004368 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00004369 // C++0x 7.2p2: The optional identifier shall not be omitted in the
4370 // declaration of a scoped enumeration.
4371 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00004372 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00004373 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00004374 }
4375
John McCall6347b682012-05-07 06:16:58 +00004376 // Okay, end the suppression area. We'll decide whether to emit the
4377 // diagnostics in a second.
4378 if (shouldDelayDiagsInTag)
4379 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00004380
Douglas Gregor0bf31402010-10-08 23:50:27 +00004381 TypeResult BaseType;
4382
Douglas Gregord1f69f62010-12-01 17:42:47 +00004383 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00004384 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Erik Pilkington6f11db12018-09-28 20:24:58 +00004385 if (AllowDeclaration && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00004386 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00004387 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00004388 // If we're in class scope, this can either be an enum declaration with
4389 // an underlying type, or a declaration of a bitfield member. We try to
4390 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00004391 // (integer literal, sizeof); if it's still ambiguous, we then consider
4392 // anything that's a simple-type-specifier followed by '(' as an
4393 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00004394 // underlying types anyway.
Faisal Valid143a0c2017-04-01 21:30:49 +00004395 EnterExpressionEvaluationContext Unevaluated(
4396 Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00004397 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00004398 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00004399 // bit-field. This is the common case.
Richard Smithee390432014-05-16 01:56:53 +00004400 if (TPR == TPResult::True)
Douglas Gregord1f69f62010-12-01 17:42:47 +00004401 PossibleBitfield = true;
4402 // If the next token starts a type-specifier-seq, it may be either a
4403 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00004404 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00004405 // fixed underlying type.
Richard Smithee390432014-05-16 01:56:53 +00004406 else if (TPR == TPResult::False &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00004407 GetLookAheadToken(2).getKind() == tok::semi) {
4408 // Consume the ':'.
4409 ConsumeToken();
4410 } else {
4411 // We have the start of a type-specifier-seq, so we have to perform
4412 // tentative parsing to determine whether we have an expression or a
4413 // type.
4414 TentativeParsingAction TPA(*this);
4415
4416 // Consume the ':'.
4417 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00004418
4419 // If we see a type specifier followed by an open-brace, we have an
4420 // ambiguity between an underlying type and a C++11 braced
4421 // function-style cast. Resolve this by always treating it as an
4422 // underlying type.
4423 // FIXME: The standard is not entirely clear on how to disambiguate in
4424 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004425 if ((getLangOpts().CPlusPlus &&
Richard Smithee390432014-05-16 01:56:53 +00004426 isCXXDeclarationSpecifier(TPResult::True) != TPResult::True) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00004427 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00004428 // We'll parse this as a bitfield later.
4429 PossibleBitfield = true;
4430 TPA.Revert();
4431 } else {
4432 // We have a type-specifier-seq.
4433 TPA.Commit();
4434 }
4435 }
4436 } else {
4437 // Consume the ':'.
4438 ConsumeToken();
4439 }
4440
4441 if (!PossibleBitfield) {
4442 SourceRange Range;
4443 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00004444
Erik Pilkingtonfa983902018-10-30 20:31:30 +00004445 if (!getLangOpts().ObjC) {
Erik Pilkington6f11db12018-09-28 20:24:58 +00004446 if (getLangOpts().CPlusPlus11)
4447 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
4448 else if (getLangOpts().CPlusPlus)
4449 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type);
4450 else if (getLangOpts().MicrosoftExt)
4451 Diag(StartLoc, diag::ext_ms_c_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00004452 else
Erik Pilkington6f11db12018-09-28 20:24:58 +00004453 Diag(StartLoc, diag::ext_clang_c_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00004454 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00004455 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00004456 }
4457
Richard Smith0f8ee222012-01-10 01:33:14 +00004458 // There are four options here. If we have 'friend enum foo;' then this is a
4459 // friend declaration, and cannot have an accompanying definition. If we have
4460 // 'enum foo;', then this is a forward declaration. If we have
4461 // 'enum foo {...' then this is a definition. Otherwise we have something
4462 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00004463 //
4464 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
4465 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
4466 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
4467 //
John McCallfaf5fb42010-08-26 23:41:50 +00004468 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00004469 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00004470 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00004471 } else if (Tok.is(tok::l_brace)) {
4472 if (DS.isFriendSpecified()) {
4473 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
4474 << SourceRange(DS.getFriendSpecLoc());
4475 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00004476 SkipUntil(tok::r_brace, StopAtSemi);
John McCall6347b682012-05-07 06:16:58 +00004477 TUK = Sema::TUK_Friend;
4478 } else {
4479 TUK = Sema::TUK_Definition;
4480 }
Richard Smith649c7b062014-01-08 00:56:48 +00004481 } else if (!isTypeSpecifier(DSC) &&
Richard Smith369b9f92012-06-25 21:37:02 +00004482 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00004483 (Tok.isAtStartOfLine() &&
4484 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00004485 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
4486 if (Tok.isNot(tok::semi)) {
4487 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00004488 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Ilya Biryukov929af672019-05-17 09:32:05 +00004489 PP.EnterToken(Tok, /*IsReinject=*/true);
Richard Smith369b9f92012-06-25 21:37:02 +00004490 Tok.setKind(tok::semi);
4491 }
John McCall6347b682012-05-07 06:16:58 +00004492 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00004493 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00004494 }
4495
4496 // If this is an elaborated type specifier, and we delayed
4497 // diagnostics before, just merge them into the current pool.
4498 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
4499 diagsFromTag.redelay();
4500 }
Richard Smith7d137e32012-03-23 03:33:32 +00004501
4502 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00004503 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00004504 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004505 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00004506 // Skip the rest of this declarator, up until the comma or semicolon.
4507 Diag(Tok, diag::err_enum_template);
Alexey Bataevee6507d2013-11-18 08:17:37 +00004508 SkipUntil(tok::comma, StopAtSemi);
Richard Smith7d137e32012-03-23 03:33:32 +00004509 return;
4510 }
4511
4512 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
4513 // Enumerations can't be explicitly instantiated.
4514 DS.SetTypeSpecError();
4515 Diag(StartLoc, diag::err_explicit_instantiation_enum);
4516 return;
4517 }
4518
4519 assert(TemplateInfo.TemplateParams && "no template parameters");
4520 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
4521 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00004522 }
Chad Rosierc1183952012-06-26 22:30:43 +00004523
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00004524 if (TUK == Sema::TUK_Reference)
4525 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00004526
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00004527 if (!Name && TUK != Sema::TUK_Definition) {
4528 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00004529
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00004530 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00004531 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00004532 return;
4533 }
Richard Smith7d137e32012-03-23 03:33:32 +00004534
Nico Weber32a0fc72016-09-03 03:01:32 +00004535 stripTypeAttributesOffDeclSpec(attrs, DS, TUK);
David Majnemer936b4112015-04-19 07:53:29 +00004536
Richard Smithd9ba2242015-05-07 03:54:19 +00004537 Sema::SkipBodyInfo SkipBody;
4538 if (!Name && TUK == Sema::TUK_Definition && Tok.is(tok::l_brace) &&
4539 NextToken().is(tok::identifier))
4540 SkipBody = Actions.shouldSkipAnonEnumBody(getCurScope(),
4541 NextToken().getIdentifierInfo(),
4542 NextToken().getLocation());
4543
Douglas Gregord6ab8742009-05-28 23:31:59 +00004544 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00004545 bool IsDependent = false;
Craig Topper161e4db2014-05-21 06:02:52 +00004546 const char *PrevSpec = nullptr;
Douglas Gregorba41d012010-04-24 16:38:41 +00004547 unsigned DiagID;
Faisal Vali7db85c52017-12-31 00:06:40 +00004548 Decl *TagDecl = Actions.ActOnTag(
4549 getCurScope(), DeclSpec::TST_enum, TUK, StartLoc, SS, Name, NameLoc,
Erich Keanec480f302018-07-12 21:09:05 +00004550 attrs, AS, DS.getModulePrivateSpecLoc(), TParams, Owned, IsDependent,
4551 ScopedEnumKWLoc, IsScopedUsingClassTag, BaseType,
Faisal Vali7db85c52017-12-31 00:06:40 +00004552 DSC == DeclSpecContext::DSC_type_specifier,
4553 DSC == DeclSpecContext::DSC_template_param ||
4554 DSC == DeclSpecContext::DSC_template_type_arg,
4555 &SkipBody);
Richard Smithd9ba2242015-05-07 03:54:19 +00004556
4557 if (SkipBody.ShouldSkip) {
4558 assert(TUK == Sema::TUK_Definition && "can only skip a definition");
4559
4560 BalancedDelimiterTracker T(*this, tok::l_brace);
4561 T.consumeOpen();
4562 T.skipToEnd();
4563
4564 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
4565 NameLoc.isValid() ? NameLoc : StartLoc,
4566 PrevSpec, DiagID, TagDecl, Owned,
4567 Actions.getASTContext().getPrintingPolicy()))
4568 Diag(StartLoc, DiagID) << PrevSpec;
4569 return;
4570 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00004571
Douglas Gregorba41d012010-04-24 16:38:41 +00004572 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00004573 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00004574 // dependent tag.
4575 if (!Name) {
4576 DS.SetTypeSpecError();
4577 Diag(Tok, diag::err_expected_type_name_after_typename);
4578 return;
4579 }
Chad Rosierc1183952012-06-26 22:30:43 +00004580
Nico Weber83ea0122014-05-03 21:57:40 +00004581 TypeResult Type = Actions.ActOnDependentTag(
4582 getCurScope(), DeclSpec::TST_enum, TUK, SS, Name, StartLoc, NameLoc);
Douglas Gregorba41d012010-04-24 16:38:41 +00004583 if (Type.isInvalid()) {
4584 DS.SetTypeSpecError();
4585 return;
4586 }
Chad Rosierc1183952012-06-26 22:30:43 +00004587
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00004588 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
4589 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00004590 PrevSpec, DiagID, Type.get(),
4591 Actions.getASTContext().getPrintingPolicy()))
Douglas Gregorba41d012010-04-24 16:38:41 +00004592 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00004593
Douglas Gregorba41d012010-04-24 16:38:41 +00004594 return;
4595 }
Mike Stump11289f42009-09-09 15:08:12 +00004596
John McCall48871652010-08-21 09:40:31 +00004597 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00004598 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00004599 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00004600 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00004601 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00004602 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregorba41d012010-04-24 16:38:41 +00004603 }
Chad Rosierc1183952012-06-26 22:30:43 +00004604
Douglas Gregorba41d012010-04-24 16:38:41 +00004605 DS.SetTypeSpecError();
4606 return;
4607 }
Richard Smith0f8ee222012-01-10 01:33:14 +00004608
Bruno Cardoso Lopesdf0ee342017-07-01 00:06:47 +00004609 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
4610 Decl *D = SkipBody.CheckSameAsPrevious ? SkipBody.New : TagDecl;
4611 ParseEnumBody(StartLoc, D);
4612 if (SkipBody.CheckSameAsPrevious &&
4613 !Actions.ActOnDuplicateDefinition(DS, TagDecl, SkipBody)) {
4614 DS.SetTypeSpecError();
4615 return;
4616 }
4617 }
Mike Stump11289f42009-09-09 15:08:12 +00004618
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00004619 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
4620 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00004621 PrevSpec, DiagID, TagDecl, Owned,
4622 Actions.getASTContext().getPrintingPolicy()))
John McCall49bfce42009-08-03 20:12:06 +00004623 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00004624}
4625
Chris Lattnerc1915e22007-01-25 07:29:02 +00004626/// ParseEnumBody - Parse a {} enclosed enumerator-list.
4627/// enumerator-list:
4628/// enumerator
4629/// enumerator-list ',' enumerator
4630/// enumerator:
Aaron Ballman730476b2014-11-08 15:33:35 +00004631/// enumeration-constant attributes[opt]
4632/// enumeration-constant attributes[opt] '=' constant-expression
Chris Lattnerc1915e22007-01-25 07:29:02 +00004633/// enumeration-constant:
4634/// identifier
4635///
John McCall48871652010-08-21 09:40:31 +00004636void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00004637 // Enter the scope of the enum body and start the definition.
Hans Wennborgfe781452014-06-17 00:00:18 +00004638 ParseScope EnumScope(this, Scope::DeclScope | Scope::EnumScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004639 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00004640
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004641 BalancedDelimiterTracker T(*this, tok::l_brace);
4642 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00004643
Chris Lattner37256fb2007-08-27 17:24:30 +00004644 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00004645 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Richard Smithf8812672016-12-02 22:38:31 +00004646 Diag(Tok, diag::err_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00004647
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004648 SmallVector<Decl *, 32> EnumConstantDecls;
Jordan Rose60ac3162015-04-30 17:20:30 +00004649 SmallVector<SuppressAccessChecks, 32> EnumAvailabilityDiags;
Chris Lattnerc1915e22007-01-25 07:29:02 +00004650
Craig Topper161e4db2014-05-21 06:02:52 +00004651 Decl *LastEnumConstDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00004652
Chris Lattnerc1915e22007-01-25 07:29:02 +00004653 // Parse the enumerator-list.
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00004654 while (Tok.isNot(tok::r_brace)) {
4655 // Parse enumerator. If failed, try skipping till the start of the next
4656 // enumerator definition.
4657 if (Tok.isNot(tok::identifier)) {
4658 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
4659 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) &&
4660 TryConsumeToken(tok::comma))
4661 continue;
4662 break;
4663 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00004664 IdentifierInfo *Ident = Tok.getIdentifierInfo();
4665 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004666
John McCall811a0f52010-10-22 23:36:17 +00004667 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00004668 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004669 MaybeParseGNUAttributes(attrs);
Aaron Ballman730476b2014-11-08 15:33:35 +00004670 ProhibitAttributes(attrs); // GNU-style attributes are prohibited.
Aaron Ballman606093a2017-10-15 15:01:42 +00004671 if (standardAttributesAllowed() && isCXX11AttributeSpecifier()) {
4672 if (getLangOpts().CPlusPlus)
Aaron Ballmanc351fba2017-12-04 20:27:34 +00004673 Diag(Tok.getLocation(), getLangOpts().CPlusPlus17
Aaron Ballman606093a2017-10-15 15:01:42 +00004674 ? diag::warn_cxx14_compat_ns_enum_attribute
4675 : diag::ext_ns_enum_attribute)
4676 << 1 /*enumerator*/;
Aaron Ballman730476b2014-11-08 15:33:35 +00004677 ParseCXX11Attributes(attrs);
4678 }
John McCall811a0f52010-10-22 23:36:17 +00004679
Chris Lattnerc1915e22007-01-25 07:29:02 +00004680 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00004681 ExprResult AssignedVal;
Jordan Rose60ac3162015-04-30 17:20:30 +00004682 EnumAvailabilityDiags.emplace_back(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00004683
Richard Smith40c3d6e32019-09-19 22:00:16 +00004684 EnterExpressionEvaluationContext ConstantEvaluated(
4685 Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
Alp Tokera3ebe6e2013-12-17 14:12:37 +00004686 if (TryConsumeToken(tok::equal, EqualLoc)) {
Richard Smith40c3d6e32019-09-19 22:00:16 +00004687 AssignedVal = ParseConstantExpressionInExprEvalContext();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00004688 if (AssignedVal.isInvalid())
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00004689 SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch);
Chris Lattnerc1915e22007-01-25 07:29:02 +00004690 }
Mike Stump11289f42009-09-09 15:08:12 +00004691
Chris Lattnerc1915e22007-01-25 07:29:02 +00004692 // Install the enumerator constant into EnumDecl.
Bruno Cardoso Lopesdf0ee342017-07-01 00:06:47 +00004693 Decl *EnumConstDecl = Actions.ActOnEnumConstant(
Erich Keanec480f302018-07-12 21:09:05 +00004694 getCurScope(), EnumDecl, LastEnumConstDecl, IdentLoc, Ident, attrs,
4695 EqualLoc, AssignedVal.get());
Jordan Rose60ac3162015-04-30 17:20:30 +00004696 EnumAvailabilityDiags.back().done();
Chad Rosierc1183952012-06-26 22:30:43 +00004697
Chris Lattner4ef40012007-06-11 01:28:17 +00004698 EnumConstantDecls.push_back(EnumConstDecl);
4699 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00004700
Douglas Gregorce66d022010-09-07 14:51:08 +00004701 if (Tok.is(tok::identifier)) {
4702 // We're missing a comma between enumerators.
Richard Smithbdb84f32016-07-22 23:36:59 +00004703 SourceLocation Loc = getEndOfPreviousToken();
Chad Rosierc1183952012-06-26 22:30:43 +00004704 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00004705 << FixItHint::CreateInsertion(Loc, ", ");
4706 continue;
4707 }
Chad Rosierc1183952012-06-26 22:30:43 +00004708
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00004709 // Emumerator definition must be finished, only comma or r_brace are
4710 // allowed here.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00004711 SourceLocation CommaLoc;
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00004712 if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) {
4713 if (EqualLoc.isValid())
4714 Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace
4715 << tok::comma;
4716 else
4717 Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator);
4718 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) {
4719 if (TryConsumeToken(tok::comma, CommaLoc))
4720 continue;
4721 } else {
4722 break;
4723 }
4724 }
Mike Stump11289f42009-09-09 15:08:12 +00004725
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00004726 // If comma is followed by r_brace, emit appropriate warning.
4727 if (Tok.is(tok::r_brace) && CommaLoc.isValid()) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004728 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00004729 Diag(CommaLoc, getLangOpts().CPlusPlus ?
4730 diag::ext_enumerator_list_comma_cxx :
4731 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00004732 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004733 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00004734 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
4735 << FixItHint::CreateRemoval(CommaLoc);
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00004736 break;
Richard Smith5d164bc2011-10-15 05:09:34 +00004737 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00004738 }
Mike Stump11289f42009-09-09 15:08:12 +00004739
Chris Lattnerc1915e22007-01-25 07:29:02 +00004740 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004741 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00004742
Chris Lattnerc1915e22007-01-25 07:29:02 +00004743 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00004744 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004745 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00004746
Erich Keanec480f302018-07-12 21:09:05 +00004747 Actions.ActOnEnumBody(StartLoc, T.getRange(), EnumDecl, EnumConstantDecls,
4748 getCurScope(), attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004749
Jordan Rose60ac3162015-04-30 17:20:30 +00004750 // Now handle enum constant availability diagnostics.
4751 assert(EnumConstantDecls.size() == EnumAvailabilityDiags.size());
4752 for (size_t i = 0, e = EnumConstantDecls.size(); i != e; ++i) {
4753 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
4754 EnumAvailabilityDiags[i].redelay();
4755 PD.complete(EnumConstantDecls[i]);
4756 }
4757
Douglas Gregor82ac25e2009-01-08 20:45:30 +00004758 EnumScope.Exit();
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00004759 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, T.getRange());
Richard Smith369b9f92012-06-25 21:37:02 +00004760
4761 // The next token must be valid after an enum definition. If not, a ';'
4762 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00004763 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
4764 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Alp Toker383d2c42014-01-01 03:08:43 +00004765 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Richard Smith369b9f92012-06-25 21:37:02 +00004766 // Push this token back into the preprocessor and change our current token
4767 // to ';' so that the rest of the code recovers as though there were an
4768 // ';' after the definition.
Ilya Biryukov929af672019-05-17 09:32:05 +00004769 PP.EnterToken(Tok, /*IsReinject=*/true);
Richard Smith369b9f92012-06-25 21:37:02 +00004770 Tok.setKind(tok::semi);
4771 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00004772}
Chris Lattner3b561a32006-08-13 00:12:11 +00004773
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004774/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
4775/// is definitely a type-specifier. Return false if it isn't part of a type
4776/// specifier or if we're not sure.
4777bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
4778 switch (Tok.getKind()) {
4779 default: return false;
4780 // type-specifiers
4781 case tok::kw_short:
4782 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004783 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004784 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004785 case tok::kw_signed:
4786 case tok::kw_unsigned:
4787 case tok::kw__Complex:
4788 case tok::kw__Imaginary:
4789 case tok::kw_void:
4790 case tok::kw_char:
4791 case tok::kw_wchar_t:
Richard Smith3a8244d2018-05-01 05:02:45 +00004792 case tok::kw_char8_t:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004793 case tok::kw_char16_t:
4794 case tok::kw_char32_t:
4795 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004796 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004797 case tok::kw_float:
4798 case tok::kw_double:
Leonard Chanf921d852018-06-04 16:07:52 +00004799 case tok::kw__Accum:
Leonard Chanab80f3c2018-06-14 14:53:51 +00004800 case tok::kw__Fract:
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00004801 case tok::kw__Float16:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00004802 case tok::kw___float128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004803 case tok::kw_bool:
4804 case tok::kw__Bool:
4805 case tok::kw__Decimal32:
4806 case tok::kw__Decimal64:
4807 case tok::kw__Decimal128:
4808 case tok::kw___vector:
Alexey Bader954ba212016-04-08 13:40:33 +00004809#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
Alexey Baderb62f1442016-04-13 08:33:41 +00004810#include "clang/Basic/OpenCLImageTypes.def"
Chad Rosierc1183952012-06-26 22:30:43 +00004811
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004812 // struct-or-union-specifier (C99) or class-specifier (C++)
4813 case tok::kw_class:
4814 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004815 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004816 case tok::kw_union:
4817 // enum-specifier
4818 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00004819
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004820 // typedef-name
4821 case tok::annot_typename:
4822 return true;
4823 }
4824}
4825
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004826/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004827/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004828bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004829 switch (Tok.getKind()) {
4830 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004831
Chris Lattner020bab92009-01-04 23:41:41 +00004832 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00004833 if (TryAltiVecVectorToken())
4834 return true;
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +00004835 LLVM_FALLTHROUGH;
Douglas Gregor333489b2009-03-27 23:10:48 +00004836 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004837 // Annotate typenames and C++ scope specifiers. If we get one, just
4838 // recurse to handle whatever we get.
4839 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004840 return true;
4841 if (Tok.is(tok::identifier))
4842 return false;
4843 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00004844
Chris Lattner020bab92009-01-04 23:41:41 +00004845 case tok::coloncolon: // ::foo::bar
4846 if (NextToken().is(tok::kw_new) || // ::new
4847 NextToken().is(tok::kw_delete)) // ::delete
4848 return false;
4849
Chris Lattner020bab92009-01-04 23:41:41 +00004850 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004851 return true;
4852 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00004853
Chris Lattnere37e2332006-08-15 04:50:22 +00004854 // GNU attributes support.
4855 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00004856 // GNU typeof support.
4857 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004858
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004859 // type-specifiers
4860 case tok::kw_short:
4861 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004862 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004863 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004864 case tok::kw_signed:
4865 case tok::kw_unsigned:
4866 case tok::kw__Complex:
4867 case tok::kw__Imaginary:
4868 case tok::kw_void:
4869 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004870 case tok::kw_wchar_t:
Richard Smith3a8244d2018-05-01 05:02:45 +00004871 case tok::kw_char8_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004872 case tok::kw_char16_t:
4873 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004874 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004875 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004876 case tok::kw_float:
4877 case tok::kw_double:
Leonard Chanf921d852018-06-04 16:07:52 +00004878 case tok::kw__Accum:
Leonard Chanab80f3c2018-06-14 14:53:51 +00004879 case tok::kw__Fract:
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00004880 case tok::kw__Float16:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00004881 case tok::kw___float128:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004882 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004883 case tok::kw__Bool:
4884 case tok::kw__Decimal32:
4885 case tok::kw__Decimal64:
4886 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004887 case tok::kw___vector:
Alexey Bader954ba212016-04-08 13:40:33 +00004888#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
Alexey Baderb62f1442016-04-13 08:33:41 +00004889#include "clang/Basic/OpenCLImageTypes.def"
Mike Stump11289f42009-09-09 15:08:12 +00004890
Chris Lattner861a2262008-04-13 18:59:07 +00004891 // struct-or-union-specifier (C99) or class-specifier (C++)
4892 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004893 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004894 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004895 case tok::kw_union:
4896 // enum-specifier
4897 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004898
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004899 // type-qualifier
4900 case tok::kw_const:
4901 case tok::kw_volatile:
4902 case tok::kw_restrict:
Leonard Chanab80f3c2018-06-14 14:53:51 +00004903 case tok::kw__Sat:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004904
John McCallea0a39e2012-11-14 00:49:39 +00004905 // Debugger support.
4906 case tok::kw___unknown_anytype:
4907
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004908 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00004909 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004910 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004911
Chris Lattner409bf7d2008-10-20 00:25:30 +00004912 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4913 case tok::less:
Erik Pilkingtonfa983902018-10-30 20:31:30 +00004914 return getLangOpts().ObjC;
Mike Stump11289f42009-09-09 15:08:12 +00004915
Steve Naroff44ac7772008-12-25 14:16:32 +00004916 case tok::kw___cdecl:
4917 case tok::kw___stdcall:
4918 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004919 case tok::kw___thiscall:
Erich Keane757d3172016-11-02 18:29:35 +00004920 case tok::kw___regcall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00004921 case tok::kw___vectorcall:
Eli Friedman53339e02009-06-08 23:27:34 +00004922 case tok::kw___w64:
4923 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004924 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004925 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004926 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004927
Douglas Gregoraea7afd2015-06-24 22:02:08 +00004928 case tok::kw__Nonnull:
4929 case tok::kw__Nullable:
4930 case tok::kw__Null_unspecified:
Douglas Gregor261a89b2015-06-19 17:51:05 +00004931
Douglas Gregorab209d82015-07-07 03:58:42 +00004932 case tok::kw___kindof:
4933
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004934 case tok::kw___private:
4935 case tok::kw___local:
4936 case tok::kw___global:
4937 case tok::kw___constant:
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00004938 case tok::kw___generic:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004939 case tok::kw___read_only:
4940 case tok::kw___read_write:
4941 case tok::kw___write_only:
Eli Friedman53339e02009-06-08 23:27:34 +00004942 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004943
Anastasia Stulova314fab62019-03-28 11:47:14 +00004944 case tok::kw_private:
4945 return getLangOpts().OpenCL;
4946
Richard Smith8e1ac332013-03-28 01:55:44 +00004947 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004948 case tok::kw__Atomic:
4949 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004950 }
4951}
4952
Chris Lattneracd58a32006-08-06 17:24:14 +00004953/// isDeclarationSpecifier() - Return true if the current token is part of a
4954/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004955///
4956/// \param DisambiguatingWithExpression True to indicate that the purpose of
4957/// this check is to disambiguate between an expression and a declaration.
4958bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004959 switch (Tok.getKind()) {
4960 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004961
Xiuli Pan9c14e282016-01-09 12:53:17 +00004962 case tok::kw_pipe:
Sven van Haastregte518bb42019-05-22 13:12:20 +00004963 return (getLangOpts().OpenCL && getLangOpts().OpenCLVersion >= 200) ||
4964 getLangOpts().OpenCLCPlusPlus;
Xiuli Pan9c14e282016-01-09 12:53:17 +00004965
Chris Lattner020bab92009-01-04 23:41:41 +00004966 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00004967 // Unfortunate hack to support "Class.factoryMethod" notation.
Erik Pilkingtonfa983902018-10-30 20:31:30 +00004968 if (getLangOpts().ObjC && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004969 return false;
John Thompson22334602010-02-05 00:12:22 +00004970 if (TryAltiVecVectorToken())
4971 return true;
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +00004972 LLVM_FALLTHROUGH;
David Blaikie15a430a2011-12-04 05:04:18 +00004973 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004974 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004975 // Annotate typenames and C++ scope specifiers. If we get one, just
4976 // recurse to handle whatever we get.
4977 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004978 return true;
4979 if (Tok.is(tok::identifier))
4980 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004981
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004982 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004983 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004984 // expression is permitted, then this is probably a class message send
4985 // missing the initial '['. In this case, we won't consider this to be
4986 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004987 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004988 isStartOfObjCClassMessageMissingOpenBracket())
4989 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004990
John McCall1f476a12010-02-26 08:45:28 +00004991 return isDeclarationSpecifier();
4992
Chris Lattner020bab92009-01-04 23:41:41 +00004993 case tok::coloncolon: // ::foo::bar
4994 if (NextToken().is(tok::kw_new) || // ::new
4995 NextToken().is(tok::kw_delete)) // ::delete
4996 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004997
Chris Lattner020bab92009-01-04 23:41:41 +00004998 // Annotate typenames and C++ scope specifiers. If we get one, just
4999 // recurse to handle whatever we get.
5000 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00005001 return true;
5002 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00005003
Chris Lattneracd58a32006-08-06 17:24:14 +00005004 // storage-class-specifier
5005 case tok::kw_typedef:
5006 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00005007 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00005008 case tok::kw_static:
5009 case tok::kw_auto:
Richard Smithe301ba22015-11-11 02:02:15 +00005010 case tok::kw___auto_type:
Chris Lattneracd58a32006-08-06 17:24:14 +00005011 case tok::kw_register:
5012 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00005013 case tok::kw_thread_local:
5014 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00005015
Douglas Gregor26701a42011-09-09 02:06:17 +00005016 // Modules
5017 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00005018
John McCallea0a39e2012-11-14 00:49:39 +00005019 // Debugger support
5020 case tok::kw___unknown_anytype:
5021
Chris Lattneracd58a32006-08-06 17:24:14 +00005022 // type-specifiers
5023 case tok::kw_short:
5024 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00005025 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00005026 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00005027 case tok::kw_signed:
5028 case tok::kw_unsigned:
5029 case tok::kw__Complex:
5030 case tok::kw__Imaginary:
5031 case tok::kw_void:
5032 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00005033 case tok::kw_wchar_t:
Richard Smith3a8244d2018-05-01 05:02:45 +00005034 case tok::kw_char8_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00005035 case tok::kw_char16_t:
5036 case tok::kw_char32_t:
5037
Chris Lattneracd58a32006-08-06 17:24:14 +00005038 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00005039 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00005040 case tok::kw_float:
5041 case tok::kw_double:
Leonard Chanf921d852018-06-04 16:07:52 +00005042 case tok::kw__Accum:
Leonard Chanab80f3c2018-06-14 14:53:51 +00005043 case tok::kw__Fract:
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00005044 case tok::kw__Float16:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00005045 case tok::kw___float128:
Chris Lattnerbb31a422007-11-15 05:25:19 +00005046 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00005047 case tok::kw__Bool:
5048 case tok::kw__Decimal32:
5049 case tok::kw__Decimal64:
5050 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00005051 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00005052
Chris Lattner861a2262008-04-13 18:59:07 +00005053 // struct-or-union-specifier (C99) or class-specifier (C++)
5054 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00005055 case tok::kw_struct:
5056 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00005057 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00005058 // enum-specifier
5059 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00005060
Chris Lattneracd58a32006-08-06 17:24:14 +00005061 // type-qualifier
5062 case tok::kw_const:
5063 case tok::kw_volatile:
5064 case tok::kw_restrict:
Leonard Chanab80f3c2018-06-14 14:53:51 +00005065 case tok::kw__Sat:
Steve Naroffad373bd2007-07-31 12:34:36 +00005066
Chris Lattneracd58a32006-08-06 17:24:14 +00005067 // function-specifier
5068 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00005069 case tok::kw_virtual:
5070 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00005071 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00005072
Richard Smith1dba27c2013-01-29 09:02:09 +00005073 // alignment-specifier
5074 case tok::kw__Alignas:
5075
Richard Smithd16fe122012-10-25 00:00:53 +00005076 // friend keyword.
5077 case tok::kw_friend:
5078
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00005079 // static_assert-declaration
5080 case tok::kw__Static_assert:
5081
Chris Lattner599e47e2007-08-09 17:01:07 +00005082 // GNU typeof support.
5083 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00005084
Chris Lattner599e47e2007-08-09 17:01:07 +00005085 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00005086 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00005087
Richard Smithd16fe122012-10-25 00:00:53 +00005088 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00005089 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00005090 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00005091
Richard Smitha6e8b682019-09-04 20:30:37 +00005092 // C++20 consteval and constinit.
Gauthier Harnisch796ed032019-06-14 08:56:20 +00005093 case tok::kw_consteval:
Richard Smitha6e8b682019-09-04 20:30:37 +00005094 case tok::kw_constinit:
Gauthier Harnisch796ed032019-06-14 08:56:20 +00005095
Richard Smith8e1ac332013-03-28 01:55:44 +00005096 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00005097 case tok::kw__Atomic:
5098 return true;
5099
Chris Lattner8b2ec162008-07-26 03:38:44 +00005100 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
5101 case tok::less:
Erik Pilkingtonfa983902018-10-30 20:31:30 +00005102 return getLangOpts().ObjC;
Mike Stump11289f42009-09-09 15:08:12 +00005103
Douglas Gregor19b7acf2011-04-27 05:41:15 +00005104 // typedef-name
5105 case tok::annot_typename:
5106 return !DisambiguatingWithExpression ||
5107 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00005108
Steve Narofff192fab2009-01-06 19:34:12 +00005109 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00005110 case tok::kw___cdecl:
5111 case tok::kw___stdcall:
5112 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00005113 case tok::kw___thiscall:
Erich Keane757d3172016-11-02 18:29:35 +00005114 case tok::kw___regcall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00005115 case tok::kw___vectorcall:
Eli Friedman53339e02009-06-08 23:27:34 +00005116 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00005117 case tok::kw___sptr:
5118 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00005119 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00005120 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00005121 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00005122 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00005123 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005124
Douglas Gregoraea7afd2015-06-24 22:02:08 +00005125 case tok::kw__Nonnull:
5126 case tok::kw__Nullable:
5127 case tok::kw__Null_unspecified:
Douglas Gregor261a89b2015-06-19 17:51:05 +00005128
Douglas Gregorab209d82015-07-07 03:58:42 +00005129 case tok::kw___kindof:
5130
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005131 case tok::kw___private:
5132 case tok::kw___local:
5133 case tok::kw___global:
5134 case tok::kw___constant:
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00005135 case tok::kw___generic:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005136 case tok::kw___read_only:
5137 case tok::kw___read_write:
5138 case tok::kw___write_only:
Alexey Bader954ba212016-04-08 13:40:33 +00005139#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
Alexey Baderb62f1442016-04-13 08:33:41 +00005140#include "clang/Basic/OpenCLImageTypes.def"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005141
Eli Friedman53339e02009-06-08 23:27:34 +00005142 return true;
Anastasia Stulova314fab62019-03-28 11:47:14 +00005143
5144 case tok::kw_private:
5145 return getLangOpts().OpenCL;
Chris Lattneracd58a32006-08-06 17:24:14 +00005146 }
5147}
5148
Richard Smith35845152017-02-07 01:37:30 +00005149bool Parser::isConstructorDeclarator(bool IsUnqualified, bool DeductionGuide) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005150 TentativeParsingAction TPA(*this);
5151
5152 // Parse the C++ scope specifier.
5153 CXXScopeSpec SS;
David Blaikieefdccaa2016-01-15 23:43:34 +00005154 if (ParseOptionalCXXScopeSpecifier(SS, nullptr,
Douglas Gregordf593fb2011-11-07 17:33:42 +00005155 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00005156 TPA.Revert();
5157 return false;
5158 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005159
5160 // Parse the constructor name.
Richard Smithaf3b3252017-05-18 19:21:48 +00005161 if (Tok.is(tok::identifier)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005162 // We already know that we have a constructor name; just consume
5163 // the token.
5164 ConsumeToken();
Richard Smithaf3b3252017-05-18 19:21:48 +00005165 } else if (Tok.is(tok::annot_template_id)) {
5166 ConsumeAnnotationToken();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005167 } else {
5168 TPA.Revert();
5169 return false;
5170 }
5171
Richard Smith8f8697f2017-02-08 01:16:55 +00005172 // There may be attributes here, appertaining to the constructor name or type
5173 // we just stepped past.
5174 SkipCXX11Attributes();
5175
Richard Smith43f340f2012-03-27 23:05:05 +00005176 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005177 if (Tok.isNot(tok::l_paren)) {
5178 TPA.Revert();
5179 return false;
5180 }
5181 ConsumeParen();
5182
Richard Smith43f340f2012-03-27 23:05:05 +00005183 // A right parenthesis, or ellipsis followed by a right parenthesis signals
5184 // that we have a constructor.
5185 if (Tok.is(tok::r_paren) ||
5186 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005187 TPA.Revert();
5188 return true;
5189 }
5190
Richard Smithf2163662013-09-06 00:12:20 +00005191 // A C++11 attribute here signals that we have a constructor, and is an
5192 // attribute on the first constructor parameter.
5193 if (getLangOpts().CPlusPlus11 &&
5194 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
5195 /*OuterMightBeMessageSend*/ true)) {
5196 TPA.Revert();
5197 return true;
5198 }
5199
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005200 // If we need to, enter the specified scope.
5201 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00005202 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005203 DeclScopeObj.EnterDeclaratorScope();
5204
Francois Pichet79f3a872011-01-31 04:54:32 +00005205 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00005206 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00005207 MaybeParseMicrosoftAttributes(Attrs);
5208
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005209 // Check whether the next token(s) are part of a declaration
5210 // specifier, in which case we have the start of a parameter and,
5211 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00005212 bool IsConstructor = false;
5213 if (isDeclarationSpecifier())
5214 IsConstructor = true;
5215 else if (Tok.is(tok::identifier) ||
5216 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
5217 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
5218 // This might be a parenthesized member name, but is more likely to
5219 // be a constructor declaration with an invalid argument type. Keep
5220 // looking.
5221 if (Tok.is(tok::annot_cxxscope))
Richard Smithaf3b3252017-05-18 19:21:48 +00005222 ConsumeAnnotationToken();
Richard Smithefd009d2012-03-27 00:56:56 +00005223 ConsumeToken();
5224
5225 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00005226 // which must have one of the following syntactic forms (see the
5227 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00005228 switch (Tok.getKind()) {
5229 case tok::l_paren:
5230 // C(X ( int));
5231 case tok::l_square:
5232 // C(X [ 5]);
5233 // C(X [ [attribute]]);
5234 case tok::coloncolon:
5235 // C(X :: Y);
5236 // C(X :: *p);
Richard Smithefd009d2012-03-27 00:56:56 +00005237 // Assume this isn't a constructor, rather than assuming it's a
5238 // constructor with an unnamed parameter of an ill-formed type.
5239 break;
5240
Richard Smith446161b2014-03-03 21:12:53 +00005241 case tok::r_paren:
5242 // C(X )
Richard Smith8f8697f2017-02-08 01:16:55 +00005243
5244 // Skip past the right-paren and any following attributes to get to
5245 // the function body or trailing-return-type.
5246 ConsumeParen();
5247 SkipCXX11Attributes();
5248
Richard Smith35845152017-02-07 01:37:30 +00005249 if (DeductionGuide) {
5250 // C(X) -> ... is a deduction guide.
Richard Smith8f8697f2017-02-08 01:16:55 +00005251 IsConstructor = Tok.is(tok::arrow);
Richard Smith35845152017-02-07 01:37:30 +00005252 break;
5253 }
Richard Smith8f8697f2017-02-08 01:16:55 +00005254 if (Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Richard Smith446161b2014-03-03 21:12:53 +00005255 // Assume these were meant to be constructors:
5256 // C(X) : (the name of a bit-field cannot be parenthesized).
5257 // C(X) try (this is otherwise ill-formed).
5258 IsConstructor = true;
5259 }
Richard Smith8f8697f2017-02-08 01:16:55 +00005260 if (Tok.is(tok::semi) || Tok.is(tok::l_brace)) {
Richard Smith446161b2014-03-03 21:12:53 +00005261 // If we have a constructor name within the class definition,
5262 // assume these were meant to be constructors:
5263 // C(X) {
5264 // C(X) ;
5265 // ... because otherwise we would be declaring a non-static data
5266 // member that is ill-formed because it's of the same type as its
5267 // surrounding class.
5268 //
5269 // FIXME: We can actually do this whether or not the name is qualified,
5270 // because if it is qualified in this context it must be being used as
Richard Smith35845152017-02-07 01:37:30 +00005271 // a constructor name.
Richard Smith446161b2014-03-03 21:12:53 +00005272 // currently, so we're somewhat conservative here.
5273 IsConstructor = IsUnqualified;
5274 }
5275 break;
5276
Richard Smithefd009d2012-03-27 00:56:56 +00005277 default:
5278 IsConstructor = true;
5279 break;
5280 }
5281 }
5282
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005283 TPA.Revert();
5284 return IsConstructor;
5285}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00005286
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005287/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00005288/// type-qualifier-list: [C99 6.7.5]
5289/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00005290/// [vendor] attributes
Aaron Ballman08b06592014-07-22 12:44:22 +00005291/// [ only if AttrReqs & AR_VendorAttributesParsed ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00005292/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00005293/// [vendor] type-qualifier-list attributes
Aaron Ballman08b06592014-07-22 12:44:22 +00005294/// [ only if AttrReqs & AR_VendorAttributesParsed ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00005295/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Aaron Ballman08b06592014-07-22 12:44:22 +00005296/// [ only if AttReqs & AR_CXX11AttributesParsed ]
5297/// Note: vendor can be GNU, MS, etc and can be explicitly controlled via
5298/// AttrRequirements bitmask values.
Alex Lorenz8f4d3992017-02-13 23:19:40 +00005299void Parser::ParseTypeQualifierListOpt(
5300 DeclSpec &DS, unsigned AttrReqs, bool AtomicAllowed,
5301 bool IdentifierRequired,
5302 Optional<llvm::function_ref<void()>> CodeCompletionHandler) {
Aaron Ballman606093a2017-10-15 15:01:42 +00005303 if (standardAttributesAllowed() && (AttrReqs & AR_CXX11AttributesParsed) &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005304 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00005305 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00005306 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005307 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005308 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00005309
5310 SourceLocation EndLoc;
5311
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005312 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00005313 bool isInvalid = false;
Craig Topper161e4db2014-05-21 06:02:52 +00005314 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +00005315 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00005316 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00005317
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005318 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00005319 case tok::code_completion:
Alex Lorenz8f4d3992017-02-13 23:19:40 +00005320 if (CodeCompletionHandler)
5321 (*CodeCompletionHandler)();
5322 else
5323 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00005324 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00005325
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005326 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00005327 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00005328 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00005329 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005330 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00005331 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00005332 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00005333 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005334 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00005335 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00005336 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005337 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00005338 case tok::kw__Atomic:
5339 if (!AtomicAllowed)
5340 goto DoneWithTypeQuals;
Aaron Ballman5cd5d562019-09-04 21:01:57 +00005341 if (!getLangOpts().C11)
5342 Diag(Tok, diag::ext_c11_feature) << Tok.getName();
Richard Smith8e1ac332013-03-28 01:55:44 +00005343 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
5344 getLangOpts());
5345 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005346
5347 // OpenCL qualifiers:
Anastasia Stulova948e37c2019-03-25 11:54:02 +00005348 case tok::kw_private:
Anastasia Stulova314fab62019-03-28 11:47:14 +00005349 if (!getLangOpts().OpenCL)
5350 goto DoneWithTypeQuals;
5351 LLVM_FALLTHROUGH;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005352 case tok::kw___private:
5353 case tok::kw___global:
5354 case tok::kw___local:
5355 case tok::kw___constant:
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00005356 case tok::kw___generic:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005357 case tok::kw___read_only:
5358 case tok::kw___write_only:
5359 case tok::kw___read_write:
Aaron Ballman05d76ea2014-01-14 01:29:54 +00005360 ParseOpenCLQualifiers(DS.getAttributes());
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005361 break;
5362
Andrey Bokhanko45d41322016-05-11 18:38:21 +00005363 case tok::kw___unaligned:
5364 isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID,
5365 getLangOpts());
5366 break;
Aaron Ballman317a77f2013-05-22 23:25:32 +00005367 case tok::kw___uptr:
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00005368 // GNU libc headers in C mode use '__uptr' as an identifier which conflicts
Alp Toker62c5b572013-11-26 01:30:10 +00005369 // with the MS modifier keyword.
Aaron Ballman08b06592014-07-22 12:44:22 +00005370 if ((AttrReqs & AR_DeclspecAttributesParsed) && !getLangOpts().CPlusPlus &&
Alp Toker47642d22013-12-03 06:13:01 +00005371 IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) {
5372 if (TryKeywordIdentFallback(false))
5373 continue;
Alp Toker62c5b572013-11-26 01:30:10 +00005374 }
Galina Kistanova77674252017-06-01 21:15:34 +00005375 LLVM_FALLTHROUGH;
Alp Toker62c5b572013-11-26 01:30:10 +00005376 case tok::kw___sptr:
Eli Friedman53339e02009-06-08 23:27:34 +00005377 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00005378 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00005379 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00005380 case tok::kw___cdecl:
5381 case tok::kw___stdcall:
5382 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00005383 case tok::kw___thiscall:
Erich Keane757d3172016-11-02 18:29:35 +00005384 case tok::kw___regcall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00005385 case tok::kw___vectorcall:
Aaron Ballman08b06592014-07-22 12:44:22 +00005386 if (AttrReqs & AR_DeclspecAttributesParsed) {
John McCall53fa7142010-12-24 02:08:15 +00005387 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00005388 continue;
5389 }
5390 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00005391 case tok::kw___pascal:
Aaron Ballman08b06592014-07-22 12:44:22 +00005392 if (AttrReqs & AR_VendorAttributesParsed) {
John McCall53fa7142010-12-24 02:08:15 +00005393 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00005394 continue;
5395 }
5396 goto DoneWithTypeQuals;
Douglas Gregor261a89b2015-06-19 17:51:05 +00005397
5398 // Nullability type specifiers.
Douglas Gregoraea7afd2015-06-24 22:02:08 +00005399 case tok::kw__Nonnull:
5400 case tok::kw__Nullable:
5401 case tok::kw__Null_unspecified:
Douglas Gregor261a89b2015-06-19 17:51:05 +00005402 ParseNullabilityTypeSpecifiers(DS.getAttributes());
5403 continue;
5404
Douglas Gregorab209d82015-07-07 03:58:42 +00005405 // Objective-C 'kindof' types.
5406 case tok::kw___kindof:
5407 DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc,
Erich Keanee891aa92018-07-13 15:07:47 +00005408 nullptr, 0, ParsedAttr::AS_Keyword);
Douglas Gregorab209d82015-07-07 03:58:42 +00005409 (void)ConsumeToken();
5410 continue;
5411
Chris Lattnere37e2332006-08-15 04:50:22 +00005412 case tok::kw___attribute:
Aaron Ballman08b06592014-07-22 12:44:22 +00005413 if (AttrReqs & AR_GNUAttributesParsedAndRejected)
5414 // When GNU attributes are expressly forbidden, diagnose their usage.
5415 Diag(Tok, diag::err_attributes_not_allowed);
5416
5417 // Parse the attributes even if they are rejected to ensure that error
5418 // recovery is graceful.
5419 if (AttrReqs & AR_GNUAttributesParsed ||
5420 AttrReqs & AR_GNUAttributesParsedAndRejected) {
John McCall53fa7142010-12-24 02:08:15 +00005421 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00005422 continue; // do *not* consume the next token!
5423 }
5424 // otherwise, FALL THROUGH!
Galina Kistanova77674252017-06-01 21:15:34 +00005425 LLVM_FALLTHROUGH;
Chris Lattnercf0bab22008-12-18 07:02:59 +00005426 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00005427 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00005428 // If this is not a type-qualifier token, we're done reading type
5429 // qualifiers. First verify that DeclSpec's are consistent.
Craig Topper25122412015-11-15 03:32:11 +00005430 DS.Finish(Actions, Actions.getASTContext().getPrintingPolicy());
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00005431 if (EndLoc.isValid())
5432 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005433 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005434 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005435
Chris Lattnerd9c3c592006-08-05 06:26:47 +00005436 // If the specifier combination wasn't legal, issue a diagnostic.
5437 if (isInvalid) {
5438 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00005439 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00005440 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00005441 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005442 }
5443}
5444
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00005445/// ParseDeclarator - Parse and verify a newly-initialized declarator.
5446///
5447void Parser::ParseDeclarator(Declarator &D) {
5448 /// This implements the 'declarator' production in the C grammar, then checks
5449 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00005450 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00005451}
5452
Richard Smith0b350b92014-10-28 16:55:02 +00005453static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang,
Faisal Vali421b2d12017-12-29 05:41:00 +00005454 DeclaratorContext TheContext) {
Richard Smith0efa75c2012-03-29 01:16:42 +00005455 if (Kind == tok::star || Kind == tok::caret)
5456 return true;
5457
Sven van Haastregte518bb42019-05-22 13:12:20 +00005458 if (Kind == tok::kw_pipe &&
5459 ((Lang.OpenCL && Lang.OpenCLVersion >= 200) || Lang.OpenCLCPlusPlus))
Xiuli Pan9c14e282016-01-09 12:53:17 +00005460 return true;
5461
Richard Smith0efa75c2012-03-29 01:16:42 +00005462 if (!Lang.CPlusPlus)
5463 return false;
5464
Richard Smith0b350b92014-10-28 16:55:02 +00005465 if (Kind == tok::amp)
5466 return true;
5467
5468 // We parse rvalue refs in C++03, because otherwise the errors are scary.
5469 // But we must not parse them in conversion-type-ids and new-type-ids, since
5470 // those can be legitimately followed by a && operator.
5471 // (The same thing can in theory happen after a trailing-return-type, but
5472 // since those are a C++11 feature, there is no rejects-valid issue there.)
5473 if (Kind == tok::ampamp)
Faisal Vali421b2d12017-12-29 05:41:00 +00005474 return Lang.CPlusPlus11 ||
5475 (TheContext != DeclaratorContext::ConversionIdContext &&
5476 TheContext != DeclaratorContext::CXXNewContext);
Richard Smith0b350b92014-10-28 16:55:02 +00005477
5478 return false;
Richard Smith0efa75c2012-03-29 01:16:42 +00005479}
5480
Xiuli Pan9c14e282016-01-09 12:53:17 +00005481// Indicates whether the given declarator is a pipe declarator.
5482static bool isPipeDeclerator(const Declarator &D) {
5483 const unsigned NumTypes = D.getNumTypeObjects();
5484
5485 for (unsigned Idx = 0; Idx != NumTypes; ++Idx)
5486 if (DeclaratorChunk::Pipe == D.getTypeObject(Idx).Kind)
5487 return true;
5488
5489 return false;
5490}
5491
Sebastian Redlbd150f42008-11-21 19:14:01 +00005492/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
5493/// is parsed by the function passed to it. Pass null, and the direct-declarator
5494/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00005495/// ptr-operator production.
5496///
Richard Smith09f76ee2011-10-19 21:33:05 +00005497/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00005498/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
5499/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00005500///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005501/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
5502/// [C] pointer[opt] direct-declarator
5503/// [C++] direct-declarator
5504/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00005505///
5506/// pointer: [C99 6.7.5]
5507/// '*' type-qualifier-list[opt]
5508/// '*' type-qualifier-list[opt] pointer
5509///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00005510/// ptr-operator:
5511/// '*' cv-qualifier-seq[opt]
5512/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00005513/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00005514/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00005515/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005516/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00005517void Parser::ParseDeclaratorInternal(Declarator &D,
5518 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00005519 if (Diags.hasAllExtensionsSilenced())
5520 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00005521
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005522 // C++ member pointers start with a '::' or a nested-name.
5523 // Member pointers get special handling, since there's no place for the
5524 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00005525 if (getLangOpts().CPlusPlus &&
Richard Smith83c2ecf2016-02-02 23:34:49 +00005526 (Tok.is(tok::coloncolon) || Tok.is(tok::kw_decltype) ||
Serge Pavlov458ea762014-07-16 05:16:52 +00005527 (Tok.is(tok::identifier) &&
5528 (NextToken().is(tok::coloncolon) || NextToken().is(tok::less))) ||
Chris Lattner803802d2009-03-24 17:04:48 +00005529 Tok.is(tok::annot_cxxscope))) {
Faisal Vali421b2d12017-12-29 05:41:00 +00005530 bool EnteringContext =
5531 D.getContext() == DeclaratorContext::FileContext ||
5532 D.getContext() == DeclaratorContext::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005533 CXXScopeSpec SS;
David Blaikieefdccaa2016-01-15 23:43:34 +00005534 ParseOptionalCXXScopeSpecifier(SS, nullptr, EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00005535
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00005536 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00005537 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005538 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00005539 if (D.mayHaveIdentifier())
5540 D.getCXXScopeSpec() = SS;
5541 else
5542 AnnotateScopeToken(SS, true);
5543
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005544 if (DirectDeclParser)
5545 (this->*DirectDeclParser)(D);
5546 return;
5547 }
5548
5549 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005550 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00005551 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005552 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005553 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005554
5555 // Recurse to parse whatever is left.
5556 ParseDeclaratorInternal(D, DirectDeclParser);
5557
5558 // Sema will have to catch (syntactically invalid) pointers into global
5559 // scope. It has to catch pointers into namespace scope anyway.
Erich Keanec480f302018-07-12 21:09:05 +00005560 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(
Stephen Kelly1c301dc2018-08-09 21:09:38 +00005561 SS, DS.getTypeQualifiers(), DS.getEndLoc()),
Erich Keanec480f302018-07-12 21:09:05 +00005562 std::move(DS.getAttributes()),
5563 /* Don't replace range end. */ SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005564 return;
5565 }
5566 }
5567
5568 tok::TokenKind Kind = Tok.getKind();
Xiuli Pan9c14e282016-01-09 12:53:17 +00005569
5570 if (D.getDeclSpec().isTypeSpecPipe() && !isPipeDeclerator(D)) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005571 DeclSpec DS(AttrFactory);
5572 ParseTypeQualifierListOpt(DS);
Xiuli Pan9c14e282016-01-09 12:53:17 +00005573
5574 D.AddTypeInfo(
5575 DeclaratorChunk::getPipe(DS.getTypeQualifiers(), DS.getPipeLoc()),
Erich Keanec480f302018-07-12 21:09:05 +00005576 std::move(DS.getAttributes()), SourceLocation());
Xiuli Pan9c14e282016-01-09 12:53:17 +00005577 }
5578
Steve Naroffec33ed92008-08-27 16:04:49 +00005579 // Not a pointer, C++ reference, or block.
Richard Smith0b350b92014-10-28 16:55:02 +00005580 if (!isPtrOperatorToken(Kind, getLangOpts(), D.getContext())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00005581 if (DirectDeclParser)
5582 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00005583 return;
5584 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005585
Sebastian Redled0f3b02009-03-15 22:02:01 +00005586 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
5587 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00005588 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005589 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00005590
Chris Lattner9eac9312009-03-27 04:18:06 +00005591 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00005592 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00005593 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005594
Aaron Ballman08b06592014-07-22 12:44:22 +00005595 // GNU attributes are not allowed here in a new-type-id, but Declspec and
5596 // C++11 attributes are allowed.
5597 unsigned Reqs = AR_CXX11AttributesParsed | AR_DeclspecAttributesParsed |
Faisal Vali421b2d12017-12-29 05:41:00 +00005598 ((D.getContext() != DeclaratorContext::CXXNewContext)
5599 ? AR_GNUAttributesParsed
5600 : AR_GNUAttributesParsedAndRejected);
Aaron Ballman08b06592014-07-22 12:44:22 +00005601 ParseTypeQualifierListOpt(DS, Reqs, true, !D.mayOmitIdentifier());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005602 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005603
Bill Wendling3708c182007-05-27 10:15:43 +00005604 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00005605 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00005606 if (Kind == tok::star)
5607 // Remember that we parsed a pointer type, and remember the type-quals.
Erich Keanec480f302018-07-12 21:09:05 +00005608 D.AddTypeInfo(DeclaratorChunk::getPointer(
5609 DS.getTypeQualifiers(), Loc, DS.getConstSpecLoc(),
5610 DS.getVolatileSpecLoc(), DS.getRestrictSpecLoc(),
5611 DS.getAtomicSpecLoc(), DS.getUnalignedSpecLoc()),
5612 std::move(DS.getAttributes()), SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00005613 else
5614 // Remember that we parsed a Block type, and remember the type-quals.
Erich Keanec480f302018-07-12 21:09:05 +00005615 D.AddTypeInfo(
5616 DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), Loc),
5617 std::move(DS.getAttributes()), SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00005618 } else {
5619 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00005620 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00005621
Sebastian Redl3b27be62009-03-23 00:00:23 +00005622 // Complain about rvalue references in C++03, but then go on and build
5623 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00005624 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005625 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005626 diag::warn_cxx98_compat_rvalue_reference :
5627 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00005628
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005629 // GNU-style and C++11 attributes are allowed here, as is restrict.
5630 ParseTypeQualifierListOpt(DS);
5631 D.ExtendWithDeclSpec(DS);
5632
Bill Wendling93efb222007-06-02 23:28:54 +00005633 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
5634 // cv-qualifiers are introduced through the use of a typedef or of a
5635 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00005636 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
5637 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
5638 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00005639 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00005640 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
5641 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00005642 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00005643 // 'restrict' is permitted as an extension.
5644 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
5645 Diag(DS.getAtomicSpecLoc(),
5646 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00005647 }
Bill Wendling3708c182007-05-27 10:15:43 +00005648
5649 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00005650 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00005651
Douglas Gregor66583c52008-11-03 15:51:28 +00005652 if (D.getNumTypeObjects() > 0) {
5653 // C++ [dcl.ref]p4: There shall be no references to references.
5654 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
5655 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00005656 if (const IdentifierInfo *II = D.getIdentifier())
5657 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
5658 << II;
5659 else
5660 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
5661 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00005662
Sebastian Redlbd150f42008-11-21 19:14:01 +00005663 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00005664 // can go ahead and build the (technically ill-formed)
5665 // declarator: reference collapsing will take care of it.
5666 }
5667 }
5668
Richard Smith8e1ac332013-03-28 01:55:44 +00005669 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00005670 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00005671 Kind == tok::amp),
Erich Keanec480f302018-07-12 21:09:05 +00005672 std::move(DS.getAttributes()), SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00005673 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00005674}
5675
Richard Trieuf4b81d02014-06-24 23:14:24 +00005676// When correcting from misplaced brackets before the identifier, the location
5677// is saved inside the declarator so that other diagnostic messages can use
5678// them. This extracts and returns that location, or returns the provided
5679// location if a stored location does not exist.
5680static SourceLocation getMissingDeclaratorIdLoc(Declarator &D,
5681 SourceLocation Loc) {
5682 if (D.getName().StartLocation.isInvalid() &&
5683 D.getName().EndLocation.isValid())
5684 return D.getName().EndLocation;
5685
5686 return Loc;
5687}
5688
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005689/// ParseDirectDeclarator
5690/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00005691/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005692/// '(' declarator ')'
5693/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00005694/// [C90] direct-declarator '[' constant-expression[opt] ']'
5695/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5696/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5697/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5698/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005699/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5700/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005701/// direct-declarator '(' parameter-type-list ')'
5702/// direct-declarator '(' identifier-list[opt] ')'
5703/// [GNU] direct-declarator '(' parameter-forward-declarations
5704/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00005705/// [C++] direct-declarator '(' parameter-declaration-clause ')'
5706/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005707/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
5708/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
5709/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00005710/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005711/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00005712///
5713/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00005714/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00005715/// '::'[opt] nested-name-specifier[opt] type-name
5716///
5717/// id-expression: [C++ 5.1]
5718/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00005719/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00005720///
5721/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00005722/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00005723/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00005724/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00005725/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00005726/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00005727///
Richard Smithbdb84f32016-07-22 23:36:59 +00005728/// C++17 adds the following, which we also handle here:
5729///
5730/// simple-declaration:
5731/// <decl-spec> '[' identifier-list ']' brace-or-equal-initializer ';'
5732///
Richard Smith1453e312012-03-27 01:42:32 +00005733/// Note, any additional constructs added here may need corresponding changes
5734/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00005735void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00005736 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00005737
David Blaikiebbafb8a2012-03-11 07:00:24 +00005738 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Richard Smithbdb84f32016-07-22 23:36:59 +00005739 // This might be a C++17 structured binding.
5740 if (Tok.is(tok::l_square) && !D.mayOmitIdentifier() &&
5741 D.getCXXScopeSpec().isEmpty())
5742 return ParseDecompositionDeclarator(D);
5743
Serge Pavlov458ea762014-07-16 05:16:52 +00005744 // Don't parse FOO:BAR as if it were a typo for FOO::BAR inside a class, in
5745 // this context it is a bitfield. Also in range-based for statement colon
5746 // may delimit for-range-declaration.
Faisal Vali421b2d12017-12-29 05:41:00 +00005747 ColonProtectionRAIIObject X(
5748 *this, D.getContext() == DeclaratorContext::MemberContext ||
5749 (D.getContext() == DeclaratorContext::ForContext &&
5750 getLangOpts().CPlusPlus11));
Serge Pavlov458ea762014-07-16 05:16:52 +00005751
Douglas Gregor7861a802009-11-03 01:35:08 +00005752 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00005753 if (D.getCXXScopeSpec().isEmpty()) {
Faisal Vali421b2d12017-12-29 05:41:00 +00005754 bool EnteringContext =
5755 D.getContext() == DeclaratorContext::FileContext ||
5756 D.getContext() == DeclaratorContext::MemberContext;
David Blaikieefdccaa2016-01-15 23:43:34 +00005757 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), nullptr,
Douglas Gregordf593fb2011-11-07 17:33:42 +00005758 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00005759 }
5760
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00005761 if (D.getCXXScopeSpec().isValid()) {
Richard Smith64e033f2015-01-15 00:48:52 +00005762 if (Actions.ShouldEnterDeclaratorScope(getCurScope(),
5763 D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00005764 // Change the declaration context for name lookup, until this function
5765 // is exited (and the declarator has been parsed).
5766 DeclScopeObj.EnterDeclaratorScope();
Alex Lorenze151f0102016-12-07 10:24:44 +00005767 else if (getObjCDeclContext()) {
5768 // Ensure that we don't interpret the next token as an identifier when
5769 // dealing with declarations in an Objective-C container.
5770 D.SetIdentifier(nullptr, Tok.getLocation());
5771 D.setInvalidType(true);
5772 ConsumeToken();
5773 goto PastIdentifier;
5774 }
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00005775 }
5776
Douglas Gregor27b4c162010-12-23 22:44:42 +00005777 // C++0x [dcl.fct]p14:
Faisal Vali8435a692014-12-04 12:40:21 +00005778 // There is a syntactic ambiguity when an ellipsis occurs at the end of a
5779 // parameter-declaration-clause without a preceding comma. In this case,
5780 // the ellipsis is parsed as part of the abstract-declarator if the type
5781 // of the parameter either names a template parameter pack that has not
5782 // been expanded or contains auto; otherwise, it is parsed as part of the
5783 // parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00005784 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Faisal Vali421b2d12017-12-29 05:41:00 +00005785 !((D.getContext() == DeclaratorContext::PrototypeContext ||
5786 D.getContext() == DeclaratorContext::LambdaExprParameterContext ||
5787 D.getContext() == DeclaratorContext::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00005788 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00005789 !D.hasGroupingParens() &&
Faisal Vali8435a692014-12-04 12:40:21 +00005790 !Actions.containsUnexpandedParameterPacks(D) &&
Faisal Vali090da2d2018-01-01 18:23:28 +00005791 D.getDeclSpec().getTypeSpecType() != TST_auto)) {
Richard Smith0efa75c2012-03-29 01:16:42 +00005792 SourceLocation EllipsisLoc = ConsumeToken();
Richard Smith0b350b92014-10-28 16:55:02 +00005793 if (isPtrOperatorToken(Tok.getKind(), getLangOpts(), D.getContext())) {
Richard Smith0efa75c2012-03-29 01:16:42 +00005794 // The ellipsis was put in the wrong place. Recover, and explain to
5795 // the user what they should have done.
5796 ParseDeclarator(D);
Nikola Smiljanic69fdc9f2014-06-06 02:58:59 +00005797 if (EllipsisLoc.isValid())
5798 DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D);
Richard Smith0efa75c2012-03-29 01:16:42 +00005799 return;
5800 } else
5801 D.setEllipsisLoc(EllipsisLoc);
5802
5803 // The ellipsis can't be followed by a parenthesized declarator. We
5804 // check for that in ParseParenDeclarator, after we have disambiguated
5805 // the l_paren token.
5806 }
5807
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00005808 if (Tok.isOneOf(tok::identifier, tok::kw_operator, tok::annot_template_id,
5809 tok::tilde)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00005810 // We found something that indicates the start of an unqualified-id.
5811 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00005812 bool AllowConstructorName;
Richard Smith35845152017-02-07 01:37:30 +00005813 bool AllowDeductionGuide;
5814 if (D.getDeclSpec().hasTypeSpecifier()) {
John McCall84821e72010-04-13 06:39:49 +00005815 AllowConstructorName = false;
Richard Smith35845152017-02-07 01:37:30 +00005816 AllowDeductionGuide = false;
5817 } else if (D.getCXXScopeSpec().isSet()) {
John McCall84821e72010-04-13 06:39:49 +00005818 AllowConstructorName =
Faisal Vali421b2d12017-12-29 05:41:00 +00005819 (D.getContext() == DeclaratorContext::FileContext ||
5820 D.getContext() == DeclaratorContext::MemberContext);
Richard Smith35845152017-02-07 01:37:30 +00005821 AllowDeductionGuide = false;
5822 } else {
Faisal Vali421b2d12017-12-29 05:41:00 +00005823 AllowConstructorName =
5824 (D.getContext() == DeclaratorContext::MemberContext);
Fangrui Song6907ce22018-07-30 19:24:48 +00005825 AllowDeductionGuide =
Faisal Vali421b2d12017-12-29 05:41:00 +00005826 (D.getContext() == DeclaratorContext::FileContext ||
5827 D.getContext() == DeclaratorContext::MemberContext);
Richard Smith35845152017-02-07 01:37:30 +00005828 }
John McCall84821e72010-04-13 06:39:49 +00005829
Richard Smith64e033f2015-01-15 00:48:52 +00005830 bool HadScope = D.getCXXScopeSpec().isValid();
Chad Rosierc1183952012-06-26 22:30:43 +00005831 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
5832 /*EnteringContext=*/true,
David Blaikieefdccaa2016-01-15 23:43:34 +00005833 /*AllowDestructorName=*/true, AllowConstructorName,
Richard Smithc08b6932018-04-27 02:00:13 +00005834 AllowDeductionGuide, nullptr, nullptr,
Richard Smith35845152017-02-07 01:37:30 +00005835 D.getName()) ||
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00005836 // Once we're past the identifier, if the scope was bad, mark the
5837 // whole declarator bad.
5838 D.getCXXScopeSpec().isInvalid()) {
Craig Topper161e4db2014-05-21 06:02:52 +00005839 D.SetIdentifier(nullptr, Tok.getLocation());
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00005840 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00005841 } else {
Richard Smith64e033f2015-01-15 00:48:52 +00005842 // ParseUnqualifiedId might have parsed a scope specifier during error
5843 // recovery. If it did so, enter that scope.
5844 if (!HadScope && D.getCXXScopeSpec().isValid() &&
5845 Actions.ShouldEnterDeclaratorScope(getCurScope(),
5846 D.getCXXScopeSpec()))
5847 DeclScopeObj.EnterDeclaratorScope();
5848
Douglas Gregor7861a802009-11-03 01:35:08 +00005849 // Parsed the unqualified-id; update range information and move along.
5850 if (D.getSourceRange().getBegin().isInvalid())
5851 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
5852 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00005853 }
Douglas Gregor7861a802009-11-03 01:35:08 +00005854 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00005855 }
Richard Smithd63db6e2015-12-19 02:40:19 +00005856
5857 if (D.getCXXScopeSpec().isNotEmpty()) {
5858 // We have a scope specifier but no following unqualified-id.
5859 Diag(PP.getLocForEndOfToken(D.getCXXScopeSpec().getEndLoc()),
5860 diag::err_expected_unqualified_id)
5861 << /*C++*/1;
5862 D.SetIdentifier(nullptr, Tok.getLocation());
5863 goto PastIdentifier;
5864 }
Douglas Gregor7861a802009-11-03 01:35:08 +00005865 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005866 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00005867 "There's a C++-specific check for tok::identifier above");
5868 assert(Tok.getIdentifierInfo() && "Not an identifier?");
5869 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Richard Trieu2664dc12014-05-05 22:06:50 +00005870 D.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00005871 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00005872 goto PastIdentifier;
Richard Smith74639b12017-05-19 01:54:59 +00005873 } else if (Tok.is(tok::identifier) && !D.mayHaveIdentifier()) {
5874 // We're not allowed an identifier here, but we got one. Try to figure out
5875 // if the user was trying to attach a name to the type, or whether the name
5876 // is some unrelated trailing syntax.
5877 bool DiagnoseIdentifier = false;
5878 if (D.hasGroupingParens())
5879 // An identifier within parens is unlikely to be intended to be anything
5880 // other than a name being "declared".
5881 DiagnoseIdentifier = true;
Richard Smith77a9c602018-02-28 03:02:23 +00005882 else if (D.getContext() == DeclaratorContext::TemplateArgContext)
Richard Smith74639b12017-05-19 01:54:59 +00005883 // T<int N> is an accidental identifier; T<int N indicates a missing '>'.
5884 DiagnoseIdentifier =
5885 NextToken().isOneOf(tok::comma, tok::greater, tok::greatergreater);
Faisal Vali421b2d12017-12-29 05:41:00 +00005886 else if (D.getContext() == DeclaratorContext::AliasDeclContext ||
5887 D.getContext() == DeclaratorContext::AliasTemplateContext)
Richard Smith74639b12017-05-19 01:54:59 +00005888 // The most likely error is that the ';' was forgotten.
5889 DiagnoseIdentifier = NextToken().isOneOf(tok::comma, tok::semi);
Richard Smithe303e352018-02-02 22:24:54 +00005890 else if ((D.getContext() == DeclaratorContext::TrailingReturnContext ||
5891 D.getContext() == DeclaratorContext::TrailingReturnVarContext) &&
Richard Smith74639b12017-05-19 01:54:59 +00005892 !isCXX11VirtSpecifier(Tok))
5893 DiagnoseIdentifier = NextToken().isOneOf(
5894 tok::comma, tok::semi, tok::equal, tok::l_brace, tok::kw_try);
5895 if (DiagnoseIdentifier) {
Richard Smithf39720b2013-10-13 22:12:28 +00005896 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
5897 << FixItHint::CreateRemoval(Tok.getLocation());
Craig Topper161e4db2014-05-21 06:02:52 +00005898 D.SetIdentifier(nullptr, Tok.getLocation());
Richard Smithf39720b2013-10-13 22:12:28 +00005899 ConsumeToken();
5900 goto PastIdentifier;
5901 }
Douglas Gregor7861a802009-11-03 01:35:08 +00005902 }
Richard Smith0efa75c2012-03-29 01:16:42 +00005903
Douglas Gregor7861a802009-11-03 01:35:08 +00005904 if (Tok.is(tok::l_paren)) {
Richard Smithe303e352018-02-02 22:24:54 +00005905 // If this might be an abstract-declarator followed by a direct-initializer,
5906 // check whether this is a valid declarator chunk. If it can't be, assume
5907 // that it's an initializer instead.
5908 if (D.mayOmitIdentifier() && D.mayBeFollowedByCXXDirectInit()) {
5909 RevertingTentativeParsingAction PA(*this);
5910 if (TryParseDeclarator(true, D.mayHaveIdentifier(), true) ==
5911 TPResult::False) {
5912 D.SetIdentifier(nullptr, Tok.getLocation());
5913 goto PastIdentifier;
5914 }
5915 }
5916
Chris Lattneracd58a32006-08-06 17:24:14 +00005917 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00005918 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00005919 // Example: 'char (*X)' or 'int (*XX)(void)'
5920 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005921
5922 // If the declarator was parenthesized, we entered the declarator
5923 // scope when parsing the parenthesized declarator, then exited
5924 // the scope already. Re-enter the scope, if we need to.
5925 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00005926 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00005927 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00005928 if (!D.isInvalidType() &&
Nico Weber6b05f382015-02-18 04:53:03 +00005929 Actions.ShouldEnterDeclaratorScope(getCurScope(),
5930 D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005931 // Change the declaration context for name lookup, until this function
5932 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00005933 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005934 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00005935 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00005936 // This could be something simple like "int" (in which case the declarator
5937 // portion is empty), if an abstract-declarator is allowed.
Craig Topper161e4db2014-05-21 06:02:52 +00005938 D.SetIdentifier(nullptr, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00005939
5940 // The grammar for abstract-pack-declarator does not allow grouping parens.
5941 // FIXME: Revisit this once core issue 1488 is resolved.
5942 if (D.hasEllipsis() && D.hasGroupingParens())
5943 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
5944 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00005945 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00005946 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00005947 LLVM_BUILTIN_TRAP;
Richard Trieuf4b81d02014-06-24 23:14:24 +00005948 if (Tok.is(tok::l_square))
5949 return ParseMisplacedBracketDeclarator(D);
Faisal Vali421b2d12017-12-29 05:41:00 +00005950 if (D.getContext() == DeclaratorContext::MemberContext) {
Alex Lorenzf1278212017-04-11 15:01:53 +00005951 // Objective-C++: Detect C++ keywords and try to prevent further errors by
5952 // treating these keyword as valid member names.
Erik Pilkingtonfa983902018-10-30 20:31:30 +00005953 if (getLangOpts().ObjC && getLangOpts().CPlusPlus &&
Alex Lorenzf1278212017-04-11 15:01:53 +00005954 Tok.getIdentifierInfo() &&
5955 Tok.getIdentifierInfo()->isCPlusPlusKeyword(getLangOpts())) {
5956 Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
5957 diag::err_expected_member_name_or_semi_objcxx_keyword)
5958 << Tok.getIdentifierInfo()
5959 << (D.getDeclSpec().isEmpty() ? SourceRange()
5960 : D.getDeclSpec().getSourceRange());
5961 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
5962 D.SetRangeEnd(Tok.getLocation());
5963 ConsumeToken();
5964 goto PastIdentifier;
5965 }
Richard Trieuf4b81d02014-06-24 23:14:24 +00005966 Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
5967 diag::err_expected_member_name_or_semi)
Richard Trieua1342402014-05-02 23:40:32 +00005968 << (D.getDeclSpec().isEmpty() ? SourceRange()
5969 : D.getDeclSpec().getSourceRange());
Richard Trieuf4b81d02014-06-24 23:14:24 +00005970 } else if (getLangOpts().CPlusPlus) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00005971 if (Tok.isOneOf(tok::period, tok::arrow))
Richard Trieu9c672672013-01-26 02:31:38 +00005972 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
Richard Trieu2f586962013-09-05 02:31:33 +00005973 else {
5974 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
5975 if (Tok.isAtStartOfLine() && Loc.isValid())
5976 Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
5977 << getLangOpts().CPlusPlus;
5978 else
Richard Trieuf4b81d02014-06-24 23:14:24 +00005979 Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
5980 diag::err_expected_unqualified_id)
Richard Trieu2f586962013-09-05 02:31:33 +00005981 << getLangOpts().CPlusPlus;
5982 }
Richard Trieuf4b81d02014-06-24 23:14:24 +00005983 } else {
5984 Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
5985 diag::err_expected_either)
5986 << tok::identifier << tok::l_paren;
5987 }
Craig Topper161e4db2014-05-21 06:02:52 +00005988 D.SetIdentifier(nullptr, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00005989 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00005990 }
Mike Stump11289f42009-09-09 15:08:12 +00005991
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00005992 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00005993 assert(D.isPastIdentifier() &&
5994 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00005995
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005996 // Don't parse attributes unless we have parsed an unparenthesized name.
5997 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00005998 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005999
Chris Lattneracd58a32006-08-06 17:24:14 +00006000 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00006001 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00006002 // Enter function-declaration scope, limiting any declarators to the
6003 // function prototype scope, including parameter declarators.
6004 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00006005 Scope::FunctionPrototypeScope|Scope::DeclScope|
6006 (D.isFunctionDeclaratorAFunctionDeclaration()
6007 ? Scope::FunctionDeclarationScope : 0));
6008
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00006009 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
6010 // In such a case, check if we actually have a function declarator; if it
6011 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00006012 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00006013 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
6014 // The name of the declarator, if any, is tentatively declared within
6015 // a possible direct initializer.
6016 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
6017 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
6018 TentativelyDeclaredIdentifiers.pop_back();
6019 if (!IsFunctionDecl)
6020 break;
6021 }
John McCall084e83d2011-03-24 11:26:52 +00006022 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006023 BalancedDelimiterTracker T(*this, tok::l_paren);
6024 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00006025 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00006026 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00006027 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00006028 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00006029 } else {
6030 break;
6031 }
6032 }
Chad Rosierc1183952012-06-26 22:30:43 +00006033}
Chris Lattneracd58a32006-08-06 17:24:14 +00006034
Richard Smithbdb84f32016-07-22 23:36:59 +00006035void Parser::ParseDecompositionDeclarator(Declarator &D) {
6036 assert(Tok.is(tok::l_square));
6037
6038 // If this doesn't look like a structured binding, maybe it's a misplaced
6039 // array declarator.
6040 // FIXME: Consume the l_square first so we don't need extra lookahead for
6041 // this.
6042 if (!(NextToken().is(tok::identifier) &&
6043 GetLookAheadToken(2).isOneOf(tok::comma, tok::r_square)) &&
6044 !(NextToken().is(tok::r_square) &&
6045 GetLookAheadToken(2).isOneOf(tok::equal, tok::l_brace)))
6046 return ParseMisplacedBracketDeclarator(D);
6047
6048 BalancedDelimiterTracker T(*this, tok::l_square);
6049 T.consumeOpen();
6050
6051 SmallVector<DecompositionDeclarator::Binding, 32> Bindings;
6052 while (Tok.isNot(tok::r_square)) {
6053 if (!Bindings.empty()) {
6054 if (Tok.is(tok::comma))
6055 ConsumeToken();
6056 else {
6057 if (Tok.is(tok::identifier)) {
6058 SourceLocation EndLoc = getEndOfPreviousToken();
6059 Diag(EndLoc, diag::err_expected)
6060 << tok::comma << FixItHint::CreateInsertion(EndLoc, ",");
6061 } else {
6062 Diag(Tok, diag::err_expected_comma_or_rsquare);
6063 }
6064
6065 SkipUntil(tok::r_square, tok::comma, tok::identifier,
6066 StopAtSemi | StopBeforeMatch);
6067 if (Tok.is(tok::comma))
6068 ConsumeToken();
6069 else if (Tok.isNot(tok::identifier))
6070 break;
6071 }
6072 }
6073
6074 if (Tok.isNot(tok::identifier)) {
6075 Diag(Tok, diag::err_expected) << tok::identifier;
6076 break;
6077 }
6078
6079 Bindings.push_back({Tok.getIdentifierInfo(), Tok.getLocation()});
6080 ConsumeToken();
6081 }
6082
6083 if (Tok.isNot(tok::r_square))
6084 // We've already diagnosed a problem here.
6085 T.skipToEnd();
6086 else {
6087 // C++17 does not allow the identifier-list in a structured binding
6088 // to be empty.
6089 if (Bindings.empty())
6090 Diag(Tok.getLocation(), diag::ext_decomp_decl_empty);
6091
6092 T.consumeClose();
6093 }
6094
6095 return D.setDecompositionBindings(T.getOpenLocation(), Bindings,
6096 T.getCloseLocation());
6097}
6098
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006099/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
6100/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00006101/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006102/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
6103///
6104/// direct-declarator:
6105/// '(' declarator ')'
6106/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00006107/// direct-declarator '(' parameter-type-list ')'
6108/// direct-declarator '(' identifier-list[opt] ')'
6109/// [GNU] direct-declarator '(' parameter-forward-declarations
6110/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006111///
6112void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006113 BalancedDelimiterTracker T(*this, tok::l_paren);
6114 T.consumeOpen();
6115
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006116 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00006117
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00006118 // Eat any attributes before we look at whether this is a grouping or function
6119 // declarator paren. If this is a grouping paren, the attribute applies to
6120 // the type being built up, for example:
6121 // int (__attribute__(()) *x)(long y)
6122 // If this ends up not being a grouping paren, the attribute applies to the
6123 // first argument, for example:
6124 // int (__attribute__(()) int x)
6125 // In either case, we need to eat any attributes to be able to determine what
6126 // sort of paren this is.
6127 //
John McCall084e83d2011-03-24 11:26:52 +00006128 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00006129 bool RequiresArg = false;
6130 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00006131 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00006132
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00006133 // We require that the argument list (if this is a non-grouping paren) be
6134 // present even if the attribute list was empty.
6135 RequiresArg = true;
6136 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00006137
Steve Naroff44ac7772008-12-25 14:16:32 +00006138 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00006139 ParseMicrosoftTypeAttributes(attrs);
6140
Dawn Perchik335e16b2010-09-03 01:29:35 +00006141 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00006142 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00006143 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00006144
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006145 // If we haven't past the identifier yet (or where the identifier would be
6146 // stored, if this is an abstract declarator), then this is probably just
6147 // grouping parens. However, if this could be an abstract-declarator, then
6148 // this could also be the start of function arguments (consider 'void()').
6149 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00006150
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006151 if (!D.mayOmitIdentifier()) {
6152 // If this can't be an abstract-declarator, this *must* be a grouping
6153 // paren, because we haven't seen the identifier yet.
6154 isGrouping = true;
6155 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00006156 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
6157 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00006158 isDeclarationSpecifier() || // 'int(int)' is a function.
6159 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006160 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
6161 // considered to be a type, not a K&R identifier-list.
6162 isGrouping = false;
6163 } else {
6164 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
6165 isGrouping = true;
6166 }
Mike Stump11289f42009-09-09 15:08:12 +00006167
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006168 // If this is a grouping paren, handle:
6169 // direct-declarator: '(' declarator ')'
6170 // direct-declarator: '(' attributes declarator ')'
6171 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00006172 SourceLocation EllipsisLoc = D.getEllipsisLoc();
6173 D.setEllipsisLoc(SourceLocation());
6174
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00006175 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00006176 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00006177 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006178 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006179 T.consumeClose();
Erich Keanec480f302018-07-12 21:09:05 +00006180 D.AddTypeInfo(
6181 DeclaratorChunk::getParen(T.getOpenLocation(), T.getCloseLocation()),
6182 std::move(attrs), T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00006183
6184 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00006185
6186 // An ellipsis cannot be placed outside parentheses.
6187 if (EllipsisLoc.isValid())
Nikola Smiljanic69fdc9f2014-06-06 02:58:59 +00006188 DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D);
Richard Smith0efa75c2012-03-29 01:16:42 +00006189
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006190 return;
6191 }
Mike Stump11289f42009-09-09 15:08:12 +00006192
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006193 // Okay, if this wasn't a grouping paren, it must be the start of a function
6194 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00006195 // identifier (and remember where it would have been), then call into
6196 // ParseFunctionDeclarator to handle of argument list.
Craig Topper161e4db2014-05-21 06:02:52 +00006197 D.SetIdentifier(nullptr, Tok.getLocation());
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006198
David Blaikie15a430a2011-12-04 05:04:18 +00006199 // Enter function-declaration scope, limiting any declarators to the
6200 // function prototype scope, including parameter declarators.
6201 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00006202 Scope::FunctionPrototypeScope | Scope::DeclScope |
6203 (D.isFunctionDeclaratorAFunctionDeclaration()
6204 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00006205 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00006206 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006207}
6208
6209/// ParseFunctionDeclarator - We are after the identifier and have parsed the
6210/// declarator D up to a paren, which indicates that we are parsing function
6211/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00006212///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006213/// If FirstArgAttrs is non-null, then the caller parsed those arguments
6214/// immediately after the open paren - they should be considered to be the
6215/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00006216///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006217/// If RequiresArg is true, then the first argument of the function is required
6218/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00006219///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006220/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
6221/// (C++11) ref-qualifier[opt], exception-specification[opt],
6222/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
6223///
6224/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00006225/// dynamic-exception-specification
6226/// noexcept-specification
6227///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006228void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006229 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006230 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00006231 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00006232 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00006233 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00006234 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00006235 // lparen is already consumed!
6236 assert(D.isPastIdentifier() && "Should not call before identifier!");
6237
6238 // This should be true when the function has typed arguments.
6239 // Otherwise, it is treated as a K&R-style function.
6240 bool HasProto = false;
6241 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006242 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00006243 // Remember where we see an ellipsis, if any.
6244 SourceLocation EllipsisLoc;
6245
6246 DeclSpec DS(AttrFactory);
6247 bool RefQualifierIsLValueRef = true;
6248 SourceLocation RefQualifierLoc;
6249 ExceptionSpecificationType ESpecType = EST_None;
6250 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006251 SmallVector<ParsedType, 2> DynamicExceptions;
6252 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00006253 ExprResult NoexceptExpr;
Hans Wennborgdcfba332015-10-06 23:40:43 +00006254 CachedTokens *ExceptionSpecTokens = nullptr;
Aaron Ballman606093a2017-10-15 15:01:42 +00006255 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00006256 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006257
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00006258 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
6259 EndLoc is the end location for the function declarator.
6260 They differ for trailing return types. */
6261 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00006262 SourceLocation LParenLoc, RParenLoc;
6263 LParenLoc = Tracker.getOpenLocation();
6264 StartLoc = LParenLoc;
6265
Douglas Gregor9e66af42011-07-05 16:44:18 +00006266 if (isFunctionDeclaratorIdentifierList()) {
6267 if (RequiresArg)
6268 Diag(Tok, diag::err_argument_required_after_attribute);
6269
6270 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
6271
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006272 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00006273 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00006274 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00006275 EndLoc = RParenLoc;
Aaron Ballman606093a2017-10-15 15:01:42 +00006276
Fangrui Song6907ce22018-07-30 19:24:48 +00006277 // If there are attributes following the identifier list, parse them and
Aaron Ballman606093a2017-10-15 15:01:42 +00006278 // prohibit them.
6279 MaybeParseCXX11Attributes(FnAttrs);
6280 ProhibitAttributes(FnAttrs);
Douglas Gregor9e66af42011-07-05 16:44:18 +00006281 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00006282 if (Tok.isNot(tok::r_paren))
Fangrui Song6907ce22018-07-30 19:24:48 +00006283 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
Faisal Vali2b391ab2013-09-26 19:54:12 +00006284 EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00006285 else if (RequiresArg)
6286 Diag(Tok, diag::err_argument_required_after_attribute);
6287
Alexey Bader1f277942017-10-11 11:16:31 +00006288 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus
6289 || getLangOpts().OpenCL;
Douglas Gregor9e66af42011-07-05 16:44:18 +00006290
6291 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006292 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00006293 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00006294 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00006295 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00006296
David Blaikiebbafb8a2012-03-11 07:00:24 +00006297 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006298 // FIXME: Accept these components in any order, and produce fixits to
6299 // correct the order if the user gets it wrong. Ideally we should deal
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00006300 // with the pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00006301
6302 // Parse cv-qualifier-seq[opt].
Aaron Ballman08b06592014-07-22 12:44:22 +00006303 ParseTypeQualifierListOpt(DS, AR_NoAttributesParsed,
Alex Lorenz8f4d3992017-02-13 23:19:40 +00006304 /*AtomicAllowed*/ false,
6305 /*IdentifierRequired=*/false,
6306 llvm::function_ref<void()>([&]() {
6307 Actions.CodeCompleteFunctionQualifiers(DS, D);
6308 }));
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006309 if (!DS.getSourceRange().getEnd().isInvalid()) {
6310 EndLoc = DS.getSourceRange().getEnd();
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006311 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00006312
6313 // Parse ref-qualifier[opt].
Ehsan Akhgaric07d1e22015-03-25 00:53:33 +00006314 if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc))
Douglas Gregor9e66af42011-07-05 16:44:18 +00006315 EndLoc = RefQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00006316
Douglas Gregor3024f072012-04-16 07:05:22 +00006317 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00006318 // If a declaration declares a member function or member function
6319 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00006320 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00006321 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00006322 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00006323 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00006324 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00006325 getLangOpts().CPlusPlus11 &&
Richard Smith990a6922014-01-17 21:01:18 +00006326 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Faisal Vali421b2d12017-12-29 05:41:00 +00006327 (D.getContext() == DeclaratorContext::MemberContext
Richard Smithad1bbb92013-03-15 00:41:52 +00006328 ? !D.getDeclSpec().isFriendSpecified()
Faisal Vali421b2d12017-12-29 05:41:00 +00006329 : D.getContext() == DeclaratorContext::FileContext &&
Richard Smithad1bbb92013-03-15 00:41:52 +00006330 D.getCXXScopeSpec().isValid() &&
6331 Actions.CurContext->isRecord());
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00006332
6333 Qualifiers Q = Qualifiers::fromCVRUMask(DS.getTypeQualifiers());
Gauthier Harnisch796ed032019-06-14 08:56:20 +00006334 if (D.getDeclSpec().hasConstexprSpecifier() && !getLangOpts().CPlusPlus14)
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00006335 Q.addConst();
Anastasia Stulova5cffa452019-01-21 16:01:38 +00006336 // FIXME: Collect C++ address spaces.
6337 // If there are multiple different address spaces, the source is invalid.
6338 // Carry on using the first addr space for the qualifiers of 'this'.
6339 // The diagnostic will be given later while creating the function
6340 // prototype for the method.
6341 if (getLangOpts().OpenCLCPlusPlus) {
6342 for (ParsedAttr &attr : DS.getAttributes()) {
6343 LangAS ASIdx = attr.asOpenCLLangAS();
6344 if (ASIdx != LangAS::Default) {
6345 Q.addAddressSpace(ASIdx);
6346 break;
6347 }
6348 }
6349 }
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00006350
6351 Sema::CXXThisScopeRAII ThisScope(
6352 Actions, dyn_cast<CXXRecordDecl>(Actions.CurContext), Q,
6353 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00006354
Douglas Gregor9e66af42011-07-05 16:44:18 +00006355 // Parse exception-specification[opt].
Richard Smith0b3a4622014-11-13 20:01:57 +00006356 bool Delayed = D.isFirstDeclarationOfMember() &&
Richard Smith3ef3e892014-11-20 22:32:11 +00006357 D.isFunctionDeclaratorAFunctionDeclaration();
6358 if (Delayed && Actions.isLibstdcxxEagerExceptionSpecHack(D) &&
6359 GetLookAheadToken(0).is(tok::kw_noexcept) &&
6360 GetLookAheadToken(1).is(tok::l_paren) &&
6361 GetLookAheadToken(2).is(tok::kw_noexcept) &&
6362 GetLookAheadToken(3).is(tok::l_paren) &&
6363 GetLookAheadToken(4).is(tok::identifier) &&
6364 GetLookAheadToken(4).getIdentifierInfo()->isStr("swap")) {
6365 // HACK: We've got an exception-specification
6366 // noexcept(noexcept(swap(...)))
6367 // or
6368 // noexcept(noexcept(swap(...)) && noexcept(swap(...)))
6369 // on a 'swap' member function. This is a libstdc++ bug; the lookup
6370 // for 'swap' will only find the function we're currently declaring,
6371 // whereas it expects to find a non-member swap through ADL. Turn off
6372 // delayed parsing to give it a chance to find what it expects.
6373 Delayed = false;
6374 }
Richard Smith0b3a4622014-11-13 20:01:57 +00006375 ESpecType = tryParseExceptionSpecification(Delayed,
6376 ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00006377 DynamicExceptions,
6378 DynamicExceptionRanges,
Richard Smith0b3a4622014-11-13 20:01:57 +00006379 NoexceptExpr,
6380 ExceptionSpecTokens);
Douglas Gregor9e66af42011-07-05 16:44:18 +00006381 if (ESpecType != EST_None)
6382 EndLoc = ESpecRange.getEnd();
6383
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006384 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
6385 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00006386 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006387
Douglas Gregor9e66af42011-07-05 16:44:18 +00006388 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00006389 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00006390 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00006391 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Faisal Vali090da2d2018-01-01 18:23:28 +00006392 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00006393 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00006394 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00006395 SourceRange Range;
Richard Smithe303e352018-02-02 22:24:54 +00006396 TrailingReturnType =
6397 ParseTrailingReturnType(Range, D.mayBeFollowedByCXXDirectInit());
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00006398 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00006399 }
Aaron Ballman606093a2017-10-15 15:01:42 +00006400 } else if (standardAttributesAllowed()) {
6401 MaybeParseCXX11Attributes(FnAttrs);
Douglas Gregor9e66af42011-07-05 16:44:18 +00006402 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00006403 }
6404
Reid Kleckner078aea92016-12-09 17:14:05 +00006405 // Collect non-parameter declarations from the prototype if this is a function
6406 // declaration. They will be moved into the scope of the function. Only do
6407 // this in C and not C++, where the decls will continue to live in the
6408 // surrounding context.
6409 SmallVector<NamedDecl *, 0> DeclsInPrototype;
6410 if (getCurScope()->getFlags() & Scope::FunctionDeclarationScope &&
6411 !getLangOpts().CPlusPlus) {
6412 for (Decl *D : getCurScope()->decls()) {
6413 NamedDecl *ND = dyn_cast<NamedDecl>(D);
6414 if (!ND || isa<ParmVarDecl>(ND))
6415 continue;
6416 DeclsInPrototype.push_back(ND);
6417 }
6418 }
6419
Douglas Gregor9e66af42011-07-05 16:44:18 +00006420 // Remember that we parsed a function type, and remember the attributes.
Erich Keanec480f302018-07-12 21:09:05 +00006421 D.AddTypeInfo(DeclaratorChunk::getFunction(
6422 HasProto, IsAmbiguous, LParenLoc, ParamInfo.data(),
6423 ParamInfo.size(), EllipsisLoc, RParenLoc,
Anastasia Stulovaa9bc4bd2019-01-09 11:25:09 +00006424 RefQualifierIsLValueRef, RefQualifierLoc,
6425 /*MutableLoc=*/SourceLocation(),
6426 ESpecType, ESpecRange, DynamicExceptions.data(),
6427 DynamicExceptionRanges.data(), DynamicExceptions.size(),
Erich Keanec480f302018-07-12 21:09:05 +00006428 NoexceptExpr.isUsable() ? NoexceptExpr.get() : nullptr,
6429 ExceptionSpecTokens, DeclsInPrototype, StartLoc,
Anastasia Stulovaa9bc4bd2019-01-09 11:25:09 +00006430 LocalEndLoc, D, TrailingReturnType, &DS),
Erich Keanec480f302018-07-12 21:09:05 +00006431 std::move(FnAttrs), EndLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00006432}
6433
Ehsan Akhgaric07d1e22015-03-25 00:53:33 +00006434/// ParseRefQualifier - Parses a member function ref-qualifier. Returns
6435/// true if a ref-qualifier is found.
6436bool Parser::ParseRefQualifier(bool &RefQualifierIsLValueRef,
6437 SourceLocation &RefQualifierLoc) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00006438 if (Tok.isOneOf(tok::amp, tok::ampamp)) {
Ehsan Akhgaric07d1e22015-03-25 00:53:33 +00006439 Diag(Tok, getLangOpts().CPlusPlus11 ?
6440 diag::warn_cxx98_compat_ref_qualifier :
6441 diag::ext_ref_qualifier);
6442
6443 RefQualifierIsLValueRef = Tok.is(tok::amp);
6444 RefQualifierLoc = ConsumeToken();
6445 return true;
6446 }
6447 return false;
6448}
6449
Douglas Gregor9e66af42011-07-05 16:44:18 +00006450/// isFunctionDeclaratorIdentifierList - This parameter list may have an
6451/// identifier list form for a K&R-style function: void foo(a,b,c)
6452///
6453/// Note that identifier-lists are only allowed for normal declarators, not for
6454/// abstract-declarators.
6455bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00006456 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00006457 && Tok.is(tok::identifier)
6458 && !TryAltiVecVectorToken()
6459 // K&R identifier lists can't have typedefs as identifiers, per C99
6460 // 6.7.5.3p11.
6461 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
6462 // Identifier lists follow a really simple grammar: the identifiers can
6463 // be followed *only* by a ", identifier" or ")". However, K&R
6464 // identifier lists are really rare in the brave new modern world, and
6465 // it is very common for someone to typo a type in a non-K&R style
6466 // list. If we are presented with something like: "void foo(intptr x,
6467 // float y)", we don't want to start parsing the function declarator as
6468 // though it is a K&R style declarator just because intptr is an
6469 // invalid type.
6470 //
6471 // To handle this, we check to see if the token after the first
6472 // identifier is a "," or ")". Only then do we parse it as an
6473 // identifier list.
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +00006474 && (!Tok.is(tok::eof) &&
6475 (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)));
Douglas Gregor9e66af42011-07-05 16:44:18 +00006476}
6477
6478/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
6479/// we found a K&R-style identifier list instead of a typed parameter list.
6480///
6481/// After returning, ParamInfo will hold the parsed parameters.
6482///
6483/// identifier-list: [C99 6.7.5]
6484/// identifier
6485/// identifier-list ',' identifier
6486///
6487void Parser::ParseFunctionDeclaratorIdentifierList(
6488 Declarator &D,
Craig Topper5603df42013-07-05 19:34:19 +00006489 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00006490 // If there was no identifier specified for the declarator, either we are in
6491 // an abstract-declarator, or we are in a parameter declarator which was found
6492 // to be abstract. In abstract-declarators, identifier lists are not valid:
6493 // diagnose this.
6494 if (!D.getIdentifier())
6495 Diag(Tok, diag::ext_ident_list_in_param);
6496
6497 // Maintain an efficient lookup of params we have seen so far.
6498 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
6499
Alp Tokera3ebe6e2013-12-17 14:12:37 +00006500 do {
Douglas Gregor9e66af42011-07-05 16:44:18 +00006501 // If this isn't an identifier, report the error and skip until ')'.
6502 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00006503 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00006504 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor9e66af42011-07-05 16:44:18 +00006505 // Forget we parsed anything.
6506 ParamInfo.clear();
6507 return;
6508 }
6509
6510 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
6511
6512 // Reject 'typedef int y; int test(x, y)', but continue parsing.
6513 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
6514 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
6515
6516 // Verify that the argument identifier has not already been mentioned.
David Blaikie82e95a32014-11-19 07:49:47 +00006517 if (!ParamsSoFar.insert(ParmII).second) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00006518 Diag(Tok, diag::err_param_redefinition) << ParmII;
6519 } else {
6520 // Remember this identifier in ParamInfo.
6521 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
6522 Tok.getLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00006523 nullptr));
Douglas Gregor9e66af42011-07-05 16:44:18 +00006524 }
6525
6526 // Eat the identifier.
6527 ConsumeToken();
Douglas Gregor9e66af42011-07-05 16:44:18 +00006528 // The list continues if we see a comma.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00006529 } while (TryConsumeToken(tok::comma));
Douglas Gregor9e66af42011-07-05 16:44:18 +00006530}
6531
6532/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
6533/// after the opening parenthesis. This function will not parse a K&R-style
6534/// identifier list.
6535///
Richard Smith2620cd92012-04-11 04:01:28 +00006536/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
6537/// caller parsed those arguments immediately after the open paren - they should
6538/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00006539///
6540/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
6541/// be the location of the ellipsis, if any was parsed.
6542///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00006543/// parameter-type-list: [C99 6.7.5]
6544/// parameter-list
6545/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00006546/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00006547///
6548/// parameter-list: [C99 6.7.5]
6549/// parameter-declaration
6550/// parameter-list ',' parameter-declaration
6551///
6552/// parameter-declaration: [C99 6.7.5]
6553/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00006554/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00006555/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00006556/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00006557/// declaration-specifiers abstract-declarator[opt]
6558/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00006559/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00006560/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00006561/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00006562///
Douglas Gregor9e66af42011-07-05 16:44:18 +00006563void Parser::ParseParameterDeclarationClause(
6564 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00006565 ParsedAttributes &FirstArgAttrs,
Craig Topper5603df42013-07-05 19:34:19 +00006566 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00006567 SourceLocation &EllipsisLoc) {
Alp Tokera3ebe6e2013-12-17 14:12:37 +00006568 do {
6569 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
6570 // before deciding this was a parameter-declaration-clause.
6571 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
Chris Lattner371ed4e2008-04-06 06:57:35 +00006572 break;
Mike Stump11289f42009-09-09 15:08:12 +00006573
Chris Lattner371ed4e2008-04-06 06:57:35 +00006574 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00006575 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00006576 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006577
Richard Smith2620cd92012-04-11 04:01:28 +00006578 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00006579 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00006580
John McCall53fa7142010-12-24 02:08:15 +00006581 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00006582 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00006583
6584 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00006585
6586 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00006587 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00006588 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00006589 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
6590 // too much hassle.
6591 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00006592
Faisal Valia534f072018-04-26 00:42:40 +00006593 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00006594
Faisal Vali2b391ab2013-09-26 19:54:12 +00006595
Fangrui Song6907ce22018-07-30 19:24:48 +00006596 // Parse the declarator. This is "PrototypeContext" or
6597 // "LambdaExprParameterContext", because we must accept either
Faisal Vali2b391ab2013-09-26 19:54:12 +00006598 // 'declarator' or 'abstract-declarator' here.
Faisal Vali421b2d12017-12-29 05:41:00 +00006599 Declarator ParmDeclarator(
6600 DS, D.getContext() == DeclaratorContext::LambdaExprContext
6601 ? DeclaratorContext::LambdaExprParameterContext
6602 : DeclaratorContext::PrototypeContext);
Faisal Vali2b391ab2013-09-26 19:54:12 +00006603 ParseDeclarator(ParmDeclarator);
Chris Lattner371ed4e2008-04-06 06:57:35 +00006604
6605 // Parse GNU attributes, if present.
Faisal Vali2b391ab2013-09-26 19:54:12 +00006606 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump11289f42009-09-09 15:08:12 +00006607
Chris Lattner371ed4e2008-04-06 06:57:35 +00006608 // Remember this parsed parameter in ParamInfo.
Faisal Vali2b391ab2013-09-26 19:54:12 +00006609 IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00006610
Douglas Gregor4d87df52008-12-16 21:30:33 +00006611 // DefArgToks is used when the parsing of default arguments needs
6612 // to be delayed.
Malcolm Parsonsff0382c2016-11-17 17:52:58 +00006613 std::unique_ptr<CachedTokens> DefArgToks;
Douglas Gregor4d87df52008-12-16 21:30:33 +00006614
Chris Lattner371ed4e2008-04-06 06:57:35 +00006615 // If no parameter was specified, verify that *something* was specified,
6616 // otherwise we have a missing type and identifier.
Craig Topper161e4db2014-05-21 06:02:52 +00006617 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == nullptr &&
Faisal Vali2b391ab2013-09-26 19:54:12 +00006618 ParmDeclarator.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00006619 // Completely missing, emit error.
6620 Diag(DSStart, diag::err_missing_param);
6621 } else {
6622 // Otherwise, we have something. Add it and let semantic analysis try
6623 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00006624
Richard Smith36ee9fb2014-08-11 23:30:23 +00006625 // Last chance to recover from a misplaced ellipsis in an attempted
6626 // parameter pack declaration.
6627 if (Tok.is(tok::ellipsis) &&
6628 (NextToken().isNot(tok::r_paren) ||
6629 (!ParmDeclarator.getEllipsisLoc().isValid() &&
6630 !Actions.isUnexpandedParameterPackPermitted())) &&
6631 Actions.containsUnexpandedParameterPacks(ParmDeclarator))
6632 DiagnoseMisplacedEllipsisInDeclarator(ConsumeToken(), ParmDeclarator);
6633
Chris Lattner371ed4e2008-04-06 06:57:35 +00006634 // Inform the actions module about the parameter declarator, so it gets
6635 // added to the current scope.
Richard Smith95e1fb02014-08-27 03:23:12 +00006636 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00006637 // Parse the default argument, if any. We parse the default
6638 // arguments in all dialects; the semantic analysis in
6639 // ActOnParamDefaultArgument will reject the default argument in
6640 // C.
6641 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00006642 SourceLocation EqualLoc = Tok.getLocation();
6643
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00006644 // Parse the default argument
Faisal Vali421b2d12017-12-29 05:41:00 +00006645 if (D.getContext() == DeclaratorContext::MemberContext) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00006646 // If we're inside a class definition, cache the tokens
6647 // corresponding to the default argument. We'll actually parse
6648 // them when we see the end of the class definition.
Malcolm Parsonsff0382c2016-11-17 17:52:58 +00006649 DefArgToks.reset(new CachedTokens);
Douglas Gregor4d87df52008-12-16 21:30:33 +00006650
David Majnemer906ed272015-01-13 05:28:24 +00006651 SourceLocation ArgStartLoc = NextToken().getLocation();
Richard Smith1fff95c2013-09-12 23:28:08 +00006652 if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
Malcolm Parsonsff0382c2016-11-17 17:52:58 +00006653 DefArgToks.reset();
Serge Pavlovb4b35782014-07-22 01:54:49 +00006654 Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00006655 } else {
Mike Stump11289f42009-09-09 15:08:12 +00006656 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
David Majnemer906ed272015-01-13 05:28:24 +00006657 ArgStartLoc);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00006658 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00006659 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00006660 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00006661 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00006662
Chad Rosierc1183952012-06-26 22:30:43 +00006663 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00006664 // used.
Faisal Valid143a0c2017-04-01 21:30:49 +00006665 EnterExpressionEvaluationContext Eval(
6666 Actions,
6667 Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed,
6668 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00006669
Sebastian Redldb63af22012-03-14 15:54:00 +00006670 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00006671 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00006672 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00006673 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00006674 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00006675 DefArgResult = ParseAssignmentExpression();
Kaelyn Takatab16e6322014-11-20 22:06:40 +00006676 DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult);
Douglas Gregor4d87df52008-12-16 21:30:33 +00006677 if (DefArgResult.isInvalid()) {
Serge Pavlovb4b35782014-07-22 01:54:49 +00006678 Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
Alexey Bataevee6507d2013-11-18 08:17:37 +00006679 SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor4d87df52008-12-16 21:30:33 +00006680 } else {
6681 // Inform the actions module about the default argument
6682 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006683 DefArgResult.get());
Douglas Gregor4d87df52008-12-16 21:30:33 +00006684 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00006685 }
6686 }
Mike Stump11289f42009-09-09 15:08:12 +00006687
6688 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Fangrui Song6907ce22018-07-30 19:24:48 +00006689 ParmDeclarator.getIdentifierLoc(),
Malcolm Parsonsff0382c2016-11-17 17:52:58 +00006690 Param, std::move(DefArgToks)));
Chris Lattner371ed4e2008-04-06 06:57:35 +00006691 }
6692
Richard Smith36ee9fb2014-08-11 23:30:23 +00006693 if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) {
6694 if (!getLangOpts().CPlusPlus) {
6695 // We have ellipsis without a preceding ',', which is ill-formed
6696 // in C. Complain and provide the fix.
6697 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
6698 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
6699 } else if (ParmDeclarator.getEllipsisLoc().isValid() ||
6700 Actions.containsUnexpandedParameterPacks(ParmDeclarator)) {
6701 // It looks like this was supposed to be a parameter pack. Warn and
6702 // point out where the ellipsis should have gone.
6703 SourceLocation ParmEllipsis = ParmDeclarator.getEllipsisLoc();
6704 Diag(EllipsisLoc, diag::warn_misplaced_ellipsis_vararg)
6705 << ParmEllipsis.isValid() << ParmEllipsis;
6706 if (ParmEllipsis.isValid()) {
6707 Diag(ParmEllipsis,
6708 diag::note_misplaced_ellipsis_vararg_existing_ellipsis);
6709 } else {
6710 Diag(ParmDeclarator.getIdentifierLoc(),
6711 diag::note_misplaced_ellipsis_vararg_add_ellipsis)
6712 << FixItHint::CreateInsertion(ParmDeclarator.getIdentifierLoc(),
6713 "...")
6714 << !ParmDeclarator.hasName();
6715 }
6716 Diag(EllipsisLoc, diag::note_misplaced_ellipsis_vararg_add_comma)
Alp Tokera3ebe6e2013-12-17 14:12:37 +00006717 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Richard Smith36ee9fb2014-08-11 23:30:23 +00006718 }
6719
6720 // We can't have any more parameters after an ellipsis.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00006721 break;
6722 }
Mike Stump11289f42009-09-09 15:08:12 +00006723
Alp Tokera3ebe6e2013-12-17 14:12:37 +00006724 // If the next token is a comma, consume it and keep reading arguments.
6725 } while (TryConsumeToken(tok::comma));
Chris Lattner6c940e62008-04-06 06:34:08 +00006726}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006727
Chris Lattnere8074e62006-08-06 18:30:15 +00006728/// [C90] direct-declarator '[' constant-expression[opt] ']'
6729/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
6730/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
6731/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
6732/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006733/// [C++11] direct-declarator '[' constant-expression[opt] ']'
6734/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00006735void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006736 if (CheckProhibitedCXX11Attribute())
6737 return;
6738
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006739 BalancedDelimiterTracker T(*this, tok::l_square);
6740 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00006741
Chris Lattner84a11622008-12-18 07:27:21 +00006742 // C array syntax has many features, but by-far the most common is [] and [4].
6743 // This code does a fast path to handle some of the most obvious cases.
6744 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006745 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00006746 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00006747 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00006748
Chris Lattner84a11622008-12-18 07:27:21 +00006749 // Remember that we parsed the empty array type.
Craig Topper161e4db2014-05-21 06:02:52 +00006750 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, nullptr,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006751 T.getOpenLocation(),
6752 T.getCloseLocation()),
Erich Keanec480f302018-07-12 21:09:05 +00006753 std::move(attrs), T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00006754 return;
6755 } else if (Tok.getKind() == tok::numeric_constant &&
6756 GetLookAheadToken(1).is(tok::r_square)) {
6757 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00006758 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00006759 ConsumeToken();
6760
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006761 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00006762 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00006763 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00006764
Chris Lattner84a11622008-12-18 07:27:21 +00006765 // Remember that we parsed a array type, and remember its features.
Erich Keanec480f302018-07-12 21:09:05 +00006766 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, ExprRes.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006767 T.getOpenLocation(),
6768 T.getCloseLocation()),
Erich Keanec480f302018-07-12 21:09:05 +00006769 std::move(attrs), T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00006770 return;
Benjamin Kramer72dae622016-02-18 15:30:24 +00006771 } else if (Tok.getKind() == tok::code_completion) {
6772 Actions.CodeCompleteBracketDeclarator(getCurScope());
6773 return cutOffParsing();
Chris Lattner84a11622008-12-18 07:27:21 +00006774 }
Mike Stump11289f42009-09-09 15:08:12 +00006775
Chris Lattnere8074e62006-08-06 18:30:15 +00006776 // If valid, this location is the position where we read the 'static' keyword.
6777 SourceLocation StaticLoc;
Alp Tokera3ebe6e2013-12-17 14:12:37 +00006778 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00006779
Chris Lattnere8074e62006-08-06 18:30:15 +00006780 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00006781 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00006782 DeclSpec DS(AttrFactory);
Aaron Ballman08b06592014-07-22 12:44:22 +00006783 ParseTypeQualifierListOpt(DS, AR_CXX11AttributesParsed);
Mike Stump11289f42009-09-09 15:08:12 +00006784
Chris Lattnere8074e62006-08-06 18:30:15 +00006785 // If we haven't already read 'static', check to see if there is one after the
6786 // type-qualifier-list.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00006787 if (!StaticLoc.isValid())
6788 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00006789
Chris Lattnere8074e62006-08-06 18:30:15 +00006790 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00006791 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00006792 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00006793
Chris Lattner521ff2b2008-04-06 05:26:30 +00006794 // Handle the case where we have '[*]' as the array size. However, a leading
6795 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00006796 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00006797 // infrequent, use of lookahead is not costly here.
6798 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00006799 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00006800
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00006801 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00006802 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00006803 StaticLoc = SourceLocation(); // Drop the static.
6804 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00006805 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00006806 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00006807 // Note, in C89, this production uses the constant-expr production instead
6808 // of assignment-expr. The only difference is that assignment-expr allows
6809 // things like '=' and '*='. Sema rejects these in C89 mode because they
6810 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00006811
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00006812 // Parse the constant-expression or assignment-expression now (depending
6813 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00006814 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00006815 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00006816 } else {
Faisal Valid143a0c2017-04-01 21:30:49 +00006817 EnterExpressionEvaluationContext Unevaluated(
6818 Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
Kaelyn Takata15867822014-11-21 18:48:04 +00006819 NumElements =
6820 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Eli Friedmane0afc982012-01-21 01:01:51 +00006821 }
David Majnemerf9834d52014-08-08 07:21:18 +00006822 } else {
6823 if (StaticLoc.isValid()) {
6824 Diag(StaticLoc, diag::err_unspecified_size_with_static);
6825 StaticLoc = SourceLocation(); // Drop the static.
6826 }
Chris Lattner62591722006-08-12 18:40:58 +00006827 }
Mike Stump11289f42009-09-09 15:08:12 +00006828
Chris Lattner62591722006-08-12 18:40:58 +00006829 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00006830 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00006831 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00006832 // If the expression was invalid, skip it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00006833 SkipUntil(tok::r_square, StopAtSemi);
Chris Lattner62591722006-08-12 18:40:58 +00006834 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00006835 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00006836
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006837 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00006838
Jordan Rose303e2f12016-11-10 23:28:17 +00006839 MaybeParseCXX11Attributes(DS.getAttributes());
Alexis Hunt96d5c762009-11-21 08:43:09 +00006840
Chris Lattner84a11622008-12-18 07:27:21 +00006841 // Remember that we parsed a array type, and remember its features.
Erich Keanec480f302018-07-12 21:09:05 +00006842 D.AddTypeInfo(
6843 DeclaratorChunk::getArray(DS.getTypeQualifiers(), StaticLoc.isValid(),
6844 isStar, NumElements.get(), T.getOpenLocation(),
6845 T.getCloseLocation()),
6846 std::move(DS.getAttributes()), T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00006847}
6848
Richard Trieuf4b81d02014-06-24 23:14:24 +00006849/// Diagnose brackets before an identifier.
6850void Parser::ParseMisplacedBracketDeclarator(Declarator &D) {
6851 assert(Tok.is(tok::l_square) && "Missing opening bracket");
6852 assert(!D.mayOmitIdentifier() && "Declarator cannot omit identifier");
6853
6854 SourceLocation StartBracketLoc = Tok.getLocation();
6855 Declarator TempDeclarator(D.getDeclSpec(), D.getContext());
6856
6857 while (Tok.is(tok::l_square)) {
6858 ParseBracketDeclarator(TempDeclarator);
6859 }
6860
6861 // Stuff the location of the start of the brackets into the Declarator.
6862 // The diagnostics from ParseDirectDeclarator will make more sense if
6863 // they use this location instead.
6864 if (Tok.is(tok::semi))
6865 D.getName().EndLocation = StartBracketLoc;
6866
6867 SourceLocation SuggestParenLoc = Tok.getLocation();
6868
6869 // Now that the brackets are removed, try parsing the declarator again.
6870 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
6871
6872 // Something went wrong parsing the brackets, in which case,
6873 // ParseBracketDeclarator has emitted an error, and we don't need to emit
6874 // one here.
6875 if (TempDeclarator.getNumTypeObjects() == 0)
6876 return;
6877
6878 // Determine if parens will need to be suggested in the diagnostic.
6879 bool NeedParens = false;
6880 if (D.getNumTypeObjects() != 0) {
6881 switch (D.getTypeObject(D.getNumTypeObjects() - 1).Kind) {
6882 case DeclaratorChunk::Pointer:
6883 case DeclaratorChunk::Reference:
6884 case DeclaratorChunk::BlockPointer:
6885 case DeclaratorChunk::MemberPointer:
Xiuli Pan9c14e282016-01-09 12:53:17 +00006886 case DeclaratorChunk::Pipe:
Richard Trieuf4b81d02014-06-24 23:14:24 +00006887 NeedParens = true;
6888 break;
6889 case DeclaratorChunk::Array:
6890 case DeclaratorChunk::Function:
6891 case DeclaratorChunk::Paren:
6892 break;
6893 }
6894 }
6895
6896 if (NeedParens) {
6897 // Create a DeclaratorChunk for the inserted parens.
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006898 SourceLocation EndLoc = PP.getLocForEndOfToken(D.getEndLoc());
Erich Keanec480f302018-07-12 21:09:05 +00006899 D.AddTypeInfo(DeclaratorChunk::getParen(SuggestParenLoc, EndLoc),
Richard Trieuf4b81d02014-06-24 23:14:24 +00006900 SourceLocation());
6901 }
6902
6903 // Adding back the bracket info to the end of the Declarator.
6904 for (unsigned i = 0, e = TempDeclarator.getNumTypeObjects(); i < e; ++i) {
6905 const DeclaratorChunk &Chunk = TempDeclarator.getTypeObject(i);
Erich Keanec480f302018-07-12 21:09:05 +00006906 D.AddTypeInfo(Chunk, SourceLocation());
Richard Trieuf4b81d02014-06-24 23:14:24 +00006907 }
6908
6909 // The missing identifier would have been diagnosed in ParseDirectDeclarator.
6910 // If parentheses are required, always suggest them.
6911 if (!D.getIdentifier() && !NeedParens)
6912 return;
6913
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006914 SourceLocation EndBracketLoc = TempDeclarator.getEndLoc();
Richard Trieuf4b81d02014-06-24 23:14:24 +00006915
6916 // Generate the move bracket error message.
6917 SourceRange BracketRange(StartBracketLoc, EndBracketLoc);
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006918 SourceLocation EndLoc = PP.getLocForEndOfToken(D.getEndLoc());
Richard Trieuf4b81d02014-06-24 23:14:24 +00006919
6920 if (NeedParens) {
6921 Diag(EndLoc, diag::err_brackets_go_after_unqualified_id)
6922 << getLangOpts().CPlusPlus
6923 << FixItHint::CreateInsertion(SuggestParenLoc, "(")
6924 << FixItHint::CreateInsertion(EndLoc, ")")
6925 << FixItHint::CreateInsertionFromRange(
6926 EndLoc, CharSourceRange(BracketRange, true))
6927 << FixItHint::CreateRemoval(BracketRange);
6928 } else {
6929 Diag(EndLoc, diag::err_brackets_go_after_unqualified_id)
6930 << getLangOpts().CPlusPlus
6931 << FixItHint::CreateInsertionFromRange(
6932 EndLoc, CharSourceRange(BracketRange, true))
6933 << FixItHint::CreateRemoval(BracketRange);
6934 }
6935}
6936
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00006937/// [GNU] typeof-specifier:
6938/// typeof ( expressions )
6939/// typeof ( type-name )
6940/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00006941///
6942void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00006943 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006944 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00006945 SourceLocation StartLoc = ConsumeToken();
6946
John McCalle8595032010-01-13 20:03:27 +00006947 const bool hasParens = Tok.is(tok::l_paren);
6948
Faisal Valid143a0c2017-04-01 21:30:49 +00006949 EnterExpressionEvaluationContext Unevaluated(
6950 Actions, Sema::ExpressionEvaluationContext::Unevaluated,
6951 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00006952
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006953 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00006954 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006955 SourceRange CastRange;
Kaelyn Takata911260742014-12-19 01:28:40 +00006956 ExprResult Operand = Actions.CorrectDelayedTyposInExpr(
6957 ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, CastTy, CastRange));
John McCalle8595032010-01-13 20:03:27 +00006958 if (hasParens)
6959 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006960
6961 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00006962 // FIXME: Not accurate, the range gets one token more than it should.
6963 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006964 else
6965 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00006966
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006967 if (isCastExpr) {
6968 if (!CastTy) {
6969 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00006970 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00006971 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00006972
Craig Topper161e4db2014-05-21 06:02:52 +00006973 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +00006974 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006975 // Check for duplicate type specifiers (e.g. "int typeof(int)").
6976 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00006977 DiagID, CastTy,
6978 Actions.getASTContext().getPrintingPolicy()))
John McCall49bfce42009-08-03 20:12:06 +00006979 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006980 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00006981 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00006982
Hiroshi Inoue533777f2017-07-02 06:12:49 +00006983 // If we get here, the operand to the typeof was an expression.
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00006984 if (Operand.isInvalid()) {
6985 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00006986 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00006987 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00006988
Eli Friedmane0afc982012-01-21 01:01:51 +00006989 // We might need to transform the operand if it is potentially evaluated.
6990 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
6991 if (Operand.isInvalid()) {
6992 DS.SetTypeSpecError();
6993 return;
6994 }
6995
Craig Topper161e4db2014-05-21 06:02:52 +00006996 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +00006997 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00006998 // Check for duplicate type specifiers (e.g. "int typeof(int)").
6999 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00007000 DiagID, Operand.get(),
7001 Actions.getASTContext().getPrintingPolicy()))
John McCall49bfce42009-08-03 20:12:06 +00007002 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00007003}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007004
Benjamin Kramere56f3932011-12-23 17:00:35 +00007005/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00007006/// _Atomic ( type-name )
7007///
7008void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00007009 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
7010 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00007011
7012 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00007013 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00007014 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00007015 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00007016
7017 TypeResult Result = ParseTypeName();
7018 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00007019 SkipUntil(tok::r_paren, StopAtSemi);
Eli Friedman0dfb8892011-10-06 23:00:33 +00007020 return;
7021 }
7022
7023 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00007024 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00007025
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00007026 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00007027 return;
7028
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00007029 DS.setTypeofParensRange(T.getRange());
7030 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00007031
Craig Topper161e4db2014-05-21 06:02:52 +00007032 const char *PrevSpec = nullptr;
Eli Friedman0dfb8892011-10-06 23:00:33 +00007033 unsigned DiagID;
7034 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007035 DiagID, Result.get(),
Erik Verbruggen888d52a2014-01-15 09:15:43 +00007036 Actions.getASTContext().getPrintingPolicy()))
Eli Friedman0dfb8892011-10-06 23:00:33 +00007037 Diag(StartLoc, DiagID) << PrevSpec;
7038}
7039
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007040/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
7041/// from TryAltiVecVectorToken.
7042bool Parser::TryAltiVecVectorTokenOutOfLine() {
7043 Token Next = NextToken();
7044 switch (Next.getKind()) {
7045 default: return false;
7046 case tok::kw_short:
7047 case tok::kw_long:
7048 case tok::kw_signed:
7049 case tok::kw_unsigned:
7050 case tok::kw_void:
7051 case tok::kw_char:
7052 case tok::kw_int:
7053 case tok::kw_float:
7054 case tok::kw_double:
7055 case tok::kw_bool:
Bill Seurercf2c96b2015-01-12 19:35:51 +00007056 case tok::kw___bool:
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007057 case tok::kw___pixel:
7058 Tok.setKind(tok::kw___vector);
7059 return true;
7060 case tok::identifier:
7061 if (Next.getIdentifierInfo() == Ident_pixel) {
7062 Tok.setKind(tok::kw___vector);
7063 return true;
7064 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00007065 if (Next.getIdentifierInfo() == Ident_bool) {
7066 Tok.setKind(tok::kw___vector);
7067 return true;
7068 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007069 return false;
7070 }
7071}
7072
7073bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
7074 const char *&PrevSpec, unsigned &DiagID,
7075 bool &isInvalid) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00007076 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007077 if (Tok.getIdentifierInfo() == Ident_vector) {
7078 Token Next = NextToken();
7079 switch (Next.getKind()) {
7080 case tok::kw_short:
7081 case tok::kw_long:
7082 case tok::kw_signed:
7083 case tok::kw_unsigned:
7084 case tok::kw_void:
7085 case tok::kw_char:
7086 case tok::kw_int:
7087 case tok::kw_float:
7088 case tok::kw_double:
7089 case tok::kw_bool:
Bill Seurercf2c96b2015-01-12 19:35:51 +00007090 case tok::kw___bool:
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007091 case tok::kw___pixel:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00007092 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007093 return true;
7094 case tok::identifier:
7095 if (Next.getIdentifierInfo() == Ident_pixel) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00007096 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy);
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007097 return true;
7098 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00007099 if (Next.getIdentifierInfo() == Ident_bool) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00007100 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy);
Bill Schmidt99a084b2013-07-03 20:54:09 +00007101 return true;
7102 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007103 break;
7104 default:
7105 break;
7106 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00007107 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007108 DS.isTypeAltiVecVector()) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00007109 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007110 return true;
Bill Schmidt99a084b2013-07-03 20:54:09 +00007111 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
7112 DS.isTypeAltiVecVector()) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00007113 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy);
Bill Schmidt99a084b2013-07-03 20:54:09 +00007114 return true;
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007115 }
7116 return false;
7117}