Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1 | //===--- ParseDecl.cpp - Declaration Parsing --------------------*- C++ -*-===// |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the Declaration portions of the Parser interfaces. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "clang/Parse/Parser.h" |
Vassil Vassilev | 11ad339 | 2017-03-23 15:11:07 +0000 | [diff] [blame] | 14 | #include "clang/Parse/RAIIObjectsForParser.h" |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Chandler Carruth | 757fcd6 | 2014-03-04 10:05:20 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclTemplate.h" |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 17 | #include "clang/AST/PrettyDeclStackTrace.h" |
Benjamin Kramer | d7d2b1f | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 18 | #include "clang/Basic/AddressSpaces.h" |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Attributes.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 20 | #include "clang/Basic/CharInfo.h" |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 21 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Parse/ParseDiagnostic.h" |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 23 | #include "clang/Sema/Lookup.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 24 | #include "clang/Sema/ParsedTemplate.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 25 | #include "clang/Sema/Scope.h" |
George Burgess IV | a19ea34 | 2016-11-10 20:43:52 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/Optional.h" |
Chris Lattner | ad9ac94 | 2007-01-23 01:14:52 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallSet.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallString.h" |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/StringSwitch.h" |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 30 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | // C99 6.7: Declarations. |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 37 | /// ParseTypeName |
| 38 | /// type-name: [C99 6.7.6] |
| 39 | /// specifier-qualifier-list abstract-declarator[opt] |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 40 | /// |
| 41 | /// Called type-id in C++. |
Douglas Gregor | 205d5e3 | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 42 | TypeResult Parser::ParseTypeName(SourceRange *Range, |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 43 | DeclaratorContext Context, |
Richard Smith | cd1c055 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 44 | AccessSpecifier AS, |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 45 | Decl **OwnedType, |
| 46 | ParsedAttributes *Attrs) { |
Richard Smith | 62dad82 | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 47 | DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context); |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 48 | if (DSC == DeclSpecContext::DSC_normal) |
| 49 | DSC = DeclSpecContext::DSC_type_specifier; |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 50 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 51 | // Parse the common declaration-specifiers piece. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 52 | DeclSpec DS(AttrFactory); |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 53 | if (Attrs) |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 54 | DS.addAttributes(*Attrs); |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 55 | ParseSpecifierQualifierList(DS, AS, DSC); |
Richard Smith | cd1c055 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 56 | if (OwnedType) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 57 | *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : nullptr; |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 58 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 59 | // Parse the abstract-declarator, if present. |
Douglas Gregor | 205d5e3 | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 60 | Declarator DeclaratorInfo(DS, Context); |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 61 | ParseDeclarator(DeclaratorInfo); |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 62 | if (Range) |
| 63 | *Range = DeclaratorInfo.getSourceRange(); |
| 64 | |
Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 65 | if (DeclaratorInfo.isInvalidType()) |
Douglas Gregor | 220cac5 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 66 | return true; |
| 67 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 68 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 71 | /// Normalizes an attribute name by dropping prefixed and suffixed __. |
George Burgess IV | 779af82 | 2017-06-30 22:33:24 +0000 | [diff] [blame] | 72 | static 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 Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 78 | /// isAttributeLateParsed - Return true if the attribute has arguments that |
| 79 | /// require late parsing. |
| 80 | static bool isAttributeLateParsed(const IdentifierInfo &II) { |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 81 | #define CLANG_ATTR_LATE_PARSED_LIST |
George Burgess IV | 779af82 | 2017-06-30 22:33:24 +0000 | [diff] [blame] | 82 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 83 | #include "clang/Parse/AttrParserStringSwitches.inc" |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 84 | .Default(false); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 85 | #undef CLANG_ATTR_LATE_PARSED_LIST |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Leonard Chan | c72aaf6 | 2019-05-07 03:20:17 +0000 | [diff] [blame] | 88 | /// Check if the a start and end source location expand to the same macro. |
Benjamin Kramer | dc5f805 | 2019-08-23 19:59:23 +0000 | [diff] [blame] | 89 | static bool FindLocsWithCommonFileID(Preprocessor &PP, SourceLocation StartLoc, |
| 90 | SourceLocation EndLoc) { |
Leonard Chan | c72aaf6 | 2019-05-07 03:20:17 +0000 | [diff] [blame] | 91 | if (!StartLoc.isMacroID() || !EndLoc.isMacroID()) |
| 92 | return false; |
| 93 | |
| 94 | SourceManager &SM = PP.getSourceManager(); |
| 95 | if (SM.getFileID(StartLoc) != SM.getFileID(EndLoc)) |
| 96 | return false; |
| 97 | |
| 98 | bool AttrStartIsInMacro = |
| 99 | Lexer::isAtStartOfMacroExpansion(StartLoc, SM, PP.getLangOpts()); |
| 100 | bool AttrEndIsInMacro = |
| 101 | Lexer::isAtEndOfMacroExpansion(EndLoc, SM, PP.getLangOpts()); |
| 102 | return AttrStartIsInMacro && AttrEndIsInMacro; |
| 103 | } |
| 104 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 105 | /// ParseGNUAttributes - Parse a non-empty attributes list. |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 106 | /// |
| 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 Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 120 | /// attrib-name |
| 121 | /// attrib-name '(' identifier ')' |
| 122 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 123 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 124 | /// |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 125 | /// [GNU] attrib-name: |
| 126 | /// identifier |
| 127 | /// typespec |
| 128 | /// typequal |
| 129 | /// storageclass |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | /// |
Richard Smith | f7ca0c0 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 131 | /// Whether an attribute takes an 'identifier' is determined by the |
| 132 | /// attrib-name. GCC's behavior here is not worth imitating: |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 133 | /// |
Richard Smith | f7ca0c0 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 134 | /// * 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 Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 144 | /// |
Richard Smith | f7ca0c0 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 145 | /// We follow the C++ model, but don't allow junk after the identifier. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 146 | void Parser::ParseGNUAttributes(ParsedAttributes &attrs, |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 147 | SourceLocation *endLoc, |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 148 | LateParsedAttrList *LateAttrs, |
| 149 | Declarator *D) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 150 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 152 | while (Tok.is(tok::kw___attribute)) { |
Leonard Chan | c72aaf6 | 2019-05-07 03:20:17 +0000 | [diff] [blame] | 153 | SourceLocation AttrTokLoc = ConsumeToken(); |
| 154 | unsigned OldNumAttrs = attrs.size(); |
| 155 | unsigned OldNumLateAttrs = LateAttrs ? LateAttrs->size() : 0; |
| 156 | |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 157 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 158 | "attribute")) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 159 | SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ; |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 160 | return; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 161 | } |
| 162 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 163 | SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ; |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 164 | return; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 165 | } |
| 166 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Aaron Ballman | 7a89909 | 2019-06-18 12:57:05 +0000 | [diff] [blame] | 167 | do { |
| 168 | // Eat preceeding commas to allow __attribute__((,,,foo)) |
| 169 | while (TryConsumeToken(tok::comma)) |
| 170 | ; |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 171 | |
| 172 | // Expect an identifier or declaration specifier (const, int, etc.) |
David Majnemer | 22fe771 | 2015-01-03 19:41:00 +0000 | [diff] [blame] | 173 | if (Tok.isAnnotation()) |
| 174 | break; |
| 175 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 176 | if (!AttrName) |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 177 | break; |
| 178 | |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 179 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 180 | |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 181 | if (Tok.isNot(tok::l_paren)) { |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 182 | attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 183 | ParsedAttr::AS_GNU); |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 184 | continue; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 185 | } |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 186 | |
| 187 | // Handle "parameterized" attributes |
| 188 | if (!LateAttrs || !isAttributeLateParsed(*AttrName)) { |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 189 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc, nullptr, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 190 | SourceLocation(), ParsedAttr::AS_GNU, D); |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 191 | 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 IV | c8b9537 | 2017-01-04 22:43:01 +0000 | [diff] [blame] | 204 | // 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 Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 210 | |
| 211 | Token Eof; |
| 212 | Eof.startToken(); |
| 213 | Eof.setLocation(Tok.getLocation()); |
| 214 | LA->Toks.push_back(Eof); |
Aaron Ballman | 7a89909 | 2019-06-18 12:57:05 +0000 | [diff] [blame] | 215 | } while (Tok.is(tok::comma)); |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 216 | |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 217 | if (ExpectAndConsume(tok::r_paren)) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 218 | SkipUntil(tok::r_paren, StopAtSemi); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 219 | SourceLocation Loc = Tok.getLocation(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 220 | if (ExpectAndConsume(tok::r_paren)) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 221 | SkipUntil(tok::r_paren, StopAtSemi); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 222 | if (endLoc) |
| 223 | *endLoc = Loc; |
Leonard Chan | c72aaf6 | 2019-05-07 03:20:17 +0000 | [diff] [blame] | 224 | |
| 225 | // If this was declared in a macro, attach the macro IdentifierInfo to the |
| 226 | // parsed attribute. |
Leonard Chan | 69aec05 | 2019-05-12 21:50:01 +0000 | [diff] [blame] | 227 | auto &SM = PP.getSourceManager(); |
| 228 | if (!SM.isWrittenInBuiltinFile(SM.getSpellingLoc(AttrTokLoc)) && |
| 229 | FindLocsWithCommonFileID(PP, AttrTokLoc, Loc)) { |
Leonard Chan | c72aaf6 | 2019-05-07 03:20:17 +0000 | [diff] [blame] | 230 | 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 Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 243 | } |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 244 | } |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 245 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 246 | /// Determine whether the given attribute has an identifier argument. |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 247 | static bool attributeHasIdentifierArg(const IdentifierInfo &II) { |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 248 | #define CLANG_ATTR_IDENTIFIER_ARG_LIST |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 249 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 250 | #include "clang/Parse/AttrParserStringSwitches.inc" |
Douglas Gregor | d2472d4 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 251 | .Default(false); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 252 | #undef CLANG_ATTR_IDENTIFIER_ARG_LIST |
Douglas Gregor | d2472d4 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 253 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 254 | |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 255 | /// Determine whether the given attribute has a variadic identifier argument. |
| 256 | static 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 Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 264 | /// Determine whether the given attribute treats kw_this as an identifier. |
| 265 | static 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 Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 273 | /// Determine whether the given attribute parses a type argument. |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 274 | static bool attributeIsTypeArgAttr(const IdentifierInfo &II) { |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 275 | #define CLANG_ATTR_TYPE_ARG_LIST |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 276 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 277 | #include "clang/Parse/AttrParserStringSwitches.inc" |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 278 | .Default(false); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 279 | #undef CLANG_ATTR_TYPE_ARG_LIST |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 282 | /// Determine whether the given attribute requires parsing its arguments |
Aaron Ballman | 15b27b9 | 2014-01-09 19:39:35 +0000 | [diff] [blame] | 283 | /// in an unevaluated context or not. |
| 284 | static bool attributeParsedArgsUnevaluated(const IdentifierInfo &II) { |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 285 | #define CLANG_ATTR_ARG_CONTEXT_LIST |
Aaron Ballman | 15b27b9 | 2014-01-09 19:39:35 +0000 | [diff] [blame] | 286 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 287 | #include "clang/Parse/AttrParserStringSwitches.inc" |
Aaron Ballman | 15b27b9 | 2014-01-09 19:39:35 +0000 | [diff] [blame] | 288 | .Default(false); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 289 | #undef CLANG_ATTR_ARG_CONTEXT_LIST |
Aaron Ballman | 15b27b9 | 2014-01-09 19:39:35 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 292 | IdentifierLoc *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 Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 301 | void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName, |
| 302 | SourceLocation AttrNameLoc, |
| 303 | ParsedAttributes &Attrs, |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 304 | SourceLocation *EndLoc, |
| 305 | IdentifierInfo *ScopeName, |
| 306 | SourceLocation ScopeLoc, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 307 | ParsedAttr::Syntax Syntax) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 308 | 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 Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 323 | SourceRange(AttrNameLoc, Parens.getCloseLocation()), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 324 | ScopeName, ScopeLoc, T.get(), Syntax); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 325 | else |
| 326 | Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 327 | ScopeName, ScopeLoc, nullptr, 0, Syntax); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Aaron Ballman | 35f9421 | 2014-04-14 16:03:22 +0000 | [diff] [blame] | 330 | unsigned Parser::ParseAttributeArgsCommon( |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 331 | IdentifierInfo *AttrName, SourceLocation AttrNameLoc, |
| 332 | ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 333 | SourceLocation ScopeLoc, ParsedAttr::Syntax Syntax) { |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 334 | // Ignore the left paren location for now. |
| 335 | ConsumeParen(); |
| 336 | |
Johannes Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 337 | bool ChangeKWThisToIdent = attributeTreatsKeywordThisAsIdentifier(*AttrName); |
Matthias Gehre | d293cbd | 2019-07-25 17:50:51 +0000 | [diff] [blame] | 338 | bool AttributeIsTypeArgAttr = attributeIsTypeArgAttr(*AttrName); |
Johannes Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 339 | |
| 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 Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 344 | ArgsVector ArgExprs; |
| 345 | if (Tok.is(tok::identifier)) { |
| 346 | // If this attribute wants an 'identifier' argument, make it so. |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 347 | bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName) || |
| 348 | attributeHasVariadicIdentifierArg(*AttrName); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 349 | ParsedAttr::Kind AttrKind = |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 350 | ParsedAttr::getParsedKind(AttrName, ScopeName, Syntax); |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 351 | |
| 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 Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 354 | if (AttrKind == ParsedAttr::UnknownAttribute || |
| 355 | AttrKind == ParsedAttr::IgnoredAttribute) { |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 356 | const Token &Next = NextToken(); |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 357 | IsIdentifierArg = Next.isOneOf(tok::r_paren, tok::comma); |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | if (IsIdentifierArg) |
| 361 | ArgExprs.push_back(ParseIdentifierLoc()); |
| 362 | } |
| 363 | |
Matthias Gehre | d293cbd | 2019-07-25 17:50:51 +0000 | [diff] [blame] | 364 | ParsedType TheParsedType; |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 365 | 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 Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 372 | // 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 Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 376 | ExprResult ArgExpr; |
Matthias Gehre | d293cbd | 2019-07-25 17:50:51 +0000 | [diff] [blame] | 377 | 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 Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 388 | 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 Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 395 | |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 396 | 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 Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 403 | } |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 404 | // 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 Gehre | d293cbd | 2019-07-25 17:50:51 +0000 | [diff] [blame] | 411 | |
| 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 Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | if (EndLoc) |
| 422 | *EndLoc = RParen; |
Aaron Ballman | 35f9421 | 2014-04-14 16:03:22 +0000 | [diff] [blame] | 423 | |
Matthias Gehre | d293cbd | 2019-07-25 17:50:51 +0000 | [diff] [blame] | 424 | return static_cast<unsigned>(ArgExprs.size() + !TheParsedType.get().isNull()); |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 427 | /// Parse the arguments to a parameterized GNU attribute or |
| 428 | /// a C++11 attribute in "gnu" namespace. |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 429 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 430 | SourceLocation AttrNameLoc, |
| 431 | ParsedAttributes &Attrs, |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 432 | SourceLocation *EndLoc, |
| 433 | IdentifierInfo *ScopeName, |
| 434 | SourceLocation ScopeLoc, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 435 | ParsedAttr::Syntax Syntax, |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 436 | Declarator *D) { |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 437 | |
| 438 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 439 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 440 | ParsedAttr::Kind AttrKind = |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 441 | ParsedAttr::getParsedKind(AttrName, ScopeName, Syntax); |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 442 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 443 | if (AttrKind == ParsedAttr::AT_Availability) { |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 444 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName, |
| 445 | ScopeLoc, Syntax); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 446 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 447 | } else if (AttrKind == ParsedAttr::AT_ExternalSourceSymbol) { |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 448 | ParseExternalSourceSymbolAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, |
| 449 | ScopeName, ScopeLoc, Syntax); |
| 450 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 451 | } else if (AttrKind == ParsedAttr::AT_ObjCBridgeRelated) { |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 452 | ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, |
| 453 | ScopeName, ScopeLoc, Syntax); |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 454 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 455 | } else if (AttrKind == ParsedAttr::AT_TypeTagForDatatype) { |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 456 | ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, |
| 457 | ScopeName, ScopeLoc, Syntax); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 458 | return; |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 459 | } else if (attributeIsTypeArgAttr(*AttrName)) { |
| 460 | ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName, |
| 461 | ScopeLoc, Syntax); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 462 | return; |
| 463 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 464 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 465 | // 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 IV | a19ea34 | 2016-11-10 20:43:52 +0000 | [diff] [blame] | 467 | llvm::Optional<ParseScope> PrototypeScope; |
Ulrich Weigand | ef5aa29 | 2015-07-13 14:13:01 +0000 | [diff] [blame] | 468 | if (normalizeAttrName(AttrName->getName()) == "enable_if" && |
| 469 | D && D->isFunctionDeclarator()) { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 470 | DeclaratorChunk::FunctionTypeInfo FTI = D->getFunctionTypeInfo(); |
George Burgess IV | a19ea34 | 2016-11-10 20:43:52 +0000 | [diff] [blame] | 471 | PrototypeScope.emplace(this, Scope::FunctionPrototypeScope | |
| 472 | Scope::FunctionDeclarationScope | |
| 473 | Scope::DeclScope); |
Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 474 | for (unsigned i = 0; i != FTI.NumParams; ++i) { |
| 475 | ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 476 | Actions.ActOnReenterCXXMethodParameter(getCurScope(), Param); |
| 477 | } |
| 478 | } |
| 479 | |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 480 | ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName, |
| 481 | ScopeLoc, Syntax); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 484 | unsigned Parser::ParseClangAttributeArgs( |
| 485 | IdentifierInfo *AttrName, SourceLocation AttrNameLoc, |
| 486 | ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 487 | SourceLocation ScopeLoc, ParsedAttr::Syntax Syntax) { |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 488 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 489 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 490 | ParsedAttr::Kind AttrKind = |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 491 | ParsedAttr::getParsedKind(AttrName, ScopeName, Syntax); |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 492 | |
Aaron Ballman | 38bbc16 | 2018-02-24 17:16:42 +0000 | [diff] [blame] | 493 | switch (AttrKind) { |
| 494 | default: |
| 495 | return ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, |
| 496 | ScopeName, ScopeLoc, Syntax); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 497 | case ParsedAttr::AT_ExternalSourceSymbol: |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 498 | ParseExternalSourceSymbolAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, |
| 499 | ScopeName, ScopeLoc, Syntax); |
Aaron Ballman | 38bbc16 | 2018-02-24 17:16:42 +0000 | [diff] [blame] | 500 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 501 | case ParsedAttr::AT_Availability: |
Aaron Ballman | 38bbc16 | 2018-02-24 17:16:42 +0000 | [diff] [blame] | 502 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName, |
| 503 | ScopeLoc, Syntax); |
| 504 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 505 | case ParsedAttr::AT_ObjCBridgeRelated: |
Aaron Ballman | c248b0f | 2018-02-24 17:37:37 +0000 | [diff] [blame] | 506 | ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, |
| 507 | ScopeName, ScopeLoc, Syntax); |
| 508 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 509 | case ParsedAttr::AT_TypeTagForDatatype: |
Aaron Ballman | a26d8ee | 2018-02-25 14:01:04 +0000 | [diff] [blame] | 510 | ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, |
| 511 | ScopeName, ScopeLoc, Syntax); |
| 512 | break; |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 513 | } |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 514 | return !Attrs.empty() ? Attrs.begin()->getNumArgs() : 0; |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 517 | bool 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 Wilson | 7c73083 | 2015-07-20 22:57:31 +0000 | [diff] [blame] | 523 | getTargetInfo(), getLangOpts())) { |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 524 | // Eat the left paren, then skip to the ending right paren. |
| 525 | ConsumeParen(); |
| 526 | SkipUntil(tok::r_paren); |
| 527 | return false; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 528 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 529 | |
Aaron Ballman | 95d5703 | 2014-04-14 16:44:26 +0000 | [diff] [blame] | 530 | SourceLocation OpenParenLoc = Tok.getLocation(); |
| 531 | |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 532 | if (AttrName->getName() == "property") { |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 533 | // The property declspec is more complex in that it can take one or two |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 534 | // assignment expressions as a parameter, but the lhs of the assignment |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 535 | // must be named get or put. |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 536 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 537 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 538 | T.expectAndConsume(diag::err_expected_lparen_after, |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 539 | AttrName->getNameStart(), tok::r_paren); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 540 | |
| 541 | enum AccessorKind { |
| 542 | AK_Invalid = -1, |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 543 | AK_Put = 0, |
| 544 | AK_Get = 1 // indices into AccessorNames |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 545 | }; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 546 | IdentifierInfo *AccessorNames[] = {nullptr, nullptr}; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 547 | 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 Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 555 | AccessorNames[AK_Put] == nullptr && |
| 556 | AccessorNames[AK_Get] == nullptr) { |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 557 | Diag(AttrNameLoc, diag::err_ms_property_no_getter_or_putter); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 558 | 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 Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 573 | // Recover from the common mistake of using 'set' instead of 'put'. |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 574 | } else if (KindStr == "set") { |
| 575 | Diag(KindLoc, diag::err_ms_property_has_set_accessor) |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 576 | << FixItHint::CreateReplacement(KindLoc, "put"); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 577 | Kind = AK_Put; |
| 578 | |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 579 | // Handle the mistake of forgetting the accessor kind by skipping |
| 580 | // this accessor. |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 581 | } 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 Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 587 | // Otherwise, complain about the unknown accessor kind. |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 588 | } 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 Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 594 | if (!NextToken().is(tok::equal)) |
| 595 | break; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | // Consume the identifier. |
| 599 | ConsumeToken(); |
| 600 | |
| 601 | // Consume the '='. |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 602 | if (!TryConsumeToken(tok::equal)) { |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 603 | Diag(Tok.getLocation(), diag::err_ms_property_expected_equal) |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 604 | << KindStr; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 605 | 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 Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 616 | } else if (AccessorNames[Kind] != nullptr) { |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 617 | // 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 Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 626 | if (TryConsumeToken(tok::comma)) |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 627 | continue; |
| 628 | |
| 629 | // If we run into the ')', stop without consuming it. |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 630 | if (Tok.is(tok::r_paren)) |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 631 | break; |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 632 | |
| 633 | Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen); |
| 634 | break; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | // Only add the property attribute if it was well-formed. |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 638 | if (!HasInvalidAccessor) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 639 | Attrs.addNewPropertyAttr(AttrName, AttrNameLoc, nullptr, SourceLocation(), |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 640 | AccessorNames[AK_Get], AccessorNames[AK_Put], |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 641 | ParsedAttr::AS_Declspec); |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 642 | T.skipToEnd(); |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 643 | return !HasInvalidAccessor; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 644 | } |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 645 | |
Aaron Ballman | 95d5703 | 2014-04-14 16:44:26 +0000 | [diff] [blame] | 646 | unsigned NumArgs = |
| 647 | ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, nullptr, nullptr, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 648 | SourceLocation(), ParsedAttr::AS_Declspec); |
Aaron Ballman | 95d5703 | 2014-04-14 16:44:26 +0000 | [diff] [blame] | 649 | |
| 650 | // If this attribute's args were parsed, and it was expected to have |
| 651 | // arguments but none were provided, emit a diagnostic. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 652 | if (!Attrs.empty() && Attrs.begin()->getMaxArgs() && !NumArgs) { |
Aaron Ballman | ef5d94c | 2014-04-15 00:36:39 +0000 | [diff] [blame] | 653 | Diag(OpenParenLoc, diag::err_attribute_requires_arguments) << AttrName; |
Aaron Ballman | 95d5703 | 2014-04-14 16:44:26 +0000 | [diff] [blame] | 654 | return false; |
| 655 | } |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 656 | return true; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Eli Friedman | 06de2b5 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 659 | /// [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 Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 665 | void Parser::ParseMicrosoftDeclSpecs(ParsedAttributes &Attrs, |
| 666 | SourceLocation *End) { |
Saleem Abdulrasool | d170c4b | 2015-10-04 17:51:05 +0000 | [diff] [blame] | 667 | assert(getLangOpts().DeclSpecKeyword && "__declspec keyword is not enabled"); |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 668 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | 06de2b5 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 669 | |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 670 | 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 Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 675 | return; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 676 | |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 677 | // 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 Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 690 | T.skipToEnd(); |
| 691 | return; |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 692 | } |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 693 | |
| 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 Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 723 | ParsedAttr::AS_Declspec); |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 724 | } |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 725 | T.consumeClose(); |
| 726 | if (End) |
| 727 | *End = T.getCloseLocation(); |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 728 | } |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 729 | } |
| 730 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 731 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 732 | // Treat these like attributes |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 733 | while (true) { |
| 734 | switch (Tok.getKind()) { |
| 735 | case tok::kw___fastcall: |
| 736 | case tok::kw___stdcall: |
| 737 | case tok::kw___thiscall: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 738 | case tok::kw___regcall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 739 | 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 Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 744 | 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 Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 749 | ParsedAttr::AS_Keyword); |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 750 | break; |
| 751 | } |
| 752 | default: |
| 753 | return; |
| 754 | } |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 755 | } |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Nico Rieck | eaaae27 | 2014-12-04 23:31:08 +0000 | [diff] [blame] | 758 | void 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 | |
| 768 | SourceLocation 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 McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 794 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 795 | // Treat these like attributes |
| 796 | while (Tok.is(tok::kw___pascal)) { |
| 797 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 798 | SourceLocation AttrNameLoc = ConsumeToken(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 799 | attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 800 | ParsedAttr::AS_Keyword); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 801 | } |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Anastasia Stulova | 6bdbcbb | 2016-02-19 18:30:11 +0000 | [diff] [blame] | 804 | void Parser::ParseOpenCLKernelAttributes(ParsedAttributes &attrs) { |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 805 | // Treat these like attributes |
| 806 | while (Tok.is(tok::kw___kernel)) { |
Richard Smith | 0cdcc98 | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 807 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 808 | SourceLocation AttrNameLoc = ConsumeToken(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 809 | attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 810 | ParsedAttr::AS_Keyword); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | |
Aaron Ballman | 05d76ea | 2014-01-14 01:29:54 +0000 | [diff] [blame] | 814 | void Parser::ParseOpenCLQualifiers(ParsedAttributes &Attrs) { |
| 815 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 816 | SourceLocation AttrNameLoc = Tok.getLocation(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 817 | Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 818 | ParsedAttr::AS_Keyword); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 821 | void Parser::ParseNullabilityTypeSpecifiers(ParsedAttributes &attrs) { |
| 822 | // Treat these like attributes, even though they're type specifiers. |
| 823 | while (true) { |
| 824 | switch (Tok.getKind()) { |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 825 | case tok::kw__Nonnull: |
| 826 | case tok::kw__Nullable: |
| 827 | case tok::kw__Null_unspecified: { |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 828 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 829 | SourceLocation AttrNameLoc = ConsumeToken(); |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 830 | if (!getLangOpts().ObjC) |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 831 | Diag(AttrNameLoc, diag::ext_nullability) |
| 832 | << AttrName; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 833 | attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 834 | ParsedAttr::AS_Keyword); |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 835 | break; |
| 836 | } |
| 837 | default: |
| 838 | return; |
| 839 | } |
| 840 | } |
| 841 | } |
| 842 | |
Fariborz Jahanian | dbc956d | 2014-10-02 16:39:45 +0000 | [diff] [blame] | 843 | static bool VersionNumberSeparator(const char Separator) { |
| 844 | return (Separator == '.' || Separator == '_'); |
| 845 | } |
| 846 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 847 | /// Parse a version number. |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 848 | /// |
| 849 | /// version: |
| 850 | /// simple-integer |
Jan Korous | 2325569 | 2018-05-17 11:51:49 +0000 | [diff] [blame] | 851 | /// simple-integer '.' simple-integer |
| 852 | /// simple-integer '_' simple-integer |
| 853 | /// simple-integer '.' simple-integer '.' simple-integer |
| 854 | /// simple-integer '_' simple-integer '_' simple-integer |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 855 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
Erik Pilkington | 29099de | 2016-07-16 00:35:23 +0000 | [diff] [blame] | 856 | Range = SourceRange(Tok.getLocation(), Tok.getEndLoc()); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 857 | |
| 858 | if (!Tok.is(tok::numeric_constant)) { |
| 859 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 860 | SkipUntil(tok::comma, tok::r_paren, |
| 861 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 862 | 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 Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 869 | SmallString<512> Buffer; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 870 | 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 Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 882 | while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 883 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 884 | ++AfterMajor; |
| 885 | } |
| 886 | |
| 887 | if (AfterMajor == 0) { |
| 888 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 889 | SkipUntil(tok::comma, tok::r_paren, |
| 890 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 891 | 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 Jahanian | ce72e63 | 2014-10-02 17:57:26 +0000 | [diff] [blame] | 906 | const char AfterMajorSeparator = ThisTokBegin[AfterMajor]; |
| 907 | if (!VersionNumberSeparator(AfterMajorSeparator) |
Fariborz Jahanian | dbc956d | 2014-10-02 16:39:45 +0000 | [diff] [blame] | 908 | || (AfterMajor + 1 == ActualLength)) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 909 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 910 | SkipUntil(tok::comma, tok::r_paren, |
| 911 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 912 | return VersionTuple(); |
| 913 | } |
| 914 | |
| 915 | // Parse the minor version. |
| 916 | unsigned AfterMinor = AfterMajor + 1; |
| 917 | unsigned Minor = 0; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 918 | while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 919 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 920 | ++AfterMinor; |
| 921 | } |
| 922 | |
| 923 | if (AfterMinor == ActualLength) { |
| 924 | ConsumeToken(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 925 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 926 | // We had major.minor. |
| 927 | if (Major == 0 && Minor == 0) { |
| 928 | Diag(Tok, diag::err_zero_version); |
| 929 | return VersionTuple(); |
| 930 | } |
| 931 | |
Jan Korous | 2325569 | 2018-05-17 11:51:49 +0000 | [diff] [blame] | 932 | return VersionTuple(Major, Minor); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Fariborz Jahanian | ce72e63 | 2014-10-02 17:57:26 +0000 | [diff] [blame] | 935 | const char AfterMinorSeparator = ThisTokBegin[AfterMinor]; |
Fariborz Jahanian | dbc956d | 2014-10-02 16:39:45 +0000 | [diff] [blame] | 936 | // If what follows is not a '.' or '_', we have a problem. |
Fariborz Jahanian | ce72e63 | 2014-10-02 17:57:26 +0000 | [diff] [blame] | 937 | if (!VersionNumberSeparator(AfterMinorSeparator)) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 938 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 939 | SkipUntil(tok::comma, tok::r_paren, |
| 940 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 941 | return VersionTuple(); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 942 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 943 | |
Fariborz Jahanian | b616161 | 2014-10-03 17:21:12 +0000 | [diff] [blame] | 944 | // Warn if separators, be it '.' or '_', do not match. |
Fariborz Jahanian | ce72e63 | 2014-10-02 17:57:26 +0000 | [diff] [blame] | 945 | if (AfterMajorSeparator != AfterMinorSeparator) |
| 946 | Diag(Tok, diag::warn_expected_consistent_version_separator); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 947 | |
| 948 | // Parse the subminor version. |
| 949 | unsigned AfterSubminor = AfterMinor + 1; |
| 950 | unsigned Subminor = 0; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 951 | while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 952 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 953 | ++AfterSubminor; |
| 954 | } |
| 955 | |
| 956 | if (AfterSubminor != ActualLength) { |
| 957 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 958 | SkipUntil(tok::comma, tok::r_paren, |
| 959 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 960 | return VersionTuple(); |
| 961 | } |
| 962 | ConsumeToken(); |
Jan Korous | 2325569 | 2018-05-17 11:51:49 +0000 | [diff] [blame] | 963 | return VersionTuple(Major, Minor, Subminor); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 966 | /// Parse the contents of the "availability" attribute. |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 967 | /// |
| 968 | /// availability-attribute: |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 969 | /// 'availability' '(' platform ',' opt-strict version-arg-list, |
| 970 | /// opt-replacement, opt-message')' |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 971 | /// |
| 972 | /// platform: |
| 973 | /// identifier |
| 974 | /// |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 975 | /// opt-strict: |
| 976 | /// 'strict' ',' |
Manman Ren | b636b90 | 2016-02-17 22:05:48 +0000 | [diff] [blame] | 977 | /// |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 978 | /// version-arg-list: |
| 979 | /// version-arg |
| 980 | /// version-arg ',' version-arg-list |
| 981 | /// |
| 982 | /// version-arg: |
| 983 | /// 'introduced' '=' version |
| 984 | /// 'deprecated' '=' version |
Douglas Gregor | fdd417f | 2012-03-11 04:53:21 +0000 | [diff] [blame] | 985 | /// 'obsoleted' = version |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 986 | /// 'unavailable' |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 987 | /// opt-replacement: |
| 988 | /// 'replacement' '=' <string> |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 989 | /// opt-message: |
| 990 | /// 'message' '=' <string> |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 991 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 992 | SourceLocation AvailabilityLoc, |
| 993 | ParsedAttributes &attrs, |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 994 | SourceLocation *endLoc, |
| 995 | IdentifierInfo *ScopeName, |
| 996 | SourceLocation ScopeLoc, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 997 | ParsedAttr::Syntax Syntax) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 998 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 999 | AvailabilityChange Changes[Unknown]; |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 1000 | ExprResult MessageExpr, ReplacementExpr; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1001 | |
| 1002 | // Opening '('. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1003 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1004 | if (T.consumeOpen()) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1005 | Diag(Tok, diag::err_expected) << tok::l_paren; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1006 | return; |
| 1007 | } |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1008 | |
Manman Ren | b636b90 | 2016-02-17 22:05:48 +0000 | [diff] [blame] | 1009 | // Parse the platform name. |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1010 | if (Tok.isNot(tok::identifier)) { |
| 1011 | Diag(Tok, diag::err_availability_expected_platform); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1012 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1013 | return; |
| 1014 | } |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 1015 | IdentifierLoc *Platform = ParseIdentifierLoc(); |
Alex Lorenz | 0b1ce8b | 2017-08-15 14:42:01 +0000 | [diff] [blame] | 1016 | 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 Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1028 | |
| 1029 | // Parse the ',' following the platform name. |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 1030 | if (ExpectAndConsume(tok::comma)) { |
| 1031 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1032 | return; |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 1033 | } |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1034 | |
| 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 Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1041 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1042 | Ident_message = PP.getIdentifierInfo("message"); |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 1043 | Ident_strict = PP.getIdentifierInfo("strict"); |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 1044 | Ident_replacement = PP.getIdentifierInfo("replacement"); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 1047 | // Parse the optional "strict", the optional "replacement" and the set of |
Manman Ren | b636b90 | 2016-02-17 22:05:48 +0000 | [diff] [blame] | 1048 | // introductions/deprecations/removals. |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 1049 | SourceLocation UnavailableLoc, StrictLoc; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1050 | do { |
| 1051 | if (Tok.isNot(tok::identifier)) { |
| 1052 | Diag(Tok, diag::err_availability_expected_change); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1053 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1054 | return; |
| 1055 | } |
| 1056 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 1057 | SourceLocation KeywordLoc = ConsumeToken(); |
| 1058 | |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 1059 | if (Keyword == Ident_strict) { |
| 1060 | if (StrictLoc.isValid()) { |
Manman Ren | b636b90 | 2016-02-17 22:05:48 +0000 | [diff] [blame] | 1061 | Diag(KeywordLoc, diag::err_availability_redundant) |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 1062 | << Keyword << SourceRange(StrictLoc); |
Manman Ren | b636b90 | 2016-02-17 22:05:48 +0000 | [diff] [blame] | 1063 | } |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 1064 | StrictLoc = KeywordLoc; |
Manman Ren | b636b90 | 2016-02-17 22:05:48 +0000 | [diff] [blame] | 1065 | continue; |
| 1066 | } |
| 1067 | |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1068 | if (Keyword == Ident_unavailable) { |
| 1069 | if (UnavailableLoc.isValid()) { |
| 1070 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 1071 | << Keyword << SourceRange(UnavailableLoc); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1072 | } |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1073 | UnavailableLoc = KeywordLoc; |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 1074 | continue; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Michael Wu | 260e962 | 2018-11-12 02:44:33 +0000 | [diff] [blame] | 1077 | 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 Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1092 | if (Tok.isNot(tok::equal)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1093 | Diag(Tok, diag::err_expected_after) << Keyword << tok::equal; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1094 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1095 | return; |
| 1096 | } |
| 1097 | ConsumeToken(); |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 1098 | if (Keyword == Ident_message || Keyword == Ident_replacement) { |
David Majnemer | 2e49830 | 2014-07-18 05:43:12 +0000 | [diff] [blame] | 1099 | if (Tok.isNot(tok::string_literal)) { |
Andy Gibbs | a8df57a | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 1100 | Diag(Tok, diag::err_expected_string_literal) |
| 1101 | << /*Source='availability attribute'*/2; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1102 | SkipUntil(tok::r_paren, StopAtSemi); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1103 | return; |
| 1104 | } |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 1105 | if (Keyword == Ident_message) |
| 1106 | MessageExpr = ParseStringLiteralExpression(); |
| 1107 | else |
| 1108 | ReplacementExpr = ParseStringLiteralExpression(); |
David Majnemer | 2e49830 | 2014-07-18 05:43:12 +0000 | [diff] [blame] | 1109 | // 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 Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 1120 | if (Keyword == Ident_message) |
| 1121 | break; |
| 1122 | else |
| 1123 | continue; |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1124 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1125 | |
Fariborz Jahanian | 5a29e6a | 2014-11-05 23:58:55 +0000 | [diff] [blame] | 1126 | // 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 Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1138 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1139 | SourceRange VersionRange; |
| 1140 | VersionTuple Version = ParseVersionTuple(VersionRange); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1141 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1142 | if (Version.empty()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1143 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1144 | 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 Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1154 | else |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1155 | Index = Unknown; |
| 1156 | |
| 1157 | if (Index < Unknown) { |
| 1158 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 1159 | Diag(KeywordLoc, diag::err_availability_redundant) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1160 | << Keyword |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1161 | << 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 Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 1173 | } while (TryConsumeToken(tok::comma)); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1174 | |
| 1175 | // Closing ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1176 | if (T.consumeClose()) |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1177 | return; |
| 1178 | |
| 1179 | if (endLoc) |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1180 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1181 | |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1182 | // 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 Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1201 | // Record this attribute |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1202 | attrs.addNew(&Availability, |
| 1203 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1204 | ScopeName, ScopeLoc, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1205 | Platform, |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1206 | Changes[Introduced], |
| 1207 | Changes[Deprecated], |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1208 | Changes[Obsoleted], |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1209 | UnavailableLoc, MessageExpr.get(), |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 1210 | Syntax, StrictLoc, ReplacementExpr.get()); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1213 | /// Parse the contents of the "external_source_symbol" attribute. |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 1214 | /// |
| 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' |
| 1226 | void Parser::ParseExternalSourceSymbolAttribute( |
| 1227 | IdentifierInfo &ExternalSourceSymbol, SourceLocation Loc, |
| 1228 | ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1229 | SourceLocation ScopeLoc, ParsedAttr::Syntax Syntax) { |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 1230 | // 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 Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1326 | /// Parse the contents of the "objc_bridge_related" attribute. |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1327 | /// 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 | /// |
| 1337 | void Parser::ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated, |
| 1338 | SourceLocation ObjCBridgeRelatedLoc, |
| 1339 | ParsedAttributes &attrs, |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1340 | SourceLocation *endLoc, |
| 1341 | IdentifierInfo *ScopeName, |
| 1342 | SourceLocation ScopeLoc, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1343 | ParsedAttr::Syntax Syntax) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1344 | // Opening '('. |
| 1345 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1346 | if (T.consumeOpen()) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1347 | Diag(Tok, diag::err_expected) << tok::l_paren; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1348 | return; |
| 1349 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1350 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1351 | // 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 Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 1358 | if (ExpectAndConsume(tok::comma)) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1359 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1360 | return; |
| 1361 | } |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1362 | |
Aaron Ballman | 48a533d | 2018-02-27 23:49:28 +0000 | [diff] [blame] | 1363 | // 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 Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1366 | IdentifierLoc *ClassMethod = nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1367 | if (Tok.is(tok::identifier)) { |
| 1368 | ClassMethod = ParseIdentifierLoc(); |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1369 | if (!TryConsumeToken(tok::colon)) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1370 | Diag(Tok, diag::err_objcbridge_related_selector_name); |
| 1371 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1372 | return; |
| 1373 | } |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1374 | } |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1375 | if (!TryConsumeToken(tok::comma)) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1376 | if (Tok.is(tok::colon)) |
| 1377 | Diag(Tok, diag::err_objcbridge_related_selector_name); |
| 1378 | else |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1379 | Diag(Tok, diag::err_expected) << tok::comma; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1380 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1381 | return; |
| 1382 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1383 | |
Aaron Ballman | 48a533d | 2018-02-27 23:49:28 +0000 | [diff] [blame] | 1384 | // Parse instance method name. Also non-optional but empty string is |
| 1385 | // permitted. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1386 | IdentifierLoc *InstanceMethod = nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1387 | if (Tok.is(tok::identifier)) |
| 1388 | InstanceMethod = ParseIdentifierLoc(); |
| 1389 | else if (Tok.isNot(tok::r_paren)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1390 | Diag(Tok, diag::err_expected) << tok::r_paren; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1391 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1392 | return; |
| 1393 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1394 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1395 | // Closing ')'. |
| 1396 | if (T.consumeClose()) |
| 1397 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1398 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1399 | if (endLoc) |
| 1400 | *endLoc = T.getCloseLocation(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1401 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1402 | // Record this attribute |
| 1403 | attrs.addNew(&ObjCBridgeRelated, |
| 1404 | SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1405 | ScopeName, ScopeLoc, |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1406 | RelatedClass, |
| 1407 | ClassMethod, |
| 1408 | InstanceMethod, |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1409 | Syntax); |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1410 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1411 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1412 | // Late Parsed Attributes: |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1413 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 1414 | |
| 1415 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 1416 | |
| 1417 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 1418 | Self->ParseLexedAttributes(*Class); |
| 1419 | } |
| 1420 | |
| 1421 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1422 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 1426 | /// scope appropriately. |
| 1427 | void 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 Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1436 | // Set or update the scope flags. |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1437 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1438 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1439 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 1440 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 1441 | |
DeLesley Hutchins | 6f86004 | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1442 | // Enter the scope of nested classes |
| 1443 | if (!AlreadyHasClassScope) |
| 1444 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 1445 | Class.TagOrTemplate); |
Benjamin Kramer | 1d373c6 | 2012-05-17 12:01:52 +0000 | [diff] [blame] | 1446 | if (!Class.LateParsedDeclarations.empty()) { |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1447 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){ |
| 1448 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 1449 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1450 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1451 | |
DeLesley Hutchins | 6f86004 | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1452 | if (!AlreadyHasClassScope) |
| 1453 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 1454 | Class.TagOrTemplate); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1457 | /// Parse all attributes in LAs, and attach them to Decl D. |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1458 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 1459 | bool EnterScope, bool OnDefinition) { |
DeLesley Hutchins | 66e300e | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1460 | assert(LAs.parseSoon() && |
| 1461 | "Attribute list should be marked for immediate parsing."); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1462 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 19c722d | 2012-08-15 22:41:04 +0000 | [diff] [blame] | 1463 | if (D) |
| 1464 | LAs[i]->addDecl(D); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1465 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
Benjamin Kramer | bafc49a | 2012-04-14 12:44:47 +0000 | [diff] [blame] | 1466 | delete LAs[i]; |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1467 | } |
| 1468 | LAs.clear(); |
| 1469 | } |
| 1470 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1471 | /// Finish parsing an attribute for which parsing was delayed. |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1472 | /// This will be called at the end of parsing a class declaration |
| 1473 | /// for each LateParsedAttribute. We consume the saved tokens and |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1474 | /// create an attribute with the arguments filled in. We add this |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1475 | /// to the Attribute list for the decl. |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1476 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 1477 | bool EnterScope, bool OnDefinition) { |
David Majnemer | d5946f5 | 2015-01-13 08:35:24 +0000 | [diff] [blame] | 1478 | // 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 Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1486 | |
| 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 Biryukov | 929af67 | 2019-05-17 09:32:05 +0000 | [diff] [blame] | 1490 | PP.EnterTokenStream(LA.Toks, true, /*IsReinject=*/true); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1491 | // Consume the previously pushed token. |
Argyrios Kyrtzidis | c36633c | 2013-03-27 23:58:17 +0000 | [diff] [blame] | 1492 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1493 | |
| 1494 | ParsedAttributes Attrs(AttrFactory); |
| 1495 | SourceLocation endLoc; |
| 1496 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1497 | if (LA.Decls.size() > 0) { |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1498 | Decl *D = LA.Decls[0]; |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1499 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1500 | RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext()); |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1501 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1502 | // Allow 'this' within late-parsed attributes. |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 1503 | Sema::CXXThisScopeRAII ThisScope(Actions, RD, Qualifiers(), |
Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 1504 | ND && ND->isCXXInstanceMember()); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1505 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1506 | 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 Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1512 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1513 | // If the Decl is on a function, add function parameters to the scope. |
| 1514 | bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
Momchil Velikov | 57c681f | 2017-08-10 15:43:06 +0000 | [diff] [blame] | 1515 | ParseScope FnScope( |
| 1516 | this, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope, |
| 1517 | HasFunScope); |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1518 | if (HasFunScope) |
| 1519 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1520 | |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1521 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1522 | nullptr, SourceLocation(), ParsedAttr::AS_GNU, |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1523 | nullptr); |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1524 | |
| 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 Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1535 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1536 | nullptr, SourceLocation(), ParsedAttr::AS_GNU, |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1537 | nullptr); |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1538 | } |
DeLesley Hutchins | 71d6103 | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 1539 | } else { |
| 1540 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1543 | if (OnDefinition && !Attrs.empty() && !Attrs.begin()->isCXX11Attribute() && |
| 1544 | Attrs.begin()->isKnownToGCC()) |
Aaron Ballman | 9a99e0d | 2014-01-20 17:18:35 +0000 | [diff] [blame] | 1545 | 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 Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1549 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1550 | |
David Majnemer | d5946f5 | 2015-01-13 08:35:24 +0000 | [diff] [blame] | 1551 | // 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 Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1560 | void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName, |
| 1561 | SourceLocation AttrNameLoc, |
| 1562 | ParsedAttributes &Attrs, |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1563 | SourceLocation *EndLoc, |
| 1564 | IdentifierInfo *ScopeName, |
| 1565 | SourceLocation ScopeLoc, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1566 | ParsedAttr::Syntax Syntax) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1567 | 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 Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1573 | Diag(Tok, diag::err_expected) << tok::identifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1574 | T.skipToEnd(); |
| 1575 | return; |
| 1576 | } |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 1577 | IdentifierLoc *ArgumentKind = ParseIdentifierLoc(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1578 | |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 1579 | if (ExpectAndConsume(tok::comma)) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1580 | T.skipToEnd(); |
| 1581 | return; |
| 1582 | } |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1583 | |
| 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 Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1593 | while (TryConsumeToken(tok::comma)) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1594 | if (Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1595 | Diag(Tok, diag::err_expected) << tok::identifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1596 | 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 Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1613 | Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, ScopeName, ScopeLoc, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1614 | ArgumentKind, MatchingCType.get(), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1615 | LayoutCompatible, MustBeNull, Syntax); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | if (EndLoc) |
| 1619 | *EndLoc = T.getCloseLocation(); |
| 1620 | } |
| 1621 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1622 | /// 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. |
| 1630 | bool 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 Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1646 | SkipUntil(tok::r_square); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1647 | 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 Carruth | d8f7d38 | 2012-04-10 16:03:08 +0000 | [diff] [blame] | 1653 | llvm_unreachable("All cases handled above."); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1656 | /// We have found the opening square brackets of a C++11 |
Richard Smith | 98155ad | 2013-02-20 01:17:14 +0000 | [diff] [blame] | 1657 | /// 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 Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1660 | void 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 Vali | c5089c0 | 2017-12-25 22:23:20 +0000 | [diff] [blame] | 1669 | // FIXME: use err_attributes_misplaced |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1670 | Diag(Loc, diag::err_attributes_not_allowed) |
| 1671 | << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange) |
| 1672 | << FixItHint::CreateRemoval(AttrRange); |
| 1673 | } |
| 1674 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1675 | void Parser::DiagnoseProhibitedAttributes( |
| 1676 | const SourceRange &Range, const SourceLocation CorrectLocation) { |
Faisal Vali | c5089c0 | 2017-12-25 22:23:20 +0000 | [diff] [blame] | 1677 | if (CorrectLocation.isValid()) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1678 | CharSourceRange AttrRange(Range, true); |
Faisal Vali | c5089c0 | 2017-12-25 22:23:20 +0000 | [diff] [blame] | 1679 | Diag(CorrectLocation, diag::err_attributes_misplaced) |
| 1680 | << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange) |
| 1681 | << FixItHint::CreateRemoval(AttrRange); |
| 1682 | } else |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1683 | Diag(Range.getBegin(), diag::err_attributes_not_allowed) << Range; |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
Richard Smith | 49cc1cc | 2016-08-18 21:59:42 +0000 | [diff] [blame] | 1686 | void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &Attrs, |
| 1687 | unsigned DiagID) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1688 | for (const ParsedAttr &AL : Attrs) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1689 | if (!AL.isCXX11Attribute() && !AL.isC2xAttribute()) |
Richard Smith | 49cc1cc | 2016-08-18 21:59:42 +0000 | [diff] [blame] | 1690 | continue; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1691 | if (AL.getKind() == ParsedAttr::UnknownAttribute) |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1692 | Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL; |
Richard Smith | 49cc1cc | 2016-08-18 21:59:42 +0000 | [diff] [blame] | 1693 | else { |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame] | 1694 | Diag(AL.getLoc(), DiagID) << AL; |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1695 | AL.setInvalid(); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1696 | } |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1697 | } |
| 1698 | } |
| 1699 | |
Nico Weber | 32a0fc7 | 2016-09-03 03:01:32 +0000 | [diff] [blame] | 1700 | // Usually, `__attribute__((attrib)) class Foo {} var` means that attribute |
| 1701 | // applies to var, not the type Foo. |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1702 | // As an exception to the rule, __declspec(align(...)) before the |
| 1703 | // class-key affects the type instead of the variable. |
Nico Weber | 32a0fc7 | 2016-09-03 03:01:32 +0000 | [diff] [blame] | 1704 | // 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. |
| 1707 | void Parser::stripTypeAttributesOffDeclSpec(ParsedAttributesWithRange &Attrs, |
| 1708 | DeclSpec &DS, |
| 1709 | Sema::TagUseKind TUK) { |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1710 | if (TUK == Sema::TUK_Reference) |
| 1711 | return; |
| 1712 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1713 | llvm::SmallVector<ParsedAttr *, 1> ToBeMoved; |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1714 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1715 | for (ParsedAttr &AL : DS.getAttributes()) { |
| 1716 | if ((AL.getKind() == ParsedAttr::AT_Aligned && |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1717 | AL.isDeclspecAttribute()) || |
| 1718 | AL.isMicrosoftAttribute()) |
| 1719 | ToBeMoved.push_back(&AL); |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1720 | } |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 1721 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1722 | for (ParsedAttr *AL : ToBeMoved) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1723 | DS.getAttributes().remove(AL); |
| 1724 | Attrs.addAtEnd(AL); |
| 1725 | } |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1728 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 1729 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1730 | /// 'Context' should be a DeclaratorContext value. This returns the |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1731 | /// location of the semicolon in DeclEnd. |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1732 | /// |
| 1733 | /// declaration: [C99 6.7] |
| 1734 | /// block-declaration -> |
| 1735 | /// simple-declaration |
| 1736 | /// others [FIXME] |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1737 | /// [C++] template-declaration |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1738 | /// [C++] namespace-definition |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1739 | /// [C++] using-directive |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1740 | /// [C++] using-declaration |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1741 | /// [C++11/C11] static_assert-declaration |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1742 | /// others... [FIXME] |
| 1743 | /// |
Nathan Huckleberry | 1e0affb | 2019-08-20 17:16:49 +0000 | [diff] [blame] | 1744 | Parser::DeclGroupPtrTy |
| 1745 | Parser::ParseDeclaration(DeclaratorContext Context, SourceLocation &DeclEnd, |
| 1746 | ParsedAttributesWithRange &attrs, |
| 1747 | SourceLocation *DeclSpecStart) { |
Argyrios Kyrtzidis | 355094e | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 1748 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | 59b7528 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 1749 | // Must temporarily exit the objective-c container scope for |
| 1750 | // parsing c none objective-c decls. |
| 1751 | ObjCDeclContextSwitch ObjCDC(*this); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1752 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1753 | Decl *SingleDecl = nullptr; |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1754 | switch (Tok.getKind()) { |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1755 | case tok::kw_template: |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1756 | case tok::kw_export: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1757 | ProhibitAttributes(attrs); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1758 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd, attrs); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1759 | break; |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1760 | case tok::kw_inline: |
Sebastian Redl | 5a5f2c7 | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 1761 | // Could be the start of an inline namespace. Allowed as an ext in C++03. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1762 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1763 | ProhibitAttributes(attrs); |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1764 | SourceLocation InlineLoc = ConsumeToken(); |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 1765 | return ParseNamespace(Context, DeclEnd, InlineLoc); |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1766 | } |
Nathan Huckleberry | 1e0affb | 2019-08-20 17:16:49 +0000 | [diff] [blame] | 1767 | return ParseSimpleDeclaration(Context, DeclEnd, attrs, true, nullptr, |
| 1768 | DeclSpecStart); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1769 | case tok::kw_namespace: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1770 | ProhibitAttributes(attrs); |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 1771 | return ParseNamespace(Context, DeclEnd); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1772 | case tok::kw_using: |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 1773 | return ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
| 1774 | DeclEnd, attrs); |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1775 | case tok::kw_static_assert: |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1776 | case tok::kw__Static_assert: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1777 | ProhibitAttributes(attrs); |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1778 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1779 | break; |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1780 | default: |
Nathan Huckleberry | 1e0affb | 2019-08-20 17:16:49 +0000 | [diff] [blame] | 1781 | return ParseSimpleDeclaration(Context, DeclEnd, attrs, true, nullptr, |
| 1782 | DeclSpecStart); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1783 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1784 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1785 | // This routine returns a DeclGroup, if the thing we parsed only contains a |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 1786 | // single decl, convert it now. |
| 1787 | return Actions.ConvertDeclToDeclGroup(SingleDecl); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 1791 | /// declaration-specifiers init-declarator-list[opt] ';' |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1792 | /// [C++11] attribute-specifier-seq decl-specifier-seq[opt] |
| 1793 | /// init-declarator-list ';' |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1794 | ///[C90/C++]init-declarator-list ';' [TODO] |
Alexey Bataev | 25ed0c0 | 2019-03-07 17:54:44 +0000 | [diff] [blame] | 1795 | /// [OMP] threadprivate-directive |
| 1796 | /// [OMP] allocate-directive [TODO] |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1797 | /// |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1798 | /// for-range-declaration: [C++11 6.5p1: stmt.ranged] |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1799 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1800 | /// |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1801 | /// If RequireSemi is false, this does not check for a ';' at the end of the |
Chris Lattner | 005fc1b | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1802 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1803 | /// |
| 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 Huckleberry | 1e0affb | 2019-08-20 17:16:49 +0000 | [diff] [blame] | 1807 | /// |
| 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. |
| 1811 | Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration( |
| 1812 | DeclaratorContext Context, SourceLocation &DeclEnd, |
| 1813 | ParsedAttributesWithRange &Attrs, bool RequireSemi, ForRangeInit *FRI, |
| 1814 | SourceLocation *DeclSpecStart) { |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1815 | // Parse the common declaration-specifiers piece. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1816 | ParsingDeclSpec DS(*this); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1817 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 1818 | DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context); |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 1819 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 1820 | |
| 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 Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 1825 | return nullptr; |
Abramo Bagnara | 1cd8368 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 1826 | |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 1827 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 1828 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1829 | if (Tok.is(tok::semi)) { |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1830 | ProhibitAttributes(Attrs); |
Argyrios Kyrtzidis | fbb2bb5 | 2012-05-16 23:49:15 +0000 | [diff] [blame] | 1831 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 005fc1b | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1832 | if (RequireSemi) ConsumeToken(); |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 1833 | RecordDecl *AnonRecord = nullptr; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1834 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 1835 | DS, AnonRecord); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1836 | DS.complete(TheDecl); |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 1837 | if (AnonRecord) { |
| 1838 | Decl* decls[] = {AnonRecord, TheDecl}; |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 1839 | return Actions.BuildDeclaratorGroup(decls); |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 1840 | } |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1841 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 1842 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1843 | |
Nathan Huckleberry | 1e0affb | 2019-08-20 17:16:49 +0000 | [diff] [blame] | 1844 | if (DeclSpecStart) |
| 1845 | DS.SetRangeStart(*DeclSpecStart); |
| 1846 | |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1847 | DS.takeAttributesFrom(Attrs); |
Reid Kleckner | d61a311 | 2014-12-15 23:16:32 +0000 | [diff] [blame] | 1848 | return ParseDeclGroup(DS, Context, &DeclEnd, FRI); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1849 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1850 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1851 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1852 | /// for a declarator. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1853 | bool Parser::MightBeDeclarator(DeclaratorContext Context) { |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1854 | 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 Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1869 | return getLangOpts().CPlusPlus; |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1870 | |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1871 | case tok::l_square: // Might be an attribute on an unnamed bit-field. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1872 | return Context == DeclaratorContext::MemberContext && |
| 1873 | getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square); |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1874 | |
| 1875 | case tok::colon: // Might be a typo for '::' or an unnamed bit-field. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1876 | return Context == DeclaratorContext::MemberContext || |
| 1877 | getLangOpts().CPlusPlus; |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1878 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1879 | 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 Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1901 | // and in block scope it's probably a label. Inside a class definition, |
| 1902 | // this is a bit-field. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1903 | return Context == DeclaratorContext::MemberContext || |
| 1904 | (getLangOpts().CPlusPlus && |
| 1905 | Context == DeclaratorContext::FileContext); |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1906 | |
| 1907 | case tok::identifier: // Possible virt-specifier. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1908 | return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken()); |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1909 | |
| 1910 | default: |
| 1911 | return false; |
| 1912 | } |
| 1913 | |
| 1914 | default: |
| 1915 | return false; |
| 1916 | } |
| 1917 | } |
| 1918 | |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1919 | /// 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. |
| 1922 | void 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 Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1929 | SkipUntil(tok::r_brace); |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1930 | if (Tok.isOneOf(tok::comma, tok::l_brace, tok::kw_try)) { |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1931 | // This declaration isn't over yet. Keep skipping. |
| 1932 | continue; |
| 1933 | } |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1934 | TryConsumeToken(tok::semi); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1935 | return; |
| 1936 | |
| 1937 | case tok::l_square: |
| 1938 | ConsumeBracket(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1939 | SkipUntil(tok::r_square); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1940 | continue; |
| 1941 | |
| 1942 | case tok::l_paren: |
| 1943 | ConsumeParen(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1944 | SkipUntil(tok::r_paren); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1945 | 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 Rose | 12e730c | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1956 | // 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 Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1960 | return; |
| 1961 | break; |
| 1962 | |
| 1963 | case tok::kw_namespace: |
| 1964 | // 'namespace' at the start of a line is almost certainly a good |
Jordan Rose | 12e730c | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1965 | // 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 Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1983 | return; |
| 1984 | break; |
| 1985 | |
| 1986 | case tok::eof: |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 1987 | case tok::annot_module_begin: |
| 1988 | case tok::annot_module_end: |
| 1989 | case tok::annot_module_include: |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1990 | return; |
| 1991 | |
| 1992 | default: |
| 1993 | break; |
| 1994 | } |
| 1995 | |
| 1996 | ConsumeAnyToken(); |
| 1997 | } |
| 1998 | } |
| 1999 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2000 | /// ParseDeclGroup - Having concluded that this is either a function |
| 2001 | /// definition or a group of object declarations, actually parse the |
| 2002 | /// result. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2003 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2004 | DeclaratorContext Context, |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2005 | SourceLocation *DeclEnd, |
| 2006 | ForRangeInit *FRI) { |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2007 | // Parse the first declarator. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2008 | ParsingDeclarator D(*this, DS, Context); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2009 | ParseDeclarator(D); |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 2010 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2011 | // Bail out if the first declarator didn't seem well-formed. |
| 2012 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 2013 | SkipMalformedDecl(); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 2014 | return nullptr; |
Chris Lattner | efb0f11 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 2015 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2016 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2017 | // 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 Hutchins | 66e300e | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 2019 | // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList. |
| 2020 | LateParsedAttrList LateParsedAttrs(true); |
Richard Smith | 99c464c | 2014-11-10 21:10:32 +0000 | [diff] [blame] | 2021 | if (D.isFunctionDeclarator()) { |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2022 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 2023 | |
Richard Smith | 99c464c | 2014-11-10 21:10:32 +0000 | [diff] [blame] | 2024 | // 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 Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2036 | Fixit &= Tok.isOneOf(tok::semi, tok::l_brace, tok::kw_try); |
Richard Smith | 99c464c | 2014-11-10 21:10:32 +0000 | [diff] [blame] | 2037 | |
| 2038 | Diag(Loc, diag::err_c11_noreturn_misplaced) |
| 2039 | << (Fixit ? FixItHint::CreateRemoval(Loc) : FixItHint()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2040 | << (Fixit ? FixItHint::CreateInsertion(D.getBeginLoc(), "_Noreturn ") |
Richard Smith | 99c464c | 2014-11-10 21:10:32 +0000 | [diff] [blame] | 2041 | : FixItHint()); |
| 2042 | } |
| 2043 | } |
| 2044 | |
Chris Lattner | dbb1e93 | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 2045 | // Check to see if we have a function *definition* which must have a body. |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 2046 | if (D.isFunctionDeclarator() && |
Chris Lattner | dbb1e93 | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 2047 | // 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 Jahanian | 712bb81 | 2012-08-10 15:54:40 +0000 | [diff] [blame] | 2050 | !isDeclarationAfterDeclarator()) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2051 | |
Reid Kleckner | d61a311 | 2014-12-15 23:16:32 +0000 | [diff] [blame] | 2052 | // 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 Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2055 | if (Context == DeclaratorContext::FileContext) { |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 2056 | if (isStartOfFunctionDefinition(D)) { |
| 2057 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 2058 | Diag(Tok, diag::err_function_declared_typedef); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2059 | |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 2060 | // 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 McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 2069 | if (isDeclarationSpecifier()) { |
Nico Weber | 6b05f38 | 2015-02-18 04:53:03 +0000 | [diff] [blame] | 2070 | // 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 Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 2076 | } else { |
| 2077 | Diag(Tok, diag::err_expected_fn_body); |
| 2078 | SkipUntil(tok::semi); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 2079 | return nullptr; |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 2080 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2081 | } else { |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 2082 | if (Tok.is(tok::l_brace)) { |
| 2083 | Diag(Tok, diag::err_function_definition_not_allowed); |
Serge Pavlov | 1de5151 | 2013-12-09 05:25:47 +0000 | [diff] [blame] | 2084 | SkipMalformedDecl(); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 2085 | return nullptr; |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 2086 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2087 | } |
| 2088 | } |
| 2089 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2090 | if (ParseAsmAttributesAfterDeclarator(D)) |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 2091 | return nullptr; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2092 | |
| 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 Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 2096 | // |
| 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 Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 2101 | if (TryConsumeToken(tok::colon, FRI->ColonLoc)) { |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 2102 | IsForRangeLoop = true; |
Alexey Bataev | bef93a9 | 2019-10-07 18:54:57 +0000 | [diff] [blame] | 2103 | if (getLangOpts().OpenMP) |
| 2104 | Actions.startOpenMPCXXRangeFor(); |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 2105 | if (Tok.is(tok::l_brace)) |
| 2106 | FRI->RangeExpr = ParseBraceInitializer(); |
| 2107 | else |
| 2108 | FRI->RangeExpr = ParseExpression(); |
| 2109 | } |
| 2110 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2111 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
George Karpenkov | ec38cf7 | 2018-03-29 00:56:24 +0000 | [diff] [blame] | 2112 | if (IsForRangeLoop) { |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 2113 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
George Karpenkov | ec38cf7 | 2018-03-29 00:56:24 +0000 | [diff] [blame] | 2114 | } else { |
| 2115 | // Obj-C for loop |
| 2116 | if (auto *VD = dyn_cast_or_null<VarDecl>(ThisDecl)) |
| 2117 | VD->setObjCForDecl(true); |
| 2118 | } |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2119 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | cf6e0c8 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 2120 | D.complete(ThisDecl); |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 2121 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2122 | } |
| 2123 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2124 | SmallVector<Decl *, 8> DeclsInGroup; |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2125 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes( |
| 2126 | D, ParsedTemplateInfo(), FRI); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2127 | if (LateParsedAttrs.size() > 0) |
| 2128 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2129 | D.complete(FirstDecl); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2130 | if (FirstDecl) |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2131 | DeclsInGroup.push_back(FirstDecl); |
| 2132 | |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2133 | bool ExpectSemi = Context != DeclaratorContext::ForContext; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2134 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2135 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 2136 | // error, bail out. |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 2137 | SourceLocation CommaLoc; |
| 2138 | while (TryConsumeToken(tok::comma, CommaLoc)) { |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 2139 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 2140 | // This comma was followed by a line-break and something which can't be |
| 2141 | // the start of a declarator. The comma was probably a typo for a |
| 2142 | // semicolon. |
| 2143 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 2144 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 2145 | ExpectSemi = false; |
| 2146 | break; |
| 2147 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2148 | |
| 2149 | // Parse the next declarator. |
| 2150 | D.clear(); |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2151 | D.setCommaLoc(CommaLoc); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2152 | |
| 2153 | // Accept attributes in an init-declarator. In the first declarator in a |
| 2154 | // declaration, these would be part of the declspec. In subsequent |
| 2155 | // declarators, they become part of the declarator itself, so that they |
| 2156 | // don't apply to declarators after *this* one. Examples: |
| 2157 | // short __attribute__((common)) var; -> declspec |
| 2158 | // short var __attribute__((common)); -> declarator |
| 2159 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2160 | MaybeParseGNUAttributes(D); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2161 | |
Nico Rieck | eaaae27 | 2014-12-04 23:31:08 +0000 | [diff] [blame] | 2162 | // MSVC parses but ignores qualifiers after the comma as an extension. |
| 2163 | if (getLangOpts().MicrosoftExt) |
| 2164 | DiagnoseAndSkipExtendedMicrosoftTypeAttributes(); |
| 2165 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2166 | ParseDeclarator(D); |
Fariborz Jahanian | 372030b | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 2167 | if (!D.isInvalidType()) { |
| 2168 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 2169 | D.complete(ThisDecl); |
| 2170 | if (ThisDecl) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2171 | DeclsInGroup.push_back(ThisDecl); |
Fariborz Jahanian | 372030b | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 2172 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2173 | } |
| 2174 | |
| 2175 | if (DeclEnd) |
| 2176 | *DeclEnd = Tok.getLocation(); |
| 2177 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 2178 | if (ExpectSemi && |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2179 | ExpectAndConsumeSemi(Context == DeclaratorContext::FileContext |
Chris Lattner | 02f1b61 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 2180 | ? diag::err_invalid_token_after_toplevel_declarator |
| 2181 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 1390134 | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 2182 | // Okay, there was no semicolon and one was expected. If we see a |
| 2183 | // declaration specifier, just assume it was missing and continue parsing. |
| 2184 | // Otherwise things are very confused and we skip to recover. |
| 2185 | if (!isDeclarationSpecifier()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2186 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 2187 | TryConsumeToken(tok::semi); |
Chris Lattner | 1390134 | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 2188 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 2191 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup); |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2194 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 2195 | /// declarator. Returns true on an error. |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2196 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2197 | // If a simple-asm-expr is present, parse it. |
| 2198 | if (Tok.is(tok::kw_asm)) { |
| 2199 | SourceLocation Loc; |
| 2200 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 2201 | if (AsmLabel.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2202 | SkipUntil(tok::semi, StopBeforeMatch); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2203 | return true; |
| 2204 | } |
| 2205 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2206 | D.setAsmLabel(AsmLabel.get()); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2207 | D.SetRangeEnd(Loc); |
| 2208 | } |
| 2209 | |
| 2210 | MaybeParseGNUAttributes(D); |
| 2211 | return false; |
| 2212 | } |
| 2213 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2214 | /// Parse 'declaration' after parsing 'declaration-specifiers |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2215 | /// declarator'. This method parses the remainder of the declaration |
| 2216 | /// (including any attributes or initializer, among other things) and |
| 2217 | /// finalizes the declaration. |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 2218 | /// |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 2219 | /// init-declarator: [C99 6.7] |
| 2220 | /// declarator |
| 2221 | /// declarator '=' initializer |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 2222 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 2223 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2224 | /// [C++] declarator initializer[opt] |
| 2225 | /// |
| 2226 | /// [C++] initializer: |
| 2227 | /// [C++] '=' initializer-clause |
| 2228 | /// [C++] '(' expression-list ')' |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 2229 | /// [C++0x] '=' 'default' [TODO] |
| 2230 | /// [C++0x] '=' 'delete' |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2231 | /// [C++0x] braced-init-list |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 2232 | /// |
| 2233 | /// According to the standard grammar, =default and =delete are function |
| 2234 | /// definitions, but that definitely doesn't fit with the parser here. |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 2235 | /// |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2236 | Decl *Parser::ParseDeclarationAfterDeclarator( |
| 2237 | Declarator &D, const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2238 | if (ParseAsmAttributesAfterDeclarator(D)) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2239 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2240 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2241 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 2242 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2243 | |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2244 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes( |
| 2245 | Declarator &D, const ParsedTemplateInfo &TemplateInfo, ForRangeInit *FRI) { |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 2246 | // RAII type used to track whether we're inside an initializer. |
| 2247 | struct InitializerScopeRAII { |
| 2248 | Parser &P; |
| 2249 | Declarator &D; |
| 2250 | Decl *ThisDecl; |
| 2251 | |
| 2252 | InitializerScopeRAII(Parser &P, Declarator &D, Decl *ThisDecl) |
| 2253 | : P(P), D(D), ThisDecl(ThisDecl) { |
| 2254 | if (ThisDecl && P.getLangOpts().CPlusPlus) { |
| 2255 | Scope *S = nullptr; |
| 2256 | if (D.getCXXScopeSpec().isSet()) { |
| 2257 | P.EnterScope(0); |
| 2258 | S = P.getCurScope(); |
| 2259 | } |
| 2260 | P.Actions.ActOnCXXEnterDeclInitializer(S, ThisDecl); |
| 2261 | } |
| 2262 | } |
| 2263 | ~InitializerScopeRAII() { pop(); } |
| 2264 | void pop() { |
| 2265 | if (ThisDecl && P.getLangOpts().CPlusPlus) { |
| 2266 | Scope *S = nullptr; |
| 2267 | if (D.getCXXScopeSpec().isSet()) |
| 2268 | S = P.getCurScope(); |
| 2269 | P.Actions.ActOnCXXExitDeclInitializer(S, ThisDecl); |
| 2270 | if (S) |
| 2271 | P.ExitScope(); |
| 2272 | } |
| 2273 | ThisDecl = nullptr; |
| 2274 | } |
| 2275 | }; |
| 2276 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2277 | // Inform the current actions module that we just parsed this declarator. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2278 | Decl *ThisDecl = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2279 | switch (TemplateInfo.Kind) { |
| 2280 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2281 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2282 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2283 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2284 | case ParsedTemplateInfo::Template: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2285 | case ParsedTemplateInfo::ExplicitSpecialization: { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2286 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2287 | *TemplateInfo.TemplateParams, |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2288 | D); |
Larisse Voufo | 833b05a | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 2289 | if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2290 | // Re-direct this decl to refer to the templated decl so that we can |
| 2291 | // initialize it. |
| 2292 | ThisDecl = VT->getTemplatedDecl(); |
| 2293 | break; |
| 2294 | } |
| 2295 | case ParsedTemplateInfo::ExplicitInstantiation: { |
| 2296 | if (Tok.is(tok::semi)) { |
| 2297 | DeclResult ThisRes = Actions.ActOnExplicitInstantiation( |
| 2298 | getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D); |
| 2299 | if (ThisRes.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2300 | SkipUntil(tok::semi, StopBeforeMatch); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2301 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2302 | } |
| 2303 | ThisDecl = ThisRes.get(); |
| 2304 | } else { |
| 2305 | // FIXME: This check should be for a variable template instantiation only. |
| 2306 | |
| 2307 | // Check that this is a valid instantiation |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 2308 | if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2309 | // If the declarator-id is not a template-id, issue a diagnostic and |
| 2310 | // recover by ignoring the 'template' keyword. |
| 2311 | Diag(Tok, diag::err_template_defn_explicit_instantiation) |
| 2312 | << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc); |
| 2313 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 2314 | } else { |
| 2315 | SourceLocation LAngleLoc = |
| 2316 | PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
| 2317 | Diag(D.getIdentifierLoc(), |
| 2318 | diag::err_explicit_instantiation_with_definition) |
| 2319 | << SourceRange(TemplateInfo.TemplateLoc) |
| 2320 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
| 2321 | |
| 2322 | // Recover as if it were an explicit specialization. |
| 2323 | TemplateParameterLists FakedParamLists; |
| 2324 | FakedParamLists.push_back(Actions.ActOnTemplateParameterList( |
Craig Topper | 96225a5 | 2015-12-24 23:58:25 +0000 | [diff] [blame] | 2325 | 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, None, |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 2326 | LAngleLoc, nullptr)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2327 | |
| 2328 | ThisDecl = |
| 2329 | Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D); |
| 2330 | } |
| 2331 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2332 | break; |
| 2333 | } |
| 2334 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2335 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2336 | // Parse declarator '=' initializer. |
Richard Trieu | c64d323 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 2337 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | 4972a6d | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 2338 | if (isTokenEqualOrEqualTypo()) { |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2339 | SourceLocation EqualLoc = ConsumeToken(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2340 | |
Anders Carlsson | 991285e | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 2341 | if (Tok.is(tok::kw_delete)) { |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2342 | if (D.isFunctionDeclarator()) |
| 2343 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 2344 | << 1 /* delete */; |
| 2345 | else |
| 2346 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Alexis Hunt | 5dafebc | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 2347 | } else if (Tok.is(tok::kw_default)) { |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2348 | if (D.isFunctionDeclarator()) |
Sebastian Redl | 46afb55 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 2349 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 2350 | << 0 /* default */; |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2351 | else |
| 2352 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2353 | } else { |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 2354 | InitializerScopeRAII InitScope(*this, D, ThisDecl); |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 2355 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2356 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2357 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Peter Collingbourne | 6b4fdc2 | 2012-07-27 12:56:09 +0000 | [diff] [blame] | 2358 | Actions.FinalizeDeclaration(ThisDecl); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2359 | cutOffParsing(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2360 | return nullptr; |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2361 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2362 | |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 2363 | PreferredType.enterVariableInit(Tok.getLocation(), ThisDecl); |
| 2364 | ExprResult Init = ParseInitializer(); |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 2365 | |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2366 | // If this is the only decl in (possibly) range based for statement, |
| 2367 | // our best guess is that the user meant ':' instead of '='. |
| 2368 | if (Tok.is(tok::r_paren) && FRI && D.isFirstDeclarator()) { |
| 2369 | Diag(EqualLoc, diag::err_single_decl_assign_in_for_range) |
| 2370 | << FixItHint::CreateReplacement(EqualLoc, ":"); |
| 2371 | // We are trying to stop parser from looking for ';' in this for |
| 2372 | // statement, therefore preventing spurious errors to be issued. |
| 2373 | FRI->ColonLoc = EqualLoc; |
| 2374 | Init = ExprError(); |
| 2375 | FRI->RangeExpr = Init; |
| 2376 | } |
| 2377 | |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 2378 | InitScope.pop(); |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 2379 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2380 | if (Init.isInvalid()) { |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2381 | SmallVector<tok::TokenKind, 2> StopTokens; |
| 2382 | StopTokens.push_back(tok::comma); |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2383 | if (D.getContext() == DeclaratorContext::ForContext || |
| 2384 | D.getContext() == DeclaratorContext::InitStmtContext) |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2385 | StopTokens.push_back(tok::r_paren); |
| 2386 | SkipUntil(StopTokens, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 604c302 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 2387 | Actions.ActOnInitializerError(ThisDecl); |
| 2388 | } else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2389 | Actions.AddInitializerToDecl(ThisDecl, Init.get(), |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2390 | /*DirectInit=*/false); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2391 | } |
| 2392 | } else if (Tok.is(tok::l_paren)) { |
| 2393 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2394 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2395 | T.consumeOpen(); |
| 2396 | |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2397 | ExprVector Exprs; |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2398 | CommaLocsTy CommaLocs; |
| 2399 | |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 2400 | InitializerScopeRAII InitScope(*this, D, ThisDecl); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 2401 | |
Ilya Biryukov | cbea95d | 2017-09-08 13:36:38 +0000 | [diff] [blame] | 2402 | auto ThisVarDecl = dyn_cast_or_null<VarDecl>(ThisDecl); |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 2403 | auto RunSignatureHelp = [&]() { |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 2404 | QualType PreferredType = Actions.ProduceConstructorSignatureHelp( |
Ilya Biryukov | cbea95d | 2017-09-08 13:36:38 +0000 | [diff] [blame] | 2405 | getCurScope(), ThisVarDecl->getType()->getCanonicalTypeInternal(), |
Ilya Biryukov | 2fab235 | 2018-08-30 13:08:03 +0000 | [diff] [blame] | 2406 | ThisDecl->getLocation(), Exprs, T.getOpenLocation()); |
Kadir Cetinkaya | a32d253 | 2018-09-10 13:46:28 +0000 | [diff] [blame] | 2407 | CalledSignatureHelp = true; |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 2408 | return PreferredType; |
Ilya Biryukov | cbea95d | 2017-09-08 13:36:38 +0000 | [diff] [blame] | 2409 | }; |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 2410 | auto SetPreferredType = [&] { |
| 2411 | PreferredType.enterFunctionArgument(Tok.getLocation(), RunSignatureHelp); |
| 2412 | }; |
| 2413 | |
| 2414 | llvm::function_ref<void()> ExpressionStarts; |
Ilya Biryukov | cbea95d | 2017-09-08 13:36:38 +0000 | [diff] [blame] | 2415 | if (ThisVarDecl) { |
| 2416 | // ParseExpressionList can sometimes succeed even when ThisDecl is not |
| 2417 | // VarDecl. This is an error and it is reported in a call to |
| 2418 | // Actions.ActOnInitializerError(). However, we call |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 2419 | // ProduceConstructorSignatureHelp only on VarDecls. |
| 2420 | ExpressionStarts = SetPreferredType; |
Ilya Biryukov | cbea95d | 2017-09-08 13:36:38 +0000 | [diff] [blame] | 2421 | } |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 2422 | if (ParseExpressionList(Exprs, CommaLocs, ExpressionStarts)) { |
Kadir Cetinkaya | a32d253 | 2018-09-10 13:46:28 +0000 | [diff] [blame] | 2423 | if (ThisVarDecl && PP.isCodeCompletionReached() && !CalledSignatureHelp) { |
| 2424 | Actions.ProduceConstructorSignatureHelp( |
| 2425 | getCurScope(), ThisVarDecl->getType()->getCanonicalTypeInternal(), |
| 2426 | ThisDecl->getLocation(), Exprs, T.getOpenLocation()); |
| 2427 | CalledSignatureHelp = true; |
| 2428 | } |
David Blaikie | eae0411 | 2012-10-10 23:15:05 +0000 | [diff] [blame] | 2429 | Actions.ActOnInitializerError(ThisDecl); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2430 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2431 | } else { |
| 2432 | // Match the ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2433 | T.consumeClose(); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2434 | |
| 2435 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 2436 | "Unexpected number of commas!"); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 2437 | |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 2438 | InitScope.pop(); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 2439 | |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2440 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 2441 | T.getCloseLocation(), |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2442 | Exprs); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2443 | Actions.AddInitializerToDecl(ThisDecl, Initializer.get(), |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2444 | /*DirectInit=*/true); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2445 | } |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2446 | } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) && |
Fariborz Jahanian | 8be1ecd | 2012-07-03 23:22:13 +0000 | [diff] [blame] | 2447 | (!CurParsedObjCImpl || !D.isFunctionDeclarator())) { |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2448 | // Parse C++0x braced-init-list. |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2449 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 2450 | |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 2451 | InitializerScopeRAII InitScope(*this, D, ThisDecl); |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2452 | |
| 2453 | ExprResult Init(ParseBraceInitializer()); |
| 2454 | |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 2455 | InitScope.pop(); |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2456 | |
| 2457 | if (Init.isInvalid()) { |
| 2458 | Actions.ActOnInitializerError(ThisDecl); |
| 2459 | } else |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2460 | Actions.AddInitializerToDecl(ThisDecl, Init.get(), /*DirectInit=*/true); |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2461 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2462 | } else { |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2463 | Actions.ActOnUninitializedDecl(ThisDecl); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 2466 | Actions.FinalizeDeclaration(ThisDecl); |
| 2467 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2468 | return ThisDecl; |
| 2469 | } |
| 2470 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2471 | /// ParseSpecifierQualifierList |
| 2472 | /// specifier-qualifier-list: |
| 2473 | /// type-specifier specifier-qualifier-list[opt] |
| 2474 | /// type-qualifier specifier-qualifier-list[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 2475 | /// [GNU] attributes specifier-qualifier-list[opt] |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2476 | /// |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2477 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 2478 | DeclSpecContext DSC) { |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2479 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 2480 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2481 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 2482 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2483 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2484 | // Validate declspec for type-name. |
| 2485 | unsigned Specs = DS.getParsedSpecifiers(); |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 2486 | if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) { |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2487 | Diag(Tok, diag::err_expected_type); |
| 2488 | DS.SetTypeSpecError(); |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 2489 | } else if (Specs == DeclSpec::PQ_None && !DS.hasAttributes()) { |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2490 | Diag(Tok, diag::err_typename_requires_specqual); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2491 | if (!DS.hasTypeSpecifier()) |
| 2492 | DS.SetTypeSpecError(); |
| 2493 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2494 | |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 2495 | // Issue diagnostic and remove storage class if present. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2496 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 2497 | if (DS.getStorageClassSpecLoc().isValid()) |
| 2498 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 2499 | else |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2500 | Diag(DS.getThreadStorageClassSpecLoc(), |
| 2501 | diag::err_typename_invalid_storageclass); |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 2502 | DS.ClearStorageClassSpecs(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2503 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2504 | |
Craig Topper | 3f13d4d2 | 2015-11-14 18:15:55 +0000 | [diff] [blame] | 2505 | // Issue diagnostic and remove function specifier if present. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2506 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2507 | if (DS.isInlineSpecified()) |
| 2508 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 2509 | if (DS.isVirtualSpecified()) |
| 2510 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 2511 | if (DS.hasExplicitSpecifier()) |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2512 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 2513 | DS.ClearFunctionSpecs(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2514 | } |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2515 | |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 2516 | // Issue diagnostic and remove constexpr specifier if present. |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 2517 | if (DS.hasConstexprSpecifier() && DSC != DeclSpecContext::DSC_condition) { |
| 2518 | Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr) |
Richard Smith | a6e8b68 | 2019-09-04 20:30:37 +0000 | [diff] [blame] | 2519 | << DS.getConstexprSpecifier(); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2520 | DS.ClearConstexprSpec(); |
| 2521 | } |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2522 | } |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 2523 | |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2524 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 2525 | /// specified token is valid after the identifier in a declarator which |
| 2526 | /// immediately follows the declspec. For example, these things are valid: |
| 2527 | /// |
| 2528 | /// int x [ 4]; // direct-declarator |
| 2529 | /// int x ( int y); // direct-declarator |
| 2530 | /// int(int x ) // direct-declarator |
| 2531 | /// int x ; // simple-declaration |
| 2532 | /// int x = 17; // init-declarator-list |
| 2533 | /// int x , y; // init-declarator-list |
| 2534 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | a723ba9 | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 2535 | /// int x : 4; // struct-declarator |
Chris Lattner | 2b988c1 | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 2536 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2537 | /// |
| 2538 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 2539 | /// ')' happens to be valid anyway). |
| 2540 | /// int (x) |
| 2541 | /// |
| 2542 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2543 | return T.isOneOf(tok::l_square, tok::l_paren, tok::r_paren, tok::semi, |
| 2544 | tok::comma, tok::equal, tok::kw_asm, tok::l_brace, |
| 2545 | tok::colon); |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2548 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 2549 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 2550 | /// the declspec has no type specifier. In this case, the declspec is either |
| 2551 | /// malformed or is "implicit int" (in K&R and C89). |
| 2552 | /// |
| 2553 | /// This method handles diagnosing this prettily and returns false if the |
| 2554 | /// declspec is done being processed. If it recovers and thinks there may be |
| 2555 | /// other pieces of declspec after it, it returns true. |
| 2556 | /// |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2557 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2558 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | a0a5d50 | 2014-05-08 22:32:00 +0000 | [diff] [blame] | 2559 | AccessSpecifier AS, DeclSpecContext DSC, |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2560 | ParsedAttributesWithRange &Attrs) { |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2561 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2562 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2563 | SourceLocation Loc = Tok.getLocation(); |
| 2564 | // If we see an identifier that is not a type name, we normally would |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 2565 | // parse it as the identifier being declared. However, when a typename |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2566 | // is typo'd or the definition is not included, this will incorrectly |
| 2567 | // parse the typename as the identifier name and fall over misparsing |
| 2568 | // later parts of the diagnostic. |
| 2569 | // |
| 2570 | // As such, we try to do some look-ahead in cases where this would |
| 2571 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 2572 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 2573 | // an identifier with implicit int, we'd get a parse error because the |
| 2574 | // next token is obviously invalid for a type. Parse these as a case |
| 2575 | // with an invalid type specifier. |
| 2576 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2577 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2578 | // Since we know that this either implicit int (which is rare) or an |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2579 | // error, do lookahead to try to do better recovery. This never applies |
| 2580 | // within a type specifier. Outside of C++, we allow this even if the |
| 2581 | // language doesn't "officially" support implicit int -- we support |
Richard Smith | 3b87038 | 2013-04-30 22:43:51 +0000 | [diff] [blame] | 2582 | // implicit int as an extension in C99 and C11. |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 2583 | if (!isTypeSpecifier(DSC) && !getLangOpts().CPlusPlus && |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2584 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2585 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 2586 | // we just avoid eating the identifier, so it will be parsed as the |
| 2587 | // identifier in the declarator. |
| 2588 | return false; |
| 2589 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2590 | |
Sven van Haastregt | e518bb4 | 2019-05-22 13:12:20 +0000 | [diff] [blame] | 2591 | // Early exit as Sema has a dedicated missing_actual_pipe_type diagnostic |
| 2592 | // for incomplete declarations such as `pipe p`. |
| 2593 | if (getLangOpts().OpenCLCPlusPlus && DS.isTypeSpecPipe()) |
| 2594 | return false; |
| 2595 | |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2596 | if (getLangOpts().CPlusPlus && |
| 2597 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| 2598 | // Don't require a type specifier if we have the 'auto' storage class |
| 2599 | // specifier in C++98 -- we'll promote it to a type specifier. |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2600 | if (SS) |
| 2601 | AnnotateScopeToken(*SS, /*IsNewAnnotation*/false); |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2602 | return false; |
| 2603 | } |
| 2604 | |
Reid Kleckner | 9a96e42 | 2016-05-24 21:23:54 +0000 | [diff] [blame] | 2605 | if (getLangOpts().CPlusPlus && (!SS || SS->isEmpty()) && |
| 2606 | getLangOpts().MSVCCompat) { |
| 2607 | // Lookup of an unqualified type name has failed in MSVC compatibility mode. |
| 2608 | // Give Sema a chance to recover if we are in a template with dependent base |
| 2609 | // classes. |
| 2610 | if (ParsedType T = Actions.ActOnMSVCUnknownTypeName( |
| 2611 | *Tok.getIdentifierInfo(), Tok.getLocation(), |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2612 | DSC == DeclSpecContext::DSC_template_type_arg)) { |
Reid Kleckner | 9a96e42 | 2016-05-24 21:23:54 +0000 | [diff] [blame] | 2613 | const char *PrevSpec; |
| 2614 | unsigned DiagID; |
| 2615 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T, |
| 2616 | Actions.getASTContext().getPrintingPolicy()); |
| 2617 | DS.SetRangeEnd(Tok.getLocation()); |
| 2618 | ConsumeToken(); |
| 2619 | return false; |
| 2620 | } |
| 2621 | } |
| 2622 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2623 | // Otherwise, if we don't consume this token, we are going to emit an |
| 2624 | // error anyway. Try to recover from various common problems. Check |
| 2625 | // to see if this was a reference to a tag name without a tag specified. |
| 2626 | // This is a common problem in C (saying 'foo' instead of 'struct foo'). |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2627 | // |
| 2628 | // C++ doesn't need this, and isTagName doesn't take SS. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2629 | if (SS == nullptr) { |
| 2630 | const char *TagName = nullptr, *FixitTagName = nullptr; |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2631 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2632 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2633 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2634 | default: break; |
Argyrios Kyrtzidis | 1f32940 | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2635 | case DeclSpec::TST_enum: |
| 2636 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 2637 | case DeclSpec::TST_union: |
| 2638 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 2639 | case DeclSpec::TST_struct: |
| 2640 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2641 | case DeclSpec::TST_interface: |
| 2642 | TagName="__interface"; FixitTagName = "__interface "; |
| 2643 | TagKind=tok::kw___interface;break; |
Argyrios Kyrtzidis | 1f32940 | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2644 | case DeclSpec::TST_class: |
| 2645 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2646 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2647 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2648 | if (TagName) { |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2649 | IdentifierInfo *TokenName = Tok.getIdentifierInfo(); |
| 2650 | LookupResult R(Actions, TokenName, SourceLocation(), |
| 2651 | Sema::LookupOrdinaryName); |
| 2652 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2653 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2654 | << TokenName << TagName << getLangOpts().CPlusPlus |
| 2655 | << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName); |
| 2656 | |
| 2657 | if (Actions.LookupParsedName(R, getCurScope(), SS)) { |
| 2658 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); |
| 2659 | I != IEnd; ++I) |
Kaelyn Uhrain | 3fe3f85 | 2012-04-27 18:26:49 +0000 | [diff] [blame] | 2660 | Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2661 | << TokenName << TagName; |
| 2662 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2663 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2664 | // Parse this as a tag as if the missing tag were present. |
| 2665 | if (TagKind == tok::kw_enum) |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2666 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, |
| 2667 | DeclSpecContext::DSC_normal); |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2668 | else |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2669 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2670 | /*EnteringContext*/ false, |
| 2671 | DeclSpecContext::DSC_normal, Attrs); |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2672 | return true; |
| 2673 | } |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2674 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2675 | |
Richard Smith | fe904f0 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2676 | // Determine whether this identifier could plausibly be the name of something |
Richard Smith | edd124e | 2012-05-15 21:42:17 +0000 | [diff] [blame] | 2677 | // being declared (with a missing type). |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2678 | if (!isTypeSpecifier(DSC) && (!SS || DSC == DeclSpecContext::DSC_top_level || |
| 2679 | DSC == DeclSpecContext::DSC_class)) { |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2680 | // Look ahead to the next token to try to figure out what this declaration |
| 2681 | // was supposed to be. |
| 2682 | switch (NextToken().getKind()) { |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2683 | case tok::l_paren: { |
| 2684 | // static x(4); // 'x' is not a type |
| 2685 | // x(int n); // 'x' is not a type |
| 2686 | // x (*p)[]; // 'x' is a type |
| 2687 | // |
Richard Smith | a0a5d50 | 2014-05-08 22:32:00 +0000 | [diff] [blame] | 2688 | // Since we're in an error case, we can afford to perform a tentative |
| 2689 | // parse to determine which case we're in. |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2690 | TentativeParsingAction PA(*this); |
| 2691 | ConsumeToken(); |
| 2692 | TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false); |
| 2693 | PA.Revert(); |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2694 | |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 2695 | if (TPR != TPResult::False) { |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2696 | // The identifier is followed by a parenthesized declarator. |
| 2697 | // It's supposed to be a type. |
| 2698 | break; |
| 2699 | } |
| 2700 | |
| 2701 | // If we're in a context where we could be declaring a constructor, |
| 2702 | // check whether this is a constructor declaration with a bogus name. |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2703 | if (DSC == DeclSpecContext::DSC_class || |
| 2704 | (DSC == DeclSpecContext::DSC_top_level && SS)) { |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2705 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2706 | if (Actions.isCurrentClassNameTypo(II, SS)) { |
| 2707 | Diag(Loc, diag::err_constructor_bad_name) |
| 2708 | << Tok.getIdentifierInfo() << II |
| 2709 | << FixItHint::CreateReplacement(Tok.getLocation(), II->getName()); |
| 2710 | Tok.setIdentifierInfo(II); |
| 2711 | } |
| 2712 | } |
| 2713 | // Fall through. |
Galina Kistanova | 7767425 | 2017-06-01 21:15:34 +0000 | [diff] [blame] | 2714 | LLVM_FALLTHROUGH; |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2715 | } |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2716 | case tok::comma: |
| 2717 | case tok::equal: |
| 2718 | case tok::kw_asm: |
| 2719 | case tok::l_brace: |
| 2720 | case tok::l_square: |
| 2721 | case tok::semi: |
| 2722 | // This looks like a variable or function declaration. The type is |
| 2723 | // probably missing. We're done parsing decl-specifiers. |
Nicolas Lesser | ee05717 | 2019-05-05 12:15:17 +0000 | [diff] [blame] | 2724 | // But only if we are not in a function prototype scope. |
| 2725 | if (getCurScope()->isFunctionPrototypeScope()) |
| 2726 | break; |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2727 | if (SS) |
| 2728 | AnnotateScopeToken(*SS, /*IsNewAnnotation*/false); |
| 2729 | return false; |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2730 | |
| 2731 | default: |
| 2732 | // This is probably supposed to be a type. This includes cases like: |
| 2733 | // int f(itn); |
| 2734 | // struct S { unsinged : 4; }; |
| 2735 | break; |
| 2736 | } |
| 2737 | } |
| 2738 | |
Reid Kleckner | c05ca5e | 2014-06-19 01:23:22 +0000 | [diff] [blame] | 2739 | // This is almost certainly an invalid type name. Let Sema emit a diagnostic |
| 2740 | // and attempt to recover. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2741 | ParsedType T; |
Kaelyn Uhrain | b5b17fe | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2742 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Richard Smith | 52f8d19 | 2017-05-10 21:32:16 +0000 | [diff] [blame] | 2743 | bool IsTemplateName = getLangOpts().CPlusPlus && NextToken().is(tok::less); |
Reid Kleckner | c05ca5e | 2014-06-19 01:23:22 +0000 | [diff] [blame] | 2744 | Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T, |
Richard Smith | 52f8d19 | 2017-05-10 21:32:16 +0000 | [diff] [blame] | 2745 | IsTemplateName); |
Reid Kleckner | c05ca5e | 2014-06-19 01:23:22 +0000 | [diff] [blame] | 2746 | if (T) { |
| 2747 | // The action has suggested that the type T could be used. Set that as |
| 2748 | // the type in the declaration specifiers, consume the would-be type |
| 2749 | // name token, and we're done. |
| 2750 | const char *PrevSpec; |
| 2751 | unsigned DiagID; |
| 2752 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T, |
| 2753 | Actions.getASTContext().getPrintingPolicy()); |
| 2754 | DS.SetRangeEnd(Tok.getLocation()); |
| 2755 | ConsumeToken(); |
| 2756 | // There may be other declaration specifiers after this. |
| 2757 | return true; |
| 2758 | } else if (II != Tok.getIdentifierInfo()) { |
| 2759 | // If no type was suggested, the correction is to a keyword |
| 2760 | Tok.setKind(II->getTokenID()); |
| 2761 | // There may be other declaration specifiers after this. |
| 2762 | return true; |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2763 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2764 | |
Reid Kleckner | c05ca5e | 2014-06-19 01:23:22 +0000 | [diff] [blame] | 2765 | // Otherwise, the action had no suggestion for us. Mark this as an error. |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2766 | DS.SetTypeSpecError(); |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2767 | DS.SetRangeEnd(Tok.getLocation()); |
| 2768 | ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2769 | |
Richard Smith | 52f8d19 | 2017-05-10 21:32:16 +0000 | [diff] [blame] | 2770 | // Eat any following template arguments. |
| 2771 | if (IsTemplateName) { |
| 2772 | SourceLocation LAngle, RAngle; |
| 2773 | TemplateArgList Args; |
| 2774 | ParseTemplateIdAfterTemplateName(true, LAngle, Args, RAngle); |
| 2775 | } |
| 2776 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2777 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 2778 | // avoid rippling error messages on subsequent uses of the same type, |
| 2779 | // could be useful if #include was forgotten. |
Richard Smith | b306579 | 2019-05-07 07:36:07 +0000 | [diff] [blame] | 2780 | return true; |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2781 | } |
| 2782 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2783 | /// Determine the declaration specifier context from the declarator |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2784 | /// context. |
| 2785 | /// |
| 2786 | /// \param Context the declarator context, which is one of the |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2787 | /// DeclaratorContext enumerator values. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2788 | Parser::DeclSpecContext |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2789 | Parser::getDeclSpecContextFromDeclaratorContext(DeclaratorContext Context) { |
| 2790 | if (Context == DeclaratorContext::MemberContext) |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2791 | return DeclSpecContext::DSC_class; |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2792 | if (Context == DeclaratorContext::FileContext) |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2793 | return DeclSpecContext::DSC_top_level; |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2794 | if (Context == DeclaratorContext::TemplateParamContext) |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2795 | return DeclSpecContext::DSC_template_param; |
Richard Smith | 77a9c60 | 2018-02-28 03:02:23 +0000 | [diff] [blame] | 2796 | if (Context == DeclaratorContext::TemplateArgContext || |
| 2797 | Context == DeclaratorContext::TemplateTypeArgContext) |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2798 | return DeclSpecContext::DSC_template_type_arg; |
Richard Smith | e303e35 | 2018-02-02 22:24:54 +0000 | [diff] [blame] | 2799 | if (Context == DeclaratorContext::TrailingReturnContext || |
| 2800 | Context == DeclaratorContext::TrailingReturnVarContext) |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2801 | return DeclSpecContext::DSC_trailing; |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2802 | if (Context == DeclaratorContext::AliasDeclContext || |
| 2803 | Context == DeclaratorContext::AliasTemplateContext) |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2804 | return DeclSpecContext::DSC_alias_declaration; |
| 2805 | return DeclSpecContext::DSC_normal; |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2808 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 2809 | /// |
| 2810 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 2811 | /// type. Ideally, the type should be propagated directly into Sema. |
| 2812 | /// |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2813 | /// [C11] type-id |
| 2814 | /// [C11] constant-expression |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2815 | /// [C++0x] type-id ...[opt] |
| 2816 | /// [C++0x] assignment-expression ...[opt] |
| 2817 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 2818 | SourceLocation &EllipsisLoc) { |
| 2819 | ExprResult ER; |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2820 | if (isTypeIdInParens()) { |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2821 | SourceLocation TypeLoc = Tok.getLocation(); |
| 2822 | ParsedType Ty = ParseTypeName().get(); |
| 2823 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2824 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 2825 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2826 | } else |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2827 | ER = ParseConstantExpression(); |
| 2828 | |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 2829 | if (getLangOpts().CPlusPlus11) |
| 2830 | TryConsumeToken(tok::ellipsis, EllipsisLoc); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2831 | |
| 2832 | return ER; |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
| 2835 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 2836 | /// attribute to Attrs. |
| 2837 | /// |
| 2838 | /// alignment-specifier: |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2839 | /// [C11] '_Alignas' '(' type-id ')' |
| 2840 | /// [C11] '_Alignas' '(' constant-expression ')' |
Richard Smith | d11c7a1 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2841 | /// [C++11] 'alignas' '(' type-id ...[opt] ')' |
| 2842 | /// [C++11] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2843 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2844 | SourceLocation *EndLoc) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2845 | assert(Tok.isOneOf(tok::kw_alignas, tok::kw__Alignas) && |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2846 | "Not an alignment-specifier!"); |
| 2847 | |
Richard Smith | d11c7a1 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2848 | IdentifierInfo *KWName = Tok.getIdentifierInfo(); |
| 2849 | SourceLocation KWLoc = ConsumeToken(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2850 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2851 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 2852 | if (T.expectAndConsume()) |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2853 | return; |
| 2854 | |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2855 | SourceLocation EllipsisLoc; |
| 2856 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2857 | if (ArgExpr.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2858 | T.skipToEnd(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2859 | return; |
| 2860 | } |
| 2861 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2862 | T.consumeClose(); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2863 | if (EndLoc) |
| 2864 | *EndLoc = T.getCloseLocation(); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2865 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2866 | ArgsVector ArgExprs; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2867 | ArgExprs.push_back(ArgExpr.get()); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2868 | Attrs.addNew(KWName, KWLoc, nullptr, KWLoc, ArgExprs.data(), 1, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2869 | ParsedAttr::AS_Keyword, EllipsisLoc); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2870 | } |
| 2871 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2872 | /// Determine whether we're looking at something that might be a declarator |
| 2873 | /// in a simple-declaration. If it can't possibly be a declarator, maybe |
| 2874 | /// diagnose a missing semicolon after a prior tag definition in the decl |
| 2875 | /// specifier. |
| 2876 | /// |
| 2877 | /// \return \c true if an error occurred and this can't be any kind of |
| 2878 | /// declaration. |
| 2879 | bool |
| 2880 | Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS, |
| 2881 | DeclSpecContext DSContext, |
| 2882 | LateParsedAttrList *LateAttrs) { |
| 2883 | assert(DS.hasTagDefinition() && "shouldn't call this"); |
| 2884 | |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2885 | bool EnteringContext = (DSContext == DeclSpecContext::DSC_class || |
| 2886 | DSContext == DeclSpecContext::DSC_top_level); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2887 | |
| 2888 | if (getLangOpts().CPlusPlus && |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2889 | Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype, |
| 2890 | tok::annot_template_id) && |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2891 | TryAnnotateCXXScopeToken(EnteringContext)) { |
| 2892 | SkipMalformedDecl(); |
| 2893 | return true; |
| 2894 | } |
| 2895 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2896 | bool HasScope = Tok.is(tok::annot_cxxscope); |
| 2897 | // Make a copy in case GetLookAheadToken invalidates the result of NextToken. |
| 2898 | Token AfterScope = HasScope ? NextToken() : Tok; |
| 2899 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2900 | // Determine whether the following tokens could possibly be a |
| 2901 | // declarator. |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2902 | bool MightBeDeclarator = true; |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2903 | if (Tok.isOneOf(tok::kw_typename, tok::annot_typename)) { |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2904 | // A declarator-id can't start with 'typename'. |
| 2905 | MightBeDeclarator = false; |
| 2906 | } else if (AfterScope.is(tok::annot_template_id)) { |
| 2907 | // If we have a type expressed as a template-id, this cannot be a |
| 2908 | // declarator-id (such a type cannot be redeclared in a simple-declaration). |
| 2909 | TemplateIdAnnotation *Annot = |
| 2910 | static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue()); |
| 2911 | if (Annot->Kind == TNK_Type_template) |
| 2912 | MightBeDeclarator = false; |
| 2913 | } else if (AfterScope.is(tok::identifier)) { |
| 2914 | const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken(); |
| 2915 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2916 | // These tokens cannot come after the declarator-id in a |
| 2917 | // simple-declaration, and are likely to come after a type-specifier. |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2918 | if (Next.isOneOf(tok::star, tok::amp, tok::ampamp, tok::identifier, |
| 2919 | tok::annot_cxxscope, tok::coloncolon)) { |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2920 | // Missing a semicolon. |
| 2921 | MightBeDeclarator = false; |
| 2922 | } else if (HasScope) { |
| 2923 | // If the declarator-id has a scope specifier, it must redeclare a |
| 2924 | // previously-declared entity. If that's a type (and this is not a |
| 2925 | // typedef), that's an error. |
| 2926 | CXXScopeSpec SS; |
| 2927 | Actions.RestoreNestedNameSpecifierAnnotation( |
| 2928 | Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS); |
| 2929 | IdentifierInfo *Name = AfterScope.getIdentifierInfo(); |
| 2930 | Sema::NameClassification Classification = Actions.ClassifyName( |
| 2931 | getCurScope(), SS, Name, AfterScope.getLocation(), Next, |
Richard Smith | 7e8fe67 | 2019-10-14 21:53:03 +0000 | [diff] [blame] | 2932 | /*CCC=*/nullptr); |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2933 | switch (Classification.getKind()) { |
| 2934 | case Sema::NC_Error: |
| 2935 | SkipMalformedDecl(); |
| 2936 | return true; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2937 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2938 | case Sema::NC_Keyword: |
Richard Smith | 7e8fe67 | 2019-10-14 21:53:03 +0000 | [diff] [blame] | 2939 | llvm_unreachable("typo correction is not possible here"); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2940 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2941 | case Sema::NC_Type: |
| 2942 | case Sema::NC_TypeTemplate: |
Richard Smith | 7e8fe67 | 2019-10-14 21:53:03 +0000 | [diff] [blame] | 2943 | case Sema::NC_UndeclaredNonType: |
| 2944 | case Sema::NC_UndeclaredTemplate: |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2945 | // Not a previously-declared non-type entity. |
| 2946 | MightBeDeclarator = false; |
| 2947 | break; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2948 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2949 | case Sema::NC_Unknown: |
Richard Smith | 7e8fe67 | 2019-10-14 21:53:03 +0000 | [diff] [blame] | 2950 | case Sema::NC_NonType: |
| 2951 | case Sema::NC_DependentNonType: |
| 2952 | case Sema::NC_ContextIndependentExpr: |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2953 | case Sema::NC_VarTemplate: |
| 2954 | case Sema::NC_FunctionTemplate: |
| 2955 | // Might be a redeclaration of a prior entity. |
| 2956 | break; |
| 2957 | } |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2958 | } |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2961 | if (MightBeDeclarator) |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2962 | return false; |
| 2963 | |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2964 | const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy(); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2965 | Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getEndLoc()), |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 2966 | diag::err_expected_after) |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2967 | << DeclSpec::getSpecifierName(DS.getTypeSpecType(), PPol) << tok::semi; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2968 | |
| 2969 | // Try to recover from the typo, by dropping the tag definition and parsing |
| 2970 | // the problematic tokens as a type. |
| 2971 | // |
| 2972 | // FIXME: Split the DeclSpec into pieces for the standalone |
| 2973 | // declaration and pieces for the following declaration, instead |
| 2974 | // of assuming that all the other pieces attach to new declaration, |
| 2975 | // and call ParsedFreeStandingDeclSpec as appropriate. |
| 2976 | DS.ClearTypeSpecType(); |
| 2977 | ParsedTemplateInfo NotATemplate; |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 2978 | ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2979 | return false; |
| 2980 | } |
| 2981 | |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 2982 | // Choose the apprpriate diagnostic error for why fixed point types are |
| 2983 | // disabled, set the previous specifier, and mark as invalid. |
| 2984 | static void SetupFixedPointError(const LangOptions &LangOpts, |
| 2985 | const char *&PrevSpec, unsigned &DiagID, |
| 2986 | bool &isInvalid) { |
| 2987 | assert(!LangOpts.FixedPoint); |
| 2988 | DiagID = diag::err_fixed_point_not_enabled; |
| 2989 | PrevSpec = ""; // Not used by diagnostic |
| 2990 | isInvalid = true; |
| 2991 | } |
| 2992 | |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 2993 | /// ParseDeclarationSpecifiers |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2994 | /// declaration-specifiers: [C99 6.7] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2995 | /// storage-class-specifier declaration-specifiers[opt] |
| 2996 | /// type-specifier declaration-specifiers[opt] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2997 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2998 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 2999 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 3000 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 3001 | /// [ObjC1] '__kindof' declaration-specifiers[opt] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 3002 | /// |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3003 | /// storage-class-specifier: [C99 6.7.1] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 3004 | /// 'typedef' |
| 3005 | /// 'extern' |
| 3006 | /// 'static' |
| 3007 | /// 'auto' |
| 3008 | /// 'register' |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 3009 | /// [C++] 'mutable' |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3010 | /// [C++11] 'thread_local' |
| 3011 | /// [C11] '_Thread_local' |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 3012 | /// [GNU] '__thread' |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3013 | /// function-specifier: [C99 6.7.4] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3014 | /// [C99] 'inline' |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3015 | /// [C++] 'virtual' |
| 3016 | /// [C++] 'explicit' |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3017 | /// [OpenCL] '__kernel' |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 3018 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 39c2a8b | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 3019 | /// 'constexpr': [C++0x dcl.constexpr] |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 3020 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 3021 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3022 | AccessSpecifier AS, |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 3023 | DeclSpecContext DSContext, |
| 3024 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 3025 | if (DS.getSourceRange().isInvalid()) { |
David Majnemer | 24b2830 | 2014-07-26 05:41:31 +0000 | [diff] [blame] | 3026 | // Start the range at the current token but make the end of the range |
| 3027 | // invalid. This will make the entire range invalid unless we successfully |
| 3028 | // consume a token. |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 3029 | DS.SetRangeStart(Tok.getLocation()); |
David Majnemer | 24b2830 | 2014-07-26 05:41:31 +0000 | [diff] [blame] | 3030 | DS.SetRangeEnd(SourceLocation()); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 3031 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3032 | |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 3033 | bool EnteringContext = (DSContext == DeclSpecContext::DSC_class || |
| 3034 | DSContext == DeclSpecContext::DSC_top_level); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3035 | bool AttrsLastTime = false; |
| 3036 | ParsedAttributesWithRange attrs(AttrFactory); |
Benjamin Kramer | e481214 | 2015-03-12 14:28:38 +0000 | [diff] [blame] | 3037 | // We use Sema's policy to get bool macros right. |
Richard Smith | 301bc21 | 2016-05-19 01:39:10 +0000 | [diff] [blame] | 3038 | PrintingPolicy Policy = Actions.getPrintingPolicy(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 3039 | while (1) { |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3040 | bool isInvalid = false; |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3041 | bool isStorageClass = false; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 3042 | const char *PrevSpec = nullptr; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3043 | unsigned DiagID = 0; |
| 3044 | |
Richard Smith | ba24f35 | 2019-05-09 19:45:46 +0000 | [diff] [blame] | 3045 | // This value needs to be set to the location of the last token if the last |
| 3046 | // token of the specifier is already consumed. |
| 3047 | SourceLocation ConsumedEnd; |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 3048 | |
David Majnemer | 51fd8a0 | 2015-07-22 23:46:18 +0000 | [diff] [blame] | 3049 | // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL |
| 3050 | // implementation for VS2013 uses _Atomic as an identifier for one of the |
| 3051 | // classes in <atomic>. |
| 3052 | // |
| 3053 | // A typedef declaration containing _Atomic<...> is among the places where |
| 3054 | // the class is used. If we are currently parsing such a declaration, treat |
| 3055 | // the token as an identifier. |
| 3056 | if (getLangOpts().MSVCCompat && Tok.is(tok::kw__Atomic) && |
| 3057 | DS.getStorageClassSpec() == clang::DeclSpec::SCS_typedef && |
| 3058 | !DS.hasTypeSpecifier() && GetLookAheadToken(1).is(tok::less)) |
| 3059 | Tok.setKind(tok::identifier); |
| 3060 | |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 3061 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 3062 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 3063 | switch (Tok.getKind()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3064 | default: |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3065 | DoneWithDeclSpec: |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3066 | if (!AttrsLastTime) |
| 3067 | ProhibitAttributes(attrs); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 3068 | else { |
| 3069 | // Reject C++11 attributes that appertain to decl specifiers as |
| 3070 | // we don't support any C++11 attributes that appertain to decl |
| 3071 | // specifiers. This also conforms to what g++ 4.8 is doing. |
Richard Smith | 49cc1cc | 2016-08-18 21:59:42 +0000 | [diff] [blame] | 3072 | ProhibitCXX11Attributes(attrs, diag::err_attribute_not_type_attr); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 3073 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3074 | DS.takeAttributesFrom(attrs); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 3075 | } |
Peter Collingbourne | 70188b3 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 3076 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3077 | // If this is not a declaration specifier token, we're done reading decl |
| 3078 | // specifiers. First verify that DeclSpec's are consistent. |
Craig Topper | 2512241 | 2015-11-15 03:32:11 +0000 | [diff] [blame] | 3079 | DS.Finish(Actions, Policy); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3080 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3081 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3082 | case tok::l_square: |
| 3083 | case tok::kw_alignas: |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 3084 | if (!standardAttributesAllowed() || !isCXX11AttributeSpecifier()) |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3085 | goto DoneWithDeclSpec; |
| 3086 | |
| 3087 | ProhibitAttributes(attrs); |
| 3088 | // FIXME: It would be good to recover by accepting the attributes, |
| 3089 | // but attempting to do that now would cause serious |
| 3090 | // madness in terms of diagnostics. |
| 3091 | attrs.clear(); |
| 3092 | attrs.Range = SourceRange(); |
| 3093 | |
| 3094 | ParseCXX11Attributes(attrs); |
| 3095 | AttrsLastTime = true; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3096 | continue; |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3097 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3098 | case tok::code_completion: { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3099 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3100 | if (DS.hasTypeSpecifier()) { |
| 3101 | bool AllowNonIdentifiers |
| 3102 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 3103 | Scope::BlockScope | |
| 3104 | Scope::TemplateParamScope | |
| 3105 | Scope::FunctionPrototypeScope | |
| 3106 | Scope::AtCatchScope)) == 0; |
| 3107 | bool AllowNestedNameSpecifiers |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 3108 | = DSContext == DeclSpecContext::DSC_top_level || |
| 3109 | (DSContext == DeclSpecContext::DSC_class && DS.isFriendSpecified()); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3110 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3111 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3112 | AllowNonIdentifiers, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 3113 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3114 | return cutOffParsing(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3115 | } |
| 3116 | |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3117 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 3118 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 3119 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 3120 | CCC = DSContext == DeclSpecContext::DSC_class ? Sema::PCC_MemberTemplate |
| 3121 | : Sema::PCC_Template; |
| 3122 | else if (DSContext == DeclSpecContext::DSC_class) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3123 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | b6c6a58 | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 3124 | else if (CurParsedObjCImpl) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3125 | CCC = Sema::PCC_ObjCImplementation; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3126 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3127 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3128 | return cutOffParsing(); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3129 | } |
| 3130 | |
Chris Lattner | bd31aa3 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 3131 | case tok::coloncolon: // ::foo::bar |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3132 | // C++ scope specifier. Annotate and loop, or bail out on error. |
Eli Friedman | 2a1d9a9 | 2013-08-15 23:59:20 +0000 | [diff] [blame] | 3133 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3134 | if (!DS.hasTypeSpecifier()) |
| 3135 | DS.SetTypeSpecError(); |
| 3136 | goto DoneWithDeclSpec; |
| 3137 | } |
John McCall | 8bc2a70 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 3138 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 3139 | goto DoneWithDeclSpec; |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3140 | continue; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3141 | |
| 3142 | case tok::annot_cxxscope: { |
Richard Smith | 3092a3b | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 3143 | if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector()) |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3144 | goto DoneWithDeclSpec; |
| 3145 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 3146 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 3147 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 3148 | Tok.getAnnotationRange(), |
| 3149 | SS); |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 3150 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3151 | // We are looking for a qualified typename. |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 3152 | Token Next = NextToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3153 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 3154 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3155 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 3156 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3157 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3158 | // If this would be a valid constructor declaration with template |
| 3159 | // arguments, we will reject the attempt to form an invalid type-id |
| 3160 | // referring to the injected-class-name when we annotate the token, |
| 3161 | // per C++ [class.qual]p2. |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3162 | // |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3163 | // To improve diagnostics for this case, parse the declaration as a |
| 3164 | // constructor (and reject the extra template arguments later). |
Argyrios Kyrtzidis | c0c5dd2 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 3165 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 3166 | if ((DSContext == DeclSpecContext::DSC_top_level || |
| 3167 | DSContext == DeclSpecContext::DSC_class) && |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 3168 | TemplateId->Name && |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3169 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS) && |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 3170 | isConstructorDeclarator(/*Unqualified*/ false)) { |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3171 | // The user meant this to be an out-of-line constructor |
| 3172 | // definition, but template arguments are not allowed |
| 3173 | // there. Just allow this as a constructor; we'll |
| 3174 | // complain about it later. |
| 3175 | goto DoneWithDeclSpec; |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 3178 | DS.getTypeSpecScope() = SS; |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3179 | ConsumeAnnotationToken(); // The C++ scope. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3180 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 3181 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3182 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 3183 | continue; |
| 3184 | } |
| 3185 | |
Douglas Gregor | c5790df | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 3186 | if (Next.is(tok::annot_typename)) { |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 3187 | DS.getTypeSpecScope() = SS; |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3188 | ConsumeAnnotationToken(); // The C++ scope. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3189 | if (Tok.getAnnotationValue()) { |
| 3190 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 7743034 | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 3191 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3192 | Tok.getAnnotationEndLoc(), |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3193 | PrevSpec, DiagID, T, Policy); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 3194 | if (isInvalid) |
| 3195 | break; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3196 | } |
Douglas Gregor | c5790df | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 3197 | else |
| 3198 | DS.SetTypeSpecError(); |
| 3199 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3200 | ConsumeAnnotationToken(); // The typename |
Douglas Gregor | c5790df | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 3201 | } |
| 3202 | |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 3203 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3204 | goto DoneWithDeclSpec; |
| 3205 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3206 | // Check whether this is a constructor declaration. If we're in a |
| 3207 | // context where the identifier could be a class name, and it has the |
| 3208 | // shape of a constructor declaration, process it as one. |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 3209 | if ((DSContext == DeclSpecContext::DSC_top_level || |
| 3210 | DSContext == DeclSpecContext::DSC_class) && |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3211 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3212 | &SS) && |
| 3213 | isConstructorDeclarator(/*Unqualified*/ false)) |
| 3214 | goto DoneWithDeclSpec; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3215 | |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 3216 | ParsedType TypeRep = |
| 3217 | Actions.getTypeName(*Next.getIdentifierInfo(), Next.getLocation(), |
| 3218 | getCurScope(), &SS, false, false, nullptr, |
| 3219 | /*IsCtorOrDtorName=*/false, |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 3220 | /*WantNontrivialTypeSourceInfo=*/true, |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 3221 | isClassTemplateDeductionContext(DSContext)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3222 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 3223 | // If the referenced identifier is not a type, then this declspec is |
| 3224 | // erroneous: We already checked about that it has no type specifier, and |
| 3225 | // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3226 | // typename. |
David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 3227 | if (!TypeRep) { |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3228 | // Eat the scope spec so the identifier is current. |
| 3229 | ConsumeAnnotationToken(); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3230 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 3231 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) { |
| 3232 | if (!Attrs.empty()) { |
| 3233 | AttrsLastTime = true; |
| 3234 | attrs.takeAllFrom(Attrs); |
| 3235 | } |
| 3236 | continue; |
| 3237 | } |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3238 | goto DoneWithDeclSpec; |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 3239 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3240 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 3241 | DS.getTypeSpecScope() = SS; |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3242 | ConsumeAnnotationToken(); // The C++ scope. |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3243 | |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3244 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3245 | DiagID, TypeRep, Policy); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3246 | if (isInvalid) |
| 3247 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3248 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3249 | DS.SetRangeEnd(Tok.getLocation()); |
| 3250 | ConsumeToken(); // The typename. |
| 3251 | |
| 3252 | continue; |
| 3253 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3254 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3255 | case tok::annot_typename: { |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 3256 | // If we've previously seen a tag definition, we were almost surely |
| 3257 | // missing a semicolon after it. |
| 3258 | if (DS.hasTypeSpecifier() && DS.hasTagDefinition()) |
| 3259 | goto DoneWithDeclSpec; |
| 3260 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3261 | if (Tok.getAnnotationValue()) { |
| 3262 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 7f8bb36 | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 3263 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3264 | DiagID, T, Policy); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3265 | } else |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 3266 | DS.SetTypeSpecError(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3267 | |
Chris Lattner | 005fc1b | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 3268 | if (isInvalid) |
| 3269 | break; |
| 3270 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3271 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3272 | ConsumeAnnotationToken(); // The typename |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3273 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3274 | continue; |
| 3275 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3276 | |
Douglas Gregor | 0687309 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 3277 | case tok::kw___is_signed: |
| 3278 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 3279 | // typically treats it as a trait. If we see __is_signed as it appears |
| 3280 | // in libstdc++, e.g., |
| 3281 | // |
| 3282 | // static const bool __is_signed; |
| 3283 | // |
| 3284 | // then treat __is_signed as an identifier rather than as a keyword. |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 3285 | if (DS.getTypeSpecType() == TST_bool && |
Douglas Gregor | 0687309 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 3286 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
Alp Toker | 47642d2 | 2013-12-03 06:13:01 +0000 | [diff] [blame] | 3287 | DS.getStorageClassSpec() == DeclSpec::SCS_static) |
| 3288 | TryKeywordIdentFallback(true); |
Douglas Gregor | 0687309 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 3289 | |
| 3290 | // We're done with the declaration-specifiers. |
| 3291 | goto DoneWithDeclSpec; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3292 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3293 | // typedef-name |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 3294 | case tok::kw___super: |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3295 | case tok::kw_decltype: |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3296 | case tok::identifier: { |
Reid Kleckner | c582f01 | 2014-07-14 18:19:58 +0000 | [diff] [blame] | 3297 | // This identifier can only be a typedef name if we haven't already seen |
| 3298 | // a type-specifier. Without this check we misparse: |
| 3299 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 3300 | if (DS.hasTypeSpecifier()) |
| 3301 | goto DoneWithDeclSpec; |
| 3302 | |
Aaron Ballman | 52d0aaa | 2017-02-14 22:47:20 +0000 | [diff] [blame] | 3303 | // If the token is an identifier named "__declspec" and Microsoft |
| 3304 | // extensions are not enabled, it is likely that there will be cascading |
| 3305 | // parse errors if this really is a __declspec attribute. Attempt to |
| 3306 | // recognize that scenario and recover gracefully. |
| 3307 | if (!getLangOpts().DeclSpecKeyword && Tok.is(tok::identifier) && |
| 3308 | Tok.getIdentifierInfo()->getName().equals("__declspec")) { |
| 3309 | Diag(Loc, diag::err_ms_attributes_not_enabled); |
| 3310 | |
| 3311 | // The next token should be an open paren. If it is, eat the entire |
| 3312 | // attribute declaration and continue. |
| 3313 | if (NextToken().is(tok::l_paren)) { |
| 3314 | // Consume the __declspec identifier. |
Richard Trieu | ba05737 | 2017-02-14 23:56:55 +0000 | [diff] [blame] | 3315 | ConsumeToken(); |
Aaron Ballman | 52d0aaa | 2017-02-14 22:47:20 +0000 | [diff] [blame] | 3316 | |
| 3317 | // Eat the parens and everything between them. |
| 3318 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 3319 | if (T.consumeOpen()) { |
| 3320 | assert(false && "Not a left paren?"); |
| 3321 | return; |
| 3322 | } |
| 3323 | T.skipToEnd(); |
| 3324 | continue; |
| 3325 | } |
| 3326 | } |
| 3327 | |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 3328 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 3329 | // so handle it as such. This is important for ctor parsing. |
| 3330 | if (getLangOpts().CPlusPlus) { |
| 3331 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
| 3332 | DS.SetTypeSpecError(); |
| 3333 | goto DoneWithDeclSpec; |
| 3334 | } |
| 3335 | if (!Tok.is(tok::identifier)) |
| 3336 | continue; |
| 3337 | } |
| 3338 | |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3339 | // Check for need to substitute AltiVec keyword tokens. |
| 3340 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 3341 | break; |
| 3342 | |
Richard Smith | 3092a3b | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 3343 | // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not |
| 3344 | // allow the use of a typedef name as a type specifier. |
| 3345 | if (DS.isTypeAltiVecVector()) |
| 3346 | goto DoneWithDeclSpec; |
| 3347 | |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 3348 | if (DSContext == DeclSpecContext::DSC_objc_method_result && |
| 3349 | isObjCInstancetype()) { |
Douglas Gregor | 5c0870a | 2015-06-19 23:18:00 +0000 | [diff] [blame] | 3350 | ParsedType TypeRep = Actions.ActOnObjCInstanceType(Loc); |
| 3351 | assert(TypeRep); |
| 3352 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
| 3353 | DiagID, TypeRep, Policy); |
| 3354 | if (isInvalid) |
| 3355 | break; |
| 3356 | |
| 3357 | DS.SetRangeEnd(Loc); |
| 3358 | ConsumeToken(); |
| 3359 | continue; |
| 3360 | } |
| 3361 | |
Richard Smith | 715ee07 | 2018-06-20 21:58:20 +0000 | [diff] [blame] | 3362 | // If we're in a context where the identifier could be a class name, |
| 3363 | // check whether this is a constructor declaration. |
| 3364 | if (getLangOpts().CPlusPlus && DSContext == DeclSpecContext::DSC_class && |
| 3365 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
| 3366 | isConstructorDeclarator(/*Unqualified*/true)) |
| 3367 | goto DoneWithDeclSpec; |
| 3368 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 3369 | ParsedType TypeRep = Actions.getTypeName( |
| 3370 | *Tok.getIdentifierInfo(), Tok.getLocation(), getCurScope(), nullptr, |
| 3371 | false, false, nullptr, false, false, |
| 3372 | isClassTemplateDeductionContext(DSContext)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3373 | |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 3374 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 3375 | // it must be an implicit int or an error. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3376 | if (!TypeRep) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3377 | ParsedAttributesWithRange Attrs(AttrFactory); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 3378 | if (ParseImplicitInt(DS, nullptr, TemplateInfo, AS, DSContext, Attrs)) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3379 | if (!Attrs.empty()) { |
| 3380 | AttrsLastTime = true; |
| 3381 | attrs.takeAllFrom(Attrs); |
| 3382 | } |
| 3383 | continue; |
| 3384 | } |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3385 | goto DoneWithDeclSpec; |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 3386 | } |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3387 | |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 3388 | // Likewise, if this is a context where the identifier could be a template |
| 3389 | // name, check whether this is a deduction guide declaration. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3390 | if (getLangOpts().CPlusPlus17 && |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 3391 | (DSContext == DeclSpecContext::DSC_class || |
| 3392 | DSContext == DeclSpecContext::DSC_top_level) && |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 3393 | Actions.isDeductionGuideName(getCurScope(), *Tok.getIdentifierInfo(), |
| 3394 | Tok.getLocation()) && |
| 3395 | isConstructorDeclarator(/*Unqualified*/ true, |
| 3396 | /*DeductionGuide*/ true)) |
| 3397 | goto DoneWithDeclSpec; |
| 3398 | |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3399 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3400 | DiagID, TypeRep, Policy); |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3401 | if (isInvalid) |
| 3402 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3403 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3404 | DS.SetRangeEnd(Tok.getLocation()); |
| 3405 | ConsumeToken(); // The identifier |
| 3406 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3407 | // Objective-C supports type arguments and protocol references |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 3408 | // following an Objective-C object or object pointer |
| 3409 | // type. Handle either one of them. |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 3410 | if (Tok.is(tok::less) && getLangOpts().ObjC) { |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 3411 | SourceLocation NewEndLoc; |
| 3412 | TypeResult NewTypeRep = parseObjCTypeArgsAndProtocolQualifiers( |
| 3413 | Loc, TypeRep, /*consumeLastToken=*/true, |
| 3414 | NewEndLoc); |
| 3415 | if (NewTypeRep.isUsable()) { |
| 3416 | DS.UpdateTypeRep(NewTypeRep.get()); |
| 3417 | DS.SetRangeEnd(NewEndLoc); |
| 3418 | } |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3419 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3420 | |
Steve Naroff | cd5e782 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 3421 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 3422 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 3423 | continue; |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3424 | } |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3425 | |
| 3426 | // type-name |
| 3427 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | c0c5dd2 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 3428 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 3429 | if (TemplateId->Kind != TNK_Type_template && |
| 3430 | TemplateId->Kind != TNK_Undeclared_template) { |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3431 | // This template-id does not refer to a type name, so we're |
| 3432 | // done with the type-specifiers. |
| 3433 | goto DoneWithDeclSpec; |
| 3434 | } |
| 3435 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3436 | // If we're in a context where the template-id could be a |
| 3437 | // constructor name or specialization, check whether this is a |
| 3438 | // constructor declaration. |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 3439 | if (getLangOpts().CPlusPlus && DSContext == DeclSpecContext::DSC_class && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3440 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 3441 | isConstructorDeclarator(TemplateId->SS.isEmpty())) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3442 | goto DoneWithDeclSpec; |
| 3443 | |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3444 | // Turn the template-id annotation token into a type annotation |
| 3445 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 3446 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3447 | continue; |
| 3448 | } |
| 3449 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 3450 | // GNU attributes support. |
| 3451 | case tok::kw___attribute: |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 3452 | ParseGNUAttributes(DS.getAttributes(), nullptr, LateAttrs); |
Chris Lattner | b95cca0 | 2006-10-17 03:01:08 +0000 | [diff] [blame] | 3453 | continue; |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 3454 | |
| 3455 | // Microsoft declspec support. |
| 3456 | case tok::kw___declspec: |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 3457 | ParseMicrosoftDeclSpecs(DS.getAttributes()); |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 3458 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3459 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3460 | // Microsoft single token adornments. |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 3461 | case tok::kw___forceinline: { |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 3462 | isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID); |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 3463 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 3464 | SourceLocation AttrNameLoc = Tok.getLocation(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 3465 | DS.getAttributes().addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3466 | nullptr, 0, ParsedAttr::AS_Keyword); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 3467 | break; |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 3468 | } |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3469 | |
Andrey Bokhanko | 45d4132 | 2016-05-11 18:38:21 +0000 | [diff] [blame] | 3470 | case tok::kw___unaligned: |
| 3471 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID, |
| 3472 | getLangOpts()); |
| 3473 | break; |
| 3474 | |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 3475 | case tok::kw___sptr: |
| 3476 | case tok::kw___uptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3477 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3478 | case tok::kw___ptr32: |
Steve Naroff | f9c29d4 | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 3479 | case tok::kw___w64: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3480 | case tok::kw___cdecl: |
| 3481 | case tok::kw___stdcall: |
| 3482 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3483 | case tok::kw___thiscall: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3484 | case tok::kw___regcall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 3485 | case tok::kw___vectorcall: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3486 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3487 | continue; |
| 3488 | |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3489 | // Borland single token adornments. |
| 3490 | case tok::kw___pascal: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3491 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3492 | continue; |
| 3493 | |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3494 | // OpenCL single token adornments. |
| 3495 | case tok::kw___kernel: |
Anastasia Stulova | 6bdbcbb | 2016-02-19 18:30:11 +0000 | [diff] [blame] | 3496 | ParseOpenCLKernelAttributes(DS.getAttributes()); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3497 | continue; |
| 3498 | |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 3499 | // Nullability type specifiers. |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 3500 | case tok::kw__Nonnull: |
| 3501 | case tok::kw__Nullable: |
| 3502 | case tok::kw__Null_unspecified: |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 3503 | ParseNullabilityTypeSpecifiers(DS.getAttributes()); |
| 3504 | continue; |
| 3505 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 3506 | // Objective-C 'kindof' types. |
| 3507 | case tok::kw___kindof: |
| 3508 | DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3509 | nullptr, 0, ParsedAttr::AS_Keyword); |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 3510 | (void)ConsumeToken(); |
| 3511 | continue; |
| 3512 | |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3513 | // storage-class-specifier |
| 3514 | case tok::kw_typedef: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3515 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3516 | PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3517 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3518 | break; |
| 3519 | case tok::kw_extern: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3520 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3521 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3522 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3523 | PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3524 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3525 | break; |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 3526 | case tok::kw___private_extern__: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3527 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3528 | Loc, PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3529 | isStorageClass = true; |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 3530 | break; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3531 | case tok::kw_static: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3532 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3533 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3534 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3535 | PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3536 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3537 | break; |
| 3538 | case tok::kw_auto: |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3539 | if (getLangOpts().CPlusPlus11) { |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3540 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3541 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3542 | PrevSpec, DiagID, Policy); |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3543 | if (!isInvalid) |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 3544 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3545 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 3546 | } else |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3547 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3548 | DiagID, Policy); |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 3549 | } else |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3550 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3551 | PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3552 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3553 | break; |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 3554 | case tok::kw___auto_type: |
| 3555 | Diag(Tok, diag::ext_auto_type); |
| 3556 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto_type, Loc, PrevSpec, |
| 3557 | DiagID, Policy); |
| 3558 | break; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3559 | case tok::kw_register: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3560 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3561 | PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3562 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3563 | break; |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 3564 | case tok::kw_mutable: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3565 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3566 | PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3567 | isStorageClass = true; |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 3568 | break; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3569 | case tok::kw___thread: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3570 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc, |
| 3571 | PrevSpec, DiagID); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3572 | isStorageClass = true; |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3573 | break; |
| 3574 | case tok::kw_thread_local: |
| 3575 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc, |
| 3576 | PrevSpec, DiagID); |
Sven van Haastregt | c410083 | 2018-04-24 14:47:29 +0000 | [diff] [blame] | 3577 | isStorageClass = true; |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3578 | break; |
| 3579 | case tok::kw__Thread_local: |
Aaron Ballman | 774bd6e | 2019-08-26 19:44:07 +0000 | [diff] [blame] | 3580 | if (!getLangOpts().C11) |
| 3581 | Diag(Tok, diag::ext_c11_feature) << Tok.getName(); |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3582 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local, |
| 3583 | Loc, PrevSpec, DiagID); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3584 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3585 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3586 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3587 | // function-specifier |
| 3588 | case tok::kw_inline: |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 3589 | isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3590 | break; |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3591 | case tok::kw_virtual: |
Anastasia Stulova | 46b55fa | 2019-07-18 10:02:35 +0000 | [diff] [blame] | 3592 | // C++ for OpenCL does not allow virtual function qualifier, to avoid |
| 3593 | // function pointers restricted in OpenCL v2.0 s6.9.a. |
Sven van Haastregt | 49ffffb | 2018-04-23 11:23:47 +0000 | [diff] [blame] | 3594 | if (getLangOpts().OpenCLCPlusPlus) { |
| 3595 | DiagID = diag::err_openclcxx_virtual_function; |
| 3596 | PrevSpec = Tok.getIdentifierInfo()->getNameStart(); |
| 3597 | isInvalid = true; |
| 3598 | } |
| 3599 | else { |
| 3600 | isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
| 3601 | } |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3602 | break; |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 3603 | case tok::kw_explicit: { |
| 3604 | SourceLocation ExplicitLoc = Loc; |
| 3605 | SourceLocation CloseParenLoc; |
| 3606 | ExplicitSpecifier ExplicitSpec(nullptr, ExplicitSpecKind::ResolvedTrue); |
Richard Smith | ba24f35 | 2019-05-09 19:45:46 +0000 | [diff] [blame] | 3607 | ConsumedEnd = ExplicitLoc; |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 3608 | ConsumeToken(); // kw_explicit |
| 3609 | if (Tok.is(tok::l_paren)) { |
| 3610 | if (getLangOpts().CPlusPlus2a) { |
| 3611 | ExprResult ExplicitExpr(static_cast<Expr *>(nullptr)); |
| 3612 | BalancedDelimiterTracker Tracker(*this, tok::l_paren); |
| 3613 | Tracker.consumeOpen(); |
| 3614 | ExplicitExpr = ParseConstantExpression(); |
Richard Smith | ba24f35 | 2019-05-09 19:45:46 +0000 | [diff] [blame] | 3615 | ConsumedEnd = Tok.getLocation(); |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 3616 | if (ExplicitExpr.isUsable()) { |
| 3617 | CloseParenLoc = Tok.getLocation(); |
| 3618 | Tracker.consumeClose(); |
| 3619 | ExplicitSpec = |
| 3620 | Actions.ActOnExplicitBoolSpecifier(ExplicitExpr.get()); |
| 3621 | } else |
| 3622 | Tracker.skipToEnd(); |
| 3623 | } else |
| 3624 | Diag(Tok.getLocation(), diag::warn_cxx2a_compat_explicit_bool); |
| 3625 | } |
| 3626 | isInvalid = DS.setFunctionSpecExplicit(ExplicitLoc, PrevSpec, DiagID, |
| 3627 | ExplicitSpec, CloseParenLoc); |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3628 | break; |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 3629 | } |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 3630 | case tok::kw__Noreturn: |
| 3631 | if (!getLangOpts().C11) |
Aaron Ballman | 1d93522 | 2019-08-27 14:41:39 +0000 | [diff] [blame] | 3632 | Diag(Tok, diag::ext_c11_feature) << Tok.getName(); |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 3633 | isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID); |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 3634 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3635 | |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 3636 | // alignment-specifier |
| 3637 | case tok::kw__Alignas: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3638 | if (!getLangOpts().C11) |
Aaron Ballman | 774bd6e | 2019-08-26 19:44:07 +0000 | [diff] [blame] | 3639 | Diag(Tok, diag::ext_c11_feature) << Tok.getName(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 3640 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 3641 | continue; |
| 3642 | |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 3643 | // friend |
| 3644 | case tok::kw_friend: |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 3645 | if (DSContext == DeclSpecContext::DSC_class) |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3646 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 3647 | else { |
| 3648 | PrevSpec = ""; // not actually used by the diagnostic |
| 3649 | DiagID = diag::err_friend_invalid_in_context; |
| 3650 | isInvalid = true; |
| 3651 | } |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 3652 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3653 | |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 3654 | // Modules |
| 3655 | case tok::kw___module_private__: |
| 3656 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 3657 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3658 | |
Richard Smith | a6e8b68 | 2019-09-04 20:30:37 +0000 | [diff] [blame] | 3659 | // constexpr, consteval, constinit specifiers |
Sebastian Redl | 39c2a8b | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 3660 | case tok::kw_constexpr: |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 3661 | isInvalid = DS.SetConstexprSpec(CSK_constexpr, Loc, PrevSpec, DiagID); |
| 3662 | break; |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 3663 | case tok::kw_consteval: |
| 3664 | isInvalid = DS.SetConstexprSpec(CSK_consteval, Loc, PrevSpec, DiagID); |
Sebastian Redl | 39c2a8b | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 3665 | break; |
Richard Smith | a6e8b68 | 2019-09-04 20:30:37 +0000 | [diff] [blame] | 3666 | case tok::kw_constinit: |
| 3667 | isInvalid = DS.SetConstexprSpec(CSK_constinit, Loc, PrevSpec, DiagID); |
| 3668 | break; |
Sebastian Redl | 39c2a8b | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 3669 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3670 | // type-specifier |
| 3671 | case tok::kw_short: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3672 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3673 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3674 | break; |
| 3675 | case tok::kw_long: |
| 3676 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3677 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3678 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3679 | else |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3680 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3681 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3682 | break; |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3683 | case tok::kw___int64: |
| 3684 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3685 | DiagID, Policy); |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3686 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3687 | case tok::kw_signed: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3688 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 3689 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3690 | break; |
| 3691 | case tok::kw_unsigned: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3692 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 3693 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3694 | break; |
| 3695 | case tok::kw__Complex: |
Aaron Ballman | 9fac4a5 | 2019-08-27 19:15:24 +0000 | [diff] [blame] | 3696 | if (!getLangOpts().C99) |
| 3697 | Diag(Tok, diag::ext_c99_feature) << Tok.getName(); |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3698 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 3699 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3700 | break; |
| 3701 | case tok::kw__Imaginary: |
Aaron Ballman | 9fac4a5 | 2019-08-27 19:15:24 +0000 | [diff] [blame] | 3702 | if (!getLangOpts().C99) |
| 3703 | Diag(Tok, diag::ext_c99_feature) << Tok.getName(); |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3704 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 3705 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3706 | break; |
| 3707 | case tok::kw_void: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3708 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3709 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3710 | break; |
| 3711 | case tok::kw_char: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3712 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3713 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3714 | break; |
| 3715 | case tok::kw_int: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3716 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3717 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3718 | break; |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3719 | case tok::kw___int128: |
| 3720 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3721 | DiagID, Policy); |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3722 | break; |
| 3723 | case tok::kw_half: |
| 3724 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3725 | DiagID, Policy); |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3726 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3727 | case tok::kw_float: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3728 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3729 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3730 | break; |
| 3731 | case tok::kw_double: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3732 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3733 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3734 | break; |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 3735 | case tok::kw__Float16: |
| 3736 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float16, Loc, PrevSpec, |
| 3737 | DiagID, Policy); |
| 3738 | break; |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 3739 | case tok::kw__Accum: |
| 3740 | if (!getLangOpts().FixedPoint) { |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 3741 | SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid); |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 3742 | } else { |
| 3743 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_accum, Loc, PrevSpec, |
| 3744 | DiagID, Policy); |
| 3745 | } |
| 3746 | break; |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 3747 | case tok::kw__Fract: |
| 3748 | if (!getLangOpts().FixedPoint) { |
| 3749 | SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid); |
| 3750 | } else { |
| 3751 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_fract, Loc, PrevSpec, |
| 3752 | DiagID, Policy); |
| 3753 | } |
| 3754 | break; |
| 3755 | case tok::kw__Sat: |
| 3756 | if (!getLangOpts().FixedPoint) { |
| 3757 | SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid); |
| 3758 | } else { |
| 3759 | isInvalid = DS.SetTypeSpecSat(Loc, PrevSpec, DiagID); |
| 3760 | } |
| 3761 | break; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 3762 | case tok::kw___float128: |
| 3763 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float128, Loc, PrevSpec, |
| 3764 | DiagID, Policy); |
| 3765 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3766 | case tok::kw_wchar_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3767 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3768 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3769 | break; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 3770 | case tok::kw_char8_t: |
| 3771 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char8, Loc, PrevSpec, |
| 3772 | DiagID, Policy); |
| 3773 | break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3774 | case tok::kw_char16_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3775 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3776 | DiagID, Policy); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3777 | break; |
| 3778 | case tok::kw_char32_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3779 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3780 | DiagID, Policy); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3781 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3782 | case tok::kw_bool: |
| 3783 | case tok::kw__Bool: |
Aaron Ballman | 27e66bf | 2019-08-27 20:33:05 +0000 | [diff] [blame] | 3784 | if (Tok.is(tok::kw__Bool) && !getLangOpts().C99) |
| 3785 | Diag(Tok, diag::ext_c99_feature) << Tok.getName(); |
| 3786 | |
Argyrios Kyrtzidis | 20ee5ae | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3787 | if (Tok.is(tok::kw_bool) && |
| 3788 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 3789 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 3790 | PrevSpec = ""; // Not used by the diagnostic. |
| 3791 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | 2b05999 | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3792 | // For better error recovery. |
| 3793 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 20ee5ae | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3794 | isInvalid = true; |
| 3795 | } else { |
| 3796 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3797 | DiagID, Policy); |
Argyrios Kyrtzidis | 20ee5ae | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3798 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3799 | break; |
| 3800 | case tok::kw__Decimal32: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3801 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3802 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3803 | break; |
| 3804 | case tok::kw__Decimal64: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3805 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3806 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3807 | break; |
| 3808 | case tok::kw__Decimal128: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3809 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3810 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3811 | break; |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3812 | case tok::kw___vector: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3813 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy); |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3814 | break; |
| 3815 | case tok::kw___pixel: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3816 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy); |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3817 | break; |
Bill Seurer | cf2c96b | 2015-01-12 19:35:51 +0000 | [diff] [blame] | 3818 | case tok::kw___bool: |
| 3819 | isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy); |
| 3820 | break; |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3821 | case tok::kw_pipe: |
Sven van Haastregt | e518bb4 | 2019-05-22 13:12:20 +0000 | [diff] [blame] | 3822 | if (!getLangOpts().OpenCL || (getLangOpts().OpenCLVersion < 200 && |
| 3823 | !getLangOpts().OpenCLCPlusPlus)) { |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3824 | // OpenCL 2.0 defined this keyword. OpenCL 1.2 and earlier should |
| 3825 | // support the "pipe" word as identifier. |
| 3826 | Tok.getIdentifierInfo()->revertTokenIDToIdentifier(); |
| 3827 | goto DoneWithDeclSpec; |
| 3828 | } |
| 3829 | isInvalid = DS.SetTypePipe(true, Loc, PrevSpec, DiagID, Policy); |
| 3830 | break; |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 3831 | #define GENERIC_IMAGE_TYPE(ImgType, Id) \ |
| 3832 | case tok::kw_##ImgType##_t: \ |
| 3833 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_##ImgType##_t, Loc, PrevSpec, \ |
| 3834 | DiagID, Policy); \ |
| 3835 | break; |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 3836 | #include "clang/Basic/OpenCLImageTypes.def" |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3837 | case tok::kw___unknown_anytype: |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 3838 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3839 | PrevSpec, DiagID, Policy); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3840 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3841 | |
| 3842 | // class-specifier: |
| 3843 | case tok::kw_class: |
| 3844 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3845 | case tok::kw___interface: |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3846 | case tok::kw_union: { |
| 3847 | tok::TokenKind Kind = Tok.getKind(); |
| 3848 | ConsumeToken(); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3849 | |
| 3850 | // These are attributes following class specifiers. |
| 3851 | // To produce better diagnostic, we parse them when |
| 3852 | // parsing class specifier. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3853 | ParsedAttributesWithRange Attributes(AttrFactory); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3854 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3855 | EnteringContext, DSContext, Attributes); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3856 | |
| 3857 | // If there are attributes following class specifier, |
| 3858 | // take them over and handle them here. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3859 | if (!Attributes.empty()) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3860 | AttrsLastTime = true; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3861 | attrs.takeAllFrom(Attributes); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3862 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3863 | continue; |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3864 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3865 | |
| 3866 | // enum-specifier: |
| 3867 | case tok::kw_enum: |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3868 | ConsumeToken(); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3869 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3870 | continue; |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 3871 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3872 | // cv-qualifier: |
| 3873 | case tok::kw_const: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3874 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3875 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3876 | break; |
| 3877 | case tok::kw_volatile: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3878 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3879 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3880 | break; |
| 3881 | case tok::kw_restrict: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3882 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3883 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3884 | break; |
| 3885 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3886 | // C++ typename-specifier: |
| 3887 | case tok::kw_typename: |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3888 | if (TryAnnotateTypeOrScopeToken()) { |
| 3889 | DS.SetTypeSpecError(); |
| 3890 | goto DoneWithDeclSpec; |
| 3891 | } |
| 3892 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3893 | continue; |
| 3894 | break; |
| 3895 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3896 | // GNU typeof support. |
| 3897 | case tok::kw_typeof: |
| 3898 | ParseTypeofSpecifier(DS); |
| 3899 | continue; |
| 3900 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3901 | case tok::annot_decltype: |
Anders Carlsson | 74948d0 | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 3902 | ParseDecltypeSpecifier(DS); |
| 3903 | continue; |
| 3904 | |
David Majnemer | 15b311c | 2016-06-14 03:20:28 +0000 | [diff] [blame] | 3905 | case tok::annot_pragma_pack: |
| 3906 | HandlePragmaPack(); |
| 3907 | continue; |
| 3908 | |
| 3909 | case tok::annot_pragma_ms_pragma: |
| 3910 | HandlePragmaMSPragma(); |
| 3911 | continue; |
| 3912 | |
| 3913 | case tok::annot_pragma_ms_vtordisp: |
| 3914 | HandlePragmaMSVtorDisp(); |
| 3915 | continue; |
| 3916 | |
| 3917 | case tok::annot_pragma_ms_pointers_to_members: |
| 3918 | HandlePragmaMSPointersToMembers(); |
| 3919 | continue; |
| 3920 | |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3921 | case tok::kw___underlying_type: |
| 3922 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3923 | continue; |
| 3924 | |
| 3925 | case tok::kw__Atomic: |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3926 | // C11 6.7.2.4/4: |
| 3927 | // If the _Atomic keyword is immediately followed by a left parenthesis, |
| 3928 | // it is interpreted as a type specifier (with a type name), not as a |
| 3929 | // type qualifier. |
Aaron Ballman | 5cd5d56 | 2019-09-04 21:01:57 +0000 | [diff] [blame] | 3930 | if (!getLangOpts().C11) |
| 3931 | Diag(Tok, diag::ext_c11_feature) << Tok.getName(); |
| 3932 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3933 | if (NextToken().is(tok::l_paren)) { |
| 3934 | ParseAtomicSpecifier(DS); |
| 3935 | continue; |
| 3936 | } |
| 3937 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 3938 | getLangOpts()); |
| 3939 | break; |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3940 | |
Sven van Haastregt | 2ca6ba1 | 2018-05-09 13:16:17 +0000 | [diff] [blame] | 3941 | // OpenCL address space qualifiers: |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3942 | case tok::kw___generic: |
| 3943 | // generic address space is introduced only in OpenCL v2.0 |
| 3944 | // see OpenCL C Spec v2.0 s6.5.5 |
Sven van Haastregt | 2ca6ba1 | 2018-05-09 13:16:17 +0000 | [diff] [blame] | 3945 | if (Actions.getLangOpts().OpenCLVersion < 200 && |
| 3946 | !Actions.getLangOpts().OpenCLCPlusPlus) { |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3947 | DiagID = diag::err_opencl_unknown_type_specifier; |
| 3948 | PrevSpec = Tok.getIdentifierInfo()->getNameStart(); |
| 3949 | isInvalid = true; |
| 3950 | break; |
| 3951 | }; |
Galina Kistanova | 7767425 | 2017-06-01 21:15:34 +0000 | [diff] [blame] | 3952 | LLVM_FALLTHROUGH; |
Anastasia Stulova | 948e37c | 2019-03-25 11:54:02 +0000 | [diff] [blame] | 3953 | case tok::kw_private: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3954 | case tok::kw___private: |
| 3955 | case tok::kw___global: |
| 3956 | case tok::kw___local: |
| 3957 | case tok::kw___constant: |
Anastasia Stulova | 2c4730d | 2019-02-15 12:07:57 +0000 | [diff] [blame] | 3958 | // OpenCL access qualifiers: |
| 3959 | case tok::kw___read_only: |
| 3960 | case tok::kw___write_only: |
| 3961 | case tok::kw___read_write: |
Aaron Ballman | 05d76ea | 2014-01-14 01:29:54 +0000 | [diff] [blame] | 3962 | ParseOpenCLQualifiers(DS.getAttributes()); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3963 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3964 | |
Steve Naroff | cfdf616 | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 3965 | case tok::less: |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3966 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3967 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 3968 | // but we support it. |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 3969 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC) |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3970 | goto DoneWithDeclSpec; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3971 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 3972 | SourceLocation StartLoc = Tok.getLocation(); |
| 3973 | SourceLocation EndLoc; |
| 3974 | TypeResult Type = parseObjCProtocolQualifierType(EndLoc); |
| 3975 | if (Type.isUsable()) { |
| 3976 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, StartLoc, |
| 3977 | PrevSpec, DiagID, Type.get(), |
| 3978 | Actions.getASTContext().getPrintingPolicy())) |
| 3979 | Diag(StartLoc, DiagID) << PrevSpec; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3980 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 3981 | DS.SetRangeEnd(EndLoc); |
| 3982 | } else { |
| 3983 | DS.SetTypeSpecError(); |
| 3984 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3985 | |
Douglas Gregor | 06e41ae | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 3986 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 3987 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 3988 | continue; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 3989 | } |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 3990 | |
Richard Smith | ba24f35 | 2019-05-09 19:45:46 +0000 | [diff] [blame] | 3991 | DS.SetRangeEnd(ConsumedEnd.isValid() ? ConsumedEnd : Tok.getLocation()); |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 3992 | |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3993 | // If the specifier wasn't legal, issue a diagnostic. |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3994 | if (isInvalid) { |
| 3995 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3996 | assert(DiagID); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3997 | |
Nick Desaulniers | 150ca53 | 2018-10-03 23:09:29 +0000 | [diff] [blame] | 3998 | if (DiagID == diag::ext_duplicate_declspec || |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 3999 | DiagID == diag::ext_warn_duplicate_declspec || |
| 4000 | DiagID == diag::err_duplicate_declspec) |
| 4001 | Diag(Loc, DiagID) << PrevSpec |
| 4002 | << FixItHint::CreateRemoval( |
| 4003 | SourceRange(Loc, DS.getEndLoc())); |
Anastasia Stulova | 43ab9a0 | 2016-05-12 16:28:25 +0000 | [diff] [blame] | 4004 | else if (DiagID == diag::err_opencl_unknown_type_specifier) { |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 4005 | Diag(Loc, DiagID) << getLangOpts().OpenCLCPlusPlus |
| 4006 | << getLangOpts().getOpenCLVersionTuple().getAsString() |
| 4007 | << PrevSpec << isStorageClass; |
Anastasia Stulova | 43ab9a0 | 2016-05-12 16:28:25 +0000 | [diff] [blame] | 4008 | } else |
Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 4009 | Diag(Loc, DiagID) << PrevSpec; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 4010 | } |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 4011 | |
Richard Smith | ba24f35 | 2019-05-09 19:45:46 +0000 | [diff] [blame] | 4012 | if (DiagID != diag::err_bool_redeclaration && ConsumedEnd.isInvalid()) |
Volodymyr Sapsai | 9f7b5cc | 2018-04-10 18:29:47 +0000 | [diff] [blame] | 4013 | // After an error the next token can be an annotation token. |
| 4014 | ConsumeAnyToken(); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 4015 | |
| 4016 | AttrsLastTime = false; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4017 | } |
| 4018 | } |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 4019 | |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 4020 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 4021 | /// semicolon. |
| 4022 | /// |
Nico Weber | 98dd085 | 2019-03-14 14:18:56 +0000 | [diff] [blame] | 4023 | /// Note that a struct declaration refers to a declaration in a struct, |
| 4024 | /// not to the declaration of a struct. |
| 4025 | /// |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 4026 | /// struct-declaration: |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 4027 | /// [C2x] attributes-specifier-seq[opt] |
| 4028 | /// specifier-qualifier-list struct-declarator-list |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 4029 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 4030 | /// [GNU] specifier-qualifier-list |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 4031 | /// struct-declarator-list: |
| 4032 | /// struct-declarator |
| 4033 | /// struct-declarator-list ',' struct-declarator |
| 4034 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 4035 | /// struct-declarator: |
| 4036 | /// declarator |
| 4037 | /// [GNU] declarator attributes[opt] |
| 4038 | /// declarator[opt] ':' constant-expression |
| 4039 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 4040 | /// |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 4041 | void Parser::ParseStructDeclaration( |
| 4042 | ParsingDeclSpec &DS, |
| 4043 | llvm::function_ref<void(ParsingFieldDeclarator &)> FieldsCallback) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4044 | |
Chris Lattner | f02ef3e | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 4045 | if (Tok.is(tok::kw___extension__)) { |
| 4046 | // __extension__ silences extension warnings in the subexpression. |
| 4047 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 4048 | ConsumeToken(); |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 4049 | return ParseStructDeclaration(DS, FieldsCallback); |
Chris Lattner | f02ef3e | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 4050 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4051 | |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 4052 | // Parse leading attributes. |
| 4053 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 4054 | MaybeParseCXX11Attributes(Attrs); |
| 4055 | DS.takeAttributesFrom(Attrs); |
| 4056 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 4057 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 4058 | ParseSpecifierQualifierList(DS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4059 | |
Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 4060 | // If there are no declarators, this is a free-standing declaration |
| 4061 | // specifier. Let the actions module cope with it. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4062 | if (Tok.is(tok::semi)) { |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 4063 | RecordDecl *AnonRecord = nullptr; |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 4064 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 4065 | DS, AnonRecord); |
| 4066 | assert(!AnonRecord && "Did not expect anonymous struct or union here"); |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 4067 | DS.complete(TheDecl); |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 4068 | return; |
| 4069 | } |
| 4070 | |
| 4071 | // Read struct-declarators until we find the semicolon. |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 4072 | bool FirstDeclarator = true; |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 4073 | SourceLocation CommaLoc; |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 4074 | while (1) { |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 4075 | ParsingFieldDeclarator DeclaratorInfo(*this, DS); |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 4076 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 4077 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4078 | // Attributes are only allowed here on successive declarators. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4079 | if (!FirstDeclarator) |
| 4080 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4081 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 4082 | /// struct-declarator: declarator |
| 4083 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | 17c3b1f | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 4084 | if (Tok.isNot(tok::colon)) { |
| 4085 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 4086 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 4087 | ParseDeclarator(DeclaratorInfo.D); |
Richard Smith | 3d1a94c | 2014-08-12 00:22:39 +0000 | [diff] [blame] | 4088 | } else |
| 4089 | DeclaratorInfo.D.SetIdentifier(nullptr, Tok.getLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4090 | |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 4091 | if (TryConsumeToken(tok::colon)) { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4092 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 4093 | if (Res.isInvalid()) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4094 | SkipUntil(tok::semi, StopBeforeMatch); |
Chris Lattner | 32295d3 | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 4095 | else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4096 | DeclaratorInfo.BitfieldSize = Res.get(); |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 4097 | } |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4098 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 4099 | // If attributes exist after the declarator, parse them. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4100 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4101 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 4102 | // We're done with this declarator; invoke the callback. |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 4103 | FieldsCallback(DeclaratorInfo); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 4104 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 4105 | // If we don't have a comma, it is either the end of the list (a ';') |
| 4106 | // or an error, bail out. |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 4107 | if (!TryConsumeToken(tok::comma, CommaLoc)) |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 4108 | return; |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4109 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 4110 | FirstDeclarator = false; |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 4111 | } |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 4112 | } |
| 4113 | |
| 4114 | /// ParseStructUnionBody |
| 4115 | /// struct-contents: |
| 4116 | /// struct-declaration-list |
| 4117 | /// [EXT] empty |
| 4118 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 4119 | /// struct-declaration-list: |
| 4120 | /// struct-declaration |
| 4121 | /// struct-declaration-list struct-declaration |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 4122 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 4123 | /// |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 4124 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
Serge Pavlov | 0c50319 | 2019-08-01 11:46:28 +0000 | [diff] [blame] | 4125 | DeclSpec::TST TagType, Decl *TagDecl) { |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 4126 | PrettyDeclStackTraceEntry CrashInfo(Actions.Context, TagDecl, RecordLoc, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4127 | "parsing struct/union body"); |
Andy Gibbs | 22e140b | 2013-04-03 09:31:19 +0000 | [diff] [blame] | 4128 | assert(!getLangOpts().CPlusPlus && "C++ declarations not supported"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4129 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4130 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 4131 | if (T.consumeOpen()) |
| 4132 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4133 | |
Douglas Gregor | 658b955 | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 4134 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4135 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 4136 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4137 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 4138 | |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 4139 | // While we still have something to read, read the declarations in the struct. |
Richard Smith | 752ada8 | 2015-11-17 23:32:01 +0000 | [diff] [blame] | 4140 | while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) && |
| 4141 | Tok.isNot(tok::eof)) { |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 4142 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4143 | |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 4144 | // Check for extraneous top-level semicolon. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4145 | if (Tok.is(tok::semi)) { |
Richard Smith | 87f5dc5 | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 4146 | ConsumeExtraSemi(InsideStruct, TagType); |
Chris Lattner | 36e46a2 | 2007-06-09 05:49:55 +0000 | [diff] [blame] | 4147 | continue; |
| 4148 | } |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 4149 | |
Andy Gibbs | c804e08 | 2013-04-03 09:46:04 +0000 | [diff] [blame] | 4150 | // Parse _Static_assert declaration. |
| 4151 | if (Tok.is(tok::kw__Static_assert)) { |
| 4152 | SourceLocation DeclEnd; |
| 4153 | ParseStaticAssertDeclaration(DeclEnd); |
| 4154 | continue; |
| 4155 | } |
| 4156 | |
Argyrios Kyrtzidis | 71c12fb | 2013-04-18 01:42:35 +0000 | [diff] [blame] | 4157 | if (Tok.is(tok::annot_pragma_pack)) { |
| 4158 | HandlePragmaPack(); |
| 4159 | continue; |
| 4160 | } |
| 4161 | |
| 4162 | if (Tok.is(tok::annot_pragma_align)) { |
| 4163 | HandlePragmaAlign(); |
| 4164 | continue; |
| 4165 | } |
| 4166 | |
Alexey Bataev | 4652e4b | 2015-08-12 07:10:54 +0000 | [diff] [blame] | 4167 | if (Tok.is(tok::annot_pragma_openmp)) { |
Alexey Bataev | 587e1de | 2016-03-30 10:43:55 +0000 | [diff] [blame] | 4168 | // Result can be ignored, because it must be always empty. |
| 4169 | AccessSpecifier AS = AS_none; |
| 4170 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 4171 | (void)ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs); |
Alexey Bataev | 4652e4b | 2015-08-12 07:10:54 +0000 | [diff] [blame] | 4172 | continue; |
| 4173 | } |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 4174 | |
Serge Pavlov | 037861b | 2019-08-04 10:08:51 +0000 | [diff] [blame] | 4175 | if (tok::isPragmaAnnotation(Tok.getKind())) { |
| 4176 | Diag(Tok.getLocation(), diag::err_pragma_misplaced_in_decl) |
| 4177 | << DeclSpec::getSpecifierName( |
| 4178 | TagType, Actions.getASTContext().getPrintingPolicy()); |
| 4179 | ConsumeAnnotationToken(); |
| 4180 | continue; |
| 4181 | } |
| 4182 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 4183 | if (!Tok.is(tok::at)) { |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 4184 | auto CFieldCallback = [&](ParsingFieldDeclarator &FD) { |
| 4185 | // Install the declarator into the current TagDecl. |
| 4186 | Decl *Field = |
| 4187 | Actions.ActOnField(getCurScope(), TagDecl, |
| 4188 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 4189 | FD.D, FD.BitfieldSize); |
| 4190 | FieldDecls.push_back(Field); |
| 4191 | FD.complete(Field); |
| 4192 | }; |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 4193 | |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 4194 | // Parse all the comma separated declarators. |
| 4195 | ParsingDeclSpec DS(*this); |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 4196 | ParseStructDeclaration(DS, CFieldCallback); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 4197 | } else { // Handle @defs |
| 4198 | ConsumeToken(); |
| 4199 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 4200 | Diag(Tok, diag::err_unexpected_at); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4201 | SkipUntil(tok::semi); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 4202 | continue; |
| 4203 | } |
| 4204 | ConsumeToken(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 4205 | ExpectAndConsume(tok::l_paren); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 4206 | if (!Tok.is(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 4207 | Diag(Tok, diag::err_expected) << tok::identifier; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4208 | SkipUntil(tok::semi); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 4209 | continue; |
| 4210 | } |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4211 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4212 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4213 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 4214 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 4215 | ConsumeToken(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 4216 | ExpectAndConsume(tok::r_paren); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4217 | } |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 4218 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 4219 | if (TryConsumeToken(tok::semi)) |
| 4220 | continue; |
| 4221 | |
| 4222 | if (Tok.is(tok::r_brace)) { |
Chris Lattner | 245c533 | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 4223 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Chris Lattner | 0c7e82d | 2007-06-09 05:54:40 +0000 | [diff] [blame] | 4224 | break; |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 4225 | } |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 4226 | |
| 4227 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 4228 | // Skip to end of block or statement to avoid ext-warning on extra ';'. |
| 4229 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
| 4230 | // If we stopped at a ';', eat it. |
| 4231 | TryConsumeToken(tok::semi); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 4232 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4233 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4234 | T.consumeClose(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4235 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4236 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 4237 | // If attributes exist after struct contents, parse them. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4238 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 15619c7 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 4239 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4240 | Actions.ActOnFields(getCurScope(), RecordLoc, TagDecl, FieldDecls, |
| 4241 | T.getOpenLocation(), T.getCloseLocation(), attrs); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 4242 | StructScope.Exit(); |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 4243 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, T.getRange()); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 4244 | } |
| 4245 | |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 4246 | /// ParseEnumSpecifier |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 4247 | /// enum-specifier: [C99 6.7.2.2] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 4248 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4249 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 4250 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 4251 | /// '}' attributes[opt] |
Aaron Ballman | 9ecff02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 4252 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 4253 | /// '}' |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 4254 | /// 'enum' identifier |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 4255 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4256 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4257 | /// [C++11] enum-head '{' enumerator-list[opt] '}' |
| 4258 | /// [C++11] enum-head '{' enumerator-list ',' '}' |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4259 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4260 | /// enum-head: [C++11] |
| 4261 | /// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt] |
| 4262 | /// enum-key attribute-specifier-seq[opt] nested-name-specifier |
| 4263 | /// identifier enum-base[opt] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4264 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4265 | /// enum-key: [C++11] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4266 | /// 'enum' |
| 4267 | /// 'enum' 'class' |
| 4268 | /// 'enum' 'struct' |
| 4269 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4270 | /// enum-base: [C++11] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4271 | /// ':' type-specifier-seq |
| 4272 | /// |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4273 | /// [C++] elaborated-type-specifier: |
| 4274 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 4275 | /// |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 4276 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | dc70c3a | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 4277 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 4278 | AccessSpecifier AS, DeclSpecContext DSC) { |
Chris Lattner | ffbc271 | 2007-01-25 06:05:38 +0000 | [diff] [blame] | 4279 | // Parse the tag portion of this. |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4280 | if (Tok.is(tok::code_completion)) { |
| 4281 | // Code completion for an enum name. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4282 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 4283 | return cutOffParsing(); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4284 | } |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 4285 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 4286 | // If attributes exist after tag, parse them. |
| 4287 | ParsedAttributesWithRange attrs(AttrFactory); |
| 4288 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4289 | MaybeParseCXX11Attributes(attrs); |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 4290 | MaybeParseMicrosoftDeclSpecs(attrs); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 4291 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4292 | SourceLocation ScopedEnumKWLoc; |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 4293 | bool IsScopedUsingClassTag = false; |
| 4294 | |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 4295 | // In C++11, recognize 'enum class' and 'enum struct'. |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 4296 | if (Tok.isOneOf(tok::kw_class, tok::kw_struct)) { |
Richard Trieu | d0d87b5 | 2013-04-23 02:47:36 +0000 | [diff] [blame] | 4297 | Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum |
| 4298 | : diag::ext_scoped_enum); |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 4299 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4300 | ScopedEnumKWLoc = ConsumeToken(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4301 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4302 | // Attributes are not allowed between these keywords. Diagnose, |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 4303 | // but then just treat them like they appeared in the right place. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 4304 | ProhibitAttributes(attrs); |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 4305 | |
| 4306 | // They are allowed afterwards, though. |
| 4307 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4308 | MaybeParseCXX11Attributes(attrs); |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 4309 | MaybeParseMicrosoftDeclSpecs(attrs); |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 4310 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4311 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4312 | // C++11 [temp.explicit]p12: |
| 4313 | // The usual access controls do not apply to names used to specify |
| 4314 | // explicit instantiations. |
| 4315 | // We extend this to also cover explicit specializations. Note that |
| 4316 | // we don't suppress if this turns out to be an elaborated type |
| 4317 | // specifier. |
| 4318 | bool shouldDelayDiagsInTag = |
| 4319 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 4320 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 4321 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4322 | |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 4323 | // Enum definitions should not be parsed in a trailing-return-type. |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 4324 | bool AllowDeclaration = DSC != DeclSpecContext::DSC_trailing; |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 4325 | |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 4326 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4327 | if (getLangOpts().CPlusPlus) { |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 4328 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 4329 | // if a fixed underlying type is allowed. |
Erik Pilkington | 6f11db1 | 2018-09-28 20:24:58 +0000 | [diff] [blame] | 4330 | ColonProtectionRAIIObject X(*this, AllowDeclaration); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4331 | |
Nico Weber | cfaa4cd | 2015-02-15 07:26:13 +0000 | [diff] [blame] | 4332 | CXXScopeSpec Spec; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 4333 | if (ParseOptionalCXXScopeSpecifier(Spec, nullptr, |
Richard Smith | 1d4b2e1 | 2013-04-01 21:43:41 +0000 | [diff] [blame] | 4334 | /*EnteringContext=*/true)) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4335 | return; |
| 4336 | |
Nico Weber | cfaa4cd | 2015-02-15 07:26:13 +0000 | [diff] [blame] | 4337 | if (Spec.isSet() && Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 4338 | Diag(Tok, diag::err_expected) << tok::identifier; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4339 | if (Tok.isNot(tok::l_brace)) { |
| 4340 | // Has no name and is not a definition. |
| 4341 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4342 | SkipUntil(tok::comma, StopAtSemi); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4343 | return; |
| 4344 | } |
| 4345 | } |
Nico Weber | cfaa4cd | 2015-02-15 07:26:13 +0000 | [diff] [blame] | 4346 | |
| 4347 | SS = Spec; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4348 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4349 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4350 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 4351 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Erik Pilkington | 6f11db1 | 2018-09-28 20:24:58 +0000 | [diff] [blame] | 4352 | !(AllowDeclaration && Tok.is(tok::colon))) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 4353 | Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4354 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4355 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4356 | SkipUntil(tok::comma, StopAtSemi); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 4357 | return; |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4358 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4359 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4360 | // If an identifier is present, consume and remember it. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 4361 | IdentifierInfo *Name = nullptr; |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4362 | SourceLocation NameLoc; |
| 4363 | if (Tok.is(tok::identifier)) { |
| 4364 | Name = Tok.getIdentifierInfo(); |
| 4365 | NameLoc = ConsumeToken(); |
| 4366 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4367 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4368 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4369 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 4370 | // declaration of a scoped enumeration. |
| 4371 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4372 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 4373 | IsScopedUsingClassTag = false; |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4374 | } |
| 4375 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4376 | // Okay, end the suppression area. We'll decide whether to emit the |
| 4377 | // diagnostics in a second. |
| 4378 | if (shouldDelayDiagsInTag) |
| 4379 | diagsFromTag.done(); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4380 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4381 | TypeResult BaseType; |
| 4382 | |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4383 | // Parse the fixed underlying type. |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 4384 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
Erik Pilkington | 6f11db1 | 2018-09-28 20:24:58 +0000 | [diff] [blame] | 4385 | if (AllowDeclaration && Tok.is(tok::colon)) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4386 | bool PossibleBitfield = false; |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 4387 | if (CanBeBitfield) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4388 | // If we're in class scope, this can either be an enum declaration with |
| 4389 | // an underlying type, or a declaration of a bitfield member. We try to |
| 4390 | // use a simple disambiguation scheme first to catch the common cases |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4391 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 4392 | // anything that's a simple-type-specifier followed by '(' as an |
| 4393 | // expression. This suffices because function types are not valid |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4394 | // underlying types anyway. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4395 | EnterExpressionEvaluationContext Unevaluated( |
| 4396 | Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4397 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4398 | // If the next token starts an expression, we know we're parsing a |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4399 | // bit-field. This is the common case. |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 4400 | if (TPR == TPResult::True) |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4401 | PossibleBitfield = true; |
| 4402 | // If the next token starts a type-specifier-seq, it may be either a |
| 4403 | // a fixed underlying type or the start of a function-style cast in C++; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4404 | // lookahead one more token to see if it's obvious that we have a |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4405 | // fixed underlying type. |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 4406 | else if (TPR == TPResult::False && |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4407 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 4408 | // Consume the ':'. |
| 4409 | ConsumeToken(); |
| 4410 | } else { |
| 4411 | // We have the start of a type-specifier-seq, so we have to perform |
| 4412 | // tentative parsing to determine whether we have an expression or a |
| 4413 | // type. |
| 4414 | TentativeParsingAction TPA(*this); |
| 4415 | |
| 4416 | // Consume the ':'. |
| 4417 | ConsumeToken(); |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 4418 | |
| 4419 | // If we see a type specifier followed by an open-brace, we have an |
| 4420 | // ambiguity between an underlying type and a C++11 braced |
| 4421 | // function-style cast. Resolve this by always treating it as an |
| 4422 | // underlying type. |
| 4423 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 4424 | // this case. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4425 | if ((getLangOpts().CPlusPlus && |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 4426 | isCXXDeclarationSpecifier(TPResult::True) != TPResult::True) || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4427 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4428 | // We'll parse this as a bitfield later. |
| 4429 | PossibleBitfield = true; |
| 4430 | TPA.Revert(); |
| 4431 | } else { |
| 4432 | // We have a type-specifier-seq. |
| 4433 | TPA.Commit(); |
| 4434 | } |
| 4435 | } |
| 4436 | } else { |
| 4437 | // Consume the ':'. |
| 4438 | ConsumeToken(); |
| 4439 | } |
| 4440 | |
| 4441 | if (!PossibleBitfield) { |
| 4442 | SourceRange Range; |
| 4443 | BaseType = ParseTypeName(&Range); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4444 | |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 4445 | if (!getLangOpts().ObjC) { |
Erik Pilkington | 6f11db1 | 2018-09-28 20:24:58 +0000 | [diff] [blame] | 4446 | if (getLangOpts().CPlusPlus11) |
| 4447 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
| 4448 | else if (getLangOpts().CPlusPlus) |
| 4449 | Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type); |
| 4450 | else if (getLangOpts().MicrosoftExt) |
| 4451 | Diag(StartLoc, diag::ext_ms_c_enum_fixed_underlying_type); |
Eli Friedman | 0d0355ab | 2012-11-02 01:34:28 +0000 | [diff] [blame] | 4452 | else |
Erik Pilkington | 6f11db1 | 2018-09-28 20:24:58 +0000 | [diff] [blame] | 4453 | Diag(StartLoc, diag::ext_clang_c_enum_fixed_underlying_type); |
Eli Friedman | 0d0355ab | 2012-11-02 01:34:28 +0000 | [diff] [blame] | 4454 | } |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4455 | } |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4456 | } |
| 4457 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4458 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 4459 | // friend declaration, and cannot have an accompanying definition. If we have |
| 4460 | // 'enum foo;', then this is a forward declaration. If we have |
| 4461 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 4462 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4463 | // |
| 4464 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 4465 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 4466 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 4467 | // |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4468 | Sema::TagUseKind TUK; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4469 | if (!AllowDeclaration) { |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 4470 | TUK = Sema::TUK_Reference; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4471 | } else if (Tok.is(tok::l_brace)) { |
| 4472 | if (DS.isFriendSpecified()) { |
| 4473 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
| 4474 | << SourceRange(DS.getFriendSpecLoc()); |
| 4475 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4476 | SkipUntil(tok::r_brace, StopAtSemi); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4477 | TUK = Sema::TUK_Friend; |
| 4478 | } else { |
| 4479 | TUK = Sema::TUK_Definition; |
| 4480 | } |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 4481 | } else if (!isTypeSpecifier(DSC) && |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 4482 | (Tok.is(tok::semi) || |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 4483 | (Tok.isAtStartOfLine() && |
| 4484 | !isValidAfterTypeSpecifier(CanBeBitfield)))) { |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 4485 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
| 4486 | if (Tok.isNot(tok::semi)) { |
| 4487 | // A semicolon was missing after this declaration. Diagnose and recover. |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 4488 | ExpectAndConsume(tok::semi, diag::err_expected_after, "enum"); |
Ilya Biryukov | 929af67 | 2019-05-17 09:32:05 +0000 | [diff] [blame] | 4489 | PP.EnterToken(Tok, /*IsReinject=*/true); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 4490 | Tok.setKind(tok::semi); |
| 4491 | } |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4492 | } else { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4493 | TUK = Sema::TUK_Reference; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4494 | } |
| 4495 | |
| 4496 | // If this is an elaborated type specifier, and we delayed |
| 4497 | // diagnostics before, just merge them into the current pool. |
| 4498 | if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) { |
| 4499 | diagsFromTag.redelay(); |
| 4500 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4501 | |
| 4502 | MultiTemplateParamsArg TParams; |
Douglas Gregor | cbbf3e3 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 4503 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4504 | TUK != Sema::TUK_Reference) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4505 | if (!getLangOpts().CPlusPlus11 || !SS.isSet()) { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4506 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 4507 | Diag(Tok, diag::err_enum_template); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4508 | SkipUntil(tok::comma, StopAtSemi); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4509 | return; |
| 4510 | } |
| 4511 | |
| 4512 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 4513 | // Enumerations can't be explicitly instantiated. |
| 4514 | DS.SetTypeSpecError(); |
| 4515 | Diag(StartLoc, diag::err_explicit_instantiation_enum); |
| 4516 | return; |
| 4517 | } |
| 4518 | |
| 4519 | assert(TemplateInfo.TemplateParams && "no template parameters"); |
| 4520 | TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(), |
| 4521 | TemplateInfo.TemplateParams->size()); |
Douglas Gregor | cbbf3e3 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 4522 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4523 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 4524 | if (TUK == Sema::TUK_Reference) |
| 4525 | ProhibitAttributes(attrs); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4526 | |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 4527 | if (!Name && TUK != Sema::TUK_Definition) { |
| 4528 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4529 | |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 4530 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4531 | SkipUntil(tok::comma, StopAtSemi); |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 4532 | return; |
| 4533 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4534 | |
Nico Weber | 32a0fc7 | 2016-09-03 03:01:32 +0000 | [diff] [blame] | 4535 | stripTypeAttributesOffDeclSpec(attrs, DS, TUK); |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 4536 | |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 4537 | Sema::SkipBodyInfo SkipBody; |
| 4538 | if (!Name && TUK == Sema::TUK_Definition && Tok.is(tok::l_brace) && |
| 4539 | NextToken().is(tok::identifier)) |
| 4540 | SkipBody = Actions.shouldSkipAnonEnumBody(getCurScope(), |
| 4541 | NextToken().getIdentifierInfo(), |
| 4542 | NextToken().getLocation()); |
| 4543 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 4544 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4545 | bool IsDependent = false; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 4546 | const char *PrevSpec = nullptr; |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4547 | unsigned DiagID; |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 4548 | Decl *TagDecl = Actions.ActOnTag( |
| 4549 | getCurScope(), DeclSpec::TST_enum, TUK, StartLoc, SS, Name, NameLoc, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4550 | attrs, AS, DS.getModulePrivateSpecLoc(), TParams, Owned, IsDependent, |
| 4551 | ScopedEnumKWLoc, IsScopedUsingClassTag, BaseType, |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 4552 | DSC == DeclSpecContext::DSC_type_specifier, |
| 4553 | DSC == DeclSpecContext::DSC_template_param || |
| 4554 | DSC == DeclSpecContext::DSC_template_type_arg, |
| 4555 | &SkipBody); |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 4556 | |
| 4557 | if (SkipBody.ShouldSkip) { |
| 4558 | assert(TUK == Sema::TUK_Definition && "can only skip a definition"); |
| 4559 | |
| 4560 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 4561 | T.consumeOpen(); |
| 4562 | T.skipToEnd(); |
| 4563 | |
| 4564 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 4565 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 4566 | PrevSpec, DiagID, TagDecl, Owned, |
| 4567 | Actions.getASTContext().getPrintingPolicy())) |
| 4568 | Diag(StartLoc, DiagID) << PrevSpec; |
| 4569 | return; |
| 4570 | } |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4571 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4572 | if (IsDependent) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4573 | // This enum has a dependent nested-name-specifier. Handle it as a |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4574 | // dependent tag. |
| 4575 | if (!Name) { |
| 4576 | DS.SetTypeSpecError(); |
| 4577 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 4578 | return; |
| 4579 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4580 | |
Nico Weber | 83ea012 | 2014-05-03 21:57:40 +0000 | [diff] [blame] | 4581 | TypeResult Type = Actions.ActOnDependentTag( |
| 4582 | getCurScope(), DeclSpec::TST_enum, TUK, SS, Name, StartLoc, NameLoc); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4583 | if (Type.isInvalid()) { |
| 4584 | DS.SetTypeSpecError(); |
| 4585 | return; |
| 4586 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4587 | |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 4588 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 4589 | NameLoc.isValid() ? NameLoc : StartLoc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 4590 | PrevSpec, DiagID, Type.get(), |
| 4591 | Actions.getASTContext().getPrintingPolicy())) |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4592 | Diag(StartLoc, DiagID) << PrevSpec; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4593 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4594 | return; |
| 4595 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4596 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4597 | if (!TagDecl) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4598 | // The action failed to produce an enumeration tag. If this is a |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4599 | // definition, consume the entire definition. |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 4600 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4601 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4602 | SkipUntil(tok::r_brace, StopAtSemi); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4603 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4604 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4605 | DS.SetTypeSpecError(); |
| 4606 | return; |
| 4607 | } |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4608 | |
Bruno Cardoso Lopes | df0ee34 | 2017-07-01 00:06:47 +0000 | [diff] [blame] | 4609 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
| 4610 | Decl *D = SkipBody.CheckSameAsPrevious ? SkipBody.New : TagDecl; |
| 4611 | ParseEnumBody(StartLoc, D); |
| 4612 | if (SkipBody.CheckSameAsPrevious && |
| 4613 | !Actions.ActOnDuplicateDefinition(DS, TagDecl, SkipBody)) { |
| 4614 | DS.SetTypeSpecError(); |
| 4615 | return; |
| 4616 | } |
| 4617 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4618 | |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 4619 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 4620 | NameLoc.isValid() ? NameLoc : StartLoc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 4621 | PrevSpec, DiagID, TagDecl, Owned, |
| 4622 | Actions.getASTContext().getPrintingPolicy())) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4623 | Diag(StartLoc, DiagID) << PrevSpec; |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 4624 | } |
| 4625 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4626 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 4627 | /// enumerator-list: |
| 4628 | /// enumerator |
| 4629 | /// enumerator-list ',' enumerator |
| 4630 | /// enumerator: |
Aaron Ballman | 730476b | 2014-11-08 15:33:35 +0000 | [diff] [blame] | 4631 | /// enumeration-constant attributes[opt] |
| 4632 | /// enumeration-constant attributes[opt] '=' constant-expression |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4633 | /// enumeration-constant: |
| 4634 | /// identifier |
| 4635 | /// |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4636 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4637 | // Enter the scope of the enum body and start the definition. |
Hans Wennborg | fe78145 | 2014-06-17 00:00:18 +0000 | [diff] [blame] | 4638 | ParseScope EnumScope(this, Scope::DeclScope | Scope::EnumScope); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4639 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4640 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4641 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 4642 | T.consumeOpen(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4643 | |
Chris Lattner | 37256fb | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 4644 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4645 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 4646 | Diag(Tok, diag::err_empty_enum); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4647 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4648 | SmallVector<Decl *, 32> EnumConstantDecls; |
Jordan Rose | 60ac316 | 2015-04-30 17:20:30 +0000 | [diff] [blame] | 4649 | SmallVector<SuppressAccessChecks, 32> EnumAvailabilityDiags; |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4650 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 4651 | Decl *LastEnumConstDecl = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4652 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4653 | // Parse the enumerator-list. |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4654 | while (Tok.isNot(tok::r_brace)) { |
| 4655 | // Parse enumerator. If failed, try skipping till the start of the next |
| 4656 | // enumerator definition. |
| 4657 | if (Tok.isNot(tok::identifier)) { |
| 4658 | Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; |
| 4659 | if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) && |
| 4660 | TryConsumeToken(tok::comma)) |
| 4661 | continue; |
| 4662 | break; |
| 4663 | } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4664 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 4665 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4666 | |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 4667 | // If attributes exist after the enumerator, parse them. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 4668 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4669 | MaybeParseGNUAttributes(attrs); |
Aaron Ballman | 730476b | 2014-11-08 15:33:35 +0000 | [diff] [blame] | 4670 | ProhibitAttributes(attrs); // GNU-style attributes are prohibited. |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 4671 | if (standardAttributesAllowed() && isCXX11AttributeSpecifier()) { |
| 4672 | if (getLangOpts().CPlusPlus) |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4673 | Diag(Tok.getLocation(), getLangOpts().CPlusPlus17 |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 4674 | ? diag::warn_cxx14_compat_ns_enum_attribute |
| 4675 | : diag::ext_ns_enum_attribute) |
| 4676 | << 1 /*enumerator*/; |
Aaron Ballman | 730476b | 2014-11-08 15:33:35 +0000 | [diff] [blame] | 4677 | ParseCXX11Attributes(attrs); |
| 4678 | } |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 4679 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4680 | SourceLocation EqualLoc; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4681 | ExprResult AssignedVal; |
Jordan Rose | 60ac316 | 2015-04-30 17:20:30 +0000 | [diff] [blame] | 4682 | EnumAvailabilityDiags.emplace_back(*this); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4683 | |
Richard Smith | 40c3d6e3 | 2019-09-19 22:00:16 +0000 | [diff] [blame] | 4684 | EnterExpressionEvaluationContext ConstantEvaluated( |
| 4685 | Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 4686 | if (TryConsumeToken(tok::equal, EqualLoc)) { |
Richard Smith | 40c3d6e3 | 2019-09-19 22:00:16 +0000 | [diff] [blame] | 4687 | AssignedVal = ParseConstantExpressionInExprEvalContext(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 4688 | if (AssignedVal.isInvalid()) |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4689 | SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch); |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4690 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4691 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4692 | // Install the enumerator constant into EnumDecl. |
Bruno Cardoso Lopes | df0ee34 | 2017-07-01 00:06:47 +0000 | [diff] [blame] | 4693 | Decl *EnumConstDecl = Actions.ActOnEnumConstant( |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4694 | getCurScope(), EnumDecl, LastEnumConstDecl, IdentLoc, Ident, attrs, |
| 4695 | EqualLoc, AssignedVal.get()); |
Jordan Rose | 60ac316 | 2015-04-30 17:20:30 +0000 | [diff] [blame] | 4696 | EnumAvailabilityDiags.back().done(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4697 | |
Chris Lattner | 4ef4001 | 2007-06-11 01:28:17 +0000 | [diff] [blame] | 4698 | EnumConstantDecls.push_back(EnumConstDecl); |
| 4699 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4700 | |
Douglas Gregor | ce66d02 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 4701 | if (Tok.is(tok::identifier)) { |
| 4702 | // We're missing a comma between enumerators. |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 4703 | SourceLocation Loc = getEndOfPreviousToken(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4704 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
Douglas Gregor | ce66d02 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 4705 | << FixItHint::CreateInsertion(Loc, ", "); |
| 4706 | continue; |
| 4707 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4708 | |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4709 | // Emumerator definition must be finished, only comma or r_brace are |
| 4710 | // allowed here. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 4711 | SourceLocation CommaLoc; |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4712 | if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) { |
| 4713 | if (EqualLoc.isValid()) |
| 4714 | Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace |
| 4715 | << tok::comma; |
| 4716 | else |
| 4717 | Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator); |
| 4718 | if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) { |
| 4719 | if (TryConsumeToken(tok::comma, CommaLoc)) |
| 4720 | continue; |
| 4721 | } else { |
| 4722 | break; |
| 4723 | } |
| 4724 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4725 | |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4726 | // If comma is followed by r_brace, emit appropriate warning. |
| 4727 | if (Tok.is(tok::r_brace) && CommaLoc.isValid()) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4728 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11) |
Richard Smith | 87f5dc5 | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 4729 | Diag(CommaLoc, getLangOpts().CPlusPlus ? |
| 4730 | diag::ext_enumerator_list_comma_cxx : |
| 4731 | diag::ext_enumerator_list_comma_c) |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4732 | << FixItHint::CreateRemoval(CommaLoc); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4733 | else if (getLangOpts().CPlusPlus11) |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4734 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 4735 | << FixItHint::CreateRemoval(CommaLoc); |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4736 | break; |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4737 | } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4738 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4739 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4740 | // Eat the }. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4741 | T.consumeClose(); |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4742 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4743 | // If attributes exist after the identifier list, parse them. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4744 | ParsedAttributes attrs(AttrFactory); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4745 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 4746 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4747 | Actions.ActOnEnumBody(StartLoc, T.getRange(), EnumDecl, EnumConstantDecls, |
| 4748 | getCurScope(), attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4749 | |
Jordan Rose | 60ac316 | 2015-04-30 17:20:30 +0000 | [diff] [blame] | 4750 | // Now handle enum constant availability diagnostics. |
| 4751 | assert(EnumConstantDecls.size() == EnumAvailabilityDiags.size()); |
| 4752 | for (size_t i = 0, e = EnumConstantDecls.size(); i != e; ++i) { |
| 4753 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
| 4754 | EnumAvailabilityDiags[i].redelay(); |
| 4755 | PD.complete(EnumConstantDecls[i]); |
| 4756 | } |
| 4757 | |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 4758 | EnumScope.Exit(); |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 4759 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, T.getRange()); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 4760 | |
| 4761 | // The next token must be valid after an enum definition. If not, a ';' |
| 4762 | // was probably forgotten. |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 4763 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
| 4764 | if (!isValidAfterTypeSpecifier(CanBeBitfield)) { |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 4765 | ExpectAndConsume(tok::semi, diag::err_expected_after, "enum"); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 4766 | // Push this token back into the preprocessor and change our current token |
| 4767 | // to ';' so that the rest of the code recovers as though there were an |
| 4768 | // ';' after the definition. |
Ilya Biryukov | 929af67 | 2019-05-17 09:32:05 +0000 | [diff] [blame] | 4769 | PP.EnterToken(Tok, /*IsReinject=*/true); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 4770 | Tok.setKind(tok::semi); |
| 4771 | } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4772 | } |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 4773 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4774 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 4775 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 4776 | /// specifier or if we're not sure. |
| 4777 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 4778 | switch (Tok.getKind()) { |
| 4779 | default: return false; |
| 4780 | // type-specifiers |
| 4781 | case tok::kw_short: |
| 4782 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4783 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4784 | case tok::kw___int128: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4785 | case tok::kw_signed: |
| 4786 | case tok::kw_unsigned: |
| 4787 | case tok::kw__Complex: |
| 4788 | case tok::kw__Imaginary: |
| 4789 | case tok::kw_void: |
| 4790 | case tok::kw_char: |
| 4791 | case tok::kw_wchar_t: |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 4792 | case tok::kw_char8_t: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4793 | case tok::kw_char16_t: |
| 4794 | case tok::kw_char32_t: |
| 4795 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4796 | case tok::kw_half: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4797 | case tok::kw_float: |
| 4798 | case tok::kw_double: |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 4799 | case tok::kw__Accum: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 4800 | case tok::kw__Fract: |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 4801 | case tok::kw__Float16: |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 4802 | case tok::kw___float128: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4803 | case tok::kw_bool: |
| 4804 | case tok::kw__Bool: |
| 4805 | case tok::kw__Decimal32: |
| 4806 | case tok::kw__Decimal64: |
| 4807 | case tok::kw__Decimal128: |
| 4808 | case tok::kw___vector: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 4809 | #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 4810 | #include "clang/Basic/OpenCLImageTypes.def" |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4811 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4812 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4813 | case tok::kw_class: |
| 4814 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4815 | case tok::kw___interface: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4816 | case tok::kw_union: |
| 4817 | // enum-specifier |
| 4818 | case tok::kw_enum: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4819 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4820 | // typedef-name |
| 4821 | case tok::annot_typename: |
| 4822 | return true; |
| 4823 | } |
| 4824 | } |
| 4825 | |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 4826 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4827 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4828 | bool Parser::isTypeSpecifierQualifier() { |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4829 | switch (Tok.getKind()) { |
| 4830 | default: return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4831 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4832 | case tok::identifier: // foo::bar |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4833 | if (TryAltiVecVectorToken()) |
| 4834 | return true; |
Reid Kleckner | 4dc0b1a | 2018-11-01 19:54:45 +0000 | [diff] [blame] | 4835 | LLVM_FALLTHROUGH; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4836 | case tok::kw_typename: // typename T::type |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4837 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4838 | // recurse to handle whatever we get. |
| 4839 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4840 | return true; |
| 4841 | if (Tok.is(tok::identifier)) |
| 4842 | return false; |
| 4843 | return isTypeSpecifierQualifier(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4844 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4845 | case tok::coloncolon: // ::foo::bar |
| 4846 | if (NextToken().is(tok::kw_new) || // ::new |
| 4847 | NextToken().is(tok::kw_delete)) // ::delete |
| 4848 | return false; |
| 4849 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4850 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4851 | return true; |
| 4852 | return isTypeSpecifierQualifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4853 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 4854 | // GNU attributes support. |
| 4855 | case tok::kw___attribute: |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4856 | // GNU typeof support. |
| 4857 | case tok::kw_typeof: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4858 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4859 | // type-specifiers |
| 4860 | case tok::kw_short: |
| 4861 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4862 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4863 | case tok::kw___int128: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4864 | case tok::kw_signed: |
| 4865 | case tok::kw_unsigned: |
| 4866 | case tok::kw__Complex: |
| 4867 | case tok::kw__Imaginary: |
| 4868 | case tok::kw_void: |
| 4869 | case tok::kw_char: |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4870 | case tok::kw_wchar_t: |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 4871 | case tok::kw_char8_t: |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4872 | case tok::kw_char16_t: |
| 4873 | case tok::kw_char32_t: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4874 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4875 | case tok::kw_half: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4876 | case tok::kw_float: |
| 4877 | case tok::kw_double: |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 4878 | case tok::kw__Accum: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 4879 | case tok::kw__Fract: |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 4880 | case tok::kw__Float16: |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 4881 | case tok::kw___float128: |
Chris Lattner | bb31a42 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 4882 | case tok::kw_bool: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4883 | case tok::kw__Bool: |
| 4884 | case tok::kw__Decimal32: |
| 4885 | case tok::kw__Decimal64: |
| 4886 | case tok::kw__Decimal128: |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4887 | case tok::kw___vector: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 4888 | #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 4889 | #include "clang/Basic/OpenCLImageTypes.def" |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4890 | |
Chris Lattner | 861a226 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 4891 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4892 | case tok::kw_class: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4893 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4894 | case tok::kw___interface: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4895 | case tok::kw_union: |
| 4896 | // enum-specifier |
| 4897 | case tok::kw_enum: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4898 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4899 | // type-qualifier |
| 4900 | case tok::kw_const: |
| 4901 | case tok::kw_volatile: |
| 4902 | case tok::kw_restrict: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 4903 | case tok::kw__Sat: |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4904 | |
John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4905 | // Debugger support. |
| 4906 | case tok::kw___unknown_anytype: |
| 4907 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4908 | // typedef-name |
Chris Lattner | a8a3f73 | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 4909 | case tok::annot_typename: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4910 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4911 | |
Chris Lattner | 409bf7d | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 4912 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4913 | case tok::less: |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 4914 | return getLangOpts().ObjC; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4915 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4916 | case tok::kw___cdecl: |
| 4917 | case tok::kw___stdcall: |
| 4918 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4919 | case tok::kw___thiscall: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4920 | case tok::kw___regcall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4921 | case tok::kw___vectorcall: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4922 | case tok::kw___w64: |
| 4923 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4924 | case tok::kw___ptr32: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4925 | case tok::kw___pascal: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4926 | case tok::kw___unaligned: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4927 | |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 4928 | case tok::kw__Nonnull: |
| 4929 | case tok::kw__Nullable: |
| 4930 | case tok::kw__Null_unspecified: |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 4931 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4932 | case tok::kw___kindof: |
| 4933 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4934 | case tok::kw___private: |
| 4935 | case tok::kw___local: |
| 4936 | case tok::kw___global: |
| 4937 | case tok::kw___constant: |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 4938 | case tok::kw___generic: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4939 | case tok::kw___read_only: |
| 4940 | case tok::kw___read_write: |
| 4941 | case tok::kw___write_only: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4942 | return true; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4943 | |
Anastasia Stulova | 314fab6 | 2019-03-28 11:47:14 +0000 | [diff] [blame] | 4944 | case tok::kw_private: |
| 4945 | return getLangOpts().OpenCL; |
| 4946 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4947 | // C11 _Atomic |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4948 | case tok::kw__Atomic: |
| 4949 | return true; |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4950 | } |
| 4951 | } |
| 4952 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4953 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 4954 | /// declaration specifier. |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4955 | /// |
| 4956 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 4957 | /// this check is to disambiguate between an expression and a declaration. |
| 4958 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4959 | switch (Tok.getKind()) { |
| 4960 | default: return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4961 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 4962 | case tok::kw_pipe: |
Sven van Haastregt | e518bb4 | 2019-05-22 13:12:20 +0000 | [diff] [blame] | 4963 | return (getLangOpts().OpenCL && getLangOpts().OpenCLVersion >= 200) || |
| 4964 | getLangOpts().OpenCLCPlusPlus; |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 4965 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4966 | case tok::identifier: // foo::bar |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4967 | // Unfortunate hack to support "Class.factoryMethod" notation. |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 4968 | if (getLangOpts().ObjC && NextToken().is(tok::period)) |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4969 | return false; |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4970 | if (TryAltiVecVectorToken()) |
| 4971 | return true; |
Reid Kleckner | 4dc0b1a | 2018-11-01 19:54:45 +0000 | [diff] [blame] | 4972 | LLVM_FALLTHROUGH; |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4973 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4974 | case tok::kw_typename: // typename T::type |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4975 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4976 | // recurse to handle whatever we get. |
| 4977 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4978 | return true; |
| 4979 | if (Tok.is(tok::identifier)) |
| 4980 | return false; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4981 | |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4982 | // If we're in Objective-C and we have an Objective-C class type followed |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4983 | // by an identifier and then either ':' or ']', in a place where an |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4984 | // expression is permitted, then this is probably a class message send |
| 4985 | // missing the initial '['. In this case, we won't consider this to be |
| 4986 | // the start of a declaration. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4987 | if (DisambiguatingWithExpression && |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4988 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 4989 | return false; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4990 | |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4991 | return isDeclarationSpecifier(); |
| 4992 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4993 | case tok::coloncolon: // ::foo::bar |
| 4994 | if (NextToken().is(tok::kw_new) || // ::new |
| 4995 | NextToken().is(tok::kw_delete)) // ::delete |
| 4996 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4997 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4998 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4999 | // recurse to handle whatever we get. |
| 5000 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 5001 | return true; |
| 5002 | return isDeclarationSpecifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5003 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5004 | // storage-class-specifier |
| 5005 | case tok::kw_typedef: |
| 5006 | case tok::kw_extern: |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 5007 | case tok::kw___private_extern__: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5008 | case tok::kw_static: |
| 5009 | case tok::kw_auto: |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 5010 | case tok::kw___auto_type: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5011 | case tok::kw_register: |
| 5012 | case tok::kw___thread: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 5013 | case tok::kw_thread_local: |
| 5014 | case tok::kw__Thread_local: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5015 | |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 5016 | // Modules |
| 5017 | case tok::kw___module_private__: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5018 | |
John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 5019 | // Debugger support |
| 5020 | case tok::kw___unknown_anytype: |
| 5021 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5022 | // type-specifiers |
| 5023 | case tok::kw_short: |
| 5024 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 5025 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 5026 | case tok::kw___int128: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5027 | case tok::kw_signed: |
| 5028 | case tok::kw_unsigned: |
| 5029 | case tok::kw__Complex: |
| 5030 | case tok::kw__Imaginary: |
| 5031 | case tok::kw_void: |
| 5032 | case tok::kw_char: |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 5033 | case tok::kw_wchar_t: |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 5034 | case tok::kw_char8_t: |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 5035 | case tok::kw_char16_t: |
| 5036 | case tok::kw_char32_t: |
| 5037 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5038 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 5039 | case tok::kw_half: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5040 | case tok::kw_float: |
| 5041 | case tok::kw_double: |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 5042 | case tok::kw__Accum: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 5043 | case tok::kw__Fract: |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 5044 | case tok::kw__Float16: |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 5045 | case tok::kw___float128: |
Chris Lattner | bb31a42 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 5046 | case tok::kw_bool: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5047 | case tok::kw__Bool: |
| 5048 | case tok::kw__Decimal32: |
| 5049 | case tok::kw__Decimal64: |
| 5050 | case tok::kw__Decimal128: |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 5051 | case tok::kw___vector: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5052 | |
Chris Lattner | 861a226 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 5053 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 5054 | case tok::kw_class: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5055 | case tok::kw_struct: |
| 5056 | case tok::kw_union: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 5057 | case tok::kw___interface: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5058 | // enum-specifier |
| 5059 | case tok::kw_enum: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5060 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5061 | // type-qualifier |
| 5062 | case tok::kw_const: |
| 5063 | case tok::kw_volatile: |
| 5064 | case tok::kw_restrict: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 5065 | case tok::kw__Sat: |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5066 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5067 | // function-specifier |
| 5068 | case tok::kw_inline: |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 5069 | case tok::kw_virtual: |
| 5070 | case tok::kw_explicit: |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 5071 | case tok::kw__Noreturn: |
Chris Lattner | 7b20dc7 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 5072 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 5073 | // alignment-specifier |
| 5074 | case tok::kw__Alignas: |
| 5075 | |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 5076 | // friend keyword. |
| 5077 | case tok::kw_friend: |
| 5078 | |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 5079 | // static_assert-declaration |
| 5080 | case tok::kw__Static_assert: |
| 5081 | |
Chris Lattner | 599e47e | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 5082 | // GNU typeof support. |
| 5083 | case tok::kw_typeof: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5084 | |
Chris Lattner | 599e47e | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 5085 | // GNU attributes. |
Chris Lattner | 7b20dc7 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 5086 | case tok::kw___attribute: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5087 | |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 5088 | // C++11 decltype and constexpr. |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5089 | case tok::annot_decltype: |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 5090 | case tok::kw_constexpr: |
Francois Pichet | e878cb6 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 5091 | |
Richard Smith | a6e8b68 | 2019-09-04 20:30:37 +0000 | [diff] [blame] | 5092 | // C++20 consteval and constinit. |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 5093 | case tok::kw_consteval: |
Richard Smith | a6e8b68 | 2019-09-04 20:30:37 +0000 | [diff] [blame] | 5094 | case tok::kw_constinit: |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 5095 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5096 | // C11 _Atomic |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5097 | case tok::kw__Atomic: |
| 5098 | return true; |
| 5099 | |
Chris Lattner | 8b2ec16 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 5100 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 5101 | case tok::less: |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 5102 | return getLangOpts().ObjC; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5103 | |
Douglas Gregor | 19b7acf | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 5104 | // typedef-name |
| 5105 | case tok::annot_typename: |
| 5106 | return !DisambiguatingWithExpression || |
| 5107 | !isStartOfObjCClassMessageMissingOpenBracket(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5108 | |
Steve Naroff | f192fab | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 5109 | case tok::kw___declspec: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 5110 | case tok::kw___cdecl: |
| 5111 | case tok::kw___stdcall: |
| 5112 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 5113 | case tok::kw___thiscall: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 5114 | case tok::kw___regcall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 5115 | case tok::kw___vectorcall: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 5116 | case tok::kw___w64: |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 5117 | case tok::kw___sptr: |
| 5118 | case tok::kw___uptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 5119 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 5120 | case tok::kw___ptr32: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 5121 | case tok::kw___forceinline: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5122 | case tok::kw___pascal: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 5123 | case tok::kw___unaligned: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 5124 | |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 5125 | case tok::kw__Nonnull: |
| 5126 | case tok::kw__Nullable: |
| 5127 | case tok::kw__Null_unspecified: |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 5128 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 5129 | case tok::kw___kindof: |
| 5130 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 5131 | case tok::kw___private: |
| 5132 | case tok::kw___local: |
| 5133 | case tok::kw___global: |
| 5134 | case tok::kw___constant: |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 5135 | case tok::kw___generic: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 5136 | case tok::kw___read_only: |
| 5137 | case tok::kw___read_write: |
| 5138 | case tok::kw___write_only: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 5139 | #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 5140 | #include "clang/Basic/OpenCLImageTypes.def" |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 5141 | |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 5142 | return true; |
Anastasia Stulova | 314fab6 | 2019-03-28 11:47:14 +0000 | [diff] [blame] | 5143 | |
| 5144 | case tok::kw_private: |
| 5145 | return getLangOpts().OpenCL; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5146 | } |
| 5147 | } |
| 5148 | |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5149 | bool Parser::isConstructorDeclarator(bool IsUnqualified, bool DeductionGuide) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5150 | TentativeParsingAction TPA(*this); |
| 5151 | |
| 5152 | // Parse the C++ scope specifier. |
| 5153 | CXXScopeSpec SS; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 5154 | if (ParseOptionalCXXScopeSpecifier(SS, nullptr, |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 5155 | /*EnteringContext=*/true)) { |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 5156 | TPA.Revert(); |
| 5157 | return false; |
| 5158 | } |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5159 | |
| 5160 | // Parse the constructor name. |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 5161 | if (Tok.is(tok::identifier)) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5162 | // We already know that we have a constructor name; just consume |
| 5163 | // the token. |
| 5164 | ConsumeToken(); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 5165 | } else if (Tok.is(tok::annot_template_id)) { |
| 5166 | ConsumeAnnotationToken(); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5167 | } else { |
| 5168 | TPA.Revert(); |
| 5169 | return false; |
| 5170 | } |
| 5171 | |
Richard Smith | 8f8697f | 2017-02-08 01:16:55 +0000 | [diff] [blame] | 5172 | // There may be attributes here, appertaining to the constructor name or type |
| 5173 | // we just stepped past. |
| 5174 | SkipCXX11Attributes(); |
| 5175 | |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 5176 | // Current class name must be followed by a left parenthesis. |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5177 | if (Tok.isNot(tok::l_paren)) { |
| 5178 | TPA.Revert(); |
| 5179 | return false; |
| 5180 | } |
| 5181 | ConsumeParen(); |
| 5182 | |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 5183 | // A right parenthesis, or ellipsis followed by a right parenthesis signals |
| 5184 | // that we have a constructor. |
| 5185 | if (Tok.is(tok::r_paren) || |
| 5186 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5187 | TPA.Revert(); |
| 5188 | return true; |
| 5189 | } |
| 5190 | |
Richard Smith | f216366 | 2013-09-06 00:12:20 +0000 | [diff] [blame] | 5191 | // A C++11 attribute here signals that we have a constructor, and is an |
| 5192 | // attribute on the first constructor parameter. |
| 5193 | if (getLangOpts().CPlusPlus11 && |
| 5194 | isCXX11AttributeSpecifier(/*Disambiguate*/ false, |
| 5195 | /*OuterMightBeMessageSend*/ true)) { |
| 5196 | TPA.Revert(); |
| 5197 | return true; |
| 5198 | } |
| 5199 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5200 | // If we need to, enter the specified scope. |
| 5201 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5202 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5203 | DeclScopeObj.EnterDeclaratorScope(); |
| 5204 | |
Francois Pichet | 79f3a87 | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 5205 | // Optionally skip Microsoft attributes. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5206 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | 79f3a87 | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 5207 | MaybeParseMicrosoftAttributes(Attrs); |
| 5208 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5209 | // Check whether the next token(s) are part of a declaration |
| 5210 | // specifier, in which case we have the start of a parameter and, |
| 5211 | // therefore, we know that this is a constructor. |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 5212 | bool IsConstructor = false; |
| 5213 | if (isDeclarationSpecifier()) |
| 5214 | IsConstructor = true; |
| 5215 | else if (Tok.is(tok::identifier) || |
| 5216 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) { |
| 5217 | // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type. |
| 5218 | // This might be a parenthesized member name, but is more likely to |
| 5219 | // be a constructor declaration with an invalid argument type. Keep |
| 5220 | // looking. |
| 5221 | if (Tok.is(tok::annot_cxxscope)) |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 5222 | ConsumeAnnotationToken(); |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 5223 | ConsumeToken(); |
| 5224 | |
| 5225 | // If this is not a constructor, we must be parsing a declarator, |
Richard Smith | 1453e31 | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 5226 | // which must have one of the following syntactic forms (see the |
| 5227 | // grammar extract at the start of ParseDirectDeclarator): |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 5228 | switch (Tok.getKind()) { |
| 5229 | case tok::l_paren: |
| 5230 | // C(X ( int)); |
| 5231 | case tok::l_square: |
| 5232 | // C(X [ 5]); |
| 5233 | // C(X [ [attribute]]); |
| 5234 | case tok::coloncolon: |
| 5235 | // C(X :: Y); |
| 5236 | // C(X :: *p); |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 5237 | // Assume this isn't a constructor, rather than assuming it's a |
| 5238 | // constructor with an unnamed parameter of an ill-formed type. |
| 5239 | break; |
| 5240 | |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 5241 | case tok::r_paren: |
| 5242 | // C(X ) |
Richard Smith | 8f8697f | 2017-02-08 01:16:55 +0000 | [diff] [blame] | 5243 | |
| 5244 | // Skip past the right-paren and any following attributes to get to |
| 5245 | // the function body or trailing-return-type. |
| 5246 | ConsumeParen(); |
| 5247 | SkipCXX11Attributes(); |
| 5248 | |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5249 | if (DeductionGuide) { |
| 5250 | // C(X) -> ... is a deduction guide. |
Richard Smith | 8f8697f | 2017-02-08 01:16:55 +0000 | [diff] [blame] | 5251 | IsConstructor = Tok.is(tok::arrow); |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5252 | break; |
| 5253 | } |
Richard Smith | 8f8697f | 2017-02-08 01:16:55 +0000 | [diff] [blame] | 5254 | if (Tok.is(tok::colon) || Tok.is(tok::kw_try)) { |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 5255 | // Assume these were meant to be constructors: |
| 5256 | // C(X) : (the name of a bit-field cannot be parenthesized). |
| 5257 | // C(X) try (this is otherwise ill-formed). |
| 5258 | IsConstructor = true; |
| 5259 | } |
Richard Smith | 8f8697f | 2017-02-08 01:16:55 +0000 | [diff] [blame] | 5260 | if (Tok.is(tok::semi) || Tok.is(tok::l_brace)) { |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 5261 | // If we have a constructor name within the class definition, |
| 5262 | // assume these were meant to be constructors: |
| 5263 | // C(X) { |
| 5264 | // C(X) ; |
| 5265 | // ... because otherwise we would be declaring a non-static data |
| 5266 | // member that is ill-formed because it's of the same type as its |
| 5267 | // surrounding class. |
| 5268 | // |
| 5269 | // FIXME: We can actually do this whether or not the name is qualified, |
| 5270 | // because if it is qualified in this context it must be being used as |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5271 | // a constructor name. |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 5272 | // currently, so we're somewhat conservative here. |
| 5273 | IsConstructor = IsUnqualified; |
| 5274 | } |
| 5275 | break; |
| 5276 | |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 5277 | default: |
| 5278 | IsConstructor = true; |
| 5279 | break; |
| 5280 | } |
| 5281 | } |
| 5282 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5283 | TPA.Revert(); |
| 5284 | return IsConstructor; |
| 5285 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 5286 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5287 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5288 | /// type-qualifier-list: [C99 6.7.5] |
| 5289 | /// type-qualifier |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5290 | /// [vendor] attributes |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5291 | /// [ only if AttrReqs & AR_VendorAttributesParsed ] |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5292 | /// type-qualifier-list type-qualifier |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5293 | /// [vendor] type-qualifier-list attributes |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5294 | /// [ only if AttrReqs & AR_VendorAttributesParsed ] |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5295 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5296 | /// [ only if AttReqs & AR_CXX11AttributesParsed ] |
| 5297 | /// Note: vendor can be GNU, MS, etc and can be explicitly controlled via |
| 5298 | /// AttrRequirements bitmask values. |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 5299 | void Parser::ParseTypeQualifierListOpt( |
| 5300 | DeclSpec &DS, unsigned AttrReqs, bool AtomicAllowed, |
| 5301 | bool IdentifierRequired, |
| 5302 | Optional<llvm::function_ref<void()>> CodeCompletionHandler) { |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 5303 | if (standardAttributesAllowed() && (AttrReqs & AR_CXX11AttributesParsed) && |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5304 | isCXX11AttributeSpecifier()) { |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5305 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 5306 | ParseCXX11Attributes(attrs); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5307 | DS.takeAttributesFrom(attrs); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5308 | } |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 5309 | |
| 5310 | SourceLocation EndLoc; |
| 5311 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5312 | while (1) { |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5313 | bool isInvalid = false; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5314 | const char *PrevSpec = nullptr; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5315 | unsigned DiagID = 0; |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 5316 | SourceLocation Loc = Tok.getLocation(); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 5317 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5318 | switch (Tok.getKind()) { |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 5319 | case tok::code_completion: |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 5320 | if (CodeCompletionHandler) |
| 5321 | (*CodeCompletionHandler)(); |
| 5322 | else |
| 5323 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 5324 | return cutOffParsing(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5325 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5326 | case tok::kw_const: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5327 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 5328 | getLangOpts()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 5329 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5330 | case tok::kw_volatile: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5331 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 5332 | getLangOpts()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 5333 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5334 | case tok::kw_restrict: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5335 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 5336 | getLangOpts()); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5337 | break; |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5338 | case tok::kw__Atomic: |
| 5339 | if (!AtomicAllowed) |
| 5340 | goto DoneWithTypeQuals; |
Aaron Ballman | 5cd5d56 | 2019-09-04 21:01:57 +0000 | [diff] [blame] | 5341 | if (!getLangOpts().C11) |
| 5342 | Diag(Tok, diag::ext_c11_feature) << Tok.getName(); |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5343 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 5344 | getLangOpts()); |
| 5345 | break; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 5346 | |
| 5347 | // OpenCL qualifiers: |
Anastasia Stulova | 948e37c | 2019-03-25 11:54:02 +0000 | [diff] [blame] | 5348 | case tok::kw_private: |
Anastasia Stulova | 314fab6 | 2019-03-28 11:47:14 +0000 | [diff] [blame] | 5349 | if (!getLangOpts().OpenCL) |
| 5350 | goto DoneWithTypeQuals; |
| 5351 | LLVM_FALLTHROUGH; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 5352 | case tok::kw___private: |
| 5353 | case tok::kw___global: |
| 5354 | case tok::kw___local: |
| 5355 | case tok::kw___constant: |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 5356 | case tok::kw___generic: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 5357 | case tok::kw___read_only: |
| 5358 | case tok::kw___write_only: |
| 5359 | case tok::kw___read_write: |
Aaron Ballman | 05d76ea | 2014-01-14 01:29:54 +0000 | [diff] [blame] | 5360 | ParseOpenCLQualifiers(DS.getAttributes()); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 5361 | break; |
| 5362 | |
Andrey Bokhanko | 45d4132 | 2016-05-11 18:38:21 +0000 | [diff] [blame] | 5363 | case tok::kw___unaligned: |
| 5364 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID, |
| 5365 | getLangOpts()); |
| 5366 | break; |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 5367 | case tok::kw___uptr: |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 5368 | // GNU libc headers in C mode use '__uptr' as an identifier which conflicts |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 5369 | // with the MS modifier keyword. |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5370 | if ((AttrReqs & AR_DeclspecAttributesParsed) && !getLangOpts().CPlusPlus && |
Alp Toker | 47642d2 | 2013-12-03 06:13:01 +0000 | [diff] [blame] | 5371 | IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) { |
| 5372 | if (TryKeywordIdentFallback(false)) |
| 5373 | continue; |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 5374 | } |
Galina Kistanova | 7767425 | 2017-06-01 21:15:34 +0000 | [diff] [blame] | 5375 | LLVM_FALLTHROUGH; |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 5376 | case tok::kw___sptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 5377 | case tok::kw___w64: |
Steve Naroff | f9c29d4 | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 5378 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 5379 | case tok::kw___ptr32: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 5380 | case tok::kw___cdecl: |
| 5381 | case tok::kw___stdcall: |
| 5382 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 5383 | case tok::kw___thiscall: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 5384 | case tok::kw___regcall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 5385 | case tok::kw___vectorcall: |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5386 | if (AttrReqs & AR_DeclspecAttributesParsed) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5387 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 5388 | continue; |
| 5389 | } |
| 5390 | goto DoneWithTypeQuals; |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5391 | case tok::kw___pascal: |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5392 | if (AttrReqs & AR_VendorAttributesParsed) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5393 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5394 | continue; |
| 5395 | } |
| 5396 | goto DoneWithTypeQuals; |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 5397 | |
| 5398 | // Nullability type specifiers. |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 5399 | case tok::kw__Nonnull: |
| 5400 | case tok::kw__Nullable: |
| 5401 | case tok::kw__Null_unspecified: |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 5402 | ParseNullabilityTypeSpecifiers(DS.getAttributes()); |
| 5403 | continue; |
| 5404 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 5405 | // Objective-C 'kindof' types. |
| 5406 | case tok::kw___kindof: |
| 5407 | DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5408 | nullptr, 0, ParsedAttr::AS_Keyword); |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 5409 | (void)ConsumeToken(); |
| 5410 | continue; |
| 5411 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 5412 | case tok::kw___attribute: |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5413 | if (AttrReqs & AR_GNUAttributesParsedAndRejected) |
| 5414 | // When GNU attributes are expressly forbidden, diagnose their usage. |
| 5415 | Diag(Tok, diag::err_attributes_not_allowed); |
| 5416 | |
| 5417 | // Parse the attributes even if they are rejected to ensure that error |
| 5418 | // recovery is graceful. |
| 5419 | if (AttrReqs & AR_GNUAttributesParsed || |
| 5420 | AttrReqs & AR_GNUAttributesParsedAndRejected) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5421 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5422 | continue; // do *not* consume the next token! |
| 5423 | } |
| 5424 | // otherwise, FALL THROUGH! |
Galina Kistanova | 7767425 | 2017-06-01 21:15:34 +0000 | [diff] [blame] | 5425 | LLVM_FALLTHROUGH; |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5426 | default: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 5427 | DoneWithTypeQuals: |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5428 | // If this is not a type-qualifier token, we're done reading type |
| 5429 | // qualifiers. First verify that DeclSpec's are consistent. |
Craig Topper | 2512241 | 2015-11-15 03:32:11 +0000 | [diff] [blame] | 5430 | DS.Finish(Actions, Actions.getASTContext().getPrintingPolicy()); |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 5431 | if (EndLoc.isValid()) |
| 5432 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5433 | return; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5434 | } |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5435 | |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 5436 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 5437 | if (isInvalid) { |
| 5438 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 5439 | Diag(Tok, DiagID) << PrevSpec; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 5440 | } |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 5441 | EndLoc = ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5442 | } |
| 5443 | } |
| 5444 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 5445 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 5446 | /// |
| 5447 | void Parser::ParseDeclarator(Declarator &D) { |
| 5448 | /// This implements the 'declarator' production in the C grammar, then checks |
| 5449 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5450 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 5451 | } |
| 5452 | |
Richard Smith | 0b350b9 | 2014-10-28 16:55:02 +0000 | [diff] [blame] | 5453 | static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang, |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5454 | DeclaratorContext TheContext) { |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5455 | if (Kind == tok::star || Kind == tok::caret) |
| 5456 | return true; |
| 5457 | |
Sven van Haastregt | e518bb4 | 2019-05-22 13:12:20 +0000 | [diff] [blame] | 5458 | if (Kind == tok::kw_pipe && |
| 5459 | ((Lang.OpenCL && Lang.OpenCLVersion >= 200) || Lang.OpenCLCPlusPlus)) |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5460 | return true; |
| 5461 | |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5462 | if (!Lang.CPlusPlus) |
| 5463 | return false; |
| 5464 | |
Richard Smith | 0b350b9 | 2014-10-28 16:55:02 +0000 | [diff] [blame] | 5465 | if (Kind == tok::amp) |
| 5466 | return true; |
| 5467 | |
| 5468 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 5469 | // But we must not parse them in conversion-type-ids and new-type-ids, since |
| 5470 | // those can be legitimately followed by a && operator. |
| 5471 | // (The same thing can in theory happen after a trailing-return-type, but |
| 5472 | // since those are a C++11 feature, there is no rejects-valid issue there.) |
| 5473 | if (Kind == tok::ampamp) |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5474 | return Lang.CPlusPlus11 || |
| 5475 | (TheContext != DeclaratorContext::ConversionIdContext && |
| 5476 | TheContext != DeclaratorContext::CXXNewContext); |
Richard Smith | 0b350b9 | 2014-10-28 16:55:02 +0000 | [diff] [blame] | 5477 | |
| 5478 | return false; |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5479 | } |
| 5480 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5481 | // Indicates whether the given declarator is a pipe declarator. |
| 5482 | static bool isPipeDeclerator(const Declarator &D) { |
| 5483 | const unsigned NumTypes = D.getNumTypeObjects(); |
| 5484 | |
| 5485 | for (unsigned Idx = 0; Idx != NumTypes; ++Idx) |
| 5486 | if (DeclaratorChunk::Pipe == D.getTypeObject(Idx).Kind) |
| 5487 | return true; |
| 5488 | |
| 5489 | return false; |
| 5490 | } |
| 5491 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5492 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 5493 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 5494 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5495 | /// ptr-operator production. |
| 5496 | /// |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 5497 | /// If the grammar of this construct is extended, matching changes must also be |
Richard Smith | 1453e31 | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 5498 | /// made to TryParseDeclarator and MightBeDeclarator, and possibly to |
| 5499 | /// isConstructorDeclarator. |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 5500 | /// |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5501 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 5502 | /// [C] pointer[opt] direct-declarator |
| 5503 | /// [C++] direct-declarator |
| 5504 | /// [C++] ptr-operator declarator |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 5505 | /// |
| 5506 | /// pointer: [C99 6.7.5] |
| 5507 | /// '*' type-qualifier-list[opt] |
| 5508 | /// '*' type-qualifier-list[opt] pointer |
| 5509 | /// |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5510 | /// ptr-operator: |
| 5511 | /// '*' cv-qualifier-seq[opt] |
| 5512 | /// '&' |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 5513 | /// [C++0x] '&&' |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5514 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 5515 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5516 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5517 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 5518 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 66a985d | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 5519 | if (Diags.hasAllExtensionsSilenced()) |
| 5520 | D.setExtension(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5521 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5522 | // C++ member pointers start with a '::' or a nested-name. |
| 5523 | // Member pointers get special handling, since there's no place for the |
| 5524 | // scope spec in the generic path below. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5525 | if (getLangOpts().CPlusPlus && |
Richard Smith | 83c2ecf | 2016-02-02 23:34:49 +0000 | [diff] [blame] | 5526 | (Tok.is(tok::coloncolon) || Tok.is(tok::kw_decltype) || |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 5527 | (Tok.is(tok::identifier) && |
| 5528 | (NextToken().is(tok::coloncolon) || NextToken().is(tok::less))) || |
Chris Lattner | 803802d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 5529 | Tok.is(tok::annot_cxxscope))) { |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5530 | bool EnteringContext = |
| 5531 | D.getContext() == DeclaratorContext::FileContext || |
| 5532 | D.getContext() == DeclaratorContext::MemberContext; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5533 | CXXScopeSpec SS; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 5534 | ParseOptionalCXXScopeSpecifier(SS, nullptr, EnteringContext); |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 5535 | |
Jeffrey Yasskin | 4e150f8 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 5536 | if (SS.isNotEmpty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5537 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5538 | // The scope spec really belongs to the direct-declarator. |
Richard Smith | 5f044ad | 2013-01-08 22:43:49 +0000 | [diff] [blame] | 5539 | if (D.mayHaveIdentifier()) |
| 5540 | D.getCXXScopeSpec() = SS; |
| 5541 | else |
| 5542 | AnnotateScopeToken(SS, true); |
| 5543 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5544 | if (DirectDeclParser) |
| 5545 | (this->*DirectDeclParser)(D); |
| 5546 | return; |
| 5547 | } |
| 5548 | |
| 5549 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5550 | D.SetRangeEnd(Loc); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5551 | DeclSpec DS(AttrFactory); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5552 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5553 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5554 | |
| 5555 | // Recurse to parse whatever is left. |
| 5556 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 5557 | |
| 5558 | // Sema will have to catch (syntactically invalid) pointers into global |
| 5559 | // scope. It has to catch pointers into namespace scope anyway. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 5560 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 5561 | SS, DS.getTypeQualifiers(), DS.getEndLoc()), |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 5562 | std::move(DS.getAttributes()), |
| 5563 | /* Don't replace range end. */ SourceLocation()); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5564 | return; |
| 5565 | } |
| 5566 | } |
| 5567 | |
| 5568 | tok::TokenKind Kind = Tok.getKind(); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5569 | |
| 5570 | if (D.getDeclSpec().isTypeSpecPipe() && !isPipeDeclerator(D)) { |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 5571 | DeclSpec DS(AttrFactory); |
| 5572 | ParseTypeQualifierListOpt(DS); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5573 | |
| 5574 | D.AddTypeInfo( |
| 5575 | DeclaratorChunk::getPipe(DS.getTypeQualifiers(), DS.getPipeLoc()), |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 5576 | std::move(DS.getAttributes()), SourceLocation()); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5577 | } |
| 5578 | |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 5579 | // Not a pointer, C++ reference, or block. |
Richard Smith | 0b350b9 | 2014-10-28 16:55:02 +0000 | [diff] [blame] | 5580 | if (!isPtrOperatorToken(Kind, getLangOpts(), D.getContext())) { |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5581 | if (DirectDeclParser) |
| 5582 | (this->*DirectDeclParser)(D); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5583 | return; |
| 5584 | } |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5585 | |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 5586 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 5587 | // '&&' -> rvalue reference |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 5588 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5589 | D.SetRangeEnd(Loc); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5590 | |
Chris Lattner | 9eac931 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 5591 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 788404f | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 5592 | // Is a pointer. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5593 | DeclSpec DS(AttrFactory); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5594 | |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5595 | // GNU attributes are not allowed here in a new-type-id, but Declspec and |
| 5596 | // C++11 attributes are allowed. |
| 5597 | unsigned Reqs = AR_CXX11AttributesParsed | AR_DeclspecAttributesParsed | |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5598 | ((D.getContext() != DeclaratorContext::CXXNewContext) |
| 5599 | ? AR_GNUAttributesParsed |
| 5600 | : AR_GNUAttributesParsedAndRejected); |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5601 | ParseTypeQualifierListOpt(DS, Reqs, true, !D.mayOmitIdentifier()); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5602 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5603 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5604 | // Recursively parse the declarator. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5605 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 5606 | if (Kind == tok::star) |
| 5607 | // Remember that we parsed a pointer type, and remember the type-quals. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 5608 | D.AddTypeInfo(DeclaratorChunk::getPointer( |
| 5609 | DS.getTypeQualifiers(), Loc, DS.getConstSpecLoc(), |
| 5610 | DS.getVolatileSpecLoc(), DS.getRestrictSpecLoc(), |
| 5611 | DS.getAtomicSpecLoc(), DS.getUnalignedSpecLoc()), |
| 5612 | std::move(DS.getAttributes()), SourceLocation()); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 5613 | else |
| 5614 | // Remember that we parsed a Block type, and remember the type-quals. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 5615 | D.AddTypeInfo( |
| 5616 | DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), Loc), |
| 5617 | std::move(DS.getAttributes()), SourceLocation()); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5618 | } else { |
| 5619 | // Is a reference |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5620 | DeclSpec DS(AttrFactory); |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 5621 | |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 5622 | // Complain about rvalue references in C++03, but then go on and build |
| 5623 | // the declarator. |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5624 | if (Kind == tok::ampamp) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5625 | Diag(Loc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5626 | diag::warn_cxx98_compat_rvalue_reference : |
| 5627 | diag::ext_rvalue_reference); |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 5628 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5629 | // GNU-style and C++11 attributes are allowed here, as is restrict. |
| 5630 | ParseTypeQualifierListOpt(DS); |
| 5631 | D.ExtendWithDeclSpec(DS); |
| 5632 | |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 5633 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 5634 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 5635 | // template type argument, in which case the cv-qualifiers are ignored. |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 5636 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 5637 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 5638 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 5639 | diag::err_invalid_reference_qualifier_application) << "const"; |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 5640 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 5641 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 5642 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5643 | // 'restrict' is permitted as an extension. |
| 5644 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
| 5645 | Diag(DS.getAtomicSpecLoc(), |
| 5646 | diag::err_invalid_reference_qualifier_application) << "_Atomic"; |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 5647 | } |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5648 | |
| 5649 | // Recursively parse the declarator. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5650 | ParseDeclaratorInternal(D, DirectDeclParser); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5651 | |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 5652 | if (D.getNumTypeObjects() > 0) { |
| 5653 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 5654 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 5655 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | ebad6a2 | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 5656 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 5657 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 5658 | << II; |
| 5659 | else |
| 5660 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 5661 | << "type name"; |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 5662 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5663 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 5664 | // can go ahead and build the (technically ill-formed) |
| 5665 | // declarator: reference collapsing will take care of it. |
| 5666 | } |
| 5667 | } |
| 5668 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5669 | // Remember that we parsed a reference type. |
Chris Lattner | 788404f | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 5670 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 5671 | Kind == tok::amp), |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 5672 | std::move(DS.getAttributes()), SourceLocation()); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5673 | } |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 5674 | } |
| 5675 | |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5676 | // When correcting from misplaced brackets before the identifier, the location |
| 5677 | // is saved inside the declarator so that other diagnostic messages can use |
| 5678 | // them. This extracts and returns that location, or returns the provided |
| 5679 | // location if a stored location does not exist. |
| 5680 | static SourceLocation getMissingDeclaratorIdLoc(Declarator &D, |
| 5681 | SourceLocation Loc) { |
| 5682 | if (D.getName().StartLocation.isInvalid() && |
| 5683 | D.getName().EndLocation.isValid()) |
| 5684 | return D.getName().EndLocation; |
| 5685 | |
| 5686 | return Loc; |
| 5687 | } |
| 5688 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5689 | /// ParseDirectDeclarator |
| 5690 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5691 | /// [C99] identifier |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5692 | /// '(' declarator ')' |
| 5693 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5694 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 5695 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 5696 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 5697 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 5698 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5699 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 5700 | /// attribute-specifier-seq[opt] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5701 | /// direct-declarator '(' parameter-type-list ')' |
| 5702 | /// direct-declarator '(' identifier-list[opt] ')' |
| 5703 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 5704 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 22c40fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 5705 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 5706 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5707 | /// [C++11] direct-declarator '(' parameter-declaration-clause ')' |
| 5708 | /// attribute-specifier-seq[opt] cv-qualifier-seq[opt] |
| 5709 | /// ref-qualifier[opt] exception-specification[opt] |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 5710 | /// [C++] declarator-id |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5711 | /// [C++11] declarator-id attribute-specifier-seq[opt] |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5712 | /// |
| 5713 | /// declarator-id: [C++ 8] |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 5714 | /// '...'[opt] id-expression |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5715 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 5716 | /// |
| 5717 | /// id-expression: [C++ 5.1] |
| 5718 | /// unqualified-id |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5719 | /// qualified-id |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5720 | /// |
| 5721 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5722 | /// identifier |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 5723 | /// operator-function-id |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5724 | /// conversion-function-id |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5725 | /// '~' class-name |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 5726 | /// template-id |
Argyrios Kyrtzidis | e442635 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 5727 | /// |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 5728 | /// C++17 adds the following, which we also handle here: |
| 5729 | /// |
| 5730 | /// simple-declaration: |
| 5731 | /// <decl-spec> '[' identifier-list ']' brace-or-equal-initializer ';' |
| 5732 | /// |
Richard Smith | 1453e31 | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 5733 | /// Note, any additional constructs added here may need corresponding changes |
| 5734 | /// in isConstructorDeclarator. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5735 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5736 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 5737 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5738 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 5739 | // This might be a C++17 structured binding. |
| 5740 | if (Tok.is(tok::l_square) && !D.mayOmitIdentifier() && |
| 5741 | D.getCXXScopeSpec().isEmpty()) |
| 5742 | return ParseDecompositionDeclarator(D); |
| 5743 | |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 5744 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR inside a class, in |
| 5745 | // this context it is a bitfield. Also in range-based for statement colon |
| 5746 | // may delimit for-range-declaration. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5747 | ColonProtectionRAIIObject X( |
| 5748 | *this, D.getContext() == DeclaratorContext::MemberContext || |
| 5749 | (D.getContext() == DeclaratorContext::ForContext && |
| 5750 | getLangOpts().CPlusPlus11)); |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 5751 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5752 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5753 | if (D.getCXXScopeSpec().isEmpty()) { |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5754 | bool EnteringContext = |
| 5755 | D.getContext() == DeclaratorContext::FileContext || |
| 5756 | D.getContext() == DeclaratorContext::MemberContext; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 5757 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), nullptr, |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 5758 | EnteringContext); |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 5759 | } |
| 5760 | |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5761 | if (D.getCXXScopeSpec().isValid()) { |
Richard Smith | 64e033f | 2015-01-15 00:48:52 +0000 | [diff] [blame] | 5762 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), |
| 5763 | D.getCXXScopeSpec())) |
John McCall | 2b058ef | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 5764 | // Change the declaration context for name lookup, until this function |
| 5765 | // is exited (and the declarator has been parsed). |
| 5766 | DeclScopeObj.EnterDeclaratorScope(); |
Alex Lorenz | e151f010 | 2016-12-07 10:24:44 +0000 | [diff] [blame] | 5767 | else if (getObjCDeclContext()) { |
| 5768 | // Ensure that we don't interpret the next token as an identifier when |
| 5769 | // dealing with declarations in an Objective-C container. |
| 5770 | D.SetIdentifier(nullptr, Tok.getLocation()); |
| 5771 | D.setInvalidType(true); |
| 5772 | ConsumeToken(); |
| 5773 | goto PastIdentifier; |
| 5774 | } |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5775 | } |
| 5776 | |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 5777 | // C++0x [dcl.fct]p14: |
Faisal Vali | 8435a69 | 2014-12-04 12:40:21 +0000 | [diff] [blame] | 5778 | // There is a syntactic ambiguity when an ellipsis occurs at the end of a |
| 5779 | // parameter-declaration-clause without a preceding comma. In this case, |
| 5780 | // the ellipsis is parsed as part of the abstract-declarator if the type |
| 5781 | // of the parameter either names a template parameter pack that has not |
| 5782 | // been expanded or contains auto; otherwise, it is parsed as part of the |
| 5783 | // parameter-declaration-clause. |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5784 | if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() && |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5785 | !((D.getContext() == DeclaratorContext::PrototypeContext || |
| 5786 | D.getContext() == DeclaratorContext::LambdaExprParameterContext || |
| 5787 | D.getContext() == DeclaratorContext::BlockLiteralContext) && |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 5788 | NextToken().is(tok::r_paren) && |
Richard Smith | b19337f | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 5789 | !D.hasGroupingParens() && |
Faisal Vali | 8435a69 | 2014-12-04 12:40:21 +0000 | [diff] [blame] | 5790 | !Actions.containsUnexpandedParameterPacks(D) && |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 5791 | D.getDeclSpec().getTypeSpecType() != TST_auto)) { |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5792 | SourceLocation EllipsisLoc = ConsumeToken(); |
Richard Smith | 0b350b9 | 2014-10-28 16:55:02 +0000 | [diff] [blame] | 5793 | if (isPtrOperatorToken(Tok.getKind(), getLangOpts(), D.getContext())) { |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5794 | // The ellipsis was put in the wrong place. Recover, and explain to |
| 5795 | // the user what they should have done. |
| 5796 | ParseDeclarator(D); |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 5797 | if (EllipsisLoc.isValid()) |
| 5798 | DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D); |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5799 | return; |
| 5800 | } else |
| 5801 | D.setEllipsisLoc(EllipsisLoc); |
| 5802 | |
| 5803 | // The ellipsis can't be followed by a parenthesized declarator. We |
| 5804 | // check for that in ParseParenDeclarator, after we have disambiguated |
| 5805 | // the l_paren token. |
| 5806 | } |
| 5807 | |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 5808 | if (Tok.isOneOf(tok::identifier, tok::kw_operator, tok::annot_template_id, |
| 5809 | tok::tilde)) { |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5810 | // We found something that indicates the start of an unqualified-id. |
| 5811 | // Parse that unqualified-id. |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 5812 | bool AllowConstructorName; |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5813 | bool AllowDeductionGuide; |
| 5814 | if (D.getDeclSpec().hasTypeSpecifier()) { |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 5815 | AllowConstructorName = false; |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5816 | AllowDeductionGuide = false; |
| 5817 | } else if (D.getCXXScopeSpec().isSet()) { |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 5818 | AllowConstructorName = |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5819 | (D.getContext() == DeclaratorContext::FileContext || |
| 5820 | D.getContext() == DeclaratorContext::MemberContext); |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5821 | AllowDeductionGuide = false; |
| 5822 | } else { |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5823 | AllowConstructorName = |
| 5824 | (D.getContext() == DeclaratorContext::MemberContext); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5825 | AllowDeductionGuide = |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5826 | (D.getContext() == DeclaratorContext::FileContext || |
| 5827 | D.getContext() == DeclaratorContext::MemberContext); |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5828 | } |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 5829 | |
Richard Smith | 64e033f | 2015-01-15 00:48:52 +0000 | [diff] [blame] | 5830 | bool HadScope = D.getCXXScopeSpec().isValid(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5831 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 5832 | /*EnteringContext=*/true, |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 5833 | /*AllowDestructorName=*/true, AllowConstructorName, |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 5834 | AllowDeductionGuide, nullptr, nullptr, |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5835 | D.getName()) || |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5836 | // Once we're past the identifier, if the scope was bad, mark the |
| 5837 | // whole declarator bad. |
| 5838 | D.getCXXScopeSpec().isInvalid()) { |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5839 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5840 | D.setInvalidType(true); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5841 | } else { |
Richard Smith | 64e033f | 2015-01-15 00:48:52 +0000 | [diff] [blame] | 5842 | // ParseUnqualifiedId might have parsed a scope specifier during error |
| 5843 | // recovery. If it did so, enter that scope. |
| 5844 | if (!HadScope && D.getCXXScopeSpec().isValid() && |
| 5845 | Actions.ShouldEnterDeclaratorScope(getCurScope(), |
| 5846 | D.getCXXScopeSpec())) |
| 5847 | DeclScopeObj.EnterDeclaratorScope(); |
| 5848 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5849 | // Parsed the unqualified-id; update range information and move along. |
| 5850 | if (D.getSourceRange().getBegin().isInvalid()) |
| 5851 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 5852 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5853 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5854 | goto PastIdentifier; |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5855 | } |
Richard Smith | d63db6e | 2015-12-19 02:40:19 +0000 | [diff] [blame] | 5856 | |
| 5857 | if (D.getCXXScopeSpec().isNotEmpty()) { |
| 5858 | // We have a scope specifier but no following unqualified-id. |
| 5859 | Diag(PP.getLocForEndOfToken(D.getCXXScopeSpec().getEndLoc()), |
| 5860 | diag::err_expected_unqualified_id) |
| 5861 | << /*C++*/1; |
| 5862 | D.SetIdentifier(nullptr, Tok.getLocation()); |
| 5863 | goto PastIdentifier; |
| 5864 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5865 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5866 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5867 | "There's a C++-specific check for tok::identifier above"); |
| 5868 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 5869 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
Richard Trieu | 2664dc1 | 2014-05-05 22:06:50 +0000 | [diff] [blame] | 5870 | D.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5871 | ConsumeToken(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5872 | goto PastIdentifier; |
Richard Smith | 74639b1 | 2017-05-19 01:54:59 +0000 | [diff] [blame] | 5873 | } else if (Tok.is(tok::identifier) && !D.mayHaveIdentifier()) { |
| 5874 | // We're not allowed an identifier here, but we got one. Try to figure out |
| 5875 | // if the user was trying to attach a name to the type, or whether the name |
| 5876 | // is some unrelated trailing syntax. |
| 5877 | bool DiagnoseIdentifier = false; |
| 5878 | if (D.hasGroupingParens()) |
| 5879 | // An identifier within parens is unlikely to be intended to be anything |
| 5880 | // other than a name being "declared". |
| 5881 | DiagnoseIdentifier = true; |
Richard Smith | 77a9c60 | 2018-02-28 03:02:23 +0000 | [diff] [blame] | 5882 | else if (D.getContext() == DeclaratorContext::TemplateArgContext) |
Richard Smith | 74639b1 | 2017-05-19 01:54:59 +0000 | [diff] [blame] | 5883 | // T<int N> is an accidental identifier; T<int N indicates a missing '>'. |
| 5884 | DiagnoseIdentifier = |
| 5885 | NextToken().isOneOf(tok::comma, tok::greater, tok::greatergreater); |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5886 | else if (D.getContext() == DeclaratorContext::AliasDeclContext || |
| 5887 | D.getContext() == DeclaratorContext::AliasTemplateContext) |
Richard Smith | 74639b1 | 2017-05-19 01:54:59 +0000 | [diff] [blame] | 5888 | // The most likely error is that the ';' was forgotten. |
| 5889 | DiagnoseIdentifier = NextToken().isOneOf(tok::comma, tok::semi); |
Richard Smith | e303e35 | 2018-02-02 22:24:54 +0000 | [diff] [blame] | 5890 | else if ((D.getContext() == DeclaratorContext::TrailingReturnContext || |
| 5891 | D.getContext() == DeclaratorContext::TrailingReturnVarContext) && |
Richard Smith | 74639b1 | 2017-05-19 01:54:59 +0000 | [diff] [blame] | 5892 | !isCXX11VirtSpecifier(Tok)) |
| 5893 | DiagnoseIdentifier = NextToken().isOneOf( |
| 5894 | tok::comma, tok::semi, tok::equal, tok::l_brace, tok::kw_try); |
| 5895 | if (DiagnoseIdentifier) { |
Richard Smith | f39720b | 2013-10-13 22:12:28 +0000 | [diff] [blame] | 5896 | Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id) |
| 5897 | << FixItHint::CreateRemoval(Tok.getLocation()); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5898 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Richard Smith | f39720b | 2013-10-13 22:12:28 +0000 | [diff] [blame] | 5899 | ConsumeToken(); |
| 5900 | goto PastIdentifier; |
| 5901 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5902 | } |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5903 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5904 | if (Tok.is(tok::l_paren)) { |
Richard Smith | e303e35 | 2018-02-02 22:24:54 +0000 | [diff] [blame] | 5905 | // If this might be an abstract-declarator followed by a direct-initializer, |
| 5906 | // check whether this is a valid declarator chunk. If it can't be, assume |
| 5907 | // that it's an initializer instead. |
| 5908 | if (D.mayOmitIdentifier() && D.mayBeFollowedByCXXDirectInit()) { |
| 5909 | RevertingTentativeParsingAction PA(*this); |
| 5910 | if (TryParseDeclarator(true, D.mayHaveIdentifier(), true) == |
| 5911 | TPResult::False) { |
| 5912 | D.SetIdentifier(nullptr, Tok.getLocation()); |
| 5913 | goto PastIdentifier; |
| 5914 | } |
| 5915 | } |
| 5916 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5917 | // direct-declarator: '(' declarator ')' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 5918 | // direct-declarator: '(' attributes declarator ')' |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5919 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 5920 | ParseParenDeclarator(D); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5921 | |
| 5922 | // If the declarator was parenthesized, we entered the declarator |
| 5923 | // scope when parsing the parenthesized declarator, then exited |
| 5924 | // the scope already. Re-enter the scope, if we need to. |
| 5925 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 5926 | // If there was an error parsing parenthesized declarator, declarator |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5927 | // scope may have been entered before. Don't do it again. |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 5928 | if (!D.isInvalidType() && |
Nico Weber | 6b05f38 | 2015-02-18 04:53:03 +0000 | [diff] [blame] | 5929 | Actions.ShouldEnterDeclaratorScope(getCurScope(), |
| 5930 | D.getCXXScopeSpec())) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5931 | // Change the declaration context for name lookup, until this function |
| 5932 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 5933 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5934 | } |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5935 | } else if (D.mayOmitIdentifier()) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5936 | // This could be something simple like "int" (in which case the declarator |
| 5937 | // portion is empty), if an abstract-declarator is allowed. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5938 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Richard Smith | b19337f | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 5939 | |
| 5940 | // The grammar for abstract-pack-declarator does not allow grouping parens. |
| 5941 | // FIXME: Revisit this once core issue 1488 is resolved. |
| 5942 | if (D.hasEllipsis() && D.hasGroupingParens()) |
| 5943 | Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()), |
| 5944 | diag::ext_abstract_pack_declarator_parens); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5945 | } else { |
David Blaikie | 5d577a2 | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 5946 | if (Tok.getKind() == tok::annot_pragma_parser_crash) |
David Blaikie | 5bd4c2a | 2012-08-21 18:56:49 +0000 | [diff] [blame] | 5947 | LLVM_BUILTIN_TRAP; |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5948 | if (Tok.is(tok::l_square)) |
| 5949 | return ParseMisplacedBracketDeclarator(D); |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5950 | if (D.getContext() == DeclaratorContext::MemberContext) { |
Alex Lorenz | f127821 | 2017-04-11 15:01:53 +0000 | [diff] [blame] | 5951 | // Objective-C++: Detect C++ keywords and try to prevent further errors by |
| 5952 | // treating these keyword as valid member names. |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 5953 | if (getLangOpts().ObjC && getLangOpts().CPlusPlus && |
Alex Lorenz | f127821 | 2017-04-11 15:01:53 +0000 | [diff] [blame] | 5954 | Tok.getIdentifierInfo() && |
| 5955 | Tok.getIdentifierInfo()->isCPlusPlusKeyword(getLangOpts())) { |
| 5956 | Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()), |
| 5957 | diag::err_expected_member_name_or_semi_objcxx_keyword) |
| 5958 | << Tok.getIdentifierInfo() |
| 5959 | << (D.getDeclSpec().isEmpty() ? SourceRange() |
| 5960 | : D.getDeclSpec().getSourceRange()); |
| 5961 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 5962 | D.SetRangeEnd(Tok.getLocation()); |
| 5963 | ConsumeToken(); |
| 5964 | goto PastIdentifier; |
| 5965 | } |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5966 | Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()), |
| 5967 | diag::err_expected_member_name_or_semi) |
Richard Trieu | a134240 | 2014-05-02 23:40:32 +0000 | [diff] [blame] | 5968 | << (D.getDeclSpec().isEmpty() ? SourceRange() |
| 5969 | : D.getDeclSpec().getSourceRange()); |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5970 | } else if (getLangOpts().CPlusPlus) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 5971 | if (Tok.isOneOf(tok::period, tok::arrow)) |
Richard Trieu | 9c67267 | 2013-01-26 02:31:38 +0000 | [diff] [blame] | 5972 | Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow); |
Richard Trieu | 2f58696 | 2013-09-05 02:31:33 +0000 | [diff] [blame] | 5973 | else { |
| 5974 | SourceLocation Loc = D.getCXXScopeSpec().getEndLoc(); |
| 5975 | if (Tok.isAtStartOfLine() && Loc.isValid()) |
| 5976 | Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id) |
| 5977 | << getLangOpts().CPlusPlus; |
| 5978 | else |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5979 | Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()), |
| 5980 | diag::err_expected_unqualified_id) |
Richard Trieu | 2f58696 | 2013-09-05 02:31:33 +0000 | [diff] [blame] | 5981 | << getLangOpts().CPlusPlus; |
| 5982 | } |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5983 | } else { |
| 5984 | Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()), |
| 5985 | diag::err_expected_either) |
| 5986 | << tok::identifier << tok::l_paren; |
| 5987 | } |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5988 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Chris Lattner | 8c5dd73 | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 5989 | D.setInvalidType(true); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5990 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5991 | |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5992 | PastIdentifier: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5993 | assert(D.isPastIdentifier() && |
| 5994 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5995 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5996 | // Don't parse attributes unless we have parsed an unparenthesized name. |
| 5997 | if (D.hasName() && !D.getNumTypeObjects()) |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5998 | MaybeParseCXX11Attributes(D); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5999 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 6000 | while (1) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 6001 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 6002 | // Enter function-declaration scope, limiting any declarators to the |
| 6003 | // function prototype scope, including parameter declarators. |
| 6004 | ParseScope PrototypeScope(this, |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 6005 | Scope::FunctionPrototypeScope|Scope::DeclScope| |
| 6006 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 6007 | ? Scope::FunctionDeclarationScope : 0)); |
| 6008 | |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6009 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 6010 | // In such a case, check if we actually have a function declarator; if it |
| 6011 | // is not, the declarator has been fully parsed. |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 6012 | bool IsAmbiguous = false; |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 6013 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 6014 | // The name of the declarator, if any, is tentatively declared within |
| 6015 | // a possible direct initializer. |
| 6016 | TentativelyDeclaredIdentifiers.push_back(D.getIdentifier()); |
| 6017 | bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous); |
| 6018 | TentativelyDeclaredIdentifiers.pop_back(); |
| 6019 | if (!IsFunctionDecl) |
| 6020 | break; |
| 6021 | } |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 6022 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6023 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 6024 | T.consumeOpen(); |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 6025 | ParseFunctionDeclarator(D, attrs, T, IsAmbiguous); |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 6026 | PrototypeScope.Exit(); |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 6027 | } else if (Tok.is(tok::l_square)) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6028 | ParseBracketDeclarator(D); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 6029 | } else { |
| 6030 | break; |
| 6031 | } |
| 6032 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 6033 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 6034 | |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 6035 | void Parser::ParseDecompositionDeclarator(Declarator &D) { |
| 6036 | assert(Tok.is(tok::l_square)); |
| 6037 | |
| 6038 | // If this doesn't look like a structured binding, maybe it's a misplaced |
| 6039 | // array declarator. |
| 6040 | // FIXME: Consume the l_square first so we don't need extra lookahead for |
| 6041 | // this. |
| 6042 | if (!(NextToken().is(tok::identifier) && |
| 6043 | GetLookAheadToken(2).isOneOf(tok::comma, tok::r_square)) && |
| 6044 | !(NextToken().is(tok::r_square) && |
| 6045 | GetLookAheadToken(2).isOneOf(tok::equal, tok::l_brace))) |
| 6046 | return ParseMisplacedBracketDeclarator(D); |
| 6047 | |
| 6048 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 6049 | T.consumeOpen(); |
| 6050 | |
| 6051 | SmallVector<DecompositionDeclarator::Binding, 32> Bindings; |
| 6052 | while (Tok.isNot(tok::r_square)) { |
| 6053 | if (!Bindings.empty()) { |
| 6054 | if (Tok.is(tok::comma)) |
| 6055 | ConsumeToken(); |
| 6056 | else { |
| 6057 | if (Tok.is(tok::identifier)) { |
| 6058 | SourceLocation EndLoc = getEndOfPreviousToken(); |
| 6059 | Diag(EndLoc, diag::err_expected) |
| 6060 | << tok::comma << FixItHint::CreateInsertion(EndLoc, ","); |
| 6061 | } else { |
| 6062 | Diag(Tok, diag::err_expected_comma_or_rsquare); |
| 6063 | } |
| 6064 | |
| 6065 | SkipUntil(tok::r_square, tok::comma, tok::identifier, |
| 6066 | StopAtSemi | StopBeforeMatch); |
| 6067 | if (Tok.is(tok::comma)) |
| 6068 | ConsumeToken(); |
| 6069 | else if (Tok.isNot(tok::identifier)) |
| 6070 | break; |
| 6071 | } |
| 6072 | } |
| 6073 | |
| 6074 | if (Tok.isNot(tok::identifier)) { |
| 6075 | Diag(Tok, diag::err_expected) << tok::identifier; |
| 6076 | break; |
| 6077 | } |
| 6078 | |
| 6079 | Bindings.push_back({Tok.getIdentifierInfo(), Tok.getLocation()}); |
| 6080 | ConsumeToken(); |
| 6081 | } |
| 6082 | |
| 6083 | if (Tok.isNot(tok::r_square)) |
| 6084 | // We've already diagnosed a problem here. |
| 6085 | T.skipToEnd(); |
| 6086 | else { |
| 6087 | // C++17 does not allow the identifier-list in a structured binding |
| 6088 | // to be empty. |
| 6089 | if (Bindings.empty()) |
| 6090 | Diag(Tok.getLocation(), diag::ext_decomp_decl_empty); |
| 6091 | |
| 6092 | T.consumeClose(); |
| 6093 | } |
| 6094 | |
| 6095 | return D.setDecompositionBindings(T.getOpenLocation(), Bindings, |
| 6096 | T.getCloseLocation()); |
| 6097 | } |
| 6098 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 6099 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 6100 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6101 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 6102 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 6103 | /// |
| 6104 | /// direct-declarator: |
| 6105 | /// '(' declarator ')' |
| 6106 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 6107 | /// direct-declarator '(' parameter-type-list ')' |
| 6108 | /// direct-declarator '(' identifier-list[opt] ')' |
| 6109 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 6110 | /// parameter-type-list[opt] ')' |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 6111 | /// |
| 6112 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6113 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 6114 | T.consumeOpen(); |
| 6115 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 6116 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6117 | |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 6118 | // Eat any attributes before we look at whether this is a grouping or function |
| 6119 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 6120 | // the type being built up, for example: |
| 6121 | // int (__attribute__(()) *x)(long y) |
| 6122 | // If this ends up not being a grouping paren, the attribute applies to the |
| 6123 | // first argument, for example: |
| 6124 | // int (__attribute__(()) int x) |
| 6125 | // In either case, we need to eat any attributes to be able to determine what |
| 6126 | // sort of paren this is. |
| 6127 | // |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 6128 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 6129 | bool RequiresArg = false; |
| 6130 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 6131 | ParseGNUAttributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6132 | |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 6133 | // We require that the argument list (if this is a non-grouping paren) be |
| 6134 | // present even if the attribute list was empty. |
| 6135 | RequiresArg = true; |
| 6136 | } |
Chad Rosier | eea9ca7 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 6137 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 6138 | // Eat any Microsoft extensions. |
Chad Rosier | eea9ca7 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 6139 | ParseMicrosoftTypeAttributes(attrs); |
| 6140 | |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 6141 | // Eat any Borland extensions. |
Ted Kremenek | 5eec2b0 | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 6142 | if (Tok.is(tok::kw___pascal)) |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 6143 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6144 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 6145 | // If we haven't past the identifier yet (or where the identifier would be |
| 6146 | // stored, if this is an abstract declarator), then this is probably just |
| 6147 | // grouping parens. However, if this could be an abstract-declarator, then |
| 6148 | // this could also be the start of function arguments (consider 'void()'). |
| 6149 | bool isGrouping; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6150 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 6151 | if (!D.mayOmitIdentifier()) { |
| 6152 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 6153 | // paren, because we haven't seen the identifier yet. |
| 6154 | isGrouping = true; |
| 6155 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 6156 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) && |
| 6157 | NextToken().is(tok::r_paren)) || // C++ int(...) |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 6158 | isDeclarationSpecifier() || // 'int(int)' is a function. |
| 6159 | isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function. |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 6160 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 6161 | // considered to be a type, not a K&R identifier-list. |
| 6162 | isGrouping = false; |
| 6163 | } else { |
| 6164 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 6165 | isGrouping = true; |
| 6166 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6167 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 6168 | // If this is a grouping paren, handle: |
| 6169 | // direct-declarator: '(' declarator ')' |
| 6170 | // direct-declarator: '(' attributes declarator ')' |
| 6171 | if (isGrouping) { |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 6172 | SourceLocation EllipsisLoc = D.getEllipsisLoc(); |
| 6173 | D.setEllipsisLoc(SourceLocation()); |
| 6174 | |
Argyrios Kyrtzidis | 8ae3684 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 6175 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6176 | D.setGroupingParens(true); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 6177 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 6178 | // Match the ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6179 | T.consumeClose(); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6180 | D.AddTypeInfo( |
| 6181 | DeclaratorChunk::getParen(T.getOpenLocation(), T.getCloseLocation()), |
| 6182 | std::move(attrs), T.getCloseLocation()); |
Argyrios Kyrtzidis | 8ae3684 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 6183 | |
| 6184 | D.setGroupingParens(hadGroupingParens); |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 6185 | |
| 6186 | // An ellipsis cannot be placed outside parentheses. |
| 6187 | if (EllipsisLoc.isValid()) |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 6188 | DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D); |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 6189 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 6190 | return; |
| 6191 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6192 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 6193 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 6194 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 6195 | // identifier (and remember where it would have been), then call into |
| 6196 | // ParseFunctionDeclarator to handle of argument list. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6197 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 6198 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 6199 | // Enter function-declaration scope, limiting any declarators to the |
| 6200 | // function prototype scope, including parameter declarators. |
| 6201 | ParseScope PrototypeScope(this, |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 6202 | Scope::FunctionPrototypeScope | Scope::DeclScope | |
| 6203 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 6204 | ? Scope::FunctionDeclarationScope : 0)); |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 6205 | ParseFunctionDeclarator(D, attrs, T, false, RequiresArg); |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 6206 | PrototypeScope.Exit(); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 6207 | } |
| 6208 | |
| 6209 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 6210 | /// declarator D up to a paren, which indicates that we are parsing function |
| 6211 | /// arguments. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 6212 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6213 | /// If FirstArgAttrs is non-null, then the caller parsed those arguments |
| 6214 | /// immediately after the open paren - they should be considered to be the |
| 6215 | /// first argument of a parameter. |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 6216 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6217 | /// If RequiresArg is true, then the first argument of the function is required |
| 6218 | /// to be present and required to not be an identifier list. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6219 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6220 | /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt], |
| 6221 | /// (C++11) ref-qualifier[opt], exception-specification[opt], |
| 6222 | /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt]. |
| 6223 | /// |
| 6224 | /// [C++11] exception-specification: |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6225 | /// dynamic-exception-specification |
| 6226 | /// noexcept-specification |
| 6227 | /// |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6228 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6229 | ParsedAttributes &FirstArgAttrs, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6230 | BalancedDelimiterTracker &Tracker, |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 6231 | bool IsAmbiguous, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6232 | bool RequiresArg) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 6233 | assert(getCurScope()->isFunctionPrototypeScope() && |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 6234 | "Should call from a Function scope"); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6235 | // lparen is already consumed! |
| 6236 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 6237 | |
| 6238 | // This should be true when the function has typed arguments. |
| 6239 | // Otherwise, it is treated as a K&R-style function. |
| 6240 | bool HasProto = false; |
| 6241 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6242 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6243 | // Remember where we see an ellipsis, if any. |
| 6244 | SourceLocation EllipsisLoc; |
| 6245 | |
| 6246 | DeclSpec DS(AttrFactory); |
| 6247 | bool RefQualifierIsLValueRef = true; |
| 6248 | SourceLocation RefQualifierLoc; |
| 6249 | ExceptionSpecificationType ESpecType = EST_None; |
| 6250 | SourceRange ESpecRange; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6251 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 6252 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6253 | ExprResult NoexceptExpr; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 6254 | CachedTokens *ExceptionSpecTokens = nullptr; |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 6255 | ParsedAttributesWithRange FnAttrs(AttrFactory); |
Richard Smith | 700537c | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 6256 | TypeResult TrailingReturnType; |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6257 | |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 6258 | /* LocalEndLoc is the end location for the local FunctionTypeLoc. |
| 6259 | EndLoc is the end location for the function declarator. |
| 6260 | They differ for trailing return types. */ |
| 6261 | SourceLocation StartLoc, LocalEndLoc, EndLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 6262 | SourceLocation LParenLoc, RParenLoc; |
| 6263 | LParenLoc = Tracker.getOpenLocation(); |
| 6264 | StartLoc = LParenLoc; |
| 6265 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6266 | if (isFunctionDeclaratorIdentifierList()) { |
| 6267 | if (RequiresArg) |
| 6268 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 6269 | |
| 6270 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 6271 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6272 | Tracker.consumeClose(); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 6273 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 6274 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 6275 | EndLoc = RParenLoc; |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 6276 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6277 | // If there are attributes following the identifier list, parse them and |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 6278 | // prohibit them. |
| 6279 | MaybeParseCXX11Attributes(FnAttrs); |
| 6280 | ProhibitAttributes(FnAttrs); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6281 | } else { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6282 | if (Tok.isNot(tok::r_paren)) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6283 | ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6284 | EllipsisLoc); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6285 | else if (RequiresArg) |
| 6286 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 6287 | |
Alexey Bader | 1f27794 | 2017-10-11 11:16:31 +0000 | [diff] [blame] | 6288 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus |
| 6289 | || getLangOpts().OpenCL; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6290 | |
| 6291 | // If we have the closing ')', eat it. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6292 | Tracker.consumeClose(); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 6293 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 6294 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 6295 | EndLoc = RParenLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6296 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6297 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6298 | // FIXME: Accept these components in any order, and produce fixits to |
| 6299 | // correct the order if the user gets it wrong. Ideally we should deal |
Ehsan Akhgari | 93ed5cf | 2015-03-25 00:53:27 +0000 | [diff] [blame] | 6300 | // with the pure-specifier in the same way. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6301 | |
| 6302 | // Parse cv-qualifier-seq[opt]. |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 6303 | ParseTypeQualifierListOpt(DS, AR_NoAttributesParsed, |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 6304 | /*AtomicAllowed*/ false, |
| 6305 | /*IdentifierRequired=*/false, |
| 6306 | llvm::function_ref<void()>([&]() { |
| 6307 | Actions.CodeCompleteFunctionQualifiers(DS, D); |
| 6308 | })); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6309 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
| 6310 | EndLoc = DS.getSourceRange().getEnd(); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6311 | } |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6312 | |
| 6313 | // Parse ref-qualifier[opt]. |
Ehsan Akhgari | c07d1e2 | 2015-03-25 00:53:33 +0000 | [diff] [blame] | 6314 | if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc)) |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6315 | EndLoc = RefQualifierLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6316 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 6317 | // C++11 [expr.prim.general]p3: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 6318 | // If a declaration declares a member function or member function |
| 6319 | // template of a class X, the expression this is a prvalue of type |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 6320 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 6321 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 6322 | // declarator. |
Richard Smith | ad1bbb9 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 6323 | // FIXME: currently, "static" case isn't handled correctly. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 6324 | bool IsCXX11MemberFunction = |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6325 | getLangOpts().CPlusPlus11 && |
Richard Smith | 990a692 | 2014-01-17 21:01:18 +0000 | [diff] [blame] | 6326 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 6327 | (D.getContext() == DeclaratorContext::MemberContext |
Richard Smith | ad1bbb9 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 6328 | ? !D.getDeclSpec().isFriendSpecified() |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 6329 | : D.getContext() == DeclaratorContext::FileContext && |
Richard Smith | ad1bbb9 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 6330 | D.getCXXScopeSpec().isValid() && |
| 6331 | Actions.CurContext->isRecord()); |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 6332 | |
| 6333 | Qualifiers Q = Qualifiers::fromCVRUMask(DS.getTypeQualifiers()); |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 6334 | if (D.getDeclSpec().hasConstexprSpecifier() && !getLangOpts().CPlusPlus14) |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 6335 | Q.addConst(); |
Anastasia Stulova | 5cffa45 | 2019-01-21 16:01:38 +0000 | [diff] [blame] | 6336 | // FIXME: Collect C++ address spaces. |
| 6337 | // If there are multiple different address spaces, the source is invalid. |
| 6338 | // Carry on using the first addr space for the qualifiers of 'this'. |
| 6339 | // The diagnostic will be given later while creating the function |
| 6340 | // prototype for the method. |
| 6341 | if (getLangOpts().OpenCLCPlusPlus) { |
| 6342 | for (ParsedAttr &attr : DS.getAttributes()) { |
| 6343 | LangAS ASIdx = attr.asOpenCLLangAS(); |
| 6344 | if (ASIdx != LangAS::Default) { |
| 6345 | Q.addAddressSpace(ASIdx); |
| 6346 | break; |
| 6347 | } |
| 6348 | } |
| 6349 | } |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 6350 | |
| 6351 | Sema::CXXThisScopeRAII ThisScope( |
| 6352 | Actions, dyn_cast<CXXRecordDecl>(Actions.CurContext), Q, |
| 6353 | IsCXX11MemberFunction); |
Richard Smith | 2331bbf | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 6354 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6355 | // Parse exception-specification[opt]. |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 6356 | bool Delayed = D.isFirstDeclarationOfMember() && |
Richard Smith | 3ef3e89 | 2014-11-20 22:32:11 +0000 | [diff] [blame] | 6357 | D.isFunctionDeclaratorAFunctionDeclaration(); |
| 6358 | if (Delayed && Actions.isLibstdcxxEagerExceptionSpecHack(D) && |
| 6359 | GetLookAheadToken(0).is(tok::kw_noexcept) && |
| 6360 | GetLookAheadToken(1).is(tok::l_paren) && |
| 6361 | GetLookAheadToken(2).is(tok::kw_noexcept) && |
| 6362 | GetLookAheadToken(3).is(tok::l_paren) && |
| 6363 | GetLookAheadToken(4).is(tok::identifier) && |
| 6364 | GetLookAheadToken(4).getIdentifierInfo()->isStr("swap")) { |
| 6365 | // HACK: We've got an exception-specification |
| 6366 | // noexcept(noexcept(swap(...))) |
| 6367 | // or |
| 6368 | // noexcept(noexcept(swap(...)) && noexcept(swap(...))) |
| 6369 | // on a 'swap' member function. This is a libstdc++ bug; the lookup |
| 6370 | // for 'swap' will only find the function we're currently declaring, |
| 6371 | // whereas it expects to find a non-member swap through ADL. Turn off |
| 6372 | // delayed parsing to give it a chance to find what it expects. |
| 6373 | Delayed = false; |
| 6374 | } |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 6375 | ESpecType = tryParseExceptionSpecification(Delayed, |
| 6376 | ESpecRange, |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 6377 | DynamicExceptions, |
| 6378 | DynamicExceptionRanges, |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 6379 | NoexceptExpr, |
| 6380 | ExceptionSpecTokens); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6381 | if (ESpecType != EST_None) |
| 6382 | EndLoc = ESpecRange.getEnd(); |
| 6383 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6384 | // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes |
| 6385 | // after the exception-specification. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 6386 | MaybeParseCXX11Attributes(FnAttrs); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6387 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6388 | // Parse trailing-return-type[opt]. |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 6389 | LocalEndLoc = EndLoc; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6390 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) { |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 6391 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 6392 | if (D.getDeclSpec().getTypeSpecType() == TST_auto) |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 6393 | StartLoc = D.getDeclSpec().getTypeSpecTypeLoc(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 6394 | LocalEndLoc = Tok.getLocation(); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 6395 | SourceRange Range; |
Richard Smith | e303e35 | 2018-02-02 22:24:54 +0000 | [diff] [blame] | 6396 | TrailingReturnType = |
| 6397 | ParseTrailingReturnType(Range, D.mayBeFollowedByCXXDirectInit()); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 6398 | EndLoc = Range.getEnd(); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6399 | } |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 6400 | } else if (standardAttributesAllowed()) { |
| 6401 | MaybeParseCXX11Attributes(FnAttrs); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6402 | } |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6403 | } |
| 6404 | |
Reid Kleckner | 078aea9 | 2016-12-09 17:14:05 +0000 | [diff] [blame] | 6405 | // Collect non-parameter declarations from the prototype if this is a function |
| 6406 | // declaration. They will be moved into the scope of the function. Only do |
| 6407 | // this in C and not C++, where the decls will continue to live in the |
| 6408 | // surrounding context. |
| 6409 | SmallVector<NamedDecl *, 0> DeclsInPrototype; |
| 6410 | if (getCurScope()->getFlags() & Scope::FunctionDeclarationScope && |
| 6411 | !getLangOpts().CPlusPlus) { |
| 6412 | for (Decl *D : getCurScope()->decls()) { |
| 6413 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 6414 | if (!ND || isa<ParmVarDecl>(ND)) |
| 6415 | continue; |
| 6416 | DeclsInPrototype.push_back(ND); |
| 6417 | } |
| 6418 | } |
| 6419 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6420 | // Remember that we parsed a function type, and remember the attributes. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6421 | D.AddTypeInfo(DeclaratorChunk::getFunction( |
| 6422 | HasProto, IsAmbiguous, LParenLoc, ParamInfo.data(), |
| 6423 | ParamInfo.size(), EllipsisLoc, RParenLoc, |
Anastasia Stulova | a9bc4bd | 2019-01-09 11:25:09 +0000 | [diff] [blame] | 6424 | RefQualifierIsLValueRef, RefQualifierLoc, |
| 6425 | /*MutableLoc=*/SourceLocation(), |
| 6426 | ESpecType, ESpecRange, DynamicExceptions.data(), |
| 6427 | DynamicExceptionRanges.data(), DynamicExceptions.size(), |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6428 | NoexceptExpr.isUsable() ? NoexceptExpr.get() : nullptr, |
| 6429 | ExceptionSpecTokens, DeclsInPrototype, StartLoc, |
Anastasia Stulova | a9bc4bd | 2019-01-09 11:25:09 +0000 | [diff] [blame] | 6430 | LocalEndLoc, D, TrailingReturnType, &DS), |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6431 | std::move(FnAttrs), EndLoc); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6432 | } |
| 6433 | |
Ehsan Akhgari | c07d1e2 | 2015-03-25 00:53:33 +0000 | [diff] [blame] | 6434 | /// ParseRefQualifier - Parses a member function ref-qualifier. Returns |
| 6435 | /// true if a ref-qualifier is found. |
| 6436 | bool Parser::ParseRefQualifier(bool &RefQualifierIsLValueRef, |
| 6437 | SourceLocation &RefQualifierLoc) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 6438 | if (Tok.isOneOf(tok::amp, tok::ampamp)) { |
Ehsan Akhgari | c07d1e2 | 2015-03-25 00:53:33 +0000 | [diff] [blame] | 6439 | Diag(Tok, getLangOpts().CPlusPlus11 ? |
| 6440 | diag::warn_cxx98_compat_ref_qualifier : |
| 6441 | diag::ext_ref_qualifier); |
| 6442 | |
| 6443 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 6444 | RefQualifierLoc = ConsumeToken(); |
| 6445 | return true; |
| 6446 | } |
| 6447 | return false; |
| 6448 | } |
| 6449 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6450 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 6451 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 6452 | /// |
| 6453 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 6454 | /// abstract-declarators. |
| 6455 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6456 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6457 | && Tok.is(tok::identifier) |
| 6458 | && !TryAltiVecVectorToken() |
| 6459 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 6460 | // 6.7.5.3p11. |
| 6461 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 6462 | // Identifier lists follow a really simple grammar: the identifiers can |
| 6463 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 6464 | // identifier lists are really rare in the brave new modern world, and |
| 6465 | // it is very common for someone to typo a type in a non-K&R style |
| 6466 | // list. If we are presented with something like: "void foo(intptr x, |
| 6467 | // float y)", we don't want to start parsing the function declarator as |
| 6468 | // though it is a K&R style declarator just because intptr is an |
| 6469 | // invalid type. |
| 6470 | // |
| 6471 | // To handle this, we check to see if the token after the first |
| 6472 | // identifier is a "," or ")". Only then do we parse it as an |
| 6473 | // identifier list. |
Bruno Cardoso Lopes | 218c874 | 2016-09-13 20:04:35 +0000 | [diff] [blame] | 6474 | && (!Tok.is(tok::eof) && |
| 6475 | (NextToken().is(tok::comma) || NextToken().is(tok::r_paren))); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6476 | } |
| 6477 | |
| 6478 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 6479 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 6480 | /// |
| 6481 | /// After returning, ParamInfo will hold the parsed parameters. |
| 6482 | /// |
| 6483 | /// identifier-list: [C99 6.7.5] |
| 6484 | /// identifier |
| 6485 | /// identifier-list ',' identifier |
| 6486 | /// |
| 6487 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 6488 | Declarator &D, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6489 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6490 | // If there was no identifier specified for the declarator, either we are in |
| 6491 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 6492 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 6493 | // diagnose this. |
| 6494 | if (!D.getIdentifier()) |
| 6495 | Diag(Tok, diag::ext_ident_list_in_param); |
| 6496 | |
| 6497 | // Maintain an efficient lookup of params we have seen so far. |
| 6498 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 6499 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6500 | do { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6501 | // If this isn't an identifier, report the error and skip until ')'. |
| 6502 | if (Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 6503 | Diag(Tok, diag::err_expected) << tok::identifier; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 6504 | SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6505 | // Forget we parsed anything. |
| 6506 | ParamInfo.clear(); |
| 6507 | return; |
| 6508 | } |
| 6509 | |
| 6510 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 6511 | |
| 6512 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 6513 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 6514 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 6515 | |
| 6516 | // Verify that the argument identifier has not already been mentioned. |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6517 | if (!ParamsSoFar.insert(ParmII).second) { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6518 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 6519 | } else { |
| 6520 | // Remember this identifier in ParamInfo. |
| 6521 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 6522 | Tok.getLocation(), |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6523 | nullptr)); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6524 | } |
| 6525 | |
| 6526 | // Eat the identifier. |
| 6527 | ConsumeToken(); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6528 | // The list continues if we see a comma. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6529 | } while (TryConsumeToken(tok::comma)); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6530 | } |
| 6531 | |
| 6532 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 6533 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 6534 | /// identifier list. |
| 6535 | /// |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 6536 | /// D is the declarator being parsed. If FirstArgAttrs is non-null, then the |
| 6537 | /// caller parsed those arguments immediately after the open paren - they should |
| 6538 | /// be considered to be part of the first parameter. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6539 | /// |
| 6540 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 6541 | /// be the location of the ellipsis, if any was parsed. |
| 6542 | /// |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 6543 | /// parameter-type-list: [C99 6.7.5] |
| 6544 | /// parameter-list |
| 6545 | /// parameter-list ',' '...' |
Douglas Gregor | 9bfc2e5 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 6546 | /// [C++] parameter-list '...' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 6547 | /// |
| 6548 | /// parameter-list: [C99 6.7.5] |
| 6549 | /// parameter-declaration |
| 6550 | /// parameter-list ',' parameter-declaration |
| 6551 | /// |
| 6552 | /// parameter-declaration: [C99 6.7.5] |
| 6553 | /// declaration-specifiers declarator |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 6554 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 6555 | /// [C++11] initializer-clause |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 6556 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 6557 | /// declaration-specifiers abstract-declarator[opt] |
| 6558 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 6559 | /// '=' assignment-expression |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 6560 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 6561 | /// [C++11] attribute-specifier-seq parameter-declaration |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 6562 | /// |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6563 | void Parser::ParseParameterDeclarationClause( |
| 6564 | Declarator &D, |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 6565 | ParsedAttributes &FirstArgAttrs, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6566 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6567 | SourceLocation &EllipsisLoc) { |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6568 | do { |
| 6569 | // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq |
| 6570 | // before deciding this was a parameter-declaration-clause. |
| 6571 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6572 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6573 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6574 | // Parse the declaration-specifiers. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 6575 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 6576 | DeclSpec DS(AttrFactory); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6577 | |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 6578 | // Parse any C++11 attributes. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 6579 | MaybeParseCXX11Attributes(DS.getAttributes()); |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 6580 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 6581 | // Skip any Microsoft attributes before a param. |
Chad Rosier | f8a2e70 | 2012-12-20 20:37:53 +0000 | [diff] [blame] | 6582 | MaybeParseMicrosoftAttributes(DS.getAttributes()); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 6583 | |
| 6584 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 6585 | |
| 6586 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 6587 | // Take them so that we only apply the attributes to the first parameter. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6588 | // FIXME: If we can leave the attributes in the token stream somehow, we can |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 6589 | // get rid of a parameter (FirstArgAttrs) and this statement. It might be |
| 6590 | // too much hassle. |
| 6591 | DS.takeAttributesFrom(FirstArgAttrs); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 6592 | |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 6593 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6594 | |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6595 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6596 | // Parse the declarator. This is "PrototypeContext" or |
| 6597 | // "LambdaExprParameterContext", because we must accept either |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6598 | // 'declarator' or 'abstract-declarator' here. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 6599 | Declarator ParmDeclarator( |
| 6600 | DS, D.getContext() == DeclaratorContext::LambdaExprContext |
| 6601 | ? DeclaratorContext::LambdaExprParameterContext |
| 6602 | : DeclaratorContext::PrototypeContext); |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6603 | ParseDeclarator(ParmDeclarator); |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6604 | |
| 6605 | // Parse GNU attributes, if present. |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6606 | MaybeParseGNUAttributes(ParmDeclarator); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6607 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6608 | // Remember this parsed parameter in ParamInfo. |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6609 | IdentifierInfo *ParmII = ParmDeclarator.getIdentifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6610 | |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6611 | // DefArgToks is used when the parsing of default arguments needs |
| 6612 | // to be delayed. |
Malcolm Parsons | ff0382c | 2016-11-17 17:52:58 +0000 | [diff] [blame] | 6613 | std::unique_ptr<CachedTokens> DefArgToks; |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6614 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6615 | // If no parameter was specified, verify that *something* was specified, |
| 6616 | // otherwise we have a missing type and identifier. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6617 | if (DS.isEmpty() && ParmDeclarator.getIdentifier() == nullptr && |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6618 | ParmDeclarator.getNumTypeObjects() == 0) { |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6619 | // Completely missing, emit error. |
| 6620 | Diag(DSStart, diag::err_missing_param); |
| 6621 | } else { |
| 6622 | // Otherwise, we have something. Add it and let semantic analysis try |
| 6623 | // to grok it and add the result to the ParamInfo we are building. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6624 | |
Richard Smith | 36ee9fb | 2014-08-11 23:30:23 +0000 | [diff] [blame] | 6625 | // Last chance to recover from a misplaced ellipsis in an attempted |
| 6626 | // parameter pack declaration. |
| 6627 | if (Tok.is(tok::ellipsis) && |
| 6628 | (NextToken().isNot(tok::r_paren) || |
| 6629 | (!ParmDeclarator.getEllipsisLoc().isValid() && |
| 6630 | !Actions.isUnexpandedParameterPackPermitted())) && |
| 6631 | Actions.containsUnexpandedParameterPacks(ParmDeclarator)) |
| 6632 | DiagnoseMisplacedEllipsisInDeclarator(ConsumeToken(), ParmDeclarator); |
| 6633 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6634 | // Inform the actions module about the parameter declarator, so it gets |
| 6635 | // added to the current scope. |
Richard Smith | 95e1fb0 | 2014-08-27 03:23:12 +0000 | [diff] [blame] | 6636 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator); |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 6637 | // Parse the default argument, if any. We parse the default |
| 6638 | // arguments in all dialects; the semantic analysis in |
| 6639 | // ActOnParamDefaultArgument will reject the default argument in |
| 6640 | // C. |
| 6641 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 6642 | SourceLocation EqualLoc = Tok.getLocation(); |
| 6643 | |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 6644 | // Parse the default argument |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 6645 | if (D.getContext() == DeclaratorContext::MemberContext) { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6646 | // If we're inside a class definition, cache the tokens |
| 6647 | // corresponding to the default argument. We'll actually parse |
| 6648 | // them when we see the end of the class definition. |
Malcolm Parsons | ff0382c | 2016-11-17 17:52:58 +0000 | [diff] [blame] | 6649 | DefArgToks.reset(new CachedTokens); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6650 | |
David Majnemer | 906ed27 | 2015-01-13 05:28:24 +0000 | [diff] [blame] | 6651 | SourceLocation ArgStartLoc = NextToken().getLocation(); |
Richard Smith | 1fff95c | 2013-09-12 23:28:08 +0000 | [diff] [blame] | 6652 | if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) { |
Malcolm Parsons | ff0382c | 2016-11-17 17:52:58 +0000 | [diff] [blame] | 6653 | DefArgToks.reset(); |
Serge Pavlov | b4b3578 | 2014-07-22 01:54:49 +0000 | [diff] [blame] | 6654 | Actions.ActOnParamDefaultArgumentError(Param, EqualLoc); |
Argyrios Kyrtzidis | 249179c | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 6655 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6656 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
David Majnemer | 906ed27 | 2015-01-13 05:28:24 +0000 | [diff] [blame] | 6657 | ArgStartLoc); |
Argyrios Kyrtzidis | 249179c | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 6658 | } |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 6659 | } else { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6660 | // Consume the '='. |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 6661 | ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6662 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 6663 | // The argument isn't actually potentially evaluated unless it is |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 6664 | // used. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 6665 | EnterExpressionEvaluationContext Eval( |
| 6666 | Actions, |
| 6667 | Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed, |
| 6668 | Param); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 6669 | |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 6670 | ExprResult DefArgResult; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6671 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
Sebastian Redl | 1678d5f | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 6672 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 6673 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 1678d5f | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 6674 | } else |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 6675 | DefArgResult = ParseAssignmentExpression(); |
Kaelyn Takata | b16e632 | 2014-11-20 22:06:40 +0000 | [diff] [blame] | 6676 | DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6677 | if (DefArgResult.isInvalid()) { |
Serge Pavlov | b4b3578 | 2014-07-22 01:54:49 +0000 | [diff] [blame] | 6678 | Actions.ActOnParamDefaultArgumentError(Param, EqualLoc); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 6679 | SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6680 | } else { |
| 6681 | // Inform the actions module about the default argument |
| 6682 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6683 | DefArgResult.get()); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6684 | } |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 6685 | } |
| 6686 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6687 | |
| 6688 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6689 | ParmDeclarator.getIdentifierLoc(), |
Malcolm Parsons | ff0382c | 2016-11-17 17:52:58 +0000 | [diff] [blame] | 6690 | Param, std::move(DefArgToks))); |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6691 | } |
| 6692 | |
Richard Smith | 36ee9fb | 2014-08-11 23:30:23 +0000 | [diff] [blame] | 6693 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) { |
| 6694 | if (!getLangOpts().CPlusPlus) { |
| 6695 | // We have ellipsis without a preceding ',', which is ill-formed |
| 6696 | // in C. Complain and provide the fix. |
| 6697 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
| 6698 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
| 6699 | } else if (ParmDeclarator.getEllipsisLoc().isValid() || |
| 6700 | Actions.containsUnexpandedParameterPacks(ParmDeclarator)) { |
| 6701 | // It looks like this was supposed to be a parameter pack. Warn and |
| 6702 | // point out where the ellipsis should have gone. |
| 6703 | SourceLocation ParmEllipsis = ParmDeclarator.getEllipsisLoc(); |
| 6704 | Diag(EllipsisLoc, diag::warn_misplaced_ellipsis_vararg) |
| 6705 | << ParmEllipsis.isValid() << ParmEllipsis; |
| 6706 | if (ParmEllipsis.isValid()) { |
| 6707 | Diag(ParmEllipsis, |
| 6708 | diag::note_misplaced_ellipsis_vararg_existing_ellipsis); |
| 6709 | } else { |
| 6710 | Diag(ParmDeclarator.getIdentifierLoc(), |
| 6711 | diag::note_misplaced_ellipsis_vararg_add_ellipsis) |
| 6712 | << FixItHint::CreateInsertion(ParmDeclarator.getIdentifierLoc(), |
| 6713 | "...") |
| 6714 | << !ParmDeclarator.hasName(); |
| 6715 | } |
| 6716 | Diag(EllipsisLoc, diag::note_misplaced_ellipsis_vararg_add_comma) |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6717 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Richard Smith | 36ee9fb | 2014-08-11 23:30:23 +0000 | [diff] [blame] | 6718 | } |
| 6719 | |
| 6720 | // We can't have any more parameters after an ellipsis. |
Douglas Gregor | 9bfc2e5 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 6721 | break; |
| 6722 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6723 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6724 | // If the next token is a comma, consume it and keep reading arguments. |
| 6725 | } while (TryConsumeToken(tok::comma)); |
Chris Lattner | 6c940e6 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 6726 | } |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 6727 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6728 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 6729 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 6730 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 6731 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 6732 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6733 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 6734 | /// attribute-specifier-seq[opt] |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6735 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6736 | if (CheckProhibitedCXX11Attribute()) |
| 6737 | return; |
| 6738 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6739 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 6740 | T.consumeOpen(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6741 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6742 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 6743 | // This code does a fast path to handle some of the most obvious cases. |
| 6744 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6745 | T.consumeClose(); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 6746 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 6747 | MaybeParseCXX11Attributes(attrs); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 6748 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6749 | // Remember that we parsed the empty array type. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6750 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, nullptr, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6751 | T.getOpenLocation(), |
| 6752 | T.getCloseLocation()), |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6753 | std::move(attrs), T.getCloseLocation()); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6754 | return; |
| 6755 | } else if (Tok.getKind() == tok::numeric_constant && |
| 6756 | GetLookAheadToken(1).is(tok::r_square)) { |
| 6757 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 6758 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6759 | ConsumeToken(); |
| 6760 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6761 | T.consumeClose(); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 6762 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 6763 | MaybeParseCXX11Attributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6764 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6765 | // Remember that we parsed a array type, and remember its features. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6766 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, ExprRes.get(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6767 | T.getOpenLocation(), |
| 6768 | T.getCloseLocation()), |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6769 | std::move(attrs), T.getCloseLocation()); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6770 | return; |
Benjamin Kramer | 72dae62 | 2016-02-18 15:30:24 +0000 | [diff] [blame] | 6771 | } else if (Tok.getKind() == tok::code_completion) { |
| 6772 | Actions.CodeCompleteBracketDeclarator(getCurScope()); |
| 6773 | return cutOffParsing(); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6774 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6775 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6776 | // If valid, this location is the position where we read the 'static' keyword. |
| 6777 | SourceLocation StaticLoc; |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6778 | TryConsumeToken(tok::kw_static, StaticLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6779 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6780 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 6781 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 6782 | DeclSpec DS(AttrFactory); |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 6783 | ParseTypeQualifierListOpt(DS, AR_CXX11AttributesParsed); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6784 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6785 | // If we haven't already read 'static', check to see if there is one after the |
| 6786 | // type-qualifier-list. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6787 | if (!StaticLoc.isValid()) |
| 6788 | TryConsumeToken(tok::kw_static, StaticLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6789 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6790 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6791 | bool isStar = false; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6792 | ExprResult NumElements; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6793 | |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 6794 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 6795 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
Sylvestre Ledru | 830885c | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 6796 | // the token after the star is a ']'. Since stars in arrays are |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 6797 | // infrequent, use of lookahead is not costly here. |
| 6798 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | c439f0d | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 6799 | ConsumeToken(); // Eat the '*'. |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 6800 | |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 6801 | if (StaticLoc.isValid()) { |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 6802 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 6803 | StaticLoc = SourceLocation(); // Drop the static. |
| 6804 | } |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 6805 | isStar = true; |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 6806 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6807 | // Note, in C89, this production uses the constant-expr production instead |
| 6808 | // of assignment-expr. The only difference is that assignment-expr allows |
| 6809 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 6810 | // are not i-c-e's, so we don't need to distinguish between the two here. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6811 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6812 | // Parse the constant-expression or assignment-expression now (depending |
| 6813 | // on dialect). |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6814 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6815 | NumElements = ParseConstantExpression(); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 6816 | } else { |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 6817 | EnterExpressionEvaluationContext Unevaluated( |
| 6818 | Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Kaelyn Takata | 1586782 | 2014-11-21 18:48:04 +0000 | [diff] [blame] | 6819 | NumElements = |
| 6820 | Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression()); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 6821 | } |
David Majnemer | f9834d5 | 2014-08-08 07:21:18 +0000 | [diff] [blame] | 6822 | } else { |
| 6823 | if (StaticLoc.isValid()) { |
| 6824 | Diag(StaticLoc, diag::err_unspecified_size_with_static); |
| 6825 | StaticLoc = SourceLocation(); // Drop the static. |
| 6826 | } |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 6827 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6828 | |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 6829 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 6830 | if (NumElements.isInvalid()) { |
Chris Lattner | cd2a8c5 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 6831 | D.setInvalidType(true); |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 6832 | // If the expression was invalid, skip it. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 6833 | SkipUntil(tok::r_square, StopAtSemi); |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 6834 | return; |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6835 | } |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 6836 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6837 | T.consumeClose(); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 6838 | |
Jordan Rose | 303e2f1 | 2016-11-10 23:28:17 +0000 | [diff] [blame] | 6839 | MaybeParseCXX11Attributes(DS.getAttributes()); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 6840 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6841 | // Remember that we parsed a array type, and remember its features. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6842 | D.AddTypeInfo( |
| 6843 | DeclaratorChunk::getArray(DS.getTypeQualifiers(), StaticLoc.isValid(), |
| 6844 | isStar, NumElements.get(), T.getOpenLocation(), |
| 6845 | T.getCloseLocation()), |
| 6846 | std::move(DS.getAttributes()), T.getCloseLocation()); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6847 | } |
| 6848 | |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 6849 | /// Diagnose brackets before an identifier. |
| 6850 | void Parser::ParseMisplacedBracketDeclarator(Declarator &D) { |
| 6851 | assert(Tok.is(tok::l_square) && "Missing opening bracket"); |
| 6852 | assert(!D.mayOmitIdentifier() && "Declarator cannot omit identifier"); |
| 6853 | |
| 6854 | SourceLocation StartBracketLoc = Tok.getLocation(); |
| 6855 | Declarator TempDeclarator(D.getDeclSpec(), D.getContext()); |
| 6856 | |
| 6857 | while (Tok.is(tok::l_square)) { |
| 6858 | ParseBracketDeclarator(TempDeclarator); |
| 6859 | } |
| 6860 | |
| 6861 | // Stuff the location of the start of the brackets into the Declarator. |
| 6862 | // The diagnostics from ParseDirectDeclarator will make more sense if |
| 6863 | // they use this location instead. |
| 6864 | if (Tok.is(tok::semi)) |
| 6865 | D.getName().EndLocation = StartBracketLoc; |
| 6866 | |
| 6867 | SourceLocation SuggestParenLoc = Tok.getLocation(); |
| 6868 | |
| 6869 | // Now that the brackets are removed, try parsing the declarator again. |
| 6870 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
| 6871 | |
| 6872 | // Something went wrong parsing the brackets, in which case, |
| 6873 | // ParseBracketDeclarator has emitted an error, and we don't need to emit |
| 6874 | // one here. |
| 6875 | if (TempDeclarator.getNumTypeObjects() == 0) |
| 6876 | return; |
| 6877 | |
| 6878 | // Determine if parens will need to be suggested in the diagnostic. |
| 6879 | bool NeedParens = false; |
| 6880 | if (D.getNumTypeObjects() != 0) { |
| 6881 | switch (D.getTypeObject(D.getNumTypeObjects() - 1).Kind) { |
| 6882 | case DeclaratorChunk::Pointer: |
| 6883 | case DeclaratorChunk::Reference: |
| 6884 | case DeclaratorChunk::BlockPointer: |
| 6885 | case DeclaratorChunk::MemberPointer: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 6886 | case DeclaratorChunk::Pipe: |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 6887 | NeedParens = true; |
| 6888 | break; |
| 6889 | case DeclaratorChunk::Array: |
| 6890 | case DeclaratorChunk::Function: |
| 6891 | case DeclaratorChunk::Paren: |
| 6892 | break; |
| 6893 | } |
| 6894 | } |
| 6895 | |
| 6896 | if (NeedParens) { |
| 6897 | // Create a DeclaratorChunk for the inserted parens. |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 6898 | SourceLocation EndLoc = PP.getLocForEndOfToken(D.getEndLoc()); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6899 | D.AddTypeInfo(DeclaratorChunk::getParen(SuggestParenLoc, EndLoc), |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 6900 | SourceLocation()); |
| 6901 | } |
| 6902 | |
| 6903 | // Adding back the bracket info to the end of the Declarator. |
| 6904 | for (unsigned i = 0, e = TempDeclarator.getNumTypeObjects(); i < e; ++i) { |
| 6905 | const DeclaratorChunk &Chunk = TempDeclarator.getTypeObject(i); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6906 | D.AddTypeInfo(Chunk, SourceLocation()); |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 6907 | } |
| 6908 | |
| 6909 | // The missing identifier would have been diagnosed in ParseDirectDeclarator. |
| 6910 | // If parentheses are required, always suggest them. |
| 6911 | if (!D.getIdentifier() && !NeedParens) |
| 6912 | return; |
| 6913 | |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 6914 | SourceLocation EndBracketLoc = TempDeclarator.getEndLoc(); |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 6915 | |
| 6916 | // Generate the move bracket error message. |
| 6917 | SourceRange BracketRange(StartBracketLoc, EndBracketLoc); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 6918 | SourceLocation EndLoc = PP.getLocForEndOfToken(D.getEndLoc()); |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 6919 | |
| 6920 | if (NeedParens) { |
| 6921 | Diag(EndLoc, diag::err_brackets_go_after_unqualified_id) |
| 6922 | << getLangOpts().CPlusPlus |
| 6923 | << FixItHint::CreateInsertion(SuggestParenLoc, "(") |
| 6924 | << FixItHint::CreateInsertion(EndLoc, ")") |
| 6925 | << FixItHint::CreateInsertionFromRange( |
| 6926 | EndLoc, CharSourceRange(BracketRange, true)) |
| 6927 | << FixItHint::CreateRemoval(BracketRange); |
| 6928 | } else { |
| 6929 | Diag(EndLoc, diag::err_brackets_go_after_unqualified_id) |
| 6930 | << getLangOpts().CPlusPlus |
| 6931 | << FixItHint::CreateInsertionFromRange( |
| 6932 | EndLoc, CharSourceRange(BracketRange, true)) |
| 6933 | << FixItHint::CreateRemoval(BracketRange); |
| 6934 | } |
| 6935 | } |
| 6936 | |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 6937 | /// [GNU] typeof-specifier: |
| 6938 | /// typeof ( expressions ) |
| 6939 | /// typeof ( type-name ) |
| 6940 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 6941 | /// |
| 6942 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 6943 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6944 | Token OpTok = Tok; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 6945 | SourceLocation StartLoc = ConsumeToken(); |
| 6946 | |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 6947 | const bool hasParens = Tok.is(tok::l_paren); |
| 6948 | |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 6949 | EnterExpressionEvaluationContext Unevaluated( |
| 6950 | Actions, Sema::ExpressionEvaluationContext::Unevaluated, |
| 6951 | Sema::ReuseLambdaContextDecl); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 6952 | |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6953 | bool isCastExpr; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6954 | ParsedType CastTy; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6955 | SourceRange CastRange; |
Kaelyn Takata | 91126074 | 2014-12-19 01:28:40 +0000 | [diff] [blame] | 6956 | ExprResult Operand = Actions.CorrectDelayedTyposInExpr( |
| 6957 | ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, CastTy, CastRange)); |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 6958 | if (hasParens) |
| 6959 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6960 | |
| 6961 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 6962 | // FIXME: Not accurate, the range gets one token more than it should. |
| 6963 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6964 | else |
| 6965 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6966 | |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6967 | if (isCastExpr) { |
| 6968 | if (!CastTy) { |
| 6969 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 6970 | return; |
Douglas Gregor | 220cac5 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 6971 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 6972 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6973 | const char *PrevSpec = nullptr; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 6974 | unsigned DiagID; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6975 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 6976 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6977 | DiagID, CastTy, |
| 6978 | Actions.getASTContext().getPrintingPolicy())) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 6979 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6980 | return; |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 6981 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 6982 | |
Hiroshi Inoue | 533777f | 2017-07-02 06:12:49 +0000 | [diff] [blame] | 6983 | // If we get here, the operand to the typeof was an expression. |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 6984 | if (Operand.isInvalid()) { |
| 6985 | DS.SetTypeSpecError(); |
Steve Naroff | 4bd2f71 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 6986 | return; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 6987 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 6988 | |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 6989 | // We might need to transform the operand if it is potentially evaluated. |
| 6990 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 6991 | if (Operand.isInvalid()) { |
| 6992 | DS.SetTypeSpecError(); |
| 6993 | return; |
| 6994 | } |
| 6995 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6996 | const char *PrevSpec = nullptr; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 6997 | unsigned DiagID; |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 6998 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 6999 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 7000 | DiagID, Operand.get(), |
| 7001 | Actions.getASTContext().getPrintingPolicy())) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 7002 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 7003 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 7004 | |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 7005 | /// [C11] atomic-specifier: |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 7006 | /// _Atomic ( type-name ) |
| 7007 | /// |
| 7008 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 7009 | assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) && |
| 7010 | "Not an atomic specifier"); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 7011 | |
| 7012 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 7013 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 7014 | if (T.consumeOpen()) |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 7015 | return; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 7016 | |
| 7017 | TypeResult Result = ParseTypeName(); |
| 7018 | if (Result.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 7019 | SkipUntil(tok::r_paren, StopAtSemi); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 7020 | return; |
| 7021 | } |
| 7022 | |
| 7023 | // Match the ')' |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 7024 | T.consumeClose(); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 7025 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 7026 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 7027 | return; |
| 7028 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 7029 | DS.setTypeofParensRange(T.getRange()); |
| 7030 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 7031 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 7032 | const char *PrevSpec = nullptr; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 7033 | unsigned DiagID; |
| 7034 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7035 | DiagID, Result.get(), |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 7036 | Actions.getASTContext().getPrintingPolicy())) |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 7037 | Diag(StartLoc, DiagID) << PrevSpec; |
| 7038 | } |
| 7039 | |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 7040 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 7041 | /// from TryAltiVecVectorToken. |
| 7042 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 7043 | Token Next = NextToken(); |
| 7044 | switch (Next.getKind()) { |
| 7045 | default: return false; |
| 7046 | case tok::kw_short: |
| 7047 | case tok::kw_long: |
| 7048 | case tok::kw_signed: |
| 7049 | case tok::kw_unsigned: |
| 7050 | case tok::kw_void: |
| 7051 | case tok::kw_char: |
| 7052 | case tok::kw_int: |
| 7053 | case tok::kw_float: |
| 7054 | case tok::kw_double: |
| 7055 | case tok::kw_bool: |
Bill Seurer | cf2c96b | 2015-01-12 19:35:51 +0000 | [diff] [blame] | 7056 | case tok::kw___bool: |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 7057 | case tok::kw___pixel: |
| 7058 | Tok.setKind(tok::kw___vector); |
| 7059 | return true; |
| 7060 | case tok::identifier: |
| 7061 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 7062 | Tok.setKind(tok::kw___vector); |
| 7063 | return true; |
| 7064 | } |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 7065 | if (Next.getIdentifierInfo() == Ident_bool) { |
| 7066 | Tok.setKind(tok::kw___vector); |
| 7067 | return true; |
| 7068 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 7069 | return false; |
| 7070 | } |
| 7071 | } |
| 7072 | |
| 7073 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 7074 | const char *&PrevSpec, unsigned &DiagID, |
| 7075 | bool &isInvalid) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 7076 | const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy(); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 7077 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 7078 | Token Next = NextToken(); |
| 7079 | switch (Next.getKind()) { |
| 7080 | case tok::kw_short: |
| 7081 | case tok::kw_long: |
| 7082 | case tok::kw_signed: |
| 7083 | case tok::kw_unsigned: |
| 7084 | case tok::kw_void: |
| 7085 | case tok::kw_char: |
| 7086 | case tok::kw_int: |
| 7087 | case tok::kw_float: |
| 7088 | case tok::kw_double: |
| 7089 | case tok::kw_bool: |
Bill Seurer | cf2c96b | 2015-01-12 19:35:51 +0000 | [diff] [blame] | 7090 | case tok::kw___bool: |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 7091 | case tok::kw___pixel: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 7092 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 7093 | return true; |
| 7094 | case tok::identifier: |
| 7095 | if (Next.getIdentifierInfo() == Ident_pixel) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 7096 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 7097 | return true; |
| 7098 | } |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 7099 | if (Next.getIdentifierInfo() == Ident_bool) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 7100 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy); |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 7101 | return true; |
| 7102 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 7103 | break; |
| 7104 | default: |
| 7105 | break; |
| 7106 | } |
Douglas Gregor | 9938e3b | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 7107 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 7108 | DS.isTypeAltiVecVector()) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 7109 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 7110 | return true; |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 7111 | } else if ((Tok.getIdentifierInfo() == Ident_bool) && |
| 7112 | DS.isTypeAltiVecVector()) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 7113 | isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy); |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 7114 | return true; |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 7115 | } |
| 7116 | return false; |
| 7117 | } |