blob: 3cf1f82943c7caf54262b7bfa574f9af3120a408 [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.
89bool FindLocsWithCommonFileID(Preprocessor &PP, SourceLocation StartLoc,
90 SourceLocation EndLoc) {
91 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 =
350 ParsedAttr::getKind(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 =
441 ParsedAttr::getKind(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 =
491 ParsedAttr::getKind(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 Keanec480f302018-07-12 21:09:05 +00001692 Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL.getName();
Richard Smith49cc1cc2016-08-18 21:59:42 +00001693 else {
Erich Keanec480f302018-07-12 21:09:05 +00001694 Diag(AL.getLoc(), DiagID) << AL.getName();
1695 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///
Faisal Vali421b2d12017-12-29 05:41:00 +00001744Parser::DeclGroupPtrTy Parser::ParseDeclaration(DeclaratorContext Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001745 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001746 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001747 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001748 // Must temporarily exit the objective-c container scope for
1749 // parsing c none objective-c decls.
1750 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001751
Craig Topper161e4db2014-05-21 06:02:52 +00001752 Decl *SingleDecl = nullptr;
Chris Lattnera5235172007-08-25 06:57:03 +00001753 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001754 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001755 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001756 ProhibitAttributes(attrs);
Erich Keanec480f302018-07-12 21:09:05 +00001757 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd, attrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001758 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001759 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001760 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001761 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001762 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001763 SourceLocation InlineLoc = ConsumeToken();
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00001764 return ParseNamespace(Context, DeclEnd, InlineLoc);
Sebastian Redl67667942010-08-27 23:12:46 +00001765 }
Rafael Espindola1bd906d2014-10-22 14:27:08 +00001766 return ParseSimpleDeclaration(Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001767 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001768 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001769 ProhibitAttributes(attrs);
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00001770 return ParseNamespace(Context, DeclEnd);
Douglas Gregord7c4d982008-12-30 03:27:21 +00001771 case tok::kw_using:
Richard Smith6f1daa42016-12-16 00:58:48 +00001772 return ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
1773 DeclEnd, attrs);
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001774 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001775 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001776 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001777 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001778 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001779 default:
Rafael Espindola1bd906d2014-10-22 14:27:08 +00001780 return ParseSimpleDeclaration(Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001781 }
Chad Rosierc1183952012-06-26 22:30:43 +00001782
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001783 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smith6f1daa42016-12-16 00:58:48 +00001784 // single decl, convert it now.
1785 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +00001786}
1787
1788/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1789/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001790/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1791/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001792///[C90/C++]init-declarator-list ';' [TODO]
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001793/// [OMP] threadprivate-directive
1794/// [OMP] allocate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001795///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001796/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001797/// attribute-specifier-seq[opt] type-specifier-seq declarator
1798///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001799/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001800/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001801///
1802/// If FRI is non-null, we might be parsing a for-range-declaration instead
1803/// of a simple-declaration. If we find that we are, we also parse the
1804/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001805Parser::DeclGroupPtrTy
Faisal Vali421b2d12017-12-29 05:41:00 +00001806Parser::ParseSimpleDeclaration(DeclaratorContext Context,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001807 SourceLocation &DeclEnd,
Richard Smith2386c8b2013-02-22 09:06:26 +00001808 ParsedAttributesWithRange &Attrs,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001809 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001810 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001811 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001812
Richard Smith404dfb42013-11-19 22:47:36 +00001813 DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
Faisal Valia534f072018-04-26 00:42:40 +00001814 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext);
Richard Smith404dfb42013-11-19 22:47:36 +00001815
1816 // If we had a free-standing type definition with a missing semicolon, we
1817 // may get this far before the problem becomes obvious.
1818 if (DS.hasTagDefinition() &&
1819 DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext))
David Blaikie0403cb12016-01-15 23:43:25 +00001820 return nullptr;
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001821
Chris Lattner0e894622006-08-13 19:58:17 +00001822 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1823 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001824 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001825 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001826 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001827 if (RequireSemi) ConsumeToken();
Nico Weber7b837f52016-01-28 19:25:00 +00001828 RecordDecl *AnonRecord = nullptr;
John McCall48871652010-08-21 09:40:31 +00001829 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Nico Weber7b837f52016-01-28 19:25:00 +00001830 DS, AnonRecord);
John McCall28a6aea2009-11-04 02:18:39 +00001831 DS.complete(TheDecl);
Nico Weber7b837f52016-01-28 19:25:00 +00001832 if (AnonRecord) {
1833 Decl* decls[] = {AnonRecord, TheDecl};
Richard Smith3beb7c62017-01-12 02:27:38 +00001834 return Actions.BuildDeclaratorGroup(decls);
Nico Weber7b837f52016-01-28 19:25:00 +00001835 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001836 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001837 }
Chad Rosierc1183952012-06-26 22:30:43 +00001838
Richard Smith2386c8b2013-02-22 09:06:26 +00001839 DS.takeAttributesFrom(Attrs);
Reid Klecknerd61a3112014-12-15 23:16:32 +00001840 return ParseDeclGroup(DS, Context, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001841}
Mike Stump11289f42009-09-09 15:08:12 +00001842
Richard Smith09f76ee2011-10-19 21:33:05 +00001843/// Returns true if this might be the start of a declarator, or a common typo
1844/// for a declarator.
Faisal Vali421b2d12017-12-29 05:41:00 +00001845bool Parser::MightBeDeclarator(DeclaratorContext Context) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001846 switch (Tok.getKind()) {
1847 case tok::annot_cxxscope:
1848 case tok::annot_template_id:
1849 case tok::caret:
1850 case tok::code_completion:
1851 case tok::coloncolon:
1852 case tok::ellipsis:
1853 case tok::kw___attribute:
1854 case tok::kw_operator:
1855 case tok::l_paren:
1856 case tok::star:
1857 return true;
1858
1859 case tok::amp:
1860 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001861 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001862
Richard Smithc8a79032012-01-09 22:31:44 +00001863 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Faisal Vali421b2d12017-12-29 05:41:00 +00001864 return Context == DeclaratorContext::MemberContext &&
1865 getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square);
Richard Smithc8a79032012-01-09 22:31:44 +00001866
1867 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
Faisal Vali421b2d12017-12-29 05:41:00 +00001868 return Context == DeclaratorContext::MemberContext ||
1869 getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001870
Richard Smith09f76ee2011-10-19 21:33:05 +00001871 case tok::identifier:
1872 switch (NextToken().getKind()) {
1873 case tok::code_completion:
1874 case tok::coloncolon:
1875 case tok::comma:
1876 case tok::equal:
1877 case tok::equalequal: // Might be a typo for '='.
1878 case tok::kw_alignas:
1879 case tok::kw_asm:
1880 case tok::kw___attribute:
1881 case tok::l_brace:
1882 case tok::l_paren:
1883 case tok::l_square:
1884 case tok::less:
1885 case tok::r_brace:
1886 case tok::r_paren:
1887 case tok::r_square:
1888 case tok::semi:
1889 return true;
1890
1891 case tok::colon:
1892 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001893 // and in block scope it's probably a label. Inside a class definition,
1894 // this is a bit-field.
Faisal Vali421b2d12017-12-29 05:41:00 +00001895 return Context == DeclaratorContext::MemberContext ||
1896 (getLangOpts().CPlusPlus &&
1897 Context == DeclaratorContext::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001898
1899 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001900 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001901
1902 default:
1903 return false;
1904 }
1905
1906 default:
1907 return false;
1908 }
1909}
1910
Richard Smithb8caac82012-04-11 20:59:20 +00001911/// Skip until we reach something which seems like a sensible place to pick
1912/// up parsing after a malformed declaration. This will sometimes stop sooner
1913/// than SkipUntil(tok::r_brace) would, but will never stop later.
1914void Parser::SkipMalformedDecl() {
1915 while (true) {
1916 switch (Tok.getKind()) {
1917 case tok::l_brace:
1918 // Skip until matching }, then stop. We've probably skipped over
1919 // a malformed class or function definition or similar.
1920 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001921 SkipUntil(tok::r_brace);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001922 if (Tok.isOneOf(tok::comma, tok::l_brace, tok::kw_try)) {
Richard Smithb8caac82012-04-11 20:59:20 +00001923 // This declaration isn't over yet. Keep skipping.
1924 continue;
1925 }
Alp Toker8fbec672013-12-17 23:29:36 +00001926 TryConsumeToken(tok::semi);
Richard Smithb8caac82012-04-11 20:59:20 +00001927 return;
1928
1929 case tok::l_square:
1930 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001931 SkipUntil(tok::r_square);
Richard Smithb8caac82012-04-11 20:59:20 +00001932 continue;
1933
1934 case tok::l_paren:
1935 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001936 SkipUntil(tok::r_paren);
Richard Smithb8caac82012-04-11 20:59:20 +00001937 continue;
1938
1939 case tok::r_brace:
1940 return;
1941
1942 case tok::semi:
1943 ConsumeToken();
1944 return;
1945
1946 case tok::kw_inline:
1947 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001948 // a good place to pick back up parsing, except in an Objective-C
1949 // @interface context.
1950 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1951 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001952 return;
1953 break;
1954
1955 case tok::kw_namespace:
1956 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001957 // place to pick back up parsing, except in an Objective-C
1958 // @interface context.
1959 if (Tok.isAtStartOfLine() &&
1960 (!ParsingInObjCContainer || CurParsedObjCImpl))
1961 return;
1962 break;
1963
1964 case tok::at:
1965 // @end is very much like } in Objective-C contexts.
1966 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1967 ParsingInObjCContainer)
1968 return;
1969 break;
1970
1971 case tok::minus:
1972 case tok::plus:
1973 // - and + probably start new method declarations in Objective-C contexts.
1974 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001975 return;
1976 break;
1977
1978 case tok::eof:
Richard Smith34f30512013-11-23 04:06:09 +00001979 case tok::annot_module_begin:
1980 case tok::annot_module_end:
1981 case tok::annot_module_include:
Richard Smithb8caac82012-04-11 20:59:20 +00001982 return;
1983
1984 default:
1985 break;
1986 }
1987
1988 ConsumeAnyToken();
1989 }
1990}
1991
John McCalld5a36322009-11-03 19:26:08 +00001992/// ParseDeclGroup - Having concluded that this is either a function
1993/// definition or a group of object declarations, actually parse the
1994/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001995Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
Faisal Vali421b2d12017-12-29 05:41:00 +00001996 DeclaratorContext Context,
Richard Smith02e85f32011-04-14 22:09:26 +00001997 SourceLocation *DeclEnd,
1998 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001999 // Parse the first declarator.
Faisal Vali421b2d12017-12-29 05:41:00 +00002000 ParsingDeclarator D(*this, DS, Context);
John McCalld5a36322009-11-03 19:26:08 +00002001 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00002002
John McCalld5a36322009-11-03 19:26:08 +00002003 // Bail out if the first declarator didn't seem well-formed.
2004 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00002005 SkipMalformedDecl();
David Blaikie0403cb12016-01-15 23:43:25 +00002006 return nullptr;
Chris Lattnerefb0f112009-03-29 17:18:04 +00002007 }
Mike Stump11289f42009-09-09 15:08:12 +00002008
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00002009 // Save late-parsed attributes for now; they need to be parsed in the
2010 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00002011 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
2012 LateParsedAttrList LateParsedAttrs(true);
Richard Smith99c464c2014-11-10 21:10:32 +00002013 if (D.isFunctionDeclarator()) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00002014 MaybeParseGNUAttributes(D, &LateParsedAttrs);
2015
Richard Smith99c464c2014-11-10 21:10:32 +00002016 // The _Noreturn keyword can't appear here, unlike the GNU noreturn
2017 // attribute. If we find the keyword here, tell the user to put it
2018 // at the start instead.
2019 if (Tok.is(tok::kw__Noreturn)) {
2020 SourceLocation Loc = ConsumeToken();
2021 const char *PrevSpec;
2022 unsigned DiagID;
2023
2024 // We can offer a fixit if it's valid to mark this function as _Noreturn
2025 // and we don't have any other declarators in this declaration.
2026 bool Fixit = !DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
2027 MaybeParseGNUAttributes(D, &LateParsedAttrs);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002028 Fixit &= Tok.isOneOf(tok::semi, tok::l_brace, tok::kw_try);
Richard Smith99c464c2014-11-10 21:10:32 +00002029
2030 Diag(Loc, diag::err_c11_noreturn_misplaced)
2031 << (Fixit ? FixItHint::CreateRemoval(Loc) : FixItHint())
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002032 << (Fixit ? FixItHint::CreateInsertion(D.getBeginLoc(), "_Noreturn ")
Richard Smith99c464c2014-11-10 21:10:32 +00002033 : FixItHint());
2034 }
2035 }
2036
Chris Lattnerdbb1e932010-07-11 22:24:20 +00002037 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00002038 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00002039 // Look at the next token to make sure that this isn't a function
2040 // declaration. We have to check this because __attribute__ might be the
2041 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00002042 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00002043
Reid Klecknerd61a3112014-12-15 23:16:32 +00002044 // Function definitions are only allowed at file scope and in C++ classes.
2045 // The C++ inline method definition case is handled elsewhere, so we only
2046 // need to handle the file scope definition case.
Faisal Vali421b2d12017-12-29 05:41:00 +00002047 if (Context == DeclaratorContext::FileContext) {
Douglas Gregor012efe22013-04-16 16:01:32 +00002048 if (isStartOfFunctionDefinition(D)) {
2049 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2050 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00002051
Douglas Gregor012efe22013-04-16 16:01:32 +00002052 // Recover by treating the 'typedef' as spurious.
2053 DS.ClearStorageClassSpecs();
2054 }
2055
2056 Decl *TheDecl =
2057 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
2058 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00002059 }
2060
Douglas Gregor012efe22013-04-16 16:01:32 +00002061 if (isDeclarationSpecifier()) {
Nico Weber6b05f382015-02-18 04:53:03 +00002062 // If there is an invalid declaration specifier right after the
2063 // function prototype, then we must be in a missing semicolon case
2064 // where this isn't actually a body. Just fall through into the code
2065 // that handles it as a prototype, and let the top-level code handle
2066 // the erroneous declspec where it would otherwise expect a comma or
2067 // semicolon.
Douglas Gregor012efe22013-04-16 16:01:32 +00002068 } else {
2069 Diag(Tok, diag::err_expected_fn_body);
2070 SkipUntil(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +00002071 return nullptr;
Douglas Gregor012efe22013-04-16 16:01:32 +00002072 }
John McCalld5a36322009-11-03 19:26:08 +00002073 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00002074 if (Tok.is(tok::l_brace)) {
2075 Diag(Tok, diag::err_function_definition_not_allowed);
Serge Pavlov1de51512013-12-09 05:25:47 +00002076 SkipMalformedDecl();
David Blaikie0403cb12016-01-15 23:43:25 +00002077 return nullptr;
Douglas Gregor012efe22013-04-16 16:01:32 +00002078 }
John McCalld5a36322009-11-03 19:26:08 +00002079 }
2080 }
2081
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00002082 if (ParseAsmAttributesAfterDeclarator(D))
David Blaikie0403cb12016-01-15 23:43:25 +00002083 return nullptr;
Richard Smith02e85f32011-04-14 22:09:26 +00002084
2085 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
2086 // must parse and analyze the for-range-initializer before the declaration is
2087 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00002088 //
2089 // Handle the Objective-C for-in loop variable similarly, although we
2090 // don't need to parse the container in advance.
2091 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
2092 bool IsForRangeLoop = false;
Alp Toker8fbec672013-12-17 23:29:36 +00002093 if (TryConsumeToken(tok::colon, FRI->ColonLoc)) {
Douglas Gregor2eb1c572013-04-08 20:52:24 +00002094 IsForRangeLoop = true;
Douglas Gregor2eb1c572013-04-08 20:52:24 +00002095 if (Tok.is(tok::l_brace))
2096 FRI->RangeExpr = ParseBraceInitializer();
2097 else
2098 FRI->RangeExpr = ParseExpression();
2099 }
2100
Richard Smith02e85f32011-04-14 22:09:26 +00002101 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
George Karpenkovec38cf72018-03-29 00:56:24 +00002102 if (IsForRangeLoop) {
Douglas Gregor2eb1c572013-04-08 20:52:24 +00002103 Actions.ActOnCXXForRangeDecl(ThisDecl);
George Karpenkovec38cf72018-03-29 00:56:24 +00002104 } else {
2105 // Obj-C for loop
2106 if (auto *VD = dyn_cast_or_null<VarDecl>(ThisDecl))
2107 VD->setObjCForDecl(true);
2108 }
Richard Smith02e85f32011-04-14 22:09:26 +00002109 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00002110 D.complete(ThisDecl);
Rafael Espindolaab417692013-07-09 12:05:01 +00002111 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00002112 }
2113
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002114 SmallVector<Decl *, 8> DeclsInGroup;
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00002115 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(
2116 D, ParsedTemplateInfo(), FRI);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00002117 if (LateParsedAttrs.size() > 0)
2118 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00002119 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00002120 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00002121 DeclsInGroup.push_back(FirstDecl);
2122
Faisal Vali421b2d12017-12-29 05:41:00 +00002123 bool ExpectSemi = Context != DeclaratorContext::ForContext;
Fangrui Song6907ce22018-07-30 19:24:48 +00002124
John McCalld5a36322009-11-03 19:26:08 +00002125 // If we don't have a comma, it is either the end of the list (a ';') or an
2126 // error, bail out.
Alp Toker8fbec672013-12-17 23:29:36 +00002127 SourceLocation CommaLoc;
2128 while (TryConsumeToken(tok::comma, CommaLoc)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00002129 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
2130 // This comma was followed by a line-break and something which can't be
2131 // the start of a declarator. The comma was probably a typo for a
2132 // semicolon.
2133 Diag(CommaLoc, diag::err_expected_semi_declaration)
2134 << FixItHint::CreateReplacement(CommaLoc, ";");
2135 ExpectSemi = false;
2136 break;
2137 }
John McCalld5a36322009-11-03 19:26:08 +00002138
2139 // Parse the next declarator.
2140 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00002141 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00002142
2143 // Accept attributes in an init-declarator. In the first declarator in a
2144 // declaration, these would be part of the declspec. In subsequent
2145 // declarators, they become part of the declarator itself, so that they
2146 // don't apply to declarators after *this* one. Examples:
2147 // short __attribute__((common)) var; -> declspec
2148 // short var __attribute__((common)); -> declarator
2149 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00002150 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00002151
Nico Rieckeaaae272014-12-04 23:31:08 +00002152 // MSVC parses but ignores qualifiers after the comma as an extension.
2153 if (getLangOpts().MicrosoftExt)
2154 DiagnoseAndSkipExtendedMicrosoftTypeAttributes();
2155
John McCalld5a36322009-11-03 19:26:08 +00002156 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00002157 if (!D.isInvalidType()) {
2158 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
2159 D.complete(ThisDecl);
2160 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00002161 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00002162 }
John McCalld5a36322009-11-03 19:26:08 +00002163 }
2164
2165 if (DeclEnd)
2166 *DeclEnd = Tok.getLocation();
2167
Richard Smith09f76ee2011-10-19 21:33:05 +00002168 if (ExpectSemi &&
Faisal Vali421b2d12017-12-29 05:41:00 +00002169 ExpectAndConsumeSemi(Context == DeclaratorContext::FileContext
Chris Lattner02f1b612012-04-28 16:12:17 +00002170 ? diag::err_invalid_token_after_toplevel_declarator
2171 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00002172 // Okay, there was no semicolon and one was expected. If we see a
2173 // declaration specifier, just assume it was missing and continue parsing.
2174 // Otherwise things are very confused and we skip to recover.
2175 if (!isDeclarationSpecifier()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002176 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Alp Toker8fbec672013-12-17 23:29:36 +00002177 TryConsumeToken(tok::semi);
Chris Lattner13901342010-07-11 22:42:07 +00002178 }
John McCalld5a36322009-11-03 19:26:08 +00002179 }
2180
Rafael Espindolaab417692013-07-09 12:05:01 +00002181 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +00002182}
2183
Richard Smith02e85f32011-04-14 22:09:26 +00002184/// Parse an optional simple-asm-expr and attributes, and attach them to a
2185/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00002186bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00002187 // If a simple-asm-expr is present, parse it.
2188 if (Tok.is(tok::kw_asm)) {
2189 SourceLocation Loc;
2190 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
2191 if (AsmLabel.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002192 SkipUntil(tok::semi, StopBeforeMatch);
Richard Smith02e85f32011-04-14 22:09:26 +00002193 return true;
2194 }
2195
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002196 D.setAsmLabel(AsmLabel.get());
Richard Smith02e85f32011-04-14 22:09:26 +00002197 D.SetRangeEnd(Loc);
2198 }
2199
2200 MaybeParseGNUAttributes(D);
2201 return false;
2202}
2203
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002204/// Parse 'declaration' after parsing 'declaration-specifiers
Douglas Gregor23996282009-05-12 21:31:51 +00002205/// declarator'. This method parses the remainder of the declaration
2206/// (including any attributes or initializer, among other things) and
2207/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00002208///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00002209/// init-declarator: [C99 6.7]
2210/// declarator
2211/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00002212/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
2213/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002214/// [C++] declarator initializer[opt]
2215///
2216/// [C++] initializer:
2217/// [C++] '=' initializer-clause
2218/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00002219/// [C++0x] '=' 'default' [TODO]
2220/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00002221/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00002222///
2223/// According to the standard grammar, =default and =delete are function
2224/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00002225///
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00002226Decl *Parser::ParseDeclarationAfterDeclarator(
2227 Declarator &D, const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00002228 if (ParseAsmAttributesAfterDeclarator(D))
Craig Topper161e4db2014-05-21 06:02:52 +00002229 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002230
Richard Smith02e85f32011-04-14 22:09:26 +00002231 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
2232}
Mike Stump11289f42009-09-09 15:08:12 +00002233
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00002234Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(
2235 Declarator &D, const ParsedTemplateInfo &TemplateInfo, ForRangeInit *FRI) {
Richard Smithc95d2c52017-09-22 04:25:05 +00002236 // RAII type used to track whether we're inside an initializer.
2237 struct InitializerScopeRAII {
2238 Parser &P;
2239 Declarator &D;
2240 Decl *ThisDecl;
2241
2242 InitializerScopeRAII(Parser &P, Declarator &D, Decl *ThisDecl)
2243 : P(P), D(D), ThisDecl(ThisDecl) {
2244 if (ThisDecl && P.getLangOpts().CPlusPlus) {
2245 Scope *S = nullptr;
2246 if (D.getCXXScopeSpec().isSet()) {
2247 P.EnterScope(0);
2248 S = P.getCurScope();
2249 }
2250 P.Actions.ActOnCXXEnterDeclInitializer(S, ThisDecl);
2251 }
2252 }
2253 ~InitializerScopeRAII() { pop(); }
2254 void pop() {
2255 if (ThisDecl && P.getLangOpts().CPlusPlus) {
2256 Scope *S = nullptr;
2257 if (D.getCXXScopeSpec().isSet())
2258 S = P.getCurScope();
2259 P.Actions.ActOnCXXExitDeclInitializer(S, ThisDecl);
2260 if (S)
2261 P.ExitScope();
2262 }
2263 ThisDecl = nullptr;
2264 }
2265 };
2266
Douglas Gregor23996282009-05-12 21:31:51 +00002267 // Inform the current actions module that we just parsed this declarator.
Craig Topper161e4db2014-05-21 06:02:52 +00002268 Decl *ThisDecl = nullptr;
Douglas Gregor450f00842009-09-25 18:43:00 +00002269 switch (TemplateInfo.Kind) {
2270 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002271 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00002272 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002273
Douglas Gregor450f00842009-09-25 18:43:00 +00002274 case ParsedTemplateInfo::Template:
Larisse Voufo39a1e502013-08-06 01:03:05 +00002275 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002276 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002277 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00002278 D);
Larisse Voufo833b05a2013-08-06 07:33:00 +00002279 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufo39a1e502013-08-06 01:03:05 +00002280 // Re-direct this decl to refer to the templated decl so that we can
2281 // initialize it.
2282 ThisDecl = VT->getTemplatedDecl();
2283 break;
2284 }
2285 case ParsedTemplateInfo::ExplicitInstantiation: {
2286 if (Tok.is(tok::semi)) {
2287 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
2288 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
2289 if (ThisRes.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002290 SkipUntil(tok::semi, StopBeforeMatch);
Craig Topper161e4db2014-05-21 06:02:52 +00002291 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00002292 }
2293 ThisDecl = ThisRes.get();
2294 } else {
2295 // FIXME: This check should be for a variable template instantiation only.
2296
2297 // Check that this is a valid instantiation
Faisal Vali2ab8c152017-12-30 04:15:27 +00002298 if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002299 // If the declarator-id is not a template-id, issue a diagnostic and
2300 // recover by ignoring the 'template' keyword.
2301 Diag(Tok, diag::err_template_defn_explicit_instantiation)
2302 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
2303 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
2304 } else {
2305 SourceLocation LAngleLoc =
2306 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
2307 Diag(D.getIdentifierLoc(),
2308 diag::err_explicit_instantiation_with_definition)
2309 << SourceRange(TemplateInfo.TemplateLoc)
2310 << FixItHint::CreateInsertion(LAngleLoc, "<>");
2311
2312 // Recover as if it were an explicit specialization.
2313 TemplateParameterLists FakedParamLists;
2314 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
Craig Topper96225a52015-12-24 23:58:25 +00002315 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, None,
Hubert Tongf608c052016-04-29 18:05:37 +00002316 LAngleLoc, nullptr));
Larisse Voufo39a1e502013-08-06 01:03:05 +00002317
2318 ThisDecl =
2319 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
2320 }
2321 }
Douglas Gregor450f00842009-09-25 18:43:00 +00002322 break;
2323 }
2324 }
Mike Stump11289f42009-09-09 15:08:12 +00002325
Douglas Gregor23996282009-05-12 21:31:51 +00002326 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00002327 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00002328 if (isTokenEqualOrEqualTypo()) {
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00002329 SourceLocation EqualLoc = ConsumeToken();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002330
Anders Carlsson991285e2010-09-24 21:25:25 +00002331 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002332 if (D.isFunctionDeclarator())
2333 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2334 << 1 /* delete */;
2335 else
2336 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00002337 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002338 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00002339 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2340 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002341 else
2342 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00002343 } else {
Richard Smithc95d2c52017-09-22 04:25:05 +00002344 InitializerScopeRAII InitScope(*this, D, ThisDecl);
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00002345
Douglas Gregor7aa6b222010-05-30 01:49:25 +00002346 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002347 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00002348 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002349 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002350 return nullptr;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00002351 }
Chad Rosierc1183952012-06-26 22:30:43 +00002352
Ilya Biryukov4f9543b2019-01-31 20:20:32 +00002353 PreferredType.enterVariableInit(Tok.getLocation(), ThisDecl);
2354 ExprResult Init = ParseInitializer();
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00002355
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00002356 // If this is the only decl in (possibly) range based for statement,
2357 // our best guess is that the user meant ':' instead of '='.
2358 if (Tok.is(tok::r_paren) && FRI && D.isFirstDeclarator()) {
2359 Diag(EqualLoc, diag::err_single_decl_assign_in_for_range)
2360 << FixItHint::CreateReplacement(EqualLoc, ":");
2361 // We are trying to stop parser from looking for ';' in this for
2362 // statement, therefore preventing spurious errors to be issued.
2363 FRI->ColonLoc = EqualLoc;
2364 Init = ExprError();
2365 FRI->RangeExpr = Init;
2366 }
2367
Richard Smithc95d2c52017-09-22 04:25:05 +00002368 InitScope.pop();
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00002369
Douglas Gregor23996282009-05-12 21:31:51 +00002370 if (Init.isInvalid()) {
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00002371 SmallVector<tok::TokenKind, 2> StopTokens;
2372 StopTokens.push_back(tok::comma);
Faisal Vali421b2d12017-12-29 05:41:00 +00002373 if (D.getContext() == DeclaratorContext::ForContext ||
2374 D.getContext() == DeclaratorContext::InitStmtContext)
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00002375 StopTokens.push_back(tok::r_paren);
2376 SkipUntil(StopTokens, StopAtSemi | StopBeforeMatch);
Douglas Gregor604c3022010-03-01 18:27:54 +00002377 Actions.ActOnInitializerError(ThisDecl);
2378 } else
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002379 Actions.AddInitializerToDecl(ThisDecl, Init.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00002380 /*DirectInit=*/false);
Douglas Gregor23996282009-05-12 21:31:51 +00002381 }
2382 } else if (Tok.is(tok::l_paren)) {
2383 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002384 BalancedDelimiterTracker T(*this, tok::l_paren);
2385 T.consumeOpen();
2386
Benjamin Kramerf0623432012-08-23 22:51:59 +00002387 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00002388 CommaLocsTy CommaLocs;
2389
Richard Smithc95d2c52017-09-22 04:25:05 +00002390 InitializerScopeRAII InitScope(*this, D, ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00002391
Ilya Biryukovcbea95d2017-09-08 13:36:38 +00002392 auto ThisVarDecl = dyn_cast_or_null<VarDecl>(ThisDecl);
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002393 auto RunSignatureHelp = [&]() {
Ilya Biryukov832c4af2018-09-07 14:04:39 +00002394 QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
Ilya Biryukovcbea95d2017-09-08 13:36:38 +00002395 getCurScope(), ThisVarDecl->getType()->getCanonicalTypeInternal(),
Ilya Biryukov2fab2352018-08-30 13:08:03 +00002396 ThisDecl->getLocation(), Exprs, T.getOpenLocation());
Kadir Cetinkayaa32d2532018-09-10 13:46:28 +00002397 CalledSignatureHelp = true;
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002398 return PreferredType;
Ilya Biryukovcbea95d2017-09-08 13:36:38 +00002399 };
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002400 auto SetPreferredType = [&] {
2401 PreferredType.enterFunctionArgument(Tok.getLocation(), RunSignatureHelp);
2402 };
2403
2404 llvm::function_ref<void()> ExpressionStarts;
Ilya Biryukovcbea95d2017-09-08 13:36:38 +00002405 if (ThisVarDecl) {
2406 // ParseExpressionList can sometimes succeed even when ThisDecl is not
2407 // VarDecl. This is an error and it is reported in a call to
2408 // Actions.ActOnInitializerError(). However, we call
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002409 // ProduceConstructorSignatureHelp only on VarDecls.
2410 ExpressionStarts = SetPreferredType;
Ilya Biryukovcbea95d2017-09-08 13:36:38 +00002411 }
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002412 if (ParseExpressionList(Exprs, CommaLocs, ExpressionStarts)) {
Kadir Cetinkayaa32d2532018-09-10 13:46:28 +00002413 if (ThisVarDecl && PP.isCodeCompletionReached() && !CalledSignatureHelp) {
2414 Actions.ProduceConstructorSignatureHelp(
2415 getCurScope(), ThisVarDecl->getType()->getCanonicalTypeInternal(),
2416 ThisDecl->getLocation(), Exprs, T.getOpenLocation());
2417 CalledSignatureHelp = true;
2418 }
David Blaikieeae04112012-10-10 23:15:05 +00002419 Actions.ActOnInitializerError(ThisDecl);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002420 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor23996282009-05-12 21:31:51 +00002421 } else {
2422 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002423 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00002424
2425 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
2426 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00002427
Richard Smithc95d2c52017-09-22 04:25:05 +00002428 InitScope.pop();
Douglas Gregor613bf102009-12-22 17:47:17 +00002429
Sebastian Redla9351792012-02-11 23:51:47 +00002430 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
2431 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002432 Exprs);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002433 Actions.AddInitializerToDecl(ThisDecl, Initializer.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00002434 /*DirectInit=*/true);
Douglas Gregor23996282009-05-12 21:31:51 +00002435 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002436 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00002437 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00002438 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00002439 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2440
Richard Smithc95d2c52017-09-22 04:25:05 +00002441 InitializerScopeRAII InitScope(*this, D, ThisDecl);
Sebastian Redl3da34892011-06-05 12:23:16 +00002442
2443 ExprResult Init(ParseBraceInitializer());
2444
Richard Smithc95d2c52017-09-22 04:25:05 +00002445 InitScope.pop();
Sebastian Redl3da34892011-06-05 12:23:16 +00002446
2447 if (Init.isInvalid()) {
2448 Actions.ActOnInitializerError(ThisDecl);
2449 } else
Richard Smith3beb7c62017-01-12 02:27:38 +00002450 Actions.AddInitializerToDecl(ThisDecl, Init.get(), /*DirectInit=*/true);
Sebastian Redl3da34892011-06-05 12:23:16 +00002451
Douglas Gregor23996282009-05-12 21:31:51 +00002452 } else {
Richard Smith3beb7c62017-01-12 02:27:38 +00002453 Actions.ActOnUninitializedDecl(ThisDecl);
Douglas Gregor23996282009-05-12 21:31:51 +00002454 }
2455
Richard Smithb2bc2e62011-02-21 20:05:19 +00002456 Actions.FinalizeDeclaration(ThisDecl);
2457
Douglas Gregor23996282009-05-12 21:31:51 +00002458 return ThisDecl;
2459}
2460
Chris Lattner1890ac82006-08-13 01:16:23 +00002461/// ParseSpecifierQualifierList
2462/// specifier-qualifier-list:
2463/// type-specifier specifier-qualifier-list[opt]
2464/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002465/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00002466///
Richard Smithc5b05522012-03-12 07:56:15 +00002467void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
2468 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00002469 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
2470 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002471 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Faisal Valia534f072018-04-26 00:42:40 +00002472 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00002473
Chris Lattner1890ac82006-08-13 01:16:23 +00002474 // Validate declspec for type-name.
2475 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith649c7b062014-01-08 00:56:48 +00002476 if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00002477 Diag(Tok, diag::err_expected_type);
2478 DS.SetTypeSpecError();
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002479 } else if (Specs == DeclSpec::PQ_None && !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00002480 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00002481 if (!DS.hasTypeSpecifier())
2482 DS.SetTypeSpecError();
2483 }
Mike Stump11289f42009-09-09 15:08:12 +00002484
Chris Lattner1b22eed2006-11-28 05:12:07 +00002485 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002486 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00002487 if (DS.getStorageClassSpecLoc().isValid())
2488 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
2489 else
Richard Smithb4a9e862013-04-12 22:46:28 +00002490 Diag(DS.getThreadStorageClassSpecLoc(),
2491 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00002492 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002493 }
Mike Stump11289f42009-09-09 15:08:12 +00002494
Craig Topper3f13d4d22015-11-14 18:15:55 +00002495 // Issue diagnostic and remove function specifier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002496 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00002497 if (DS.isInlineSpecified())
2498 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
2499 if (DS.isVirtualSpecified())
2500 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
Richard Smith76b90272019-05-09 03:59:21 +00002501 if (DS.hasExplicitSpecifier())
Douglas Gregor61956c42008-10-31 09:07:45 +00002502 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00002503 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002504 }
Richard Smithc5b05522012-03-12 07:56:15 +00002505
Richard Smith76b90272019-05-09 03:59:21 +00002506 // Issue diagnostic and remove constexpr specifier if present.
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002507 if (DS.hasConstexprSpecifier() && DSC != DeclSpecContext::DSC_condition) {
2508 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr)
2509 << (DS.getConstexprSpecifier() == CSK_consteval);
Richard Smithc5b05522012-03-12 07:56:15 +00002510 DS.ClearConstexprSpec();
2511 }
Chris Lattner1890ac82006-08-13 01:16:23 +00002512}
Chris Lattner53361ac2006-08-10 05:19:57 +00002513
Chris Lattner6cc055a2009-04-12 20:42:31 +00002514/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2515/// specified token is valid after the identifier in a declarator which
2516/// immediately follows the declspec. For example, these things are valid:
2517///
2518/// int x [ 4]; // direct-declarator
2519/// int x ( int y); // direct-declarator
2520/// int(int x ) // direct-declarator
2521/// int x ; // simple-declaration
2522/// int x = 17; // init-declarator-list
2523/// int x , y; // init-declarator-list
2524/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002525/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002526/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002527///
2528/// This is not, because 'x' does not immediately follow the declspec (though
2529/// ')' happens to be valid anyway).
2530/// int (x)
2531///
2532static bool isValidAfterIdentifierInDeclarator(const Token &T) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002533 return T.isOneOf(tok::l_square, tok::l_paren, tok::r_paren, tok::semi,
2534 tok::comma, tok::equal, tok::kw_asm, tok::l_brace,
2535 tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002536}
2537
Chris Lattner20a0c612009-04-14 21:34:55 +00002538/// ParseImplicitInt - This method is called when we have an non-typename
2539/// identifier in a declspec (which normally terminates the decl spec) when
2540/// the declspec has no type specifier. In this case, the declspec is either
2541/// malformed or is "implicit int" (in K&R and C89).
2542///
2543/// This method handles diagnosing this prettily and returns false if the
2544/// declspec is done being processed. If it recovers and thinks there may be
2545/// other pieces of declspec after it, it returns true.
2546///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002547bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002548 const ParsedTemplateInfo &TemplateInfo,
Richard Smitha0a5d502014-05-08 22:32:00 +00002549 AccessSpecifier AS, DeclSpecContext DSC,
Michael Han9407e502012-11-26 22:54:45 +00002550 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002551 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002552
Chris Lattner20a0c612009-04-14 21:34:55 +00002553 SourceLocation Loc = Tok.getLocation();
2554 // If we see an identifier that is not a type name, we normally would
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002555 // parse it as the identifier being declared. However, when a typename
Chris Lattner20a0c612009-04-14 21:34:55 +00002556 // is typo'd or the definition is not included, this will incorrectly
2557 // parse the typename as the identifier name and fall over misparsing
2558 // later parts of the diagnostic.
2559 //
2560 // As such, we try to do some look-ahead in cases where this would
2561 // otherwise be an "implicit-int" case to see if this is invalid. For
2562 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2563 // an identifier with implicit int, we'd get a parse error because the
2564 // next token is obviously invalid for a type. Parse these as a case
2565 // with an invalid type specifier.
2566 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002567
Chris Lattner20a0c612009-04-14 21:34:55 +00002568 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002569 // error, do lookahead to try to do better recovery. This never applies
2570 // within a type specifier. Outside of C++, we allow this even if the
2571 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002572 // implicit int as an extension in C99 and C11.
Richard Smith649c7b062014-01-08 00:56:48 +00002573 if (!isTypeSpecifier(DSC) && !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002574 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002575 // If this token is valid for implicit int, e.g. "static x = 4", then
2576 // we just avoid eating the identifier, so it will be parsed as the
2577 // identifier in the declarator.
2578 return false;
2579 }
Mike Stump11289f42009-09-09 15:08:12 +00002580
Sven van Haastregte518bb42019-05-22 13:12:20 +00002581 // Early exit as Sema has a dedicated missing_actual_pipe_type diagnostic
2582 // for incomplete declarations such as `pipe p`.
2583 if (getLangOpts().OpenCLCPlusPlus && DS.isTypeSpecPipe())
2584 return false;
2585
Richard Smitha952ebb2012-05-15 21:01:51 +00002586 if (getLangOpts().CPlusPlus &&
2587 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2588 // Don't require a type specifier if we have the 'auto' storage class
2589 // specifier in C++98 -- we'll promote it to a type specifier.
Richard Smithfb8b7b92013-10-15 00:00:26 +00002590 if (SS)
2591 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
Richard Smitha952ebb2012-05-15 21:01:51 +00002592 return false;
2593 }
2594
Reid Kleckner9a96e422016-05-24 21:23:54 +00002595 if (getLangOpts().CPlusPlus && (!SS || SS->isEmpty()) &&
2596 getLangOpts().MSVCCompat) {
2597 // Lookup of an unqualified type name has failed in MSVC compatibility mode.
2598 // Give Sema a chance to recover if we are in a template with dependent base
2599 // classes.
2600 if (ParsedType T = Actions.ActOnMSVCUnknownTypeName(
2601 *Tok.getIdentifierInfo(), Tok.getLocation(),
Faisal Vali7db85c52017-12-31 00:06:40 +00002602 DSC == DeclSpecContext::DSC_template_type_arg)) {
Reid Kleckner9a96e422016-05-24 21:23:54 +00002603 const char *PrevSpec;
2604 unsigned DiagID;
2605 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
2606 Actions.getASTContext().getPrintingPolicy());
2607 DS.SetRangeEnd(Tok.getLocation());
2608 ConsumeToken();
2609 return false;
2610 }
2611 }
2612
Chris Lattner20a0c612009-04-14 21:34:55 +00002613 // Otherwise, if we don't consume this token, we are going to emit an
2614 // error anyway. Try to recover from various common problems. Check
2615 // to see if this was a reference to a tag name without a tag specified.
2616 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002617 //
2618 // C++ doesn't need this, and isTagName doesn't take SS.
Craig Topper161e4db2014-05-21 06:02:52 +00002619 if (SS == nullptr) {
2620 const char *TagName = nullptr, *FixitTagName = nullptr;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002621 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002622
Douglas Gregor0be31a22010-07-02 17:43:08 +00002623 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002624 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002625 case DeclSpec::TST_enum:
2626 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2627 case DeclSpec::TST_union:
2628 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2629 case DeclSpec::TST_struct:
2630 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002631 case DeclSpec::TST_interface:
2632 TagName="__interface"; FixitTagName = "__interface ";
2633 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002634 case DeclSpec::TST_class:
2635 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002636 }
Mike Stump11289f42009-09-09 15:08:12 +00002637
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002638 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002639 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2640 LookupResult R(Actions, TokenName, SourceLocation(),
2641 Sema::LookupOrdinaryName);
2642
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002643 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002644 << TokenName << TagName << getLangOpts().CPlusPlus
2645 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2646
2647 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2648 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2649 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002650 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002651 << TokenName << TagName;
2652 }
Mike Stump11289f42009-09-09 15:08:12 +00002653
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002654 // Parse this as a tag as if the missing tag were present.
2655 if (TagKind == tok::kw_enum)
Faisal Vali7db85c52017-12-31 00:06:40 +00002656 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS,
2657 DeclSpecContext::DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002658 else
Richard Smithc5b05522012-03-12 07:56:15 +00002659 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Faisal Vali7db85c52017-12-31 00:06:40 +00002660 /*EnteringContext*/ false,
2661 DeclSpecContext::DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002662 return true;
2663 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002664 }
Mike Stump11289f42009-09-09 15:08:12 +00002665
Richard Smithfe904f02012-05-15 21:29:55 +00002666 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002667 // being declared (with a missing type).
Faisal Vali7db85c52017-12-31 00:06:40 +00002668 if (!isTypeSpecifier(DSC) && (!SS || DSC == DeclSpecContext::DSC_top_level ||
2669 DSC == DeclSpecContext::DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002670 // Look ahead to the next token to try to figure out what this declaration
2671 // was supposed to be.
2672 switch (NextToken().getKind()) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002673 case tok::l_paren: {
2674 // static x(4); // 'x' is not a type
2675 // x(int n); // 'x' is not a type
2676 // x (*p)[]; // 'x' is a type
2677 //
Richard Smitha0a5d502014-05-08 22:32:00 +00002678 // Since we're in an error case, we can afford to perform a tentative
2679 // parse to determine which case we're in.
Richard Smitha952ebb2012-05-15 21:01:51 +00002680 TentativeParsingAction PA(*this);
2681 ConsumeToken();
2682 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2683 PA.Revert();
Richard Smithfb8b7b92013-10-15 00:00:26 +00002684
Richard Smithee390432014-05-16 01:56:53 +00002685 if (TPR != TPResult::False) {
Richard Smithfb8b7b92013-10-15 00:00:26 +00002686 // The identifier is followed by a parenthesized declarator.
2687 // It's supposed to be a type.
2688 break;
2689 }
2690
2691 // If we're in a context where we could be declaring a constructor,
2692 // check whether this is a constructor declaration with a bogus name.
Faisal Vali7db85c52017-12-31 00:06:40 +00002693 if (DSC == DeclSpecContext::DSC_class ||
2694 (DSC == DeclSpecContext::DSC_top_level && SS)) {
Richard Smithfb8b7b92013-10-15 00:00:26 +00002695 IdentifierInfo *II = Tok.getIdentifierInfo();
2696 if (Actions.isCurrentClassNameTypo(II, SS)) {
2697 Diag(Loc, diag::err_constructor_bad_name)
2698 << Tok.getIdentifierInfo() << II
2699 << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
2700 Tok.setIdentifierInfo(II);
2701 }
2702 }
2703 // Fall through.
Galina Kistanova77674252017-06-01 21:15:34 +00002704 LLVM_FALLTHROUGH;
Richard Smitha952ebb2012-05-15 21:01:51 +00002705 }
Richard Smithfb8b7b92013-10-15 00:00:26 +00002706 case tok::comma:
2707 case tok::equal:
2708 case tok::kw_asm:
2709 case tok::l_brace:
2710 case tok::l_square:
2711 case tok::semi:
2712 // This looks like a variable or function declaration. The type is
2713 // probably missing. We're done parsing decl-specifiers.
Nicolas Lesseree057172019-05-05 12:15:17 +00002714 // But only if we are not in a function prototype scope.
2715 if (getCurScope()->isFunctionPrototypeScope())
2716 break;
Richard Smithfb8b7b92013-10-15 00:00:26 +00002717 if (SS)
2718 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2719 return false;
Richard Smitha952ebb2012-05-15 21:01:51 +00002720
2721 default:
2722 // This is probably supposed to be a type. This includes cases like:
2723 // int f(itn);
2724 // struct S { unsinged : 4; };
2725 break;
2726 }
2727 }
2728
Reid Klecknerc05ca5e2014-06-19 01:23:22 +00002729 // This is almost certainly an invalid type name. Let Sema emit a diagnostic
2730 // and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002731 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002732 IdentifierInfo *II = Tok.getIdentifierInfo();
Richard Smith52f8d192017-05-10 21:32:16 +00002733 bool IsTemplateName = getLangOpts().CPlusPlus && NextToken().is(tok::less);
Reid Klecknerc05ca5e2014-06-19 01:23:22 +00002734 Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T,
Richard Smith52f8d192017-05-10 21:32:16 +00002735 IsTemplateName);
Reid Klecknerc05ca5e2014-06-19 01:23:22 +00002736 if (T) {
2737 // The action has suggested that the type T could be used. Set that as
2738 // the type in the declaration specifiers, consume the would-be type
2739 // name token, and we're done.
2740 const char *PrevSpec;
2741 unsigned DiagID;
2742 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
2743 Actions.getASTContext().getPrintingPolicy());
2744 DS.SetRangeEnd(Tok.getLocation());
2745 ConsumeToken();
2746 // There may be other declaration specifiers after this.
2747 return true;
2748 } else if (II != Tok.getIdentifierInfo()) {
2749 // If no type was suggested, the correction is to a keyword
2750 Tok.setKind(II->getTokenID());
2751 // There may be other declaration specifiers after this.
2752 return true;
Douglas Gregor15e56022009-10-13 23:27:22 +00002753 }
Mike Stump11289f42009-09-09 15:08:12 +00002754
Reid Klecknerc05ca5e2014-06-19 01:23:22 +00002755 // Otherwise, the action had no suggestion for us. Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002756 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002757 DS.SetRangeEnd(Tok.getLocation());
2758 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002759
Richard Smith52f8d192017-05-10 21:32:16 +00002760 // Eat any following template arguments.
2761 if (IsTemplateName) {
2762 SourceLocation LAngle, RAngle;
2763 TemplateArgList Args;
2764 ParseTemplateIdAfterTemplateName(true, LAngle, Args, RAngle);
2765 }
2766
Chris Lattner20a0c612009-04-14 21:34:55 +00002767 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2768 // avoid rippling error messages on subsequent uses of the same type,
2769 // could be useful if #include was forgotten.
Richard Smithb3065792019-05-07 07:36:07 +00002770 return true;
Chris Lattner20a0c612009-04-14 21:34:55 +00002771}
2772
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002773/// Determine the declaration specifier context from the declarator
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002774/// context.
2775///
2776/// \param Context the declarator context, which is one of the
Faisal Vali421b2d12017-12-29 05:41:00 +00002777/// DeclaratorContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002778Parser::DeclSpecContext
Faisal Vali421b2d12017-12-29 05:41:00 +00002779Parser::getDeclSpecContextFromDeclaratorContext(DeclaratorContext Context) {
2780 if (Context == DeclaratorContext::MemberContext)
Faisal Vali7db85c52017-12-31 00:06:40 +00002781 return DeclSpecContext::DSC_class;
Faisal Vali421b2d12017-12-29 05:41:00 +00002782 if (Context == DeclaratorContext::FileContext)
Faisal Vali7db85c52017-12-31 00:06:40 +00002783 return DeclSpecContext::DSC_top_level;
Faisal Vali421b2d12017-12-29 05:41:00 +00002784 if (Context == DeclaratorContext::TemplateParamContext)
Faisal Vali7db85c52017-12-31 00:06:40 +00002785 return DeclSpecContext::DSC_template_param;
Richard Smith77a9c602018-02-28 03:02:23 +00002786 if (Context == DeclaratorContext::TemplateArgContext ||
2787 Context == DeclaratorContext::TemplateTypeArgContext)
Faisal Vali7db85c52017-12-31 00:06:40 +00002788 return DeclSpecContext::DSC_template_type_arg;
Richard Smithe303e352018-02-02 22:24:54 +00002789 if (Context == DeclaratorContext::TrailingReturnContext ||
2790 Context == DeclaratorContext::TrailingReturnVarContext)
Faisal Vali7db85c52017-12-31 00:06:40 +00002791 return DeclSpecContext::DSC_trailing;
Faisal Vali421b2d12017-12-29 05:41:00 +00002792 if (Context == DeclaratorContext::AliasDeclContext ||
2793 Context == DeclaratorContext::AliasTemplateContext)
Faisal Vali7db85c52017-12-31 00:06:40 +00002794 return DeclSpecContext::DSC_alias_declaration;
2795 return DeclSpecContext::DSC_normal;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002796}
2797
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002798/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2799///
2800/// FIXME: Simply returns an alignof() expression if the argument is a
2801/// type. Ideally, the type should be propagated directly into Sema.
2802///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002803/// [C11] type-id
2804/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002805/// [C++0x] type-id ...[opt]
2806/// [C++0x] assignment-expression ...[opt]
2807ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2808 SourceLocation &EllipsisLoc) {
2809 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002810 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002811 SourceLocation TypeLoc = Tok.getLocation();
2812 ParsedType Ty = ParseTypeName().get();
2813 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002814 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2815 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002816 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002817 ER = ParseConstantExpression();
2818
Alp Toker8fbec672013-12-17 23:29:36 +00002819 if (getLangOpts().CPlusPlus11)
2820 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002821
2822 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002823}
2824
2825/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2826/// attribute to Attrs.
2827///
2828/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002829/// [C11] '_Alignas' '(' type-id ')'
2830/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002831/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2832/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002833void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002834 SourceLocation *EndLoc) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002835 assert(Tok.isOneOf(tok::kw_alignas, tok::kw__Alignas) &&
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002836 "Not an alignment-specifier!");
2837
Richard Smithd11c7a12013-01-29 01:48:07 +00002838 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2839 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002840
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002841 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00002842 if (T.expectAndConsume())
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002843 return;
2844
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002845 SourceLocation EllipsisLoc;
2846 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002847 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002848 T.skipToEnd();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002849 return;
2850 }
2851
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002852 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002853 if (EndLoc)
2854 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002855
Aaron Ballman00e99962013-08-31 01:11:41 +00002856 ArgsVector ArgExprs;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002857 ArgExprs.push_back(ArgExpr.get());
Craig Topper161e4db2014-05-21 06:02:52 +00002858 Attrs.addNew(KWName, KWLoc, nullptr, KWLoc, ArgExprs.data(), 1,
Erich Keanee891aa92018-07-13 15:07:47 +00002859 ParsedAttr::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002860}
2861
Richard Smith404dfb42013-11-19 22:47:36 +00002862/// Determine whether we're looking at something that might be a declarator
2863/// in a simple-declaration. If it can't possibly be a declarator, maybe
2864/// diagnose a missing semicolon after a prior tag definition in the decl
2865/// specifier.
2866///
2867/// \return \c true if an error occurred and this can't be any kind of
2868/// declaration.
2869bool
2870Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
2871 DeclSpecContext DSContext,
2872 LateParsedAttrList *LateAttrs) {
2873 assert(DS.hasTagDefinition() && "shouldn't call this");
2874
Faisal Vali7db85c52017-12-31 00:06:40 +00002875 bool EnteringContext = (DSContext == DeclSpecContext::DSC_class ||
2876 DSContext == DeclSpecContext::DSC_top_level);
Richard Smith404dfb42013-11-19 22:47:36 +00002877
2878 if (getLangOpts().CPlusPlus &&
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002879 Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype,
2880 tok::annot_template_id) &&
Richard Smith404dfb42013-11-19 22:47:36 +00002881 TryAnnotateCXXScopeToken(EnteringContext)) {
2882 SkipMalformedDecl();
2883 return true;
2884 }
2885
Richard Smith698875a2013-11-20 23:40:57 +00002886 bool HasScope = Tok.is(tok::annot_cxxscope);
2887 // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
2888 Token AfterScope = HasScope ? NextToken() : Tok;
2889
Richard Smith404dfb42013-11-19 22:47:36 +00002890 // Determine whether the following tokens could possibly be a
2891 // declarator.
Richard Smith698875a2013-11-20 23:40:57 +00002892 bool MightBeDeclarator = true;
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002893 if (Tok.isOneOf(tok::kw_typename, tok::annot_typename)) {
Richard Smith698875a2013-11-20 23:40:57 +00002894 // A declarator-id can't start with 'typename'.
2895 MightBeDeclarator = false;
2896 } else if (AfterScope.is(tok::annot_template_id)) {
2897 // If we have a type expressed as a template-id, this cannot be a
2898 // declarator-id (such a type cannot be redeclared in a simple-declaration).
2899 TemplateIdAnnotation *Annot =
2900 static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
2901 if (Annot->Kind == TNK_Type_template)
2902 MightBeDeclarator = false;
2903 } else if (AfterScope.is(tok::identifier)) {
2904 const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken();
2905
Richard Smith404dfb42013-11-19 22:47:36 +00002906 // These tokens cannot come after the declarator-id in a
2907 // simple-declaration, and are likely to come after a type-specifier.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002908 if (Next.isOneOf(tok::star, tok::amp, tok::ampamp, tok::identifier,
2909 tok::annot_cxxscope, tok::coloncolon)) {
Richard Smith698875a2013-11-20 23:40:57 +00002910 // Missing a semicolon.
2911 MightBeDeclarator = false;
2912 } else if (HasScope) {
2913 // If the declarator-id has a scope specifier, it must redeclare a
2914 // previously-declared entity. If that's a type (and this is not a
2915 // typedef), that's an error.
2916 CXXScopeSpec SS;
2917 Actions.RestoreNestedNameSpecifierAnnotation(
2918 Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
2919 IdentifierInfo *Name = AfterScope.getIdentifierInfo();
2920 Sema::NameClassification Classification = Actions.ClassifyName(
2921 getCurScope(), SS, Name, AfterScope.getLocation(), Next,
Bruno Ricci70ad3962019-03-25 17:08:51 +00002922 /*IsAddressOfOperand=*/false, /*CCC=*/nullptr);
Richard Smith698875a2013-11-20 23:40:57 +00002923 switch (Classification.getKind()) {
2924 case Sema::NC_Error:
2925 SkipMalformedDecl();
2926 return true;
Richard Smith404dfb42013-11-19 22:47:36 +00002927
Richard Smith698875a2013-11-20 23:40:57 +00002928 case Sema::NC_Keyword:
2929 case Sema::NC_NestedNameSpecifier:
2930 llvm_unreachable("typo correction and nested name specifiers not "
2931 "possible here");
Richard Smith404dfb42013-11-19 22:47:36 +00002932
Richard Smith698875a2013-11-20 23:40:57 +00002933 case Sema::NC_Type:
2934 case Sema::NC_TypeTemplate:
2935 // Not a previously-declared non-type entity.
2936 MightBeDeclarator = false;
2937 break;
Richard Smith404dfb42013-11-19 22:47:36 +00002938
Richard Smith698875a2013-11-20 23:40:57 +00002939 case Sema::NC_Unknown:
2940 case Sema::NC_Expression:
2941 case Sema::NC_VarTemplate:
2942 case Sema::NC_FunctionTemplate:
Richard Smithb23c5e82019-05-09 03:31:27 +00002943 case Sema::NC_UndeclaredTemplate:
Richard Smith698875a2013-11-20 23:40:57 +00002944 // Might be a redeclaration of a prior entity.
2945 break;
2946 }
Richard Smith404dfb42013-11-19 22:47:36 +00002947 }
Richard Smith404dfb42013-11-19 22:47:36 +00002948 }
2949
Richard Smith698875a2013-11-20 23:40:57 +00002950 if (MightBeDeclarator)
Richard Smith404dfb42013-11-19 22:47:36 +00002951 return false;
2952
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002953 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002954 Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getEndLoc()),
Alp Toker383d2c42014-01-01 03:08:43 +00002955 diag::err_expected_after)
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002956 << DeclSpec::getSpecifierName(DS.getTypeSpecType(), PPol) << tok::semi;
Richard Smith404dfb42013-11-19 22:47:36 +00002957
2958 // Try to recover from the typo, by dropping the tag definition and parsing
2959 // the problematic tokens as a type.
2960 //
2961 // FIXME: Split the DeclSpec into pieces for the standalone
2962 // declaration and pieces for the following declaration, instead
2963 // of assuming that all the other pieces attach to new declaration,
2964 // and call ParsedFreeStandingDeclSpec as appropriate.
2965 DS.ClearTypeSpecType();
2966 ParsedTemplateInfo NotATemplate;
Faisal Valia534f072018-04-26 00:42:40 +00002967 ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
Richard Smith404dfb42013-11-19 22:47:36 +00002968 return false;
2969}
2970
Leonard Chanab80f3c2018-06-14 14:53:51 +00002971// Choose the apprpriate diagnostic error for why fixed point types are
2972// disabled, set the previous specifier, and mark as invalid.
2973static void SetupFixedPointError(const LangOptions &LangOpts,
2974 const char *&PrevSpec, unsigned &DiagID,
2975 bool &isInvalid) {
2976 assert(!LangOpts.FixedPoint);
2977 DiagID = diag::err_fixed_point_not_enabled;
2978 PrevSpec = ""; // Not used by diagnostic
2979 isInvalid = true;
2980}
2981
Faisal Valia534f072018-04-26 00:42:40 +00002982/// ParseDeclarationSpecifiers
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002983/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002984/// storage-class-specifier declaration-specifiers[opt]
2985/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002986/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002987/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002988/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002989/// [Clang] '__module_private__' declaration-specifiers[opt]
Douglas Gregorab209d82015-07-07 03:58:42 +00002990/// [ObjC1] '__kindof' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002991///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002992/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002993/// 'typedef'
2994/// 'extern'
2995/// 'static'
2996/// 'auto'
2997/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002998/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00002999/// [C++11] 'thread_local'
3000/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00003001/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003002/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00003003/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00003004/// [C++] 'virtual'
3005/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003006/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00003007/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00003008/// 'constexpr': [C++0x dcl.constexpr]
Faisal Valia534f072018-04-26 00:42:40 +00003009void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00003010 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00003011 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00003012 DeclSpecContext DSContext,
3013 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00003014 if (DS.getSourceRange().isInvalid()) {
David Majnemer24b28302014-07-26 05:41:31 +00003015 // Start the range at the current token but make the end of the range
3016 // invalid. This will make the entire range invalid unless we successfully
3017 // consume a token.
Douglas Gregor0e7dde52011-04-24 05:37:28 +00003018 DS.SetRangeStart(Tok.getLocation());
David Majnemer24b28302014-07-26 05:41:31 +00003019 DS.SetRangeEnd(SourceLocation());
Douglas Gregor0e7dde52011-04-24 05:37:28 +00003020 }
Chad Rosierc1183952012-06-26 22:30:43 +00003021
Faisal Vali7db85c52017-12-31 00:06:40 +00003022 bool EnteringContext = (DSContext == DeclSpecContext::DSC_class ||
3023 DSContext == DeclSpecContext::DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003024 bool AttrsLastTime = false;
3025 ParsedAttributesWithRange attrs(AttrFactory);
Benjamin Kramere4812142015-03-12 14:28:38 +00003026 // We use Sema's policy to get bool macros right.
Richard Smith301bc212016-05-19 01:39:10 +00003027 PrintingPolicy Policy = Actions.getPrintingPolicy();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003028 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00003029 bool isInvalid = false;
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003030 bool isStorageClass = false;
Craig Topper161e4db2014-05-21 06:02:52 +00003031 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +00003032 unsigned DiagID = 0;
3033
Richard Smithba24f352019-05-09 19:45:46 +00003034 // This value needs to be set to the location of the last token if the last
3035 // token of the specifier is already consumed.
3036 SourceLocation ConsumedEnd;
Richard Smith76b90272019-05-09 03:59:21 +00003037
David Majnemer51fd8a02015-07-22 23:46:18 +00003038 // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL
3039 // implementation for VS2013 uses _Atomic as an identifier for one of the
3040 // classes in <atomic>.
3041 //
3042 // A typedef declaration containing _Atomic<...> is among the places where
3043 // the class is used. If we are currently parsing such a declaration, treat
3044 // the token as an identifier.
3045 if (getLangOpts().MSVCCompat && Tok.is(tok::kw__Atomic) &&
3046 DS.getStorageClassSpec() == clang::DeclSpec::SCS_typedef &&
3047 !DS.hasTypeSpecifier() && GetLookAheadToken(1).is(tok::less))
3048 Tok.setKind(tok::identifier);
3049
Chris Lattner4d8f8732006-11-28 05:05:08 +00003050 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00003051
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003052 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00003053 default:
Chris Lattner0974b232008-07-26 00:20:22 +00003054 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003055 if (!AttrsLastTime)
3056 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00003057 else {
3058 // Reject C++11 attributes that appertain to decl specifiers as
3059 // we don't support any C++11 attributes that appertain to decl
3060 // specifiers. This also conforms to what g++ 4.8 is doing.
Richard Smith49cc1cc2016-08-18 21:59:42 +00003061 ProhibitCXX11Attributes(attrs, diag::err_attribute_not_type_attr);
Michael Han64536a62012-11-06 19:34:54 +00003062
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003063 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00003064 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00003065
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003066 // If this is not a declaration specifier token, we're done reading decl
3067 // specifiers. First verify that DeclSpec's are consistent.
Craig Topper25122412015-11-15 03:32:11 +00003068 DS.Finish(Actions, Policy);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003069 return;
Mike Stump11289f42009-09-09 15:08:12 +00003070
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003071 case tok::l_square:
3072 case tok::kw_alignas:
Aaron Ballman606093a2017-10-15 15:01:42 +00003073 if (!standardAttributesAllowed() || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003074 goto DoneWithDeclSpec;
3075
3076 ProhibitAttributes(attrs);
3077 // FIXME: It would be good to recover by accepting the attributes,
3078 // but attempting to do that now would cause serious
3079 // madness in terms of diagnostics.
3080 attrs.clear();
3081 attrs.Range = SourceRange();
3082
3083 ParseCXX11Attributes(attrs);
3084 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00003085 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003086
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003087 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00003088 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003089 if (DS.hasTypeSpecifier()) {
3090 bool AllowNonIdentifiers
3091 = (getCurScope()->getFlags() & (Scope::ControlScope |
3092 Scope::BlockScope |
3093 Scope::TemplateParamScope |
3094 Scope::FunctionPrototypeScope |
3095 Scope::AtCatchScope)) == 0;
3096 bool AllowNestedNameSpecifiers
Faisal Vali7db85c52017-12-31 00:06:40 +00003097 = DSContext == DeclSpecContext::DSC_top_level ||
3098 (DSContext == DeclSpecContext::DSC_class && DS.isFriendSpecified());
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003099
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00003100 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00003101 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00003102 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003103 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00003104 }
3105
Douglas Gregor80039242011-02-15 20:33:25 +00003106 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
3107 CCC = Sema::PCC_LocalDeclarationSpecifiers;
3108 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Faisal Vali7db85c52017-12-31 00:06:40 +00003109 CCC = DSContext == DeclSpecContext::DSC_class ? Sema::PCC_MemberTemplate
3110 : Sema::PCC_Template;
3111 else if (DSContext == DeclSpecContext::DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00003112 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00003113 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00003114 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00003115
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003116 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003117 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003118 }
3119
Chris Lattnerbd31aa32009-01-05 00:07:25 +00003120 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00003121 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman2a1d9a92013-08-15 23:59:20 +00003122 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00003123 if (!DS.hasTypeSpecifier())
3124 DS.SetTypeSpecError();
3125 goto DoneWithDeclSpec;
3126 }
John McCall8bc2a702010-03-01 18:20:46 +00003127 if (Tok.is(tok::coloncolon)) // ::new or ::delete
3128 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00003129 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003130
3131 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00003132 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003133 goto DoneWithDeclSpec;
3134
John McCall9dab4e62009-12-12 11:40:51 +00003135 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00003136 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
3137 Tok.getAnnotationRange(),
3138 SS);
John McCall9dab4e62009-12-12 11:40:51 +00003139
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003140 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00003141 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00003142 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00003143 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00003144 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00003145 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003146
Richard Smith74f02342017-01-19 21:00:13 +00003147 // If this would be a valid constructor declaration with template
3148 // arguments, we will reject the attempt to form an invalid type-id
3149 // referring to the injected-class-name when we annotate the token,
3150 // per C++ [class.qual]p2.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003151 //
Richard Smith74f02342017-01-19 21:00:13 +00003152 // To improve diagnostics for this case, parse the declaration as a
3153 // constructor (and reject the extra template arguments later).
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00003154 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Faisal Vali7db85c52017-12-31 00:06:40 +00003155 if ((DSContext == DeclSpecContext::DSC_top_level ||
3156 DSContext == DeclSpecContext::DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00003157 TemplateId->Name &&
Richard Smith74f02342017-01-19 21:00:13 +00003158 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS) &&
Faisal Vali7db85c52017-12-31 00:06:40 +00003159 isConstructorDeclarator(/*Unqualified*/ false)) {
Richard Smith74f02342017-01-19 21:00:13 +00003160 // The user meant this to be an out-of-line constructor
3161 // definition, but template arguments are not allowed
3162 // there. Just allow this as a constructor; we'll
3163 // complain about it later.
3164 goto DoneWithDeclSpec;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003165 }
3166
John McCall9dab4e62009-12-12 11:40:51 +00003167 DS.getTypeSpecScope() = SS;
Richard Smithaf3b3252017-05-18 19:21:48 +00003168 ConsumeAnnotationToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00003169 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00003170 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00003171 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00003172 continue;
3173 }
3174
Douglas Gregorc5790df2009-09-28 07:26:33 +00003175 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00003176 DS.getTypeSpecScope() = SS;
Richard Smithaf3b3252017-05-18 19:21:48 +00003177 ConsumeAnnotationToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00003178 if (Tok.getAnnotationValue()) {
3179 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00003180 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00003181 Tok.getAnnotationEndLoc(),
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003182 PrevSpec, DiagID, T, Policy);
Richard Smithda837032012-09-14 18:27:01 +00003183 if (isInvalid)
3184 break;
John McCallba7bf592010-08-24 05:47:05 +00003185 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00003186 else
3187 DS.SetTypeSpecError();
3188 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
Richard Smithaf3b3252017-05-18 19:21:48 +00003189 ConsumeAnnotationToken(); // The typename
Douglas Gregorc5790df2009-09-28 07:26:33 +00003190 }
3191
Douglas Gregor167fa622009-03-25 15:40:00 +00003192 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003193 goto DoneWithDeclSpec;
3194
Richard Smith74f02342017-01-19 21:00:13 +00003195 // Check whether this is a constructor declaration. If we're in a
3196 // context where the identifier could be a class name, and it has the
3197 // shape of a constructor declaration, process it as one.
Faisal Vali7db85c52017-12-31 00:06:40 +00003198 if ((DSContext == DeclSpecContext::DSC_top_level ||
3199 DSContext == DeclSpecContext::DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00003200 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Richard Smith74f02342017-01-19 21:00:13 +00003201 &SS) &&
3202 isConstructorDeclarator(/*Unqualified*/ false))
3203 goto DoneWithDeclSpec;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003204
David Blaikieefdccaa2016-01-15 23:43:34 +00003205 ParsedType TypeRep =
3206 Actions.getTypeName(*Next.getIdentifierInfo(), Next.getLocation(),
3207 getCurScope(), &SS, false, false, nullptr,
3208 /*IsCtorOrDtorName=*/false,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00003209 /*WantNontrivialTypeSourceInfo=*/true,
Richard Smith600b5262017-01-26 20:40:47 +00003210 isClassTemplateDeductionContext(DSContext));
Douglas Gregor8bf42052009-02-09 18:46:07 +00003211
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00003212 // If the referenced identifier is not a type, then this declspec is
3213 // erroneous: We already checked about that it has no type specifier, and
3214 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00003215 // typename.
David Blaikie7d170102013-05-15 07:37:26 +00003216 if (!TypeRep) {
Richard Smithaf3b3252017-05-18 19:21:48 +00003217 // Eat the scope spec so the identifier is current.
3218 ConsumeAnnotationToken();
Michael Han9407e502012-11-26 22:54:45 +00003219 ParsedAttributesWithRange Attrs(AttrFactory);
3220 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
3221 if (!Attrs.empty()) {
3222 AttrsLastTime = true;
3223 attrs.takeAllFrom(Attrs);
3224 }
3225 continue;
3226 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003227 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00003228 }
Mike Stump11289f42009-09-09 15:08:12 +00003229
John McCall9dab4e62009-12-12 11:40:51 +00003230 DS.getTypeSpecScope() = SS;
Richard Smithaf3b3252017-05-18 19:21:48 +00003231 ConsumeAnnotationToken(); // The C++ scope.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003232
Douglas Gregor9817f4a2009-02-09 15:09:02 +00003233 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003234 DiagID, TypeRep, Policy);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003235 if (isInvalid)
3236 break;
Mike Stump11289f42009-09-09 15:08:12 +00003237
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003238 DS.SetRangeEnd(Tok.getLocation());
3239 ConsumeToken(); // The typename.
3240
3241 continue;
3242 }
Mike Stump11289f42009-09-09 15:08:12 +00003243
Chris Lattnere387d9e2009-01-21 19:48:37 +00003244 case tok::annot_typename: {
Richard Smith404dfb42013-11-19 22:47:36 +00003245 // If we've previously seen a tag definition, we were almost surely
3246 // missing a semicolon after it.
3247 if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
3248 goto DoneWithDeclSpec;
3249
John McCallba7bf592010-08-24 05:47:05 +00003250 if (Tok.getAnnotationValue()) {
3251 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00003252 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003253 DiagID, T, Policy);
John McCallba7bf592010-08-24 05:47:05 +00003254 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00003255 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00003256
Chris Lattner005fc1b2010-04-05 18:18:31 +00003257 if (isInvalid)
3258 break;
3259
Chris Lattnere387d9e2009-01-21 19:48:37 +00003260 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
Richard Smithaf3b3252017-05-18 19:21:48 +00003261 ConsumeAnnotationToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00003262
Chris Lattnere387d9e2009-01-21 19:48:37 +00003263 continue;
3264 }
Mike Stump11289f42009-09-09 15:08:12 +00003265
Douglas Gregor06873092011-04-28 15:48:45 +00003266 case tok::kw___is_signed:
3267 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
3268 // typically treats it as a trait. If we see __is_signed as it appears
3269 // in libstdc++, e.g.,
3270 //
3271 // static const bool __is_signed;
3272 //
3273 // then treat __is_signed as an identifier rather than as a keyword.
Faisal Vali090da2d2018-01-01 18:23:28 +00003274 if (DS.getTypeSpecType() == TST_bool &&
Douglas Gregor06873092011-04-28 15:48:45 +00003275 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
Alp Toker47642d22013-12-03 06:13:01 +00003276 DS.getStorageClassSpec() == DeclSpec::SCS_static)
3277 TryKeywordIdentFallback(true);
Douglas Gregor06873092011-04-28 15:48:45 +00003278
3279 // We're done with the declaration-specifiers.
3280 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003281
Chris Lattner16fac4f2008-07-26 01:18:38 +00003282 // typedef-name
Nikola Smiljanic67860242014-09-26 00:28:20 +00003283 case tok::kw___super:
David Blaikie15a430a2011-12-04 05:04:18 +00003284 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003285 case tok::identifier: {
Reid Klecknerc582f012014-07-14 18:19:58 +00003286 // This identifier can only be a typedef name if we haven't already seen
3287 // a type-specifier. Without this check we misparse:
3288 // typedef int X; struct Y { short X; }; as 'short int'.
3289 if (DS.hasTypeSpecifier())
3290 goto DoneWithDeclSpec;
3291
Aaron Ballman52d0aaa2017-02-14 22:47:20 +00003292 // If the token is an identifier named "__declspec" and Microsoft
3293 // extensions are not enabled, it is likely that there will be cascading
3294 // parse errors if this really is a __declspec attribute. Attempt to
3295 // recognize that scenario and recover gracefully.
3296 if (!getLangOpts().DeclSpecKeyword && Tok.is(tok::identifier) &&
3297 Tok.getIdentifierInfo()->getName().equals("__declspec")) {
3298 Diag(Loc, diag::err_ms_attributes_not_enabled);
3299
3300 // The next token should be an open paren. If it is, eat the entire
3301 // attribute declaration and continue.
3302 if (NextToken().is(tok::l_paren)) {
3303 // Consume the __declspec identifier.
Richard Trieuba057372017-02-14 23:56:55 +00003304 ConsumeToken();
Aaron Ballman52d0aaa2017-02-14 22:47:20 +00003305
3306 // Eat the parens and everything between them.
3307 BalancedDelimiterTracker T(*this, tok::l_paren);
3308 if (T.consumeOpen()) {
3309 assert(false && "Not a left paren?");
3310 return;
3311 }
3312 T.skipToEnd();
3313 continue;
3314 }
3315 }
3316
Serge Pavlov458ea762014-07-16 05:16:52 +00003317 // In C++, check to see if this is a scope specifier like foo::bar::, if
3318 // so handle it as such. This is important for ctor parsing.
3319 if (getLangOpts().CPlusPlus) {
3320 if (TryAnnotateCXXScopeToken(EnteringContext)) {
3321 DS.SetTypeSpecError();
3322 goto DoneWithDeclSpec;
3323 }
3324 if (!Tok.is(tok::identifier))
3325 continue;
3326 }
3327
John Thompson22334602010-02-05 00:12:22 +00003328 // Check for need to substitute AltiVec keyword tokens.
3329 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
3330 break;
3331
Richard Smith3092a3b2012-05-09 18:56:43 +00003332 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
3333 // allow the use of a typedef name as a type specifier.
3334 if (DS.isTypeAltiVecVector())
3335 goto DoneWithDeclSpec;
3336
Faisal Vali7db85c52017-12-31 00:06:40 +00003337 if (DSContext == DeclSpecContext::DSC_objc_method_result &&
3338 isObjCInstancetype()) {
Douglas Gregor5c0870a2015-06-19 23:18:00 +00003339 ParsedType TypeRep = Actions.ActOnObjCInstanceType(Loc);
3340 assert(TypeRep);
3341 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
3342 DiagID, TypeRep, Policy);
3343 if (isInvalid)
3344 break;
3345
3346 DS.SetRangeEnd(Loc);
3347 ConsumeToken();
3348 continue;
3349 }
3350
Richard Smith715ee072018-06-20 21:58:20 +00003351 // If we're in a context where the identifier could be a class name,
3352 // check whether this is a constructor declaration.
3353 if (getLangOpts().CPlusPlus && DSContext == DeclSpecContext::DSC_class &&
3354 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
3355 isConstructorDeclarator(/*Unqualified*/true))
3356 goto DoneWithDeclSpec;
3357
Richard Smith600b5262017-01-26 20:40:47 +00003358 ParsedType TypeRep = Actions.getTypeName(
3359 *Tok.getIdentifierInfo(), Tok.getLocation(), getCurScope(), nullptr,
3360 false, false, nullptr, false, false,
3361 isClassTemplateDeductionContext(DSContext));
Douglas Gregor8bf42052009-02-09 18:46:07 +00003362
Chris Lattner6cc055a2009-04-12 20:42:31 +00003363 // If this is not a typedef name, don't parse it as part of the declspec,
3364 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00003365 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00003366 ParsedAttributesWithRange Attrs(AttrFactory);
Craig Topper161e4db2014-05-21 06:02:52 +00003367 if (ParseImplicitInt(DS, nullptr, TemplateInfo, AS, DSContext, Attrs)) {
Michael Han9407e502012-11-26 22:54:45 +00003368 if (!Attrs.empty()) {
3369 AttrsLastTime = true;
3370 attrs.takeAllFrom(Attrs);
3371 }
3372 continue;
3373 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00003374 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00003375 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00003376
Richard Smith35845152017-02-07 01:37:30 +00003377 // Likewise, if this is a context where the identifier could be a template
3378 // name, check whether this is a deduction guide declaration.
Aaron Ballmanc351fba2017-12-04 20:27:34 +00003379 if (getLangOpts().CPlusPlus17 &&
Faisal Vali7db85c52017-12-31 00:06:40 +00003380 (DSContext == DeclSpecContext::DSC_class ||
3381 DSContext == DeclSpecContext::DSC_top_level) &&
Richard Smith35845152017-02-07 01:37:30 +00003382 Actions.isDeductionGuideName(getCurScope(), *Tok.getIdentifierInfo(),
3383 Tok.getLocation()) &&
3384 isConstructorDeclarator(/*Unqualified*/ true,
3385 /*DeductionGuide*/ true))
3386 goto DoneWithDeclSpec;
3387
Douglas Gregor9817f4a2009-02-09 15:09:02 +00003388 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003389 DiagID, TypeRep, Policy);
Chris Lattner16fac4f2008-07-26 01:18:38 +00003390 if (isInvalid)
3391 break;
Mike Stump11289f42009-09-09 15:08:12 +00003392
Chris Lattner16fac4f2008-07-26 01:18:38 +00003393 DS.SetRangeEnd(Tok.getLocation());
3394 ConsumeToken(); // The identifier
3395
Douglas Gregore9d95f12015-07-07 03:57:35 +00003396 // Objective-C supports type arguments and protocol references
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003397 // following an Objective-C object or object pointer
3398 // type. Handle either one of them.
Erik Pilkingtonfa983902018-10-30 20:31:30 +00003399 if (Tok.is(tok::less) && getLangOpts().ObjC) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003400 SourceLocation NewEndLoc;
3401 TypeResult NewTypeRep = parseObjCTypeArgsAndProtocolQualifiers(
3402 Loc, TypeRep, /*consumeLastToken=*/true,
3403 NewEndLoc);
3404 if (NewTypeRep.isUsable()) {
3405 DS.UpdateTypeRep(NewTypeRep.get());
3406 DS.SetRangeEnd(NewEndLoc);
3407 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00003408 }
Chad Rosierc1183952012-06-26 22:30:43 +00003409
Steve Naroffcd5e7822008-09-22 10:28:57 +00003410 // Need to support trailing type qualifiers (e.g. "id<p> const").
3411 // If a type specifier follows, it will be diagnosed elsewhere.
3412 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00003413 }
Douglas Gregor7f741122009-02-25 19:37:18 +00003414
3415 // type-name
3416 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00003417 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Richard Smithb23c5e82019-05-09 03:31:27 +00003418 if (TemplateId->Kind != TNK_Type_template &&
3419 TemplateId->Kind != TNK_Undeclared_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00003420 // This template-id does not refer to a type name, so we're
3421 // done with the type-specifiers.
3422 goto DoneWithDeclSpec;
3423 }
3424
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003425 // If we're in a context where the template-id could be a
3426 // constructor name or specialization, check whether this is a
3427 // constructor declaration.
Faisal Vali7db85c52017-12-31 00:06:40 +00003428 if (getLangOpts().CPlusPlus && DSContext == DeclSpecContext::DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00003429 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Richard Smith446161b2014-03-03 21:12:53 +00003430 isConstructorDeclarator(TemplateId->SS.isEmpty()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003431 goto DoneWithDeclSpec;
3432
Douglas Gregor7f741122009-02-25 19:37:18 +00003433 // Turn the template-id annotation token into a type annotation
3434 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00003435 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00003436 continue;
3437 }
3438
Chris Lattnere37e2332006-08-15 04:50:22 +00003439 // GNU attributes support.
3440 case tok::kw___attribute:
Craig Topper161e4db2014-05-21 06:02:52 +00003441 ParseGNUAttributes(DS.getAttributes(), nullptr, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00003442 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00003443
3444 // Microsoft declspec support.
3445 case tok::kw___declspec:
Aaron Ballman068aa512015-05-20 20:58:33 +00003446 ParseMicrosoftDeclSpecs(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00003447 continue;
Mike Stump11289f42009-09-09 15:08:12 +00003448
Steve Naroff44ac7772008-12-25 14:16:32 +00003449 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00003450 case tok::kw___forceinline: {
Serge Pavlov750db652013-11-13 06:57:53 +00003451 isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00003452 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00003453 SourceLocation AttrNameLoc = Tok.getLocation();
Craig Topper161e4db2014-05-21 06:02:52 +00003454 DS.getAttributes().addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc,
Erich Keanee891aa92018-07-13 15:07:47 +00003455 nullptr, 0, ParsedAttr::AS_Keyword);
Richard Smithda837032012-09-14 18:27:01 +00003456 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00003457 }
Eli Friedman53339e02009-06-08 23:27:34 +00003458
Andrey Bokhanko45d41322016-05-11 18:38:21 +00003459 case tok::kw___unaligned:
3460 isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID,
3461 getLangOpts());
3462 break;
3463
Aaron Ballman317a77f2013-05-22 23:25:32 +00003464 case tok::kw___sptr:
3465 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00003466 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003467 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00003468 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00003469 case tok::kw___cdecl:
3470 case tok::kw___stdcall:
3471 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003472 case tok::kw___thiscall:
Erich Keane757d3172016-11-02 18:29:35 +00003473 case tok::kw___regcall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00003474 case tok::kw___vectorcall:
John McCall53fa7142010-12-24 02:08:15 +00003475 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003476 continue;
3477
Dawn Perchik335e16b2010-09-03 01:29:35 +00003478 // Borland single token adornments.
3479 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00003480 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00003481 continue;
3482
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003483 // OpenCL single token adornments.
3484 case tok::kw___kernel:
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +00003485 ParseOpenCLKernelAttributes(DS.getAttributes());
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003486 continue;
3487
Douglas Gregor261a89b2015-06-19 17:51:05 +00003488 // Nullability type specifiers.
Douglas Gregoraea7afd2015-06-24 22:02:08 +00003489 case tok::kw__Nonnull:
3490 case tok::kw__Nullable:
3491 case tok::kw__Null_unspecified:
Douglas Gregor261a89b2015-06-19 17:51:05 +00003492 ParseNullabilityTypeSpecifiers(DS.getAttributes());
3493 continue;
3494
Douglas Gregorab209d82015-07-07 03:58:42 +00003495 // Objective-C 'kindof' types.
3496 case tok::kw___kindof:
3497 DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc,
Erich Keanee891aa92018-07-13 15:07:47 +00003498 nullptr, 0, ParsedAttr::AS_Keyword);
Douglas Gregorab209d82015-07-07 03:58:42 +00003499 (void)ConsumeToken();
3500 continue;
3501
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003502 // storage-class-specifier
3503 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003504 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003505 PrevSpec, DiagID, Policy);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003506 isStorageClass = true;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003507 break;
3508 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00003509 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00003510 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003511 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003512 PrevSpec, DiagID, Policy);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003513 isStorageClass = true;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003514 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00003515 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003516 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003517 Loc, PrevSpec, DiagID, Policy);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003518 isStorageClass = true;
Steve Naroff2050b0d2007-12-18 00:16:02 +00003519 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003520 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00003521 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00003522 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003523 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003524 PrevSpec, DiagID, Policy);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003525 isStorageClass = true;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003526 break;
3527 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003528 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003529 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003530 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003531 PrevSpec, DiagID, Policy);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003532 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00003533 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003534 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00003535 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003536 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003537 DiagID, Policy);
Richard Smith58c74332011-09-04 19:54:14 +00003538 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003539 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003540 PrevSpec, DiagID, Policy);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003541 isStorageClass = true;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003542 break;
Richard Smithe301ba22015-11-11 02:02:15 +00003543 case tok::kw___auto_type:
3544 Diag(Tok, diag::ext_auto_type);
3545 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto_type, Loc, PrevSpec,
3546 DiagID, Policy);
3547 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003548 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003549 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003550 PrevSpec, DiagID, Policy);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003551 isStorageClass = true;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003552 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00003553 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003554 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003555 PrevSpec, DiagID, Policy);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003556 isStorageClass = true;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00003557 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003558 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00003559 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
3560 PrevSpec, DiagID);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003561 isStorageClass = true;
Richard Smithb4a9e862013-04-12 22:46:28 +00003562 break;
3563 case tok::kw_thread_local:
3564 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
3565 PrevSpec, DiagID);
Sven van Haastregtc4100832018-04-24 14:47:29 +00003566 isStorageClass = true;
Richard Smithb4a9e862013-04-12 22:46:28 +00003567 break;
3568 case tok::kw__Thread_local:
3569 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
3570 Loc, PrevSpec, DiagID);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003571 isStorageClass = true;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003572 break;
Mike Stump11289f42009-09-09 15:08:12 +00003573
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003574 // function-specifier
3575 case tok::kw_inline:
Serge Pavlov750db652013-11-13 06:57:53 +00003576 isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003577 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00003578 case tok::kw_virtual:
Anastasia Stulova46b55fa2019-07-18 10:02:35 +00003579 // C++ for OpenCL does not allow virtual function qualifier, to avoid
3580 // function pointers restricted in OpenCL v2.0 s6.9.a.
Sven van Haastregt49ffffb2018-04-23 11:23:47 +00003581 if (getLangOpts().OpenCLCPlusPlus) {
3582 DiagID = diag::err_openclcxx_virtual_function;
3583 PrevSpec = Tok.getIdentifierInfo()->getNameStart();
3584 isInvalid = true;
3585 }
3586 else {
3587 isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
3588 }
Douglas Gregor61956c42008-10-31 09:07:45 +00003589 break;
Richard Smith76b90272019-05-09 03:59:21 +00003590 case tok::kw_explicit: {
3591 SourceLocation ExplicitLoc = Loc;
3592 SourceLocation CloseParenLoc;
3593 ExplicitSpecifier ExplicitSpec(nullptr, ExplicitSpecKind::ResolvedTrue);
Richard Smithba24f352019-05-09 19:45:46 +00003594 ConsumedEnd = ExplicitLoc;
Richard Smith76b90272019-05-09 03:59:21 +00003595 ConsumeToken(); // kw_explicit
3596 if (Tok.is(tok::l_paren)) {
3597 if (getLangOpts().CPlusPlus2a) {
3598 ExprResult ExplicitExpr(static_cast<Expr *>(nullptr));
3599 BalancedDelimiterTracker Tracker(*this, tok::l_paren);
3600 Tracker.consumeOpen();
3601 ExplicitExpr = ParseConstantExpression();
Richard Smithba24f352019-05-09 19:45:46 +00003602 ConsumedEnd = Tok.getLocation();
Richard Smith76b90272019-05-09 03:59:21 +00003603 if (ExplicitExpr.isUsable()) {
3604 CloseParenLoc = Tok.getLocation();
3605 Tracker.consumeClose();
3606 ExplicitSpec =
3607 Actions.ActOnExplicitBoolSpecifier(ExplicitExpr.get());
3608 } else
3609 Tracker.skipToEnd();
3610 } else
3611 Diag(Tok.getLocation(), diag::warn_cxx2a_compat_explicit_bool);
3612 }
3613 isInvalid = DS.setFunctionSpecExplicit(ExplicitLoc, PrevSpec, DiagID,
3614 ExplicitSpec, CloseParenLoc);
Douglas Gregor61956c42008-10-31 09:07:45 +00003615 break;
Richard Smith76b90272019-05-09 03:59:21 +00003616 }
Richard Smith0015f092013-01-17 22:16:11 +00003617 case tok::kw__Noreturn:
3618 if (!getLangOpts().C11)
3619 Diag(Loc, diag::ext_c11_noreturn);
Serge Pavlov750db652013-11-13 06:57:53 +00003620 isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
Richard Smith0015f092013-01-17 22:16:11 +00003621 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003622
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003623 // alignment-specifier
3624 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003625 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00003626 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003627 ParseAlignmentSpecifier(DS.getAttributes());
3628 continue;
3629
Anders Carlssoncd8db412009-05-06 04:46:28 +00003630 // friend
3631 case tok::kw_friend:
Faisal Vali7db85c52017-12-31 00:06:40 +00003632 if (DSContext == DeclSpecContext::DSC_class)
John McCall07e91c02009-08-06 02:15:43 +00003633 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
3634 else {
3635 PrevSpec = ""; // not actually used by the diagnostic
3636 DiagID = diag::err_friend_invalid_in_context;
3637 isInvalid = true;
3638 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00003639 break;
Mike Stump11289f42009-09-09 15:08:12 +00003640
Douglas Gregor26701a42011-09-09 02:06:17 +00003641 // Modules
3642 case tok::kw___module_private__:
3643 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
3644 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003645
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00003646 // constexpr
3647 case tok::kw_constexpr:
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003648 isInvalid = DS.SetConstexprSpec(CSK_constexpr, Loc, PrevSpec, DiagID);
3649 break;
3650
3651 // consteval
3652 case tok::kw_consteval:
3653 isInvalid = DS.SetConstexprSpec(CSK_consteval, Loc, PrevSpec, DiagID);
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00003654 break;
3655
Chris Lattnere387d9e2009-01-21 19:48:37 +00003656 // type-specifier
3657 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00003658 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003659 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003660 break;
3661 case tok::kw_long:
3662 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00003663 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003664 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003665 else
John McCall49bfce42009-08-03 20:12:06 +00003666 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003667 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003668 break;
Francois Pichet84133e42011-04-28 01:59:37 +00003669 case tok::kw___int64:
3670 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003671 DiagID, Policy);
Francois Pichet84133e42011-04-28 01:59:37 +00003672 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003673 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00003674 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
3675 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003676 break;
3677 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00003678 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
3679 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003680 break;
3681 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00003682 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
3683 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003684 break;
3685 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00003686 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
3687 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003688 break;
3689 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00003690 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003691 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003692 break;
3693 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00003694 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003695 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003696 break;
3697 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00003698 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003699 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003700 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00003701 case tok::kw___int128:
3702 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003703 DiagID, Policy);
Richard Smithf016bbc2012-04-04 06:24:32 +00003704 break;
3705 case tok::kw_half:
3706 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003707 DiagID, Policy);
Richard Smithf016bbc2012-04-04 06:24:32 +00003708 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003709 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00003710 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003711 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003712 break;
3713 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00003714 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003715 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003716 break;
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00003717 case tok::kw__Float16:
3718 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float16, Loc, PrevSpec,
3719 DiagID, Policy);
3720 break;
Leonard Chanf921d852018-06-04 16:07:52 +00003721 case tok::kw__Accum:
3722 if (!getLangOpts().FixedPoint) {
Leonard Chanab80f3c2018-06-14 14:53:51 +00003723 SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid);
Leonard Chanf921d852018-06-04 16:07:52 +00003724 } else {
3725 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_accum, Loc, PrevSpec,
3726 DiagID, Policy);
3727 }
3728 break;
Leonard Chanab80f3c2018-06-14 14:53:51 +00003729 case tok::kw__Fract:
3730 if (!getLangOpts().FixedPoint) {
3731 SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid);
3732 } else {
3733 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_fract, Loc, PrevSpec,
3734 DiagID, Policy);
3735 }
3736 break;
3737 case tok::kw__Sat:
3738 if (!getLangOpts().FixedPoint) {
3739 SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid);
3740 } else {
3741 isInvalid = DS.SetTypeSpecSat(Loc, PrevSpec, DiagID);
3742 }
3743 break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00003744 case tok::kw___float128:
3745 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float128, Loc, PrevSpec,
3746 DiagID, Policy);
3747 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003748 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00003749 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003750 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003751 break;
Richard Smith3a8244d2018-05-01 05:02:45 +00003752 case tok::kw_char8_t:
3753 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char8, Loc, PrevSpec,
3754 DiagID, Policy);
3755 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003756 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00003757 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003758 DiagID, Policy);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003759 break;
3760 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00003761 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003762 DiagID, Policy);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003763 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003764 case tok::kw_bool:
3765 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003766 if (Tok.is(tok::kw_bool) &&
3767 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
3768 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
3769 PrevSpec = ""; // Not used by the diagnostic.
3770 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003771 // For better error recovery.
3772 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003773 isInvalid = true;
3774 } else {
3775 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003776 DiagID, Policy);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003777 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003778 break;
3779 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00003780 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003781 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003782 break;
3783 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00003784 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003785 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003786 break;
3787 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00003788 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003789 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003790 break;
John Thompson22334602010-02-05 00:12:22 +00003791 case tok::kw___vector:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003792 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
John Thompson22334602010-02-05 00:12:22 +00003793 break;
3794 case tok::kw___pixel:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003795 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
John Thompson22334602010-02-05 00:12:22 +00003796 break;
Bill Seurercf2c96b2015-01-12 19:35:51 +00003797 case tok::kw___bool:
3798 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy);
3799 break;
Xiuli Pan9c14e282016-01-09 12:53:17 +00003800 case tok::kw_pipe:
Sven van Haastregte518bb42019-05-22 13:12:20 +00003801 if (!getLangOpts().OpenCL || (getLangOpts().OpenCLVersion < 200 &&
3802 !getLangOpts().OpenCLCPlusPlus)) {
Xiuli Pan9c14e282016-01-09 12:53:17 +00003803 // OpenCL 2.0 defined this keyword. OpenCL 1.2 and earlier should
3804 // support the "pipe" word as identifier.
3805 Tok.getIdentifierInfo()->revertTokenIDToIdentifier();
3806 goto DoneWithDeclSpec;
3807 }
3808 isInvalid = DS.SetTypePipe(true, Loc, PrevSpec, DiagID, Policy);
3809 break;
Alexey Bader954ba212016-04-08 13:40:33 +00003810#define GENERIC_IMAGE_TYPE(ImgType, Id) \
3811 case tok::kw_##ImgType##_t: \
3812 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_##ImgType##_t, Loc, PrevSpec, \
3813 DiagID, Policy); \
3814 break;
Alexey Baderb62f1442016-04-13 08:33:41 +00003815#include "clang/Basic/OpenCLImageTypes.def"
John McCall39439732011-04-09 22:50:59 +00003816 case tok::kw___unknown_anytype:
Faisal Vali090da2d2018-01-01 18:23:28 +00003817 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003818 PrevSpec, DiagID, Policy);
John McCall39439732011-04-09 22:50:59 +00003819 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003820
3821 // class-specifier:
3822 case tok::kw_class:
3823 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003824 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003825 case tok::kw_union: {
3826 tok::TokenKind Kind = Tok.getKind();
3827 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00003828
3829 // These are attributes following class specifiers.
3830 // To produce better diagnostic, we parse them when
3831 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00003832 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00003833 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00003834 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003835
3836 // If there are attributes following class specifier,
3837 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00003838 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00003839 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00003840 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003841 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003842 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003843 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003844
3845 // enum-specifier:
3846 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003847 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003848 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003849 continue;
Faisal Valia534f072018-04-26 00:42:40 +00003850
Chris Lattnere387d9e2009-01-21 19:48:37 +00003851 // cv-qualifier:
3852 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003853 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003854 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003855 break;
3856 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003857 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003858 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003859 break;
3860 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003861 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003862 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003863 break;
3864
Douglas Gregor333489b2009-03-27 23:10:48 +00003865 // C++ typename-specifier:
3866 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003867 if (TryAnnotateTypeOrScopeToken()) {
3868 DS.SetTypeSpecError();
3869 goto DoneWithDeclSpec;
3870 }
3871 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003872 continue;
3873 break;
3874
Chris Lattnere387d9e2009-01-21 19:48:37 +00003875 // GNU typeof support.
3876 case tok::kw_typeof:
3877 ParseTypeofSpecifier(DS);
3878 continue;
3879
David Blaikie15a430a2011-12-04 05:04:18 +00003880 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003881 ParseDecltypeSpecifier(DS);
3882 continue;
3883
David Majnemer15b311c2016-06-14 03:20:28 +00003884 case tok::annot_pragma_pack:
3885 HandlePragmaPack();
3886 continue;
3887
3888 case tok::annot_pragma_ms_pragma:
3889 HandlePragmaMSPragma();
3890 continue;
3891
3892 case tok::annot_pragma_ms_vtordisp:
3893 HandlePragmaMSVtorDisp();
3894 continue;
3895
3896 case tok::annot_pragma_ms_pointers_to_members:
3897 HandlePragmaMSPointersToMembers();
3898 continue;
3899
Alexis Hunt4a257072011-05-19 05:37:45 +00003900 case tok::kw___underlying_type:
3901 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003902 continue;
3903
3904 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003905 // C11 6.7.2.4/4:
3906 // If the _Atomic keyword is immediately followed by a left parenthesis,
3907 // it is interpreted as a type specifier (with a type name), not as a
3908 // type qualifier.
3909 if (NextToken().is(tok::l_paren)) {
3910 ParseAtomicSpecifier(DS);
3911 continue;
3912 }
3913 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3914 getLangOpts());
3915 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003916
Sven van Haastregt2ca6ba12018-05-09 13:16:17 +00003917 // OpenCL address space qualifiers:
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003918 case tok::kw___generic:
3919 // generic address space is introduced only in OpenCL v2.0
3920 // see OpenCL C Spec v2.0 s6.5.5
Sven van Haastregt2ca6ba12018-05-09 13:16:17 +00003921 if (Actions.getLangOpts().OpenCLVersion < 200 &&
3922 !Actions.getLangOpts().OpenCLCPlusPlus) {
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003923 DiagID = diag::err_opencl_unknown_type_specifier;
3924 PrevSpec = Tok.getIdentifierInfo()->getNameStart();
3925 isInvalid = true;
3926 break;
3927 };
Galina Kistanova77674252017-06-01 21:15:34 +00003928 LLVM_FALLTHROUGH;
Anastasia Stulova948e37c2019-03-25 11:54:02 +00003929 case tok::kw_private:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003930 case tok::kw___private:
3931 case tok::kw___global:
3932 case tok::kw___local:
3933 case tok::kw___constant:
Anastasia Stulova2c4730d2019-02-15 12:07:57 +00003934 // OpenCL access qualifiers:
3935 case tok::kw___read_only:
3936 case tok::kw___write_only:
3937 case tok::kw___read_write:
Aaron Ballman05d76ea2014-01-14 01:29:54 +00003938 ParseOpenCLQualifiers(DS.getAttributes());
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003939 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003940
Steve Naroffcfdf6162008-06-05 00:02:44 +00003941 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003942 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003943 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3944 // but we support it.
Erik Pilkingtonfa983902018-10-30 20:31:30 +00003945 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC)
Chris Lattner0974b232008-07-26 00:20:22 +00003946 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003947
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003948 SourceLocation StartLoc = Tok.getLocation();
3949 SourceLocation EndLoc;
3950 TypeResult Type = parseObjCProtocolQualifierType(EndLoc);
3951 if (Type.isUsable()) {
3952 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, StartLoc,
3953 PrevSpec, DiagID, Type.get(),
3954 Actions.getASTContext().getPrintingPolicy()))
3955 Diag(StartLoc, DiagID) << PrevSpec;
Fangrui Song6907ce22018-07-30 19:24:48 +00003956
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003957 DS.SetRangeEnd(EndLoc);
3958 } else {
3959 DS.SetTypeSpecError();
3960 }
Chad Rosierc1183952012-06-26 22:30:43 +00003961
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003962 // Need to support trailing type qualifiers (e.g. "id<p> const").
3963 // If a type specifier follows, it will be diagnosed elsewhere.
3964 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003965 }
Richard Smith76b90272019-05-09 03:59:21 +00003966
Richard Smithba24f352019-05-09 19:45:46 +00003967 DS.SetRangeEnd(ConsumedEnd.isValid() ? ConsumedEnd : Tok.getLocation());
Richard Smith76b90272019-05-09 03:59:21 +00003968
John McCall49bfce42009-08-03 20:12:06 +00003969 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003970 if (isInvalid) {
3971 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003972 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003973
Nick Desaulniers150ca532018-10-03 23:09:29 +00003974 if (DiagID == diag::ext_duplicate_declspec ||
Richard Smith76b90272019-05-09 03:59:21 +00003975 DiagID == diag::ext_warn_duplicate_declspec ||
3976 DiagID == diag::err_duplicate_declspec)
3977 Diag(Loc, DiagID) << PrevSpec
3978 << FixItHint::CreateRemoval(
3979 SourceRange(Loc, DS.getEndLoc()));
Anastasia Stulova43ab9a02016-05-12 16:28:25 +00003980 else if (DiagID == diag::err_opencl_unknown_type_specifier) {
Richard Smith76b90272019-05-09 03:59:21 +00003981 Diag(Loc, DiagID) << getLangOpts().OpenCLCPlusPlus
3982 << getLangOpts().getOpenCLVersionTuple().getAsString()
3983 << PrevSpec << isStorageClass;
Anastasia Stulova43ab9a02016-05-12 16:28:25 +00003984 } else
Richard Smith76b90272019-05-09 03:59:21 +00003985 Diag(Loc, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003986 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003987
Richard Smithba24f352019-05-09 19:45:46 +00003988 if (DiagID != diag::err_bool_redeclaration && ConsumedEnd.isInvalid())
Volodymyr Sapsai9f7b5cc2018-04-10 18:29:47 +00003989 // After an error the next token can be an annotation token.
3990 ConsumeAnyToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003991
3992 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003993 }
3994}
Douglas Gregoreb31f392008-12-01 23:54:00 +00003995
Chris Lattner70ae4912007-10-29 04:42:53 +00003996/// ParseStructDeclaration - Parse a struct declaration without the terminating
3997/// semicolon.
3998///
Nico Weber98dd0852019-03-14 14:18:56 +00003999/// Note that a struct declaration refers to a declaration in a struct,
4000/// not to the declaration of a struct.
4001///
Chris Lattner90a26b02007-01-23 04:38:16 +00004002/// struct-declaration:
Aaron Ballman606093a2017-10-15 15:01:42 +00004003/// [C2x] attributes-specifier-seq[opt]
4004/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00004005/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00004006/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00004007/// struct-declarator-list:
4008/// struct-declarator
4009/// struct-declarator-list ',' struct-declarator
4010/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
4011/// struct-declarator:
4012/// declarator
4013/// [GNU] declarator attributes[opt]
4014/// declarator[opt] ':' constant-expression
4015/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
4016///
Benjamin Kramera39beb92014-09-03 11:06:10 +00004017void Parser::ParseStructDeclaration(
4018 ParsingDeclSpec &DS,
4019 llvm::function_ref<void(ParsingFieldDeclarator &)> FieldsCallback) {
Chad Rosierc1183952012-06-26 22:30:43 +00004020
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00004021 if (Tok.is(tok::kw___extension__)) {
4022 // __extension__ silences extension warnings in the subexpression.
4023 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00004024 ConsumeToken();
Benjamin Kramera39beb92014-09-03 11:06:10 +00004025 return ParseStructDeclaration(DS, FieldsCallback);
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00004026 }
Mike Stump11289f42009-09-09 15:08:12 +00004027
Aaron Ballman606093a2017-10-15 15:01:42 +00004028 // Parse leading attributes.
4029 ParsedAttributesWithRange Attrs(AttrFactory);
4030 MaybeParseCXX11Attributes(Attrs);
4031 DS.takeAttributesFrom(Attrs);
4032
Steve Naroff97170802007-08-20 22:28:22 +00004033 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00004034 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00004035
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00004036 // If there are no declarators, this is a free-standing declaration
4037 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00004038 if (Tok.is(tok::semi)) {
Nico Weber7b837f52016-01-28 19:25:00 +00004039 RecordDecl *AnonRecord = nullptr;
Eli Friedman89b1f2c2012-08-08 23:04:35 +00004040 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Nico Weber7b837f52016-01-28 19:25:00 +00004041 DS, AnonRecord);
4042 assert(!AnonRecord && "Did not expect anonymous struct or union here");
Eli Friedman89b1f2c2012-08-08 23:04:35 +00004043 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00004044 return;
4045 }
4046
4047 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00004048 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00004049 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00004050 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00004051 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00004052 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00004053
Bill Wendling44426052012-12-20 19:22:21 +00004054 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00004055 if (!FirstDeclarator)
4056 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00004057
Steve Naroff97170802007-08-20 22:28:22 +00004058 /// struct-declarator: declarator
4059 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00004060 if (Tok.isNot(tok::colon)) {
4061 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
4062 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00004063 ParseDeclarator(DeclaratorInfo.D);
Richard Smith3d1a94c2014-08-12 00:22:39 +00004064 } else
4065 DeclaratorInfo.D.SetIdentifier(nullptr, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00004066
Alp Toker8fbec672013-12-17 23:29:36 +00004067 if (TryConsumeToken(tok::colon)) {
John McCalldadc5752010-08-24 06:29:42 +00004068 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00004069 if (Res.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00004070 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner32295d32008-04-10 06:15:14 +00004071 else
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004072 DeclaratorInfo.BitfieldSize = Res.get();
Steve Naroff97170802007-08-20 22:28:22 +00004073 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004074
Steve Naroff97170802007-08-20 22:28:22 +00004075 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00004076 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004077
John McCallcfefb6d2009-11-03 02:38:08 +00004078 // We're done with this declarator; invoke the callback.
Benjamin Kramera39beb92014-09-03 11:06:10 +00004079 FieldsCallback(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00004080
Steve Naroff97170802007-08-20 22:28:22 +00004081 // If we don't have a comma, it is either the end of the list (a ';')
4082 // or an error, bail out.
Alp Toker8fbec672013-12-17 23:29:36 +00004083 if (!TryConsumeToken(tok::comma, CommaLoc))
Chris Lattner70ae4912007-10-29 04:42:53 +00004084 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004085
John McCallcfefb6d2009-11-03 02:38:08 +00004086 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00004087 }
Steve Naroff97170802007-08-20 22:28:22 +00004088}
4089
4090/// ParseStructUnionBody
4091/// struct-contents:
4092/// struct-declaration-list
4093/// [EXT] empty
4094/// [GNU] "struct-declaration-list" without terminatoring ';'
4095/// struct-declaration-list:
4096/// struct-declaration
4097/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00004098/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00004099///
Chris Lattner1300fb92007-01-23 23:42:53 +00004100void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Faisal Vali090da2d2018-01-01 18:23:28 +00004101 unsigned TagType, Decl *TagDecl) {
Jordan Rose1e879d82018-03-23 00:07:18 +00004102 PrettyDeclStackTraceEntry CrashInfo(Actions.Context, TagDecl, RecordLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00004103 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00004104 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00004105
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004106 BalancedDelimiterTracker T(*this, tok::l_brace);
4107 if (T.consumeOpen())
4108 return;
Mike Stump11289f42009-09-09 15:08:12 +00004109
Douglas Gregor658b9552009-01-09 22:42:13 +00004110 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004111 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00004112
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004113 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00004114
Chris Lattner7b9ace62007-01-23 20:11:08 +00004115 // While we still have something to read, read the declarations in the struct.
Richard Smith752ada82015-11-17 23:32:01 +00004116 while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
4117 Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00004118 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00004119
Chris Lattner736ed5d2007-06-09 05:59:07 +00004120 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00004121 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00004122 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00004123 continue;
4124 }
Chris Lattnera12405b2008-04-10 06:46:29 +00004125
Andy Gibbsc804e082013-04-03 09:46:04 +00004126 // Parse _Static_assert declaration.
4127 if (Tok.is(tok::kw__Static_assert)) {
4128 SourceLocation DeclEnd;
4129 ParseStaticAssertDeclaration(DeclEnd);
4130 continue;
4131 }
4132
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00004133 if (Tok.is(tok::annot_pragma_pack)) {
4134 HandlePragmaPack();
4135 continue;
4136 }
4137
4138 if (Tok.is(tok::annot_pragma_align)) {
4139 HandlePragmaAlign();
4140 continue;
4141 }
4142
Alexey Bataev4652e4b2015-08-12 07:10:54 +00004143 if (Tok.is(tok::annot_pragma_openmp)) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00004144 // Result can be ignored, because it must be always empty.
4145 AccessSpecifier AS = AS_none;
4146 ParsedAttributesWithRange Attrs(AttrFactory);
4147 (void)ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs);
Alexey Bataev4652e4b2015-08-12 07:10:54 +00004148 continue;
4149 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00004150
John McCallcfefb6d2009-11-03 02:38:08 +00004151 if (!Tok.is(tok::at)) {
Benjamin Kramera39beb92014-09-03 11:06:10 +00004152 auto CFieldCallback = [&](ParsingFieldDeclarator &FD) {
4153 // Install the declarator into the current TagDecl.
4154 Decl *Field =
4155 Actions.ActOnField(getCurScope(), TagDecl,
4156 FD.D.getDeclSpec().getSourceRange().getBegin(),
4157 FD.D, FD.BitfieldSize);
4158 FieldDecls.push_back(Field);
4159 FD.complete(Field);
4160 };
John McCallcfefb6d2009-11-03 02:38:08 +00004161
Eli Friedman89b1f2c2012-08-08 23:04:35 +00004162 // Parse all the comma separated declarators.
4163 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00004164 ParseStructDeclaration(DS, CFieldCallback);
Chris Lattner535b8302008-06-21 19:39:06 +00004165 } else { // Handle @defs
4166 ConsumeToken();
4167 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
4168 Diag(Tok, diag::err_unexpected_at);
Alexey Bataevee6507d2013-11-18 08:17:37 +00004169 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00004170 continue;
4171 }
4172 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00004173 ExpectAndConsume(tok::l_paren);
Chris Lattner535b8302008-06-21 19:39:06 +00004174 if (!Tok.is(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00004175 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00004176 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00004177 continue;
4178 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004179 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00004180 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00004181 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00004182 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
4183 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00004184 ExpectAndConsume(tok::r_paren);
Mike Stump11289f42009-09-09 15:08:12 +00004185 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00004186
Alp Tokera3ebe6e2013-12-17 14:12:37 +00004187 if (TryConsumeToken(tok::semi))
4188 continue;
4189
4190 if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00004191 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00004192 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00004193 }
Alp Tokera3ebe6e2013-12-17 14:12:37 +00004194
4195 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
4196 // Skip to end of block or statement to avoid ext-warning on extra ';'.
4197 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
4198 // If we stopped at a ';', eat it.
4199 TryConsumeToken(tok::semi);
Chris Lattner90a26b02007-01-23 04:38:16 +00004200 }
Mike Stump11289f42009-09-09 15:08:12 +00004201
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004202 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00004203
John McCall084e83d2011-03-24 11:26:52 +00004204 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00004205 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00004206 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00004207
Erich Keanec480f302018-07-12 21:09:05 +00004208 Actions.ActOnFields(getCurScope(), RecordLoc, TagDecl, FieldDecls,
4209 T.getOpenLocation(), T.getCloseLocation(), attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00004210 StructScope.Exit();
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00004211 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, T.getRange());
Chris Lattner90a26b02007-01-23 04:38:16 +00004212}
4213
Chris Lattner3b561a32006-08-13 00:12:11 +00004214/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00004215/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00004216/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004217///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00004218/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
4219/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00004220/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
4221/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00004222/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00004223/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004224///
Richard Smith7d137e32012-03-23 03:33:32 +00004225/// [C++11] enum-head '{' enumerator-list[opt] '}'
4226/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00004227///
Richard Smith7d137e32012-03-23 03:33:32 +00004228/// enum-head: [C++11]
4229/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
4230/// enum-key attribute-specifier-seq[opt] nested-name-specifier
4231/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00004232///
Richard Smith7d137e32012-03-23 03:33:32 +00004233/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00004234/// 'enum'
4235/// 'enum' 'class'
4236/// 'enum' 'struct'
4237///
Richard Smith7d137e32012-03-23 03:33:32 +00004238/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00004239/// ':' type-specifier-seq
4240///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004241/// [C++] elaborated-type-specifier:
4242/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
4243///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00004244void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00004245 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00004246 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00004247 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00004248 if (Tok.is(tok::code_completion)) {
4249 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00004250 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004251 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00004252 }
John McCallcb432fa2011-07-06 05:58:41 +00004253
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00004254 // If attributes exist after tag, parse them.
4255 ParsedAttributesWithRange attrs(AttrFactory);
4256 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00004257 MaybeParseCXX11Attributes(attrs);
Aaron Ballman068aa512015-05-20 20:58:33 +00004258 MaybeParseMicrosoftDeclSpecs(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00004259
Richard Smith0f8ee222012-01-10 01:33:14 +00004260 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00004261 bool IsScopedUsingClassTag = false;
4262
John McCallbeae29a2012-06-23 22:30:04 +00004263 // In C++11, recognize 'enum class' and 'enum struct'.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00004264 if (Tok.isOneOf(tok::kw_class, tok::kw_struct)) {
Richard Trieud0d87b52013-04-23 02:47:36 +00004265 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
4266 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00004267 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00004268 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00004269
Bill Wendling44426052012-12-20 19:22:21 +00004270 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00004271 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00004272 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00004273
4274 // They are allowed afterwards, though.
4275 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00004276 MaybeParseCXX11Attributes(attrs);
Aaron Ballman068aa512015-05-20 20:58:33 +00004277 MaybeParseMicrosoftDeclSpecs(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00004278 }
Richard Smith7d137e32012-03-23 03:33:32 +00004279
John McCall6347b682012-05-07 06:16:58 +00004280 // C++11 [temp.explicit]p12:
4281 // The usual access controls do not apply to names used to specify
4282 // explicit instantiations.
4283 // We extend this to also cover explicit specializations. Note that
4284 // we don't suppress if this turns out to be an elaborated type
4285 // specifier.
4286 bool shouldDelayDiagsInTag =
4287 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
4288 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
4289 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00004290
Richard Smithbfdb1082012-03-12 08:56:40 +00004291 // Enum definitions should not be parsed in a trailing-return-type.
Faisal Vali7db85c52017-12-31 00:06:40 +00004292 bool AllowDeclaration = DSC != DeclSpecContext::DSC_trailing;
Richard Smithbfdb1082012-03-12 08:56:40 +00004293
Abramo Bagnarad7548482010-05-19 21:37:53 +00004294 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004295 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00004296 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
4297 // if a fixed underlying type is allowed.
Erik Pilkington6f11db12018-09-28 20:24:58 +00004298 ColonProtectionRAIIObject X(*this, AllowDeclaration);
Chad Rosierc1183952012-06-26 22:30:43 +00004299
Nico Webercfaa4cd2015-02-15 07:26:13 +00004300 CXXScopeSpec Spec;
David Blaikieefdccaa2016-01-15 23:43:34 +00004301 if (ParseOptionalCXXScopeSpecifier(Spec, nullptr,
Richard Smith1d4b2e12013-04-01 21:43:41 +00004302 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00004303 return;
4304
Nico Webercfaa4cd2015-02-15 07:26:13 +00004305 if (Spec.isSet() && Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00004306 Diag(Tok, diag::err_expected) << tok::identifier;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004307 if (Tok.isNot(tok::l_brace)) {
4308 // Has no name and is not a definition.
4309 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00004310 SkipUntil(tok::comma, StopAtSemi);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004311 return;
4312 }
4313 }
Nico Webercfaa4cd2015-02-15 07:26:13 +00004314
4315 SS = Spec;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004316 }
Mike Stump11289f42009-09-09 15:08:12 +00004317
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00004318 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00004319 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Erik Pilkington6f11db12018-09-28 20:24:58 +00004320 !(AllowDeclaration && Tok.is(tok::colon))) {
Alp Tokerec543272013-12-24 09:48:30 +00004321 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
Mike Stump11289f42009-09-09 15:08:12 +00004322
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00004323 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00004324 SkipUntil(tok::comma, StopAtSemi);
Chris Lattner3b561a32006-08-13 00:12:11 +00004325 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00004326 }
Mike Stump11289f42009-09-09 15:08:12 +00004327
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00004328 // If an identifier is present, consume and remember it.
Craig Topper161e4db2014-05-21 06:02:52 +00004329 IdentifierInfo *Name = nullptr;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00004330 SourceLocation NameLoc;
4331 if (Tok.is(tok::identifier)) {
4332 Name = Tok.getIdentifierInfo();
4333 NameLoc = ConsumeToken();
4334 }
Mike Stump11289f42009-09-09 15:08:12 +00004335
Richard Smith0f8ee222012-01-10 01:33:14 +00004336 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00004337 // C++0x 7.2p2: The optional identifier shall not be omitted in the
4338 // declaration of a scoped enumeration.
4339 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00004340 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00004341 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00004342 }
4343
John McCall6347b682012-05-07 06:16:58 +00004344 // Okay, end the suppression area. We'll decide whether to emit the
4345 // diagnostics in a second.
4346 if (shouldDelayDiagsInTag)
4347 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00004348
Douglas Gregor0bf31402010-10-08 23:50:27 +00004349 TypeResult BaseType;
4350
Douglas Gregord1f69f62010-12-01 17:42:47 +00004351 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00004352 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Erik Pilkington6f11db12018-09-28 20:24:58 +00004353 if (AllowDeclaration && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00004354 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00004355 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00004356 // If we're in class scope, this can either be an enum declaration with
4357 // an underlying type, or a declaration of a bitfield member. We try to
4358 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00004359 // (integer literal, sizeof); if it's still ambiguous, we then consider
4360 // anything that's a simple-type-specifier followed by '(' as an
4361 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00004362 // underlying types anyway.
Faisal Valid143a0c2017-04-01 21:30:49 +00004363 EnterExpressionEvaluationContext Unevaluated(
4364 Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00004365 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00004366 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00004367 // bit-field. This is the common case.
Richard Smithee390432014-05-16 01:56:53 +00004368 if (TPR == TPResult::True)
Douglas Gregord1f69f62010-12-01 17:42:47 +00004369 PossibleBitfield = true;
4370 // If the next token starts a type-specifier-seq, it may be either a
4371 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00004372 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00004373 // fixed underlying type.
Richard Smithee390432014-05-16 01:56:53 +00004374 else if (TPR == TPResult::False &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00004375 GetLookAheadToken(2).getKind() == tok::semi) {
4376 // Consume the ':'.
4377 ConsumeToken();
4378 } else {
4379 // We have the start of a type-specifier-seq, so we have to perform
4380 // tentative parsing to determine whether we have an expression or a
4381 // type.
4382 TentativeParsingAction TPA(*this);
4383
4384 // Consume the ':'.
4385 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00004386
4387 // If we see a type specifier followed by an open-brace, we have an
4388 // ambiguity between an underlying type and a C++11 braced
4389 // function-style cast. Resolve this by always treating it as an
4390 // underlying type.
4391 // FIXME: The standard is not entirely clear on how to disambiguate in
4392 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004393 if ((getLangOpts().CPlusPlus &&
Richard Smithee390432014-05-16 01:56:53 +00004394 isCXXDeclarationSpecifier(TPResult::True) != TPResult::True) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00004395 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00004396 // We'll parse this as a bitfield later.
4397 PossibleBitfield = true;
4398 TPA.Revert();
4399 } else {
4400 // We have a type-specifier-seq.
4401 TPA.Commit();
4402 }
4403 }
4404 } else {
4405 // Consume the ':'.
4406 ConsumeToken();
4407 }
4408
4409 if (!PossibleBitfield) {
4410 SourceRange Range;
4411 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00004412
Erik Pilkingtonfa983902018-10-30 20:31:30 +00004413 if (!getLangOpts().ObjC) {
Erik Pilkington6f11db12018-09-28 20:24:58 +00004414 if (getLangOpts().CPlusPlus11)
4415 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
4416 else if (getLangOpts().CPlusPlus)
4417 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type);
4418 else if (getLangOpts().MicrosoftExt)
4419 Diag(StartLoc, diag::ext_ms_c_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00004420 else
Erik Pilkington6f11db12018-09-28 20:24:58 +00004421 Diag(StartLoc, diag::ext_clang_c_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00004422 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00004423 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00004424 }
4425
Richard Smith0f8ee222012-01-10 01:33:14 +00004426 // There are four options here. If we have 'friend enum foo;' then this is a
4427 // friend declaration, and cannot have an accompanying definition. If we have
4428 // 'enum foo;', then this is a forward declaration. If we have
4429 // 'enum foo {...' then this is a definition. Otherwise we have something
4430 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00004431 //
4432 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
4433 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
4434 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
4435 //
John McCallfaf5fb42010-08-26 23:41:50 +00004436 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00004437 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00004438 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00004439 } else if (Tok.is(tok::l_brace)) {
4440 if (DS.isFriendSpecified()) {
4441 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
4442 << SourceRange(DS.getFriendSpecLoc());
4443 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00004444 SkipUntil(tok::r_brace, StopAtSemi);
John McCall6347b682012-05-07 06:16:58 +00004445 TUK = Sema::TUK_Friend;
4446 } else {
4447 TUK = Sema::TUK_Definition;
4448 }
Richard Smith649c7b062014-01-08 00:56:48 +00004449 } else if (!isTypeSpecifier(DSC) &&
Richard Smith369b9f92012-06-25 21:37:02 +00004450 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00004451 (Tok.isAtStartOfLine() &&
4452 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00004453 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
4454 if (Tok.isNot(tok::semi)) {
4455 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00004456 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Ilya Biryukov929af672019-05-17 09:32:05 +00004457 PP.EnterToken(Tok, /*IsReinject=*/true);
Richard Smith369b9f92012-06-25 21:37:02 +00004458 Tok.setKind(tok::semi);
4459 }
John McCall6347b682012-05-07 06:16:58 +00004460 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00004461 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00004462 }
4463
4464 // If this is an elaborated type specifier, and we delayed
4465 // diagnostics before, just merge them into the current pool.
4466 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
4467 diagsFromTag.redelay();
4468 }
Richard Smith7d137e32012-03-23 03:33:32 +00004469
4470 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00004471 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00004472 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004473 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00004474 // Skip the rest of this declarator, up until the comma or semicolon.
4475 Diag(Tok, diag::err_enum_template);
Alexey Bataevee6507d2013-11-18 08:17:37 +00004476 SkipUntil(tok::comma, StopAtSemi);
Richard Smith7d137e32012-03-23 03:33:32 +00004477 return;
4478 }
4479
4480 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
4481 // Enumerations can't be explicitly instantiated.
4482 DS.SetTypeSpecError();
4483 Diag(StartLoc, diag::err_explicit_instantiation_enum);
4484 return;
4485 }
4486
4487 assert(TemplateInfo.TemplateParams && "no template parameters");
4488 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
4489 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00004490 }
Chad Rosierc1183952012-06-26 22:30:43 +00004491
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00004492 if (TUK == Sema::TUK_Reference)
4493 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00004494
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00004495 if (!Name && TUK != Sema::TUK_Definition) {
4496 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00004497
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00004498 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00004499 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00004500 return;
4501 }
Richard Smith7d137e32012-03-23 03:33:32 +00004502
Nico Weber32a0fc72016-09-03 03:01:32 +00004503 stripTypeAttributesOffDeclSpec(attrs, DS, TUK);
David Majnemer936b4112015-04-19 07:53:29 +00004504
Richard Smithd9ba2242015-05-07 03:54:19 +00004505 Sema::SkipBodyInfo SkipBody;
4506 if (!Name && TUK == Sema::TUK_Definition && Tok.is(tok::l_brace) &&
4507 NextToken().is(tok::identifier))
4508 SkipBody = Actions.shouldSkipAnonEnumBody(getCurScope(),
4509 NextToken().getIdentifierInfo(),
4510 NextToken().getLocation());
4511
Douglas Gregord6ab8742009-05-28 23:31:59 +00004512 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00004513 bool IsDependent = false;
Craig Topper161e4db2014-05-21 06:02:52 +00004514 const char *PrevSpec = nullptr;
Douglas Gregorba41d012010-04-24 16:38:41 +00004515 unsigned DiagID;
Faisal Vali7db85c52017-12-31 00:06:40 +00004516 Decl *TagDecl = Actions.ActOnTag(
4517 getCurScope(), DeclSpec::TST_enum, TUK, StartLoc, SS, Name, NameLoc,
Erich Keanec480f302018-07-12 21:09:05 +00004518 attrs, AS, DS.getModulePrivateSpecLoc(), TParams, Owned, IsDependent,
4519 ScopedEnumKWLoc, IsScopedUsingClassTag, BaseType,
Faisal Vali7db85c52017-12-31 00:06:40 +00004520 DSC == DeclSpecContext::DSC_type_specifier,
4521 DSC == DeclSpecContext::DSC_template_param ||
4522 DSC == DeclSpecContext::DSC_template_type_arg,
4523 &SkipBody);
Richard Smithd9ba2242015-05-07 03:54:19 +00004524
4525 if (SkipBody.ShouldSkip) {
4526 assert(TUK == Sema::TUK_Definition && "can only skip a definition");
4527
4528 BalancedDelimiterTracker T(*this, tok::l_brace);
4529 T.consumeOpen();
4530 T.skipToEnd();
4531
4532 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
4533 NameLoc.isValid() ? NameLoc : StartLoc,
4534 PrevSpec, DiagID, TagDecl, Owned,
4535 Actions.getASTContext().getPrintingPolicy()))
4536 Diag(StartLoc, DiagID) << PrevSpec;
4537 return;
4538 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00004539
Douglas Gregorba41d012010-04-24 16:38:41 +00004540 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00004541 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00004542 // dependent tag.
4543 if (!Name) {
4544 DS.SetTypeSpecError();
4545 Diag(Tok, diag::err_expected_type_name_after_typename);
4546 return;
4547 }
Chad Rosierc1183952012-06-26 22:30:43 +00004548
Nico Weber83ea0122014-05-03 21:57:40 +00004549 TypeResult Type = Actions.ActOnDependentTag(
4550 getCurScope(), DeclSpec::TST_enum, TUK, SS, Name, StartLoc, NameLoc);
Douglas Gregorba41d012010-04-24 16:38:41 +00004551 if (Type.isInvalid()) {
4552 DS.SetTypeSpecError();
4553 return;
4554 }
Chad Rosierc1183952012-06-26 22:30:43 +00004555
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00004556 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
4557 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00004558 PrevSpec, DiagID, Type.get(),
4559 Actions.getASTContext().getPrintingPolicy()))
Douglas Gregorba41d012010-04-24 16:38:41 +00004560 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00004561
Douglas Gregorba41d012010-04-24 16:38:41 +00004562 return;
4563 }
Mike Stump11289f42009-09-09 15:08:12 +00004564
John McCall48871652010-08-21 09:40:31 +00004565 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00004566 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00004567 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00004568 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00004569 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00004570 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregorba41d012010-04-24 16:38:41 +00004571 }
Chad Rosierc1183952012-06-26 22:30:43 +00004572
Douglas Gregorba41d012010-04-24 16:38:41 +00004573 DS.SetTypeSpecError();
4574 return;
4575 }
Richard Smith0f8ee222012-01-10 01:33:14 +00004576
Bruno Cardoso Lopesdf0ee342017-07-01 00:06:47 +00004577 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
4578 Decl *D = SkipBody.CheckSameAsPrevious ? SkipBody.New : TagDecl;
4579 ParseEnumBody(StartLoc, D);
4580 if (SkipBody.CheckSameAsPrevious &&
4581 !Actions.ActOnDuplicateDefinition(DS, TagDecl, SkipBody)) {
4582 DS.SetTypeSpecError();
4583 return;
4584 }
4585 }
Mike Stump11289f42009-09-09 15:08:12 +00004586
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00004587 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
4588 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00004589 PrevSpec, DiagID, TagDecl, Owned,
4590 Actions.getASTContext().getPrintingPolicy()))
John McCall49bfce42009-08-03 20:12:06 +00004591 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00004592}
4593
Chris Lattnerc1915e22007-01-25 07:29:02 +00004594/// ParseEnumBody - Parse a {} enclosed enumerator-list.
4595/// enumerator-list:
4596/// enumerator
4597/// enumerator-list ',' enumerator
4598/// enumerator:
Aaron Ballman730476b2014-11-08 15:33:35 +00004599/// enumeration-constant attributes[opt]
4600/// enumeration-constant attributes[opt] '=' constant-expression
Chris Lattnerc1915e22007-01-25 07:29:02 +00004601/// enumeration-constant:
4602/// identifier
4603///
John McCall48871652010-08-21 09:40:31 +00004604void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00004605 // Enter the scope of the enum body and start the definition.
Hans Wennborgfe781452014-06-17 00:00:18 +00004606 ParseScope EnumScope(this, Scope::DeclScope | Scope::EnumScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004607 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00004608
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004609 BalancedDelimiterTracker T(*this, tok::l_brace);
4610 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00004611
Chris Lattner37256fb2007-08-27 17:24:30 +00004612 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00004613 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Richard Smithf8812672016-12-02 22:38:31 +00004614 Diag(Tok, diag::err_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00004615
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004616 SmallVector<Decl *, 32> EnumConstantDecls;
Jordan Rose60ac3162015-04-30 17:20:30 +00004617 SmallVector<SuppressAccessChecks, 32> EnumAvailabilityDiags;
Chris Lattnerc1915e22007-01-25 07:29:02 +00004618
Craig Topper161e4db2014-05-21 06:02:52 +00004619 Decl *LastEnumConstDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00004620
Chris Lattnerc1915e22007-01-25 07:29:02 +00004621 // Parse the enumerator-list.
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00004622 while (Tok.isNot(tok::r_brace)) {
4623 // Parse enumerator. If failed, try skipping till the start of the next
4624 // enumerator definition.
4625 if (Tok.isNot(tok::identifier)) {
4626 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
4627 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) &&
4628 TryConsumeToken(tok::comma))
4629 continue;
4630 break;
4631 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00004632 IdentifierInfo *Ident = Tok.getIdentifierInfo();
4633 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004634
John McCall811a0f52010-10-22 23:36:17 +00004635 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00004636 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004637 MaybeParseGNUAttributes(attrs);
Aaron Ballman730476b2014-11-08 15:33:35 +00004638 ProhibitAttributes(attrs); // GNU-style attributes are prohibited.
Aaron Ballman606093a2017-10-15 15:01:42 +00004639 if (standardAttributesAllowed() && isCXX11AttributeSpecifier()) {
4640 if (getLangOpts().CPlusPlus)
Aaron Ballmanc351fba2017-12-04 20:27:34 +00004641 Diag(Tok.getLocation(), getLangOpts().CPlusPlus17
Aaron Ballman606093a2017-10-15 15:01:42 +00004642 ? diag::warn_cxx14_compat_ns_enum_attribute
4643 : diag::ext_ns_enum_attribute)
4644 << 1 /*enumerator*/;
Aaron Ballman730476b2014-11-08 15:33:35 +00004645 ParseCXX11Attributes(attrs);
4646 }
John McCall811a0f52010-10-22 23:36:17 +00004647
Chris Lattnerc1915e22007-01-25 07:29:02 +00004648 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00004649 ExprResult AssignedVal;
Jordan Rose60ac3162015-04-30 17:20:30 +00004650 EnumAvailabilityDiags.emplace_back(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00004651
Alp Tokera3ebe6e2013-12-17 14:12:37 +00004652 if (TryConsumeToken(tok::equal, EqualLoc)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00004653 AssignedVal = ParseConstantExpression();
4654 if (AssignedVal.isInvalid())
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00004655 SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch);
Chris Lattnerc1915e22007-01-25 07:29:02 +00004656 }
Mike Stump11289f42009-09-09 15:08:12 +00004657
Chris Lattnerc1915e22007-01-25 07:29:02 +00004658 // Install the enumerator constant into EnumDecl.
Bruno Cardoso Lopesdf0ee342017-07-01 00:06:47 +00004659 Decl *EnumConstDecl = Actions.ActOnEnumConstant(
Erich Keanec480f302018-07-12 21:09:05 +00004660 getCurScope(), EnumDecl, LastEnumConstDecl, IdentLoc, Ident, attrs,
4661 EqualLoc, AssignedVal.get());
Jordan Rose60ac3162015-04-30 17:20:30 +00004662 EnumAvailabilityDiags.back().done();
Chad Rosierc1183952012-06-26 22:30:43 +00004663
Chris Lattner4ef40012007-06-11 01:28:17 +00004664 EnumConstantDecls.push_back(EnumConstDecl);
4665 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00004666
Douglas Gregorce66d022010-09-07 14:51:08 +00004667 if (Tok.is(tok::identifier)) {
4668 // We're missing a comma between enumerators.
Richard Smithbdb84f32016-07-22 23:36:59 +00004669 SourceLocation Loc = getEndOfPreviousToken();
Chad Rosierc1183952012-06-26 22:30:43 +00004670 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00004671 << FixItHint::CreateInsertion(Loc, ", ");
4672 continue;
4673 }
Chad Rosierc1183952012-06-26 22:30:43 +00004674
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00004675 // Emumerator definition must be finished, only comma or r_brace are
4676 // allowed here.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00004677 SourceLocation CommaLoc;
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00004678 if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) {
4679 if (EqualLoc.isValid())
4680 Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace
4681 << tok::comma;
4682 else
4683 Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator);
4684 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) {
4685 if (TryConsumeToken(tok::comma, CommaLoc))
4686 continue;
4687 } else {
4688 break;
4689 }
4690 }
Mike Stump11289f42009-09-09 15:08:12 +00004691
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00004692 // If comma is followed by r_brace, emit appropriate warning.
4693 if (Tok.is(tok::r_brace) && CommaLoc.isValid()) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004694 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00004695 Diag(CommaLoc, getLangOpts().CPlusPlus ?
4696 diag::ext_enumerator_list_comma_cxx :
4697 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00004698 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004699 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00004700 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
4701 << FixItHint::CreateRemoval(CommaLoc);
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00004702 break;
Richard Smith5d164bc2011-10-15 05:09:34 +00004703 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00004704 }
Mike Stump11289f42009-09-09 15:08:12 +00004705
Chris Lattnerc1915e22007-01-25 07:29:02 +00004706 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004707 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00004708
Chris Lattnerc1915e22007-01-25 07:29:02 +00004709 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00004710 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004711 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00004712
Erich Keanec480f302018-07-12 21:09:05 +00004713 Actions.ActOnEnumBody(StartLoc, T.getRange(), EnumDecl, EnumConstantDecls,
4714 getCurScope(), attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004715
Jordan Rose60ac3162015-04-30 17:20:30 +00004716 // Now handle enum constant availability diagnostics.
4717 assert(EnumConstantDecls.size() == EnumAvailabilityDiags.size());
4718 for (size_t i = 0, e = EnumConstantDecls.size(); i != e; ++i) {
4719 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
4720 EnumAvailabilityDiags[i].redelay();
4721 PD.complete(EnumConstantDecls[i]);
4722 }
4723
Douglas Gregor82ac25e2009-01-08 20:45:30 +00004724 EnumScope.Exit();
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00004725 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, T.getRange());
Richard Smith369b9f92012-06-25 21:37:02 +00004726
4727 // The next token must be valid after an enum definition. If not, a ';'
4728 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00004729 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
4730 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Alp Toker383d2c42014-01-01 03:08:43 +00004731 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Richard Smith369b9f92012-06-25 21:37:02 +00004732 // Push this token back into the preprocessor and change our current token
4733 // to ';' so that the rest of the code recovers as though there were an
4734 // ';' after the definition.
Ilya Biryukov929af672019-05-17 09:32:05 +00004735 PP.EnterToken(Tok, /*IsReinject=*/true);
Richard Smith369b9f92012-06-25 21:37:02 +00004736 Tok.setKind(tok::semi);
4737 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00004738}
Chris Lattner3b561a32006-08-13 00:12:11 +00004739
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004740/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
4741/// is definitely a type-specifier. Return false if it isn't part of a type
4742/// specifier or if we're not sure.
4743bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
4744 switch (Tok.getKind()) {
4745 default: return false;
4746 // type-specifiers
4747 case tok::kw_short:
4748 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004749 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004750 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004751 case tok::kw_signed:
4752 case tok::kw_unsigned:
4753 case tok::kw__Complex:
4754 case tok::kw__Imaginary:
4755 case tok::kw_void:
4756 case tok::kw_char:
4757 case tok::kw_wchar_t:
Richard Smith3a8244d2018-05-01 05:02:45 +00004758 case tok::kw_char8_t:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004759 case tok::kw_char16_t:
4760 case tok::kw_char32_t:
4761 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004762 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004763 case tok::kw_float:
4764 case tok::kw_double:
Leonard Chanf921d852018-06-04 16:07:52 +00004765 case tok::kw__Accum:
Leonard Chanab80f3c2018-06-14 14:53:51 +00004766 case tok::kw__Fract:
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00004767 case tok::kw__Float16:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00004768 case tok::kw___float128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004769 case tok::kw_bool:
4770 case tok::kw__Bool:
4771 case tok::kw__Decimal32:
4772 case tok::kw__Decimal64:
4773 case tok::kw__Decimal128:
4774 case tok::kw___vector:
Alexey Bader954ba212016-04-08 13:40:33 +00004775#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
Alexey Baderb62f1442016-04-13 08:33:41 +00004776#include "clang/Basic/OpenCLImageTypes.def"
Chad Rosierc1183952012-06-26 22:30:43 +00004777
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004778 // struct-or-union-specifier (C99) or class-specifier (C++)
4779 case tok::kw_class:
4780 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004781 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004782 case tok::kw_union:
4783 // enum-specifier
4784 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00004785
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004786 // typedef-name
4787 case tok::annot_typename:
4788 return true;
4789 }
4790}
4791
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004792/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004793/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004794bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004795 switch (Tok.getKind()) {
4796 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004797
Chris Lattner020bab92009-01-04 23:41:41 +00004798 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00004799 if (TryAltiVecVectorToken())
4800 return true;
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +00004801 LLVM_FALLTHROUGH;
Douglas Gregor333489b2009-03-27 23:10:48 +00004802 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004803 // Annotate typenames and C++ scope specifiers. If we get one, just
4804 // recurse to handle whatever we get.
4805 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004806 return true;
4807 if (Tok.is(tok::identifier))
4808 return false;
4809 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00004810
Chris Lattner020bab92009-01-04 23:41:41 +00004811 case tok::coloncolon: // ::foo::bar
4812 if (NextToken().is(tok::kw_new) || // ::new
4813 NextToken().is(tok::kw_delete)) // ::delete
4814 return false;
4815
Chris Lattner020bab92009-01-04 23:41:41 +00004816 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004817 return true;
4818 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00004819
Chris Lattnere37e2332006-08-15 04:50:22 +00004820 // GNU attributes support.
4821 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00004822 // GNU typeof support.
4823 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004824
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004825 // type-specifiers
4826 case tok::kw_short:
4827 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004828 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004829 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004830 case tok::kw_signed:
4831 case tok::kw_unsigned:
4832 case tok::kw__Complex:
4833 case tok::kw__Imaginary:
4834 case tok::kw_void:
4835 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004836 case tok::kw_wchar_t:
Richard Smith3a8244d2018-05-01 05:02:45 +00004837 case tok::kw_char8_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004838 case tok::kw_char16_t:
4839 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004840 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004841 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004842 case tok::kw_float:
4843 case tok::kw_double:
Leonard Chanf921d852018-06-04 16:07:52 +00004844 case tok::kw__Accum:
Leonard Chanab80f3c2018-06-14 14:53:51 +00004845 case tok::kw__Fract:
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00004846 case tok::kw__Float16:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00004847 case tok::kw___float128:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004848 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004849 case tok::kw__Bool:
4850 case tok::kw__Decimal32:
4851 case tok::kw__Decimal64:
4852 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004853 case tok::kw___vector:
Alexey Bader954ba212016-04-08 13:40:33 +00004854#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
Alexey Baderb62f1442016-04-13 08:33:41 +00004855#include "clang/Basic/OpenCLImageTypes.def"
Mike Stump11289f42009-09-09 15:08:12 +00004856
Chris Lattner861a2262008-04-13 18:59:07 +00004857 // struct-or-union-specifier (C99) or class-specifier (C++)
4858 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004859 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004860 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004861 case tok::kw_union:
4862 // enum-specifier
4863 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004864
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004865 // type-qualifier
4866 case tok::kw_const:
4867 case tok::kw_volatile:
4868 case tok::kw_restrict:
Leonard Chanab80f3c2018-06-14 14:53:51 +00004869 case tok::kw__Sat:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004870
John McCallea0a39e2012-11-14 00:49:39 +00004871 // Debugger support.
4872 case tok::kw___unknown_anytype:
4873
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004874 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00004875 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004876 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004877
Chris Lattner409bf7d2008-10-20 00:25:30 +00004878 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4879 case tok::less:
Erik Pilkingtonfa983902018-10-30 20:31:30 +00004880 return getLangOpts().ObjC;
Mike Stump11289f42009-09-09 15:08:12 +00004881
Steve Naroff44ac7772008-12-25 14:16:32 +00004882 case tok::kw___cdecl:
4883 case tok::kw___stdcall:
4884 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004885 case tok::kw___thiscall:
Erich Keane757d3172016-11-02 18:29:35 +00004886 case tok::kw___regcall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00004887 case tok::kw___vectorcall:
Eli Friedman53339e02009-06-08 23:27:34 +00004888 case tok::kw___w64:
4889 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004890 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004891 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004892 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004893
Douglas Gregoraea7afd2015-06-24 22:02:08 +00004894 case tok::kw__Nonnull:
4895 case tok::kw__Nullable:
4896 case tok::kw__Null_unspecified:
Douglas Gregor261a89b2015-06-19 17:51:05 +00004897
Douglas Gregorab209d82015-07-07 03:58:42 +00004898 case tok::kw___kindof:
4899
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004900 case tok::kw___private:
4901 case tok::kw___local:
4902 case tok::kw___global:
4903 case tok::kw___constant:
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00004904 case tok::kw___generic:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004905 case tok::kw___read_only:
4906 case tok::kw___read_write:
4907 case tok::kw___write_only:
Eli Friedman53339e02009-06-08 23:27:34 +00004908 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004909
Anastasia Stulova314fab62019-03-28 11:47:14 +00004910 case tok::kw_private:
4911 return getLangOpts().OpenCL;
4912
Richard Smith8e1ac332013-03-28 01:55:44 +00004913 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004914 case tok::kw__Atomic:
4915 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004916 }
4917}
4918
Chris Lattneracd58a32006-08-06 17:24:14 +00004919/// isDeclarationSpecifier() - Return true if the current token is part of a
4920/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004921///
4922/// \param DisambiguatingWithExpression True to indicate that the purpose of
4923/// this check is to disambiguate between an expression and a declaration.
4924bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004925 switch (Tok.getKind()) {
4926 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004927
Xiuli Pan9c14e282016-01-09 12:53:17 +00004928 case tok::kw_pipe:
Sven van Haastregte518bb42019-05-22 13:12:20 +00004929 return (getLangOpts().OpenCL && getLangOpts().OpenCLVersion >= 200) ||
4930 getLangOpts().OpenCLCPlusPlus;
Xiuli Pan9c14e282016-01-09 12:53:17 +00004931
Chris Lattner020bab92009-01-04 23:41:41 +00004932 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00004933 // Unfortunate hack to support "Class.factoryMethod" notation.
Erik Pilkingtonfa983902018-10-30 20:31:30 +00004934 if (getLangOpts().ObjC && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004935 return false;
John Thompson22334602010-02-05 00:12:22 +00004936 if (TryAltiVecVectorToken())
4937 return true;
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +00004938 LLVM_FALLTHROUGH;
David Blaikie15a430a2011-12-04 05:04:18 +00004939 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004940 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004941 // Annotate typenames and C++ scope specifiers. If we get one, just
4942 // recurse to handle whatever we get.
4943 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004944 return true;
4945 if (Tok.is(tok::identifier))
4946 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004947
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004948 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004949 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004950 // expression is permitted, then this is probably a class message send
4951 // missing the initial '['. In this case, we won't consider this to be
4952 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004953 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004954 isStartOfObjCClassMessageMissingOpenBracket())
4955 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004956
John McCall1f476a12010-02-26 08:45:28 +00004957 return isDeclarationSpecifier();
4958
Chris Lattner020bab92009-01-04 23:41:41 +00004959 case tok::coloncolon: // ::foo::bar
4960 if (NextToken().is(tok::kw_new) || // ::new
4961 NextToken().is(tok::kw_delete)) // ::delete
4962 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004963
Chris Lattner020bab92009-01-04 23:41:41 +00004964 // Annotate typenames and C++ scope specifiers. If we get one, just
4965 // recurse to handle whatever we get.
4966 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004967 return true;
4968 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004969
Chris Lattneracd58a32006-08-06 17:24:14 +00004970 // storage-class-specifier
4971 case tok::kw_typedef:
4972 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004973 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004974 case tok::kw_static:
4975 case tok::kw_auto:
Richard Smithe301ba22015-11-11 02:02:15 +00004976 case tok::kw___auto_type:
Chris Lattneracd58a32006-08-06 17:24:14 +00004977 case tok::kw_register:
4978 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004979 case tok::kw_thread_local:
4980 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004981
Douglas Gregor26701a42011-09-09 02:06:17 +00004982 // Modules
4983 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00004984
John McCallea0a39e2012-11-14 00:49:39 +00004985 // Debugger support
4986 case tok::kw___unknown_anytype:
4987
Chris Lattneracd58a32006-08-06 17:24:14 +00004988 // type-specifiers
4989 case tok::kw_short:
4990 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004991 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004992 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00004993 case tok::kw_signed:
4994 case tok::kw_unsigned:
4995 case tok::kw__Complex:
4996 case tok::kw__Imaginary:
4997 case tok::kw_void:
4998 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004999 case tok::kw_wchar_t:
Richard Smith3a8244d2018-05-01 05:02:45 +00005000 case tok::kw_char8_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00005001 case tok::kw_char16_t:
5002 case tok::kw_char32_t:
5003
Chris Lattneracd58a32006-08-06 17:24:14 +00005004 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00005005 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00005006 case tok::kw_float:
5007 case tok::kw_double:
Leonard Chanf921d852018-06-04 16:07:52 +00005008 case tok::kw__Accum:
Leonard Chanab80f3c2018-06-14 14:53:51 +00005009 case tok::kw__Fract:
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00005010 case tok::kw__Float16:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00005011 case tok::kw___float128:
Chris Lattnerbb31a422007-11-15 05:25:19 +00005012 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00005013 case tok::kw__Bool:
5014 case tok::kw__Decimal32:
5015 case tok::kw__Decimal64:
5016 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00005017 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00005018
Chris Lattner861a2262008-04-13 18:59:07 +00005019 // struct-or-union-specifier (C99) or class-specifier (C++)
5020 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00005021 case tok::kw_struct:
5022 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00005023 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00005024 // enum-specifier
5025 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00005026
Chris Lattneracd58a32006-08-06 17:24:14 +00005027 // type-qualifier
5028 case tok::kw_const:
5029 case tok::kw_volatile:
5030 case tok::kw_restrict:
Leonard Chanab80f3c2018-06-14 14:53:51 +00005031 case tok::kw__Sat:
Steve Naroffad373bd2007-07-31 12:34:36 +00005032
Chris Lattneracd58a32006-08-06 17:24:14 +00005033 // function-specifier
5034 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00005035 case tok::kw_virtual:
5036 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00005037 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00005038
Richard Smith1dba27c2013-01-29 09:02:09 +00005039 // alignment-specifier
5040 case tok::kw__Alignas:
5041
Richard Smithd16fe122012-10-25 00:00:53 +00005042 // friend keyword.
5043 case tok::kw_friend:
5044
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00005045 // static_assert-declaration
5046 case tok::kw__Static_assert:
5047
Chris Lattner599e47e2007-08-09 17:01:07 +00005048 // GNU typeof support.
5049 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00005050
Chris Lattner599e47e2007-08-09 17:01:07 +00005051 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00005052 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00005053
Richard Smithd16fe122012-10-25 00:00:53 +00005054 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00005055 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00005056 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00005057
Gauthier Harnisch796ed032019-06-14 08:56:20 +00005058 // C++20 consteval.
5059 case tok::kw_consteval:
5060
Richard Smith8e1ac332013-03-28 01:55:44 +00005061 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00005062 case tok::kw__Atomic:
5063 return true;
5064
Chris Lattner8b2ec162008-07-26 03:38:44 +00005065 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
5066 case tok::less:
Erik Pilkingtonfa983902018-10-30 20:31:30 +00005067 return getLangOpts().ObjC;
Mike Stump11289f42009-09-09 15:08:12 +00005068
Douglas Gregor19b7acf2011-04-27 05:41:15 +00005069 // typedef-name
5070 case tok::annot_typename:
5071 return !DisambiguatingWithExpression ||
5072 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00005073
Steve Narofff192fab2009-01-06 19:34:12 +00005074 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00005075 case tok::kw___cdecl:
5076 case tok::kw___stdcall:
5077 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00005078 case tok::kw___thiscall:
Erich Keane757d3172016-11-02 18:29:35 +00005079 case tok::kw___regcall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00005080 case tok::kw___vectorcall:
Eli Friedman53339e02009-06-08 23:27:34 +00005081 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00005082 case tok::kw___sptr:
5083 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00005084 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00005085 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00005086 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00005087 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00005088 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005089
Douglas Gregoraea7afd2015-06-24 22:02:08 +00005090 case tok::kw__Nonnull:
5091 case tok::kw__Nullable:
5092 case tok::kw__Null_unspecified:
Douglas Gregor261a89b2015-06-19 17:51:05 +00005093
Douglas Gregorab209d82015-07-07 03:58:42 +00005094 case tok::kw___kindof:
5095
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005096 case tok::kw___private:
5097 case tok::kw___local:
5098 case tok::kw___global:
5099 case tok::kw___constant:
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00005100 case tok::kw___generic:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005101 case tok::kw___read_only:
5102 case tok::kw___read_write:
5103 case tok::kw___write_only:
Alexey Bader954ba212016-04-08 13:40:33 +00005104#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
Alexey Baderb62f1442016-04-13 08:33:41 +00005105#include "clang/Basic/OpenCLImageTypes.def"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005106
Eli Friedman53339e02009-06-08 23:27:34 +00005107 return true;
Anastasia Stulova314fab62019-03-28 11:47:14 +00005108
5109 case tok::kw_private:
5110 return getLangOpts().OpenCL;
Chris Lattneracd58a32006-08-06 17:24:14 +00005111 }
5112}
5113
Richard Smith35845152017-02-07 01:37:30 +00005114bool Parser::isConstructorDeclarator(bool IsUnqualified, bool DeductionGuide) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005115 TentativeParsingAction TPA(*this);
5116
5117 // Parse the C++ scope specifier.
5118 CXXScopeSpec SS;
David Blaikieefdccaa2016-01-15 23:43:34 +00005119 if (ParseOptionalCXXScopeSpecifier(SS, nullptr,
Douglas Gregordf593fb2011-11-07 17:33:42 +00005120 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00005121 TPA.Revert();
5122 return false;
5123 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005124
5125 // Parse the constructor name.
Richard Smithaf3b3252017-05-18 19:21:48 +00005126 if (Tok.is(tok::identifier)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005127 // We already know that we have a constructor name; just consume
5128 // the token.
5129 ConsumeToken();
Richard Smithaf3b3252017-05-18 19:21:48 +00005130 } else if (Tok.is(tok::annot_template_id)) {
5131 ConsumeAnnotationToken();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005132 } else {
5133 TPA.Revert();
5134 return false;
5135 }
5136
Richard Smith8f8697f2017-02-08 01:16:55 +00005137 // There may be attributes here, appertaining to the constructor name or type
5138 // we just stepped past.
5139 SkipCXX11Attributes();
5140
Richard Smith43f340f2012-03-27 23:05:05 +00005141 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005142 if (Tok.isNot(tok::l_paren)) {
5143 TPA.Revert();
5144 return false;
5145 }
5146 ConsumeParen();
5147
Richard Smith43f340f2012-03-27 23:05:05 +00005148 // A right parenthesis, or ellipsis followed by a right parenthesis signals
5149 // that we have a constructor.
5150 if (Tok.is(tok::r_paren) ||
5151 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005152 TPA.Revert();
5153 return true;
5154 }
5155
Richard Smithf2163662013-09-06 00:12:20 +00005156 // A C++11 attribute here signals that we have a constructor, and is an
5157 // attribute on the first constructor parameter.
5158 if (getLangOpts().CPlusPlus11 &&
5159 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
5160 /*OuterMightBeMessageSend*/ true)) {
5161 TPA.Revert();
5162 return true;
5163 }
5164
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005165 // If we need to, enter the specified scope.
5166 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00005167 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005168 DeclScopeObj.EnterDeclaratorScope();
5169
Francois Pichet79f3a872011-01-31 04:54:32 +00005170 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00005171 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00005172 MaybeParseMicrosoftAttributes(Attrs);
5173
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005174 // Check whether the next token(s) are part of a declaration
5175 // specifier, in which case we have the start of a parameter and,
5176 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00005177 bool IsConstructor = false;
5178 if (isDeclarationSpecifier())
5179 IsConstructor = true;
5180 else if (Tok.is(tok::identifier) ||
5181 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
5182 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
5183 // This might be a parenthesized member name, but is more likely to
5184 // be a constructor declaration with an invalid argument type. Keep
5185 // looking.
5186 if (Tok.is(tok::annot_cxxscope))
Richard Smithaf3b3252017-05-18 19:21:48 +00005187 ConsumeAnnotationToken();
Richard Smithefd009d2012-03-27 00:56:56 +00005188 ConsumeToken();
5189
5190 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00005191 // which must have one of the following syntactic forms (see the
5192 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00005193 switch (Tok.getKind()) {
5194 case tok::l_paren:
5195 // C(X ( int));
5196 case tok::l_square:
5197 // C(X [ 5]);
5198 // C(X [ [attribute]]);
5199 case tok::coloncolon:
5200 // C(X :: Y);
5201 // C(X :: *p);
Richard Smithefd009d2012-03-27 00:56:56 +00005202 // Assume this isn't a constructor, rather than assuming it's a
5203 // constructor with an unnamed parameter of an ill-formed type.
5204 break;
5205
Richard Smith446161b2014-03-03 21:12:53 +00005206 case tok::r_paren:
5207 // C(X )
Richard Smith8f8697f2017-02-08 01:16:55 +00005208
5209 // Skip past the right-paren and any following attributes to get to
5210 // the function body or trailing-return-type.
5211 ConsumeParen();
5212 SkipCXX11Attributes();
5213
Richard Smith35845152017-02-07 01:37:30 +00005214 if (DeductionGuide) {
5215 // C(X) -> ... is a deduction guide.
Richard Smith8f8697f2017-02-08 01:16:55 +00005216 IsConstructor = Tok.is(tok::arrow);
Richard Smith35845152017-02-07 01:37:30 +00005217 break;
5218 }
Richard Smith8f8697f2017-02-08 01:16:55 +00005219 if (Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Richard Smith446161b2014-03-03 21:12:53 +00005220 // Assume these were meant to be constructors:
5221 // C(X) : (the name of a bit-field cannot be parenthesized).
5222 // C(X) try (this is otherwise ill-formed).
5223 IsConstructor = true;
5224 }
Richard Smith8f8697f2017-02-08 01:16:55 +00005225 if (Tok.is(tok::semi) || Tok.is(tok::l_brace)) {
Richard Smith446161b2014-03-03 21:12:53 +00005226 // If we have a constructor name within the class definition,
5227 // assume these were meant to be constructors:
5228 // C(X) {
5229 // C(X) ;
5230 // ... because otherwise we would be declaring a non-static data
5231 // member that is ill-formed because it's of the same type as its
5232 // surrounding class.
5233 //
5234 // FIXME: We can actually do this whether or not the name is qualified,
5235 // because if it is qualified in this context it must be being used as
Richard Smith35845152017-02-07 01:37:30 +00005236 // a constructor name.
Richard Smith446161b2014-03-03 21:12:53 +00005237 // currently, so we're somewhat conservative here.
5238 IsConstructor = IsUnqualified;
5239 }
5240 break;
5241
Richard Smithefd009d2012-03-27 00:56:56 +00005242 default:
5243 IsConstructor = true;
5244 break;
5245 }
5246 }
5247
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005248 TPA.Revert();
5249 return IsConstructor;
5250}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00005251
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005252/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00005253/// type-qualifier-list: [C99 6.7.5]
5254/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00005255/// [vendor] attributes
Aaron Ballman08b06592014-07-22 12:44:22 +00005256/// [ only if AttrReqs & AR_VendorAttributesParsed ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00005257/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00005258/// [vendor] type-qualifier-list attributes
Aaron Ballman08b06592014-07-22 12:44:22 +00005259/// [ only if AttrReqs & AR_VendorAttributesParsed ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00005260/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Aaron Ballman08b06592014-07-22 12:44:22 +00005261/// [ only if AttReqs & AR_CXX11AttributesParsed ]
5262/// Note: vendor can be GNU, MS, etc and can be explicitly controlled via
5263/// AttrRequirements bitmask values.
Alex Lorenz8f4d3992017-02-13 23:19:40 +00005264void Parser::ParseTypeQualifierListOpt(
5265 DeclSpec &DS, unsigned AttrReqs, bool AtomicAllowed,
5266 bool IdentifierRequired,
5267 Optional<llvm::function_ref<void()>> CodeCompletionHandler) {
Aaron Ballman606093a2017-10-15 15:01:42 +00005268 if (standardAttributesAllowed() && (AttrReqs & AR_CXX11AttributesParsed) &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005269 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00005270 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00005271 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005272 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005273 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00005274
5275 SourceLocation EndLoc;
5276
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005277 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00005278 bool isInvalid = false;
Craig Topper161e4db2014-05-21 06:02:52 +00005279 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +00005280 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00005281 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00005282
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005283 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00005284 case tok::code_completion:
Alex Lorenz8f4d3992017-02-13 23:19:40 +00005285 if (CodeCompletionHandler)
5286 (*CodeCompletionHandler)();
5287 else
5288 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00005289 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00005290
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005291 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00005292 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00005293 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00005294 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005295 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00005296 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00005297 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00005298 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005299 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00005300 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00005301 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005302 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00005303 case tok::kw__Atomic:
5304 if (!AtomicAllowed)
5305 goto DoneWithTypeQuals;
5306 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
5307 getLangOpts());
5308 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005309
5310 // OpenCL qualifiers:
Anastasia Stulova948e37c2019-03-25 11:54:02 +00005311 case tok::kw_private:
Anastasia Stulova314fab62019-03-28 11:47:14 +00005312 if (!getLangOpts().OpenCL)
5313 goto DoneWithTypeQuals;
5314 LLVM_FALLTHROUGH;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005315 case tok::kw___private:
5316 case tok::kw___global:
5317 case tok::kw___local:
5318 case tok::kw___constant:
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00005319 case tok::kw___generic:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005320 case tok::kw___read_only:
5321 case tok::kw___write_only:
5322 case tok::kw___read_write:
Aaron Ballman05d76ea2014-01-14 01:29:54 +00005323 ParseOpenCLQualifiers(DS.getAttributes());
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005324 break;
5325
Andrey Bokhanko45d41322016-05-11 18:38:21 +00005326 case tok::kw___unaligned:
5327 isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID,
5328 getLangOpts());
5329 break;
Aaron Ballman317a77f2013-05-22 23:25:32 +00005330 case tok::kw___uptr:
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00005331 // GNU libc headers in C mode use '__uptr' as an identifier which conflicts
Alp Toker62c5b572013-11-26 01:30:10 +00005332 // with the MS modifier keyword.
Aaron Ballman08b06592014-07-22 12:44:22 +00005333 if ((AttrReqs & AR_DeclspecAttributesParsed) && !getLangOpts().CPlusPlus &&
Alp Toker47642d22013-12-03 06:13:01 +00005334 IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) {
5335 if (TryKeywordIdentFallback(false))
5336 continue;
Alp Toker62c5b572013-11-26 01:30:10 +00005337 }
Galina Kistanova77674252017-06-01 21:15:34 +00005338 LLVM_FALLTHROUGH;
Alp Toker62c5b572013-11-26 01:30:10 +00005339 case tok::kw___sptr:
Eli Friedman53339e02009-06-08 23:27:34 +00005340 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00005341 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00005342 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00005343 case tok::kw___cdecl:
5344 case tok::kw___stdcall:
5345 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00005346 case tok::kw___thiscall:
Erich Keane757d3172016-11-02 18:29:35 +00005347 case tok::kw___regcall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00005348 case tok::kw___vectorcall:
Aaron Ballman08b06592014-07-22 12:44:22 +00005349 if (AttrReqs & AR_DeclspecAttributesParsed) {
John McCall53fa7142010-12-24 02:08:15 +00005350 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00005351 continue;
5352 }
5353 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00005354 case tok::kw___pascal:
Aaron Ballman08b06592014-07-22 12:44:22 +00005355 if (AttrReqs & AR_VendorAttributesParsed) {
John McCall53fa7142010-12-24 02:08:15 +00005356 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00005357 continue;
5358 }
5359 goto DoneWithTypeQuals;
Douglas Gregor261a89b2015-06-19 17:51:05 +00005360
5361 // Nullability type specifiers.
Douglas Gregoraea7afd2015-06-24 22:02:08 +00005362 case tok::kw__Nonnull:
5363 case tok::kw__Nullable:
5364 case tok::kw__Null_unspecified:
Douglas Gregor261a89b2015-06-19 17:51:05 +00005365 ParseNullabilityTypeSpecifiers(DS.getAttributes());
5366 continue;
5367
Douglas Gregorab209d82015-07-07 03:58:42 +00005368 // Objective-C 'kindof' types.
5369 case tok::kw___kindof:
5370 DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc,
Erich Keanee891aa92018-07-13 15:07:47 +00005371 nullptr, 0, ParsedAttr::AS_Keyword);
Douglas Gregorab209d82015-07-07 03:58:42 +00005372 (void)ConsumeToken();
5373 continue;
5374
Chris Lattnere37e2332006-08-15 04:50:22 +00005375 case tok::kw___attribute:
Aaron Ballman08b06592014-07-22 12:44:22 +00005376 if (AttrReqs & AR_GNUAttributesParsedAndRejected)
5377 // When GNU attributes are expressly forbidden, diagnose their usage.
5378 Diag(Tok, diag::err_attributes_not_allowed);
5379
5380 // Parse the attributes even if they are rejected to ensure that error
5381 // recovery is graceful.
5382 if (AttrReqs & AR_GNUAttributesParsed ||
5383 AttrReqs & AR_GNUAttributesParsedAndRejected) {
John McCall53fa7142010-12-24 02:08:15 +00005384 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00005385 continue; // do *not* consume the next token!
5386 }
5387 // otherwise, FALL THROUGH!
Galina Kistanova77674252017-06-01 21:15:34 +00005388 LLVM_FALLTHROUGH;
Chris Lattnercf0bab22008-12-18 07:02:59 +00005389 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00005390 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00005391 // If this is not a type-qualifier token, we're done reading type
5392 // qualifiers. First verify that DeclSpec's are consistent.
Craig Topper25122412015-11-15 03:32:11 +00005393 DS.Finish(Actions, Actions.getASTContext().getPrintingPolicy());
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00005394 if (EndLoc.isValid())
5395 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005396 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005397 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005398
Chris Lattnerd9c3c592006-08-05 06:26:47 +00005399 // If the specifier combination wasn't legal, issue a diagnostic.
5400 if (isInvalid) {
5401 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00005402 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00005403 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00005404 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005405 }
5406}
5407
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00005408/// ParseDeclarator - Parse and verify a newly-initialized declarator.
5409///
5410void Parser::ParseDeclarator(Declarator &D) {
5411 /// This implements the 'declarator' production in the C grammar, then checks
5412 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00005413 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00005414}
5415
Richard Smith0b350b92014-10-28 16:55:02 +00005416static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang,
Faisal Vali421b2d12017-12-29 05:41:00 +00005417 DeclaratorContext TheContext) {
Richard Smith0efa75c2012-03-29 01:16:42 +00005418 if (Kind == tok::star || Kind == tok::caret)
5419 return true;
5420
Sven van Haastregte518bb42019-05-22 13:12:20 +00005421 if (Kind == tok::kw_pipe &&
5422 ((Lang.OpenCL && Lang.OpenCLVersion >= 200) || Lang.OpenCLCPlusPlus))
Xiuli Pan9c14e282016-01-09 12:53:17 +00005423 return true;
5424
Richard Smith0efa75c2012-03-29 01:16:42 +00005425 if (!Lang.CPlusPlus)
5426 return false;
5427
Richard Smith0b350b92014-10-28 16:55:02 +00005428 if (Kind == tok::amp)
5429 return true;
5430
5431 // We parse rvalue refs in C++03, because otherwise the errors are scary.
5432 // But we must not parse them in conversion-type-ids and new-type-ids, since
5433 // those can be legitimately followed by a && operator.
5434 // (The same thing can in theory happen after a trailing-return-type, but
5435 // since those are a C++11 feature, there is no rejects-valid issue there.)
5436 if (Kind == tok::ampamp)
Faisal Vali421b2d12017-12-29 05:41:00 +00005437 return Lang.CPlusPlus11 ||
5438 (TheContext != DeclaratorContext::ConversionIdContext &&
5439 TheContext != DeclaratorContext::CXXNewContext);
Richard Smith0b350b92014-10-28 16:55:02 +00005440
5441 return false;
Richard Smith0efa75c2012-03-29 01:16:42 +00005442}
5443
Xiuli Pan9c14e282016-01-09 12:53:17 +00005444// Indicates whether the given declarator is a pipe declarator.
5445static bool isPipeDeclerator(const Declarator &D) {
5446 const unsigned NumTypes = D.getNumTypeObjects();
5447
5448 for (unsigned Idx = 0; Idx != NumTypes; ++Idx)
5449 if (DeclaratorChunk::Pipe == D.getTypeObject(Idx).Kind)
5450 return true;
5451
5452 return false;
5453}
5454
Sebastian Redlbd150f42008-11-21 19:14:01 +00005455/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
5456/// is parsed by the function passed to it. Pass null, and the direct-declarator
5457/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00005458/// ptr-operator production.
5459///
Richard Smith09f76ee2011-10-19 21:33:05 +00005460/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00005461/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
5462/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00005463///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005464/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
5465/// [C] pointer[opt] direct-declarator
5466/// [C++] direct-declarator
5467/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00005468///
5469/// pointer: [C99 6.7.5]
5470/// '*' type-qualifier-list[opt]
5471/// '*' type-qualifier-list[opt] pointer
5472///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00005473/// ptr-operator:
5474/// '*' cv-qualifier-seq[opt]
5475/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00005476/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00005477/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00005478/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005479/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00005480void Parser::ParseDeclaratorInternal(Declarator &D,
5481 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00005482 if (Diags.hasAllExtensionsSilenced())
5483 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00005484
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005485 // C++ member pointers start with a '::' or a nested-name.
5486 // Member pointers get special handling, since there's no place for the
5487 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00005488 if (getLangOpts().CPlusPlus &&
Richard Smith83c2ecf2016-02-02 23:34:49 +00005489 (Tok.is(tok::coloncolon) || Tok.is(tok::kw_decltype) ||
Serge Pavlov458ea762014-07-16 05:16:52 +00005490 (Tok.is(tok::identifier) &&
5491 (NextToken().is(tok::coloncolon) || NextToken().is(tok::less))) ||
Chris Lattner803802d2009-03-24 17:04:48 +00005492 Tok.is(tok::annot_cxxscope))) {
Faisal Vali421b2d12017-12-29 05:41:00 +00005493 bool EnteringContext =
5494 D.getContext() == DeclaratorContext::FileContext ||
5495 D.getContext() == DeclaratorContext::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005496 CXXScopeSpec SS;
David Blaikieefdccaa2016-01-15 23:43:34 +00005497 ParseOptionalCXXScopeSpecifier(SS, nullptr, EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00005498
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00005499 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00005500 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005501 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00005502 if (D.mayHaveIdentifier())
5503 D.getCXXScopeSpec() = SS;
5504 else
5505 AnnotateScopeToken(SS, true);
5506
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005507 if (DirectDeclParser)
5508 (this->*DirectDeclParser)(D);
5509 return;
5510 }
5511
5512 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005513 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00005514 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005515 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005516 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005517
5518 // Recurse to parse whatever is left.
5519 ParseDeclaratorInternal(D, DirectDeclParser);
5520
5521 // Sema will have to catch (syntactically invalid) pointers into global
5522 // scope. It has to catch pointers into namespace scope anyway.
Erich Keanec480f302018-07-12 21:09:05 +00005523 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(
Stephen Kelly1c301dc2018-08-09 21:09:38 +00005524 SS, DS.getTypeQualifiers(), DS.getEndLoc()),
Erich Keanec480f302018-07-12 21:09:05 +00005525 std::move(DS.getAttributes()),
5526 /* Don't replace range end. */ SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005527 return;
5528 }
5529 }
5530
5531 tok::TokenKind Kind = Tok.getKind();
Xiuli Pan9c14e282016-01-09 12:53:17 +00005532
5533 if (D.getDeclSpec().isTypeSpecPipe() && !isPipeDeclerator(D)) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005534 DeclSpec DS(AttrFactory);
5535 ParseTypeQualifierListOpt(DS);
Xiuli Pan9c14e282016-01-09 12:53:17 +00005536
5537 D.AddTypeInfo(
5538 DeclaratorChunk::getPipe(DS.getTypeQualifiers(), DS.getPipeLoc()),
Erich Keanec480f302018-07-12 21:09:05 +00005539 std::move(DS.getAttributes()), SourceLocation());
Xiuli Pan9c14e282016-01-09 12:53:17 +00005540 }
5541
Steve Naroffec33ed92008-08-27 16:04:49 +00005542 // Not a pointer, C++ reference, or block.
Richard Smith0b350b92014-10-28 16:55:02 +00005543 if (!isPtrOperatorToken(Kind, getLangOpts(), D.getContext())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00005544 if (DirectDeclParser)
5545 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00005546 return;
5547 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005548
Sebastian Redled0f3b02009-03-15 22:02:01 +00005549 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
5550 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00005551 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005552 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00005553
Chris Lattner9eac9312009-03-27 04:18:06 +00005554 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00005555 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00005556 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005557
Aaron Ballman08b06592014-07-22 12:44:22 +00005558 // GNU attributes are not allowed here in a new-type-id, but Declspec and
5559 // C++11 attributes are allowed.
5560 unsigned Reqs = AR_CXX11AttributesParsed | AR_DeclspecAttributesParsed |
Faisal Vali421b2d12017-12-29 05:41:00 +00005561 ((D.getContext() != DeclaratorContext::CXXNewContext)
5562 ? AR_GNUAttributesParsed
5563 : AR_GNUAttributesParsedAndRejected);
Aaron Ballman08b06592014-07-22 12:44:22 +00005564 ParseTypeQualifierListOpt(DS, Reqs, true, !D.mayOmitIdentifier());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005565 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005566
Bill Wendling3708c182007-05-27 10:15:43 +00005567 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00005568 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00005569 if (Kind == tok::star)
5570 // Remember that we parsed a pointer type, and remember the type-quals.
Erich Keanec480f302018-07-12 21:09:05 +00005571 D.AddTypeInfo(DeclaratorChunk::getPointer(
5572 DS.getTypeQualifiers(), Loc, DS.getConstSpecLoc(),
5573 DS.getVolatileSpecLoc(), DS.getRestrictSpecLoc(),
5574 DS.getAtomicSpecLoc(), DS.getUnalignedSpecLoc()),
5575 std::move(DS.getAttributes()), SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00005576 else
5577 // Remember that we parsed a Block type, and remember the type-quals.
Erich Keanec480f302018-07-12 21:09:05 +00005578 D.AddTypeInfo(
5579 DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), Loc),
5580 std::move(DS.getAttributes()), SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00005581 } else {
5582 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00005583 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00005584
Sebastian Redl3b27be62009-03-23 00:00:23 +00005585 // Complain about rvalue references in C++03, but then go on and build
5586 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00005587 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005588 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005589 diag::warn_cxx98_compat_rvalue_reference :
5590 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00005591
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005592 // GNU-style and C++11 attributes are allowed here, as is restrict.
5593 ParseTypeQualifierListOpt(DS);
5594 D.ExtendWithDeclSpec(DS);
5595
Bill Wendling93efb222007-06-02 23:28:54 +00005596 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
5597 // cv-qualifiers are introduced through the use of a typedef or of a
5598 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00005599 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
5600 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
5601 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00005602 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00005603 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
5604 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00005605 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00005606 // 'restrict' is permitted as an extension.
5607 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
5608 Diag(DS.getAtomicSpecLoc(),
5609 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00005610 }
Bill Wendling3708c182007-05-27 10:15:43 +00005611
5612 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00005613 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00005614
Douglas Gregor66583c52008-11-03 15:51:28 +00005615 if (D.getNumTypeObjects() > 0) {
5616 // C++ [dcl.ref]p4: There shall be no references to references.
5617 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
5618 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00005619 if (const IdentifierInfo *II = D.getIdentifier())
5620 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
5621 << II;
5622 else
5623 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
5624 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00005625
Sebastian Redlbd150f42008-11-21 19:14:01 +00005626 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00005627 // can go ahead and build the (technically ill-formed)
5628 // declarator: reference collapsing will take care of it.
5629 }
5630 }
5631
Richard Smith8e1ac332013-03-28 01:55:44 +00005632 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00005633 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00005634 Kind == tok::amp),
Erich Keanec480f302018-07-12 21:09:05 +00005635 std::move(DS.getAttributes()), SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00005636 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00005637}
5638
Richard Trieuf4b81d02014-06-24 23:14:24 +00005639// When correcting from misplaced brackets before the identifier, the location
5640// is saved inside the declarator so that other diagnostic messages can use
5641// them. This extracts and returns that location, or returns the provided
5642// location if a stored location does not exist.
5643static SourceLocation getMissingDeclaratorIdLoc(Declarator &D,
5644 SourceLocation Loc) {
5645 if (D.getName().StartLocation.isInvalid() &&
5646 D.getName().EndLocation.isValid())
5647 return D.getName().EndLocation;
5648
5649 return Loc;
5650}
5651
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005652/// ParseDirectDeclarator
5653/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00005654/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005655/// '(' declarator ')'
5656/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00005657/// [C90] direct-declarator '[' constant-expression[opt] ']'
5658/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5659/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5660/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5661/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005662/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5663/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005664/// direct-declarator '(' parameter-type-list ')'
5665/// direct-declarator '(' identifier-list[opt] ')'
5666/// [GNU] direct-declarator '(' parameter-forward-declarations
5667/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00005668/// [C++] direct-declarator '(' parameter-declaration-clause ')'
5669/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005670/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
5671/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
5672/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00005673/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005674/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00005675///
5676/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00005677/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00005678/// '::'[opt] nested-name-specifier[opt] type-name
5679///
5680/// id-expression: [C++ 5.1]
5681/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00005682/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00005683///
5684/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00005685/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00005686/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00005687/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00005688/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00005689/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00005690///
Richard Smithbdb84f32016-07-22 23:36:59 +00005691/// C++17 adds the following, which we also handle here:
5692///
5693/// simple-declaration:
5694/// <decl-spec> '[' identifier-list ']' brace-or-equal-initializer ';'
5695///
Richard Smith1453e312012-03-27 01:42:32 +00005696/// Note, any additional constructs added here may need corresponding changes
5697/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00005698void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00005699 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00005700
David Blaikiebbafb8a2012-03-11 07:00:24 +00005701 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Richard Smithbdb84f32016-07-22 23:36:59 +00005702 // This might be a C++17 structured binding.
5703 if (Tok.is(tok::l_square) && !D.mayOmitIdentifier() &&
5704 D.getCXXScopeSpec().isEmpty())
5705 return ParseDecompositionDeclarator(D);
5706
Serge Pavlov458ea762014-07-16 05:16:52 +00005707 // Don't parse FOO:BAR as if it were a typo for FOO::BAR inside a class, in
5708 // this context it is a bitfield. Also in range-based for statement colon
5709 // may delimit for-range-declaration.
Faisal Vali421b2d12017-12-29 05:41:00 +00005710 ColonProtectionRAIIObject X(
5711 *this, D.getContext() == DeclaratorContext::MemberContext ||
5712 (D.getContext() == DeclaratorContext::ForContext &&
5713 getLangOpts().CPlusPlus11));
Serge Pavlov458ea762014-07-16 05:16:52 +00005714
Douglas Gregor7861a802009-11-03 01:35:08 +00005715 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00005716 if (D.getCXXScopeSpec().isEmpty()) {
Faisal Vali421b2d12017-12-29 05:41:00 +00005717 bool EnteringContext =
5718 D.getContext() == DeclaratorContext::FileContext ||
5719 D.getContext() == DeclaratorContext::MemberContext;
David Blaikieefdccaa2016-01-15 23:43:34 +00005720 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), nullptr,
Douglas Gregordf593fb2011-11-07 17:33:42 +00005721 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00005722 }
5723
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00005724 if (D.getCXXScopeSpec().isValid()) {
Richard Smith64e033f2015-01-15 00:48:52 +00005725 if (Actions.ShouldEnterDeclaratorScope(getCurScope(),
5726 D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00005727 // Change the declaration context for name lookup, until this function
5728 // is exited (and the declarator has been parsed).
5729 DeclScopeObj.EnterDeclaratorScope();
Alex Lorenze151f0102016-12-07 10:24:44 +00005730 else if (getObjCDeclContext()) {
5731 // Ensure that we don't interpret the next token as an identifier when
5732 // dealing with declarations in an Objective-C container.
5733 D.SetIdentifier(nullptr, Tok.getLocation());
5734 D.setInvalidType(true);
5735 ConsumeToken();
5736 goto PastIdentifier;
5737 }
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00005738 }
5739
Douglas Gregor27b4c162010-12-23 22:44:42 +00005740 // C++0x [dcl.fct]p14:
Faisal Vali8435a692014-12-04 12:40:21 +00005741 // There is a syntactic ambiguity when an ellipsis occurs at the end of a
5742 // parameter-declaration-clause without a preceding comma. In this case,
5743 // the ellipsis is parsed as part of the abstract-declarator if the type
5744 // of the parameter either names a template parameter pack that has not
5745 // been expanded or contains auto; otherwise, it is parsed as part of the
5746 // parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00005747 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Faisal Vali421b2d12017-12-29 05:41:00 +00005748 !((D.getContext() == DeclaratorContext::PrototypeContext ||
5749 D.getContext() == DeclaratorContext::LambdaExprParameterContext ||
5750 D.getContext() == DeclaratorContext::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00005751 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00005752 !D.hasGroupingParens() &&
Faisal Vali8435a692014-12-04 12:40:21 +00005753 !Actions.containsUnexpandedParameterPacks(D) &&
Faisal Vali090da2d2018-01-01 18:23:28 +00005754 D.getDeclSpec().getTypeSpecType() != TST_auto)) {
Richard Smith0efa75c2012-03-29 01:16:42 +00005755 SourceLocation EllipsisLoc = ConsumeToken();
Richard Smith0b350b92014-10-28 16:55:02 +00005756 if (isPtrOperatorToken(Tok.getKind(), getLangOpts(), D.getContext())) {
Richard Smith0efa75c2012-03-29 01:16:42 +00005757 // The ellipsis was put in the wrong place. Recover, and explain to
5758 // the user what they should have done.
5759 ParseDeclarator(D);
Nikola Smiljanic69fdc9f2014-06-06 02:58:59 +00005760 if (EllipsisLoc.isValid())
5761 DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D);
Richard Smith0efa75c2012-03-29 01:16:42 +00005762 return;
5763 } else
5764 D.setEllipsisLoc(EllipsisLoc);
5765
5766 // The ellipsis can't be followed by a parenthesized declarator. We
5767 // check for that in ParseParenDeclarator, after we have disambiguated
5768 // the l_paren token.
5769 }
5770
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00005771 if (Tok.isOneOf(tok::identifier, tok::kw_operator, tok::annot_template_id,
5772 tok::tilde)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00005773 // We found something that indicates the start of an unqualified-id.
5774 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00005775 bool AllowConstructorName;
Richard Smith35845152017-02-07 01:37:30 +00005776 bool AllowDeductionGuide;
5777 if (D.getDeclSpec().hasTypeSpecifier()) {
John McCall84821e72010-04-13 06:39:49 +00005778 AllowConstructorName = false;
Richard Smith35845152017-02-07 01:37:30 +00005779 AllowDeductionGuide = false;
5780 } else if (D.getCXXScopeSpec().isSet()) {
John McCall84821e72010-04-13 06:39:49 +00005781 AllowConstructorName =
Faisal Vali421b2d12017-12-29 05:41:00 +00005782 (D.getContext() == DeclaratorContext::FileContext ||
5783 D.getContext() == DeclaratorContext::MemberContext);
Richard Smith35845152017-02-07 01:37:30 +00005784 AllowDeductionGuide = false;
5785 } else {
Faisal Vali421b2d12017-12-29 05:41:00 +00005786 AllowConstructorName =
5787 (D.getContext() == DeclaratorContext::MemberContext);
Fangrui Song6907ce22018-07-30 19:24:48 +00005788 AllowDeductionGuide =
Faisal Vali421b2d12017-12-29 05:41:00 +00005789 (D.getContext() == DeclaratorContext::FileContext ||
5790 D.getContext() == DeclaratorContext::MemberContext);
Richard Smith35845152017-02-07 01:37:30 +00005791 }
John McCall84821e72010-04-13 06:39:49 +00005792
Richard Smith64e033f2015-01-15 00:48:52 +00005793 bool HadScope = D.getCXXScopeSpec().isValid();
Chad Rosierc1183952012-06-26 22:30:43 +00005794 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
5795 /*EnteringContext=*/true,
David Blaikieefdccaa2016-01-15 23:43:34 +00005796 /*AllowDestructorName=*/true, AllowConstructorName,
Richard Smithc08b6932018-04-27 02:00:13 +00005797 AllowDeductionGuide, nullptr, nullptr,
Richard Smith35845152017-02-07 01:37:30 +00005798 D.getName()) ||
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00005799 // Once we're past the identifier, if the scope was bad, mark the
5800 // whole declarator bad.
5801 D.getCXXScopeSpec().isInvalid()) {
Craig Topper161e4db2014-05-21 06:02:52 +00005802 D.SetIdentifier(nullptr, Tok.getLocation());
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00005803 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00005804 } else {
Richard Smith64e033f2015-01-15 00:48:52 +00005805 // ParseUnqualifiedId might have parsed a scope specifier during error
5806 // recovery. If it did so, enter that scope.
5807 if (!HadScope && D.getCXXScopeSpec().isValid() &&
5808 Actions.ShouldEnterDeclaratorScope(getCurScope(),
5809 D.getCXXScopeSpec()))
5810 DeclScopeObj.EnterDeclaratorScope();
5811
Douglas Gregor7861a802009-11-03 01:35:08 +00005812 // Parsed the unqualified-id; update range information and move along.
5813 if (D.getSourceRange().getBegin().isInvalid())
5814 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
5815 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00005816 }
Douglas Gregor7861a802009-11-03 01:35:08 +00005817 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00005818 }
Richard Smithd63db6e2015-12-19 02:40:19 +00005819
5820 if (D.getCXXScopeSpec().isNotEmpty()) {
5821 // We have a scope specifier but no following unqualified-id.
5822 Diag(PP.getLocForEndOfToken(D.getCXXScopeSpec().getEndLoc()),
5823 diag::err_expected_unqualified_id)
5824 << /*C++*/1;
5825 D.SetIdentifier(nullptr, Tok.getLocation());
5826 goto PastIdentifier;
5827 }
Douglas Gregor7861a802009-11-03 01:35:08 +00005828 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005829 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00005830 "There's a C++-specific check for tok::identifier above");
5831 assert(Tok.getIdentifierInfo() && "Not an identifier?");
5832 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Richard Trieu2664dc12014-05-05 22:06:50 +00005833 D.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00005834 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00005835 goto PastIdentifier;
Richard Smith74639b12017-05-19 01:54:59 +00005836 } else if (Tok.is(tok::identifier) && !D.mayHaveIdentifier()) {
5837 // We're not allowed an identifier here, but we got one. Try to figure out
5838 // if the user was trying to attach a name to the type, or whether the name
5839 // is some unrelated trailing syntax.
5840 bool DiagnoseIdentifier = false;
5841 if (D.hasGroupingParens())
5842 // An identifier within parens is unlikely to be intended to be anything
5843 // other than a name being "declared".
5844 DiagnoseIdentifier = true;
Richard Smith77a9c602018-02-28 03:02:23 +00005845 else if (D.getContext() == DeclaratorContext::TemplateArgContext)
Richard Smith74639b12017-05-19 01:54:59 +00005846 // T<int N> is an accidental identifier; T<int N indicates a missing '>'.
5847 DiagnoseIdentifier =
5848 NextToken().isOneOf(tok::comma, tok::greater, tok::greatergreater);
Faisal Vali421b2d12017-12-29 05:41:00 +00005849 else if (D.getContext() == DeclaratorContext::AliasDeclContext ||
5850 D.getContext() == DeclaratorContext::AliasTemplateContext)
Richard Smith74639b12017-05-19 01:54:59 +00005851 // The most likely error is that the ';' was forgotten.
5852 DiagnoseIdentifier = NextToken().isOneOf(tok::comma, tok::semi);
Richard Smithe303e352018-02-02 22:24:54 +00005853 else if ((D.getContext() == DeclaratorContext::TrailingReturnContext ||
5854 D.getContext() == DeclaratorContext::TrailingReturnVarContext) &&
Richard Smith74639b12017-05-19 01:54:59 +00005855 !isCXX11VirtSpecifier(Tok))
5856 DiagnoseIdentifier = NextToken().isOneOf(
5857 tok::comma, tok::semi, tok::equal, tok::l_brace, tok::kw_try);
5858 if (DiagnoseIdentifier) {
Richard Smithf39720b2013-10-13 22:12:28 +00005859 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
5860 << FixItHint::CreateRemoval(Tok.getLocation());
Craig Topper161e4db2014-05-21 06:02:52 +00005861 D.SetIdentifier(nullptr, Tok.getLocation());
Richard Smithf39720b2013-10-13 22:12:28 +00005862 ConsumeToken();
5863 goto PastIdentifier;
5864 }
Douglas Gregor7861a802009-11-03 01:35:08 +00005865 }
Richard Smith0efa75c2012-03-29 01:16:42 +00005866
Douglas Gregor7861a802009-11-03 01:35:08 +00005867 if (Tok.is(tok::l_paren)) {
Richard Smithe303e352018-02-02 22:24:54 +00005868 // If this might be an abstract-declarator followed by a direct-initializer,
5869 // check whether this is a valid declarator chunk. If it can't be, assume
5870 // that it's an initializer instead.
5871 if (D.mayOmitIdentifier() && D.mayBeFollowedByCXXDirectInit()) {
5872 RevertingTentativeParsingAction PA(*this);
5873 if (TryParseDeclarator(true, D.mayHaveIdentifier(), true) ==
5874 TPResult::False) {
5875 D.SetIdentifier(nullptr, Tok.getLocation());
5876 goto PastIdentifier;
5877 }
5878 }
5879
Chris Lattneracd58a32006-08-06 17:24:14 +00005880 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00005881 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00005882 // Example: 'char (*X)' or 'int (*XX)(void)'
5883 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005884
5885 // If the declarator was parenthesized, we entered the declarator
5886 // scope when parsing the parenthesized declarator, then exited
5887 // the scope already. Re-enter the scope, if we need to.
5888 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00005889 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00005890 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00005891 if (!D.isInvalidType() &&
Nico Weber6b05f382015-02-18 04:53:03 +00005892 Actions.ShouldEnterDeclaratorScope(getCurScope(),
5893 D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005894 // Change the declaration context for name lookup, until this function
5895 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00005896 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005897 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00005898 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00005899 // This could be something simple like "int" (in which case the declarator
5900 // portion is empty), if an abstract-declarator is allowed.
Craig Topper161e4db2014-05-21 06:02:52 +00005901 D.SetIdentifier(nullptr, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00005902
5903 // The grammar for abstract-pack-declarator does not allow grouping parens.
5904 // FIXME: Revisit this once core issue 1488 is resolved.
5905 if (D.hasEllipsis() && D.hasGroupingParens())
5906 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
5907 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00005908 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00005909 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00005910 LLVM_BUILTIN_TRAP;
Richard Trieuf4b81d02014-06-24 23:14:24 +00005911 if (Tok.is(tok::l_square))
5912 return ParseMisplacedBracketDeclarator(D);
Faisal Vali421b2d12017-12-29 05:41:00 +00005913 if (D.getContext() == DeclaratorContext::MemberContext) {
Alex Lorenzf1278212017-04-11 15:01:53 +00005914 // Objective-C++: Detect C++ keywords and try to prevent further errors by
5915 // treating these keyword as valid member names.
Erik Pilkingtonfa983902018-10-30 20:31:30 +00005916 if (getLangOpts().ObjC && getLangOpts().CPlusPlus &&
Alex Lorenzf1278212017-04-11 15:01:53 +00005917 Tok.getIdentifierInfo() &&
5918 Tok.getIdentifierInfo()->isCPlusPlusKeyword(getLangOpts())) {
5919 Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
5920 diag::err_expected_member_name_or_semi_objcxx_keyword)
5921 << Tok.getIdentifierInfo()
5922 << (D.getDeclSpec().isEmpty() ? SourceRange()
5923 : D.getDeclSpec().getSourceRange());
5924 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
5925 D.SetRangeEnd(Tok.getLocation());
5926 ConsumeToken();
5927 goto PastIdentifier;
5928 }
Richard Trieuf4b81d02014-06-24 23:14:24 +00005929 Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
5930 diag::err_expected_member_name_or_semi)
Richard Trieua1342402014-05-02 23:40:32 +00005931 << (D.getDeclSpec().isEmpty() ? SourceRange()
5932 : D.getDeclSpec().getSourceRange());
Richard Trieuf4b81d02014-06-24 23:14:24 +00005933 } else if (getLangOpts().CPlusPlus) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00005934 if (Tok.isOneOf(tok::period, tok::arrow))
Richard Trieu9c672672013-01-26 02:31:38 +00005935 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
Richard Trieu2f586962013-09-05 02:31:33 +00005936 else {
5937 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
5938 if (Tok.isAtStartOfLine() && Loc.isValid())
5939 Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
5940 << getLangOpts().CPlusPlus;
5941 else
Richard Trieuf4b81d02014-06-24 23:14:24 +00005942 Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
5943 diag::err_expected_unqualified_id)
Richard Trieu2f586962013-09-05 02:31:33 +00005944 << getLangOpts().CPlusPlus;
5945 }
Richard Trieuf4b81d02014-06-24 23:14:24 +00005946 } else {
5947 Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
5948 diag::err_expected_either)
5949 << tok::identifier << tok::l_paren;
5950 }
Craig Topper161e4db2014-05-21 06:02:52 +00005951 D.SetIdentifier(nullptr, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00005952 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00005953 }
Mike Stump11289f42009-09-09 15:08:12 +00005954
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00005955 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00005956 assert(D.isPastIdentifier() &&
5957 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00005958
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005959 // Don't parse attributes unless we have parsed an unparenthesized name.
5960 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00005961 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005962
Chris Lattneracd58a32006-08-06 17:24:14 +00005963 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00005964 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00005965 // Enter function-declaration scope, limiting any declarators to the
5966 // function prototype scope, including parameter declarators.
5967 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00005968 Scope::FunctionPrototypeScope|Scope::DeclScope|
5969 (D.isFunctionDeclaratorAFunctionDeclaration()
5970 ? Scope::FunctionDeclarationScope : 0));
5971
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00005972 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
5973 // In such a case, check if we actually have a function declarator; if it
5974 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00005975 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00005976 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
5977 // The name of the declarator, if any, is tentatively declared within
5978 // a possible direct initializer.
5979 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
5980 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
5981 TentativelyDeclaredIdentifiers.pop_back();
5982 if (!IsFunctionDecl)
5983 break;
5984 }
John McCall084e83d2011-03-24 11:26:52 +00005985 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005986 BalancedDelimiterTracker T(*this, tok::l_paren);
5987 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00005988 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00005989 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00005990 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00005991 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00005992 } else {
5993 break;
5994 }
5995 }
Chad Rosierc1183952012-06-26 22:30:43 +00005996}
Chris Lattneracd58a32006-08-06 17:24:14 +00005997
Richard Smithbdb84f32016-07-22 23:36:59 +00005998void Parser::ParseDecompositionDeclarator(Declarator &D) {
5999 assert(Tok.is(tok::l_square));
6000
6001 // If this doesn't look like a structured binding, maybe it's a misplaced
6002 // array declarator.
6003 // FIXME: Consume the l_square first so we don't need extra lookahead for
6004 // this.
6005 if (!(NextToken().is(tok::identifier) &&
6006 GetLookAheadToken(2).isOneOf(tok::comma, tok::r_square)) &&
6007 !(NextToken().is(tok::r_square) &&
6008 GetLookAheadToken(2).isOneOf(tok::equal, tok::l_brace)))
6009 return ParseMisplacedBracketDeclarator(D);
6010
6011 BalancedDelimiterTracker T(*this, tok::l_square);
6012 T.consumeOpen();
6013
6014 SmallVector<DecompositionDeclarator::Binding, 32> Bindings;
6015 while (Tok.isNot(tok::r_square)) {
6016 if (!Bindings.empty()) {
6017 if (Tok.is(tok::comma))
6018 ConsumeToken();
6019 else {
6020 if (Tok.is(tok::identifier)) {
6021 SourceLocation EndLoc = getEndOfPreviousToken();
6022 Diag(EndLoc, diag::err_expected)
6023 << tok::comma << FixItHint::CreateInsertion(EndLoc, ",");
6024 } else {
6025 Diag(Tok, diag::err_expected_comma_or_rsquare);
6026 }
6027
6028 SkipUntil(tok::r_square, tok::comma, tok::identifier,
6029 StopAtSemi | StopBeforeMatch);
6030 if (Tok.is(tok::comma))
6031 ConsumeToken();
6032 else if (Tok.isNot(tok::identifier))
6033 break;
6034 }
6035 }
6036
6037 if (Tok.isNot(tok::identifier)) {
6038 Diag(Tok, diag::err_expected) << tok::identifier;
6039 break;
6040 }
6041
6042 Bindings.push_back({Tok.getIdentifierInfo(), Tok.getLocation()});
6043 ConsumeToken();
6044 }
6045
6046 if (Tok.isNot(tok::r_square))
6047 // We've already diagnosed a problem here.
6048 T.skipToEnd();
6049 else {
6050 // C++17 does not allow the identifier-list in a structured binding
6051 // to be empty.
6052 if (Bindings.empty())
6053 Diag(Tok.getLocation(), diag::ext_decomp_decl_empty);
6054
6055 T.consumeClose();
6056 }
6057
6058 return D.setDecompositionBindings(T.getOpenLocation(), Bindings,
6059 T.getCloseLocation());
6060}
6061
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006062/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
6063/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00006064/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006065/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
6066///
6067/// direct-declarator:
6068/// '(' declarator ')'
6069/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00006070/// direct-declarator '(' parameter-type-list ')'
6071/// direct-declarator '(' identifier-list[opt] ')'
6072/// [GNU] direct-declarator '(' parameter-forward-declarations
6073/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006074///
6075void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006076 BalancedDelimiterTracker T(*this, tok::l_paren);
6077 T.consumeOpen();
6078
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006079 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00006080
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00006081 // Eat any attributes before we look at whether this is a grouping or function
6082 // declarator paren. If this is a grouping paren, the attribute applies to
6083 // the type being built up, for example:
6084 // int (__attribute__(()) *x)(long y)
6085 // If this ends up not being a grouping paren, the attribute applies to the
6086 // first argument, for example:
6087 // int (__attribute__(()) int x)
6088 // In either case, we need to eat any attributes to be able to determine what
6089 // sort of paren this is.
6090 //
John McCall084e83d2011-03-24 11:26:52 +00006091 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00006092 bool RequiresArg = false;
6093 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00006094 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00006095
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00006096 // We require that the argument list (if this is a non-grouping paren) be
6097 // present even if the attribute list was empty.
6098 RequiresArg = true;
6099 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00006100
Steve Naroff44ac7772008-12-25 14:16:32 +00006101 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00006102 ParseMicrosoftTypeAttributes(attrs);
6103
Dawn Perchik335e16b2010-09-03 01:29:35 +00006104 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00006105 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00006106 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00006107
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006108 // If we haven't past the identifier yet (or where the identifier would be
6109 // stored, if this is an abstract declarator), then this is probably just
6110 // grouping parens. However, if this could be an abstract-declarator, then
6111 // this could also be the start of function arguments (consider 'void()').
6112 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00006113
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006114 if (!D.mayOmitIdentifier()) {
6115 // If this can't be an abstract-declarator, this *must* be a grouping
6116 // paren, because we haven't seen the identifier yet.
6117 isGrouping = true;
6118 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00006119 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
6120 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00006121 isDeclarationSpecifier() || // 'int(int)' is a function.
6122 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006123 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
6124 // considered to be a type, not a K&R identifier-list.
6125 isGrouping = false;
6126 } else {
6127 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
6128 isGrouping = true;
6129 }
Mike Stump11289f42009-09-09 15:08:12 +00006130
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006131 // If this is a grouping paren, handle:
6132 // direct-declarator: '(' declarator ')'
6133 // direct-declarator: '(' attributes declarator ')'
6134 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00006135 SourceLocation EllipsisLoc = D.getEllipsisLoc();
6136 D.setEllipsisLoc(SourceLocation());
6137
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00006138 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00006139 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00006140 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006141 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006142 T.consumeClose();
Erich Keanec480f302018-07-12 21:09:05 +00006143 D.AddTypeInfo(
6144 DeclaratorChunk::getParen(T.getOpenLocation(), T.getCloseLocation()),
6145 std::move(attrs), T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00006146
6147 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00006148
6149 // An ellipsis cannot be placed outside parentheses.
6150 if (EllipsisLoc.isValid())
Nikola Smiljanic69fdc9f2014-06-06 02:58:59 +00006151 DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D);
Richard Smith0efa75c2012-03-29 01:16:42 +00006152
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006153 return;
6154 }
Mike Stump11289f42009-09-09 15:08:12 +00006155
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006156 // Okay, if this wasn't a grouping paren, it must be the start of a function
6157 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00006158 // identifier (and remember where it would have been), then call into
6159 // ParseFunctionDeclarator to handle of argument list.
Craig Topper161e4db2014-05-21 06:02:52 +00006160 D.SetIdentifier(nullptr, Tok.getLocation());
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006161
David Blaikie15a430a2011-12-04 05:04:18 +00006162 // Enter function-declaration scope, limiting any declarators to the
6163 // function prototype scope, including parameter declarators.
6164 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00006165 Scope::FunctionPrototypeScope | Scope::DeclScope |
6166 (D.isFunctionDeclaratorAFunctionDeclaration()
6167 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00006168 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00006169 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006170}
6171
6172/// ParseFunctionDeclarator - We are after the identifier and have parsed the
6173/// declarator D up to a paren, which indicates that we are parsing function
6174/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00006175///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006176/// If FirstArgAttrs is non-null, then the caller parsed those arguments
6177/// immediately after the open paren - they should be considered to be the
6178/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00006179///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006180/// If RequiresArg is true, then the first argument of the function is required
6181/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00006182///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006183/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
6184/// (C++11) ref-qualifier[opt], exception-specification[opt],
6185/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
6186///
6187/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00006188/// dynamic-exception-specification
6189/// noexcept-specification
6190///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006191void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006192 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006193 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00006194 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00006195 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00006196 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00006197 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00006198 // lparen is already consumed!
6199 assert(D.isPastIdentifier() && "Should not call before identifier!");
6200
6201 // This should be true when the function has typed arguments.
6202 // Otherwise, it is treated as a K&R-style function.
6203 bool HasProto = false;
6204 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006205 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00006206 // Remember where we see an ellipsis, if any.
6207 SourceLocation EllipsisLoc;
6208
6209 DeclSpec DS(AttrFactory);
6210 bool RefQualifierIsLValueRef = true;
6211 SourceLocation RefQualifierLoc;
6212 ExceptionSpecificationType ESpecType = EST_None;
6213 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006214 SmallVector<ParsedType, 2> DynamicExceptions;
6215 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00006216 ExprResult NoexceptExpr;
Hans Wennborgdcfba332015-10-06 23:40:43 +00006217 CachedTokens *ExceptionSpecTokens = nullptr;
Aaron Ballman606093a2017-10-15 15:01:42 +00006218 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00006219 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006220
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00006221 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
6222 EndLoc is the end location for the function declarator.
6223 They differ for trailing return types. */
6224 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00006225 SourceLocation LParenLoc, RParenLoc;
6226 LParenLoc = Tracker.getOpenLocation();
6227 StartLoc = LParenLoc;
6228
Douglas Gregor9e66af42011-07-05 16:44:18 +00006229 if (isFunctionDeclaratorIdentifierList()) {
6230 if (RequiresArg)
6231 Diag(Tok, diag::err_argument_required_after_attribute);
6232
6233 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
6234
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006235 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00006236 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00006237 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00006238 EndLoc = RParenLoc;
Aaron Ballman606093a2017-10-15 15:01:42 +00006239
Fangrui Song6907ce22018-07-30 19:24:48 +00006240 // If there are attributes following the identifier list, parse them and
Aaron Ballman606093a2017-10-15 15:01:42 +00006241 // prohibit them.
6242 MaybeParseCXX11Attributes(FnAttrs);
6243 ProhibitAttributes(FnAttrs);
Douglas Gregor9e66af42011-07-05 16:44:18 +00006244 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00006245 if (Tok.isNot(tok::r_paren))
Fangrui Song6907ce22018-07-30 19:24:48 +00006246 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
Faisal Vali2b391ab2013-09-26 19:54:12 +00006247 EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00006248 else if (RequiresArg)
6249 Diag(Tok, diag::err_argument_required_after_attribute);
6250
Alexey Bader1f277942017-10-11 11:16:31 +00006251 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus
6252 || getLangOpts().OpenCL;
Douglas Gregor9e66af42011-07-05 16:44:18 +00006253
6254 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006255 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00006256 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00006257 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00006258 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00006259
David Blaikiebbafb8a2012-03-11 07:00:24 +00006260 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006261 // FIXME: Accept these components in any order, and produce fixits to
6262 // correct the order if the user gets it wrong. Ideally we should deal
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00006263 // with the pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00006264
6265 // Parse cv-qualifier-seq[opt].
Aaron Ballman08b06592014-07-22 12:44:22 +00006266 ParseTypeQualifierListOpt(DS, AR_NoAttributesParsed,
Alex Lorenz8f4d3992017-02-13 23:19:40 +00006267 /*AtomicAllowed*/ false,
6268 /*IdentifierRequired=*/false,
6269 llvm::function_ref<void()>([&]() {
6270 Actions.CodeCompleteFunctionQualifiers(DS, D);
6271 }));
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006272 if (!DS.getSourceRange().getEnd().isInvalid()) {
6273 EndLoc = DS.getSourceRange().getEnd();
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006274 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00006275
6276 // Parse ref-qualifier[opt].
Ehsan Akhgaric07d1e22015-03-25 00:53:33 +00006277 if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc))
Douglas Gregor9e66af42011-07-05 16:44:18 +00006278 EndLoc = RefQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00006279
Douglas Gregor3024f072012-04-16 07:05:22 +00006280 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00006281 // If a declaration declares a member function or member function
6282 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00006283 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00006284 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00006285 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00006286 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00006287 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00006288 getLangOpts().CPlusPlus11 &&
Richard Smith990a6922014-01-17 21:01:18 +00006289 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Faisal Vali421b2d12017-12-29 05:41:00 +00006290 (D.getContext() == DeclaratorContext::MemberContext
Richard Smithad1bbb92013-03-15 00:41:52 +00006291 ? !D.getDeclSpec().isFriendSpecified()
Faisal Vali421b2d12017-12-29 05:41:00 +00006292 : D.getContext() == DeclaratorContext::FileContext &&
Richard Smithad1bbb92013-03-15 00:41:52 +00006293 D.getCXXScopeSpec().isValid() &&
6294 Actions.CurContext->isRecord());
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00006295
6296 Qualifiers Q = Qualifiers::fromCVRUMask(DS.getTypeQualifiers());
Gauthier Harnisch796ed032019-06-14 08:56:20 +00006297 if (D.getDeclSpec().hasConstexprSpecifier() && !getLangOpts().CPlusPlus14)
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00006298 Q.addConst();
Anastasia Stulova5cffa452019-01-21 16:01:38 +00006299 // FIXME: Collect C++ address spaces.
6300 // If there are multiple different address spaces, the source is invalid.
6301 // Carry on using the first addr space for the qualifiers of 'this'.
6302 // The diagnostic will be given later while creating the function
6303 // prototype for the method.
6304 if (getLangOpts().OpenCLCPlusPlus) {
6305 for (ParsedAttr &attr : DS.getAttributes()) {
6306 LangAS ASIdx = attr.asOpenCLLangAS();
6307 if (ASIdx != LangAS::Default) {
6308 Q.addAddressSpace(ASIdx);
6309 break;
6310 }
6311 }
6312 }
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00006313
6314 Sema::CXXThisScopeRAII ThisScope(
6315 Actions, dyn_cast<CXXRecordDecl>(Actions.CurContext), Q,
6316 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00006317
Douglas Gregor9e66af42011-07-05 16:44:18 +00006318 // Parse exception-specification[opt].
Richard Smith0b3a4622014-11-13 20:01:57 +00006319 bool Delayed = D.isFirstDeclarationOfMember() &&
Richard Smith3ef3e892014-11-20 22:32:11 +00006320 D.isFunctionDeclaratorAFunctionDeclaration();
6321 if (Delayed && Actions.isLibstdcxxEagerExceptionSpecHack(D) &&
6322 GetLookAheadToken(0).is(tok::kw_noexcept) &&
6323 GetLookAheadToken(1).is(tok::l_paren) &&
6324 GetLookAheadToken(2).is(tok::kw_noexcept) &&
6325 GetLookAheadToken(3).is(tok::l_paren) &&
6326 GetLookAheadToken(4).is(tok::identifier) &&
6327 GetLookAheadToken(4).getIdentifierInfo()->isStr("swap")) {
6328 // HACK: We've got an exception-specification
6329 // noexcept(noexcept(swap(...)))
6330 // or
6331 // noexcept(noexcept(swap(...)) && noexcept(swap(...)))
6332 // on a 'swap' member function. This is a libstdc++ bug; the lookup
6333 // for 'swap' will only find the function we're currently declaring,
6334 // whereas it expects to find a non-member swap through ADL. Turn off
6335 // delayed parsing to give it a chance to find what it expects.
6336 Delayed = false;
6337 }
Richard Smith0b3a4622014-11-13 20:01:57 +00006338 ESpecType = tryParseExceptionSpecification(Delayed,
6339 ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00006340 DynamicExceptions,
6341 DynamicExceptionRanges,
Richard Smith0b3a4622014-11-13 20:01:57 +00006342 NoexceptExpr,
6343 ExceptionSpecTokens);
Douglas Gregor9e66af42011-07-05 16:44:18 +00006344 if (ESpecType != EST_None)
6345 EndLoc = ESpecRange.getEnd();
6346
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006347 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
6348 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00006349 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006350
Douglas Gregor9e66af42011-07-05 16:44:18 +00006351 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00006352 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00006353 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00006354 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Faisal Vali090da2d2018-01-01 18:23:28 +00006355 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00006356 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00006357 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00006358 SourceRange Range;
Richard Smithe303e352018-02-02 22:24:54 +00006359 TrailingReturnType =
6360 ParseTrailingReturnType(Range, D.mayBeFollowedByCXXDirectInit());
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00006361 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00006362 }
Aaron Ballman606093a2017-10-15 15:01:42 +00006363 } else if (standardAttributesAllowed()) {
6364 MaybeParseCXX11Attributes(FnAttrs);
Douglas Gregor9e66af42011-07-05 16:44:18 +00006365 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00006366 }
6367
Reid Kleckner078aea92016-12-09 17:14:05 +00006368 // Collect non-parameter declarations from the prototype if this is a function
6369 // declaration. They will be moved into the scope of the function. Only do
6370 // this in C and not C++, where the decls will continue to live in the
6371 // surrounding context.
6372 SmallVector<NamedDecl *, 0> DeclsInPrototype;
6373 if (getCurScope()->getFlags() & Scope::FunctionDeclarationScope &&
6374 !getLangOpts().CPlusPlus) {
6375 for (Decl *D : getCurScope()->decls()) {
6376 NamedDecl *ND = dyn_cast<NamedDecl>(D);
6377 if (!ND || isa<ParmVarDecl>(ND))
6378 continue;
6379 DeclsInPrototype.push_back(ND);
6380 }
6381 }
6382
Douglas Gregor9e66af42011-07-05 16:44:18 +00006383 // Remember that we parsed a function type, and remember the attributes.
Erich Keanec480f302018-07-12 21:09:05 +00006384 D.AddTypeInfo(DeclaratorChunk::getFunction(
6385 HasProto, IsAmbiguous, LParenLoc, ParamInfo.data(),
6386 ParamInfo.size(), EllipsisLoc, RParenLoc,
Anastasia Stulovaa9bc4bd2019-01-09 11:25:09 +00006387 RefQualifierIsLValueRef, RefQualifierLoc,
6388 /*MutableLoc=*/SourceLocation(),
6389 ESpecType, ESpecRange, DynamicExceptions.data(),
6390 DynamicExceptionRanges.data(), DynamicExceptions.size(),
Erich Keanec480f302018-07-12 21:09:05 +00006391 NoexceptExpr.isUsable() ? NoexceptExpr.get() : nullptr,
6392 ExceptionSpecTokens, DeclsInPrototype, StartLoc,
Anastasia Stulovaa9bc4bd2019-01-09 11:25:09 +00006393 LocalEndLoc, D, TrailingReturnType, &DS),
Erich Keanec480f302018-07-12 21:09:05 +00006394 std::move(FnAttrs), EndLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00006395}
6396
Ehsan Akhgaric07d1e22015-03-25 00:53:33 +00006397/// ParseRefQualifier - Parses a member function ref-qualifier. Returns
6398/// true if a ref-qualifier is found.
6399bool Parser::ParseRefQualifier(bool &RefQualifierIsLValueRef,
6400 SourceLocation &RefQualifierLoc) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00006401 if (Tok.isOneOf(tok::amp, tok::ampamp)) {
Ehsan Akhgaric07d1e22015-03-25 00:53:33 +00006402 Diag(Tok, getLangOpts().CPlusPlus11 ?
6403 diag::warn_cxx98_compat_ref_qualifier :
6404 diag::ext_ref_qualifier);
6405
6406 RefQualifierIsLValueRef = Tok.is(tok::amp);
6407 RefQualifierLoc = ConsumeToken();
6408 return true;
6409 }
6410 return false;
6411}
6412
Douglas Gregor9e66af42011-07-05 16:44:18 +00006413/// isFunctionDeclaratorIdentifierList - This parameter list may have an
6414/// identifier list form for a K&R-style function: void foo(a,b,c)
6415///
6416/// Note that identifier-lists are only allowed for normal declarators, not for
6417/// abstract-declarators.
6418bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00006419 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00006420 && Tok.is(tok::identifier)
6421 && !TryAltiVecVectorToken()
6422 // K&R identifier lists can't have typedefs as identifiers, per C99
6423 // 6.7.5.3p11.
6424 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
6425 // Identifier lists follow a really simple grammar: the identifiers can
6426 // be followed *only* by a ", identifier" or ")". However, K&R
6427 // identifier lists are really rare in the brave new modern world, and
6428 // it is very common for someone to typo a type in a non-K&R style
6429 // list. If we are presented with something like: "void foo(intptr x,
6430 // float y)", we don't want to start parsing the function declarator as
6431 // though it is a K&R style declarator just because intptr is an
6432 // invalid type.
6433 //
6434 // To handle this, we check to see if the token after the first
6435 // identifier is a "," or ")". Only then do we parse it as an
6436 // identifier list.
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +00006437 && (!Tok.is(tok::eof) &&
6438 (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)));
Douglas Gregor9e66af42011-07-05 16:44:18 +00006439}
6440
6441/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
6442/// we found a K&R-style identifier list instead of a typed parameter list.
6443///
6444/// After returning, ParamInfo will hold the parsed parameters.
6445///
6446/// identifier-list: [C99 6.7.5]
6447/// identifier
6448/// identifier-list ',' identifier
6449///
6450void Parser::ParseFunctionDeclaratorIdentifierList(
6451 Declarator &D,
Craig Topper5603df42013-07-05 19:34:19 +00006452 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00006453 // If there was no identifier specified for the declarator, either we are in
6454 // an abstract-declarator, or we are in a parameter declarator which was found
6455 // to be abstract. In abstract-declarators, identifier lists are not valid:
6456 // diagnose this.
6457 if (!D.getIdentifier())
6458 Diag(Tok, diag::ext_ident_list_in_param);
6459
6460 // Maintain an efficient lookup of params we have seen so far.
6461 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
6462
Alp Tokera3ebe6e2013-12-17 14:12:37 +00006463 do {
Douglas Gregor9e66af42011-07-05 16:44:18 +00006464 // If this isn't an identifier, report the error and skip until ')'.
6465 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00006466 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00006467 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor9e66af42011-07-05 16:44:18 +00006468 // Forget we parsed anything.
6469 ParamInfo.clear();
6470 return;
6471 }
6472
6473 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
6474
6475 // Reject 'typedef int y; int test(x, y)', but continue parsing.
6476 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
6477 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
6478
6479 // Verify that the argument identifier has not already been mentioned.
David Blaikie82e95a32014-11-19 07:49:47 +00006480 if (!ParamsSoFar.insert(ParmII).second) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00006481 Diag(Tok, diag::err_param_redefinition) << ParmII;
6482 } else {
6483 // Remember this identifier in ParamInfo.
6484 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
6485 Tok.getLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00006486 nullptr));
Douglas Gregor9e66af42011-07-05 16:44:18 +00006487 }
6488
6489 // Eat the identifier.
6490 ConsumeToken();
Douglas Gregor9e66af42011-07-05 16:44:18 +00006491 // The list continues if we see a comma.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00006492 } while (TryConsumeToken(tok::comma));
Douglas Gregor9e66af42011-07-05 16:44:18 +00006493}
6494
6495/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
6496/// after the opening parenthesis. This function will not parse a K&R-style
6497/// identifier list.
6498///
Richard Smith2620cd92012-04-11 04:01:28 +00006499/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
6500/// caller parsed those arguments immediately after the open paren - they should
6501/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00006502///
6503/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
6504/// be the location of the ellipsis, if any was parsed.
6505///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00006506/// parameter-type-list: [C99 6.7.5]
6507/// parameter-list
6508/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00006509/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00006510///
6511/// parameter-list: [C99 6.7.5]
6512/// parameter-declaration
6513/// parameter-list ',' parameter-declaration
6514///
6515/// parameter-declaration: [C99 6.7.5]
6516/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00006517/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00006518/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00006519/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00006520/// declaration-specifiers abstract-declarator[opt]
6521/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00006522/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00006523/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00006524/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00006525///
Douglas Gregor9e66af42011-07-05 16:44:18 +00006526void Parser::ParseParameterDeclarationClause(
6527 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00006528 ParsedAttributes &FirstArgAttrs,
Craig Topper5603df42013-07-05 19:34:19 +00006529 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00006530 SourceLocation &EllipsisLoc) {
Alp Tokera3ebe6e2013-12-17 14:12:37 +00006531 do {
6532 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
6533 // before deciding this was a parameter-declaration-clause.
6534 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
Chris Lattner371ed4e2008-04-06 06:57:35 +00006535 break;
Mike Stump11289f42009-09-09 15:08:12 +00006536
Chris Lattner371ed4e2008-04-06 06:57:35 +00006537 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00006538 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00006539 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006540
Richard Smith2620cd92012-04-11 04:01:28 +00006541 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00006542 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00006543
John McCall53fa7142010-12-24 02:08:15 +00006544 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00006545 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00006546
6547 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00006548
6549 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00006550 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00006551 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00006552 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
6553 // too much hassle.
6554 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00006555
Faisal Valia534f072018-04-26 00:42:40 +00006556 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00006557
Faisal Vali2b391ab2013-09-26 19:54:12 +00006558
Fangrui Song6907ce22018-07-30 19:24:48 +00006559 // Parse the declarator. This is "PrototypeContext" or
6560 // "LambdaExprParameterContext", because we must accept either
Faisal Vali2b391ab2013-09-26 19:54:12 +00006561 // 'declarator' or 'abstract-declarator' here.
Faisal Vali421b2d12017-12-29 05:41:00 +00006562 Declarator ParmDeclarator(
6563 DS, D.getContext() == DeclaratorContext::LambdaExprContext
6564 ? DeclaratorContext::LambdaExprParameterContext
6565 : DeclaratorContext::PrototypeContext);
Faisal Vali2b391ab2013-09-26 19:54:12 +00006566 ParseDeclarator(ParmDeclarator);
Chris Lattner371ed4e2008-04-06 06:57:35 +00006567
6568 // Parse GNU attributes, if present.
Faisal Vali2b391ab2013-09-26 19:54:12 +00006569 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump11289f42009-09-09 15:08:12 +00006570
Chris Lattner371ed4e2008-04-06 06:57:35 +00006571 // Remember this parsed parameter in ParamInfo.
Faisal Vali2b391ab2013-09-26 19:54:12 +00006572 IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00006573
Douglas Gregor4d87df52008-12-16 21:30:33 +00006574 // DefArgToks is used when the parsing of default arguments needs
6575 // to be delayed.
Malcolm Parsonsff0382c2016-11-17 17:52:58 +00006576 std::unique_ptr<CachedTokens> DefArgToks;
Douglas Gregor4d87df52008-12-16 21:30:33 +00006577
Chris Lattner371ed4e2008-04-06 06:57:35 +00006578 // If no parameter was specified, verify that *something* was specified,
6579 // otherwise we have a missing type and identifier.
Craig Topper161e4db2014-05-21 06:02:52 +00006580 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == nullptr &&
Faisal Vali2b391ab2013-09-26 19:54:12 +00006581 ParmDeclarator.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00006582 // Completely missing, emit error.
6583 Diag(DSStart, diag::err_missing_param);
6584 } else {
6585 // Otherwise, we have something. Add it and let semantic analysis try
6586 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00006587
Richard Smith36ee9fb2014-08-11 23:30:23 +00006588 // Last chance to recover from a misplaced ellipsis in an attempted
6589 // parameter pack declaration.
6590 if (Tok.is(tok::ellipsis) &&
6591 (NextToken().isNot(tok::r_paren) ||
6592 (!ParmDeclarator.getEllipsisLoc().isValid() &&
6593 !Actions.isUnexpandedParameterPackPermitted())) &&
6594 Actions.containsUnexpandedParameterPacks(ParmDeclarator))
6595 DiagnoseMisplacedEllipsisInDeclarator(ConsumeToken(), ParmDeclarator);
6596
Chris Lattner371ed4e2008-04-06 06:57:35 +00006597 // Inform the actions module about the parameter declarator, so it gets
6598 // added to the current scope.
Richard Smith95e1fb02014-08-27 03:23:12 +00006599 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00006600 // Parse the default argument, if any. We parse the default
6601 // arguments in all dialects; the semantic analysis in
6602 // ActOnParamDefaultArgument will reject the default argument in
6603 // C.
6604 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00006605 SourceLocation EqualLoc = Tok.getLocation();
6606
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00006607 // Parse the default argument
Faisal Vali421b2d12017-12-29 05:41:00 +00006608 if (D.getContext() == DeclaratorContext::MemberContext) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00006609 // If we're inside a class definition, cache the tokens
6610 // corresponding to the default argument. We'll actually parse
6611 // them when we see the end of the class definition.
Malcolm Parsonsff0382c2016-11-17 17:52:58 +00006612 DefArgToks.reset(new CachedTokens);
Douglas Gregor4d87df52008-12-16 21:30:33 +00006613
David Majnemer906ed272015-01-13 05:28:24 +00006614 SourceLocation ArgStartLoc = NextToken().getLocation();
Richard Smith1fff95c2013-09-12 23:28:08 +00006615 if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
Malcolm Parsonsff0382c2016-11-17 17:52:58 +00006616 DefArgToks.reset();
Serge Pavlovb4b35782014-07-22 01:54:49 +00006617 Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00006618 } else {
Mike Stump11289f42009-09-09 15:08:12 +00006619 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
David Majnemer906ed272015-01-13 05:28:24 +00006620 ArgStartLoc);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00006621 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00006622 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00006623 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00006624 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00006625
Chad Rosierc1183952012-06-26 22:30:43 +00006626 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00006627 // used.
Faisal Valid143a0c2017-04-01 21:30:49 +00006628 EnterExpressionEvaluationContext Eval(
6629 Actions,
6630 Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed,
6631 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00006632
Sebastian Redldb63af22012-03-14 15:54:00 +00006633 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00006634 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00006635 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00006636 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00006637 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00006638 DefArgResult = ParseAssignmentExpression();
Kaelyn Takatab16e6322014-11-20 22:06:40 +00006639 DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult);
Douglas Gregor4d87df52008-12-16 21:30:33 +00006640 if (DefArgResult.isInvalid()) {
Serge Pavlovb4b35782014-07-22 01:54:49 +00006641 Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
Alexey Bataevee6507d2013-11-18 08:17:37 +00006642 SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor4d87df52008-12-16 21:30:33 +00006643 } else {
6644 // Inform the actions module about the default argument
6645 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006646 DefArgResult.get());
Douglas Gregor4d87df52008-12-16 21:30:33 +00006647 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00006648 }
6649 }
Mike Stump11289f42009-09-09 15:08:12 +00006650
6651 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Fangrui Song6907ce22018-07-30 19:24:48 +00006652 ParmDeclarator.getIdentifierLoc(),
Malcolm Parsonsff0382c2016-11-17 17:52:58 +00006653 Param, std::move(DefArgToks)));
Chris Lattner371ed4e2008-04-06 06:57:35 +00006654 }
6655
Richard Smith36ee9fb2014-08-11 23:30:23 +00006656 if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) {
6657 if (!getLangOpts().CPlusPlus) {
6658 // We have ellipsis without a preceding ',', which is ill-formed
6659 // in C. Complain and provide the fix.
6660 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
6661 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
6662 } else if (ParmDeclarator.getEllipsisLoc().isValid() ||
6663 Actions.containsUnexpandedParameterPacks(ParmDeclarator)) {
6664 // It looks like this was supposed to be a parameter pack. Warn and
6665 // point out where the ellipsis should have gone.
6666 SourceLocation ParmEllipsis = ParmDeclarator.getEllipsisLoc();
6667 Diag(EllipsisLoc, diag::warn_misplaced_ellipsis_vararg)
6668 << ParmEllipsis.isValid() << ParmEllipsis;
6669 if (ParmEllipsis.isValid()) {
6670 Diag(ParmEllipsis,
6671 diag::note_misplaced_ellipsis_vararg_existing_ellipsis);
6672 } else {
6673 Diag(ParmDeclarator.getIdentifierLoc(),
6674 diag::note_misplaced_ellipsis_vararg_add_ellipsis)
6675 << FixItHint::CreateInsertion(ParmDeclarator.getIdentifierLoc(),
6676 "...")
6677 << !ParmDeclarator.hasName();
6678 }
6679 Diag(EllipsisLoc, diag::note_misplaced_ellipsis_vararg_add_comma)
Alp Tokera3ebe6e2013-12-17 14:12:37 +00006680 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Richard Smith36ee9fb2014-08-11 23:30:23 +00006681 }
6682
6683 // We can't have any more parameters after an ellipsis.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00006684 break;
6685 }
Mike Stump11289f42009-09-09 15:08:12 +00006686
Alp Tokera3ebe6e2013-12-17 14:12:37 +00006687 // If the next token is a comma, consume it and keep reading arguments.
6688 } while (TryConsumeToken(tok::comma));
Chris Lattner6c940e62008-04-06 06:34:08 +00006689}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006690
Chris Lattnere8074e62006-08-06 18:30:15 +00006691/// [C90] direct-declarator '[' constant-expression[opt] ']'
6692/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
6693/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
6694/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
6695/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006696/// [C++11] direct-declarator '[' constant-expression[opt] ']'
6697/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00006698void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006699 if (CheckProhibitedCXX11Attribute())
6700 return;
6701
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006702 BalancedDelimiterTracker T(*this, tok::l_square);
6703 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00006704
Chris Lattner84a11622008-12-18 07:27:21 +00006705 // C array syntax has many features, but by-far the most common is [] and [4].
6706 // This code does a fast path to handle some of the most obvious cases.
6707 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006708 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00006709 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00006710 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00006711
Chris Lattner84a11622008-12-18 07:27:21 +00006712 // Remember that we parsed the empty array type.
Craig Topper161e4db2014-05-21 06:02:52 +00006713 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, nullptr,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006714 T.getOpenLocation(),
6715 T.getCloseLocation()),
Erich Keanec480f302018-07-12 21:09:05 +00006716 std::move(attrs), T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00006717 return;
6718 } else if (Tok.getKind() == tok::numeric_constant &&
6719 GetLookAheadToken(1).is(tok::r_square)) {
6720 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00006721 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00006722 ConsumeToken();
6723
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006724 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00006725 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00006726 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00006727
Chris Lattner84a11622008-12-18 07:27:21 +00006728 // Remember that we parsed a array type, and remember its features.
Erich Keanec480f302018-07-12 21:09:05 +00006729 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, ExprRes.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006730 T.getOpenLocation(),
6731 T.getCloseLocation()),
Erich Keanec480f302018-07-12 21:09:05 +00006732 std::move(attrs), T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00006733 return;
Benjamin Kramer72dae622016-02-18 15:30:24 +00006734 } else if (Tok.getKind() == tok::code_completion) {
6735 Actions.CodeCompleteBracketDeclarator(getCurScope());
6736 return cutOffParsing();
Chris Lattner84a11622008-12-18 07:27:21 +00006737 }
Mike Stump11289f42009-09-09 15:08:12 +00006738
Chris Lattnere8074e62006-08-06 18:30:15 +00006739 // If valid, this location is the position where we read the 'static' keyword.
6740 SourceLocation StaticLoc;
Alp Tokera3ebe6e2013-12-17 14:12:37 +00006741 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00006742
Chris Lattnere8074e62006-08-06 18:30:15 +00006743 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00006744 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00006745 DeclSpec DS(AttrFactory);
Aaron Ballman08b06592014-07-22 12:44:22 +00006746 ParseTypeQualifierListOpt(DS, AR_CXX11AttributesParsed);
Mike Stump11289f42009-09-09 15:08:12 +00006747
Chris Lattnere8074e62006-08-06 18:30:15 +00006748 // If we haven't already read 'static', check to see if there is one after the
6749 // type-qualifier-list.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00006750 if (!StaticLoc.isValid())
6751 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00006752
Chris Lattnere8074e62006-08-06 18:30:15 +00006753 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00006754 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00006755 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00006756
Chris Lattner521ff2b2008-04-06 05:26:30 +00006757 // Handle the case where we have '[*]' as the array size. However, a leading
6758 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00006759 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00006760 // infrequent, use of lookahead is not costly here.
6761 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00006762 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00006763
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00006764 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00006765 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00006766 StaticLoc = SourceLocation(); // Drop the static.
6767 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00006768 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00006769 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00006770 // Note, in C89, this production uses the constant-expr production instead
6771 // of assignment-expr. The only difference is that assignment-expr allows
6772 // things like '=' and '*='. Sema rejects these in C89 mode because they
6773 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00006774
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00006775 // Parse the constant-expression or assignment-expression now (depending
6776 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00006777 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00006778 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00006779 } else {
Faisal Valid143a0c2017-04-01 21:30:49 +00006780 EnterExpressionEvaluationContext Unevaluated(
6781 Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
Kaelyn Takata15867822014-11-21 18:48:04 +00006782 NumElements =
6783 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Eli Friedmane0afc982012-01-21 01:01:51 +00006784 }
David Majnemerf9834d52014-08-08 07:21:18 +00006785 } else {
6786 if (StaticLoc.isValid()) {
6787 Diag(StaticLoc, diag::err_unspecified_size_with_static);
6788 StaticLoc = SourceLocation(); // Drop the static.
6789 }
Chris Lattner62591722006-08-12 18:40:58 +00006790 }
Mike Stump11289f42009-09-09 15:08:12 +00006791
Chris Lattner62591722006-08-12 18:40:58 +00006792 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00006793 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00006794 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00006795 // If the expression was invalid, skip it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00006796 SkipUntil(tok::r_square, StopAtSemi);
Chris Lattner62591722006-08-12 18:40:58 +00006797 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00006798 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00006799
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006800 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00006801
Jordan Rose303e2f12016-11-10 23:28:17 +00006802 MaybeParseCXX11Attributes(DS.getAttributes());
Alexis Hunt96d5c762009-11-21 08:43:09 +00006803
Chris Lattner84a11622008-12-18 07:27:21 +00006804 // Remember that we parsed a array type, and remember its features.
Erich Keanec480f302018-07-12 21:09:05 +00006805 D.AddTypeInfo(
6806 DeclaratorChunk::getArray(DS.getTypeQualifiers(), StaticLoc.isValid(),
6807 isStar, NumElements.get(), T.getOpenLocation(),
6808 T.getCloseLocation()),
6809 std::move(DS.getAttributes()), T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00006810}
6811
Richard Trieuf4b81d02014-06-24 23:14:24 +00006812/// Diagnose brackets before an identifier.
6813void Parser::ParseMisplacedBracketDeclarator(Declarator &D) {
6814 assert(Tok.is(tok::l_square) && "Missing opening bracket");
6815 assert(!D.mayOmitIdentifier() && "Declarator cannot omit identifier");
6816
6817 SourceLocation StartBracketLoc = Tok.getLocation();
6818 Declarator TempDeclarator(D.getDeclSpec(), D.getContext());
6819
6820 while (Tok.is(tok::l_square)) {
6821 ParseBracketDeclarator(TempDeclarator);
6822 }
6823
6824 // Stuff the location of the start of the brackets into the Declarator.
6825 // The diagnostics from ParseDirectDeclarator will make more sense if
6826 // they use this location instead.
6827 if (Tok.is(tok::semi))
6828 D.getName().EndLocation = StartBracketLoc;
6829
6830 SourceLocation SuggestParenLoc = Tok.getLocation();
6831
6832 // Now that the brackets are removed, try parsing the declarator again.
6833 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
6834
6835 // Something went wrong parsing the brackets, in which case,
6836 // ParseBracketDeclarator has emitted an error, and we don't need to emit
6837 // one here.
6838 if (TempDeclarator.getNumTypeObjects() == 0)
6839 return;
6840
6841 // Determine if parens will need to be suggested in the diagnostic.
6842 bool NeedParens = false;
6843 if (D.getNumTypeObjects() != 0) {
6844 switch (D.getTypeObject(D.getNumTypeObjects() - 1).Kind) {
6845 case DeclaratorChunk::Pointer:
6846 case DeclaratorChunk::Reference:
6847 case DeclaratorChunk::BlockPointer:
6848 case DeclaratorChunk::MemberPointer:
Xiuli Pan9c14e282016-01-09 12:53:17 +00006849 case DeclaratorChunk::Pipe:
Richard Trieuf4b81d02014-06-24 23:14:24 +00006850 NeedParens = true;
6851 break;
6852 case DeclaratorChunk::Array:
6853 case DeclaratorChunk::Function:
6854 case DeclaratorChunk::Paren:
6855 break;
6856 }
6857 }
6858
6859 if (NeedParens) {
6860 // Create a DeclaratorChunk for the inserted parens.
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006861 SourceLocation EndLoc = PP.getLocForEndOfToken(D.getEndLoc());
Erich Keanec480f302018-07-12 21:09:05 +00006862 D.AddTypeInfo(DeclaratorChunk::getParen(SuggestParenLoc, EndLoc),
Richard Trieuf4b81d02014-06-24 23:14:24 +00006863 SourceLocation());
6864 }
6865
6866 // Adding back the bracket info to the end of the Declarator.
6867 for (unsigned i = 0, e = TempDeclarator.getNumTypeObjects(); i < e; ++i) {
6868 const DeclaratorChunk &Chunk = TempDeclarator.getTypeObject(i);
Erich Keanec480f302018-07-12 21:09:05 +00006869 D.AddTypeInfo(Chunk, SourceLocation());
Richard Trieuf4b81d02014-06-24 23:14:24 +00006870 }
6871
6872 // The missing identifier would have been diagnosed in ParseDirectDeclarator.
6873 // If parentheses are required, always suggest them.
6874 if (!D.getIdentifier() && !NeedParens)
6875 return;
6876
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006877 SourceLocation EndBracketLoc = TempDeclarator.getEndLoc();
Richard Trieuf4b81d02014-06-24 23:14:24 +00006878
6879 // Generate the move bracket error message.
6880 SourceRange BracketRange(StartBracketLoc, EndBracketLoc);
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006881 SourceLocation EndLoc = PP.getLocForEndOfToken(D.getEndLoc());
Richard Trieuf4b81d02014-06-24 23:14:24 +00006882
6883 if (NeedParens) {
6884 Diag(EndLoc, diag::err_brackets_go_after_unqualified_id)
6885 << getLangOpts().CPlusPlus
6886 << FixItHint::CreateInsertion(SuggestParenLoc, "(")
6887 << FixItHint::CreateInsertion(EndLoc, ")")
6888 << FixItHint::CreateInsertionFromRange(
6889 EndLoc, CharSourceRange(BracketRange, true))
6890 << FixItHint::CreateRemoval(BracketRange);
6891 } else {
6892 Diag(EndLoc, diag::err_brackets_go_after_unqualified_id)
6893 << getLangOpts().CPlusPlus
6894 << FixItHint::CreateInsertionFromRange(
6895 EndLoc, CharSourceRange(BracketRange, true))
6896 << FixItHint::CreateRemoval(BracketRange);
6897 }
6898}
6899
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00006900/// [GNU] typeof-specifier:
6901/// typeof ( expressions )
6902/// typeof ( type-name )
6903/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00006904///
6905void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00006906 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006907 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00006908 SourceLocation StartLoc = ConsumeToken();
6909
John McCalle8595032010-01-13 20:03:27 +00006910 const bool hasParens = Tok.is(tok::l_paren);
6911
Faisal Valid143a0c2017-04-01 21:30:49 +00006912 EnterExpressionEvaluationContext Unevaluated(
6913 Actions, Sema::ExpressionEvaluationContext::Unevaluated,
6914 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00006915
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006916 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00006917 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006918 SourceRange CastRange;
Kaelyn Takata911260742014-12-19 01:28:40 +00006919 ExprResult Operand = Actions.CorrectDelayedTyposInExpr(
6920 ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, CastTy, CastRange));
John McCalle8595032010-01-13 20:03:27 +00006921 if (hasParens)
6922 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006923
6924 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00006925 // FIXME: Not accurate, the range gets one token more than it should.
6926 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006927 else
6928 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00006929
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006930 if (isCastExpr) {
6931 if (!CastTy) {
6932 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00006933 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00006934 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00006935
Craig Topper161e4db2014-05-21 06:02:52 +00006936 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +00006937 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006938 // Check for duplicate type specifiers (e.g. "int typeof(int)").
6939 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00006940 DiagID, CastTy,
6941 Actions.getASTContext().getPrintingPolicy()))
John McCall49bfce42009-08-03 20:12:06 +00006942 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006943 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00006944 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00006945
Hiroshi Inoue533777f2017-07-02 06:12:49 +00006946 // If we get here, the operand to the typeof was an expression.
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00006947 if (Operand.isInvalid()) {
6948 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00006949 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00006950 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00006951
Eli Friedmane0afc982012-01-21 01:01:51 +00006952 // We might need to transform the operand if it is potentially evaluated.
6953 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
6954 if (Operand.isInvalid()) {
6955 DS.SetTypeSpecError();
6956 return;
6957 }
6958
Craig Topper161e4db2014-05-21 06:02:52 +00006959 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +00006960 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00006961 // Check for duplicate type specifiers (e.g. "int typeof(int)").
6962 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00006963 DiagID, Operand.get(),
6964 Actions.getASTContext().getPrintingPolicy()))
John McCall49bfce42009-08-03 20:12:06 +00006965 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00006966}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00006967
Benjamin Kramere56f3932011-12-23 17:00:35 +00006968/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00006969/// _Atomic ( type-name )
6970///
6971void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00006972 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
6973 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00006974
6975 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006976 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00006977 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00006978 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00006979
6980 TypeResult Result = ParseTypeName();
6981 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00006982 SkipUntil(tok::r_paren, StopAtSemi);
Eli Friedman0dfb8892011-10-06 23:00:33 +00006983 return;
6984 }
6985
6986 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006987 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00006988
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006989 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00006990 return;
6991
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006992 DS.setTypeofParensRange(T.getRange());
6993 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00006994
Craig Topper161e4db2014-05-21 06:02:52 +00006995 const char *PrevSpec = nullptr;
Eli Friedman0dfb8892011-10-06 23:00:33 +00006996 unsigned DiagID;
6997 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006998 DiagID, Result.get(),
Erik Verbruggen888d52a2014-01-15 09:15:43 +00006999 Actions.getASTContext().getPrintingPolicy()))
Eli Friedman0dfb8892011-10-06 23:00:33 +00007000 Diag(StartLoc, DiagID) << PrevSpec;
7001}
7002
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007003/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
7004/// from TryAltiVecVectorToken.
7005bool Parser::TryAltiVecVectorTokenOutOfLine() {
7006 Token Next = NextToken();
7007 switch (Next.getKind()) {
7008 default: return false;
7009 case tok::kw_short:
7010 case tok::kw_long:
7011 case tok::kw_signed:
7012 case tok::kw_unsigned:
7013 case tok::kw_void:
7014 case tok::kw_char:
7015 case tok::kw_int:
7016 case tok::kw_float:
7017 case tok::kw_double:
7018 case tok::kw_bool:
Bill Seurercf2c96b2015-01-12 19:35:51 +00007019 case tok::kw___bool:
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007020 case tok::kw___pixel:
7021 Tok.setKind(tok::kw___vector);
7022 return true;
7023 case tok::identifier:
7024 if (Next.getIdentifierInfo() == Ident_pixel) {
7025 Tok.setKind(tok::kw___vector);
7026 return true;
7027 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00007028 if (Next.getIdentifierInfo() == Ident_bool) {
7029 Tok.setKind(tok::kw___vector);
7030 return true;
7031 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007032 return false;
7033 }
7034}
7035
7036bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
7037 const char *&PrevSpec, unsigned &DiagID,
7038 bool &isInvalid) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00007039 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007040 if (Tok.getIdentifierInfo() == Ident_vector) {
7041 Token Next = NextToken();
7042 switch (Next.getKind()) {
7043 case tok::kw_short:
7044 case tok::kw_long:
7045 case tok::kw_signed:
7046 case tok::kw_unsigned:
7047 case tok::kw_void:
7048 case tok::kw_char:
7049 case tok::kw_int:
7050 case tok::kw_float:
7051 case tok::kw_double:
7052 case tok::kw_bool:
Bill Seurercf2c96b2015-01-12 19:35:51 +00007053 case tok::kw___bool:
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007054 case tok::kw___pixel:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00007055 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007056 return true;
7057 case tok::identifier:
7058 if (Next.getIdentifierInfo() == Ident_pixel) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00007059 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy);
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007060 return true;
7061 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00007062 if (Next.getIdentifierInfo() == Ident_bool) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00007063 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy);
Bill Schmidt99a084b2013-07-03 20:54:09 +00007064 return true;
7065 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007066 break;
7067 default:
7068 break;
7069 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00007070 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007071 DS.isTypeAltiVecVector()) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00007072 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007073 return true;
Bill Schmidt99a084b2013-07-03 20:54:09 +00007074 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
7075 DS.isTypeAltiVecVector()) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00007076 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy);
Bill Schmidt99a084b2013-07-03 20:54:09 +00007077 return true;
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007078 }
7079 return false;
7080}