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 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Declaration portions of the Parser interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Parse/Parser.h" |
Vassil Vassilev | 11ad339 | 2017-03-23 15:11:07 +0000 | [diff] [blame] | 15 | #include "clang/Parse/RAIIObjectsForParser.h" |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Chandler Carruth | 757fcd6 | 2014-03-04 10:05:20 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclTemplate.h" |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 18 | #include "clang/AST/PrettyDeclStackTrace.h" |
Benjamin Kramer | d7d2b1f | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 19 | #include "clang/Basic/AddressSpaces.h" |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Attributes.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 21 | #include "clang/Basic/CharInfo.h" |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "clang/Parse/ParseDiagnostic.h" |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 24 | #include "clang/Sema/Lookup.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 25 | #include "clang/Sema/ParsedTemplate.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 26 | #include "clang/Sema/Scope.h" |
Craig Topper | abb83ae | 2015-11-14 19:31:56 +0000 | [diff] [blame] | 27 | #include "clang/Sema/SemaDiagnostic.h" |
George Burgess IV | a19ea34 | 2016-11-10 20:43:52 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/Optional.h" |
Chris Lattner | ad9ac94 | 2007-01-23 01:14:52 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallSet.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallString.h" |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/StringSwitch.h" |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 32 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 33 | using namespace clang; |
| 34 | |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | // C99 6.7: Declarations. |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 39 | /// ParseTypeName |
| 40 | /// type-name: [C99 6.7.6] |
| 41 | /// specifier-qualifier-list abstract-declarator[opt] |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 42 | /// |
| 43 | /// Called type-id in C++. |
Douglas Gregor | 205d5e3 | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 44 | TypeResult Parser::ParseTypeName(SourceRange *Range, |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 45 | DeclaratorContext Context, |
Richard Smith | cd1c055 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 46 | AccessSpecifier AS, |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 47 | Decl **OwnedType, |
| 48 | ParsedAttributes *Attrs) { |
Richard Smith | 62dad82 | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 49 | DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context); |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 50 | if (DSC == DeclSpecContext::DSC_normal) |
| 51 | DSC = DeclSpecContext::DSC_type_specifier; |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 52 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 53 | // Parse the common declaration-specifiers piece. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 54 | DeclSpec DS(AttrFactory); |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 55 | if (Attrs) |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 56 | DS.addAttributes(*Attrs); |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 57 | ParseSpecifierQualifierList(DS, AS, DSC); |
Richard Smith | cd1c055 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 58 | if (OwnedType) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 59 | *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : nullptr; |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 60 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 61 | // Parse the abstract-declarator, if present. |
Douglas Gregor | 205d5e3 | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 62 | Declarator DeclaratorInfo(DS, Context); |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 63 | ParseDeclarator(DeclaratorInfo); |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 64 | if (Range) |
| 65 | *Range = DeclaratorInfo.getSourceRange(); |
| 66 | |
Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 67 | if (DeclaratorInfo.isInvalidType()) |
Douglas Gregor | 220cac5 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 68 | return true; |
| 69 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 70 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 73 | /// Normalizes an attribute name by dropping prefixed and suffixed __. |
George Burgess IV | 779af82 | 2017-06-30 22:33:24 +0000 | [diff] [blame] | 74 | static StringRef normalizeAttrName(StringRef Name) { |
| 75 | if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__")) |
| 76 | return Name.drop_front(2).drop_back(2); |
| 77 | return Name; |
| 78 | } |
| 79 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 80 | /// isAttributeLateParsed - Return true if the attribute has arguments that |
| 81 | /// require late parsing. |
| 82 | static bool isAttributeLateParsed(const IdentifierInfo &II) { |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 83 | #define CLANG_ATTR_LATE_PARSED_LIST |
George Burgess IV | 779af82 | 2017-06-30 22:33:24 +0000 | [diff] [blame] | 84 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 85 | #include "clang/Parse/AttrParserStringSwitches.inc" |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 86 | .Default(false); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 87 | #undef CLANG_ATTR_LATE_PARSED_LIST |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 90 | /// ParseGNUAttributes - Parse a non-empty attributes list. |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 91 | /// |
| 92 | /// [GNU] attributes: |
| 93 | /// attribute |
| 94 | /// attributes attribute |
| 95 | /// |
| 96 | /// [GNU] attribute: |
| 97 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 98 | /// |
| 99 | /// [GNU] attribute-list: |
| 100 | /// attrib |
| 101 | /// attribute_list ',' attrib |
| 102 | /// |
| 103 | /// [GNU] attrib: |
| 104 | /// empty |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 105 | /// attrib-name |
| 106 | /// attrib-name '(' identifier ')' |
| 107 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 108 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 109 | /// |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 110 | /// [GNU] attrib-name: |
| 111 | /// identifier |
| 112 | /// typespec |
| 113 | /// typequal |
| 114 | /// storageclass |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | /// |
Richard Smith | f7ca0c0 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 116 | /// Whether an attribute takes an 'identifier' is determined by the |
| 117 | /// attrib-name. GCC's behavior here is not worth imitating: |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 118 | /// |
Richard Smith | f7ca0c0 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 119 | /// * In C mode, if the attribute argument list starts with an identifier |
| 120 | /// followed by a ',' or an ')', and the identifier doesn't resolve to |
| 121 | /// a type, it is parsed as an identifier. If the attribute actually |
| 122 | /// wanted an expression, it's out of luck (but it turns out that no |
| 123 | /// attributes work that way, because C constant expressions are very |
| 124 | /// limited). |
| 125 | /// * In C++ mode, if the attribute argument list starts with an identifier, |
| 126 | /// and the attribute *wants* an identifier, it is parsed as an identifier. |
| 127 | /// At block scope, any additional tokens between the identifier and the |
| 128 | /// ',' or ')' are ignored, otherwise they produce a parse error. |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 129 | /// |
Richard Smith | f7ca0c0 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 130 | /// 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] | 131 | void Parser::ParseGNUAttributes(ParsedAttributes &attrs, |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 132 | SourceLocation *endLoc, |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 133 | LateParsedAttrList *LateAttrs, |
| 134 | Declarator *D) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 135 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 137 | while (Tok.is(tok::kw___attribute)) { |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 138 | ConsumeToken(); |
| 139 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 140 | "attribute")) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 141 | SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ; |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 142 | return; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 143 | } |
| 144 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 145 | SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ; |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 146 | return; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 147 | } |
| 148 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 149 | while (true) { |
| 150 | // Allow empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 151 | if (TryConsumeToken(tok::comma)) |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 152 | continue; |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 153 | |
| 154 | // Expect an identifier or declaration specifier (const, int, etc.) |
David Majnemer | 22fe771 | 2015-01-03 19:41:00 +0000 | [diff] [blame] | 155 | if (Tok.isAnnotation()) |
| 156 | break; |
| 157 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 158 | if (!AttrName) |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 159 | break; |
| 160 | |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 161 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 163 | if (Tok.isNot(tok::l_paren)) { |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 164 | attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 165 | ParsedAttr::AS_GNU); |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 166 | continue; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 167 | } |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 168 | |
| 169 | // Handle "parameterized" attributes |
| 170 | if (!LateAttrs || !isAttributeLateParsed(*AttrName)) { |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 171 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc, nullptr, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 172 | SourceLocation(), ParsedAttr::AS_GNU, D); |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 173 | continue; |
| 174 | } |
| 175 | |
| 176 | // Handle attributes with arguments that require late parsing. |
| 177 | LateParsedAttribute *LA = |
| 178 | new LateParsedAttribute(this, *AttrName, AttrNameLoc); |
| 179 | LateAttrs->push_back(LA); |
| 180 | |
| 181 | // Attributes in a class are parsed at the end of the class, along |
| 182 | // with other late-parsed declarations. |
| 183 | if (!ClassStack.empty() && !LateAttrs->parseSoon()) |
| 184 | getCurrentClass().LateParsedDeclarations.push_back(LA); |
| 185 | |
George Burgess IV | c8b9537 | 2017-01-04 22:43:01 +0000 | [diff] [blame] | 186 | // Be sure ConsumeAndStoreUntil doesn't see the start l_paren, since it |
| 187 | // recursively consumes balanced parens. |
| 188 | LA->Toks.push_back(Tok); |
| 189 | ConsumeParen(); |
| 190 | // Consume everything up to and including the matching right parens. |
| 191 | ConsumeAndStoreUntil(tok::r_paren, LA->Toks, /*StopAtSemi=*/true); |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 192 | |
| 193 | Token Eof; |
| 194 | Eof.startToken(); |
| 195 | Eof.setLocation(Tok.getLocation()); |
| 196 | LA->Toks.push_back(Eof); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 197 | } |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 198 | |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 199 | if (ExpectAndConsume(tok::r_paren)) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 200 | SkipUntil(tok::r_paren, StopAtSemi); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 201 | SourceLocation Loc = Tok.getLocation(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 202 | if (ExpectAndConsume(tok::r_paren)) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 203 | SkipUntil(tok::r_paren, StopAtSemi); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 204 | if (endLoc) |
| 205 | *endLoc = Loc; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 206 | } |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 207 | } |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 208 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 209 | /// Determine whether the given attribute has an identifier argument. |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 210 | static bool attributeHasIdentifierArg(const IdentifierInfo &II) { |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 211 | #define CLANG_ATTR_IDENTIFIER_ARG_LIST |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 212 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 213 | #include "clang/Parse/AttrParserStringSwitches.inc" |
Douglas Gregor | d2472d4 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 214 | .Default(false); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 215 | #undef CLANG_ATTR_IDENTIFIER_ARG_LIST |
Douglas Gregor | d2472d4 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 216 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 217 | |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 218 | /// Determine whether the given attribute has a variadic identifier argument. |
| 219 | static bool attributeHasVariadicIdentifierArg(const IdentifierInfo &II) { |
| 220 | #define CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST |
| 221 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
| 222 | #include "clang/Parse/AttrParserStringSwitches.inc" |
| 223 | .Default(false); |
| 224 | #undef CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST |
| 225 | } |
| 226 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 227 | /// Determine whether the given attribute parses a type argument. |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 228 | static bool attributeIsTypeArgAttr(const IdentifierInfo &II) { |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 229 | #define CLANG_ATTR_TYPE_ARG_LIST |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 230 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 231 | #include "clang/Parse/AttrParserStringSwitches.inc" |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 232 | .Default(false); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 233 | #undef CLANG_ATTR_TYPE_ARG_LIST |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 236 | /// Determine whether the given attribute requires parsing its arguments |
Aaron Ballman | 15b27b9 | 2014-01-09 19:39:35 +0000 | [diff] [blame] | 237 | /// in an unevaluated context or not. |
| 238 | static bool attributeParsedArgsUnevaluated(const IdentifierInfo &II) { |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 239 | #define CLANG_ATTR_ARG_CONTEXT_LIST |
Aaron Ballman | 15b27b9 | 2014-01-09 19:39:35 +0000 | [diff] [blame] | 240 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 241 | #include "clang/Parse/AttrParserStringSwitches.inc" |
Aaron Ballman | 15b27b9 | 2014-01-09 19:39:35 +0000 | [diff] [blame] | 242 | .Default(false); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 243 | #undef CLANG_ATTR_ARG_CONTEXT_LIST |
Aaron Ballman | 15b27b9 | 2014-01-09 19:39:35 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 246 | IdentifierLoc *Parser::ParseIdentifierLoc() { |
| 247 | assert(Tok.is(tok::identifier) && "expected an identifier"); |
| 248 | IdentifierLoc *IL = IdentifierLoc::create(Actions.Context, |
| 249 | Tok.getLocation(), |
| 250 | Tok.getIdentifierInfo()); |
| 251 | ConsumeToken(); |
| 252 | return IL; |
| 253 | } |
| 254 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 255 | void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName, |
| 256 | SourceLocation AttrNameLoc, |
| 257 | ParsedAttributes &Attrs, |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 258 | SourceLocation *EndLoc, |
| 259 | IdentifierInfo *ScopeName, |
| 260 | SourceLocation ScopeLoc, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 261 | ParsedAttr::Syntax Syntax) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 262 | BalancedDelimiterTracker Parens(*this, tok::l_paren); |
| 263 | Parens.consumeOpen(); |
| 264 | |
| 265 | TypeResult T; |
| 266 | if (Tok.isNot(tok::r_paren)) |
| 267 | T = ParseTypeName(); |
| 268 | |
| 269 | if (Parens.consumeClose()) |
| 270 | return; |
| 271 | |
| 272 | if (T.isInvalid()) |
| 273 | return; |
| 274 | |
| 275 | if (T.isUsable()) |
| 276 | Attrs.addNewTypeAttr(&AttrName, |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 277 | SourceRange(AttrNameLoc, Parens.getCloseLocation()), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 278 | ScopeName, ScopeLoc, T.get(), Syntax); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 279 | else |
| 280 | Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 281 | ScopeName, ScopeLoc, nullptr, 0, Syntax); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Aaron Ballman | 35f9421 | 2014-04-14 16:03:22 +0000 | [diff] [blame] | 284 | unsigned Parser::ParseAttributeArgsCommon( |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 285 | IdentifierInfo *AttrName, SourceLocation AttrNameLoc, |
| 286 | ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 287 | SourceLocation ScopeLoc, ParsedAttr::Syntax Syntax) { |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 288 | // Ignore the left paren location for now. |
| 289 | ConsumeParen(); |
| 290 | |
| 291 | ArgsVector ArgExprs; |
| 292 | if (Tok.is(tok::identifier)) { |
| 293 | // If this attribute wants an 'identifier' argument, make it so. |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 294 | bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName) || |
| 295 | attributeHasVariadicIdentifierArg(*AttrName); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 296 | ParsedAttr::Kind AttrKind = |
| 297 | ParsedAttr::getKind(AttrName, ScopeName, Syntax); |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 298 | |
| 299 | // If we don't know how to parse this attribute, but this is the only |
| 300 | // token in this argument, assume it's meant to be an identifier. |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 301 | if (AttrKind == ParsedAttr::UnknownAttribute || |
| 302 | AttrKind == ParsedAttr::IgnoredAttribute) { |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 303 | const Token &Next = NextToken(); |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 304 | IsIdentifierArg = Next.isOneOf(tok::r_paren, tok::comma); |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | if (IsIdentifierArg) |
| 308 | ArgExprs.push_back(ParseIdentifierLoc()); |
| 309 | } |
| 310 | |
| 311 | if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) { |
| 312 | // Eat the comma. |
| 313 | if (!ArgExprs.empty()) |
| 314 | ConsumeToken(); |
| 315 | |
| 316 | // Parse the non-empty comma-separated list of expressions. |
| 317 | do { |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 318 | ExprResult ArgExpr; |
| 319 | if (Tok.is(tok::identifier) && |
| 320 | attributeHasVariadicIdentifierArg(*AttrName)) { |
| 321 | ArgExprs.push_back(ParseIdentifierLoc()); |
| 322 | } else { |
| 323 | bool Uneval = attributeParsedArgsUnevaluated(*AttrName); |
| 324 | EnterExpressionEvaluationContext Unevaluated( |
| 325 | Actions, |
| 326 | Uneval ? Sema::ExpressionEvaluationContext::Unevaluated |
| 327 | : Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 328 | |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 329 | ExprResult ArgExpr( |
| 330 | Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression())); |
| 331 | if (ArgExpr.isInvalid()) { |
| 332 | SkipUntil(tok::r_paren, StopAtSemi); |
| 333 | return 0; |
| 334 | } |
| 335 | ArgExprs.push_back(ArgExpr.get()); |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 336 | } |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 337 | // Eat the comma, move to the next argument |
| 338 | } while (TryConsumeToken(tok::comma)); |
| 339 | } |
| 340 | |
| 341 | SourceLocation RParen = Tok.getLocation(); |
| 342 | if (!ExpectAndConsume(tok::r_paren)) { |
| 343 | SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc; |
| 344 | Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc, |
| 345 | ArgExprs.data(), ArgExprs.size(), Syntax); |
| 346 | } |
| 347 | |
| 348 | if (EndLoc) |
| 349 | *EndLoc = RParen; |
Aaron Ballman | 35f9421 | 2014-04-14 16:03:22 +0000 | [diff] [blame] | 350 | |
| 351 | return static_cast<unsigned>(ArgExprs.size()); |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 354 | /// Parse the arguments to a parameterized GNU attribute or |
| 355 | /// a C++11 attribute in "gnu" namespace. |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 356 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 357 | SourceLocation AttrNameLoc, |
| 358 | ParsedAttributes &Attrs, |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 359 | SourceLocation *EndLoc, |
| 360 | IdentifierInfo *ScopeName, |
| 361 | SourceLocation ScopeLoc, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 362 | ParsedAttr::Syntax Syntax, |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 363 | Declarator *D) { |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 364 | |
| 365 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 366 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 367 | ParsedAttr::Kind AttrKind = |
| 368 | ParsedAttr::getKind(AttrName, ScopeName, Syntax); |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 369 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 370 | if (AttrKind == ParsedAttr::AT_Availability) { |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 371 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName, |
| 372 | ScopeLoc, Syntax); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 373 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 374 | } else if (AttrKind == ParsedAttr::AT_ExternalSourceSymbol) { |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 375 | ParseExternalSourceSymbolAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, |
| 376 | ScopeName, ScopeLoc, Syntax); |
| 377 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 378 | } else if (AttrKind == ParsedAttr::AT_ObjCBridgeRelated) { |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 379 | ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, |
| 380 | ScopeName, ScopeLoc, Syntax); |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 381 | return; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 382 | } else if (AttrKind == ParsedAttr::AT_TypeTagForDatatype) { |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 383 | ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, |
| 384 | ScopeName, ScopeLoc, Syntax); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 385 | return; |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 386 | } else if (attributeIsTypeArgAttr(*AttrName)) { |
| 387 | ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName, |
| 388 | ScopeLoc, Syntax); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 389 | return; |
| 390 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 391 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 392 | // These may refer to the function arguments, but need to be parsed early to |
| 393 | // participate in determining whether it's a redeclaration. |
George Burgess IV | a19ea34 | 2016-11-10 20:43:52 +0000 | [diff] [blame] | 394 | llvm::Optional<ParseScope> PrototypeScope; |
Ulrich Weigand | ef5aa29 | 2015-07-13 14:13:01 +0000 | [diff] [blame] | 395 | if (normalizeAttrName(AttrName->getName()) == "enable_if" && |
| 396 | D && D->isFunctionDeclarator()) { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 397 | DeclaratorChunk::FunctionTypeInfo FTI = D->getFunctionTypeInfo(); |
George Burgess IV | a19ea34 | 2016-11-10 20:43:52 +0000 | [diff] [blame] | 398 | PrototypeScope.emplace(this, Scope::FunctionPrototypeScope | |
| 399 | Scope::FunctionDeclarationScope | |
| 400 | Scope::DeclScope); |
Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 401 | for (unsigned i = 0; i != FTI.NumParams; ++i) { |
| 402 | ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 403 | Actions.ActOnReenterCXXMethodParameter(getCurScope(), Param); |
| 404 | } |
| 405 | } |
| 406 | |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 407 | ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName, |
| 408 | ScopeLoc, Syntax); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 411 | unsigned Parser::ParseClangAttributeArgs( |
| 412 | IdentifierInfo *AttrName, SourceLocation AttrNameLoc, |
| 413 | ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 414 | SourceLocation ScopeLoc, ParsedAttr::Syntax Syntax) { |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 415 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 416 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 417 | ParsedAttr::Kind AttrKind = |
| 418 | ParsedAttr::getKind(AttrName, ScopeName, Syntax); |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 419 | |
Aaron Ballman | 38bbc16 | 2018-02-24 17:16:42 +0000 | [diff] [blame] | 420 | switch (AttrKind) { |
| 421 | default: |
| 422 | return ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, |
| 423 | ScopeName, ScopeLoc, Syntax); |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 424 | case ParsedAttr::AT_ExternalSourceSymbol: |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 425 | ParseExternalSourceSymbolAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, |
| 426 | ScopeName, ScopeLoc, Syntax); |
Aaron Ballman | 38bbc16 | 2018-02-24 17:16:42 +0000 | [diff] [blame] | 427 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 428 | case ParsedAttr::AT_Availability: |
Aaron Ballman | 38bbc16 | 2018-02-24 17:16:42 +0000 | [diff] [blame] | 429 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName, |
| 430 | ScopeLoc, Syntax); |
| 431 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 432 | case ParsedAttr::AT_ObjCBridgeRelated: |
Aaron Ballman | c248b0f | 2018-02-24 17:37:37 +0000 | [diff] [blame] | 433 | ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, |
| 434 | ScopeName, ScopeLoc, Syntax); |
| 435 | break; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 436 | case ParsedAttr::AT_TypeTagForDatatype: |
Aaron Ballman | a26d8ee | 2018-02-25 14:01:04 +0000 | [diff] [blame] | 437 | ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, |
| 438 | ScopeName, ScopeLoc, Syntax); |
| 439 | break; |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 440 | } |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 441 | return !Attrs.empty() ? Attrs.begin()->getNumArgs() : 0; |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 444 | bool Parser::ParseMicrosoftDeclSpecArgs(IdentifierInfo *AttrName, |
| 445 | SourceLocation AttrNameLoc, |
| 446 | ParsedAttributes &Attrs) { |
| 447 | // If the attribute isn't known, we will not attempt to parse any |
| 448 | // arguments. |
| 449 | if (!hasAttribute(AttrSyntax::Declspec, nullptr, AttrName, |
Bob Wilson | 7c73083 | 2015-07-20 22:57:31 +0000 | [diff] [blame] | 450 | getTargetInfo(), getLangOpts())) { |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 451 | // Eat the left paren, then skip to the ending right paren. |
| 452 | ConsumeParen(); |
| 453 | SkipUntil(tok::r_paren); |
| 454 | return false; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 455 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 456 | |
Aaron Ballman | 95d5703 | 2014-04-14 16:44:26 +0000 | [diff] [blame] | 457 | SourceLocation OpenParenLoc = Tok.getLocation(); |
| 458 | |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 459 | if (AttrName->getName() == "property") { |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 460 | // 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] | 461 | // assignment expressions as a parameter, but the lhs of the assignment |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 462 | // must be named get or put. |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 463 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 464 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 465 | T.expectAndConsume(diag::err_expected_lparen_after, |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 466 | AttrName->getNameStart(), tok::r_paren); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 467 | |
| 468 | enum AccessorKind { |
| 469 | AK_Invalid = -1, |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 470 | AK_Put = 0, |
| 471 | AK_Get = 1 // indices into AccessorNames |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 472 | }; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 473 | IdentifierInfo *AccessorNames[] = {nullptr, nullptr}; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 474 | bool HasInvalidAccessor = false; |
| 475 | |
| 476 | // Parse the accessor specifications. |
| 477 | while (true) { |
| 478 | // Stop if this doesn't look like an accessor spec. |
| 479 | if (!Tok.is(tok::identifier)) { |
| 480 | // If the user wrote a completely empty list, use a special diagnostic. |
| 481 | if (Tok.is(tok::r_paren) && !HasInvalidAccessor && |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 482 | AccessorNames[AK_Put] == nullptr && |
| 483 | AccessorNames[AK_Get] == nullptr) { |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 484 | Diag(AttrNameLoc, diag::err_ms_property_no_getter_or_putter); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 485 | break; |
| 486 | } |
| 487 | |
| 488 | Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor); |
| 489 | break; |
| 490 | } |
| 491 | |
| 492 | AccessorKind Kind; |
| 493 | SourceLocation KindLoc = Tok.getLocation(); |
| 494 | StringRef KindStr = Tok.getIdentifierInfo()->getName(); |
| 495 | if (KindStr == "get") { |
| 496 | Kind = AK_Get; |
| 497 | } else if (KindStr == "put") { |
| 498 | Kind = AK_Put; |
| 499 | |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 500 | // Recover from the common mistake of using 'set' instead of 'put'. |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 501 | } else if (KindStr == "set") { |
| 502 | Diag(KindLoc, diag::err_ms_property_has_set_accessor) |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 503 | << FixItHint::CreateReplacement(KindLoc, "put"); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 504 | Kind = AK_Put; |
| 505 | |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 506 | // Handle the mistake of forgetting the accessor kind by skipping |
| 507 | // this accessor. |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 508 | } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) { |
| 509 | Diag(KindLoc, diag::err_ms_property_missing_accessor_kind); |
| 510 | ConsumeToken(); |
| 511 | HasInvalidAccessor = true; |
| 512 | goto next_property_accessor; |
| 513 | |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 514 | // Otherwise, complain about the unknown accessor kind. |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 515 | } else { |
| 516 | Diag(KindLoc, diag::err_ms_property_unknown_accessor); |
| 517 | HasInvalidAccessor = true; |
| 518 | Kind = AK_Invalid; |
| 519 | |
| 520 | // 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] | 521 | if (!NextToken().is(tok::equal)) |
| 522 | break; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | // Consume the identifier. |
| 526 | ConsumeToken(); |
| 527 | |
| 528 | // Consume the '='. |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 529 | if (!TryConsumeToken(tok::equal)) { |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 530 | Diag(Tok.getLocation(), diag::err_ms_property_expected_equal) |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 531 | << KindStr; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 532 | break; |
| 533 | } |
| 534 | |
| 535 | // Expect the method name. |
| 536 | if (!Tok.is(tok::identifier)) { |
| 537 | Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name); |
| 538 | break; |
| 539 | } |
| 540 | |
| 541 | if (Kind == AK_Invalid) { |
| 542 | // Just drop invalid accessors. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 543 | } else if (AccessorNames[Kind] != nullptr) { |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 544 | // Complain about the repeated accessor, ignore it, and keep parsing. |
| 545 | Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr; |
| 546 | } else { |
| 547 | AccessorNames[Kind] = Tok.getIdentifierInfo(); |
| 548 | } |
| 549 | ConsumeToken(); |
| 550 | |
| 551 | next_property_accessor: |
| 552 | // Keep processing accessors until we run out. |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 553 | if (TryConsumeToken(tok::comma)) |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 554 | continue; |
| 555 | |
| 556 | // If we run into the ')', stop without consuming it. |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 557 | if (Tok.is(tok::r_paren)) |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 558 | break; |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 559 | |
| 560 | Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen); |
| 561 | break; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | // Only add the property attribute if it was well-formed. |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 565 | if (!HasInvalidAccessor) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 566 | Attrs.addNewPropertyAttr(AttrName, AttrNameLoc, nullptr, SourceLocation(), |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 567 | AccessorNames[AK_Get], AccessorNames[AK_Put], |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 568 | ParsedAttr::AS_Declspec); |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 569 | T.skipToEnd(); |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 570 | return !HasInvalidAccessor; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 571 | } |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 572 | |
Aaron Ballman | 95d5703 | 2014-04-14 16:44:26 +0000 | [diff] [blame] | 573 | unsigned NumArgs = |
| 574 | ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, nullptr, nullptr, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 575 | SourceLocation(), ParsedAttr::AS_Declspec); |
Aaron Ballman | 95d5703 | 2014-04-14 16:44:26 +0000 | [diff] [blame] | 576 | |
| 577 | // If this attribute's args were parsed, and it was expected to have |
| 578 | // arguments but none were provided, emit a diagnostic. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 579 | if (!Attrs.empty() && Attrs.begin()->getMaxArgs() && !NumArgs) { |
Aaron Ballman | ef5d94c | 2014-04-15 00:36:39 +0000 | [diff] [blame] | 580 | Diag(OpenParenLoc, diag::err_attribute_requires_arguments) << AttrName; |
Aaron Ballman | 95d5703 | 2014-04-14 16:44:26 +0000 | [diff] [blame] | 581 | return false; |
| 582 | } |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 583 | return true; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Eli Friedman | 06de2b5 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 586 | /// [MS] decl-specifier: |
| 587 | /// __declspec ( extended-decl-modifier-seq ) |
| 588 | /// |
| 589 | /// [MS] extended-decl-modifier-seq: |
| 590 | /// extended-decl-modifier[opt] |
| 591 | /// extended-decl-modifier extended-decl-modifier-seq |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 592 | void Parser::ParseMicrosoftDeclSpecs(ParsedAttributes &Attrs, |
| 593 | SourceLocation *End) { |
Saleem Abdulrasool | d170c4b | 2015-10-04 17:51:05 +0000 | [diff] [blame] | 594 | assert(getLangOpts().DeclSpecKeyword && "__declspec keyword is not enabled"); |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 595 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | 06de2b5 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 596 | |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 597 | while (Tok.is(tok::kw___declspec)) { |
| 598 | ConsumeToken(); |
| 599 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 600 | if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec", |
| 601 | tok::r_paren)) |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 602 | return; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 603 | |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 604 | // An empty declspec is perfectly legal and should not warn. Additionally, |
| 605 | // you can specify multiple attributes per declspec. |
| 606 | while (Tok.isNot(tok::r_paren)) { |
| 607 | // Attribute not present. |
| 608 | if (TryConsumeToken(tok::comma)) |
| 609 | continue; |
| 610 | |
| 611 | // We expect either a well-known identifier or a generic string. Anything |
| 612 | // else is a malformed declspec. |
| 613 | bool IsString = Tok.getKind() == tok::string_literal; |
| 614 | if (!IsString && Tok.getKind() != tok::identifier && |
| 615 | Tok.getKind() != tok::kw_restrict) { |
| 616 | Diag(Tok, diag::err_ms_declspec_type); |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 617 | T.skipToEnd(); |
| 618 | return; |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 619 | } |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 620 | |
| 621 | IdentifierInfo *AttrName; |
| 622 | SourceLocation AttrNameLoc; |
| 623 | if (IsString) { |
| 624 | SmallString<8> StrBuffer; |
| 625 | bool Invalid = false; |
| 626 | StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid); |
| 627 | if (Invalid) { |
| 628 | T.skipToEnd(); |
| 629 | return; |
| 630 | } |
| 631 | AttrName = PP.getIdentifierInfo(Str); |
| 632 | AttrNameLoc = ConsumeStringToken(); |
| 633 | } else { |
| 634 | AttrName = Tok.getIdentifierInfo(); |
| 635 | AttrNameLoc = ConsumeToken(); |
| 636 | } |
| 637 | |
| 638 | bool AttrHandled = false; |
| 639 | |
| 640 | // Parse attribute arguments. |
| 641 | if (Tok.is(tok::l_paren)) |
| 642 | AttrHandled = ParseMicrosoftDeclSpecArgs(AttrName, AttrNameLoc, Attrs); |
| 643 | else if (AttrName->getName() == "property") |
| 644 | // The property attribute must have an argument list. |
| 645 | Diag(Tok.getLocation(), diag::err_expected_lparen_after) |
| 646 | << AttrName->getName(); |
| 647 | |
| 648 | if (!AttrHandled) |
| 649 | Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 650 | ParsedAttr::AS_Declspec); |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 651 | } |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 652 | T.consumeClose(); |
| 653 | if (End) |
| 654 | *End = T.getCloseLocation(); |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 655 | } |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 656 | } |
| 657 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 658 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 659 | // Treat these like attributes |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 660 | while (true) { |
| 661 | switch (Tok.getKind()) { |
| 662 | case tok::kw___fastcall: |
| 663 | case tok::kw___stdcall: |
| 664 | case tok::kw___thiscall: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 665 | case tok::kw___regcall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 666 | case tok::kw___cdecl: |
| 667 | case tok::kw___vectorcall: |
| 668 | case tok::kw___ptr64: |
| 669 | case tok::kw___w64: |
| 670 | case tok::kw___ptr32: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 671 | case tok::kw___sptr: |
| 672 | case tok::kw___uptr: { |
| 673 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 674 | SourceLocation AttrNameLoc = ConsumeToken(); |
| 675 | attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 676 | ParsedAttr::AS_Keyword); |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 677 | break; |
| 678 | } |
| 679 | default: |
| 680 | return; |
| 681 | } |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 682 | } |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Nico Rieck | eaaae27 | 2014-12-04 23:31:08 +0000 | [diff] [blame] | 685 | void Parser::DiagnoseAndSkipExtendedMicrosoftTypeAttributes() { |
| 686 | SourceLocation StartLoc = Tok.getLocation(); |
| 687 | SourceLocation EndLoc = SkipExtendedMicrosoftTypeAttributes(); |
| 688 | |
| 689 | if (EndLoc.isValid()) { |
| 690 | SourceRange Range(StartLoc, EndLoc); |
| 691 | Diag(StartLoc, diag::warn_microsoft_qualifiers_ignored) << Range; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | SourceLocation Parser::SkipExtendedMicrosoftTypeAttributes() { |
| 696 | SourceLocation EndLoc; |
| 697 | |
| 698 | while (true) { |
| 699 | switch (Tok.getKind()) { |
| 700 | case tok::kw_const: |
| 701 | case tok::kw_volatile: |
| 702 | case tok::kw___fastcall: |
| 703 | case tok::kw___stdcall: |
| 704 | case tok::kw___thiscall: |
| 705 | case tok::kw___cdecl: |
| 706 | case tok::kw___vectorcall: |
| 707 | case tok::kw___ptr32: |
| 708 | case tok::kw___ptr64: |
| 709 | case tok::kw___w64: |
| 710 | case tok::kw___unaligned: |
| 711 | case tok::kw___sptr: |
| 712 | case tok::kw___uptr: |
| 713 | EndLoc = ConsumeToken(); |
| 714 | break; |
| 715 | default: |
| 716 | return EndLoc; |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 721 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 722 | // Treat these like attributes |
| 723 | while (Tok.is(tok::kw___pascal)) { |
| 724 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 725 | SourceLocation AttrNameLoc = ConsumeToken(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 726 | attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 727 | ParsedAttr::AS_Keyword); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 728 | } |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Anastasia Stulova | 6bdbcbb | 2016-02-19 18:30:11 +0000 | [diff] [blame] | 731 | void Parser::ParseOpenCLKernelAttributes(ParsedAttributes &attrs) { |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 732 | // Treat these like attributes |
| 733 | while (Tok.is(tok::kw___kernel)) { |
Richard Smith | 0cdcc98 | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 734 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 735 | SourceLocation AttrNameLoc = ConsumeToken(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 736 | attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 737 | ParsedAttr::AS_Keyword); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | |
Aaron Ballman | 05d76ea | 2014-01-14 01:29:54 +0000 | [diff] [blame] | 741 | void Parser::ParseOpenCLQualifiers(ParsedAttributes &Attrs) { |
| 742 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 743 | SourceLocation AttrNameLoc = Tok.getLocation(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 744 | Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 745 | ParsedAttr::AS_Keyword); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 748 | void Parser::ParseNullabilityTypeSpecifiers(ParsedAttributes &attrs) { |
| 749 | // Treat these like attributes, even though they're type specifiers. |
| 750 | while (true) { |
| 751 | switch (Tok.getKind()) { |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 752 | case tok::kw__Nonnull: |
| 753 | case tok::kw__Nullable: |
| 754 | case tok::kw__Null_unspecified: { |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 755 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 756 | SourceLocation AttrNameLoc = ConsumeToken(); |
| 757 | if (!getLangOpts().ObjC1) |
| 758 | Diag(AttrNameLoc, diag::ext_nullability) |
| 759 | << AttrName; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 760 | attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 761 | ParsedAttr::AS_Keyword); |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 762 | break; |
| 763 | } |
| 764 | default: |
| 765 | return; |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | |
Fariborz Jahanian | dbc956d | 2014-10-02 16:39:45 +0000 | [diff] [blame] | 770 | static bool VersionNumberSeparator(const char Separator) { |
| 771 | return (Separator == '.' || Separator == '_'); |
| 772 | } |
| 773 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 774 | /// Parse a version number. |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 775 | /// |
| 776 | /// version: |
| 777 | /// simple-integer |
Jan Korous | 2325569 | 2018-05-17 11:51:49 +0000 | [diff] [blame] | 778 | /// simple-integer '.' simple-integer |
| 779 | /// simple-integer '_' simple-integer |
| 780 | /// simple-integer '.' simple-integer '.' simple-integer |
| 781 | /// simple-integer '_' simple-integer '_' simple-integer |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 782 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
Erik Pilkington | 29099de | 2016-07-16 00:35:23 +0000 | [diff] [blame] | 783 | Range = SourceRange(Tok.getLocation(), Tok.getEndLoc()); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 784 | |
| 785 | if (!Tok.is(tok::numeric_constant)) { |
| 786 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 787 | SkipUntil(tok::comma, tok::r_paren, |
| 788 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 789 | return VersionTuple(); |
| 790 | } |
| 791 | |
| 792 | // Parse the major (and possibly minor and subminor) versions, which |
| 793 | // are stored in the numeric constant. We utilize a quirk of the |
| 794 | // lexer, which is that it handles something like 1.2.3 as a single |
| 795 | // numeric constant, rather than two separate tokens. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 796 | SmallString<512> Buffer; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 797 | Buffer.resize(Tok.getLength()+1); |
| 798 | const char *ThisTokBegin = &Buffer[0]; |
| 799 | |
| 800 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 801 | bool Invalid = false; |
| 802 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 803 | if (Invalid) |
| 804 | return VersionTuple(); |
| 805 | |
| 806 | // Parse the major version. |
| 807 | unsigned AfterMajor = 0; |
| 808 | unsigned Major = 0; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 809 | while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 810 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 811 | ++AfterMajor; |
| 812 | } |
| 813 | |
| 814 | if (AfterMajor == 0) { |
| 815 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 816 | SkipUntil(tok::comma, tok::r_paren, |
| 817 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 818 | return VersionTuple(); |
| 819 | } |
| 820 | |
| 821 | if (AfterMajor == ActualLength) { |
| 822 | ConsumeToken(); |
| 823 | |
| 824 | // We only had a single version component. |
| 825 | if (Major == 0) { |
| 826 | Diag(Tok, diag::err_zero_version); |
| 827 | return VersionTuple(); |
| 828 | } |
| 829 | |
| 830 | return VersionTuple(Major); |
| 831 | } |
| 832 | |
Fariborz Jahanian | ce72e63 | 2014-10-02 17:57:26 +0000 | [diff] [blame] | 833 | const char AfterMajorSeparator = ThisTokBegin[AfterMajor]; |
| 834 | if (!VersionNumberSeparator(AfterMajorSeparator) |
Fariborz Jahanian | dbc956d | 2014-10-02 16:39:45 +0000 | [diff] [blame] | 835 | || (AfterMajor + 1 == ActualLength)) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 836 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 837 | SkipUntil(tok::comma, tok::r_paren, |
| 838 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 839 | return VersionTuple(); |
| 840 | } |
| 841 | |
| 842 | // Parse the minor version. |
| 843 | unsigned AfterMinor = AfterMajor + 1; |
| 844 | unsigned Minor = 0; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 845 | while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 846 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 847 | ++AfterMinor; |
| 848 | } |
| 849 | |
| 850 | if (AfterMinor == ActualLength) { |
| 851 | ConsumeToken(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 852 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 853 | // We had major.minor. |
| 854 | if (Major == 0 && Minor == 0) { |
| 855 | Diag(Tok, diag::err_zero_version); |
| 856 | return VersionTuple(); |
| 857 | } |
| 858 | |
Jan Korous | 2325569 | 2018-05-17 11:51:49 +0000 | [diff] [blame] | 859 | return VersionTuple(Major, Minor); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Fariborz Jahanian | ce72e63 | 2014-10-02 17:57:26 +0000 | [diff] [blame] | 862 | const char AfterMinorSeparator = ThisTokBegin[AfterMinor]; |
Fariborz Jahanian | dbc956d | 2014-10-02 16:39:45 +0000 | [diff] [blame] | 863 | // If what follows is not a '.' or '_', we have a problem. |
Fariborz Jahanian | ce72e63 | 2014-10-02 17:57:26 +0000 | [diff] [blame] | 864 | if (!VersionNumberSeparator(AfterMinorSeparator)) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 865 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 866 | SkipUntil(tok::comma, tok::r_paren, |
| 867 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 868 | return VersionTuple(); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 869 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 870 | |
Fariborz Jahanian | b616161 | 2014-10-03 17:21:12 +0000 | [diff] [blame] | 871 | // Warn if separators, be it '.' or '_', do not match. |
Fariborz Jahanian | ce72e63 | 2014-10-02 17:57:26 +0000 | [diff] [blame] | 872 | if (AfterMajorSeparator != AfterMinorSeparator) |
| 873 | Diag(Tok, diag::warn_expected_consistent_version_separator); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 874 | |
| 875 | // Parse the subminor version. |
| 876 | unsigned AfterSubminor = AfterMinor + 1; |
| 877 | unsigned Subminor = 0; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 878 | while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 879 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 880 | ++AfterSubminor; |
| 881 | } |
| 882 | |
| 883 | if (AfterSubminor != ActualLength) { |
| 884 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 885 | SkipUntil(tok::comma, tok::r_paren, |
| 886 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 887 | return VersionTuple(); |
| 888 | } |
| 889 | ConsumeToken(); |
Jan Korous | 2325569 | 2018-05-17 11:51:49 +0000 | [diff] [blame] | 890 | return VersionTuple(Major, Minor, Subminor); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 893 | /// Parse the contents of the "availability" attribute. |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 894 | /// |
| 895 | /// availability-attribute: |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 896 | /// 'availability' '(' platform ',' opt-strict version-arg-list, |
| 897 | /// opt-replacement, opt-message')' |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 898 | /// |
| 899 | /// platform: |
| 900 | /// identifier |
| 901 | /// |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 902 | /// opt-strict: |
| 903 | /// 'strict' ',' |
Manman Ren | b636b90 | 2016-02-17 22:05:48 +0000 | [diff] [blame] | 904 | /// |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 905 | /// version-arg-list: |
| 906 | /// version-arg |
| 907 | /// version-arg ',' version-arg-list |
| 908 | /// |
| 909 | /// version-arg: |
| 910 | /// 'introduced' '=' version |
| 911 | /// 'deprecated' '=' version |
Douglas Gregor | fdd417f | 2012-03-11 04:53:21 +0000 | [diff] [blame] | 912 | /// 'obsoleted' = version |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 913 | /// 'unavailable' |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 914 | /// opt-replacement: |
| 915 | /// 'replacement' '=' <string> |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 916 | /// opt-message: |
| 917 | /// 'message' '=' <string> |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 918 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 919 | SourceLocation AvailabilityLoc, |
| 920 | ParsedAttributes &attrs, |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 921 | SourceLocation *endLoc, |
| 922 | IdentifierInfo *ScopeName, |
| 923 | SourceLocation ScopeLoc, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 924 | ParsedAttr::Syntax Syntax) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 925 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 926 | AvailabilityChange Changes[Unknown]; |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 927 | ExprResult MessageExpr, ReplacementExpr; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 928 | |
| 929 | // Opening '('. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 930 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 931 | if (T.consumeOpen()) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 932 | Diag(Tok, diag::err_expected) << tok::l_paren; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 933 | return; |
| 934 | } |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 935 | |
Manman Ren | b636b90 | 2016-02-17 22:05:48 +0000 | [diff] [blame] | 936 | // Parse the platform name. |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 937 | if (Tok.isNot(tok::identifier)) { |
| 938 | Diag(Tok, diag::err_availability_expected_platform); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 939 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 940 | return; |
| 941 | } |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 942 | IdentifierLoc *Platform = ParseIdentifierLoc(); |
Alex Lorenz | 0b1ce8b | 2017-08-15 14:42:01 +0000 | [diff] [blame] | 943 | if (const IdentifierInfo *const Ident = Platform->Ident) { |
| 944 | // Canonicalize platform name from "macosx" to "macos". |
| 945 | if (Ident->getName() == "macosx") |
| 946 | Platform->Ident = PP.getIdentifierInfo("macos"); |
| 947 | // Canonicalize platform name from "macosx_app_extension" to |
| 948 | // "macos_app_extension". |
| 949 | else if (Ident->getName() == "macosx_app_extension") |
| 950 | Platform->Ident = PP.getIdentifierInfo("macos_app_extension"); |
| 951 | else |
| 952 | Platform->Ident = PP.getIdentifierInfo( |
| 953 | AvailabilityAttr::canonicalizePlatformName(Ident->getName())); |
| 954 | } |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 955 | |
| 956 | // Parse the ',' following the platform name. |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 957 | if (ExpectAndConsume(tok::comma)) { |
| 958 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 959 | return; |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 960 | } |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 961 | |
| 962 | // If we haven't grabbed the pointers for the identifiers |
| 963 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 964 | if (!Ident_introduced) { |
| 965 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 966 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 967 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 968 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 969 | Ident_message = PP.getIdentifierInfo("message"); |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 970 | Ident_strict = PP.getIdentifierInfo("strict"); |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 971 | Ident_replacement = PP.getIdentifierInfo("replacement"); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 974 | // Parse the optional "strict", the optional "replacement" and the set of |
Manman Ren | b636b90 | 2016-02-17 22:05:48 +0000 | [diff] [blame] | 975 | // introductions/deprecations/removals. |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 976 | SourceLocation UnavailableLoc, StrictLoc; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 977 | do { |
| 978 | if (Tok.isNot(tok::identifier)) { |
| 979 | Diag(Tok, diag::err_availability_expected_change); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 980 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 981 | return; |
| 982 | } |
| 983 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 984 | SourceLocation KeywordLoc = ConsumeToken(); |
| 985 | |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 986 | if (Keyword == Ident_strict) { |
| 987 | if (StrictLoc.isValid()) { |
Manman Ren | b636b90 | 2016-02-17 22:05:48 +0000 | [diff] [blame] | 988 | Diag(KeywordLoc, diag::err_availability_redundant) |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 989 | << Keyword << SourceRange(StrictLoc); |
Manman Ren | b636b90 | 2016-02-17 22:05:48 +0000 | [diff] [blame] | 990 | } |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 991 | StrictLoc = KeywordLoc; |
Manman Ren | b636b90 | 2016-02-17 22:05:48 +0000 | [diff] [blame] | 992 | continue; |
| 993 | } |
| 994 | |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 995 | if (Keyword == Ident_unavailable) { |
| 996 | if (UnavailableLoc.isValid()) { |
| 997 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 998 | << Keyword << SourceRange(UnavailableLoc); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 999 | } |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1000 | UnavailableLoc = KeywordLoc; |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 1001 | continue; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1004 | if (Tok.isNot(tok::equal)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1005 | Diag(Tok, diag::err_expected_after) << Keyword << tok::equal; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1006 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1007 | return; |
| 1008 | } |
| 1009 | ConsumeToken(); |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 1010 | if (Keyword == Ident_message || Keyword == Ident_replacement) { |
David Majnemer | 2e49830 | 2014-07-18 05:43:12 +0000 | [diff] [blame] | 1011 | if (Tok.isNot(tok::string_literal)) { |
Andy Gibbs | a8df57a | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 1012 | Diag(Tok, diag::err_expected_string_literal) |
| 1013 | << /*Source='availability attribute'*/2; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1014 | SkipUntil(tok::r_paren, StopAtSemi); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1015 | return; |
| 1016 | } |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 1017 | if (Keyword == Ident_message) |
| 1018 | MessageExpr = ParseStringLiteralExpression(); |
| 1019 | else |
| 1020 | ReplacementExpr = ParseStringLiteralExpression(); |
David Majnemer | 2e49830 | 2014-07-18 05:43:12 +0000 | [diff] [blame] | 1021 | // Also reject wide string literals. |
| 1022 | if (StringLiteral *MessageStringLiteral = |
| 1023 | cast_or_null<StringLiteral>(MessageExpr.get())) { |
| 1024 | if (MessageStringLiteral->getCharByteWidth() != 1) { |
| 1025 | Diag(MessageStringLiteral->getSourceRange().getBegin(), |
| 1026 | diag::err_expected_string_literal) |
| 1027 | << /*Source='availability attribute'*/ 2; |
| 1028 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1029 | return; |
| 1030 | } |
| 1031 | } |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 1032 | if (Keyword == Ident_message) |
| 1033 | break; |
| 1034 | else |
| 1035 | continue; |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1036 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1037 | |
Fariborz Jahanian | 5a29e6a | 2014-11-05 23:58:55 +0000 | [diff] [blame] | 1038 | // Special handling of 'NA' only when applied to introduced or |
| 1039 | // deprecated. |
| 1040 | if ((Keyword == Ident_introduced || Keyword == Ident_deprecated) && |
| 1041 | Tok.is(tok::identifier)) { |
| 1042 | IdentifierInfo *NA = Tok.getIdentifierInfo(); |
| 1043 | if (NA->getName() == "NA") { |
| 1044 | ConsumeToken(); |
| 1045 | if (Keyword == Ident_introduced) |
| 1046 | UnavailableLoc = KeywordLoc; |
| 1047 | continue; |
| 1048 | } |
| 1049 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1050 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1051 | SourceRange VersionRange; |
| 1052 | VersionTuple Version = ParseVersionTuple(VersionRange); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1053 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1054 | if (Version.empty()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1055 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1056 | return; |
| 1057 | } |
| 1058 | |
| 1059 | unsigned Index; |
| 1060 | if (Keyword == Ident_introduced) |
| 1061 | Index = Introduced; |
| 1062 | else if (Keyword == Ident_deprecated) |
| 1063 | Index = Deprecated; |
| 1064 | else if (Keyword == Ident_obsoleted) |
| 1065 | Index = Obsoleted; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1066 | else |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1067 | Index = Unknown; |
| 1068 | |
| 1069 | if (Index < Unknown) { |
| 1070 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 1071 | Diag(KeywordLoc, diag::err_availability_redundant) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1072 | << Keyword |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1073 | << SourceRange(Changes[Index].KeywordLoc, |
| 1074 | Changes[Index].VersionRange.getEnd()); |
| 1075 | } |
| 1076 | |
| 1077 | Changes[Index].KeywordLoc = KeywordLoc; |
| 1078 | Changes[Index].Version = Version; |
| 1079 | Changes[Index].VersionRange = VersionRange; |
| 1080 | } else { |
| 1081 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 1082 | << Keyword << VersionRange; |
| 1083 | } |
| 1084 | |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 1085 | } while (TryConsumeToken(tok::comma)); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1086 | |
| 1087 | // Closing ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1088 | if (T.consumeClose()) |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1089 | return; |
| 1090 | |
| 1091 | if (endLoc) |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1092 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1093 | |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1094 | // The 'unavailable' availability cannot be combined with any other |
| 1095 | // availability changes. Make sure that hasn't happened. |
| 1096 | if (UnavailableLoc.isValid()) { |
| 1097 | bool Complained = false; |
| 1098 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 1099 | if (Changes[Index].KeywordLoc.isValid()) { |
| 1100 | if (!Complained) { |
| 1101 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 1102 | << SourceRange(Changes[Index].KeywordLoc, |
| 1103 | Changes[Index].VersionRange.getEnd()); |
| 1104 | Complained = true; |
| 1105 | } |
| 1106 | |
| 1107 | // Clear out the availability. |
| 1108 | Changes[Index] = AvailabilityChange(); |
| 1109 | } |
| 1110 | } |
| 1111 | } |
| 1112 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1113 | // Record this attribute |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1114 | attrs.addNew(&Availability, |
| 1115 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1116 | ScopeName, ScopeLoc, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1117 | Platform, |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1118 | Changes[Introduced], |
| 1119 | Changes[Deprecated], |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1120 | Changes[Obsoleted], |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1121 | UnavailableLoc, MessageExpr.get(), |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 1122 | Syntax, StrictLoc, ReplacementExpr.get()); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1125 | /// Parse the contents of the "external_source_symbol" attribute. |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 1126 | /// |
| 1127 | /// external-source-symbol-attribute: |
| 1128 | /// 'external_source_symbol' '(' keyword-arg-list ')' |
| 1129 | /// |
| 1130 | /// keyword-arg-list: |
| 1131 | /// keyword-arg |
| 1132 | /// keyword-arg ',' keyword-arg-list |
| 1133 | /// |
| 1134 | /// keyword-arg: |
| 1135 | /// 'language' '=' <string> |
| 1136 | /// 'defined_in' '=' <string> |
| 1137 | /// 'generated_declaration' |
| 1138 | void Parser::ParseExternalSourceSymbolAttribute( |
| 1139 | IdentifierInfo &ExternalSourceSymbol, SourceLocation Loc, |
| 1140 | ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1141 | SourceLocation ScopeLoc, ParsedAttr::Syntax Syntax) { |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 1142 | // Opening '('. |
| 1143 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1144 | if (T.expectAndConsume()) |
| 1145 | return; |
| 1146 | |
| 1147 | // Initialize the pointers for the keyword identifiers when required. |
| 1148 | if (!Ident_language) { |
| 1149 | Ident_language = PP.getIdentifierInfo("language"); |
| 1150 | Ident_defined_in = PP.getIdentifierInfo("defined_in"); |
| 1151 | Ident_generated_declaration = PP.getIdentifierInfo("generated_declaration"); |
| 1152 | } |
| 1153 | |
| 1154 | ExprResult Language; |
| 1155 | bool HasLanguage = false; |
| 1156 | ExprResult DefinedInExpr; |
| 1157 | bool HasDefinedIn = false; |
| 1158 | IdentifierLoc *GeneratedDeclaration = nullptr; |
| 1159 | |
| 1160 | // Parse the language/defined_in/generated_declaration keywords |
| 1161 | do { |
| 1162 | if (Tok.isNot(tok::identifier)) { |
| 1163 | Diag(Tok, diag::err_external_source_symbol_expected_keyword); |
| 1164 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1165 | return; |
| 1166 | } |
| 1167 | |
| 1168 | SourceLocation KeywordLoc = Tok.getLocation(); |
| 1169 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 1170 | if (Keyword == Ident_generated_declaration) { |
| 1171 | if (GeneratedDeclaration) { |
| 1172 | Diag(Tok, diag::err_external_source_symbol_duplicate_clause) << Keyword; |
| 1173 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1174 | return; |
| 1175 | } |
| 1176 | GeneratedDeclaration = ParseIdentifierLoc(); |
| 1177 | continue; |
| 1178 | } |
| 1179 | |
| 1180 | if (Keyword != Ident_language && Keyword != Ident_defined_in) { |
| 1181 | Diag(Tok, diag::err_external_source_symbol_expected_keyword); |
| 1182 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1183 | return; |
| 1184 | } |
| 1185 | |
| 1186 | ConsumeToken(); |
| 1187 | if (ExpectAndConsume(tok::equal, diag::err_expected_after, |
| 1188 | Keyword->getName())) { |
| 1189 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1190 | return; |
| 1191 | } |
| 1192 | |
| 1193 | bool HadLanguage = HasLanguage, HadDefinedIn = HasDefinedIn; |
| 1194 | if (Keyword == Ident_language) |
| 1195 | HasLanguage = true; |
| 1196 | else |
| 1197 | HasDefinedIn = true; |
| 1198 | |
| 1199 | if (Tok.isNot(tok::string_literal)) { |
| 1200 | Diag(Tok, diag::err_expected_string_literal) |
| 1201 | << /*Source='external_source_symbol attribute'*/ 3 |
| 1202 | << /*language | source container*/ (Keyword != Ident_language); |
| 1203 | SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch); |
| 1204 | continue; |
| 1205 | } |
| 1206 | if (Keyword == Ident_language) { |
| 1207 | if (HadLanguage) { |
| 1208 | Diag(KeywordLoc, diag::err_external_source_symbol_duplicate_clause) |
| 1209 | << Keyword; |
| 1210 | ParseStringLiteralExpression(); |
| 1211 | continue; |
| 1212 | } |
| 1213 | Language = ParseStringLiteralExpression(); |
| 1214 | } else { |
| 1215 | assert(Keyword == Ident_defined_in && "Invalid clause keyword!"); |
| 1216 | if (HadDefinedIn) { |
| 1217 | Diag(KeywordLoc, diag::err_external_source_symbol_duplicate_clause) |
| 1218 | << Keyword; |
| 1219 | ParseStringLiteralExpression(); |
| 1220 | continue; |
| 1221 | } |
| 1222 | DefinedInExpr = ParseStringLiteralExpression(); |
| 1223 | } |
| 1224 | } while (TryConsumeToken(tok::comma)); |
| 1225 | |
| 1226 | // Closing ')'. |
| 1227 | if (T.consumeClose()) |
| 1228 | return; |
| 1229 | if (EndLoc) |
| 1230 | *EndLoc = T.getCloseLocation(); |
| 1231 | |
| 1232 | ArgsUnion Args[] = {Language.get(), DefinedInExpr.get(), |
| 1233 | GeneratedDeclaration}; |
| 1234 | Attrs.addNew(&ExternalSourceSymbol, SourceRange(Loc, T.getCloseLocation()), |
| 1235 | ScopeName, ScopeLoc, Args, llvm::array_lengthof(Args), Syntax); |
| 1236 | } |
| 1237 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1238 | /// Parse the contents of the "objc_bridge_related" attribute. |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1239 | /// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')' |
| 1240 | /// related_class: |
| 1241 | /// Identifier |
| 1242 | /// |
| 1243 | /// opt-class_method: |
| 1244 | /// Identifier: | <empty> |
| 1245 | /// |
| 1246 | /// opt-instance_method: |
| 1247 | /// Identifier | <empty> |
| 1248 | /// |
| 1249 | void Parser::ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated, |
| 1250 | SourceLocation ObjCBridgeRelatedLoc, |
| 1251 | ParsedAttributes &attrs, |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1252 | SourceLocation *endLoc, |
| 1253 | IdentifierInfo *ScopeName, |
| 1254 | SourceLocation ScopeLoc, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1255 | ParsedAttr::Syntax Syntax) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1256 | // Opening '('. |
| 1257 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1258 | if (T.consumeOpen()) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1259 | Diag(Tok, diag::err_expected) << tok::l_paren; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1260 | return; |
| 1261 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1262 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1263 | // Parse the related class name. |
| 1264 | if (Tok.isNot(tok::identifier)) { |
| 1265 | Diag(Tok, diag::err_objcbridge_related_expected_related_class); |
| 1266 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1267 | return; |
| 1268 | } |
| 1269 | IdentifierLoc *RelatedClass = ParseIdentifierLoc(); |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 1270 | if (ExpectAndConsume(tok::comma)) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1271 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1272 | return; |
| 1273 | } |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1274 | |
Aaron Ballman | 48a533d | 2018-02-27 23:49:28 +0000 | [diff] [blame] | 1275 | // Parse class method name. It's non-optional in the sense that a trailing |
| 1276 | // comma is required, but it can be the empty string, and then we record a |
| 1277 | // nullptr. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1278 | IdentifierLoc *ClassMethod = nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1279 | if (Tok.is(tok::identifier)) { |
| 1280 | ClassMethod = ParseIdentifierLoc(); |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1281 | if (!TryConsumeToken(tok::colon)) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1282 | Diag(Tok, diag::err_objcbridge_related_selector_name); |
| 1283 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1284 | return; |
| 1285 | } |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1286 | } |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1287 | if (!TryConsumeToken(tok::comma)) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1288 | if (Tok.is(tok::colon)) |
| 1289 | Diag(Tok, diag::err_objcbridge_related_selector_name); |
| 1290 | else |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1291 | Diag(Tok, diag::err_expected) << tok::comma; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1292 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1293 | return; |
| 1294 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1295 | |
Aaron Ballman | 48a533d | 2018-02-27 23:49:28 +0000 | [diff] [blame] | 1296 | // Parse instance method name. Also non-optional but empty string is |
| 1297 | // permitted. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1298 | IdentifierLoc *InstanceMethod = nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1299 | if (Tok.is(tok::identifier)) |
| 1300 | InstanceMethod = ParseIdentifierLoc(); |
| 1301 | else if (Tok.isNot(tok::r_paren)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1302 | Diag(Tok, diag::err_expected) << tok::r_paren; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1303 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1304 | return; |
| 1305 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1306 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1307 | // Closing ')'. |
| 1308 | if (T.consumeClose()) |
| 1309 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1310 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1311 | if (endLoc) |
| 1312 | *endLoc = T.getCloseLocation(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1313 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1314 | // Record this attribute |
| 1315 | attrs.addNew(&ObjCBridgeRelated, |
| 1316 | SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1317 | ScopeName, ScopeLoc, |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1318 | RelatedClass, |
| 1319 | ClassMethod, |
| 1320 | InstanceMethod, |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1321 | Syntax); |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1322 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1323 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1324 | // Late Parsed Attributes: |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1325 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 1326 | |
| 1327 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 1328 | |
| 1329 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 1330 | Self->ParseLexedAttributes(*Class); |
| 1331 | } |
| 1332 | |
| 1333 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1334 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 1338 | /// scope appropriately. |
| 1339 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 1340 | // Deal with templates |
| 1341 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 1342 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 1343 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 1344 | HasTemplateScope); |
| 1345 | if (HasTemplateScope) |
| 1346 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 1347 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1348 | // Set or update the scope flags. |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1349 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1350 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1351 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 1352 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 1353 | |
DeLesley Hutchins | 6f86004 | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1354 | // Enter the scope of nested classes |
| 1355 | if (!AlreadyHasClassScope) |
| 1356 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 1357 | Class.TagOrTemplate); |
Benjamin Kramer | 1d373c6 | 2012-05-17 12:01:52 +0000 | [diff] [blame] | 1358 | if (!Class.LateParsedDeclarations.empty()) { |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1359 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){ |
| 1360 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 1361 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1362 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1363 | |
DeLesley Hutchins | 6f86004 | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1364 | if (!AlreadyHasClassScope) |
| 1365 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 1366 | Class.TagOrTemplate); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1369 | /// Parse all attributes in LAs, and attach them to Decl D. |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1370 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 1371 | bool EnterScope, bool OnDefinition) { |
DeLesley Hutchins | 66e300e | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1372 | assert(LAs.parseSoon() && |
| 1373 | "Attribute list should be marked for immediate parsing."); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1374 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 19c722d | 2012-08-15 22:41:04 +0000 | [diff] [blame] | 1375 | if (D) |
| 1376 | LAs[i]->addDecl(D); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1377 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
Benjamin Kramer | bafc49a | 2012-04-14 12:44:47 +0000 | [diff] [blame] | 1378 | delete LAs[i]; |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1379 | } |
| 1380 | LAs.clear(); |
| 1381 | } |
| 1382 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1383 | /// Finish parsing an attribute for which parsing was delayed. |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1384 | /// This will be called at the end of parsing a class declaration |
| 1385 | /// for each LateParsedAttribute. We consume the saved tokens and |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1386 | /// create an attribute with the arguments filled in. We add this |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1387 | /// to the Attribute list for the decl. |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1388 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 1389 | bool EnterScope, bool OnDefinition) { |
David Majnemer | d5946f5 | 2015-01-13 08:35:24 +0000 | [diff] [blame] | 1390 | // Create a fake EOF so that attribute parsing won't go off the end of the |
| 1391 | // attribute. |
| 1392 | Token AttrEnd; |
| 1393 | AttrEnd.startToken(); |
| 1394 | AttrEnd.setKind(tok::eof); |
| 1395 | AttrEnd.setLocation(Tok.getLocation()); |
| 1396 | AttrEnd.setEofData(LA.Toks.data()); |
| 1397 | LA.Toks.push_back(AttrEnd); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1398 | |
| 1399 | // Append the current token at the end of the new token stream so that it |
| 1400 | // doesn't get lost. |
| 1401 | LA.Toks.push_back(Tok); |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1402 | PP.EnterTokenStream(LA.Toks, true); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1403 | // Consume the previously pushed token. |
Argyrios Kyrtzidis | c36633c | 2013-03-27 23:58:17 +0000 | [diff] [blame] | 1404 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1405 | |
| 1406 | ParsedAttributes Attrs(AttrFactory); |
| 1407 | SourceLocation endLoc; |
| 1408 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1409 | if (LA.Decls.size() > 0) { |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1410 | Decl *D = LA.Decls[0]; |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1411 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1412 | RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext()); |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1413 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1414 | // Allow 'this' within late-parsed attributes. |
Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 1415 | Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0, |
| 1416 | ND && ND->isCXXInstanceMember()); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1417 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1418 | if (LA.Decls.size() == 1) { |
| 1419 | // If the Decl is templatized, add template parameters to scope. |
| 1420 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 1421 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 1422 | if (HasTemplateScope) |
| 1423 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1424 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1425 | // If the Decl is on a function, add function parameters to the scope. |
| 1426 | bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
Momchil Velikov | 57c681f | 2017-08-10 15:43:06 +0000 | [diff] [blame] | 1427 | ParseScope FnScope( |
| 1428 | this, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope, |
| 1429 | HasFunScope); |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1430 | if (HasFunScope) |
| 1431 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1432 | |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1433 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1434 | nullptr, SourceLocation(), ParsedAttr::AS_GNU, |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1435 | nullptr); |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1436 | |
| 1437 | if (HasFunScope) { |
| 1438 | Actions.ActOnExitFunctionContext(); |
| 1439 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 1440 | } |
| 1441 | if (HasTemplateScope) { |
| 1442 | TempScope.Exit(); |
| 1443 | } |
| 1444 | } else { |
| 1445 | // If there are multiple decls, then the decl cannot be within the |
| 1446 | // function scope. |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1447 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1448 | nullptr, SourceLocation(), ParsedAttr::AS_GNU, |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1449 | nullptr); |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1450 | } |
DeLesley Hutchins | 71d6103 | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 1451 | } else { |
| 1452 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1455 | if (OnDefinition && !Attrs.empty() && !Attrs.begin()->isCXX11Attribute() && |
| 1456 | Attrs.begin()->isKnownToGCC()) |
Aaron Ballman | 9a99e0d | 2014-01-20 17:18:35 +0000 | [diff] [blame] | 1457 | Diag(Tok, diag::warn_attribute_on_function_definition) |
| 1458 | << &LA.AttrName; |
| 1459 | |
| 1460 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1461 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1462 | |
David Majnemer | d5946f5 | 2015-01-13 08:35:24 +0000 | [diff] [blame] | 1463 | // Due to a parsing error, we either went over the cached tokens or |
| 1464 | // there are still cached tokens left, so we skip the leftover tokens. |
| 1465 | while (Tok.isNot(tok::eof)) |
| 1466 | ConsumeAnyToken(); |
| 1467 | |
| 1468 | if (Tok.is(tok::eof) && Tok.getEofData() == AttrEnd.getEofData()) |
| 1469 | ConsumeAnyToken(); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1472 | void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName, |
| 1473 | SourceLocation AttrNameLoc, |
| 1474 | ParsedAttributes &Attrs, |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1475 | SourceLocation *EndLoc, |
| 1476 | IdentifierInfo *ScopeName, |
| 1477 | SourceLocation ScopeLoc, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1478 | ParsedAttr::Syntax Syntax) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1479 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 1480 | |
| 1481 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1482 | T.consumeOpen(); |
| 1483 | |
| 1484 | if (Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1485 | Diag(Tok, diag::err_expected) << tok::identifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1486 | T.skipToEnd(); |
| 1487 | return; |
| 1488 | } |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 1489 | IdentifierLoc *ArgumentKind = ParseIdentifierLoc(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1490 | |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 1491 | if (ExpectAndConsume(tok::comma)) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1492 | T.skipToEnd(); |
| 1493 | return; |
| 1494 | } |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1495 | |
| 1496 | SourceRange MatchingCTypeRange; |
| 1497 | TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange); |
| 1498 | if (MatchingCType.isInvalid()) { |
| 1499 | T.skipToEnd(); |
| 1500 | return; |
| 1501 | } |
| 1502 | |
| 1503 | bool LayoutCompatible = false; |
| 1504 | bool MustBeNull = false; |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1505 | while (TryConsumeToken(tok::comma)) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1506 | if (Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1507 | Diag(Tok, diag::err_expected) << tok::identifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1508 | T.skipToEnd(); |
| 1509 | return; |
| 1510 | } |
| 1511 | IdentifierInfo *Flag = Tok.getIdentifierInfo(); |
| 1512 | if (Flag->isStr("layout_compatible")) |
| 1513 | LayoutCompatible = true; |
| 1514 | else if (Flag->isStr("must_be_null")) |
| 1515 | MustBeNull = true; |
| 1516 | else { |
| 1517 | Diag(Tok, diag::err_type_safety_unknown_flag) << Flag; |
| 1518 | T.skipToEnd(); |
| 1519 | return; |
| 1520 | } |
| 1521 | ConsumeToken(); // consume flag |
| 1522 | } |
| 1523 | |
| 1524 | if (!T.consumeClose()) { |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1525 | Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, ScopeName, ScopeLoc, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1526 | ArgumentKind, MatchingCType.get(), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1527 | LayoutCompatible, MustBeNull, Syntax); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | if (EndLoc) |
| 1531 | *EndLoc = T.getCloseLocation(); |
| 1532 | } |
| 1533 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1534 | /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets |
| 1535 | /// of a C++11 attribute-specifier in a location where an attribute is not |
| 1536 | /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this |
| 1537 | /// situation. |
| 1538 | /// |
| 1539 | /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if |
| 1540 | /// this doesn't appear to actually be an attribute-specifier, and the caller |
| 1541 | /// should try to parse it. |
| 1542 | bool Parser::DiagnoseProhibitedCXX11Attribute() { |
| 1543 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)); |
| 1544 | |
| 1545 | switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) { |
| 1546 | case CAK_NotAttributeSpecifier: |
| 1547 | // No diagnostic: we're in Obj-C++11 and this is not actually an attribute. |
| 1548 | return false; |
| 1549 | |
| 1550 | case CAK_InvalidAttributeSpecifier: |
| 1551 | Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute); |
| 1552 | return false; |
| 1553 | |
| 1554 | case CAK_AttributeSpecifier: |
| 1555 | // Parse and discard the attributes. |
| 1556 | SourceLocation BeginLoc = ConsumeBracket(); |
| 1557 | ConsumeBracket(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1558 | SkipUntil(tok::r_square); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1559 | assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied"); |
| 1560 | SourceLocation EndLoc = ConsumeBracket(); |
| 1561 | Diag(BeginLoc, diag::err_attributes_not_allowed) |
| 1562 | << SourceRange(BeginLoc, EndLoc); |
| 1563 | return true; |
| 1564 | } |
Chandler Carruth | d8f7d38 | 2012-04-10 16:03:08 +0000 | [diff] [blame] | 1565 | llvm_unreachable("All cases handled above."); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1568 | /// We have found the opening square brackets of a C++11 |
Richard Smith | 98155ad | 2013-02-20 01:17:14 +0000 | [diff] [blame] | 1569 | /// attribute-specifier in a location where an attribute is not permitted, but |
| 1570 | /// we know where the attributes ought to be written. Parse them anyway, and |
| 1571 | /// provide a fixit moving them to the right place. |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1572 | void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs, |
| 1573 | SourceLocation CorrectLocation) { |
| 1574 | assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) || |
| 1575 | Tok.is(tok::kw_alignas)); |
| 1576 | |
| 1577 | // Consume the attributes. |
| 1578 | SourceLocation Loc = Tok.getLocation(); |
| 1579 | ParseCXX11Attributes(Attrs); |
| 1580 | CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true); |
Faisal Vali | c5089c0 | 2017-12-25 22:23:20 +0000 | [diff] [blame] | 1581 | // FIXME: use err_attributes_misplaced |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1582 | Diag(Loc, diag::err_attributes_not_allowed) |
| 1583 | << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange) |
| 1584 | << FixItHint::CreateRemoval(AttrRange); |
| 1585 | } |
| 1586 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1587 | void Parser::DiagnoseProhibitedAttributes( |
| 1588 | const SourceRange &Range, const SourceLocation CorrectLocation) { |
Faisal Vali | c5089c0 | 2017-12-25 22:23:20 +0000 | [diff] [blame] | 1589 | if (CorrectLocation.isValid()) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1590 | CharSourceRange AttrRange(Range, true); |
Faisal Vali | c5089c0 | 2017-12-25 22:23:20 +0000 | [diff] [blame] | 1591 | Diag(CorrectLocation, diag::err_attributes_misplaced) |
| 1592 | << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange) |
| 1593 | << FixItHint::CreateRemoval(AttrRange); |
| 1594 | } else |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1595 | Diag(Range.getBegin(), diag::err_attributes_not_allowed) << Range; |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Richard Smith | 49cc1cc | 2016-08-18 21:59:42 +0000 | [diff] [blame] | 1598 | void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &Attrs, |
| 1599 | unsigned DiagID) { |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1600 | for (const ParsedAttr &AL : Attrs) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1601 | if (!AL.isCXX11Attribute() && !AL.isC2xAttribute()) |
Richard Smith | 49cc1cc | 2016-08-18 21:59:42 +0000 | [diff] [blame] | 1602 | continue; |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1603 | if (AL.getKind() == ParsedAttr::UnknownAttribute) |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1604 | Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL.getName(); |
Richard Smith | 49cc1cc | 2016-08-18 21:59:42 +0000 | [diff] [blame] | 1605 | else { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1606 | Diag(AL.getLoc(), DiagID) << AL.getName(); |
| 1607 | AL.setInvalid(); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1608 | } |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1609 | } |
| 1610 | } |
| 1611 | |
Nico Weber | 32a0fc7 | 2016-09-03 03:01:32 +0000 | [diff] [blame] | 1612 | // Usually, `__attribute__((attrib)) class Foo {} var` means that attribute |
| 1613 | // applies to var, not the type Foo. |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1614 | // As an exception to the rule, __declspec(align(...)) before the |
| 1615 | // class-key affects the type instead of the variable. |
Nico Weber | 32a0fc7 | 2016-09-03 03:01:32 +0000 | [diff] [blame] | 1616 | // Also, Microsoft-style [attributes] seem to affect the type instead of the |
| 1617 | // variable. |
| 1618 | // This function moves attributes that should apply to the type off DS to Attrs. |
| 1619 | void Parser::stripTypeAttributesOffDeclSpec(ParsedAttributesWithRange &Attrs, |
| 1620 | DeclSpec &DS, |
| 1621 | Sema::TagUseKind TUK) { |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1622 | if (TUK == Sema::TUK_Reference) |
| 1623 | return; |
| 1624 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1625 | llvm::SmallVector<ParsedAttr *, 1> ToBeMoved; |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1626 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1627 | for (ParsedAttr &AL : DS.getAttributes()) { |
| 1628 | if ((AL.getKind() == ParsedAttr::AT_Aligned && |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1629 | AL.isDeclspecAttribute()) || |
| 1630 | AL.isMicrosoftAttribute()) |
| 1631 | ToBeMoved.push_back(&AL); |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1632 | } |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 1633 | |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1634 | for (ParsedAttr *AL : ToBeMoved) { |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1635 | DS.getAttributes().remove(AL); |
| 1636 | Attrs.addAtEnd(AL); |
| 1637 | } |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1638 | } |
| 1639 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1640 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 1641 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1642 | /// 'Context' should be a DeclaratorContext value. This returns the |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1643 | /// location of the semicolon in DeclEnd. |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1644 | /// |
| 1645 | /// declaration: [C99 6.7] |
| 1646 | /// block-declaration -> |
| 1647 | /// simple-declaration |
| 1648 | /// others [FIXME] |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1649 | /// [C++] template-declaration |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1650 | /// [C++] namespace-definition |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1651 | /// [C++] using-directive |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1652 | /// [C++] using-declaration |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1653 | /// [C++11/C11] static_assert-declaration |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1654 | /// others... [FIXME] |
| 1655 | /// |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1656 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(DeclaratorContext Context, |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1657 | SourceLocation &DeclEnd, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1658 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 355094e | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 1659 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | 59b7528 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 1660 | // Must temporarily exit the objective-c container scope for |
| 1661 | // parsing c none objective-c decls. |
| 1662 | ObjCDeclContextSwitch ObjCDC(*this); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1663 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1664 | Decl *SingleDecl = nullptr; |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1665 | switch (Tok.getKind()) { |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1666 | case tok::kw_template: |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1667 | case tok::kw_export: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1668 | ProhibitAttributes(attrs); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1669 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd, attrs); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1670 | break; |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1671 | case tok::kw_inline: |
Sebastian Redl | 5a5f2c7 | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 1672 | // 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] | 1673 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1674 | ProhibitAttributes(attrs); |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1675 | SourceLocation InlineLoc = ConsumeToken(); |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 1676 | return ParseNamespace(Context, DeclEnd, InlineLoc); |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1677 | } |
Rafael Espindola | 1bd906d | 2014-10-22 14:27:08 +0000 | [diff] [blame] | 1678 | return ParseSimpleDeclaration(Context, DeclEnd, attrs, |
Fariborz Jahanian | 1db5c94 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1679 | true); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1680 | case tok::kw_namespace: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1681 | ProhibitAttributes(attrs); |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 1682 | return ParseNamespace(Context, DeclEnd); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1683 | case tok::kw_using: |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 1684 | return ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
| 1685 | DeclEnd, attrs); |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1686 | case tok::kw_static_assert: |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1687 | case tok::kw__Static_assert: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1688 | ProhibitAttributes(attrs); |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1689 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1690 | break; |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1691 | default: |
Rafael Espindola | 1bd906d | 2014-10-22 14:27:08 +0000 | [diff] [blame] | 1692 | return ParseSimpleDeclaration(Context, DeclEnd, attrs, true); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1693 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1694 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1695 | // 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] | 1696 | // single decl, convert it now. |
| 1697 | return Actions.ConvertDeclToDeclGroup(SingleDecl); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 1701 | /// declaration-specifiers init-declarator-list[opt] ';' |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1702 | /// [C++11] attribute-specifier-seq decl-specifier-seq[opt] |
| 1703 | /// init-declarator-list ';' |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1704 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 1705 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1706 | /// |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1707 | /// for-range-declaration: [C++11 6.5p1: stmt.ranged] |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1708 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1709 | /// |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1710 | /// 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] | 1711 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1712 | /// |
| 1713 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 1714 | /// of a simple-declaration. If we find that we are, we also parse the |
| 1715 | /// for-range-initializer, and place it here. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1716 | Parser::DeclGroupPtrTy |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1717 | Parser::ParseSimpleDeclaration(DeclaratorContext Context, |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1718 | SourceLocation &DeclEnd, |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1719 | ParsedAttributesWithRange &Attrs, |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1720 | bool RequireSemi, ForRangeInit *FRI) { |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1721 | // Parse the common declaration-specifiers piece. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1722 | ParsingDeclSpec DS(*this); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1723 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 1724 | DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context); |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 1725 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 1726 | |
| 1727 | // If we had a free-standing type definition with a missing semicolon, we |
| 1728 | // may get this far before the problem becomes obvious. |
| 1729 | if (DS.hasTagDefinition() && |
| 1730 | DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext)) |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 1731 | return nullptr; |
Abramo Bagnara | 1cd8368 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 1732 | |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 1733 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 1734 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1735 | if (Tok.is(tok::semi)) { |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1736 | ProhibitAttributes(Attrs); |
Argyrios Kyrtzidis | fbb2bb5 | 2012-05-16 23:49:15 +0000 | [diff] [blame] | 1737 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 005fc1b | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1738 | if (RequireSemi) ConsumeToken(); |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 1739 | RecordDecl *AnonRecord = nullptr; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1740 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 1741 | DS, AnonRecord); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1742 | DS.complete(TheDecl); |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 1743 | if (AnonRecord) { |
| 1744 | Decl* decls[] = {AnonRecord, TheDecl}; |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 1745 | return Actions.BuildDeclaratorGroup(decls); |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 1746 | } |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1747 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 1748 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1749 | |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1750 | DS.takeAttributesFrom(Attrs); |
Reid Kleckner | d61a311 | 2014-12-15 23:16:32 +0000 | [diff] [blame] | 1751 | return ParseDeclGroup(DS, Context, &DeclEnd, FRI); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1752 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1753 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1754 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1755 | /// for a declarator. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1756 | bool Parser::MightBeDeclarator(DeclaratorContext Context) { |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1757 | switch (Tok.getKind()) { |
| 1758 | case tok::annot_cxxscope: |
| 1759 | case tok::annot_template_id: |
| 1760 | case tok::caret: |
| 1761 | case tok::code_completion: |
| 1762 | case tok::coloncolon: |
| 1763 | case tok::ellipsis: |
| 1764 | case tok::kw___attribute: |
| 1765 | case tok::kw_operator: |
| 1766 | case tok::l_paren: |
| 1767 | case tok::star: |
| 1768 | return true; |
| 1769 | |
| 1770 | case tok::amp: |
| 1771 | case tok::ampamp: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1772 | return getLangOpts().CPlusPlus; |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1773 | |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1774 | 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] | 1775 | return Context == DeclaratorContext::MemberContext && |
| 1776 | getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square); |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1777 | |
| 1778 | 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] | 1779 | return Context == DeclaratorContext::MemberContext || |
| 1780 | getLangOpts().CPlusPlus; |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1781 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1782 | case tok::identifier: |
| 1783 | switch (NextToken().getKind()) { |
| 1784 | case tok::code_completion: |
| 1785 | case tok::coloncolon: |
| 1786 | case tok::comma: |
| 1787 | case tok::equal: |
| 1788 | case tok::equalequal: // Might be a typo for '='. |
| 1789 | case tok::kw_alignas: |
| 1790 | case tok::kw_asm: |
| 1791 | case tok::kw___attribute: |
| 1792 | case tok::l_brace: |
| 1793 | case tok::l_paren: |
| 1794 | case tok::l_square: |
| 1795 | case tok::less: |
| 1796 | case tok::r_brace: |
| 1797 | case tok::r_paren: |
| 1798 | case tok::r_square: |
| 1799 | case tok::semi: |
| 1800 | return true; |
| 1801 | |
| 1802 | case tok::colon: |
| 1803 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1804 | // and in block scope it's probably a label. Inside a class definition, |
| 1805 | // this is a bit-field. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1806 | return Context == DeclaratorContext::MemberContext || |
| 1807 | (getLangOpts().CPlusPlus && |
| 1808 | Context == DeclaratorContext::FileContext); |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1809 | |
| 1810 | case tok::identifier: // Possible virt-specifier. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1811 | return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken()); |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1812 | |
| 1813 | default: |
| 1814 | return false; |
| 1815 | } |
| 1816 | |
| 1817 | default: |
| 1818 | return false; |
| 1819 | } |
| 1820 | } |
| 1821 | |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1822 | /// Skip until we reach something which seems like a sensible place to pick |
| 1823 | /// up parsing after a malformed declaration. This will sometimes stop sooner |
| 1824 | /// than SkipUntil(tok::r_brace) would, but will never stop later. |
| 1825 | void Parser::SkipMalformedDecl() { |
| 1826 | while (true) { |
| 1827 | switch (Tok.getKind()) { |
| 1828 | case tok::l_brace: |
| 1829 | // Skip until matching }, then stop. We've probably skipped over |
| 1830 | // a malformed class or function definition or similar. |
| 1831 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1832 | SkipUntil(tok::r_brace); |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1833 | if (Tok.isOneOf(tok::comma, tok::l_brace, tok::kw_try)) { |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1834 | // This declaration isn't over yet. Keep skipping. |
| 1835 | continue; |
| 1836 | } |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1837 | TryConsumeToken(tok::semi); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1838 | return; |
| 1839 | |
| 1840 | case tok::l_square: |
| 1841 | ConsumeBracket(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1842 | SkipUntil(tok::r_square); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1843 | continue; |
| 1844 | |
| 1845 | case tok::l_paren: |
| 1846 | ConsumeParen(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1847 | SkipUntil(tok::r_paren); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1848 | continue; |
| 1849 | |
| 1850 | case tok::r_brace: |
| 1851 | return; |
| 1852 | |
| 1853 | case tok::semi: |
| 1854 | ConsumeToken(); |
| 1855 | return; |
| 1856 | |
| 1857 | case tok::kw_inline: |
| 1858 | // 'inline namespace' at the start of a line is almost certainly |
Jordan Rose | 12e730c | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1859 | // a good place to pick back up parsing, except in an Objective-C |
| 1860 | // @interface context. |
| 1861 | if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) && |
| 1862 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1863 | return; |
| 1864 | break; |
| 1865 | |
| 1866 | case tok::kw_namespace: |
| 1867 | // 'namespace' at the start of a line is almost certainly a good |
Jordan Rose | 12e730c | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1868 | // place to pick back up parsing, except in an Objective-C |
| 1869 | // @interface context. |
| 1870 | if (Tok.isAtStartOfLine() && |
| 1871 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
| 1872 | return; |
| 1873 | break; |
| 1874 | |
| 1875 | case tok::at: |
| 1876 | // @end is very much like } in Objective-C contexts. |
| 1877 | if (NextToken().isObjCAtKeyword(tok::objc_end) && |
| 1878 | ParsingInObjCContainer) |
| 1879 | return; |
| 1880 | break; |
| 1881 | |
| 1882 | case tok::minus: |
| 1883 | case tok::plus: |
| 1884 | // - and + probably start new method declarations in Objective-C contexts. |
| 1885 | if (Tok.isAtStartOfLine() && ParsingInObjCContainer) |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1886 | return; |
| 1887 | break; |
| 1888 | |
| 1889 | case tok::eof: |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 1890 | case tok::annot_module_begin: |
| 1891 | case tok::annot_module_end: |
| 1892 | case tok::annot_module_include: |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1893 | return; |
| 1894 | |
| 1895 | default: |
| 1896 | break; |
| 1897 | } |
| 1898 | |
| 1899 | ConsumeAnyToken(); |
| 1900 | } |
| 1901 | } |
| 1902 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1903 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1904 | /// definition or a group of object declarations, actually parse the |
| 1905 | /// result. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1906 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1907 | DeclaratorContext Context, |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1908 | SourceLocation *DeclEnd, |
| 1909 | ForRangeInit *FRI) { |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1910 | // Parse the first declarator. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1911 | ParsingDeclarator D(*this, DS, Context); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1912 | ParseDeclarator(D); |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1913 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1914 | // Bail out if the first declarator didn't seem well-formed. |
| 1915 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1916 | SkipMalformedDecl(); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 1917 | return nullptr; |
Chris Lattner | efb0f11 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1918 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1919 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1920 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1921 | // appropriate function scope after the function Decl has been constructed. |
DeLesley Hutchins | 66e300e | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1922 | // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList. |
| 1923 | LateParsedAttrList LateParsedAttrs(true); |
Richard Smith | 99c464c | 2014-11-10 21:10:32 +0000 | [diff] [blame] | 1924 | if (D.isFunctionDeclarator()) { |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1925 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1926 | |
Richard Smith | 99c464c | 2014-11-10 21:10:32 +0000 | [diff] [blame] | 1927 | // The _Noreturn keyword can't appear here, unlike the GNU noreturn |
| 1928 | // attribute. If we find the keyword here, tell the user to put it |
| 1929 | // at the start instead. |
| 1930 | if (Tok.is(tok::kw__Noreturn)) { |
| 1931 | SourceLocation Loc = ConsumeToken(); |
| 1932 | const char *PrevSpec; |
| 1933 | unsigned DiagID; |
| 1934 | |
| 1935 | // We can offer a fixit if it's valid to mark this function as _Noreturn |
| 1936 | // and we don't have any other declarators in this declaration. |
| 1937 | bool Fixit = !DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID); |
| 1938 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1939 | Fixit &= Tok.isOneOf(tok::semi, tok::l_brace, tok::kw_try); |
Richard Smith | 99c464c | 2014-11-10 21:10:32 +0000 | [diff] [blame] | 1940 | |
| 1941 | Diag(Loc, diag::err_c11_noreturn_misplaced) |
| 1942 | << (Fixit ? FixItHint::CreateRemoval(Loc) : FixItHint()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1943 | << (Fixit ? FixItHint::CreateInsertion(D.getBeginLoc(), "_Noreturn ") |
Richard Smith | 99c464c | 2014-11-10 21:10:32 +0000 | [diff] [blame] | 1944 | : FixItHint()); |
| 1945 | } |
| 1946 | } |
| 1947 | |
Chris Lattner | dbb1e93 | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1948 | // 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] | 1949 | if (D.isFunctionDeclarator() && |
Chris Lattner | dbb1e93 | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1950 | // Look at the next token to make sure that this isn't a function |
| 1951 | // declaration. We have to check this because __attribute__ might be the |
| 1952 | // start of a function definition in GCC-extended K&R C. |
Fariborz Jahanian | 712bb81 | 2012-08-10 15:54:40 +0000 | [diff] [blame] | 1953 | !isDeclarationAfterDeclarator()) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1954 | |
Reid Kleckner | d61a311 | 2014-12-15 23:16:32 +0000 | [diff] [blame] | 1955 | // Function definitions are only allowed at file scope and in C++ classes. |
| 1956 | // The C++ inline method definition case is handled elsewhere, so we only |
| 1957 | // need to handle the file scope definition case. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1958 | if (Context == DeclaratorContext::FileContext) { |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1959 | if (isStartOfFunctionDefinition(D)) { |
| 1960 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1961 | Diag(Tok, diag::err_function_declared_typedef); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1962 | |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1963 | // Recover by treating the 'typedef' as spurious. |
| 1964 | DS.ClearStorageClassSpecs(); |
| 1965 | } |
| 1966 | |
| 1967 | Decl *TheDecl = |
| 1968 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
| 1969 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1972 | if (isDeclarationSpecifier()) { |
Nico Weber | 6b05f38 | 2015-02-18 04:53:03 +0000 | [diff] [blame] | 1973 | // If there is an invalid declaration specifier right after the |
| 1974 | // function prototype, then we must be in a missing semicolon case |
| 1975 | // where this isn't actually a body. Just fall through into the code |
| 1976 | // that handles it as a prototype, and let the top-level code handle |
| 1977 | // the erroneous declspec where it would otherwise expect a comma or |
| 1978 | // semicolon. |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1979 | } else { |
| 1980 | Diag(Tok, diag::err_expected_fn_body); |
| 1981 | SkipUntil(tok::semi); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 1982 | return nullptr; |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1983 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1984 | } else { |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1985 | if (Tok.is(tok::l_brace)) { |
| 1986 | Diag(Tok, diag::err_function_definition_not_allowed); |
Serge Pavlov | 1de5151 | 2013-12-09 05:25:47 +0000 | [diff] [blame] | 1987 | SkipMalformedDecl(); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 1988 | return nullptr; |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1989 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1990 | } |
| 1991 | } |
| 1992 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1993 | if (ParseAsmAttributesAfterDeclarator(D)) |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 1994 | return nullptr; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1995 | |
| 1996 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1997 | // must parse and analyze the for-range-initializer before the declaration is |
| 1998 | // analyzed. |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1999 | // |
| 2000 | // Handle the Objective-C for-in loop variable similarly, although we |
| 2001 | // don't need to parse the container in advance. |
| 2002 | if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) { |
| 2003 | bool IsForRangeLoop = false; |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 2004 | if (TryConsumeToken(tok::colon, FRI->ColonLoc)) { |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 2005 | IsForRangeLoop = true; |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 2006 | if (Tok.is(tok::l_brace)) |
| 2007 | FRI->RangeExpr = ParseBraceInitializer(); |
| 2008 | else |
| 2009 | FRI->RangeExpr = ParseExpression(); |
| 2010 | } |
| 2011 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2012 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
George Karpenkov | ec38cf7 | 2018-03-29 00:56:24 +0000 | [diff] [blame] | 2013 | if (IsForRangeLoop) { |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 2014 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
George Karpenkov | ec38cf7 | 2018-03-29 00:56:24 +0000 | [diff] [blame] | 2015 | } else { |
| 2016 | // Obj-C for loop |
| 2017 | if (auto *VD = dyn_cast_or_null<VarDecl>(ThisDecl)) |
| 2018 | VD->setObjCForDecl(true); |
| 2019 | } |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2020 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | cf6e0c8 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 2021 | D.complete(ThisDecl); |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 2022 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2025 | SmallVector<Decl *, 8> DeclsInGroup; |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2026 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes( |
| 2027 | D, ParsedTemplateInfo(), FRI); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2028 | if (LateParsedAttrs.size() > 0) |
| 2029 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2030 | D.complete(FirstDecl); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2031 | if (FirstDecl) |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2032 | DeclsInGroup.push_back(FirstDecl); |
| 2033 | |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2034 | bool ExpectSemi = Context != DeclaratorContext::ForContext; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2035 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2036 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 2037 | // error, bail out. |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 2038 | SourceLocation CommaLoc; |
| 2039 | while (TryConsumeToken(tok::comma, CommaLoc)) { |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 2040 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 2041 | // This comma was followed by a line-break and something which can't be |
| 2042 | // the start of a declarator. The comma was probably a typo for a |
| 2043 | // semicolon. |
| 2044 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 2045 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 2046 | ExpectSemi = false; |
| 2047 | break; |
| 2048 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2049 | |
| 2050 | // Parse the next declarator. |
| 2051 | D.clear(); |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2052 | D.setCommaLoc(CommaLoc); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2053 | |
| 2054 | // Accept attributes in an init-declarator. In the first declarator in a |
| 2055 | // declaration, these would be part of the declspec. In subsequent |
| 2056 | // declarators, they become part of the declarator itself, so that they |
| 2057 | // don't apply to declarators after *this* one. Examples: |
| 2058 | // short __attribute__((common)) var; -> declspec |
| 2059 | // short var __attribute__((common)); -> declarator |
| 2060 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2061 | MaybeParseGNUAttributes(D); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2062 | |
Nico Rieck | eaaae27 | 2014-12-04 23:31:08 +0000 | [diff] [blame] | 2063 | // MSVC parses but ignores qualifiers after the comma as an extension. |
| 2064 | if (getLangOpts().MicrosoftExt) |
| 2065 | DiagnoseAndSkipExtendedMicrosoftTypeAttributes(); |
| 2066 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2067 | ParseDeclarator(D); |
Fariborz Jahanian | 372030b | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 2068 | if (!D.isInvalidType()) { |
| 2069 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 2070 | D.complete(ThisDecl); |
| 2071 | if (ThisDecl) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2072 | DeclsInGroup.push_back(ThisDecl); |
Fariborz Jahanian | 372030b | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 2073 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | if (DeclEnd) |
| 2077 | *DeclEnd = Tok.getLocation(); |
| 2078 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 2079 | if (ExpectSemi && |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2080 | ExpectAndConsumeSemi(Context == DeclaratorContext::FileContext |
Chris Lattner | 02f1b61 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 2081 | ? diag::err_invalid_token_after_toplevel_declarator |
| 2082 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 1390134 | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 2083 | // Okay, there was no semicolon and one was expected. If we see a |
| 2084 | // declaration specifier, just assume it was missing and continue parsing. |
| 2085 | // Otherwise things are very confused and we skip to recover. |
| 2086 | if (!isDeclarationSpecifier()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2087 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 2088 | TryConsumeToken(tok::semi); |
Chris Lattner | 1390134 | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 2089 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 2092 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup); |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2095 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 2096 | /// declarator. Returns true on an error. |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2097 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2098 | // If a simple-asm-expr is present, parse it. |
| 2099 | if (Tok.is(tok::kw_asm)) { |
| 2100 | SourceLocation Loc; |
| 2101 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 2102 | if (AsmLabel.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2103 | SkipUntil(tok::semi, StopBeforeMatch); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2104 | return true; |
| 2105 | } |
| 2106 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2107 | D.setAsmLabel(AsmLabel.get()); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2108 | D.SetRangeEnd(Loc); |
| 2109 | } |
| 2110 | |
| 2111 | MaybeParseGNUAttributes(D); |
| 2112 | return false; |
| 2113 | } |
| 2114 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2115 | /// Parse 'declaration' after parsing 'declaration-specifiers |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2116 | /// declarator'. This method parses the remainder of the declaration |
| 2117 | /// (including any attributes or initializer, among other things) and |
| 2118 | /// finalizes the declaration. |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 2119 | /// |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 2120 | /// init-declarator: [C99 6.7] |
| 2121 | /// declarator |
| 2122 | /// declarator '=' initializer |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 2123 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 2124 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2125 | /// [C++] declarator initializer[opt] |
| 2126 | /// |
| 2127 | /// [C++] initializer: |
| 2128 | /// [C++] '=' initializer-clause |
| 2129 | /// [C++] '(' expression-list ')' |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 2130 | /// [C++0x] '=' 'default' [TODO] |
| 2131 | /// [C++0x] '=' 'delete' |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2132 | /// [C++0x] braced-init-list |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 2133 | /// |
| 2134 | /// According to the standard grammar, =default and =delete are function |
| 2135 | /// definitions, but that definitely doesn't fit with the parser here. |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 2136 | /// |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2137 | Decl *Parser::ParseDeclarationAfterDeclarator( |
| 2138 | Declarator &D, const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2139 | if (ParseAsmAttributesAfterDeclarator(D)) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2140 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2141 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2142 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 2143 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2144 | |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2145 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes( |
| 2146 | Declarator &D, const ParsedTemplateInfo &TemplateInfo, ForRangeInit *FRI) { |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 2147 | // RAII type used to track whether we're inside an initializer. |
| 2148 | struct InitializerScopeRAII { |
| 2149 | Parser &P; |
| 2150 | Declarator &D; |
| 2151 | Decl *ThisDecl; |
| 2152 | |
| 2153 | InitializerScopeRAII(Parser &P, Declarator &D, Decl *ThisDecl) |
| 2154 | : P(P), D(D), ThisDecl(ThisDecl) { |
| 2155 | if (ThisDecl && P.getLangOpts().CPlusPlus) { |
| 2156 | Scope *S = nullptr; |
| 2157 | if (D.getCXXScopeSpec().isSet()) { |
| 2158 | P.EnterScope(0); |
| 2159 | S = P.getCurScope(); |
| 2160 | } |
| 2161 | P.Actions.ActOnCXXEnterDeclInitializer(S, ThisDecl); |
| 2162 | } |
| 2163 | } |
| 2164 | ~InitializerScopeRAII() { pop(); } |
| 2165 | void pop() { |
| 2166 | if (ThisDecl && P.getLangOpts().CPlusPlus) { |
| 2167 | Scope *S = nullptr; |
| 2168 | if (D.getCXXScopeSpec().isSet()) |
| 2169 | S = P.getCurScope(); |
| 2170 | P.Actions.ActOnCXXExitDeclInitializer(S, ThisDecl); |
| 2171 | if (S) |
| 2172 | P.ExitScope(); |
| 2173 | } |
| 2174 | ThisDecl = nullptr; |
| 2175 | } |
| 2176 | }; |
| 2177 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2178 | // Inform the current actions module that we just parsed this declarator. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2179 | Decl *ThisDecl = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2180 | switch (TemplateInfo.Kind) { |
| 2181 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2182 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2183 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2184 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2185 | case ParsedTemplateInfo::Template: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2186 | case ParsedTemplateInfo::ExplicitSpecialization: { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2187 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2188 | *TemplateInfo.TemplateParams, |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2189 | D); |
Larisse Voufo | 833b05a | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 2190 | if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2191 | // Re-direct this decl to refer to the templated decl so that we can |
| 2192 | // initialize it. |
| 2193 | ThisDecl = VT->getTemplatedDecl(); |
| 2194 | break; |
| 2195 | } |
| 2196 | case ParsedTemplateInfo::ExplicitInstantiation: { |
| 2197 | if (Tok.is(tok::semi)) { |
| 2198 | DeclResult ThisRes = Actions.ActOnExplicitInstantiation( |
| 2199 | getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D); |
| 2200 | if (ThisRes.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2201 | SkipUntil(tok::semi, StopBeforeMatch); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2202 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2203 | } |
| 2204 | ThisDecl = ThisRes.get(); |
| 2205 | } else { |
| 2206 | // FIXME: This check should be for a variable template instantiation only. |
| 2207 | |
| 2208 | // Check that this is a valid instantiation |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 2209 | if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2210 | // If the declarator-id is not a template-id, issue a diagnostic and |
| 2211 | // recover by ignoring the 'template' keyword. |
| 2212 | Diag(Tok, diag::err_template_defn_explicit_instantiation) |
| 2213 | << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc); |
| 2214 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 2215 | } else { |
| 2216 | SourceLocation LAngleLoc = |
| 2217 | PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
| 2218 | Diag(D.getIdentifierLoc(), |
| 2219 | diag::err_explicit_instantiation_with_definition) |
| 2220 | << SourceRange(TemplateInfo.TemplateLoc) |
| 2221 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
| 2222 | |
| 2223 | // Recover as if it were an explicit specialization. |
| 2224 | TemplateParameterLists FakedParamLists; |
| 2225 | FakedParamLists.push_back(Actions.ActOnTemplateParameterList( |
Craig Topper | 96225a5 | 2015-12-24 23:58:25 +0000 | [diff] [blame] | 2226 | 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, None, |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 2227 | LAngleLoc, nullptr)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2228 | |
| 2229 | ThisDecl = |
| 2230 | Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D); |
| 2231 | } |
| 2232 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2233 | break; |
| 2234 | } |
| 2235 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2236 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2237 | // Parse declarator '=' initializer. |
Richard Trieu | c64d323 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 2238 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | 4972a6d | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 2239 | if (isTokenEqualOrEqualTypo()) { |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2240 | SourceLocation EqualLoc = ConsumeToken(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2241 | |
Anders Carlsson | 991285e | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 2242 | if (Tok.is(tok::kw_delete)) { |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2243 | if (D.isFunctionDeclarator()) |
| 2244 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 2245 | << 1 /* delete */; |
| 2246 | else |
| 2247 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Alexis Hunt | 5dafebc | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 2248 | } else if (Tok.is(tok::kw_default)) { |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2249 | if (D.isFunctionDeclarator()) |
Sebastian Redl | 46afb55 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 2250 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 2251 | << 0 /* default */; |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2252 | else |
| 2253 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2254 | } else { |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 2255 | InitializerScopeRAII InitScope(*this, D, ThisDecl); |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 2256 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2257 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2258 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Peter Collingbourne | 6b4fdc2 | 2012-07-27 12:56:09 +0000 | [diff] [blame] | 2259 | Actions.FinalizeDeclaration(ThisDecl); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2260 | cutOffParsing(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2261 | return nullptr; |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2262 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2263 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2264 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 2265 | |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2266 | // If this is the only decl in (possibly) range based for statement, |
| 2267 | // our best guess is that the user meant ':' instead of '='. |
| 2268 | if (Tok.is(tok::r_paren) && FRI && D.isFirstDeclarator()) { |
| 2269 | Diag(EqualLoc, diag::err_single_decl_assign_in_for_range) |
| 2270 | << FixItHint::CreateReplacement(EqualLoc, ":"); |
| 2271 | // We are trying to stop parser from looking for ';' in this for |
| 2272 | // statement, therefore preventing spurious errors to be issued. |
| 2273 | FRI->ColonLoc = EqualLoc; |
| 2274 | Init = ExprError(); |
| 2275 | FRI->RangeExpr = Init; |
| 2276 | } |
| 2277 | |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 2278 | InitScope.pop(); |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 2279 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2280 | if (Init.isInvalid()) { |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2281 | SmallVector<tok::TokenKind, 2> StopTokens; |
| 2282 | StopTokens.push_back(tok::comma); |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2283 | if (D.getContext() == DeclaratorContext::ForContext || |
| 2284 | D.getContext() == DeclaratorContext::InitStmtContext) |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2285 | StopTokens.push_back(tok::r_paren); |
| 2286 | SkipUntil(StopTokens, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 604c302 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 2287 | Actions.ActOnInitializerError(ThisDecl); |
| 2288 | } else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2289 | Actions.AddInitializerToDecl(ThisDecl, Init.get(), |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2290 | /*DirectInit=*/false); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2291 | } |
| 2292 | } else if (Tok.is(tok::l_paren)) { |
| 2293 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2294 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2295 | T.consumeOpen(); |
| 2296 | |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2297 | ExprVector Exprs; |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2298 | CommaLocsTy CommaLocs; |
| 2299 | |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 2300 | InitializerScopeRAII InitScope(*this, D, ThisDecl); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 2301 | |
Ilya Biryukov | cbea95d | 2017-09-08 13:36:38 +0000 | [diff] [blame] | 2302 | llvm::function_ref<void()> ExprListCompleter; |
| 2303 | auto ThisVarDecl = dyn_cast_or_null<VarDecl>(ThisDecl); |
| 2304 | auto ConstructorCompleter = [&, ThisVarDecl] { |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 2305 | QualType PreferredType = Actions.ProduceConstructorSignatureHelp( |
Ilya Biryukov | cbea95d | 2017-09-08 13:36:38 +0000 | [diff] [blame] | 2306 | getCurScope(), ThisVarDecl->getType()->getCanonicalTypeInternal(), |
Ilya Biryukov | 2fab235 | 2018-08-30 13:08:03 +0000 | [diff] [blame] | 2307 | ThisDecl->getLocation(), Exprs, T.getOpenLocation()); |
Kadir Cetinkaya | a32d253 | 2018-09-10 13:46:28 +0000 | [diff] [blame] | 2308 | CalledSignatureHelp = true; |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 2309 | Actions.CodeCompleteExpression(getCurScope(), PreferredType); |
Ilya Biryukov | cbea95d | 2017-09-08 13:36:38 +0000 | [diff] [blame] | 2310 | }; |
| 2311 | if (ThisVarDecl) { |
| 2312 | // ParseExpressionList can sometimes succeed even when ThisDecl is not |
| 2313 | // VarDecl. This is an error and it is reported in a call to |
| 2314 | // Actions.ActOnInitializerError(). However, we call |
Ilya Biryukov | 832c4af | 2018-09-07 14:04:39 +0000 | [diff] [blame] | 2315 | // ProduceConstructorSignatureHelp only on VarDecls, falling back to |
| 2316 | // default completer in other cases. |
Ilya Biryukov | cbea95d | 2017-09-08 13:36:38 +0000 | [diff] [blame] | 2317 | ExprListCompleter = ConstructorCompleter; |
| 2318 | } |
| 2319 | |
| 2320 | if (ParseExpressionList(Exprs, CommaLocs, ExprListCompleter)) { |
Kadir Cetinkaya | a32d253 | 2018-09-10 13:46:28 +0000 | [diff] [blame] | 2321 | if (ThisVarDecl && PP.isCodeCompletionReached() && !CalledSignatureHelp) { |
| 2322 | Actions.ProduceConstructorSignatureHelp( |
| 2323 | getCurScope(), ThisVarDecl->getType()->getCanonicalTypeInternal(), |
| 2324 | ThisDecl->getLocation(), Exprs, T.getOpenLocation()); |
| 2325 | CalledSignatureHelp = true; |
| 2326 | } |
David Blaikie | eae0411 | 2012-10-10 23:15:05 +0000 | [diff] [blame] | 2327 | Actions.ActOnInitializerError(ThisDecl); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2328 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2329 | } else { |
| 2330 | // Match the ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2331 | T.consumeClose(); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2332 | |
| 2333 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 2334 | "Unexpected number of commas!"); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 2335 | |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 2336 | InitScope.pop(); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 2337 | |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2338 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 2339 | T.getCloseLocation(), |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2340 | Exprs); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2341 | Actions.AddInitializerToDecl(ThisDecl, Initializer.get(), |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2342 | /*DirectInit=*/true); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2343 | } |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2344 | } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) && |
Fariborz Jahanian | 8be1ecd | 2012-07-03 23:22:13 +0000 | [diff] [blame] | 2345 | (!CurParsedObjCImpl || !D.isFunctionDeclarator())) { |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2346 | // Parse C++0x braced-init-list. |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2347 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 2348 | |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 2349 | InitializerScopeRAII InitScope(*this, D, ThisDecl); |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2350 | |
| 2351 | ExprResult Init(ParseBraceInitializer()); |
| 2352 | |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 2353 | InitScope.pop(); |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2354 | |
| 2355 | if (Init.isInvalid()) { |
| 2356 | Actions.ActOnInitializerError(ThisDecl); |
| 2357 | } else |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2358 | Actions.AddInitializerToDecl(ThisDecl, Init.get(), /*DirectInit=*/true); |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2359 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2360 | } else { |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2361 | Actions.ActOnUninitializedDecl(ThisDecl); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2362 | } |
| 2363 | |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 2364 | Actions.FinalizeDeclaration(ThisDecl); |
| 2365 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2366 | return ThisDecl; |
| 2367 | } |
| 2368 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2369 | /// ParseSpecifierQualifierList |
| 2370 | /// specifier-qualifier-list: |
| 2371 | /// type-specifier specifier-qualifier-list[opt] |
| 2372 | /// type-qualifier specifier-qualifier-list[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 2373 | /// [GNU] attributes specifier-qualifier-list[opt] |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2374 | /// |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2375 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 2376 | DeclSpecContext DSC) { |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2377 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 2378 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2379 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 2380 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2381 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2382 | // Validate declspec for type-name. |
| 2383 | unsigned Specs = DS.getParsedSpecifiers(); |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 2384 | if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) { |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2385 | Diag(Tok, diag::err_expected_type); |
| 2386 | DS.SetTypeSpecError(); |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 2387 | } else if (Specs == DeclSpec::PQ_None && !DS.hasAttributes()) { |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2388 | Diag(Tok, diag::err_typename_requires_specqual); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2389 | if (!DS.hasTypeSpecifier()) |
| 2390 | DS.SetTypeSpecError(); |
| 2391 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2392 | |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 2393 | // Issue diagnostic and remove storage class if present. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2394 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 2395 | if (DS.getStorageClassSpecLoc().isValid()) |
| 2396 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 2397 | else |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2398 | Diag(DS.getThreadStorageClassSpecLoc(), |
| 2399 | diag::err_typename_invalid_storageclass); |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 2400 | DS.ClearStorageClassSpecs(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2401 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2402 | |
Craig Topper | 3f13d4d2 | 2015-11-14 18:15:55 +0000 | [diff] [blame] | 2403 | // Issue diagnostic and remove function specifier if present. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2404 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2405 | if (DS.isInlineSpecified()) |
| 2406 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 2407 | if (DS.isVirtualSpecified()) |
| 2408 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 2409 | if (DS.isExplicitSpecified()) |
| 2410 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 2411 | DS.ClearFunctionSpecs(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2412 | } |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2413 | |
| 2414 | // Issue diagnostic and remove constexpr specfier if present. |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2415 | if (DS.isConstexprSpecified() && DSC != DeclSpecContext::DSC_condition) { |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2416 | Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr); |
| 2417 | DS.ClearConstexprSpec(); |
| 2418 | } |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2419 | } |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 2420 | |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2421 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 2422 | /// specified token is valid after the identifier in a declarator which |
| 2423 | /// immediately follows the declspec. For example, these things are valid: |
| 2424 | /// |
| 2425 | /// int x [ 4]; // direct-declarator |
| 2426 | /// int x ( int y); // direct-declarator |
| 2427 | /// int(int x ) // direct-declarator |
| 2428 | /// int x ; // simple-declaration |
| 2429 | /// int x = 17; // init-declarator-list |
| 2430 | /// int x , y; // init-declarator-list |
| 2431 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | a723ba9 | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 2432 | /// int x : 4; // struct-declarator |
Chris Lattner | 2b988c1 | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 2433 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2434 | /// |
| 2435 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 2436 | /// ')' happens to be valid anyway). |
| 2437 | /// int (x) |
| 2438 | /// |
| 2439 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2440 | return T.isOneOf(tok::l_square, tok::l_paren, tok::r_paren, tok::semi, |
| 2441 | tok::comma, tok::equal, tok::kw_asm, tok::l_brace, |
| 2442 | tok::colon); |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2445 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 2446 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 2447 | /// the declspec has no type specifier. In this case, the declspec is either |
| 2448 | /// malformed or is "implicit int" (in K&R and C89). |
| 2449 | /// |
| 2450 | /// This method handles diagnosing this prettily and returns false if the |
| 2451 | /// declspec is done being processed. If it recovers and thinks there may be |
| 2452 | /// other pieces of declspec after it, it returns true. |
| 2453 | /// |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2454 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2455 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | a0a5d50 | 2014-05-08 22:32:00 +0000 | [diff] [blame] | 2456 | AccessSpecifier AS, DeclSpecContext DSC, |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2457 | ParsedAttributesWithRange &Attrs) { |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2458 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2459 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2460 | SourceLocation Loc = Tok.getLocation(); |
| 2461 | // 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] | 2462 | // parse it as the identifier being declared. However, when a typename |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2463 | // is typo'd or the definition is not included, this will incorrectly |
| 2464 | // parse the typename as the identifier name and fall over misparsing |
| 2465 | // later parts of the diagnostic. |
| 2466 | // |
| 2467 | // As such, we try to do some look-ahead in cases where this would |
| 2468 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 2469 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 2470 | // an identifier with implicit int, we'd get a parse error because the |
| 2471 | // next token is obviously invalid for a type. Parse these as a case |
| 2472 | // with an invalid type specifier. |
| 2473 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2474 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2475 | // 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] | 2476 | // error, do lookahead to try to do better recovery. This never applies |
| 2477 | // within a type specifier. Outside of C++, we allow this even if the |
| 2478 | // language doesn't "officially" support implicit int -- we support |
Richard Smith | 3b87038 | 2013-04-30 22:43:51 +0000 | [diff] [blame] | 2479 | // implicit int as an extension in C99 and C11. |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 2480 | if (!isTypeSpecifier(DSC) && !getLangOpts().CPlusPlus && |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2481 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2482 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 2483 | // we just avoid eating the identifier, so it will be parsed as the |
| 2484 | // identifier in the declarator. |
| 2485 | return false; |
| 2486 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2487 | |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2488 | if (getLangOpts().CPlusPlus && |
| 2489 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| 2490 | // Don't require a type specifier if we have the 'auto' storage class |
| 2491 | // specifier in C++98 -- we'll promote it to a type specifier. |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2492 | if (SS) |
| 2493 | AnnotateScopeToken(*SS, /*IsNewAnnotation*/false); |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2494 | return false; |
| 2495 | } |
| 2496 | |
Reid Kleckner | 9a96e42 | 2016-05-24 21:23:54 +0000 | [diff] [blame] | 2497 | if (getLangOpts().CPlusPlus && (!SS || SS->isEmpty()) && |
| 2498 | getLangOpts().MSVCCompat) { |
| 2499 | // Lookup of an unqualified type name has failed in MSVC compatibility mode. |
| 2500 | // Give Sema a chance to recover if we are in a template with dependent base |
| 2501 | // classes. |
| 2502 | if (ParsedType T = Actions.ActOnMSVCUnknownTypeName( |
| 2503 | *Tok.getIdentifierInfo(), Tok.getLocation(), |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2504 | DSC == DeclSpecContext::DSC_template_type_arg)) { |
Reid Kleckner | 9a96e42 | 2016-05-24 21:23:54 +0000 | [diff] [blame] | 2505 | const char *PrevSpec; |
| 2506 | unsigned DiagID; |
| 2507 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T, |
| 2508 | Actions.getASTContext().getPrintingPolicy()); |
| 2509 | DS.SetRangeEnd(Tok.getLocation()); |
| 2510 | ConsumeToken(); |
| 2511 | return false; |
| 2512 | } |
| 2513 | } |
| 2514 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2515 | // Otherwise, if we don't consume this token, we are going to emit an |
| 2516 | // error anyway. Try to recover from various common problems. Check |
| 2517 | // to see if this was a reference to a tag name without a tag specified. |
| 2518 | // 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] | 2519 | // |
| 2520 | // C++ doesn't need this, and isTagName doesn't take SS. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2521 | if (SS == nullptr) { |
| 2522 | const char *TagName = nullptr, *FixitTagName = nullptr; |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2523 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2524 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2525 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2526 | default: break; |
Argyrios Kyrtzidis | 1f32940 | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2527 | case DeclSpec::TST_enum: |
| 2528 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 2529 | case DeclSpec::TST_union: |
| 2530 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 2531 | case DeclSpec::TST_struct: |
| 2532 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2533 | case DeclSpec::TST_interface: |
| 2534 | TagName="__interface"; FixitTagName = "__interface "; |
| 2535 | TagKind=tok::kw___interface;break; |
Argyrios Kyrtzidis | 1f32940 | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2536 | case DeclSpec::TST_class: |
| 2537 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2538 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2539 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2540 | if (TagName) { |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2541 | IdentifierInfo *TokenName = Tok.getIdentifierInfo(); |
| 2542 | LookupResult R(Actions, TokenName, SourceLocation(), |
| 2543 | Sema::LookupOrdinaryName); |
| 2544 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2545 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2546 | << TokenName << TagName << getLangOpts().CPlusPlus |
| 2547 | << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName); |
| 2548 | |
| 2549 | if (Actions.LookupParsedName(R, getCurScope(), SS)) { |
| 2550 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); |
| 2551 | I != IEnd; ++I) |
Kaelyn Uhrain | 3fe3f85 | 2012-04-27 18:26:49 +0000 | [diff] [blame] | 2552 | Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2553 | << TokenName << TagName; |
| 2554 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2555 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2556 | // Parse this as a tag as if the missing tag were present. |
| 2557 | if (TagKind == tok::kw_enum) |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2558 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, |
| 2559 | DeclSpecContext::DSC_normal); |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2560 | else |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2561 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2562 | /*EnteringContext*/ false, |
| 2563 | DeclSpecContext::DSC_normal, Attrs); |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2564 | return true; |
| 2565 | } |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2566 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2567 | |
Richard Smith | fe904f0 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2568 | // Determine whether this identifier could plausibly be the name of something |
Richard Smith | edd124e | 2012-05-15 21:42:17 +0000 | [diff] [blame] | 2569 | // being declared (with a missing type). |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2570 | if (!isTypeSpecifier(DSC) && (!SS || DSC == DeclSpecContext::DSC_top_level || |
| 2571 | DSC == DeclSpecContext::DSC_class)) { |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2572 | // Look ahead to the next token to try to figure out what this declaration |
| 2573 | // was supposed to be. |
| 2574 | switch (NextToken().getKind()) { |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2575 | case tok::l_paren: { |
| 2576 | // static x(4); // 'x' is not a type |
| 2577 | // x(int n); // 'x' is not a type |
| 2578 | // x (*p)[]; // 'x' is a type |
| 2579 | // |
Richard Smith | a0a5d50 | 2014-05-08 22:32:00 +0000 | [diff] [blame] | 2580 | // Since we're in an error case, we can afford to perform a tentative |
| 2581 | // parse to determine which case we're in. |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2582 | TentativeParsingAction PA(*this); |
| 2583 | ConsumeToken(); |
| 2584 | TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false); |
| 2585 | PA.Revert(); |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2586 | |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 2587 | if (TPR != TPResult::False) { |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2588 | // The identifier is followed by a parenthesized declarator. |
| 2589 | // It's supposed to be a type. |
| 2590 | break; |
| 2591 | } |
| 2592 | |
| 2593 | // If we're in a context where we could be declaring a constructor, |
| 2594 | // check whether this is a constructor declaration with a bogus name. |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2595 | if (DSC == DeclSpecContext::DSC_class || |
| 2596 | (DSC == DeclSpecContext::DSC_top_level && SS)) { |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2597 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2598 | if (Actions.isCurrentClassNameTypo(II, SS)) { |
| 2599 | Diag(Loc, diag::err_constructor_bad_name) |
| 2600 | << Tok.getIdentifierInfo() << II |
| 2601 | << FixItHint::CreateReplacement(Tok.getLocation(), II->getName()); |
| 2602 | Tok.setIdentifierInfo(II); |
| 2603 | } |
| 2604 | } |
| 2605 | // Fall through. |
Galina Kistanova | 7767425 | 2017-06-01 21:15:34 +0000 | [diff] [blame] | 2606 | LLVM_FALLTHROUGH; |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2607 | } |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2608 | case tok::comma: |
| 2609 | case tok::equal: |
| 2610 | case tok::kw_asm: |
| 2611 | case tok::l_brace: |
| 2612 | case tok::l_square: |
| 2613 | case tok::semi: |
| 2614 | // This looks like a variable or function declaration. The type is |
| 2615 | // probably missing. We're done parsing decl-specifiers. |
| 2616 | if (SS) |
| 2617 | AnnotateScopeToken(*SS, /*IsNewAnnotation*/false); |
| 2618 | return false; |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2619 | |
| 2620 | default: |
| 2621 | // This is probably supposed to be a type. This includes cases like: |
| 2622 | // int f(itn); |
| 2623 | // struct S { unsinged : 4; }; |
| 2624 | break; |
| 2625 | } |
| 2626 | } |
| 2627 | |
Reid Kleckner | c05ca5e | 2014-06-19 01:23:22 +0000 | [diff] [blame] | 2628 | // This is almost certainly an invalid type name. Let Sema emit a diagnostic |
| 2629 | // and attempt to recover. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2630 | ParsedType T; |
Kaelyn Uhrain | b5b17fe | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2631 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Richard Smith | 52f8d19 | 2017-05-10 21:32:16 +0000 | [diff] [blame] | 2632 | bool IsTemplateName = getLangOpts().CPlusPlus && NextToken().is(tok::less); |
Reid Kleckner | c05ca5e | 2014-06-19 01:23:22 +0000 | [diff] [blame] | 2633 | Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T, |
Richard Smith | 52f8d19 | 2017-05-10 21:32:16 +0000 | [diff] [blame] | 2634 | IsTemplateName); |
Reid Kleckner | c05ca5e | 2014-06-19 01:23:22 +0000 | [diff] [blame] | 2635 | if (T) { |
| 2636 | // The action has suggested that the type T could be used. Set that as |
| 2637 | // the type in the declaration specifiers, consume the would-be type |
| 2638 | // name token, and we're done. |
| 2639 | const char *PrevSpec; |
| 2640 | unsigned DiagID; |
| 2641 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T, |
| 2642 | Actions.getASTContext().getPrintingPolicy()); |
| 2643 | DS.SetRangeEnd(Tok.getLocation()); |
| 2644 | ConsumeToken(); |
| 2645 | // There may be other declaration specifiers after this. |
| 2646 | return true; |
| 2647 | } else if (II != Tok.getIdentifierInfo()) { |
| 2648 | // If no type was suggested, the correction is to a keyword |
| 2649 | Tok.setKind(II->getTokenID()); |
| 2650 | // There may be other declaration specifiers after this. |
| 2651 | return true; |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2652 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2653 | |
Reid Kleckner | c05ca5e | 2014-06-19 01:23:22 +0000 | [diff] [blame] | 2654 | // 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] | 2655 | DS.SetTypeSpecError(); |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2656 | DS.SetRangeEnd(Tok.getLocation()); |
| 2657 | ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2658 | |
Richard Smith | 52f8d19 | 2017-05-10 21:32:16 +0000 | [diff] [blame] | 2659 | // Eat any following template arguments. |
| 2660 | if (IsTemplateName) { |
| 2661 | SourceLocation LAngle, RAngle; |
| 2662 | TemplateArgList Args; |
| 2663 | ParseTemplateIdAfterTemplateName(true, LAngle, Args, RAngle); |
| 2664 | } |
| 2665 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2666 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 2667 | // avoid rippling error messages on subsequent uses of the same type, |
| 2668 | // could be useful if #include was forgotten. |
| 2669 | return false; |
| 2670 | } |
| 2671 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2672 | /// Determine the declaration specifier context from the declarator |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2673 | /// context. |
| 2674 | /// |
| 2675 | /// \param Context the declarator context, which is one of the |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2676 | /// DeclaratorContext enumerator values. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2677 | Parser::DeclSpecContext |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2678 | Parser::getDeclSpecContextFromDeclaratorContext(DeclaratorContext Context) { |
| 2679 | if (Context == DeclaratorContext::MemberContext) |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2680 | return DeclSpecContext::DSC_class; |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2681 | if (Context == DeclaratorContext::FileContext) |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2682 | return DeclSpecContext::DSC_top_level; |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2683 | if (Context == DeclaratorContext::TemplateParamContext) |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2684 | return DeclSpecContext::DSC_template_param; |
Richard Smith | 77a9c60 | 2018-02-28 03:02:23 +0000 | [diff] [blame] | 2685 | if (Context == DeclaratorContext::TemplateArgContext || |
| 2686 | Context == DeclaratorContext::TemplateTypeArgContext) |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2687 | return DeclSpecContext::DSC_template_type_arg; |
Richard Smith | e303e35 | 2018-02-02 22:24:54 +0000 | [diff] [blame] | 2688 | if (Context == DeclaratorContext::TrailingReturnContext || |
| 2689 | Context == DeclaratorContext::TrailingReturnVarContext) |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2690 | return DeclSpecContext::DSC_trailing; |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2691 | if (Context == DeclaratorContext::AliasDeclContext || |
| 2692 | Context == DeclaratorContext::AliasTemplateContext) |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2693 | return DeclSpecContext::DSC_alias_declaration; |
| 2694 | return DeclSpecContext::DSC_normal; |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2695 | } |
| 2696 | |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2697 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 2698 | /// |
| 2699 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 2700 | /// type. Ideally, the type should be propagated directly into Sema. |
| 2701 | /// |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2702 | /// [C11] type-id |
| 2703 | /// [C11] constant-expression |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2704 | /// [C++0x] type-id ...[opt] |
| 2705 | /// [C++0x] assignment-expression ...[opt] |
| 2706 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 2707 | SourceLocation &EllipsisLoc) { |
| 2708 | ExprResult ER; |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2709 | if (isTypeIdInParens()) { |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2710 | SourceLocation TypeLoc = Tok.getLocation(); |
| 2711 | ParsedType Ty = ParseTypeName().get(); |
| 2712 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2713 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 2714 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2715 | } else |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2716 | ER = ParseConstantExpression(); |
| 2717 | |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 2718 | if (getLangOpts().CPlusPlus11) |
| 2719 | TryConsumeToken(tok::ellipsis, EllipsisLoc); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2720 | |
| 2721 | return ER; |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2722 | } |
| 2723 | |
| 2724 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 2725 | /// attribute to Attrs. |
| 2726 | /// |
| 2727 | /// alignment-specifier: |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2728 | /// [C11] '_Alignas' '(' type-id ')' |
| 2729 | /// [C11] '_Alignas' '(' constant-expression ')' |
Richard Smith | d11c7a1 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2730 | /// [C++11] 'alignas' '(' type-id ...[opt] ')' |
| 2731 | /// [C++11] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2732 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2733 | SourceLocation *EndLoc) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2734 | assert(Tok.isOneOf(tok::kw_alignas, tok::kw__Alignas) && |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2735 | "Not an alignment-specifier!"); |
| 2736 | |
Richard Smith | d11c7a1 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2737 | IdentifierInfo *KWName = Tok.getIdentifierInfo(); |
| 2738 | SourceLocation KWLoc = ConsumeToken(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2739 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2740 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 2741 | if (T.expectAndConsume()) |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2742 | return; |
| 2743 | |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2744 | SourceLocation EllipsisLoc; |
| 2745 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2746 | if (ArgExpr.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2747 | T.skipToEnd(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2748 | return; |
| 2749 | } |
| 2750 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2751 | T.consumeClose(); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2752 | if (EndLoc) |
| 2753 | *EndLoc = T.getCloseLocation(); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2754 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2755 | ArgsVector ArgExprs; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2756 | ArgExprs.push_back(ArgExpr.get()); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2757 | Attrs.addNew(KWName, KWLoc, nullptr, KWLoc, ArgExprs.data(), 1, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 2758 | ParsedAttr::AS_Keyword, EllipsisLoc); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2759 | } |
| 2760 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2761 | /// Determine whether we're looking at something that might be a declarator |
| 2762 | /// in a simple-declaration. If it can't possibly be a declarator, maybe |
| 2763 | /// diagnose a missing semicolon after a prior tag definition in the decl |
| 2764 | /// specifier. |
| 2765 | /// |
| 2766 | /// \return \c true if an error occurred and this can't be any kind of |
| 2767 | /// declaration. |
| 2768 | bool |
| 2769 | Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS, |
| 2770 | DeclSpecContext DSContext, |
| 2771 | LateParsedAttrList *LateAttrs) { |
| 2772 | assert(DS.hasTagDefinition() && "shouldn't call this"); |
| 2773 | |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2774 | bool EnteringContext = (DSContext == DeclSpecContext::DSC_class || |
| 2775 | DSContext == DeclSpecContext::DSC_top_level); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2776 | |
| 2777 | if (getLangOpts().CPlusPlus && |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2778 | Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype, |
| 2779 | tok::annot_template_id) && |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2780 | TryAnnotateCXXScopeToken(EnteringContext)) { |
| 2781 | SkipMalformedDecl(); |
| 2782 | return true; |
| 2783 | } |
| 2784 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2785 | bool HasScope = Tok.is(tok::annot_cxxscope); |
| 2786 | // Make a copy in case GetLookAheadToken invalidates the result of NextToken. |
| 2787 | Token AfterScope = HasScope ? NextToken() : Tok; |
| 2788 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2789 | // Determine whether the following tokens could possibly be a |
| 2790 | // declarator. |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2791 | bool MightBeDeclarator = true; |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2792 | if (Tok.isOneOf(tok::kw_typename, tok::annot_typename)) { |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2793 | // A declarator-id can't start with 'typename'. |
| 2794 | MightBeDeclarator = false; |
| 2795 | } else if (AfterScope.is(tok::annot_template_id)) { |
| 2796 | // If we have a type expressed as a template-id, this cannot be a |
| 2797 | // declarator-id (such a type cannot be redeclared in a simple-declaration). |
| 2798 | TemplateIdAnnotation *Annot = |
| 2799 | static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue()); |
| 2800 | if (Annot->Kind == TNK_Type_template) |
| 2801 | MightBeDeclarator = false; |
| 2802 | } else if (AfterScope.is(tok::identifier)) { |
| 2803 | const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken(); |
| 2804 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2805 | // These tokens cannot come after the declarator-id in a |
| 2806 | // simple-declaration, and are likely to come after a type-specifier. |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2807 | if (Next.isOneOf(tok::star, tok::amp, tok::ampamp, tok::identifier, |
| 2808 | tok::annot_cxxscope, tok::coloncolon)) { |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2809 | // Missing a semicolon. |
| 2810 | MightBeDeclarator = false; |
| 2811 | } else if (HasScope) { |
| 2812 | // If the declarator-id has a scope specifier, it must redeclare a |
| 2813 | // previously-declared entity. If that's a type (and this is not a |
| 2814 | // typedef), that's an error. |
| 2815 | CXXScopeSpec SS; |
| 2816 | Actions.RestoreNestedNameSpecifierAnnotation( |
| 2817 | Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS); |
| 2818 | IdentifierInfo *Name = AfterScope.getIdentifierInfo(); |
| 2819 | Sema::NameClassification Classification = Actions.ClassifyName( |
| 2820 | getCurScope(), SS, Name, AfterScope.getLocation(), Next, |
| 2821 | /*IsAddressOfOperand*/false); |
| 2822 | switch (Classification.getKind()) { |
| 2823 | case Sema::NC_Error: |
| 2824 | SkipMalformedDecl(); |
| 2825 | return true; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2826 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2827 | case Sema::NC_Keyword: |
| 2828 | case Sema::NC_NestedNameSpecifier: |
| 2829 | llvm_unreachable("typo correction and nested name specifiers not " |
| 2830 | "possible here"); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2831 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2832 | case Sema::NC_Type: |
| 2833 | case Sema::NC_TypeTemplate: |
| 2834 | // Not a previously-declared non-type entity. |
| 2835 | MightBeDeclarator = false; |
| 2836 | break; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2837 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2838 | case Sema::NC_Unknown: |
| 2839 | case Sema::NC_Expression: |
| 2840 | case Sema::NC_VarTemplate: |
| 2841 | case Sema::NC_FunctionTemplate: |
| 2842 | // Might be a redeclaration of a prior entity. |
| 2843 | break; |
| 2844 | } |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2845 | } |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2848 | if (MightBeDeclarator) |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2849 | return false; |
| 2850 | |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2851 | const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy(); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2852 | Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getEndLoc()), |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 2853 | diag::err_expected_after) |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2854 | << DeclSpec::getSpecifierName(DS.getTypeSpecType(), PPol) << tok::semi; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2855 | |
| 2856 | // Try to recover from the typo, by dropping the tag definition and parsing |
| 2857 | // the problematic tokens as a type. |
| 2858 | // |
| 2859 | // FIXME: Split the DeclSpec into pieces for the standalone |
| 2860 | // declaration and pieces for the following declaration, instead |
| 2861 | // of assuming that all the other pieces attach to new declaration, |
| 2862 | // and call ParsedFreeStandingDeclSpec as appropriate. |
| 2863 | DS.ClearTypeSpecType(); |
| 2864 | ParsedTemplateInfo NotATemplate; |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 2865 | ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2866 | return false; |
| 2867 | } |
| 2868 | |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 2869 | // Choose the apprpriate diagnostic error for why fixed point types are |
| 2870 | // disabled, set the previous specifier, and mark as invalid. |
| 2871 | static void SetupFixedPointError(const LangOptions &LangOpts, |
| 2872 | const char *&PrevSpec, unsigned &DiagID, |
| 2873 | bool &isInvalid) { |
| 2874 | assert(!LangOpts.FixedPoint); |
| 2875 | DiagID = diag::err_fixed_point_not_enabled; |
| 2876 | PrevSpec = ""; // Not used by diagnostic |
| 2877 | isInvalid = true; |
| 2878 | } |
| 2879 | |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 2880 | /// ParseDeclarationSpecifiers |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2881 | /// declaration-specifiers: [C99 6.7] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2882 | /// storage-class-specifier declaration-specifiers[opt] |
| 2883 | /// type-specifier declaration-specifiers[opt] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2884 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2885 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 2886 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2887 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 2888 | /// [ObjC1] '__kindof' declaration-specifiers[opt] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2889 | /// |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2890 | /// storage-class-specifier: [C99 6.7.1] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 2891 | /// 'typedef' |
| 2892 | /// 'extern' |
| 2893 | /// 'static' |
| 2894 | /// 'auto' |
| 2895 | /// 'register' |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2896 | /// [C++] 'mutable' |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2897 | /// [C++11] 'thread_local' |
| 2898 | /// [C11] '_Thread_local' |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 2899 | /// [GNU] '__thread' |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2900 | /// function-specifier: [C99 6.7.4] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2901 | /// [C99] 'inline' |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2902 | /// [C++] 'virtual' |
| 2903 | /// [C++] 'explicit' |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2904 | /// [OpenCL] '__kernel' |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2905 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 39c2a8b | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2906 | /// 'constexpr': [C++0x dcl.constexpr] |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 2907 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2908 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2909 | AccessSpecifier AS, |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2910 | DeclSpecContext DSContext, |
| 2911 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2912 | if (DS.getSourceRange().isInvalid()) { |
David Majnemer | 24b2830 | 2014-07-26 05:41:31 +0000 | [diff] [blame] | 2913 | // Start the range at the current token but make the end of the range |
| 2914 | // invalid. This will make the entire range invalid unless we successfully |
| 2915 | // consume a token. |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2916 | DS.SetRangeStart(Tok.getLocation()); |
David Majnemer | 24b2830 | 2014-07-26 05:41:31 +0000 | [diff] [blame] | 2917 | DS.SetRangeEnd(SourceLocation()); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2918 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2919 | |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2920 | bool EnteringContext = (DSContext == DeclSpecContext::DSC_class || |
| 2921 | DSContext == DeclSpecContext::DSC_top_level); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2922 | bool AttrsLastTime = false; |
| 2923 | ParsedAttributesWithRange attrs(AttrFactory); |
Benjamin Kramer | e481214 | 2015-03-12 14:28:38 +0000 | [diff] [blame] | 2924 | // We use Sema's policy to get bool macros right. |
Richard Smith | 301bc21 | 2016-05-19 01:39:10 +0000 | [diff] [blame] | 2925 | PrintingPolicy Policy = Actions.getPrintingPolicy(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2926 | while (1) { |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2927 | bool isInvalid = false; |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 2928 | bool isStorageClass = false; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2929 | const char *PrevSpec = nullptr; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2930 | unsigned DiagID = 0; |
| 2931 | |
David Majnemer | 51fd8a0 | 2015-07-22 23:46:18 +0000 | [diff] [blame] | 2932 | // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL |
| 2933 | // implementation for VS2013 uses _Atomic as an identifier for one of the |
| 2934 | // classes in <atomic>. |
| 2935 | // |
| 2936 | // A typedef declaration containing _Atomic<...> is among the places where |
| 2937 | // the class is used. If we are currently parsing such a declaration, treat |
| 2938 | // the token as an identifier. |
| 2939 | if (getLangOpts().MSVCCompat && Tok.is(tok::kw__Atomic) && |
| 2940 | DS.getStorageClassSpec() == clang::DeclSpec::SCS_typedef && |
| 2941 | !DS.hasTypeSpecifier() && GetLookAheadToken(1).is(tok::less)) |
| 2942 | Tok.setKind(tok::identifier); |
| 2943 | |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 2944 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2945 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2946 | switch (Tok.getKind()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2947 | default: |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2948 | DoneWithDeclSpec: |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2949 | if (!AttrsLastTime) |
| 2950 | ProhibitAttributes(attrs); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2951 | else { |
| 2952 | // Reject C++11 attributes that appertain to decl specifiers as |
| 2953 | // we don't support any C++11 attributes that appertain to decl |
| 2954 | // specifiers. This also conforms to what g++ 4.8 is doing. |
Richard Smith | 49cc1cc | 2016-08-18 21:59:42 +0000 | [diff] [blame] | 2955 | ProhibitCXX11Attributes(attrs, diag::err_attribute_not_type_attr); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2956 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2957 | DS.takeAttributesFrom(attrs); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2958 | } |
Peter Collingbourne | 70188b3 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 2959 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2960 | // If this is not a declaration specifier token, we're done reading decl |
| 2961 | // specifiers. First verify that DeclSpec's are consistent. |
Craig Topper | 2512241 | 2015-11-15 03:32:11 +0000 | [diff] [blame] | 2962 | DS.Finish(Actions, Policy); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2963 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2964 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2965 | case tok::l_square: |
| 2966 | case tok::kw_alignas: |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 2967 | if (!standardAttributesAllowed() || !isCXX11AttributeSpecifier()) |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2968 | goto DoneWithDeclSpec; |
| 2969 | |
| 2970 | ProhibitAttributes(attrs); |
| 2971 | // FIXME: It would be good to recover by accepting the attributes, |
| 2972 | // but attempting to do that now would cause serious |
| 2973 | // madness in terms of diagnostics. |
| 2974 | attrs.clear(); |
| 2975 | attrs.Range = SourceRange(); |
| 2976 | |
| 2977 | ParseCXX11Attributes(attrs); |
| 2978 | AttrsLastTime = true; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2979 | continue; |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2980 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2981 | case tok::code_completion: { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2982 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2983 | if (DS.hasTypeSpecifier()) { |
| 2984 | bool AllowNonIdentifiers |
| 2985 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 2986 | Scope::BlockScope | |
| 2987 | Scope::TemplateParamScope | |
| 2988 | Scope::FunctionPrototypeScope | |
| 2989 | Scope::AtCatchScope)) == 0; |
| 2990 | bool AllowNestedNameSpecifiers |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2991 | = DSContext == DeclSpecContext::DSC_top_level || |
| 2992 | (DSContext == DeclSpecContext::DSC_class && DS.isFriendSpecified()); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2993 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2994 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2995 | AllowNonIdentifiers, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2996 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2997 | return cutOffParsing(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 3000 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 3001 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 3002 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 3003 | CCC = DSContext == DeclSpecContext::DSC_class ? Sema::PCC_MemberTemplate |
| 3004 | : Sema::PCC_Template; |
| 3005 | else if (DSContext == DeclSpecContext::DSC_class) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3006 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | b6c6a58 | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 3007 | else if (CurParsedObjCImpl) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3008 | CCC = Sema::PCC_ObjCImplementation; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3009 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3010 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3011 | return cutOffParsing(); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3012 | } |
| 3013 | |
Chris Lattner | bd31aa3 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 3014 | case tok::coloncolon: // ::foo::bar |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3015 | // C++ scope specifier. Annotate and loop, or bail out on error. |
Eli Friedman | 2a1d9a9 | 2013-08-15 23:59:20 +0000 | [diff] [blame] | 3016 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3017 | if (!DS.hasTypeSpecifier()) |
| 3018 | DS.SetTypeSpecError(); |
| 3019 | goto DoneWithDeclSpec; |
| 3020 | } |
John McCall | 8bc2a70 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 3021 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 3022 | goto DoneWithDeclSpec; |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3023 | continue; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3024 | |
| 3025 | case tok::annot_cxxscope: { |
Richard Smith | 3092a3b | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 3026 | if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector()) |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3027 | goto DoneWithDeclSpec; |
| 3028 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 3029 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 3030 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 3031 | Tok.getAnnotationRange(), |
| 3032 | SS); |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 3033 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3034 | // We are looking for a qualified typename. |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 3035 | Token Next = NextToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3036 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 3037 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3038 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 3039 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3040 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3041 | // If this would be a valid constructor declaration with template |
| 3042 | // arguments, we will reject the attempt to form an invalid type-id |
| 3043 | // referring to the injected-class-name when we annotate the token, |
| 3044 | // per C++ [class.qual]p2. |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3045 | // |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3046 | // To improve diagnostics for this case, parse the declaration as a |
| 3047 | // constructor (and reject the extra template arguments later). |
Argyrios Kyrtzidis | c0c5dd2 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 3048 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 3049 | if ((DSContext == DeclSpecContext::DSC_top_level || |
| 3050 | DSContext == DeclSpecContext::DSC_class) && |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 3051 | TemplateId->Name && |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3052 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS) && |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 3053 | isConstructorDeclarator(/*Unqualified*/ false)) { |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3054 | // The user meant this to be an out-of-line constructor |
| 3055 | // definition, but template arguments are not allowed |
| 3056 | // there. Just allow this as a constructor; we'll |
| 3057 | // complain about it later. |
| 3058 | goto DoneWithDeclSpec; |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3059 | } |
| 3060 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 3061 | DS.getTypeSpecScope() = SS; |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3062 | ConsumeAnnotationToken(); // The C++ scope. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3063 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 3064 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3065 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 3066 | continue; |
| 3067 | } |
| 3068 | |
Douglas Gregor | c5790df | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 3069 | if (Next.is(tok::annot_typename)) { |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 3070 | DS.getTypeSpecScope() = SS; |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3071 | ConsumeAnnotationToken(); // The C++ scope. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3072 | if (Tok.getAnnotationValue()) { |
| 3073 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 7743034 | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 3074 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3075 | Tok.getAnnotationEndLoc(), |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3076 | PrevSpec, DiagID, T, Policy); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 3077 | if (isInvalid) |
| 3078 | break; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3079 | } |
Douglas Gregor | c5790df | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 3080 | else |
| 3081 | DS.SetTypeSpecError(); |
| 3082 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3083 | ConsumeAnnotationToken(); // The typename |
Douglas Gregor | c5790df | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 3084 | } |
| 3085 | |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 3086 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3087 | goto DoneWithDeclSpec; |
| 3088 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3089 | // Check whether this is a constructor declaration. If we're in a |
| 3090 | // context where the identifier could be a class name, and it has the |
| 3091 | // shape of a constructor declaration, process it as one. |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 3092 | if ((DSContext == DeclSpecContext::DSC_top_level || |
| 3093 | DSContext == DeclSpecContext::DSC_class) && |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3094 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3095 | &SS) && |
| 3096 | isConstructorDeclarator(/*Unqualified*/ false)) |
| 3097 | goto DoneWithDeclSpec; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3098 | |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 3099 | ParsedType TypeRep = |
| 3100 | Actions.getTypeName(*Next.getIdentifierInfo(), Next.getLocation(), |
| 3101 | getCurScope(), &SS, false, false, nullptr, |
| 3102 | /*IsCtorOrDtorName=*/false, |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 3103 | /*WantNonTrivialSourceInfo=*/true, |
| 3104 | isClassTemplateDeductionContext(DSContext)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3105 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 3106 | // If the referenced identifier is not a type, then this declspec is |
| 3107 | // erroneous: We already checked about that it has no type specifier, and |
| 3108 | // 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] | 3109 | // typename. |
David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 3110 | if (!TypeRep) { |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3111 | // Eat the scope spec so the identifier is current. |
| 3112 | ConsumeAnnotationToken(); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3113 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 3114 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) { |
| 3115 | if (!Attrs.empty()) { |
| 3116 | AttrsLastTime = true; |
| 3117 | attrs.takeAllFrom(Attrs); |
| 3118 | } |
| 3119 | continue; |
| 3120 | } |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3121 | goto DoneWithDeclSpec; |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 3122 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3123 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 3124 | DS.getTypeSpecScope() = SS; |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3125 | ConsumeAnnotationToken(); // The C++ scope. |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3126 | |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3127 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3128 | DiagID, TypeRep, Policy); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3129 | if (isInvalid) |
| 3130 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3131 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3132 | DS.SetRangeEnd(Tok.getLocation()); |
| 3133 | ConsumeToken(); // The typename. |
| 3134 | |
| 3135 | continue; |
| 3136 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3137 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3138 | case tok::annot_typename: { |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 3139 | // If we've previously seen a tag definition, we were almost surely |
| 3140 | // missing a semicolon after it. |
| 3141 | if (DS.hasTypeSpecifier() && DS.hasTagDefinition()) |
| 3142 | goto DoneWithDeclSpec; |
| 3143 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3144 | if (Tok.getAnnotationValue()) { |
| 3145 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 7f8bb36 | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 3146 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3147 | DiagID, T, Policy); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3148 | } else |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 3149 | DS.SetTypeSpecError(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3150 | |
Chris Lattner | 005fc1b | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 3151 | if (isInvalid) |
| 3152 | break; |
| 3153 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3154 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3155 | ConsumeAnnotationToken(); // The typename |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3156 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3157 | continue; |
| 3158 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3159 | |
Douglas Gregor | 0687309 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 3160 | case tok::kw___is_signed: |
| 3161 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 3162 | // typically treats it as a trait. If we see __is_signed as it appears |
| 3163 | // in libstdc++, e.g., |
| 3164 | // |
| 3165 | // static const bool __is_signed; |
| 3166 | // |
| 3167 | // then treat __is_signed as an identifier rather than as a keyword. |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 3168 | if (DS.getTypeSpecType() == TST_bool && |
Douglas Gregor | 0687309 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 3169 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
Alp Toker | 47642d2 | 2013-12-03 06:13:01 +0000 | [diff] [blame] | 3170 | DS.getStorageClassSpec() == DeclSpec::SCS_static) |
| 3171 | TryKeywordIdentFallback(true); |
Douglas Gregor | 0687309 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 3172 | |
| 3173 | // We're done with the declaration-specifiers. |
| 3174 | goto DoneWithDeclSpec; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3175 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3176 | // typedef-name |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 3177 | case tok::kw___super: |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3178 | case tok::kw_decltype: |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3179 | case tok::identifier: { |
Reid Kleckner | c582f01 | 2014-07-14 18:19:58 +0000 | [diff] [blame] | 3180 | // This identifier can only be a typedef name if we haven't already seen |
| 3181 | // a type-specifier. Without this check we misparse: |
| 3182 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 3183 | if (DS.hasTypeSpecifier()) |
| 3184 | goto DoneWithDeclSpec; |
| 3185 | |
Aaron Ballman | 52d0aaa | 2017-02-14 22:47:20 +0000 | [diff] [blame] | 3186 | // If the token is an identifier named "__declspec" and Microsoft |
| 3187 | // extensions are not enabled, it is likely that there will be cascading |
| 3188 | // parse errors if this really is a __declspec attribute. Attempt to |
| 3189 | // recognize that scenario and recover gracefully. |
| 3190 | if (!getLangOpts().DeclSpecKeyword && Tok.is(tok::identifier) && |
| 3191 | Tok.getIdentifierInfo()->getName().equals("__declspec")) { |
| 3192 | Diag(Loc, diag::err_ms_attributes_not_enabled); |
| 3193 | |
| 3194 | // The next token should be an open paren. If it is, eat the entire |
| 3195 | // attribute declaration and continue. |
| 3196 | if (NextToken().is(tok::l_paren)) { |
| 3197 | // Consume the __declspec identifier. |
Richard Trieu | ba05737 | 2017-02-14 23:56:55 +0000 | [diff] [blame] | 3198 | ConsumeToken(); |
Aaron Ballman | 52d0aaa | 2017-02-14 22:47:20 +0000 | [diff] [blame] | 3199 | |
| 3200 | // Eat the parens and everything between them. |
| 3201 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 3202 | if (T.consumeOpen()) { |
| 3203 | assert(false && "Not a left paren?"); |
| 3204 | return; |
| 3205 | } |
| 3206 | T.skipToEnd(); |
| 3207 | continue; |
| 3208 | } |
| 3209 | } |
| 3210 | |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 3211 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 3212 | // so handle it as such. This is important for ctor parsing. |
| 3213 | if (getLangOpts().CPlusPlus) { |
| 3214 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
| 3215 | DS.SetTypeSpecError(); |
| 3216 | goto DoneWithDeclSpec; |
| 3217 | } |
| 3218 | if (!Tok.is(tok::identifier)) |
| 3219 | continue; |
| 3220 | } |
| 3221 | |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3222 | // Check for need to substitute AltiVec keyword tokens. |
| 3223 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 3224 | break; |
| 3225 | |
Richard Smith | 3092a3b | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 3226 | // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not |
| 3227 | // allow the use of a typedef name as a type specifier. |
| 3228 | if (DS.isTypeAltiVecVector()) |
| 3229 | goto DoneWithDeclSpec; |
| 3230 | |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 3231 | if (DSContext == DeclSpecContext::DSC_objc_method_result && |
| 3232 | isObjCInstancetype()) { |
Douglas Gregor | 5c0870a | 2015-06-19 23:18:00 +0000 | [diff] [blame] | 3233 | ParsedType TypeRep = Actions.ActOnObjCInstanceType(Loc); |
| 3234 | assert(TypeRep); |
| 3235 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
| 3236 | DiagID, TypeRep, Policy); |
| 3237 | if (isInvalid) |
| 3238 | break; |
| 3239 | |
| 3240 | DS.SetRangeEnd(Loc); |
| 3241 | ConsumeToken(); |
| 3242 | continue; |
| 3243 | } |
| 3244 | |
Richard Smith | 715ee07 | 2018-06-20 21:58:20 +0000 | [diff] [blame] | 3245 | // If we're in a context where the identifier could be a class name, |
| 3246 | // check whether this is a constructor declaration. |
| 3247 | if (getLangOpts().CPlusPlus && DSContext == DeclSpecContext::DSC_class && |
| 3248 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
| 3249 | isConstructorDeclarator(/*Unqualified*/true)) |
| 3250 | goto DoneWithDeclSpec; |
| 3251 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 3252 | ParsedType TypeRep = Actions.getTypeName( |
| 3253 | *Tok.getIdentifierInfo(), Tok.getLocation(), getCurScope(), nullptr, |
| 3254 | false, false, nullptr, false, false, |
| 3255 | isClassTemplateDeductionContext(DSContext)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3256 | |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 3257 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 3258 | // it must be an implicit int or an error. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3259 | if (!TypeRep) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3260 | ParsedAttributesWithRange Attrs(AttrFactory); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 3261 | if (ParseImplicitInt(DS, nullptr, TemplateInfo, AS, DSContext, Attrs)) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3262 | if (!Attrs.empty()) { |
| 3263 | AttrsLastTime = true; |
| 3264 | attrs.takeAllFrom(Attrs); |
| 3265 | } |
| 3266 | continue; |
| 3267 | } |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3268 | goto DoneWithDeclSpec; |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 3269 | } |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3270 | |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 3271 | // Likewise, if this is a context where the identifier could be a template |
| 3272 | // name, check whether this is a deduction guide declaration. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3273 | if (getLangOpts().CPlusPlus17 && |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 3274 | (DSContext == DeclSpecContext::DSC_class || |
| 3275 | DSContext == DeclSpecContext::DSC_top_level) && |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 3276 | Actions.isDeductionGuideName(getCurScope(), *Tok.getIdentifierInfo(), |
| 3277 | Tok.getLocation()) && |
| 3278 | isConstructorDeclarator(/*Unqualified*/ true, |
| 3279 | /*DeductionGuide*/ true)) |
| 3280 | goto DoneWithDeclSpec; |
| 3281 | |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3282 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3283 | DiagID, TypeRep, Policy); |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3284 | if (isInvalid) |
| 3285 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3286 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3287 | DS.SetRangeEnd(Tok.getLocation()); |
| 3288 | ConsumeToken(); // The identifier |
| 3289 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3290 | // Objective-C supports type arguments and protocol references |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 3291 | // following an Objective-C object or object pointer |
| 3292 | // type. Handle either one of them. |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3293 | if (Tok.is(tok::less) && getLangOpts().ObjC1) { |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 3294 | SourceLocation NewEndLoc; |
| 3295 | TypeResult NewTypeRep = parseObjCTypeArgsAndProtocolQualifiers( |
| 3296 | Loc, TypeRep, /*consumeLastToken=*/true, |
| 3297 | NewEndLoc); |
| 3298 | if (NewTypeRep.isUsable()) { |
| 3299 | DS.UpdateTypeRep(NewTypeRep.get()); |
| 3300 | DS.SetRangeEnd(NewEndLoc); |
| 3301 | } |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3302 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3303 | |
Steve Naroff | cd5e782 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 3304 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 3305 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 3306 | continue; |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3307 | } |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3308 | |
| 3309 | // type-name |
| 3310 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | c0c5dd2 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 3311 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3312 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3313 | // This template-id does not refer to a type name, so we're |
| 3314 | // done with the type-specifiers. |
| 3315 | goto DoneWithDeclSpec; |
| 3316 | } |
| 3317 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3318 | // If we're in a context where the template-id could be a |
| 3319 | // constructor name or specialization, check whether this is a |
| 3320 | // constructor declaration. |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 3321 | if (getLangOpts().CPlusPlus && DSContext == DeclSpecContext::DSC_class && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3322 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 3323 | isConstructorDeclarator(TemplateId->SS.isEmpty())) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3324 | goto DoneWithDeclSpec; |
| 3325 | |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3326 | // Turn the template-id annotation token into a type annotation |
| 3327 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 3328 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3329 | continue; |
| 3330 | } |
| 3331 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 3332 | // GNU attributes support. |
| 3333 | case tok::kw___attribute: |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 3334 | ParseGNUAttributes(DS.getAttributes(), nullptr, LateAttrs); |
Chris Lattner | b95cca0 | 2006-10-17 03:01:08 +0000 | [diff] [blame] | 3335 | continue; |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 3336 | |
| 3337 | // Microsoft declspec support. |
| 3338 | case tok::kw___declspec: |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 3339 | ParseMicrosoftDeclSpecs(DS.getAttributes()); |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 3340 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3341 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3342 | // Microsoft single token adornments. |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 3343 | case tok::kw___forceinline: { |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 3344 | isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID); |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 3345 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 3346 | SourceLocation AttrNameLoc = Tok.getLocation(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 3347 | DS.getAttributes().addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3348 | nullptr, 0, ParsedAttr::AS_Keyword); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 3349 | break; |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 3350 | } |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3351 | |
Andrey Bokhanko | 45d4132 | 2016-05-11 18:38:21 +0000 | [diff] [blame] | 3352 | case tok::kw___unaligned: |
| 3353 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID, |
| 3354 | getLangOpts()); |
| 3355 | break; |
| 3356 | |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 3357 | case tok::kw___sptr: |
| 3358 | case tok::kw___uptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3359 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3360 | case tok::kw___ptr32: |
Steve Naroff | f9c29d4 | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 3361 | case tok::kw___w64: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3362 | case tok::kw___cdecl: |
| 3363 | case tok::kw___stdcall: |
| 3364 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3365 | case tok::kw___thiscall: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3366 | case tok::kw___regcall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 3367 | case tok::kw___vectorcall: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3368 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3369 | continue; |
| 3370 | |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3371 | // Borland single token adornments. |
| 3372 | case tok::kw___pascal: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3373 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3374 | continue; |
| 3375 | |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3376 | // OpenCL single token adornments. |
| 3377 | case tok::kw___kernel: |
Anastasia Stulova | 6bdbcbb | 2016-02-19 18:30:11 +0000 | [diff] [blame] | 3378 | ParseOpenCLKernelAttributes(DS.getAttributes()); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3379 | continue; |
| 3380 | |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 3381 | // Nullability type specifiers. |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 3382 | case tok::kw__Nonnull: |
| 3383 | case tok::kw__Nullable: |
| 3384 | case tok::kw__Null_unspecified: |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 3385 | ParseNullabilityTypeSpecifiers(DS.getAttributes()); |
| 3386 | continue; |
| 3387 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 3388 | // Objective-C 'kindof' types. |
| 3389 | case tok::kw___kindof: |
| 3390 | DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 3391 | nullptr, 0, ParsedAttr::AS_Keyword); |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 3392 | (void)ConsumeToken(); |
| 3393 | continue; |
| 3394 | |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3395 | // storage-class-specifier |
| 3396 | case tok::kw_typedef: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3397 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3398 | PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3399 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3400 | break; |
| 3401 | case tok::kw_extern: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3402 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3403 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3404 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3405 | PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3406 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3407 | break; |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 3408 | case tok::kw___private_extern__: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3409 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3410 | Loc, PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3411 | isStorageClass = true; |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 3412 | break; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3413 | case tok::kw_static: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3414 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3415 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3416 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3417 | PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3418 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3419 | break; |
| 3420 | case tok::kw_auto: |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3421 | if (getLangOpts().CPlusPlus11) { |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3422 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3423 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3424 | PrevSpec, DiagID, Policy); |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3425 | if (!isInvalid) |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 3426 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3427 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 3428 | } else |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3429 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3430 | DiagID, Policy); |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 3431 | } else |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3432 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3433 | PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3434 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3435 | break; |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 3436 | case tok::kw___auto_type: |
| 3437 | Diag(Tok, diag::ext_auto_type); |
| 3438 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto_type, Loc, PrevSpec, |
| 3439 | DiagID, Policy); |
| 3440 | break; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3441 | case tok::kw_register: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3442 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3443 | PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3444 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3445 | break; |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 3446 | case tok::kw_mutable: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3447 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3448 | PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3449 | isStorageClass = true; |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 3450 | break; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3451 | case tok::kw___thread: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3452 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc, |
| 3453 | PrevSpec, DiagID); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3454 | isStorageClass = true; |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3455 | break; |
| 3456 | case tok::kw_thread_local: |
| 3457 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc, |
| 3458 | PrevSpec, DiagID); |
Sven van Haastregt | c410083 | 2018-04-24 14:47:29 +0000 | [diff] [blame] | 3459 | isStorageClass = true; |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3460 | break; |
| 3461 | case tok::kw__Thread_local: |
| 3462 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local, |
| 3463 | Loc, PrevSpec, DiagID); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3464 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3465 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3466 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3467 | // function-specifier |
| 3468 | case tok::kw_inline: |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 3469 | isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3470 | break; |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3471 | case tok::kw_virtual: |
Sven van Haastregt | 49ffffb | 2018-04-23 11:23:47 +0000 | [diff] [blame] | 3472 | // OpenCL C++ v1.0 s2.9: the virtual function qualifier is not supported. |
| 3473 | if (getLangOpts().OpenCLCPlusPlus) { |
| 3474 | DiagID = diag::err_openclcxx_virtual_function; |
| 3475 | PrevSpec = Tok.getIdentifierInfo()->getNameStart(); |
| 3476 | isInvalid = true; |
| 3477 | } |
| 3478 | else { |
| 3479 | isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
| 3480 | } |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3481 | break; |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3482 | case tok::kw_explicit: |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 3483 | isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3484 | break; |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 3485 | case tok::kw__Noreturn: |
| 3486 | if (!getLangOpts().C11) |
| 3487 | Diag(Loc, diag::ext_c11_noreturn); |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 3488 | isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID); |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 3489 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3490 | |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 3491 | // alignment-specifier |
| 3492 | case tok::kw__Alignas: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3493 | if (!getLangOpts().C11) |
Jordan Rose | 58d5472 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 3494 | Diag(Tok, diag::ext_c11_alignment) << Tok.getName(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 3495 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 3496 | continue; |
| 3497 | |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 3498 | // friend |
| 3499 | case tok::kw_friend: |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 3500 | if (DSContext == DeclSpecContext::DSC_class) |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3501 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 3502 | else { |
| 3503 | PrevSpec = ""; // not actually used by the diagnostic |
| 3504 | DiagID = diag::err_friend_invalid_in_context; |
| 3505 | isInvalid = true; |
| 3506 | } |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 3507 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3508 | |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 3509 | // Modules |
| 3510 | case tok::kw___module_private__: |
| 3511 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 3512 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3513 | |
Sebastian Redl | 39c2a8b | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 3514 | // constexpr |
| 3515 | case tok::kw_constexpr: |
| 3516 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 3517 | break; |
| 3518 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3519 | // type-specifier |
| 3520 | case tok::kw_short: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3521 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3522 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3523 | break; |
| 3524 | case tok::kw_long: |
| 3525 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3526 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3527 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3528 | else |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3529 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3530 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3531 | break; |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3532 | case tok::kw___int64: |
| 3533 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3534 | DiagID, Policy); |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3535 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3536 | case tok::kw_signed: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3537 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 3538 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3539 | break; |
| 3540 | case tok::kw_unsigned: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3541 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 3542 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3543 | break; |
| 3544 | case tok::kw__Complex: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3545 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 3546 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3547 | break; |
| 3548 | case tok::kw__Imaginary: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3549 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 3550 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3551 | break; |
| 3552 | case tok::kw_void: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3553 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3554 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3555 | break; |
| 3556 | case tok::kw_char: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3557 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3558 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3559 | break; |
| 3560 | case tok::kw_int: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3561 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3562 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3563 | break; |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3564 | case tok::kw___int128: |
| 3565 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3566 | DiagID, Policy); |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3567 | break; |
| 3568 | case tok::kw_half: |
| 3569 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3570 | DiagID, Policy); |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3571 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3572 | case tok::kw_float: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3573 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3574 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3575 | break; |
| 3576 | case tok::kw_double: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3577 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3578 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3579 | break; |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 3580 | case tok::kw__Float16: |
| 3581 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float16, Loc, PrevSpec, |
| 3582 | DiagID, Policy); |
| 3583 | break; |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 3584 | case tok::kw__Accum: |
| 3585 | if (!getLangOpts().FixedPoint) { |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 3586 | SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid); |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 3587 | } else { |
| 3588 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_accum, Loc, PrevSpec, |
| 3589 | DiagID, Policy); |
| 3590 | } |
| 3591 | break; |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 3592 | case tok::kw__Fract: |
| 3593 | if (!getLangOpts().FixedPoint) { |
| 3594 | SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid); |
| 3595 | } else { |
| 3596 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_fract, Loc, PrevSpec, |
| 3597 | DiagID, Policy); |
| 3598 | } |
| 3599 | break; |
| 3600 | case tok::kw__Sat: |
| 3601 | if (!getLangOpts().FixedPoint) { |
| 3602 | SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid); |
| 3603 | } else { |
| 3604 | isInvalid = DS.SetTypeSpecSat(Loc, PrevSpec, DiagID); |
| 3605 | } |
| 3606 | break; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 3607 | case tok::kw___float128: |
| 3608 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float128, Loc, PrevSpec, |
| 3609 | DiagID, Policy); |
| 3610 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3611 | case tok::kw_wchar_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3612 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3613 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3614 | break; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 3615 | case tok::kw_char8_t: |
| 3616 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char8, Loc, PrevSpec, |
| 3617 | DiagID, Policy); |
| 3618 | break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3619 | case tok::kw_char16_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3620 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3621 | DiagID, Policy); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3622 | break; |
| 3623 | case tok::kw_char32_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3624 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3625 | DiagID, Policy); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3626 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3627 | case tok::kw_bool: |
| 3628 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 20ee5ae | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3629 | if (Tok.is(tok::kw_bool) && |
| 3630 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 3631 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 3632 | PrevSpec = ""; // Not used by the diagnostic. |
| 3633 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | 2b05999 | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3634 | // For better error recovery. |
| 3635 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 20ee5ae | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3636 | isInvalid = true; |
| 3637 | } else { |
| 3638 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3639 | DiagID, Policy); |
Argyrios Kyrtzidis | 20ee5ae | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3640 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3641 | break; |
| 3642 | case tok::kw__Decimal32: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3643 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3644 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3645 | break; |
| 3646 | case tok::kw__Decimal64: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3647 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3648 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3649 | break; |
| 3650 | case tok::kw__Decimal128: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3651 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3652 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3653 | break; |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3654 | case tok::kw___vector: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3655 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy); |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3656 | break; |
| 3657 | case tok::kw___pixel: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3658 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy); |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3659 | break; |
Bill Seurer | cf2c96b | 2015-01-12 19:35:51 +0000 | [diff] [blame] | 3660 | case tok::kw___bool: |
| 3661 | isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy); |
| 3662 | break; |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3663 | case tok::kw_pipe: |
| 3664 | if (!getLangOpts().OpenCL || (getLangOpts().OpenCLVersion < 200)) { |
| 3665 | // OpenCL 2.0 defined this keyword. OpenCL 1.2 and earlier should |
| 3666 | // support the "pipe" word as identifier. |
| 3667 | Tok.getIdentifierInfo()->revertTokenIDToIdentifier(); |
| 3668 | goto DoneWithDeclSpec; |
| 3669 | } |
| 3670 | isInvalid = DS.SetTypePipe(true, Loc, PrevSpec, DiagID, Policy); |
| 3671 | break; |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 3672 | #define GENERIC_IMAGE_TYPE(ImgType, Id) \ |
| 3673 | case tok::kw_##ImgType##_t: \ |
| 3674 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_##ImgType##_t, Loc, PrevSpec, \ |
| 3675 | DiagID, Policy); \ |
| 3676 | break; |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 3677 | #include "clang/Basic/OpenCLImageTypes.def" |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3678 | case tok::kw___unknown_anytype: |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 3679 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3680 | PrevSpec, DiagID, Policy); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3681 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3682 | |
| 3683 | // class-specifier: |
| 3684 | case tok::kw_class: |
| 3685 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3686 | case tok::kw___interface: |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3687 | case tok::kw_union: { |
| 3688 | tok::TokenKind Kind = Tok.getKind(); |
| 3689 | ConsumeToken(); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3690 | |
| 3691 | // These are attributes following class specifiers. |
| 3692 | // To produce better diagnostic, we parse them when |
| 3693 | // parsing class specifier. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3694 | ParsedAttributesWithRange Attributes(AttrFactory); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3695 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3696 | EnteringContext, DSContext, Attributes); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3697 | |
| 3698 | // If there are attributes following class specifier, |
| 3699 | // take them over and handle them here. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3700 | if (!Attributes.empty()) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3701 | AttrsLastTime = true; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3702 | attrs.takeAllFrom(Attributes); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3703 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3704 | continue; |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3705 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3706 | |
| 3707 | // enum-specifier: |
| 3708 | case tok::kw_enum: |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3709 | ConsumeToken(); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3710 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3711 | continue; |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 3712 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3713 | // cv-qualifier: |
| 3714 | case tok::kw_const: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3715 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3716 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3717 | break; |
| 3718 | case tok::kw_volatile: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3719 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3720 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3721 | break; |
| 3722 | case tok::kw_restrict: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3723 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3724 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3725 | break; |
| 3726 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3727 | // C++ typename-specifier: |
| 3728 | case tok::kw_typename: |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3729 | if (TryAnnotateTypeOrScopeToken()) { |
| 3730 | DS.SetTypeSpecError(); |
| 3731 | goto DoneWithDeclSpec; |
| 3732 | } |
| 3733 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3734 | continue; |
| 3735 | break; |
| 3736 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3737 | // GNU typeof support. |
| 3738 | case tok::kw_typeof: |
| 3739 | ParseTypeofSpecifier(DS); |
| 3740 | continue; |
| 3741 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3742 | case tok::annot_decltype: |
Anders Carlsson | 74948d0 | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 3743 | ParseDecltypeSpecifier(DS); |
| 3744 | continue; |
| 3745 | |
David Majnemer | 15b311c | 2016-06-14 03:20:28 +0000 | [diff] [blame] | 3746 | case tok::annot_pragma_pack: |
| 3747 | HandlePragmaPack(); |
| 3748 | continue; |
| 3749 | |
| 3750 | case tok::annot_pragma_ms_pragma: |
| 3751 | HandlePragmaMSPragma(); |
| 3752 | continue; |
| 3753 | |
| 3754 | case tok::annot_pragma_ms_vtordisp: |
| 3755 | HandlePragmaMSVtorDisp(); |
| 3756 | continue; |
| 3757 | |
| 3758 | case tok::annot_pragma_ms_pointers_to_members: |
| 3759 | HandlePragmaMSPointersToMembers(); |
| 3760 | continue; |
| 3761 | |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3762 | case tok::kw___underlying_type: |
| 3763 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3764 | continue; |
| 3765 | |
| 3766 | case tok::kw__Atomic: |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3767 | // C11 6.7.2.4/4: |
| 3768 | // If the _Atomic keyword is immediately followed by a left parenthesis, |
| 3769 | // it is interpreted as a type specifier (with a type name), not as a |
| 3770 | // type qualifier. |
| 3771 | if (NextToken().is(tok::l_paren)) { |
| 3772 | ParseAtomicSpecifier(DS); |
| 3773 | continue; |
| 3774 | } |
| 3775 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 3776 | getLangOpts()); |
| 3777 | break; |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3778 | |
Sven van Haastregt | 2ca6ba1 | 2018-05-09 13:16:17 +0000 | [diff] [blame] | 3779 | // OpenCL access qualifiers: |
| 3780 | case tok::kw___read_only: |
| 3781 | case tok::kw___write_only: |
| 3782 | case tok::kw___read_write: |
| 3783 | // OpenCL C++ 1.0 s2.2: access qualifiers are reserved keywords. |
| 3784 | if (Actions.getLangOpts().OpenCLCPlusPlus) { |
| 3785 | DiagID = diag::err_openclcxx_reserved; |
| 3786 | PrevSpec = Tok.getIdentifierInfo()->getNameStart(); |
| 3787 | isInvalid = true; |
| 3788 | } |
| 3789 | ParseOpenCLQualifiers(DS.getAttributes()); |
| 3790 | break; |
| 3791 | |
| 3792 | // OpenCL address space qualifiers: |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3793 | case tok::kw___generic: |
| 3794 | // generic address space is introduced only in OpenCL v2.0 |
| 3795 | // see OpenCL C Spec v2.0 s6.5.5 |
Sven van Haastregt | 2ca6ba1 | 2018-05-09 13:16:17 +0000 | [diff] [blame] | 3796 | if (Actions.getLangOpts().OpenCLVersion < 200 && |
| 3797 | !Actions.getLangOpts().OpenCLCPlusPlus) { |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3798 | DiagID = diag::err_opencl_unknown_type_specifier; |
| 3799 | PrevSpec = Tok.getIdentifierInfo()->getNameStart(); |
| 3800 | isInvalid = true; |
| 3801 | break; |
| 3802 | }; |
Galina Kistanova | 7767425 | 2017-06-01 21:15:34 +0000 | [diff] [blame] | 3803 | LLVM_FALLTHROUGH; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3804 | case tok::kw___private: |
| 3805 | case tok::kw___global: |
| 3806 | case tok::kw___local: |
| 3807 | case tok::kw___constant: |
Aaron Ballman | 05d76ea | 2014-01-14 01:29:54 +0000 | [diff] [blame] | 3808 | ParseOpenCLQualifiers(DS.getAttributes()); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3809 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3810 | |
Steve Naroff | cfdf616 | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 3811 | case tok::less: |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3812 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3813 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 3814 | // but we support it. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3815 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3816 | goto DoneWithDeclSpec; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3817 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 3818 | SourceLocation StartLoc = Tok.getLocation(); |
| 3819 | SourceLocation EndLoc; |
| 3820 | TypeResult Type = parseObjCProtocolQualifierType(EndLoc); |
| 3821 | if (Type.isUsable()) { |
| 3822 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, StartLoc, |
| 3823 | PrevSpec, DiagID, Type.get(), |
| 3824 | Actions.getASTContext().getPrintingPolicy())) |
| 3825 | Diag(StartLoc, DiagID) << PrevSpec; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3826 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 3827 | DS.SetRangeEnd(EndLoc); |
| 3828 | } else { |
| 3829 | DS.SetTypeSpecError(); |
| 3830 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3831 | |
Douglas Gregor | 06e41ae | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 3832 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 3833 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 3834 | continue; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 3835 | } |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3836 | // If the specifier wasn't legal, issue a diagnostic. |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3837 | if (isInvalid) { |
| 3838 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3839 | assert(DiagID); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3840 | |
Douglas Gregor | a05f5ab | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 3841 | if (DiagID == diag::ext_duplicate_declspec) |
| 3842 | Diag(Tok, DiagID) |
| 3843 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
Anastasia Stulova | 43ab9a0 | 2016-05-12 16:28:25 +0000 | [diff] [blame] | 3844 | else if (DiagID == diag::err_opencl_unknown_type_specifier) { |
Sven van Haastregt | 2ca6ba1 | 2018-05-09 13:16:17 +0000 | [diff] [blame] | 3845 | Diag(Tok, DiagID) << getLangOpts().OpenCLCPlusPlus |
| 3846 | << getLangOpts().getOpenCLVersionTuple().getAsString() |
| 3847 | << PrevSpec << isStorageClass; |
Anastasia Stulova | 43ab9a0 | 2016-05-12 16:28:25 +0000 | [diff] [blame] | 3848 | } else |
Douglas Gregor | a05f5ab | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 3849 | Diag(Tok, DiagID) << PrevSpec; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3850 | } |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3851 | |
Chris Lattner | 2e23209 | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 3852 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | 2b05999 | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3853 | if (DiagID != diag::err_bool_redeclaration) |
Volodymyr Sapsai | 9f7b5cc | 2018-04-10 18:29:47 +0000 | [diff] [blame] | 3854 | // After an error the next token can be an annotation token. |
| 3855 | ConsumeAnyToken(); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3856 | |
| 3857 | AttrsLastTime = false; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 3858 | } |
| 3859 | } |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 3860 | |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3861 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 3862 | /// semicolon. |
| 3863 | /// |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3864 | /// struct-declaration: |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 3865 | /// [C2x] attributes-specifier-seq[opt] |
| 3866 | /// specifier-qualifier-list struct-declarator-list |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 3867 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3868 | /// [GNU] specifier-qualifier-list |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3869 | /// struct-declarator-list: |
| 3870 | /// struct-declarator |
| 3871 | /// struct-declarator-list ',' struct-declarator |
| 3872 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 3873 | /// struct-declarator: |
| 3874 | /// declarator |
| 3875 | /// [GNU] declarator attributes[opt] |
| 3876 | /// declarator[opt] ':' constant-expression |
| 3877 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 3878 | /// |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 3879 | void Parser::ParseStructDeclaration( |
| 3880 | ParsingDeclSpec &DS, |
| 3881 | llvm::function_ref<void(ParsingFieldDeclarator &)> FieldsCallback) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3882 | |
Chris Lattner | f02ef3e | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3883 | if (Tok.is(tok::kw___extension__)) { |
| 3884 | // __extension__ silences extension warnings in the subexpression. |
| 3885 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3886 | ConsumeToken(); |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 3887 | return ParseStructDeclaration(DS, FieldsCallback); |
Chris Lattner | f02ef3e | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3888 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3889 | |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 3890 | // Parse leading attributes. |
| 3891 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 3892 | MaybeParseCXX11Attributes(Attrs); |
| 3893 | DS.takeAttributesFrom(Attrs); |
| 3894 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3895 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3896 | ParseSpecifierQualifierList(DS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3897 | |
Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3898 | // If there are no declarators, this is a free-standing declaration |
| 3899 | // specifier. Let the actions module cope with it. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3900 | if (Tok.is(tok::semi)) { |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 3901 | RecordDecl *AnonRecord = nullptr; |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3902 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 3903 | DS, AnonRecord); |
| 3904 | assert(!AnonRecord && "Did not expect anonymous struct or union here"); |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3905 | DS.complete(TheDecl); |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3906 | return; |
| 3907 | } |
| 3908 | |
| 3909 | // Read struct-declarators until we find the semicolon. |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3910 | bool FirstDeclarator = true; |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3911 | SourceLocation CommaLoc; |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3912 | while (1) { |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3913 | ParsingFieldDeclarator DeclaratorInfo(*this, DS); |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3914 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3915 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3916 | // Attributes are only allowed here on successive declarators. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3917 | if (!FirstDeclarator) |
| 3918 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3919 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3920 | /// struct-declarator: declarator |
| 3921 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | 17c3b1f | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3922 | if (Tok.isNot(tok::colon)) { |
| 3923 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 3924 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3925 | ParseDeclarator(DeclaratorInfo.D); |
Richard Smith | 3d1a94c | 2014-08-12 00:22:39 +0000 | [diff] [blame] | 3926 | } else |
| 3927 | DeclaratorInfo.D.SetIdentifier(nullptr, Tok.getLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3928 | |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 3929 | if (TryConsumeToken(tok::colon)) { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3930 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3931 | if (Res.isInvalid()) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3932 | SkipUntil(tok::semi, StopBeforeMatch); |
Chris Lattner | 32295d3 | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 3933 | else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3934 | DeclaratorInfo.BitfieldSize = Res.get(); |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3935 | } |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3936 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3937 | // If attributes exist after the declarator, parse them. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3938 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3939 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3940 | // We're done with this declarator; invoke the callback. |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 3941 | FieldsCallback(DeclaratorInfo); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3942 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3943 | // If we don't have a comma, it is either the end of the list (a ';') |
| 3944 | // or an error, bail out. |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 3945 | if (!TryConsumeToken(tok::comma, CommaLoc)) |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3946 | return; |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3947 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3948 | FirstDeclarator = false; |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3949 | } |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3950 | } |
| 3951 | |
| 3952 | /// ParseStructUnionBody |
| 3953 | /// struct-contents: |
| 3954 | /// struct-declaration-list |
| 3955 | /// [EXT] empty |
| 3956 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 3957 | /// struct-declaration-list: |
| 3958 | /// struct-declaration |
| 3959 | /// struct-declaration-list struct-declaration |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3960 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3961 | /// |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 3962 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 3963 | unsigned TagType, Decl *TagDecl) { |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 3964 | PrettyDeclStackTraceEntry CrashInfo(Actions.Context, TagDecl, RecordLoc, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3965 | "parsing struct/union body"); |
Andy Gibbs | 22e140b | 2013-04-03 09:31:19 +0000 | [diff] [blame] | 3966 | assert(!getLangOpts().CPlusPlus && "C++ declarations not supported"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3967 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3968 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3969 | if (T.consumeOpen()) |
| 3970 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3971 | |
Douglas Gregor | 658b955 | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 3972 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3973 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3974 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3975 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3976 | |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 3977 | // 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] | 3978 | while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) && |
| 3979 | Tok.isNot(tok::eof)) { |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3980 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3981 | |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 3982 | // Check for extraneous top-level semicolon. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3983 | if (Tok.is(tok::semi)) { |
Richard Smith | 87f5dc5 | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3984 | ConsumeExtraSemi(InsideStruct, TagType); |
Chris Lattner | 36e46a2 | 2007-06-09 05:49:55 +0000 | [diff] [blame] | 3985 | continue; |
| 3986 | } |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3987 | |
Andy Gibbs | c804e08 | 2013-04-03 09:46:04 +0000 | [diff] [blame] | 3988 | // Parse _Static_assert declaration. |
| 3989 | if (Tok.is(tok::kw__Static_assert)) { |
| 3990 | SourceLocation DeclEnd; |
| 3991 | ParseStaticAssertDeclaration(DeclEnd); |
| 3992 | continue; |
| 3993 | } |
| 3994 | |
Argyrios Kyrtzidis | 71c12fb | 2013-04-18 01:42:35 +0000 | [diff] [blame] | 3995 | if (Tok.is(tok::annot_pragma_pack)) { |
| 3996 | HandlePragmaPack(); |
| 3997 | continue; |
| 3998 | } |
| 3999 | |
| 4000 | if (Tok.is(tok::annot_pragma_align)) { |
| 4001 | HandlePragmaAlign(); |
| 4002 | continue; |
| 4003 | } |
| 4004 | |
Alexey Bataev | 4652e4b | 2015-08-12 07:10:54 +0000 | [diff] [blame] | 4005 | if (Tok.is(tok::annot_pragma_openmp)) { |
Alexey Bataev | 587e1de | 2016-03-30 10:43:55 +0000 | [diff] [blame] | 4006 | // Result can be ignored, because it must be always empty. |
| 4007 | AccessSpecifier AS = AS_none; |
| 4008 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 4009 | (void)ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs); |
Alexey Bataev | 4652e4b | 2015-08-12 07:10:54 +0000 | [diff] [blame] | 4010 | continue; |
| 4011 | } |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 4012 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 4013 | if (!Tok.is(tok::at)) { |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 4014 | auto CFieldCallback = [&](ParsingFieldDeclarator &FD) { |
| 4015 | // Install the declarator into the current TagDecl. |
| 4016 | Decl *Field = |
| 4017 | Actions.ActOnField(getCurScope(), TagDecl, |
| 4018 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 4019 | FD.D, FD.BitfieldSize); |
| 4020 | FieldDecls.push_back(Field); |
| 4021 | FD.complete(Field); |
| 4022 | }; |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 4023 | |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 4024 | // Parse all the comma separated declarators. |
| 4025 | ParsingDeclSpec DS(*this); |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 4026 | ParseStructDeclaration(DS, CFieldCallback); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 4027 | } else { // Handle @defs |
| 4028 | ConsumeToken(); |
| 4029 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 4030 | Diag(Tok, diag::err_unexpected_at); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4031 | SkipUntil(tok::semi); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 4032 | continue; |
| 4033 | } |
| 4034 | ConsumeToken(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 4035 | ExpectAndConsume(tok::l_paren); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 4036 | if (!Tok.is(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 4037 | Diag(Tok, diag::err_expected) << tok::identifier; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4038 | SkipUntil(tok::semi); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 4039 | continue; |
| 4040 | } |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4041 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4042 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4043 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 4044 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 4045 | ConsumeToken(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 4046 | ExpectAndConsume(tok::r_paren); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4047 | } |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 4048 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 4049 | if (TryConsumeToken(tok::semi)) |
| 4050 | continue; |
| 4051 | |
| 4052 | if (Tok.is(tok::r_brace)) { |
Chris Lattner | 245c533 | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 4053 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Chris Lattner | 0c7e82d | 2007-06-09 05:54:40 +0000 | [diff] [blame] | 4054 | break; |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 4055 | } |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 4056 | |
| 4057 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 4058 | // Skip to end of block or statement to avoid ext-warning on extra ';'. |
| 4059 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
| 4060 | // If we stopped at a ';', eat it. |
| 4061 | TryConsumeToken(tok::semi); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 4062 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4063 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4064 | T.consumeClose(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4065 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4066 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 4067 | // If attributes exist after struct contents, parse them. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4068 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 15619c7 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 4069 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4070 | Actions.ActOnFields(getCurScope(), RecordLoc, TagDecl, FieldDecls, |
| 4071 | T.getOpenLocation(), T.getCloseLocation(), attrs); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 4072 | StructScope.Exit(); |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 4073 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, T.getRange()); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 4074 | } |
| 4075 | |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 4076 | /// ParseEnumSpecifier |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 4077 | /// enum-specifier: [C99 6.7.2.2] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 4078 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4079 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 4080 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 4081 | /// '}' attributes[opt] |
Aaron Ballman | 9ecff02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 4082 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 4083 | /// '}' |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 4084 | /// 'enum' identifier |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 4085 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4086 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4087 | /// [C++11] enum-head '{' enumerator-list[opt] '}' |
| 4088 | /// [C++11] enum-head '{' enumerator-list ',' '}' |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4089 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4090 | /// enum-head: [C++11] |
| 4091 | /// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt] |
| 4092 | /// enum-key attribute-specifier-seq[opt] nested-name-specifier |
| 4093 | /// identifier enum-base[opt] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4094 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4095 | /// enum-key: [C++11] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4096 | /// 'enum' |
| 4097 | /// 'enum' 'class' |
| 4098 | /// 'enum' 'struct' |
| 4099 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4100 | /// enum-base: [C++11] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4101 | /// ':' type-specifier-seq |
| 4102 | /// |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4103 | /// [C++] elaborated-type-specifier: |
| 4104 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 4105 | /// |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 4106 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | dc70c3a | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 4107 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 4108 | AccessSpecifier AS, DeclSpecContext DSC) { |
Chris Lattner | ffbc271 | 2007-01-25 06:05:38 +0000 | [diff] [blame] | 4109 | // Parse the tag portion of this. |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4110 | if (Tok.is(tok::code_completion)) { |
| 4111 | // Code completion for an enum name. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4112 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 4113 | return cutOffParsing(); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4114 | } |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 4115 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 4116 | // If attributes exist after tag, parse them. |
| 4117 | ParsedAttributesWithRange attrs(AttrFactory); |
| 4118 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4119 | MaybeParseCXX11Attributes(attrs); |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 4120 | MaybeParseMicrosoftDeclSpecs(attrs); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 4121 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4122 | SourceLocation ScopedEnumKWLoc; |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 4123 | bool IsScopedUsingClassTag = false; |
| 4124 | |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 4125 | // In C++11, recognize 'enum class' and 'enum struct'. |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 4126 | if (Tok.isOneOf(tok::kw_class, tok::kw_struct)) { |
Richard Trieu | d0d87b5 | 2013-04-23 02:47:36 +0000 | [diff] [blame] | 4127 | Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum |
| 4128 | : diag::ext_scoped_enum); |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 4129 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4130 | ScopedEnumKWLoc = ConsumeToken(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4131 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4132 | // Attributes are not allowed between these keywords. Diagnose, |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 4133 | // but then just treat them like they appeared in the right place. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 4134 | ProhibitAttributes(attrs); |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 4135 | |
| 4136 | // They are allowed afterwards, though. |
| 4137 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4138 | MaybeParseCXX11Attributes(attrs); |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 4139 | MaybeParseMicrosoftDeclSpecs(attrs); |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 4140 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4141 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4142 | // C++11 [temp.explicit]p12: |
| 4143 | // The usual access controls do not apply to names used to specify |
| 4144 | // explicit instantiations. |
| 4145 | // We extend this to also cover explicit specializations. Note that |
| 4146 | // we don't suppress if this turns out to be an elaborated type |
| 4147 | // specifier. |
| 4148 | bool shouldDelayDiagsInTag = |
| 4149 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 4150 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 4151 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4152 | |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 4153 | // Enum definitions should not be parsed in a trailing-return-type. |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 4154 | bool AllowDeclaration = DSC != DeclSpecContext::DSC_trailing; |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 4155 | |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 4156 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4157 | if (getLangOpts().CPlusPlus) { |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 4158 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 4159 | // if a fixed underlying type is allowed. |
Erik Pilkington | 6f11db1 | 2018-09-28 20:24:58 +0000 | [diff] [blame^] | 4160 | ColonProtectionRAIIObject X(*this, AllowDeclaration); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4161 | |
Nico Weber | cfaa4cd | 2015-02-15 07:26:13 +0000 | [diff] [blame] | 4162 | CXXScopeSpec Spec; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 4163 | if (ParseOptionalCXXScopeSpecifier(Spec, nullptr, |
Richard Smith | 1d4b2e1 | 2013-04-01 21:43:41 +0000 | [diff] [blame] | 4164 | /*EnteringContext=*/true)) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4165 | return; |
| 4166 | |
Nico Weber | cfaa4cd | 2015-02-15 07:26:13 +0000 | [diff] [blame] | 4167 | if (Spec.isSet() && Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 4168 | Diag(Tok, diag::err_expected) << tok::identifier; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4169 | if (Tok.isNot(tok::l_brace)) { |
| 4170 | // Has no name and is not a definition. |
| 4171 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4172 | SkipUntil(tok::comma, StopAtSemi); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4173 | return; |
| 4174 | } |
| 4175 | } |
Nico Weber | cfaa4cd | 2015-02-15 07:26:13 +0000 | [diff] [blame] | 4176 | |
| 4177 | SS = Spec; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4178 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4179 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4180 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 4181 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Erik Pilkington | 6f11db1 | 2018-09-28 20:24:58 +0000 | [diff] [blame^] | 4182 | !(AllowDeclaration && Tok.is(tok::colon))) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 4183 | Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4184 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4185 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4186 | SkipUntil(tok::comma, StopAtSemi); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 4187 | return; |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4188 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4189 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4190 | // If an identifier is present, consume and remember it. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 4191 | IdentifierInfo *Name = nullptr; |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4192 | SourceLocation NameLoc; |
| 4193 | if (Tok.is(tok::identifier)) { |
| 4194 | Name = Tok.getIdentifierInfo(); |
| 4195 | NameLoc = ConsumeToken(); |
| 4196 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4197 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4198 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4199 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 4200 | // declaration of a scoped enumeration. |
| 4201 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4202 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 4203 | IsScopedUsingClassTag = false; |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4204 | } |
| 4205 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4206 | // Okay, end the suppression area. We'll decide whether to emit the |
| 4207 | // diagnostics in a second. |
| 4208 | if (shouldDelayDiagsInTag) |
| 4209 | diagsFromTag.done(); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4210 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4211 | TypeResult BaseType; |
| 4212 | |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4213 | // Parse the fixed underlying type. |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 4214 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
Erik Pilkington | 6f11db1 | 2018-09-28 20:24:58 +0000 | [diff] [blame^] | 4215 | if (AllowDeclaration && Tok.is(tok::colon)) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4216 | bool PossibleBitfield = false; |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 4217 | if (CanBeBitfield) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4218 | // If we're in class scope, this can either be an enum declaration with |
| 4219 | // an underlying type, or a declaration of a bitfield member. We try to |
| 4220 | // use a simple disambiguation scheme first to catch the common cases |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4221 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 4222 | // anything that's a simple-type-specifier followed by '(' as an |
| 4223 | // expression. This suffices because function types are not valid |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4224 | // underlying types anyway. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4225 | EnterExpressionEvaluationContext Unevaluated( |
| 4226 | Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4227 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4228 | // 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] | 4229 | // bit-field. This is the common case. |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 4230 | if (TPR == TPResult::True) |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4231 | PossibleBitfield = true; |
| 4232 | // If the next token starts a type-specifier-seq, it may be either a |
| 4233 | // 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] | 4234 | // 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] | 4235 | // fixed underlying type. |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 4236 | else if (TPR == TPResult::False && |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4237 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 4238 | // Consume the ':'. |
| 4239 | ConsumeToken(); |
| 4240 | } else { |
| 4241 | // We have the start of a type-specifier-seq, so we have to perform |
| 4242 | // tentative parsing to determine whether we have an expression or a |
| 4243 | // type. |
| 4244 | TentativeParsingAction TPA(*this); |
| 4245 | |
| 4246 | // Consume the ':'. |
| 4247 | ConsumeToken(); |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 4248 | |
| 4249 | // If we see a type specifier followed by an open-brace, we have an |
| 4250 | // ambiguity between an underlying type and a C++11 braced |
| 4251 | // function-style cast. Resolve this by always treating it as an |
| 4252 | // underlying type. |
| 4253 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 4254 | // this case. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4255 | if ((getLangOpts().CPlusPlus && |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 4256 | isCXXDeclarationSpecifier(TPResult::True) != TPResult::True) || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4257 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4258 | // We'll parse this as a bitfield later. |
| 4259 | PossibleBitfield = true; |
| 4260 | TPA.Revert(); |
| 4261 | } else { |
| 4262 | // We have a type-specifier-seq. |
| 4263 | TPA.Commit(); |
| 4264 | } |
| 4265 | } |
| 4266 | } else { |
| 4267 | // Consume the ':'. |
| 4268 | ConsumeToken(); |
| 4269 | } |
| 4270 | |
| 4271 | if (!PossibleBitfield) { |
| 4272 | SourceRange Range; |
| 4273 | BaseType = ParseTypeName(&Range); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4274 | |
Erik Pilkington | 6f11db1 | 2018-09-28 20:24:58 +0000 | [diff] [blame^] | 4275 | if (!getLangOpts().ObjC2) { |
| 4276 | if (getLangOpts().CPlusPlus11) |
| 4277 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
| 4278 | else if (getLangOpts().CPlusPlus) |
| 4279 | Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type); |
| 4280 | else if (getLangOpts().MicrosoftExt) |
| 4281 | Diag(StartLoc, diag::ext_ms_c_enum_fixed_underlying_type); |
Eli Friedman | 0d0355ab | 2012-11-02 01:34:28 +0000 | [diff] [blame] | 4282 | else |
Erik Pilkington | 6f11db1 | 2018-09-28 20:24:58 +0000 | [diff] [blame^] | 4283 | Diag(StartLoc, diag::ext_clang_c_enum_fixed_underlying_type); |
Eli Friedman | 0d0355ab | 2012-11-02 01:34:28 +0000 | [diff] [blame] | 4284 | } |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4285 | } |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4286 | } |
| 4287 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4288 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 4289 | // friend declaration, and cannot have an accompanying definition. If we have |
| 4290 | // 'enum foo;', then this is a forward declaration. If we have |
| 4291 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 4292 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4293 | // |
| 4294 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 4295 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 4296 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 4297 | // |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4298 | Sema::TagUseKind TUK; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4299 | if (!AllowDeclaration) { |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 4300 | TUK = Sema::TUK_Reference; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4301 | } else if (Tok.is(tok::l_brace)) { |
| 4302 | if (DS.isFriendSpecified()) { |
| 4303 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
| 4304 | << SourceRange(DS.getFriendSpecLoc()); |
| 4305 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4306 | SkipUntil(tok::r_brace, StopAtSemi); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4307 | TUK = Sema::TUK_Friend; |
| 4308 | } else { |
| 4309 | TUK = Sema::TUK_Definition; |
| 4310 | } |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 4311 | } else if (!isTypeSpecifier(DSC) && |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 4312 | (Tok.is(tok::semi) || |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 4313 | (Tok.isAtStartOfLine() && |
| 4314 | !isValidAfterTypeSpecifier(CanBeBitfield)))) { |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 4315 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
| 4316 | if (Tok.isNot(tok::semi)) { |
| 4317 | // A semicolon was missing after this declaration. Diagnose and recover. |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 4318 | ExpectAndConsume(tok::semi, diag::err_expected_after, "enum"); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 4319 | PP.EnterToken(Tok); |
| 4320 | Tok.setKind(tok::semi); |
| 4321 | } |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4322 | } else { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4323 | TUK = Sema::TUK_Reference; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4324 | } |
| 4325 | |
| 4326 | // If this is an elaborated type specifier, and we delayed |
| 4327 | // diagnostics before, just merge them into the current pool. |
| 4328 | if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) { |
| 4329 | diagsFromTag.redelay(); |
| 4330 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4331 | |
| 4332 | MultiTemplateParamsArg TParams; |
Douglas Gregor | cbbf3e3 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 4333 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4334 | TUK != Sema::TUK_Reference) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4335 | if (!getLangOpts().CPlusPlus11 || !SS.isSet()) { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4336 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 4337 | Diag(Tok, diag::err_enum_template); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4338 | SkipUntil(tok::comma, StopAtSemi); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4339 | return; |
| 4340 | } |
| 4341 | |
| 4342 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 4343 | // Enumerations can't be explicitly instantiated. |
| 4344 | DS.SetTypeSpecError(); |
| 4345 | Diag(StartLoc, diag::err_explicit_instantiation_enum); |
| 4346 | return; |
| 4347 | } |
| 4348 | |
| 4349 | assert(TemplateInfo.TemplateParams && "no template parameters"); |
| 4350 | TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(), |
| 4351 | TemplateInfo.TemplateParams->size()); |
Douglas Gregor | cbbf3e3 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 4352 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4353 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 4354 | if (TUK == Sema::TUK_Reference) |
| 4355 | ProhibitAttributes(attrs); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4356 | |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 4357 | if (!Name && TUK != Sema::TUK_Definition) { |
| 4358 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4359 | |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 4360 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4361 | SkipUntil(tok::comma, StopAtSemi); |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 4362 | return; |
| 4363 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4364 | |
Nico Weber | 32a0fc7 | 2016-09-03 03:01:32 +0000 | [diff] [blame] | 4365 | stripTypeAttributesOffDeclSpec(attrs, DS, TUK); |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 4366 | |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 4367 | Sema::SkipBodyInfo SkipBody; |
| 4368 | if (!Name && TUK == Sema::TUK_Definition && Tok.is(tok::l_brace) && |
| 4369 | NextToken().is(tok::identifier)) |
| 4370 | SkipBody = Actions.shouldSkipAnonEnumBody(getCurScope(), |
| 4371 | NextToken().getIdentifierInfo(), |
| 4372 | NextToken().getLocation()); |
| 4373 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 4374 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4375 | bool IsDependent = false; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 4376 | const char *PrevSpec = nullptr; |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4377 | unsigned DiagID; |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 4378 | Decl *TagDecl = Actions.ActOnTag( |
| 4379 | getCurScope(), DeclSpec::TST_enum, TUK, StartLoc, SS, Name, NameLoc, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4380 | attrs, AS, DS.getModulePrivateSpecLoc(), TParams, Owned, IsDependent, |
| 4381 | ScopedEnumKWLoc, IsScopedUsingClassTag, BaseType, |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 4382 | DSC == DeclSpecContext::DSC_type_specifier, |
| 4383 | DSC == DeclSpecContext::DSC_template_param || |
| 4384 | DSC == DeclSpecContext::DSC_template_type_arg, |
| 4385 | &SkipBody); |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 4386 | |
| 4387 | if (SkipBody.ShouldSkip) { |
| 4388 | assert(TUK == Sema::TUK_Definition && "can only skip a definition"); |
| 4389 | |
| 4390 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 4391 | T.consumeOpen(); |
| 4392 | T.skipToEnd(); |
| 4393 | |
| 4394 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 4395 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 4396 | PrevSpec, DiagID, TagDecl, Owned, |
| 4397 | Actions.getASTContext().getPrintingPolicy())) |
| 4398 | Diag(StartLoc, DiagID) << PrevSpec; |
| 4399 | return; |
| 4400 | } |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4401 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4402 | if (IsDependent) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4403 | // This enum has a dependent nested-name-specifier. Handle it as a |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4404 | // dependent tag. |
| 4405 | if (!Name) { |
| 4406 | DS.SetTypeSpecError(); |
| 4407 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 4408 | return; |
| 4409 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4410 | |
Nico Weber | 83ea012 | 2014-05-03 21:57:40 +0000 | [diff] [blame] | 4411 | TypeResult Type = Actions.ActOnDependentTag( |
| 4412 | getCurScope(), DeclSpec::TST_enum, TUK, SS, Name, StartLoc, NameLoc); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4413 | if (Type.isInvalid()) { |
| 4414 | DS.SetTypeSpecError(); |
| 4415 | return; |
| 4416 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4417 | |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 4418 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 4419 | NameLoc.isValid() ? NameLoc : StartLoc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 4420 | PrevSpec, DiagID, Type.get(), |
| 4421 | Actions.getASTContext().getPrintingPolicy())) |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4422 | Diag(StartLoc, DiagID) << PrevSpec; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4423 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4424 | return; |
| 4425 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4426 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4427 | if (!TagDecl) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4428 | // The action failed to produce an enumeration tag. If this is a |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4429 | // definition, consume the entire definition. |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 4430 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4431 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4432 | SkipUntil(tok::r_brace, StopAtSemi); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4433 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4434 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4435 | DS.SetTypeSpecError(); |
| 4436 | return; |
| 4437 | } |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4438 | |
Bruno Cardoso Lopes | df0ee34 | 2017-07-01 00:06:47 +0000 | [diff] [blame] | 4439 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
| 4440 | Decl *D = SkipBody.CheckSameAsPrevious ? SkipBody.New : TagDecl; |
| 4441 | ParseEnumBody(StartLoc, D); |
| 4442 | if (SkipBody.CheckSameAsPrevious && |
| 4443 | !Actions.ActOnDuplicateDefinition(DS, TagDecl, SkipBody)) { |
| 4444 | DS.SetTypeSpecError(); |
| 4445 | return; |
| 4446 | } |
| 4447 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4448 | |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 4449 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 4450 | NameLoc.isValid() ? NameLoc : StartLoc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 4451 | PrevSpec, DiagID, TagDecl, Owned, |
| 4452 | Actions.getASTContext().getPrintingPolicy())) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4453 | Diag(StartLoc, DiagID) << PrevSpec; |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 4454 | } |
| 4455 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4456 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 4457 | /// enumerator-list: |
| 4458 | /// enumerator |
| 4459 | /// enumerator-list ',' enumerator |
| 4460 | /// enumerator: |
Aaron Ballman | 730476b | 2014-11-08 15:33:35 +0000 | [diff] [blame] | 4461 | /// enumeration-constant attributes[opt] |
| 4462 | /// enumeration-constant attributes[opt] '=' constant-expression |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4463 | /// enumeration-constant: |
| 4464 | /// identifier |
| 4465 | /// |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4466 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4467 | // Enter the scope of the enum body and start the definition. |
Hans Wennborg | fe78145 | 2014-06-17 00:00:18 +0000 | [diff] [blame] | 4468 | ParseScope EnumScope(this, Scope::DeclScope | Scope::EnumScope); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4469 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4470 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4471 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 4472 | T.consumeOpen(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4473 | |
Chris Lattner | 37256fb | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 4474 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4475 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 4476 | Diag(Tok, diag::err_empty_enum); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4477 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4478 | SmallVector<Decl *, 32> EnumConstantDecls; |
Jordan Rose | 60ac316 | 2015-04-30 17:20:30 +0000 | [diff] [blame] | 4479 | SmallVector<SuppressAccessChecks, 32> EnumAvailabilityDiags; |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4480 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 4481 | Decl *LastEnumConstDecl = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4482 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4483 | // Parse the enumerator-list. |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4484 | while (Tok.isNot(tok::r_brace)) { |
| 4485 | // Parse enumerator. If failed, try skipping till the start of the next |
| 4486 | // enumerator definition. |
| 4487 | if (Tok.isNot(tok::identifier)) { |
| 4488 | Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; |
| 4489 | if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) && |
| 4490 | TryConsumeToken(tok::comma)) |
| 4491 | continue; |
| 4492 | break; |
| 4493 | } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4494 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 4495 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4496 | |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 4497 | // If attributes exist after the enumerator, parse them. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 4498 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4499 | MaybeParseGNUAttributes(attrs); |
Aaron Ballman | 730476b | 2014-11-08 15:33:35 +0000 | [diff] [blame] | 4500 | ProhibitAttributes(attrs); // GNU-style attributes are prohibited. |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 4501 | if (standardAttributesAllowed() && isCXX11AttributeSpecifier()) { |
| 4502 | if (getLangOpts().CPlusPlus) |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4503 | Diag(Tok.getLocation(), getLangOpts().CPlusPlus17 |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 4504 | ? diag::warn_cxx14_compat_ns_enum_attribute |
| 4505 | : diag::ext_ns_enum_attribute) |
| 4506 | << 1 /*enumerator*/; |
Aaron Ballman | 730476b | 2014-11-08 15:33:35 +0000 | [diff] [blame] | 4507 | ParseCXX11Attributes(attrs); |
| 4508 | } |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 4509 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4510 | SourceLocation EqualLoc; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4511 | ExprResult AssignedVal; |
Jordan Rose | 60ac316 | 2015-04-30 17:20:30 +0000 | [diff] [blame] | 4512 | EnumAvailabilityDiags.emplace_back(*this); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4513 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 4514 | if (TryConsumeToken(tok::equal, EqualLoc)) { |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 4515 | AssignedVal = ParseConstantExpression(); |
| 4516 | if (AssignedVal.isInvalid()) |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4517 | SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch); |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4518 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4519 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4520 | // Install the enumerator constant into EnumDecl. |
Bruno Cardoso Lopes | df0ee34 | 2017-07-01 00:06:47 +0000 | [diff] [blame] | 4521 | Decl *EnumConstDecl = Actions.ActOnEnumConstant( |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4522 | getCurScope(), EnumDecl, LastEnumConstDecl, IdentLoc, Ident, attrs, |
| 4523 | EqualLoc, AssignedVal.get()); |
Jordan Rose | 60ac316 | 2015-04-30 17:20:30 +0000 | [diff] [blame] | 4524 | EnumAvailabilityDiags.back().done(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4525 | |
Chris Lattner | 4ef4001 | 2007-06-11 01:28:17 +0000 | [diff] [blame] | 4526 | EnumConstantDecls.push_back(EnumConstDecl); |
| 4527 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4528 | |
Douglas Gregor | ce66d02 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 4529 | if (Tok.is(tok::identifier)) { |
| 4530 | // We're missing a comma between enumerators. |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 4531 | SourceLocation Loc = getEndOfPreviousToken(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4532 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
Douglas Gregor | ce66d02 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 4533 | << FixItHint::CreateInsertion(Loc, ", "); |
| 4534 | continue; |
| 4535 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4536 | |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4537 | // Emumerator definition must be finished, only comma or r_brace are |
| 4538 | // allowed here. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 4539 | SourceLocation CommaLoc; |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4540 | if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) { |
| 4541 | if (EqualLoc.isValid()) |
| 4542 | Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace |
| 4543 | << tok::comma; |
| 4544 | else |
| 4545 | Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator); |
| 4546 | if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) { |
| 4547 | if (TryConsumeToken(tok::comma, CommaLoc)) |
| 4548 | continue; |
| 4549 | } else { |
| 4550 | break; |
| 4551 | } |
| 4552 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4553 | |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4554 | // If comma is followed by r_brace, emit appropriate warning. |
| 4555 | if (Tok.is(tok::r_brace) && CommaLoc.isValid()) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4556 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11) |
Richard Smith | 87f5dc5 | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 4557 | Diag(CommaLoc, getLangOpts().CPlusPlus ? |
| 4558 | diag::ext_enumerator_list_comma_cxx : |
| 4559 | diag::ext_enumerator_list_comma_c) |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4560 | << FixItHint::CreateRemoval(CommaLoc); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4561 | else if (getLangOpts().CPlusPlus11) |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4562 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 4563 | << FixItHint::CreateRemoval(CommaLoc); |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4564 | break; |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4565 | } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4566 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4567 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4568 | // Eat the }. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4569 | T.consumeClose(); |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4570 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4571 | // If attributes exist after the identifier list, parse them. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4572 | ParsedAttributes attrs(AttrFactory); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4573 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 4574 | |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 4575 | Actions.ActOnEnumBody(StartLoc, T.getRange(), EnumDecl, EnumConstantDecls, |
| 4576 | getCurScope(), attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4577 | |
Jordan Rose | 60ac316 | 2015-04-30 17:20:30 +0000 | [diff] [blame] | 4578 | // Now handle enum constant availability diagnostics. |
| 4579 | assert(EnumConstantDecls.size() == EnumAvailabilityDiags.size()); |
| 4580 | for (size_t i = 0, e = EnumConstantDecls.size(); i != e; ++i) { |
| 4581 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
| 4582 | EnumAvailabilityDiags[i].redelay(); |
| 4583 | PD.complete(EnumConstantDecls[i]); |
| 4584 | } |
| 4585 | |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 4586 | EnumScope.Exit(); |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 4587 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, T.getRange()); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 4588 | |
| 4589 | // The next token must be valid after an enum definition. If not, a ';' |
| 4590 | // was probably forgotten. |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 4591 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
| 4592 | if (!isValidAfterTypeSpecifier(CanBeBitfield)) { |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 4593 | ExpectAndConsume(tok::semi, diag::err_expected_after, "enum"); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 4594 | // Push this token back into the preprocessor and change our current token |
| 4595 | // to ';' so that the rest of the code recovers as though there were an |
| 4596 | // ';' after the definition. |
| 4597 | PP.EnterToken(Tok); |
| 4598 | Tok.setKind(tok::semi); |
| 4599 | } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4600 | } |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 4601 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4602 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 4603 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 4604 | /// specifier or if we're not sure. |
| 4605 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 4606 | switch (Tok.getKind()) { |
| 4607 | default: return false; |
| 4608 | // type-specifiers |
| 4609 | case tok::kw_short: |
| 4610 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4611 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4612 | case tok::kw___int128: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4613 | case tok::kw_signed: |
| 4614 | case tok::kw_unsigned: |
| 4615 | case tok::kw__Complex: |
| 4616 | case tok::kw__Imaginary: |
| 4617 | case tok::kw_void: |
| 4618 | case tok::kw_char: |
| 4619 | case tok::kw_wchar_t: |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 4620 | case tok::kw_char8_t: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4621 | case tok::kw_char16_t: |
| 4622 | case tok::kw_char32_t: |
| 4623 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4624 | case tok::kw_half: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4625 | case tok::kw_float: |
| 4626 | case tok::kw_double: |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 4627 | case tok::kw__Accum: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 4628 | case tok::kw__Fract: |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 4629 | case tok::kw__Float16: |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 4630 | case tok::kw___float128: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4631 | case tok::kw_bool: |
| 4632 | case tok::kw__Bool: |
| 4633 | case tok::kw__Decimal32: |
| 4634 | case tok::kw__Decimal64: |
| 4635 | case tok::kw__Decimal128: |
| 4636 | case tok::kw___vector: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 4637 | #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 4638 | #include "clang/Basic/OpenCLImageTypes.def" |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4639 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4640 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4641 | case tok::kw_class: |
| 4642 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4643 | case tok::kw___interface: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4644 | case tok::kw_union: |
| 4645 | // enum-specifier |
| 4646 | case tok::kw_enum: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4647 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4648 | // typedef-name |
| 4649 | case tok::annot_typename: |
| 4650 | return true; |
| 4651 | } |
| 4652 | } |
| 4653 | |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 4654 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4655 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4656 | bool Parser::isTypeSpecifierQualifier() { |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4657 | switch (Tok.getKind()) { |
| 4658 | default: return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4659 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4660 | case tok::identifier: // foo::bar |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4661 | if (TryAltiVecVectorToken()) |
| 4662 | return true; |
| 4663 | // Fall through. |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4664 | case tok::kw_typename: // typename T::type |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4665 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4666 | // recurse to handle whatever we get. |
| 4667 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4668 | return true; |
| 4669 | if (Tok.is(tok::identifier)) |
| 4670 | return false; |
| 4671 | return isTypeSpecifierQualifier(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4672 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4673 | case tok::coloncolon: // ::foo::bar |
| 4674 | if (NextToken().is(tok::kw_new) || // ::new |
| 4675 | NextToken().is(tok::kw_delete)) // ::delete |
| 4676 | return false; |
| 4677 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4678 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4679 | return true; |
| 4680 | return isTypeSpecifierQualifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4681 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 4682 | // GNU attributes support. |
| 4683 | case tok::kw___attribute: |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4684 | // GNU typeof support. |
| 4685 | case tok::kw_typeof: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4686 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4687 | // type-specifiers |
| 4688 | case tok::kw_short: |
| 4689 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4690 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4691 | case tok::kw___int128: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4692 | case tok::kw_signed: |
| 4693 | case tok::kw_unsigned: |
| 4694 | case tok::kw__Complex: |
| 4695 | case tok::kw__Imaginary: |
| 4696 | case tok::kw_void: |
| 4697 | case tok::kw_char: |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4698 | case tok::kw_wchar_t: |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 4699 | case tok::kw_char8_t: |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4700 | case tok::kw_char16_t: |
| 4701 | case tok::kw_char32_t: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4702 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4703 | case tok::kw_half: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4704 | case tok::kw_float: |
| 4705 | case tok::kw_double: |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 4706 | case tok::kw__Accum: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 4707 | case tok::kw__Fract: |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 4708 | case tok::kw__Float16: |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 4709 | case tok::kw___float128: |
Chris Lattner | bb31a42 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 4710 | case tok::kw_bool: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4711 | case tok::kw__Bool: |
| 4712 | case tok::kw__Decimal32: |
| 4713 | case tok::kw__Decimal64: |
| 4714 | case tok::kw__Decimal128: |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4715 | case tok::kw___vector: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 4716 | #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 4717 | #include "clang/Basic/OpenCLImageTypes.def" |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4718 | |
Chris Lattner | 861a226 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 4719 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4720 | case tok::kw_class: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4721 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4722 | case tok::kw___interface: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4723 | case tok::kw_union: |
| 4724 | // enum-specifier |
| 4725 | case tok::kw_enum: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4726 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4727 | // type-qualifier |
| 4728 | case tok::kw_const: |
| 4729 | case tok::kw_volatile: |
| 4730 | case tok::kw_restrict: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 4731 | case tok::kw__Sat: |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4732 | |
John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4733 | // Debugger support. |
| 4734 | case tok::kw___unknown_anytype: |
| 4735 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4736 | // typedef-name |
Chris Lattner | a8a3f73 | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 4737 | case tok::annot_typename: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4738 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4739 | |
Chris Lattner | 409bf7d | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 4740 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4741 | case tok::less: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4742 | return getLangOpts().ObjC1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4743 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4744 | case tok::kw___cdecl: |
| 4745 | case tok::kw___stdcall: |
| 4746 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4747 | case tok::kw___thiscall: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4748 | case tok::kw___regcall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4749 | case tok::kw___vectorcall: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4750 | case tok::kw___w64: |
| 4751 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4752 | case tok::kw___ptr32: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4753 | case tok::kw___pascal: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4754 | case tok::kw___unaligned: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4755 | |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 4756 | case tok::kw__Nonnull: |
| 4757 | case tok::kw__Nullable: |
| 4758 | case tok::kw__Null_unspecified: |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 4759 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4760 | case tok::kw___kindof: |
| 4761 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4762 | case tok::kw___private: |
| 4763 | case tok::kw___local: |
| 4764 | case tok::kw___global: |
| 4765 | case tok::kw___constant: |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 4766 | case tok::kw___generic: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4767 | case tok::kw___read_only: |
| 4768 | case tok::kw___read_write: |
| 4769 | case tok::kw___write_only: |
| 4770 | |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4771 | return true; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4772 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4773 | // C11 _Atomic |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4774 | case tok::kw__Atomic: |
| 4775 | return true; |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4776 | } |
| 4777 | } |
| 4778 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4779 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 4780 | /// declaration specifier. |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4781 | /// |
| 4782 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 4783 | /// this check is to disambiguate between an expression and a declaration. |
| 4784 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4785 | switch (Tok.getKind()) { |
| 4786 | default: return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4787 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 4788 | case tok::kw_pipe: |
| 4789 | return getLangOpts().OpenCL && (getLangOpts().OpenCLVersion >= 200); |
| 4790 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4791 | case tok::identifier: // foo::bar |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4792 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4793 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4794 | return false; |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4795 | if (TryAltiVecVectorToken()) |
| 4796 | return true; |
| 4797 | // Fall through. |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4798 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4799 | case tok::kw_typename: // typename T::type |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4800 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4801 | // recurse to handle whatever we get. |
| 4802 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4803 | return true; |
| 4804 | if (Tok.is(tok::identifier)) |
| 4805 | return false; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4806 | |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4807 | // 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] | 4808 | // by an identifier and then either ':' or ']', in a place where an |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4809 | // expression is permitted, then this is probably a class message send |
| 4810 | // missing the initial '['. In this case, we won't consider this to be |
| 4811 | // the start of a declaration. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4812 | if (DisambiguatingWithExpression && |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4813 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 4814 | return false; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4815 | |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4816 | return isDeclarationSpecifier(); |
| 4817 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4818 | case tok::coloncolon: // ::foo::bar |
| 4819 | if (NextToken().is(tok::kw_new) || // ::new |
| 4820 | NextToken().is(tok::kw_delete)) // ::delete |
| 4821 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4822 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4823 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4824 | // recurse to handle whatever we get. |
| 4825 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4826 | return true; |
| 4827 | return isDeclarationSpecifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4828 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4829 | // storage-class-specifier |
| 4830 | case tok::kw_typedef: |
| 4831 | case tok::kw_extern: |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 4832 | case tok::kw___private_extern__: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4833 | case tok::kw_static: |
| 4834 | case tok::kw_auto: |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4835 | case tok::kw___auto_type: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4836 | case tok::kw_register: |
| 4837 | case tok::kw___thread: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4838 | case tok::kw_thread_local: |
| 4839 | case tok::kw__Thread_local: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4840 | |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 4841 | // Modules |
| 4842 | case tok::kw___module_private__: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4843 | |
John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4844 | // Debugger support |
| 4845 | case tok::kw___unknown_anytype: |
| 4846 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4847 | // type-specifiers |
| 4848 | case tok::kw_short: |
| 4849 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4850 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4851 | case tok::kw___int128: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4852 | case tok::kw_signed: |
| 4853 | case tok::kw_unsigned: |
| 4854 | case tok::kw__Complex: |
| 4855 | case tok::kw__Imaginary: |
| 4856 | case tok::kw_void: |
| 4857 | case tok::kw_char: |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4858 | case tok::kw_wchar_t: |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 4859 | case tok::kw_char8_t: |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4860 | case tok::kw_char16_t: |
| 4861 | case tok::kw_char32_t: |
| 4862 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4863 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4864 | case tok::kw_half: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4865 | case tok::kw_float: |
| 4866 | case tok::kw_double: |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 4867 | case tok::kw__Accum: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 4868 | case tok::kw__Fract: |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 4869 | case tok::kw__Float16: |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 4870 | case tok::kw___float128: |
Chris Lattner | bb31a42 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 4871 | case tok::kw_bool: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4872 | case tok::kw__Bool: |
| 4873 | case tok::kw__Decimal32: |
| 4874 | case tok::kw__Decimal64: |
| 4875 | case tok::kw__Decimal128: |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4876 | case tok::kw___vector: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4877 | |
Chris Lattner | 861a226 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 4878 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4879 | case tok::kw_class: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4880 | case tok::kw_struct: |
| 4881 | case tok::kw_union: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4882 | case tok::kw___interface: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4883 | // enum-specifier |
| 4884 | case tok::kw_enum: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4885 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4886 | // type-qualifier |
| 4887 | case tok::kw_const: |
| 4888 | case tok::kw_volatile: |
| 4889 | case tok::kw_restrict: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 4890 | case tok::kw__Sat: |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4891 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4892 | // function-specifier |
| 4893 | case tok::kw_inline: |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4894 | case tok::kw_virtual: |
| 4895 | case tok::kw_explicit: |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4896 | case tok::kw__Noreturn: |
Chris Lattner | 7b20dc7 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4897 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 4898 | // alignment-specifier |
| 4899 | case tok::kw__Alignas: |
| 4900 | |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4901 | // friend keyword. |
| 4902 | case tok::kw_friend: |
| 4903 | |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 4904 | // static_assert-declaration |
| 4905 | case tok::kw__Static_assert: |
| 4906 | |
Chris Lattner | 599e47e | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4907 | // GNU typeof support. |
| 4908 | case tok::kw_typeof: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4909 | |
Chris Lattner | 599e47e | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4910 | // GNU attributes. |
Chris Lattner | 7b20dc7 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4911 | case tok::kw___attribute: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4912 | |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4913 | // C++11 decltype and constexpr. |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4914 | case tok::annot_decltype: |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4915 | case tok::kw_constexpr: |
Francois Pichet | e878cb6 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 4916 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4917 | // C11 _Atomic |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4918 | case tok::kw__Atomic: |
| 4919 | return true; |
| 4920 | |
Chris Lattner | 8b2ec16 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 4921 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4922 | case tok::less: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4923 | return getLangOpts().ObjC1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4924 | |
Douglas Gregor | 19b7acf | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 4925 | // typedef-name |
| 4926 | case tok::annot_typename: |
| 4927 | return !DisambiguatingWithExpression || |
| 4928 | !isStartOfObjCClassMessageMissingOpenBracket(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4929 | |
Steve Naroff | f192fab | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 4930 | case tok::kw___declspec: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4931 | case tok::kw___cdecl: |
| 4932 | case tok::kw___stdcall: |
| 4933 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4934 | case tok::kw___thiscall: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4935 | case tok::kw___regcall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4936 | case tok::kw___vectorcall: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4937 | case tok::kw___w64: |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4938 | case tok::kw___sptr: |
| 4939 | case tok::kw___uptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4940 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4941 | case tok::kw___ptr32: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4942 | case tok::kw___forceinline: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4943 | case tok::kw___pascal: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4944 | case tok::kw___unaligned: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4945 | |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 4946 | case tok::kw__Nonnull: |
| 4947 | case tok::kw__Nullable: |
| 4948 | case tok::kw__Null_unspecified: |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 4949 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4950 | case tok::kw___kindof: |
| 4951 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4952 | case tok::kw___private: |
| 4953 | case tok::kw___local: |
| 4954 | case tok::kw___global: |
| 4955 | case tok::kw___constant: |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 4956 | case tok::kw___generic: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4957 | case tok::kw___read_only: |
| 4958 | case tok::kw___read_write: |
| 4959 | case tok::kw___write_only: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 4960 | #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 4961 | #include "clang/Basic/OpenCLImageTypes.def" |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4962 | |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4963 | return true; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4964 | } |
| 4965 | } |
| 4966 | |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 4967 | bool Parser::isConstructorDeclarator(bool IsUnqualified, bool DeductionGuide) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4968 | TentativeParsingAction TPA(*this); |
| 4969 | |
| 4970 | // Parse the C++ scope specifier. |
| 4971 | CXXScopeSpec SS; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 4972 | if (ParseOptionalCXXScopeSpecifier(SS, nullptr, |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4973 | /*EnteringContext=*/true)) { |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4974 | TPA.Revert(); |
| 4975 | return false; |
| 4976 | } |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4977 | |
| 4978 | // Parse the constructor name. |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 4979 | if (Tok.is(tok::identifier)) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4980 | // We already know that we have a constructor name; just consume |
| 4981 | // the token. |
| 4982 | ConsumeToken(); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 4983 | } else if (Tok.is(tok::annot_template_id)) { |
| 4984 | ConsumeAnnotationToken(); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4985 | } else { |
| 4986 | TPA.Revert(); |
| 4987 | return false; |
| 4988 | } |
| 4989 | |
Richard Smith | 8f8697f | 2017-02-08 01:16:55 +0000 | [diff] [blame] | 4990 | // There may be attributes here, appertaining to the constructor name or type |
| 4991 | // we just stepped past. |
| 4992 | SkipCXX11Attributes(); |
| 4993 | |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4994 | // Current class name must be followed by a left parenthesis. |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4995 | if (Tok.isNot(tok::l_paren)) { |
| 4996 | TPA.Revert(); |
| 4997 | return false; |
| 4998 | } |
| 4999 | ConsumeParen(); |
| 5000 | |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 5001 | // A right parenthesis, or ellipsis followed by a right parenthesis signals |
| 5002 | // that we have a constructor. |
| 5003 | if (Tok.is(tok::r_paren) || |
| 5004 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5005 | TPA.Revert(); |
| 5006 | return true; |
| 5007 | } |
| 5008 | |
Richard Smith | f216366 | 2013-09-06 00:12:20 +0000 | [diff] [blame] | 5009 | // A C++11 attribute here signals that we have a constructor, and is an |
| 5010 | // attribute on the first constructor parameter. |
| 5011 | if (getLangOpts().CPlusPlus11 && |
| 5012 | isCXX11AttributeSpecifier(/*Disambiguate*/ false, |
| 5013 | /*OuterMightBeMessageSend*/ true)) { |
| 5014 | TPA.Revert(); |
| 5015 | return true; |
| 5016 | } |
| 5017 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5018 | // If we need to, enter the specified scope. |
| 5019 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5020 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5021 | DeclScopeObj.EnterDeclaratorScope(); |
| 5022 | |
Francois Pichet | 79f3a87 | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 5023 | // Optionally skip Microsoft attributes. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5024 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | 79f3a87 | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 5025 | MaybeParseMicrosoftAttributes(Attrs); |
| 5026 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5027 | // Check whether the next token(s) are part of a declaration |
| 5028 | // specifier, in which case we have the start of a parameter and, |
| 5029 | // therefore, we know that this is a constructor. |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 5030 | bool IsConstructor = false; |
| 5031 | if (isDeclarationSpecifier()) |
| 5032 | IsConstructor = true; |
| 5033 | else if (Tok.is(tok::identifier) || |
| 5034 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) { |
| 5035 | // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type. |
| 5036 | // This might be a parenthesized member name, but is more likely to |
| 5037 | // be a constructor declaration with an invalid argument type. Keep |
| 5038 | // looking. |
| 5039 | if (Tok.is(tok::annot_cxxscope)) |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 5040 | ConsumeAnnotationToken(); |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 5041 | ConsumeToken(); |
| 5042 | |
| 5043 | // If this is not a constructor, we must be parsing a declarator, |
Richard Smith | 1453e31 | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 5044 | // which must have one of the following syntactic forms (see the |
| 5045 | // grammar extract at the start of ParseDirectDeclarator): |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 5046 | switch (Tok.getKind()) { |
| 5047 | case tok::l_paren: |
| 5048 | // C(X ( int)); |
| 5049 | case tok::l_square: |
| 5050 | // C(X [ 5]); |
| 5051 | // C(X [ [attribute]]); |
| 5052 | case tok::coloncolon: |
| 5053 | // C(X :: Y); |
| 5054 | // C(X :: *p); |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 5055 | // Assume this isn't a constructor, rather than assuming it's a |
| 5056 | // constructor with an unnamed parameter of an ill-formed type. |
| 5057 | break; |
| 5058 | |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 5059 | case tok::r_paren: |
| 5060 | // C(X ) |
Richard Smith | 8f8697f | 2017-02-08 01:16:55 +0000 | [diff] [blame] | 5061 | |
| 5062 | // Skip past the right-paren and any following attributes to get to |
| 5063 | // the function body or trailing-return-type. |
| 5064 | ConsumeParen(); |
| 5065 | SkipCXX11Attributes(); |
| 5066 | |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5067 | if (DeductionGuide) { |
| 5068 | // C(X) -> ... is a deduction guide. |
Richard Smith | 8f8697f | 2017-02-08 01:16:55 +0000 | [diff] [blame] | 5069 | IsConstructor = Tok.is(tok::arrow); |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5070 | break; |
| 5071 | } |
Richard Smith | 8f8697f | 2017-02-08 01:16:55 +0000 | [diff] [blame] | 5072 | if (Tok.is(tok::colon) || Tok.is(tok::kw_try)) { |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 5073 | // Assume these were meant to be constructors: |
| 5074 | // C(X) : (the name of a bit-field cannot be parenthesized). |
| 5075 | // C(X) try (this is otherwise ill-formed). |
| 5076 | IsConstructor = true; |
| 5077 | } |
Richard Smith | 8f8697f | 2017-02-08 01:16:55 +0000 | [diff] [blame] | 5078 | if (Tok.is(tok::semi) || Tok.is(tok::l_brace)) { |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 5079 | // If we have a constructor name within the class definition, |
| 5080 | // assume these were meant to be constructors: |
| 5081 | // C(X) { |
| 5082 | // C(X) ; |
| 5083 | // ... because otherwise we would be declaring a non-static data |
| 5084 | // member that is ill-formed because it's of the same type as its |
| 5085 | // surrounding class. |
| 5086 | // |
| 5087 | // FIXME: We can actually do this whether or not the name is qualified, |
| 5088 | // 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] | 5089 | // a constructor name. |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 5090 | // currently, so we're somewhat conservative here. |
| 5091 | IsConstructor = IsUnqualified; |
| 5092 | } |
| 5093 | break; |
| 5094 | |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 5095 | default: |
| 5096 | IsConstructor = true; |
| 5097 | break; |
| 5098 | } |
| 5099 | } |
| 5100 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5101 | TPA.Revert(); |
| 5102 | return IsConstructor; |
| 5103 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 5104 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5105 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5106 | /// type-qualifier-list: [C99 6.7.5] |
| 5107 | /// type-qualifier |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5108 | /// [vendor] attributes |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5109 | /// [ only if AttrReqs & AR_VendorAttributesParsed ] |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5110 | /// type-qualifier-list type-qualifier |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5111 | /// [vendor] type-qualifier-list attributes |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5112 | /// [ only if AttrReqs & AR_VendorAttributesParsed ] |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5113 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5114 | /// [ only if AttReqs & AR_CXX11AttributesParsed ] |
| 5115 | /// Note: vendor can be GNU, MS, etc and can be explicitly controlled via |
| 5116 | /// AttrRequirements bitmask values. |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 5117 | void Parser::ParseTypeQualifierListOpt( |
| 5118 | DeclSpec &DS, unsigned AttrReqs, bool AtomicAllowed, |
| 5119 | bool IdentifierRequired, |
| 5120 | Optional<llvm::function_ref<void()>> CodeCompletionHandler) { |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 5121 | if (standardAttributesAllowed() && (AttrReqs & AR_CXX11AttributesParsed) && |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5122 | isCXX11AttributeSpecifier()) { |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5123 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 5124 | ParseCXX11Attributes(attrs); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5125 | DS.takeAttributesFrom(attrs); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5126 | } |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 5127 | |
| 5128 | SourceLocation EndLoc; |
| 5129 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5130 | while (1) { |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5131 | bool isInvalid = false; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5132 | const char *PrevSpec = nullptr; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5133 | unsigned DiagID = 0; |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 5134 | SourceLocation Loc = Tok.getLocation(); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 5135 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5136 | switch (Tok.getKind()) { |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 5137 | case tok::code_completion: |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 5138 | if (CodeCompletionHandler) |
| 5139 | (*CodeCompletionHandler)(); |
| 5140 | else |
| 5141 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 5142 | return cutOffParsing(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5143 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5144 | case tok::kw_const: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5145 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 5146 | getLangOpts()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 5147 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5148 | case tok::kw_volatile: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5149 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 5150 | getLangOpts()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 5151 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5152 | case tok::kw_restrict: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5153 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 5154 | getLangOpts()); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5155 | break; |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5156 | case tok::kw__Atomic: |
| 5157 | if (!AtomicAllowed) |
| 5158 | goto DoneWithTypeQuals; |
| 5159 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 5160 | getLangOpts()); |
| 5161 | break; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 5162 | |
| 5163 | // OpenCL qualifiers: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 5164 | case tok::kw___private: |
| 5165 | case tok::kw___global: |
| 5166 | case tok::kw___local: |
| 5167 | case tok::kw___constant: |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 5168 | case tok::kw___generic: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 5169 | case tok::kw___read_only: |
| 5170 | case tok::kw___write_only: |
| 5171 | case tok::kw___read_write: |
Aaron Ballman | 05d76ea | 2014-01-14 01:29:54 +0000 | [diff] [blame] | 5172 | ParseOpenCLQualifiers(DS.getAttributes()); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 5173 | break; |
| 5174 | |
Andrey Bokhanko | 45d4132 | 2016-05-11 18:38:21 +0000 | [diff] [blame] | 5175 | case tok::kw___unaligned: |
| 5176 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID, |
| 5177 | getLangOpts()); |
| 5178 | break; |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 5179 | case tok::kw___uptr: |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 5180 | // 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] | 5181 | // with the MS modifier keyword. |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5182 | if ((AttrReqs & AR_DeclspecAttributesParsed) && !getLangOpts().CPlusPlus && |
Alp Toker | 47642d2 | 2013-12-03 06:13:01 +0000 | [diff] [blame] | 5183 | IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) { |
| 5184 | if (TryKeywordIdentFallback(false)) |
| 5185 | continue; |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 5186 | } |
Galina Kistanova | 7767425 | 2017-06-01 21:15:34 +0000 | [diff] [blame] | 5187 | LLVM_FALLTHROUGH; |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 5188 | case tok::kw___sptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 5189 | case tok::kw___w64: |
Steve Naroff | f9c29d4 | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 5190 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 5191 | case tok::kw___ptr32: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 5192 | case tok::kw___cdecl: |
| 5193 | case tok::kw___stdcall: |
| 5194 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 5195 | case tok::kw___thiscall: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 5196 | case tok::kw___regcall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 5197 | case tok::kw___vectorcall: |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5198 | if (AttrReqs & AR_DeclspecAttributesParsed) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5199 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 5200 | continue; |
| 5201 | } |
| 5202 | goto DoneWithTypeQuals; |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5203 | case tok::kw___pascal: |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5204 | if (AttrReqs & AR_VendorAttributesParsed) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5205 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5206 | continue; |
| 5207 | } |
| 5208 | goto DoneWithTypeQuals; |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 5209 | |
| 5210 | // Nullability type specifiers. |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 5211 | case tok::kw__Nonnull: |
| 5212 | case tok::kw__Nullable: |
| 5213 | case tok::kw__Null_unspecified: |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 5214 | ParseNullabilityTypeSpecifiers(DS.getAttributes()); |
| 5215 | continue; |
| 5216 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 5217 | // Objective-C 'kindof' types. |
| 5218 | case tok::kw___kindof: |
| 5219 | DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc, |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 5220 | nullptr, 0, ParsedAttr::AS_Keyword); |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 5221 | (void)ConsumeToken(); |
| 5222 | continue; |
| 5223 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 5224 | case tok::kw___attribute: |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5225 | if (AttrReqs & AR_GNUAttributesParsedAndRejected) |
| 5226 | // When GNU attributes are expressly forbidden, diagnose their usage. |
| 5227 | Diag(Tok, diag::err_attributes_not_allowed); |
| 5228 | |
| 5229 | // Parse the attributes even if they are rejected to ensure that error |
| 5230 | // recovery is graceful. |
| 5231 | if (AttrReqs & AR_GNUAttributesParsed || |
| 5232 | AttrReqs & AR_GNUAttributesParsedAndRejected) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5233 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5234 | continue; // do *not* consume the next token! |
| 5235 | } |
| 5236 | // otherwise, FALL THROUGH! |
Galina Kistanova | 7767425 | 2017-06-01 21:15:34 +0000 | [diff] [blame] | 5237 | LLVM_FALLTHROUGH; |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5238 | default: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 5239 | DoneWithTypeQuals: |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5240 | // If this is not a type-qualifier token, we're done reading type |
| 5241 | // qualifiers. First verify that DeclSpec's are consistent. |
Craig Topper | 2512241 | 2015-11-15 03:32:11 +0000 | [diff] [blame] | 5242 | DS.Finish(Actions, Actions.getASTContext().getPrintingPolicy()); |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 5243 | if (EndLoc.isValid()) |
| 5244 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5245 | return; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5246 | } |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5247 | |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 5248 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 5249 | if (isInvalid) { |
| 5250 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 5251 | Diag(Tok, DiagID) << PrevSpec; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 5252 | } |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 5253 | EndLoc = ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5254 | } |
| 5255 | } |
| 5256 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 5257 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 5258 | /// |
| 5259 | void Parser::ParseDeclarator(Declarator &D) { |
| 5260 | /// This implements the 'declarator' production in the C grammar, then checks |
| 5261 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5262 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 5263 | } |
| 5264 | |
Richard Smith | 0b350b9 | 2014-10-28 16:55:02 +0000 | [diff] [blame] | 5265 | static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang, |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5266 | DeclaratorContext TheContext) { |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5267 | if (Kind == tok::star || Kind == tok::caret) |
| 5268 | return true; |
| 5269 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5270 | if ((Kind == tok::kw_pipe) && Lang.OpenCL && (Lang.OpenCLVersion >= 200)) |
| 5271 | return true; |
| 5272 | |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5273 | if (!Lang.CPlusPlus) |
| 5274 | return false; |
| 5275 | |
Richard Smith | 0b350b9 | 2014-10-28 16:55:02 +0000 | [diff] [blame] | 5276 | if (Kind == tok::amp) |
| 5277 | return true; |
| 5278 | |
| 5279 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 5280 | // But we must not parse them in conversion-type-ids and new-type-ids, since |
| 5281 | // those can be legitimately followed by a && operator. |
| 5282 | // (The same thing can in theory happen after a trailing-return-type, but |
| 5283 | // since those are a C++11 feature, there is no rejects-valid issue there.) |
| 5284 | if (Kind == tok::ampamp) |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5285 | return Lang.CPlusPlus11 || |
| 5286 | (TheContext != DeclaratorContext::ConversionIdContext && |
| 5287 | TheContext != DeclaratorContext::CXXNewContext); |
Richard Smith | 0b350b9 | 2014-10-28 16:55:02 +0000 | [diff] [blame] | 5288 | |
| 5289 | return false; |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5290 | } |
| 5291 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5292 | // Indicates whether the given declarator is a pipe declarator. |
| 5293 | static bool isPipeDeclerator(const Declarator &D) { |
| 5294 | const unsigned NumTypes = D.getNumTypeObjects(); |
| 5295 | |
| 5296 | for (unsigned Idx = 0; Idx != NumTypes; ++Idx) |
| 5297 | if (DeclaratorChunk::Pipe == D.getTypeObject(Idx).Kind) |
| 5298 | return true; |
| 5299 | |
| 5300 | return false; |
| 5301 | } |
| 5302 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5303 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 5304 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 5305 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5306 | /// ptr-operator production. |
| 5307 | /// |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 5308 | /// 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] | 5309 | /// made to TryParseDeclarator and MightBeDeclarator, and possibly to |
| 5310 | /// isConstructorDeclarator. |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 5311 | /// |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5312 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 5313 | /// [C] pointer[opt] direct-declarator |
| 5314 | /// [C++] direct-declarator |
| 5315 | /// [C++] ptr-operator declarator |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 5316 | /// |
| 5317 | /// pointer: [C99 6.7.5] |
| 5318 | /// '*' type-qualifier-list[opt] |
| 5319 | /// '*' type-qualifier-list[opt] pointer |
| 5320 | /// |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5321 | /// ptr-operator: |
| 5322 | /// '*' cv-qualifier-seq[opt] |
| 5323 | /// '&' |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 5324 | /// [C++0x] '&&' |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5325 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 5326 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5327 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5328 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 5329 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 66a985d | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 5330 | if (Diags.hasAllExtensionsSilenced()) |
| 5331 | D.setExtension(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5332 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5333 | // C++ member pointers start with a '::' or a nested-name. |
| 5334 | // Member pointers get special handling, since there's no place for the |
| 5335 | // scope spec in the generic path below. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5336 | if (getLangOpts().CPlusPlus && |
Richard Smith | 83c2ecf | 2016-02-02 23:34:49 +0000 | [diff] [blame] | 5337 | (Tok.is(tok::coloncolon) || Tok.is(tok::kw_decltype) || |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 5338 | (Tok.is(tok::identifier) && |
| 5339 | (NextToken().is(tok::coloncolon) || NextToken().is(tok::less))) || |
Chris Lattner | 803802d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 5340 | Tok.is(tok::annot_cxxscope))) { |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5341 | bool EnteringContext = |
| 5342 | D.getContext() == DeclaratorContext::FileContext || |
| 5343 | D.getContext() == DeclaratorContext::MemberContext; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5344 | CXXScopeSpec SS; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 5345 | ParseOptionalCXXScopeSpecifier(SS, nullptr, EnteringContext); |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 5346 | |
Jeffrey Yasskin | 4e150f8 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 5347 | if (SS.isNotEmpty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5348 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5349 | // The scope spec really belongs to the direct-declarator. |
Richard Smith | 5f044ad | 2013-01-08 22:43:49 +0000 | [diff] [blame] | 5350 | if (D.mayHaveIdentifier()) |
| 5351 | D.getCXXScopeSpec() = SS; |
| 5352 | else |
| 5353 | AnnotateScopeToken(SS, true); |
| 5354 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5355 | if (DirectDeclParser) |
| 5356 | (this->*DirectDeclParser)(D); |
| 5357 | return; |
| 5358 | } |
| 5359 | |
| 5360 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5361 | D.SetRangeEnd(Loc); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5362 | DeclSpec DS(AttrFactory); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5363 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5364 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5365 | |
| 5366 | // Recurse to parse whatever is left. |
| 5367 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 5368 | |
| 5369 | // Sema will have to catch (syntactically invalid) pointers into global |
| 5370 | // scope. It has to catch pointers into namespace scope anyway. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 5371 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 5372 | SS, DS.getTypeQualifiers(), DS.getEndLoc()), |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 5373 | std::move(DS.getAttributes()), |
| 5374 | /* Don't replace range end. */ SourceLocation()); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5375 | return; |
| 5376 | } |
| 5377 | } |
| 5378 | |
| 5379 | tok::TokenKind Kind = Tok.getKind(); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5380 | |
| 5381 | if (D.getDeclSpec().isTypeSpecPipe() && !isPipeDeclerator(D)) { |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 5382 | DeclSpec DS(AttrFactory); |
| 5383 | ParseTypeQualifierListOpt(DS); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5384 | |
| 5385 | D.AddTypeInfo( |
| 5386 | DeclaratorChunk::getPipe(DS.getTypeQualifiers(), DS.getPipeLoc()), |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 5387 | std::move(DS.getAttributes()), SourceLocation()); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5388 | } |
| 5389 | |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 5390 | // Not a pointer, C++ reference, or block. |
Richard Smith | 0b350b9 | 2014-10-28 16:55:02 +0000 | [diff] [blame] | 5391 | if (!isPtrOperatorToken(Kind, getLangOpts(), D.getContext())) { |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5392 | if (DirectDeclParser) |
| 5393 | (this->*DirectDeclParser)(D); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5394 | return; |
| 5395 | } |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5396 | |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 5397 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 5398 | // '&&' -> rvalue reference |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 5399 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5400 | D.SetRangeEnd(Loc); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5401 | |
Chris Lattner | 9eac931 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 5402 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 788404f | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 5403 | // Is a pointer. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5404 | DeclSpec DS(AttrFactory); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5405 | |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5406 | // GNU attributes are not allowed here in a new-type-id, but Declspec and |
| 5407 | // C++11 attributes are allowed. |
| 5408 | unsigned Reqs = AR_CXX11AttributesParsed | AR_DeclspecAttributesParsed | |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5409 | ((D.getContext() != DeclaratorContext::CXXNewContext) |
| 5410 | ? AR_GNUAttributesParsed |
| 5411 | : AR_GNUAttributesParsedAndRejected); |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5412 | ParseTypeQualifierListOpt(DS, Reqs, true, !D.mayOmitIdentifier()); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5413 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5414 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5415 | // Recursively parse the declarator. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5416 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 5417 | if (Kind == tok::star) |
| 5418 | // Remember that we parsed a pointer type, and remember the type-quals. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 5419 | D.AddTypeInfo(DeclaratorChunk::getPointer( |
| 5420 | DS.getTypeQualifiers(), Loc, DS.getConstSpecLoc(), |
| 5421 | DS.getVolatileSpecLoc(), DS.getRestrictSpecLoc(), |
| 5422 | DS.getAtomicSpecLoc(), DS.getUnalignedSpecLoc()), |
| 5423 | std::move(DS.getAttributes()), SourceLocation()); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 5424 | else |
| 5425 | // Remember that we parsed a Block type, and remember the type-quals. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 5426 | D.AddTypeInfo( |
| 5427 | DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), Loc), |
| 5428 | std::move(DS.getAttributes()), SourceLocation()); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5429 | } else { |
| 5430 | // Is a reference |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5431 | DeclSpec DS(AttrFactory); |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 5432 | |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 5433 | // Complain about rvalue references in C++03, but then go on and build |
| 5434 | // the declarator. |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5435 | if (Kind == tok::ampamp) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5436 | Diag(Loc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5437 | diag::warn_cxx98_compat_rvalue_reference : |
| 5438 | diag::ext_rvalue_reference); |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 5439 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5440 | // GNU-style and C++11 attributes are allowed here, as is restrict. |
| 5441 | ParseTypeQualifierListOpt(DS); |
| 5442 | D.ExtendWithDeclSpec(DS); |
| 5443 | |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 5444 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 5445 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 5446 | // template type argument, in which case the cv-qualifiers are ignored. |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 5447 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 5448 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 5449 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 5450 | diag::err_invalid_reference_qualifier_application) << "const"; |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 5451 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 5452 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 5453 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5454 | // 'restrict' is permitted as an extension. |
| 5455 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
| 5456 | Diag(DS.getAtomicSpecLoc(), |
| 5457 | diag::err_invalid_reference_qualifier_application) << "_Atomic"; |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 5458 | } |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5459 | |
| 5460 | // Recursively parse the declarator. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5461 | ParseDeclaratorInternal(D, DirectDeclParser); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5462 | |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 5463 | if (D.getNumTypeObjects() > 0) { |
| 5464 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 5465 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 5466 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | ebad6a2 | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 5467 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 5468 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 5469 | << II; |
| 5470 | else |
| 5471 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 5472 | << "type name"; |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 5473 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5474 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 5475 | // can go ahead and build the (technically ill-formed) |
| 5476 | // declarator: reference collapsing will take care of it. |
| 5477 | } |
| 5478 | } |
| 5479 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5480 | // Remember that we parsed a reference type. |
Chris Lattner | 788404f | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 5481 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 5482 | Kind == tok::amp), |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 5483 | std::move(DS.getAttributes()), SourceLocation()); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5484 | } |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 5485 | } |
| 5486 | |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5487 | // When correcting from misplaced brackets before the identifier, the location |
| 5488 | // is saved inside the declarator so that other diagnostic messages can use |
| 5489 | // them. This extracts and returns that location, or returns the provided |
| 5490 | // location if a stored location does not exist. |
| 5491 | static SourceLocation getMissingDeclaratorIdLoc(Declarator &D, |
| 5492 | SourceLocation Loc) { |
| 5493 | if (D.getName().StartLocation.isInvalid() && |
| 5494 | D.getName().EndLocation.isValid()) |
| 5495 | return D.getName().EndLocation; |
| 5496 | |
| 5497 | return Loc; |
| 5498 | } |
| 5499 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5500 | /// ParseDirectDeclarator |
| 5501 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5502 | /// [C99] identifier |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5503 | /// '(' declarator ')' |
| 5504 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5505 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 5506 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 5507 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 5508 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 5509 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5510 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 5511 | /// attribute-specifier-seq[opt] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5512 | /// direct-declarator '(' parameter-type-list ')' |
| 5513 | /// direct-declarator '(' identifier-list[opt] ')' |
| 5514 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 5515 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 22c40fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 5516 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 5517 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5518 | /// [C++11] direct-declarator '(' parameter-declaration-clause ')' |
| 5519 | /// attribute-specifier-seq[opt] cv-qualifier-seq[opt] |
| 5520 | /// ref-qualifier[opt] exception-specification[opt] |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 5521 | /// [C++] declarator-id |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5522 | /// [C++11] declarator-id attribute-specifier-seq[opt] |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5523 | /// |
| 5524 | /// declarator-id: [C++ 8] |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 5525 | /// '...'[opt] id-expression |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5526 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 5527 | /// |
| 5528 | /// id-expression: [C++ 5.1] |
| 5529 | /// unqualified-id |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5530 | /// qualified-id |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5531 | /// |
| 5532 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5533 | /// identifier |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 5534 | /// operator-function-id |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5535 | /// conversion-function-id |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5536 | /// '~' class-name |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 5537 | /// template-id |
Argyrios Kyrtzidis | e442635 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 5538 | /// |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 5539 | /// C++17 adds the following, which we also handle here: |
| 5540 | /// |
| 5541 | /// simple-declaration: |
| 5542 | /// <decl-spec> '[' identifier-list ']' brace-or-equal-initializer ';' |
| 5543 | /// |
Richard Smith | 1453e31 | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 5544 | /// Note, any additional constructs added here may need corresponding changes |
| 5545 | /// in isConstructorDeclarator. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5546 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5547 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 5548 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5549 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 5550 | // This might be a C++17 structured binding. |
| 5551 | if (Tok.is(tok::l_square) && !D.mayOmitIdentifier() && |
| 5552 | D.getCXXScopeSpec().isEmpty()) |
| 5553 | return ParseDecompositionDeclarator(D); |
| 5554 | |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 5555 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR inside a class, in |
| 5556 | // this context it is a bitfield. Also in range-based for statement colon |
| 5557 | // may delimit for-range-declaration. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5558 | ColonProtectionRAIIObject X( |
| 5559 | *this, D.getContext() == DeclaratorContext::MemberContext || |
| 5560 | (D.getContext() == DeclaratorContext::ForContext && |
| 5561 | getLangOpts().CPlusPlus11)); |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 5562 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5563 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5564 | if (D.getCXXScopeSpec().isEmpty()) { |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5565 | bool EnteringContext = |
| 5566 | D.getContext() == DeclaratorContext::FileContext || |
| 5567 | D.getContext() == DeclaratorContext::MemberContext; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 5568 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), nullptr, |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 5569 | EnteringContext); |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 5570 | } |
| 5571 | |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5572 | if (D.getCXXScopeSpec().isValid()) { |
Richard Smith | 64e033f | 2015-01-15 00:48:52 +0000 | [diff] [blame] | 5573 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), |
| 5574 | D.getCXXScopeSpec())) |
John McCall | 2b058ef | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 5575 | // Change the declaration context for name lookup, until this function |
| 5576 | // is exited (and the declarator has been parsed). |
| 5577 | DeclScopeObj.EnterDeclaratorScope(); |
Alex Lorenz | e151f010 | 2016-12-07 10:24:44 +0000 | [diff] [blame] | 5578 | else if (getObjCDeclContext()) { |
| 5579 | // Ensure that we don't interpret the next token as an identifier when |
| 5580 | // dealing with declarations in an Objective-C container. |
| 5581 | D.SetIdentifier(nullptr, Tok.getLocation()); |
| 5582 | D.setInvalidType(true); |
| 5583 | ConsumeToken(); |
| 5584 | goto PastIdentifier; |
| 5585 | } |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5586 | } |
| 5587 | |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 5588 | // C++0x [dcl.fct]p14: |
Faisal Vali | 8435a69 | 2014-12-04 12:40:21 +0000 | [diff] [blame] | 5589 | // There is a syntactic ambiguity when an ellipsis occurs at the end of a |
| 5590 | // parameter-declaration-clause without a preceding comma. In this case, |
| 5591 | // the ellipsis is parsed as part of the abstract-declarator if the type |
| 5592 | // of the parameter either names a template parameter pack that has not |
| 5593 | // been expanded or contains auto; otherwise, it is parsed as part of the |
| 5594 | // parameter-declaration-clause. |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5595 | if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() && |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5596 | !((D.getContext() == DeclaratorContext::PrototypeContext || |
| 5597 | D.getContext() == DeclaratorContext::LambdaExprParameterContext || |
| 5598 | D.getContext() == DeclaratorContext::BlockLiteralContext) && |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 5599 | NextToken().is(tok::r_paren) && |
Richard Smith | b19337f | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 5600 | !D.hasGroupingParens() && |
Faisal Vali | 8435a69 | 2014-12-04 12:40:21 +0000 | [diff] [blame] | 5601 | !Actions.containsUnexpandedParameterPacks(D) && |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 5602 | D.getDeclSpec().getTypeSpecType() != TST_auto)) { |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5603 | SourceLocation EllipsisLoc = ConsumeToken(); |
Richard Smith | 0b350b9 | 2014-10-28 16:55:02 +0000 | [diff] [blame] | 5604 | if (isPtrOperatorToken(Tok.getKind(), getLangOpts(), D.getContext())) { |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5605 | // The ellipsis was put in the wrong place. Recover, and explain to |
| 5606 | // the user what they should have done. |
| 5607 | ParseDeclarator(D); |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 5608 | if (EllipsisLoc.isValid()) |
| 5609 | DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D); |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5610 | return; |
| 5611 | } else |
| 5612 | D.setEllipsisLoc(EllipsisLoc); |
| 5613 | |
| 5614 | // The ellipsis can't be followed by a parenthesized declarator. We |
| 5615 | // check for that in ParseParenDeclarator, after we have disambiguated |
| 5616 | // the l_paren token. |
| 5617 | } |
| 5618 | |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 5619 | if (Tok.isOneOf(tok::identifier, tok::kw_operator, tok::annot_template_id, |
| 5620 | tok::tilde)) { |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5621 | // We found something that indicates the start of an unqualified-id. |
| 5622 | // Parse that unqualified-id. |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 5623 | bool AllowConstructorName; |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5624 | bool AllowDeductionGuide; |
| 5625 | if (D.getDeclSpec().hasTypeSpecifier()) { |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 5626 | AllowConstructorName = false; |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5627 | AllowDeductionGuide = false; |
| 5628 | } else if (D.getCXXScopeSpec().isSet()) { |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 5629 | AllowConstructorName = |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5630 | (D.getContext() == DeclaratorContext::FileContext || |
| 5631 | D.getContext() == DeclaratorContext::MemberContext); |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5632 | AllowDeductionGuide = false; |
| 5633 | } else { |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5634 | AllowConstructorName = |
| 5635 | (D.getContext() == DeclaratorContext::MemberContext); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5636 | AllowDeductionGuide = |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5637 | (D.getContext() == DeclaratorContext::FileContext || |
| 5638 | D.getContext() == DeclaratorContext::MemberContext); |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5639 | } |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 5640 | |
Richard Smith | 64e033f | 2015-01-15 00:48:52 +0000 | [diff] [blame] | 5641 | bool HadScope = D.getCXXScopeSpec().isValid(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5642 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 5643 | /*EnteringContext=*/true, |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 5644 | /*AllowDestructorName=*/true, AllowConstructorName, |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 5645 | AllowDeductionGuide, nullptr, nullptr, |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5646 | D.getName()) || |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5647 | // Once we're past the identifier, if the scope was bad, mark the |
| 5648 | // whole declarator bad. |
| 5649 | D.getCXXScopeSpec().isInvalid()) { |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5650 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5651 | D.setInvalidType(true); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5652 | } else { |
Richard Smith | 64e033f | 2015-01-15 00:48:52 +0000 | [diff] [blame] | 5653 | // ParseUnqualifiedId might have parsed a scope specifier during error |
| 5654 | // recovery. If it did so, enter that scope. |
| 5655 | if (!HadScope && D.getCXXScopeSpec().isValid() && |
| 5656 | Actions.ShouldEnterDeclaratorScope(getCurScope(), |
| 5657 | D.getCXXScopeSpec())) |
| 5658 | DeclScopeObj.EnterDeclaratorScope(); |
| 5659 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5660 | // Parsed the unqualified-id; update range information and move along. |
| 5661 | if (D.getSourceRange().getBegin().isInvalid()) |
| 5662 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 5663 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5664 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5665 | goto PastIdentifier; |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5666 | } |
Richard Smith | d63db6e | 2015-12-19 02:40:19 +0000 | [diff] [blame] | 5667 | |
| 5668 | if (D.getCXXScopeSpec().isNotEmpty()) { |
| 5669 | // We have a scope specifier but no following unqualified-id. |
| 5670 | Diag(PP.getLocForEndOfToken(D.getCXXScopeSpec().getEndLoc()), |
| 5671 | diag::err_expected_unqualified_id) |
| 5672 | << /*C++*/1; |
| 5673 | D.SetIdentifier(nullptr, Tok.getLocation()); |
| 5674 | goto PastIdentifier; |
| 5675 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5676 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5677 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5678 | "There's a C++-specific check for tok::identifier above"); |
| 5679 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 5680 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
Richard Trieu | 2664dc1 | 2014-05-05 22:06:50 +0000 | [diff] [blame] | 5681 | D.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5682 | ConsumeToken(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5683 | goto PastIdentifier; |
Richard Smith | 74639b1 | 2017-05-19 01:54:59 +0000 | [diff] [blame] | 5684 | } else if (Tok.is(tok::identifier) && !D.mayHaveIdentifier()) { |
| 5685 | // We're not allowed an identifier here, but we got one. Try to figure out |
| 5686 | // if the user was trying to attach a name to the type, or whether the name |
| 5687 | // is some unrelated trailing syntax. |
| 5688 | bool DiagnoseIdentifier = false; |
| 5689 | if (D.hasGroupingParens()) |
| 5690 | // An identifier within parens is unlikely to be intended to be anything |
| 5691 | // other than a name being "declared". |
| 5692 | DiagnoseIdentifier = true; |
Richard Smith | 77a9c60 | 2018-02-28 03:02:23 +0000 | [diff] [blame] | 5693 | else if (D.getContext() == DeclaratorContext::TemplateArgContext) |
Richard Smith | 74639b1 | 2017-05-19 01:54:59 +0000 | [diff] [blame] | 5694 | // T<int N> is an accidental identifier; T<int N indicates a missing '>'. |
| 5695 | DiagnoseIdentifier = |
| 5696 | NextToken().isOneOf(tok::comma, tok::greater, tok::greatergreater); |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5697 | else if (D.getContext() == DeclaratorContext::AliasDeclContext || |
| 5698 | D.getContext() == DeclaratorContext::AliasTemplateContext) |
Richard Smith | 74639b1 | 2017-05-19 01:54:59 +0000 | [diff] [blame] | 5699 | // The most likely error is that the ';' was forgotten. |
| 5700 | DiagnoseIdentifier = NextToken().isOneOf(tok::comma, tok::semi); |
Richard Smith | e303e35 | 2018-02-02 22:24:54 +0000 | [diff] [blame] | 5701 | else if ((D.getContext() == DeclaratorContext::TrailingReturnContext || |
| 5702 | D.getContext() == DeclaratorContext::TrailingReturnVarContext) && |
Richard Smith | 74639b1 | 2017-05-19 01:54:59 +0000 | [diff] [blame] | 5703 | !isCXX11VirtSpecifier(Tok)) |
| 5704 | DiagnoseIdentifier = NextToken().isOneOf( |
| 5705 | tok::comma, tok::semi, tok::equal, tok::l_brace, tok::kw_try); |
| 5706 | if (DiagnoseIdentifier) { |
Richard Smith | f39720b | 2013-10-13 22:12:28 +0000 | [diff] [blame] | 5707 | Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id) |
| 5708 | << FixItHint::CreateRemoval(Tok.getLocation()); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5709 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Richard Smith | f39720b | 2013-10-13 22:12:28 +0000 | [diff] [blame] | 5710 | ConsumeToken(); |
| 5711 | goto PastIdentifier; |
| 5712 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5713 | } |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5714 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5715 | if (Tok.is(tok::l_paren)) { |
Richard Smith | e303e35 | 2018-02-02 22:24:54 +0000 | [diff] [blame] | 5716 | // If this might be an abstract-declarator followed by a direct-initializer, |
| 5717 | // check whether this is a valid declarator chunk. If it can't be, assume |
| 5718 | // that it's an initializer instead. |
| 5719 | if (D.mayOmitIdentifier() && D.mayBeFollowedByCXXDirectInit()) { |
| 5720 | RevertingTentativeParsingAction PA(*this); |
| 5721 | if (TryParseDeclarator(true, D.mayHaveIdentifier(), true) == |
| 5722 | TPResult::False) { |
| 5723 | D.SetIdentifier(nullptr, Tok.getLocation()); |
| 5724 | goto PastIdentifier; |
| 5725 | } |
| 5726 | } |
| 5727 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5728 | // direct-declarator: '(' declarator ')' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 5729 | // direct-declarator: '(' attributes declarator ')' |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5730 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 5731 | ParseParenDeclarator(D); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5732 | |
| 5733 | // If the declarator was parenthesized, we entered the declarator |
| 5734 | // scope when parsing the parenthesized declarator, then exited |
| 5735 | // the scope already. Re-enter the scope, if we need to. |
| 5736 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 5737 | // If there was an error parsing parenthesized declarator, declarator |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5738 | // scope may have been entered before. Don't do it again. |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 5739 | if (!D.isInvalidType() && |
Nico Weber | 6b05f38 | 2015-02-18 04:53:03 +0000 | [diff] [blame] | 5740 | Actions.ShouldEnterDeclaratorScope(getCurScope(), |
| 5741 | D.getCXXScopeSpec())) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5742 | // Change the declaration context for name lookup, until this function |
| 5743 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 5744 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5745 | } |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5746 | } else if (D.mayOmitIdentifier()) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5747 | // This could be something simple like "int" (in which case the declarator |
| 5748 | // portion is empty), if an abstract-declarator is allowed. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5749 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Richard Smith | b19337f | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 5750 | |
| 5751 | // The grammar for abstract-pack-declarator does not allow grouping parens. |
| 5752 | // FIXME: Revisit this once core issue 1488 is resolved. |
| 5753 | if (D.hasEllipsis() && D.hasGroupingParens()) |
| 5754 | Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()), |
| 5755 | diag::ext_abstract_pack_declarator_parens); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5756 | } else { |
David Blaikie | 5d577a2 | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 5757 | if (Tok.getKind() == tok::annot_pragma_parser_crash) |
David Blaikie | 5bd4c2a | 2012-08-21 18:56:49 +0000 | [diff] [blame] | 5758 | LLVM_BUILTIN_TRAP; |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5759 | if (Tok.is(tok::l_square)) |
| 5760 | return ParseMisplacedBracketDeclarator(D); |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 5761 | if (D.getContext() == DeclaratorContext::MemberContext) { |
Alex Lorenz | f127821 | 2017-04-11 15:01:53 +0000 | [diff] [blame] | 5762 | // Objective-C++: Detect C++ keywords and try to prevent further errors by |
| 5763 | // treating these keyword as valid member names. |
| 5764 | if (getLangOpts().ObjC1 && getLangOpts().CPlusPlus && |
| 5765 | Tok.getIdentifierInfo() && |
| 5766 | Tok.getIdentifierInfo()->isCPlusPlusKeyword(getLangOpts())) { |
| 5767 | Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()), |
| 5768 | diag::err_expected_member_name_or_semi_objcxx_keyword) |
| 5769 | << Tok.getIdentifierInfo() |
| 5770 | << (D.getDeclSpec().isEmpty() ? SourceRange() |
| 5771 | : D.getDeclSpec().getSourceRange()); |
| 5772 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 5773 | D.SetRangeEnd(Tok.getLocation()); |
| 5774 | ConsumeToken(); |
| 5775 | goto PastIdentifier; |
| 5776 | } |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5777 | Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()), |
| 5778 | diag::err_expected_member_name_or_semi) |
Richard Trieu | a134240 | 2014-05-02 23:40:32 +0000 | [diff] [blame] | 5779 | << (D.getDeclSpec().isEmpty() ? SourceRange() |
| 5780 | : D.getDeclSpec().getSourceRange()); |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5781 | } else if (getLangOpts().CPlusPlus) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 5782 | if (Tok.isOneOf(tok::period, tok::arrow)) |
Richard Trieu | 9c67267 | 2013-01-26 02:31:38 +0000 | [diff] [blame] | 5783 | Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow); |
Richard Trieu | 2f58696 | 2013-09-05 02:31:33 +0000 | [diff] [blame] | 5784 | else { |
| 5785 | SourceLocation Loc = D.getCXXScopeSpec().getEndLoc(); |
| 5786 | if (Tok.isAtStartOfLine() && Loc.isValid()) |
| 5787 | Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id) |
| 5788 | << getLangOpts().CPlusPlus; |
| 5789 | else |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5790 | Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()), |
| 5791 | diag::err_expected_unqualified_id) |
Richard Trieu | 2f58696 | 2013-09-05 02:31:33 +0000 | [diff] [blame] | 5792 | << getLangOpts().CPlusPlus; |
| 5793 | } |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5794 | } else { |
| 5795 | Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()), |
| 5796 | diag::err_expected_either) |
| 5797 | << tok::identifier << tok::l_paren; |
| 5798 | } |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5799 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Chris Lattner | 8c5dd73 | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 5800 | D.setInvalidType(true); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5801 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5802 | |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5803 | PastIdentifier: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5804 | assert(D.isPastIdentifier() && |
| 5805 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5806 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5807 | // Don't parse attributes unless we have parsed an unparenthesized name. |
| 5808 | if (D.hasName() && !D.getNumTypeObjects()) |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5809 | MaybeParseCXX11Attributes(D); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5810 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5811 | while (1) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5812 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5813 | // Enter function-declaration scope, limiting any declarators to the |
| 5814 | // function prototype scope, including parameter declarators. |
| 5815 | ParseScope PrototypeScope(this, |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 5816 | Scope::FunctionPrototypeScope|Scope::DeclScope| |
| 5817 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 5818 | ? Scope::FunctionDeclarationScope : 0)); |
| 5819 | |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5820 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 5821 | // In such a case, check if we actually have a function declarator; if it |
| 5822 | // is not, the declarator has been fully parsed. |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5823 | bool IsAmbiguous = false; |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 5824 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 5825 | // The name of the declarator, if any, is tentatively declared within |
| 5826 | // a possible direct initializer. |
| 5827 | TentativelyDeclaredIdentifiers.push_back(D.getIdentifier()); |
| 5828 | bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous); |
| 5829 | TentativelyDeclaredIdentifiers.pop_back(); |
| 5830 | if (!IsFunctionDecl) |
| 5831 | break; |
| 5832 | } |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5833 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5834 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 5835 | T.consumeOpen(); |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5836 | ParseFunctionDeclarator(D, attrs, T, IsAmbiguous); |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5837 | PrototypeScope.Exit(); |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5838 | } else if (Tok.is(tok::l_square)) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5839 | ParseBracketDeclarator(D); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5840 | } else { |
| 5841 | break; |
| 5842 | } |
| 5843 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5844 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5845 | |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 5846 | void Parser::ParseDecompositionDeclarator(Declarator &D) { |
| 5847 | assert(Tok.is(tok::l_square)); |
| 5848 | |
| 5849 | // If this doesn't look like a structured binding, maybe it's a misplaced |
| 5850 | // array declarator. |
| 5851 | // FIXME: Consume the l_square first so we don't need extra lookahead for |
| 5852 | // this. |
| 5853 | if (!(NextToken().is(tok::identifier) && |
| 5854 | GetLookAheadToken(2).isOneOf(tok::comma, tok::r_square)) && |
| 5855 | !(NextToken().is(tok::r_square) && |
| 5856 | GetLookAheadToken(2).isOneOf(tok::equal, tok::l_brace))) |
| 5857 | return ParseMisplacedBracketDeclarator(D); |
| 5858 | |
| 5859 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 5860 | T.consumeOpen(); |
| 5861 | |
| 5862 | SmallVector<DecompositionDeclarator::Binding, 32> Bindings; |
| 5863 | while (Tok.isNot(tok::r_square)) { |
| 5864 | if (!Bindings.empty()) { |
| 5865 | if (Tok.is(tok::comma)) |
| 5866 | ConsumeToken(); |
| 5867 | else { |
| 5868 | if (Tok.is(tok::identifier)) { |
| 5869 | SourceLocation EndLoc = getEndOfPreviousToken(); |
| 5870 | Diag(EndLoc, diag::err_expected) |
| 5871 | << tok::comma << FixItHint::CreateInsertion(EndLoc, ","); |
| 5872 | } else { |
| 5873 | Diag(Tok, diag::err_expected_comma_or_rsquare); |
| 5874 | } |
| 5875 | |
| 5876 | SkipUntil(tok::r_square, tok::comma, tok::identifier, |
| 5877 | StopAtSemi | StopBeforeMatch); |
| 5878 | if (Tok.is(tok::comma)) |
| 5879 | ConsumeToken(); |
| 5880 | else if (Tok.isNot(tok::identifier)) |
| 5881 | break; |
| 5882 | } |
| 5883 | } |
| 5884 | |
| 5885 | if (Tok.isNot(tok::identifier)) { |
| 5886 | Diag(Tok, diag::err_expected) << tok::identifier; |
| 5887 | break; |
| 5888 | } |
| 5889 | |
| 5890 | Bindings.push_back({Tok.getIdentifierInfo(), Tok.getLocation()}); |
| 5891 | ConsumeToken(); |
| 5892 | } |
| 5893 | |
| 5894 | if (Tok.isNot(tok::r_square)) |
| 5895 | // We've already diagnosed a problem here. |
| 5896 | T.skipToEnd(); |
| 5897 | else { |
| 5898 | // C++17 does not allow the identifier-list in a structured binding |
| 5899 | // to be empty. |
| 5900 | if (Bindings.empty()) |
| 5901 | Diag(Tok.getLocation(), diag::ext_decomp_decl_empty); |
| 5902 | |
| 5903 | T.consumeClose(); |
| 5904 | } |
| 5905 | |
| 5906 | return D.setDecompositionBindings(T.getOpenLocation(), Bindings, |
| 5907 | T.getCloseLocation()); |
| 5908 | } |
| 5909 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5910 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 5911 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5912 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5913 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 5914 | /// |
| 5915 | /// direct-declarator: |
| 5916 | /// '(' declarator ')' |
| 5917 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5918 | /// direct-declarator '(' parameter-type-list ')' |
| 5919 | /// direct-declarator '(' identifier-list[opt] ')' |
| 5920 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 5921 | /// parameter-type-list[opt] ')' |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5922 | /// |
| 5923 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5924 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 5925 | T.consumeOpen(); |
| 5926 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5927 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5928 | |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5929 | // Eat any attributes before we look at whether this is a grouping or function |
| 5930 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 5931 | // the type being built up, for example: |
| 5932 | // int (__attribute__(()) *x)(long y) |
| 5933 | // If this ends up not being a grouping paren, the attribute applies to the |
| 5934 | // first argument, for example: |
| 5935 | // int (__attribute__(()) int x) |
| 5936 | // In either case, we need to eat any attributes to be able to determine what |
| 5937 | // sort of paren this is. |
| 5938 | // |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5939 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5940 | bool RequiresArg = false; |
| 5941 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5942 | ParseGNUAttributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5943 | |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5944 | // We require that the argument list (if this is a non-grouping paren) be |
| 5945 | // present even if the attribute list was empty. |
| 5946 | RequiresArg = true; |
| 5947 | } |
Chad Rosier | eea9ca7 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 5948 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 5949 | // Eat any Microsoft extensions. |
Chad Rosier | eea9ca7 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 5950 | ParseMicrosoftTypeAttributes(attrs); |
| 5951 | |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5952 | // Eat any Borland extensions. |
Ted Kremenek | 5eec2b0 | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 5953 | if (Tok.is(tok::kw___pascal)) |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5954 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5955 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5956 | // If we haven't past the identifier yet (or where the identifier would be |
| 5957 | // stored, if this is an abstract declarator), then this is probably just |
| 5958 | // grouping parens. However, if this could be an abstract-declarator, then |
| 5959 | // this could also be the start of function arguments (consider 'void()'). |
| 5960 | bool isGrouping; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5961 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5962 | if (!D.mayOmitIdentifier()) { |
| 5963 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 5964 | // paren, because we haven't seen the identifier yet. |
| 5965 | isGrouping = true; |
| 5966 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 5967 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) && |
| 5968 | NextToken().is(tok::r_paren)) || // C++ int(...) |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5969 | isDeclarationSpecifier() || // 'int(int)' is a function. |
| 5970 | isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function. |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5971 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 5972 | // considered to be a type, not a K&R identifier-list. |
| 5973 | isGrouping = false; |
| 5974 | } else { |
| 5975 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 5976 | isGrouping = true; |
| 5977 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5978 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5979 | // If this is a grouping paren, handle: |
| 5980 | // direct-declarator: '(' declarator ')' |
| 5981 | // direct-declarator: '(' attributes declarator ')' |
| 5982 | if (isGrouping) { |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5983 | SourceLocation EllipsisLoc = D.getEllipsisLoc(); |
| 5984 | D.setEllipsisLoc(SourceLocation()); |
| 5985 | |
Argyrios Kyrtzidis | 8ae3684 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 5986 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5987 | D.setGroupingParens(true); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5988 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5989 | // Match the ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5990 | T.consumeClose(); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 5991 | D.AddTypeInfo( |
| 5992 | DeclaratorChunk::getParen(T.getOpenLocation(), T.getCloseLocation()), |
| 5993 | std::move(attrs), T.getCloseLocation()); |
Argyrios Kyrtzidis | 8ae3684 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 5994 | |
| 5995 | D.setGroupingParens(hadGroupingParens); |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5996 | |
| 5997 | // An ellipsis cannot be placed outside parentheses. |
| 5998 | if (EllipsisLoc.isValid()) |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 5999 | DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D); |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 6000 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 6001 | return; |
| 6002 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6003 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 6004 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 6005 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 6006 | // identifier (and remember where it would have been), then call into |
| 6007 | // ParseFunctionDeclarator to handle of argument list. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6008 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 6009 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 6010 | // Enter function-declaration scope, limiting any declarators to the |
| 6011 | // function prototype scope, including parameter declarators. |
| 6012 | ParseScope PrototypeScope(this, |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 6013 | Scope::FunctionPrototypeScope | Scope::DeclScope | |
| 6014 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 6015 | ? Scope::FunctionDeclarationScope : 0)); |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 6016 | ParseFunctionDeclarator(D, attrs, T, false, RequiresArg); |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 6017 | PrototypeScope.Exit(); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 6018 | } |
| 6019 | |
| 6020 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 6021 | /// declarator D up to a paren, which indicates that we are parsing function |
| 6022 | /// arguments. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 6023 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6024 | /// If FirstArgAttrs is non-null, then the caller parsed those arguments |
| 6025 | /// immediately after the open paren - they should be considered to be the |
| 6026 | /// first argument of a parameter. |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 6027 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6028 | /// If RequiresArg is true, then the first argument of the function is required |
| 6029 | /// to be present and required to not be an identifier list. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6030 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6031 | /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt], |
| 6032 | /// (C++11) ref-qualifier[opt], exception-specification[opt], |
| 6033 | /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt]. |
| 6034 | /// |
| 6035 | /// [C++11] exception-specification: |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6036 | /// dynamic-exception-specification |
| 6037 | /// noexcept-specification |
| 6038 | /// |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6039 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6040 | ParsedAttributes &FirstArgAttrs, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6041 | BalancedDelimiterTracker &Tracker, |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 6042 | bool IsAmbiguous, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6043 | bool RequiresArg) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 6044 | assert(getCurScope()->isFunctionPrototypeScope() && |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 6045 | "Should call from a Function scope"); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6046 | // lparen is already consumed! |
| 6047 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 6048 | |
| 6049 | // This should be true when the function has typed arguments. |
| 6050 | // Otherwise, it is treated as a K&R-style function. |
| 6051 | bool HasProto = false; |
| 6052 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6053 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6054 | // Remember where we see an ellipsis, if any. |
| 6055 | SourceLocation EllipsisLoc; |
| 6056 | |
| 6057 | DeclSpec DS(AttrFactory); |
| 6058 | bool RefQualifierIsLValueRef = true; |
| 6059 | SourceLocation RefQualifierLoc; |
Douglas Gregor | e248eea | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 6060 | SourceLocation ConstQualifierLoc; |
| 6061 | SourceLocation VolatileQualifierLoc; |
Hal Finkel | 23a0739 | 2014-10-20 17:32:04 +0000 | [diff] [blame] | 6062 | SourceLocation RestrictQualifierLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6063 | ExceptionSpecificationType ESpecType = EST_None; |
| 6064 | SourceRange ESpecRange; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6065 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 6066 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6067 | ExprResult NoexceptExpr; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 6068 | CachedTokens *ExceptionSpecTokens = nullptr; |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 6069 | ParsedAttributesWithRange FnAttrs(AttrFactory); |
Richard Smith | 700537c | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 6070 | TypeResult TrailingReturnType; |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6071 | |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 6072 | /* LocalEndLoc is the end location for the local FunctionTypeLoc. |
| 6073 | EndLoc is the end location for the function declarator. |
| 6074 | They differ for trailing return types. */ |
| 6075 | SourceLocation StartLoc, LocalEndLoc, EndLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 6076 | SourceLocation LParenLoc, RParenLoc; |
| 6077 | LParenLoc = Tracker.getOpenLocation(); |
| 6078 | StartLoc = LParenLoc; |
| 6079 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6080 | if (isFunctionDeclaratorIdentifierList()) { |
| 6081 | if (RequiresArg) |
| 6082 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 6083 | |
| 6084 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 6085 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6086 | Tracker.consumeClose(); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 6087 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 6088 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 6089 | EndLoc = RParenLoc; |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 6090 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6091 | // If there are attributes following the identifier list, parse them and |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 6092 | // prohibit them. |
| 6093 | MaybeParseCXX11Attributes(FnAttrs); |
| 6094 | ProhibitAttributes(FnAttrs); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6095 | } else { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6096 | if (Tok.isNot(tok::r_paren)) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6097 | ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6098 | EllipsisLoc); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6099 | else if (RequiresArg) |
| 6100 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 6101 | |
Alexey Bader | 1f27794 | 2017-10-11 11:16:31 +0000 | [diff] [blame] | 6102 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus |
| 6103 | || getLangOpts().OpenCL; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6104 | |
| 6105 | // If we have the closing ')', eat it. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6106 | Tracker.consumeClose(); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 6107 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 6108 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 6109 | EndLoc = RParenLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6110 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6111 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6112 | // FIXME: Accept these components in any order, and produce fixits to |
| 6113 | // 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] | 6114 | // with the pure-specifier in the same way. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6115 | |
| 6116 | // Parse cv-qualifier-seq[opt]. |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 6117 | ParseTypeQualifierListOpt(DS, AR_NoAttributesParsed, |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 6118 | /*AtomicAllowed*/ false, |
| 6119 | /*IdentifierRequired=*/false, |
| 6120 | llvm::function_ref<void()>([&]() { |
| 6121 | Actions.CodeCompleteFunctionQualifiers(DS, D); |
| 6122 | })); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6123 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
| 6124 | EndLoc = DS.getSourceRange().getEnd(); |
| 6125 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 6126 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
Hal Finkel | 23a0739 | 2014-10-20 17:32:04 +0000 | [diff] [blame] | 6127 | RestrictQualifierLoc = DS.getRestrictSpecLoc(); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6128 | } |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6129 | |
| 6130 | // Parse ref-qualifier[opt]. |
Ehsan Akhgari | c07d1e2 | 2015-03-25 00:53:33 +0000 | [diff] [blame] | 6131 | if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc)) |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6132 | EndLoc = RefQualifierLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6133 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 6134 | // C++11 [expr.prim.general]p3: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 6135 | // If a declaration declares a member function or member function |
| 6136 | // 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] | 6137 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 6138 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 6139 | // declarator. |
Richard Smith | ad1bbb9 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 6140 | // FIXME: currently, "static" case isn't handled correctly. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 6141 | bool IsCXX11MemberFunction = |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6142 | getLangOpts().CPlusPlus11 && |
Richard Smith | 990a692 | 2014-01-17 21:01:18 +0000 | [diff] [blame] | 6143 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 6144 | (D.getContext() == DeclaratorContext::MemberContext |
Richard Smith | ad1bbb9 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 6145 | ? !D.getDeclSpec().isFriendSpecified() |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 6146 | : D.getContext() == DeclaratorContext::FileContext && |
Richard Smith | ad1bbb9 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 6147 | D.getCXXScopeSpec().isValid() && |
| 6148 | Actions.CurContext->isRecord()); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 6149 | Sema::CXXThisScopeRAII ThisScope(Actions, |
| 6150 | dyn_cast<CXXRecordDecl>(Actions.CurContext), |
Richard Smith | 01141a9 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 6151 | DS.getTypeQualifiers() | |
Richard Smith | 034185c | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 6152 | (D.getDeclSpec().isConstexprSpecified() && |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 6153 | !getLangOpts().CPlusPlus14 |
Richard Smith | 01141a9 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 6154 | ? Qualifiers::Const : 0), |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 6155 | IsCXX11MemberFunction); |
Richard Smith | 2331bbf | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 6156 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6157 | // Parse exception-specification[opt]. |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 6158 | bool Delayed = D.isFirstDeclarationOfMember() && |
Richard Smith | 3ef3e89 | 2014-11-20 22:32:11 +0000 | [diff] [blame] | 6159 | D.isFunctionDeclaratorAFunctionDeclaration(); |
| 6160 | if (Delayed && Actions.isLibstdcxxEagerExceptionSpecHack(D) && |
| 6161 | GetLookAheadToken(0).is(tok::kw_noexcept) && |
| 6162 | GetLookAheadToken(1).is(tok::l_paren) && |
| 6163 | GetLookAheadToken(2).is(tok::kw_noexcept) && |
| 6164 | GetLookAheadToken(3).is(tok::l_paren) && |
| 6165 | GetLookAheadToken(4).is(tok::identifier) && |
| 6166 | GetLookAheadToken(4).getIdentifierInfo()->isStr("swap")) { |
| 6167 | // HACK: We've got an exception-specification |
| 6168 | // noexcept(noexcept(swap(...))) |
| 6169 | // or |
| 6170 | // noexcept(noexcept(swap(...)) && noexcept(swap(...))) |
| 6171 | // on a 'swap' member function. This is a libstdc++ bug; the lookup |
| 6172 | // for 'swap' will only find the function we're currently declaring, |
| 6173 | // whereas it expects to find a non-member swap through ADL. Turn off |
| 6174 | // delayed parsing to give it a chance to find what it expects. |
| 6175 | Delayed = false; |
| 6176 | } |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 6177 | ESpecType = tryParseExceptionSpecification(Delayed, |
| 6178 | ESpecRange, |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 6179 | DynamicExceptions, |
| 6180 | DynamicExceptionRanges, |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 6181 | NoexceptExpr, |
| 6182 | ExceptionSpecTokens); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6183 | if (ESpecType != EST_None) |
| 6184 | EndLoc = ESpecRange.getEnd(); |
| 6185 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6186 | // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes |
| 6187 | // after the exception-specification. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 6188 | MaybeParseCXX11Attributes(FnAttrs); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6189 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6190 | // Parse trailing-return-type[opt]. |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 6191 | LocalEndLoc = EndLoc; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6192 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) { |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 6193 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 6194 | if (D.getDeclSpec().getTypeSpecType() == TST_auto) |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 6195 | StartLoc = D.getDeclSpec().getTypeSpecTypeLoc(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 6196 | LocalEndLoc = Tok.getLocation(); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 6197 | SourceRange Range; |
Richard Smith | e303e35 | 2018-02-02 22:24:54 +0000 | [diff] [blame] | 6198 | TrailingReturnType = |
| 6199 | ParseTrailingReturnType(Range, D.mayBeFollowedByCXXDirectInit()); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 6200 | EndLoc = Range.getEnd(); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6201 | } |
Aaron Ballman | 606093a | 2017-10-15 15:01:42 +0000 | [diff] [blame] | 6202 | } else if (standardAttributesAllowed()) { |
| 6203 | MaybeParseCXX11Attributes(FnAttrs); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6204 | } |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6205 | } |
| 6206 | |
Reid Kleckner | 078aea9 | 2016-12-09 17:14:05 +0000 | [diff] [blame] | 6207 | // Collect non-parameter declarations from the prototype if this is a function |
| 6208 | // declaration. They will be moved into the scope of the function. Only do |
| 6209 | // this in C and not C++, where the decls will continue to live in the |
| 6210 | // surrounding context. |
| 6211 | SmallVector<NamedDecl *, 0> DeclsInPrototype; |
| 6212 | if (getCurScope()->getFlags() & Scope::FunctionDeclarationScope && |
| 6213 | !getLangOpts().CPlusPlus) { |
| 6214 | for (Decl *D : getCurScope()->decls()) { |
| 6215 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 6216 | if (!ND || isa<ParmVarDecl>(ND)) |
| 6217 | continue; |
| 6218 | DeclsInPrototype.push_back(ND); |
| 6219 | } |
| 6220 | } |
| 6221 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6222 | // Remember that we parsed a function type, and remember the attributes. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6223 | D.AddTypeInfo(DeclaratorChunk::getFunction( |
| 6224 | HasProto, IsAmbiguous, LParenLoc, ParamInfo.data(), |
| 6225 | ParamInfo.size(), EllipsisLoc, RParenLoc, |
| 6226 | DS.getTypeQualifiers(), RefQualifierIsLValueRef, |
| 6227 | RefQualifierLoc, ConstQualifierLoc, VolatileQualifierLoc, |
| 6228 | RestrictQualifierLoc, |
| 6229 | /*MutableLoc=*/SourceLocation(), ESpecType, ESpecRange, |
| 6230 | DynamicExceptions.data(), DynamicExceptionRanges.data(), |
| 6231 | DynamicExceptions.size(), |
| 6232 | NoexceptExpr.isUsable() ? NoexceptExpr.get() : nullptr, |
| 6233 | ExceptionSpecTokens, DeclsInPrototype, StartLoc, |
| 6234 | LocalEndLoc, D, TrailingReturnType), |
| 6235 | std::move(FnAttrs), EndLoc); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6236 | } |
| 6237 | |
Ehsan Akhgari | c07d1e2 | 2015-03-25 00:53:33 +0000 | [diff] [blame] | 6238 | /// ParseRefQualifier - Parses a member function ref-qualifier. Returns |
| 6239 | /// true if a ref-qualifier is found. |
| 6240 | bool Parser::ParseRefQualifier(bool &RefQualifierIsLValueRef, |
| 6241 | SourceLocation &RefQualifierLoc) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 6242 | if (Tok.isOneOf(tok::amp, tok::ampamp)) { |
Ehsan Akhgari | c07d1e2 | 2015-03-25 00:53:33 +0000 | [diff] [blame] | 6243 | Diag(Tok, getLangOpts().CPlusPlus11 ? |
| 6244 | diag::warn_cxx98_compat_ref_qualifier : |
| 6245 | diag::ext_ref_qualifier); |
| 6246 | |
| 6247 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 6248 | RefQualifierLoc = ConsumeToken(); |
| 6249 | return true; |
| 6250 | } |
| 6251 | return false; |
| 6252 | } |
| 6253 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6254 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 6255 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 6256 | /// |
| 6257 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 6258 | /// abstract-declarators. |
| 6259 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6260 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6261 | && Tok.is(tok::identifier) |
| 6262 | && !TryAltiVecVectorToken() |
| 6263 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 6264 | // 6.7.5.3p11. |
| 6265 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 6266 | // Identifier lists follow a really simple grammar: the identifiers can |
| 6267 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 6268 | // identifier lists are really rare in the brave new modern world, and |
| 6269 | // it is very common for someone to typo a type in a non-K&R style |
| 6270 | // list. If we are presented with something like: "void foo(intptr x, |
| 6271 | // float y)", we don't want to start parsing the function declarator as |
| 6272 | // though it is a K&R style declarator just because intptr is an |
| 6273 | // invalid type. |
| 6274 | // |
| 6275 | // To handle this, we check to see if the token after the first |
| 6276 | // identifier is a "," or ")". Only then do we parse it as an |
| 6277 | // identifier list. |
Bruno Cardoso Lopes | 218c874 | 2016-09-13 20:04:35 +0000 | [diff] [blame] | 6278 | && (!Tok.is(tok::eof) && |
| 6279 | (NextToken().is(tok::comma) || NextToken().is(tok::r_paren))); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6280 | } |
| 6281 | |
| 6282 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 6283 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 6284 | /// |
| 6285 | /// After returning, ParamInfo will hold the parsed parameters. |
| 6286 | /// |
| 6287 | /// identifier-list: [C99 6.7.5] |
| 6288 | /// identifier |
| 6289 | /// identifier-list ',' identifier |
| 6290 | /// |
| 6291 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 6292 | Declarator &D, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6293 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6294 | // If there was no identifier specified for the declarator, either we are in |
| 6295 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 6296 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 6297 | // diagnose this. |
| 6298 | if (!D.getIdentifier()) |
| 6299 | Diag(Tok, diag::ext_ident_list_in_param); |
| 6300 | |
| 6301 | // Maintain an efficient lookup of params we have seen so far. |
| 6302 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 6303 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6304 | do { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6305 | // If this isn't an identifier, report the error and skip until ')'. |
| 6306 | if (Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 6307 | Diag(Tok, diag::err_expected) << tok::identifier; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 6308 | SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6309 | // Forget we parsed anything. |
| 6310 | ParamInfo.clear(); |
| 6311 | return; |
| 6312 | } |
| 6313 | |
| 6314 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 6315 | |
| 6316 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 6317 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 6318 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 6319 | |
| 6320 | // Verify that the argument identifier has not already been mentioned. |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6321 | if (!ParamsSoFar.insert(ParmII).second) { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6322 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 6323 | } else { |
| 6324 | // Remember this identifier in ParamInfo. |
| 6325 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 6326 | Tok.getLocation(), |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6327 | nullptr)); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6328 | } |
| 6329 | |
| 6330 | // Eat the identifier. |
| 6331 | ConsumeToken(); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6332 | // The list continues if we see a comma. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6333 | } while (TryConsumeToken(tok::comma)); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6334 | } |
| 6335 | |
| 6336 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 6337 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 6338 | /// identifier list. |
| 6339 | /// |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 6340 | /// D is the declarator being parsed. If FirstArgAttrs is non-null, then the |
| 6341 | /// caller parsed those arguments immediately after the open paren - they should |
| 6342 | /// be considered to be part of the first parameter. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6343 | /// |
| 6344 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 6345 | /// be the location of the ellipsis, if any was parsed. |
| 6346 | /// |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 6347 | /// parameter-type-list: [C99 6.7.5] |
| 6348 | /// parameter-list |
| 6349 | /// parameter-list ',' '...' |
Douglas Gregor | 9bfc2e5 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 6350 | /// [C++] parameter-list '...' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 6351 | /// |
| 6352 | /// parameter-list: [C99 6.7.5] |
| 6353 | /// parameter-declaration |
| 6354 | /// parameter-list ',' parameter-declaration |
| 6355 | /// |
| 6356 | /// parameter-declaration: [C99 6.7.5] |
| 6357 | /// declaration-specifiers declarator |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 6358 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 6359 | /// [C++11] initializer-clause |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 6360 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 6361 | /// declaration-specifiers abstract-declarator[opt] |
| 6362 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 6363 | /// '=' assignment-expression |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 6364 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 6365 | /// [C++11] attribute-specifier-seq parameter-declaration |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 6366 | /// |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6367 | void Parser::ParseParameterDeclarationClause( |
| 6368 | Declarator &D, |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 6369 | ParsedAttributes &FirstArgAttrs, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6370 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6371 | SourceLocation &EllipsisLoc) { |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6372 | do { |
| 6373 | // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq |
| 6374 | // before deciding this was a parameter-declaration-clause. |
| 6375 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6376 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6377 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6378 | // Parse the declaration-specifiers. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 6379 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 6380 | DeclSpec DS(AttrFactory); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6381 | |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 6382 | // Parse any C++11 attributes. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 6383 | MaybeParseCXX11Attributes(DS.getAttributes()); |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 6384 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 6385 | // Skip any Microsoft attributes before a param. |
Chad Rosier | f8a2e70 | 2012-12-20 20:37:53 +0000 | [diff] [blame] | 6386 | MaybeParseMicrosoftAttributes(DS.getAttributes()); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 6387 | |
| 6388 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 6389 | |
| 6390 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 6391 | // 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] | 6392 | // 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] | 6393 | // get rid of a parameter (FirstArgAttrs) and this statement. It might be |
| 6394 | // too much hassle. |
| 6395 | DS.takeAttributesFrom(FirstArgAttrs); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 6396 | |
Faisal Vali | a534f07 | 2018-04-26 00:42:40 +0000 | [diff] [blame] | 6397 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6398 | |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6399 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6400 | // Parse the declarator. This is "PrototypeContext" or |
| 6401 | // "LambdaExprParameterContext", because we must accept either |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6402 | // 'declarator' or 'abstract-declarator' here. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 6403 | Declarator ParmDeclarator( |
| 6404 | DS, D.getContext() == DeclaratorContext::LambdaExprContext |
| 6405 | ? DeclaratorContext::LambdaExprParameterContext |
| 6406 | : DeclaratorContext::PrototypeContext); |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6407 | ParseDeclarator(ParmDeclarator); |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6408 | |
| 6409 | // Parse GNU attributes, if present. |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6410 | MaybeParseGNUAttributes(ParmDeclarator); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6411 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6412 | // Remember this parsed parameter in ParamInfo. |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6413 | IdentifierInfo *ParmII = ParmDeclarator.getIdentifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6414 | |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6415 | // DefArgToks is used when the parsing of default arguments needs |
| 6416 | // to be delayed. |
Malcolm Parsons | ff0382c | 2016-11-17 17:52:58 +0000 | [diff] [blame] | 6417 | std::unique_ptr<CachedTokens> DefArgToks; |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6418 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6419 | // If no parameter was specified, verify that *something* was specified, |
| 6420 | // otherwise we have a missing type and identifier. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6421 | if (DS.isEmpty() && ParmDeclarator.getIdentifier() == nullptr && |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6422 | ParmDeclarator.getNumTypeObjects() == 0) { |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6423 | // Completely missing, emit error. |
| 6424 | Diag(DSStart, diag::err_missing_param); |
| 6425 | } else { |
| 6426 | // Otherwise, we have something. Add it and let semantic analysis try |
| 6427 | // 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] | 6428 | |
Richard Smith | 36ee9fb | 2014-08-11 23:30:23 +0000 | [diff] [blame] | 6429 | // Last chance to recover from a misplaced ellipsis in an attempted |
| 6430 | // parameter pack declaration. |
| 6431 | if (Tok.is(tok::ellipsis) && |
| 6432 | (NextToken().isNot(tok::r_paren) || |
| 6433 | (!ParmDeclarator.getEllipsisLoc().isValid() && |
| 6434 | !Actions.isUnexpandedParameterPackPermitted())) && |
| 6435 | Actions.containsUnexpandedParameterPacks(ParmDeclarator)) |
| 6436 | DiagnoseMisplacedEllipsisInDeclarator(ConsumeToken(), ParmDeclarator); |
| 6437 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6438 | // Inform the actions module about the parameter declarator, so it gets |
| 6439 | // added to the current scope. |
Richard Smith | 95e1fb0 | 2014-08-27 03:23:12 +0000 | [diff] [blame] | 6440 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator); |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 6441 | // Parse the default argument, if any. We parse the default |
| 6442 | // arguments in all dialects; the semantic analysis in |
| 6443 | // ActOnParamDefaultArgument will reject the default argument in |
| 6444 | // C. |
| 6445 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 6446 | SourceLocation EqualLoc = Tok.getLocation(); |
| 6447 | |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 6448 | // Parse the default argument |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 6449 | if (D.getContext() == DeclaratorContext::MemberContext) { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6450 | // If we're inside a class definition, cache the tokens |
| 6451 | // corresponding to the default argument. We'll actually parse |
| 6452 | // them when we see the end of the class definition. |
Malcolm Parsons | ff0382c | 2016-11-17 17:52:58 +0000 | [diff] [blame] | 6453 | DefArgToks.reset(new CachedTokens); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6454 | |
David Majnemer | 906ed27 | 2015-01-13 05:28:24 +0000 | [diff] [blame] | 6455 | SourceLocation ArgStartLoc = NextToken().getLocation(); |
Richard Smith | 1fff95c | 2013-09-12 23:28:08 +0000 | [diff] [blame] | 6456 | if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) { |
Malcolm Parsons | ff0382c | 2016-11-17 17:52:58 +0000 | [diff] [blame] | 6457 | DefArgToks.reset(); |
Serge Pavlov | b4b3578 | 2014-07-22 01:54:49 +0000 | [diff] [blame] | 6458 | Actions.ActOnParamDefaultArgumentError(Param, EqualLoc); |
Argyrios Kyrtzidis | 249179c | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 6459 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6460 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
David Majnemer | 906ed27 | 2015-01-13 05:28:24 +0000 | [diff] [blame] | 6461 | ArgStartLoc); |
Argyrios Kyrtzidis | 249179c | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 6462 | } |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 6463 | } else { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6464 | // Consume the '='. |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 6465 | ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6466 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 6467 | // The argument isn't actually potentially evaluated unless it is |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 6468 | // used. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 6469 | EnterExpressionEvaluationContext Eval( |
| 6470 | Actions, |
| 6471 | Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed, |
| 6472 | Param); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 6473 | |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 6474 | ExprResult DefArgResult; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6475 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
Sebastian Redl | 1678d5f | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 6476 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 6477 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 1678d5f | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 6478 | } else |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 6479 | DefArgResult = ParseAssignmentExpression(); |
Kaelyn Takata | b16e632 | 2014-11-20 22:06:40 +0000 | [diff] [blame] | 6480 | DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6481 | if (DefArgResult.isInvalid()) { |
Serge Pavlov | b4b3578 | 2014-07-22 01:54:49 +0000 | [diff] [blame] | 6482 | Actions.ActOnParamDefaultArgumentError(Param, EqualLoc); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 6483 | SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6484 | } else { |
| 6485 | // Inform the actions module about the default argument |
| 6486 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6487 | DefArgResult.get()); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6488 | } |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 6489 | } |
| 6490 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6491 | |
| 6492 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6493 | ParmDeclarator.getIdentifierLoc(), |
Malcolm Parsons | ff0382c | 2016-11-17 17:52:58 +0000 | [diff] [blame] | 6494 | Param, std::move(DefArgToks))); |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6495 | } |
| 6496 | |
Richard Smith | 36ee9fb | 2014-08-11 23:30:23 +0000 | [diff] [blame] | 6497 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) { |
| 6498 | if (!getLangOpts().CPlusPlus) { |
| 6499 | // We have ellipsis without a preceding ',', which is ill-formed |
| 6500 | // in C. Complain and provide the fix. |
| 6501 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
| 6502 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
| 6503 | } else if (ParmDeclarator.getEllipsisLoc().isValid() || |
| 6504 | Actions.containsUnexpandedParameterPacks(ParmDeclarator)) { |
| 6505 | // It looks like this was supposed to be a parameter pack. Warn and |
| 6506 | // point out where the ellipsis should have gone. |
| 6507 | SourceLocation ParmEllipsis = ParmDeclarator.getEllipsisLoc(); |
| 6508 | Diag(EllipsisLoc, diag::warn_misplaced_ellipsis_vararg) |
| 6509 | << ParmEllipsis.isValid() << ParmEllipsis; |
| 6510 | if (ParmEllipsis.isValid()) { |
| 6511 | Diag(ParmEllipsis, |
| 6512 | diag::note_misplaced_ellipsis_vararg_existing_ellipsis); |
| 6513 | } else { |
| 6514 | Diag(ParmDeclarator.getIdentifierLoc(), |
| 6515 | diag::note_misplaced_ellipsis_vararg_add_ellipsis) |
| 6516 | << FixItHint::CreateInsertion(ParmDeclarator.getIdentifierLoc(), |
| 6517 | "...") |
| 6518 | << !ParmDeclarator.hasName(); |
| 6519 | } |
| 6520 | Diag(EllipsisLoc, diag::note_misplaced_ellipsis_vararg_add_comma) |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6521 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Richard Smith | 36ee9fb | 2014-08-11 23:30:23 +0000 | [diff] [blame] | 6522 | } |
| 6523 | |
| 6524 | // We can't have any more parameters after an ellipsis. |
Douglas Gregor | 9bfc2e5 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 6525 | break; |
| 6526 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6527 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6528 | // If the next token is a comma, consume it and keep reading arguments. |
| 6529 | } while (TryConsumeToken(tok::comma)); |
Chris Lattner | 6c940e6 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 6530 | } |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 6531 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6532 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 6533 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 6534 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 6535 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 6536 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6537 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 6538 | /// attribute-specifier-seq[opt] |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6539 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6540 | if (CheckProhibitedCXX11Attribute()) |
| 6541 | return; |
| 6542 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6543 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 6544 | T.consumeOpen(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6545 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6546 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 6547 | // This code does a fast path to handle some of the most obvious cases. |
| 6548 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6549 | T.consumeClose(); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 6550 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 6551 | MaybeParseCXX11Attributes(attrs); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 6552 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6553 | // Remember that we parsed the empty array type. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6554 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, nullptr, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6555 | T.getOpenLocation(), |
| 6556 | T.getCloseLocation()), |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6557 | std::move(attrs), T.getCloseLocation()); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6558 | return; |
| 6559 | } else if (Tok.getKind() == tok::numeric_constant && |
| 6560 | GetLookAheadToken(1).is(tok::r_square)) { |
| 6561 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 6562 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6563 | ConsumeToken(); |
| 6564 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6565 | T.consumeClose(); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 6566 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 6567 | MaybeParseCXX11Attributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6568 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6569 | // Remember that we parsed a array type, and remember its features. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6570 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, ExprRes.get(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6571 | T.getOpenLocation(), |
| 6572 | T.getCloseLocation()), |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6573 | std::move(attrs), T.getCloseLocation()); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6574 | return; |
Benjamin Kramer | 72dae62 | 2016-02-18 15:30:24 +0000 | [diff] [blame] | 6575 | } else if (Tok.getKind() == tok::code_completion) { |
| 6576 | Actions.CodeCompleteBracketDeclarator(getCurScope()); |
| 6577 | return cutOffParsing(); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6578 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6579 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6580 | // If valid, this location is the position where we read the 'static' keyword. |
| 6581 | SourceLocation StaticLoc; |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6582 | TryConsumeToken(tok::kw_static, StaticLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6583 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6584 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 6585 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 6586 | DeclSpec DS(AttrFactory); |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 6587 | ParseTypeQualifierListOpt(DS, AR_CXX11AttributesParsed); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6588 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6589 | // If we haven't already read 'static', check to see if there is one after the |
| 6590 | // type-qualifier-list. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6591 | if (!StaticLoc.isValid()) |
| 6592 | TryConsumeToken(tok::kw_static, StaticLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6593 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6594 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6595 | bool isStar = false; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6596 | ExprResult NumElements; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6597 | |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 6598 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 6599 | // 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] | 6600 | // the token after the star is a ']'. Since stars in arrays are |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 6601 | // infrequent, use of lookahead is not costly here. |
| 6602 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | c439f0d | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 6603 | ConsumeToken(); // Eat the '*'. |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 6604 | |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 6605 | if (StaticLoc.isValid()) { |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 6606 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 6607 | StaticLoc = SourceLocation(); // Drop the static. |
| 6608 | } |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 6609 | isStar = true; |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 6610 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6611 | // Note, in C89, this production uses the constant-expr production instead |
| 6612 | // of assignment-expr. The only difference is that assignment-expr allows |
| 6613 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 6614 | // 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] | 6615 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6616 | // Parse the constant-expression or assignment-expression now (depending |
| 6617 | // on dialect). |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6618 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6619 | NumElements = ParseConstantExpression(); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 6620 | } else { |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 6621 | EnterExpressionEvaluationContext Unevaluated( |
| 6622 | Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Kaelyn Takata | 1586782 | 2014-11-21 18:48:04 +0000 | [diff] [blame] | 6623 | NumElements = |
| 6624 | Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression()); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 6625 | } |
David Majnemer | f9834d5 | 2014-08-08 07:21:18 +0000 | [diff] [blame] | 6626 | } else { |
| 6627 | if (StaticLoc.isValid()) { |
| 6628 | Diag(StaticLoc, diag::err_unspecified_size_with_static); |
| 6629 | StaticLoc = SourceLocation(); // Drop the static. |
| 6630 | } |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 6631 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6632 | |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 6633 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 6634 | if (NumElements.isInvalid()) { |
Chris Lattner | cd2a8c5 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 6635 | D.setInvalidType(true); |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 6636 | // If the expression was invalid, skip it. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 6637 | SkipUntil(tok::r_square, StopAtSemi); |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 6638 | return; |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6639 | } |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 6640 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6641 | T.consumeClose(); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 6642 | |
Jordan Rose | 303e2f1 | 2016-11-10 23:28:17 +0000 | [diff] [blame] | 6643 | MaybeParseCXX11Attributes(DS.getAttributes()); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 6644 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6645 | // Remember that we parsed a array type, and remember its features. |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6646 | D.AddTypeInfo( |
| 6647 | DeclaratorChunk::getArray(DS.getTypeQualifiers(), StaticLoc.isValid(), |
| 6648 | isStar, NumElements.get(), T.getOpenLocation(), |
| 6649 | T.getCloseLocation()), |
| 6650 | std::move(DS.getAttributes()), T.getCloseLocation()); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6651 | } |
| 6652 | |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 6653 | /// Diagnose brackets before an identifier. |
| 6654 | void Parser::ParseMisplacedBracketDeclarator(Declarator &D) { |
| 6655 | assert(Tok.is(tok::l_square) && "Missing opening bracket"); |
| 6656 | assert(!D.mayOmitIdentifier() && "Declarator cannot omit identifier"); |
| 6657 | |
| 6658 | SourceLocation StartBracketLoc = Tok.getLocation(); |
| 6659 | Declarator TempDeclarator(D.getDeclSpec(), D.getContext()); |
| 6660 | |
| 6661 | while (Tok.is(tok::l_square)) { |
| 6662 | ParseBracketDeclarator(TempDeclarator); |
| 6663 | } |
| 6664 | |
| 6665 | // Stuff the location of the start of the brackets into the Declarator. |
| 6666 | // The diagnostics from ParseDirectDeclarator will make more sense if |
| 6667 | // they use this location instead. |
| 6668 | if (Tok.is(tok::semi)) |
| 6669 | D.getName().EndLocation = StartBracketLoc; |
| 6670 | |
| 6671 | SourceLocation SuggestParenLoc = Tok.getLocation(); |
| 6672 | |
| 6673 | // Now that the brackets are removed, try parsing the declarator again. |
| 6674 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
| 6675 | |
| 6676 | // Something went wrong parsing the brackets, in which case, |
| 6677 | // ParseBracketDeclarator has emitted an error, and we don't need to emit |
| 6678 | // one here. |
| 6679 | if (TempDeclarator.getNumTypeObjects() == 0) |
| 6680 | return; |
| 6681 | |
| 6682 | // Determine if parens will need to be suggested in the diagnostic. |
| 6683 | bool NeedParens = false; |
| 6684 | if (D.getNumTypeObjects() != 0) { |
| 6685 | switch (D.getTypeObject(D.getNumTypeObjects() - 1).Kind) { |
| 6686 | case DeclaratorChunk::Pointer: |
| 6687 | case DeclaratorChunk::Reference: |
| 6688 | case DeclaratorChunk::BlockPointer: |
| 6689 | case DeclaratorChunk::MemberPointer: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 6690 | case DeclaratorChunk::Pipe: |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 6691 | NeedParens = true; |
| 6692 | break; |
| 6693 | case DeclaratorChunk::Array: |
| 6694 | case DeclaratorChunk::Function: |
| 6695 | case DeclaratorChunk::Paren: |
| 6696 | break; |
| 6697 | } |
| 6698 | } |
| 6699 | |
| 6700 | if (NeedParens) { |
| 6701 | // Create a DeclaratorChunk for the inserted parens. |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 6702 | SourceLocation EndLoc = PP.getLocForEndOfToken(D.getEndLoc()); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6703 | D.AddTypeInfo(DeclaratorChunk::getParen(SuggestParenLoc, EndLoc), |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 6704 | SourceLocation()); |
| 6705 | } |
| 6706 | |
| 6707 | // Adding back the bracket info to the end of the Declarator. |
| 6708 | for (unsigned i = 0, e = TempDeclarator.getNumTypeObjects(); i < e; ++i) { |
| 6709 | const DeclaratorChunk &Chunk = TempDeclarator.getTypeObject(i); |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 6710 | D.AddTypeInfo(Chunk, SourceLocation()); |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 6711 | } |
| 6712 | |
| 6713 | // The missing identifier would have been diagnosed in ParseDirectDeclarator. |
| 6714 | // If parentheses are required, always suggest them. |
| 6715 | if (!D.getIdentifier() && !NeedParens) |
| 6716 | return; |
| 6717 | |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 6718 | SourceLocation EndBracketLoc = TempDeclarator.getEndLoc(); |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 6719 | |
| 6720 | // Generate the move bracket error message. |
| 6721 | SourceRange BracketRange(StartBracketLoc, EndBracketLoc); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 6722 | SourceLocation EndLoc = PP.getLocForEndOfToken(D.getEndLoc()); |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 6723 | |
| 6724 | if (NeedParens) { |
| 6725 | Diag(EndLoc, diag::err_brackets_go_after_unqualified_id) |
| 6726 | << getLangOpts().CPlusPlus |
| 6727 | << FixItHint::CreateInsertion(SuggestParenLoc, "(") |
| 6728 | << FixItHint::CreateInsertion(EndLoc, ")") |
| 6729 | << FixItHint::CreateInsertionFromRange( |
| 6730 | EndLoc, CharSourceRange(BracketRange, true)) |
| 6731 | << FixItHint::CreateRemoval(BracketRange); |
| 6732 | } else { |
| 6733 | Diag(EndLoc, diag::err_brackets_go_after_unqualified_id) |
| 6734 | << getLangOpts().CPlusPlus |
| 6735 | << FixItHint::CreateInsertionFromRange( |
| 6736 | EndLoc, CharSourceRange(BracketRange, true)) |
| 6737 | << FixItHint::CreateRemoval(BracketRange); |
| 6738 | } |
| 6739 | } |
| 6740 | |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 6741 | /// [GNU] typeof-specifier: |
| 6742 | /// typeof ( expressions ) |
| 6743 | /// typeof ( type-name ) |
| 6744 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 6745 | /// |
| 6746 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 6747 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6748 | Token OpTok = Tok; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 6749 | SourceLocation StartLoc = ConsumeToken(); |
| 6750 | |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 6751 | const bool hasParens = Tok.is(tok::l_paren); |
| 6752 | |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 6753 | EnterExpressionEvaluationContext Unevaluated( |
| 6754 | Actions, Sema::ExpressionEvaluationContext::Unevaluated, |
| 6755 | Sema::ReuseLambdaContextDecl); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 6756 | |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6757 | bool isCastExpr; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6758 | ParsedType CastTy; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6759 | SourceRange CastRange; |
Kaelyn Takata | 91126074 | 2014-12-19 01:28:40 +0000 | [diff] [blame] | 6760 | ExprResult Operand = Actions.CorrectDelayedTyposInExpr( |
| 6761 | ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, CastTy, CastRange)); |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 6762 | if (hasParens) |
| 6763 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6764 | |
| 6765 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 6766 | // FIXME: Not accurate, the range gets one token more than it should. |
| 6767 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6768 | else |
| 6769 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6770 | |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6771 | if (isCastExpr) { |
| 6772 | if (!CastTy) { |
| 6773 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 6774 | return; |
Douglas Gregor | 220cac5 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 6775 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 6776 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6777 | const char *PrevSpec = nullptr; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 6778 | unsigned DiagID; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6779 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 6780 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6781 | DiagID, CastTy, |
| 6782 | Actions.getASTContext().getPrintingPolicy())) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 6783 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6784 | return; |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 6785 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 6786 | |
Hiroshi Inoue | 533777f | 2017-07-02 06:12:49 +0000 | [diff] [blame] | 6787 | // If we get here, the operand to the typeof was an expression. |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 6788 | if (Operand.isInvalid()) { |
| 6789 | DS.SetTypeSpecError(); |
Steve Naroff | 4bd2f71 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 6790 | return; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 6791 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 6792 | |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 6793 | // We might need to transform the operand if it is potentially evaluated. |
| 6794 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 6795 | if (Operand.isInvalid()) { |
| 6796 | DS.SetTypeSpecError(); |
| 6797 | return; |
| 6798 | } |
| 6799 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6800 | const char *PrevSpec = nullptr; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 6801 | unsigned DiagID; |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 6802 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 6803 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6804 | DiagID, Operand.get(), |
| 6805 | Actions.getASTContext().getPrintingPolicy())) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 6806 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 6807 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6808 | |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 6809 | /// [C11] atomic-specifier: |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6810 | /// _Atomic ( type-name ) |
| 6811 | /// |
| 6812 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 6813 | assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) && |
| 6814 | "Not an atomic specifier"); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6815 | |
| 6816 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6817 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 6818 | if (T.consumeOpen()) |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6819 | return; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6820 | |
| 6821 | TypeResult Result = ParseTypeName(); |
| 6822 | if (Result.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 6823 | SkipUntil(tok::r_paren, StopAtSemi); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6824 | return; |
| 6825 | } |
| 6826 | |
| 6827 | // Match the ')' |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6828 | T.consumeClose(); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6829 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6830 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6831 | return; |
| 6832 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6833 | DS.setTypeofParensRange(T.getRange()); |
| 6834 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6835 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6836 | const char *PrevSpec = nullptr; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6837 | unsigned DiagID; |
| 6838 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6839 | DiagID, Result.get(), |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6840 | Actions.getASTContext().getPrintingPolicy())) |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6841 | Diag(StartLoc, DiagID) << PrevSpec; |
| 6842 | } |
| 6843 | |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6844 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 6845 | /// from TryAltiVecVectorToken. |
| 6846 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 6847 | Token Next = NextToken(); |
| 6848 | switch (Next.getKind()) { |
| 6849 | default: return false; |
| 6850 | case tok::kw_short: |
| 6851 | case tok::kw_long: |
| 6852 | case tok::kw_signed: |
| 6853 | case tok::kw_unsigned: |
| 6854 | case tok::kw_void: |
| 6855 | case tok::kw_char: |
| 6856 | case tok::kw_int: |
| 6857 | case tok::kw_float: |
| 6858 | case tok::kw_double: |
| 6859 | case tok::kw_bool: |
Bill Seurer | cf2c96b | 2015-01-12 19:35:51 +0000 | [diff] [blame] | 6860 | case tok::kw___bool: |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6861 | case tok::kw___pixel: |
| 6862 | Tok.setKind(tok::kw___vector); |
| 6863 | return true; |
| 6864 | case tok::identifier: |
| 6865 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 6866 | Tok.setKind(tok::kw___vector); |
| 6867 | return true; |
| 6868 | } |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 6869 | if (Next.getIdentifierInfo() == Ident_bool) { |
| 6870 | Tok.setKind(tok::kw___vector); |
| 6871 | return true; |
| 6872 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6873 | return false; |
| 6874 | } |
| 6875 | } |
| 6876 | |
| 6877 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 6878 | const char *&PrevSpec, unsigned &DiagID, |
| 6879 | bool &isInvalid) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6880 | const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy(); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6881 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 6882 | Token Next = NextToken(); |
| 6883 | switch (Next.getKind()) { |
| 6884 | case tok::kw_short: |
| 6885 | case tok::kw_long: |
| 6886 | case tok::kw_signed: |
| 6887 | case tok::kw_unsigned: |
| 6888 | case tok::kw_void: |
| 6889 | case tok::kw_char: |
| 6890 | case tok::kw_int: |
| 6891 | case tok::kw_float: |
| 6892 | case tok::kw_double: |
| 6893 | case tok::kw_bool: |
Bill Seurer | cf2c96b | 2015-01-12 19:35:51 +0000 | [diff] [blame] | 6894 | case tok::kw___bool: |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6895 | case tok::kw___pixel: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6896 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6897 | return true; |
| 6898 | case tok::identifier: |
| 6899 | if (Next.getIdentifierInfo() == Ident_pixel) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6900 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6901 | return true; |
| 6902 | } |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 6903 | if (Next.getIdentifierInfo() == Ident_bool) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6904 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy); |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 6905 | return true; |
| 6906 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6907 | break; |
| 6908 | default: |
| 6909 | break; |
| 6910 | } |
Douglas Gregor | 9938e3b | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 6911 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6912 | DS.isTypeAltiVecVector()) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6913 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6914 | return true; |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 6915 | } else if ((Tok.getIdentifierInfo() == Ident_bool) && |
| 6916 | DS.isTypeAltiVecVector()) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6917 | isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy); |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 6918 | return true; |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6919 | } |
| 6920 | return false; |
| 6921 | } |