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" |
Benjamin Kramer | d7d2b1f | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 18 | #include "clang/Basic/AddressSpaces.h" |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Attributes.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 20 | #include "clang/Basic/CharInfo.h" |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 21 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Parse/ParseDiagnostic.h" |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 23 | #include "clang/Sema/Lookup.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 24 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 25 | #include "clang/Sema/PrettyDeclStackTrace.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" |
Anastasia Stulova | 43ab9a0 | 2016-05-12 16:28:25 +0000 | [diff] [blame] | 32 | #include "llvm/Support/ScopedPrinter.h" |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 33 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | // C99 6.7: Declarations. |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 40 | /// ParseTypeName |
| 41 | /// type-name: [C99 6.7.6] |
| 42 | /// specifier-qualifier-list abstract-declarator[opt] |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 43 | /// |
| 44 | /// Called type-id in C++. |
Douglas Gregor | 205d5e3 | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 45 | TypeResult Parser::ParseTypeName(SourceRange *Range, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 46 | Declarator::TheContext Context, |
Richard Smith | cd1c055 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 47 | AccessSpecifier AS, |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 48 | Decl **OwnedType, |
| 49 | ParsedAttributes *Attrs) { |
Richard Smith | 62dad82 | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 50 | DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context); |
Richard Smith | 2f07ad5 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 51 | if (DSC == DSC_normal) |
| 52 | DSC = DSC_type_specifier; |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 53 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 54 | // Parse the common declaration-specifiers piece. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 55 | DeclSpec DS(AttrFactory); |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 56 | if (Attrs) |
| 57 | DS.addAttributes(Attrs->getList()); |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 58 | ParseSpecifierQualifierList(DS, AS, DSC); |
Richard Smith | cd1c055 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 59 | if (OwnedType) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 60 | *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : nullptr; |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 61 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 62 | // Parse the abstract-declarator, if present. |
Douglas Gregor | 205d5e3 | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 63 | Declarator DeclaratorInfo(DS, Context); |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 64 | ParseDeclarator(DeclaratorInfo); |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 65 | if (Range) |
| 66 | *Range = DeclaratorInfo.getSourceRange(); |
| 67 | |
Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 68 | if (DeclaratorInfo.isInvalidType()) |
Douglas Gregor | 220cac5 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 69 | return true; |
| 70 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 71 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 72 | } |
| 73 | |
George Burgess IV | 779af82 | 2017-06-30 22:33:24 +0000 | [diff] [blame] | 74 | /// \brief Normalizes an attribute name by dropping prefixed and suffixed __. |
| 75 | static StringRef normalizeAttrName(StringRef Name) { |
| 76 | if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__")) |
| 77 | return Name.drop_front(2).drop_back(2); |
| 78 | return Name; |
| 79 | } |
| 80 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 81 | /// isAttributeLateParsed - Return true if the attribute has arguments that |
| 82 | /// require late parsing. |
| 83 | static bool isAttributeLateParsed(const IdentifierInfo &II) { |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 84 | #define CLANG_ATTR_LATE_PARSED_LIST |
George Burgess IV | 779af82 | 2017-06-30 22:33:24 +0000 | [diff] [blame] | 85 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 86 | #include "clang/Parse/AttrParserStringSwitches.inc" |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 87 | .Default(false); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 88 | #undef CLANG_ATTR_LATE_PARSED_LIST |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 91 | /// ParseGNUAttributes - Parse a non-empty attributes list. |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 92 | /// |
| 93 | /// [GNU] attributes: |
| 94 | /// attribute |
| 95 | /// attributes attribute |
| 96 | /// |
| 97 | /// [GNU] attribute: |
| 98 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 99 | /// |
| 100 | /// [GNU] attribute-list: |
| 101 | /// attrib |
| 102 | /// attribute_list ',' attrib |
| 103 | /// |
| 104 | /// [GNU] attrib: |
| 105 | /// empty |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 106 | /// attrib-name |
| 107 | /// attrib-name '(' identifier ')' |
| 108 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 109 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 110 | /// |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 111 | /// [GNU] attrib-name: |
| 112 | /// identifier |
| 113 | /// typespec |
| 114 | /// typequal |
| 115 | /// storageclass |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | /// |
Richard Smith | f7ca0c0 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 117 | /// Whether an attribute takes an 'identifier' is determined by the |
| 118 | /// attrib-name. GCC's behavior here is not worth imitating: |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 119 | /// |
Richard Smith | f7ca0c0 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 120 | /// * In C mode, if the attribute argument list starts with an identifier |
| 121 | /// followed by a ',' or an ')', and the identifier doesn't resolve to |
| 122 | /// a type, it is parsed as an identifier. If the attribute actually |
| 123 | /// wanted an expression, it's out of luck (but it turns out that no |
| 124 | /// attributes work that way, because C constant expressions are very |
| 125 | /// limited). |
| 126 | /// * In C++ mode, if the attribute argument list starts with an identifier, |
| 127 | /// and the attribute *wants* an identifier, it is parsed as an identifier. |
| 128 | /// At block scope, any additional tokens between the identifier and the |
| 129 | /// ',' or ')' are ignored, otherwise they produce a parse error. |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 130 | /// |
Richard Smith | f7ca0c0 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 131 | /// 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] | 132 | void Parser::ParseGNUAttributes(ParsedAttributes &attrs, |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 133 | SourceLocation *endLoc, |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 134 | LateParsedAttrList *LateAttrs, |
| 135 | Declarator *D) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 136 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 138 | while (Tok.is(tok::kw___attribute)) { |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 139 | ConsumeToken(); |
| 140 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 141 | "attribute")) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 142 | SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ; |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 143 | return; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 144 | } |
| 145 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 146 | SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ; |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 147 | return; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 148 | } |
| 149 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 150 | while (true) { |
| 151 | // Allow empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 152 | if (TryConsumeToken(tok::comma)) |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 153 | continue; |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 154 | |
| 155 | // Expect an identifier or declaration specifier (const, int, etc.) |
David Majnemer | 22fe771 | 2015-01-03 19:41:00 +0000 | [diff] [blame] | 156 | if (Tok.isAnnotation()) |
| 157 | break; |
| 158 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 159 | if (!AttrName) |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 160 | break; |
| 161 | |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 162 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 | |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 164 | if (Tok.isNot(tok::l_paren)) { |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 165 | attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 166 | AttributeList::AS_GNU); |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 167 | continue; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 168 | } |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 169 | |
| 170 | // Handle "parameterized" attributes |
| 171 | if (!LateAttrs || !isAttributeLateParsed(*AttrName)) { |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 172 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc, nullptr, |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 173 | SourceLocation(), AttributeList::AS_GNU, D); |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 174 | continue; |
| 175 | } |
| 176 | |
| 177 | // Handle attributes with arguments that require late parsing. |
| 178 | LateParsedAttribute *LA = |
| 179 | new LateParsedAttribute(this, *AttrName, AttrNameLoc); |
| 180 | LateAttrs->push_back(LA); |
| 181 | |
| 182 | // Attributes in a class are parsed at the end of the class, along |
| 183 | // with other late-parsed declarations. |
| 184 | if (!ClassStack.empty() && !LateAttrs->parseSoon()) |
| 185 | getCurrentClass().LateParsedDeclarations.push_back(LA); |
| 186 | |
George Burgess IV | c8b9537 | 2017-01-04 22:43:01 +0000 | [diff] [blame] | 187 | // Be sure ConsumeAndStoreUntil doesn't see the start l_paren, since it |
| 188 | // recursively consumes balanced parens. |
| 189 | LA->Toks.push_back(Tok); |
| 190 | ConsumeParen(); |
| 191 | // Consume everything up to and including the matching right parens. |
| 192 | ConsumeAndStoreUntil(tok::r_paren, LA->Toks, /*StopAtSemi=*/true); |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 193 | |
| 194 | Token Eof; |
| 195 | Eof.startToken(); |
| 196 | Eof.setLocation(Tok.getLocation()); |
| 197 | LA->Toks.push_back(Eof); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 198 | } |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 199 | |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 200 | if (ExpectAndConsume(tok::r_paren)) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 201 | SkipUntil(tok::r_paren, StopAtSemi); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 202 | SourceLocation Loc = Tok.getLocation(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 203 | if (ExpectAndConsume(tok::r_paren)) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 204 | SkipUntil(tok::r_paren, StopAtSemi); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 205 | if (endLoc) |
| 206 | *endLoc = Loc; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 207 | } |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 208 | } |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 209 | |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 210 | /// \brief Determine whether the given attribute has an identifier argument. |
| 211 | static bool attributeHasIdentifierArg(const IdentifierInfo &II) { |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 212 | #define CLANG_ATTR_IDENTIFIER_ARG_LIST |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 213 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 214 | #include "clang/Parse/AttrParserStringSwitches.inc" |
Douglas Gregor | d2472d4 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 215 | .Default(false); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 216 | #undef CLANG_ATTR_IDENTIFIER_ARG_LIST |
Douglas Gregor | d2472d4 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 217 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 218 | |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 219 | /// \brief Determine whether the given attribute parses a type argument. |
| 220 | static bool attributeIsTypeArgAttr(const IdentifierInfo &II) { |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 221 | #define CLANG_ATTR_TYPE_ARG_LIST |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 222 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 223 | #include "clang/Parse/AttrParserStringSwitches.inc" |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 224 | .Default(false); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 225 | #undef CLANG_ATTR_TYPE_ARG_LIST |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Aaron Ballman | 15b27b9 | 2014-01-09 19:39:35 +0000 | [diff] [blame] | 228 | /// \brief Determine whether the given attribute requires parsing its arguments |
| 229 | /// in an unevaluated context or not. |
| 230 | static bool attributeParsedArgsUnevaluated(const IdentifierInfo &II) { |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 231 | #define CLANG_ATTR_ARG_CONTEXT_LIST |
Aaron Ballman | 15b27b9 | 2014-01-09 19:39:35 +0000 | [diff] [blame] | 232 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 233 | #include "clang/Parse/AttrParserStringSwitches.inc" |
Aaron Ballman | 15b27b9 | 2014-01-09 19:39:35 +0000 | [diff] [blame] | 234 | .Default(false); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 235 | #undef CLANG_ATTR_ARG_CONTEXT_LIST |
Aaron Ballman | 15b27b9 | 2014-01-09 19:39:35 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 238 | IdentifierLoc *Parser::ParseIdentifierLoc() { |
| 239 | assert(Tok.is(tok::identifier) && "expected an identifier"); |
| 240 | IdentifierLoc *IL = IdentifierLoc::create(Actions.Context, |
| 241 | Tok.getLocation(), |
| 242 | Tok.getIdentifierInfo()); |
| 243 | ConsumeToken(); |
| 244 | return IL; |
| 245 | } |
| 246 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 247 | void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName, |
| 248 | SourceLocation AttrNameLoc, |
| 249 | ParsedAttributes &Attrs, |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 250 | SourceLocation *EndLoc, |
| 251 | IdentifierInfo *ScopeName, |
| 252 | SourceLocation ScopeLoc, |
| 253 | AttributeList::Syntax Syntax) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 254 | BalancedDelimiterTracker Parens(*this, tok::l_paren); |
| 255 | Parens.consumeOpen(); |
| 256 | |
| 257 | TypeResult T; |
| 258 | if (Tok.isNot(tok::r_paren)) |
| 259 | T = ParseTypeName(); |
| 260 | |
| 261 | if (Parens.consumeClose()) |
| 262 | return; |
| 263 | |
| 264 | if (T.isInvalid()) |
| 265 | return; |
| 266 | |
| 267 | if (T.isUsable()) |
| 268 | Attrs.addNewTypeAttr(&AttrName, |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 269 | SourceRange(AttrNameLoc, Parens.getCloseLocation()), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 270 | ScopeName, ScopeLoc, T.get(), Syntax); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 271 | else |
| 272 | Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 273 | ScopeName, ScopeLoc, nullptr, 0, Syntax); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Aaron Ballman | 35f9421 | 2014-04-14 16:03:22 +0000 | [diff] [blame] | 276 | unsigned Parser::ParseAttributeArgsCommon( |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 277 | IdentifierInfo *AttrName, SourceLocation AttrNameLoc, |
| 278 | ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName, |
| 279 | SourceLocation ScopeLoc, AttributeList::Syntax Syntax) { |
| 280 | // Ignore the left paren location for now. |
| 281 | ConsumeParen(); |
| 282 | |
| 283 | ArgsVector ArgExprs; |
| 284 | if (Tok.is(tok::identifier)) { |
| 285 | // If this attribute wants an 'identifier' argument, make it so. |
| 286 | bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName); |
| 287 | AttributeList::Kind AttrKind = |
| 288 | AttributeList::getKind(AttrName, ScopeName, Syntax); |
| 289 | |
| 290 | // If we don't know how to parse this attribute, but this is the only |
| 291 | // token in this argument, assume it's meant to be an identifier. |
| 292 | if (AttrKind == AttributeList::UnknownAttribute || |
| 293 | AttrKind == AttributeList::IgnoredAttribute) { |
| 294 | const Token &Next = NextToken(); |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 295 | IsIdentifierArg = Next.isOneOf(tok::r_paren, tok::comma); |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | if (IsIdentifierArg) |
| 299 | ArgExprs.push_back(ParseIdentifierLoc()); |
| 300 | } |
| 301 | |
| 302 | if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) { |
| 303 | // Eat the comma. |
| 304 | if (!ArgExprs.empty()) |
| 305 | ConsumeToken(); |
| 306 | |
| 307 | // Parse the non-empty comma-separated list of expressions. |
| 308 | do { |
Richard Smith | 917316f | 2017-01-07 19:42:26 +0000 | [diff] [blame] | 309 | bool Uneval = attributeParsedArgsUnevaluated(*AttrName); |
George Burgess IV | a19ea34 | 2016-11-10 20:43:52 +0000 | [diff] [blame] | 310 | EnterExpressionEvaluationContext Unevaluated( |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 311 | Actions, |
| 312 | Uneval ? Sema::ExpressionEvaluationContext::Unevaluated |
| 313 | : Sema::ExpressionEvaluationContext::ConstantEvaluated, |
Richard Smith | 917316f | 2017-01-07 19:42:26 +0000 | [diff] [blame] | 314 | /*LambdaContextDecl=*/nullptr, |
| 315 | /*IsDecltype=*/false); |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 316 | |
Kaelyn Takata | b16e632 | 2014-11-20 22:06:40 +0000 | [diff] [blame] | 317 | ExprResult ArgExpr( |
| 318 | Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression())); |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 319 | if (ArgExpr.isInvalid()) { |
| 320 | SkipUntil(tok::r_paren, StopAtSemi); |
Aaron Ballman | 35f9421 | 2014-04-14 16:03:22 +0000 | [diff] [blame] | 321 | return 0; |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 322 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 323 | ArgExprs.push_back(ArgExpr.get()); |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 324 | // Eat the comma, move to the next argument |
| 325 | } while (TryConsumeToken(tok::comma)); |
| 326 | } |
| 327 | |
| 328 | SourceLocation RParen = Tok.getLocation(); |
| 329 | if (!ExpectAndConsume(tok::r_paren)) { |
| 330 | SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc; |
| 331 | Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc, |
| 332 | ArgExprs.data(), ArgExprs.size(), Syntax); |
| 333 | } |
| 334 | |
| 335 | if (EndLoc) |
| 336 | *EndLoc = RParen; |
Aaron Ballman | 35f9421 | 2014-04-14 16:03:22 +0000 | [diff] [blame] | 337 | |
| 338 | return static_cast<unsigned>(ArgExprs.size()); |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 341 | /// Parse the arguments to a parameterized GNU attribute or |
| 342 | /// a C++11 attribute in "gnu" namespace. |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 343 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 344 | SourceLocation AttrNameLoc, |
| 345 | ParsedAttributes &Attrs, |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 346 | SourceLocation *EndLoc, |
| 347 | IdentifierInfo *ScopeName, |
| 348 | SourceLocation ScopeLoc, |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 349 | AttributeList::Syntax Syntax, |
| 350 | Declarator *D) { |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 351 | |
| 352 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 353 | |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 354 | AttributeList::Kind AttrKind = |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 355 | AttributeList::getKind(AttrName, ScopeName, Syntax); |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 356 | |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 357 | if (AttrKind == AttributeList::AT_Availability) { |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 358 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName, |
| 359 | ScopeLoc, Syntax); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 360 | return; |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 361 | } else if (AttrKind == AttributeList::AT_ExternalSourceSymbol) { |
| 362 | ParseExternalSourceSymbolAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, |
| 363 | ScopeName, ScopeLoc, Syntax); |
| 364 | return; |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 365 | } else if (AttrKind == AttributeList::AT_ObjCBridgeRelated) { |
| 366 | ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, |
| 367 | ScopeName, ScopeLoc, Syntax); |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 368 | return; |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 369 | } else if (AttrKind == AttributeList::AT_TypeTagForDatatype) { |
| 370 | ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, |
| 371 | ScopeName, ScopeLoc, Syntax); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 372 | return; |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 373 | } else if (attributeIsTypeArgAttr(*AttrName)) { |
| 374 | ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName, |
| 375 | ScopeLoc, Syntax); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 376 | return; |
| 377 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 378 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 379 | // These may refer to the function arguments, but need to be parsed early to |
| 380 | // participate in determining whether it's a redeclaration. |
George Burgess IV | a19ea34 | 2016-11-10 20:43:52 +0000 | [diff] [blame] | 381 | llvm::Optional<ParseScope> PrototypeScope; |
Ulrich Weigand | ef5aa29 | 2015-07-13 14:13:01 +0000 | [diff] [blame] | 382 | if (normalizeAttrName(AttrName->getName()) == "enable_if" && |
| 383 | D && D->isFunctionDeclarator()) { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 384 | DeclaratorChunk::FunctionTypeInfo FTI = D->getFunctionTypeInfo(); |
George Burgess IV | a19ea34 | 2016-11-10 20:43:52 +0000 | [diff] [blame] | 385 | PrototypeScope.emplace(this, Scope::FunctionPrototypeScope | |
| 386 | Scope::FunctionDeclarationScope | |
| 387 | Scope::DeclScope); |
Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 388 | for (unsigned i = 0; i != FTI.NumParams; ++i) { |
| 389 | ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 390 | Actions.ActOnReenterCXXMethodParameter(getCurScope(), Param); |
| 391 | } |
| 392 | } |
| 393 | |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 394 | ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName, |
| 395 | ScopeLoc, Syntax); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 398 | unsigned Parser::ParseClangAttributeArgs( |
| 399 | IdentifierInfo *AttrName, SourceLocation AttrNameLoc, |
| 400 | ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName, |
| 401 | SourceLocation ScopeLoc, AttributeList::Syntax Syntax) { |
| 402 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 403 | |
| 404 | AttributeList::Kind AttrKind = |
| 405 | AttributeList::getKind(AttrName, ScopeName, Syntax); |
| 406 | |
| 407 | if (AttrKind == AttributeList::AT_ExternalSourceSymbol) { |
| 408 | ParseExternalSourceSymbolAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, |
| 409 | ScopeName, ScopeLoc, Syntax); |
| 410 | return Attrs.getList() ? Attrs.getList()->getNumArgs() : 0; |
| 411 | } |
| 412 | |
| 413 | return ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, |
| 414 | ScopeName, ScopeLoc, Syntax); |
| 415 | } |
| 416 | |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 417 | bool Parser::ParseMicrosoftDeclSpecArgs(IdentifierInfo *AttrName, |
| 418 | SourceLocation AttrNameLoc, |
| 419 | ParsedAttributes &Attrs) { |
| 420 | // If the attribute isn't known, we will not attempt to parse any |
| 421 | // arguments. |
| 422 | if (!hasAttribute(AttrSyntax::Declspec, nullptr, AttrName, |
Bob Wilson | 7c73083 | 2015-07-20 22:57:31 +0000 | [diff] [blame] | 423 | getTargetInfo(), getLangOpts())) { |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 424 | // Eat the left paren, then skip to the ending right paren. |
| 425 | ConsumeParen(); |
| 426 | SkipUntil(tok::r_paren); |
| 427 | return false; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 428 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 429 | |
Aaron Ballman | 95d5703 | 2014-04-14 16:44:26 +0000 | [diff] [blame] | 430 | SourceLocation OpenParenLoc = Tok.getLocation(); |
| 431 | |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 432 | if (AttrName->getName() == "property") { |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 433 | // 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] | 434 | // assignment expressions as a parameter, but the lhs of the assignment |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 435 | // must be named get or put. |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 436 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 437 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 438 | T.expectAndConsume(diag::err_expected_lparen_after, |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 439 | AttrName->getNameStart(), tok::r_paren); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 440 | |
| 441 | enum AccessorKind { |
| 442 | AK_Invalid = -1, |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 443 | AK_Put = 0, |
| 444 | AK_Get = 1 // indices into AccessorNames |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 445 | }; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 446 | IdentifierInfo *AccessorNames[] = {nullptr, nullptr}; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 447 | bool HasInvalidAccessor = false; |
| 448 | |
| 449 | // Parse the accessor specifications. |
| 450 | while (true) { |
| 451 | // Stop if this doesn't look like an accessor spec. |
| 452 | if (!Tok.is(tok::identifier)) { |
| 453 | // If the user wrote a completely empty list, use a special diagnostic. |
| 454 | if (Tok.is(tok::r_paren) && !HasInvalidAccessor && |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 455 | AccessorNames[AK_Put] == nullptr && |
| 456 | AccessorNames[AK_Get] == nullptr) { |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 457 | Diag(AttrNameLoc, diag::err_ms_property_no_getter_or_putter); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 458 | break; |
| 459 | } |
| 460 | |
| 461 | Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor); |
| 462 | break; |
| 463 | } |
| 464 | |
| 465 | AccessorKind Kind; |
| 466 | SourceLocation KindLoc = Tok.getLocation(); |
| 467 | StringRef KindStr = Tok.getIdentifierInfo()->getName(); |
| 468 | if (KindStr == "get") { |
| 469 | Kind = AK_Get; |
| 470 | } else if (KindStr == "put") { |
| 471 | Kind = AK_Put; |
| 472 | |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 473 | // Recover from the common mistake of using 'set' instead of 'put'. |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 474 | } else if (KindStr == "set") { |
| 475 | Diag(KindLoc, diag::err_ms_property_has_set_accessor) |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 476 | << FixItHint::CreateReplacement(KindLoc, "put"); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 477 | Kind = AK_Put; |
| 478 | |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 479 | // Handle the mistake of forgetting the accessor kind by skipping |
| 480 | // this accessor. |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 481 | } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) { |
| 482 | Diag(KindLoc, diag::err_ms_property_missing_accessor_kind); |
| 483 | ConsumeToken(); |
| 484 | HasInvalidAccessor = true; |
| 485 | goto next_property_accessor; |
| 486 | |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 487 | // Otherwise, complain about the unknown accessor kind. |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 488 | } else { |
| 489 | Diag(KindLoc, diag::err_ms_property_unknown_accessor); |
| 490 | HasInvalidAccessor = true; |
| 491 | Kind = AK_Invalid; |
| 492 | |
| 493 | // 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] | 494 | if (!NextToken().is(tok::equal)) |
| 495 | break; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | // Consume the identifier. |
| 499 | ConsumeToken(); |
| 500 | |
| 501 | // Consume the '='. |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 502 | if (!TryConsumeToken(tok::equal)) { |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 503 | Diag(Tok.getLocation(), diag::err_ms_property_expected_equal) |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 504 | << KindStr; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 505 | break; |
| 506 | } |
| 507 | |
| 508 | // Expect the method name. |
| 509 | if (!Tok.is(tok::identifier)) { |
| 510 | Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name); |
| 511 | break; |
| 512 | } |
| 513 | |
| 514 | if (Kind == AK_Invalid) { |
| 515 | // Just drop invalid accessors. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 516 | } else if (AccessorNames[Kind] != nullptr) { |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 517 | // Complain about the repeated accessor, ignore it, and keep parsing. |
| 518 | Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr; |
| 519 | } else { |
| 520 | AccessorNames[Kind] = Tok.getIdentifierInfo(); |
| 521 | } |
| 522 | ConsumeToken(); |
| 523 | |
| 524 | next_property_accessor: |
| 525 | // Keep processing accessors until we run out. |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 526 | if (TryConsumeToken(tok::comma)) |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 527 | continue; |
| 528 | |
| 529 | // If we run into the ')', stop without consuming it. |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 530 | if (Tok.is(tok::r_paren)) |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 531 | break; |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 532 | |
| 533 | Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen); |
| 534 | break; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | // Only add the property attribute if it was well-formed. |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 538 | if (!HasInvalidAccessor) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 539 | Attrs.addNewPropertyAttr(AttrName, AttrNameLoc, nullptr, SourceLocation(), |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 540 | AccessorNames[AK_Get], AccessorNames[AK_Put], |
| 541 | AttributeList::AS_Declspec); |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 542 | T.skipToEnd(); |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 543 | return !HasInvalidAccessor; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 544 | } |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 545 | |
Aaron Ballman | 95d5703 | 2014-04-14 16:44:26 +0000 | [diff] [blame] | 546 | unsigned NumArgs = |
| 547 | ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, nullptr, nullptr, |
| 548 | SourceLocation(), AttributeList::AS_Declspec); |
| 549 | |
| 550 | // If this attribute's args were parsed, and it was expected to have |
| 551 | // arguments but none were provided, emit a diagnostic. |
| 552 | const AttributeList *Attr = Attrs.getList(); |
| 553 | if (Attr && Attr->getMaxArgs() && !NumArgs) { |
Aaron Ballman | ef5d94c | 2014-04-15 00:36:39 +0000 | [diff] [blame] | 554 | Diag(OpenParenLoc, diag::err_attribute_requires_arguments) << AttrName; |
Aaron Ballman | 95d5703 | 2014-04-14 16:44:26 +0000 | [diff] [blame] | 555 | return false; |
| 556 | } |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 557 | return true; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Eli Friedman | 06de2b5 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 560 | /// [MS] decl-specifier: |
| 561 | /// __declspec ( extended-decl-modifier-seq ) |
| 562 | /// |
| 563 | /// [MS] extended-decl-modifier-seq: |
| 564 | /// extended-decl-modifier[opt] |
| 565 | /// extended-decl-modifier extended-decl-modifier-seq |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 566 | void Parser::ParseMicrosoftDeclSpecs(ParsedAttributes &Attrs, |
| 567 | SourceLocation *End) { |
Saleem Abdulrasool | d170c4b | 2015-10-04 17:51:05 +0000 | [diff] [blame] | 568 | assert(getLangOpts().DeclSpecKeyword && "__declspec keyword is not enabled"); |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 569 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | 06de2b5 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 570 | |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 571 | while (Tok.is(tok::kw___declspec)) { |
| 572 | ConsumeToken(); |
| 573 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 574 | if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec", |
| 575 | tok::r_paren)) |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 576 | return; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 577 | |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 578 | // An empty declspec is perfectly legal and should not warn. Additionally, |
| 579 | // you can specify multiple attributes per declspec. |
| 580 | while (Tok.isNot(tok::r_paren)) { |
| 581 | // Attribute not present. |
| 582 | if (TryConsumeToken(tok::comma)) |
| 583 | continue; |
| 584 | |
| 585 | // We expect either a well-known identifier or a generic string. Anything |
| 586 | // else is a malformed declspec. |
| 587 | bool IsString = Tok.getKind() == tok::string_literal; |
| 588 | if (!IsString && Tok.getKind() != tok::identifier && |
| 589 | Tok.getKind() != tok::kw_restrict) { |
| 590 | Diag(Tok, diag::err_ms_declspec_type); |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 591 | T.skipToEnd(); |
| 592 | return; |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 593 | } |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 594 | |
| 595 | IdentifierInfo *AttrName; |
| 596 | SourceLocation AttrNameLoc; |
| 597 | if (IsString) { |
| 598 | SmallString<8> StrBuffer; |
| 599 | bool Invalid = false; |
| 600 | StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid); |
| 601 | if (Invalid) { |
| 602 | T.skipToEnd(); |
| 603 | return; |
| 604 | } |
| 605 | AttrName = PP.getIdentifierInfo(Str); |
| 606 | AttrNameLoc = ConsumeStringToken(); |
| 607 | } else { |
| 608 | AttrName = Tok.getIdentifierInfo(); |
| 609 | AttrNameLoc = ConsumeToken(); |
| 610 | } |
| 611 | |
| 612 | bool AttrHandled = false; |
| 613 | |
| 614 | // Parse attribute arguments. |
| 615 | if (Tok.is(tok::l_paren)) |
| 616 | AttrHandled = ParseMicrosoftDeclSpecArgs(AttrName, AttrNameLoc, Attrs); |
| 617 | else if (AttrName->getName() == "property") |
| 618 | // The property attribute must have an argument list. |
| 619 | Diag(Tok.getLocation(), diag::err_expected_lparen_after) |
| 620 | << AttrName->getName(); |
| 621 | |
| 622 | if (!AttrHandled) |
| 623 | Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
| 624 | AttributeList::AS_Declspec); |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 625 | } |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 626 | T.consumeClose(); |
| 627 | if (End) |
| 628 | *End = T.getCloseLocation(); |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 629 | } |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 630 | } |
| 631 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 632 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 633 | // Treat these like attributes |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 634 | while (true) { |
| 635 | switch (Tok.getKind()) { |
| 636 | case tok::kw___fastcall: |
| 637 | case tok::kw___stdcall: |
| 638 | case tok::kw___thiscall: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 639 | case tok::kw___regcall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 640 | case tok::kw___cdecl: |
| 641 | case tok::kw___vectorcall: |
| 642 | case tok::kw___ptr64: |
| 643 | case tok::kw___w64: |
| 644 | case tok::kw___ptr32: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 645 | case tok::kw___sptr: |
| 646 | case tok::kw___uptr: { |
| 647 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 648 | SourceLocation AttrNameLoc = ConsumeToken(); |
| 649 | attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
| 650 | AttributeList::AS_Keyword); |
| 651 | break; |
| 652 | } |
| 653 | default: |
| 654 | return; |
| 655 | } |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 656 | } |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Nico Rieck | eaaae27 | 2014-12-04 23:31:08 +0000 | [diff] [blame] | 659 | void Parser::DiagnoseAndSkipExtendedMicrosoftTypeAttributes() { |
| 660 | SourceLocation StartLoc = Tok.getLocation(); |
| 661 | SourceLocation EndLoc = SkipExtendedMicrosoftTypeAttributes(); |
| 662 | |
| 663 | if (EndLoc.isValid()) { |
| 664 | SourceRange Range(StartLoc, EndLoc); |
| 665 | Diag(StartLoc, diag::warn_microsoft_qualifiers_ignored) << Range; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | SourceLocation Parser::SkipExtendedMicrosoftTypeAttributes() { |
| 670 | SourceLocation EndLoc; |
| 671 | |
| 672 | while (true) { |
| 673 | switch (Tok.getKind()) { |
| 674 | case tok::kw_const: |
| 675 | case tok::kw_volatile: |
| 676 | case tok::kw___fastcall: |
| 677 | case tok::kw___stdcall: |
| 678 | case tok::kw___thiscall: |
| 679 | case tok::kw___cdecl: |
| 680 | case tok::kw___vectorcall: |
| 681 | case tok::kw___ptr32: |
| 682 | case tok::kw___ptr64: |
| 683 | case tok::kw___w64: |
| 684 | case tok::kw___unaligned: |
| 685 | case tok::kw___sptr: |
| 686 | case tok::kw___uptr: |
| 687 | EndLoc = ConsumeToken(); |
| 688 | break; |
| 689 | default: |
| 690 | return EndLoc; |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 695 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 696 | // Treat these like attributes |
| 697 | while (Tok.is(tok::kw___pascal)) { |
| 698 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 699 | SourceLocation AttrNameLoc = ConsumeToken(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 700 | attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 701 | AttributeList::AS_Keyword); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 702 | } |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Anastasia Stulova | 6bdbcbb | 2016-02-19 18:30:11 +0000 | [diff] [blame] | 705 | void Parser::ParseOpenCLKernelAttributes(ParsedAttributes &attrs) { |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 706 | // Treat these like attributes |
| 707 | while (Tok.is(tok::kw___kernel)) { |
Richard Smith | 0cdcc98 | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 708 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 709 | SourceLocation AttrNameLoc = ConsumeToken(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 710 | attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 711 | AttributeList::AS_Keyword); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | |
Aaron Ballman | 05d76ea | 2014-01-14 01:29:54 +0000 | [diff] [blame] | 715 | void Parser::ParseOpenCLQualifiers(ParsedAttributes &Attrs) { |
| 716 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 717 | SourceLocation AttrNameLoc = Tok.getLocation(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 718 | Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Aaron Ballman | 2689133 | 2014-01-14 17:41:53 +0000 | [diff] [blame] | 719 | AttributeList::AS_Keyword); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 722 | void Parser::ParseNullabilityTypeSpecifiers(ParsedAttributes &attrs) { |
| 723 | // Treat these like attributes, even though they're type specifiers. |
| 724 | while (true) { |
| 725 | switch (Tok.getKind()) { |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 726 | case tok::kw__Nonnull: |
| 727 | case tok::kw__Nullable: |
| 728 | case tok::kw__Null_unspecified: { |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 729 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 730 | SourceLocation AttrNameLoc = ConsumeToken(); |
| 731 | if (!getLangOpts().ObjC1) |
| 732 | Diag(AttrNameLoc, diag::ext_nullability) |
| 733 | << AttrName; |
| 734 | attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
| 735 | AttributeList::AS_Keyword); |
| 736 | break; |
| 737 | } |
| 738 | default: |
| 739 | return; |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | |
Fariborz Jahanian | dbc956d | 2014-10-02 16:39:45 +0000 | [diff] [blame] | 744 | static bool VersionNumberSeparator(const char Separator) { |
| 745 | return (Separator == '.' || Separator == '_'); |
| 746 | } |
| 747 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 748 | /// \brief Parse a version number. |
| 749 | /// |
| 750 | /// version: |
| 751 | /// simple-integer |
| 752 | /// simple-integer ',' simple-integer |
| 753 | /// simple-integer ',' simple-integer ',' simple-integer |
| 754 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
Erik Pilkington | 29099de | 2016-07-16 00:35:23 +0000 | [diff] [blame] | 755 | Range = SourceRange(Tok.getLocation(), Tok.getEndLoc()); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 756 | |
| 757 | if (!Tok.is(tok::numeric_constant)) { |
| 758 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 759 | SkipUntil(tok::comma, tok::r_paren, |
| 760 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 761 | return VersionTuple(); |
| 762 | } |
| 763 | |
| 764 | // Parse the major (and possibly minor and subminor) versions, which |
| 765 | // are stored in the numeric constant. We utilize a quirk of the |
| 766 | // lexer, which is that it handles something like 1.2.3 as a single |
| 767 | // numeric constant, rather than two separate tokens. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 768 | SmallString<512> Buffer; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 769 | Buffer.resize(Tok.getLength()+1); |
| 770 | const char *ThisTokBegin = &Buffer[0]; |
| 771 | |
| 772 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 773 | bool Invalid = false; |
| 774 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 775 | if (Invalid) |
| 776 | return VersionTuple(); |
| 777 | |
| 778 | // Parse the major version. |
| 779 | unsigned AfterMajor = 0; |
| 780 | unsigned Major = 0; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 781 | while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 782 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 783 | ++AfterMajor; |
| 784 | } |
| 785 | |
| 786 | if (AfterMajor == 0) { |
| 787 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 788 | SkipUntil(tok::comma, tok::r_paren, |
| 789 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 790 | return VersionTuple(); |
| 791 | } |
| 792 | |
| 793 | if (AfterMajor == ActualLength) { |
| 794 | ConsumeToken(); |
| 795 | |
| 796 | // We only had a single version component. |
| 797 | if (Major == 0) { |
| 798 | Diag(Tok, diag::err_zero_version); |
| 799 | return VersionTuple(); |
| 800 | } |
| 801 | |
| 802 | return VersionTuple(Major); |
| 803 | } |
| 804 | |
Fariborz Jahanian | ce72e63 | 2014-10-02 17:57:26 +0000 | [diff] [blame] | 805 | const char AfterMajorSeparator = ThisTokBegin[AfterMajor]; |
| 806 | if (!VersionNumberSeparator(AfterMajorSeparator) |
Fariborz Jahanian | dbc956d | 2014-10-02 16:39:45 +0000 | [diff] [blame] | 807 | || (AfterMajor + 1 == ActualLength)) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 808 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 809 | SkipUntil(tok::comma, tok::r_paren, |
| 810 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 811 | return VersionTuple(); |
| 812 | } |
| 813 | |
| 814 | // Parse the minor version. |
| 815 | unsigned AfterMinor = AfterMajor + 1; |
| 816 | unsigned Minor = 0; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 817 | while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 818 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 819 | ++AfterMinor; |
| 820 | } |
| 821 | |
| 822 | if (AfterMinor == ActualLength) { |
| 823 | ConsumeToken(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 824 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 825 | // We had major.minor. |
| 826 | if (Major == 0 && Minor == 0) { |
| 827 | Diag(Tok, diag::err_zero_version); |
| 828 | return VersionTuple(); |
| 829 | } |
| 830 | |
Fariborz Jahanian | 2618dba | 2014-10-06 16:46:02 +0000 | [diff] [blame] | 831 | return VersionTuple(Major, Minor, (AfterMajorSeparator == '_')); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Fariborz Jahanian | ce72e63 | 2014-10-02 17:57:26 +0000 | [diff] [blame] | 834 | const char AfterMinorSeparator = ThisTokBegin[AfterMinor]; |
Fariborz Jahanian | dbc956d | 2014-10-02 16:39:45 +0000 | [diff] [blame] | 835 | // If what follows is not a '.' or '_', we have a problem. |
Fariborz Jahanian | ce72e63 | 2014-10-02 17:57:26 +0000 | [diff] [blame] | 836 | if (!VersionNumberSeparator(AfterMinorSeparator)) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 837 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 838 | SkipUntil(tok::comma, tok::r_paren, |
| 839 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 840 | return VersionTuple(); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 841 | } |
Fariborz Jahanian | ce72e63 | 2014-10-02 17:57:26 +0000 | [diff] [blame] | 842 | |
Fariborz Jahanian | b616161 | 2014-10-03 17:21:12 +0000 | [diff] [blame] | 843 | // Warn if separators, be it '.' or '_', do not match. |
Fariborz Jahanian | ce72e63 | 2014-10-02 17:57:26 +0000 | [diff] [blame] | 844 | if (AfterMajorSeparator != AfterMinorSeparator) |
| 845 | Diag(Tok, diag::warn_expected_consistent_version_separator); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 846 | |
| 847 | // Parse the subminor version. |
| 848 | unsigned AfterSubminor = AfterMinor + 1; |
| 849 | unsigned Subminor = 0; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 850 | while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 851 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 852 | ++AfterSubminor; |
| 853 | } |
| 854 | |
| 855 | if (AfterSubminor != ActualLength) { |
| 856 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 857 | SkipUntil(tok::comma, tok::r_paren, |
| 858 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 859 | return VersionTuple(); |
| 860 | } |
| 861 | ConsumeToken(); |
Fariborz Jahanian | 2618dba | 2014-10-06 16:46:02 +0000 | [diff] [blame] | 862 | return VersionTuple(Major, Minor, Subminor, (AfterMajorSeparator == '_')); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | /// \brief Parse the contents of the "availability" attribute. |
| 866 | /// |
| 867 | /// availability-attribute: |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 868 | /// 'availability' '(' platform ',' opt-strict version-arg-list, |
| 869 | /// opt-replacement, opt-message')' |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 870 | /// |
| 871 | /// platform: |
| 872 | /// identifier |
| 873 | /// |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 874 | /// opt-strict: |
| 875 | /// 'strict' ',' |
Manman Ren | b636b90 | 2016-02-17 22:05:48 +0000 | [diff] [blame] | 876 | /// |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 877 | /// version-arg-list: |
| 878 | /// version-arg |
| 879 | /// version-arg ',' version-arg-list |
| 880 | /// |
| 881 | /// version-arg: |
| 882 | /// 'introduced' '=' version |
| 883 | /// 'deprecated' '=' version |
Douglas Gregor | fdd417f | 2012-03-11 04:53:21 +0000 | [diff] [blame] | 884 | /// 'obsoleted' = version |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 885 | /// 'unavailable' |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 886 | /// opt-replacement: |
| 887 | /// 'replacement' '=' <string> |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 888 | /// opt-message: |
| 889 | /// 'message' '=' <string> |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 890 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 891 | SourceLocation AvailabilityLoc, |
| 892 | ParsedAttributes &attrs, |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 893 | SourceLocation *endLoc, |
| 894 | IdentifierInfo *ScopeName, |
| 895 | SourceLocation ScopeLoc, |
| 896 | AttributeList::Syntax Syntax) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 897 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 898 | AvailabilityChange Changes[Unknown]; |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 899 | ExprResult MessageExpr, ReplacementExpr; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 900 | |
| 901 | // Opening '('. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 902 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 903 | if (T.consumeOpen()) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 904 | Diag(Tok, diag::err_expected) << tok::l_paren; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 905 | return; |
| 906 | } |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 907 | |
Manman Ren | b636b90 | 2016-02-17 22:05:48 +0000 | [diff] [blame] | 908 | // Parse the platform name. |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 909 | if (Tok.isNot(tok::identifier)) { |
| 910 | Diag(Tok, diag::err_availability_expected_platform); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 911 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 912 | return; |
| 913 | } |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 914 | IdentifierLoc *Platform = ParseIdentifierLoc(); |
Alex Lorenz | 0b1ce8b | 2017-08-15 14:42:01 +0000 | [diff] [blame] | 915 | if (const IdentifierInfo *const Ident = Platform->Ident) { |
| 916 | // Canonicalize platform name from "macosx" to "macos". |
| 917 | if (Ident->getName() == "macosx") |
| 918 | Platform->Ident = PP.getIdentifierInfo("macos"); |
| 919 | // Canonicalize platform name from "macosx_app_extension" to |
| 920 | // "macos_app_extension". |
| 921 | else if (Ident->getName() == "macosx_app_extension") |
| 922 | Platform->Ident = PP.getIdentifierInfo("macos_app_extension"); |
| 923 | else |
| 924 | Platform->Ident = PP.getIdentifierInfo( |
| 925 | AvailabilityAttr::canonicalizePlatformName(Ident->getName())); |
| 926 | } |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 927 | |
| 928 | // Parse the ',' following the platform name. |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 929 | if (ExpectAndConsume(tok::comma)) { |
| 930 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 931 | return; |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 932 | } |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 933 | |
| 934 | // If we haven't grabbed the pointers for the identifiers |
| 935 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 936 | if (!Ident_introduced) { |
| 937 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 938 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 939 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 940 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 941 | Ident_message = PP.getIdentifierInfo("message"); |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 942 | Ident_strict = PP.getIdentifierInfo("strict"); |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 943 | Ident_replacement = PP.getIdentifierInfo("replacement"); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 946 | // Parse the optional "strict", the optional "replacement" and the set of |
Manman Ren | b636b90 | 2016-02-17 22:05:48 +0000 | [diff] [blame] | 947 | // introductions/deprecations/removals. |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 948 | SourceLocation UnavailableLoc, StrictLoc; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 949 | do { |
| 950 | if (Tok.isNot(tok::identifier)) { |
| 951 | Diag(Tok, diag::err_availability_expected_change); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 952 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 953 | return; |
| 954 | } |
| 955 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 956 | SourceLocation KeywordLoc = ConsumeToken(); |
| 957 | |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 958 | if (Keyword == Ident_strict) { |
| 959 | if (StrictLoc.isValid()) { |
Manman Ren | b636b90 | 2016-02-17 22:05:48 +0000 | [diff] [blame] | 960 | Diag(KeywordLoc, diag::err_availability_redundant) |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 961 | << Keyword << SourceRange(StrictLoc); |
Manman Ren | b636b90 | 2016-02-17 22:05:48 +0000 | [diff] [blame] | 962 | } |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 963 | StrictLoc = KeywordLoc; |
Manman Ren | b636b90 | 2016-02-17 22:05:48 +0000 | [diff] [blame] | 964 | continue; |
| 965 | } |
| 966 | |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 967 | if (Keyword == Ident_unavailable) { |
| 968 | if (UnavailableLoc.isValid()) { |
| 969 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 970 | << Keyword << SourceRange(UnavailableLoc); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 971 | } |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 972 | UnavailableLoc = KeywordLoc; |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 973 | continue; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 976 | if (Tok.isNot(tok::equal)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 977 | Diag(Tok, diag::err_expected_after) << Keyword << tok::equal; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 978 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 979 | return; |
| 980 | } |
| 981 | ConsumeToken(); |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 982 | if (Keyword == Ident_message || Keyword == Ident_replacement) { |
David Majnemer | 2e49830 | 2014-07-18 05:43:12 +0000 | [diff] [blame] | 983 | if (Tok.isNot(tok::string_literal)) { |
Andy Gibbs | a8df57a | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 984 | Diag(Tok, diag::err_expected_string_literal) |
| 985 | << /*Source='availability attribute'*/2; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 986 | SkipUntil(tok::r_paren, StopAtSemi); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 987 | return; |
| 988 | } |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 989 | if (Keyword == Ident_message) |
| 990 | MessageExpr = ParseStringLiteralExpression(); |
| 991 | else |
| 992 | ReplacementExpr = ParseStringLiteralExpression(); |
David Majnemer | 2e49830 | 2014-07-18 05:43:12 +0000 | [diff] [blame] | 993 | // Also reject wide string literals. |
| 994 | if (StringLiteral *MessageStringLiteral = |
| 995 | cast_or_null<StringLiteral>(MessageExpr.get())) { |
| 996 | if (MessageStringLiteral->getCharByteWidth() != 1) { |
| 997 | Diag(MessageStringLiteral->getSourceRange().getBegin(), |
| 998 | diag::err_expected_string_literal) |
| 999 | << /*Source='availability attribute'*/ 2; |
| 1000 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1001 | return; |
| 1002 | } |
| 1003 | } |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 1004 | if (Keyword == Ident_message) |
| 1005 | break; |
| 1006 | else |
| 1007 | continue; |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1008 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1009 | |
Fariborz Jahanian | 5a29e6a | 2014-11-05 23:58:55 +0000 | [diff] [blame] | 1010 | // Special handling of 'NA' only when applied to introduced or |
| 1011 | // deprecated. |
| 1012 | if ((Keyword == Ident_introduced || Keyword == Ident_deprecated) && |
| 1013 | Tok.is(tok::identifier)) { |
| 1014 | IdentifierInfo *NA = Tok.getIdentifierInfo(); |
| 1015 | if (NA->getName() == "NA") { |
| 1016 | ConsumeToken(); |
| 1017 | if (Keyword == Ident_introduced) |
| 1018 | UnavailableLoc = KeywordLoc; |
| 1019 | continue; |
| 1020 | } |
| 1021 | } |
| 1022 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1023 | SourceRange VersionRange; |
| 1024 | VersionTuple Version = ParseVersionTuple(VersionRange); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1025 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1026 | if (Version.empty()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1027 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1028 | return; |
| 1029 | } |
| 1030 | |
| 1031 | unsigned Index; |
| 1032 | if (Keyword == Ident_introduced) |
| 1033 | Index = Introduced; |
| 1034 | else if (Keyword == Ident_deprecated) |
| 1035 | Index = Deprecated; |
| 1036 | else if (Keyword == Ident_obsoleted) |
| 1037 | Index = Obsoleted; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1038 | else |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1039 | Index = Unknown; |
| 1040 | |
| 1041 | if (Index < Unknown) { |
| 1042 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 1043 | Diag(KeywordLoc, diag::err_availability_redundant) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1044 | << Keyword |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1045 | << SourceRange(Changes[Index].KeywordLoc, |
| 1046 | Changes[Index].VersionRange.getEnd()); |
| 1047 | } |
| 1048 | |
| 1049 | Changes[Index].KeywordLoc = KeywordLoc; |
| 1050 | Changes[Index].Version = Version; |
| 1051 | Changes[Index].VersionRange = VersionRange; |
| 1052 | } else { |
| 1053 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 1054 | << Keyword << VersionRange; |
| 1055 | } |
| 1056 | |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 1057 | } while (TryConsumeToken(tok::comma)); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1058 | |
| 1059 | // Closing ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1060 | if (T.consumeClose()) |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1061 | return; |
| 1062 | |
| 1063 | if (endLoc) |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1064 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1065 | |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1066 | // The 'unavailable' availability cannot be combined with any other |
| 1067 | // availability changes. Make sure that hasn't happened. |
| 1068 | if (UnavailableLoc.isValid()) { |
| 1069 | bool Complained = false; |
| 1070 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 1071 | if (Changes[Index].KeywordLoc.isValid()) { |
| 1072 | if (!Complained) { |
| 1073 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 1074 | << SourceRange(Changes[Index].KeywordLoc, |
| 1075 | Changes[Index].VersionRange.getEnd()); |
| 1076 | Complained = true; |
| 1077 | } |
| 1078 | |
| 1079 | // Clear out the availability. |
| 1080 | Changes[Index] = AvailabilityChange(); |
| 1081 | } |
| 1082 | } |
| 1083 | } |
| 1084 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1085 | // Record this attribute |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1086 | attrs.addNew(&Availability, |
| 1087 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1088 | ScopeName, ScopeLoc, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1089 | Platform, |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1090 | Changes[Introduced], |
| 1091 | Changes[Deprecated], |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1092 | Changes[Obsoleted], |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1093 | UnavailableLoc, MessageExpr.get(), |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 1094 | Syntax, StrictLoc, ReplacementExpr.get()); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 1097 | /// \brief Parse the contents of the "external_source_symbol" attribute. |
| 1098 | /// |
| 1099 | /// external-source-symbol-attribute: |
| 1100 | /// 'external_source_symbol' '(' keyword-arg-list ')' |
| 1101 | /// |
| 1102 | /// keyword-arg-list: |
| 1103 | /// keyword-arg |
| 1104 | /// keyword-arg ',' keyword-arg-list |
| 1105 | /// |
| 1106 | /// keyword-arg: |
| 1107 | /// 'language' '=' <string> |
| 1108 | /// 'defined_in' '=' <string> |
| 1109 | /// 'generated_declaration' |
| 1110 | void Parser::ParseExternalSourceSymbolAttribute( |
| 1111 | IdentifierInfo &ExternalSourceSymbol, SourceLocation Loc, |
| 1112 | ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName, |
| 1113 | SourceLocation ScopeLoc, AttributeList::Syntax Syntax) { |
| 1114 | // Opening '('. |
| 1115 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1116 | if (T.expectAndConsume()) |
| 1117 | return; |
| 1118 | |
| 1119 | // Initialize the pointers for the keyword identifiers when required. |
| 1120 | if (!Ident_language) { |
| 1121 | Ident_language = PP.getIdentifierInfo("language"); |
| 1122 | Ident_defined_in = PP.getIdentifierInfo("defined_in"); |
| 1123 | Ident_generated_declaration = PP.getIdentifierInfo("generated_declaration"); |
| 1124 | } |
| 1125 | |
| 1126 | ExprResult Language; |
| 1127 | bool HasLanguage = false; |
| 1128 | ExprResult DefinedInExpr; |
| 1129 | bool HasDefinedIn = false; |
| 1130 | IdentifierLoc *GeneratedDeclaration = nullptr; |
| 1131 | |
| 1132 | // Parse the language/defined_in/generated_declaration keywords |
| 1133 | do { |
| 1134 | if (Tok.isNot(tok::identifier)) { |
| 1135 | Diag(Tok, diag::err_external_source_symbol_expected_keyword); |
| 1136 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1137 | return; |
| 1138 | } |
| 1139 | |
| 1140 | SourceLocation KeywordLoc = Tok.getLocation(); |
| 1141 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 1142 | if (Keyword == Ident_generated_declaration) { |
| 1143 | if (GeneratedDeclaration) { |
| 1144 | Diag(Tok, diag::err_external_source_symbol_duplicate_clause) << Keyword; |
| 1145 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1146 | return; |
| 1147 | } |
| 1148 | GeneratedDeclaration = ParseIdentifierLoc(); |
| 1149 | continue; |
| 1150 | } |
| 1151 | |
| 1152 | if (Keyword != Ident_language && Keyword != Ident_defined_in) { |
| 1153 | Diag(Tok, diag::err_external_source_symbol_expected_keyword); |
| 1154 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1155 | return; |
| 1156 | } |
| 1157 | |
| 1158 | ConsumeToken(); |
| 1159 | if (ExpectAndConsume(tok::equal, diag::err_expected_after, |
| 1160 | Keyword->getName())) { |
| 1161 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1162 | return; |
| 1163 | } |
| 1164 | |
| 1165 | bool HadLanguage = HasLanguage, HadDefinedIn = HasDefinedIn; |
| 1166 | if (Keyword == Ident_language) |
| 1167 | HasLanguage = true; |
| 1168 | else |
| 1169 | HasDefinedIn = true; |
| 1170 | |
| 1171 | if (Tok.isNot(tok::string_literal)) { |
| 1172 | Diag(Tok, diag::err_expected_string_literal) |
| 1173 | << /*Source='external_source_symbol attribute'*/ 3 |
| 1174 | << /*language | source container*/ (Keyword != Ident_language); |
| 1175 | SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch); |
| 1176 | continue; |
| 1177 | } |
| 1178 | if (Keyword == Ident_language) { |
| 1179 | if (HadLanguage) { |
| 1180 | Diag(KeywordLoc, diag::err_external_source_symbol_duplicate_clause) |
| 1181 | << Keyword; |
| 1182 | ParseStringLiteralExpression(); |
| 1183 | continue; |
| 1184 | } |
| 1185 | Language = ParseStringLiteralExpression(); |
| 1186 | } else { |
| 1187 | assert(Keyword == Ident_defined_in && "Invalid clause keyword!"); |
| 1188 | if (HadDefinedIn) { |
| 1189 | Diag(KeywordLoc, diag::err_external_source_symbol_duplicate_clause) |
| 1190 | << Keyword; |
| 1191 | ParseStringLiteralExpression(); |
| 1192 | continue; |
| 1193 | } |
| 1194 | DefinedInExpr = ParseStringLiteralExpression(); |
| 1195 | } |
| 1196 | } while (TryConsumeToken(tok::comma)); |
| 1197 | |
| 1198 | // Closing ')'. |
| 1199 | if (T.consumeClose()) |
| 1200 | return; |
| 1201 | if (EndLoc) |
| 1202 | *EndLoc = T.getCloseLocation(); |
| 1203 | |
| 1204 | ArgsUnion Args[] = {Language.get(), DefinedInExpr.get(), |
| 1205 | GeneratedDeclaration}; |
| 1206 | Attrs.addNew(&ExternalSourceSymbol, SourceRange(Loc, T.getCloseLocation()), |
| 1207 | ScopeName, ScopeLoc, Args, llvm::array_lengthof(Args), Syntax); |
| 1208 | } |
| 1209 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1210 | /// \brief Parse the contents of the "objc_bridge_related" attribute. |
| 1211 | /// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')' |
| 1212 | /// related_class: |
| 1213 | /// Identifier |
| 1214 | /// |
| 1215 | /// opt-class_method: |
| 1216 | /// Identifier: | <empty> |
| 1217 | /// |
| 1218 | /// opt-instance_method: |
| 1219 | /// Identifier | <empty> |
| 1220 | /// |
| 1221 | void Parser::ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated, |
| 1222 | SourceLocation ObjCBridgeRelatedLoc, |
| 1223 | ParsedAttributes &attrs, |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1224 | SourceLocation *endLoc, |
| 1225 | IdentifierInfo *ScopeName, |
| 1226 | SourceLocation ScopeLoc, |
| 1227 | AttributeList::Syntax Syntax) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1228 | // Opening '('. |
| 1229 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1230 | if (T.consumeOpen()) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1231 | Diag(Tok, diag::err_expected) << tok::l_paren; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1232 | return; |
| 1233 | } |
| 1234 | |
| 1235 | // Parse the related class name. |
| 1236 | if (Tok.isNot(tok::identifier)) { |
| 1237 | Diag(Tok, diag::err_objcbridge_related_expected_related_class); |
| 1238 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1239 | return; |
| 1240 | } |
| 1241 | IdentifierLoc *RelatedClass = ParseIdentifierLoc(); |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 1242 | if (ExpectAndConsume(tok::comma)) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1243 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1244 | return; |
| 1245 | } |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1246 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1247 | // Parse optional class method name. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1248 | IdentifierLoc *ClassMethod = nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1249 | if (Tok.is(tok::identifier)) { |
| 1250 | ClassMethod = ParseIdentifierLoc(); |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1251 | if (!TryConsumeToken(tok::colon)) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1252 | Diag(Tok, diag::err_objcbridge_related_selector_name); |
| 1253 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1254 | return; |
| 1255 | } |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1256 | } |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1257 | if (!TryConsumeToken(tok::comma)) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1258 | if (Tok.is(tok::colon)) |
| 1259 | Diag(Tok, diag::err_objcbridge_related_selector_name); |
| 1260 | else |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1261 | Diag(Tok, diag::err_expected) << tok::comma; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1262 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1263 | return; |
| 1264 | } |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1265 | |
| 1266 | // Parse optional instance method name. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1267 | IdentifierLoc *InstanceMethod = nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1268 | if (Tok.is(tok::identifier)) |
| 1269 | InstanceMethod = ParseIdentifierLoc(); |
| 1270 | else if (Tok.isNot(tok::r_paren)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1271 | Diag(Tok, diag::err_expected) << tok::r_paren; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1272 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1273 | return; |
| 1274 | } |
| 1275 | |
| 1276 | // Closing ')'. |
| 1277 | if (T.consumeClose()) |
| 1278 | return; |
| 1279 | |
| 1280 | if (endLoc) |
| 1281 | *endLoc = T.getCloseLocation(); |
| 1282 | |
| 1283 | // Record this attribute |
| 1284 | attrs.addNew(&ObjCBridgeRelated, |
| 1285 | SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1286 | ScopeName, ScopeLoc, |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1287 | RelatedClass, |
| 1288 | ClassMethod, |
| 1289 | InstanceMethod, |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1290 | Syntax); |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1291 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1292 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1293 | // Late Parsed Attributes: |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1294 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 1295 | |
| 1296 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 1297 | |
| 1298 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 1299 | Self->ParseLexedAttributes(*Class); |
| 1300 | } |
| 1301 | |
| 1302 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1303 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 1307 | /// scope appropriately. |
| 1308 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 1309 | // Deal with templates |
| 1310 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 1311 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 1312 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 1313 | HasTemplateScope); |
| 1314 | if (HasTemplateScope) |
| 1315 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 1316 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1317 | // Set or update the scope flags. |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1318 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1319 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1320 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 1321 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 1322 | |
DeLesley Hutchins | 6f86004 | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1323 | // Enter the scope of nested classes |
| 1324 | if (!AlreadyHasClassScope) |
| 1325 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 1326 | Class.TagOrTemplate); |
Benjamin Kramer | 1d373c6 | 2012-05-17 12:01:52 +0000 | [diff] [blame] | 1327 | if (!Class.LateParsedDeclarations.empty()) { |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1328 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){ |
| 1329 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 1330 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1331 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1332 | |
DeLesley Hutchins | 6f86004 | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1333 | if (!AlreadyHasClassScope) |
| 1334 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 1335 | Class.TagOrTemplate); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1338 | /// \brief Parse all attributes in LAs, and attach them to Decl D. |
| 1339 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 1340 | bool EnterScope, bool OnDefinition) { |
DeLesley Hutchins | 66e300e | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1341 | assert(LAs.parseSoon() && |
| 1342 | "Attribute list should be marked for immediate parsing."); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1343 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 19c722d | 2012-08-15 22:41:04 +0000 | [diff] [blame] | 1344 | if (D) |
| 1345 | LAs[i]->addDecl(D); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1346 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
Benjamin Kramer | bafc49a | 2012-04-14 12:44:47 +0000 | [diff] [blame] | 1347 | delete LAs[i]; |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1348 | } |
| 1349 | LAs.clear(); |
| 1350 | } |
| 1351 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1352 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 1353 | /// This will be called at the end of parsing a class declaration |
| 1354 | /// for each LateParsedAttribute. We consume the saved tokens and |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1355 | /// create an attribute with the arguments filled in. We add this |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1356 | /// to the Attribute list for the decl. |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1357 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 1358 | bool EnterScope, bool OnDefinition) { |
David Majnemer | d5946f5 | 2015-01-13 08:35:24 +0000 | [diff] [blame] | 1359 | // Create a fake EOF so that attribute parsing won't go off the end of the |
| 1360 | // attribute. |
| 1361 | Token AttrEnd; |
| 1362 | AttrEnd.startToken(); |
| 1363 | AttrEnd.setKind(tok::eof); |
| 1364 | AttrEnd.setLocation(Tok.getLocation()); |
| 1365 | AttrEnd.setEofData(LA.Toks.data()); |
| 1366 | LA.Toks.push_back(AttrEnd); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1367 | |
| 1368 | // Append the current token at the end of the new token stream so that it |
| 1369 | // doesn't get lost. |
| 1370 | LA.Toks.push_back(Tok); |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1371 | PP.EnterTokenStream(LA.Toks, true); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1372 | // Consume the previously pushed token. |
Argyrios Kyrtzidis | c36633c | 2013-03-27 23:58:17 +0000 | [diff] [blame] | 1373 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1374 | |
| 1375 | ParsedAttributes Attrs(AttrFactory); |
| 1376 | SourceLocation endLoc; |
| 1377 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1378 | if (LA.Decls.size() > 0) { |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1379 | Decl *D = LA.Decls[0]; |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1380 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1381 | RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext()); |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1382 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1383 | // Allow 'this' within late-parsed attributes. |
Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 1384 | Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0, |
| 1385 | ND && ND->isCXXInstanceMember()); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1386 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1387 | if (LA.Decls.size() == 1) { |
| 1388 | // If the Decl is templatized, add template parameters to scope. |
| 1389 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 1390 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 1391 | if (HasTemplateScope) |
| 1392 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1393 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1394 | // If the Decl is on a function, add function parameters to the scope. |
| 1395 | bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
Momchil Velikov | 57c681f | 2017-08-10 15:43:06 +0000 | [diff] [blame] | 1396 | ParseScope FnScope( |
| 1397 | this, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope, |
| 1398 | HasFunScope); |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1399 | if (HasFunScope) |
| 1400 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1401 | |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1402 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1403 | nullptr, SourceLocation(), AttributeList::AS_GNU, |
| 1404 | nullptr); |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1405 | |
| 1406 | if (HasFunScope) { |
| 1407 | Actions.ActOnExitFunctionContext(); |
| 1408 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 1409 | } |
| 1410 | if (HasTemplateScope) { |
| 1411 | TempScope.Exit(); |
| 1412 | } |
| 1413 | } else { |
| 1414 | // If there are multiple decls, then the decl cannot be within the |
| 1415 | // function scope. |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1416 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1417 | nullptr, SourceLocation(), AttributeList::AS_GNU, |
| 1418 | nullptr); |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1419 | } |
DeLesley Hutchins | 71d6103 | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 1420 | } else { |
| 1421 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
Aaron Ballman | 9a99e0d | 2014-01-20 17:18:35 +0000 | [diff] [blame] | 1424 | const AttributeList *AL = Attrs.getList(); |
| 1425 | if (OnDefinition && AL && !AL->isCXX11Attribute() && |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1426 | AL->isKnownToGCC()) |
Aaron Ballman | 9a99e0d | 2014-01-20 17:18:35 +0000 | [diff] [blame] | 1427 | Diag(Tok, diag::warn_attribute_on_function_definition) |
| 1428 | << &LA.AttrName; |
| 1429 | |
| 1430 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1431 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1432 | |
David Majnemer | d5946f5 | 2015-01-13 08:35:24 +0000 | [diff] [blame] | 1433 | // Due to a parsing error, we either went over the cached tokens or |
| 1434 | // there are still cached tokens left, so we skip the leftover tokens. |
| 1435 | while (Tok.isNot(tok::eof)) |
| 1436 | ConsumeAnyToken(); |
| 1437 | |
| 1438 | if (Tok.is(tok::eof) && Tok.getEofData() == AttrEnd.getEofData()) |
| 1439 | ConsumeAnyToken(); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1442 | void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName, |
| 1443 | SourceLocation AttrNameLoc, |
| 1444 | ParsedAttributes &Attrs, |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1445 | SourceLocation *EndLoc, |
| 1446 | IdentifierInfo *ScopeName, |
| 1447 | SourceLocation ScopeLoc, |
| 1448 | AttributeList::Syntax Syntax) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1449 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 1450 | |
| 1451 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1452 | T.consumeOpen(); |
| 1453 | |
| 1454 | if (Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1455 | Diag(Tok, diag::err_expected) << tok::identifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1456 | T.skipToEnd(); |
| 1457 | return; |
| 1458 | } |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 1459 | IdentifierLoc *ArgumentKind = ParseIdentifierLoc(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1460 | |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 1461 | if (ExpectAndConsume(tok::comma)) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1462 | T.skipToEnd(); |
| 1463 | return; |
| 1464 | } |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1465 | |
| 1466 | SourceRange MatchingCTypeRange; |
| 1467 | TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange); |
| 1468 | if (MatchingCType.isInvalid()) { |
| 1469 | T.skipToEnd(); |
| 1470 | return; |
| 1471 | } |
| 1472 | |
| 1473 | bool LayoutCompatible = false; |
| 1474 | bool MustBeNull = false; |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1475 | while (TryConsumeToken(tok::comma)) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1476 | if (Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1477 | Diag(Tok, diag::err_expected) << tok::identifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1478 | T.skipToEnd(); |
| 1479 | return; |
| 1480 | } |
| 1481 | IdentifierInfo *Flag = Tok.getIdentifierInfo(); |
| 1482 | if (Flag->isStr("layout_compatible")) |
| 1483 | LayoutCompatible = true; |
| 1484 | else if (Flag->isStr("must_be_null")) |
| 1485 | MustBeNull = true; |
| 1486 | else { |
| 1487 | Diag(Tok, diag::err_type_safety_unknown_flag) << Flag; |
| 1488 | T.skipToEnd(); |
| 1489 | return; |
| 1490 | } |
| 1491 | ConsumeToken(); // consume flag |
| 1492 | } |
| 1493 | |
| 1494 | if (!T.consumeClose()) { |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1495 | Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, ScopeName, ScopeLoc, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1496 | ArgumentKind, MatchingCType.get(), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1497 | LayoutCompatible, MustBeNull, Syntax); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | if (EndLoc) |
| 1501 | *EndLoc = T.getCloseLocation(); |
| 1502 | } |
| 1503 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1504 | /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets |
| 1505 | /// of a C++11 attribute-specifier in a location where an attribute is not |
| 1506 | /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this |
| 1507 | /// situation. |
| 1508 | /// |
| 1509 | /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if |
| 1510 | /// this doesn't appear to actually be an attribute-specifier, and the caller |
| 1511 | /// should try to parse it. |
| 1512 | bool Parser::DiagnoseProhibitedCXX11Attribute() { |
| 1513 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)); |
| 1514 | |
| 1515 | switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) { |
| 1516 | case CAK_NotAttributeSpecifier: |
| 1517 | // No diagnostic: we're in Obj-C++11 and this is not actually an attribute. |
| 1518 | return false; |
| 1519 | |
| 1520 | case CAK_InvalidAttributeSpecifier: |
| 1521 | Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute); |
| 1522 | return false; |
| 1523 | |
| 1524 | case CAK_AttributeSpecifier: |
| 1525 | // Parse and discard the attributes. |
| 1526 | SourceLocation BeginLoc = ConsumeBracket(); |
| 1527 | ConsumeBracket(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1528 | SkipUntil(tok::r_square); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1529 | assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied"); |
| 1530 | SourceLocation EndLoc = ConsumeBracket(); |
| 1531 | Diag(BeginLoc, diag::err_attributes_not_allowed) |
| 1532 | << SourceRange(BeginLoc, EndLoc); |
| 1533 | return true; |
| 1534 | } |
Chandler Carruth | d8f7d38 | 2012-04-10 16:03:08 +0000 | [diff] [blame] | 1535 | llvm_unreachable("All cases handled above."); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
Richard Smith | 98155ad | 2013-02-20 01:17:14 +0000 | [diff] [blame] | 1538 | /// \brief We have found the opening square brackets of a C++11 |
| 1539 | /// attribute-specifier in a location where an attribute is not permitted, but |
| 1540 | /// we know where the attributes ought to be written. Parse them anyway, and |
| 1541 | /// provide a fixit moving them to the right place. |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1542 | void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs, |
| 1543 | SourceLocation CorrectLocation) { |
| 1544 | assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) || |
| 1545 | Tok.is(tok::kw_alignas)); |
| 1546 | |
| 1547 | // Consume the attributes. |
| 1548 | SourceLocation Loc = Tok.getLocation(); |
| 1549 | ParseCXX11Attributes(Attrs); |
| 1550 | CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true); |
| 1551 | |
| 1552 | Diag(Loc, diag::err_attributes_not_allowed) |
| 1553 | << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange) |
| 1554 | << FixItHint::CreateRemoval(AttrRange); |
| 1555 | } |
| 1556 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1557 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 1558 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 1559 | << attrs.Range; |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
Richard Smith | 49cc1cc | 2016-08-18 21:59:42 +0000 | [diff] [blame] | 1562 | void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &Attrs, |
| 1563 | unsigned DiagID) { |
| 1564 | for (AttributeList *Attr = Attrs.getList(); Attr; Attr = Attr->getNext()) { |
| 1565 | if (!Attr->isCXX11Attribute()) |
| 1566 | continue; |
| 1567 | if (Attr->getKind() == AttributeList::UnknownAttribute) |
| 1568 | Diag(Attr->getLoc(), diag::warn_unknown_attribute_ignored) |
| 1569 | << Attr->getName(); |
| 1570 | else { |
| 1571 | Diag(Attr->getLoc(), DiagID) |
| 1572 | << Attr->getName(); |
| 1573 | Attr->setInvalid(); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1574 | } |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1575 | } |
| 1576 | } |
| 1577 | |
Nico Weber | 32a0fc7 | 2016-09-03 03:01:32 +0000 | [diff] [blame] | 1578 | // Usually, `__attribute__((attrib)) class Foo {} var` means that attribute |
| 1579 | // applies to var, not the type Foo. |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1580 | // As an exception to the rule, __declspec(align(...)) before the |
| 1581 | // class-key affects the type instead of the variable. |
Nico Weber | 32a0fc7 | 2016-09-03 03:01:32 +0000 | [diff] [blame] | 1582 | // Also, Microsoft-style [attributes] seem to affect the type instead of the |
| 1583 | // variable. |
| 1584 | // This function moves attributes that should apply to the type off DS to Attrs. |
| 1585 | void Parser::stripTypeAttributesOffDeclSpec(ParsedAttributesWithRange &Attrs, |
| 1586 | DeclSpec &DS, |
| 1587 | Sema::TagUseKind TUK) { |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1588 | if (TUK == Sema::TUK_Reference) |
| 1589 | return; |
| 1590 | |
| 1591 | ParsedAttributes &PA = DS.getAttributes(); |
| 1592 | AttributeList *AL = PA.getList(); |
| 1593 | AttributeList *Prev = nullptr; |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 1594 | AttributeList *TypeAttrHead = nullptr; |
| 1595 | AttributeList *TypeAttrTail = nullptr; |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1596 | while (AL) { |
| 1597 | AttributeList *Next = AL->getNext(); |
| 1598 | |
Nico Weber | 32a0fc7 | 2016-09-03 03:01:32 +0000 | [diff] [blame] | 1599 | if ((AL->getKind() == AttributeList::AT_Aligned && |
| 1600 | AL->isDeclspecAttribute()) || |
| 1601 | AL->isMicrosoftAttribute()) { |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1602 | // Stitch the attribute into the tag's attribute list. |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 1603 | if (TypeAttrTail) |
| 1604 | TypeAttrTail->setNext(AL); |
| 1605 | else |
| 1606 | TypeAttrHead = AL; |
| 1607 | TypeAttrTail = AL; |
| 1608 | TypeAttrTail->setNext(nullptr); |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1609 | |
| 1610 | // Remove the attribute from the variable's attribute list. |
| 1611 | if (Prev) { |
| 1612 | // Set the last variable attribute's next attribute to be the attribute |
| 1613 | // after the current one. |
| 1614 | Prev->setNext(Next); |
| 1615 | } else { |
| 1616 | // Removing the head of the list requires us to reset the head to the |
| 1617 | // next attribute. |
| 1618 | PA.set(Next); |
| 1619 | } |
| 1620 | } else { |
| 1621 | Prev = AL; |
| 1622 | } |
| 1623 | |
| 1624 | AL = Next; |
| 1625 | } |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 1626 | |
| 1627 | // Find end of type attributes Attrs and add NewTypeAttributes in the same |
| 1628 | // order they were in originally. (Remember, in AttributeList things earlier |
| 1629 | // in source order are later in the list, since new attributes are added to |
| 1630 | // the front of the list.) |
| 1631 | Attrs.addAllAtEnd(TypeAttrHead); |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1634 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 1635 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1636 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 1637 | /// location of the semicolon in DeclEnd. |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1638 | /// |
| 1639 | /// declaration: [C99 6.7] |
| 1640 | /// block-declaration -> |
| 1641 | /// simple-declaration |
| 1642 | /// others [FIXME] |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1643 | /// [C++] template-declaration |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1644 | /// [C++] namespace-definition |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1645 | /// [C++] using-directive |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1646 | /// [C++] using-declaration |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1647 | /// [C++11/C11] static_assert-declaration |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1648 | /// others... [FIXME] |
| 1649 | /// |
Rafael Espindola | 1bd906d | 2014-10-22 14:27:08 +0000 | [diff] [blame] | 1650 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context, |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1651 | SourceLocation &DeclEnd, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1652 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 355094e | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 1653 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | 59b7528 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 1654 | // Must temporarily exit the objective-c container scope for |
| 1655 | // parsing c none objective-c decls. |
| 1656 | ObjCDeclContextSwitch ObjCDC(*this); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1657 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1658 | Decl *SingleDecl = nullptr; |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1659 | switch (Tok.getKind()) { |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1660 | case tok::kw_template: |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1661 | case tok::kw_export: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1662 | ProhibitAttributes(attrs); |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1663 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1664 | break; |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1665 | case tok::kw_inline: |
Sebastian Redl | 5a5f2c7 | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 1666 | // 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] | 1667 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1668 | ProhibitAttributes(attrs); |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1669 | SourceLocation InlineLoc = ConsumeToken(); |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 1670 | return ParseNamespace(Context, DeclEnd, InlineLoc); |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1671 | } |
Rafael Espindola | 1bd906d | 2014-10-22 14:27:08 +0000 | [diff] [blame] | 1672 | return ParseSimpleDeclaration(Context, DeclEnd, attrs, |
Fariborz Jahanian | 1db5c94 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1673 | true); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1674 | case tok::kw_namespace: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1675 | ProhibitAttributes(attrs); |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 1676 | return ParseNamespace(Context, DeclEnd); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1677 | case tok::kw_using: |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 1678 | return ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
| 1679 | DeclEnd, attrs); |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1680 | case tok::kw_static_assert: |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1681 | case tok::kw__Static_assert: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1682 | ProhibitAttributes(attrs); |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1683 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1684 | break; |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1685 | default: |
Rafael Espindola | 1bd906d | 2014-10-22 14:27:08 +0000 | [diff] [blame] | 1686 | return ParseSimpleDeclaration(Context, DeclEnd, attrs, true); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1687 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1688 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1689 | // 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] | 1690 | // single decl, convert it now. |
| 1691 | return Actions.ConvertDeclToDeclGroup(SingleDecl); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 1695 | /// declaration-specifiers init-declarator-list[opt] ';' |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1696 | /// [C++11] attribute-specifier-seq decl-specifier-seq[opt] |
| 1697 | /// init-declarator-list ';' |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1698 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 1699 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1700 | /// |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1701 | /// for-range-declaration: [C++11 6.5p1: stmt.ranged] |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1702 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1703 | /// |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1704 | /// 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] | 1705 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1706 | /// |
| 1707 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 1708 | /// of a simple-declaration. If we find that we are, we also parse the |
| 1709 | /// for-range-initializer, and place it here. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1710 | Parser::DeclGroupPtrTy |
Rafael Espindola | 1bd906d | 2014-10-22 14:27:08 +0000 | [diff] [blame] | 1711 | Parser::ParseSimpleDeclaration(unsigned Context, |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1712 | SourceLocation &DeclEnd, |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1713 | ParsedAttributesWithRange &Attrs, |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1714 | bool RequireSemi, ForRangeInit *FRI) { |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1715 | // Parse the common declaration-specifiers piece. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1716 | ParsingDeclSpec DS(*this); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1717 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 1718 | DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context); |
| 1719 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext); |
| 1720 | |
| 1721 | // If we had a free-standing type definition with a missing semicolon, we |
| 1722 | // may get this far before the problem becomes obvious. |
| 1723 | if (DS.hasTagDefinition() && |
| 1724 | DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext)) |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 1725 | return nullptr; |
Abramo Bagnara | 1cd8368 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 1726 | |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 1727 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 1728 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1729 | if (Tok.is(tok::semi)) { |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1730 | ProhibitAttributes(Attrs); |
Argyrios Kyrtzidis | fbb2bb5 | 2012-05-16 23:49:15 +0000 | [diff] [blame] | 1731 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 005fc1b | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1732 | if (RequireSemi) ConsumeToken(); |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 1733 | RecordDecl *AnonRecord = nullptr; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1734 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 1735 | DS, AnonRecord); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1736 | DS.complete(TheDecl); |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 1737 | if (AnonRecord) { |
| 1738 | Decl* decls[] = {AnonRecord, TheDecl}; |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 1739 | return Actions.BuildDeclaratorGroup(decls); |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 1740 | } |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1741 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 1742 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1743 | |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1744 | DS.takeAttributesFrom(Attrs); |
Reid Kleckner | d61a311 | 2014-12-15 23:16:32 +0000 | [diff] [blame] | 1745 | return ParseDeclGroup(DS, Context, &DeclEnd, FRI); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1746 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1747 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1748 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1749 | /// for a declarator. |
| 1750 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 1751 | switch (Tok.getKind()) { |
| 1752 | case tok::annot_cxxscope: |
| 1753 | case tok::annot_template_id: |
| 1754 | case tok::caret: |
| 1755 | case tok::code_completion: |
| 1756 | case tok::coloncolon: |
| 1757 | case tok::ellipsis: |
| 1758 | case tok::kw___attribute: |
| 1759 | case tok::kw_operator: |
| 1760 | case tok::l_paren: |
| 1761 | case tok::star: |
| 1762 | return true; |
| 1763 | |
| 1764 | case tok::amp: |
| 1765 | case tok::ampamp: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1766 | return getLangOpts().CPlusPlus; |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1767 | |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1768 | case tok::l_square: // Might be an attribute on an unnamed bit-field. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1769 | return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 && |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1770 | NextToken().is(tok::l_square); |
| 1771 | |
| 1772 | case tok::colon: // Might be a typo for '::' or an unnamed bit-field. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1773 | return Context == Declarator::MemberContext || getLangOpts().CPlusPlus; |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1774 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1775 | case tok::identifier: |
| 1776 | switch (NextToken().getKind()) { |
| 1777 | case tok::code_completion: |
| 1778 | case tok::coloncolon: |
| 1779 | case tok::comma: |
| 1780 | case tok::equal: |
| 1781 | case tok::equalequal: // Might be a typo for '='. |
| 1782 | case tok::kw_alignas: |
| 1783 | case tok::kw_asm: |
| 1784 | case tok::kw___attribute: |
| 1785 | case tok::l_brace: |
| 1786 | case tok::l_paren: |
| 1787 | case tok::l_square: |
| 1788 | case tok::less: |
| 1789 | case tok::r_brace: |
| 1790 | case tok::r_paren: |
| 1791 | case tok::r_square: |
| 1792 | case tok::semi: |
| 1793 | return true; |
| 1794 | |
| 1795 | case tok::colon: |
| 1796 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1797 | // and in block scope it's probably a label. Inside a class definition, |
| 1798 | // this is a bit-field. |
| 1799 | return Context == Declarator::MemberContext || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1800 | (getLangOpts().CPlusPlus && Context == Declarator::FileContext); |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1801 | |
| 1802 | case tok::identifier: // Possible virt-specifier. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1803 | return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken()); |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1804 | |
| 1805 | default: |
| 1806 | return false; |
| 1807 | } |
| 1808 | |
| 1809 | default: |
| 1810 | return false; |
| 1811 | } |
| 1812 | } |
| 1813 | |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1814 | /// Skip until we reach something which seems like a sensible place to pick |
| 1815 | /// up parsing after a malformed declaration. This will sometimes stop sooner |
| 1816 | /// than SkipUntil(tok::r_brace) would, but will never stop later. |
| 1817 | void Parser::SkipMalformedDecl() { |
| 1818 | while (true) { |
| 1819 | switch (Tok.getKind()) { |
| 1820 | case tok::l_brace: |
| 1821 | // Skip until matching }, then stop. We've probably skipped over |
| 1822 | // a malformed class or function definition or similar. |
| 1823 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1824 | SkipUntil(tok::r_brace); |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1825 | if (Tok.isOneOf(tok::comma, tok::l_brace, tok::kw_try)) { |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1826 | // This declaration isn't over yet. Keep skipping. |
| 1827 | continue; |
| 1828 | } |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1829 | TryConsumeToken(tok::semi); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1830 | return; |
| 1831 | |
| 1832 | case tok::l_square: |
| 1833 | ConsumeBracket(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1834 | SkipUntil(tok::r_square); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1835 | continue; |
| 1836 | |
| 1837 | case tok::l_paren: |
| 1838 | ConsumeParen(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1839 | SkipUntil(tok::r_paren); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1840 | continue; |
| 1841 | |
| 1842 | case tok::r_brace: |
| 1843 | return; |
| 1844 | |
| 1845 | case tok::semi: |
| 1846 | ConsumeToken(); |
| 1847 | return; |
| 1848 | |
| 1849 | case tok::kw_inline: |
| 1850 | // 'inline namespace' at the start of a line is almost certainly |
Jordan Rose | 12e730c | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1851 | // a good place to pick back up parsing, except in an Objective-C |
| 1852 | // @interface context. |
| 1853 | if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) && |
| 1854 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1855 | return; |
| 1856 | break; |
| 1857 | |
| 1858 | case tok::kw_namespace: |
| 1859 | // 'namespace' at the start of a line is almost certainly a good |
Jordan Rose | 12e730c | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1860 | // place to pick back up parsing, except in an Objective-C |
| 1861 | // @interface context. |
| 1862 | if (Tok.isAtStartOfLine() && |
| 1863 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
| 1864 | return; |
| 1865 | break; |
| 1866 | |
| 1867 | case tok::at: |
| 1868 | // @end is very much like } in Objective-C contexts. |
| 1869 | if (NextToken().isObjCAtKeyword(tok::objc_end) && |
| 1870 | ParsingInObjCContainer) |
| 1871 | return; |
| 1872 | break; |
| 1873 | |
| 1874 | case tok::minus: |
| 1875 | case tok::plus: |
| 1876 | // - and + probably start new method declarations in Objective-C contexts. |
| 1877 | if (Tok.isAtStartOfLine() && ParsingInObjCContainer) |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1878 | return; |
| 1879 | break; |
| 1880 | |
| 1881 | case tok::eof: |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 1882 | case tok::annot_module_begin: |
| 1883 | case tok::annot_module_end: |
| 1884 | case tok::annot_module_include: |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1885 | return; |
| 1886 | |
| 1887 | default: |
| 1888 | break; |
| 1889 | } |
| 1890 | |
| 1891 | ConsumeAnyToken(); |
| 1892 | } |
| 1893 | } |
| 1894 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1895 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1896 | /// definition or a group of object declarations, actually parse the |
| 1897 | /// result. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1898 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1899 | unsigned Context, |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1900 | SourceLocation *DeclEnd, |
| 1901 | ForRangeInit *FRI) { |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1902 | // Parse the first declarator. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1903 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1904 | ParseDeclarator(D); |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1905 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1906 | // Bail out if the first declarator didn't seem well-formed. |
| 1907 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1908 | SkipMalformedDecl(); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 1909 | return nullptr; |
Chris Lattner | efb0f11 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1910 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1911 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1912 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1913 | // appropriate function scope after the function Decl has been constructed. |
DeLesley Hutchins | 66e300e | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1914 | // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList. |
| 1915 | LateParsedAttrList LateParsedAttrs(true); |
Richard Smith | 99c464c | 2014-11-10 21:10:32 +0000 | [diff] [blame] | 1916 | if (D.isFunctionDeclarator()) { |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1917 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1918 | |
Richard Smith | 99c464c | 2014-11-10 21:10:32 +0000 | [diff] [blame] | 1919 | // The _Noreturn keyword can't appear here, unlike the GNU noreturn |
| 1920 | // attribute. If we find the keyword here, tell the user to put it |
| 1921 | // at the start instead. |
| 1922 | if (Tok.is(tok::kw__Noreturn)) { |
| 1923 | SourceLocation Loc = ConsumeToken(); |
| 1924 | const char *PrevSpec; |
| 1925 | unsigned DiagID; |
| 1926 | |
| 1927 | // We can offer a fixit if it's valid to mark this function as _Noreturn |
| 1928 | // and we don't have any other declarators in this declaration. |
| 1929 | bool Fixit = !DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID); |
| 1930 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1931 | Fixit &= Tok.isOneOf(tok::semi, tok::l_brace, tok::kw_try); |
Richard Smith | 99c464c | 2014-11-10 21:10:32 +0000 | [diff] [blame] | 1932 | |
| 1933 | Diag(Loc, diag::err_c11_noreturn_misplaced) |
| 1934 | << (Fixit ? FixItHint::CreateRemoval(Loc) : FixItHint()) |
| 1935 | << (Fixit ? FixItHint::CreateInsertion(D.getLocStart(), "_Noreturn ") |
| 1936 | : FixItHint()); |
| 1937 | } |
| 1938 | } |
| 1939 | |
Chris Lattner | dbb1e93 | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1940 | // 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] | 1941 | if (D.isFunctionDeclarator() && |
Chris Lattner | dbb1e93 | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1942 | // Look at the next token to make sure that this isn't a function |
| 1943 | // declaration. We have to check this because __attribute__ might be the |
| 1944 | // start of a function definition in GCC-extended K&R C. |
Fariborz Jahanian | 712bb81 | 2012-08-10 15:54:40 +0000 | [diff] [blame] | 1945 | !isDeclarationAfterDeclarator()) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1946 | |
Reid Kleckner | d61a311 | 2014-12-15 23:16:32 +0000 | [diff] [blame] | 1947 | // Function definitions are only allowed at file scope and in C++ classes. |
| 1948 | // The C++ inline method definition case is handled elsewhere, so we only |
| 1949 | // need to handle the file scope definition case. |
| 1950 | if (Context == Declarator::FileContext) { |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1951 | if (isStartOfFunctionDefinition(D)) { |
| 1952 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1953 | Diag(Tok, diag::err_function_declared_typedef); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1954 | |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1955 | // Recover by treating the 'typedef' as spurious. |
| 1956 | DS.ClearStorageClassSpecs(); |
| 1957 | } |
| 1958 | |
| 1959 | Decl *TheDecl = |
| 1960 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
| 1961 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1964 | if (isDeclarationSpecifier()) { |
Nico Weber | 6b05f38 | 2015-02-18 04:53:03 +0000 | [diff] [blame] | 1965 | // If there is an invalid declaration specifier right after the |
| 1966 | // function prototype, then we must be in a missing semicolon case |
| 1967 | // where this isn't actually a body. Just fall through into the code |
| 1968 | // that handles it as a prototype, and let the top-level code handle |
| 1969 | // the erroneous declspec where it would otherwise expect a comma or |
| 1970 | // semicolon. |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1971 | } else { |
| 1972 | Diag(Tok, diag::err_expected_fn_body); |
| 1973 | SkipUntil(tok::semi); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 1974 | return nullptr; |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1975 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1976 | } else { |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1977 | if (Tok.is(tok::l_brace)) { |
| 1978 | Diag(Tok, diag::err_function_definition_not_allowed); |
Serge Pavlov | 1de5151 | 2013-12-09 05:25:47 +0000 | [diff] [blame] | 1979 | SkipMalformedDecl(); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 1980 | return nullptr; |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1981 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1982 | } |
| 1983 | } |
| 1984 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1985 | if (ParseAsmAttributesAfterDeclarator(D)) |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 1986 | return nullptr; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1987 | |
| 1988 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1989 | // must parse and analyze the for-range-initializer before the declaration is |
| 1990 | // analyzed. |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1991 | // |
| 1992 | // Handle the Objective-C for-in loop variable similarly, although we |
| 1993 | // don't need to parse the container in advance. |
| 1994 | if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) { |
| 1995 | bool IsForRangeLoop = false; |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1996 | if (TryConsumeToken(tok::colon, FRI->ColonLoc)) { |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1997 | IsForRangeLoop = true; |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1998 | if (Tok.is(tok::l_brace)) |
| 1999 | FRI->RangeExpr = ParseBraceInitializer(); |
| 2000 | else |
| 2001 | FRI->RangeExpr = ParseExpression(); |
| 2002 | } |
| 2003 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2004 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 2005 | if (IsForRangeLoop) |
| 2006 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2007 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | cf6e0c8 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 2008 | D.complete(ThisDecl); |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 2009 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2012 | SmallVector<Decl *, 8> DeclsInGroup; |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2013 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes( |
| 2014 | D, ParsedTemplateInfo(), FRI); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2015 | if (LateParsedAttrs.size() > 0) |
| 2016 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2017 | D.complete(FirstDecl); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2018 | if (FirstDecl) |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2019 | DeclsInGroup.push_back(FirstDecl); |
| 2020 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 2021 | bool ExpectSemi = Context != Declarator::ForContext; |
Fariborz Jahanian | 577574a | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 2022 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2023 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 2024 | // error, bail out. |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 2025 | SourceLocation CommaLoc; |
| 2026 | while (TryConsumeToken(tok::comma, CommaLoc)) { |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 2027 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 2028 | // This comma was followed by a line-break and something which can't be |
| 2029 | // the start of a declarator. The comma was probably a typo for a |
| 2030 | // semicolon. |
| 2031 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 2032 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 2033 | ExpectSemi = false; |
| 2034 | break; |
| 2035 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2036 | |
| 2037 | // Parse the next declarator. |
| 2038 | D.clear(); |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2039 | D.setCommaLoc(CommaLoc); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2040 | |
| 2041 | // Accept attributes in an init-declarator. In the first declarator in a |
| 2042 | // declaration, these would be part of the declspec. In subsequent |
| 2043 | // declarators, they become part of the declarator itself, so that they |
| 2044 | // don't apply to declarators after *this* one. Examples: |
| 2045 | // short __attribute__((common)) var; -> declspec |
| 2046 | // short var __attribute__((common)); -> declarator |
| 2047 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2048 | MaybeParseGNUAttributes(D); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2049 | |
Nico Rieck | eaaae27 | 2014-12-04 23:31:08 +0000 | [diff] [blame] | 2050 | // MSVC parses but ignores qualifiers after the comma as an extension. |
| 2051 | if (getLangOpts().MicrosoftExt) |
| 2052 | DiagnoseAndSkipExtendedMicrosoftTypeAttributes(); |
| 2053 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2054 | ParseDeclarator(D); |
Fariborz Jahanian | 372030b | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 2055 | if (!D.isInvalidType()) { |
| 2056 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 2057 | D.complete(ThisDecl); |
| 2058 | if (ThisDecl) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2059 | DeclsInGroup.push_back(ThisDecl); |
Fariborz Jahanian | 372030b | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 2060 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | if (DeclEnd) |
| 2064 | *DeclEnd = Tok.getLocation(); |
| 2065 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 2066 | if (ExpectSemi && |
Chris Lattner | 02f1b61 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 2067 | ExpectAndConsumeSemi(Context == Declarator::FileContext |
| 2068 | ? diag::err_invalid_token_after_toplevel_declarator |
| 2069 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 1390134 | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 2070 | // Okay, there was no semicolon and one was expected. If we see a |
| 2071 | // declaration specifier, just assume it was missing and continue parsing. |
| 2072 | // Otherwise things are very confused and we skip to recover. |
| 2073 | if (!isDeclarationSpecifier()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2074 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 2075 | TryConsumeToken(tok::semi); |
Chris Lattner | 1390134 | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 2076 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 2079 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup); |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2082 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 2083 | /// declarator. Returns true on an error. |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2084 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2085 | // If a simple-asm-expr is present, parse it. |
| 2086 | if (Tok.is(tok::kw_asm)) { |
| 2087 | SourceLocation Loc; |
| 2088 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 2089 | if (AsmLabel.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2090 | SkipUntil(tok::semi, StopBeforeMatch); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2091 | return true; |
| 2092 | } |
| 2093 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2094 | D.setAsmLabel(AsmLabel.get()); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2095 | D.SetRangeEnd(Loc); |
| 2096 | } |
| 2097 | |
| 2098 | MaybeParseGNUAttributes(D); |
| 2099 | return false; |
| 2100 | } |
| 2101 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2102 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 2103 | /// declarator'. This method parses the remainder of the declaration |
| 2104 | /// (including any attributes or initializer, among other things) and |
| 2105 | /// finalizes the declaration. |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 2106 | /// |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 2107 | /// init-declarator: [C99 6.7] |
| 2108 | /// declarator |
| 2109 | /// declarator '=' initializer |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 2110 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 2111 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2112 | /// [C++] declarator initializer[opt] |
| 2113 | /// |
| 2114 | /// [C++] initializer: |
| 2115 | /// [C++] '=' initializer-clause |
| 2116 | /// [C++] '(' expression-list ')' |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 2117 | /// [C++0x] '=' 'default' [TODO] |
| 2118 | /// [C++0x] '=' 'delete' |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2119 | /// [C++0x] braced-init-list |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 2120 | /// |
| 2121 | /// According to the standard grammar, =default and =delete are function |
| 2122 | /// definitions, but that definitely doesn't fit with the parser here. |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 2123 | /// |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2124 | Decl *Parser::ParseDeclarationAfterDeclarator( |
| 2125 | Declarator &D, const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2126 | if (ParseAsmAttributesAfterDeclarator(D)) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2127 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2128 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2129 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 2130 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2131 | |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2132 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes( |
| 2133 | Declarator &D, const ParsedTemplateInfo &TemplateInfo, ForRangeInit *FRI) { |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 2134 | // RAII type used to track whether we're inside an initializer. |
| 2135 | struct InitializerScopeRAII { |
| 2136 | Parser &P; |
| 2137 | Declarator &D; |
| 2138 | Decl *ThisDecl; |
| 2139 | |
| 2140 | InitializerScopeRAII(Parser &P, Declarator &D, Decl *ThisDecl) |
| 2141 | : P(P), D(D), ThisDecl(ThisDecl) { |
| 2142 | if (ThisDecl && P.getLangOpts().CPlusPlus) { |
| 2143 | Scope *S = nullptr; |
| 2144 | if (D.getCXXScopeSpec().isSet()) { |
| 2145 | P.EnterScope(0); |
| 2146 | S = P.getCurScope(); |
| 2147 | } |
| 2148 | P.Actions.ActOnCXXEnterDeclInitializer(S, ThisDecl); |
| 2149 | } |
| 2150 | } |
| 2151 | ~InitializerScopeRAII() { pop(); } |
| 2152 | void pop() { |
| 2153 | if (ThisDecl && P.getLangOpts().CPlusPlus) { |
| 2154 | Scope *S = nullptr; |
| 2155 | if (D.getCXXScopeSpec().isSet()) |
| 2156 | S = P.getCurScope(); |
| 2157 | P.Actions.ActOnCXXExitDeclInitializer(S, ThisDecl); |
| 2158 | if (S) |
| 2159 | P.ExitScope(); |
| 2160 | } |
| 2161 | ThisDecl = nullptr; |
| 2162 | } |
| 2163 | }; |
| 2164 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2165 | // Inform the current actions module that we just parsed this declarator. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2166 | Decl *ThisDecl = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2167 | switch (TemplateInfo.Kind) { |
| 2168 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2169 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2170 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2171 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2172 | case ParsedTemplateInfo::Template: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2173 | case ParsedTemplateInfo::ExplicitSpecialization: { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2174 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2175 | *TemplateInfo.TemplateParams, |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2176 | D); |
Larisse Voufo | 833b05a | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 2177 | if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2178 | // Re-direct this decl to refer to the templated decl so that we can |
| 2179 | // initialize it. |
| 2180 | ThisDecl = VT->getTemplatedDecl(); |
| 2181 | break; |
| 2182 | } |
| 2183 | case ParsedTemplateInfo::ExplicitInstantiation: { |
| 2184 | if (Tok.is(tok::semi)) { |
| 2185 | DeclResult ThisRes = Actions.ActOnExplicitInstantiation( |
| 2186 | getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D); |
| 2187 | if (ThisRes.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2188 | SkipUntil(tok::semi, StopBeforeMatch); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2189 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2190 | } |
| 2191 | ThisDecl = ThisRes.get(); |
| 2192 | } else { |
| 2193 | // FIXME: This check should be for a variable template instantiation only. |
| 2194 | |
| 2195 | // Check that this is a valid instantiation |
| 2196 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 2197 | // If the declarator-id is not a template-id, issue a diagnostic and |
| 2198 | // recover by ignoring the 'template' keyword. |
| 2199 | Diag(Tok, diag::err_template_defn_explicit_instantiation) |
| 2200 | << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc); |
| 2201 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 2202 | } else { |
| 2203 | SourceLocation LAngleLoc = |
| 2204 | PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
| 2205 | Diag(D.getIdentifierLoc(), |
| 2206 | diag::err_explicit_instantiation_with_definition) |
| 2207 | << SourceRange(TemplateInfo.TemplateLoc) |
| 2208 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
| 2209 | |
| 2210 | // Recover as if it were an explicit specialization. |
| 2211 | TemplateParameterLists FakedParamLists; |
| 2212 | FakedParamLists.push_back(Actions.ActOnTemplateParameterList( |
Craig Topper | 96225a5 | 2015-12-24 23:58:25 +0000 | [diff] [blame] | 2213 | 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, None, |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 2214 | LAngleLoc, nullptr)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2215 | |
| 2216 | ThisDecl = |
| 2217 | Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D); |
| 2218 | } |
| 2219 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2220 | break; |
| 2221 | } |
| 2222 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2223 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2224 | // Parse declarator '=' initializer. |
Richard Trieu | c64d323 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 2225 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | 4972a6d | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 2226 | if (isTokenEqualOrEqualTypo()) { |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2227 | SourceLocation EqualLoc = ConsumeToken(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2228 | |
Anders Carlsson | 991285e | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 2229 | if (Tok.is(tok::kw_delete)) { |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2230 | if (D.isFunctionDeclarator()) |
| 2231 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 2232 | << 1 /* delete */; |
| 2233 | else |
| 2234 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Alexis Hunt | 5dafebc | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 2235 | } else if (Tok.is(tok::kw_default)) { |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2236 | if (D.isFunctionDeclarator()) |
Sebastian Redl | 46afb55 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 2237 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 2238 | << 0 /* default */; |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2239 | else |
| 2240 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2241 | } else { |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 2242 | InitializerScopeRAII InitScope(*this, D, ThisDecl); |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 2243 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2244 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2245 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Peter Collingbourne | 6b4fdc2 | 2012-07-27 12:56:09 +0000 | [diff] [blame] | 2246 | Actions.FinalizeDeclaration(ThisDecl); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2247 | cutOffParsing(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2248 | return nullptr; |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2249 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2250 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2251 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 2252 | |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2253 | // If this is the only decl in (possibly) range based for statement, |
| 2254 | // our best guess is that the user meant ':' instead of '='. |
| 2255 | if (Tok.is(tok::r_paren) && FRI && D.isFirstDeclarator()) { |
| 2256 | Diag(EqualLoc, diag::err_single_decl_assign_in_for_range) |
| 2257 | << FixItHint::CreateReplacement(EqualLoc, ":"); |
| 2258 | // We are trying to stop parser from looking for ';' in this for |
| 2259 | // statement, therefore preventing spurious errors to be issued. |
| 2260 | FRI->ColonLoc = EqualLoc; |
| 2261 | Init = ExprError(); |
| 2262 | FRI->RangeExpr = Init; |
| 2263 | } |
| 2264 | |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 2265 | InitScope.pop(); |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 2266 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2267 | if (Init.isInvalid()) { |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2268 | SmallVector<tok::TokenKind, 2> StopTokens; |
| 2269 | StopTokens.push_back(tok::comma); |
Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 2270 | if (D.getContext() == Declarator::ForContext || |
| 2271 | D.getContext() == Declarator::InitStmtContext) |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2272 | StopTokens.push_back(tok::r_paren); |
| 2273 | SkipUntil(StopTokens, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 604c302 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 2274 | Actions.ActOnInitializerError(ThisDecl); |
| 2275 | } else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2276 | Actions.AddInitializerToDecl(ThisDecl, Init.get(), |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2277 | /*DirectInit=*/false); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2278 | } |
| 2279 | } else if (Tok.is(tok::l_paren)) { |
| 2280 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2281 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2282 | T.consumeOpen(); |
| 2283 | |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2284 | ExprVector Exprs; |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2285 | CommaLocsTy CommaLocs; |
| 2286 | |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 2287 | InitializerScopeRAII InitScope(*this, D, ThisDecl); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 2288 | |
Ilya Biryukov | cbea95d | 2017-09-08 13:36:38 +0000 | [diff] [blame] | 2289 | llvm::function_ref<void()> ExprListCompleter; |
| 2290 | auto ThisVarDecl = dyn_cast_or_null<VarDecl>(ThisDecl); |
| 2291 | auto ConstructorCompleter = [&, ThisVarDecl] { |
| 2292 | Actions.CodeCompleteConstructor( |
| 2293 | getCurScope(), ThisVarDecl->getType()->getCanonicalTypeInternal(), |
| 2294 | ThisDecl->getLocation(), Exprs); |
| 2295 | }; |
| 2296 | if (ThisVarDecl) { |
| 2297 | // ParseExpressionList can sometimes succeed even when ThisDecl is not |
| 2298 | // VarDecl. This is an error and it is reported in a call to |
| 2299 | // Actions.ActOnInitializerError(). However, we call |
| 2300 | // CodeCompleteConstructor only on VarDecls, falling back to default |
| 2301 | // completer in other cases. |
| 2302 | ExprListCompleter = ConstructorCompleter; |
| 2303 | } |
| 2304 | |
| 2305 | if (ParseExpressionList(Exprs, CommaLocs, ExprListCompleter)) { |
David Blaikie | eae0411 | 2012-10-10 23:15:05 +0000 | [diff] [blame] | 2306 | Actions.ActOnInitializerError(ThisDecl); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2307 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2308 | } else { |
| 2309 | // Match the ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2310 | T.consumeClose(); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2311 | |
| 2312 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 2313 | "Unexpected number of commas!"); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 2314 | |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 2315 | InitScope.pop(); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 2316 | |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2317 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 2318 | T.getCloseLocation(), |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2319 | Exprs); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2320 | Actions.AddInitializerToDecl(ThisDecl, Initializer.get(), |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2321 | /*DirectInit=*/true); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2322 | } |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2323 | } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) && |
Fariborz Jahanian | 8be1ecd | 2012-07-03 23:22:13 +0000 | [diff] [blame] | 2324 | (!CurParsedObjCImpl || !D.isFunctionDeclarator())) { |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2325 | // Parse C++0x braced-init-list. |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2326 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 2327 | |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 2328 | InitializerScopeRAII InitScope(*this, D, ThisDecl); |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2329 | |
| 2330 | ExprResult Init(ParseBraceInitializer()); |
| 2331 | |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 2332 | InitScope.pop(); |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2333 | |
| 2334 | if (Init.isInvalid()) { |
| 2335 | Actions.ActOnInitializerError(ThisDecl); |
| 2336 | } else |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2337 | Actions.AddInitializerToDecl(ThisDecl, Init.get(), /*DirectInit=*/true); |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2338 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2339 | } else { |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2340 | Actions.ActOnUninitializedDecl(ThisDecl); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2341 | } |
| 2342 | |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 2343 | Actions.FinalizeDeclaration(ThisDecl); |
| 2344 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2345 | return ThisDecl; |
| 2346 | } |
| 2347 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2348 | /// ParseSpecifierQualifierList |
| 2349 | /// specifier-qualifier-list: |
| 2350 | /// type-specifier specifier-qualifier-list[opt] |
| 2351 | /// type-qualifier specifier-qualifier-list[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 2352 | /// [GNU] attributes specifier-qualifier-list[opt] |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2353 | /// |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2354 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 2355 | DeclSpecContext DSC) { |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2356 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 2357 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2358 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2359 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2360 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2361 | // Validate declspec for type-name. |
| 2362 | unsigned Specs = DS.getParsedSpecifiers(); |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 2363 | if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) { |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2364 | Diag(Tok, diag::err_expected_type); |
| 2365 | DS.SetTypeSpecError(); |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 2366 | } else if (Specs == DeclSpec::PQ_None && !DS.hasAttributes()) { |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2367 | Diag(Tok, diag::err_typename_requires_specqual); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2368 | if (!DS.hasTypeSpecifier()) |
| 2369 | DS.SetTypeSpecError(); |
| 2370 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2371 | |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 2372 | // Issue diagnostic and remove storage class if present. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2373 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 2374 | if (DS.getStorageClassSpecLoc().isValid()) |
| 2375 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 2376 | else |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2377 | Diag(DS.getThreadStorageClassSpecLoc(), |
| 2378 | diag::err_typename_invalid_storageclass); |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 2379 | DS.ClearStorageClassSpecs(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2380 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2381 | |
Craig Topper | 3f13d4d2 | 2015-11-14 18:15:55 +0000 | [diff] [blame] | 2382 | // Issue diagnostic and remove function specifier if present. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2383 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2384 | if (DS.isInlineSpecified()) |
| 2385 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 2386 | if (DS.isVirtualSpecified()) |
| 2387 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 2388 | if (DS.isExplicitSpecified()) |
| 2389 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 2390 | DS.ClearFunctionSpecs(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2391 | } |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2392 | |
| 2393 | // Issue diagnostic and remove constexpr specfier if present. |
Meador Inge | f0af05c | 2015-06-25 22:06:40 +0000 | [diff] [blame] | 2394 | if (DS.isConstexprSpecified() && DSC != DSC_condition) { |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2395 | Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr); |
| 2396 | DS.ClearConstexprSpec(); |
| 2397 | } |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2398 | } |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 2399 | |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2400 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 2401 | /// specified token is valid after the identifier in a declarator which |
| 2402 | /// immediately follows the declspec. For example, these things are valid: |
| 2403 | /// |
| 2404 | /// int x [ 4]; // direct-declarator |
| 2405 | /// int x ( int y); // direct-declarator |
| 2406 | /// int(int x ) // direct-declarator |
| 2407 | /// int x ; // simple-declaration |
| 2408 | /// int x = 17; // init-declarator-list |
| 2409 | /// int x , y; // init-declarator-list |
| 2410 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | a723ba9 | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 2411 | /// int x : 4; // struct-declarator |
Chris Lattner | 2b988c1 | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 2412 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2413 | /// |
| 2414 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 2415 | /// ')' happens to be valid anyway). |
| 2416 | /// int (x) |
| 2417 | /// |
| 2418 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2419 | return T.isOneOf(tok::l_square, tok::l_paren, tok::r_paren, tok::semi, |
| 2420 | tok::comma, tok::equal, tok::kw_asm, tok::l_brace, |
| 2421 | tok::colon); |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2422 | } |
| 2423 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2424 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 2425 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 2426 | /// the declspec has no type specifier. In this case, the declspec is either |
| 2427 | /// malformed or is "implicit int" (in K&R and C89). |
| 2428 | /// |
| 2429 | /// This method handles diagnosing this prettily and returns false if the |
| 2430 | /// declspec is done being processed. If it recovers and thinks there may be |
| 2431 | /// other pieces of declspec after it, it returns true. |
| 2432 | /// |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2433 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2434 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | a0a5d50 | 2014-05-08 22:32:00 +0000 | [diff] [blame] | 2435 | AccessSpecifier AS, DeclSpecContext DSC, |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2436 | ParsedAttributesWithRange &Attrs) { |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2437 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2438 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2439 | SourceLocation Loc = Tok.getLocation(); |
| 2440 | // If we see an identifier that is not a type name, we normally would |
| 2441 | // parse it as the identifer being declared. However, when a typename |
| 2442 | // is typo'd or the definition is not included, this will incorrectly |
| 2443 | // parse the typename as the identifier name and fall over misparsing |
| 2444 | // later parts of the diagnostic. |
| 2445 | // |
| 2446 | // As such, we try to do some look-ahead in cases where this would |
| 2447 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 2448 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 2449 | // an identifier with implicit int, we'd get a parse error because the |
| 2450 | // next token is obviously invalid for a type. Parse these as a case |
| 2451 | // with an invalid type specifier. |
| 2452 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2453 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2454 | // 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] | 2455 | // error, do lookahead to try to do better recovery. This never applies |
| 2456 | // within a type specifier. Outside of C++, we allow this even if the |
| 2457 | // language doesn't "officially" support implicit int -- we support |
Richard Smith | 3b87038 | 2013-04-30 22:43:51 +0000 | [diff] [blame] | 2458 | // implicit int as an extension in C99 and C11. |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 2459 | if (!isTypeSpecifier(DSC) && !getLangOpts().CPlusPlus && |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2460 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2461 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 2462 | // we just avoid eating the identifier, so it will be parsed as the |
| 2463 | // identifier in the declarator. |
| 2464 | return false; |
| 2465 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2466 | |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2467 | if (getLangOpts().CPlusPlus && |
| 2468 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| 2469 | // Don't require a type specifier if we have the 'auto' storage class |
| 2470 | // specifier in C++98 -- we'll promote it to a type specifier. |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2471 | if (SS) |
| 2472 | AnnotateScopeToken(*SS, /*IsNewAnnotation*/false); |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2473 | return false; |
| 2474 | } |
| 2475 | |
Reid Kleckner | 9a96e42 | 2016-05-24 21:23:54 +0000 | [diff] [blame] | 2476 | if (getLangOpts().CPlusPlus && (!SS || SS->isEmpty()) && |
| 2477 | getLangOpts().MSVCCompat) { |
| 2478 | // Lookup of an unqualified type name has failed in MSVC compatibility mode. |
| 2479 | // Give Sema a chance to recover if we are in a template with dependent base |
| 2480 | // classes. |
| 2481 | if (ParsedType T = Actions.ActOnMSVCUnknownTypeName( |
| 2482 | *Tok.getIdentifierInfo(), Tok.getLocation(), |
| 2483 | DSC == DSC_template_type_arg)) { |
| 2484 | const char *PrevSpec; |
| 2485 | unsigned DiagID; |
| 2486 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T, |
| 2487 | Actions.getASTContext().getPrintingPolicy()); |
| 2488 | DS.SetRangeEnd(Tok.getLocation()); |
| 2489 | ConsumeToken(); |
| 2490 | return false; |
| 2491 | } |
| 2492 | } |
| 2493 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2494 | // Otherwise, if we don't consume this token, we are going to emit an |
| 2495 | // error anyway. Try to recover from various common problems. Check |
| 2496 | // to see if this was a reference to a tag name without a tag specified. |
| 2497 | // 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] | 2498 | // |
| 2499 | // C++ doesn't need this, and isTagName doesn't take SS. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2500 | if (SS == nullptr) { |
| 2501 | const char *TagName = nullptr, *FixitTagName = nullptr; |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2502 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2503 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2504 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2505 | default: break; |
Argyrios Kyrtzidis | 1f32940 | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2506 | case DeclSpec::TST_enum: |
| 2507 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 2508 | case DeclSpec::TST_union: |
| 2509 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 2510 | case DeclSpec::TST_struct: |
| 2511 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2512 | case DeclSpec::TST_interface: |
| 2513 | TagName="__interface"; FixitTagName = "__interface "; |
| 2514 | TagKind=tok::kw___interface;break; |
Argyrios Kyrtzidis | 1f32940 | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2515 | case DeclSpec::TST_class: |
| 2516 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2517 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2518 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2519 | if (TagName) { |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2520 | IdentifierInfo *TokenName = Tok.getIdentifierInfo(); |
| 2521 | LookupResult R(Actions, TokenName, SourceLocation(), |
| 2522 | Sema::LookupOrdinaryName); |
| 2523 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2524 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2525 | << TokenName << TagName << getLangOpts().CPlusPlus |
| 2526 | << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName); |
| 2527 | |
| 2528 | if (Actions.LookupParsedName(R, getCurScope(), SS)) { |
| 2529 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); |
| 2530 | I != IEnd; ++I) |
Kaelyn Uhrain | 3fe3f85 | 2012-04-27 18:26:49 +0000 | [diff] [blame] | 2531 | Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2532 | << TokenName << TagName; |
| 2533 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2534 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2535 | // Parse this as a tag as if the missing tag were present. |
| 2536 | if (TagKind == tok::kw_enum) |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2537 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal); |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2538 | else |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2539 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2540 | /*EnteringContext*/ false, DSC_normal, Attrs); |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2541 | return true; |
| 2542 | } |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2543 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2544 | |
Richard Smith | fe904f0 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2545 | // Determine whether this identifier could plausibly be the name of something |
Richard Smith | edd124e | 2012-05-15 21:42:17 +0000 | [diff] [blame] | 2546 | // being declared (with a missing type). |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 2547 | if (!isTypeSpecifier(DSC) && |
Richard Smith | fe904f0 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2548 | (!SS || DSC == DSC_top_level || DSC == DSC_class)) { |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2549 | // Look ahead to the next token to try to figure out what this declaration |
| 2550 | // was supposed to be. |
| 2551 | switch (NextToken().getKind()) { |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2552 | case tok::l_paren: { |
| 2553 | // static x(4); // 'x' is not a type |
| 2554 | // x(int n); // 'x' is not a type |
| 2555 | // x (*p)[]; // 'x' is a type |
| 2556 | // |
Richard Smith | a0a5d50 | 2014-05-08 22:32:00 +0000 | [diff] [blame] | 2557 | // Since we're in an error case, we can afford to perform a tentative |
| 2558 | // parse to determine which case we're in. |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2559 | TentativeParsingAction PA(*this); |
| 2560 | ConsumeToken(); |
| 2561 | TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false); |
| 2562 | PA.Revert(); |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2563 | |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 2564 | if (TPR != TPResult::False) { |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2565 | // The identifier is followed by a parenthesized declarator. |
| 2566 | // It's supposed to be a type. |
| 2567 | break; |
| 2568 | } |
| 2569 | |
| 2570 | // If we're in a context where we could be declaring a constructor, |
| 2571 | // check whether this is a constructor declaration with a bogus name. |
| 2572 | if (DSC == DSC_class || (DSC == DSC_top_level && SS)) { |
| 2573 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2574 | if (Actions.isCurrentClassNameTypo(II, SS)) { |
| 2575 | Diag(Loc, diag::err_constructor_bad_name) |
| 2576 | << Tok.getIdentifierInfo() << II |
| 2577 | << FixItHint::CreateReplacement(Tok.getLocation(), II->getName()); |
| 2578 | Tok.setIdentifierInfo(II); |
| 2579 | } |
| 2580 | } |
| 2581 | // Fall through. |
Galina Kistanova | 7767425 | 2017-06-01 21:15:34 +0000 | [diff] [blame] | 2582 | LLVM_FALLTHROUGH; |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2583 | } |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2584 | case tok::comma: |
| 2585 | case tok::equal: |
| 2586 | case tok::kw_asm: |
| 2587 | case tok::l_brace: |
| 2588 | case tok::l_square: |
| 2589 | case tok::semi: |
| 2590 | // This looks like a variable or function declaration. The type is |
| 2591 | // probably missing. We're done parsing decl-specifiers. |
| 2592 | if (SS) |
| 2593 | AnnotateScopeToken(*SS, /*IsNewAnnotation*/false); |
| 2594 | return false; |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2595 | |
| 2596 | default: |
| 2597 | // This is probably supposed to be a type. This includes cases like: |
| 2598 | // int f(itn); |
| 2599 | // struct S { unsinged : 4; }; |
| 2600 | break; |
| 2601 | } |
| 2602 | } |
| 2603 | |
Reid Kleckner | c05ca5e | 2014-06-19 01:23:22 +0000 | [diff] [blame] | 2604 | // This is almost certainly an invalid type name. Let Sema emit a diagnostic |
| 2605 | // and attempt to recover. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2606 | ParsedType T; |
Kaelyn Uhrain | b5b17fe | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2607 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Richard Smith | 52f8d19 | 2017-05-10 21:32:16 +0000 | [diff] [blame] | 2608 | bool IsTemplateName = getLangOpts().CPlusPlus && NextToken().is(tok::less); |
Reid Kleckner | c05ca5e | 2014-06-19 01:23:22 +0000 | [diff] [blame] | 2609 | Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T, |
Richard Smith | 52f8d19 | 2017-05-10 21:32:16 +0000 | [diff] [blame] | 2610 | IsTemplateName); |
Reid Kleckner | c05ca5e | 2014-06-19 01:23:22 +0000 | [diff] [blame] | 2611 | if (T) { |
| 2612 | // The action has suggested that the type T could be used. Set that as |
| 2613 | // the type in the declaration specifiers, consume the would-be type |
| 2614 | // name token, and we're done. |
| 2615 | const char *PrevSpec; |
| 2616 | unsigned DiagID; |
| 2617 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T, |
| 2618 | Actions.getASTContext().getPrintingPolicy()); |
| 2619 | DS.SetRangeEnd(Tok.getLocation()); |
| 2620 | ConsumeToken(); |
| 2621 | // There may be other declaration specifiers after this. |
| 2622 | return true; |
| 2623 | } else if (II != Tok.getIdentifierInfo()) { |
| 2624 | // If no type was suggested, the correction is to a keyword |
| 2625 | Tok.setKind(II->getTokenID()); |
| 2626 | // There may be other declaration specifiers after this. |
| 2627 | return true; |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2628 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2629 | |
Reid Kleckner | c05ca5e | 2014-06-19 01:23:22 +0000 | [diff] [blame] | 2630 | // 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] | 2631 | DS.SetTypeSpecError(); |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2632 | DS.SetRangeEnd(Tok.getLocation()); |
| 2633 | ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2634 | |
Richard Smith | 52f8d19 | 2017-05-10 21:32:16 +0000 | [diff] [blame] | 2635 | // Eat any following template arguments. |
| 2636 | if (IsTemplateName) { |
| 2637 | SourceLocation LAngle, RAngle; |
| 2638 | TemplateArgList Args; |
| 2639 | ParseTemplateIdAfterTemplateName(true, LAngle, Args, RAngle); |
| 2640 | } |
| 2641 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2642 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 2643 | // avoid rippling error messages on subsequent uses of the same type, |
| 2644 | // could be useful if #include was forgotten. |
| 2645 | return false; |
| 2646 | } |
| 2647 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2648 | /// \brief Determine the declaration specifier context from the declarator |
| 2649 | /// context. |
| 2650 | /// |
| 2651 | /// \param Context the declarator context, which is one of the |
| 2652 | /// Declarator::TheContext enumerator values. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2653 | Parser::DeclSpecContext |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2654 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 2655 | if (Context == Declarator::MemberContext) |
| 2656 | return DSC_class; |
| 2657 | if (Context == Declarator::FileContext) |
| 2658 | return DSC_top_level; |
Akira Hatanaka | 12ddcee | 2017-06-26 18:46:12 +0000 | [diff] [blame] | 2659 | if (Context == Declarator::TemplateParamContext) |
| 2660 | return DSC_template_param; |
Reid Kleckner | df6e4a0 | 2014-06-06 22:36:36 +0000 | [diff] [blame] | 2661 | if (Context == Declarator::TemplateTypeArgContext) |
| 2662 | return DSC_template_type_arg; |
Richard Smith | 62dad82 | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 2663 | if (Context == Declarator::TrailingReturnContext) |
| 2664 | return DSC_trailing; |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 2665 | if (Context == Declarator::AliasDeclContext || |
| 2666 | Context == Declarator::AliasTemplateContext) |
| 2667 | return DSC_alias_declaration; |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2668 | return DSC_normal; |
| 2669 | } |
| 2670 | |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2671 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 2672 | /// |
| 2673 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 2674 | /// type. Ideally, the type should be propagated directly into Sema. |
| 2675 | /// |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2676 | /// [C11] type-id |
| 2677 | /// [C11] constant-expression |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2678 | /// [C++0x] type-id ...[opt] |
| 2679 | /// [C++0x] assignment-expression ...[opt] |
| 2680 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 2681 | SourceLocation &EllipsisLoc) { |
| 2682 | ExprResult ER; |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2683 | if (isTypeIdInParens()) { |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2684 | SourceLocation TypeLoc = Tok.getLocation(); |
| 2685 | ParsedType Ty = ParseTypeName().get(); |
| 2686 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2687 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 2688 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2689 | } else |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2690 | ER = ParseConstantExpression(); |
| 2691 | |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 2692 | if (getLangOpts().CPlusPlus11) |
| 2693 | TryConsumeToken(tok::ellipsis, EllipsisLoc); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2694 | |
| 2695 | return ER; |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2696 | } |
| 2697 | |
| 2698 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 2699 | /// attribute to Attrs. |
| 2700 | /// |
| 2701 | /// alignment-specifier: |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2702 | /// [C11] '_Alignas' '(' type-id ')' |
| 2703 | /// [C11] '_Alignas' '(' constant-expression ')' |
Richard Smith | d11c7a1 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2704 | /// [C++11] 'alignas' '(' type-id ...[opt] ')' |
| 2705 | /// [C++11] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2706 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2707 | SourceLocation *EndLoc) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2708 | assert(Tok.isOneOf(tok::kw_alignas, tok::kw__Alignas) && |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2709 | "Not an alignment-specifier!"); |
| 2710 | |
Richard Smith | d11c7a1 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2711 | IdentifierInfo *KWName = Tok.getIdentifierInfo(); |
| 2712 | SourceLocation KWLoc = ConsumeToken(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2713 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2714 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 2715 | if (T.expectAndConsume()) |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2716 | return; |
| 2717 | |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2718 | SourceLocation EllipsisLoc; |
| 2719 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2720 | if (ArgExpr.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2721 | T.skipToEnd(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2722 | return; |
| 2723 | } |
| 2724 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2725 | T.consumeClose(); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2726 | if (EndLoc) |
| 2727 | *EndLoc = T.getCloseLocation(); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2728 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2729 | ArgsVector ArgExprs; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2730 | ArgExprs.push_back(ArgExpr.get()); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2731 | Attrs.addNew(KWName, KWLoc, nullptr, KWLoc, ArgExprs.data(), 1, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2732 | AttributeList::AS_Keyword, EllipsisLoc); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2733 | } |
| 2734 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2735 | /// Determine whether we're looking at something that might be a declarator |
| 2736 | /// in a simple-declaration. If it can't possibly be a declarator, maybe |
| 2737 | /// diagnose a missing semicolon after a prior tag definition in the decl |
| 2738 | /// specifier. |
| 2739 | /// |
| 2740 | /// \return \c true if an error occurred and this can't be any kind of |
| 2741 | /// declaration. |
| 2742 | bool |
| 2743 | Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS, |
| 2744 | DeclSpecContext DSContext, |
| 2745 | LateParsedAttrList *LateAttrs) { |
| 2746 | assert(DS.hasTagDefinition() && "shouldn't call this"); |
| 2747 | |
| 2748 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2749 | |
| 2750 | if (getLangOpts().CPlusPlus && |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2751 | Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype, |
| 2752 | tok::annot_template_id) && |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2753 | TryAnnotateCXXScopeToken(EnteringContext)) { |
| 2754 | SkipMalformedDecl(); |
| 2755 | return true; |
| 2756 | } |
| 2757 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2758 | bool HasScope = Tok.is(tok::annot_cxxscope); |
| 2759 | // Make a copy in case GetLookAheadToken invalidates the result of NextToken. |
| 2760 | Token AfterScope = HasScope ? NextToken() : Tok; |
| 2761 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2762 | // Determine whether the following tokens could possibly be a |
| 2763 | // declarator. |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2764 | bool MightBeDeclarator = true; |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2765 | if (Tok.isOneOf(tok::kw_typename, tok::annot_typename)) { |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2766 | // A declarator-id can't start with 'typename'. |
| 2767 | MightBeDeclarator = false; |
| 2768 | } else if (AfterScope.is(tok::annot_template_id)) { |
| 2769 | // If we have a type expressed as a template-id, this cannot be a |
| 2770 | // declarator-id (such a type cannot be redeclared in a simple-declaration). |
| 2771 | TemplateIdAnnotation *Annot = |
| 2772 | static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue()); |
| 2773 | if (Annot->Kind == TNK_Type_template) |
| 2774 | MightBeDeclarator = false; |
| 2775 | } else if (AfterScope.is(tok::identifier)) { |
| 2776 | const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken(); |
| 2777 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2778 | // These tokens cannot come after the declarator-id in a |
| 2779 | // simple-declaration, and are likely to come after a type-specifier. |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2780 | if (Next.isOneOf(tok::star, tok::amp, tok::ampamp, tok::identifier, |
| 2781 | tok::annot_cxxscope, tok::coloncolon)) { |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2782 | // Missing a semicolon. |
| 2783 | MightBeDeclarator = false; |
| 2784 | } else if (HasScope) { |
| 2785 | // If the declarator-id has a scope specifier, it must redeclare a |
| 2786 | // previously-declared entity. If that's a type (and this is not a |
| 2787 | // typedef), that's an error. |
| 2788 | CXXScopeSpec SS; |
| 2789 | Actions.RestoreNestedNameSpecifierAnnotation( |
| 2790 | Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS); |
| 2791 | IdentifierInfo *Name = AfterScope.getIdentifierInfo(); |
| 2792 | Sema::NameClassification Classification = Actions.ClassifyName( |
| 2793 | getCurScope(), SS, Name, AfterScope.getLocation(), Next, |
| 2794 | /*IsAddressOfOperand*/false); |
| 2795 | switch (Classification.getKind()) { |
| 2796 | case Sema::NC_Error: |
| 2797 | SkipMalformedDecl(); |
| 2798 | return true; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2799 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2800 | case Sema::NC_Keyword: |
| 2801 | case Sema::NC_NestedNameSpecifier: |
| 2802 | llvm_unreachable("typo correction and nested name specifiers not " |
| 2803 | "possible here"); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2804 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2805 | case Sema::NC_Type: |
| 2806 | case Sema::NC_TypeTemplate: |
| 2807 | // Not a previously-declared non-type entity. |
| 2808 | MightBeDeclarator = false; |
| 2809 | break; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2810 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2811 | case Sema::NC_Unknown: |
| 2812 | case Sema::NC_Expression: |
| 2813 | case Sema::NC_VarTemplate: |
| 2814 | case Sema::NC_FunctionTemplate: |
| 2815 | // Might be a redeclaration of a prior entity. |
| 2816 | break; |
| 2817 | } |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2818 | } |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2819 | } |
| 2820 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2821 | if (MightBeDeclarator) |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2822 | return false; |
| 2823 | |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2824 | const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy(); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2825 | Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()), |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 2826 | diag::err_expected_after) |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2827 | << DeclSpec::getSpecifierName(DS.getTypeSpecType(), PPol) << tok::semi; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2828 | |
| 2829 | // Try to recover from the typo, by dropping the tag definition and parsing |
| 2830 | // the problematic tokens as a type. |
| 2831 | // |
| 2832 | // FIXME: Split the DeclSpec into pieces for the standalone |
| 2833 | // declaration and pieces for the following declaration, instead |
| 2834 | // of assuming that all the other pieces attach to new declaration, |
| 2835 | // and call ParsedFreeStandingDeclSpec as appropriate. |
| 2836 | DS.ClearTypeSpecType(); |
| 2837 | ParsedTemplateInfo NotATemplate; |
| 2838 | ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs); |
| 2839 | return false; |
| 2840 | } |
| 2841 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2842 | /// ParseDeclarationSpecifiers |
| 2843 | /// declaration-specifiers: [C99 6.7] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2844 | /// storage-class-specifier declaration-specifiers[opt] |
| 2845 | /// type-specifier declaration-specifiers[opt] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2846 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2847 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 2848 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2849 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 2850 | /// [ObjC1] '__kindof' declaration-specifiers[opt] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2851 | /// |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2852 | /// storage-class-specifier: [C99 6.7.1] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 2853 | /// 'typedef' |
| 2854 | /// 'extern' |
| 2855 | /// 'static' |
| 2856 | /// 'auto' |
| 2857 | /// 'register' |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2858 | /// [C++] 'mutable' |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2859 | /// [C++11] 'thread_local' |
| 2860 | /// [C11] '_Thread_local' |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 2861 | /// [GNU] '__thread' |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2862 | /// function-specifier: [C99 6.7.4] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2863 | /// [C99] 'inline' |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2864 | /// [C++] 'virtual' |
| 2865 | /// [C++] 'explicit' |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2866 | /// [OpenCL] '__kernel' |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2867 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 39c2a8b | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2868 | /// 'constexpr': [C++0x dcl.constexpr] |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 2869 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2870 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2871 | AccessSpecifier AS, |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2872 | DeclSpecContext DSContext, |
| 2873 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2874 | if (DS.getSourceRange().isInvalid()) { |
David Majnemer | 24b2830 | 2014-07-26 05:41:31 +0000 | [diff] [blame] | 2875 | // Start the range at the current token but make the end of the range |
| 2876 | // invalid. This will make the entire range invalid unless we successfully |
| 2877 | // consume a token. |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2878 | DS.SetRangeStart(Tok.getLocation()); |
David Majnemer | 24b2830 | 2014-07-26 05:41:31 +0000 | [diff] [blame] | 2879 | DS.SetRangeEnd(SourceLocation()); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2880 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2881 | |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2882 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2883 | bool AttrsLastTime = false; |
| 2884 | ParsedAttributesWithRange attrs(AttrFactory); |
Benjamin Kramer | e481214 | 2015-03-12 14:28:38 +0000 | [diff] [blame] | 2885 | // We use Sema's policy to get bool macros right. |
Richard Smith | 301bc21 | 2016-05-19 01:39:10 +0000 | [diff] [blame] | 2886 | PrintingPolicy Policy = Actions.getPrintingPolicy(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2887 | while (1) { |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2888 | bool isInvalid = false; |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 2889 | bool isStorageClass = false; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2890 | const char *PrevSpec = nullptr; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2891 | unsigned DiagID = 0; |
| 2892 | |
David Majnemer | 51fd8a0 | 2015-07-22 23:46:18 +0000 | [diff] [blame] | 2893 | // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL |
| 2894 | // implementation for VS2013 uses _Atomic as an identifier for one of the |
| 2895 | // classes in <atomic>. |
| 2896 | // |
| 2897 | // A typedef declaration containing _Atomic<...> is among the places where |
| 2898 | // the class is used. If we are currently parsing such a declaration, treat |
| 2899 | // the token as an identifier. |
| 2900 | if (getLangOpts().MSVCCompat && Tok.is(tok::kw__Atomic) && |
| 2901 | DS.getStorageClassSpec() == clang::DeclSpec::SCS_typedef && |
| 2902 | !DS.hasTypeSpecifier() && GetLookAheadToken(1).is(tok::less)) |
| 2903 | Tok.setKind(tok::identifier); |
| 2904 | |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 2905 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2906 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2907 | switch (Tok.getKind()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2908 | default: |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2909 | DoneWithDeclSpec: |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2910 | if (!AttrsLastTime) |
| 2911 | ProhibitAttributes(attrs); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2912 | else { |
| 2913 | // Reject C++11 attributes that appertain to decl specifiers as |
| 2914 | // we don't support any C++11 attributes that appertain to decl |
| 2915 | // specifiers. This also conforms to what g++ 4.8 is doing. |
Richard Smith | 49cc1cc | 2016-08-18 21:59:42 +0000 | [diff] [blame] | 2916 | ProhibitCXX11Attributes(attrs, diag::err_attribute_not_type_attr); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2917 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2918 | DS.takeAttributesFrom(attrs); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2919 | } |
Peter Collingbourne | 70188b3 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 2920 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2921 | // If this is not a declaration specifier token, we're done reading decl |
| 2922 | // specifiers. First verify that DeclSpec's are consistent. |
Craig Topper | 2512241 | 2015-11-15 03:32:11 +0000 | [diff] [blame] | 2923 | DS.Finish(Actions, Policy); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2924 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2925 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2926 | case tok::l_square: |
| 2927 | case tok::kw_alignas: |
Richard Smith | 4cabd04 | 2013-02-22 09:15:49 +0000 | [diff] [blame] | 2928 | if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier()) |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2929 | goto DoneWithDeclSpec; |
| 2930 | |
| 2931 | ProhibitAttributes(attrs); |
| 2932 | // FIXME: It would be good to recover by accepting the attributes, |
| 2933 | // but attempting to do that now would cause serious |
| 2934 | // madness in terms of diagnostics. |
| 2935 | attrs.clear(); |
| 2936 | attrs.Range = SourceRange(); |
| 2937 | |
| 2938 | ParseCXX11Attributes(attrs); |
| 2939 | AttrsLastTime = true; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2940 | continue; |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2941 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2942 | case tok::code_completion: { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2943 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2944 | if (DS.hasTypeSpecifier()) { |
| 2945 | bool AllowNonIdentifiers |
| 2946 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 2947 | Scope::BlockScope | |
| 2948 | Scope::TemplateParamScope | |
| 2949 | Scope::FunctionPrototypeScope | |
| 2950 | Scope::AtCatchScope)) == 0; |
| 2951 | bool AllowNestedNameSpecifiers |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2952 | = DSContext == DSC_top_level || |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2953 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 2954 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2955 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2956 | AllowNonIdentifiers, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2957 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2958 | return cutOffParsing(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2961 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 2962 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 2963 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2964 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2965 | : Sema::PCC_Template; |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2966 | else if (DSContext == DSC_class) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2967 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | b6c6a58 | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 2968 | else if (CurParsedObjCImpl) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2969 | CCC = Sema::PCC_ObjCImplementation; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2970 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2971 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2972 | return cutOffParsing(); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2973 | } |
| 2974 | |
Chris Lattner | bd31aa3 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2975 | case tok::coloncolon: // ::foo::bar |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2976 | // C++ scope specifier. Annotate and loop, or bail out on error. |
Eli Friedman | 2a1d9a9 | 2013-08-15 23:59:20 +0000 | [diff] [blame] | 2977 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2978 | if (!DS.hasTypeSpecifier()) |
| 2979 | DS.SetTypeSpecError(); |
| 2980 | goto DoneWithDeclSpec; |
| 2981 | } |
John McCall | 8bc2a70 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 2982 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 2983 | goto DoneWithDeclSpec; |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2984 | continue; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2985 | |
| 2986 | case tok::annot_cxxscope: { |
Richard Smith | 3092a3b | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2987 | if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector()) |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2988 | goto DoneWithDeclSpec; |
| 2989 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2990 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 2991 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 2992 | Tok.getAnnotationRange(), |
| 2993 | SS); |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2994 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2995 | // We are looking for a qualified typename. |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2996 | Token Next = NextToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2997 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2998 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2999 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 3000 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3001 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3002 | // If this would be a valid constructor declaration with template |
| 3003 | // arguments, we will reject the attempt to form an invalid type-id |
| 3004 | // referring to the injected-class-name when we annotate the token, |
| 3005 | // per C++ [class.qual]p2. |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3006 | // |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3007 | // To improve diagnostics for this case, parse the declaration as a |
| 3008 | // constructor (and reject the extra template arguments later). |
Argyrios Kyrtzidis | c0c5dd2 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 3009 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
Dmitri Gribenko | d1c91f1 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 3010 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 3011 | TemplateId->Name && |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3012 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS) && |
| 3013 | isConstructorDeclarator(/*Unqualified*/false)) { |
| 3014 | // The user meant this to be an out-of-line constructor |
| 3015 | // definition, but template arguments are not allowed |
| 3016 | // there. Just allow this as a constructor; we'll |
| 3017 | // complain about it later. |
| 3018 | goto DoneWithDeclSpec; |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 3021 | DS.getTypeSpecScope() = SS; |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3022 | ConsumeAnnotationToken(); // The C++ scope. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3023 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 3024 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3025 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 3026 | continue; |
| 3027 | } |
| 3028 | |
Douglas Gregor | c5790df | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 3029 | if (Next.is(tok::annot_typename)) { |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 3030 | DS.getTypeSpecScope() = SS; |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3031 | ConsumeAnnotationToken(); // The C++ scope. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3032 | if (Tok.getAnnotationValue()) { |
| 3033 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 7743034 | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 3034 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3035 | Tok.getAnnotationEndLoc(), |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3036 | PrevSpec, DiagID, T, Policy); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 3037 | if (isInvalid) |
| 3038 | break; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3039 | } |
Douglas Gregor | c5790df | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 3040 | else |
| 3041 | DS.SetTypeSpecError(); |
| 3042 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3043 | ConsumeAnnotationToken(); // The typename |
Douglas Gregor | c5790df | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 3046 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3047 | goto DoneWithDeclSpec; |
| 3048 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3049 | // Check whether this is a constructor declaration. If we're in a |
| 3050 | // context where the identifier could be a class name, and it has the |
| 3051 | // shape of a constructor declaration, process it as one. |
Dmitri Gribenko | d1c91f1 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 3052 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3053 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3054 | &SS) && |
| 3055 | isConstructorDeclarator(/*Unqualified*/ false)) |
| 3056 | goto DoneWithDeclSpec; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3057 | |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 3058 | ParsedType TypeRep = |
| 3059 | Actions.getTypeName(*Next.getIdentifierInfo(), Next.getLocation(), |
| 3060 | getCurScope(), &SS, false, false, nullptr, |
| 3061 | /*IsCtorOrDtorName=*/false, |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 3062 | /*WantNonTrivialSourceInfo=*/true, |
| 3063 | isClassTemplateDeductionContext(DSContext)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3064 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 3065 | // If the referenced identifier is not a type, then this declspec is |
| 3066 | // erroneous: We already checked about that it has no type specifier, and |
| 3067 | // 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] | 3068 | // typename. |
David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 3069 | if (!TypeRep) { |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3070 | // Eat the scope spec so the identifier is current. |
| 3071 | ConsumeAnnotationToken(); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3072 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 3073 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) { |
| 3074 | if (!Attrs.empty()) { |
| 3075 | AttrsLastTime = true; |
| 3076 | attrs.takeAllFrom(Attrs); |
| 3077 | } |
| 3078 | continue; |
| 3079 | } |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3080 | goto DoneWithDeclSpec; |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 3081 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3082 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 3083 | DS.getTypeSpecScope() = SS; |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3084 | ConsumeAnnotationToken(); // The C++ scope. |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3085 | |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3086 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3087 | DiagID, TypeRep, Policy); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3088 | if (isInvalid) |
| 3089 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3090 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3091 | DS.SetRangeEnd(Tok.getLocation()); |
| 3092 | ConsumeToken(); // The typename. |
| 3093 | |
| 3094 | continue; |
| 3095 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3096 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3097 | case tok::annot_typename: { |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 3098 | // If we've previously seen a tag definition, we were almost surely |
| 3099 | // missing a semicolon after it. |
| 3100 | if (DS.hasTypeSpecifier() && DS.hasTagDefinition()) |
| 3101 | goto DoneWithDeclSpec; |
| 3102 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3103 | if (Tok.getAnnotationValue()) { |
| 3104 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 7f8bb36 | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 3105 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3106 | DiagID, T, Policy); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3107 | } else |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 3108 | DS.SetTypeSpecError(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3109 | |
Chris Lattner | 005fc1b | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 3110 | if (isInvalid) |
| 3111 | break; |
| 3112 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3113 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3114 | ConsumeAnnotationToken(); // The typename |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3115 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3116 | continue; |
| 3117 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3118 | |
Douglas Gregor | 0687309 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 3119 | case tok::kw___is_signed: |
| 3120 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 3121 | // typically treats it as a trait. If we see __is_signed as it appears |
| 3122 | // in libstdc++, e.g., |
| 3123 | // |
| 3124 | // static const bool __is_signed; |
| 3125 | // |
| 3126 | // then treat __is_signed as an identifier rather than as a keyword. |
| 3127 | if (DS.getTypeSpecType() == TST_bool && |
| 3128 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
Alp Toker | 47642d2 | 2013-12-03 06:13:01 +0000 | [diff] [blame] | 3129 | DS.getStorageClassSpec() == DeclSpec::SCS_static) |
| 3130 | TryKeywordIdentFallback(true); |
Douglas Gregor | 0687309 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 3131 | |
| 3132 | // We're done with the declaration-specifiers. |
| 3133 | goto DoneWithDeclSpec; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3134 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3135 | // typedef-name |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 3136 | case tok::kw___super: |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3137 | case tok::kw_decltype: |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3138 | case tok::identifier: { |
Reid Kleckner | c582f01 | 2014-07-14 18:19:58 +0000 | [diff] [blame] | 3139 | // This identifier can only be a typedef name if we haven't already seen |
| 3140 | // a type-specifier. Without this check we misparse: |
| 3141 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 3142 | if (DS.hasTypeSpecifier()) |
| 3143 | goto DoneWithDeclSpec; |
| 3144 | |
Aaron Ballman | 52d0aaa | 2017-02-14 22:47:20 +0000 | [diff] [blame] | 3145 | // If the token is an identifier named "__declspec" and Microsoft |
| 3146 | // extensions are not enabled, it is likely that there will be cascading |
| 3147 | // parse errors if this really is a __declspec attribute. Attempt to |
| 3148 | // recognize that scenario and recover gracefully. |
| 3149 | if (!getLangOpts().DeclSpecKeyword && Tok.is(tok::identifier) && |
| 3150 | Tok.getIdentifierInfo()->getName().equals("__declspec")) { |
| 3151 | Diag(Loc, diag::err_ms_attributes_not_enabled); |
| 3152 | |
| 3153 | // The next token should be an open paren. If it is, eat the entire |
| 3154 | // attribute declaration and continue. |
| 3155 | if (NextToken().is(tok::l_paren)) { |
| 3156 | // Consume the __declspec identifier. |
Richard Trieu | ba05737 | 2017-02-14 23:56:55 +0000 | [diff] [blame] | 3157 | ConsumeToken(); |
Aaron Ballman | 52d0aaa | 2017-02-14 22:47:20 +0000 | [diff] [blame] | 3158 | |
| 3159 | // Eat the parens and everything between them. |
| 3160 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 3161 | if (T.consumeOpen()) { |
| 3162 | assert(false && "Not a left paren?"); |
| 3163 | return; |
| 3164 | } |
| 3165 | T.skipToEnd(); |
| 3166 | continue; |
| 3167 | } |
| 3168 | } |
| 3169 | |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 3170 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 3171 | // so handle it as such. This is important for ctor parsing. |
| 3172 | if (getLangOpts().CPlusPlus) { |
| 3173 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
| 3174 | DS.SetTypeSpecError(); |
| 3175 | goto DoneWithDeclSpec; |
| 3176 | } |
| 3177 | if (!Tok.is(tok::identifier)) |
| 3178 | continue; |
| 3179 | } |
| 3180 | |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3181 | // Check for need to substitute AltiVec keyword tokens. |
| 3182 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 3183 | break; |
| 3184 | |
Richard Smith | 3092a3b | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 3185 | // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not |
| 3186 | // allow the use of a typedef name as a type specifier. |
| 3187 | if (DS.isTypeAltiVecVector()) |
| 3188 | goto DoneWithDeclSpec; |
| 3189 | |
Douglas Gregor | 5c0870a | 2015-06-19 23:18:00 +0000 | [diff] [blame] | 3190 | if (DSContext == DSC_objc_method_result && isObjCInstancetype()) { |
| 3191 | ParsedType TypeRep = Actions.ActOnObjCInstanceType(Loc); |
| 3192 | assert(TypeRep); |
| 3193 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
| 3194 | DiagID, TypeRep, Policy); |
| 3195 | if (isInvalid) |
| 3196 | break; |
| 3197 | |
| 3198 | DS.SetRangeEnd(Loc); |
| 3199 | ConsumeToken(); |
| 3200 | continue; |
| 3201 | } |
| 3202 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 3203 | ParsedType TypeRep = Actions.getTypeName( |
| 3204 | *Tok.getIdentifierInfo(), Tok.getLocation(), getCurScope(), nullptr, |
| 3205 | false, false, nullptr, false, false, |
| 3206 | isClassTemplateDeductionContext(DSContext)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3207 | |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 3208 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 3209 | // it must be an implicit int or an error. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3210 | if (!TypeRep) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3211 | ParsedAttributesWithRange Attrs(AttrFactory); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 3212 | if (ParseImplicitInt(DS, nullptr, TemplateInfo, AS, DSContext, Attrs)) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3213 | if (!Attrs.empty()) { |
| 3214 | AttrsLastTime = true; |
| 3215 | attrs.takeAllFrom(Attrs); |
| 3216 | } |
| 3217 | continue; |
| 3218 | } |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3219 | goto DoneWithDeclSpec; |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 3220 | } |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3221 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3222 | // If we're in a context where the identifier could be a class name, |
| 3223 | // check whether this is a constructor declaration. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3224 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3225 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 3226 | isConstructorDeclarator(/*Unqualified*/true)) |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3227 | goto DoneWithDeclSpec; |
| 3228 | |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 3229 | // Likewise, if this is a context where the identifier could be a template |
| 3230 | // name, check whether this is a deduction guide declaration. |
| 3231 | if (getLangOpts().CPlusPlus1z && |
| 3232 | (DSContext == DSC_class || DSContext == DSC_top_level) && |
| 3233 | Actions.isDeductionGuideName(getCurScope(), *Tok.getIdentifierInfo(), |
| 3234 | Tok.getLocation()) && |
| 3235 | isConstructorDeclarator(/*Unqualified*/ true, |
| 3236 | /*DeductionGuide*/ true)) |
| 3237 | goto DoneWithDeclSpec; |
| 3238 | |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3239 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3240 | DiagID, TypeRep, Policy); |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3241 | if (isInvalid) |
| 3242 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3243 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3244 | DS.SetRangeEnd(Tok.getLocation()); |
| 3245 | ConsumeToken(); // The identifier |
| 3246 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3247 | // Objective-C supports type arguments and protocol references |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 3248 | // following an Objective-C object or object pointer |
| 3249 | // type. Handle either one of them. |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3250 | if (Tok.is(tok::less) && getLangOpts().ObjC1) { |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 3251 | SourceLocation NewEndLoc; |
| 3252 | TypeResult NewTypeRep = parseObjCTypeArgsAndProtocolQualifiers( |
| 3253 | Loc, TypeRep, /*consumeLastToken=*/true, |
| 3254 | NewEndLoc); |
| 3255 | if (NewTypeRep.isUsable()) { |
| 3256 | DS.UpdateTypeRep(NewTypeRep.get()); |
| 3257 | DS.SetRangeEnd(NewEndLoc); |
| 3258 | } |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3259 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3260 | |
Steve Naroff | cd5e782 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 3261 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 3262 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 3263 | continue; |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3264 | } |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3265 | |
| 3266 | // type-name |
| 3267 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | c0c5dd2 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 3268 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3269 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3270 | // This template-id does not refer to a type name, so we're |
| 3271 | // done with the type-specifiers. |
| 3272 | goto DoneWithDeclSpec; |
| 3273 | } |
| 3274 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3275 | // If we're in a context where the template-id could be a |
| 3276 | // constructor name or specialization, check whether this is a |
| 3277 | // constructor declaration. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3278 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3279 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 3280 | isConstructorDeclarator(TemplateId->SS.isEmpty())) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3281 | goto DoneWithDeclSpec; |
| 3282 | |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3283 | // Turn the template-id annotation token into a type annotation |
| 3284 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 3285 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3286 | continue; |
| 3287 | } |
| 3288 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 3289 | // GNU attributes support. |
| 3290 | case tok::kw___attribute: |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 3291 | ParseGNUAttributes(DS.getAttributes(), nullptr, LateAttrs); |
Chris Lattner | b95cca0 | 2006-10-17 03:01:08 +0000 | [diff] [blame] | 3292 | continue; |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 3293 | |
| 3294 | // Microsoft declspec support. |
| 3295 | case tok::kw___declspec: |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 3296 | ParseMicrosoftDeclSpecs(DS.getAttributes()); |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 3297 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3298 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3299 | // Microsoft single token adornments. |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 3300 | case tok::kw___forceinline: { |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 3301 | isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID); |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 3302 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 3303 | SourceLocation AttrNameLoc = Tok.getLocation(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 3304 | DS.getAttributes().addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, |
| 3305 | nullptr, 0, AttributeList::AS_Keyword); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 3306 | break; |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 3307 | } |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3308 | |
Andrey Bokhanko | 45d4132 | 2016-05-11 18:38:21 +0000 | [diff] [blame] | 3309 | case tok::kw___unaligned: |
| 3310 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID, |
| 3311 | getLangOpts()); |
| 3312 | break; |
| 3313 | |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 3314 | case tok::kw___sptr: |
| 3315 | case tok::kw___uptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3316 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3317 | case tok::kw___ptr32: |
Steve Naroff | f9c29d4 | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 3318 | case tok::kw___w64: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3319 | case tok::kw___cdecl: |
| 3320 | case tok::kw___stdcall: |
| 3321 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3322 | case tok::kw___thiscall: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3323 | case tok::kw___regcall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 3324 | case tok::kw___vectorcall: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3325 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3326 | continue; |
| 3327 | |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3328 | // Borland single token adornments. |
| 3329 | case tok::kw___pascal: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3330 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3331 | continue; |
| 3332 | |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3333 | // OpenCL single token adornments. |
| 3334 | case tok::kw___kernel: |
Anastasia Stulova | 6bdbcbb | 2016-02-19 18:30:11 +0000 | [diff] [blame] | 3335 | ParseOpenCLKernelAttributes(DS.getAttributes()); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3336 | continue; |
| 3337 | |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 3338 | // Nullability type specifiers. |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 3339 | case tok::kw__Nonnull: |
| 3340 | case tok::kw__Nullable: |
| 3341 | case tok::kw__Null_unspecified: |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 3342 | ParseNullabilityTypeSpecifiers(DS.getAttributes()); |
| 3343 | continue; |
| 3344 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 3345 | // Objective-C 'kindof' types. |
| 3346 | case tok::kw___kindof: |
| 3347 | DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc, |
| 3348 | nullptr, 0, AttributeList::AS_Keyword); |
| 3349 | (void)ConsumeToken(); |
| 3350 | continue; |
| 3351 | |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3352 | // storage-class-specifier |
| 3353 | case tok::kw_typedef: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3354 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3355 | PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3356 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3357 | break; |
| 3358 | case tok::kw_extern: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3359 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3360 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3361 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3362 | PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3363 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3364 | break; |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 3365 | case tok::kw___private_extern__: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3366 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3367 | Loc, PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3368 | isStorageClass = true; |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 3369 | break; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3370 | case tok::kw_static: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3371 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3372 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3373 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3374 | PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3375 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3376 | break; |
| 3377 | case tok::kw_auto: |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3378 | if (getLangOpts().CPlusPlus11) { |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3379 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3380 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3381 | PrevSpec, DiagID, Policy); |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3382 | if (!isInvalid) |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 3383 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3384 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 3385 | } else |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3386 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3387 | DiagID, Policy); |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 3388 | } else |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3389 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3390 | PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3391 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3392 | break; |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 3393 | case tok::kw___auto_type: |
| 3394 | Diag(Tok, diag::ext_auto_type); |
| 3395 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto_type, Loc, PrevSpec, |
| 3396 | DiagID, Policy); |
| 3397 | break; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3398 | case tok::kw_register: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3399 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3400 | PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3401 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3402 | break; |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 3403 | case tok::kw_mutable: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3404 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, 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; |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 3407 | break; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3408 | case tok::kw___thread: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3409 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc, |
| 3410 | PrevSpec, DiagID); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3411 | isStorageClass = true; |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3412 | break; |
| 3413 | case tok::kw_thread_local: |
| 3414 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc, |
| 3415 | PrevSpec, DiagID); |
| 3416 | break; |
| 3417 | case tok::kw__Thread_local: |
| 3418 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local, |
| 3419 | Loc, PrevSpec, DiagID); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3420 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3421 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3422 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3423 | // function-specifier |
| 3424 | case tok::kw_inline: |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 3425 | isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3426 | break; |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3427 | case tok::kw_virtual: |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 3428 | isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3429 | break; |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3430 | case tok::kw_explicit: |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 3431 | isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3432 | break; |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 3433 | case tok::kw__Noreturn: |
| 3434 | if (!getLangOpts().C11) |
| 3435 | Diag(Loc, diag::ext_c11_noreturn); |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 3436 | isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID); |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 3437 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3438 | |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 3439 | // alignment-specifier |
| 3440 | case tok::kw__Alignas: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3441 | if (!getLangOpts().C11) |
Jordan Rose | 58d5472 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 3442 | Diag(Tok, diag::ext_c11_alignment) << Tok.getName(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 3443 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 3444 | continue; |
| 3445 | |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 3446 | // friend |
| 3447 | case tok::kw_friend: |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3448 | if (DSContext == DSC_class) |
| 3449 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 3450 | else { |
| 3451 | PrevSpec = ""; // not actually used by the diagnostic |
| 3452 | DiagID = diag::err_friend_invalid_in_context; |
| 3453 | isInvalid = true; |
| 3454 | } |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 3455 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3456 | |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 3457 | // Modules |
| 3458 | case tok::kw___module_private__: |
| 3459 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 3460 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3461 | |
Sebastian Redl | 39c2a8b | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 3462 | // constexpr |
| 3463 | case tok::kw_constexpr: |
| 3464 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 3465 | break; |
| 3466 | |
Hubert Tong | 375f00a | 2015-06-30 12:14:52 +0000 | [diff] [blame] | 3467 | // concept |
| 3468 | case tok::kw_concept: |
| 3469 | isInvalid = DS.SetConceptSpec(Loc, PrevSpec, DiagID); |
| 3470 | break; |
| 3471 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3472 | // type-specifier |
| 3473 | case tok::kw_short: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3474 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3475 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3476 | break; |
| 3477 | case tok::kw_long: |
| 3478 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3479 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3480 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3481 | else |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3482 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3483 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3484 | break; |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3485 | case tok::kw___int64: |
| 3486 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3487 | DiagID, Policy); |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3488 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3489 | case tok::kw_signed: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3490 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 3491 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3492 | break; |
| 3493 | case tok::kw_unsigned: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3494 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 3495 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3496 | break; |
| 3497 | case tok::kw__Complex: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3498 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 3499 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3500 | break; |
| 3501 | case tok::kw__Imaginary: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3502 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 3503 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3504 | break; |
| 3505 | case tok::kw_void: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3506 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3507 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3508 | break; |
| 3509 | case tok::kw_char: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3510 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3511 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3512 | break; |
| 3513 | case tok::kw_int: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3514 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3515 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3516 | break; |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3517 | case tok::kw___int128: |
| 3518 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3519 | DiagID, Policy); |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3520 | break; |
| 3521 | case tok::kw_half: |
| 3522 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3523 | DiagID, Policy); |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3524 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3525 | case tok::kw_float: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3526 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, 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 | break; |
| 3529 | case tok::kw_double: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3530 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3531 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3532 | break; |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 3533 | case tok::kw__Float16: |
| 3534 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float16, Loc, PrevSpec, |
| 3535 | DiagID, Policy); |
| 3536 | break; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 3537 | case tok::kw___float128: |
| 3538 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float128, Loc, PrevSpec, |
| 3539 | DiagID, Policy); |
| 3540 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3541 | case tok::kw_wchar_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3542 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3543 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3544 | break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3545 | case tok::kw_char16_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3546 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3547 | DiagID, Policy); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3548 | break; |
| 3549 | case tok::kw_char32_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3550 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3551 | DiagID, Policy); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3552 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3553 | case tok::kw_bool: |
| 3554 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 20ee5ae | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3555 | if (Tok.is(tok::kw_bool) && |
| 3556 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 3557 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 3558 | PrevSpec = ""; // Not used by the diagnostic. |
| 3559 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | 2b05999 | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3560 | // For better error recovery. |
| 3561 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 20ee5ae | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3562 | isInvalid = true; |
| 3563 | } else { |
| 3564 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3565 | DiagID, Policy); |
Argyrios Kyrtzidis | 20ee5ae | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3566 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3567 | break; |
| 3568 | case tok::kw__Decimal32: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3569 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3570 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3571 | break; |
| 3572 | case tok::kw__Decimal64: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3573 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, 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__Decimal128: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3577 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, 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; |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3580 | case tok::kw___vector: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3581 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy); |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3582 | break; |
| 3583 | case tok::kw___pixel: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3584 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy); |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3585 | break; |
Bill Seurer | cf2c96b | 2015-01-12 19:35:51 +0000 | [diff] [blame] | 3586 | case tok::kw___bool: |
| 3587 | isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy); |
| 3588 | break; |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3589 | case tok::kw_pipe: |
| 3590 | if (!getLangOpts().OpenCL || (getLangOpts().OpenCLVersion < 200)) { |
| 3591 | // OpenCL 2.0 defined this keyword. OpenCL 1.2 and earlier should |
| 3592 | // support the "pipe" word as identifier. |
| 3593 | Tok.getIdentifierInfo()->revertTokenIDToIdentifier(); |
| 3594 | goto DoneWithDeclSpec; |
| 3595 | } |
| 3596 | isInvalid = DS.SetTypePipe(true, Loc, PrevSpec, DiagID, Policy); |
| 3597 | break; |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 3598 | #define GENERIC_IMAGE_TYPE(ImgType, Id) \ |
| 3599 | case tok::kw_##ImgType##_t: \ |
| 3600 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_##ImgType##_t, Loc, PrevSpec, \ |
| 3601 | DiagID, Policy); \ |
| 3602 | break; |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 3603 | #include "clang/Basic/OpenCLImageTypes.def" |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3604 | case tok::kw___unknown_anytype: |
| 3605 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3606 | PrevSpec, DiagID, Policy); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3607 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3608 | |
| 3609 | // class-specifier: |
| 3610 | case tok::kw_class: |
| 3611 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3612 | case tok::kw___interface: |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3613 | case tok::kw_union: { |
| 3614 | tok::TokenKind Kind = Tok.getKind(); |
| 3615 | ConsumeToken(); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3616 | |
| 3617 | // These are attributes following class specifiers. |
| 3618 | // To produce better diagnostic, we parse them when |
| 3619 | // parsing class specifier. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3620 | ParsedAttributesWithRange Attributes(AttrFactory); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3621 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3622 | EnteringContext, DSContext, Attributes); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3623 | |
| 3624 | // If there are attributes following class specifier, |
| 3625 | // take them over and handle them here. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3626 | if (!Attributes.empty()) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3627 | AttrsLastTime = true; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3628 | attrs.takeAllFrom(Attributes); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3629 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3630 | continue; |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3631 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3632 | |
| 3633 | // enum-specifier: |
| 3634 | case tok::kw_enum: |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3635 | ConsumeToken(); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3636 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3637 | continue; |
| 3638 | |
| 3639 | // cv-qualifier: |
| 3640 | case tok::kw_const: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3641 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3642 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3643 | break; |
| 3644 | case tok::kw_volatile: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3645 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3646 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3647 | break; |
| 3648 | case tok::kw_restrict: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3649 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3650 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3651 | break; |
| 3652 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3653 | // C++ typename-specifier: |
| 3654 | case tok::kw_typename: |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3655 | if (TryAnnotateTypeOrScopeToken()) { |
| 3656 | DS.SetTypeSpecError(); |
| 3657 | goto DoneWithDeclSpec; |
| 3658 | } |
| 3659 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3660 | continue; |
| 3661 | break; |
| 3662 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3663 | // GNU typeof support. |
| 3664 | case tok::kw_typeof: |
| 3665 | ParseTypeofSpecifier(DS); |
| 3666 | continue; |
| 3667 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3668 | case tok::annot_decltype: |
Anders Carlsson | 74948d0 | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 3669 | ParseDecltypeSpecifier(DS); |
| 3670 | continue; |
| 3671 | |
David Majnemer | 15b311c | 2016-06-14 03:20:28 +0000 | [diff] [blame] | 3672 | case tok::annot_pragma_pack: |
| 3673 | HandlePragmaPack(); |
| 3674 | continue; |
| 3675 | |
| 3676 | case tok::annot_pragma_ms_pragma: |
| 3677 | HandlePragmaMSPragma(); |
| 3678 | continue; |
| 3679 | |
| 3680 | case tok::annot_pragma_ms_vtordisp: |
| 3681 | HandlePragmaMSVtorDisp(); |
| 3682 | continue; |
| 3683 | |
| 3684 | case tok::annot_pragma_ms_pointers_to_members: |
| 3685 | HandlePragmaMSPointersToMembers(); |
| 3686 | continue; |
| 3687 | |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3688 | case tok::kw___underlying_type: |
| 3689 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3690 | continue; |
| 3691 | |
| 3692 | case tok::kw__Atomic: |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3693 | // C11 6.7.2.4/4: |
| 3694 | // If the _Atomic keyword is immediately followed by a left parenthesis, |
| 3695 | // it is interpreted as a type specifier (with a type name), not as a |
| 3696 | // type qualifier. |
| 3697 | if (NextToken().is(tok::l_paren)) { |
| 3698 | ParseAtomicSpecifier(DS); |
| 3699 | continue; |
| 3700 | } |
| 3701 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 3702 | getLangOpts()); |
| 3703 | break; |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3704 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3705 | // OpenCL qualifiers: |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3706 | case tok::kw___generic: |
| 3707 | // generic address space is introduced only in OpenCL v2.0 |
| 3708 | // see OpenCL C Spec v2.0 s6.5.5 |
| 3709 | if (Actions.getLangOpts().OpenCLVersion < 200) { |
| 3710 | DiagID = diag::err_opencl_unknown_type_specifier; |
| 3711 | PrevSpec = Tok.getIdentifierInfo()->getNameStart(); |
| 3712 | isInvalid = true; |
| 3713 | break; |
| 3714 | }; |
Galina Kistanova | 7767425 | 2017-06-01 21:15:34 +0000 | [diff] [blame] | 3715 | LLVM_FALLTHROUGH; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3716 | case tok::kw___private: |
| 3717 | case tok::kw___global: |
| 3718 | case tok::kw___local: |
| 3719 | case tok::kw___constant: |
| 3720 | case tok::kw___read_only: |
| 3721 | case tok::kw___write_only: |
| 3722 | case tok::kw___read_write: |
Aaron Ballman | 05d76ea | 2014-01-14 01:29:54 +0000 | [diff] [blame] | 3723 | ParseOpenCLQualifiers(DS.getAttributes()); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3724 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3725 | |
Steve Naroff | cfdf616 | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 3726 | case tok::less: |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3727 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3728 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 3729 | // but we support it. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3730 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3731 | goto DoneWithDeclSpec; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3732 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 3733 | SourceLocation StartLoc = Tok.getLocation(); |
| 3734 | SourceLocation EndLoc; |
| 3735 | TypeResult Type = parseObjCProtocolQualifierType(EndLoc); |
| 3736 | if (Type.isUsable()) { |
| 3737 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, StartLoc, |
| 3738 | PrevSpec, DiagID, Type.get(), |
| 3739 | Actions.getASTContext().getPrintingPolicy())) |
| 3740 | Diag(StartLoc, DiagID) << PrevSpec; |
| 3741 | |
| 3742 | DS.SetRangeEnd(EndLoc); |
| 3743 | } else { |
| 3744 | DS.SetTypeSpecError(); |
| 3745 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3746 | |
Douglas Gregor | 06e41ae | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 3747 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 3748 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 3749 | continue; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 3750 | } |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3751 | // If the specifier wasn't legal, issue a diagnostic. |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3752 | if (isInvalid) { |
| 3753 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3754 | assert(DiagID); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3755 | |
Douglas Gregor | a05f5ab | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 3756 | if (DiagID == diag::ext_duplicate_declspec) |
| 3757 | Diag(Tok, DiagID) |
| 3758 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
Anastasia Stulova | 43ab9a0 | 2016-05-12 16:28:25 +0000 | [diff] [blame] | 3759 | else if (DiagID == diag::err_opencl_unknown_type_specifier) { |
| 3760 | const int OpenCLVer = getLangOpts().OpenCLVersion; |
| 3761 | std::string VerSpec = llvm::to_string(OpenCLVer / 100) + |
| 3762 | std::string (".") + |
| 3763 | llvm::to_string((OpenCLVer % 100) / 10); |
| 3764 | Diag(Tok, DiagID) << VerSpec << PrevSpec << isStorageClass; |
| 3765 | } else |
Douglas Gregor | a05f5ab | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 3766 | Diag(Tok, DiagID) << PrevSpec; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3767 | } |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3768 | |
Chris Lattner | 2e23209 | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 3769 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | 2b05999 | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3770 | if (DiagID != diag::err_bool_redeclaration) |
| 3771 | ConsumeToken(); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3772 | |
| 3773 | AttrsLastTime = false; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 3774 | } |
| 3775 | } |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 3776 | |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3777 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 3778 | /// semicolon. |
| 3779 | /// |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3780 | /// struct-declaration: |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3781 | /// specifier-qualifier-list struct-declarator-list |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 3782 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3783 | /// [GNU] specifier-qualifier-list |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3784 | /// struct-declarator-list: |
| 3785 | /// struct-declarator |
| 3786 | /// struct-declarator-list ',' struct-declarator |
| 3787 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 3788 | /// struct-declarator: |
| 3789 | /// declarator |
| 3790 | /// [GNU] declarator attributes[opt] |
| 3791 | /// declarator[opt] ':' constant-expression |
| 3792 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 3793 | /// |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 3794 | void Parser::ParseStructDeclaration( |
| 3795 | ParsingDeclSpec &DS, |
| 3796 | llvm::function_ref<void(ParsingFieldDeclarator &)> FieldsCallback) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3797 | |
Chris Lattner | f02ef3e | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3798 | if (Tok.is(tok::kw___extension__)) { |
| 3799 | // __extension__ silences extension warnings in the subexpression. |
| 3800 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3801 | ConsumeToken(); |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 3802 | return ParseStructDeclaration(DS, FieldsCallback); |
Chris Lattner | f02ef3e | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3803 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3804 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3805 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3806 | ParseSpecifierQualifierList(DS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3807 | |
Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3808 | // If there are no declarators, this is a free-standing declaration |
| 3809 | // specifier. Let the actions module cope with it. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3810 | if (Tok.is(tok::semi)) { |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 3811 | RecordDecl *AnonRecord = nullptr; |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3812 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 3813 | DS, AnonRecord); |
| 3814 | assert(!AnonRecord && "Did not expect anonymous struct or union here"); |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3815 | DS.complete(TheDecl); |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3816 | return; |
| 3817 | } |
| 3818 | |
| 3819 | // Read struct-declarators until we find the semicolon. |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3820 | bool FirstDeclarator = true; |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3821 | SourceLocation CommaLoc; |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3822 | while (1) { |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3823 | ParsingFieldDeclarator DeclaratorInfo(*this, DS); |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3824 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3825 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3826 | // Attributes are only allowed here on successive declarators. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3827 | if (!FirstDeclarator) |
| 3828 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3829 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3830 | /// struct-declarator: declarator |
| 3831 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | 17c3b1f | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3832 | if (Tok.isNot(tok::colon)) { |
| 3833 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 3834 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3835 | ParseDeclarator(DeclaratorInfo.D); |
Richard Smith | 3d1a94c | 2014-08-12 00:22:39 +0000 | [diff] [blame] | 3836 | } else |
| 3837 | DeclaratorInfo.D.SetIdentifier(nullptr, Tok.getLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3838 | |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 3839 | if (TryConsumeToken(tok::colon)) { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3840 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3841 | if (Res.isInvalid()) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3842 | SkipUntil(tok::semi, StopBeforeMatch); |
Chris Lattner | 32295d3 | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 3843 | else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3844 | DeclaratorInfo.BitfieldSize = Res.get(); |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3845 | } |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3846 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3847 | // If attributes exist after the declarator, parse them. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3848 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3849 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3850 | // We're done with this declarator; invoke the callback. |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 3851 | FieldsCallback(DeclaratorInfo); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3852 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3853 | // If we don't have a comma, it is either the end of the list (a ';') |
| 3854 | // or an error, bail out. |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 3855 | if (!TryConsumeToken(tok::comma, CommaLoc)) |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3856 | return; |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3857 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3858 | FirstDeclarator = false; |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3859 | } |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3860 | } |
| 3861 | |
| 3862 | /// ParseStructUnionBody |
| 3863 | /// struct-contents: |
| 3864 | /// struct-declaration-list |
| 3865 | /// [EXT] empty |
| 3866 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 3867 | /// struct-declaration-list: |
| 3868 | /// struct-declaration |
| 3869 | /// struct-declaration-list struct-declaration |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3870 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3871 | /// |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 3872 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3873 | unsigned TagType, Decl *TagDecl) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3874 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 3875 | "parsing struct/union body"); |
Andy Gibbs | 22e140b | 2013-04-03 09:31:19 +0000 | [diff] [blame] | 3876 | assert(!getLangOpts().CPlusPlus && "C++ declarations not supported"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3877 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3878 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3879 | if (T.consumeOpen()) |
| 3880 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3881 | |
Douglas Gregor | 658b955 | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 3882 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3883 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3884 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3885 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3886 | |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 3887 | // 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] | 3888 | while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) && |
| 3889 | Tok.isNot(tok::eof)) { |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3890 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3891 | |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 3892 | // Check for extraneous top-level semicolon. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3893 | if (Tok.is(tok::semi)) { |
Richard Smith | 87f5dc5 | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3894 | ConsumeExtraSemi(InsideStruct, TagType); |
Chris Lattner | 36e46a2 | 2007-06-09 05:49:55 +0000 | [diff] [blame] | 3895 | continue; |
| 3896 | } |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3897 | |
Andy Gibbs | c804e08 | 2013-04-03 09:46:04 +0000 | [diff] [blame] | 3898 | // Parse _Static_assert declaration. |
| 3899 | if (Tok.is(tok::kw__Static_assert)) { |
| 3900 | SourceLocation DeclEnd; |
| 3901 | ParseStaticAssertDeclaration(DeclEnd); |
| 3902 | continue; |
| 3903 | } |
| 3904 | |
Argyrios Kyrtzidis | 71c12fb | 2013-04-18 01:42:35 +0000 | [diff] [blame] | 3905 | if (Tok.is(tok::annot_pragma_pack)) { |
| 3906 | HandlePragmaPack(); |
| 3907 | continue; |
| 3908 | } |
| 3909 | |
| 3910 | if (Tok.is(tok::annot_pragma_align)) { |
| 3911 | HandlePragmaAlign(); |
| 3912 | continue; |
| 3913 | } |
| 3914 | |
Alexey Bataev | 4652e4b | 2015-08-12 07:10:54 +0000 | [diff] [blame] | 3915 | if (Tok.is(tok::annot_pragma_openmp)) { |
Alexey Bataev | 587e1de | 2016-03-30 10:43:55 +0000 | [diff] [blame] | 3916 | // Result can be ignored, because it must be always empty. |
| 3917 | AccessSpecifier AS = AS_none; |
| 3918 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 3919 | (void)ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs); |
Alexey Bataev | 4652e4b | 2015-08-12 07:10:54 +0000 | [diff] [blame] | 3920 | continue; |
| 3921 | } |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 3922 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3923 | if (!Tok.is(tok::at)) { |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 3924 | auto CFieldCallback = [&](ParsingFieldDeclarator &FD) { |
| 3925 | // Install the declarator into the current TagDecl. |
| 3926 | Decl *Field = |
| 3927 | Actions.ActOnField(getCurScope(), TagDecl, |
| 3928 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 3929 | FD.D, FD.BitfieldSize); |
| 3930 | FieldDecls.push_back(Field); |
| 3931 | FD.complete(Field); |
| 3932 | }; |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3933 | |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3934 | // Parse all the comma separated declarators. |
| 3935 | ParsingDeclSpec DS(*this); |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 3936 | ParseStructDeclaration(DS, CFieldCallback); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3937 | } else { // Handle @defs |
| 3938 | ConsumeToken(); |
| 3939 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 3940 | Diag(Tok, diag::err_unexpected_at); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3941 | SkipUntil(tok::semi); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3942 | continue; |
| 3943 | } |
| 3944 | ConsumeToken(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 3945 | ExpectAndConsume(tok::l_paren); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3946 | if (!Tok.is(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 3947 | Diag(Tok, diag::err_expected) << tok::identifier; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3948 | SkipUntil(tok::semi); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3949 | continue; |
| 3950 | } |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3951 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3952 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3953 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3954 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 3955 | ConsumeToken(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 3956 | ExpectAndConsume(tok::r_paren); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3957 | } |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 3958 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 3959 | if (TryConsumeToken(tok::semi)) |
| 3960 | continue; |
| 3961 | |
| 3962 | if (Tok.is(tok::r_brace)) { |
Chris Lattner | 245c533 | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3963 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Chris Lattner | 0c7e82d | 2007-06-09 05:54:40 +0000 | [diff] [blame] | 3964 | break; |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3965 | } |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 3966 | |
| 3967 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 3968 | // Skip to end of block or statement to avoid ext-warning on extra ';'. |
| 3969 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
| 3970 | // If we stopped at a ';', eat it. |
| 3971 | TryConsumeToken(tok::semi); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3972 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3973 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3974 | T.consumeClose(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3975 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3976 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3977 | // If attributes exist after struct contents, parse them. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3978 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 15619c7 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 3979 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3980 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 751c558 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 3981 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3982 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3983 | attrs.getList()); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3984 | StructScope.Exit(); |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 3985 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, T.getRange()); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3986 | } |
| 3987 | |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3988 | /// ParseEnumSpecifier |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 3989 | /// enum-specifier: [C99 6.7.2.2] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3990 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3991 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 3992 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3993 | /// '}' attributes[opt] |
Aaron Ballman | 9ecff02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 3994 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3995 | /// '}' |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3996 | /// 'enum' identifier |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 3997 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3998 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3999 | /// [C++11] enum-head '{' enumerator-list[opt] '}' |
| 4000 | /// [C++11] enum-head '{' enumerator-list ',' '}' |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4001 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4002 | /// enum-head: [C++11] |
| 4003 | /// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt] |
| 4004 | /// enum-key attribute-specifier-seq[opt] nested-name-specifier |
| 4005 | /// identifier enum-base[opt] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4006 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4007 | /// enum-key: [C++11] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4008 | /// 'enum' |
| 4009 | /// 'enum' 'class' |
| 4010 | /// 'enum' 'struct' |
| 4011 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4012 | /// enum-base: [C++11] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4013 | /// ':' type-specifier-seq |
| 4014 | /// |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4015 | /// [C++] elaborated-type-specifier: |
| 4016 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 4017 | /// |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 4018 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | dc70c3a | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 4019 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 4020 | AccessSpecifier AS, DeclSpecContext DSC) { |
Chris Lattner | ffbc271 | 2007-01-25 06:05:38 +0000 | [diff] [blame] | 4021 | // Parse the tag portion of this. |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4022 | if (Tok.is(tok::code_completion)) { |
| 4023 | // Code completion for an enum name. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4024 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 4025 | return cutOffParsing(); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 4026 | } |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 4027 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 4028 | // If attributes exist after tag, parse them. |
| 4029 | ParsedAttributesWithRange attrs(AttrFactory); |
| 4030 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4031 | MaybeParseCXX11Attributes(attrs); |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 4032 | MaybeParseMicrosoftDeclSpecs(attrs); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 4033 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4034 | SourceLocation ScopedEnumKWLoc; |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 4035 | bool IsScopedUsingClassTag = false; |
| 4036 | |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 4037 | // In C++11, recognize 'enum class' and 'enum struct'. |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 4038 | if (Tok.isOneOf(tok::kw_class, tok::kw_struct)) { |
Richard Trieu | d0d87b5 | 2013-04-23 02:47:36 +0000 | [diff] [blame] | 4039 | Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum |
| 4040 | : diag::ext_scoped_enum); |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 4041 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4042 | ScopedEnumKWLoc = ConsumeToken(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4043 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4044 | // Attributes are not allowed between these keywords. Diagnose, |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 4045 | // but then just treat them like they appeared in the right place. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 4046 | ProhibitAttributes(attrs); |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 4047 | |
| 4048 | // They are allowed afterwards, though. |
| 4049 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4050 | MaybeParseCXX11Attributes(attrs); |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 4051 | MaybeParseMicrosoftDeclSpecs(attrs); |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 4052 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4053 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4054 | // C++11 [temp.explicit]p12: |
| 4055 | // The usual access controls do not apply to names used to specify |
| 4056 | // explicit instantiations. |
| 4057 | // We extend this to also cover explicit specializations. Note that |
| 4058 | // we don't suppress if this turns out to be an elaborated type |
| 4059 | // specifier. |
| 4060 | bool shouldDelayDiagsInTag = |
| 4061 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 4062 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 4063 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4064 | |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 4065 | // Enum definitions should not be parsed in a trailing-return-type. |
| 4066 | bool AllowDeclaration = DSC != DSC_trailing; |
| 4067 | |
| 4068 | bool AllowFixedUnderlyingType = AllowDeclaration && |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4069 | (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt || |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 4070 | getLangOpts().ObjC2); |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 4071 | |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 4072 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4073 | if (getLangOpts().CPlusPlus) { |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 4074 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 4075 | // if a fixed underlying type is allowed. |
| 4076 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4077 | |
Nico Weber | cfaa4cd | 2015-02-15 07:26:13 +0000 | [diff] [blame] | 4078 | CXXScopeSpec Spec; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 4079 | if (ParseOptionalCXXScopeSpecifier(Spec, nullptr, |
Richard Smith | 1d4b2e1 | 2013-04-01 21:43:41 +0000 | [diff] [blame] | 4080 | /*EnteringContext=*/true)) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4081 | return; |
| 4082 | |
Nico Weber | cfaa4cd | 2015-02-15 07:26:13 +0000 | [diff] [blame] | 4083 | if (Spec.isSet() && Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 4084 | Diag(Tok, diag::err_expected) << tok::identifier; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4085 | if (Tok.isNot(tok::l_brace)) { |
| 4086 | // Has no name and is not a definition. |
| 4087 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4088 | SkipUntil(tok::comma, StopAtSemi); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4089 | return; |
| 4090 | } |
| 4091 | } |
Nico Weber | cfaa4cd | 2015-02-15 07:26:13 +0000 | [diff] [blame] | 4092 | |
| 4093 | SS = Spec; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4094 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4095 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4096 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 4097 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 4098 | !(AllowFixedUnderlyingType && Tok.is(tok::colon))) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 4099 | Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4100 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4101 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4102 | SkipUntil(tok::comma, StopAtSemi); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 4103 | return; |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4104 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4105 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4106 | // If an identifier is present, consume and remember it. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 4107 | IdentifierInfo *Name = nullptr; |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4108 | SourceLocation NameLoc; |
| 4109 | if (Tok.is(tok::identifier)) { |
| 4110 | Name = Tok.getIdentifierInfo(); |
| 4111 | NameLoc = ConsumeToken(); |
| 4112 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4113 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4114 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4115 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 4116 | // declaration of a scoped enumeration. |
| 4117 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4118 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 4119 | IsScopedUsingClassTag = false; |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4120 | } |
| 4121 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4122 | // Okay, end the suppression area. We'll decide whether to emit the |
| 4123 | // diagnostics in a second. |
| 4124 | if (shouldDelayDiagsInTag) |
| 4125 | diagsFromTag.done(); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4126 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4127 | TypeResult BaseType; |
| 4128 | |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4129 | // Parse the fixed underlying type. |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 4130 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 4131 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4132 | bool PossibleBitfield = false; |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 4133 | if (CanBeBitfield) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4134 | // If we're in class scope, this can either be an enum declaration with |
| 4135 | // an underlying type, or a declaration of a bitfield member. We try to |
| 4136 | // use a simple disambiguation scheme first to catch the common cases |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4137 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 4138 | // anything that's a simple-type-specifier followed by '(' as an |
| 4139 | // expression. This suffices because function types are not valid |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4140 | // underlying types anyway. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4141 | EnterExpressionEvaluationContext Unevaluated( |
| 4142 | Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4143 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4144 | // 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] | 4145 | // bit-field. This is the common case. |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 4146 | if (TPR == TPResult::True) |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4147 | PossibleBitfield = true; |
| 4148 | // If the next token starts a type-specifier-seq, it may be either a |
| 4149 | // 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] | 4150 | // 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] | 4151 | // fixed underlying type. |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 4152 | else if (TPR == TPResult::False && |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4153 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 4154 | // Consume the ':'. |
| 4155 | ConsumeToken(); |
| 4156 | } else { |
| 4157 | // We have the start of a type-specifier-seq, so we have to perform |
| 4158 | // tentative parsing to determine whether we have an expression or a |
| 4159 | // type. |
| 4160 | TentativeParsingAction TPA(*this); |
| 4161 | |
| 4162 | // Consume the ':'. |
| 4163 | ConsumeToken(); |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 4164 | |
| 4165 | // If we see a type specifier followed by an open-brace, we have an |
| 4166 | // ambiguity between an underlying type and a C++11 braced |
| 4167 | // function-style cast. Resolve this by always treating it as an |
| 4168 | // underlying type. |
| 4169 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 4170 | // this case. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4171 | if ((getLangOpts().CPlusPlus && |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 4172 | isCXXDeclarationSpecifier(TPResult::True) != TPResult::True) || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4173 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4174 | // We'll parse this as a bitfield later. |
| 4175 | PossibleBitfield = true; |
| 4176 | TPA.Revert(); |
| 4177 | } else { |
| 4178 | // We have a type-specifier-seq. |
| 4179 | TPA.Commit(); |
| 4180 | } |
| 4181 | } |
| 4182 | } else { |
| 4183 | // Consume the ':'. |
| 4184 | ConsumeToken(); |
| 4185 | } |
| 4186 | |
| 4187 | if (!PossibleBitfield) { |
| 4188 | SourceRange Range; |
| 4189 | BaseType = ParseTypeName(&Range); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4190 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4191 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4192 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Eli Friedman | 0d0355ab | 2012-11-02 01:34:28 +0000 | [diff] [blame] | 4193 | } else if (!getLangOpts().ObjC2) { |
| 4194 | if (getLangOpts().CPlusPlus) |
| 4195 | Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range; |
| 4196 | else |
| 4197 | Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range; |
| 4198 | } |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4199 | } |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4200 | } |
| 4201 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4202 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 4203 | // friend declaration, and cannot have an accompanying definition. If we have |
| 4204 | // 'enum foo;', then this is a forward declaration. If we have |
| 4205 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 4206 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4207 | // |
| 4208 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 4209 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 4210 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 4211 | // |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4212 | Sema::TagUseKind TUK; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4213 | if (!AllowDeclaration) { |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 4214 | TUK = Sema::TUK_Reference; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4215 | } else if (Tok.is(tok::l_brace)) { |
| 4216 | if (DS.isFriendSpecified()) { |
| 4217 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
| 4218 | << SourceRange(DS.getFriendSpecLoc()); |
| 4219 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4220 | SkipUntil(tok::r_brace, StopAtSemi); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4221 | TUK = Sema::TUK_Friend; |
| 4222 | } else { |
| 4223 | TUK = Sema::TUK_Definition; |
| 4224 | } |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 4225 | } else if (!isTypeSpecifier(DSC) && |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 4226 | (Tok.is(tok::semi) || |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 4227 | (Tok.isAtStartOfLine() && |
| 4228 | !isValidAfterTypeSpecifier(CanBeBitfield)))) { |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 4229 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
| 4230 | if (Tok.isNot(tok::semi)) { |
| 4231 | // A semicolon was missing after this declaration. Diagnose and recover. |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 4232 | ExpectAndConsume(tok::semi, diag::err_expected_after, "enum"); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 4233 | PP.EnterToken(Tok); |
| 4234 | Tok.setKind(tok::semi); |
| 4235 | } |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4236 | } else { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4237 | TUK = Sema::TUK_Reference; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4238 | } |
| 4239 | |
| 4240 | // If this is an elaborated type specifier, and we delayed |
| 4241 | // diagnostics before, just merge them into the current pool. |
| 4242 | if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) { |
| 4243 | diagsFromTag.redelay(); |
| 4244 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4245 | |
| 4246 | MultiTemplateParamsArg TParams; |
Douglas Gregor | cbbf3e3 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 4247 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4248 | TUK != Sema::TUK_Reference) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4249 | if (!getLangOpts().CPlusPlus11 || !SS.isSet()) { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4250 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 4251 | Diag(Tok, diag::err_enum_template); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4252 | SkipUntil(tok::comma, StopAtSemi); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4253 | return; |
| 4254 | } |
| 4255 | |
| 4256 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 4257 | // Enumerations can't be explicitly instantiated. |
| 4258 | DS.SetTypeSpecError(); |
| 4259 | Diag(StartLoc, diag::err_explicit_instantiation_enum); |
| 4260 | return; |
| 4261 | } |
| 4262 | |
| 4263 | assert(TemplateInfo.TemplateParams && "no template parameters"); |
| 4264 | TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(), |
| 4265 | TemplateInfo.TemplateParams->size()); |
Douglas Gregor | cbbf3e3 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 4266 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4267 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 4268 | if (TUK == Sema::TUK_Reference) |
| 4269 | ProhibitAttributes(attrs); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4270 | |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 4271 | if (!Name && TUK != Sema::TUK_Definition) { |
| 4272 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4273 | |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 4274 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4275 | SkipUntil(tok::comma, StopAtSemi); |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 4276 | return; |
| 4277 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4278 | |
Nico Weber | 32a0fc7 | 2016-09-03 03:01:32 +0000 | [diff] [blame] | 4279 | stripTypeAttributesOffDeclSpec(attrs, DS, TUK); |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 4280 | |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 4281 | Sema::SkipBodyInfo SkipBody; |
| 4282 | if (!Name && TUK == Sema::TUK_Definition && Tok.is(tok::l_brace) && |
| 4283 | NextToken().is(tok::identifier)) |
| 4284 | SkipBody = Actions.shouldSkipAnonEnumBody(getCurScope(), |
| 4285 | NextToken().getIdentifierInfo(), |
| 4286 | NextToken().getLocation()); |
| 4287 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 4288 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4289 | bool IsDependent = false; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 4290 | const char *PrevSpec = nullptr; |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4291 | unsigned DiagID; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4292 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4293 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4294 | AS, DS.getModulePrivateSpecLoc(), TParams, |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4295 | Owned, IsDependent, ScopedEnumKWLoc, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 4296 | IsScopedUsingClassTag, BaseType, |
Akira Hatanaka | 12ddcee | 2017-06-26 18:46:12 +0000 | [diff] [blame] | 4297 | DSC == DSC_type_specifier, |
| 4298 | DSC == DSC_template_param || |
| 4299 | DSC == DSC_template_type_arg, &SkipBody); |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 4300 | |
| 4301 | if (SkipBody.ShouldSkip) { |
| 4302 | assert(TUK == Sema::TUK_Definition && "can only skip a definition"); |
| 4303 | |
| 4304 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 4305 | T.consumeOpen(); |
| 4306 | T.skipToEnd(); |
| 4307 | |
| 4308 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 4309 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 4310 | PrevSpec, DiagID, TagDecl, Owned, |
| 4311 | Actions.getASTContext().getPrintingPolicy())) |
| 4312 | Diag(StartLoc, DiagID) << PrevSpec; |
| 4313 | return; |
| 4314 | } |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4315 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4316 | if (IsDependent) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4317 | // This enum has a dependent nested-name-specifier. Handle it as a |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4318 | // dependent tag. |
| 4319 | if (!Name) { |
| 4320 | DS.SetTypeSpecError(); |
| 4321 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 4322 | return; |
| 4323 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4324 | |
Nico Weber | 83ea012 | 2014-05-03 21:57:40 +0000 | [diff] [blame] | 4325 | TypeResult Type = Actions.ActOnDependentTag( |
| 4326 | getCurScope(), DeclSpec::TST_enum, TUK, SS, Name, StartLoc, NameLoc); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4327 | if (Type.isInvalid()) { |
| 4328 | DS.SetTypeSpecError(); |
| 4329 | return; |
| 4330 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4331 | |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 4332 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 4333 | NameLoc.isValid() ? NameLoc : StartLoc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 4334 | PrevSpec, DiagID, Type.get(), |
| 4335 | Actions.getASTContext().getPrintingPolicy())) |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4336 | Diag(StartLoc, DiagID) << PrevSpec; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4337 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4338 | return; |
| 4339 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4340 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4341 | if (!TagDecl) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4342 | // The action failed to produce an enumeration tag. If this is a |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4343 | // definition, consume the entire definition. |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 4344 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4345 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4346 | SkipUntil(tok::r_brace, StopAtSemi); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4347 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4348 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4349 | DS.SetTypeSpecError(); |
| 4350 | return; |
| 4351 | } |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4352 | |
Bruno Cardoso Lopes | df0ee34 | 2017-07-01 00:06:47 +0000 | [diff] [blame] | 4353 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
| 4354 | Decl *D = SkipBody.CheckSameAsPrevious ? SkipBody.New : TagDecl; |
| 4355 | ParseEnumBody(StartLoc, D); |
| 4356 | if (SkipBody.CheckSameAsPrevious && |
| 4357 | !Actions.ActOnDuplicateDefinition(DS, TagDecl, SkipBody)) { |
| 4358 | DS.SetTypeSpecError(); |
| 4359 | return; |
| 4360 | } |
| 4361 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4362 | |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 4363 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 4364 | NameLoc.isValid() ? NameLoc : StartLoc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 4365 | PrevSpec, DiagID, TagDecl, Owned, |
| 4366 | Actions.getASTContext().getPrintingPolicy())) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4367 | Diag(StartLoc, DiagID) << PrevSpec; |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 4368 | } |
| 4369 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4370 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 4371 | /// enumerator-list: |
| 4372 | /// enumerator |
| 4373 | /// enumerator-list ',' enumerator |
| 4374 | /// enumerator: |
Aaron Ballman | 730476b | 2014-11-08 15:33:35 +0000 | [diff] [blame] | 4375 | /// enumeration-constant attributes[opt] |
| 4376 | /// enumeration-constant attributes[opt] '=' constant-expression |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4377 | /// enumeration-constant: |
| 4378 | /// identifier |
| 4379 | /// |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4380 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4381 | // Enter the scope of the enum body and start the definition. |
Hans Wennborg | fe78145 | 2014-06-17 00:00:18 +0000 | [diff] [blame] | 4382 | ParseScope EnumScope(this, Scope::DeclScope | Scope::EnumScope); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4383 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4384 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4385 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 4386 | T.consumeOpen(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4387 | |
Chris Lattner | 37256fb | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 4388 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4389 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 4390 | Diag(Tok, diag::err_empty_enum); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4391 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4392 | SmallVector<Decl *, 32> EnumConstantDecls; |
Jordan Rose | 60ac316 | 2015-04-30 17:20:30 +0000 | [diff] [blame] | 4393 | SmallVector<SuppressAccessChecks, 32> EnumAvailabilityDiags; |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4394 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 4395 | Decl *LastEnumConstDecl = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4396 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4397 | // Parse the enumerator-list. |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4398 | while (Tok.isNot(tok::r_brace)) { |
| 4399 | // Parse enumerator. If failed, try skipping till the start of the next |
| 4400 | // enumerator definition. |
| 4401 | if (Tok.isNot(tok::identifier)) { |
| 4402 | Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; |
| 4403 | if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) && |
| 4404 | TryConsumeToken(tok::comma)) |
| 4405 | continue; |
| 4406 | break; |
| 4407 | } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4408 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 4409 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4410 | |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 4411 | // If attributes exist after the enumerator, parse them. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 4412 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4413 | MaybeParseGNUAttributes(attrs); |
Aaron Ballman | 730476b | 2014-11-08 15:33:35 +0000 | [diff] [blame] | 4414 | ProhibitAttributes(attrs); // GNU-style attributes are prohibited. |
| 4415 | if (getLangOpts().CPlusPlus11 && isCXX11AttributeSpecifier()) { |
| 4416 | if (!getLangOpts().CPlusPlus1z) |
| 4417 | Diag(Tok.getLocation(), diag::warn_cxx14_compat_attribute) |
Aaron Ballman | c0ae7df | 2014-11-08 17:07:15 +0000 | [diff] [blame] | 4418 | << 1 /*enumerator*/; |
Aaron Ballman | 730476b | 2014-11-08 15:33:35 +0000 | [diff] [blame] | 4419 | ParseCXX11Attributes(attrs); |
| 4420 | } |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 4421 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4422 | SourceLocation EqualLoc; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4423 | ExprResult AssignedVal; |
Jordan Rose | 60ac316 | 2015-04-30 17:20:30 +0000 | [diff] [blame] | 4424 | EnumAvailabilityDiags.emplace_back(*this); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4425 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 4426 | if (TryConsumeToken(tok::equal, EqualLoc)) { |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 4427 | AssignedVal = ParseConstantExpression(); |
| 4428 | if (AssignedVal.isInvalid()) |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4429 | SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch); |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4430 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4431 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4432 | // Install the enumerator constant into EnumDecl. |
Bruno Cardoso Lopes | df0ee34 | 2017-07-01 00:06:47 +0000 | [diff] [blame] | 4433 | Decl *EnumConstDecl = Actions.ActOnEnumConstant( |
| 4434 | getCurScope(), EnumDecl, LastEnumConstDecl, IdentLoc, Ident, |
| 4435 | attrs.getList(), EqualLoc, AssignedVal.get()); |
Jordan Rose | 60ac316 | 2015-04-30 17:20:30 +0000 | [diff] [blame] | 4436 | EnumAvailabilityDiags.back().done(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4437 | |
Chris Lattner | 4ef4001 | 2007-06-11 01:28:17 +0000 | [diff] [blame] | 4438 | EnumConstantDecls.push_back(EnumConstDecl); |
| 4439 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4440 | |
Douglas Gregor | ce66d02 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 4441 | if (Tok.is(tok::identifier)) { |
| 4442 | // We're missing a comma between enumerators. |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 4443 | SourceLocation Loc = getEndOfPreviousToken(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4444 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
Douglas Gregor | ce66d02 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 4445 | << FixItHint::CreateInsertion(Loc, ", "); |
| 4446 | continue; |
| 4447 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4448 | |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4449 | // Emumerator definition must be finished, only comma or r_brace are |
| 4450 | // allowed here. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 4451 | SourceLocation CommaLoc; |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4452 | if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) { |
| 4453 | if (EqualLoc.isValid()) |
| 4454 | Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace |
| 4455 | << tok::comma; |
| 4456 | else |
| 4457 | Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator); |
| 4458 | if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) { |
| 4459 | if (TryConsumeToken(tok::comma, CommaLoc)) |
| 4460 | continue; |
| 4461 | } else { |
| 4462 | break; |
| 4463 | } |
| 4464 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4465 | |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4466 | // If comma is followed by r_brace, emit appropriate warning. |
| 4467 | if (Tok.is(tok::r_brace) && CommaLoc.isValid()) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4468 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11) |
Richard Smith | 87f5dc5 | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 4469 | Diag(CommaLoc, getLangOpts().CPlusPlus ? |
| 4470 | diag::ext_enumerator_list_comma_cxx : |
| 4471 | diag::ext_enumerator_list_comma_c) |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4472 | << FixItHint::CreateRemoval(CommaLoc); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4473 | else if (getLangOpts().CPlusPlus11) |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4474 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 4475 | << FixItHint::CreateRemoval(CommaLoc); |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4476 | break; |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4477 | } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4478 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4479 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4480 | // Eat the }. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4481 | T.consumeClose(); |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4482 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4483 | // If attributes exist after the identifier list, parse them. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4484 | ParsedAttributes attrs(AttrFactory); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4485 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 4486 | |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 4487 | Actions.ActOnEnumBody(StartLoc, T.getRange(), |
Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 4488 | EnumDecl, EnumConstantDecls, |
| 4489 | getCurScope(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4490 | attrs.getList()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4491 | |
Jordan Rose | 60ac316 | 2015-04-30 17:20:30 +0000 | [diff] [blame] | 4492 | // Now handle enum constant availability diagnostics. |
| 4493 | assert(EnumConstantDecls.size() == EnumAvailabilityDiags.size()); |
| 4494 | for (size_t i = 0, e = EnumConstantDecls.size(); i != e; ++i) { |
| 4495 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
| 4496 | EnumAvailabilityDiags[i].redelay(); |
| 4497 | PD.complete(EnumConstantDecls[i]); |
| 4498 | } |
| 4499 | |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 4500 | EnumScope.Exit(); |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 4501 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, T.getRange()); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 4502 | |
| 4503 | // The next token must be valid after an enum definition. If not, a ';' |
| 4504 | // was probably forgotten. |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 4505 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
| 4506 | if (!isValidAfterTypeSpecifier(CanBeBitfield)) { |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 4507 | ExpectAndConsume(tok::semi, diag::err_expected_after, "enum"); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 4508 | // Push this token back into the preprocessor and change our current token |
| 4509 | // to ';' so that the rest of the code recovers as though there were an |
| 4510 | // ';' after the definition. |
| 4511 | PP.EnterToken(Tok); |
| 4512 | Tok.setKind(tok::semi); |
| 4513 | } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4514 | } |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 4515 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4516 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 4517 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 4518 | /// specifier or if we're not sure. |
| 4519 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 4520 | switch (Tok.getKind()) { |
| 4521 | default: return false; |
| 4522 | // type-specifiers |
| 4523 | case tok::kw_short: |
| 4524 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4525 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4526 | case tok::kw___int128: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4527 | case tok::kw_signed: |
| 4528 | case tok::kw_unsigned: |
| 4529 | case tok::kw__Complex: |
| 4530 | case tok::kw__Imaginary: |
| 4531 | case tok::kw_void: |
| 4532 | case tok::kw_char: |
| 4533 | case tok::kw_wchar_t: |
| 4534 | case tok::kw_char16_t: |
| 4535 | case tok::kw_char32_t: |
| 4536 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4537 | case tok::kw_half: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4538 | case tok::kw_float: |
| 4539 | case tok::kw_double: |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 4540 | case tok::kw__Float16: |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 4541 | case tok::kw___float128: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4542 | case tok::kw_bool: |
| 4543 | case tok::kw__Bool: |
| 4544 | case tok::kw__Decimal32: |
| 4545 | case tok::kw__Decimal64: |
| 4546 | case tok::kw__Decimal128: |
| 4547 | case tok::kw___vector: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 4548 | #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 4549 | #include "clang/Basic/OpenCLImageTypes.def" |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4550 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4551 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4552 | case tok::kw_class: |
| 4553 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4554 | case tok::kw___interface: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4555 | case tok::kw_union: |
| 4556 | // enum-specifier |
| 4557 | case tok::kw_enum: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4558 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4559 | // typedef-name |
| 4560 | case tok::annot_typename: |
| 4561 | return true; |
| 4562 | } |
| 4563 | } |
| 4564 | |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 4565 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4566 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4567 | bool Parser::isTypeSpecifierQualifier() { |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4568 | switch (Tok.getKind()) { |
| 4569 | default: return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4570 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4571 | case tok::identifier: // foo::bar |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4572 | if (TryAltiVecVectorToken()) |
| 4573 | return true; |
| 4574 | // Fall through. |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4575 | case tok::kw_typename: // typename T::type |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4576 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4577 | // recurse to handle whatever we get. |
| 4578 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4579 | return true; |
| 4580 | if (Tok.is(tok::identifier)) |
| 4581 | return false; |
| 4582 | return isTypeSpecifierQualifier(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4583 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4584 | case tok::coloncolon: // ::foo::bar |
| 4585 | if (NextToken().is(tok::kw_new) || // ::new |
| 4586 | NextToken().is(tok::kw_delete)) // ::delete |
| 4587 | return false; |
| 4588 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4589 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4590 | return true; |
| 4591 | return isTypeSpecifierQualifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4592 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 4593 | // GNU attributes support. |
| 4594 | case tok::kw___attribute: |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4595 | // GNU typeof support. |
| 4596 | case tok::kw_typeof: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4597 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4598 | // type-specifiers |
| 4599 | case tok::kw_short: |
| 4600 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4601 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4602 | case tok::kw___int128: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4603 | case tok::kw_signed: |
| 4604 | case tok::kw_unsigned: |
| 4605 | case tok::kw__Complex: |
| 4606 | case tok::kw__Imaginary: |
| 4607 | case tok::kw_void: |
| 4608 | case tok::kw_char: |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4609 | case tok::kw_wchar_t: |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4610 | case tok::kw_char16_t: |
| 4611 | case tok::kw_char32_t: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4612 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4613 | case tok::kw_half: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4614 | case tok::kw_float: |
| 4615 | case tok::kw_double: |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 4616 | case tok::kw__Float16: |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 4617 | case tok::kw___float128: |
Chris Lattner | bb31a42 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 4618 | case tok::kw_bool: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4619 | case tok::kw__Bool: |
| 4620 | case tok::kw__Decimal32: |
| 4621 | case tok::kw__Decimal64: |
| 4622 | case tok::kw__Decimal128: |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4623 | case tok::kw___vector: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 4624 | #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 4625 | #include "clang/Basic/OpenCLImageTypes.def" |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4626 | |
Chris Lattner | 861a226 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 4627 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4628 | case tok::kw_class: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4629 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4630 | case tok::kw___interface: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4631 | case tok::kw_union: |
| 4632 | // enum-specifier |
| 4633 | case tok::kw_enum: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4634 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4635 | // type-qualifier |
| 4636 | case tok::kw_const: |
| 4637 | case tok::kw_volatile: |
| 4638 | case tok::kw_restrict: |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4639 | |
John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4640 | // Debugger support. |
| 4641 | case tok::kw___unknown_anytype: |
| 4642 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4643 | // typedef-name |
Chris Lattner | a8a3f73 | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 4644 | case tok::annot_typename: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4645 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4646 | |
Chris Lattner | 409bf7d | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 4647 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4648 | case tok::less: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4649 | return getLangOpts().ObjC1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4650 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4651 | case tok::kw___cdecl: |
| 4652 | case tok::kw___stdcall: |
| 4653 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4654 | case tok::kw___thiscall: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4655 | case tok::kw___regcall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4656 | case tok::kw___vectorcall: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4657 | case tok::kw___w64: |
| 4658 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4659 | case tok::kw___ptr32: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4660 | case tok::kw___pascal: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4661 | case tok::kw___unaligned: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4662 | |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 4663 | case tok::kw__Nonnull: |
| 4664 | case tok::kw__Nullable: |
| 4665 | case tok::kw__Null_unspecified: |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 4666 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4667 | case tok::kw___kindof: |
| 4668 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4669 | case tok::kw___private: |
| 4670 | case tok::kw___local: |
| 4671 | case tok::kw___global: |
| 4672 | case tok::kw___constant: |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 4673 | case tok::kw___generic: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4674 | case tok::kw___read_only: |
| 4675 | case tok::kw___read_write: |
| 4676 | case tok::kw___write_only: |
| 4677 | |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4678 | return true; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4679 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4680 | // C11 _Atomic |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4681 | case tok::kw__Atomic: |
| 4682 | return true; |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4683 | } |
| 4684 | } |
| 4685 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4686 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 4687 | /// declaration specifier. |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4688 | /// |
| 4689 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 4690 | /// this check is to disambiguate between an expression and a declaration. |
| 4691 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4692 | switch (Tok.getKind()) { |
| 4693 | default: return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4694 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 4695 | case tok::kw_pipe: |
| 4696 | return getLangOpts().OpenCL && (getLangOpts().OpenCLVersion >= 200); |
| 4697 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4698 | case tok::identifier: // foo::bar |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4699 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4700 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4701 | return false; |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4702 | if (TryAltiVecVectorToken()) |
| 4703 | return true; |
| 4704 | // Fall through. |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4705 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4706 | case tok::kw_typename: // typename T::type |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4707 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4708 | // recurse to handle whatever we get. |
| 4709 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4710 | return true; |
| 4711 | if (Tok.is(tok::identifier)) |
| 4712 | return false; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4713 | |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4714 | // 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] | 4715 | // by an identifier and then either ':' or ']', in a place where an |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4716 | // expression is permitted, then this is probably a class message send |
| 4717 | // missing the initial '['. In this case, we won't consider this to be |
| 4718 | // the start of a declaration. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4719 | if (DisambiguatingWithExpression && |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4720 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 4721 | return false; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4722 | |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4723 | return isDeclarationSpecifier(); |
| 4724 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4725 | case tok::coloncolon: // ::foo::bar |
| 4726 | if (NextToken().is(tok::kw_new) || // ::new |
| 4727 | NextToken().is(tok::kw_delete)) // ::delete |
| 4728 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4729 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4730 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4731 | // recurse to handle whatever we get. |
| 4732 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4733 | return true; |
| 4734 | return isDeclarationSpecifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4735 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4736 | // storage-class-specifier |
| 4737 | case tok::kw_typedef: |
| 4738 | case tok::kw_extern: |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 4739 | case tok::kw___private_extern__: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4740 | case tok::kw_static: |
| 4741 | case tok::kw_auto: |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4742 | case tok::kw___auto_type: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4743 | case tok::kw_register: |
| 4744 | case tok::kw___thread: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4745 | case tok::kw_thread_local: |
| 4746 | case tok::kw__Thread_local: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4747 | |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 4748 | // Modules |
| 4749 | case tok::kw___module_private__: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4750 | |
John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4751 | // Debugger support |
| 4752 | case tok::kw___unknown_anytype: |
| 4753 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4754 | // type-specifiers |
| 4755 | case tok::kw_short: |
| 4756 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4757 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4758 | case tok::kw___int128: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4759 | case tok::kw_signed: |
| 4760 | case tok::kw_unsigned: |
| 4761 | case tok::kw__Complex: |
| 4762 | case tok::kw__Imaginary: |
| 4763 | case tok::kw_void: |
| 4764 | case tok::kw_char: |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4765 | case tok::kw_wchar_t: |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4766 | case tok::kw_char16_t: |
| 4767 | case tok::kw_char32_t: |
| 4768 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4769 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4770 | case tok::kw_half: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4771 | case tok::kw_float: |
| 4772 | case tok::kw_double: |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 4773 | case tok::kw__Float16: |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 4774 | case tok::kw___float128: |
Chris Lattner | bb31a42 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 4775 | case tok::kw_bool: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4776 | case tok::kw__Bool: |
| 4777 | case tok::kw__Decimal32: |
| 4778 | case tok::kw__Decimal64: |
| 4779 | case tok::kw__Decimal128: |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4780 | case tok::kw___vector: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4781 | |
Chris Lattner | 861a226 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 4782 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4783 | case tok::kw_class: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4784 | case tok::kw_struct: |
| 4785 | case tok::kw_union: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4786 | case tok::kw___interface: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4787 | // enum-specifier |
| 4788 | case tok::kw_enum: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4789 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4790 | // type-qualifier |
| 4791 | case tok::kw_const: |
| 4792 | case tok::kw_volatile: |
| 4793 | case tok::kw_restrict: |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4794 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4795 | // function-specifier |
| 4796 | case tok::kw_inline: |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4797 | case tok::kw_virtual: |
| 4798 | case tok::kw_explicit: |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4799 | case tok::kw__Noreturn: |
Chris Lattner | 7b20dc7 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4800 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 4801 | // alignment-specifier |
| 4802 | case tok::kw__Alignas: |
| 4803 | |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4804 | // friend keyword. |
| 4805 | case tok::kw_friend: |
| 4806 | |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 4807 | // static_assert-declaration |
| 4808 | case tok::kw__Static_assert: |
| 4809 | |
Chris Lattner | 599e47e | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4810 | // GNU typeof support. |
| 4811 | case tok::kw_typeof: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4812 | |
Chris Lattner | 599e47e | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4813 | // GNU attributes. |
Chris Lattner | 7b20dc7 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4814 | case tok::kw___attribute: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4815 | |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4816 | // C++11 decltype and constexpr. |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4817 | case tok::annot_decltype: |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4818 | case tok::kw_constexpr: |
Francois Pichet | e878cb6 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 4819 | |
Hubert Tong | 375f00a | 2015-06-30 12:14:52 +0000 | [diff] [blame] | 4820 | // C++ Concepts TS - concept |
| 4821 | case tok::kw_concept: |
| 4822 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4823 | // C11 _Atomic |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4824 | case tok::kw__Atomic: |
| 4825 | return true; |
| 4826 | |
Chris Lattner | 8b2ec16 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 4827 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4828 | case tok::less: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4829 | return getLangOpts().ObjC1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4830 | |
Douglas Gregor | 19b7acf | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 4831 | // typedef-name |
| 4832 | case tok::annot_typename: |
| 4833 | return !DisambiguatingWithExpression || |
| 4834 | !isStartOfObjCClassMessageMissingOpenBracket(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4835 | |
Steve Naroff | f192fab | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 4836 | case tok::kw___declspec: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4837 | case tok::kw___cdecl: |
| 4838 | case tok::kw___stdcall: |
| 4839 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4840 | case tok::kw___thiscall: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4841 | case tok::kw___regcall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4842 | case tok::kw___vectorcall: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4843 | case tok::kw___w64: |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4844 | case tok::kw___sptr: |
| 4845 | case tok::kw___uptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4846 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4847 | case tok::kw___ptr32: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4848 | case tok::kw___forceinline: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4849 | case tok::kw___pascal: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4850 | case tok::kw___unaligned: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4851 | |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 4852 | case tok::kw__Nonnull: |
| 4853 | case tok::kw__Nullable: |
| 4854 | case tok::kw__Null_unspecified: |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 4855 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4856 | case tok::kw___kindof: |
| 4857 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4858 | case tok::kw___private: |
| 4859 | case tok::kw___local: |
| 4860 | case tok::kw___global: |
| 4861 | case tok::kw___constant: |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 4862 | case tok::kw___generic: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4863 | case tok::kw___read_only: |
| 4864 | case tok::kw___read_write: |
| 4865 | case tok::kw___write_only: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 4866 | #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 4867 | #include "clang/Basic/OpenCLImageTypes.def" |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4868 | |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4869 | return true; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4870 | } |
| 4871 | } |
| 4872 | |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 4873 | bool Parser::isConstructorDeclarator(bool IsUnqualified, bool DeductionGuide) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4874 | TentativeParsingAction TPA(*this); |
| 4875 | |
| 4876 | // Parse the C++ scope specifier. |
| 4877 | CXXScopeSpec SS; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 4878 | if (ParseOptionalCXXScopeSpecifier(SS, nullptr, |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4879 | /*EnteringContext=*/true)) { |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4880 | TPA.Revert(); |
| 4881 | return false; |
| 4882 | } |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4883 | |
| 4884 | // Parse the constructor name. |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 4885 | if (Tok.is(tok::identifier)) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4886 | // We already know that we have a constructor name; just consume |
| 4887 | // the token. |
| 4888 | ConsumeToken(); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 4889 | } else if (Tok.is(tok::annot_template_id)) { |
| 4890 | ConsumeAnnotationToken(); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4891 | } else { |
| 4892 | TPA.Revert(); |
| 4893 | return false; |
| 4894 | } |
| 4895 | |
Richard Smith | 8f8697f | 2017-02-08 01:16:55 +0000 | [diff] [blame] | 4896 | // There may be attributes here, appertaining to the constructor name or type |
| 4897 | // we just stepped past. |
| 4898 | SkipCXX11Attributes(); |
| 4899 | |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4900 | // Current class name must be followed by a left parenthesis. |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4901 | if (Tok.isNot(tok::l_paren)) { |
| 4902 | TPA.Revert(); |
| 4903 | return false; |
| 4904 | } |
| 4905 | ConsumeParen(); |
| 4906 | |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4907 | // A right parenthesis, or ellipsis followed by a right parenthesis signals |
| 4908 | // that we have a constructor. |
| 4909 | if (Tok.is(tok::r_paren) || |
| 4910 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4911 | TPA.Revert(); |
| 4912 | return true; |
| 4913 | } |
| 4914 | |
Richard Smith | f216366 | 2013-09-06 00:12:20 +0000 | [diff] [blame] | 4915 | // A C++11 attribute here signals that we have a constructor, and is an |
| 4916 | // attribute on the first constructor parameter. |
| 4917 | if (getLangOpts().CPlusPlus11 && |
| 4918 | isCXX11AttributeSpecifier(/*Disambiguate*/ false, |
| 4919 | /*OuterMightBeMessageSend*/ true)) { |
| 4920 | TPA.Revert(); |
| 4921 | return true; |
| 4922 | } |
| 4923 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4924 | // If we need to, enter the specified scope. |
| 4925 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4926 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4927 | DeclScopeObj.EnterDeclaratorScope(); |
| 4928 | |
Francois Pichet | 79f3a87 | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4929 | // Optionally skip Microsoft attributes. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4930 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | 79f3a87 | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4931 | MaybeParseMicrosoftAttributes(Attrs); |
| 4932 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4933 | // Check whether the next token(s) are part of a declaration |
| 4934 | // specifier, in which case we have the start of a parameter and, |
| 4935 | // therefore, we know that this is a constructor. |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4936 | bool IsConstructor = false; |
| 4937 | if (isDeclarationSpecifier()) |
| 4938 | IsConstructor = true; |
| 4939 | else if (Tok.is(tok::identifier) || |
| 4940 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) { |
| 4941 | // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type. |
| 4942 | // This might be a parenthesized member name, but is more likely to |
| 4943 | // be a constructor declaration with an invalid argument type. Keep |
| 4944 | // looking. |
| 4945 | if (Tok.is(tok::annot_cxxscope)) |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 4946 | ConsumeAnnotationToken(); |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4947 | ConsumeToken(); |
| 4948 | |
| 4949 | // If this is not a constructor, we must be parsing a declarator, |
Richard Smith | 1453e31 | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4950 | // which must have one of the following syntactic forms (see the |
| 4951 | // grammar extract at the start of ParseDirectDeclarator): |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4952 | switch (Tok.getKind()) { |
| 4953 | case tok::l_paren: |
| 4954 | // C(X ( int)); |
| 4955 | case tok::l_square: |
| 4956 | // C(X [ 5]); |
| 4957 | // C(X [ [attribute]]); |
| 4958 | case tok::coloncolon: |
| 4959 | // C(X :: Y); |
| 4960 | // C(X :: *p); |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4961 | // Assume this isn't a constructor, rather than assuming it's a |
| 4962 | // constructor with an unnamed parameter of an ill-formed type. |
| 4963 | break; |
| 4964 | |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 4965 | case tok::r_paren: |
| 4966 | // C(X ) |
Richard Smith | 8f8697f | 2017-02-08 01:16:55 +0000 | [diff] [blame] | 4967 | |
| 4968 | // Skip past the right-paren and any following attributes to get to |
| 4969 | // the function body or trailing-return-type. |
| 4970 | ConsumeParen(); |
| 4971 | SkipCXX11Attributes(); |
| 4972 | |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 4973 | if (DeductionGuide) { |
| 4974 | // C(X) -> ... is a deduction guide. |
Richard Smith | 8f8697f | 2017-02-08 01:16:55 +0000 | [diff] [blame] | 4975 | IsConstructor = Tok.is(tok::arrow); |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 4976 | break; |
| 4977 | } |
Richard Smith | 8f8697f | 2017-02-08 01:16:55 +0000 | [diff] [blame] | 4978 | if (Tok.is(tok::colon) || Tok.is(tok::kw_try)) { |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 4979 | // Assume these were meant to be constructors: |
| 4980 | // C(X) : (the name of a bit-field cannot be parenthesized). |
| 4981 | // C(X) try (this is otherwise ill-formed). |
| 4982 | IsConstructor = true; |
| 4983 | } |
Richard Smith | 8f8697f | 2017-02-08 01:16:55 +0000 | [diff] [blame] | 4984 | if (Tok.is(tok::semi) || Tok.is(tok::l_brace)) { |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 4985 | // If we have a constructor name within the class definition, |
| 4986 | // assume these were meant to be constructors: |
| 4987 | // C(X) { |
| 4988 | // C(X) ; |
| 4989 | // ... because otherwise we would be declaring a non-static data |
| 4990 | // member that is ill-formed because it's of the same type as its |
| 4991 | // surrounding class. |
| 4992 | // |
| 4993 | // FIXME: We can actually do this whether or not the name is qualified, |
| 4994 | // 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] | 4995 | // a constructor name. |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 4996 | // currently, so we're somewhat conservative here. |
| 4997 | IsConstructor = IsUnqualified; |
| 4998 | } |
| 4999 | break; |
| 5000 | |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 5001 | default: |
| 5002 | IsConstructor = true; |
| 5003 | break; |
| 5004 | } |
| 5005 | } |
| 5006 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5007 | TPA.Revert(); |
| 5008 | return IsConstructor; |
| 5009 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 5010 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5011 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5012 | /// type-qualifier-list: [C99 6.7.5] |
| 5013 | /// type-qualifier |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5014 | /// [vendor] attributes |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5015 | /// [ only if AttrReqs & AR_VendorAttributesParsed ] |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5016 | /// type-qualifier-list type-qualifier |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5017 | /// [vendor] type-qualifier-list attributes |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5018 | /// [ only if AttrReqs & AR_VendorAttributesParsed ] |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5019 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5020 | /// [ only if AttReqs & AR_CXX11AttributesParsed ] |
| 5021 | /// Note: vendor can be GNU, MS, etc and can be explicitly controlled via |
| 5022 | /// AttrRequirements bitmask values. |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 5023 | void Parser::ParseTypeQualifierListOpt( |
| 5024 | DeclSpec &DS, unsigned AttrReqs, bool AtomicAllowed, |
| 5025 | bool IdentifierRequired, |
| 5026 | Optional<llvm::function_ref<void()>> CodeCompletionHandler) { |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5027 | if (getLangOpts().CPlusPlus11 && (AttrReqs & AR_CXX11AttributesParsed) && |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5028 | isCXX11AttributeSpecifier()) { |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5029 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 5030 | ParseCXX11Attributes(attrs); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5031 | DS.takeAttributesFrom(attrs); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5032 | } |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 5033 | |
| 5034 | SourceLocation EndLoc; |
| 5035 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5036 | while (1) { |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5037 | bool isInvalid = false; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5038 | const char *PrevSpec = nullptr; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5039 | unsigned DiagID = 0; |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 5040 | SourceLocation Loc = Tok.getLocation(); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 5041 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5042 | switch (Tok.getKind()) { |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 5043 | case tok::code_completion: |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 5044 | if (CodeCompletionHandler) |
| 5045 | (*CodeCompletionHandler)(); |
| 5046 | else |
| 5047 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 5048 | return cutOffParsing(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5049 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5050 | case tok::kw_const: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5051 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 5052 | getLangOpts()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 5053 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5054 | case tok::kw_volatile: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5055 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 5056 | getLangOpts()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 5057 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5058 | case tok::kw_restrict: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5059 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 5060 | getLangOpts()); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5061 | break; |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5062 | case tok::kw__Atomic: |
| 5063 | if (!AtomicAllowed) |
| 5064 | goto DoneWithTypeQuals; |
| 5065 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 5066 | getLangOpts()); |
| 5067 | break; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 5068 | |
| 5069 | // OpenCL qualifiers: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 5070 | case tok::kw___private: |
| 5071 | case tok::kw___global: |
| 5072 | case tok::kw___local: |
| 5073 | case tok::kw___constant: |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 5074 | case tok::kw___generic: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 5075 | case tok::kw___read_only: |
| 5076 | case tok::kw___write_only: |
| 5077 | case tok::kw___read_write: |
Aaron Ballman | 05d76ea | 2014-01-14 01:29:54 +0000 | [diff] [blame] | 5078 | ParseOpenCLQualifiers(DS.getAttributes()); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 5079 | break; |
| 5080 | |
Andrey Bokhanko | 45d4132 | 2016-05-11 18:38:21 +0000 | [diff] [blame] | 5081 | case tok::kw___unaligned: |
| 5082 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID, |
| 5083 | getLangOpts()); |
| 5084 | break; |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 5085 | case tok::kw___uptr: |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 5086 | // GNU libc headers in C mode use '__uptr' as an identifer which conflicts |
| 5087 | // with the MS modifier keyword. |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5088 | if ((AttrReqs & AR_DeclspecAttributesParsed) && !getLangOpts().CPlusPlus && |
Alp Toker | 47642d2 | 2013-12-03 06:13:01 +0000 | [diff] [blame] | 5089 | IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) { |
| 5090 | if (TryKeywordIdentFallback(false)) |
| 5091 | continue; |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 5092 | } |
Galina Kistanova | 7767425 | 2017-06-01 21:15:34 +0000 | [diff] [blame] | 5093 | LLVM_FALLTHROUGH; |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 5094 | case tok::kw___sptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 5095 | case tok::kw___w64: |
Steve Naroff | f9c29d4 | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 5096 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 5097 | case tok::kw___ptr32: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 5098 | case tok::kw___cdecl: |
| 5099 | case tok::kw___stdcall: |
| 5100 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 5101 | case tok::kw___thiscall: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 5102 | case tok::kw___regcall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 5103 | case tok::kw___vectorcall: |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5104 | if (AttrReqs & AR_DeclspecAttributesParsed) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5105 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 5106 | continue; |
| 5107 | } |
| 5108 | goto DoneWithTypeQuals; |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5109 | case tok::kw___pascal: |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5110 | if (AttrReqs & AR_VendorAttributesParsed) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5111 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5112 | continue; |
| 5113 | } |
| 5114 | goto DoneWithTypeQuals; |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 5115 | |
| 5116 | // Nullability type specifiers. |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 5117 | case tok::kw__Nonnull: |
| 5118 | case tok::kw__Nullable: |
| 5119 | case tok::kw__Null_unspecified: |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 5120 | ParseNullabilityTypeSpecifiers(DS.getAttributes()); |
| 5121 | continue; |
| 5122 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 5123 | // Objective-C 'kindof' types. |
| 5124 | case tok::kw___kindof: |
| 5125 | DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc, |
| 5126 | nullptr, 0, AttributeList::AS_Keyword); |
| 5127 | (void)ConsumeToken(); |
| 5128 | continue; |
| 5129 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 5130 | case tok::kw___attribute: |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5131 | if (AttrReqs & AR_GNUAttributesParsedAndRejected) |
| 5132 | // When GNU attributes are expressly forbidden, diagnose their usage. |
| 5133 | Diag(Tok, diag::err_attributes_not_allowed); |
| 5134 | |
| 5135 | // Parse the attributes even if they are rejected to ensure that error |
| 5136 | // recovery is graceful. |
| 5137 | if (AttrReqs & AR_GNUAttributesParsed || |
| 5138 | AttrReqs & AR_GNUAttributesParsedAndRejected) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5139 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5140 | continue; // do *not* consume the next token! |
| 5141 | } |
| 5142 | // otherwise, FALL THROUGH! |
Galina Kistanova | 7767425 | 2017-06-01 21:15:34 +0000 | [diff] [blame] | 5143 | LLVM_FALLTHROUGH; |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5144 | default: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 5145 | DoneWithTypeQuals: |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5146 | // If this is not a type-qualifier token, we're done reading type |
| 5147 | // qualifiers. First verify that DeclSpec's are consistent. |
Craig Topper | 2512241 | 2015-11-15 03:32:11 +0000 | [diff] [blame] | 5148 | DS.Finish(Actions, Actions.getASTContext().getPrintingPolicy()); |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 5149 | if (EndLoc.isValid()) |
| 5150 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5151 | return; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5152 | } |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5153 | |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 5154 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 5155 | if (isInvalid) { |
| 5156 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 5157 | Diag(Tok, DiagID) << PrevSpec; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 5158 | } |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 5159 | EndLoc = ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5160 | } |
| 5161 | } |
| 5162 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 5163 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 5164 | /// |
| 5165 | void Parser::ParseDeclarator(Declarator &D) { |
| 5166 | /// This implements the 'declarator' production in the C grammar, then checks |
| 5167 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5168 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 5169 | } |
| 5170 | |
Richard Smith | 0b350b9 | 2014-10-28 16:55:02 +0000 | [diff] [blame] | 5171 | static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang, |
| 5172 | unsigned TheContext) { |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5173 | if (Kind == tok::star || Kind == tok::caret) |
| 5174 | return true; |
| 5175 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5176 | if ((Kind == tok::kw_pipe) && Lang.OpenCL && (Lang.OpenCLVersion >= 200)) |
| 5177 | return true; |
| 5178 | |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5179 | if (!Lang.CPlusPlus) |
| 5180 | return false; |
| 5181 | |
Richard Smith | 0b350b9 | 2014-10-28 16:55:02 +0000 | [diff] [blame] | 5182 | if (Kind == tok::amp) |
| 5183 | return true; |
| 5184 | |
| 5185 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 5186 | // But we must not parse them in conversion-type-ids and new-type-ids, since |
| 5187 | // those can be legitimately followed by a && operator. |
| 5188 | // (The same thing can in theory happen after a trailing-return-type, but |
| 5189 | // since those are a C++11 feature, there is no rejects-valid issue there.) |
| 5190 | if (Kind == tok::ampamp) |
| 5191 | return Lang.CPlusPlus11 || (TheContext != Declarator::ConversionIdContext && |
| 5192 | TheContext != Declarator::CXXNewContext); |
| 5193 | |
| 5194 | return false; |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5195 | } |
| 5196 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5197 | // Indicates whether the given declarator is a pipe declarator. |
| 5198 | static bool isPipeDeclerator(const Declarator &D) { |
| 5199 | const unsigned NumTypes = D.getNumTypeObjects(); |
| 5200 | |
| 5201 | for (unsigned Idx = 0; Idx != NumTypes; ++Idx) |
| 5202 | if (DeclaratorChunk::Pipe == D.getTypeObject(Idx).Kind) |
| 5203 | return true; |
| 5204 | |
| 5205 | return false; |
| 5206 | } |
| 5207 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5208 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 5209 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 5210 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5211 | /// ptr-operator production. |
| 5212 | /// |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 5213 | /// 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] | 5214 | /// made to TryParseDeclarator and MightBeDeclarator, and possibly to |
| 5215 | /// isConstructorDeclarator. |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 5216 | /// |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5217 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 5218 | /// [C] pointer[opt] direct-declarator |
| 5219 | /// [C++] direct-declarator |
| 5220 | /// [C++] ptr-operator declarator |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 5221 | /// |
| 5222 | /// pointer: [C99 6.7.5] |
| 5223 | /// '*' type-qualifier-list[opt] |
| 5224 | /// '*' type-qualifier-list[opt] pointer |
| 5225 | /// |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5226 | /// ptr-operator: |
| 5227 | /// '*' cv-qualifier-seq[opt] |
| 5228 | /// '&' |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 5229 | /// [C++0x] '&&' |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5230 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 5231 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5232 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5233 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 5234 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 66a985d | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 5235 | if (Diags.hasAllExtensionsSilenced()) |
| 5236 | D.setExtension(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5237 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5238 | // C++ member pointers start with a '::' or a nested-name. |
| 5239 | // Member pointers get special handling, since there's no place for the |
| 5240 | // scope spec in the generic path below. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5241 | if (getLangOpts().CPlusPlus && |
Richard Smith | 83c2ecf | 2016-02-02 23:34:49 +0000 | [diff] [blame] | 5242 | (Tok.is(tok::coloncolon) || Tok.is(tok::kw_decltype) || |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 5243 | (Tok.is(tok::identifier) && |
| 5244 | (NextToken().is(tok::coloncolon) || NextToken().is(tok::less))) || |
Chris Lattner | 803802d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 5245 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 5246 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 5247 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5248 | CXXScopeSpec SS; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 5249 | ParseOptionalCXXScopeSpecifier(SS, nullptr, EnteringContext); |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 5250 | |
Jeffrey Yasskin | 4e150f8 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 5251 | if (SS.isNotEmpty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5252 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5253 | // The scope spec really belongs to the direct-declarator. |
Richard Smith | 5f044ad | 2013-01-08 22:43:49 +0000 | [diff] [blame] | 5254 | if (D.mayHaveIdentifier()) |
| 5255 | D.getCXXScopeSpec() = SS; |
| 5256 | else |
| 5257 | AnnotateScopeToken(SS, true); |
| 5258 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5259 | if (DirectDeclParser) |
| 5260 | (this->*DirectDeclParser)(D); |
| 5261 | return; |
| 5262 | } |
| 5263 | |
| 5264 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5265 | D.SetRangeEnd(Loc); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5266 | DeclSpec DS(AttrFactory); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5267 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5268 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5269 | |
| 5270 | // Recurse to parse whatever is left. |
| 5271 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 5272 | |
| 5273 | // Sema will have to catch (syntactically invalid) pointers into global |
| 5274 | // scope. It has to catch pointers into namespace scope anyway. |
| 5275 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
Richard Smith | a865a16 | 2014-12-19 02:07:47 +0000 | [diff] [blame] | 5276 | DS.getLocEnd()), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5277 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5278 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5279 | return; |
| 5280 | } |
| 5281 | } |
| 5282 | |
| 5283 | tok::TokenKind Kind = Tok.getKind(); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5284 | |
| 5285 | if (D.getDeclSpec().isTypeSpecPipe() && !isPipeDeclerator(D)) { |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 5286 | DeclSpec DS(AttrFactory); |
| 5287 | ParseTypeQualifierListOpt(DS); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5288 | |
| 5289 | D.AddTypeInfo( |
| 5290 | DeclaratorChunk::getPipe(DS.getTypeQualifiers(), DS.getPipeLoc()), |
| 5291 | DS.getAttributes(), SourceLocation()); |
| 5292 | } |
| 5293 | |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 5294 | // Not a pointer, C++ reference, or block. |
Richard Smith | 0b350b9 | 2014-10-28 16:55:02 +0000 | [diff] [blame] | 5295 | if (!isPtrOperatorToken(Kind, getLangOpts(), D.getContext())) { |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5296 | if (DirectDeclParser) |
| 5297 | (this->*DirectDeclParser)(D); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5298 | return; |
| 5299 | } |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5300 | |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 5301 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 5302 | // '&&' -> rvalue reference |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 5303 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5304 | D.SetRangeEnd(Loc); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5305 | |
Chris Lattner | 9eac931 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 5306 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 788404f | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 5307 | // Is a pointer. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5308 | DeclSpec DS(AttrFactory); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5309 | |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5310 | // GNU attributes are not allowed here in a new-type-id, but Declspec and |
| 5311 | // C++11 attributes are allowed. |
| 5312 | unsigned Reqs = AR_CXX11AttributesParsed | AR_DeclspecAttributesParsed | |
| 5313 | ((D.getContext() != Declarator::CXXNewContext) |
| 5314 | ? AR_GNUAttributesParsed |
| 5315 | : AR_GNUAttributesParsedAndRejected); |
| 5316 | ParseTypeQualifierListOpt(DS, Reqs, true, !D.mayOmitIdentifier()); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5317 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5318 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5319 | // Recursively parse the declarator. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5320 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 5321 | if (Kind == tok::star) |
| 5322 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 5323 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | e71b378d | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 5324 | DS.getConstSpecLoc(), |
| 5325 | DS.getVolatileSpecLoc(), |
Benjamin Kramer | d0028dc | 2015-03-29 16:42:06 +0000 | [diff] [blame] | 5326 | DS.getRestrictSpecLoc(), |
Andrey Bokhanko | 45d4132 | 2016-05-11 18:38:21 +0000 | [diff] [blame] | 5327 | DS.getAtomicSpecLoc(), |
| 5328 | DS.getUnalignedSpecLoc()), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5329 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5330 | SourceLocation()); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 5331 | else |
| 5332 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5333 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5334 | Loc), |
| 5335 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5336 | SourceLocation()); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5337 | } else { |
| 5338 | // Is a reference |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5339 | DeclSpec DS(AttrFactory); |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 5340 | |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 5341 | // Complain about rvalue references in C++03, but then go on and build |
| 5342 | // the declarator. |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5343 | if (Kind == tok::ampamp) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5344 | Diag(Loc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5345 | diag::warn_cxx98_compat_rvalue_reference : |
| 5346 | diag::ext_rvalue_reference); |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 5347 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5348 | // GNU-style and C++11 attributes are allowed here, as is restrict. |
| 5349 | ParseTypeQualifierListOpt(DS); |
| 5350 | D.ExtendWithDeclSpec(DS); |
| 5351 | |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 5352 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 5353 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 5354 | // template type argument, in which case the cv-qualifiers are ignored. |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 5355 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 5356 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 5357 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 5358 | diag::err_invalid_reference_qualifier_application) << "const"; |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 5359 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 5360 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 5361 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5362 | // 'restrict' is permitted as an extension. |
| 5363 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
| 5364 | Diag(DS.getAtomicSpecLoc(), |
| 5365 | diag::err_invalid_reference_qualifier_application) << "_Atomic"; |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 5366 | } |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5367 | |
| 5368 | // Recursively parse the declarator. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5369 | ParseDeclaratorInternal(D, DirectDeclParser); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5370 | |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 5371 | if (D.getNumTypeObjects() > 0) { |
| 5372 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 5373 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 5374 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | ebad6a2 | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 5375 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 5376 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 5377 | << II; |
| 5378 | else |
| 5379 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 5380 | << "type name"; |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 5381 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5382 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 5383 | // can go ahead and build the (technically ill-formed) |
| 5384 | // declarator: reference collapsing will take care of it. |
| 5385 | } |
| 5386 | } |
| 5387 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5388 | // Remember that we parsed a reference type. |
Chris Lattner | 788404f | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 5389 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 5390 | Kind == tok::amp), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5391 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5392 | SourceLocation()); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5393 | } |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 5394 | } |
| 5395 | |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5396 | // When correcting from misplaced brackets before the identifier, the location |
| 5397 | // is saved inside the declarator so that other diagnostic messages can use |
| 5398 | // them. This extracts and returns that location, or returns the provided |
| 5399 | // location if a stored location does not exist. |
| 5400 | static SourceLocation getMissingDeclaratorIdLoc(Declarator &D, |
| 5401 | SourceLocation Loc) { |
| 5402 | if (D.getName().StartLocation.isInvalid() && |
| 5403 | D.getName().EndLocation.isValid()) |
| 5404 | return D.getName().EndLocation; |
| 5405 | |
| 5406 | return Loc; |
| 5407 | } |
| 5408 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5409 | /// ParseDirectDeclarator |
| 5410 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5411 | /// [C99] identifier |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5412 | /// '(' declarator ')' |
| 5413 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5414 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 5415 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 5416 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 5417 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 5418 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5419 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 5420 | /// attribute-specifier-seq[opt] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5421 | /// direct-declarator '(' parameter-type-list ')' |
| 5422 | /// direct-declarator '(' identifier-list[opt] ')' |
| 5423 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 5424 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 22c40fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 5425 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 5426 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5427 | /// [C++11] direct-declarator '(' parameter-declaration-clause ')' |
| 5428 | /// attribute-specifier-seq[opt] cv-qualifier-seq[opt] |
| 5429 | /// ref-qualifier[opt] exception-specification[opt] |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 5430 | /// [C++] declarator-id |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5431 | /// [C++11] declarator-id attribute-specifier-seq[opt] |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5432 | /// |
| 5433 | /// declarator-id: [C++ 8] |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 5434 | /// '...'[opt] id-expression |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5435 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 5436 | /// |
| 5437 | /// id-expression: [C++ 5.1] |
| 5438 | /// unqualified-id |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5439 | /// qualified-id |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5440 | /// |
| 5441 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5442 | /// identifier |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 5443 | /// operator-function-id |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5444 | /// conversion-function-id |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5445 | /// '~' class-name |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 5446 | /// template-id |
Argyrios Kyrtzidis | e442635 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 5447 | /// |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 5448 | /// C++17 adds the following, which we also handle here: |
| 5449 | /// |
| 5450 | /// simple-declaration: |
| 5451 | /// <decl-spec> '[' identifier-list ']' brace-or-equal-initializer ';' |
| 5452 | /// |
Richard Smith | 1453e31 | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 5453 | /// Note, any additional constructs added here may need corresponding changes |
| 5454 | /// in isConstructorDeclarator. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5455 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5456 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 5457 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5458 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 5459 | // This might be a C++17 structured binding. |
| 5460 | if (Tok.is(tok::l_square) && !D.mayOmitIdentifier() && |
| 5461 | D.getCXXScopeSpec().isEmpty()) |
| 5462 | return ParseDecompositionDeclarator(D); |
| 5463 | |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 5464 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR inside a class, in |
| 5465 | // this context it is a bitfield. Also in range-based for statement colon |
| 5466 | // may delimit for-range-declaration. |
| 5467 | ColonProtectionRAIIObject X(*this, |
| 5468 | D.getContext() == Declarator::MemberContext || |
| 5469 | (D.getContext() == Declarator::ForContext && |
| 5470 | getLangOpts().CPlusPlus11)); |
| 5471 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5472 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5473 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 5474 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 5475 | D.getContext() == Declarator::MemberContext; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 5476 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), nullptr, |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 5477 | EnteringContext); |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 5478 | } |
| 5479 | |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5480 | if (D.getCXXScopeSpec().isValid()) { |
Richard Smith | 64e033f | 2015-01-15 00:48:52 +0000 | [diff] [blame] | 5481 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), |
| 5482 | D.getCXXScopeSpec())) |
John McCall | 2b058ef | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 5483 | // Change the declaration context for name lookup, until this function |
| 5484 | // is exited (and the declarator has been parsed). |
| 5485 | DeclScopeObj.EnterDeclaratorScope(); |
Alex Lorenz | e151f010 | 2016-12-07 10:24:44 +0000 | [diff] [blame] | 5486 | else if (getObjCDeclContext()) { |
| 5487 | // Ensure that we don't interpret the next token as an identifier when |
| 5488 | // dealing with declarations in an Objective-C container. |
| 5489 | D.SetIdentifier(nullptr, Tok.getLocation()); |
| 5490 | D.setInvalidType(true); |
| 5491 | ConsumeToken(); |
| 5492 | goto PastIdentifier; |
| 5493 | } |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5494 | } |
| 5495 | |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 5496 | // C++0x [dcl.fct]p14: |
Faisal Vali | 8435a69 | 2014-12-04 12:40:21 +0000 | [diff] [blame] | 5497 | // There is a syntactic ambiguity when an ellipsis occurs at the end of a |
| 5498 | // parameter-declaration-clause without a preceding comma. In this case, |
| 5499 | // the ellipsis is parsed as part of the abstract-declarator if the type |
| 5500 | // of the parameter either names a template parameter pack that has not |
| 5501 | // been expanded or contains auto; otherwise, it is parsed as part of the |
| 5502 | // parameter-declaration-clause. |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5503 | if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() && |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 5504 | !((D.getContext() == Declarator::PrototypeContext || |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5505 | D.getContext() == Declarator::LambdaExprParameterContext || |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 5506 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 5507 | NextToken().is(tok::r_paren) && |
Richard Smith | b19337f | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 5508 | !D.hasGroupingParens() && |
Faisal Vali | 8435a69 | 2014-12-04 12:40:21 +0000 | [diff] [blame] | 5509 | !Actions.containsUnexpandedParameterPacks(D) && |
| 5510 | D.getDeclSpec().getTypeSpecType() != TST_auto)) { |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5511 | SourceLocation EllipsisLoc = ConsumeToken(); |
Richard Smith | 0b350b9 | 2014-10-28 16:55:02 +0000 | [diff] [blame] | 5512 | if (isPtrOperatorToken(Tok.getKind(), getLangOpts(), D.getContext())) { |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5513 | // The ellipsis was put in the wrong place. Recover, and explain to |
| 5514 | // the user what they should have done. |
| 5515 | ParseDeclarator(D); |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 5516 | if (EllipsisLoc.isValid()) |
| 5517 | DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D); |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5518 | return; |
| 5519 | } else |
| 5520 | D.setEllipsisLoc(EllipsisLoc); |
| 5521 | |
| 5522 | // The ellipsis can't be followed by a parenthesized declarator. We |
| 5523 | // check for that in ParseParenDeclarator, after we have disambiguated |
| 5524 | // the l_paren token. |
| 5525 | } |
| 5526 | |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 5527 | if (Tok.isOneOf(tok::identifier, tok::kw_operator, tok::annot_template_id, |
| 5528 | tok::tilde)) { |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5529 | // We found something that indicates the start of an unqualified-id. |
| 5530 | // Parse that unqualified-id. |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 5531 | bool AllowConstructorName; |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5532 | bool AllowDeductionGuide; |
| 5533 | if (D.getDeclSpec().hasTypeSpecifier()) { |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 5534 | AllowConstructorName = false; |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5535 | AllowDeductionGuide = false; |
| 5536 | } else if (D.getCXXScopeSpec().isSet()) { |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 5537 | AllowConstructorName = |
| 5538 | (D.getContext() == Declarator::FileContext || |
Dmitri Gribenko | d1c91f1 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 5539 | D.getContext() == Declarator::MemberContext); |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5540 | AllowDeductionGuide = false; |
| 5541 | } else { |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 5542 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5543 | AllowDeductionGuide = |
| 5544 | (D.getContext() == Declarator::FileContext || |
| 5545 | D.getContext() == Declarator::MemberContext); |
| 5546 | } |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 5547 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 5548 | SourceLocation TemplateKWLoc; |
Richard Smith | 64e033f | 2015-01-15 00:48:52 +0000 | [diff] [blame] | 5549 | bool HadScope = D.getCXXScopeSpec().isValid(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5550 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 5551 | /*EnteringContext=*/true, |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 5552 | /*AllowDestructorName=*/true, AllowConstructorName, |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5553 | AllowDeductionGuide, nullptr, TemplateKWLoc, |
| 5554 | D.getName()) || |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5555 | // Once we're past the identifier, if the scope was bad, mark the |
| 5556 | // whole declarator bad. |
| 5557 | D.getCXXScopeSpec().isInvalid()) { |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5558 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5559 | D.setInvalidType(true); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5560 | } else { |
Richard Smith | 64e033f | 2015-01-15 00:48:52 +0000 | [diff] [blame] | 5561 | // ParseUnqualifiedId might have parsed a scope specifier during error |
| 5562 | // recovery. If it did so, enter that scope. |
| 5563 | if (!HadScope && D.getCXXScopeSpec().isValid() && |
| 5564 | Actions.ShouldEnterDeclaratorScope(getCurScope(), |
| 5565 | D.getCXXScopeSpec())) |
| 5566 | DeclScopeObj.EnterDeclaratorScope(); |
| 5567 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5568 | // Parsed the unqualified-id; update range information and move along. |
| 5569 | if (D.getSourceRange().getBegin().isInvalid()) |
| 5570 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 5571 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5572 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5573 | goto PastIdentifier; |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5574 | } |
Richard Smith | d63db6e | 2015-12-19 02:40:19 +0000 | [diff] [blame] | 5575 | |
| 5576 | if (D.getCXXScopeSpec().isNotEmpty()) { |
| 5577 | // We have a scope specifier but no following unqualified-id. |
| 5578 | Diag(PP.getLocForEndOfToken(D.getCXXScopeSpec().getEndLoc()), |
| 5579 | diag::err_expected_unqualified_id) |
| 5580 | << /*C++*/1; |
| 5581 | D.SetIdentifier(nullptr, Tok.getLocation()); |
| 5582 | goto PastIdentifier; |
| 5583 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5584 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5585 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5586 | "There's a C++-specific check for tok::identifier above"); |
| 5587 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 5588 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
Richard Trieu | 2664dc1 | 2014-05-05 22:06:50 +0000 | [diff] [blame] | 5589 | D.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5590 | ConsumeToken(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5591 | goto PastIdentifier; |
Richard Smith | 74639b1 | 2017-05-19 01:54:59 +0000 | [diff] [blame] | 5592 | } else if (Tok.is(tok::identifier) && !D.mayHaveIdentifier()) { |
| 5593 | // We're not allowed an identifier here, but we got one. Try to figure out |
| 5594 | // if the user was trying to attach a name to the type, or whether the name |
| 5595 | // is some unrelated trailing syntax. |
| 5596 | bool DiagnoseIdentifier = false; |
| 5597 | if (D.hasGroupingParens()) |
| 5598 | // An identifier within parens is unlikely to be intended to be anything |
| 5599 | // other than a name being "declared". |
| 5600 | DiagnoseIdentifier = true; |
| 5601 | else if (D.getContext() == Declarator::TemplateTypeArgContext) |
| 5602 | // T<int N> is an accidental identifier; T<int N indicates a missing '>'. |
| 5603 | DiagnoseIdentifier = |
| 5604 | NextToken().isOneOf(tok::comma, tok::greater, tok::greatergreater); |
| 5605 | else if (D.getContext() == Declarator::AliasDeclContext || |
| 5606 | D.getContext() == Declarator::AliasTemplateContext) |
| 5607 | // The most likely error is that the ';' was forgotten. |
| 5608 | DiagnoseIdentifier = NextToken().isOneOf(tok::comma, tok::semi); |
| 5609 | else if (D.getContext() == Declarator::TrailingReturnContext && |
| 5610 | !isCXX11VirtSpecifier(Tok)) |
| 5611 | DiagnoseIdentifier = NextToken().isOneOf( |
| 5612 | tok::comma, tok::semi, tok::equal, tok::l_brace, tok::kw_try); |
| 5613 | if (DiagnoseIdentifier) { |
Richard Smith | f39720b | 2013-10-13 22:12:28 +0000 | [diff] [blame] | 5614 | Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id) |
| 5615 | << FixItHint::CreateRemoval(Tok.getLocation()); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5616 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Richard Smith | f39720b | 2013-10-13 22:12:28 +0000 | [diff] [blame] | 5617 | ConsumeToken(); |
| 5618 | goto PastIdentifier; |
| 5619 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5620 | } |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5621 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5622 | if (Tok.is(tok::l_paren)) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5623 | // direct-declarator: '(' declarator ')' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 5624 | // direct-declarator: '(' attributes declarator ')' |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5625 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 5626 | ParseParenDeclarator(D); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5627 | |
| 5628 | // If the declarator was parenthesized, we entered the declarator |
| 5629 | // scope when parsing the parenthesized declarator, then exited |
| 5630 | // the scope already. Re-enter the scope, if we need to. |
| 5631 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 5632 | // If there was an error parsing parenthesized declarator, declarator |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5633 | // scope may have been entered before. Don't do it again. |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 5634 | if (!D.isInvalidType() && |
Nico Weber | 6b05f38 | 2015-02-18 04:53:03 +0000 | [diff] [blame] | 5635 | Actions.ShouldEnterDeclaratorScope(getCurScope(), |
| 5636 | D.getCXXScopeSpec())) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5637 | // Change the declaration context for name lookup, until this function |
| 5638 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 5639 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5640 | } |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5641 | } else if (D.mayOmitIdentifier()) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5642 | // This could be something simple like "int" (in which case the declarator |
| 5643 | // portion is empty), if an abstract-declarator is allowed. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5644 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Richard Smith | b19337f | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 5645 | |
| 5646 | // The grammar for abstract-pack-declarator does not allow grouping parens. |
| 5647 | // FIXME: Revisit this once core issue 1488 is resolved. |
| 5648 | if (D.hasEllipsis() && D.hasGroupingParens()) |
| 5649 | Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()), |
| 5650 | diag::ext_abstract_pack_declarator_parens); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5651 | } else { |
David Blaikie | 5d577a2 | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 5652 | if (Tok.getKind() == tok::annot_pragma_parser_crash) |
David Blaikie | 5bd4c2a | 2012-08-21 18:56:49 +0000 | [diff] [blame] | 5653 | LLVM_BUILTIN_TRAP; |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5654 | if (Tok.is(tok::l_square)) |
| 5655 | return ParseMisplacedBracketDeclarator(D); |
| 5656 | if (D.getContext() == Declarator::MemberContext) { |
Alex Lorenz | f127821 | 2017-04-11 15:01:53 +0000 | [diff] [blame] | 5657 | // Objective-C++: Detect C++ keywords and try to prevent further errors by |
| 5658 | // treating these keyword as valid member names. |
| 5659 | if (getLangOpts().ObjC1 && getLangOpts().CPlusPlus && |
| 5660 | Tok.getIdentifierInfo() && |
| 5661 | Tok.getIdentifierInfo()->isCPlusPlusKeyword(getLangOpts())) { |
| 5662 | Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()), |
| 5663 | diag::err_expected_member_name_or_semi_objcxx_keyword) |
| 5664 | << Tok.getIdentifierInfo() |
| 5665 | << (D.getDeclSpec().isEmpty() ? SourceRange() |
| 5666 | : D.getDeclSpec().getSourceRange()); |
| 5667 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 5668 | D.SetRangeEnd(Tok.getLocation()); |
| 5669 | ConsumeToken(); |
| 5670 | goto PastIdentifier; |
| 5671 | } |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5672 | Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()), |
| 5673 | diag::err_expected_member_name_or_semi) |
Richard Trieu | a134240 | 2014-05-02 23:40:32 +0000 | [diff] [blame] | 5674 | << (D.getDeclSpec().isEmpty() ? SourceRange() |
| 5675 | : D.getDeclSpec().getSourceRange()); |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5676 | } else if (getLangOpts().CPlusPlus) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 5677 | if (Tok.isOneOf(tok::period, tok::arrow)) |
Richard Trieu | 9c67267 | 2013-01-26 02:31:38 +0000 | [diff] [blame] | 5678 | Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow); |
Richard Trieu | 2f58696 | 2013-09-05 02:31:33 +0000 | [diff] [blame] | 5679 | else { |
| 5680 | SourceLocation Loc = D.getCXXScopeSpec().getEndLoc(); |
| 5681 | if (Tok.isAtStartOfLine() && Loc.isValid()) |
| 5682 | Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id) |
| 5683 | << getLangOpts().CPlusPlus; |
| 5684 | else |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5685 | Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()), |
| 5686 | diag::err_expected_unqualified_id) |
Richard Trieu | 2f58696 | 2013-09-05 02:31:33 +0000 | [diff] [blame] | 5687 | << getLangOpts().CPlusPlus; |
| 5688 | } |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5689 | } else { |
| 5690 | Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()), |
| 5691 | diag::err_expected_either) |
| 5692 | << tok::identifier << tok::l_paren; |
| 5693 | } |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5694 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Chris Lattner | 8c5dd73 | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 5695 | D.setInvalidType(true); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5696 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5697 | |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5698 | PastIdentifier: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5699 | assert(D.isPastIdentifier() && |
| 5700 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5701 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5702 | // Don't parse attributes unless we have parsed an unparenthesized name. |
| 5703 | if (D.hasName() && !D.getNumTypeObjects()) |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5704 | MaybeParseCXX11Attributes(D); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5705 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5706 | while (1) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5707 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5708 | // Enter function-declaration scope, limiting any declarators to the |
| 5709 | // function prototype scope, including parameter declarators. |
| 5710 | ParseScope PrototypeScope(this, |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 5711 | Scope::FunctionPrototypeScope|Scope::DeclScope| |
| 5712 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 5713 | ? Scope::FunctionDeclarationScope : 0)); |
| 5714 | |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5715 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 5716 | // In such a case, check if we actually have a function declarator; if it |
| 5717 | // is not, the declarator has been fully parsed. |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5718 | bool IsAmbiguous = false; |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 5719 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 5720 | // The name of the declarator, if any, is tentatively declared within |
| 5721 | // a possible direct initializer. |
| 5722 | TentativelyDeclaredIdentifiers.push_back(D.getIdentifier()); |
| 5723 | bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous); |
| 5724 | TentativelyDeclaredIdentifiers.pop_back(); |
| 5725 | if (!IsFunctionDecl) |
| 5726 | break; |
| 5727 | } |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5728 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5729 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 5730 | T.consumeOpen(); |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5731 | ParseFunctionDeclarator(D, attrs, T, IsAmbiguous); |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5732 | PrototypeScope.Exit(); |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5733 | } else if (Tok.is(tok::l_square)) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5734 | ParseBracketDeclarator(D); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5735 | } else { |
| 5736 | break; |
| 5737 | } |
| 5738 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5739 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5740 | |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 5741 | void Parser::ParseDecompositionDeclarator(Declarator &D) { |
| 5742 | assert(Tok.is(tok::l_square)); |
| 5743 | |
| 5744 | // If this doesn't look like a structured binding, maybe it's a misplaced |
| 5745 | // array declarator. |
| 5746 | // FIXME: Consume the l_square first so we don't need extra lookahead for |
| 5747 | // this. |
| 5748 | if (!(NextToken().is(tok::identifier) && |
| 5749 | GetLookAheadToken(2).isOneOf(tok::comma, tok::r_square)) && |
| 5750 | !(NextToken().is(tok::r_square) && |
| 5751 | GetLookAheadToken(2).isOneOf(tok::equal, tok::l_brace))) |
| 5752 | return ParseMisplacedBracketDeclarator(D); |
| 5753 | |
| 5754 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 5755 | T.consumeOpen(); |
| 5756 | |
| 5757 | SmallVector<DecompositionDeclarator::Binding, 32> Bindings; |
| 5758 | while (Tok.isNot(tok::r_square)) { |
| 5759 | if (!Bindings.empty()) { |
| 5760 | if (Tok.is(tok::comma)) |
| 5761 | ConsumeToken(); |
| 5762 | else { |
| 5763 | if (Tok.is(tok::identifier)) { |
| 5764 | SourceLocation EndLoc = getEndOfPreviousToken(); |
| 5765 | Diag(EndLoc, diag::err_expected) |
| 5766 | << tok::comma << FixItHint::CreateInsertion(EndLoc, ","); |
| 5767 | } else { |
| 5768 | Diag(Tok, diag::err_expected_comma_or_rsquare); |
| 5769 | } |
| 5770 | |
| 5771 | SkipUntil(tok::r_square, tok::comma, tok::identifier, |
| 5772 | StopAtSemi | StopBeforeMatch); |
| 5773 | if (Tok.is(tok::comma)) |
| 5774 | ConsumeToken(); |
| 5775 | else if (Tok.isNot(tok::identifier)) |
| 5776 | break; |
| 5777 | } |
| 5778 | } |
| 5779 | |
| 5780 | if (Tok.isNot(tok::identifier)) { |
| 5781 | Diag(Tok, diag::err_expected) << tok::identifier; |
| 5782 | break; |
| 5783 | } |
| 5784 | |
| 5785 | Bindings.push_back({Tok.getIdentifierInfo(), Tok.getLocation()}); |
| 5786 | ConsumeToken(); |
| 5787 | } |
| 5788 | |
| 5789 | if (Tok.isNot(tok::r_square)) |
| 5790 | // We've already diagnosed a problem here. |
| 5791 | T.skipToEnd(); |
| 5792 | else { |
| 5793 | // C++17 does not allow the identifier-list in a structured binding |
| 5794 | // to be empty. |
| 5795 | if (Bindings.empty()) |
| 5796 | Diag(Tok.getLocation(), diag::ext_decomp_decl_empty); |
| 5797 | |
| 5798 | T.consumeClose(); |
| 5799 | } |
| 5800 | |
| 5801 | return D.setDecompositionBindings(T.getOpenLocation(), Bindings, |
| 5802 | T.getCloseLocation()); |
| 5803 | } |
| 5804 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5805 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 5806 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5807 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5808 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 5809 | /// |
| 5810 | /// direct-declarator: |
| 5811 | /// '(' declarator ')' |
| 5812 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5813 | /// direct-declarator '(' parameter-type-list ')' |
| 5814 | /// direct-declarator '(' identifier-list[opt] ')' |
| 5815 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 5816 | /// parameter-type-list[opt] ')' |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5817 | /// |
| 5818 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5819 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 5820 | T.consumeOpen(); |
| 5821 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5822 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5823 | |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5824 | // Eat any attributes before we look at whether this is a grouping or function |
| 5825 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 5826 | // the type being built up, for example: |
| 5827 | // int (__attribute__(()) *x)(long y) |
| 5828 | // If this ends up not being a grouping paren, the attribute applies to the |
| 5829 | // first argument, for example: |
| 5830 | // int (__attribute__(()) int x) |
| 5831 | // In either case, we need to eat any attributes to be able to determine what |
| 5832 | // sort of paren this is. |
| 5833 | // |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5834 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5835 | bool RequiresArg = false; |
| 5836 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5837 | ParseGNUAttributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5838 | |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5839 | // We require that the argument list (if this is a non-grouping paren) be |
| 5840 | // present even if the attribute list was empty. |
| 5841 | RequiresArg = true; |
| 5842 | } |
Chad Rosier | eea9ca7 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 5843 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 5844 | // Eat any Microsoft extensions. |
Chad Rosier | eea9ca7 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 5845 | ParseMicrosoftTypeAttributes(attrs); |
| 5846 | |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5847 | // Eat any Borland extensions. |
Ted Kremenek | 5eec2b0 | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 5848 | if (Tok.is(tok::kw___pascal)) |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5849 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5850 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5851 | // If we haven't past the identifier yet (or where the identifier would be |
| 5852 | // stored, if this is an abstract declarator), then this is probably just |
| 5853 | // grouping parens. However, if this could be an abstract-declarator, then |
| 5854 | // this could also be the start of function arguments (consider 'void()'). |
| 5855 | bool isGrouping; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5856 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5857 | if (!D.mayOmitIdentifier()) { |
| 5858 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 5859 | // paren, because we haven't seen the identifier yet. |
| 5860 | isGrouping = true; |
| 5861 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 5862 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) && |
| 5863 | NextToken().is(tok::r_paren)) || // C++ int(...) |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5864 | isDeclarationSpecifier() || // 'int(int)' is a function. |
| 5865 | isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function. |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5866 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 5867 | // considered to be a type, not a K&R identifier-list. |
| 5868 | isGrouping = false; |
| 5869 | } else { |
| 5870 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 5871 | isGrouping = true; |
| 5872 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5873 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5874 | // If this is a grouping paren, handle: |
| 5875 | // direct-declarator: '(' declarator ')' |
| 5876 | // direct-declarator: '(' attributes declarator ')' |
| 5877 | if (isGrouping) { |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5878 | SourceLocation EllipsisLoc = D.getEllipsisLoc(); |
| 5879 | D.setEllipsisLoc(SourceLocation()); |
| 5880 | |
Argyrios Kyrtzidis | 8ae3684 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 5881 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5882 | D.setGroupingParens(true); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5883 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5884 | // Match the ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5885 | T.consumeClose(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5886 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5887 | T.getCloseLocation()), |
| 5888 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 8ae3684 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 5889 | |
| 5890 | D.setGroupingParens(hadGroupingParens); |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5891 | |
| 5892 | // An ellipsis cannot be placed outside parentheses. |
| 5893 | if (EllipsisLoc.isValid()) |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 5894 | DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D); |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5895 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5896 | return; |
| 5897 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5898 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5899 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 5900 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5901 | // identifier (and remember where it would have been), then call into |
| 5902 | // ParseFunctionDeclarator to handle of argument list. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5903 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5904 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5905 | // Enter function-declaration scope, limiting any declarators to the |
| 5906 | // function prototype scope, including parameter declarators. |
| 5907 | ParseScope PrototypeScope(this, |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 5908 | Scope::FunctionPrototypeScope | Scope::DeclScope | |
| 5909 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 5910 | ? Scope::FunctionDeclarationScope : 0)); |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5911 | ParseFunctionDeclarator(D, attrs, T, false, RequiresArg); |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5912 | PrototypeScope.Exit(); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5913 | } |
| 5914 | |
| 5915 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 5916 | /// declarator D up to a paren, which indicates that we are parsing function |
| 5917 | /// arguments. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5918 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5919 | /// If FirstArgAttrs is non-null, then the caller parsed those arguments |
| 5920 | /// immediately after the open paren - they should be considered to be the |
| 5921 | /// first argument of a parameter. |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5922 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5923 | /// If RequiresArg is true, then the first argument of the function is required |
| 5924 | /// to be present and required to not be an identifier list. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5925 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5926 | /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt], |
| 5927 | /// (C++11) ref-qualifier[opt], exception-specification[opt], |
| 5928 | /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt]. |
| 5929 | /// |
| 5930 | /// [C++11] exception-specification: |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5931 | /// dynamic-exception-specification |
| 5932 | /// noexcept-specification |
| 5933 | /// |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5934 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5935 | ParsedAttributes &FirstArgAttrs, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5936 | BalancedDelimiterTracker &Tracker, |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5937 | bool IsAmbiguous, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5938 | bool RequiresArg) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5939 | assert(getCurScope()->isFunctionPrototypeScope() && |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5940 | "Should call from a Function scope"); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5941 | // lparen is already consumed! |
| 5942 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 5943 | |
| 5944 | // This should be true when the function has typed arguments. |
| 5945 | // Otherwise, it is treated as a K&R-style function. |
| 5946 | bool HasProto = false; |
| 5947 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5948 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5949 | // Remember where we see an ellipsis, if any. |
| 5950 | SourceLocation EllipsisLoc; |
| 5951 | |
| 5952 | DeclSpec DS(AttrFactory); |
| 5953 | bool RefQualifierIsLValueRef = true; |
| 5954 | SourceLocation RefQualifierLoc; |
Douglas Gregor | e248eea | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 5955 | SourceLocation ConstQualifierLoc; |
| 5956 | SourceLocation VolatileQualifierLoc; |
Hal Finkel | 23a0739 | 2014-10-20 17:32:04 +0000 | [diff] [blame] | 5957 | SourceLocation RestrictQualifierLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5958 | ExceptionSpecificationType ESpecType = EST_None; |
| 5959 | SourceRange ESpecRange; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5960 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 5961 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5962 | ExprResult NoexceptExpr; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 5963 | CachedTokens *ExceptionSpecTokens = nullptr; |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5964 | ParsedAttributes FnAttrs(AttrFactory); |
Richard Smith | 700537c | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 5965 | TypeResult TrailingReturnType; |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5966 | |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5967 | /* LocalEndLoc is the end location for the local FunctionTypeLoc. |
| 5968 | EndLoc is the end location for the function declarator. |
| 5969 | They differ for trailing return types. */ |
| 5970 | SourceLocation StartLoc, LocalEndLoc, EndLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5971 | SourceLocation LParenLoc, RParenLoc; |
| 5972 | LParenLoc = Tracker.getOpenLocation(); |
| 5973 | StartLoc = LParenLoc; |
| 5974 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5975 | if (isFunctionDeclaratorIdentifierList()) { |
| 5976 | if (RequiresArg) |
| 5977 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 5978 | |
| 5979 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 5980 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5981 | Tracker.consumeClose(); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5982 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5983 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5984 | EndLoc = RParenLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5985 | } else { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5986 | if (Tok.isNot(tok::r_paren)) |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5987 | ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, |
| 5988 | EllipsisLoc); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5989 | else if (RequiresArg) |
| 5990 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 5991 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5992 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5993 | |
| 5994 | // If we have the closing ')', eat it. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5995 | Tracker.consumeClose(); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5996 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5997 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5998 | EndLoc = RParenLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5999 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6000 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6001 | // FIXME: Accept these components in any order, and produce fixits to |
| 6002 | // 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] | 6003 | // with the pure-specifier in the same way. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6004 | |
| 6005 | // Parse cv-qualifier-seq[opt]. |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 6006 | ParseTypeQualifierListOpt(DS, AR_NoAttributesParsed, |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 6007 | /*AtomicAllowed*/ false, |
| 6008 | /*IdentifierRequired=*/false, |
| 6009 | llvm::function_ref<void()>([&]() { |
| 6010 | Actions.CodeCompleteFunctionQualifiers(DS, D); |
| 6011 | })); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6012 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
| 6013 | EndLoc = DS.getSourceRange().getEnd(); |
| 6014 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 6015 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
Hal Finkel | 23a0739 | 2014-10-20 17:32:04 +0000 | [diff] [blame] | 6016 | RestrictQualifierLoc = DS.getRestrictSpecLoc(); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6017 | } |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6018 | |
| 6019 | // Parse ref-qualifier[opt]. |
Ehsan Akhgari | c07d1e2 | 2015-03-25 00:53:33 +0000 | [diff] [blame] | 6020 | if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc)) |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6021 | EndLoc = RefQualifierLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6022 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 6023 | // C++11 [expr.prim.general]p3: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 6024 | // If a declaration declares a member function or member function |
| 6025 | // 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] | 6026 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 6027 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 6028 | // declarator. |
Richard Smith | ad1bbb9 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 6029 | // FIXME: currently, "static" case isn't handled correctly. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 6030 | bool IsCXX11MemberFunction = |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6031 | getLangOpts().CPlusPlus11 && |
Richard Smith | 990a692 | 2014-01-17 21:01:18 +0000 | [diff] [blame] | 6032 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
Richard Smith | ad1bbb9 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 6033 | (D.getContext() == Declarator::MemberContext |
| 6034 | ? !D.getDeclSpec().isFriendSpecified() |
| 6035 | : D.getContext() == Declarator::FileContext && |
| 6036 | D.getCXXScopeSpec().isValid() && |
| 6037 | Actions.CurContext->isRecord()); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 6038 | Sema::CXXThisScopeRAII ThisScope(Actions, |
| 6039 | dyn_cast<CXXRecordDecl>(Actions.CurContext), |
Richard Smith | 01141a9 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 6040 | DS.getTypeQualifiers() | |
Richard Smith | 034185c | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 6041 | (D.getDeclSpec().isConstexprSpecified() && |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 6042 | !getLangOpts().CPlusPlus14 |
Richard Smith | 01141a9 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 6043 | ? Qualifiers::Const : 0), |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 6044 | IsCXX11MemberFunction); |
Richard Smith | 2331bbf | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 6045 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6046 | // Parse exception-specification[opt]. |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 6047 | bool Delayed = D.isFirstDeclarationOfMember() && |
Richard Smith | 3ef3e89 | 2014-11-20 22:32:11 +0000 | [diff] [blame] | 6048 | D.isFunctionDeclaratorAFunctionDeclaration(); |
| 6049 | if (Delayed && Actions.isLibstdcxxEagerExceptionSpecHack(D) && |
| 6050 | GetLookAheadToken(0).is(tok::kw_noexcept) && |
| 6051 | GetLookAheadToken(1).is(tok::l_paren) && |
| 6052 | GetLookAheadToken(2).is(tok::kw_noexcept) && |
| 6053 | GetLookAheadToken(3).is(tok::l_paren) && |
| 6054 | GetLookAheadToken(4).is(tok::identifier) && |
| 6055 | GetLookAheadToken(4).getIdentifierInfo()->isStr("swap")) { |
| 6056 | // HACK: We've got an exception-specification |
| 6057 | // noexcept(noexcept(swap(...))) |
| 6058 | // or |
| 6059 | // noexcept(noexcept(swap(...)) && noexcept(swap(...))) |
| 6060 | // on a 'swap' member function. This is a libstdc++ bug; the lookup |
| 6061 | // for 'swap' will only find the function we're currently declaring, |
| 6062 | // whereas it expects to find a non-member swap through ADL. Turn off |
| 6063 | // delayed parsing to give it a chance to find what it expects. |
| 6064 | Delayed = false; |
| 6065 | } |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 6066 | ESpecType = tryParseExceptionSpecification(Delayed, |
| 6067 | ESpecRange, |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 6068 | DynamicExceptions, |
| 6069 | DynamicExceptionRanges, |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 6070 | NoexceptExpr, |
| 6071 | ExceptionSpecTokens); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6072 | if (ESpecType != EST_None) |
| 6073 | EndLoc = ESpecRange.getEnd(); |
| 6074 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6075 | // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes |
| 6076 | // after the exception-specification. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 6077 | MaybeParseCXX11Attributes(FnAttrs); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6078 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6079 | // Parse trailing-return-type[opt]. |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 6080 | LocalEndLoc = EndLoc; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6081 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) { |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 6082 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 6083 | if (D.getDeclSpec().getTypeSpecType() == TST_auto) |
| 6084 | StartLoc = D.getDeclSpec().getTypeSpecTypeLoc(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 6085 | LocalEndLoc = Tok.getLocation(); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 6086 | SourceRange Range; |
Richard Smith | 700537c | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 6087 | TrailingReturnType = ParseTrailingReturnType(Range); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 6088 | EndLoc = Range.getEnd(); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6089 | } |
| 6090 | } |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6091 | } |
| 6092 | |
Reid Kleckner | 078aea9 | 2016-12-09 17:14:05 +0000 | [diff] [blame] | 6093 | // Collect non-parameter declarations from the prototype if this is a function |
| 6094 | // declaration. They will be moved into the scope of the function. Only do |
| 6095 | // this in C and not C++, where the decls will continue to live in the |
| 6096 | // surrounding context. |
| 6097 | SmallVector<NamedDecl *, 0> DeclsInPrototype; |
| 6098 | if (getCurScope()->getFlags() & Scope::FunctionDeclarationScope && |
| 6099 | !getLangOpts().CPlusPlus) { |
| 6100 | for (Decl *D : getCurScope()->decls()) { |
| 6101 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 6102 | if (!ND || isa<ParmVarDecl>(ND)) |
| 6103 | continue; |
| 6104 | DeclsInPrototype.push_back(ND); |
| 6105 | } |
| 6106 | } |
| 6107 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6108 | // Remember that we parsed a function type, and remember the attributes. |
| 6109 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 6110 | IsAmbiguous, |
| 6111 | LParenLoc, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6112 | ParamInfo.data(), ParamInfo.size(), |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 6113 | EllipsisLoc, RParenLoc, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6114 | DS.getTypeQualifiers(), |
| 6115 | RefQualifierIsLValueRef, |
Douglas Gregor | e248eea | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 6116 | RefQualifierLoc, ConstQualifierLoc, |
| 6117 | VolatileQualifierLoc, |
Hal Finkel | 23a0739 | 2014-10-20 17:32:04 +0000 | [diff] [blame] | 6118 | RestrictQualifierLoc, |
Douglas Gregor | ad69e65 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 6119 | /*MutableLoc=*/SourceLocation(), |
Nathan Wilson | e23a9a4 | 2015-08-26 04:19:36 +0000 | [diff] [blame] | 6120 | ESpecType, ESpecRange, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6121 | DynamicExceptions.data(), |
| 6122 | DynamicExceptionRanges.data(), |
| 6123 | DynamicExceptions.size(), |
| 6124 | NoexceptExpr.isUsable() ? |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6125 | NoexceptExpr.get() : nullptr, |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 6126 | ExceptionSpecTokens, |
Reid Kleckner | 078aea9 | 2016-12-09 17:14:05 +0000 | [diff] [blame] | 6127 | DeclsInPrototype, |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 6128 | StartLoc, LocalEndLoc, D, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6129 | TrailingReturnType), |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6130 | FnAttrs, EndLoc); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6131 | } |
| 6132 | |
Ehsan Akhgari | c07d1e2 | 2015-03-25 00:53:33 +0000 | [diff] [blame] | 6133 | /// ParseRefQualifier - Parses a member function ref-qualifier. Returns |
| 6134 | /// true if a ref-qualifier is found. |
| 6135 | bool Parser::ParseRefQualifier(bool &RefQualifierIsLValueRef, |
| 6136 | SourceLocation &RefQualifierLoc) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 6137 | if (Tok.isOneOf(tok::amp, tok::ampamp)) { |
Ehsan Akhgari | c07d1e2 | 2015-03-25 00:53:33 +0000 | [diff] [blame] | 6138 | Diag(Tok, getLangOpts().CPlusPlus11 ? |
| 6139 | diag::warn_cxx98_compat_ref_qualifier : |
| 6140 | diag::ext_ref_qualifier); |
| 6141 | |
| 6142 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 6143 | RefQualifierLoc = ConsumeToken(); |
| 6144 | return true; |
| 6145 | } |
| 6146 | return false; |
| 6147 | } |
| 6148 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6149 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 6150 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 6151 | /// |
| 6152 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 6153 | /// abstract-declarators. |
| 6154 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6155 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6156 | && Tok.is(tok::identifier) |
| 6157 | && !TryAltiVecVectorToken() |
| 6158 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 6159 | // 6.7.5.3p11. |
| 6160 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 6161 | // Identifier lists follow a really simple grammar: the identifiers can |
| 6162 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 6163 | // identifier lists are really rare in the brave new modern world, and |
| 6164 | // it is very common for someone to typo a type in a non-K&R style |
| 6165 | // list. If we are presented with something like: "void foo(intptr x, |
| 6166 | // float y)", we don't want to start parsing the function declarator as |
| 6167 | // though it is a K&R style declarator just because intptr is an |
| 6168 | // invalid type. |
| 6169 | // |
| 6170 | // To handle this, we check to see if the token after the first |
| 6171 | // identifier is a "," or ")". Only then do we parse it as an |
| 6172 | // identifier list. |
Bruno Cardoso Lopes | 218c874 | 2016-09-13 20:04:35 +0000 | [diff] [blame] | 6173 | && (!Tok.is(tok::eof) && |
| 6174 | (NextToken().is(tok::comma) || NextToken().is(tok::r_paren))); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6175 | } |
| 6176 | |
| 6177 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 6178 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 6179 | /// |
| 6180 | /// After returning, ParamInfo will hold the parsed parameters. |
| 6181 | /// |
| 6182 | /// identifier-list: [C99 6.7.5] |
| 6183 | /// identifier |
| 6184 | /// identifier-list ',' identifier |
| 6185 | /// |
| 6186 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 6187 | Declarator &D, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6188 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6189 | // If there was no identifier specified for the declarator, either we are in |
| 6190 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 6191 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 6192 | // diagnose this. |
| 6193 | if (!D.getIdentifier()) |
| 6194 | Diag(Tok, diag::ext_ident_list_in_param); |
| 6195 | |
| 6196 | // Maintain an efficient lookup of params we have seen so far. |
| 6197 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 6198 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6199 | do { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6200 | // If this isn't an identifier, report the error and skip until ')'. |
| 6201 | if (Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 6202 | Diag(Tok, diag::err_expected) << tok::identifier; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 6203 | SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6204 | // Forget we parsed anything. |
| 6205 | ParamInfo.clear(); |
| 6206 | return; |
| 6207 | } |
| 6208 | |
| 6209 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 6210 | |
| 6211 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 6212 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 6213 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 6214 | |
| 6215 | // Verify that the argument identifier has not already been mentioned. |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6216 | if (!ParamsSoFar.insert(ParmII).second) { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6217 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 6218 | } else { |
| 6219 | // Remember this identifier in ParamInfo. |
| 6220 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 6221 | Tok.getLocation(), |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6222 | nullptr)); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6223 | } |
| 6224 | |
| 6225 | // Eat the identifier. |
| 6226 | ConsumeToken(); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6227 | // The list continues if we see a comma. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6228 | } while (TryConsumeToken(tok::comma)); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6229 | } |
| 6230 | |
| 6231 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 6232 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 6233 | /// identifier list. |
| 6234 | /// |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 6235 | /// D is the declarator being parsed. If FirstArgAttrs is non-null, then the |
| 6236 | /// caller parsed those arguments immediately after the open paren - they should |
| 6237 | /// be considered to be part of the first parameter. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6238 | /// |
| 6239 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 6240 | /// be the location of the ellipsis, if any was parsed. |
| 6241 | /// |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 6242 | /// parameter-type-list: [C99 6.7.5] |
| 6243 | /// parameter-list |
| 6244 | /// parameter-list ',' '...' |
Douglas Gregor | 9bfc2e5 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 6245 | /// [C++] parameter-list '...' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 6246 | /// |
| 6247 | /// parameter-list: [C99 6.7.5] |
| 6248 | /// parameter-declaration |
| 6249 | /// parameter-list ',' parameter-declaration |
| 6250 | /// |
| 6251 | /// parameter-declaration: [C99 6.7.5] |
| 6252 | /// declaration-specifiers declarator |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 6253 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 6254 | /// [C++11] initializer-clause |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 6255 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 6256 | /// declaration-specifiers abstract-declarator[opt] |
| 6257 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 6258 | /// '=' assignment-expression |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 6259 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 6260 | /// [C++11] attribute-specifier-seq parameter-declaration |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 6261 | /// |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6262 | void Parser::ParseParameterDeclarationClause( |
| 6263 | Declarator &D, |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 6264 | ParsedAttributes &FirstArgAttrs, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6265 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6266 | SourceLocation &EllipsisLoc) { |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6267 | do { |
| 6268 | // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq |
| 6269 | // before deciding this was a parameter-declaration-clause. |
| 6270 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6271 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6272 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6273 | // Parse the declaration-specifiers. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 6274 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 6275 | DeclSpec DS(AttrFactory); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6276 | |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 6277 | // Parse any C++11 attributes. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 6278 | MaybeParseCXX11Attributes(DS.getAttributes()); |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 6279 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 6280 | // Skip any Microsoft attributes before a param. |
Chad Rosier | f8a2e70 | 2012-12-20 20:37:53 +0000 | [diff] [blame] | 6281 | MaybeParseMicrosoftAttributes(DS.getAttributes()); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 6282 | |
| 6283 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 6284 | |
| 6285 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 6286 | // 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] | 6287 | // 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] | 6288 | // get rid of a parameter (FirstArgAttrs) and this statement. It might be |
| 6289 | // too much hassle. |
| 6290 | DS.takeAttributesFrom(FirstArgAttrs); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 6291 | |
Chris Lattner | de39c3e | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 6292 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6293 | |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6294 | |
| 6295 | // Parse the declarator. This is "PrototypeContext" or |
| 6296 | // "LambdaExprParameterContext", because we must accept either |
| 6297 | // 'declarator' or 'abstract-declarator' here. |
| 6298 | Declarator ParmDeclarator(DS, |
| 6299 | D.getContext() == Declarator::LambdaExprContext ? |
| 6300 | Declarator::LambdaExprParameterContext : |
| 6301 | Declarator::PrototypeContext); |
| 6302 | ParseDeclarator(ParmDeclarator); |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6303 | |
| 6304 | // Parse GNU attributes, if present. |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6305 | MaybeParseGNUAttributes(ParmDeclarator); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6306 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6307 | // Remember this parsed parameter in ParamInfo. |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6308 | IdentifierInfo *ParmII = ParmDeclarator.getIdentifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6309 | |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6310 | // DefArgToks is used when the parsing of default arguments needs |
| 6311 | // to be delayed. |
Malcolm Parsons | ff0382c | 2016-11-17 17:52:58 +0000 | [diff] [blame] | 6312 | std::unique_ptr<CachedTokens> DefArgToks; |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6313 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6314 | // If no parameter was specified, verify that *something* was specified, |
| 6315 | // otherwise we have a missing type and identifier. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6316 | if (DS.isEmpty() && ParmDeclarator.getIdentifier() == nullptr && |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6317 | ParmDeclarator.getNumTypeObjects() == 0) { |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6318 | // Completely missing, emit error. |
| 6319 | Diag(DSStart, diag::err_missing_param); |
| 6320 | } else { |
| 6321 | // Otherwise, we have something. Add it and let semantic analysis try |
| 6322 | // 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] | 6323 | |
Richard Smith | 36ee9fb | 2014-08-11 23:30:23 +0000 | [diff] [blame] | 6324 | // Last chance to recover from a misplaced ellipsis in an attempted |
| 6325 | // parameter pack declaration. |
| 6326 | if (Tok.is(tok::ellipsis) && |
| 6327 | (NextToken().isNot(tok::r_paren) || |
| 6328 | (!ParmDeclarator.getEllipsisLoc().isValid() && |
| 6329 | !Actions.isUnexpandedParameterPackPermitted())) && |
| 6330 | Actions.containsUnexpandedParameterPacks(ParmDeclarator)) |
| 6331 | DiagnoseMisplacedEllipsisInDeclarator(ConsumeToken(), ParmDeclarator); |
| 6332 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6333 | // Inform the actions module about the parameter declarator, so it gets |
| 6334 | // added to the current scope. |
Richard Smith | 95e1fb0 | 2014-08-27 03:23:12 +0000 | [diff] [blame] | 6335 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator); |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 6336 | // Parse the default argument, if any. We parse the default |
| 6337 | // arguments in all dialects; the semantic analysis in |
| 6338 | // ActOnParamDefaultArgument will reject the default argument in |
| 6339 | // C. |
| 6340 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 6341 | SourceLocation EqualLoc = Tok.getLocation(); |
| 6342 | |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 6343 | // Parse the default argument |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6344 | if (D.getContext() == Declarator::MemberContext) { |
| 6345 | // If we're inside a class definition, cache the tokens |
| 6346 | // corresponding to the default argument. We'll actually parse |
| 6347 | // them when we see the end of the class definition. |
Malcolm Parsons | ff0382c | 2016-11-17 17:52:58 +0000 | [diff] [blame] | 6348 | DefArgToks.reset(new CachedTokens); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6349 | |
David Majnemer | 906ed27 | 2015-01-13 05:28:24 +0000 | [diff] [blame] | 6350 | SourceLocation ArgStartLoc = NextToken().getLocation(); |
Richard Smith | 1fff95c | 2013-09-12 23:28:08 +0000 | [diff] [blame] | 6351 | if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) { |
Malcolm Parsons | ff0382c | 2016-11-17 17:52:58 +0000 | [diff] [blame] | 6352 | DefArgToks.reset(); |
Serge Pavlov | b4b3578 | 2014-07-22 01:54:49 +0000 | [diff] [blame] | 6353 | Actions.ActOnParamDefaultArgumentError(Param, EqualLoc); |
Argyrios Kyrtzidis | 249179c | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 6354 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6355 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
David Majnemer | 906ed27 | 2015-01-13 05:28:24 +0000 | [diff] [blame] | 6356 | ArgStartLoc); |
Argyrios Kyrtzidis | 249179c | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 6357 | } |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 6358 | } else { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6359 | // Consume the '='. |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 6360 | ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6361 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 6362 | // The argument isn't actually potentially evaluated unless it is |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 6363 | // used. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 6364 | EnterExpressionEvaluationContext Eval( |
| 6365 | Actions, |
| 6366 | Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed, |
| 6367 | Param); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 6368 | |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 6369 | ExprResult DefArgResult; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6370 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
Sebastian Redl | 1678d5f | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 6371 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 6372 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 1678d5f | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 6373 | } else |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 6374 | DefArgResult = ParseAssignmentExpression(); |
Kaelyn Takata | b16e632 | 2014-11-20 22:06:40 +0000 | [diff] [blame] | 6375 | DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6376 | if (DefArgResult.isInvalid()) { |
Serge Pavlov | b4b3578 | 2014-07-22 01:54:49 +0000 | [diff] [blame] | 6377 | Actions.ActOnParamDefaultArgumentError(Param, EqualLoc); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 6378 | SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6379 | } else { |
| 6380 | // Inform the actions module about the default argument |
| 6381 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6382 | DefArgResult.get()); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6383 | } |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 6384 | } |
| 6385 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6386 | |
| 6387 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6388 | ParmDeclarator.getIdentifierLoc(), |
Malcolm Parsons | ff0382c | 2016-11-17 17:52:58 +0000 | [diff] [blame] | 6389 | Param, std::move(DefArgToks))); |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6390 | } |
| 6391 | |
Richard Smith | 36ee9fb | 2014-08-11 23:30:23 +0000 | [diff] [blame] | 6392 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) { |
| 6393 | if (!getLangOpts().CPlusPlus) { |
| 6394 | // We have ellipsis without a preceding ',', which is ill-formed |
| 6395 | // in C. Complain and provide the fix. |
| 6396 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
| 6397 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
| 6398 | } else if (ParmDeclarator.getEllipsisLoc().isValid() || |
| 6399 | Actions.containsUnexpandedParameterPacks(ParmDeclarator)) { |
| 6400 | // It looks like this was supposed to be a parameter pack. Warn and |
| 6401 | // point out where the ellipsis should have gone. |
| 6402 | SourceLocation ParmEllipsis = ParmDeclarator.getEllipsisLoc(); |
| 6403 | Diag(EllipsisLoc, diag::warn_misplaced_ellipsis_vararg) |
| 6404 | << ParmEllipsis.isValid() << ParmEllipsis; |
| 6405 | if (ParmEllipsis.isValid()) { |
| 6406 | Diag(ParmEllipsis, |
| 6407 | diag::note_misplaced_ellipsis_vararg_existing_ellipsis); |
| 6408 | } else { |
| 6409 | Diag(ParmDeclarator.getIdentifierLoc(), |
| 6410 | diag::note_misplaced_ellipsis_vararg_add_ellipsis) |
| 6411 | << FixItHint::CreateInsertion(ParmDeclarator.getIdentifierLoc(), |
| 6412 | "...") |
| 6413 | << !ParmDeclarator.hasName(); |
| 6414 | } |
| 6415 | Diag(EllipsisLoc, diag::note_misplaced_ellipsis_vararg_add_comma) |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6416 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Richard Smith | 36ee9fb | 2014-08-11 23:30:23 +0000 | [diff] [blame] | 6417 | } |
| 6418 | |
| 6419 | // We can't have any more parameters after an ellipsis. |
Douglas Gregor | 9bfc2e5 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 6420 | break; |
| 6421 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6422 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6423 | // If the next token is a comma, consume it and keep reading arguments. |
| 6424 | } while (TryConsumeToken(tok::comma)); |
Chris Lattner | 6c940e6 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 6425 | } |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 6426 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6427 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 6428 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 6429 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 6430 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 6431 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6432 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 6433 | /// attribute-specifier-seq[opt] |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6434 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6435 | if (CheckProhibitedCXX11Attribute()) |
| 6436 | return; |
| 6437 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6438 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 6439 | T.consumeOpen(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6440 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6441 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 6442 | // This code does a fast path to handle some of the most obvious cases. |
| 6443 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6444 | T.consumeClose(); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 6445 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 6446 | MaybeParseCXX11Attributes(attrs); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 6447 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6448 | // Remember that we parsed the empty array type. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6449 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, nullptr, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6450 | T.getOpenLocation(), |
| 6451 | T.getCloseLocation()), |
| 6452 | attrs, T.getCloseLocation()); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6453 | return; |
| 6454 | } else if (Tok.getKind() == tok::numeric_constant && |
| 6455 | GetLookAheadToken(1).is(tok::r_square)) { |
| 6456 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 6457 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6458 | ConsumeToken(); |
| 6459 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6460 | T.consumeClose(); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 6461 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 6462 | MaybeParseCXX11Attributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6463 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6464 | // Remember that we parsed a array type, and remember its features. |
Nikola Smiljanic | c531dcc | 2013-01-11 08:33:05 +0000 | [diff] [blame] | 6465 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6466 | ExprRes.get(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6467 | T.getOpenLocation(), |
| 6468 | T.getCloseLocation()), |
| 6469 | attrs, T.getCloseLocation()); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6470 | return; |
Benjamin Kramer | 72dae62 | 2016-02-18 15:30:24 +0000 | [diff] [blame] | 6471 | } else if (Tok.getKind() == tok::code_completion) { |
| 6472 | Actions.CodeCompleteBracketDeclarator(getCurScope()); |
| 6473 | return cutOffParsing(); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6474 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6475 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6476 | // If valid, this location is the position where we read the 'static' keyword. |
| 6477 | SourceLocation StaticLoc; |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6478 | TryConsumeToken(tok::kw_static, StaticLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6479 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6480 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 6481 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 6482 | DeclSpec DS(AttrFactory); |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 6483 | ParseTypeQualifierListOpt(DS, AR_CXX11AttributesParsed); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6484 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6485 | // If we haven't already read 'static', check to see if there is one after the |
| 6486 | // type-qualifier-list. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6487 | if (!StaticLoc.isValid()) |
| 6488 | TryConsumeToken(tok::kw_static, StaticLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6489 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6490 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6491 | bool isStar = false; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6492 | ExprResult NumElements; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6493 | |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 6494 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 6495 | // 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] | 6496 | // the token after the star is a ']'. Since stars in arrays are |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 6497 | // infrequent, use of lookahead is not costly here. |
| 6498 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | c439f0d | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 6499 | ConsumeToken(); // Eat the '*'. |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 6500 | |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 6501 | if (StaticLoc.isValid()) { |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 6502 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 6503 | StaticLoc = SourceLocation(); // Drop the static. |
| 6504 | } |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 6505 | isStar = true; |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 6506 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6507 | // Note, in C89, this production uses the constant-expr production instead |
| 6508 | // of assignment-expr. The only difference is that assignment-expr allows |
| 6509 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 6510 | // 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] | 6511 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6512 | // Parse the constant-expression or assignment-expression now (depending |
| 6513 | // on dialect). |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6514 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6515 | NumElements = ParseConstantExpression(); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 6516 | } else { |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 6517 | EnterExpressionEvaluationContext Unevaluated( |
| 6518 | Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Kaelyn Takata | 1586782 | 2014-11-21 18:48:04 +0000 | [diff] [blame] | 6519 | NumElements = |
| 6520 | Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression()); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 6521 | } |
David Majnemer | f9834d5 | 2014-08-08 07:21:18 +0000 | [diff] [blame] | 6522 | } else { |
| 6523 | if (StaticLoc.isValid()) { |
| 6524 | Diag(StaticLoc, diag::err_unspecified_size_with_static); |
| 6525 | StaticLoc = SourceLocation(); // Drop the static. |
| 6526 | } |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 6527 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6528 | |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 6529 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 6530 | if (NumElements.isInvalid()) { |
Chris Lattner | cd2a8c5 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 6531 | D.setInvalidType(true); |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 6532 | // If the expression was invalid, skip it. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 6533 | SkipUntil(tok::r_square, StopAtSemi); |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 6534 | return; |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6535 | } |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 6536 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6537 | T.consumeClose(); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 6538 | |
Jordan Rose | 303e2f1 | 2016-11-10 23:28:17 +0000 | [diff] [blame] | 6539 | MaybeParseCXX11Attributes(DS.getAttributes()); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 6540 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6541 | // Remember that we parsed a array type, and remember its features. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 6542 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 6543 | StaticLoc.isValid(), isStar, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6544 | NumElements.get(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6545 | T.getOpenLocation(), |
| 6546 | T.getCloseLocation()), |
Jordan Rose | 303e2f1 | 2016-11-10 23:28:17 +0000 | [diff] [blame] | 6547 | DS.getAttributes(), T.getCloseLocation()); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6548 | } |
| 6549 | |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 6550 | /// Diagnose brackets before an identifier. |
| 6551 | void Parser::ParseMisplacedBracketDeclarator(Declarator &D) { |
| 6552 | assert(Tok.is(tok::l_square) && "Missing opening bracket"); |
| 6553 | assert(!D.mayOmitIdentifier() && "Declarator cannot omit identifier"); |
| 6554 | |
| 6555 | SourceLocation StartBracketLoc = Tok.getLocation(); |
| 6556 | Declarator TempDeclarator(D.getDeclSpec(), D.getContext()); |
| 6557 | |
| 6558 | while (Tok.is(tok::l_square)) { |
| 6559 | ParseBracketDeclarator(TempDeclarator); |
| 6560 | } |
| 6561 | |
| 6562 | // Stuff the location of the start of the brackets into the Declarator. |
| 6563 | // The diagnostics from ParseDirectDeclarator will make more sense if |
| 6564 | // they use this location instead. |
| 6565 | if (Tok.is(tok::semi)) |
| 6566 | D.getName().EndLocation = StartBracketLoc; |
| 6567 | |
| 6568 | SourceLocation SuggestParenLoc = Tok.getLocation(); |
| 6569 | |
| 6570 | // Now that the brackets are removed, try parsing the declarator again. |
| 6571 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
| 6572 | |
| 6573 | // Something went wrong parsing the brackets, in which case, |
| 6574 | // ParseBracketDeclarator has emitted an error, and we don't need to emit |
| 6575 | // one here. |
| 6576 | if (TempDeclarator.getNumTypeObjects() == 0) |
| 6577 | return; |
| 6578 | |
| 6579 | // Determine if parens will need to be suggested in the diagnostic. |
| 6580 | bool NeedParens = false; |
| 6581 | if (D.getNumTypeObjects() != 0) { |
| 6582 | switch (D.getTypeObject(D.getNumTypeObjects() - 1).Kind) { |
| 6583 | case DeclaratorChunk::Pointer: |
| 6584 | case DeclaratorChunk::Reference: |
| 6585 | case DeclaratorChunk::BlockPointer: |
| 6586 | case DeclaratorChunk::MemberPointer: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 6587 | case DeclaratorChunk::Pipe: |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 6588 | NeedParens = true; |
| 6589 | break; |
| 6590 | case DeclaratorChunk::Array: |
| 6591 | case DeclaratorChunk::Function: |
| 6592 | case DeclaratorChunk::Paren: |
| 6593 | break; |
| 6594 | } |
| 6595 | } |
| 6596 | |
| 6597 | if (NeedParens) { |
| 6598 | // Create a DeclaratorChunk for the inserted parens. |
| 6599 | ParsedAttributes attrs(AttrFactory); |
| 6600 | SourceLocation EndLoc = PP.getLocForEndOfToken(D.getLocEnd()); |
| 6601 | D.AddTypeInfo(DeclaratorChunk::getParen(SuggestParenLoc, EndLoc), attrs, |
| 6602 | SourceLocation()); |
| 6603 | } |
| 6604 | |
| 6605 | // Adding back the bracket info to the end of the Declarator. |
| 6606 | for (unsigned i = 0, e = TempDeclarator.getNumTypeObjects(); i < e; ++i) { |
| 6607 | const DeclaratorChunk &Chunk = TempDeclarator.getTypeObject(i); |
| 6608 | ParsedAttributes attrs(AttrFactory); |
| 6609 | attrs.set(Chunk.Common.AttrList); |
| 6610 | D.AddTypeInfo(Chunk, attrs, SourceLocation()); |
| 6611 | } |
| 6612 | |
| 6613 | // The missing identifier would have been diagnosed in ParseDirectDeclarator. |
| 6614 | // If parentheses are required, always suggest them. |
| 6615 | if (!D.getIdentifier() && !NeedParens) |
| 6616 | return; |
| 6617 | |
| 6618 | SourceLocation EndBracketLoc = TempDeclarator.getLocEnd(); |
| 6619 | |
| 6620 | // Generate the move bracket error message. |
| 6621 | SourceRange BracketRange(StartBracketLoc, EndBracketLoc); |
| 6622 | SourceLocation EndLoc = PP.getLocForEndOfToken(D.getLocEnd()); |
| 6623 | |
| 6624 | if (NeedParens) { |
| 6625 | Diag(EndLoc, diag::err_brackets_go_after_unqualified_id) |
| 6626 | << getLangOpts().CPlusPlus |
| 6627 | << FixItHint::CreateInsertion(SuggestParenLoc, "(") |
| 6628 | << FixItHint::CreateInsertion(EndLoc, ")") |
| 6629 | << FixItHint::CreateInsertionFromRange( |
| 6630 | EndLoc, CharSourceRange(BracketRange, true)) |
| 6631 | << FixItHint::CreateRemoval(BracketRange); |
| 6632 | } else { |
| 6633 | Diag(EndLoc, diag::err_brackets_go_after_unqualified_id) |
| 6634 | << getLangOpts().CPlusPlus |
| 6635 | << FixItHint::CreateInsertionFromRange( |
| 6636 | EndLoc, CharSourceRange(BracketRange, true)) |
| 6637 | << FixItHint::CreateRemoval(BracketRange); |
| 6638 | } |
| 6639 | } |
| 6640 | |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 6641 | /// [GNU] typeof-specifier: |
| 6642 | /// typeof ( expressions ) |
| 6643 | /// typeof ( type-name ) |
| 6644 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 6645 | /// |
| 6646 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 6647 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6648 | Token OpTok = Tok; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 6649 | SourceLocation StartLoc = ConsumeToken(); |
| 6650 | |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 6651 | const bool hasParens = Tok.is(tok::l_paren); |
| 6652 | |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 6653 | EnterExpressionEvaluationContext Unevaluated( |
| 6654 | Actions, Sema::ExpressionEvaluationContext::Unevaluated, |
| 6655 | Sema::ReuseLambdaContextDecl); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 6656 | |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6657 | bool isCastExpr; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6658 | ParsedType CastTy; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6659 | SourceRange CastRange; |
Kaelyn Takata | 91126074 | 2014-12-19 01:28:40 +0000 | [diff] [blame] | 6660 | ExprResult Operand = Actions.CorrectDelayedTyposInExpr( |
| 6661 | ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, CastTy, CastRange)); |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 6662 | if (hasParens) |
| 6663 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6664 | |
| 6665 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 6666 | // FIXME: Not accurate, the range gets one token more than it should. |
| 6667 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6668 | else |
| 6669 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6670 | |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6671 | if (isCastExpr) { |
| 6672 | if (!CastTy) { |
| 6673 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 6674 | return; |
Douglas Gregor | 220cac5 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 6675 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 6676 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6677 | const char *PrevSpec = nullptr; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 6678 | unsigned DiagID; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6679 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 6680 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6681 | DiagID, CastTy, |
| 6682 | Actions.getASTContext().getPrintingPolicy())) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 6683 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6684 | return; |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 6685 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 6686 | |
Hiroshi Inoue | 533777f | 2017-07-02 06:12:49 +0000 | [diff] [blame] | 6687 | // If we get here, the operand to the typeof was an expression. |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 6688 | if (Operand.isInvalid()) { |
| 6689 | DS.SetTypeSpecError(); |
Steve Naroff | 4bd2f71 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 6690 | return; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 6691 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 6692 | |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 6693 | // We might need to transform the operand if it is potentially evaluated. |
| 6694 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 6695 | if (Operand.isInvalid()) { |
| 6696 | DS.SetTypeSpecError(); |
| 6697 | return; |
| 6698 | } |
| 6699 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6700 | const char *PrevSpec = nullptr; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 6701 | unsigned DiagID; |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 6702 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 6703 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6704 | DiagID, Operand.get(), |
| 6705 | Actions.getASTContext().getPrintingPolicy())) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 6706 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 6707 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6708 | |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 6709 | /// [C11] atomic-specifier: |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6710 | /// _Atomic ( type-name ) |
| 6711 | /// |
| 6712 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 6713 | assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) && |
| 6714 | "Not an atomic specifier"); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6715 | |
| 6716 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6717 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 6718 | if (T.consumeOpen()) |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6719 | return; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6720 | |
| 6721 | TypeResult Result = ParseTypeName(); |
| 6722 | if (Result.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 6723 | SkipUntil(tok::r_paren, StopAtSemi); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6724 | return; |
| 6725 | } |
| 6726 | |
| 6727 | // Match the ')' |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6728 | T.consumeClose(); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6729 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6730 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6731 | return; |
| 6732 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6733 | DS.setTypeofParensRange(T.getRange()); |
| 6734 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6735 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6736 | const char *PrevSpec = nullptr; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6737 | unsigned DiagID; |
| 6738 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6739 | DiagID, Result.get(), |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6740 | Actions.getASTContext().getPrintingPolicy())) |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6741 | Diag(StartLoc, DiagID) << PrevSpec; |
| 6742 | } |
| 6743 | |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6744 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 6745 | /// from TryAltiVecVectorToken. |
| 6746 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 6747 | Token Next = NextToken(); |
| 6748 | switch (Next.getKind()) { |
| 6749 | default: return false; |
| 6750 | case tok::kw_short: |
| 6751 | case tok::kw_long: |
| 6752 | case tok::kw_signed: |
| 6753 | case tok::kw_unsigned: |
| 6754 | case tok::kw_void: |
| 6755 | case tok::kw_char: |
| 6756 | case tok::kw_int: |
| 6757 | case tok::kw_float: |
| 6758 | case tok::kw_double: |
| 6759 | case tok::kw_bool: |
Bill Seurer | cf2c96b | 2015-01-12 19:35:51 +0000 | [diff] [blame] | 6760 | case tok::kw___bool: |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6761 | case tok::kw___pixel: |
| 6762 | Tok.setKind(tok::kw___vector); |
| 6763 | return true; |
| 6764 | case tok::identifier: |
| 6765 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 6766 | Tok.setKind(tok::kw___vector); |
| 6767 | return true; |
| 6768 | } |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 6769 | if (Next.getIdentifierInfo() == Ident_bool) { |
| 6770 | Tok.setKind(tok::kw___vector); |
| 6771 | return true; |
| 6772 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6773 | return false; |
| 6774 | } |
| 6775 | } |
| 6776 | |
| 6777 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 6778 | const char *&PrevSpec, unsigned &DiagID, |
| 6779 | bool &isInvalid) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6780 | const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy(); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6781 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 6782 | Token Next = NextToken(); |
| 6783 | switch (Next.getKind()) { |
| 6784 | case tok::kw_short: |
| 6785 | case tok::kw_long: |
| 6786 | case tok::kw_signed: |
| 6787 | case tok::kw_unsigned: |
| 6788 | case tok::kw_void: |
| 6789 | case tok::kw_char: |
| 6790 | case tok::kw_int: |
| 6791 | case tok::kw_float: |
| 6792 | case tok::kw_double: |
| 6793 | case tok::kw_bool: |
Bill Seurer | cf2c96b | 2015-01-12 19:35:51 +0000 | [diff] [blame] | 6794 | case tok::kw___bool: |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6795 | case tok::kw___pixel: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6796 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6797 | return true; |
| 6798 | case tok::identifier: |
| 6799 | if (Next.getIdentifierInfo() == Ident_pixel) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6800 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6801 | return true; |
| 6802 | } |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 6803 | if (Next.getIdentifierInfo() == Ident_bool) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6804 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy); |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 6805 | return true; |
| 6806 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6807 | break; |
| 6808 | default: |
| 6809 | break; |
| 6810 | } |
Douglas Gregor | 9938e3b | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 6811 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6812 | DS.isTypeAltiVecVector()) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6813 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6814 | return true; |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 6815 | } else if ((Tok.getIdentifierInfo() == Ident_bool) && |
| 6816 | DS.isTypeAltiVecVector()) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6817 | isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy); |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 6818 | return true; |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6819 | } |
| 6820 | return false; |
| 6821 | } |