blob: 3de52ce8754f2500431cee3cf8b7781be5898acf [file] [log] [blame]
Hans Wennborgdcfba332015-10-06 23:40:43 +00001//===--- ParseDecl.cpp - Declaration Parsing --------------------*- C++ -*-===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Declaration portions of the Parser interfaces.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000014#include "clang/Parse/RAIIObjectsForParser.h"
Erik Verbruggen888d52a2014-01-15 09:15:43 +000015#include "clang/AST/ASTContext.h"
Chandler Carruth757fcd62014-03-04 10:05:20 +000016#include "clang/AST/DeclTemplate.h"
Jordan Rose1e879d82018-03-23 00:07:18 +000017#include "clang/AST/PrettyDeclStackTrace.h"
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000018#include "clang/Basic/AddressSpaces.h"
Aaron Ballmanfdd783a2014-03-31 18:18:43 +000019#include "clang/Basic/Attributes.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000020#include "clang/Basic/CharInfo.h"
Aaron Ballmanfdd783a2014-03-31 18:18:43 +000021#include "clang/Basic/TargetInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrain031643e2012-04-26 23:36:17 +000023#include "clang/Sema/Lookup.h"
John McCall8b0666c2010-08-20 18:27:03 +000024#include "clang/Sema/ParsedTemplate.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000025#include "clang/Sema/Scope.h"
George Burgess IVa19ea342016-11-10 20:43:52 +000026#include "llvm/ADT/Optional.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000027#include "llvm/ADT/SmallSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000028#include "llvm/ADT/SmallString.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000029#include "llvm/ADT/StringSwitch.h"
Hans Wennborgdcfba332015-10-06 23:40:43 +000030
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000031using namespace clang;
32
33//===----------------------------------------------------------------------===//
34// C99 6.7: Declarations.
35//===----------------------------------------------------------------------===//
36
Chris Lattnerf5fbd792006-08-10 23:56:11 +000037/// ParseTypeName
38/// type-name: [C99 6.7.6]
39/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000040///
41/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000042TypeResult Parser::ParseTypeName(SourceRange *Range,
Faisal Vali421b2d12017-12-29 05:41:00 +000043 DeclaratorContext Context,
Richard Smithcd1c0552011-07-01 19:46:12 +000044 AccessSpecifier AS,
Richard Smith54ecd982013-02-20 19:22:51 +000045 Decl **OwnedType,
46 ParsedAttributes *Attrs) {
Richard Smith62dad822012-03-15 01:02:11 +000047 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Faisal Vali7db85c52017-12-31 00:06:40 +000048 if (DSC == DeclSpecContext::DSC_normal)
49 DSC = DeclSpecContext::DSC_type_specifier;
Richard Smithbfdb1082012-03-12 08:56:40 +000050
Chris Lattnerf5fbd792006-08-10 23:56:11 +000051 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000052 DeclSpec DS(AttrFactory);
Richard Smith54ecd982013-02-20 19:22:51 +000053 if (Attrs)
Erich Keanec480f302018-07-12 21:09:05 +000054 DS.addAttributes(*Attrs);
Richard Smithbfdb1082012-03-12 08:56:40 +000055 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithcd1c0552011-07-01 19:46:12 +000056 if (OwnedType)
Craig Topper161e4db2014-05-21 06:02:52 +000057 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : nullptr;
Sebastian Redld6434562009-05-29 18:02:33 +000058
Chris Lattnerf5fbd792006-08-10 23:56:11 +000059 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000060 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000061 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000062 if (Range)
63 *Range = DeclaratorInfo.getSourceRange();
64
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000065 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000066 return true;
67
Douglas Gregor0be31a22010-07-02 17:43:08 +000068 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000069}
70
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000071/// Normalizes an attribute name by dropping prefixed and suffixed __.
George Burgess IV779af822017-06-30 22:33:24 +000072static StringRef normalizeAttrName(StringRef Name) {
73 if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__"))
74 return Name.drop_front(2).drop_back(2);
75 return Name;
76}
77
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000078/// isAttributeLateParsed - Return true if the attribute has arguments that
79/// require late parsing.
80static bool isAttributeLateParsed(const IdentifierInfo &II) {
Aaron Ballman35db2b32014-01-29 22:13:45 +000081#define CLANG_ATTR_LATE_PARSED_LIST
George Burgess IV779af822017-06-30 22:33:24 +000082 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Aaron Ballman35db2b32014-01-29 22:13:45 +000083#include "clang/Parse/AttrParserStringSwitches.inc"
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000084 .Default(false);
Aaron Ballman35db2b32014-01-29 22:13:45 +000085#undef CLANG_ATTR_LATE_PARSED_LIST
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000086}
87
Leonard Chanc72aaf62019-05-07 03:20:17 +000088/// Check if the a start and end source location expand to the same macro.
Benjamin Kramerdc5f8052019-08-23 19:59:23 +000089static bool FindLocsWithCommonFileID(Preprocessor &PP, SourceLocation StartLoc,
90 SourceLocation EndLoc) {
Leonard Chanc72aaf62019-05-07 03:20:17 +000091 if (!StartLoc.isMacroID() || !EndLoc.isMacroID())
92 return false;
93
94 SourceManager &SM = PP.getSourceManager();
95 if (SM.getFileID(StartLoc) != SM.getFileID(EndLoc))
96 return false;
97
98 bool AttrStartIsInMacro =
99 Lexer::isAtStartOfMacroExpansion(StartLoc, SM, PP.getLangOpts());
100 bool AttrEndIsInMacro =
101 Lexer::isAtEndOfMacroExpansion(EndLoc, SM, PP.getLangOpts());
102 return AttrStartIsInMacro && AttrEndIsInMacro;
103}
104
Alexis Hunt96d5c762009-11-21 08:43:09 +0000105/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000106///
107/// [GNU] attributes:
108/// attribute
109/// attributes attribute
110///
111/// [GNU] attribute:
112/// '__attribute__' '(' '(' attribute-list ')' ')'
113///
114/// [GNU] attribute-list:
115/// attrib
116/// attribute_list ',' attrib
117///
118/// [GNU] attrib:
119/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +0000120/// attrib-name
121/// attrib-name '(' identifier ')'
122/// attrib-name '(' identifier ',' nonempty-expr-list ')'
123/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000124///
Steve Naroff0f2fe172007-06-01 17:11:19 +0000125/// [GNU] attrib-name:
126/// identifier
127/// typespec
128/// typequal
129/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +0000130///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000131/// Whether an attribute takes an 'identifier' is determined by the
132/// attrib-name. GCC's behavior here is not worth imitating:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000133///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000134/// * In C mode, if the attribute argument list starts with an identifier
135/// followed by a ',' or an ')', and the identifier doesn't resolve to
136/// a type, it is parsed as an identifier. If the attribute actually
137/// wanted an expression, it's out of luck (but it turns out that no
138/// attributes work that way, because C constant expressions are very
139/// limited).
140/// * In C++ mode, if the attribute argument list starts with an identifier,
141/// and the attribute *wants* an identifier, it is parsed as an identifier.
142/// At block scope, any additional tokens between the identifier and the
143/// ',' or ')' are ignored, otherwise they produce a parse error.
Richard Smithb12bf692011-10-17 21:20:17 +0000144///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000145/// We follow the C++ model, but don't allow junk after the identifier.
John McCall53fa7142010-12-24 02:08:15 +0000146void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000147 SourceLocation *endLoc,
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000148 LateParsedAttrList *LateAttrs,
149 Declarator *D) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000150 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000151
Chris Lattner76c72282007-10-09 17:33:22 +0000152 while (Tok.is(tok::kw___attribute)) {
Leonard Chanc72aaf62019-05-07 03:20:17 +0000153 SourceLocation AttrTokLoc = ConsumeToken();
154 unsigned OldNumAttrs = attrs.size();
155 unsigned OldNumLateAttrs = LateAttrs ? LateAttrs->size() : 0;
156
Steve Naroff0f2fe172007-06-01 17:11:19 +0000157 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
158 "attribute")) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000159 SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000160 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000161 }
162 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000163 SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000164 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000165 }
166 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Aaron Ballman7a899092019-06-18 12:57:05 +0000167 do {
168 // Eat preceeding commas to allow __attribute__((,,,foo))
169 while (TryConsumeToken(tok::comma))
170 ;
Alp Toker094e5212014-01-05 03:27:11 +0000171
172 // Expect an identifier or declaration specifier (const, int, etc.)
David Majnemer22fe7712015-01-03 19:41:00 +0000173 if (Tok.isAnnotation())
174 break;
175 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
176 if (!AttrName)
Alp Toker094e5212014-01-05 03:27:11 +0000177 break;
178
Steve Naroff0f2fe172007-06-01 17:11:19 +0000179 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000180
Alp Toker094e5212014-01-05 03:27:11 +0000181 if (Tok.isNot(tok::l_paren)) {
Craig Topper161e4db2014-05-21 06:02:52 +0000182 attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Erich Keanee891aa92018-07-13 15:07:47 +0000183 ParsedAttr::AS_GNU);
Alp Toker094e5212014-01-05 03:27:11 +0000184 continue;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000185 }
Alp Toker094e5212014-01-05 03:27:11 +0000186
187 // Handle "parameterized" attributes
188 if (!LateAttrs || !isAttributeLateParsed(*AttrName)) {
Craig Topper161e4db2014-05-21 06:02:52 +0000189 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc, nullptr,
Erich Keanee891aa92018-07-13 15:07:47 +0000190 SourceLocation(), ParsedAttr::AS_GNU, D);
Alp Toker094e5212014-01-05 03:27:11 +0000191 continue;
192 }
193
194 // Handle attributes with arguments that require late parsing.
195 LateParsedAttribute *LA =
196 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
197 LateAttrs->push_back(LA);
198
199 // Attributes in a class are parsed at the end of the class, along
200 // with other late-parsed declarations.
201 if (!ClassStack.empty() && !LateAttrs->parseSoon())
202 getCurrentClass().LateParsedDeclarations.push_back(LA);
203
George Burgess IVc8b95372017-01-04 22:43:01 +0000204 // Be sure ConsumeAndStoreUntil doesn't see the start l_paren, since it
205 // recursively consumes balanced parens.
206 LA->Toks.push_back(Tok);
207 ConsumeParen();
208 // Consume everything up to and including the matching right parens.
209 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, /*StopAtSemi=*/true);
Alp Toker094e5212014-01-05 03:27:11 +0000210
211 Token Eof;
212 Eof.startToken();
213 Eof.setLocation(Tok.getLocation());
214 LA->Toks.push_back(Eof);
Aaron Ballman7a899092019-06-18 12:57:05 +0000215 } while (Tok.is(tok::comma));
Alp Toker094e5212014-01-05 03:27:11 +0000216
Alp Toker383d2c42014-01-01 03:08:43 +0000217 if (ExpectAndConsume(tok::r_paren))
Alexey Bataevee6507d2013-11-18 08:17:37 +0000218 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000219 SourceLocation Loc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000220 if (ExpectAndConsume(tok::r_paren))
Alexey Bataevee6507d2013-11-18 08:17:37 +0000221 SkipUntil(tok::r_paren, StopAtSemi);
John McCall53fa7142010-12-24 02:08:15 +0000222 if (endLoc)
223 *endLoc = Loc;
Leonard Chanc72aaf62019-05-07 03:20:17 +0000224
225 // If this was declared in a macro, attach the macro IdentifierInfo to the
226 // parsed attribute.
Leonard Chan69aec052019-05-12 21:50:01 +0000227 auto &SM = PP.getSourceManager();
228 if (!SM.isWrittenInBuiltinFile(SM.getSpellingLoc(AttrTokLoc)) &&
229 FindLocsWithCommonFileID(PP, AttrTokLoc, Loc)) {
Leonard Chanc72aaf62019-05-07 03:20:17 +0000230 CharSourceRange ExpansionRange = SM.getExpansionRange(AttrTokLoc);
231 StringRef FoundName =
232 Lexer::getSourceText(ExpansionRange, SM, PP.getLangOpts());
233 IdentifierInfo *MacroII = PP.getIdentifierInfo(FoundName);
234
235 for (unsigned i = OldNumAttrs; i < attrs.size(); ++i)
236 attrs[i].setMacroIdentifier(MacroII, ExpansionRange.getBegin());
237
238 if (LateAttrs) {
239 for (unsigned i = OldNumLateAttrs; i < LateAttrs->size(); ++i)
240 (*LateAttrs)[i]->MacroII = MacroII;
241 }
242 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000243 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000244}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000245
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000246/// Determine whether the given attribute has an identifier argument.
Aaron Ballman4768b312013-11-04 12:55:56 +0000247static bool attributeHasIdentifierArg(const IdentifierInfo &II) {
Aaron Ballman35db2b32014-01-29 22:13:45 +0000248#define CLANG_ATTR_IDENTIFIER_ARG_LIST
Aaron Ballman4768b312013-11-04 12:55:56 +0000249 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Aaron Ballman35db2b32014-01-29 22:13:45 +0000250#include "clang/Parse/AttrParserStringSwitches.inc"
Douglas Gregord2472d42013-05-02 23:25:32 +0000251 .Default(false);
Aaron Ballman35db2b32014-01-29 22:13:45 +0000252#undef CLANG_ATTR_IDENTIFIER_ARG_LIST
Douglas Gregord2472d42013-05-02 23:25:32 +0000253}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000254
Erich Keane3efe0022018-07-20 14:13:28 +0000255/// Determine whether the given attribute has a variadic identifier argument.
256static bool attributeHasVariadicIdentifierArg(const IdentifierInfo &II) {
257#define CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST
258 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
259#include "clang/Parse/AttrParserStringSwitches.inc"
260 .Default(false);
261#undef CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST
262}
263
Johannes Doerfertac991bb2019-01-19 05:36:54 +0000264/// Determine whether the given attribute treats kw_this as an identifier.
265static bool attributeTreatsKeywordThisAsIdentifier(const IdentifierInfo &II) {
266#define CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST
267 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
268#include "clang/Parse/AttrParserStringSwitches.inc"
269 .Default(false);
270#undef CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST
271}
272
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000273/// Determine whether the given attribute parses a type argument.
Aaron Ballman4768b312013-11-04 12:55:56 +0000274static bool attributeIsTypeArgAttr(const IdentifierInfo &II) {
Aaron Ballman35db2b32014-01-29 22:13:45 +0000275#define CLANG_ATTR_TYPE_ARG_LIST
Aaron Ballman4768b312013-11-04 12:55:56 +0000276 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Aaron Ballman35db2b32014-01-29 22:13:45 +0000277#include "clang/Parse/AttrParserStringSwitches.inc"
Aaron Ballman4768b312013-11-04 12:55:56 +0000278 .Default(false);
Aaron Ballman35db2b32014-01-29 22:13:45 +0000279#undef CLANG_ATTR_TYPE_ARG_LIST
Aaron Ballman4768b312013-11-04 12:55:56 +0000280}
281
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000282/// Determine whether the given attribute requires parsing its arguments
Aaron Ballman15b27b92014-01-09 19:39:35 +0000283/// in an unevaluated context or not.
284static bool attributeParsedArgsUnevaluated(const IdentifierInfo &II) {
Aaron Ballman35db2b32014-01-29 22:13:45 +0000285#define CLANG_ATTR_ARG_CONTEXT_LIST
Aaron Ballman15b27b92014-01-09 19:39:35 +0000286 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Aaron Ballman35db2b32014-01-29 22:13:45 +0000287#include "clang/Parse/AttrParserStringSwitches.inc"
Aaron Ballman15b27b92014-01-09 19:39:35 +0000288 .Default(false);
Aaron Ballman35db2b32014-01-29 22:13:45 +0000289#undef CLANG_ATTR_ARG_CONTEXT_LIST
Aaron Ballman15b27b92014-01-09 19:39:35 +0000290}
291
Richard Smithfeefaf52013-09-03 18:01:40 +0000292IdentifierLoc *Parser::ParseIdentifierLoc() {
293 assert(Tok.is(tok::identifier) && "expected an identifier");
294 IdentifierLoc *IL = IdentifierLoc::create(Actions.Context,
295 Tok.getLocation(),
296 Tok.getIdentifierInfo());
297 ConsumeToken();
298 return IL;
299}
300
Richard Smithb1f9a282013-10-31 01:56:18 +0000301void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName,
302 SourceLocation AttrNameLoc,
303 ParsedAttributes &Attrs,
Aaron Ballman80f1529c2014-07-16 20:21:50 +0000304 SourceLocation *EndLoc,
305 IdentifierInfo *ScopeName,
306 SourceLocation ScopeLoc,
Erich Keanee891aa92018-07-13 15:07:47 +0000307 ParsedAttr::Syntax Syntax) {
Richard Smithb1f9a282013-10-31 01:56:18 +0000308 BalancedDelimiterTracker Parens(*this, tok::l_paren);
309 Parens.consumeOpen();
310
311 TypeResult T;
312 if (Tok.isNot(tok::r_paren))
313 T = ParseTypeName();
314
315 if (Parens.consumeClose())
316 return;
317
318 if (T.isInvalid())
319 return;
320
321 if (T.isUsable())
322 Attrs.addNewTypeAttr(&AttrName,
Craig Topper161e4db2014-05-21 06:02:52 +0000323 SourceRange(AttrNameLoc, Parens.getCloseLocation()),
Aaron Ballman80f1529c2014-07-16 20:21:50 +0000324 ScopeName, ScopeLoc, T.get(), Syntax);
Richard Smithb1f9a282013-10-31 01:56:18 +0000325 else
326 Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()),
Aaron Ballman80f1529c2014-07-16 20:21:50 +0000327 ScopeName, ScopeLoc, nullptr, 0, Syntax);
Richard Smithb1f9a282013-10-31 01:56:18 +0000328}
329
Aaron Ballman35f94212014-04-14 16:03:22 +0000330unsigned Parser::ParseAttributeArgsCommon(
Aaron Ballmanb8e20392014-03-31 17:32:39 +0000331 IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
332 ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
Erich Keanee891aa92018-07-13 15:07:47 +0000333 SourceLocation ScopeLoc, ParsedAttr::Syntax Syntax) {
Aaron Ballmanb8e20392014-03-31 17:32:39 +0000334 // Ignore the left paren location for now.
335 ConsumeParen();
336
Johannes Doerfertac991bb2019-01-19 05:36:54 +0000337 bool ChangeKWThisToIdent = attributeTreatsKeywordThisAsIdentifier(*AttrName);
Matthias Gehred293cbd2019-07-25 17:50:51 +0000338 bool AttributeIsTypeArgAttr = attributeIsTypeArgAttr(*AttrName);
Johannes Doerfertac991bb2019-01-19 05:36:54 +0000339
340 // Interpret "kw_this" as an identifier if the attributed requests it.
341 if (ChangeKWThisToIdent && Tok.is(tok::kw_this))
342 Tok.setKind(tok::identifier);
343
Aaron Ballmanb8e20392014-03-31 17:32:39 +0000344 ArgsVector ArgExprs;
345 if (Tok.is(tok::identifier)) {
346 // If this attribute wants an 'identifier' argument, make it so.
Erich Keane3efe0022018-07-20 14:13:28 +0000347 bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName) ||
348 attributeHasVariadicIdentifierArg(*AttrName);
Erich Keanee891aa92018-07-13 15:07:47 +0000349 ParsedAttr::Kind AttrKind =
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///
Nathan Huckleberry1e0affb2019-08-20 17:16:49 +00001744Parser::DeclGroupPtrTy
1745Parser::ParseDeclaration(DeclaratorContext Context, SourceLocation &DeclEnd,
1746 ParsedAttributesWithRange &attrs,
1747 SourceLocation *DeclSpecStart) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001748 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001749 // Must temporarily exit the objective-c container scope for
1750 // parsing c none objective-c decls.
1751 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001752
Craig Topper161e4db2014-05-21 06:02:52 +00001753 Decl *SingleDecl = nullptr;
Chris Lattnera5235172007-08-25 06:57:03 +00001754 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001755 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001756 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001757 ProhibitAttributes(attrs);
Erich Keanec480f302018-07-12 21:09:05 +00001758 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd, attrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001759 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001760 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001761 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001762 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001763 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001764 SourceLocation InlineLoc = ConsumeToken();
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00001765 return ParseNamespace(Context, DeclEnd, InlineLoc);
Sebastian Redl67667942010-08-27 23:12:46 +00001766 }
Nathan Huckleberry1e0affb2019-08-20 17:16:49 +00001767 return ParseSimpleDeclaration(Context, DeclEnd, attrs, true, nullptr,
1768 DeclSpecStart);
Chris Lattnera5235172007-08-25 06:57:03 +00001769 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001770 ProhibitAttributes(attrs);
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +00001771 return ParseNamespace(Context, DeclEnd);
Douglas Gregord7c4d982008-12-30 03:27:21 +00001772 case tok::kw_using:
Richard Smith6f1daa42016-12-16 00:58:48 +00001773 return ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
1774 DeclEnd, attrs);
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001775 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001776 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001777 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001778 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001779 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001780 default:
Nathan Huckleberry1e0affb2019-08-20 17:16:49 +00001781 return ParseSimpleDeclaration(Context, DeclEnd, attrs, true, nullptr,
1782 DeclSpecStart);
Chris Lattnera5235172007-08-25 06:57:03 +00001783 }
Chad Rosierc1183952012-06-26 22:30:43 +00001784
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001785 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smith6f1daa42016-12-16 00:58:48 +00001786 // single decl, convert it now.
1787 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +00001788}
1789
1790/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1791/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001792/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1793/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001794///[C90/C++]init-declarator-list ';' [TODO]
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001795/// [OMP] threadprivate-directive
1796/// [OMP] allocate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001797///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001798/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001799/// attribute-specifier-seq[opt] type-specifier-seq declarator
1800///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001801/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001802/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001803///
1804/// If FRI is non-null, we might be parsing a for-range-declaration instead
1805/// of a simple-declaration. If we find that we are, we also parse the
1806/// for-range-initializer, and place it here.
Nathan Huckleberry1e0affb2019-08-20 17:16:49 +00001807///
1808/// DeclSpecStart is used when decl-specifiers are parsed before parsing
1809/// the Declaration. The SourceLocation for this Decl is set to
1810/// DeclSpecStart if DeclSpecStart is non-null.
1811Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(
1812 DeclaratorContext Context, SourceLocation &DeclEnd,
1813 ParsedAttributesWithRange &Attrs, bool RequireSemi, ForRangeInit *FRI,
1814 SourceLocation *DeclSpecStart) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001815 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001816 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001817
Richard Smith404dfb42013-11-19 22:47:36 +00001818 DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
Faisal Valia534f072018-04-26 00:42:40 +00001819 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext);
Richard Smith404dfb42013-11-19 22:47:36 +00001820
1821 // If we had a free-standing type definition with a missing semicolon, we
1822 // may get this far before the problem becomes obvious.
1823 if (DS.hasTagDefinition() &&
1824 DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext))
David Blaikie0403cb12016-01-15 23:43:25 +00001825 return nullptr;
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001826
Chris Lattner0e894622006-08-13 19:58:17 +00001827 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1828 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001829 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001830 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001831 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001832 if (RequireSemi) ConsumeToken();
Nico Weber7b837f52016-01-28 19:25:00 +00001833 RecordDecl *AnonRecord = nullptr;
John McCall48871652010-08-21 09:40:31 +00001834 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Nico Weber7b837f52016-01-28 19:25:00 +00001835 DS, AnonRecord);
John McCall28a6aea2009-11-04 02:18:39 +00001836 DS.complete(TheDecl);
Nico Weber7b837f52016-01-28 19:25:00 +00001837 if (AnonRecord) {
1838 Decl* decls[] = {AnonRecord, TheDecl};
Richard Smith3beb7c62017-01-12 02:27:38 +00001839 return Actions.BuildDeclaratorGroup(decls);
Nico Weber7b837f52016-01-28 19:25:00 +00001840 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001841 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001842 }
Chad Rosierc1183952012-06-26 22:30:43 +00001843
Nathan Huckleberry1e0affb2019-08-20 17:16:49 +00001844 if (DeclSpecStart)
1845 DS.SetRangeStart(*DeclSpecStart);
1846
Richard Smith2386c8b2013-02-22 09:06:26 +00001847 DS.takeAttributesFrom(Attrs);
Reid Klecknerd61a3112014-12-15 23:16:32 +00001848 return ParseDeclGroup(DS, Context, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001849}
Mike Stump11289f42009-09-09 15:08:12 +00001850
Richard Smith09f76ee2011-10-19 21:33:05 +00001851/// Returns true if this might be the start of a declarator, or a common typo
1852/// for a declarator.
Faisal Vali421b2d12017-12-29 05:41:00 +00001853bool Parser::MightBeDeclarator(DeclaratorContext Context) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001854 switch (Tok.getKind()) {
1855 case tok::annot_cxxscope:
1856 case tok::annot_template_id:
1857 case tok::caret:
1858 case tok::code_completion:
1859 case tok::coloncolon:
1860 case tok::ellipsis:
1861 case tok::kw___attribute:
1862 case tok::kw_operator:
1863 case tok::l_paren:
1864 case tok::star:
1865 return true;
1866
1867 case tok::amp:
1868 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001869 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001870
Richard Smithc8a79032012-01-09 22:31:44 +00001871 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Faisal Vali421b2d12017-12-29 05:41:00 +00001872 return Context == DeclaratorContext::MemberContext &&
1873 getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square);
Richard Smithc8a79032012-01-09 22:31:44 +00001874
1875 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
Faisal Vali421b2d12017-12-29 05:41:00 +00001876 return Context == DeclaratorContext::MemberContext ||
1877 getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001878
Richard Smith09f76ee2011-10-19 21:33:05 +00001879 case tok::identifier:
1880 switch (NextToken().getKind()) {
1881 case tok::code_completion:
1882 case tok::coloncolon:
1883 case tok::comma:
1884 case tok::equal:
1885 case tok::equalequal: // Might be a typo for '='.
1886 case tok::kw_alignas:
1887 case tok::kw_asm:
1888 case tok::kw___attribute:
1889 case tok::l_brace:
1890 case tok::l_paren:
1891 case tok::l_square:
1892 case tok::less:
1893 case tok::r_brace:
1894 case tok::r_paren:
1895 case tok::r_square:
1896 case tok::semi:
1897 return true;
1898
1899 case tok::colon:
1900 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001901 // and in block scope it's probably a label. Inside a class definition,
1902 // this is a bit-field.
Faisal Vali421b2d12017-12-29 05:41:00 +00001903 return Context == DeclaratorContext::MemberContext ||
1904 (getLangOpts().CPlusPlus &&
1905 Context == DeclaratorContext::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001906
1907 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001908 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001909
1910 default:
1911 return false;
1912 }
1913
1914 default:
1915 return false;
1916 }
1917}
1918
Richard Smithb8caac82012-04-11 20:59:20 +00001919/// Skip until we reach something which seems like a sensible place to pick
1920/// up parsing after a malformed declaration. This will sometimes stop sooner
1921/// than SkipUntil(tok::r_brace) would, but will never stop later.
1922void Parser::SkipMalformedDecl() {
1923 while (true) {
1924 switch (Tok.getKind()) {
1925 case tok::l_brace:
1926 // Skip until matching }, then stop. We've probably skipped over
1927 // a malformed class or function definition or similar.
1928 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001929 SkipUntil(tok::r_brace);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001930 if (Tok.isOneOf(tok::comma, tok::l_brace, tok::kw_try)) {
Richard Smithb8caac82012-04-11 20:59:20 +00001931 // This declaration isn't over yet. Keep skipping.
1932 continue;
1933 }
Alp Toker8fbec672013-12-17 23:29:36 +00001934 TryConsumeToken(tok::semi);
Richard Smithb8caac82012-04-11 20:59:20 +00001935 return;
1936
1937 case tok::l_square:
1938 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001939 SkipUntil(tok::r_square);
Richard Smithb8caac82012-04-11 20:59:20 +00001940 continue;
1941
1942 case tok::l_paren:
1943 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001944 SkipUntil(tok::r_paren);
Richard Smithb8caac82012-04-11 20:59:20 +00001945 continue;
1946
1947 case tok::r_brace:
1948 return;
1949
1950 case tok::semi:
1951 ConsumeToken();
1952 return;
1953
1954 case tok::kw_inline:
1955 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001956 // a good place to pick back up parsing, except in an Objective-C
1957 // @interface context.
1958 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1959 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001960 return;
1961 break;
1962
1963 case tok::kw_namespace:
1964 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001965 // place to pick back up parsing, except in an Objective-C
1966 // @interface context.
1967 if (Tok.isAtStartOfLine() &&
1968 (!ParsingInObjCContainer || CurParsedObjCImpl))
1969 return;
1970 break;
1971
1972 case tok::at:
1973 // @end is very much like } in Objective-C contexts.
1974 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1975 ParsingInObjCContainer)
1976 return;
1977 break;
1978
1979 case tok::minus:
1980 case tok::plus:
1981 // - and + probably start new method declarations in Objective-C contexts.
1982 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001983 return;
1984 break;
1985
1986 case tok::eof:
Richard Smith34f30512013-11-23 04:06:09 +00001987 case tok::annot_module_begin:
1988 case tok::annot_module_end:
1989 case tok::annot_module_include:
Richard Smithb8caac82012-04-11 20:59:20 +00001990 return;
1991
1992 default:
1993 break;
1994 }
1995
1996 ConsumeAnyToken();
1997 }
1998}
1999
John McCalld5a36322009-11-03 19:26:08 +00002000/// ParseDeclGroup - Having concluded that this is either a function
2001/// definition or a group of object declarations, actually parse the
2002/// result.
John McCall28a6aea2009-11-04 02:18:39 +00002003Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
Faisal Vali421b2d12017-12-29 05:41:00 +00002004 DeclaratorContext Context,
Richard Smith02e85f32011-04-14 22:09:26 +00002005 SourceLocation *DeclEnd,
2006 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00002007 // Parse the first declarator.
Faisal Vali421b2d12017-12-29 05:41:00 +00002008 ParsingDeclarator D(*this, DS, Context);
John McCalld5a36322009-11-03 19:26:08 +00002009 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00002010
John McCalld5a36322009-11-03 19:26:08 +00002011 // Bail out if the first declarator didn't seem well-formed.
2012 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00002013 SkipMalformedDecl();
David Blaikie0403cb12016-01-15 23:43:25 +00002014 return nullptr;
Chris Lattnerefb0f112009-03-29 17:18:04 +00002015 }
Mike Stump11289f42009-09-09 15:08:12 +00002016
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00002017 // Save late-parsed attributes for now; they need to be parsed in the
2018 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00002019 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
2020 LateParsedAttrList LateParsedAttrs(true);
Richard Smith99c464c2014-11-10 21:10:32 +00002021 if (D.isFunctionDeclarator()) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00002022 MaybeParseGNUAttributes(D, &LateParsedAttrs);
2023
Richard Smith99c464c2014-11-10 21:10:32 +00002024 // The _Noreturn keyword can't appear here, unlike the GNU noreturn
2025 // attribute. If we find the keyword here, tell the user to put it
2026 // at the start instead.
2027 if (Tok.is(tok::kw__Noreturn)) {
2028 SourceLocation Loc = ConsumeToken();
2029 const char *PrevSpec;
2030 unsigned DiagID;
2031
2032 // We can offer a fixit if it's valid to mark this function as _Noreturn
2033 // and we don't have any other declarators in this declaration.
2034 bool Fixit = !DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
2035 MaybeParseGNUAttributes(D, &LateParsedAttrs);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002036 Fixit &= Tok.isOneOf(tok::semi, tok::l_brace, tok::kw_try);
Richard Smith99c464c2014-11-10 21:10:32 +00002037
2038 Diag(Loc, diag::err_c11_noreturn_misplaced)
2039 << (Fixit ? FixItHint::CreateRemoval(Loc) : FixItHint())
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002040 << (Fixit ? FixItHint::CreateInsertion(D.getBeginLoc(), "_Noreturn ")
Richard Smith99c464c2014-11-10 21:10:32 +00002041 : FixItHint());
2042 }
2043 }
2044
Chris Lattnerdbb1e932010-07-11 22:24:20 +00002045 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00002046 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00002047 // Look at the next token to make sure that this isn't a function
2048 // declaration. We have to check this because __attribute__ might be the
2049 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00002050 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00002051
Reid Klecknerd61a3112014-12-15 23:16:32 +00002052 // Function definitions are only allowed at file scope and in C++ classes.
2053 // The C++ inline method definition case is handled elsewhere, so we only
2054 // need to handle the file scope definition case.
Faisal Vali421b2d12017-12-29 05:41:00 +00002055 if (Context == DeclaratorContext::FileContext) {
Douglas Gregor012efe22013-04-16 16:01:32 +00002056 if (isStartOfFunctionDefinition(D)) {
2057 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2058 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00002059
Douglas Gregor012efe22013-04-16 16:01:32 +00002060 // Recover by treating the 'typedef' as spurious.
2061 DS.ClearStorageClassSpecs();
2062 }
2063
2064 Decl *TheDecl =
2065 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
2066 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00002067 }
2068
Douglas Gregor012efe22013-04-16 16:01:32 +00002069 if (isDeclarationSpecifier()) {
Nico Weber6b05f382015-02-18 04:53:03 +00002070 // If there is an invalid declaration specifier right after the
2071 // function prototype, then we must be in a missing semicolon case
2072 // where this isn't actually a body. Just fall through into the code
2073 // that handles it as a prototype, and let the top-level code handle
2074 // the erroneous declspec where it would otherwise expect a comma or
2075 // semicolon.
Douglas Gregor012efe22013-04-16 16:01:32 +00002076 } else {
2077 Diag(Tok, diag::err_expected_fn_body);
2078 SkipUntil(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +00002079 return nullptr;
Douglas Gregor012efe22013-04-16 16:01:32 +00002080 }
John McCalld5a36322009-11-03 19:26:08 +00002081 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00002082 if (Tok.is(tok::l_brace)) {
2083 Diag(Tok, diag::err_function_definition_not_allowed);
Serge Pavlov1de51512013-12-09 05:25:47 +00002084 SkipMalformedDecl();
David Blaikie0403cb12016-01-15 23:43:25 +00002085 return nullptr;
Douglas Gregor012efe22013-04-16 16:01:32 +00002086 }
John McCalld5a36322009-11-03 19:26:08 +00002087 }
2088 }
2089
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00002090 if (ParseAsmAttributesAfterDeclarator(D))
David Blaikie0403cb12016-01-15 23:43:25 +00002091 return nullptr;
Richard Smith02e85f32011-04-14 22:09:26 +00002092
2093 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
2094 // must parse and analyze the for-range-initializer before the declaration is
2095 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00002096 //
2097 // Handle the Objective-C for-in loop variable similarly, although we
2098 // don't need to parse the container in advance.
2099 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
2100 bool IsForRangeLoop = false;
Alp Toker8fbec672013-12-17 23:29:36 +00002101 if (TryConsumeToken(tok::colon, FRI->ColonLoc)) {
Douglas Gregor2eb1c572013-04-08 20:52:24 +00002102 IsForRangeLoop = true;
Douglas Gregor2eb1c572013-04-08 20:52:24 +00002103 if (Tok.is(tok::l_brace))
2104 FRI->RangeExpr = ParseBraceInitializer();
2105 else
2106 FRI->RangeExpr = ParseExpression();
2107 }
2108
Richard Smith02e85f32011-04-14 22:09:26 +00002109 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
George Karpenkovec38cf72018-03-29 00:56:24 +00002110 if (IsForRangeLoop) {
Douglas Gregor2eb1c572013-04-08 20:52:24 +00002111 Actions.ActOnCXXForRangeDecl(ThisDecl);
George Karpenkovec38cf72018-03-29 00:56:24 +00002112 } else {
2113 // Obj-C for loop
2114 if (auto *VD = dyn_cast_or_null<VarDecl>(ThisDecl))
2115 VD->setObjCForDecl(true);
2116 }
Richard Smith02e85f32011-04-14 22:09:26 +00002117 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00002118 D.complete(ThisDecl);
Rafael Espindolaab417692013-07-09 12:05:01 +00002119 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00002120 }
2121
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002122 SmallVector<Decl *, 8> DeclsInGroup;
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00002123 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(
2124 D, ParsedTemplateInfo(), FRI);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00002125 if (LateParsedAttrs.size() > 0)
2126 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00002127 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00002128 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00002129 DeclsInGroup.push_back(FirstDecl);
2130
Faisal Vali421b2d12017-12-29 05:41:00 +00002131 bool ExpectSemi = Context != DeclaratorContext::ForContext;
Fangrui Song6907ce22018-07-30 19:24:48 +00002132
John McCalld5a36322009-11-03 19:26:08 +00002133 // If we don't have a comma, it is either the end of the list (a ';') or an
2134 // error, bail out.
Alp Toker8fbec672013-12-17 23:29:36 +00002135 SourceLocation CommaLoc;
2136 while (TryConsumeToken(tok::comma, CommaLoc)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00002137 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
2138 // This comma was followed by a line-break and something which can't be
2139 // the start of a declarator. The comma was probably a typo for a
2140 // semicolon.
2141 Diag(CommaLoc, diag::err_expected_semi_declaration)
2142 << FixItHint::CreateReplacement(CommaLoc, ";");
2143 ExpectSemi = false;
2144 break;
2145 }
John McCalld5a36322009-11-03 19:26:08 +00002146
2147 // Parse the next declarator.
2148 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00002149 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00002150
2151 // Accept attributes in an init-declarator. In the first declarator in a
2152 // declaration, these would be part of the declspec. In subsequent
2153 // declarators, they become part of the declarator itself, so that they
2154 // don't apply to declarators after *this* one. Examples:
2155 // short __attribute__((common)) var; -> declspec
2156 // short var __attribute__((common)); -> declarator
2157 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00002158 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00002159
Nico Rieckeaaae272014-12-04 23:31:08 +00002160 // MSVC parses but ignores qualifiers after the comma as an extension.
2161 if (getLangOpts().MicrosoftExt)
2162 DiagnoseAndSkipExtendedMicrosoftTypeAttributes();
2163
John McCalld5a36322009-11-03 19:26:08 +00002164 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00002165 if (!D.isInvalidType()) {
2166 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
2167 D.complete(ThisDecl);
2168 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00002169 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00002170 }
John McCalld5a36322009-11-03 19:26:08 +00002171 }
2172
2173 if (DeclEnd)
2174 *DeclEnd = Tok.getLocation();
2175
Richard Smith09f76ee2011-10-19 21:33:05 +00002176 if (ExpectSemi &&
Faisal Vali421b2d12017-12-29 05:41:00 +00002177 ExpectAndConsumeSemi(Context == DeclaratorContext::FileContext
Chris Lattner02f1b612012-04-28 16:12:17 +00002178 ? diag::err_invalid_token_after_toplevel_declarator
2179 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00002180 // Okay, there was no semicolon and one was expected. If we see a
2181 // declaration specifier, just assume it was missing and continue parsing.
2182 // Otherwise things are very confused and we skip to recover.
2183 if (!isDeclarationSpecifier()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002184 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Alp Toker8fbec672013-12-17 23:29:36 +00002185 TryConsumeToken(tok::semi);
Chris Lattner13901342010-07-11 22:42:07 +00002186 }
John McCalld5a36322009-11-03 19:26:08 +00002187 }
2188
Rafael Espindolaab417692013-07-09 12:05:01 +00002189 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +00002190}
2191
Richard Smith02e85f32011-04-14 22:09:26 +00002192/// Parse an optional simple-asm-expr and attributes, and attach them to a
2193/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00002194bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00002195 // If a simple-asm-expr is present, parse it.
2196 if (Tok.is(tok::kw_asm)) {
2197 SourceLocation Loc;
2198 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
2199 if (AsmLabel.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002200 SkipUntil(tok::semi, StopBeforeMatch);
Richard Smith02e85f32011-04-14 22:09:26 +00002201 return true;
2202 }
2203
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002204 D.setAsmLabel(AsmLabel.get());
Richard Smith02e85f32011-04-14 22:09:26 +00002205 D.SetRangeEnd(Loc);
2206 }
2207
2208 MaybeParseGNUAttributes(D);
2209 return false;
2210}
2211
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002212/// Parse 'declaration' after parsing 'declaration-specifiers
Douglas Gregor23996282009-05-12 21:31:51 +00002213/// declarator'. This method parses the remainder of the declaration
2214/// (including any attributes or initializer, among other things) and
2215/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00002216///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00002217/// init-declarator: [C99 6.7]
2218/// declarator
2219/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00002220/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
2221/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002222/// [C++] declarator initializer[opt]
2223///
2224/// [C++] initializer:
2225/// [C++] '=' initializer-clause
2226/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00002227/// [C++0x] '=' 'default' [TODO]
2228/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00002229/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00002230///
2231/// According to the standard grammar, =default and =delete are function
2232/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00002233///
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00002234Decl *Parser::ParseDeclarationAfterDeclarator(
2235 Declarator &D, const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00002236 if (ParseAsmAttributesAfterDeclarator(D))
Craig Topper161e4db2014-05-21 06:02:52 +00002237 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00002238
Richard Smith02e85f32011-04-14 22:09:26 +00002239 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
2240}
Mike Stump11289f42009-09-09 15:08:12 +00002241
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00002242Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(
2243 Declarator &D, const ParsedTemplateInfo &TemplateInfo, ForRangeInit *FRI) {
Richard Smithc95d2c52017-09-22 04:25:05 +00002244 // RAII type used to track whether we're inside an initializer.
2245 struct InitializerScopeRAII {
2246 Parser &P;
2247 Declarator &D;
2248 Decl *ThisDecl;
2249
2250 InitializerScopeRAII(Parser &P, Declarator &D, Decl *ThisDecl)
2251 : P(P), D(D), ThisDecl(ThisDecl) {
2252 if (ThisDecl && P.getLangOpts().CPlusPlus) {
2253 Scope *S = nullptr;
2254 if (D.getCXXScopeSpec().isSet()) {
2255 P.EnterScope(0);
2256 S = P.getCurScope();
2257 }
2258 P.Actions.ActOnCXXEnterDeclInitializer(S, ThisDecl);
2259 }
2260 }
2261 ~InitializerScopeRAII() { pop(); }
2262 void pop() {
2263 if (ThisDecl && P.getLangOpts().CPlusPlus) {
2264 Scope *S = nullptr;
2265 if (D.getCXXScopeSpec().isSet())
2266 S = P.getCurScope();
2267 P.Actions.ActOnCXXExitDeclInitializer(S, ThisDecl);
2268 if (S)
2269 P.ExitScope();
2270 }
2271 ThisDecl = nullptr;
2272 }
2273 };
2274
Douglas Gregor23996282009-05-12 21:31:51 +00002275 // Inform the current actions module that we just parsed this declarator.
Craig Topper161e4db2014-05-21 06:02:52 +00002276 Decl *ThisDecl = nullptr;
Douglas Gregor450f00842009-09-25 18:43:00 +00002277 switch (TemplateInfo.Kind) {
2278 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00002279 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00002280 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002281
Douglas Gregor450f00842009-09-25 18:43:00 +00002282 case ParsedTemplateInfo::Template:
Larisse Voufo39a1e502013-08-06 01:03:05 +00002283 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002284 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002285 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00002286 D);
Larisse Voufo833b05a2013-08-06 07:33:00 +00002287 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufo39a1e502013-08-06 01:03:05 +00002288 // Re-direct this decl to refer to the templated decl so that we can
2289 // initialize it.
2290 ThisDecl = VT->getTemplatedDecl();
2291 break;
2292 }
2293 case ParsedTemplateInfo::ExplicitInstantiation: {
2294 if (Tok.is(tok::semi)) {
2295 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
2296 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
2297 if (ThisRes.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002298 SkipUntil(tok::semi, StopBeforeMatch);
Craig Topper161e4db2014-05-21 06:02:52 +00002299 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00002300 }
2301 ThisDecl = ThisRes.get();
2302 } else {
2303 // FIXME: This check should be for a variable template instantiation only.
2304
2305 // Check that this is a valid instantiation
Faisal Vali2ab8c152017-12-30 04:15:27 +00002306 if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00002307 // If the declarator-id is not a template-id, issue a diagnostic and
2308 // recover by ignoring the 'template' keyword.
2309 Diag(Tok, diag::err_template_defn_explicit_instantiation)
2310 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
2311 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
2312 } else {
2313 SourceLocation LAngleLoc =
2314 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
2315 Diag(D.getIdentifierLoc(),
2316 diag::err_explicit_instantiation_with_definition)
2317 << SourceRange(TemplateInfo.TemplateLoc)
2318 << FixItHint::CreateInsertion(LAngleLoc, "<>");
2319
2320 // Recover as if it were an explicit specialization.
2321 TemplateParameterLists FakedParamLists;
2322 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
Craig Topper96225a52015-12-24 23:58:25 +00002323 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, None,
Hubert Tongf608c052016-04-29 18:05:37 +00002324 LAngleLoc, nullptr));
Larisse Voufo39a1e502013-08-06 01:03:05 +00002325
2326 ThisDecl =
2327 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
2328 }
2329 }
Douglas Gregor450f00842009-09-25 18:43:00 +00002330 break;
2331 }
2332 }
Mike Stump11289f42009-09-09 15:08:12 +00002333
Douglas Gregor23996282009-05-12 21:31:51 +00002334 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00002335 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00002336 if (isTokenEqualOrEqualTypo()) {
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00002337 SourceLocation EqualLoc = ConsumeToken();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002338
Anders Carlsson991285e2010-09-24 21:25:25 +00002339 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002340 if (D.isFunctionDeclarator())
2341 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2342 << 1 /* delete */;
2343 else
2344 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00002345 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002346 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00002347 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2348 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002349 else
2350 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00002351 } else {
Richard Smithc95d2c52017-09-22 04:25:05 +00002352 InitializerScopeRAII InitScope(*this, D, ThisDecl);
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00002353
Douglas Gregor7aa6b222010-05-30 01:49:25 +00002354 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002355 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00002356 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002357 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00002358 return nullptr;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00002359 }
Chad Rosierc1183952012-06-26 22:30:43 +00002360
Ilya Biryukov4f9543b2019-01-31 20:20:32 +00002361 PreferredType.enterVariableInit(Tok.getLocation(), ThisDecl);
2362 ExprResult Init = ParseInitializer();
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00002363
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00002364 // If this is the only decl in (possibly) range based for statement,
2365 // our best guess is that the user meant ':' instead of '='.
2366 if (Tok.is(tok::r_paren) && FRI && D.isFirstDeclarator()) {
2367 Diag(EqualLoc, diag::err_single_decl_assign_in_for_range)
2368 << FixItHint::CreateReplacement(EqualLoc, ":");
2369 // We are trying to stop parser from looking for ';' in this for
2370 // statement, therefore preventing spurious errors to be issued.
2371 FRI->ColonLoc = EqualLoc;
2372 Init = ExprError();
2373 FRI->RangeExpr = Init;
2374 }
2375
Richard Smithc95d2c52017-09-22 04:25:05 +00002376 InitScope.pop();
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00002377
Douglas Gregor23996282009-05-12 21:31:51 +00002378 if (Init.isInvalid()) {
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00002379 SmallVector<tok::TokenKind, 2> StopTokens;
2380 StopTokens.push_back(tok::comma);
Faisal Vali421b2d12017-12-29 05:41:00 +00002381 if (D.getContext() == DeclaratorContext::ForContext ||
2382 D.getContext() == DeclaratorContext::InitStmtContext)
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00002383 StopTokens.push_back(tok::r_paren);
2384 SkipUntil(StopTokens, StopAtSemi | StopBeforeMatch);
Douglas Gregor604c3022010-03-01 18:27:54 +00002385 Actions.ActOnInitializerError(ThisDecl);
2386 } else
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002387 Actions.AddInitializerToDecl(ThisDecl, Init.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00002388 /*DirectInit=*/false);
Douglas Gregor23996282009-05-12 21:31:51 +00002389 }
2390 } else if (Tok.is(tok::l_paren)) {
2391 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002392 BalancedDelimiterTracker T(*this, tok::l_paren);
2393 T.consumeOpen();
2394
Benjamin Kramerf0623432012-08-23 22:51:59 +00002395 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00002396 CommaLocsTy CommaLocs;
2397
Richard Smithc95d2c52017-09-22 04:25:05 +00002398 InitializerScopeRAII InitScope(*this, D, ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00002399
Ilya Biryukovcbea95d2017-09-08 13:36:38 +00002400 auto ThisVarDecl = dyn_cast_or_null<VarDecl>(ThisDecl);
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002401 auto RunSignatureHelp = [&]() {
Ilya Biryukov832c4af2018-09-07 14:04:39 +00002402 QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
Ilya Biryukovcbea95d2017-09-08 13:36:38 +00002403 getCurScope(), ThisVarDecl->getType()->getCanonicalTypeInternal(),
Ilya Biryukov2fab2352018-08-30 13:08:03 +00002404 ThisDecl->getLocation(), Exprs, T.getOpenLocation());
Kadir Cetinkayaa32d2532018-09-10 13:46:28 +00002405 CalledSignatureHelp = true;
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002406 return PreferredType;
Ilya Biryukovcbea95d2017-09-08 13:36:38 +00002407 };
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002408 auto SetPreferredType = [&] {
2409 PreferredType.enterFunctionArgument(Tok.getLocation(), RunSignatureHelp);
2410 };
2411
2412 llvm::function_ref<void()> ExpressionStarts;
Ilya Biryukovcbea95d2017-09-08 13:36:38 +00002413 if (ThisVarDecl) {
2414 // ParseExpressionList can sometimes succeed even when ThisDecl is not
2415 // VarDecl. This is an error and it is reported in a call to
2416 // Actions.ActOnInitializerError(). However, we call
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002417 // ProduceConstructorSignatureHelp only on VarDecls.
2418 ExpressionStarts = SetPreferredType;
Ilya Biryukovcbea95d2017-09-08 13:36:38 +00002419 }
Ilya Biryukovff2a9972019-02-26 11:01:50 +00002420 if (ParseExpressionList(Exprs, CommaLocs, ExpressionStarts)) {
Kadir Cetinkayaa32d2532018-09-10 13:46:28 +00002421 if (ThisVarDecl && PP.isCodeCompletionReached() && !CalledSignatureHelp) {
2422 Actions.ProduceConstructorSignatureHelp(
2423 getCurScope(), ThisVarDecl->getType()->getCanonicalTypeInternal(),
2424 ThisDecl->getLocation(), Exprs, T.getOpenLocation());
2425 CalledSignatureHelp = true;
2426 }
David Blaikieeae04112012-10-10 23:15:05 +00002427 Actions.ActOnInitializerError(ThisDecl);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002428 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor23996282009-05-12 21:31:51 +00002429 } else {
2430 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002431 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00002432
2433 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
2434 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00002435
Richard Smithc95d2c52017-09-22 04:25:05 +00002436 InitScope.pop();
Douglas Gregor613bf102009-12-22 17:47:17 +00002437
Sebastian Redla9351792012-02-11 23:51:47 +00002438 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
2439 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002440 Exprs);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002441 Actions.AddInitializerToDecl(ThisDecl, Initializer.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00002442 /*DirectInit=*/true);
Douglas Gregor23996282009-05-12 21:31:51 +00002443 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002444 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00002445 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00002446 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00002447 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2448
Richard Smithc95d2c52017-09-22 04:25:05 +00002449 InitializerScopeRAII InitScope(*this, D, ThisDecl);
Sebastian Redl3da34892011-06-05 12:23:16 +00002450
2451 ExprResult Init(ParseBraceInitializer());
2452
Richard Smithc95d2c52017-09-22 04:25:05 +00002453 InitScope.pop();
Sebastian Redl3da34892011-06-05 12:23:16 +00002454
2455 if (Init.isInvalid()) {
2456 Actions.ActOnInitializerError(ThisDecl);
2457 } else
Richard Smith3beb7c62017-01-12 02:27:38 +00002458 Actions.AddInitializerToDecl(ThisDecl, Init.get(), /*DirectInit=*/true);
Sebastian Redl3da34892011-06-05 12:23:16 +00002459
Douglas Gregor23996282009-05-12 21:31:51 +00002460 } else {
Richard Smith3beb7c62017-01-12 02:27:38 +00002461 Actions.ActOnUninitializedDecl(ThisDecl);
Douglas Gregor23996282009-05-12 21:31:51 +00002462 }
2463
Richard Smithb2bc2e62011-02-21 20:05:19 +00002464 Actions.FinalizeDeclaration(ThisDecl);
2465
Douglas Gregor23996282009-05-12 21:31:51 +00002466 return ThisDecl;
2467}
2468
Chris Lattner1890ac82006-08-13 01:16:23 +00002469/// ParseSpecifierQualifierList
2470/// specifier-qualifier-list:
2471/// type-specifier specifier-qualifier-list[opt]
2472/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002473/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00002474///
Richard Smithc5b05522012-03-12 07:56:15 +00002475void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
2476 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00002477 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
2478 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002479 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Faisal Valia534f072018-04-26 00:42:40 +00002480 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00002481
Chris Lattner1890ac82006-08-13 01:16:23 +00002482 // Validate declspec for type-name.
2483 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith649c7b062014-01-08 00:56:48 +00002484 if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00002485 Diag(Tok, diag::err_expected_type);
2486 DS.SetTypeSpecError();
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00002487 } else if (Specs == DeclSpec::PQ_None && !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00002488 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00002489 if (!DS.hasTypeSpecifier())
2490 DS.SetTypeSpecError();
2491 }
Mike Stump11289f42009-09-09 15:08:12 +00002492
Chris Lattner1b22eed2006-11-28 05:12:07 +00002493 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002494 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00002495 if (DS.getStorageClassSpecLoc().isValid())
2496 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
2497 else
Richard Smithb4a9e862013-04-12 22:46:28 +00002498 Diag(DS.getThreadStorageClassSpecLoc(),
2499 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00002500 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002501 }
Mike Stump11289f42009-09-09 15:08:12 +00002502
Craig Topper3f13d4d22015-11-14 18:15:55 +00002503 // Issue diagnostic and remove function specifier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002504 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00002505 if (DS.isInlineSpecified())
2506 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
2507 if (DS.isVirtualSpecified())
2508 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
Richard Smith76b90272019-05-09 03:59:21 +00002509 if (DS.hasExplicitSpecifier())
Douglas Gregor61956c42008-10-31 09:07:45 +00002510 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00002511 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002512 }
Richard Smithc5b05522012-03-12 07:56:15 +00002513
Richard Smith76b90272019-05-09 03:59:21 +00002514 // Issue diagnostic and remove constexpr specifier if present.
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002515 if (DS.hasConstexprSpecifier() && DSC != DeclSpecContext::DSC_condition) {
2516 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr)
2517 << (DS.getConstexprSpecifier() == CSK_consteval);
Richard Smithc5b05522012-03-12 07:56:15 +00002518 DS.ClearConstexprSpec();
2519 }
Chris Lattner1890ac82006-08-13 01:16:23 +00002520}
Chris Lattner53361ac2006-08-10 05:19:57 +00002521
Chris Lattner6cc055a2009-04-12 20:42:31 +00002522/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2523/// specified token is valid after the identifier in a declarator which
2524/// immediately follows the declspec. For example, these things are valid:
2525///
2526/// int x [ 4]; // direct-declarator
2527/// int x ( int y); // direct-declarator
2528/// int(int x ) // direct-declarator
2529/// int x ; // simple-declaration
2530/// int x = 17; // init-declarator-list
2531/// int x , y; // init-declarator-list
2532/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002533/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002534/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002535///
2536/// This is not, because 'x' does not immediately follow the declspec (though
2537/// ')' happens to be valid anyway).
2538/// int (x)
2539///
2540static bool isValidAfterIdentifierInDeclarator(const Token &T) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002541 return T.isOneOf(tok::l_square, tok::l_paren, tok::r_paren, tok::semi,
2542 tok::comma, tok::equal, tok::kw_asm, tok::l_brace,
2543 tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002544}
2545
Chris Lattner20a0c612009-04-14 21:34:55 +00002546/// ParseImplicitInt - This method is called when we have an non-typename
2547/// identifier in a declspec (which normally terminates the decl spec) when
2548/// the declspec has no type specifier. In this case, the declspec is either
2549/// malformed or is "implicit int" (in K&R and C89).
2550///
2551/// This method handles diagnosing this prettily and returns false if the
2552/// declspec is done being processed. If it recovers and thinks there may be
2553/// other pieces of declspec after it, it returns true.
2554///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002555bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002556 const ParsedTemplateInfo &TemplateInfo,
Richard Smitha0a5d502014-05-08 22:32:00 +00002557 AccessSpecifier AS, DeclSpecContext DSC,
Michael Han9407e502012-11-26 22:54:45 +00002558 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002559 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002560
Chris Lattner20a0c612009-04-14 21:34:55 +00002561 SourceLocation Loc = Tok.getLocation();
2562 // If we see an identifier that is not a type name, we normally would
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002563 // parse it as the identifier being declared. However, when a typename
Chris Lattner20a0c612009-04-14 21:34:55 +00002564 // is typo'd or the definition is not included, this will incorrectly
2565 // parse the typename as the identifier name and fall over misparsing
2566 // later parts of the diagnostic.
2567 //
2568 // As such, we try to do some look-ahead in cases where this would
2569 // otherwise be an "implicit-int" case to see if this is invalid. For
2570 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2571 // an identifier with implicit int, we'd get a parse error because the
2572 // next token is obviously invalid for a type. Parse these as a case
2573 // with an invalid type specifier.
2574 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002575
Chris Lattner20a0c612009-04-14 21:34:55 +00002576 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002577 // error, do lookahead to try to do better recovery. This never applies
2578 // within a type specifier. Outside of C++, we allow this even if the
2579 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002580 // implicit int as an extension in C99 and C11.
Richard Smith649c7b062014-01-08 00:56:48 +00002581 if (!isTypeSpecifier(DSC) && !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002582 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002583 // If this token is valid for implicit int, e.g. "static x = 4", then
2584 // we just avoid eating the identifier, so it will be parsed as the
2585 // identifier in the declarator.
2586 return false;
2587 }
Mike Stump11289f42009-09-09 15:08:12 +00002588
Sven van Haastregte518bb42019-05-22 13:12:20 +00002589 // Early exit as Sema has a dedicated missing_actual_pipe_type diagnostic
2590 // for incomplete declarations such as `pipe p`.
2591 if (getLangOpts().OpenCLCPlusPlus && DS.isTypeSpecPipe())
2592 return false;
2593
Richard Smitha952ebb2012-05-15 21:01:51 +00002594 if (getLangOpts().CPlusPlus &&
2595 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2596 // Don't require a type specifier if we have the 'auto' storage class
2597 // specifier in C++98 -- we'll promote it to a type specifier.
Richard Smithfb8b7b92013-10-15 00:00:26 +00002598 if (SS)
2599 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
Richard Smitha952ebb2012-05-15 21:01:51 +00002600 return false;
2601 }
2602
Reid Kleckner9a96e422016-05-24 21:23:54 +00002603 if (getLangOpts().CPlusPlus && (!SS || SS->isEmpty()) &&
2604 getLangOpts().MSVCCompat) {
2605 // Lookup of an unqualified type name has failed in MSVC compatibility mode.
2606 // Give Sema a chance to recover if we are in a template with dependent base
2607 // classes.
2608 if (ParsedType T = Actions.ActOnMSVCUnknownTypeName(
2609 *Tok.getIdentifierInfo(), Tok.getLocation(),
Faisal Vali7db85c52017-12-31 00:06:40 +00002610 DSC == DeclSpecContext::DSC_template_type_arg)) {
Reid Kleckner9a96e422016-05-24 21:23:54 +00002611 const char *PrevSpec;
2612 unsigned DiagID;
2613 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
2614 Actions.getASTContext().getPrintingPolicy());
2615 DS.SetRangeEnd(Tok.getLocation());
2616 ConsumeToken();
2617 return false;
2618 }
2619 }
2620
Chris Lattner20a0c612009-04-14 21:34:55 +00002621 // Otherwise, if we don't consume this token, we are going to emit an
2622 // error anyway. Try to recover from various common problems. Check
2623 // to see if this was a reference to a tag name without a tag specified.
2624 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002625 //
2626 // C++ doesn't need this, and isTagName doesn't take SS.
Craig Topper161e4db2014-05-21 06:02:52 +00002627 if (SS == nullptr) {
2628 const char *TagName = nullptr, *FixitTagName = nullptr;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002629 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002630
Douglas Gregor0be31a22010-07-02 17:43:08 +00002631 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002632 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002633 case DeclSpec::TST_enum:
2634 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2635 case DeclSpec::TST_union:
2636 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2637 case DeclSpec::TST_struct:
2638 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002639 case DeclSpec::TST_interface:
2640 TagName="__interface"; FixitTagName = "__interface ";
2641 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002642 case DeclSpec::TST_class:
2643 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002644 }
Mike Stump11289f42009-09-09 15:08:12 +00002645
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002646 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002647 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2648 LookupResult R(Actions, TokenName, SourceLocation(),
2649 Sema::LookupOrdinaryName);
2650
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002651 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002652 << TokenName << TagName << getLangOpts().CPlusPlus
2653 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2654
2655 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2656 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2657 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002658 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002659 << TokenName << TagName;
2660 }
Mike Stump11289f42009-09-09 15:08:12 +00002661
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002662 // Parse this as a tag as if the missing tag were present.
2663 if (TagKind == tok::kw_enum)
Faisal Vali7db85c52017-12-31 00:06:40 +00002664 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS,
2665 DeclSpecContext::DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002666 else
Richard Smithc5b05522012-03-12 07:56:15 +00002667 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Faisal Vali7db85c52017-12-31 00:06:40 +00002668 /*EnteringContext*/ false,
2669 DeclSpecContext::DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002670 return true;
2671 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002672 }
Mike Stump11289f42009-09-09 15:08:12 +00002673
Richard Smithfe904f02012-05-15 21:29:55 +00002674 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002675 // being declared (with a missing type).
Faisal Vali7db85c52017-12-31 00:06:40 +00002676 if (!isTypeSpecifier(DSC) && (!SS || DSC == DeclSpecContext::DSC_top_level ||
2677 DSC == DeclSpecContext::DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002678 // Look ahead to the next token to try to figure out what this declaration
2679 // was supposed to be.
2680 switch (NextToken().getKind()) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002681 case tok::l_paren: {
2682 // static x(4); // 'x' is not a type
2683 // x(int n); // 'x' is not a type
2684 // x (*p)[]; // 'x' is a type
2685 //
Richard Smitha0a5d502014-05-08 22:32:00 +00002686 // Since we're in an error case, we can afford to perform a tentative
2687 // parse to determine which case we're in.
Richard Smitha952ebb2012-05-15 21:01:51 +00002688 TentativeParsingAction PA(*this);
2689 ConsumeToken();
2690 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2691 PA.Revert();
Richard Smithfb8b7b92013-10-15 00:00:26 +00002692
Richard Smithee390432014-05-16 01:56:53 +00002693 if (TPR != TPResult::False) {
Richard Smithfb8b7b92013-10-15 00:00:26 +00002694 // The identifier is followed by a parenthesized declarator.
2695 // It's supposed to be a type.
2696 break;
2697 }
2698
2699 // If we're in a context where we could be declaring a constructor,
2700 // check whether this is a constructor declaration with a bogus name.
Faisal Vali7db85c52017-12-31 00:06:40 +00002701 if (DSC == DeclSpecContext::DSC_class ||
2702 (DSC == DeclSpecContext::DSC_top_level && SS)) {
Richard Smithfb8b7b92013-10-15 00:00:26 +00002703 IdentifierInfo *II = Tok.getIdentifierInfo();
2704 if (Actions.isCurrentClassNameTypo(II, SS)) {
2705 Diag(Loc, diag::err_constructor_bad_name)
2706 << Tok.getIdentifierInfo() << II
2707 << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
2708 Tok.setIdentifierInfo(II);
2709 }
2710 }
2711 // Fall through.
Galina Kistanova77674252017-06-01 21:15:34 +00002712 LLVM_FALLTHROUGH;
Richard Smitha952ebb2012-05-15 21:01:51 +00002713 }
Richard Smithfb8b7b92013-10-15 00:00:26 +00002714 case tok::comma:
2715 case tok::equal:
2716 case tok::kw_asm:
2717 case tok::l_brace:
2718 case tok::l_square:
2719 case tok::semi:
2720 // This looks like a variable or function declaration. The type is
2721 // probably missing. We're done parsing decl-specifiers.
Nicolas Lesseree057172019-05-05 12:15:17 +00002722 // But only if we are not in a function prototype scope.
2723 if (getCurScope()->isFunctionPrototypeScope())
2724 break;
Richard Smithfb8b7b92013-10-15 00:00:26 +00002725 if (SS)
2726 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2727 return false;
Richard Smitha952ebb2012-05-15 21:01:51 +00002728
2729 default:
2730 // This is probably supposed to be a type. This includes cases like:
2731 // int f(itn);
2732 // struct S { unsinged : 4; };
2733 break;
2734 }
2735 }
2736
Reid Klecknerc05ca5e2014-06-19 01:23:22 +00002737 // This is almost certainly an invalid type name. Let Sema emit a diagnostic
2738 // and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002739 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002740 IdentifierInfo *II = Tok.getIdentifierInfo();
Richard Smith52f8d192017-05-10 21:32:16 +00002741 bool IsTemplateName = getLangOpts().CPlusPlus && NextToken().is(tok::less);
Reid Klecknerc05ca5e2014-06-19 01:23:22 +00002742 Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T,
Richard Smith52f8d192017-05-10 21:32:16 +00002743 IsTemplateName);
Reid Klecknerc05ca5e2014-06-19 01:23:22 +00002744 if (T) {
2745 // The action has suggested that the type T could be used. Set that as
2746 // the type in the declaration specifiers, consume the would-be type
2747 // name token, and we're done.
2748 const char *PrevSpec;
2749 unsigned DiagID;
2750 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
2751 Actions.getASTContext().getPrintingPolicy());
2752 DS.SetRangeEnd(Tok.getLocation());
2753 ConsumeToken();
2754 // There may be other declaration specifiers after this.
2755 return true;
2756 } else if (II != Tok.getIdentifierInfo()) {
2757 // If no type was suggested, the correction is to a keyword
2758 Tok.setKind(II->getTokenID());
2759 // There may be other declaration specifiers after this.
2760 return true;
Douglas Gregor15e56022009-10-13 23:27:22 +00002761 }
Mike Stump11289f42009-09-09 15:08:12 +00002762
Reid Klecknerc05ca5e2014-06-19 01:23:22 +00002763 // Otherwise, the action had no suggestion for us. Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002764 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002765 DS.SetRangeEnd(Tok.getLocation());
2766 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002767
Richard Smith52f8d192017-05-10 21:32:16 +00002768 // Eat any following template arguments.
2769 if (IsTemplateName) {
2770 SourceLocation LAngle, RAngle;
2771 TemplateArgList Args;
2772 ParseTemplateIdAfterTemplateName(true, LAngle, Args, RAngle);
2773 }
2774
Chris Lattner20a0c612009-04-14 21:34:55 +00002775 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2776 // avoid rippling error messages on subsequent uses of the same type,
2777 // could be useful if #include was forgotten.
Richard Smithb3065792019-05-07 07:36:07 +00002778 return true;
Chris Lattner20a0c612009-04-14 21:34:55 +00002779}
2780
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002781/// Determine the declaration specifier context from the declarator
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002782/// context.
2783///
2784/// \param Context the declarator context, which is one of the
Faisal Vali421b2d12017-12-29 05:41:00 +00002785/// DeclaratorContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002786Parser::DeclSpecContext
Faisal Vali421b2d12017-12-29 05:41:00 +00002787Parser::getDeclSpecContextFromDeclaratorContext(DeclaratorContext Context) {
2788 if (Context == DeclaratorContext::MemberContext)
Faisal Vali7db85c52017-12-31 00:06:40 +00002789 return DeclSpecContext::DSC_class;
Faisal Vali421b2d12017-12-29 05:41:00 +00002790 if (Context == DeclaratorContext::FileContext)
Faisal Vali7db85c52017-12-31 00:06:40 +00002791 return DeclSpecContext::DSC_top_level;
Faisal Vali421b2d12017-12-29 05:41:00 +00002792 if (Context == DeclaratorContext::TemplateParamContext)
Faisal Vali7db85c52017-12-31 00:06:40 +00002793 return DeclSpecContext::DSC_template_param;
Richard Smith77a9c602018-02-28 03:02:23 +00002794 if (Context == DeclaratorContext::TemplateArgContext ||
2795 Context == DeclaratorContext::TemplateTypeArgContext)
Faisal Vali7db85c52017-12-31 00:06:40 +00002796 return DeclSpecContext::DSC_template_type_arg;
Richard Smithe303e352018-02-02 22:24:54 +00002797 if (Context == DeclaratorContext::TrailingReturnContext ||
2798 Context == DeclaratorContext::TrailingReturnVarContext)
Faisal Vali7db85c52017-12-31 00:06:40 +00002799 return DeclSpecContext::DSC_trailing;
Faisal Vali421b2d12017-12-29 05:41:00 +00002800 if (Context == DeclaratorContext::AliasDeclContext ||
2801 Context == DeclaratorContext::AliasTemplateContext)
Faisal Vali7db85c52017-12-31 00:06:40 +00002802 return DeclSpecContext::DSC_alias_declaration;
2803 return DeclSpecContext::DSC_normal;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002804}
2805
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002806/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2807///
2808/// FIXME: Simply returns an alignof() expression if the argument is a
2809/// type. Ideally, the type should be propagated directly into Sema.
2810///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002811/// [C11] type-id
2812/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002813/// [C++0x] type-id ...[opt]
2814/// [C++0x] assignment-expression ...[opt]
2815ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2816 SourceLocation &EllipsisLoc) {
2817 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002818 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002819 SourceLocation TypeLoc = Tok.getLocation();
2820 ParsedType Ty = ParseTypeName().get();
2821 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002822 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2823 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002824 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002825 ER = ParseConstantExpression();
2826
Alp Toker8fbec672013-12-17 23:29:36 +00002827 if (getLangOpts().CPlusPlus11)
2828 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002829
2830 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002831}
2832
2833/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2834/// attribute to Attrs.
2835///
2836/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002837/// [C11] '_Alignas' '(' type-id ')'
2838/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002839/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2840/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002841void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002842 SourceLocation *EndLoc) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002843 assert(Tok.isOneOf(tok::kw_alignas, tok::kw__Alignas) &&
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002844 "Not an alignment-specifier!");
2845
Richard Smithd11c7a12013-01-29 01:48:07 +00002846 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2847 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002848
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002849 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00002850 if (T.expectAndConsume())
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002851 return;
2852
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002853 SourceLocation EllipsisLoc;
2854 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002855 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002856 T.skipToEnd();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002857 return;
2858 }
2859
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002860 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002861 if (EndLoc)
2862 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002863
Aaron Ballman00e99962013-08-31 01:11:41 +00002864 ArgsVector ArgExprs;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002865 ArgExprs.push_back(ArgExpr.get());
Craig Topper161e4db2014-05-21 06:02:52 +00002866 Attrs.addNew(KWName, KWLoc, nullptr, KWLoc, ArgExprs.data(), 1,
Erich Keanee891aa92018-07-13 15:07:47 +00002867 ParsedAttr::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002868}
2869
Richard Smith404dfb42013-11-19 22:47:36 +00002870/// Determine whether we're looking at something that might be a declarator
2871/// in a simple-declaration. If it can't possibly be a declarator, maybe
2872/// diagnose a missing semicolon after a prior tag definition in the decl
2873/// specifier.
2874///
2875/// \return \c true if an error occurred and this can't be any kind of
2876/// declaration.
2877bool
2878Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
2879 DeclSpecContext DSContext,
2880 LateParsedAttrList *LateAttrs) {
2881 assert(DS.hasTagDefinition() && "shouldn't call this");
2882
Faisal Vali7db85c52017-12-31 00:06:40 +00002883 bool EnteringContext = (DSContext == DeclSpecContext::DSC_class ||
2884 DSContext == DeclSpecContext::DSC_top_level);
Richard Smith404dfb42013-11-19 22:47:36 +00002885
2886 if (getLangOpts().CPlusPlus &&
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002887 Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype,
2888 tok::annot_template_id) &&
Richard Smith404dfb42013-11-19 22:47:36 +00002889 TryAnnotateCXXScopeToken(EnteringContext)) {
2890 SkipMalformedDecl();
2891 return true;
2892 }
2893
Richard Smith698875a2013-11-20 23:40:57 +00002894 bool HasScope = Tok.is(tok::annot_cxxscope);
2895 // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
2896 Token AfterScope = HasScope ? NextToken() : Tok;
2897
Richard Smith404dfb42013-11-19 22:47:36 +00002898 // Determine whether the following tokens could possibly be a
2899 // declarator.
Richard Smith698875a2013-11-20 23:40:57 +00002900 bool MightBeDeclarator = true;
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002901 if (Tok.isOneOf(tok::kw_typename, tok::annot_typename)) {
Richard Smith698875a2013-11-20 23:40:57 +00002902 // A declarator-id can't start with 'typename'.
2903 MightBeDeclarator = false;
2904 } else if (AfterScope.is(tok::annot_template_id)) {
2905 // If we have a type expressed as a template-id, this cannot be a
2906 // declarator-id (such a type cannot be redeclared in a simple-declaration).
2907 TemplateIdAnnotation *Annot =
2908 static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
2909 if (Annot->Kind == TNK_Type_template)
2910 MightBeDeclarator = false;
2911 } else if (AfterScope.is(tok::identifier)) {
2912 const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken();
2913
Richard Smith404dfb42013-11-19 22:47:36 +00002914 // These tokens cannot come after the declarator-id in a
2915 // simple-declaration, and are likely to come after a type-specifier.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002916 if (Next.isOneOf(tok::star, tok::amp, tok::ampamp, tok::identifier,
2917 tok::annot_cxxscope, tok::coloncolon)) {
Richard Smith698875a2013-11-20 23:40:57 +00002918 // Missing a semicolon.
2919 MightBeDeclarator = false;
2920 } else if (HasScope) {
2921 // If the declarator-id has a scope specifier, it must redeclare a
2922 // previously-declared entity. If that's a type (and this is not a
2923 // typedef), that's an error.
2924 CXXScopeSpec SS;
2925 Actions.RestoreNestedNameSpecifierAnnotation(
2926 Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
2927 IdentifierInfo *Name = AfterScope.getIdentifierInfo();
2928 Sema::NameClassification Classification = Actions.ClassifyName(
2929 getCurScope(), SS, Name, AfterScope.getLocation(), Next,
Bruno Ricci70ad3962019-03-25 17:08:51 +00002930 /*IsAddressOfOperand=*/false, /*CCC=*/nullptr);
Richard Smith698875a2013-11-20 23:40:57 +00002931 switch (Classification.getKind()) {
2932 case Sema::NC_Error:
2933 SkipMalformedDecl();
2934 return true;
Richard Smith404dfb42013-11-19 22:47:36 +00002935
Richard Smith698875a2013-11-20 23:40:57 +00002936 case Sema::NC_Keyword:
2937 case Sema::NC_NestedNameSpecifier:
2938 llvm_unreachable("typo correction and nested name specifiers not "
2939 "possible here");
Richard Smith404dfb42013-11-19 22:47:36 +00002940
Richard Smith698875a2013-11-20 23:40:57 +00002941 case Sema::NC_Type:
2942 case Sema::NC_TypeTemplate:
2943 // Not a previously-declared non-type entity.
2944 MightBeDeclarator = false;
2945 break;
Richard Smith404dfb42013-11-19 22:47:36 +00002946
Richard Smith698875a2013-11-20 23:40:57 +00002947 case Sema::NC_Unknown:
2948 case Sema::NC_Expression:
2949 case Sema::NC_VarTemplate:
2950 case Sema::NC_FunctionTemplate:
Richard Smithb23c5e82019-05-09 03:31:27 +00002951 case Sema::NC_UndeclaredTemplate:
Richard Smith698875a2013-11-20 23:40:57 +00002952 // Might be a redeclaration of a prior entity.
2953 break;
2954 }
Richard Smith404dfb42013-11-19 22:47:36 +00002955 }
Richard Smith404dfb42013-11-19 22:47:36 +00002956 }
2957
Richard Smith698875a2013-11-20 23:40:57 +00002958 if (MightBeDeclarator)
Richard Smith404dfb42013-11-19 22:47:36 +00002959 return false;
2960
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002961 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002962 Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getEndLoc()),
Alp Toker383d2c42014-01-01 03:08:43 +00002963 diag::err_expected_after)
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002964 << DeclSpec::getSpecifierName(DS.getTypeSpecType(), PPol) << tok::semi;
Richard Smith404dfb42013-11-19 22:47:36 +00002965
2966 // Try to recover from the typo, by dropping the tag definition and parsing
2967 // the problematic tokens as a type.
2968 //
2969 // FIXME: Split the DeclSpec into pieces for the standalone
2970 // declaration and pieces for the following declaration, instead
2971 // of assuming that all the other pieces attach to new declaration,
2972 // and call ParsedFreeStandingDeclSpec as appropriate.
2973 DS.ClearTypeSpecType();
2974 ParsedTemplateInfo NotATemplate;
Faisal Valia534f072018-04-26 00:42:40 +00002975 ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
Richard Smith404dfb42013-11-19 22:47:36 +00002976 return false;
2977}
2978
Leonard Chanab80f3c2018-06-14 14:53:51 +00002979// Choose the apprpriate diagnostic error for why fixed point types are
2980// disabled, set the previous specifier, and mark as invalid.
2981static void SetupFixedPointError(const LangOptions &LangOpts,
2982 const char *&PrevSpec, unsigned &DiagID,
2983 bool &isInvalid) {
2984 assert(!LangOpts.FixedPoint);
2985 DiagID = diag::err_fixed_point_not_enabled;
2986 PrevSpec = ""; // Not used by diagnostic
2987 isInvalid = true;
2988}
2989
Faisal Valia534f072018-04-26 00:42:40 +00002990/// ParseDeclarationSpecifiers
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002991/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002992/// storage-class-specifier declaration-specifiers[opt]
2993/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002994/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002995/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002996/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002997/// [Clang] '__module_private__' declaration-specifiers[opt]
Douglas Gregorab209d82015-07-07 03:58:42 +00002998/// [ObjC1] '__kindof' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002999///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003000/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00003001/// 'typedef'
3002/// 'extern'
3003/// 'static'
3004/// 'auto'
3005/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00003006/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00003007/// [C++11] 'thread_local'
3008/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00003009/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003010/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00003011/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00003012/// [C++] 'virtual'
3013/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003014/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00003015/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00003016/// 'constexpr': [C++0x dcl.constexpr]
Faisal Valia534f072018-04-26 00:42:40 +00003017void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00003018 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00003019 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00003020 DeclSpecContext DSContext,
3021 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00003022 if (DS.getSourceRange().isInvalid()) {
David Majnemer24b28302014-07-26 05:41:31 +00003023 // Start the range at the current token but make the end of the range
3024 // invalid. This will make the entire range invalid unless we successfully
3025 // consume a token.
Douglas Gregor0e7dde52011-04-24 05:37:28 +00003026 DS.SetRangeStart(Tok.getLocation());
David Majnemer24b28302014-07-26 05:41:31 +00003027 DS.SetRangeEnd(SourceLocation());
Douglas Gregor0e7dde52011-04-24 05:37:28 +00003028 }
Chad Rosierc1183952012-06-26 22:30:43 +00003029
Faisal Vali7db85c52017-12-31 00:06:40 +00003030 bool EnteringContext = (DSContext == DeclSpecContext::DSC_class ||
3031 DSContext == DeclSpecContext::DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003032 bool AttrsLastTime = false;
3033 ParsedAttributesWithRange attrs(AttrFactory);
Benjamin Kramere4812142015-03-12 14:28:38 +00003034 // We use Sema's policy to get bool macros right.
Richard Smith301bc212016-05-19 01:39:10 +00003035 PrintingPolicy Policy = Actions.getPrintingPolicy();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003036 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00003037 bool isInvalid = false;
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003038 bool isStorageClass = false;
Craig Topper161e4db2014-05-21 06:02:52 +00003039 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +00003040 unsigned DiagID = 0;
3041
Richard Smithba24f352019-05-09 19:45:46 +00003042 // This value needs to be set to the location of the last token if the last
3043 // token of the specifier is already consumed.
3044 SourceLocation ConsumedEnd;
Richard Smith76b90272019-05-09 03:59:21 +00003045
David Majnemer51fd8a02015-07-22 23:46:18 +00003046 // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL
3047 // implementation for VS2013 uses _Atomic as an identifier for one of the
3048 // classes in <atomic>.
3049 //
3050 // A typedef declaration containing _Atomic<...> is among the places where
3051 // the class is used. If we are currently parsing such a declaration, treat
3052 // the token as an identifier.
3053 if (getLangOpts().MSVCCompat && Tok.is(tok::kw__Atomic) &&
3054 DS.getStorageClassSpec() == clang::DeclSpec::SCS_typedef &&
3055 !DS.hasTypeSpecifier() && GetLookAheadToken(1).is(tok::less))
3056 Tok.setKind(tok::identifier);
3057
Chris Lattner4d8f8732006-11-28 05:05:08 +00003058 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00003059
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003060 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00003061 default:
Chris Lattner0974b232008-07-26 00:20:22 +00003062 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003063 if (!AttrsLastTime)
3064 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00003065 else {
3066 // Reject C++11 attributes that appertain to decl specifiers as
3067 // we don't support any C++11 attributes that appertain to decl
3068 // specifiers. This also conforms to what g++ 4.8 is doing.
Richard Smith49cc1cc2016-08-18 21:59:42 +00003069 ProhibitCXX11Attributes(attrs, diag::err_attribute_not_type_attr);
Michael Han64536a62012-11-06 19:34:54 +00003070
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003071 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00003072 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00003073
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003074 // If this is not a declaration specifier token, we're done reading decl
3075 // specifiers. First verify that DeclSpec's are consistent.
Craig Topper25122412015-11-15 03:32:11 +00003076 DS.Finish(Actions, Policy);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003077 return;
Mike Stump11289f42009-09-09 15:08:12 +00003078
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003079 case tok::l_square:
3080 case tok::kw_alignas:
Aaron Ballman606093a2017-10-15 15:01:42 +00003081 if (!standardAttributesAllowed() || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003082 goto DoneWithDeclSpec;
3083
3084 ProhibitAttributes(attrs);
3085 // FIXME: It would be good to recover by accepting the attributes,
3086 // but attempting to do that now would cause serious
3087 // madness in terms of diagnostics.
3088 attrs.clear();
3089 attrs.Range = SourceRange();
3090
3091 ParseCXX11Attributes(attrs);
3092 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00003093 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003094
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003095 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00003096 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003097 if (DS.hasTypeSpecifier()) {
3098 bool AllowNonIdentifiers
3099 = (getCurScope()->getFlags() & (Scope::ControlScope |
3100 Scope::BlockScope |
3101 Scope::TemplateParamScope |
3102 Scope::FunctionPrototypeScope |
3103 Scope::AtCatchScope)) == 0;
3104 bool AllowNestedNameSpecifiers
Faisal Vali7db85c52017-12-31 00:06:40 +00003105 = DSContext == DeclSpecContext::DSC_top_level ||
3106 (DSContext == DeclSpecContext::DSC_class && DS.isFriendSpecified());
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003107
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00003108 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00003109 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00003110 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003111 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00003112 }
3113
Douglas Gregor80039242011-02-15 20:33:25 +00003114 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
3115 CCC = Sema::PCC_LocalDeclarationSpecifiers;
3116 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Faisal Vali7db85c52017-12-31 00:06:40 +00003117 CCC = DSContext == DeclSpecContext::DSC_class ? Sema::PCC_MemberTemplate
3118 : Sema::PCC_Template;
3119 else if (DSContext == DeclSpecContext::DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00003120 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00003121 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00003122 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00003123
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003124 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003125 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003126 }
3127
Chris Lattnerbd31aa32009-01-05 00:07:25 +00003128 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00003129 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman2a1d9a92013-08-15 23:59:20 +00003130 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00003131 if (!DS.hasTypeSpecifier())
3132 DS.SetTypeSpecError();
3133 goto DoneWithDeclSpec;
3134 }
John McCall8bc2a702010-03-01 18:20:46 +00003135 if (Tok.is(tok::coloncolon)) // ::new or ::delete
3136 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00003137 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003138
3139 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00003140 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003141 goto DoneWithDeclSpec;
3142
John McCall9dab4e62009-12-12 11:40:51 +00003143 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00003144 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
3145 Tok.getAnnotationRange(),
3146 SS);
John McCall9dab4e62009-12-12 11:40:51 +00003147
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003148 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00003149 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00003150 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00003151 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00003152 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00003153 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003154
Richard Smith74f02342017-01-19 21:00:13 +00003155 // If this would be a valid constructor declaration with template
3156 // arguments, we will reject the attempt to form an invalid type-id
3157 // referring to the injected-class-name when we annotate the token,
3158 // per C++ [class.qual]p2.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003159 //
Richard Smith74f02342017-01-19 21:00:13 +00003160 // To improve diagnostics for this case, parse the declaration as a
3161 // constructor (and reject the extra template arguments later).
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00003162 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Faisal Vali7db85c52017-12-31 00:06:40 +00003163 if ((DSContext == DeclSpecContext::DSC_top_level ||
3164 DSContext == DeclSpecContext::DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00003165 TemplateId->Name &&
Richard Smith74f02342017-01-19 21:00:13 +00003166 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS) &&
Faisal Vali7db85c52017-12-31 00:06:40 +00003167 isConstructorDeclarator(/*Unqualified*/ false)) {
Richard Smith74f02342017-01-19 21:00:13 +00003168 // The user meant this to be an out-of-line constructor
3169 // definition, but template arguments are not allowed
3170 // there. Just allow this as a constructor; we'll
3171 // complain about it later.
3172 goto DoneWithDeclSpec;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003173 }
3174
John McCall9dab4e62009-12-12 11:40:51 +00003175 DS.getTypeSpecScope() = SS;
Richard Smithaf3b3252017-05-18 19:21:48 +00003176 ConsumeAnnotationToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00003177 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00003178 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00003179 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00003180 continue;
3181 }
3182
Douglas Gregorc5790df2009-09-28 07:26:33 +00003183 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00003184 DS.getTypeSpecScope() = SS;
Richard Smithaf3b3252017-05-18 19:21:48 +00003185 ConsumeAnnotationToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00003186 if (Tok.getAnnotationValue()) {
3187 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00003188 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00003189 Tok.getAnnotationEndLoc(),
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003190 PrevSpec, DiagID, T, Policy);
Richard Smithda837032012-09-14 18:27:01 +00003191 if (isInvalid)
3192 break;
John McCallba7bf592010-08-24 05:47:05 +00003193 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00003194 else
3195 DS.SetTypeSpecError();
3196 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
Richard Smithaf3b3252017-05-18 19:21:48 +00003197 ConsumeAnnotationToken(); // The typename
Douglas Gregorc5790df2009-09-28 07:26:33 +00003198 }
3199
Douglas Gregor167fa622009-03-25 15:40:00 +00003200 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003201 goto DoneWithDeclSpec;
3202
Richard Smith74f02342017-01-19 21:00:13 +00003203 // Check whether this is a constructor declaration. If we're in a
3204 // context where the identifier could be a class name, and it has the
3205 // shape of a constructor declaration, process it as one.
Faisal Vali7db85c52017-12-31 00:06:40 +00003206 if ((DSContext == DeclSpecContext::DSC_top_level ||
3207 DSContext == DeclSpecContext::DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00003208 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Richard Smith74f02342017-01-19 21:00:13 +00003209 &SS) &&
3210 isConstructorDeclarator(/*Unqualified*/ false))
3211 goto DoneWithDeclSpec;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003212
David Blaikieefdccaa2016-01-15 23:43:34 +00003213 ParsedType TypeRep =
3214 Actions.getTypeName(*Next.getIdentifierInfo(), Next.getLocation(),
3215 getCurScope(), &SS, false, false, nullptr,
3216 /*IsCtorOrDtorName=*/false,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00003217 /*WantNontrivialTypeSourceInfo=*/true,
Richard Smith600b5262017-01-26 20:40:47 +00003218 isClassTemplateDeductionContext(DSContext));
Douglas Gregor8bf42052009-02-09 18:46:07 +00003219
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00003220 // If the referenced identifier is not a type, then this declspec is
3221 // erroneous: We already checked about that it has no type specifier, and
3222 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00003223 // typename.
David Blaikie7d170102013-05-15 07:37:26 +00003224 if (!TypeRep) {
Richard Smithaf3b3252017-05-18 19:21:48 +00003225 // Eat the scope spec so the identifier is current.
3226 ConsumeAnnotationToken();
Michael Han9407e502012-11-26 22:54:45 +00003227 ParsedAttributesWithRange Attrs(AttrFactory);
3228 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
3229 if (!Attrs.empty()) {
3230 AttrsLastTime = true;
3231 attrs.takeAllFrom(Attrs);
3232 }
3233 continue;
3234 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003235 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00003236 }
Mike Stump11289f42009-09-09 15:08:12 +00003237
John McCall9dab4e62009-12-12 11:40:51 +00003238 DS.getTypeSpecScope() = SS;
Richard Smithaf3b3252017-05-18 19:21:48 +00003239 ConsumeAnnotationToken(); // The C++ scope.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003240
Douglas Gregor9817f4a2009-02-09 15:09:02 +00003241 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003242 DiagID, TypeRep, Policy);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003243 if (isInvalid)
3244 break;
Mike Stump11289f42009-09-09 15:08:12 +00003245
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003246 DS.SetRangeEnd(Tok.getLocation());
3247 ConsumeToken(); // The typename.
3248
3249 continue;
3250 }
Mike Stump11289f42009-09-09 15:08:12 +00003251
Chris Lattnere387d9e2009-01-21 19:48:37 +00003252 case tok::annot_typename: {
Richard Smith404dfb42013-11-19 22:47:36 +00003253 // If we've previously seen a tag definition, we were almost surely
3254 // missing a semicolon after it.
3255 if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
3256 goto DoneWithDeclSpec;
3257
John McCallba7bf592010-08-24 05:47:05 +00003258 if (Tok.getAnnotationValue()) {
3259 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00003260 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003261 DiagID, T, Policy);
John McCallba7bf592010-08-24 05:47:05 +00003262 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00003263 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00003264
Chris Lattner005fc1b2010-04-05 18:18:31 +00003265 if (isInvalid)
3266 break;
3267
Chris Lattnere387d9e2009-01-21 19:48:37 +00003268 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
Richard Smithaf3b3252017-05-18 19:21:48 +00003269 ConsumeAnnotationToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00003270
Chris Lattnere387d9e2009-01-21 19:48:37 +00003271 continue;
3272 }
Mike Stump11289f42009-09-09 15:08:12 +00003273
Douglas Gregor06873092011-04-28 15:48:45 +00003274 case tok::kw___is_signed:
3275 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
3276 // typically treats it as a trait. If we see __is_signed as it appears
3277 // in libstdc++, e.g.,
3278 //
3279 // static const bool __is_signed;
3280 //
3281 // then treat __is_signed as an identifier rather than as a keyword.
Faisal Vali090da2d2018-01-01 18:23:28 +00003282 if (DS.getTypeSpecType() == TST_bool &&
Douglas Gregor06873092011-04-28 15:48:45 +00003283 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
Alp Toker47642d22013-12-03 06:13:01 +00003284 DS.getStorageClassSpec() == DeclSpec::SCS_static)
3285 TryKeywordIdentFallback(true);
Douglas Gregor06873092011-04-28 15:48:45 +00003286
3287 // We're done with the declaration-specifiers.
3288 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003289
Chris Lattner16fac4f2008-07-26 01:18:38 +00003290 // typedef-name
Nikola Smiljanic67860242014-09-26 00:28:20 +00003291 case tok::kw___super:
David Blaikie15a430a2011-12-04 05:04:18 +00003292 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003293 case tok::identifier: {
Reid Klecknerc582f012014-07-14 18:19:58 +00003294 // This identifier can only be a typedef name if we haven't already seen
3295 // a type-specifier. Without this check we misparse:
3296 // typedef int X; struct Y { short X; }; as 'short int'.
3297 if (DS.hasTypeSpecifier())
3298 goto DoneWithDeclSpec;
3299
Aaron Ballman52d0aaa2017-02-14 22:47:20 +00003300 // If the token is an identifier named "__declspec" and Microsoft
3301 // extensions are not enabled, it is likely that there will be cascading
3302 // parse errors if this really is a __declspec attribute. Attempt to
3303 // recognize that scenario and recover gracefully.
3304 if (!getLangOpts().DeclSpecKeyword && Tok.is(tok::identifier) &&
3305 Tok.getIdentifierInfo()->getName().equals("__declspec")) {
3306 Diag(Loc, diag::err_ms_attributes_not_enabled);
3307
3308 // The next token should be an open paren. If it is, eat the entire
3309 // attribute declaration and continue.
3310 if (NextToken().is(tok::l_paren)) {
3311 // Consume the __declspec identifier.
Richard Trieuba057372017-02-14 23:56:55 +00003312 ConsumeToken();
Aaron Ballman52d0aaa2017-02-14 22:47:20 +00003313
3314 // Eat the parens and everything between them.
3315 BalancedDelimiterTracker T(*this, tok::l_paren);
3316 if (T.consumeOpen()) {
3317 assert(false && "Not a left paren?");
3318 return;
3319 }
3320 T.skipToEnd();
3321 continue;
3322 }
3323 }
3324
Serge Pavlov458ea762014-07-16 05:16:52 +00003325 // In C++, check to see if this is a scope specifier like foo::bar::, if
3326 // so handle it as such. This is important for ctor parsing.
3327 if (getLangOpts().CPlusPlus) {
3328 if (TryAnnotateCXXScopeToken(EnteringContext)) {
3329 DS.SetTypeSpecError();
3330 goto DoneWithDeclSpec;
3331 }
3332 if (!Tok.is(tok::identifier))
3333 continue;
3334 }
3335
John Thompson22334602010-02-05 00:12:22 +00003336 // Check for need to substitute AltiVec keyword tokens.
3337 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
3338 break;
3339
Richard Smith3092a3b2012-05-09 18:56:43 +00003340 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
3341 // allow the use of a typedef name as a type specifier.
3342 if (DS.isTypeAltiVecVector())
3343 goto DoneWithDeclSpec;
3344
Faisal Vali7db85c52017-12-31 00:06:40 +00003345 if (DSContext == DeclSpecContext::DSC_objc_method_result &&
3346 isObjCInstancetype()) {
Douglas Gregor5c0870a2015-06-19 23:18:00 +00003347 ParsedType TypeRep = Actions.ActOnObjCInstanceType(Loc);
3348 assert(TypeRep);
3349 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
3350 DiagID, TypeRep, Policy);
3351 if (isInvalid)
3352 break;
3353
3354 DS.SetRangeEnd(Loc);
3355 ConsumeToken();
3356 continue;
3357 }
3358
Richard Smith715ee072018-06-20 21:58:20 +00003359 // If we're in a context where the identifier could be a class name,
3360 // check whether this is a constructor declaration.
3361 if (getLangOpts().CPlusPlus && DSContext == DeclSpecContext::DSC_class &&
3362 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
3363 isConstructorDeclarator(/*Unqualified*/true))
3364 goto DoneWithDeclSpec;
3365
Richard Smith600b5262017-01-26 20:40:47 +00003366 ParsedType TypeRep = Actions.getTypeName(
3367 *Tok.getIdentifierInfo(), Tok.getLocation(), getCurScope(), nullptr,
3368 false, false, nullptr, false, false,
3369 isClassTemplateDeductionContext(DSContext));
Douglas Gregor8bf42052009-02-09 18:46:07 +00003370
Chris Lattner6cc055a2009-04-12 20:42:31 +00003371 // If this is not a typedef name, don't parse it as part of the declspec,
3372 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00003373 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00003374 ParsedAttributesWithRange Attrs(AttrFactory);
Craig Topper161e4db2014-05-21 06:02:52 +00003375 if (ParseImplicitInt(DS, nullptr, TemplateInfo, AS, DSContext, Attrs)) {
Michael Han9407e502012-11-26 22:54:45 +00003376 if (!Attrs.empty()) {
3377 AttrsLastTime = true;
3378 attrs.takeAllFrom(Attrs);
3379 }
3380 continue;
3381 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00003382 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00003383 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00003384
Richard Smith35845152017-02-07 01:37:30 +00003385 // Likewise, if this is a context where the identifier could be a template
3386 // name, check whether this is a deduction guide declaration.
Aaron Ballmanc351fba2017-12-04 20:27:34 +00003387 if (getLangOpts().CPlusPlus17 &&
Faisal Vali7db85c52017-12-31 00:06:40 +00003388 (DSContext == DeclSpecContext::DSC_class ||
3389 DSContext == DeclSpecContext::DSC_top_level) &&
Richard Smith35845152017-02-07 01:37:30 +00003390 Actions.isDeductionGuideName(getCurScope(), *Tok.getIdentifierInfo(),
3391 Tok.getLocation()) &&
3392 isConstructorDeclarator(/*Unqualified*/ true,
3393 /*DeductionGuide*/ true))
3394 goto DoneWithDeclSpec;
3395
Douglas Gregor9817f4a2009-02-09 15:09:02 +00003396 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003397 DiagID, TypeRep, Policy);
Chris Lattner16fac4f2008-07-26 01:18:38 +00003398 if (isInvalid)
3399 break;
Mike Stump11289f42009-09-09 15:08:12 +00003400
Chris Lattner16fac4f2008-07-26 01:18:38 +00003401 DS.SetRangeEnd(Tok.getLocation());
3402 ConsumeToken(); // The identifier
3403
Douglas Gregore9d95f12015-07-07 03:57:35 +00003404 // Objective-C supports type arguments and protocol references
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003405 // following an Objective-C object or object pointer
3406 // type. Handle either one of them.
Erik Pilkingtonfa983902018-10-30 20:31:30 +00003407 if (Tok.is(tok::less) && getLangOpts().ObjC) {
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003408 SourceLocation NewEndLoc;
3409 TypeResult NewTypeRep = parseObjCTypeArgsAndProtocolQualifiers(
3410 Loc, TypeRep, /*consumeLastToken=*/true,
3411 NewEndLoc);
3412 if (NewTypeRep.isUsable()) {
3413 DS.UpdateTypeRep(NewTypeRep.get());
3414 DS.SetRangeEnd(NewEndLoc);
3415 }
Douglas Gregore9d95f12015-07-07 03:57:35 +00003416 }
Chad Rosierc1183952012-06-26 22:30:43 +00003417
Steve Naroffcd5e7822008-09-22 10:28:57 +00003418 // Need to support trailing type qualifiers (e.g. "id<p> const").
3419 // If a type specifier follows, it will be diagnosed elsewhere.
3420 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00003421 }
Douglas Gregor7f741122009-02-25 19:37:18 +00003422
3423 // type-name
3424 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00003425 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Richard Smithb23c5e82019-05-09 03:31:27 +00003426 if (TemplateId->Kind != TNK_Type_template &&
3427 TemplateId->Kind != TNK_Undeclared_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00003428 // This template-id does not refer to a type name, so we're
3429 // done with the type-specifiers.
3430 goto DoneWithDeclSpec;
3431 }
3432
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003433 // If we're in a context where the template-id could be a
3434 // constructor name or specialization, check whether this is a
3435 // constructor declaration.
Faisal Vali7db85c52017-12-31 00:06:40 +00003436 if (getLangOpts().CPlusPlus && DSContext == DeclSpecContext::DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00003437 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Richard Smith446161b2014-03-03 21:12:53 +00003438 isConstructorDeclarator(TemplateId->SS.isEmpty()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003439 goto DoneWithDeclSpec;
3440
Douglas Gregor7f741122009-02-25 19:37:18 +00003441 // Turn the template-id annotation token into a type annotation
3442 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00003443 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00003444 continue;
3445 }
3446
Chris Lattnere37e2332006-08-15 04:50:22 +00003447 // GNU attributes support.
3448 case tok::kw___attribute:
Craig Topper161e4db2014-05-21 06:02:52 +00003449 ParseGNUAttributes(DS.getAttributes(), nullptr, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00003450 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00003451
3452 // Microsoft declspec support.
3453 case tok::kw___declspec:
Aaron Ballman068aa512015-05-20 20:58:33 +00003454 ParseMicrosoftDeclSpecs(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00003455 continue;
Mike Stump11289f42009-09-09 15:08:12 +00003456
Steve Naroff44ac7772008-12-25 14:16:32 +00003457 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00003458 case tok::kw___forceinline: {
Serge Pavlov750db652013-11-13 06:57:53 +00003459 isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00003460 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00003461 SourceLocation AttrNameLoc = Tok.getLocation();
Craig Topper161e4db2014-05-21 06:02:52 +00003462 DS.getAttributes().addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc,
Erich Keanee891aa92018-07-13 15:07:47 +00003463 nullptr, 0, ParsedAttr::AS_Keyword);
Richard Smithda837032012-09-14 18:27:01 +00003464 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00003465 }
Eli Friedman53339e02009-06-08 23:27:34 +00003466
Andrey Bokhanko45d41322016-05-11 18:38:21 +00003467 case tok::kw___unaligned:
3468 isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID,
3469 getLangOpts());
3470 break;
3471
Aaron Ballman317a77f2013-05-22 23:25:32 +00003472 case tok::kw___sptr:
3473 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00003474 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003475 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00003476 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00003477 case tok::kw___cdecl:
3478 case tok::kw___stdcall:
3479 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003480 case tok::kw___thiscall:
Erich Keane757d3172016-11-02 18:29:35 +00003481 case tok::kw___regcall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00003482 case tok::kw___vectorcall:
John McCall53fa7142010-12-24 02:08:15 +00003483 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003484 continue;
3485
Dawn Perchik335e16b2010-09-03 01:29:35 +00003486 // Borland single token adornments.
3487 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00003488 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00003489 continue;
3490
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003491 // OpenCL single token adornments.
3492 case tok::kw___kernel:
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +00003493 ParseOpenCLKernelAttributes(DS.getAttributes());
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003494 continue;
3495
Douglas Gregor261a89b2015-06-19 17:51:05 +00003496 // Nullability type specifiers.
Douglas Gregoraea7afd2015-06-24 22:02:08 +00003497 case tok::kw__Nonnull:
3498 case tok::kw__Nullable:
3499 case tok::kw__Null_unspecified:
Douglas Gregor261a89b2015-06-19 17:51:05 +00003500 ParseNullabilityTypeSpecifiers(DS.getAttributes());
3501 continue;
3502
Douglas Gregorab209d82015-07-07 03:58:42 +00003503 // Objective-C 'kindof' types.
3504 case tok::kw___kindof:
3505 DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc,
Erich Keanee891aa92018-07-13 15:07:47 +00003506 nullptr, 0, ParsedAttr::AS_Keyword);
Douglas Gregorab209d82015-07-07 03:58:42 +00003507 (void)ConsumeToken();
3508 continue;
3509
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003510 // storage-class-specifier
3511 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003512 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003513 PrevSpec, DiagID, Policy);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003514 isStorageClass = true;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003515 break;
3516 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00003517 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00003518 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003519 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003520 PrevSpec, DiagID, Policy);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003521 isStorageClass = true;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003522 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00003523 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003524 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003525 Loc, PrevSpec, DiagID, Policy);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003526 isStorageClass = true;
Steve Naroff2050b0d2007-12-18 00:16:02 +00003527 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003528 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00003529 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00003530 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003531 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003532 PrevSpec, DiagID, Policy);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003533 isStorageClass = true;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003534 break;
3535 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003536 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003537 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003538 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003539 PrevSpec, DiagID, Policy);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003540 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00003541 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003542 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00003543 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003544 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003545 DiagID, Policy);
Richard Smith58c74332011-09-04 19:54:14 +00003546 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003547 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003548 PrevSpec, DiagID, Policy);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003549 isStorageClass = true;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003550 break;
Richard Smithe301ba22015-11-11 02:02:15 +00003551 case tok::kw___auto_type:
3552 Diag(Tok, diag::ext_auto_type);
3553 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto_type, Loc, PrevSpec,
3554 DiagID, Policy);
3555 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003556 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003557 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003558 PrevSpec, DiagID, Policy);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003559 isStorageClass = true;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003560 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00003561 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00003562 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003563 PrevSpec, DiagID, Policy);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003564 isStorageClass = true;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00003565 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003566 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00003567 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
3568 PrevSpec, DiagID);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003569 isStorageClass = true;
Richard Smithb4a9e862013-04-12 22:46:28 +00003570 break;
3571 case tok::kw_thread_local:
3572 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
3573 PrevSpec, DiagID);
Sven van Haastregtc4100832018-04-24 14:47:29 +00003574 isStorageClass = true;
Richard Smithb4a9e862013-04-12 22:46:28 +00003575 break;
3576 case tok::kw__Thread_local:
3577 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
3578 Loc, PrevSpec, DiagID);
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003579 isStorageClass = true;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00003580 break;
Mike Stump11289f42009-09-09 15:08:12 +00003581
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003582 // function-specifier
3583 case tok::kw_inline:
Serge Pavlov750db652013-11-13 06:57:53 +00003584 isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003585 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00003586 case tok::kw_virtual:
Anastasia Stulova46b55fa2019-07-18 10:02:35 +00003587 // C++ for OpenCL does not allow virtual function qualifier, to avoid
3588 // function pointers restricted in OpenCL v2.0 s6.9.a.
Sven van Haastregt49ffffb2018-04-23 11:23:47 +00003589 if (getLangOpts().OpenCLCPlusPlus) {
3590 DiagID = diag::err_openclcxx_virtual_function;
3591 PrevSpec = Tok.getIdentifierInfo()->getNameStart();
3592 isInvalid = true;
3593 }
3594 else {
3595 isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
3596 }
Douglas Gregor61956c42008-10-31 09:07:45 +00003597 break;
Richard Smith76b90272019-05-09 03:59:21 +00003598 case tok::kw_explicit: {
3599 SourceLocation ExplicitLoc = Loc;
3600 SourceLocation CloseParenLoc;
3601 ExplicitSpecifier ExplicitSpec(nullptr, ExplicitSpecKind::ResolvedTrue);
Richard Smithba24f352019-05-09 19:45:46 +00003602 ConsumedEnd = ExplicitLoc;
Richard Smith76b90272019-05-09 03:59:21 +00003603 ConsumeToken(); // kw_explicit
3604 if (Tok.is(tok::l_paren)) {
3605 if (getLangOpts().CPlusPlus2a) {
3606 ExprResult ExplicitExpr(static_cast<Expr *>(nullptr));
3607 BalancedDelimiterTracker Tracker(*this, tok::l_paren);
3608 Tracker.consumeOpen();
3609 ExplicitExpr = ParseConstantExpression();
Richard Smithba24f352019-05-09 19:45:46 +00003610 ConsumedEnd = Tok.getLocation();
Richard Smith76b90272019-05-09 03:59:21 +00003611 if (ExplicitExpr.isUsable()) {
3612 CloseParenLoc = Tok.getLocation();
3613 Tracker.consumeClose();
3614 ExplicitSpec =
3615 Actions.ActOnExplicitBoolSpecifier(ExplicitExpr.get());
3616 } else
3617 Tracker.skipToEnd();
3618 } else
3619 Diag(Tok.getLocation(), diag::warn_cxx2a_compat_explicit_bool);
3620 }
3621 isInvalid = DS.setFunctionSpecExplicit(ExplicitLoc, PrevSpec, DiagID,
3622 ExplicitSpec, CloseParenLoc);
Douglas Gregor61956c42008-10-31 09:07:45 +00003623 break;
Richard Smith76b90272019-05-09 03:59:21 +00003624 }
Richard Smith0015f092013-01-17 22:16:11 +00003625 case tok::kw__Noreturn:
3626 if (!getLangOpts().C11)
3627 Diag(Loc, diag::ext_c11_noreturn);
Serge Pavlov750db652013-11-13 06:57:53 +00003628 isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
Richard Smith0015f092013-01-17 22:16:11 +00003629 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003630
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003631 // alignment-specifier
3632 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003633 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00003634 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003635 ParseAlignmentSpecifier(DS.getAttributes());
3636 continue;
3637
Anders Carlssoncd8db412009-05-06 04:46:28 +00003638 // friend
3639 case tok::kw_friend:
Faisal Vali7db85c52017-12-31 00:06:40 +00003640 if (DSContext == DeclSpecContext::DSC_class)
John McCall07e91c02009-08-06 02:15:43 +00003641 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
3642 else {
3643 PrevSpec = ""; // not actually used by the diagnostic
3644 DiagID = diag::err_friend_invalid_in_context;
3645 isInvalid = true;
3646 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00003647 break;
Mike Stump11289f42009-09-09 15:08:12 +00003648
Douglas Gregor26701a42011-09-09 02:06:17 +00003649 // Modules
3650 case tok::kw___module_private__:
3651 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
3652 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003653
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00003654 // constexpr
3655 case tok::kw_constexpr:
Gauthier Harnisch796ed032019-06-14 08:56:20 +00003656 isInvalid = DS.SetConstexprSpec(CSK_constexpr, Loc, PrevSpec, DiagID);
3657 break;
3658
3659 // consteval
3660 case tok::kw_consteval:
3661 isInvalid = DS.SetConstexprSpec(CSK_consteval, Loc, PrevSpec, DiagID);
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00003662 break;
3663
Chris Lattnere387d9e2009-01-21 19:48:37 +00003664 // type-specifier
3665 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00003666 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003667 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003668 break;
3669 case tok::kw_long:
3670 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00003671 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003672 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003673 else
John McCall49bfce42009-08-03 20:12:06 +00003674 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003675 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003676 break;
Francois Pichet84133e42011-04-28 01:59:37 +00003677 case tok::kw___int64:
3678 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003679 DiagID, Policy);
Francois Pichet84133e42011-04-28 01:59:37 +00003680 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003681 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00003682 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
3683 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003684 break;
3685 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00003686 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
3687 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003688 break;
3689 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00003690 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
3691 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003692 break;
3693 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00003694 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
3695 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003696 break;
3697 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00003698 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003699 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003700 break;
3701 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00003702 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003703 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003704 break;
3705 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00003706 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003707 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003708 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00003709 case tok::kw___int128:
3710 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003711 DiagID, Policy);
Richard Smithf016bbc2012-04-04 06:24:32 +00003712 break;
3713 case tok::kw_half:
3714 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003715 DiagID, Policy);
Richard Smithf016bbc2012-04-04 06:24:32 +00003716 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003717 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00003718 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003719 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003720 break;
3721 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00003722 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003723 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003724 break;
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00003725 case tok::kw__Float16:
3726 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float16, Loc, PrevSpec,
3727 DiagID, Policy);
3728 break;
Leonard Chanf921d852018-06-04 16:07:52 +00003729 case tok::kw__Accum:
3730 if (!getLangOpts().FixedPoint) {
Leonard Chanab80f3c2018-06-14 14:53:51 +00003731 SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid);
Leonard Chanf921d852018-06-04 16:07:52 +00003732 } else {
3733 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_accum, Loc, PrevSpec,
3734 DiagID, Policy);
3735 }
3736 break;
Leonard Chanab80f3c2018-06-14 14:53:51 +00003737 case tok::kw__Fract:
3738 if (!getLangOpts().FixedPoint) {
3739 SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid);
3740 } else {
3741 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_fract, Loc, PrevSpec,
3742 DiagID, Policy);
3743 }
3744 break;
3745 case tok::kw__Sat:
3746 if (!getLangOpts().FixedPoint) {
3747 SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid);
3748 } else {
3749 isInvalid = DS.SetTypeSpecSat(Loc, PrevSpec, DiagID);
3750 }
3751 break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00003752 case tok::kw___float128:
3753 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float128, Loc, PrevSpec,
3754 DiagID, Policy);
3755 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003756 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00003757 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003758 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003759 break;
Richard Smith3a8244d2018-05-01 05:02:45 +00003760 case tok::kw_char8_t:
3761 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char8, Loc, PrevSpec,
3762 DiagID, Policy);
3763 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003764 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00003765 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003766 DiagID, Policy);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003767 break;
3768 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00003769 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003770 DiagID, Policy);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003771 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003772 case tok::kw_bool:
3773 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003774 if (Tok.is(tok::kw_bool) &&
3775 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
3776 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
3777 PrevSpec = ""; // Not used by the diagnostic.
3778 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003779 // For better error recovery.
3780 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003781 isInvalid = true;
3782 } else {
3783 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003784 DiagID, Policy);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003785 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003786 break;
3787 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00003788 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003789 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003790 break;
3791 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00003792 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003793 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003794 break;
3795 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00003796 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003797 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003798 break;
John Thompson22334602010-02-05 00:12:22 +00003799 case tok::kw___vector:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003800 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
John Thompson22334602010-02-05 00:12:22 +00003801 break;
3802 case tok::kw___pixel:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003803 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
John Thompson22334602010-02-05 00:12:22 +00003804 break;
Bill Seurercf2c96b2015-01-12 19:35:51 +00003805 case tok::kw___bool:
3806 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy);
3807 break;
Xiuli Pan9c14e282016-01-09 12:53:17 +00003808 case tok::kw_pipe:
Sven van Haastregte518bb42019-05-22 13:12:20 +00003809 if (!getLangOpts().OpenCL || (getLangOpts().OpenCLVersion < 200 &&
3810 !getLangOpts().OpenCLCPlusPlus)) {
Xiuli Pan9c14e282016-01-09 12:53:17 +00003811 // OpenCL 2.0 defined this keyword. OpenCL 1.2 and earlier should
3812 // support the "pipe" word as identifier.
3813 Tok.getIdentifierInfo()->revertTokenIDToIdentifier();
3814 goto DoneWithDeclSpec;
3815 }
3816 isInvalid = DS.SetTypePipe(true, Loc, PrevSpec, DiagID, Policy);
3817 break;
Alexey Bader954ba212016-04-08 13:40:33 +00003818#define GENERIC_IMAGE_TYPE(ImgType, Id) \
3819 case tok::kw_##ImgType##_t: \
3820 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_##ImgType##_t, Loc, PrevSpec, \
3821 DiagID, Policy); \
3822 break;
Alexey Baderb62f1442016-04-13 08:33:41 +00003823#include "clang/Basic/OpenCLImageTypes.def"
John McCall39439732011-04-09 22:50:59 +00003824 case tok::kw___unknown_anytype:
Faisal Vali090da2d2018-01-01 18:23:28 +00003825 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003826 PrevSpec, DiagID, Policy);
John McCall39439732011-04-09 22:50:59 +00003827 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003828
3829 // class-specifier:
3830 case tok::kw_class:
3831 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003832 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003833 case tok::kw_union: {
3834 tok::TokenKind Kind = Tok.getKind();
3835 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00003836
3837 // These are attributes following class specifiers.
3838 // To produce better diagnostic, we parse them when
3839 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00003840 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00003841 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00003842 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003843
3844 // If there are attributes following class specifier,
3845 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00003846 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00003847 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00003848 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003849 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003850 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003851 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003852
3853 // enum-specifier:
3854 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003855 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003856 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003857 continue;
Faisal Valia534f072018-04-26 00:42:40 +00003858
Chris Lattnere387d9e2009-01-21 19:48:37 +00003859 // cv-qualifier:
3860 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003861 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003862 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003863 break;
3864 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003865 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003866 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003867 break;
3868 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003869 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003870 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003871 break;
3872
Douglas Gregor333489b2009-03-27 23:10:48 +00003873 // C++ typename-specifier:
3874 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003875 if (TryAnnotateTypeOrScopeToken()) {
3876 DS.SetTypeSpecError();
3877 goto DoneWithDeclSpec;
3878 }
3879 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003880 continue;
3881 break;
3882
Chris Lattnere387d9e2009-01-21 19:48:37 +00003883 // GNU typeof support.
3884 case tok::kw_typeof:
3885 ParseTypeofSpecifier(DS);
3886 continue;
3887
David Blaikie15a430a2011-12-04 05:04:18 +00003888 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003889 ParseDecltypeSpecifier(DS);
3890 continue;
3891
David Majnemer15b311c2016-06-14 03:20:28 +00003892 case tok::annot_pragma_pack:
3893 HandlePragmaPack();
3894 continue;
3895
3896 case tok::annot_pragma_ms_pragma:
3897 HandlePragmaMSPragma();
3898 continue;
3899
3900 case tok::annot_pragma_ms_vtordisp:
3901 HandlePragmaMSVtorDisp();
3902 continue;
3903
3904 case tok::annot_pragma_ms_pointers_to_members:
3905 HandlePragmaMSPointersToMembers();
3906 continue;
3907
Alexis Hunt4a257072011-05-19 05:37:45 +00003908 case tok::kw___underlying_type:
3909 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003910 continue;
3911
3912 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003913 // C11 6.7.2.4/4:
3914 // If the _Atomic keyword is immediately followed by a left parenthesis,
3915 // it is interpreted as a type specifier (with a type name), not as a
3916 // type qualifier.
3917 if (NextToken().is(tok::l_paren)) {
3918 ParseAtomicSpecifier(DS);
3919 continue;
3920 }
3921 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3922 getLangOpts());
3923 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003924
Sven van Haastregt2ca6ba12018-05-09 13:16:17 +00003925 // OpenCL address space qualifiers:
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003926 case tok::kw___generic:
3927 // generic address space is introduced only in OpenCL v2.0
3928 // see OpenCL C Spec v2.0 s6.5.5
Sven van Haastregt2ca6ba12018-05-09 13:16:17 +00003929 if (Actions.getLangOpts().OpenCLVersion < 200 &&
3930 !Actions.getLangOpts().OpenCLCPlusPlus) {
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00003931 DiagID = diag::err_opencl_unknown_type_specifier;
3932 PrevSpec = Tok.getIdentifierInfo()->getNameStart();
3933 isInvalid = true;
3934 break;
3935 };
Galina Kistanova77674252017-06-01 21:15:34 +00003936 LLVM_FALLTHROUGH;
Anastasia Stulova948e37c2019-03-25 11:54:02 +00003937 case tok::kw_private:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003938 case tok::kw___private:
3939 case tok::kw___global:
3940 case tok::kw___local:
3941 case tok::kw___constant:
Anastasia Stulova2c4730d2019-02-15 12:07:57 +00003942 // OpenCL access qualifiers:
3943 case tok::kw___read_only:
3944 case tok::kw___write_only:
3945 case tok::kw___read_write:
Aaron Ballman05d76ea2014-01-14 01:29:54 +00003946 ParseOpenCLQualifiers(DS.getAttributes());
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003947 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003948
Steve Naroffcfdf6162008-06-05 00:02:44 +00003949 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003950 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003951 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3952 // but we support it.
Erik Pilkingtonfa983902018-10-30 20:31:30 +00003953 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC)
Chris Lattner0974b232008-07-26 00:20:22 +00003954 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003955
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003956 SourceLocation StartLoc = Tok.getLocation();
3957 SourceLocation EndLoc;
3958 TypeResult Type = parseObjCProtocolQualifierType(EndLoc);
3959 if (Type.isUsable()) {
3960 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, StartLoc,
3961 PrevSpec, DiagID, Type.get(),
3962 Actions.getASTContext().getPrintingPolicy()))
3963 Diag(StartLoc, DiagID) << PrevSpec;
Fangrui Song6907ce22018-07-30 19:24:48 +00003964
Douglas Gregor9bda6cf2015-07-07 03:58:14 +00003965 DS.SetRangeEnd(EndLoc);
3966 } else {
3967 DS.SetTypeSpecError();
3968 }
Chad Rosierc1183952012-06-26 22:30:43 +00003969
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003970 // Need to support trailing type qualifiers (e.g. "id<p> const").
3971 // If a type specifier follows, it will be diagnosed elsewhere.
3972 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003973 }
Richard Smith76b90272019-05-09 03:59:21 +00003974
Richard Smithba24f352019-05-09 19:45:46 +00003975 DS.SetRangeEnd(ConsumedEnd.isValid() ? ConsumedEnd : Tok.getLocation());
Richard Smith76b90272019-05-09 03:59:21 +00003976
John McCall49bfce42009-08-03 20:12:06 +00003977 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003978 if (isInvalid) {
3979 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003980 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003981
Nick Desaulniers150ca532018-10-03 23:09:29 +00003982 if (DiagID == diag::ext_duplicate_declspec ||
Richard Smith76b90272019-05-09 03:59:21 +00003983 DiagID == diag::ext_warn_duplicate_declspec ||
3984 DiagID == diag::err_duplicate_declspec)
3985 Diag(Loc, DiagID) << PrevSpec
3986 << FixItHint::CreateRemoval(
3987 SourceRange(Loc, DS.getEndLoc()));
Anastasia Stulova43ab9a02016-05-12 16:28:25 +00003988 else if (DiagID == diag::err_opencl_unknown_type_specifier) {
Richard Smith76b90272019-05-09 03:59:21 +00003989 Diag(Loc, DiagID) << getLangOpts().OpenCLCPlusPlus
3990 << getLangOpts().getOpenCLVersionTuple().getAsString()
3991 << PrevSpec << isStorageClass;
Anastasia Stulova43ab9a02016-05-12 16:28:25 +00003992 } else
Richard Smith76b90272019-05-09 03:59:21 +00003993 Diag(Loc, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003994 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003995
Richard Smithba24f352019-05-09 19:45:46 +00003996 if (DiagID != diag::err_bool_redeclaration && ConsumedEnd.isInvalid())
Volodymyr Sapsai9f7b5cc2018-04-10 18:29:47 +00003997 // After an error the next token can be an annotation token.
3998 ConsumeAnyToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003999
4000 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004001 }
4002}
Douglas Gregoreb31f392008-12-01 23:54:00 +00004003
Chris Lattner70ae4912007-10-29 04:42:53 +00004004/// ParseStructDeclaration - Parse a struct declaration without the terminating
4005/// semicolon.
4006///
Nico Weber98dd0852019-03-14 14:18:56 +00004007/// Note that a struct declaration refers to a declaration in a struct,
4008/// not to the declaration of a struct.
4009///
Chris Lattner90a26b02007-01-23 04:38:16 +00004010/// struct-declaration:
Aaron Ballman606093a2017-10-15 15:01:42 +00004011/// [C2x] attributes-specifier-seq[opt]
4012/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00004013/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00004014/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00004015/// struct-declarator-list:
4016/// struct-declarator
4017/// struct-declarator-list ',' struct-declarator
4018/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
4019/// struct-declarator:
4020/// declarator
4021/// [GNU] declarator attributes[opt]
4022/// declarator[opt] ':' constant-expression
4023/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
4024///
Benjamin Kramera39beb92014-09-03 11:06:10 +00004025void Parser::ParseStructDeclaration(
4026 ParsingDeclSpec &DS,
4027 llvm::function_ref<void(ParsingFieldDeclarator &)> FieldsCallback) {
Chad Rosierc1183952012-06-26 22:30:43 +00004028
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00004029 if (Tok.is(tok::kw___extension__)) {
4030 // __extension__ silences extension warnings in the subexpression.
4031 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00004032 ConsumeToken();
Benjamin Kramera39beb92014-09-03 11:06:10 +00004033 return ParseStructDeclaration(DS, FieldsCallback);
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00004034 }
Mike Stump11289f42009-09-09 15:08:12 +00004035
Aaron Ballman606093a2017-10-15 15:01:42 +00004036 // Parse leading attributes.
4037 ParsedAttributesWithRange Attrs(AttrFactory);
4038 MaybeParseCXX11Attributes(Attrs);
4039 DS.takeAttributesFrom(Attrs);
4040
Steve Naroff97170802007-08-20 22:28:22 +00004041 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00004042 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00004043
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00004044 // If there are no declarators, this is a free-standing declaration
4045 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00004046 if (Tok.is(tok::semi)) {
Nico Weber7b837f52016-01-28 19:25:00 +00004047 RecordDecl *AnonRecord = nullptr;
Eli Friedman89b1f2c2012-08-08 23:04:35 +00004048 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Nico Weber7b837f52016-01-28 19:25:00 +00004049 DS, AnonRecord);
4050 assert(!AnonRecord && "Did not expect anonymous struct or union here");
Eli Friedman89b1f2c2012-08-08 23:04:35 +00004051 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00004052 return;
4053 }
4054
4055 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00004056 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00004057 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00004058 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00004059 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00004060 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00004061
Bill Wendling44426052012-12-20 19:22:21 +00004062 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00004063 if (!FirstDeclarator)
4064 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00004065
Steve Naroff97170802007-08-20 22:28:22 +00004066 /// struct-declarator: declarator
4067 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00004068 if (Tok.isNot(tok::colon)) {
4069 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
4070 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00004071 ParseDeclarator(DeclaratorInfo.D);
Richard Smith3d1a94c2014-08-12 00:22:39 +00004072 } else
4073 DeclaratorInfo.D.SetIdentifier(nullptr, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00004074
Alp Toker8fbec672013-12-17 23:29:36 +00004075 if (TryConsumeToken(tok::colon)) {
John McCalldadc5752010-08-24 06:29:42 +00004076 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00004077 if (Res.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00004078 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner32295d32008-04-10 06:15:14 +00004079 else
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004080 DeclaratorInfo.BitfieldSize = Res.get();
Steve Naroff97170802007-08-20 22:28:22 +00004081 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004082
Steve Naroff97170802007-08-20 22:28:22 +00004083 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00004084 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004085
John McCallcfefb6d2009-11-03 02:38:08 +00004086 // We're done with this declarator; invoke the callback.
Benjamin Kramera39beb92014-09-03 11:06:10 +00004087 FieldsCallback(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00004088
Steve Naroff97170802007-08-20 22:28:22 +00004089 // If we don't have a comma, it is either the end of the list (a ';')
4090 // or an error, bail out.
Alp Toker8fbec672013-12-17 23:29:36 +00004091 if (!TryConsumeToken(tok::comma, CommaLoc))
Chris Lattner70ae4912007-10-29 04:42:53 +00004092 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004093
John McCallcfefb6d2009-11-03 02:38:08 +00004094 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00004095 }
Steve Naroff97170802007-08-20 22:28:22 +00004096}
4097
4098/// ParseStructUnionBody
4099/// struct-contents:
4100/// struct-declaration-list
4101/// [EXT] empty
4102/// [GNU] "struct-declaration-list" without terminatoring ';'
4103/// struct-declaration-list:
4104/// struct-declaration
4105/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00004106/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00004107///
Chris Lattner1300fb92007-01-23 23:42:53 +00004108void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Serge Pavlov0c503192019-08-01 11:46:28 +00004109 DeclSpec::TST TagType, Decl *TagDecl) {
Jordan Rose1e879d82018-03-23 00:07:18 +00004110 PrettyDeclStackTraceEntry CrashInfo(Actions.Context, TagDecl, RecordLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00004111 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00004112 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00004113
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004114 BalancedDelimiterTracker T(*this, tok::l_brace);
4115 if (T.consumeOpen())
4116 return;
Mike Stump11289f42009-09-09 15:08:12 +00004117
Douglas Gregor658b9552009-01-09 22:42:13 +00004118 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004119 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00004120
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004121 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00004122
Chris Lattner7b9ace62007-01-23 20:11:08 +00004123 // While we still have something to read, read the declarations in the struct.
Richard Smith752ada82015-11-17 23:32:01 +00004124 while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
4125 Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00004126 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00004127
Chris Lattner736ed5d2007-06-09 05:59:07 +00004128 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00004129 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00004130 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00004131 continue;
4132 }
Chris Lattnera12405b2008-04-10 06:46:29 +00004133
Andy Gibbsc804e082013-04-03 09:46:04 +00004134 // Parse _Static_assert declaration.
4135 if (Tok.is(tok::kw__Static_assert)) {
4136 SourceLocation DeclEnd;
4137 ParseStaticAssertDeclaration(DeclEnd);
4138 continue;
4139 }
4140
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00004141 if (Tok.is(tok::annot_pragma_pack)) {
4142 HandlePragmaPack();
4143 continue;
4144 }
4145
4146 if (Tok.is(tok::annot_pragma_align)) {
4147 HandlePragmaAlign();
4148 continue;
4149 }
4150
Alexey Bataev4652e4b2015-08-12 07:10:54 +00004151 if (Tok.is(tok::annot_pragma_openmp)) {
Alexey Bataev587e1de2016-03-30 10:43:55 +00004152 // Result can be ignored, because it must be always empty.
4153 AccessSpecifier AS = AS_none;
4154 ParsedAttributesWithRange Attrs(AttrFactory);
4155 (void)ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs);
Alexey Bataev4652e4b2015-08-12 07:10:54 +00004156 continue;
4157 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00004158
Serge Pavlov037861b2019-08-04 10:08:51 +00004159 if (tok::isPragmaAnnotation(Tok.getKind())) {
4160 Diag(Tok.getLocation(), diag::err_pragma_misplaced_in_decl)
4161 << DeclSpec::getSpecifierName(
4162 TagType, Actions.getASTContext().getPrintingPolicy());
4163 ConsumeAnnotationToken();
4164 continue;
4165 }
4166
John McCallcfefb6d2009-11-03 02:38:08 +00004167 if (!Tok.is(tok::at)) {
Benjamin Kramera39beb92014-09-03 11:06:10 +00004168 auto CFieldCallback = [&](ParsingFieldDeclarator &FD) {
4169 // Install the declarator into the current TagDecl.
4170 Decl *Field =
4171 Actions.ActOnField(getCurScope(), TagDecl,
4172 FD.D.getDeclSpec().getSourceRange().getBegin(),
4173 FD.D, FD.BitfieldSize);
4174 FieldDecls.push_back(Field);
4175 FD.complete(Field);
4176 };
John McCallcfefb6d2009-11-03 02:38:08 +00004177
Eli Friedman89b1f2c2012-08-08 23:04:35 +00004178 // Parse all the comma separated declarators.
4179 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00004180 ParseStructDeclaration(DS, CFieldCallback);
Chris Lattner535b8302008-06-21 19:39:06 +00004181 } else { // Handle @defs
4182 ConsumeToken();
4183 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
4184 Diag(Tok, diag::err_unexpected_at);
Alexey Bataevee6507d2013-11-18 08:17:37 +00004185 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00004186 continue;
4187 }
4188 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00004189 ExpectAndConsume(tok::l_paren);
Chris Lattner535b8302008-06-21 19:39:06 +00004190 if (!Tok.is(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00004191 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00004192 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00004193 continue;
4194 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004195 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00004196 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00004197 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00004198 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
4199 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00004200 ExpectAndConsume(tok::r_paren);
Mike Stump11289f42009-09-09 15:08:12 +00004201 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00004202
Alp Tokera3ebe6e2013-12-17 14:12:37 +00004203 if (TryConsumeToken(tok::semi))
4204 continue;
4205
4206 if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00004207 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00004208 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00004209 }
Alp Tokera3ebe6e2013-12-17 14:12:37 +00004210
4211 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
4212 // Skip to end of block or statement to avoid ext-warning on extra ';'.
4213 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
4214 // If we stopped at a ';', eat it.
4215 TryConsumeToken(tok::semi);
Chris Lattner90a26b02007-01-23 04:38:16 +00004216 }
Mike Stump11289f42009-09-09 15:08:12 +00004217
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004218 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00004219
John McCall084e83d2011-03-24 11:26:52 +00004220 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00004221 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00004222 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00004223
Erich Keanec480f302018-07-12 21:09:05 +00004224 Actions.ActOnFields(getCurScope(), RecordLoc, TagDecl, FieldDecls,
4225 T.getOpenLocation(), T.getCloseLocation(), attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00004226 StructScope.Exit();
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00004227 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, T.getRange());
Chris Lattner90a26b02007-01-23 04:38:16 +00004228}
4229
Chris Lattner3b561a32006-08-13 00:12:11 +00004230/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00004231/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00004232/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004233///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00004234/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
4235/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00004236/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
4237/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00004238/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00004239/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004240///
Richard Smith7d137e32012-03-23 03:33:32 +00004241/// [C++11] enum-head '{' enumerator-list[opt] '}'
4242/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00004243///
Richard Smith7d137e32012-03-23 03:33:32 +00004244/// enum-head: [C++11]
4245/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
4246/// enum-key attribute-specifier-seq[opt] nested-name-specifier
4247/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00004248///
Richard Smith7d137e32012-03-23 03:33:32 +00004249/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00004250/// 'enum'
4251/// 'enum' 'class'
4252/// 'enum' 'struct'
4253///
Richard Smith7d137e32012-03-23 03:33:32 +00004254/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00004255/// ':' type-specifier-seq
4256///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004257/// [C++] elaborated-type-specifier:
4258/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
4259///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00004260void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00004261 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00004262 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00004263 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00004264 if (Tok.is(tok::code_completion)) {
4265 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00004266 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004267 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00004268 }
John McCallcb432fa2011-07-06 05:58:41 +00004269
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00004270 // If attributes exist after tag, parse them.
4271 ParsedAttributesWithRange attrs(AttrFactory);
4272 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00004273 MaybeParseCXX11Attributes(attrs);
Aaron Ballman068aa512015-05-20 20:58:33 +00004274 MaybeParseMicrosoftDeclSpecs(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00004275
Richard Smith0f8ee222012-01-10 01:33:14 +00004276 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00004277 bool IsScopedUsingClassTag = false;
4278
John McCallbeae29a2012-06-23 22:30:04 +00004279 // In C++11, recognize 'enum class' and 'enum struct'.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00004280 if (Tok.isOneOf(tok::kw_class, tok::kw_struct)) {
Richard Trieud0d87b52013-04-23 02:47:36 +00004281 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
4282 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00004283 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00004284 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00004285
Bill Wendling44426052012-12-20 19:22:21 +00004286 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00004287 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00004288 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00004289
4290 // They are allowed afterwards, though.
4291 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00004292 MaybeParseCXX11Attributes(attrs);
Aaron Ballman068aa512015-05-20 20:58:33 +00004293 MaybeParseMicrosoftDeclSpecs(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00004294 }
Richard Smith7d137e32012-03-23 03:33:32 +00004295
John McCall6347b682012-05-07 06:16:58 +00004296 // C++11 [temp.explicit]p12:
4297 // The usual access controls do not apply to names used to specify
4298 // explicit instantiations.
4299 // We extend this to also cover explicit specializations. Note that
4300 // we don't suppress if this turns out to be an elaborated type
4301 // specifier.
4302 bool shouldDelayDiagsInTag =
4303 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
4304 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
4305 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00004306
Richard Smithbfdb1082012-03-12 08:56:40 +00004307 // Enum definitions should not be parsed in a trailing-return-type.
Faisal Vali7db85c52017-12-31 00:06:40 +00004308 bool AllowDeclaration = DSC != DeclSpecContext::DSC_trailing;
Richard Smithbfdb1082012-03-12 08:56:40 +00004309
Abramo Bagnarad7548482010-05-19 21:37:53 +00004310 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004311 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00004312 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
4313 // if a fixed underlying type is allowed.
Erik Pilkington6f11db12018-09-28 20:24:58 +00004314 ColonProtectionRAIIObject X(*this, AllowDeclaration);
Chad Rosierc1183952012-06-26 22:30:43 +00004315
Nico Webercfaa4cd2015-02-15 07:26:13 +00004316 CXXScopeSpec Spec;
David Blaikieefdccaa2016-01-15 23:43:34 +00004317 if (ParseOptionalCXXScopeSpecifier(Spec, nullptr,
Richard Smith1d4b2e12013-04-01 21:43:41 +00004318 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00004319 return;
4320
Nico Webercfaa4cd2015-02-15 07:26:13 +00004321 if (Spec.isSet() && Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00004322 Diag(Tok, diag::err_expected) << tok::identifier;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004323 if (Tok.isNot(tok::l_brace)) {
4324 // Has no name and is not a definition.
4325 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00004326 SkipUntil(tok::comma, StopAtSemi);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004327 return;
4328 }
4329 }
Nico Webercfaa4cd2015-02-15 07:26:13 +00004330
4331 SS = Spec;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004332 }
Mike Stump11289f42009-09-09 15:08:12 +00004333
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00004334 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00004335 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Erik Pilkington6f11db12018-09-28 20:24:58 +00004336 !(AllowDeclaration && Tok.is(tok::colon))) {
Alp Tokerec543272013-12-24 09:48:30 +00004337 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
Mike Stump11289f42009-09-09 15:08:12 +00004338
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00004339 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00004340 SkipUntil(tok::comma, StopAtSemi);
Chris Lattner3b561a32006-08-13 00:12:11 +00004341 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00004342 }
Mike Stump11289f42009-09-09 15:08:12 +00004343
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00004344 // If an identifier is present, consume and remember it.
Craig Topper161e4db2014-05-21 06:02:52 +00004345 IdentifierInfo *Name = nullptr;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00004346 SourceLocation NameLoc;
4347 if (Tok.is(tok::identifier)) {
4348 Name = Tok.getIdentifierInfo();
4349 NameLoc = ConsumeToken();
4350 }
Mike Stump11289f42009-09-09 15:08:12 +00004351
Richard Smith0f8ee222012-01-10 01:33:14 +00004352 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00004353 // C++0x 7.2p2: The optional identifier shall not be omitted in the
4354 // declaration of a scoped enumeration.
4355 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00004356 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00004357 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00004358 }
4359
John McCall6347b682012-05-07 06:16:58 +00004360 // Okay, end the suppression area. We'll decide whether to emit the
4361 // diagnostics in a second.
4362 if (shouldDelayDiagsInTag)
4363 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00004364
Douglas Gregor0bf31402010-10-08 23:50:27 +00004365 TypeResult BaseType;
4366
Douglas Gregord1f69f62010-12-01 17:42:47 +00004367 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00004368 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Erik Pilkington6f11db12018-09-28 20:24:58 +00004369 if (AllowDeclaration && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00004370 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00004371 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00004372 // If we're in class scope, this can either be an enum declaration with
4373 // an underlying type, or a declaration of a bitfield member. We try to
4374 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00004375 // (integer literal, sizeof); if it's still ambiguous, we then consider
4376 // anything that's a simple-type-specifier followed by '(' as an
4377 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00004378 // underlying types anyway.
Faisal Valid143a0c2017-04-01 21:30:49 +00004379 EnterExpressionEvaluationContext Unevaluated(
4380 Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00004381 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00004382 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00004383 // bit-field. This is the common case.
Richard Smithee390432014-05-16 01:56:53 +00004384 if (TPR == TPResult::True)
Douglas Gregord1f69f62010-12-01 17:42:47 +00004385 PossibleBitfield = true;
4386 // If the next token starts a type-specifier-seq, it may be either a
4387 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00004388 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00004389 // fixed underlying type.
Richard Smithee390432014-05-16 01:56:53 +00004390 else if (TPR == TPResult::False &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00004391 GetLookAheadToken(2).getKind() == tok::semi) {
4392 // Consume the ':'.
4393 ConsumeToken();
4394 } else {
4395 // We have the start of a type-specifier-seq, so we have to perform
4396 // tentative parsing to determine whether we have an expression or a
4397 // type.
4398 TentativeParsingAction TPA(*this);
4399
4400 // Consume the ':'.
4401 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00004402
4403 // If we see a type specifier followed by an open-brace, we have an
4404 // ambiguity between an underlying type and a C++11 braced
4405 // function-style cast. Resolve this by always treating it as an
4406 // underlying type.
4407 // FIXME: The standard is not entirely clear on how to disambiguate in
4408 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004409 if ((getLangOpts().CPlusPlus &&
Richard Smithee390432014-05-16 01:56:53 +00004410 isCXXDeclarationSpecifier(TPResult::True) != TPResult::True) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00004411 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00004412 // We'll parse this as a bitfield later.
4413 PossibleBitfield = true;
4414 TPA.Revert();
4415 } else {
4416 // We have a type-specifier-seq.
4417 TPA.Commit();
4418 }
4419 }
4420 } else {
4421 // Consume the ':'.
4422 ConsumeToken();
4423 }
4424
4425 if (!PossibleBitfield) {
4426 SourceRange Range;
4427 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00004428
Erik Pilkingtonfa983902018-10-30 20:31:30 +00004429 if (!getLangOpts().ObjC) {
Erik Pilkington6f11db12018-09-28 20:24:58 +00004430 if (getLangOpts().CPlusPlus11)
4431 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
4432 else if (getLangOpts().CPlusPlus)
4433 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type);
4434 else if (getLangOpts().MicrosoftExt)
4435 Diag(StartLoc, diag::ext_ms_c_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00004436 else
Erik Pilkington6f11db12018-09-28 20:24:58 +00004437 Diag(StartLoc, diag::ext_clang_c_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00004438 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00004439 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00004440 }
4441
Richard Smith0f8ee222012-01-10 01:33:14 +00004442 // There are four options here. If we have 'friend enum foo;' then this is a
4443 // friend declaration, and cannot have an accompanying definition. If we have
4444 // 'enum foo;', then this is a forward declaration. If we have
4445 // 'enum foo {...' then this is a definition. Otherwise we have something
4446 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00004447 //
4448 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
4449 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
4450 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
4451 //
John McCallfaf5fb42010-08-26 23:41:50 +00004452 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00004453 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00004454 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00004455 } else if (Tok.is(tok::l_brace)) {
4456 if (DS.isFriendSpecified()) {
4457 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
4458 << SourceRange(DS.getFriendSpecLoc());
4459 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00004460 SkipUntil(tok::r_brace, StopAtSemi);
John McCall6347b682012-05-07 06:16:58 +00004461 TUK = Sema::TUK_Friend;
4462 } else {
4463 TUK = Sema::TUK_Definition;
4464 }
Richard Smith649c7b062014-01-08 00:56:48 +00004465 } else if (!isTypeSpecifier(DSC) &&
Richard Smith369b9f92012-06-25 21:37:02 +00004466 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00004467 (Tok.isAtStartOfLine() &&
4468 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00004469 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
4470 if (Tok.isNot(tok::semi)) {
4471 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00004472 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Ilya Biryukov929af672019-05-17 09:32:05 +00004473 PP.EnterToken(Tok, /*IsReinject=*/true);
Richard Smith369b9f92012-06-25 21:37:02 +00004474 Tok.setKind(tok::semi);
4475 }
John McCall6347b682012-05-07 06:16:58 +00004476 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00004477 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00004478 }
4479
4480 // If this is an elaborated type specifier, and we delayed
4481 // diagnostics before, just merge them into the current pool.
4482 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
4483 diagsFromTag.redelay();
4484 }
Richard Smith7d137e32012-03-23 03:33:32 +00004485
4486 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00004487 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00004488 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004489 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00004490 // Skip the rest of this declarator, up until the comma or semicolon.
4491 Diag(Tok, diag::err_enum_template);
Alexey Bataevee6507d2013-11-18 08:17:37 +00004492 SkipUntil(tok::comma, StopAtSemi);
Richard Smith7d137e32012-03-23 03:33:32 +00004493 return;
4494 }
4495
4496 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
4497 // Enumerations can't be explicitly instantiated.
4498 DS.SetTypeSpecError();
4499 Diag(StartLoc, diag::err_explicit_instantiation_enum);
4500 return;
4501 }
4502
4503 assert(TemplateInfo.TemplateParams && "no template parameters");
4504 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
4505 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00004506 }
Chad Rosierc1183952012-06-26 22:30:43 +00004507
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00004508 if (TUK == Sema::TUK_Reference)
4509 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00004510
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00004511 if (!Name && TUK != Sema::TUK_Definition) {
4512 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00004513
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00004514 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00004515 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00004516 return;
4517 }
Richard Smith7d137e32012-03-23 03:33:32 +00004518
Nico Weber32a0fc72016-09-03 03:01:32 +00004519 stripTypeAttributesOffDeclSpec(attrs, DS, TUK);
David Majnemer936b4112015-04-19 07:53:29 +00004520
Richard Smithd9ba2242015-05-07 03:54:19 +00004521 Sema::SkipBodyInfo SkipBody;
4522 if (!Name && TUK == Sema::TUK_Definition && Tok.is(tok::l_brace) &&
4523 NextToken().is(tok::identifier))
4524 SkipBody = Actions.shouldSkipAnonEnumBody(getCurScope(),
4525 NextToken().getIdentifierInfo(),
4526 NextToken().getLocation());
4527
Douglas Gregord6ab8742009-05-28 23:31:59 +00004528 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00004529 bool IsDependent = false;
Craig Topper161e4db2014-05-21 06:02:52 +00004530 const char *PrevSpec = nullptr;
Douglas Gregorba41d012010-04-24 16:38:41 +00004531 unsigned DiagID;
Faisal Vali7db85c52017-12-31 00:06:40 +00004532 Decl *TagDecl = Actions.ActOnTag(
4533 getCurScope(), DeclSpec::TST_enum, TUK, StartLoc, SS, Name, NameLoc,
Erich Keanec480f302018-07-12 21:09:05 +00004534 attrs, AS, DS.getModulePrivateSpecLoc(), TParams, Owned, IsDependent,
4535 ScopedEnumKWLoc, IsScopedUsingClassTag, BaseType,
Faisal Vali7db85c52017-12-31 00:06:40 +00004536 DSC == DeclSpecContext::DSC_type_specifier,
4537 DSC == DeclSpecContext::DSC_template_param ||
4538 DSC == DeclSpecContext::DSC_template_type_arg,
4539 &SkipBody);
Richard Smithd9ba2242015-05-07 03:54:19 +00004540
4541 if (SkipBody.ShouldSkip) {
4542 assert(TUK == Sema::TUK_Definition && "can only skip a definition");
4543
4544 BalancedDelimiterTracker T(*this, tok::l_brace);
4545 T.consumeOpen();
4546 T.skipToEnd();
4547
4548 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
4549 NameLoc.isValid() ? NameLoc : StartLoc,
4550 PrevSpec, DiagID, TagDecl, Owned,
4551 Actions.getASTContext().getPrintingPolicy()))
4552 Diag(StartLoc, DiagID) << PrevSpec;
4553 return;
4554 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00004555
Douglas Gregorba41d012010-04-24 16:38:41 +00004556 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00004557 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00004558 // dependent tag.
4559 if (!Name) {
4560 DS.SetTypeSpecError();
4561 Diag(Tok, diag::err_expected_type_name_after_typename);
4562 return;
4563 }
Chad Rosierc1183952012-06-26 22:30:43 +00004564
Nico Weber83ea0122014-05-03 21:57:40 +00004565 TypeResult Type = Actions.ActOnDependentTag(
4566 getCurScope(), DeclSpec::TST_enum, TUK, SS, Name, StartLoc, NameLoc);
Douglas Gregorba41d012010-04-24 16:38:41 +00004567 if (Type.isInvalid()) {
4568 DS.SetTypeSpecError();
4569 return;
4570 }
Chad Rosierc1183952012-06-26 22:30:43 +00004571
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00004572 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
4573 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00004574 PrevSpec, DiagID, Type.get(),
4575 Actions.getASTContext().getPrintingPolicy()))
Douglas Gregorba41d012010-04-24 16:38:41 +00004576 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00004577
Douglas Gregorba41d012010-04-24 16:38:41 +00004578 return;
4579 }
Mike Stump11289f42009-09-09 15:08:12 +00004580
John McCall48871652010-08-21 09:40:31 +00004581 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00004582 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00004583 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00004584 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00004585 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00004586 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregorba41d012010-04-24 16:38:41 +00004587 }
Chad Rosierc1183952012-06-26 22:30:43 +00004588
Douglas Gregorba41d012010-04-24 16:38:41 +00004589 DS.SetTypeSpecError();
4590 return;
4591 }
Richard Smith0f8ee222012-01-10 01:33:14 +00004592
Bruno Cardoso Lopesdf0ee342017-07-01 00:06:47 +00004593 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
4594 Decl *D = SkipBody.CheckSameAsPrevious ? SkipBody.New : TagDecl;
4595 ParseEnumBody(StartLoc, D);
4596 if (SkipBody.CheckSameAsPrevious &&
4597 !Actions.ActOnDuplicateDefinition(DS, TagDecl, SkipBody)) {
4598 DS.SetTypeSpecError();
4599 return;
4600 }
4601 }
Mike Stump11289f42009-09-09 15:08:12 +00004602
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00004603 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
4604 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00004605 PrevSpec, DiagID, TagDecl, Owned,
4606 Actions.getASTContext().getPrintingPolicy()))
John McCall49bfce42009-08-03 20:12:06 +00004607 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00004608}
4609
Chris Lattnerc1915e22007-01-25 07:29:02 +00004610/// ParseEnumBody - Parse a {} enclosed enumerator-list.
4611/// enumerator-list:
4612/// enumerator
4613/// enumerator-list ',' enumerator
4614/// enumerator:
Aaron Ballman730476b2014-11-08 15:33:35 +00004615/// enumeration-constant attributes[opt]
4616/// enumeration-constant attributes[opt] '=' constant-expression
Chris Lattnerc1915e22007-01-25 07:29:02 +00004617/// enumeration-constant:
4618/// identifier
4619///
John McCall48871652010-08-21 09:40:31 +00004620void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00004621 // Enter the scope of the enum body and start the definition.
Hans Wennborgfe781452014-06-17 00:00:18 +00004622 ParseScope EnumScope(this, Scope::DeclScope | Scope::EnumScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004623 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00004624
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004625 BalancedDelimiterTracker T(*this, tok::l_brace);
4626 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00004627
Chris Lattner37256fb2007-08-27 17:24:30 +00004628 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00004629 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Richard Smithf8812672016-12-02 22:38:31 +00004630 Diag(Tok, diag::err_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00004631
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004632 SmallVector<Decl *, 32> EnumConstantDecls;
Jordan Rose60ac3162015-04-30 17:20:30 +00004633 SmallVector<SuppressAccessChecks, 32> EnumAvailabilityDiags;
Chris Lattnerc1915e22007-01-25 07:29:02 +00004634
Craig Topper161e4db2014-05-21 06:02:52 +00004635 Decl *LastEnumConstDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00004636
Chris Lattnerc1915e22007-01-25 07:29:02 +00004637 // Parse the enumerator-list.
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00004638 while (Tok.isNot(tok::r_brace)) {
4639 // Parse enumerator. If failed, try skipping till the start of the next
4640 // enumerator definition.
4641 if (Tok.isNot(tok::identifier)) {
4642 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
4643 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) &&
4644 TryConsumeToken(tok::comma))
4645 continue;
4646 break;
4647 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00004648 IdentifierInfo *Ident = Tok.getIdentifierInfo();
4649 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004650
John McCall811a0f52010-10-22 23:36:17 +00004651 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00004652 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004653 MaybeParseGNUAttributes(attrs);
Aaron Ballman730476b2014-11-08 15:33:35 +00004654 ProhibitAttributes(attrs); // GNU-style attributes are prohibited.
Aaron Ballman606093a2017-10-15 15:01:42 +00004655 if (standardAttributesAllowed() && isCXX11AttributeSpecifier()) {
4656 if (getLangOpts().CPlusPlus)
Aaron Ballmanc351fba2017-12-04 20:27:34 +00004657 Diag(Tok.getLocation(), getLangOpts().CPlusPlus17
Aaron Ballman606093a2017-10-15 15:01:42 +00004658 ? diag::warn_cxx14_compat_ns_enum_attribute
4659 : diag::ext_ns_enum_attribute)
4660 << 1 /*enumerator*/;
Aaron Ballman730476b2014-11-08 15:33:35 +00004661 ParseCXX11Attributes(attrs);
4662 }
John McCall811a0f52010-10-22 23:36:17 +00004663
Chris Lattnerc1915e22007-01-25 07:29:02 +00004664 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00004665 ExprResult AssignedVal;
Jordan Rose60ac3162015-04-30 17:20:30 +00004666 EnumAvailabilityDiags.emplace_back(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00004667
Alp Tokera3ebe6e2013-12-17 14:12:37 +00004668 if (TryConsumeToken(tok::equal, EqualLoc)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00004669 AssignedVal = ParseConstantExpression();
4670 if (AssignedVal.isInvalid())
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00004671 SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch);
Chris Lattnerc1915e22007-01-25 07:29:02 +00004672 }
Mike Stump11289f42009-09-09 15:08:12 +00004673
Chris Lattnerc1915e22007-01-25 07:29:02 +00004674 // Install the enumerator constant into EnumDecl.
Bruno Cardoso Lopesdf0ee342017-07-01 00:06:47 +00004675 Decl *EnumConstDecl = Actions.ActOnEnumConstant(
Erich Keanec480f302018-07-12 21:09:05 +00004676 getCurScope(), EnumDecl, LastEnumConstDecl, IdentLoc, Ident, attrs,
4677 EqualLoc, AssignedVal.get());
Jordan Rose60ac3162015-04-30 17:20:30 +00004678 EnumAvailabilityDiags.back().done();
Chad Rosierc1183952012-06-26 22:30:43 +00004679
Chris Lattner4ef40012007-06-11 01:28:17 +00004680 EnumConstantDecls.push_back(EnumConstDecl);
4681 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00004682
Douglas Gregorce66d022010-09-07 14:51:08 +00004683 if (Tok.is(tok::identifier)) {
4684 // We're missing a comma between enumerators.
Richard Smithbdb84f32016-07-22 23:36:59 +00004685 SourceLocation Loc = getEndOfPreviousToken();
Chad Rosierc1183952012-06-26 22:30:43 +00004686 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00004687 << FixItHint::CreateInsertion(Loc, ", ");
4688 continue;
4689 }
Chad Rosierc1183952012-06-26 22:30:43 +00004690
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00004691 // Emumerator definition must be finished, only comma or r_brace are
4692 // allowed here.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00004693 SourceLocation CommaLoc;
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00004694 if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) {
4695 if (EqualLoc.isValid())
4696 Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace
4697 << tok::comma;
4698 else
4699 Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator);
4700 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) {
4701 if (TryConsumeToken(tok::comma, CommaLoc))
4702 continue;
4703 } else {
4704 break;
4705 }
4706 }
Mike Stump11289f42009-09-09 15:08:12 +00004707
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00004708 // If comma is followed by r_brace, emit appropriate warning.
4709 if (Tok.is(tok::r_brace) && CommaLoc.isValid()) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004710 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00004711 Diag(CommaLoc, getLangOpts().CPlusPlus ?
4712 diag::ext_enumerator_list_comma_cxx :
4713 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00004714 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004715 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00004716 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
4717 << FixItHint::CreateRemoval(CommaLoc);
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00004718 break;
Richard Smith5d164bc2011-10-15 05:09:34 +00004719 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00004720 }
Mike Stump11289f42009-09-09 15:08:12 +00004721
Chris Lattnerc1915e22007-01-25 07:29:02 +00004722 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004723 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00004724
Chris Lattnerc1915e22007-01-25 07:29:02 +00004725 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00004726 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004727 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00004728
Erich Keanec480f302018-07-12 21:09:05 +00004729 Actions.ActOnEnumBody(StartLoc, T.getRange(), EnumDecl, EnumConstantDecls,
4730 getCurScope(), attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004731
Jordan Rose60ac3162015-04-30 17:20:30 +00004732 // Now handle enum constant availability diagnostics.
4733 assert(EnumConstantDecls.size() == EnumAvailabilityDiags.size());
4734 for (size_t i = 0, e = EnumConstantDecls.size(); i != e; ++i) {
4735 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
4736 EnumAvailabilityDiags[i].redelay();
4737 PD.complete(EnumConstantDecls[i]);
4738 }
4739
Douglas Gregor82ac25e2009-01-08 20:45:30 +00004740 EnumScope.Exit();
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00004741 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, T.getRange());
Richard Smith369b9f92012-06-25 21:37:02 +00004742
4743 // The next token must be valid after an enum definition. If not, a ';'
4744 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00004745 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
4746 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Alp Toker383d2c42014-01-01 03:08:43 +00004747 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Richard Smith369b9f92012-06-25 21:37:02 +00004748 // Push this token back into the preprocessor and change our current token
4749 // to ';' so that the rest of the code recovers as though there were an
4750 // ';' after the definition.
Ilya Biryukov929af672019-05-17 09:32:05 +00004751 PP.EnterToken(Tok, /*IsReinject=*/true);
Richard Smith369b9f92012-06-25 21:37:02 +00004752 Tok.setKind(tok::semi);
4753 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00004754}
Chris Lattner3b561a32006-08-13 00:12:11 +00004755
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004756/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
4757/// is definitely a type-specifier. Return false if it isn't part of a type
4758/// specifier or if we're not sure.
4759bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
4760 switch (Tok.getKind()) {
4761 default: return false;
4762 // type-specifiers
4763 case tok::kw_short:
4764 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004765 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004766 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004767 case tok::kw_signed:
4768 case tok::kw_unsigned:
4769 case tok::kw__Complex:
4770 case tok::kw__Imaginary:
4771 case tok::kw_void:
4772 case tok::kw_char:
4773 case tok::kw_wchar_t:
Richard Smith3a8244d2018-05-01 05:02:45 +00004774 case tok::kw_char8_t:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004775 case tok::kw_char16_t:
4776 case tok::kw_char32_t:
4777 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004778 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004779 case tok::kw_float:
4780 case tok::kw_double:
Leonard Chanf921d852018-06-04 16:07:52 +00004781 case tok::kw__Accum:
Leonard Chanab80f3c2018-06-14 14:53:51 +00004782 case tok::kw__Fract:
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00004783 case tok::kw__Float16:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00004784 case tok::kw___float128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004785 case tok::kw_bool:
4786 case tok::kw__Bool:
4787 case tok::kw__Decimal32:
4788 case tok::kw__Decimal64:
4789 case tok::kw__Decimal128:
4790 case tok::kw___vector:
Alexey Bader954ba212016-04-08 13:40:33 +00004791#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
Alexey Baderb62f1442016-04-13 08:33:41 +00004792#include "clang/Basic/OpenCLImageTypes.def"
Chad Rosierc1183952012-06-26 22:30:43 +00004793
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004794 // struct-or-union-specifier (C99) or class-specifier (C++)
4795 case tok::kw_class:
4796 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004797 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004798 case tok::kw_union:
4799 // enum-specifier
4800 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00004801
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004802 // typedef-name
4803 case tok::annot_typename:
4804 return true;
4805 }
4806}
4807
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004808/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004809/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004810bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004811 switch (Tok.getKind()) {
4812 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004813
Chris Lattner020bab92009-01-04 23:41:41 +00004814 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00004815 if (TryAltiVecVectorToken())
4816 return true;
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +00004817 LLVM_FALLTHROUGH;
Douglas Gregor333489b2009-03-27 23:10:48 +00004818 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004819 // Annotate typenames and C++ scope specifiers. If we get one, just
4820 // recurse to handle whatever we get.
4821 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004822 return true;
4823 if (Tok.is(tok::identifier))
4824 return false;
4825 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00004826
Chris Lattner020bab92009-01-04 23:41:41 +00004827 case tok::coloncolon: // ::foo::bar
4828 if (NextToken().is(tok::kw_new) || // ::new
4829 NextToken().is(tok::kw_delete)) // ::delete
4830 return false;
4831
Chris Lattner020bab92009-01-04 23:41:41 +00004832 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004833 return true;
4834 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00004835
Chris Lattnere37e2332006-08-15 04:50:22 +00004836 // GNU attributes support.
4837 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00004838 // GNU typeof support.
4839 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004840
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004841 // type-specifiers
4842 case tok::kw_short:
4843 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004844 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004845 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004846 case tok::kw_signed:
4847 case tok::kw_unsigned:
4848 case tok::kw__Complex:
4849 case tok::kw__Imaginary:
4850 case tok::kw_void:
4851 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004852 case tok::kw_wchar_t:
Richard Smith3a8244d2018-05-01 05:02:45 +00004853 case tok::kw_char8_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004854 case tok::kw_char16_t:
4855 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004856 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004857 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004858 case tok::kw_float:
4859 case tok::kw_double:
Leonard Chanf921d852018-06-04 16:07:52 +00004860 case tok::kw__Accum:
Leonard Chanab80f3c2018-06-14 14:53:51 +00004861 case tok::kw__Fract:
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00004862 case tok::kw__Float16:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00004863 case tok::kw___float128:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004864 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004865 case tok::kw__Bool:
4866 case tok::kw__Decimal32:
4867 case tok::kw__Decimal64:
4868 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004869 case tok::kw___vector:
Alexey Bader954ba212016-04-08 13:40:33 +00004870#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
Alexey Baderb62f1442016-04-13 08:33:41 +00004871#include "clang/Basic/OpenCLImageTypes.def"
Mike Stump11289f42009-09-09 15:08:12 +00004872
Chris Lattner861a2262008-04-13 18:59:07 +00004873 // struct-or-union-specifier (C99) or class-specifier (C++)
4874 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004875 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004876 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004877 case tok::kw_union:
4878 // enum-specifier
4879 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004880
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004881 // type-qualifier
4882 case tok::kw_const:
4883 case tok::kw_volatile:
4884 case tok::kw_restrict:
Leonard Chanab80f3c2018-06-14 14:53:51 +00004885 case tok::kw__Sat:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004886
John McCallea0a39e2012-11-14 00:49:39 +00004887 // Debugger support.
4888 case tok::kw___unknown_anytype:
4889
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004890 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00004891 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004892 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004893
Chris Lattner409bf7d2008-10-20 00:25:30 +00004894 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4895 case tok::less:
Erik Pilkingtonfa983902018-10-30 20:31:30 +00004896 return getLangOpts().ObjC;
Mike Stump11289f42009-09-09 15:08:12 +00004897
Steve Naroff44ac7772008-12-25 14:16:32 +00004898 case tok::kw___cdecl:
4899 case tok::kw___stdcall:
4900 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004901 case tok::kw___thiscall:
Erich Keane757d3172016-11-02 18:29:35 +00004902 case tok::kw___regcall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00004903 case tok::kw___vectorcall:
Eli Friedman53339e02009-06-08 23:27:34 +00004904 case tok::kw___w64:
4905 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004906 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004907 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004908 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004909
Douglas Gregoraea7afd2015-06-24 22:02:08 +00004910 case tok::kw__Nonnull:
4911 case tok::kw__Nullable:
4912 case tok::kw__Null_unspecified:
Douglas Gregor261a89b2015-06-19 17:51:05 +00004913
Douglas Gregorab209d82015-07-07 03:58:42 +00004914 case tok::kw___kindof:
4915
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004916 case tok::kw___private:
4917 case tok::kw___local:
4918 case tok::kw___global:
4919 case tok::kw___constant:
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00004920 case tok::kw___generic:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004921 case tok::kw___read_only:
4922 case tok::kw___read_write:
4923 case tok::kw___write_only:
Eli Friedman53339e02009-06-08 23:27:34 +00004924 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004925
Anastasia Stulova314fab62019-03-28 11:47:14 +00004926 case tok::kw_private:
4927 return getLangOpts().OpenCL;
4928
Richard Smith8e1ac332013-03-28 01:55:44 +00004929 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004930 case tok::kw__Atomic:
4931 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004932 }
4933}
4934
Chris Lattneracd58a32006-08-06 17:24:14 +00004935/// isDeclarationSpecifier() - Return true if the current token is part of a
4936/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004937///
4938/// \param DisambiguatingWithExpression True to indicate that the purpose of
4939/// this check is to disambiguate between an expression and a declaration.
4940bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004941 switch (Tok.getKind()) {
4942 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004943
Xiuli Pan9c14e282016-01-09 12:53:17 +00004944 case tok::kw_pipe:
Sven van Haastregte518bb42019-05-22 13:12:20 +00004945 return (getLangOpts().OpenCL && getLangOpts().OpenCLVersion >= 200) ||
4946 getLangOpts().OpenCLCPlusPlus;
Xiuli Pan9c14e282016-01-09 12:53:17 +00004947
Chris Lattner020bab92009-01-04 23:41:41 +00004948 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00004949 // Unfortunate hack to support "Class.factoryMethod" notation.
Erik Pilkingtonfa983902018-10-30 20:31:30 +00004950 if (getLangOpts().ObjC && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004951 return false;
John Thompson22334602010-02-05 00:12:22 +00004952 if (TryAltiVecVectorToken())
4953 return true;
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +00004954 LLVM_FALLTHROUGH;
David Blaikie15a430a2011-12-04 05:04:18 +00004955 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004956 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004957 // Annotate typenames and C++ scope specifiers. If we get one, just
4958 // recurse to handle whatever we get.
4959 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004960 return true;
4961 if (Tok.is(tok::identifier))
4962 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004963
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004964 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004965 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004966 // expression is permitted, then this is probably a class message send
4967 // missing the initial '['. In this case, we won't consider this to be
4968 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004969 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004970 isStartOfObjCClassMessageMissingOpenBracket())
4971 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004972
John McCall1f476a12010-02-26 08:45:28 +00004973 return isDeclarationSpecifier();
4974
Chris Lattner020bab92009-01-04 23:41:41 +00004975 case tok::coloncolon: // ::foo::bar
4976 if (NextToken().is(tok::kw_new) || // ::new
4977 NextToken().is(tok::kw_delete)) // ::delete
4978 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004979
Chris Lattner020bab92009-01-04 23:41:41 +00004980 // Annotate typenames and C++ scope specifiers. If we get one, just
4981 // recurse to handle whatever we get.
4982 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004983 return true;
4984 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004985
Chris Lattneracd58a32006-08-06 17:24:14 +00004986 // storage-class-specifier
4987 case tok::kw_typedef:
4988 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004989 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004990 case tok::kw_static:
4991 case tok::kw_auto:
Richard Smithe301ba22015-11-11 02:02:15 +00004992 case tok::kw___auto_type:
Chris Lattneracd58a32006-08-06 17:24:14 +00004993 case tok::kw_register:
4994 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004995 case tok::kw_thread_local:
4996 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004997
Douglas Gregor26701a42011-09-09 02:06:17 +00004998 // Modules
4999 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00005000
John McCallea0a39e2012-11-14 00:49:39 +00005001 // Debugger support
5002 case tok::kw___unknown_anytype:
5003
Chris Lattneracd58a32006-08-06 17:24:14 +00005004 // type-specifiers
5005 case tok::kw_short:
5006 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00005007 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00005008 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00005009 case tok::kw_signed:
5010 case tok::kw_unsigned:
5011 case tok::kw__Complex:
5012 case tok::kw__Imaginary:
5013 case tok::kw_void:
5014 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00005015 case tok::kw_wchar_t:
Richard Smith3a8244d2018-05-01 05:02:45 +00005016 case tok::kw_char8_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00005017 case tok::kw_char16_t:
5018 case tok::kw_char32_t:
5019
Chris Lattneracd58a32006-08-06 17:24:14 +00005020 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00005021 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00005022 case tok::kw_float:
5023 case tok::kw_double:
Leonard Chanf921d852018-06-04 16:07:52 +00005024 case tok::kw__Accum:
Leonard Chanab80f3c2018-06-14 14:53:51 +00005025 case tok::kw__Fract:
Sjoerd Meijercc623ad2017-09-08 15:15:00 +00005026 case tok::kw__Float16:
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00005027 case tok::kw___float128:
Chris Lattnerbb31a422007-11-15 05:25:19 +00005028 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00005029 case tok::kw__Bool:
5030 case tok::kw__Decimal32:
5031 case tok::kw__Decimal64:
5032 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00005033 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00005034
Chris Lattner861a2262008-04-13 18:59:07 +00005035 // struct-or-union-specifier (C99) or class-specifier (C++)
5036 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00005037 case tok::kw_struct:
5038 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00005039 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00005040 // enum-specifier
5041 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00005042
Chris Lattneracd58a32006-08-06 17:24:14 +00005043 // type-qualifier
5044 case tok::kw_const:
5045 case tok::kw_volatile:
5046 case tok::kw_restrict:
Leonard Chanab80f3c2018-06-14 14:53:51 +00005047 case tok::kw__Sat:
Steve Naroffad373bd2007-07-31 12:34:36 +00005048
Chris Lattneracd58a32006-08-06 17:24:14 +00005049 // function-specifier
5050 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00005051 case tok::kw_virtual:
5052 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00005053 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00005054
Richard Smith1dba27c2013-01-29 09:02:09 +00005055 // alignment-specifier
5056 case tok::kw__Alignas:
5057
Richard Smithd16fe122012-10-25 00:00:53 +00005058 // friend keyword.
5059 case tok::kw_friend:
5060
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00005061 // static_assert-declaration
5062 case tok::kw__Static_assert:
5063
Chris Lattner599e47e2007-08-09 17:01:07 +00005064 // GNU typeof support.
5065 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00005066
Chris Lattner599e47e2007-08-09 17:01:07 +00005067 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00005068 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00005069
Richard Smithd16fe122012-10-25 00:00:53 +00005070 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00005071 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00005072 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00005073
Gauthier Harnisch796ed032019-06-14 08:56:20 +00005074 // C++20 consteval.
5075 case tok::kw_consteval:
5076
Richard Smith8e1ac332013-03-28 01:55:44 +00005077 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00005078 case tok::kw__Atomic:
5079 return true;
5080
Chris Lattner8b2ec162008-07-26 03:38:44 +00005081 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
5082 case tok::less:
Erik Pilkingtonfa983902018-10-30 20:31:30 +00005083 return getLangOpts().ObjC;
Mike Stump11289f42009-09-09 15:08:12 +00005084
Douglas Gregor19b7acf2011-04-27 05:41:15 +00005085 // typedef-name
5086 case tok::annot_typename:
5087 return !DisambiguatingWithExpression ||
5088 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00005089
Steve Narofff192fab2009-01-06 19:34:12 +00005090 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00005091 case tok::kw___cdecl:
5092 case tok::kw___stdcall:
5093 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00005094 case tok::kw___thiscall:
Erich Keane757d3172016-11-02 18:29:35 +00005095 case tok::kw___regcall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00005096 case tok::kw___vectorcall:
Eli Friedman53339e02009-06-08 23:27:34 +00005097 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00005098 case tok::kw___sptr:
5099 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00005100 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00005101 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00005102 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00005103 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00005104 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005105
Douglas Gregoraea7afd2015-06-24 22:02:08 +00005106 case tok::kw__Nonnull:
5107 case tok::kw__Nullable:
5108 case tok::kw__Null_unspecified:
Douglas Gregor261a89b2015-06-19 17:51:05 +00005109
Douglas Gregorab209d82015-07-07 03:58:42 +00005110 case tok::kw___kindof:
5111
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005112 case tok::kw___private:
5113 case tok::kw___local:
5114 case tok::kw___global:
5115 case tok::kw___constant:
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00005116 case tok::kw___generic:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005117 case tok::kw___read_only:
5118 case tok::kw___read_write:
5119 case tok::kw___write_only:
Alexey Bader954ba212016-04-08 13:40:33 +00005120#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
Alexey Baderb62f1442016-04-13 08:33:41 +00005121#include "clang/Basic/OpenCLImageTypes.def"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005122
Eli Friedman53339e02009-06-08 23:27:34 +00005123 return true;
Anastasia Stulova314fab62019-03-28 11:47:14 +00005124
5125 case tok::kw_private:
5126 return getLangOpts().OpenCL;
Chris Lattneracd58a32006-08-06 17:24:14 +00005127 }
5128}
5129
Richard Smith35845152017-02-07 01:37:30 +00005130bool Parser::isConstructorDeclarator(bool IsUnqualified, bool DeductionGuide) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005131 TentativeParsingAction TPA(*this);
5132
5133 // Parse the C++ scope specifier.
5134 CXXScopeSpec SS;
David Blaikieefdccaa2016-01-15 23:43:34 +00005135 if (ParseOptionalCXXScopeSpecifier(SS, nullptr,
Douglas Gregordf593fb2011-11-07 17:33:42 +00005136 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00005137 TPA.Revert();
5138 return false;
5139 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005140
5141 // Parse the constructor name.
Richard Smithaf3b3252017-05-18 19:21:48 +00005142 if (Tok.is(tok::identifier)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005143 // We already know that we have a constructor name; just consume
5144 // the token.
5145 ConsumeToken();
Richard Smithaf3b3252017-05-18 19:21:48 +00005146 } else if (Tok.is(tok::annot_template_id)) {
5147 ConsumeAnnotationToken();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005148 } else {
5149 TPA.Revert();
5150 return false;
5151 }
5152
Richard Smith8f8697f2017-02-08 01:16:55 +00005153 // There may be attributes here, appertaining to the constructor name or type
5154 // we just stepped past.
5155 SkipCXX11Attributes();
5156
Richard Smith43f340f2012-03-27 23:05:05 +00005157 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005158 if (Tok.isNot(tok::l_paren)) {
5159 TPA.Revert();
5160 return false;
5161 }
5162 ConsumeParen();
5163
Richard Smith43f340f2012-03-27 23:05:05 +00005164 // A right parenthesis, or ellipsis followed by a right parenthesis signals
5165 // that we have a constructor.
5166 if (Tok.is(tok::r_paren) ||
5167 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005168 TPA.Revert();
5169 return true;
5170 }
5171
Richard Smithf2163662013-09-06 00:12:20 +00005172 // A C++11 attribute here signals that we have a constructor, and is an
5173 // attribute on the first constructor parameter.
5174 if (getLangOpts().CPlusPlus11 &&
5175 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
5176 /*OuterMightBeMessageSend*/ true)) {
5177 TPA.Revert();
5178 return true;
5179 }
5180
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005181 // If we need to, enter the specified scope.
5182 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00005183 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005184 DeclScopeObj.EnterDeclaratorScope();
5185
Francois Pichet79f3a872011-01-31 04:54:32 +00005186 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00005187 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00005188 MaybeParseMicrosoftAttributes(Attrs);
5189
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005190 // Check whether the next token(s) are part of a declaration
5191 // specifier, in which case we have the start of a parameter and,
5192 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00005193 bool IsConstructor = false;
5194 if (isDeclarationSpecifier())
5195 IsConstructor = true;
5196 else if (Tok.is(tok::identifier) ||
5197 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
5198 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
5199 // This might be a parenthesized member name, but is more likely to
5200 // be a constructor declaration with an invalid argument type. Keep
5201 // looking.
5202 if (Tok.is(tok::annot_cxxscope))
Richard Smithaf3b3252017-05-18 19:21:48 +00005203 ConsumeAnnotationToken();
Richard Smithefd009d2012-03-27 00:56:56 +00005204 ConsumeToken();
5205
5206 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00005207 // which must have one of the following syntactic forms (see the
5208 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00005209 switch (Tok.getKind()) {
5210 case tok::l_paren:
5211 // C(X ( int));
5212 case tok::l_square:
5213 // C(X [ 5]);
5214 // C(X [ [attribute]]);
5215 case tok::coloncolon:
5216 // C(X :: Y);
5217 // C(X :: *p);
Richard Smithefd009d2012-03-27 00:56:56 +00005218 // Assume this isn't a constructor, rather than assuming it's a
5219 // constructor with an unnamed parameter of an ill-formed type.
5220 break;
5221
Richard Smith446161b2014-03-03 21:12:53 +00005222 case tok::r_paren:
5223 // C(X )
Richard Smith8f8697f2017-02-08 01:16:55 +00005224
5225 // Skip past the right-paren and any following attributes to get to
5226 // the function body or trailing-return-type.
5227 ConsumeParen();
5228 SkipCXX11Attributes();
5229
Richard Smith35845152017-02-07 01:37:30 +00005230 if (DeductionGuide) {
5231 // C(X) -> ... is a deduction guide.
Richard Smith8f8697f2017-02-08 01:16:55 +00005232 IsConstructor = Tok.is(tok::arrow);
Richard Smith35845152017-02-07 01:37:30 +00005233 break;
5234 }
Richard Smith8f8697f2017-02-08 01:16:55 +00005235 if (Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Richard Smith446161b2014-03-03 21:12:53 +00005236 // Assume these were meant to be constructors:
5237 // C(X) : (the name of a bit-field cannot be parenthesized).
5238 // C(X) try (this is otherwise ill-formed).
5239 IsConstructor = true;
5240 }
Richard Smith8f8697f2017-02-08 01:16:55 +00005241 if (Tok.is(tok::semi) || Tok.is(tok::l_brace)) {
Richard Smith446161b2014-03-03 21:12:53 +00005242 // If we have a constructor name within the class definition,
5243 // assume these were meant to be constructors:
5244 // C(X) {
5245 // C(X) ;
5246 // ... because otherwise we would be declaring a non-static data
5247 // member that is ill-formed because it's of the same type as its
5248 // surrounding class.
5249 //
5250 // FIXME: We can actually do this whether or not the name is qualified,
5251 // because if it is qualified in this context it must be being used as
Richard Smith35845152017-02-07 01:37:30 +00005252 // a constructor name.
Richard Smith446161b2014-03-03 21:12:53 +00005253 // currently, so we're somewhat conservative here.
5254 IsConstructor = IsUnqualified;
5255 }
5256 break;
5257
Richard Smithefd009d2012-03-27 00:56:56 +00005258 default:
5259 IsConstructor = true;
5260 break;
5261 }
5262 }
5263
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005264 TPA.Revert();
5265 return IsConstructor;
5266}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00005267
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005268/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00005269/// type-qualifier-list: [C99 6.7.5]
5270/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00005271/// [vendor] attributes
Aaron Ballman08b06592014-07-22 12:44:22 +00005272/// [ only if AttrReqs & AR_VendorAttributesParsed ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00005273/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00005274/// [vendor] type-qualifier-list attributes
Aaron Ballman08b06592014-07-22 12:44:22 +00005275/// [ only if AttrReqs & AR_VendorAttributesParsed ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00005276/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Aaron Ballman08b06592014-07-22 12:44:22 +00005277/// [ only if AttReqs & AR_CXX11AttributesParsed ]
5278/// Note: vendor can be GNU, MS, etc and can be explicitly controlled via
5279/// AttrRequirements bitmask values.
Alex Lorenz8f4d3992017-02-13 23:19:40 +00005280void Parser::ParseTypeQualifierListOpt(
5281 DeclSpec &DS, unsigned AttrReqs, bool AtomicAllowed,
5282 bool IdentifierRequired,
5283 Optional<llvm::function_ref<void()>> CodeCompletionHandler) {
Aaron Ballman606093a2017-10-15 15:01:42 +00005284 if (standardAttributesAllowed() && (AttrReqs & AR_CXX11AttributesParsed) &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005285 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00005286 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00005287 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005288 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005289 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00005290
5291 SourceLocation EndLoc;
5292
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005293 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00005294 bool isInvalid = false;
Craig Topper161e4db2014-05-21 06:02:52 +00005295 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +00005296 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00005297 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00005298
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005299 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00005300 case tok::code_completion:
Alex Lorenz8f4d3992017-02-13 23:19:40 +00005301 if (CodeCompletionHandler)
5302 (*CodeCompletionHandler)();
5303 else
5304 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00005305 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00005306
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005307 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00005308 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00005309 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00005310 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005311 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00005312 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00005313 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00005314 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005315 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00005316 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00005317 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005318 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00005319 case tok::kw__Atomic:
5320 if (!AtomicAllowed)
5321 goto DoneWithTypeQuals;
5322 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
5323 getLangOpts());
5324 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005325
5326 // OpenCL qualifiers:
Anastasia Stulova948e37c2019-03-25 11:54:02 +00005327 case tok::kw_private:
Anastasia Stulova314fab62019-03-28 11:47:14 +00005328 if (!getLangOpts().OpenCL)
5329 goto DoneWithTypeQuals;
5330 LLVM_FALLTHROUGH;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005331 case tok::kw___private:
5332 case tok::kw___global:
5333 case tok::kw___local:
5334 case tok::kw___constant:
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +00005335 case tok::kw___generic:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005336 case tok::kw___read_only:
5337 case tok::kw___write_only:
5338 case tok::kw___read_write:
Aaron Ballman05d76ea2014-01-14 01:29:54 +00005339 ParseOpenCLQualifiers(DS.getAttributes());
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00005340 break;
5341
Andrey Bokhanko45d41322016-05-11 18:38:21 +00005342 case tok::kw___unaligned:
5343 isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID,
5344 getLangOpts());
5345 break;
Aaron Ballman317a77f2013-05-22 23:25:32 +00005346 case tok::kw___uptr:
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00005347 // GNU libc headers in C mode use '__uptr' as an identifier which conflicts
Alp Toker62c5b572013-11-26 01:30:10 +00005348 // with the MS modifier keyword.
Aaron Ballman08b06592014-07-22 12:44:22 +00005349 if ((AttrReqs & AR_DeclspecAttributesParsed) && !getLangOpts().CPlusPlus &&
Alp Toker47642d22013-12-03 06:13:01 +00005350 IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) {
5351 if (TryKeywordIdentFallback(false))
5352 continue;
Alp Toker62c5b572013-11-26 01:30:10 +00005353 }
Galina Kistanova77674252017-06-01 21:15:34 +00005354 LLVM_FALLTHROUGH;
Alp Toker62c5b572013-11-26 01:30:10 +00005355 case tok::kw___sptr:
Eli Friedman53339e02009-06-08 23:27:34 +00005356 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00005357 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00005358 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00005359 case tok::kw___cdecl:
5360 case tok::kw___stdcall:
5361 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00005362 case tok::kw___thiscall:
Erich Keane757d3172016-11-02 18:29:35 +00005363 case tok::kw___regcall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00005364 case tok::kw___vectorcall:
Aaron Ballman08b06592014-07-22 12:44:22 +00005365 if (AttrReqs & AR_DeclspecAttributesParsed) {
John McCall53fa7142010-12-24 02:08:15 +00005366 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00005367 continue;
5368 }
5369 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00005370 case tok::kw___pascal:
Aaron Ballman08b06592014-07-22 12:44:22 +00005371 if (AttrReqs & AR_VendorAttributesParsed) {
John McCall53fa7142010-12-24 02:08:15 +00005372 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00005373 continue;
5374 }
5375 goto DoneWithTypeQuals;
Douglas Gregor261a89b2015-06-19 17:51:05 +00005376
5377 // Nullability type specifiers.
Douglas Gregoraea7afd2015-06-24 22:02:08 +00005378 case tok::kw__Nonnull:
5379 case tok::kw__Nullable:
5380 case tok::kw__Null_unspecified:
Douglas Gregor261a89b2015-06-19 17:51:05 +00005381 ParseNullabilityTypeSpecifiers(DS.getAttributes());
5382 continue;
5383
Douglas Gregorab209d82015-07-07 03:58:42 +00005384 // Objective-C 'kindof' types.
5385 case tok::kw___kindof:
5386 DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc,
Erich Keanee891aa92018-07-13 15:07:47 +00005387 nullptr, 0, ParsedAttr::AS_Keyword);
Douglas Gregorab209d82015-07-07 03:58:42 +00005388 (void)ConsumeToken();
5389 continue;
5390
Chris Lattnere37e2332006-08-15 04:50:22 +00005391 case tok::kw___attribute:
Aaron Ballman08b06592014-07-22 12:44:22 +00005392 if (AttrReqs & AR_GNUAttributesParsedAndRejected)
5393 // When GNU attributes are expressly forbidden, diagnose their usage.
5394 Diag(Tok, diag::err_attributes_not_allowed);
5395
5396 // Parse the attributes even if they are rejected to ensure that error
5397 // recovery is graceful.
5398 if (AttrReqs & AR_GNUAttributesParsed ||
5399 AttrReqs & AR_GNUAttributesParsedAndRejected) {
John McCall53fa7142010-12-24 02:08:15 +00005400 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00005401 continue; // do *not* consume the next token!
5402 }
5403 // otherwise, FALL THROUGH!
Galina Kistanova77674252017-06-01 21:15:34 +00005404 LLVM_FALLTHROUGH;
Chris Lattnercf0bab22008-12-18 07:02:59 +00005405 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00005406 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00005407 // If this is not a type-qualifier token, we're done reading type
5408 // qualifiers. First verify that DeclSpec's are consistent.
Craig Topper25122412015-11-15 03:32:11 +00005409 DS.Finish(Actions, Actions.getASTContext().getPrintingPolicy());
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00005410 if (EndLoc.isValid())
5411 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005412 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005413 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005414
Chris Lattnerd9c3c592006-08-05 06:26:47 +00005415 // If the specifier combination wasn't legal, issue a diagnostic.
5416 if (isInvalid) {
5417 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00005418 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00005419 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00005420 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005421 }
5422}
5423
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00005424/// ParseDeclarator - Parse and verify a newly-initialized declarator.
5425///
5426void Parser::ParseDeclarator(Declarator &D) {
5427 /// This implements the 'declarator' production in the C grammar, then checks
5428 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00005429 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00005430}
5431
Richard Smith0b350b92014-10-28 16:55:02 +00005432static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang,
Faisal Vali421b2d12017-12-29 05:41:00 +00005433 DeclaratorContext TheContext) {
Richard Smith0efa75c2012-03-29 01:16:42 +00005434 if (Kind == tok::star || Kind == tok::caret)
5435 return true;
5436
Sven van Haastregte518bb42019-05-22 13:12:20 +00005437 if (Kind == tok::kw_pipe &&
5438 ((Lang.OpenCL && Lang.OpenCLVersion >= 200) || Lang.OpenCLCPlusPlus))
Xiuli Pan9c14e282016-01-09 12:53:17 +00005439 return true;
5440
Richard Smith0efa75c2012-03-29 01:16:42 +00005441 if (!Lang.CPlusPlus)
5442 return false;
5443
Richard Smith0b350b92014-10-28 16:55:02 +00005444 if (Kind == tok::amp)
5445 return true;
5446
5447 // We parse rvalue refs in C++03, because otherwise the errors are scary.
5448 // But we must not parse them in conversion-type-ids and new-type-ids, since
5449 // those can be legitimately followed by a && operator.
5450 // (The same thing can in theory happen after a trailing-return-type, but
5451 // since those are a C++11 feature, there is no rejects-valid issue there.)
5452 if (Kind == tok::ampamp)
Faisal Vali421b2d12017-12-29 05:41:00 +00005453 return Lang.CPlusPlus11 ||
5454 (TheContext != DeclaratorContext::ConversionIdContext &&
5455 TheContext != DeclaratorContext::CXXNewContext);
Richard Smith0b350b92014-10-28 16:55:02 +00005456
5457 return false;
Richard Smith0efa75c2012-03-29 01:16:42 +00005458}
5459
Xiuli Pan9c14e282016-01-09 12:53:17 +00005460// Indicates whether the given declarator is a pipe declarator.
5461static bool isPipeDeclerator(const Declarator &D) {
5462 const unsigned NumTypes = D.getNumTypeObjects();
5463
5464 for (unsigned Idx = 0; Idx != NumTypes; ++Idx)
5465 if (DeclaratorChunk::Pipe == D.getTypeObject(Idx).Kind)
5466 return true;
5467
5468 return false;
5469}
5470
Sebastian Redlbd150f42008-11-21 19:14:01 +00005471/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
5472/// is parsed by the function passed to it. Pass null, and the direct-declarator
5473/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00005474/// ptr-operator production.
5475///
Richard Smith09f76ee2011-10-19 21:33:05 +00005476/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00005477/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
5478/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00005479///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005480/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
5481/// [C] pointer[opt] direct-declarator
5482/// [C++] direct-declarator
5483/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00005484///
5485/// pointer: [C99 6.7.5]
5486/// '*' type-qualifier-list[opt]
5487/// '*' type-qualifier-list[opt] pointer
5488///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00005489/// ptr-operator:
5490/// '*' cv-qualifier-seq[opt]
5491/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00005492/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00005493/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00005494/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005495/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00005496void Parser::ParseDeclaratorInternal(Declarator &D,
5497 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00005498 if (Diags.hasAllExtensionsSilenced())
5499 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00005500
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005501 // C++ member pointers start with a '::' or a nested-name.
5502 // Member pointers get special handling, since there's no place for the
5503 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00005504 if (getLangOpts().CPlusPlus &&
Richard Smith83c2ecf2016-02-02 23:34:49 +00005505 (Tok.is(tok::coloncolon) || Tok.is(tok::kw_decltype) ||
Serge Pavlov458ea762014-07-16 05:16:52 +00005506 (Tok.is(tok::identifier) &&
5507 (NextToken().is(tok::coloncolon) || NextToken().is(tok::less))) ||
Chris Lattner803802d2009-03-24 17:04:48 +00005508 Tok.is(tok::annot_cxxscope))) {
Faisal Vali421b2d12017-12-29 05:41:00 +00005509 bool EnteringContext =
5510 D.getContext() == DeclaratorContext::FileContext ||
5511 D.getContext() == DeclaratorContext::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005512 CXXScopeSpec SS;
David Blaikieefdccaa2016-01-15 23:43:34 +00005513 ParseOptionalCXXScopeSpecifier(SS, nullptr, EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00005514
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00005515 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00005516 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005517 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00005518 if (D.mayHaveIdentifier())
5519 D.getCXXScopeSpec() = SS;
5520 else
5521 AnnotateScopeToken(SS, true);
5522
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005523 if (DirectDeclParser)
5524 (this->*DirectDeclParser)(D);
5525 return;
5526 }
5527
5528 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005529 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00005530 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005531 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005532 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005533
5534 // Recurse to parse whatever is left.
5535 ParseDeclaratorInternal(D, DirectDeclParser);
5536
5537 // Sema will have to catch (syntactically invalid) pointers into global
5538 // scope. It has to catch pointers into namespace scope anyway.
Erich Keanec480f302018-07-12 21:09:05 +00005539 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(
Stephen Kelly1c301dc2018-08-09 21:09:38 +00005540 SS, DS.getTypeQualifiers(), DS.getEndLoc()),
Erich Keanec480f302018-07-12 21:09:05 +00005541 std::move(DS.getAttributes()),
5542 /* Don't replace range end. */ SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005543 return;
5544 }
5545 }
5546
5547 tok::TokenKind Kind = Tok.getKind();
Xiuli Pan9c14e282016-01-09 12:53:17 +00005548
5549 if (D.getDeclSpec().isTypeSpecPipe() && !isPipeDeclerator(D)) {
Xiuli Pan11e13f62016-02-26 03:13:03 +00005550 DeclSpec DS(AttrFactory);
5551 ParseTypeQualifierListOpt(DS);
Xiuli Pan9c14e282016-01-09 12:53:17 +00005552
5553 D.AddTypeInfo(
5554 DeclaratorChunk::getPipe(DS.getTypeQualifiers(), DS.getPipeLoc()),
Erich Keanec480f302018-07-12 21:09:05 +00005555 std::move(DS.getAttributes()), SourceLocation());
Xiuli Pan9c14e282016-01-09 12:53:17 +00005556 }
5557
Steve Naroffec33ed92008-08-27 16:04:49 +00005558 // Not a pointer, C++ reference, or block.
Richard Smith0b350b92014-10-28 16:55:02 +00005559 if (!isPtrOperatorToken(Kind, getLangOpts(), D.getContext())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00005560 if (DirectDeclParser)
5561 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00005562 return;
5563 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005564
Sebastian Redled0f3b02009-03-15 22:02:01 +00005565 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
5566 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00005567 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005568 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00005569
Chris Lattner9eac9312009-03-27 04:18:06 +00005570 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00005571 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00005572 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005573
Aaron Ballman08b06592014-07-22 12:44:22 +00005574 // GNU attributes are not allowed here in a new-type-id, but Declspec and
5575 // C++11 attributes are allowed.
5576 unsigned Reqs = AR_CXX11AttributesParsed | AR_DeclspecAttributesParsed |
Faisal Vali421b2d12017-12-29 05:41:00 +00005577 ((D.getContext() != DeclaratorContext::CXXNewContext)
5578 ? AR_GNUAttributesParsed
5579 : AR_GNUAttributesParsedAndRejected);
Aaron Ballman08b06592014-07-22 12:44:22 +00005580 ParseTypeQualifierListOpt(DS, Reqs, true, !D.mayOmitIdentifier());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005581 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00005582
Bill Wendling3708c182007-05-27 10:15:43 +00005583 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00005584 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00005585 if (Kind == tok::star)
5586 // Remember that we parsed a pointer type, and remember the type-quals.
Erich Keanec480f302018-07-12 21:09:05 +00005587 D.AddTypeInfo(DeclaratorChunk::getPointer(
5588 DS.getTypeQualifiers(), Loc, DS.getConstSpecLoc(),
5589 DS.getVolatileSpecLoc(), DS.getRestrictSpecLoc(),
5590 DS.getAtomicSpecLoc(), DS.getUnalignedSpecLoc()),
5591 std::move(DS.getAttributes()), SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00005592 else
5593 // Remember that we parsed a Block type, and remember the type-quals.
Erich Keanec480f302018-07-12 21:09:05 +00005594 D.AddTypeInfo(
5595 DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), Loc),
5596 std::move(DS.getAttributes()), SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00005597 } else {
5598 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00005599 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00005600
Sebastian Redl3b27be62009-03-23 00:00:23 +00005601 // Complain about rvalue references in C++03, but then go on and build
5602 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00005603 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005604 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005605 diag::warn_cxx98_compat_rvalue_reference :
5606 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00005607
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005608 // GNU-style and C++11 attributes are allowed here, as is restrict.
5609 ParseTypeQualifierListOpt(DS);
5610 D.ExtendWithDeclSpec(DS);
5611
Bill Wendling93efb222007-06-02 23:28:54 +00005612 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
5613 // cv-qualifiers are introduced through the use of a typedef or of a
5614 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00005615 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
5616 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
5617 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00005618 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00005619 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
5620 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00005621 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00005622 // 'restrict' is permitted as an extension.
5623 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
5624 Diag(DS.getAtomicSpecLoc(),
5625 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00005626 }
Bill Wendling3708c182007-05-27 10:15:43 +00005627
5628 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00005629 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00005630
Douglas Gregor66583c52008-11-03 15:51:28 +00005631 if (D.getNumTypeObjects() > 0) {
5632 // C++ [dcl.ref]p4: There shall be no references to references.
5633 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
5634 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00005635 if (const IdentifierInfo *II = D.getIdentifier())
5636 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
5637 << II;
5638 else
5639 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
5640 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00005641
Sebastian Redlbd150f42008-11-21 19:14:01 +00005642 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00005643 // can go ahead and build the (technically ill-formed)
5644 // declarator: reference collapsing will take care of it.
5645 }
5646 }
5647
Richard Smith8e1ac332013-03-28 01:55:44 +00005648 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00005649 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00005650 Kind == tok::amp),
Erich Keanec480f302018-07-12 21:09:05 +00005651 std::move(DS.getAttributes()), SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00005652 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00005653}
5654
Richard Trieuf4b81d02014-06-24 23:14:24 +00005655// When correcting from misplaced brackets before the identifier, the location
5656// is saved inside the declarator so that other diagnostic messages can use
5657// them. This extracts and returns that location, or returns the provided
5658// location if a stored location does not exist.
5659static SourceLocation getMissingDeclaratorIdLoc(Declarator &D,
5660 SourceLocation Loc) {
5661 if (D.getName().StartLocation.isInvalid() &&
5662 D.getName().EndLocation.isValid())
5663 return D.getName().EndLocation;
5664
5665 return Loc;
5666}
5667
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005668/// ParseDirectDeclarator
5669/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00005670/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005671/// '(' declarator ')'
5672/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00005673/// [C90] direct-declarator '[' constant-expression[opt] ']'
5674/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5675/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5676/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5677/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005678/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5679/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005680/// direct-declarator '(' parameter-type-list ')'
5681/// direct-declarator '(' identifier-list[opt] ')'
5682/// [GNU] direct-declarator '(' parameter-forward-declarations
5683/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00005684/// [C++] direct-declarator '(' parameter-declaration-clause ')'
5685/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005686/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
5687/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
5688/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00005689/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005690/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00005691///
5692/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00005693/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00005694/// '::'[opt] nested-name-specifier[opt] type-name
5695///
5696/// id-expression: [C++ 5.1]
5697/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00005698/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00005699///
5700/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00005701/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00005702/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00005703/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00005704/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00005705/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00005706///
Richard Smithbdb84f32016-07-22 23:36:59 +00005707/// C++17 adds the following, which we also handle here:
5708///
5709/// simple-declaration:
5710/// <decl-spec> '[' identifier-list ']' brace-or-equal-initializer ';'
5711///
Richard Smith1453e312012-03-27 01:42:32 +00005712/// Note, any additional constructs added here may need corresponding changes
5713/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00005714void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00005715 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00005716
David Blaikiebbafb8a2012-03-11 07:00:24 +00005717 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Richard Smithbdb84f32016-07-22 23:36:59 +00005718 // This might be a C++17 structured binding.
5719 if (Tok.is(tok::l_square) && !D.mayOmitIdentifier() &&
5720 D.getCXXScopeSpec().isEmpty())
5721 return ParseDecompositionDeclarator(D);
5722
Serge Pavlov458ea762014-07-16 05:16:52 +00005723 // Don't parse FOO:BAR as if it were a typo for FOO::BAR inside a class, in
5724 // this context it is a bitfield. Also in range-based for statement colon
5725 // may delimit for-range-declaration.
Faisal Vali421b2d12017-12-29 05:41:00 +00005726 ColonProtectionRAIIObject X(
5727 *this, D.getContext() == DeclaratorContext::MemberContext ||
5728 (D.getContext() == DeclaratorContext::ForContext &&
5729 getLangOpts().CPlusPlus11));
Serge Pavlov458ea762014-07-16 05:16:52 +00005730
Douglas Gregor7861a802009-11-03 01:35:08 +00005731 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00005732 if (D.getCXXScopeSpec().isEmpty()) {
Faisal Vali421b2d12017-12-29 05:41:00 +00005733 bool EnteringContext =
5734 D.getContext() == DeclaratorContext::FileContext ||
5735 D.getContext() == DeclaratorContext::MemberContext;
David Blaikieefdccaa2016-01-15 23:43:34 +00005736 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), nullptr,
Douglas Gregordf593fb2011-11-07 17:33:42 +00005737 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00005738 }
5739
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00005740 if (D.getCXXScopeSpec().isValid()) {
Richard Smith64e033f2015-01-15 00:48:52 +00005741 if (Actions.ShouldEnterDeclaratorScope(getCurScope(),
5742 D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00005743 // Change the declaration context for name lookup, until this function
5744 // is exited (and the declarator has been parsed).
5745 DeclScopeObj.EnterDeclaratorScope();
Alex Lorenze151f0102016-12-07 10:24:44 +00005746 else if (getObjCDeclContext()) {
5747 // Ensure that we don't interpret the next token as an identifier when
5748 // dealing with declarations in an Objective-C container.
5749 D.SetIdentifier(nullptr, Tok.getLocation());
5750 D.setInvalidType(true);
5751 ConsumeToken();
5752 goto PastIdentifier;
5753 }
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00005754 }
5755
Douglas Gregor27b4c162010-12-23 22:44:42 +00005756 // C++0x [dcl.fct]p14:
Faisal Vali8435a692014-12-04 12:40:21 +00005757 // There is a syntactic ambiguity when an ellipsis occurs at the end of a
5758 // parameter-declaration-clause without a preceding comma. In this case,
5759 // the ellipsis is parsed as part of the abstract-declarator if the type
5760 // of the parameter either names a template parameter pack that has not
5761 // been expanded or contains auto; otherwise, it is parsed as part of the
5762 // parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00005763 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Faisal Vali421b2d12017-12-29 05:41:00 +00005764 !((D.getContext() == DeclaratorContext::PrototypeContext ||
5765 D.getContext() == DeclaratorContext::LambdaExprParameterContext ||
5766 D.getContext() == DeclaratorContext::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00005767 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00005768 !D.hasGroupingParens() &&
Faisal Vali8435a692014-12-04 12:40:21 +00005769 !Actions.containsUnexpandedParameterPacks(D) &&
Faisal Vali090da2d2018-01-01 18:23:28 +00005770 D.getDeclSpec().getTypeSpecType() != TST_auto)) {
Richard Smith0efa75c2012-03-29 01:16:42 +00005771 SourceLocation EllipsisLoc = ConsumeToken();
Richard Smith0b350b92014-10-28 16:55:02 +00005772 if (isPtrOperatorToken(Tok.getKind(), getLangOpts(), D.getContext())) {
Richard Smith0efa75c2012-03-29 01:16:42 +00005773 // The ellipsis was put in the wrong place. Recover, and explain to
5774 // the user what they should have done.
5775 ParseDeclarator(D);
Nikola Smiljanic69fdc9f2014-06-06 02:58:59 +00005776 if (EllipsisLoc.isValid())
5777 DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D);
Richard Smith0efa75c2012-03-29 01:16:42 +00005778 return;
5779 } else
5780 D.setEllipsisLoc(EllipsisLoc);
5781
5782 // The ellipsis can't be followed by a parenthesized declarator. We
5783 // check for that in ParseParenDeclarator, after we have disambiguated
5784 // the l_paren token.
5785 }
5786
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00005787 if (Tok.isOneOf(tok::identifier, tok::kw_operator, tok::annot_template_id,
5788 tok::tilde)) {
Douglas Gregor7861a802009-11-03 01:35:08 +00005789 // We found something that indicates the start of an unqualified-id.
5790 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00005791 bool AllowConstructorName;
Richard Smith35845152017-02-07 01:37:30 +00005792 bool AllowDeductionGuide;
5793 if (D.getDeclSpec().hasTypeSpecifier()) {
John McCall84821e72010-04-13 06:39:49 +00005794 AllowConstructorName = false;
Richard Smith35845152017-02-07 01:37:30 +00005795 AllowDeductionGuide = false;
5796 } else if (D.getCXXScopeSpec().isSet()) {
John McCall84821e72010-04-13 06:39:49 +00005797 AllowConstructorName =
Faisal Vali421b2d12017-12-29 05:41:00 +00005798 (D.getContext() == DeclaratorContext::FileContext ||
5799 D.getContext() == DeclaratorContext::MemberContext);
Richard Smith35845152017-02-07 01:37:30 +00005800 AllowDeductionGuide = false;
5801 } else {
Faisal Vali421b2d12017-12-29 05:41:00 +00005802 AllowConstructorName =
5803 (D.getContext() == DeclaratorContext::MemberContext);
Fangrui Song6907ce22018-07-30 19:24:48 +00005804 AllowDeductionGuide =
Faisal Vali421b2d12017-12-29 05:41:00 +00005805 (D.getContext() == DeclaratorContext::FileContext ||
5806 D.getContext() == DeclaratorContext::MemberContext);
Richard Smith35845152017-02-07 01:37:30 +00005807 }
John McCall84821e72010-04-13 06:39:49 +00005808
Richard Smith64e033f2015-01-15 00:48:52 +00005809 bool HadScope = D.getCXXScopeSpec().isValid();
Chad Rosierc1183952012-06-26 22:30:43 +00005810 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
5811 /*EnteringContext=*/true,
David Blaikieefdccaa2016-01-15 23:43:34 +00005812 /*AllowDestructorName=*/true, AllowConstructorName,
Richard Smithc08b6932018-04-27 02:00:13 +00005813 AllowDeductionGuide, nullptr, nullptr,
Richard Smith35845152017-02-07 01:37:30 +00005814 D.getName()) ||
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00005815 // Once we're past the identifier, if the scope was bad, mark the
5816 // whole declarator bad.
5817 D.getCXXScopeSpec().isInvalid()) {
Craig Topper161e4db2014-05-21 06:02:52 +00005818 D.SetIdentifier(nullptr, Tok.getLocation());
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00005819 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00005820 } else {
Richard Smith64e033f2015-01-15 00:48:52 +00005821 // ParseUnqualifiedId might have parsed a scope specifier during error
5822 // recovery. If it did so, enter that scope.
5823 if (!HadScope && D.getCXXScopeSpec().isValid() &&
5824 Actions.ShouldEnterDeclaratorScope(getCurScope(),
5825 D.getCXXScopeSpec()))
5826 DeclScopeObj.EnterDeclaratorScope();
5827
Douglas Gregor7861a802009-11-03 01:35:08 +00005828 // Parsed the unqualified-id; update range information and move along.
5829 if (D.getSourceRange().getBegin().isInvalid())
5830 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
5831 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00005832 }
Douglas Gregor7861a802009-11-03 01:35:08 +00005833 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00005834 }
Richard Smithd63db6e2015-12-19 02:40:19 +00005835
5836 if (D.getCXXScopeSpec().isNotEmpty()) {
5837 // We have a scope specifier but no following unqualified-id.
5838 Diag(PP.getLocForEndOfToken(D.getCXXScopeSpec().getEndLoc()),
5839 diag::err_expected_unqualified_id)
5840 << /*C++*/1;
5841 D.SetIdentifier(nullptr, Tok.getLocation());
5842 goto PastIdentifier;
5843 }
Douglas Gregor7861a802009-11-03 01:35:08 +00005844 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005845 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00005846 "There's a C++-specific check for tok::identifier above");
5847 assert(Tok.getIdentifierInfo() && "Not an identifier?");
5848 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Richard Trieu2664dc12014-05-05 22:06:50 +00005849 D.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00005850 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00005851 goto PastIdentifier;
Richard Smith74639b12017-05-19 01:54:59 +00005852 } else if (Tok.is(tok::identifier) && !D.mayHaveIdentifier()) {
5853 // We're not allowed an identifier here, but we got one. Try to figure out
5854 // if the user was trying to attach a name to the type, or whether the name
5855 // is some unrelated trailing syntax.
5856 bool DiagnoseIdentifier = false;
5857 if (D.hasGroupingParens())
5858 // An identifier within parens is unlikely to be intended to be anything
5859 // other than a name being "declared".
5860 DiagnoseIdentifier = true;
Richard Smith77a9c602018-02-28 03:02:23 +00005861 else if (D.getContext() == DeclaratorContext::TemplateArgContext)
Richard Smith74639b12017-05-19 01:54:59 +00005862 // T<int N> is an accidental identifier; T<int N indicates a missing '>'.
5863 DiagnoseIdentifier =
5864 NextToken().isOneOf(tok::comma, tok::greater, tok::greatergreater);
Faisal Vali421b2d12017-12-29 05:41:00 +00005865 else if (D.getContext() == DeclaratorContext::AliasDeclContext ||
5866 D.getContext() == DeclaratorContext::AliasTemplateContext)
Richard Smith74639b12017-05-19 01:54:59 +00005867 // The most likely error is that the ';' was forgotten.
5868 DiagnoseIdentifier = NextToken().isOneOf(tok::comma, tok::semi);
Richard Smithe303e352018-02-02 22:24:54 +00005869 else if ((D.getContext() == DeclaratorContext::TrailingReturnContext ||
5870 D.getContext() == DeclaratorContext::TrailingReturnVarContext) &&
Richard Smith74639b12017-05-19 01:54:59 +00005871 !isCXX11VirtSpecifier(Tok))
5872 DiagnoseIdentifier = NextToken().isOneOf(
5873 tok::comma, tok::semi, tok::equal, tok::l_brace, tok::kw_try);
5874 if (DiagnoseIdentifier) {
Richard Smithf39720b2013-10-13 22:12:28 +00005875 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
5876 << FixItHint::CreateRemoval(Tok.getLocation());
Craig Topper161e4db2014-05-21 06:02:52 +00005877 D.SetIdentifier(nullptr, Tok.getLocation());
Richard Smithf39720b2013-10-13 22:12:28 +00005878 ConsumeToken();
5879 goto PastIdentifier;
5880 }
Douglas Gregor7861a802009-11-03 01:35:08 +00005881 }
Richard Smith0efa75c2012-03-29 01:16:42 +00005882
Douglas Gregor7861a802009-11-03 01:35:08 +00005883 if (Tok.is(tok::l_paren)) {
Richard Smithe303e352018-02-02 22:24:54 +00005884 // If this might be an abstract-declarator followed by a direct-initializer,
5885 // check whether this is a valid declarator chunk. If it can't be, assume
5886 // that it's an initializer instead.
5887 if (D.mayOmitIdentifier() && D.mayBeFollowedByCXXDirectInit()) {
5888 RevertingTentativeParsingAction PA(*this);
5889 if (TryParseDeclarator(true, D.mayHaveIdentifier(), true) ==
5890 TPResult::False) {
5891 D.SetIdentifier(nullptr, Tok.getLocation());
5892 goto PastIdentifier;
5893 }
5894 }
5895
Chris Lattneracd58a32006-08-06 17:24:14 +00005896 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00005897 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00005898 // Example: 'char (*X)' or 'int (*XX)(void)'
5899 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005900
5901 // If the declarator was parenthesized, we entered the declarator
5902 // scope when parsing the parenthesized declarator, then exited
5903 // the scope already. Re-enter the scope, if we need to.
5904 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00005905 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00005906 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00005907 if (!D.isInvalidType() &&
Nico Weber6b05f382015-02-18 04:53:03 +00005908 Actions.ShouldEnterDeclaratorScope(getCurScope(),
5909 D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005910 // Change the declaration context for name lookup, until this function
5911 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00005912 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00005913 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00005914 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00005915 // This could be something simple like "int" (in which case the declarator
5916 // portion is empty), if an abstract-declarator is allowed.
Craig Topper161e4db2014-05-21 06:02:52 +00005917 D.SetIdentifier(nullptr, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00005918
5919 // The grammar for abstract-pack-declarator does not allow grouping parens.
5920 // FIXME: Revisit this once core issue 1488 is resolved.
5921 if (D.hasEllipsis() && D.hasGroupingParens())
5922 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
5923 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00005924 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00005925 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00005926 LLVM_BUILTIN_TRAP;
Richard Trieuf4b81d02014-06-24 23:14:24 +00005927 if (Tok.is(tok::l_square))
5928 return ParseMisplacedBracketDeclarator(D);
Faisal Vali421b2d12017-12-29 05:41:00 +00005929 if (D.getContext() == DeclaratorContext::MemberContext) {
Alex Lorenzf1278212017-04-11 15:01:53 +00005930 // Objective-C++: Detect C++ keywords and try to prevent further errors by
5931 // treating these keyword as valid member names.
Erik Pilkingtonfa983902018-10-30 20:31:30 +00005932 if (getLangOpts().ObjC && getLangOpts().CPlusPlus &&
Alex Lorenzf1278212017-04-11 15:01:53 +00005933 Tok.getIdentifierInfo() &&
5934 Tok.getIdentifierInfo()->isCPlusPlusKeyword(getLangOpts())) {
5935 Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
5936 diag::err_expected_member_name_or_semi_objcxx_keyword)
5937 << Tok.getIdentifierInfo()
5938 << (D.getDeclSpec().isEmpty() ? SourceRange()
5939 : D.getDeclSpec().getSourceRange());
5940 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
5941 D.SetRangeEnd(Tok.getLocation());
5942 ConsumeToken();
5943 goto PastIdentifier;
5944 }
Richard Trieuf4b81d02014-06-24 23:14:24 +00005945 Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
5946 diag::err_expected_member_name_or_semi)
Richard Trieua1342402014-05-02 23:40:32 +00005947 << (D.getDeclSpec().isEmpty() ? SourceRange()
5948 : D.getDeclSpec().getSourceRange());
Richard Trieuf4b81d02014-06-24 23:14:24 +00005949 } else if (getLangOpts().CPlusPlus) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00005950 if (Tok.isOneOf(tok::period, tok::arrow))
Richard Trieu9c672672013-01-26 02:31:38 +00005951 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
Richard Trieu2f586962013-09-05 02:31:33 +00005952 else {
5953 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
5954 if (Tok.isAtStartOfLine() && Loc.isValid())
5955 Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
5956 << getLangOpts().CPlusPlus;
5957 else
Richard Trieuf4b81d02014-06-24 23:14:24 +00005958 Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
5959 diag::err_expected_unqualified_id)
Richard Trieu2f586962013-09-05 02:31:33 +00005960 << getLangOpts().CPlusPlus;
5961 }
Richard Trieuf4b81d02014-06-24 23:14:24 +00005962 } else {
5963 Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
5964 diag::err_expected_either)
5965 << tok::identifier << tok::l_paren;
5966 }
Craig Topper161e4db2014-05-21 06:02:52 +00005967 D.SetIdentifier(nullptr, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00005968 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00005969 }
Mike Stump11289f42009-09-09 15:08:12 +00005970
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00005971 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00005972 assert(D.isPastIdentifier() &&
5973 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00005974
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005975 // Don't parse attributes unless we have parsed an unparenthesized name.
5976 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00005977 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005978
Chris Lattneracd58a32006-08-06 17:24:14 +00005979 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00005980 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00005981 // Enter function-declaration scope, limiting any declarators to the
5982 // function prototype scope, including parameter declarators.
5983 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00005984 Scope::FunctionPrototypeScope|Scope::DeclScope|
5985 (D.isFunctionDeclaratorAFunctionDeclaration()
5986 ? Scope::FunctionDeclarationScope : 0));
5987
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00005988 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
5989 // In such a case, check if we actually have a function declarator; if it
5990 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00005991 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00005992 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
5993 // The name of the declarator, if any, is tentatively declared within
5994 // a possible direct initializer.
5995 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
5996 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
5997 TentativelyDeclaredIdentifiers.pop_back();
5998 if (!IsFunctionDecl)
5999 break;
6000 }
John McCall084e83d2011-03-24 11:26:52 +00006001 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006002 BalancedDelimiterTracker T(*this, tok::l_paren);
6003 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00006004 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00006005 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00006006 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00006007 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00006008 } else {
6009 break;
6010 }
6011 }
Chad Rosierc1183952012-06-26 22:30:43 +00006012}
Chris Lattneracd58a32006-08-06 17:24:14 +00006013
Richard Smithbdb84f32016-07-22 23:36:59 +00006014void Parser::ParseDecompositionDeclarator(Declarator &D) {
6015 assert(Tok.is(tok::l_square));
6016
6017 // If this doesn't look like a structured binding, maybe it's a misplaced
6018 // array declarator.
6019 // FIXME: Consume the l_square first so we don't need extra lookahead for
6020 // this.
6021 if (!(NextToken().is(tok::identifier) &&
6022 GetLookAheadToken(2).isOneOf(tok::comma, tok::r_square)) &&
6023 !(NextToken().is(tok::r_square) &&
6024 GetLookAheadToken(2).isOneOf(tok::equal, tok::l_brace)))
6025 return ParseMisplacedBracketDeclarator(D);
6026
6027 BalancedDelimiterTracker T(*this, tok::l_square);
6028 T.consumeOpen();
6029
6030 SmallVector<DecompositionDeclarator::Binding, 32> Bindings;
6031 while (Tok.isNot(tok::r_square)) {
6032 if (!Bindings.empty()) {
6033 if (Tok.is(tok::comma))
6034 ConsumeToken();
6035 else {
6036 if (Tok.is(tok::identifier)) {
6037 SourceLocation EndLoc = getEndOfPreviousToken();
6038 Diag(EndLoc, diag::err_expected)
6039 << tok::comma << FixItHint::CreateInsertion(EndLoc, ",");
6040 } else {
6041 Diag(Tok, diag::err_expected_comma_or_rsquare);
6042 }
6043
6044 SkipUntil(tok::r_square, tok::comma, tok::identifier,
6045 StopAtSemi | StopBeforeMatch);
6046 if (Tok.is(tok::comma))
6047 ConsumeToken();
6048 else if (Tok.isNot(tok::identifier))
6049 break;
6050 }
6051 }
6052
6053 if (Tok.isNot(tok::identifier)) {
6054 Diag(Tok, diag::err_expected) << tok::identifier;
6055 break;
6056 }
6057
6058 Bindings.push_back({Tok.getIdentifierInfo(), Tok.getLocation()});
6059 ConsumeToken();
6060 }
6061
6062 if (Tok.isNot(tok::r_square))
6063 // We've already diagnosed a problem here.
6064 T.skipToEnd();
6065 else {
6066 // C++17 does not allow the identifier-list in a structured binding
6067 // to be empty.
6068 if (Bindings.empty())
6069 Diag(Tok.getLocation(), diag::ext_decomp_decl_empty);
6070
6071 T.consumeClose();
6072 }
6073
6074 return D.setDecompositionBindings(T.getOpenLocation(), Bindings,
6075 T.getCloseLocation());
6076}
6077
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006078/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
6079/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00006080/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006081/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
6082///
6083/// direct-declarator:
6084/// '(' declarator ')'
6085/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00006086/// direct-declarator '(' parameter-type-list ')'
6087/// direct-declarator '(' identifier-list[opt] ')'
6088/// [GNU] direct-declarator '(' parameter-forward-declarations
6089/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006090///
6091void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006092 BalancedDelimiterTracker T(*this, tok::l_paren);
6093 T.consumeOpen();
6094
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006095 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00006096
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00006097 // Eat any attributes before we look at whether this is a grouping or function
6098 // declarator paren. If this is a grouping paren, the attribute applies to
6099 // the type being built up, for example:
6100 // int (__attribute__(()) *x)(long y)
6101 // If this ends up not being a grouping paren, the attribute applies to the
6102 // first argument, for example:
6103 // int (__attribute__(()) int x)
6104 // In either case, we need to eat any attributes to be able to determine what
6105 // sort of paren this is.
6106 //
John McCall084e83d2011-03-24 11:26:52 +00006107 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00006108 bool RequiresArg = false;
6109 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00006110 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00006111
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00006112 // We require that the argument list (if this is a non-grouping paren) be
6113 // present even if the attribute list was empty.
6114 RequiresArg = true;
6115 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00006116
Steve Naroff44ac7772008-12-25 14:16:32 +00006117 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00006118 ParseMicrosoftTypeAttributes(attrs);
6119
Dawn Perchik335e16b2010-09-03 01:29:35 +00006120 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00006121 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00006122 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00006123
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006124 // If we haven't past the identifier yet (or where the identifier would be
6125 // stored, if this is an abstract declarator), then this is probably just
6126 // grouping parens. However, if this could be an abstract-declarator, then
6127 // this could also be the start of function arguments (consider 'void()').
6128 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00006129
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006130 if (!D.mayOmitIdentifier()) {
6131 // If this can't be an abstract-declarator, this *must* be a grouping
6132 // paren, because we haven't seen the identifier yet.
6133 isGrouping = true;
6134 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00006135 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
6136 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00006137 isDeclarationSpecifier() || // 'int(int)' is a function.
6138 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006139 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
6140 // considered to be a type, not a K&R identifier-list.
6141 isGrouping = false;
6142 } else {
6143 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
6144 isGrouping = true;
6145 }
Mike Stump11289f42009-09-09 15:08:12 +00006146
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006147 // If this is a grouping paren, handle:
6148 // direct-declarator: '(' declarator ')'
6149 // direct-declarator: '(' attributes declarator ')'
6150 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00006151 SourceLocation EllipsisLoc = D.getEllipsisLoc();
6152 D.setEllipsisLoc(SourceLocation());
6153
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00006154 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00006155 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00006156 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006157 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006158 T.consumeClose();
Erich Keanec480f302018-07-12 21:09:05 +00006159 D.AddTypeInfo(
6160 DeclaratorChunk::getParen(T.getOpenLocation(), T.getCloseLocation()),
6161 std::move(attrs), T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00006162
6163 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00006164
6165 // An ellipsis cannot be placed outside parentheses.
6166 if (EllipsisLoc.isValid())
Nikola Smiljanic69fdc9f2014-06-06 02:58:59 +00006167 DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D);
Richard Smith0efa75c2012-03-29 01:16:42 +00006168
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006169 return;
6170 }
Mike Stump11289f42009-09-09 15:08:12 +00006171
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006172 // Okay, if this wasn't a grouping paren, it must be the start of a function
6173 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00006174 // identifier (and remember where it would have been), then call into
6175 // ParseFunctionDeclarator to handle of argument list.
Craig Topper161e4db2014-05-21 06:02:52 +00006176 D.SetIdentifier(nullptr, Tok.getLocation());
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006177
David Blaikie15a430a2011-12-04 05:04:18 +00006178 // Enter function-declaration scope, limiting any declarators to the
6179 // function prototype scope, including parameter declarators.
6180 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00006181 Scope::FunctionPrototypeScope | Scope::DeclScope |
6182 (D.isFunctionDeclaratorAFunctionDeclaration()
6183 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00006184 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00006185 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006186}
6187
6188/// ParseFunctionDeclarator - We are after the identifier and have parsed the
6189/// declarator D up to a paren, which indicates that we are parsing function
6190/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00006191///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006192/// If FirstArgAttrs is non-null, then the caller parsed those arguments
6193/// immediately after the open paren - they should be considered to be the
6194/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00006195///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006196/// If RequiresArg is true, then the first argument of the function is required
6197/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00006198///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006199/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
6200/// (C++11) ref-qualifier[opt], exception-specification[opt],
6201/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
6202///
6203/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00006204/// dynamic-exception-specification
6205/// noexcept-specification
6206///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006207void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006208 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006209 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00006210 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00006211 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00006212 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00006213 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00006214 // lparen is already consumed!
6215 assert(D.isPastIdentifier() && "Should not call before identifier!");
6216
6217 // This should be true when the function has typed arguments.
6218 // Otherwise, it is treated as a K&R-style function.
6219 bool HasProto = false;
6220 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006221 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00006222 // Remember where we see an ellipsis, if any.
6223 SourceLocation EllipsisLoc;
6224
6225 DeclSpec DS(AttrFactory);
6226 bool RefQualifierIsLValueRef = true;
6227 SourceLocation RefQualifierLoc;
6228 ExceptionSpecificationType ESpecType = EST_None;
6229 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006230 SmallVector<ParsedType, 2> DynamicExceptions;
6231 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00006232 ExprResult NoexceptExpr;
Hans Wennborgdcfba332015-10-06 23:40:43 +00006233 CachedTokens *ExceptionSpecTokens = nullptr;
Aaron Ballman606093a2017-10-15 15:01:42 +00006234 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00006235 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006236
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00006237 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
6238 EndLoc is the end location for the function declarator.
6239 They differ for trailing return types. */
6240 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00006241 SourceLocation LParenLoc, RParenLoc;
6242 LParenLoc = Tracker.getOpenLocation();
6243 StartLoc = LParenLoc;
6244
Douglas Gregor9e66af42011-07-05 16:44:18 +00006245 if (isFunctionDeclaratorIdentifierList()) {
6246 if (RequiresArg)
6247 Diag(Tok, diag::err_argument_required_after_attribute);
6248
6249 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
6250
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006251 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00006252 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00006253 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00006254 EndLoc = RParenLoc;
Aaron Ballman606093a2017-10-15 15:01:42 +00006255
Fangrui Song6907ce22018-07-30 19:24:48 +00006256 // If there are attributes following the identifier list, parse them and
Aaron Ballman606093a2017-10-15 15:01:42 +00006257 // prohibit them.
6258 MaybeParseCXX11Attributes(FnAttrs);
6259 ProhibitAttributes(FnAttrs);
Douglas Gregor9e66af42011-07-05 16:44:18 +00006260 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00006261 if (Tok.isNot(tok::r_paren))
Fangrui Song6907ce22018-07-30 19:24:48 +00006262 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
Faisal Vali2b391ab2013-09-26 19:54:12 +00006263 EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00006264 else if (RequiresArg)
6265 Diag(Tok, diag::err_argument_required_after_attribute);
6266
Alexey Bader1f277942017-10-11 11:16:31 +00006267 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus
6268 || getLangOpts().OpenCL;
Douglas Gregor9e66af42011-07-05 16:44:18 +00006269
6270 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006271 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00006272 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00006273 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00006274 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00006275
David Blaikiebbafb8a2012-03-11 07:00:24 +00006276 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006277 // FIXME: Accept these components in any order, and produce fixits to
6278 // correct the order if the user gets it wrong. Ideally we should deal
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00006279 // with the pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00006280
6281 // Parse cv-qualifier-seq[opt].
Aaron Ballman08b06592014-07-22 12:44:22 +00006282 ParseTypeQualifierListOpt(DS, AR_NoAttributesParsed,
Alex Lorenz8f4d3992017-02-13 23:19:40 +00006283 /*AtomicAllowed*/ false,
6284 /*IdentifierRequired=*/false,
6285 llvm::function_ref<void()>([&]() {
6286 Actions.CodeCompleteFunctionQualifiers(DS, D);
6287 }));
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006288 if (!DS.getSourceRange().getEnd().isInvalid()) {
6289 EndLoc = DS.getSourceRange().getEnd();
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006290 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00006291
6292 // Parse ref-qualifier[opt].
Ehsan Akhgaric07d1e22015-03-25 00:53:33 +00006293 if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc))
Douglas Gregor9e66af42011-07-05 16:44:18 +00006294 EndLoc = RefQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00006295
Douglas Gregor3024f072012-04-16 07:05:22 +00006296 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00006297 // If a declaration declares a member function or member function
6298 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00006299 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00006300 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00006301 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00006302 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00006303 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00006304 getLangOpts().CPlusPlus11 &&
Richard Smith990a6922014-01-17 21:01:18 +00006305 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Faisal Vali421b2d12017-12-29 05:41:00 +00006306 (D.getContext() == DeclaratorContext::MemberContext
Richard Smithad1bbb92013-03-15 00:41:52 +00006307 ? !D.getDeclSpec().isFriendSpecified()
Faisal Vali421b2d12017-12-29 05:41:00 +00006308 : D.getContext() == DeclaratorContext::FileContext &&
Richard Smithad1bbb92013-03-15 00:41:52 +00006309 D.getCXXScopeSpec().isValid() &&
6310 Actions.CurContext->isRecord());
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00006311
6312 Qualifiers Q = Qualifiers::fromCVRUMask(DS.getTypeQualifiers());
Gauthier Harnisch796ed032019-06-14 08:56:20 +00006313 if (D.getDeclSpec().hasConstexprSpecifier() && !getLangOpts().CPlusPlus14)
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00006314 Q.addConst();
Anastasia Stulova5cffa452019-01-21 16:01:38 +00006315 // FIXME: Collect C++ address spaces.
6316 // If there are multiple different address spaces, the source is invalid.
6317 // Carry on using the first addr space for the qualifiers of 'this'.
6318 // The diagnostic will be given later while creating the function
6319 // prototype for the method.
6320 if (getLangOpts().OpenCLCPlusPlus) {
6321 for (ParsedAttr &attr : DS.getAttributes()) {
6322 LangAS ASIdx = attr.asOpenCLLangAS();
6323 if (ASIdx != LangAS::Default) {
6324 Q.addAddressSpace(ASIdx);
6325 break;
6326 }
6327 }
6328 }
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00006329
6330 Sema::CXXThisScopeRAII ThisScope(
6331 Actions, dyn_cast<CXXRecordDecl>(Actions.CurContext), Q,
6332 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00006333
Douglas Gregor9e66af42011-07-05 16:44:18 +00006334 // Parse exception-specification[opt].
Richard Smith0b3a4622014-11-13 20:01:57 +00006335 bool Delayed = D.isFirstDeclarationOfMember() &&
Richard Smith3ef3e892014-11-20 22:32:11 +00006336 D.isFunctionDeclaratorAFunctionDeclaration();
6337 if (Delayed && Actions.isLibstdcxxEagerExceptionSpecHack(D) &&
6338 GetLookAheadToken(0).is(tok::kw_noexcept) &&
6339 GetLookAheadToken(1).is(tok::l_paren) &&
6340 GetLookAheadToken(2).is(tok::kw_noexcept) &&
6341 GetLookAheadToken(3).is(tok::l_paren) &&
6342 GetLookAheadToken(4).is(tok::identifier) &&
6343 GetLookAheadToken(4).getIdentifierInfo()->isStr("swap")) {
6344 // HACK: We've got an exception-specification
6345 // noexcept(noexcept(swap(...)))
6346 // or
6347 // noexcept(noexcept(swap(...)) && noexcept(swap(...)))
6348 // on a 'swap' member function. This is a libstdc++ bug; the lookup
6349 // for 'swap' will only find the function we're currently declaring,
6350 // whereas it expects to find a non-member swap through ADL. Turn off
6351 // delayed parsing to give it a chance to find what it expects.
6352 Delayed = false;
6353 }
Richard Smith0b3a4622014-11-13 20:01:57 +00006354 ESpecType = tryParseExceptionSpecification(Delayed,
6355 ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00006356 DynamicExceptions,
6357 DynamicExceptionRanges,
Richard Smith0b3a4622014-11-13 20:01:57 +00006358 NoexceptExpr,
6359 ExceptionSpecTokens);
Douglas Gregor9e66af42011-07-05 16:44:18 +00006360 if (ESpecType != EST_None)
6361 EndLoc = ESpecRange.getEnd();
6362
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006363 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
6364 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00006365 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006366
Douglas Gregor9e66af42011-07-05 16:44:18 +00006367 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00006368 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00006369 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00006370 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Faisal Vali090da2d2018-01-01 18:23:28 +00006371 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00006372 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00006373 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00006374 SourceRange Range;
Richard Smithe303e352018-02-02 22:24:54 +00006375 TrailingReturnType =
6376 ParseTrailingReturnType(Range, D.mayBeFollowedByCXXDirectInit());
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00006377 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00006378 }
Aaron Ballman606093a2017-10-15 15:01:42 +00006379 } else if (standardAttributesAllowed()) {
6380 MaybeParseCXX11Attributes(FnAttrs);
Douglas Gregor9e66af42011-07-05 16:44:18 +00006381 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00006382 }
6383
Reid Kleckner078aea92016-12-09 17:14:05 +00006384 // Collect non-parameter declarations from the prototype if this is a function
6385 // declaration. They will be moved into the scope of the function. Only do
6386 // this in C and not C++, where the decls will continue to live in the
6387 // surrounding context.
6388 SmallVector<NamedDecl *, 0> DeclsInPrototype;
6389 if (getCurScope()->getFlags() & Scope::FunctionDeclarationScope &&
6390 !getLangOpts().CPlusPlus) {
6391 for (Decl *D : getCurScope()->decls()) {
6392 NamedDecl *ND = dyn_cast<NamedDecl>(D);
6393 if (!ND || isa<ParmVarDecl>(ND))
6394 continue;
6395 DeclsInPrototype.push_back(ND);
6396 }
6397 }
6398
Douglas Gregor9e66af42011-07-05 16:44:18 +00006399 // Remember that we parsed a function type, and remember the attributes.
Erich Keanec480f302018-07-12 21:09:05 +00006400 D.AddTypeInfo(DeclaratorChunk::getFunction(
6401 HasProto, IsAmbiguous, LParenLoc, ParamInfo.data(),
6402 ParamInfo.size(), EllipsisLoc, RParenLoc,
Anastasia Stulovaa9bc4bd2019-01-09 11:25:09 +00006403 RefQualifierIsLValueRef, RefQualifierLoc,
6404 /*MutableLoc=*/SourceLocation(),
6405 ESpecType, ESpecRange, DynamicExceptions.data(),
6406 DynamicExceptionRanges.data(), DynamicExceptions.size(),
Erich Keanec480f302018-07-12 21:09:05 +00006407 NoexceptExpr.isUsable() ? NoexceptExpr.get() : nullptr,
6408 ExceptionSpecTokens, DeclsInPrototype, StartLoc,
Anastasia Stulovaa9bc4bd2019-01-09 11:25:09 +00006409 LocalEndLoc, D, TrailingReturnType, &DS),
Erich Keanec480f302018-07-12 21:09:05 +00006410 std::move(FnAttrs), EndLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00006411}
6412
Ehsan Akhgaric07d1e22015-03-25 00:53:33 +00006413/// ParseRefQualifier - Parses a member function ref-qualifier. Returns
6414/// true if a ref-qualifier is found.
6415bool Parser::ParseRefQualifier(bool &RefQualifierIsLValueRef,
6416 SourceLocation &RefQualifierLoc) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00006417 if (Tok.isOneOf(tok::amp, tok::ampamp)) {
Ehsan Akhgaric07d1e22015-03-25 00:53:33 +00006418 Diag(Tok, getLangOpts().CPlusPlus11 ?
6419 diag::warn_cxx98_compat_ref_qualifier :
6420 diag::ext_ref_qualifier);
6421
6422 RefQualifierIsLValueRef = Tok.is(tok::amp);
6423 RefQualifierLoc = ConsumeToken();
6424 return true;
6425 }
6426 return false;
6427}
6428
Douglas Gregor9e66af42011-07-05 16:44:18 +00006429/// isFunctionDeclaratorIdentifierList - This parameter list may have an
6430/// identifier list form for a K&R-style function: void foo(a,b,c)
6431///
6432/// Note that identifier-lists are only allowed for normal declarators, not for
6433/// abstract-declarators.
6434bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00006435 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00006436 && Tok.is(tok::identifier)
6437 && !TryAltiVecVectorToken()
6438 // K&R identifier lists can't have typedefs as identifiers, per C99
6439 // 6.7.5.3p11.
6440 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
6441 // Identifier lists follow a really simple grammar: the identifiers can
6442 // be followed *only* by a ", identifier" or ")". However, K&R
6443 // identifier lists are really rare in the brave new modern world, and
6444 // it is very common for someone to typo a type in a non-K&R style
6445 // list. If we are presented with something like: "void foo(intptr x,
6446 // float y)", we don't want to start parsing the function declarator as
6447 // though it is a K&R style declarator just because intptr is an
6448 // invalid type.
6449 //
6450 // To handle this, we check to see if the token after the first
6451 // identifier is a "," or ")". Only then do we parse it as an
6452 // identifier list.
Bruno Cardoso Lopes218c8742016-09-13 20:04:35 +00006453 && (!Tok.is(tok::eof) &&
6454 (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)));
Douglas Gregor9e66af42011-07-05 16:44:18 +00006455}
6456
6457/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
6458/// we found a K&R-style identifier list instead of a typed parameter list.
6459///
6460/// After returning, ParamInfo will hold the parsed parameters.
6461///
6462/// identifier-list: [C99 6.7.5]
6463/// identifier
6464/// identifier-list ',' identifier
6465///
6466void Parser::ParseFunctionDeclaratorIdentifierList(
6467 Declarator &D,
Craig Topper5603df42013-07-05 19:34:19 +00006468 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00006469 // If there was no identifier specified for the declarator, either we are in
6470 // an abstract-declarator, or we are in a parameter declarator which was found
6471 // to be abstract. In abstract-declarators, identifier lists are not valid:
6472 // diagnose this.
6473 if (!D.getIdentifier())
6474 Diag(Tok, diag::ext_ident_list_in_param);
6475
6476 // Maintain an efficient lookup of params we have seen so far.
6477 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
6478
Alp Tokera3ebe6e2013-12-17 14:12:37 +00006479 do {
Douglas Gregor9e66af42011-07-05 16:44:18 +00006480 // If this isn't an identifier, report the error and skip until ')'.
6481 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00006482 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00006483 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor9e66af42011-07-05 16:44:18 +00006484 // Forget we parsed anything.
6485 ParamInfo.clear();
6486 return;
6487 }
6488
6489 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
6490
6491 // Reject 'typedef int y; int test(x, y)', but continue parsing.
6492 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
6493 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
6494
6495 // Verify that the argument identifier has not already been mentioned.
David Blaikie82e95a32014-11-19 07:49:47 +00006496 if (!ParamsSoFar.insert(ParmII).second) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00006497 Diag(Tok, diag::err_param_redefinition) << ParmII;
6498 } else {
6499 // Remember this identifier in ParamInfo.
6500 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
6501 Tok.getLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00006502 nullptr));
Douglas Gregor9e66af42011-07-05 16:44:18 +00006503 }
6504
6505 // Eat the identifier.
6506 ConsumeToken();
Douglas Gregor9e66af42011-07-05 16:44:18 +00006507 // The list continues if we see a comma.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00006508 } while (TryConsumeToken(tok::comma));
Douglas Gregor9e66af42011-07-05 16:44:18 +00006509}
6510
6511/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
6512/// after the opening parenthesis. This function will not parse a K&R-style
6513/// identifier list.
6514///
Richard Smith2620cd92012-04-11 04:01:28 +00006515/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
6516/// caller parsed those arguments immediately after the open paren - they should
6517/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00006518///
6519/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
6520/// be the location of the ellipsis, if any was parsed.
6521///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00006522/// parameter-type-list: [C99 6.7.5]
6523/// parameter-list
6524/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00006525/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00006526///
6527/// parameter-list: [C99 6.7.5]
6528/// parameter-declaration
6529/// parameter-list ',' parameter-declaration
6530///
6531/// parameter-declaration: [C99 6.7.5]
6532/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00006533/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00006534/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00006535/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00006536/// declaration-specifiers abstract-declarator[opt]
6537/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00006538/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00006539/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00006540/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00006541///
Douglas Gregor9e66af42011-07-05 16:44:18 +00006542void Parser::ParseParameterDeclarationClause(
6543 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00006544 ParsedAttributes &FirstArgAttrs,
Craig Topper5603df42013-07-05 19:34:19 +00006545 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00006546 SourceLocation &EllipsisLoc) {
Alp Tokera3ebe6e2013-12-17 14:12:37 +00006547 do {
6548 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
6549 // before deciding this was a parameter-declaration-clause.
6550 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
Chris Lattner371ed4e2008-04-06 06:57:35 +00006551 break;
Mike Stump11289f42009-09-09 15:08:12 +00006552
Chris Lattner371ed4e2008-04-06 06:57:35 +00006553 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00006554 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00006555 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006556
Richard Smith2620cd92012-04-11 04:01:28 +00006557 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00006558 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00006559
John McCall53fa7142010-12-24 02:08:15 +00006560 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00006561 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00006562
6563 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00006564
6565 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00006566 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00006567 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00006568 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
6569 // too much hassle.
6570 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00006571
Faisal Valia534f072018-04-26 00:42:40 +00006572 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00006573
Faisal Vali2b391ab2013-09-26 19:54:12 +00006574
Fangrui Song6907ce22018-07-30 19:24:48 +00006575 // Parse the declarator. This is "PrototypeContext" or
6576 // "LambdaExprParameterContext", because we must accept either
Faisal Vali2b391ab2013-09-26 19:54:12 +00006577 // 'declarator' or 'abstract-declarator' here.
Faisal Vali421b2d12017-12-29 05:41:00 +00006578 Declarator ParmDeclarator(
6579 DS, D.getContext() == DeclaratorContext::LambdaExprContext
6580 ? DeclaratorContext::LambdaExprParameterContext
6581 : DeclaratorContext::PrototypeContext);
Faisal Vali2b391ab2013-09-26 19:54:12 +00006582 ParseDeclarator(ParmDeclarator);
Chris Lattner371ed4e2008-04-06 06:57:35 +00006583
6584 // Parse GNU attributes, if present.
Faisal Vali2b391ab2013-09-26 19:54:12 +00006585 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump11289f42009-09-09 15:08:12 +00006586
Chris Lattner371ed4e2008-04-06 06:57:35 +00006587 // Remember this parsed parameter in ParamInfo.
Faisal Vali2b391ab2013-09-26 19:54:12 +00006588 IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00006589
Douglas Gregor4d87df52008-12-16 21:30:33 +00006590 // DefArgToks is used when the parsing of default arguments needs
6591 // to be delayed.
Malcolm Parsonsff0382c2016-11-17 17:52:58 +00006592 std::unique_ptr<CachedTokens> DefArgToks;
Douglas Gregor4d87df52008-12-16 21:30:33 +00006593
Chris Lattner371ed4e2008-04-06 06:57:35 +00006594 // If no parameter was specified, verify that *something* was specified,
6595 // otherwise we have a missing type and identifier.
Craig Topper161e4db2014-05-21 06:02:52 +00006596 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == nullptr &&
Faisal Vali2b391ab2013-09-26 19:54:12 +00006597 ParmDeclarator.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00006598 // Completely missing, emit error.
6599 Diag(DSStart, diag::err_missing_param);
6600 } else {
6601 // Otherwise, we have something. Add it and let semantic analysis try
6602 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00006603
Richard Smith36ee9fb2014-08-11 23:30:23 +00006604 // Last chance to recover from a misplaced ellipsis in an attempted
6605 // parameter pack declaration.
6606 if (Tok.is(tok::ellipsis) &&
6607 (NextToken().isNot(tok::r_paren) ||
6608 (!ParmDeclarator.getEllipsisLoc().isValid() &&
6609 !Actions.isUnexpandedParameterPackPermitted())) &&
6610 Actions.containsUnexpandedParameterPacks(ParmDeclarator))
6611 DiagnoseMisplacedEllipsisInDeclarator(ConsumeToken(), ParmDeclarator);
6612
Chris Lattner371ed4e2008-04-06 06:57:35 +00006613 // Inform the actions module about the parameter declarator, so it gets
6614 // added to the current scope.
Richard Smith95e1fb02014-08-27 03:23:12 +00006615 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00006616 // Parse the default argument, if any. We parse the default
6617 // arguments in all dialects; the semantic analysis in
6618 // ActOnParamDefaultArgument will reject the default argument in
6619 // C.
6620 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00006621 SourceLocation EqualLoc = Tok.getLocation();
6622
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00006623 // Parse the default argument
Faisal Vali421b2d12017-12-29 05:41:00 +00006624 if (D.getContext() == DeclaratorContext::MemberContext) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00006625 // If we're inside a class definition, cache the tokens
6626 // corresponding to the default argument. We'll actually parse
6627 // them when we see the end of the class definition.
Malcolm Parsonsff0382c2016-11-17 17:52:58 +00006628 DefArgToks.reset(new CachedTokens);
Douglas Gregor4d87df52008-12-16 21:30:33 +00006629
David Majnemer906ed272015-01-13 05:28:24 +00006630 SourceLocation ArgStartLoc = NextToken().getLocation();
Richard Smith1fff95c2013-09-12 23:28:08 +00006631 if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
Malcolm Parsonsff0382c2016-11-17 17:52:58 +00006632 DefArgToks.reset();
Serge Pavlovb4b35782014-07-22 01:54:49 +00006633 Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00006634 } else {
Mike Stump11289f42009-09-09 15:08:12 +00006635 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
David Majnemer906ed272015-01-13 05:28:24 +00006636 ArgStartLoc);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00006637 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00006638 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00006639 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00006640 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00006641
Chad Rosierc1183952012-06-26 22:30:43 +00006642 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00006643 // used.
Faisal Valid143a0c2017-04-01 21:30:49 +00006644 EnterExpressionEvaluationContext Eval(
6645 Actions,
6646 Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed,
6647 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00006648
Sebastian Redldb63af22012-03-14 15:54:00 +00006649 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00006650 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00006651 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00006652 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00006653 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00006654 DefArgResult = ParseAssignmentExpression();
Kaelyn Takatab16e6322014-11-20 22:06:40 +00006655 DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult);
Douglas Gregor4d87df52008-12-16 21:30:33 +00006656 if (DefArgResult.isInvalid()) {
Serge Pavlovb4b35782014-07-22 01:54:49 +00006657 Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
Alexey Bataevee6507d2013-11-18 08:17:37 +00006658 SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor4d87df52008-12-16 21:30:33 +00006659 } else {
6660 // Inform the actions module about the default argument
6661 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006662 DefArgResult.get());
Douglas Gregor4d87df52008-12-16 21:30:33 +00006663 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00006664 }
6665 }
Mike Stump11289f42009-09-09 15:08:12 +00006666
6667 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Fangrui Song6907ce22018-07-30 19:24:48 +00006668 ParmDeclarator.getIdentifierLoc(),
Malcolm Parsonsff0382c2016-11-17 17:52:58 +00006669 Param, std::move(DefArgToks)));
Chris Lattner371ed4e2008-04-06 06:57:35 +00006670 }
6671
Richard Smith36ee9fb2014-08-11 23:30:23 +00006672 if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) {
6673 if (!getLangOpts().CPlusPlus) {
6674 // We have ellipsis without a preceding ',', which is ill-formed
6675 // in C. Complain and provide the fix.
6676 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
6677 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
6678 } else if (ParmDeclarator.getEllipsisLoc().isValid() ||
6679 Actions.containsUnexpandedParameterPacks(ParmDeclarator)) {
6680 // It looks like this was supposed to be a parameter pack. Warn and
6681 // point out where the ellipsis should have gone.
6682 SourceLocation ParmEllipsis = ParmDeclarator.getEllipsisLoc();
6683 Diag(EllipsisLoc, diag::warn_misplaced_ellipsis_vararg)
6684 << ParmEllipsis.isValid() << ParmEllipsis;
6685 if (ParmEllipsis.isValid()) {
6686 Diag(ParmEllipsis,
6687 diag::note_misplaced_ellipsis_vararg_existing_ellipsis);
6688 } else {
6689 Diag(ParmDeclarator.getIdentifierLoc(),
6690 diag::note_misplaced_ellipsis_vararg_add_ellipsis)
6691 << FixItHint::CreateInsertion(ParmDeclarator.getIdentifierLoc(),
6692 "...")
6693 << !ParmDeclarator.hasName();
6694 }
6695 Diag(EllipsisLoc, diag::note_misplaced_ellipsis_vararg_add_comma)
Alp Tokera3ebe6e2013-12-17 14:12:37 +00006696 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Richard Smith36ee9fb2014-08-11 23:30:23 +00006697 }
6698
6699 // We can't have any more parameters after an ellipsis.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00006700 break;
6701 }
Mike Stump11289f42009-09-09 15:08:12 +00006702
Alp Tokera3ebe6e2013-12-17 14:12:37 +00006703 // If the next token is a comma, consume it and keep reading arguments.
6704 } while (TryConsumeToken(tok::comma));
Chris Lattner6c940e62008-04-06 06:34:08 +00006705}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00006706
Chris Lattnere8074e62006-08-06 18:30:15 +00006707/// [C90] direct-declarator '[' constant-expression[opt] ']'
6708/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
6709/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
6710/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
6711/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006712/// [C++11] direct-declarator '[' constant-expression[opt] ']'
6713/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00006714void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00006715 if (CheckProhibitedCXX11Attribute())
6716 return;
6717
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006718 BalancedDelimiterTracker T(*this, tok::l_square);
6719 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00006720
Chris Lattner84a11622008-12-18 07:27:21 +00006721 // C array syntax has many features, but by-far the most common is [] and [4].
6722 // This code does a fast path to handle some of the most obvious cases.
6723 if (Tok.getKind() == tok::r_square) {
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);
Chad Rosierc1183952012-06-26 22:30:43 +00006727
Chris Lattner84a11622008-12-18 07:27:21 +00006728 // Remember that we parsed the empty array type.
Craig Topper161e4db2014-05-21 06:02:52 +00006729 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, nullptr,
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;
6734 } else if (Tok.getKind() == tok::numeric_constant &&
6735 GetLookAheadToken(1).is(tok::r_square)) {
6736 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00006737 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00006738 ConsumeToken();
6739
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006740 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00006741 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00006742 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00006743
Chris Lattner84a11622008-12-18 07:27:21 +00006744 // Remember that we parsed a array type, and remember its features.
Erich Keanec480f302018-07-12 21:09:05 +00006745 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, ExprRes.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006746 T.getOpenLocation(),
6747 T.getCloseLocation()),
Erich Keanec480f302018-07-12 21:09:05 +00006748 std::move(attrs), T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00006749 return;
Benjamin Kramer72dae622016-02-18 15:30:24 +00006750 } else if (Tok.getKind() == tok::code_completion) {
6751 Actions.CodeCompleteBracketDeclarator(getCurScope());
6752 return cutOffParsing();
Chris Lattner84a11622008-12-18 07:27:21 +00006753 }
Mike Stump11289f42009-09-09 15:08:12 +00006754
Chris Lattnere8074e62006-08-06 18:30:15 +00006755 // If valid, this location is the position where we read the 'static' keyword.
6756 SourceLocation StaticLoc;
Alp Tokera3ebe6e2013-12-17 14:12:37 +00006757 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00006758
Chris Lattnere8074e62006-08-06 18:30:15 +00006759 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00006760 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00006761 DeclSpec DS(AttrFactory);
Aaron Ballman08b06592014-07-22 12:44:22 +00006762 ParseTypeQualifierListOpt(DS, AR_CXX11AttributesParsed);
Mike Stump11289f42009-09-09 15:08:12 +00006763
Chris Lattnere8074e62006-08-06 18:30:15 +00006764 // If we haven't already read 'static', check to see if there is one after the
6765 // type-qualifier-list.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00006766 if (!StaticLoc.isValid())
6767 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00006768
Chris Lattnere8074e62006-08-06 18:30:15 +00006769 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00006770 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00006771 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00006772
Chris Lattner521ff2b2008-04-06 05:26:30 +00006773 // Handle the case where we have '[*]' as the array size. However, a leading
6774 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00006775 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00006776 // infrequent, use of lookahead is not costly here.
6777 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00006778 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00006779
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00006780 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00006781 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00006782 StaticLoc = SourceLocation(); // Drop the static.
6783 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00006784 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00006785 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00006786 // Note, in C89, this production uses the constant-expr production instead
6787 // of assignment-expr. The only difference is that assignment-expr allows
6788 // things like '=' and '*='. Sema rejects these in C89 mode because they
6789 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00006790
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00006791 // Parse the constant-expression or assignment-expression now (depending
6792 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00006793 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00006794 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00006795 } else {
Faisal Valid143a0c2017-04-01 21:30:49 +00006796 EnterExpressionEvaluationContext Unevaluated(
6797 Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
Kaelyn Takata15867822014-11-21 18:48:04 +00006798 NumElements =
6799 Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
Eli Friedmane0afc982012-01-21 01:01:51 +00006800 }
David Majnemerf9834d52014-08-08 07:21:18 +00006801 } else {
6802 if (StaticLoc.isValid()) {
6803 Diag(StaticLoc, diag::err_unspecified_size_with_static);
6804 StaticLoc = SourceLocation(); // Drop the static.
6805 }
Chris Lattner62591722006-08-12 18:40:58 +00006806 }
Mike Stump11289f42009-09-09 15:08:12 +00006807
Chris Lattner62591722006-08-12 18:40:58 +00006808 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00006809 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00006810 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00006811 // If the expression was invalid, skip it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00006812 SkipUntil(tok::r_square, StopAtSemi);
Chris Lattner62591722006-08-12 18:40:58 +00006813 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00006814 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00006815
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006816 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00006817
Jordan Rose303e2f12016-11-10 23:28:17 +00006818 MaybeParseCXX11Attributes(DS.getAttributes());
Alexis Hunt96d5c762009-11-21 08:43:09 +00006819
Chris Lattner84a11622008-12-18 07:27:21 +00006820 // Remember that we parsed a array type, and remember its features.
Erich Keanec480f302018-07-12 21:09:05 +00006821 D.AddTypeInfo(
6822 DeclaratorChunk::getArray(DS.getTypeQualifiers(), StaticLoc.isValid(),
6823 isStar, NumElements.get(), T.getOpenLocation(),
6824 T.getCloseLocation()),
6825 std::move(DS.getAttributes()), T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00006826}
6827
Richard Trieuf4b81d02014-06-24 23:14:24 +00006828/// Diagnose brackets before an identifier.
6829void Parser::ParseMisplacedBracketDeclarator(Declarator &D) {
6830 assert(Tok.is(tok::l_square) && "Missing opening bracket");
6831 assert(!D.mayOmitIdentifier() && "Declarator cannot omit identifier");
6832
6833 SourceLocation StartBracketLoc = Tok.getLocation();
6834 Declarator TempDeclarator(D.getDeclSpec(), D.getContext());
6835
6836 while (Tok.is(tok::l_square)) {
6837 ParseBracketDeclarator(TempDeclarator);
6838 }
6839
6840 // Stuff the location of the start of the brackets into the Declarator.
6841 // The diagnostics from ParseDirectDeclarator will make more sense if
6842 // they use this location instead.
6843 if (Tok.is(tok::semi))
6844 D.getName().EndLocation = StartBracketLoc;
6845
6846 SourceLocation SuggestParenLoc = Tok.getLocation();
6847
6848 // Now that the brackets are removed, try parsing the declarator again.
6849 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
6850
6851 // Something went wrong parsing the brackets, in which case,
6852 // ParseBracketDeclarator has emitted an error, and we don't need to emit
6853 // one here.
6854 if (TempDeclarator.getNumTypeObjects() == 0)
6855 return;
6856
6857 // Determine if parens will need to be suggested in the diagnostic.
6858 bool NeedParens = false;
6859 if (D.getNumTypeObjects() != 0) {
6860 switch (D.getTypeObject(D.getNumTypeObjects() - 1).Kind) {
6861 case DeclaratorChunk::Pointer:
6862 case DeclaratorChunk::Reference:
6863 case DeclaratorChunk::BlockPointer:
6864 case DeclaratorChunk::MemberPointer:
Xiuli Pan9c14e282016-01-09 12:53:17 +00006865 case DeclaratorChunk::Pipe:
Richard Trieuf4b81d02014-06-24 23:14:24 +00006866 NeedParens = true;
6867 break;
6868 case DeclaratorChunk::Array:
6869 case DeclaratorChunk::Function:
6870 case DeclaratorChunk::Paren:
6871 break;
6872 }
6873 }
6874
6875 if (NeedParens) {
6876 // Create a DeclaratorChunk for the inserted parens.
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006877 SourceLocation EndLoc = PP.getLocForEndOfToken(D.getEndLoc());
Erich Keanec480f302018-07-12 21:09:05 +00006878 D.AddTypeInfo(DeclaratorChunk::getParen(SuggestParenLoc, EndLoc),
Richard Trieuf4b81d02014-06-24 23:14:24 +00006879 SourceLocation());
6880 }
6881
6882 // Adding back the bracket info to the end of the Declarator.
6883 for (unsigned i = 0, e = TempDeclarator.getNumTypeObjects(); i < e; ++i) {
6884 const DeclaratorChunk &Chunk = TempDeclarator.getTypeObject(i);
Erich Keanec480f302018-07-12 21:09:05 +00006885 D.AddTypeInfo(Chunk, SourceLocation());
Richard Trieuf4b81d02014-06-24 23:14:24 +00006886 }
6887
6888 // The missing identifier would have been diagnosed in ParseDirectDeclarator.
6889 // If parentheses are required, always suggest them.
6890 if (!D.getIdentifier() && !NeedParens)
6891 return;
6892
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006893 SourceLocation EndBracketLoc = TempDeclarator.getEndLoc();
Richard Trieuf4b81d02014-06-24 23:14:24 +00006894
6895 // Generate the move bracket error message.
6896 SourceRange BracketRange(StartBracketLoc, EndBracketLoc);
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006897 SourceLocation EndLoc = PP.getLocForEndOfToken(D.getEndLoc());
Richard Trieuf4b81d02014-06-24 23:14:24 +00006898
6899 if (NeedParens) {
6900 Diag(EndLoc, diag::err_brackets_go_after_unqualified_id)
6901 << getLangOpts().CPlusPlus
6902 << FixItHint::CreateInsertion(SuggestParenLoc, "(")
6903 << FixItHint::CreateInsertion(EndLoc, ")")
6904 << FixItHint::CreateInsertionFromRange(
6905 EndLoc, CharSourceRange(BracketRange, true))
6906 << FixItHint::CreateRemoval(BracketRange);
6907 } else {
6908 Diag(EndLoc, diag::err_brackets_go_after_unqualified_id)
6909 << getLangOpts().CPlusPlus
6910 << FixItHint::CreateInsertionFromRange(
6911 EndLoc, CharSourceRange(BracketRange, true))
6912 << FixItHint::CreateRemoval(BracketRange);
6913 }
6914}
6915
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00006916/// [GNU] typeof-specifier:
6917/// typeof ( expressions )
6918/// typeof ( type-name )
6919/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00006920///
6921void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00006922 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006923 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00006924 SourceLocation StartLoc = ConsumeToken();
6925
John McCalle8595032010-01-13 20:03:27 +00006926 const bool hasParens = Tok.is(tok::l_paren);
6927
Faisal Valid143a0c2017-04-01 21:30:49 +00006928 EnterExpressionEvaluationContext Unevaluated(
6929 Actions, Sema::ExpressionEvaluationContext::Unevaluated,
6930 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00006931
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006932 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00006933 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006934 SourceRange CastRange;
Kaelyn Takata911260742014-12-19 01:28:40 +00006935 ExprResult Operand = Actions.CorrectDelayedTyposInExpr(
6936 ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, CastTy, CastRange));
John McCalle8595032010-01-13 20:03:27 +00006937 if (hasParens)
6938 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006939
6940 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00006941 // FIXME: Not accurate, the range gets one token more than it should.
6942 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006943 else
6944 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00006945
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006946 if (isCastExpr) {
6947 if (!CastTy) {
6948 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00006949 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00006950 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00006951
Craig Topper161e4db2014-05-21 06:02:52 +00006952 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +00006953 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006954 // Check for duplicate type specifiers (e.g. "int typeof(int)").
6955 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00006956 DiagID, CastTy,
6957 Actions.getASTContext().getPrintingPolicy()))
John McCall49bfce42009-08-03 20:12:06 +00006958 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00006959 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00006960 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00006961
Hiroshi Inoue533777f2017-07-02 06:12:49 +00006962 // If we get here, the operand to the typeof was an expression.
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00006963 if (Operand.isInvalid()) {
6964 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00006965 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00006966 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00006967
Eli Friedmane0afc982012-01-21 01:01:51 +00006968 // We might need to transform the operand if it is potentially evaluated.
6969 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
6970 if (Operand.isInvalid()) {
6971 DS.SetTypeSpecError();
6972 return;
6973 }
6974
Craig Topper161e4db2014-05-21 06:02:52 +00006975 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +00006976 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00006977 // Check for duplicate type specifiers (e.g. "int typeof(int)").
6978 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00006979 DiagID, Operand.get(),
6980 Actions.getASTContext().getPrintingPolicy()))
John McCall49bfce42009-08-03 20:12:06 +00006981 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00006982}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00006983
Benjamin Kramere56f3932011-12-23 17:00:35 +00006984/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00006985/// _Atomic ( type-name )
6986///
6987void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00006988 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
6989 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00006990
6991 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00006992 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00006993 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00006994 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00006995
6996 TypeResult Result = ParseTypeName();
6997 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00006998 SkipUntil(tok::r_paren, StopAtSemi);
Eli Friedman0dfb8892011-10-06 23:00:33 +00006999 return;
7000 }
7001
7002 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00007003 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00007004
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00007005 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00007006 return;
7007
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00007008 DS.setTypeofParensRange(T.getRange());
7009 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00007010
Craig Topper161e4db2014-05-21 06:02:52 +00007011 const char *PrevSpec = nullptr;
Eli Friedman0dfb8892011-10-06 23:00:33 +00007012 unsigned DiagID;
7013 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007014 DiagID, Result.get(),
Erik Verbruggen888d52a2014-01-15 09:15:43 +00007015 Actions.getASTContext().getPrintingPolicy()))
Eli Friedman0dfb8892011-10-06 23:00:33 +00007016 Diag(StartLoc, DiagID) << PrevSpec;
7017}
7018
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007019/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
7020/// from TryAltiVecVectorToken.
7021bool Parser::TryAltiVecVectorTokenOutOfLine() {
7022 Token Next = NextToken();
7023 switch (Next.getKind()) {
7024 default: return false;
7025 case tok::kw_short:
7026 case tok::kw_long:
7027 case tok::kw_signed:
7028 case tok::kw_unsigned:
7029 case tok::kw_void:
7030 case tok::kw_char:
7031 case tok::kw_int:
7032 case tok::kw_float:
7033 case tok::kw_double:
7034 case tok::kw_bool:
Bill Seurercf2c96b2015-01-12 19:35:51 +00007035 case tok::kw___bool:
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007036 case tok::kw___pixel:
7037 Tok.setKind(tok::kw___vector);
7038 return true;
7039 case tok::identifier:
7040 if (Next.getIdentifierInfo() == Ident_pixel) {
7041 Tok.setKind(tok::kw___vector);
7042 return true;
7043 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00007044 if (Next.getIdentifierInfo() == Ident_bool) {
7045 Tok.setKind(tok::kw___vector);
7046 return true;
7047 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007048 return false;
7049 }
7050}
7051
7052bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
7053 const char *&PrevSpec, unsigned &DiagID,
7054 bool &isInvalid) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00007055 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007056 if (Tok.getIdentifierInfo() == Ident_vector) {
7057 Token Next = NextToken();
7058 switch (Next.getKind()) {
7059 case tok::kw_short:
7060 case tok::kw_long:
7061 case tok::kw_signed:
7062 case tok::kw_unsigned:
7063 case tok::kw_void:
7064 case tok::kw_char:
7065 case tok::kw_int:
7066 case tok::kw_float:
7067 case tok::kw_double:
7068 case tok::kw_bool:
Bill Seurercf2c96b2015-01-12 19:35:51 +00007069 case tok::kw___bool:
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007070 case tok::kw___pixel:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00007071 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007072 return true;
7073 case tok::identifier:
7074 if (Next.getIdentifierInfo() == Ident_pixel) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00007075 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy);
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007076 return true;
7077 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00007078 if (Next.getIdentifierInfo() == Ident_bool) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00007079 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy);
Bill Schmidt99a084b2013-07-03 20:54:09 +00007080 return true;
7081 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007082 break;
7083 default:
7084 break;
7085 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00007086 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007087 DS.isTypeAltiVecVector()) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00007088 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007089 return true;
Bill Schmidt99a084b2013-07-03 20:54:09 +00007090 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
7091 DS.isTypeAltiVecVector()) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00007092 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy);
Bill Schmidt99a084b2013-07-03 20:54:09 +00007093 return true;
Chris Lattner73a9c7d2010-02-28 18:33:55 +00007094 }
7095 return false;
7096}