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(); |
Manman Ren | ccf25bb | 2016-06-28 20:55:30 +0000 | [diff] [blame] | 915 | // Canonicalize platform name from "macosx" to "macos". |
| 916 | if (Platform->Ident && Platform->Ident->getName() == "macosx") |
| 917 | Platform->Ident = PP.getIdentifierInfo("macos"); |
| 918 | // Canonicalize platform name from "macosx_app_extension" to |
| 919 | // "macos_app_extension". |
| 920 | if (Platform->Ident && Platform->Ident->getName() == "macosx_app_extension") |
| 921 | Platform->Ident = PP.getIdentifierInfo("macos_app_extension"); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 922 | |
| 923 | // Parse the ',' following the platform name. |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 924 | if (ExpectAndConsume(tok::comma)) { |
| 925 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 926 | return; |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 927 | } |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 928 | |
| 929 | // If we haven't grabbed the pointers for the identifiers |
| 930 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 931 | if (!Ident_introduced) { |
| 932 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 933 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 934 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 935 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 936 | Ident_message = PP.getIdentifierInfo("message"); |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 937 | Ident_strict = PP.getIdentifierInfo("strict"); |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 938 | Ident_replacement = PP.getIdentifierInfo("replacement"); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 941 | // Parse the optional "strict", the optional "replacement" and the set of |
Manman Ren | b636b90 | 2016-02-17 22:05:48 +0000 | [diff] [blame] | 942 | // introductions/deprecations/removals. |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 943 | SourceLocation UnavailableLoc, StrictLoc; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 944 | do { |
| 945 | if (Tok.isNot(tok::identifier)) { |
| 946 | Diag(Tok, diag::err_availability_expected_change); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 947 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 948 | return; |
| 949 | } |
| 950 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 951 | SourceLocation KeywordLoc = ConsumeToken(); |
| 952 | |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 953 | if (Keyword == Ident_strict) { |
| 954 | if (StrictLoc.isValid()) { |
Manman Ren | b636b90 | 2016-02-17 22:05:48 +0000 | [diff] [blame] | 955 | Diag(KeywordLoc, diag::err_availability_redundant) |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 956 | << Keyword << SourceRange(StrictLoc); |
Manman Ren | b636b90 | 2016-02-17 22:05:48 +0000 | [diff] [blame] | 957 | } |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 958 | StrictLoc = KeywordLoc; |
Manman Ren | b636b90 | 2016-02-17 22:05:48 +0000 | [diff] [blame] | 959 | continue; |
| 960 | } |
| 961 | |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 962 | if (Keyword == Ident_unavailable) { |
| 963 | if (UnavailableLoc.isValid()) { |
| 964 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 965 | << Keyword << SourceRange(UnavailableLoc); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 966 | } |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 967 | UnavailableLoc = KeywordLoc; |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 968 | continue; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 971 | if (Tok.isNot(tok::equal)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 972 | Diag(Tok, diag::err_expected_after) << Keyword << tok::equal; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 973 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 974 | return; |
| 975 | } |
| 976 | ConsumeToken(); |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 977 | if (Keyword == Ident_message || Keyword == Ident_replacement) { |
David Majnemer | 2e49830 | 2014-07-18 05:43:12 +0000 | [diff] [blame] | 978 | if (Tok.isNot(tok::string_literal)) { |
Andy Gibbs | a8df57a | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 979 | Diag(Tok, diag::err_expected_string_literal) |
| 980 | << /*Source='availability attribute'*/2; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 981 | SkipUntil(tok::r_paren, StopAtSemi); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 982 | return; |
| 983 | } |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 984 | if (Keyword == Ident_message) |
| 985 | MessageExpr = ParseStringLiteralExpression(); |
| 986 | else |
| 987 | ReplacementExpr = ParseStringLiteralExpression(); |
David Majnemer | 2e49830 | 2014-07-18 05:43:12 +0000 | [diff] [blame] | 988 | // Also reject wide string literals. |
| 989 | if (StringLiteral *MessageStringLiteral = |
| 990 | cast_or_null<StringLiteral>(MessageExpr.get())) { |
| 991 | if (MessageStringLiteral->getCharByteWidth() != 1) { |
| 992 | Diag(MessageStringLiteral->getSourceRange().getBegin(), |
| 993 | diag::err_expected_string_literal) |
| 994 | << /*Source='availability attribute'*/ 2; |
| 995 | SkipUntil(tok::r_paren, StopAtSemi); |
| 996 | return; |
| 997 | } |
| 998 | } |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 999 | if (Keyword == Ident_message) |
| 1000 | break; |
| 1001 | else |
| 1002 | continue; |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1003 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1004 | |
Fariborz Jahanian | 5a29e6a | 2014-11-05 23:58:55 +0000 | [diff] [blame] | 1005 | // Special handling of 'NA' only when applied to introduced or |
| 1006 | // deprecated. |
| 1007 | if ((Keyword == Ident_introduced || Keyword == Ident_deprecated) && |
| 1008 | Tok.is(tok::identifier)) { |
| 1009 | IdentifierInfo *NA = Tok.getIdentifierInfo(); |
| 1010 | if (NA->getName() == "NA") { |
| 1011 | ConsumeToken(); |
| 1012 | if (Keyword == Ident_introduced) |
| 1013 | UnavailableLoc = KeywordLoc; |
| 1014 | continue; |
| 1015 | } |
| 1016 | } |
| 1017 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1018 | SourceRange VersionRange; |
| 1019 | VersionTuple Version = ParseVersionTuple(VersionRange); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1020 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1021 | if (Version.empty()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1022 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1023 | return; |
| 1024 | } |
| 1025 | |
| 1026 | unsigned Index; |
| 1027 | if (Keyword == Ident_introduced) |
| 1028 | Index = Introduced; |
| 1029 | else if (Keyword == Ident_deprecated) |
| 1030 | Index = Deprecated; |
| 1031 | else if (Keyword == Ident_obsoleted) |
| 1032 | Index = Obsoleted; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1033 | else |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1034 | Index = Unknown; |
| 1035 | |
| 1036 | if (Index < Unknown) { |
| 1037 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 1038 | Diag(KeywordLoc, diag::err_availability_redundant) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1039 | << Keyword |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1040 | << SourceRange(Changes[Index].KeywordLoc, |
| 1041 | Changes[Index].VersionRange.getEnd()); |
| 1042 | } |
| 1043 | |
| 1044 | Changes[Index].KeywordLoc = KeywordLoc; |
| 1045 | Changes[Index].Version = Version; |
| 1046 | Changes[Index].VersionRange = VersionRange; |
| 1047 | } else { |
| 1048 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 1049 | << Keyword << VersionRange; |
| 1050 | } |
| 1051 | |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 1052 | } while (TryConsumeToken(tok::comma)); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1053 | |
| 1054 | // Closing ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1055 | if (T.consumeClose()) |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1056 | return; |
| 1057 | |
| 1058 | if (endLoc) |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1059 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1060 | |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1061 | // The 'unavailable' availability cannot be combined with any other |
| 1062 | // availability changes. Make sure that hasn't happened. |
| 1063 | if (UnavailableLoc.isValid()) { |
| 1064 | bool Complained = false; |
| 1065 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 1066 | if (Changes[Index].KeywordLoc.isValid()) { |
| 1067 | if (!Complained) { |
| 1068 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 1069 | << SourceRange(Changes[Index].KeywordLoc, |
| 1070 | Changes[Index].VersionRange.getEnd()); |
| 1071 | Complained = true; |
| 1072 | } |
| 1073 | |
| 1074 | // Clear out the availability. |
| 1075 | Changes[Index] = AvailabilityChange(); |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1080 | // Record this attribute |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1081 | attrs.addNew(&Availability, |
| 1082 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1083 | ScopeName, ScopeLoc, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1084 | Platform, |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1085 | Changes[Introduced], |
| 1086 | Changes[Deprecated], |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1087 | Changes[Obsoleted], |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1088 | UnavailableLoc, MessageExpr.get(), |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 1089 | Syntax, StrictLoc, ReplacementExpr.get()); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 1092 | /// \brief Parse the contents of the "external_source_symbol" attribute. |
| 1093 | /// |
| 1094 | /// external-source-symbol-attribute: |
| 1095 | /// 'external_source_symbol' '(' keyword-arg-list ')' |
| 1096 | /// |
| 1097 | /// keyword-arg-list: |
| 1098 | /// keyword-arg |
| 1099 | /// keyword-arg ',' keyword-arg-list |
| 1100 | /// |
| 1101 | /// keyword-arg: |
| 1102 | /// 'language' '=' <string> |
| 1103 | /// 'defined_in' '=' <string> |
| 1104 | /// 'generated_declaration' |
| 1105 | void Parser::ParseExternalSourceSymbolAttribute( |
| 1106 | IdentifierInfo &ExternalSourceSymbol, SourceLocation Loc, |
| 1107 | ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName, |
| 1108 | SourceLocation ScopeLoc, AttributeList::Syntax Syntax) { |
| 1109 | // Opening '('. |
| 1110 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1111 | if (T.expectAndConsume()) |
| 1112 | return; |
| 1113 | |
| 1114 | // Initialize the pointers for the keyword identifiers when required. |
| 1115 | if (!Ident_language) { |
| 1116 | Ident_language = PP.getIdentifierInfo("language"); |
| 1117 | Ident_defined_in = PP.getIdentifierInfo("defined_in"); |
| 1118 | Ident_generated_declaration = PP.getIdentifierInfo("generated_declaration"); |
| 1119 | } |
| 1120 | |
| 1121 | ExprResult Language; |
| 1122 | bool HasLanguage = false; |
| 1123 | ExprResult DefinedInExpr; |
| 1124 | bool HasDefinedIn = false; |
| 1125 | IdentifierLoc *GeneratedDeclaration = nullptr; |
| 1126 | |
| 1127 | // Parse the language/defined_in/generated_declaration keywords |
| 1128 | do { |
| 1129 | if (Tok.isNot(tok::identifier)) { |
| 1130 | Diag(Tok, diag::err_external_source_symbol_expected_keyword); |
| 1131 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1132 | return; |
| 1133 | } |
| 1134 | |
| 1135 | SourceLocation KeywordLoc = Tok.getLocation(); |
| 1136 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 1137 | if (Keyword == Ident_generated_declaration) { |
| 1138 | if (GeneratedDeclaration) { |
| 1139 | Diag(Tok, diag::err_external_source_symbol_duplicate_clause) << Keyword; |
| 1140 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1141 | return; |
| 1142 | } |
| 1143 | GeneratedDeclaration = ParseIdentifierLoc(); |
| 1144 | continue; |
| 1145 | } |
| 1146 | |
| 1147 | if (Keyword != Ident_language && Keyword != Ident_defined_in) { |
| 1148 | Diag(Tok, diag::err_external_source_symbol_expected_keyword); |
| 1149 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1150 | return; |
| 1151 | } |
| 1152 | |
| 1153 | ConsumeToken(); |
| 1154 | if (ExpectAndConsume(tok::equal, diag::err_expected_after, |
| 1155 | Keyword->getName())) { |
| 1156 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1157 | return; |
| 1158 | } |
| 1159 | |
| 1160 | bool HadLanguage = HasLanguage, HadDefinedIn = HasDefinedIn; |
| 1161 | if (Keyword == Ident_language) |
| 1162 | HasLanguage = true; |
| 1163 | else |
| 1164 | HasDefinedIn = true; |
| 1165 | |
| 1166 | if (Tok.isNot(tok::string_literal)) { |
| 1167 | Diag(Tok, diag::err_expected_string_literal) |
| 1168 | << /*Source='external_source_symbol attribute'*/ 3 |
| 1169 | << /*language | source container*/ (Keyword != Ident_language); |
| 1170 | SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch); |
| 1171 | continue; |
| 1172 | } |
| 1173 | if (Keyword == Ident_language) { |
| 1174 | if (HadLanguage) { |
| 1175 | Diag(KeywordLoc, diag::err_external_source_symbol_duplicate_clause) |
| 1176 | << Keyword; |
| 1177 | ParseStringLiteralExpression(); |
| 1178 | continue; |
| 1179 | } |
| 1180 | Language = ParseStringLiteralExpression(); |
| 1181 | } else { |
| 1182 | assert(Keyword == Ident_defined_in && "Invalid clause keyword!"); |
| 1183 | if (HadDefinedIn) { |
| 1184 | Diag(KeywordLoc, diag::err_external_source_symbol_duplicate_clause) |
| 1185 | << Keyword; |
| 1186 | ParseStringLiteralExpression(); |
| 1187 | continue; |
| 1188 | } |
| 1189 | DefinedInExpr = ParseStringLiteralExpression(); |
| 1190 | } |
| 1191 | } while (TryConsumeToken(tok::comma)); |
| 1192 | |
| 1193 | // Closing ')'. |
| 1194 | if (T.consumeClose()) |
| 1195 | return; |
| 1196 | if (EndLoc) |
| 1197 | *EndLoc = T.getCloseLocation(); |
| 1198 | |
| 1199 | ArgsUnion Args[] = {Language.get(), DefinedInExpr.get(), |
| 1200 | GeneratedDeclaration}; |
| 1201 | Attrs.addNew(&ExternalSourceSymbol, SourceRange(Loc, T.getCloseLocation()), |
| 1202 | ScopeName, ScopeLoc, Args, llvm::array_lengthof(Args), Syntax); |
| 1203 | } |
| 1204 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1205 | /// \brief Parse the contents of the "objc_bridge_related" attribute. |
| 1206 | /// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')' |
| 1207 | /// related_class: |
| 1208 | /// Identifier |
| 1209 | /// |
| 1210 | /// opt-class_method: |
| 1211 | /// Identifier: | <empty> |
| 1212 | /// |
| 1213 | /// opt-instance_method: |
| 1214 | /// Identifier | <empty> |
| 1215 | /// |
| 1216 | void Parser::ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated, |
| 1217 | SourceLocation ObjCBridgeRelatedLoc, |
| 1218 | ParsedAttributes &attrs, |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1219 | SourceLocation *endLoc, |
| 1220 | IdentifierInfo *ScopeName, |
| 1221 | SourceLocation ScopeLoc, |
| 1222 | AttributeList::Syntax Syntax) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1223 | // Opening '('. |
| 1224 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1225 | if (T.consumeOpen()) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1226 | Diag(Tok, diag::err_expected) << tok::l_paren; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1227 | return; |
| 1228 | } |
| 1229 | |
| 1230 | // Parse the related class name. |
| 1231 | if (Tok.isNot(tok::identifier)) { |
| 1232 | Diag(Tok, diag::err_objcbridge_related_expected_related_class); |
| 1233 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1234 | return; |
| 1235 | } |
| 1236 | IdentifierLoc *RelatedClass = ParseIdentifierLoc(); |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 1237 | if (ExpectAndConsume(tok::comma)) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1238 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1239 | return; |
| 1240 | } |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1241 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1242 | // Parse optional class method name. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1243 | IdentifierLoc *ClassMethod = nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1244 | if (Tok.is(tok::identifier)) { |
| 1245 | ClassMethod = ParseIdentifierLoc(); |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1246 | if (!TryConsumeToken(tok::colon)) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1247 | Diag(Tok, diag::err_objcbridge_related_selector_name); |
| 1248 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1249 | return; |
| 1250 | } |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1251 | } |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1252 | if (!TryConsumeToken(tok::comma)) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1253 | if (Tok.is(tok::colon)) |
| 1254 | Diag(Tok, diag::err_objcbridge_related_selector_name); |
| 1255 | else |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1256 | Diag(Tok, diag::err_expected) << tok::comma; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1257 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1258 | return; |
| 1259 | } |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1260 | |
| 1261 | // Parse optional instance method name. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1262 | IdentifierLoc *InstanceMethod = nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1263 | if (Tok.is(tok::identifier)) |
| 1264 | InstanceMethod = ParseIdentifierLoc(); |
| 1265 | else if (Tok.isNot(tok::r_paren)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1266 | Diag(Tok, diag::err_expected) << tok::r_paren; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1267 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1268 | return; |
| 1269 | } |
| 1270 | |
| 1271 | // Closing ')'. |
| 1272 | if (T.consumeClose()) |
| 1273 | return; |
| 1274 | |
| 1275 | if (endLoc) |
| 1276 | *endLoc = T.getCloseLocation(); |
| 1277 | |
| 1278 | // Record this attribute |
| 1279 | attrs.addNew(&ObjCBridgeRelated, |
| 1280 | SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1281 | ScopeName, ScopeLoc, |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1282 | RelatedClass, |
| 1283 | ClassMethod, |
| 1284 | InstanceMethod, |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1285 | Syntax); |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1286 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1287 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1288 | // Late Parsed Attributes: |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1289 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 1290 | |
| 1291 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 1292 | |
| 1293 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 1294 | Self->ParseLexedAttributes(*Class); |
| 1295 | } |
| 1296 | |
| 1297 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1298 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 1302 | /// scope appropriately. |
| 1303 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 1304 | // Deal with templates |
| 1305 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 1306 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 1307 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 1308 | HasTemplateScope); |
| 1309 | if (HasTemplateScope) |
| 1310 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 1311 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1312 | // Set or update the scope flags. |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1313 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1314 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1315 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 1316 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 1317 | |
DeLesley Hutchins | 6f86004 | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1318 | // Enter the scope of nested classes |
| 1319 | if (!AlreadyHasClassScope) |
| 1320 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 1321 | Class.TagOrTemplate); |
Benjamin Kramer | 1d373c6 | 2012-05-17 12:01:52 +0000 | [diff] [blame] | 1322 | if (!Class.LateParsedDeclarations.empty()) { |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1323 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){ |
| 1324 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 1325 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1326 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1327 | |
DeLesley Hutchins | 6f86004 | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1328 | if (!AlreadyHasClassScope) |
| 1329 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 1330 | Class.TagOrTemplate); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1333 | /// \brief Parse all attributes in LAs, and attach them to Decl D. |
| 1334 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 1335 | bool EnterScope, bool OnDefinition) { |
DeLesley Hutchins | 66e300e | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1336 | assert(LAs.parseSoon() && |
| 1337 | "Attribute list should be marked for immediate parsing."); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1338 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 19c722d | 2012-08-15 22:41:04 +0000 | [diff] [blame] | 1339 | if (D) |
| 1340 | LAs[i]->addDecl(D); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1341 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
Benjamin Kramer | bafc49a | 2012-04-14 12:44:47 +0000 | [diff] [blame] | 1342 | delete LAs[i]; |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1343 | } |
| 1344 | LAs.clear(); |
| 1345 | } |
| 1346 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1347 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 1348 | /// This will be called at the end of parsing a class declaration |
| 1349 | /// for each LateParsedAttribute. We consume the saved tokens and |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1350 | /// create an attribute with the arguments filled in. We add this |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1351 | /// to the Attribute list for the decl. |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1352 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 1353 | bool EnterScope, bool OnDefinition) { |
David Majnemer | d5946f5 | 2015-01-13 08:35:24 +0000 | [diff] [blame] | 1354 | // Create a fake EOF so that attribute parsing won't go off the end of the |
| 1355 | // attribute. |
| 1356 | Token AttrEnd; |
| 1357 | AttrEnd.startToken(); |
| 1358 | AttrEnd.setKind(tok::eof); |
| 1359 | AttrEnd.setLocation(Tok.getLocation()); |
| 1360 | AttrEnd.setEofData(LA.Toks.data()); |
| 1361 | LA.Toks.push_back(AttrEnd); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1362 | |
| 1363 | // Append the current token at the end of the new token stream so that it |
| 1364 | // doesn't get lost. |
| 1365 | LA.Toks.push_back(Tok); |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1366 | PP.EnterTokenStream(LA.Toks, true); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1367 | // Consume the previously pushed token. |
Argyrios Kyrtzidis | c36633c | 2013-03-27 23:58:17 +0000 | [diff] [blame] | 1368 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1369 | |
| 1370 | ParsedAttributes Attrs(AttrFactory); |
| 1371 | SourceLocation endLoc; |
| 1372 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1373 | if (LA.Decls.size() > 0) { |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1374 | Decl *D = LA.Decls[0]; |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1375 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1376 | RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext()); |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1377 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1378 | // Allow 'this' within late-parsed attributes. |
Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 1379 | Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0, |
| 1380 | ND && ND->isCXXInstanceMember()); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1381 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1382 | if (LA.Decls.size() == 1) { |
| 1383 | // If the Decl is templatized, add template parameters to scope. |
| 1384 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 1385 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 1386 | if (HasTemplateScope) |
| 1387 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1388 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1389 | // If the Decl is on a function, add function parameters to the scope. |
| 1390 | bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
Momchil Velikov | 57c681f | 2017-08-10 15:43:06 +0000 | [diff] [blame] | 1391 | ParseScope FnScope( |
| 1392 | this, Scope::FnScope | Scope::DeclScope | Scope::CompoundStmtScope, |
| 1393 | HasFunScope); |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1394 | if (HasFunScope) |
| 1395 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1396 | |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1397 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1398 | nullptr, SourceLocation(), AttributeList::AS_GNU, |
| 1399 | nullptr); |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1400 | |
| 1401 | if (HasFunScope) { |
| 1402 | Actions.ActOnExitFunctionContext(); |
| 1403 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 1404 | } |
| 1405 | if (HasTemplateScope) { |
| 1406 | TempScope.Exit(); |
| 1407 | } |
| 1408 | } else { |
| 1409 | // If there are multiple decls, then the decl cannot be within the |
| 1410 | // function scope. |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1411 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1412 | nullptr, SourceLocation(), AttributeList::AS_GNU, |
| 1413 | nullptr); |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1414 | } |
DeLesley Hutchins | 71d6103 | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 1415 | } else { |
| 1416 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
Aaron Ballman | 9a99e0d | 2014-01-20 17:18:35 +0000 | [diff] [blame] | 1419 | const AttributeList *AL = Attrs.getList(); |
| 1420 | if (OnDefinition && AL && !AL->isCXX11Attribute() && |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1421 | AL->isKnownToGCC()) |
Aaron Ballman | 9a99e0d | 2014-01-20 17:18:35 +0000 | [diff] [blame] | 1422 | Diag(Tok, diag::warn_attribute_on_function_definition) |
| 1423 | << &LA.AttrName; |
| 1424 | |
| 1425 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1426 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1427 | |
David Majnemer | d5946f5 | 2015-01-13 08:35:24 +0000 | [diff] [blame] | 1428 | // Due to a parsing error, we either went over the cached tokens or |
| 1429 | // there are still cached tokens left, so we skip the leftover tokens. |
| 1430 | while (Tok.isNot(tok::eof)) |
| 1431 | ConsumeAnyToken(); |
| 1432 | |
| 1433 | if (Tok.is(tok::eof) && Tok.getEofData() == AttrEnd.getEofData()) |
| 1434 | ConsumeAnyToken(); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1437 | void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName, |
| 1438 | SourceLocation AttrNameLoc, |
| 1439 | ParsedAttributes &Attrs, |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1440 | SourceLocation *EndLoc, |
| 1441 | IdentifierInfo *ScopeName, |
| 1442 | SourceLocation ScopeLoc, |
| 1443 | AttributeList::Syntax Syntax) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1444 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 1445 | |
| 1446 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1447 | T.consumeOpen(); |
| 1448 | |
| 1449 | if (Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1450 | Diag(Tok, diag::err_expected) << tok::identifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1451 | T.skipToEnd(); |
| 1452 | return; |
| 1453 | } |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 1454 | IdentifierLoc *ArgumentKind = ParseIdentifierLoc(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1455 | |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 1456 | if (ExpectAndConsume(tok::comma)) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1457 | T.skipToEnd(); |
| 1458 | return; |
| 1459 | } |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1460 | |
| 1461 | SourceRange MatchingCTypeRange; |
| 1462 | TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange); |
| 1463 | if (MatchingCType.isInvalid()) { |
| 1464 | T.skipToEnd(); |
| 1465 | return; |
| 1466 | } |
| 1467 | |
| 1468 | bool LayoutCompatible = false; |
| 1469 | bool MustBeNull = false; |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1470 | while (TryConsumeToken(tok::comma)) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1471 | if (Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1472 | Diag(Tok, diag::err_expected) << tok::identifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1473 | T.skipToEnd(); |
| 1474 | return; |
| 1475 | } |
| 1476 | IdentifierInfo *Flag = Tok.getIdentifierInfo(); |
| 1477 | if (Flag->isStr("layout_compatible")) |
| 1478 | LayoutCompatible = true; |
| 1479 | else if (Flag->isStr("must_be_null")) |
| 1480 | MustBeNull = true; |
| 1481 | else { |
| 1482 | Diag(Tok, diag::err_type_safety_unknown_flag) << Flag; |
| 1483 | T.skipToEnd(); |
| 1484 | return; |
| 1485 | } |
| 1486 | ConsumeToken(); // consume flag |
| 1487 | } |
| 1488 | |
| 1489 | if (!T.consumeClose()) { |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1490 | Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, ScopeName, ScopeLoc, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1491 | ArgumentKind, MatchingCType.get(), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1492 | LayoutCompatible, MustBeNull, Syntax); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | if (EndLoc) |
| 1496 | *EndLoc = T.getCloseLocation(); |
| 1497 | } |
| 1498 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1499 | /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets |
| 1500 | /// of a C++11 attribute-specifier in a location where an attribute is not |
| 1501 | /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this |
| 1502 | /// situation. |
| 1503 | /// |
| 1504 | /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if |
| 1505 | /// this doesn't appear to actually be an attribute-specifier, and the caller |
| 1506 | /// should try to parse it. |
| 1507 | bool Parser::DiagnoseProhibitedCXX11Attribute() { |
| 1508 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)); |
| 1509 | |
| 1510 | switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) { |
| 1511 | case CAK_NotAttributeSpecifier: |
| 1512 | // No diagnostic: we're in Obj-C++11 and this is not actually an attribute. |
| 1513 | return false; |
| 1514 | |
| 1515 | case CAK_InvalidAttributeSpecifier: |
| 1516 | Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute); |
| 1517 | return false; |
| 1518 | |
| 1519 | case CAK_AttributeSpecifier: |
| 1520 | // Parse and discard the attributes. |
| 1521 | SourceLocation BeginLoc = ConsumeBracket(); |
| 1522 | ConsumeBracket(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1523 | SkipUntil(tok::r_square); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1524 | assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied"); |
| 1525 | SourceLocation EndLoc = ConsumeBracket(); |
| 1526 | Diag(BeginLoc, diag::err_attributes_not_allowed) |
| 1527 | << SourceRange(BeginLoc, EndLoc); |
| 1528 | return true; |
| 1529 | } |
Chandler Carruth | d8f7d38 | 2012-04-10 16:03:08 +0000 | [diff] [blame] | 1530 | llvm_unreachable("All cases handled above."); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
Richard Smith | 98155ad | 2013-02-20 01:17:14 +0000 | [diff] [blame] | 1533 | /// \brief We have found the opening square brackets of a C++11 |
| 1534 | /// attribute-specifier in a location where an attribute is not permitted, but |
| 1535 | /// we know where the attributes ought to be written. Parse them anyway, and |
| 1536 | /// provide a fixit moving them to the right place. |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1537 | void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs, |
| 1538 | SourceLocation CorrectLocation) { |
| 1539 | assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) || |
| 1540 | Tok.is(tok::kw_alignas)); |
| 1541 | |
| 1542 | // Consume the attributes. |
| 1543 | SourceLocation Loc = Tok.getLocation(); |
| 1544 | ParseCXX11Attributes(Attrs); |
| 1545 | CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true); |
| 1546 | |
| 1547 | Diag(Loc, diag::err_attributes_not_allowed) |
| 1548 | << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange) |
| 1549 | << FixItHint::CreateRemoval(AttrRange); |
| 1550 | } |
| 1551 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1552 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 1553 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 1554 | << attrs.Range; |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
Richard Smith | 49cc1cc | 2016-08-18 21:59:42 +0000 | [diff] [blame] | 1557 | void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &Attrs, |
| 1558 | unsigned DiagID) { |
| 1559 | for (AttributeList *Attr = Attrs.getList(); Attr; Attr = Attr->getNext()) { |
| 1560 | if (!Attr->isCXX11Attribute()) |
| 1561 | continue; |
| 1562 | if (Attr->getKind() == AttributeList::UnknownAttribute) |
| 1563 | Diag(Attr->getLoc(), diag::warn_unknown_attribute_ignored) |
| 1564 | << Attr->getName(); |
| 1565 | else { |
| 1566 | Diag(Attr->getLoc(), DiagID) |
| 1567 | << Attr->getName(); |
| 1568 | Attr->setInvalid(); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1569 | } |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1570 | } |
| 1571 | } |
| 1572 | |
Nico Weber | 32a0fc7 | 2016-09-03 03:01:32 +0000 | [diff] [blame] | 1573 | // Usually, `__attribute__((attrib)) class Foo {} var` means that attribute |
| 1574 | // applies to var, not the type Foo. |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1575 | // As an exception to the rule, __declspec(align(...)) before the |
| 1576 | // class-key affects the type instead of the variable. |
Nico Weber | 32a0fc7 | 2016-09-03 03:01:32 +0000 | [diff] [blame] | 1577 | // Also, Microsoft-style [attributes] seem to affect the type instead of the |
| 1578 | // variable. |
| 1579 | // This function moves attributes that should apply to the type off DS to Attrs. |
| 1580 | void Parser::stripTypeAttributesOffDeclSpec(ParsedAttributesWithRange &Attrs, |
| 1581 | DeclSpec &DS, |
| 1582 | Sema::TagUseKind TUK) { |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1583 | if (TUK == Sema::TUK_Reference) |
| 1584 | return; |
| 1585 | |
| 1586 | ParsedAttributes &PA = DS.getAttributes(); |
| 1587 | AttributeList *AL = PA.getList(); |
| 1588 | AttributeList *Prev = nullptr; |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 1589 | AttributeList *TypeAttrHead = nullptr; |
| 1590 | AttributeList *TypeAttrTail = nullptr; |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1591 | while (AL) { |
| 1592 | AttributeList *Next = AL->getNext(); |
| 1593 | |
Nico Weber | 32a0fc7 | 2016-09-03 03:01:32 +0000 | [diff] [blame] | 1594 | if ((AL->getKind() == AttributeList::AT_Aligned && |
| 1595 | AL->isDeclspecAttribute()) || |
| 1596 | AL->isMicrosoftAttribute()) { |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1597 | // Stitch the attribute into the tag's attribute list. |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 1598 | if (TypeAttrTail) |
| 1599 | TypeAttrTail->setNext(AL); |
| 1600 | else |
| 1601 | TypeAttrHead = AL; |
| 1602 | TypeAttrTail = AL; |
| 1603 | TypeAttrTail->setNext(nullptr); |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1604 | |
| 1605 | // Remove the attribute from the variable's attribute list. |
| 1606 | if (Prev) { |
| 1607 | // Set the last variable attribute's next attribute to be the attribute |
| 1608 | // after the current one. |
| 1609 | Prev->setNext(Next); |
| 1610 | } else { |
| 1611 | // Removing the head of the list requires us to reset the head to the |
| 1612 | // next attribute. |
| 1613 | PA.set(Next); |
| 1614 | } |
| 1615 | } else { |
| 1616 | Prev = AL; |
| 1617 | } |
| 1618 | |
| 1619 | AL = Next; |
| 1620 | } |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 1621 | |
| 1622 | // Find end of type attributes Attrs and add NewTypeAttributes in the same |
| 1623 | // order they were in originally. (Remember, in AttributeList things earlier |
| 1624 | // in source order are later in the list, since new attributes are added to |
| 1625 | // the front of the list.) |
| 1626 | Attrs.addAllAtEnd(TypeAttrHead); |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1629 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 1630 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1631 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 1632 | /// location of the semicolon in DeclEnd. |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1633 | /// |
| 1634 | /// declaration: [C99 6.7] |
| 1635 | /// block-declaration -> |
| 1636 | /// simple-declaration |
| 1637 | /// others [FIXME] |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1638 | /// [C++] template-declaration |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1639 | /// [C++] namespace-definition |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1640 | /// [C++] using-directive |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1641 | /// [C++] using-declaration |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1642 | /// [C++11/C11] static_assert-declaration |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1643 | /// others... [FIXME] |
| 1644 | /// |
Rafael Espindola | 1bd906d | 2014-10-22 14:27:08 +0000 | [diff] [blame] | 1645 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context, |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1646 | SourceLocation &DeclEnd, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1647 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 355094e | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 1648 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | 59b7528 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 1649 | // Must temporarily exit the objective-c container scope for |
| 1650 | // parsing c none objective-c decls. |
| 1651 | ObjCDeclContextSwitch ObjCDC(*this); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1652 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1653 | Decl *SingleDecl = nullptr; |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1654 | switch (Tok.getKind()) { |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1655 | case tok::kw_template: |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1656 | case tok::kw_export: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1657 | ProhibitAttributes(attrs); |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1658 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1659 | break; |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1660 | case tok::kw_inline: |
Sebastian Redl | 5a5f2c7 | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 1661 | // 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] | 1662 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1663 | ProhibitAttributes(attrs); |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1664 | SourceLocation InlineLoc = ConsumeToken(); |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 1665 | return ParseNamespace(Context, DeclEnd, InlineLoc); |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1666 | } |
Rafael Espindola | 1bd906d | 2014-10-22 14:27:08 +0000 | [diff] [blame] | 1667 | return ParseSimpleDeclaration(Context, DeclEnd, attrs, |
Fariborz Jahanian | 1db5c94 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1668 | true); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1669 | case tok::kw_namespace: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1670 | ProhibitAttributes(attrs); |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 1671 | return ParseNamespace(Context, DeclEnd); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1672 | case tok::kw_using: |
Richard Smith | 6f1daa4 | 2016-12-16 00:58:48 +0000 | [diff] [blame] | 1673 | return ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
| 1674 | DeclEnd, attrs); |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1675 | case tok::kw_static_assert: |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1676 | case tok::kw__Static_assert: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1677 | ProhibitAttributes(attrs); |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1678 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1679 | break; |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1680 | default: |
Rafael Espindola | 1bd906d | 2014-10-22 14:27:08 +0000 | [diff] [blame] | 1681 | return ParseSimpleDeclaration(Context, DeclEnd, attrs, true); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1682 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1683 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1684 | // 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] | 1685 | // single decl, convert it now. |
| 1686 | return Actions.ConvertDeclToDeclGroup(SingleDecl); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 1690 | /// declaration-specifiers init-declarator-list[opt] ';' |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1691 | /// [C++11] attribute-specifier-seq decl-specifier-seq[opt] |
| 1692 | /// init-declarator-list ';' |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1693 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 1694 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1695 | /// |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1696 | /// for-range-declaration: [C++11 6.5p1: stmt.ranged] |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1697 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1698 | /// |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1699 | /// 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] | 1700 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1701 | /// |
| 1702 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 1703 | /// of a simple-declaration. If we find that we are, we also parse the |
| 1704 | /// for-range-initializer, and place it here. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1705 | Parser::DeclGroupPtrTy |
Rafael Espindola | 1bd906d | 2014-10-22 14:27:08 +0000 | [diff] [blame] | 1706 | Parser::ParseSimpleDeclaration(unsigned Context, |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1707 | SourceLocation &DeclEnd, |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1708 | ParsedAttributesWithRange &Attrs, |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1709 | bool RequireSemi, ForRangeInit *FRI) { |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1710 | // Parse the common declaration-specifiers piece. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1711 | ParsingDeclSpec DS(*this); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1712 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 1713 | DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context); |
| 1714 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext); |
| 1715 | |
| 1716 | // If we had a free-standing type definition with a missing semicolon, we |
| 1717 | // may get this far before the problem becomes obvious. |
| 1718 | if (DS.hasTagDefinition() && |
| 1719 | DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext)) |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 1720 | return nullptr; |
Abramo Bagnara | 1cd8368 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 1721 | |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 1722 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 1723 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1724 | if (Tok.is(tok::semi)) { |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1725 | ProhibitAttributes(Attrs); |
Argyrios Kyrtzidis | fbb2bb5 | 2012-05-16 23:49:15 +0000 | [diff] [blame] | 1726 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 005fc1b | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1727 | if (RequireSemi) ConsumeToken(); |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 1728 | RecordDecl *AnonRecord = nullptr; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1729 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 1730 | DS, AnonRecord); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1731 | DS.complete(TheDecl); |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 1732 | if (AnonRecord) { |
| 1733 | Decl* decls[] = {AnonRecord, TheDecl}; |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 1734 | return Actions.BuildDeclaratorGroup(decls); |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 1735 | } |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1736 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 1737 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1738 | |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1739 | DS.takeAttributesFrom(Attrs); |
Reid Kleckner | d61a311 | 2014-12-15 23:16:32 +0000 | [diff] [blame] | 1740 | return ParseDeclGroup(DS, Context, &DeclEnd, FRI); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1741 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1742 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1743 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1744 | /// for a declarator. |
| 1745 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 1746 | switch (Tok.getKind()) { |
| 1747 | case tok::annot_cxxscope: |
| 1748 | case tok::annot_template_id: |
| 1749 | case tok::caret: |
| 1750 | case tok::code_completion: |
| 1751 | case tok::coloncolon: |
| 1752 | case tok::ellipsis: |
| 1753 | case tok::kw___attribute: |
| 1754 | case tok::kw_operator: |
| 1755 | case tok::l_paren: |
| 1756 | case tok::star: |
| 1757 | return true; |
| 1758 | |
| 1759 | case tok::amp: |
| 1760 | case tok::ampamp: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1761 | return getLangOpts().CPlusPlus; |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1762 | |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1763 | 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] | 1764 | return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 && |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1765 | NextToken().is(tok::l_square); |
| 1766 | |
| 1767 | 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] | 1768 | return Context == Declarator::MemberContext || getLangOpts().CPlusPlus; |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1769 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1770 | case tok::identifier: |
| 1771 | switch (NextToken().getKind()) { |
| 1772 | case tok::code_completion: |
| 1773 | case tok::coloncolon: |
| 1774 | case tok::comma: |
| 1775 | case tok::equal: |
| 1776 | case tok::equalequal: // Might be a typo for '='. |
| 1777 | case tok::kw_alignas: |
| 1778 | case tok::kw_asm: |
| 1779 | case tok::kw___attribute: |
| 1780 | case tok::l_brace: |
| 1781 | case tok::l_paren: |
| 1782 | case tok::l_square: |
| 1783 | case tok::less: |
| 1784 | case tok::r_brace: |
| 1785 | case tok::r_paren: |
| 1786 | case tok::r_square: |
| 1787 | case tok::semi: |
| 1788 | return true; |
| 1789 | |
| 1790 | case tok::colon: |
| 1791 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1792 | // and in block scope it's probably a label. Inside a class definition, |
| 1793 | // this is a bit-field. |
| 1794 | return Context == Declarator::MemberContext || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1795 | (getLangOpts().CPlusPlus && Context == Declarator::FileContext); |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1796 | |
| 1797 | case tok::identifier: // Possible virt-specifier. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1798 | return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken()); |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1799 | |
| 1800 | default: |
| 1801 | return false; |
| 1802 | } |
| 1803 | |
| 1804 | default: |
| 1805 | return false; |
| 1806 | } |
| 1807 | } |
| 1808 | |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1809 | /// Skip until we reach something which seems like a sensible place to pick |
| 1810 | /// up parsing after a malformed declaration. This will sometimes stop sooner |
| 1811 | /// than SkipUntil(tok::r_brace) would, but will never stop later. |
| 1812 | void Parser::SkipMalformedDecl() { |
| 1813 | while (true) { |
| 1814 | switch (Tok.getKind()) { |
| 1815 | case tok::l_brace: |
| 1816 | // Skip until matching }, then stop. We've probably skipped over |
| 1817 | // a malformed class or function definition or similar. |
| 1818 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1819 | SkipUntil(tok::r_brace); |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1820 | if (Tok.isOneOf(tok::comma, tok::l_brace, tok::kw_try)) { |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1821 | // This declaration isn't over yet. Keep skipping. |
| 1822 | continue; |
| 1823 | } |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1824 | TryConsumeToken(tok::semi); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1825 | return; |
| 1826 | |
| 1827 | case tok::l_square: |
| 1828 | ConsumeBracket(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1829 | SkipUntil(tok::r_square); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1830 | continue; |
| 1831 | |
| 1832 | case tok::l_paren: |
| 1833 | ConsumeParen(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1834 | SkipUntil(tok::r_paren); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1835 | continue; |
| 1836 | |
| 1837 | case tok::r_brace: |
| 1838 | return; |
| 1839 | |
| 1840 | case tok::semi: |
| 1841 | ConsumeToken(); |
| 1842 | return; |
| 1843 | |
| 1844 | case tok::kw_inline: |
| 1845 | // 'inline namespace' at the start of a line is almost certainly |
Jordan Rose | 12e730c | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1846 | // a good place to pick back up parsing, except in an Objective-C |
| 1847 | // @interface context. |
| 1848 | if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) && |
| 1849 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1850 | return; |
| 1851 | break; |
| 1852 | |
| 1853 | case tok::kw_namespace: |
| 1854 | // 'namespace' at the start of a line is almost certainly a good |
Jordan Rose | 12e730c | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1855 | // place to pick back up parsing, except in an Objective-C |
| 1856 | // @interface context. |
| 1857 | if (Tok.isAtStartOfLine() && |
| 1858 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
| 1859 | return; |
| 1860 | break; |
| 1861 | |
| 1862 | case tok::at: |
| 1863 | // @end is very much like } in Objective-C contexts. |
| 1864 | if (NextToken().isObjCAtKeyword(tok::objc_end) && |
| 1865 | ParsingInObjCContainer) |
| 1866 | return; |
| 1867 | break; |
| 1868 | |
| 1869 | case tok::minus: |
| 1870 | case tok::plus: |
| 1871 | // - and + probably start new method declarations in Objective-C contexts. |
| 1872 | if (Tok.isAtStartOfLine() && ParsingInObjCContainer) |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1873 | return; |
| 1874 | break; |
| 1875 | |
| 1876 | case tok::eof: |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 1877 | case tok::annot_module_begin: |
| 1878 | case tok::annot_module_end: |
| 1879 | case tok::annot_module_include: |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1880 | return; |
| 1881 | |
| 1882 | default: |
| 1883 | break; |
| 1884 | } |
| 1885 | |
| 1886 | ConsumeAnyToken(); |
| 1887 | } |
| 1888 | } |
| 1889 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1890 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1891 | /// definition or a group of object declarations, actually parse the |
| 1892 | /// result. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1893 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1894 | unsigned Context, |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1895 | SourceLocation *DeclEnd, |
| 1896 | ForRangeInit *FRI) { |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1897 | // Parse the first declarator. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1898 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1899 | ParseDeclarator(D); |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1900 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1901 | // Bail out if the first declarator didn't seem well-formed. |
| 1902 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1903 | SkipMalformedDecl(); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 1904 | return nullptr; |
Chris Lattner | efb0f11 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1905 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1906 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1907 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1908 | // appropriate function scope after the function Decl has been constructed. |
DeLesley Hutchins | 66e300e | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1909 | // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList. |
| 1910 | LateParsedAttrList LateParsedAttrs(true); |
Richard Smith | 99c464c | 2014-11-10 21:10:32 +0000 | [diff] [blame] | 1911 | if (D.isFunctionDeclarator()) { |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1912 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1913 | |
Richard Smith | 99c464c | 2014-11-10 21:10:32 +0000 | [diff] [blame] | 1914 | // The _Noreturn keyword can't appear here, unlike the GNU noreturn |
| 1915 | // attribute. If we find the keyword here, tell the user to put it |
| 1916 | // at the start instead. |
| 1917 | if (Tok.is(tok::kw__Noreturn)) { |
| 1918 | SourceLocation Loc = ConsumeToken(); |
| 1919 | const char *PrevSpec; |
| 1920 | unsigned DiagID; |
| 1921 | |
| 1922 | // We can offer a fixit if it's valid to mark this function as _Noreturn |
| 1923 | // and we don't have any other declarators in this declaration. |
| 1924 | bool Fixit = !DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID); |
| 1925 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1926 | Fixit &= Tok.isOneOf(tok::semi, tok::l_brace, tok::kw_try); |
Richard Smith | 99c464c | 2014-11-10 21:10:32 +0000 | [diff] [blame] | 1927 | |
| 1928 | Diag(Loc, diag::err_c11_noreturn_misplaced) |
| 1929 | << (Fixit ? FixItHint::CreateRemoval(Loc) : FixItHint()) |
| 1930 | << (Fixit ? FixItHint::CreateInsertion(D.getLocStart(), "_Noreturn ") |
| 1931 | : FixItHint()); |
| 1932 | } |
| 1933 | } |
| 1934 | |
Chris Lattner | dbb1e93 | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1935 | // 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] | 1936 | if (D.isFunctionDeclarator() && |
Chris Lattner | dbb1e93 | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1937 | // Look at the next token to make sure that this isn't a function |
| 1938 | // declaration. We have to check this because __attribute__ might be the |
| 1939 | // start of a function definition in GCC-extended K&R C. |
Fariborz Jahanian | 712bb81 | 2012-08-10 15:54:40 +0000 | [diff] [blame] | 1940 | !isDeclarationAfterDeclarator()) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1941 | |
Reid Kleckner | d61a311 | 2014-12-15 23:16:32 +0000 | [diff] [blame] | 1942 | // Function definitions are only allowed at file scope and in C++ classes. |
| 1943 | // The C++ inline method definition case is handled elsewhere, so we only |
| 1944 | // need to handle the file scope definition case. |
| 1945 | if (Context == Declarator::FileContext) { |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1946 | if (isStartOfFunctionDefinition(D)) { |
| 1947 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1948 | Diag(Tok, diag::err_function_declared_typedef); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1949 | |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1950 | // Recover by treating the 'typedef' as spurious. |
| 1951 | DS.ClearStorageClassSpecs(); |
| 1952 | } |
| 1953 | |
| 1954 | Decl *TheDecl = |
| 1955 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
| 1956 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1959 | if (isDeclarationSpecifier()) { |
Nico Weber | 6b05f38 | 2015-02-18 04:53:03 +0000 | [diff] [blame] | 1960 | // If there is an invalid declaration specifier right after the |
| 1961 | // function prototype, then we must be in a missing semicolon case |
| 1962 | // where this isn't actually a body. Just fall through into the code |
| 1963 | // that handles it as a prototype, and let the top-level code handle |
| 1964 | // the erroneous declspec where it would otherwise expect a comma or |
| 1965 | // semicolon. |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1966 | } else { |
| 1967 | Diag(Tok, diag::err_expected_fn_body); |
| 1968 | SkipUntil(tok::semi); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 1969 | return nullptr; |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1970 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1971 | } else { |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1972 | if (Tok.is(tok::l_brace)) { |
| 1973 | Diag(Tok, diag::err_function_definition_not_allowed); |
Serge Pavlov | 1de5151 | 2013-12-09 05:25:47 +0000 | [diff] [blame] | 1974 | SkipMalformedDecl(); |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 1975 | return nullptr; |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1976 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1977 | } |
| 1978 | } |
| 1979 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1980 | if (ParseAsmAttributesAfterDeclarator(D)) |
David Blaikie | 0403cb1 | 2016-01-15 23:43:25 +0000 | [diff] [blame] | 1981 | return nullptr; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1982 | |
| 1983 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1984 | // must parse and analyze the for-range-initializer before the declaration is |
| 1985 | // analyzed. |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1986 | // |
| 1987 | // Handle the Objective-C for-in loop variable similarly, although we |
| 1988 | // don't need to parse the container in advance. |
| 1989 | if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) { |
| 1990 | bool IsForRangeLoop = false; |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1991 | if (TryConsumeToken(tok::colon, FRI->ColonLoc)) { |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1992 | IsForRangeLoop = true; |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1993 | if (Tok.is(tok::l_brace)) |
| 1994 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1995 | else |
| 1996 | FRI->RangeExpr = ParseExpression(); |
| 1997 | } |
| 1998 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1999 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 2000 | if (IsForRangeLoop) |
| 2001 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2002 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | cf6e0c8 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 2003 | D.complete(ThisDecl); |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 2004 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2007 | SmallVector<Decl *, 8> DeclsInGroup; |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2008 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes( |
| 2009 | D, ParsedTemplateInfo(), FRI); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2010 | if (LateParsedAttrs.size() > 0) |
| 2011 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2012 | D.complete(FirstDecl); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2013 | if (FirstDecl) |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2014 | DeclsInGroup.push_back(FirstDecl); |
| 2015 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 2016 | bool ExpectSemi = Context != Declarator::ForContext; |
Fariborz Jahanian | 577574a | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 2017 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2018 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 2019 | // error, bail out. |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 2020 | SourceLocation CommaLoc; |
| 2021 | while (TryConsumeToken(tok::comma, CommaLoc)) { |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 2022 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 2023 | // This comma was followed by a line-break and something which can't be |
| 2024 | // the start of a declarator. The comma was probably a typo for a |
| 2025 | // semicolon. |
| 2026 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 2027 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 2028 | ExpectSemi = false; |
| 2029 | break; |
| 2030 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2031 | |
| 2032 | // Parse the next declarator. |
| 2033 | D.clear(); |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2034 | D.setCommaLoc(CommaLoc); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2035 | |
| 2036 | // Accept attributes in an init-declarator. In the first declarator in a |
| 2037 | // declaration, these would be part of the declspec. In subsequent |
| 2038 | // declarators, they become part of the declarator itself, so that they |
| 2039 | // don't apply to declarators after *this* one. Examples: |
| 2040 | // short __attribute__((common)) var; -> declspec |
| 2041 | // short var __attribute__((common)); -> declarator |
| 2042 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2043 | MaybeParseGNUAttributes(D); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2044 | |
Nico Rieck | eaaae27 | 2014-12-04 23:31:08 +0000 | [diff] [blame] | 2045 | // MSVC parses but ignores qualifiers after the comma as an extension. |
| 2046 | if (getLangOpts().MicrosoftExt) |
| 2047 | DiagnoseAndSkipExtendedMicrosoftTypeAttributes(); |
| 2048 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2049 | ParseDeclarator(D); |
Fariborz Jahanian | 372030b | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 2050 | if (!D.isInvalidType()) { |
| 2051 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 2052 | D.complete(ThisDecl); |
| 2053 | if (ThisDecl) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2054 | DeclsInGroup.push_back(ThisDecl); |
Fariborz Jahanian | 372030b | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 2055 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | if (DeclEnd) |
| 2059 | *DeclEnd = Tok.getLocation(); |
| 2060 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 2061 | if (ExpectSemi && |
Chris Lattner | 02f1b61 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 2062 | ExpectAndConsumeSemi(Context == Declarator::FileContext |
| 2063 | ? diag::err_invalid_token_after_toplevel_declarator |
| 2064 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 1390134 | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 2065 | // Okay, there was no semicolon and one was expected. If we see a |
| 2066 | // declaration specifier, just assume it was missing and continue parsing. |
| 2067 | // Otherwise things are very confused and we skip to recover. |
| 2068 | if (!isDeclarationSpecifier()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2069 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 2070 | TryConsumeToken(tok::semi); |
Chris Lattner | 1390134 | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 2071 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 2074 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup); |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2077 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 2078 | /// declarator. Returns true on an error. |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2079 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2080 | // If a simple-asm-expr is present, parse it. |
| 2081 | if (Tok.is(tok::kw_asm)) { |
| 2082 | SourceLocation Loc; |
| 2083 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 2084 | if (AsmLabel.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2085 | SkipUntil(tok::semi, StopBeforeMatch); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2086 | return true; |
| 2087 | } |
| 2088 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2089 | D.setAsmLabel(AsmLabel.get()); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2090 | D.SetRangeEnd(Loc); |
| 2091 | } |
| 2092 | |
| 2093 | MaybeParseGNUAttributes(D); |
| 2094 | return false; |
| 2095 | } |
| 2096 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2097 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 2098 | /// declarator'. This method parses the remainder of the declaration |
| 2099 | /// (including any attributes or initializer, among other things) and |
| 2100 | /// finalizes the declaration. |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 2101 | /// |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 2102 | /// init-declarator: [C99 6.7] |
| 2103 | /// declarator |
| 2104 | /// declarator '=' initializer |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 2105 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 2106 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2107 | /// [C++] declarator initializer[opt] |
| 2108 | /// |
| 2109 | /// [C++] initializer: |
| 2110 | /// [C++] '=' initializer-clause |
| 2111 | /// [C++] '(' expression-list ')' |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 2112 | /// [C++0x] '=' 'default' [TODO] |
| 2113 | /// [C++0x] '=' 'delete' |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2114 | /// [C++0x] braced-init-list |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 2115 | /// |
| 2116 | /// According to the standard grammar, =default and =delete are function |
| 2117 | /// definitions, but that definitely doesn't fit with the parser here. |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 2118 | /// |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2119 | Decl *Parser::ParseDeclarationAfterDeclarator( |
| 2120 | Declarator &D, const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 2121 | if (ParseAsmAttributesAfterDeclarator(D)) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2122 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2123 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 2124 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 2125 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2126 | |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2127 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes( |
| 2128 | Declarator &D, const ParsedTemplateInfo &TemplateInfo, ForRangeInit *FRI) { |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2129 | // Inform the current actions module that we just parsed this declarator. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2130 | Decl *ThisDecl = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2131 | switch (TemplateInfo.Kind) { |
| 2132 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2133 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2134 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2135 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2136 | case ParsedTemplateInfo::Template: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2137 | case ParsedTemplateInfo::ExplicitSpecialization: { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2138 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2139 | *TemplateInfo.TemplateParams, |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2140 | D); |
Larisse Voufo | 833b05a | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 2141 | if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2142 | // Re-direct this decl to refer to the templated decl so that we can |
| 2143 | // initialize it. |
| 2144 | ThisDecl = VT->getTemplatedDecl(); |
| 2145 | break; |
| 2146 | } |
| 2147 | case ParsedTemplateInfo::ExplicitInstantiation: { |
| 2148 | if (Tok.is(tok::semi)) { |
| 2149 | DeclResult ThisRes = Actions.ActOnExplicitInstantiation( |
| 2150 | getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D); |
| 2151 | if (ThisRes.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2152 | SkipUntil(tok::semi, StopBeforeMatch); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2153 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2154 | } |
| 2155 | ThisDecl = ThisRes.get(); |
| 2156 | } else { |
| 2157 | // FIXME: This check should be for a variable template instantiation only. |
| 2158 | |
| 2159 | // Check that this is a valid instantiation |
| 2160 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 2161 | // If the declarator-id is not a template-id, issue a diagnostic and |
| 2162 | // recover by ignoring the 'template' keyword. |
| 2163 | Diag(Tok, diag::err_template_defn_explicit_instantiation) |
| 2164 | << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc); |
| 2165 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 2166 | } else { |
| 2167 | SourceLocation LAngleLoc = |
| 2168 | PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
| 2169 | Diag(D.getIdentifierLoc(), |
| 2170 | diag::err_explicit_instantiation_with_definition) |
| 2171 | << SourceRange(TemplateInfo.TemplateLoc) |
| 2172 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
| 2173 | |
| 2174 | // Recover as if it were an explicit specialization. |
| 2175 | TemplateParameterLists FakedParamLists; |
| 2176 | FakedParamLists.push_back(Actions.ActOnTemplateParameterList( |
Craig Topper | 96225a5 | 2015-12-24 23:58:25 +0000 | [diff] [blame] | 2177 | 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, None, |
Hubert Tong | f608c05 | 2016-04-29 18:05:37 +0000 | [diff] [blame] | 2178 | LAngleLoc, nullptr)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2179 | |
| 2180 | ThisDecl = |
| 2181 | Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D); |
| 2182 | } |
| 2183 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2184 | break; |
| 2185 | } |
| 2186 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2187 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2188 | // Parse declarator '=' initializer. |
Richard Trieu | c64d323 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 2189 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | 4972a6d | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 2190 | if (isTokenEqualOrEqualTypo()) { |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2191 | SourceLocation EqualLoc = ConsumeToken(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2192 | |
Anders Carlsson | 991285e | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 2193 | if (Tok.is(tok::kw_delete)) { |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2194 | if (D.isFunctionDeclarator()) |
| 2195 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 2196 | << 1 /* delete */; |
| 2197 | else |
| 2198 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Alexis Hunt | 5dafebc | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 2199 | } else if (Tok.is(tok::kw_default)) { |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2200 | if (D.isFunctionDeclarator()) |
Sebastian Redl | 46afb55 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 2201 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 2202 | << 0 /* default */; |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 2203 | else |
| 2204 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2205 | } else { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2206 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 2207 | EnterScope(0); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2208 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 2209 | } |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 2210 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2211 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2212 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Peter Collingbourne | 6b4fdc2 | 2012-07-27 12:56:09 +0000 | [diff] [blame] | 2213 | Actions.FinalizeDeclaration(ThisDecl); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2214 | cutOffParsing(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2215 | return nullptr; |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 2216 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2217 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2218 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 2219 | |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2220 | // If this is the only decl in (possibly) range based for statement, |
| 2221 | // our best guess is that the user meant ':' instead of '='. |
| 2222 | if (Tok.is(tok::r_paren) && FRI && D.isFirstDeclarator()) { |
| 2223 | Diag(EqualLoc, diag::err_single_decl_assign_in_for_range) |
| 2224 | << FixItHint::CreateReplacement(EqualLoc, ":"); |
| 2225 | // We are trying to stop parser from looking for ';' in this for |
| 2226 | // statement, therefore preventing spurious errors to be issued. |
| 2227 | FRI->ColonLoc = EqualLoc; |
| 2228 | Init = ExprError(); |
| 2229 | FRI->RangeExpr = Init; |
| 2230 | } |
| 2231 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2232 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2233 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 2234 | ExitScope(); |
| 2235 | } |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 2236 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2237 | if (Init.isInvalid()) { |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2238 | SmallVector<tok::TokenKind, 2> StopTokens; |
| 2239 | StopTokens.push_back(tok::comma); |
Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 2240 | if (D.getContext() == Declarator::ForContext || |
| 2241 | D.getContext() == Declarator::InitStmtContext) |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 2242 | StopTokens.push_back(tok::r_paren); |
| 2243 | SkipUntil(StopTokens, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 604c302 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 2244 | Actions.ActOnInitializerError(ThisDecl); |
| 2245 | } else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2246 | Actions.AddInitializerToDecl(ThisDecl, Init.get(), |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2247 | /*DirectInit=*/false); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2248 | } |
| 2249 | } else if (Tok.is(tok::l_paren)) { |
| 2250 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2251 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2252 | T.consumeOpen(); |
| 2253 | |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2254 | ExprVector Exprs; |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2255 | CommaLocsTy CommaLocs; |
| 2256 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2257 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 2258 | EnterScope(0); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2259 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 2262 | if (ParseExpressionList(Exprs, CommaLocs, [&] { |
| 2263 | Actions.CodeCompleteConstructor(getCurScope(), |
| 2264 | cast<VarDecl>(ThisDecl)->getType()->getCanonicalTypeInternal(), |
| 2265 | ThisDecl->getLocation(), Exprs); |
| 2266 | })) { |
David Blaikie | eae0411 | 2012-10-10 23:15:05 +0000 | [diff] [blame] | 2267 | Actions.ActOnInitializerError(ThisDecl); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2268 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 2269 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2270 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2271 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 2272 | ExitScope(); |
| 2273 | } |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2274 | } else { |
| 2275 | // Match the ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2276 | T.consumeClose(); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2277 | |
| 2278 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 2279 | "Unexpected number of commas!"); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 2280 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2281 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2282 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 2283 | ExitScope(); |
| 2284 | } |
| 2285 | |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2286 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 2287 | T.getCloseLocation(), |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2288 | Exprs); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2289 | Actions.AddInitializerToDecl(ThisDecl, Initializer.get(), |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2290 | /*DirectInit=*/true); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2291 | } |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2292 | } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) && |
Fariborz Jahanian | 8be1ecd | 2012-07-03 23:22:13 +0000 | [diff] [blame] | 2293 | (!CurParsedObjCImpl || !D.isFunctionDeclarator())) { |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2294 | // Parse C++0x braced-init-list. |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2295 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 2296 | |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2297 | if (D.getCXXScopeSpec().isSet()) { |
| 2298 | EnterScope(0); |
| 2299 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 2300 | } |
| 2301 | |
| 2302 | ExprResult Init(ParseBraceInitializer()); |
| 2303 | |
| 2304 | if (D.getCXXScopeSpec().isSet()) { |
| 2305 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 2306 | ExitScope(); |
| 2307 | } |
| 2308 | |
| 2309 | if (Init.isInvalid()) { |
| 2310 | Actions.ActOnInitializerError(ThisDecl); |
| 2311 | } else |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2312 | Actions.AddInitializerToDecl(ThisDecl, Init.get(), /*DirectInit=*/true); |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2313 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2314 | } else { |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 2315 | Actions.ActOnUninitializedDecl(ThisDecl); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 2318 | Actions.FinalizeDeclaration(ThisDecl); |
| 2319 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2320 | return ThisDecl; |
| 2321 | } |
| 2322 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2323 | /// ParseSpecifierQualifierList |
| 2324 | /// specifier-qualifier-list: |
| 2325 | /// type-specifier specifier-qualifier-list[opt] |
| 2326 | /// type-qualifier specifier-qualifier-list[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 2327 | /// [GNU] attributes specifier-qualifier-list[opt] |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2328 | /// |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2329 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 2330 | DeclSpecContext DSC) { |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2331 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 2332 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2333 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2334 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2335 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2336 | // Validate declspec for type-name. |
| 2337 | unsigned Specs = DS.getParsedSpecifiers(); |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 2338 | if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) { |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2339 | Diag(Tok, diag::err_expected_type); |
| 2340 | DS.SetTypeSpecError(); |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 2341 | } else if (Specs == DeclSpec::PQ_None && !DS.hasAttributes()) { |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2342 | Diag(Tok, diag::err_typename_requires_specqual); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2343 | if (!DS.hasTypeSpecifier()) |
| 2344 | DS.SetTypeSpecError(); |
| 2345 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2346 | |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 2347 | // Issue diagnostic and remove storage class if present. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2348 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 2349 | if (DS.getStorageClassSpecLoc().isValid()) |
| 2350 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 2351 | else |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2352 | Diag(DS.getThreadStorageClassSpecLoc(), |
| 2353 | diag::err_typename_invalid_storageclass); |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 2354 | DS.ClearStorageClassSpecs(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2355 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2356 | |
Craig Topper | 3f13d4d2 | 2015-11-14 18:15:55 +0000 | [diff] [blame] | 2357 | // Issue diagnostic and remove function specifier if present. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2358 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2359 | if (DS.isInlineSpecified()) |
| 2360 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 2361 | if (DS.isVirtualSpecified()) |
| 2362 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 2363 | if (DS.isExplicitSpecified()) |
| 2364 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 2365 | DS.ClearFunctionSpecs(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2366 | } |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2367 | |
| 2368 | // Issue diagnostic and remove constexpr specfier if present. |
Meador Inge | f0af05c | 2015-06-25 22:06:40 +0000 | [diff] [blame] | 2369 | if (DS.isConstexprSpecified() && DSC != DSC_condition) { |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2370 | Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr); |
| 2371 | DS.ClearConstexprSpec(); |
| 2372 | } |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2373 | } |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 2374 | |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2375 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 2376 | /// specified token is valid after the identifier in a declarator which |
| 2377 | /// immediately follows the declspec. For example, these things are valid: |
| 2378 | /// |
| 2379 | /// int x [ 4]; // direct-declarator |
| 2380 | /// int x ( int y); // direct-declarator |
| 2381 | /// int(int x ) // direct-declarator |
| 2382 | /// int x ; // simple-declaration |
| 2383 | /// int x = 17; // init-declarator-list |
| 2384 | /// int x , y; // init-declarator-list |
| 2385 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | a723ba9 | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 2386 | /// int x : 4; // struct-declarator |
Chris Lattner | 2b988c1 | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 2387 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2388 | /// |
| 2389 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 2390 | /// ')' happens to be valid anyway). |
| 2391 | /// int (x) |
| 2392 | /// |
| 2393 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2394 | return T.isOneOf(tok::l_square, tok::l_paren, tok::r_paren, tok::semi, |
| 2395 | tok::comma, tok::equal, tok::kw_asm, tok::l_brace, |
| 2396 | tok::colon); |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2397 | } |
| 2398 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2399 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 2400 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 2401 | /// the declspec has no type specifier. In this case, the declspec is either |
| 2402 | /// malformed or is "implicit int" (in K&R and C89). |
| 2403 | /// |
| 2404 | /// This method handles diagnosing this prettily and returns false if the |
| 2405 | /// declspec is done being processed. If it recovers and thinks there may be |
| 2406 | /// other pieces of declspec after it, it returns true. |
| 2407 | /// |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2408 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2409 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | a0a5d50 | 2014-05-08 22:32:00 +0000 | [diff] [blame] | 2410 | AccessSpecifier AS, DeclSpecContext DSC, |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2411 | ParsedAttributesWithRange &Attrs) { |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2412 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2413 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2414 | SourceLocation Loc = Tok.getLocation(); |
| 2415 | // If we see an identifier that is not a type name, we normally would |
| 2416 | // parse it as the identifer being declared. However, when a typename |
| 2417 | // is typo'd or the definition is not included, this will incorrectly |
| 2418 | // parse the typename as the identifier name and fall over misparsing |
| 2419 | // later parts of the diagnostic. |
| 2420 | // |
| 2421 | // As such, we try to do some look-ahead in cases where this would |
| 2422 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 2423 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 2424 | // an identifier with implicit int, we'd get a parse error because the |
| 2425 | // next token is obviously invalid for a type. Parse these as a case |
| 2426 | // with an invalid type specifier. |
| 2427 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2428 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2429 | // 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] | 2430 | // error, do lookahead to try to do better recovery. This never applies |
| 2431 | // within a type specifier. Outside of C++, we allow this even if the |
| 2432 | // language doesn't "officially" support implicit int -- we support |
Richard Smith | 3b87038 | 2013-04-30 22:43:51 +0000 | [diff] [blame] | 2433 | // implicit int as an extension in C99 and C11. |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 2434 | if (!isTypeSpecifier(DSC) && !getLangOpts().CPlusPlus && |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2435 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2436 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 2437 | // we just avoid eating the identifier, so it will be parsed as the |
| 2438 | // identifier in the declarator. |
| 2439 | return false; |
| 2440 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2441 | |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2442 | if (getLangOpts().CPlusPlus && |
| 2443 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| 2444 | // Don't require a type specifier if we have the 'auto' storage class |
| 2445 | // specifier in C++98 -- we'll promote it to a type specifier. |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2446 | if (SS) |
| 2447 | AnnotateScopeToken(*SS, /*IsNewAnnotation*/false); |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2448 | return false; |
| 2449 | } |
| 2450 | |
Reid Kleckner | 9a96e42 | 2016-05-24 21:23:54 +0000 | [diff] [blame] | 2451 | if (getLangOpts().CPlusPlus && (!SS || SS->isEmpty()) && |
| 2452 | getLangOpts().MSVCCompat) { |
| 2453 | // Lookup of an unqualified type name has failed in MSVC compatibility mode. |
| 2454 | // Give Sema a chance to recover if we are in a template with dependent base |
| 2455 | // classes. |
| 2456 | if (ParsedType T = Actions.ActOnMSVCUnknownTypeName( |
| 2457 | *Tok.getIdentifierInfo(), Tok.getLocation(), |
| 2458 | DSC == DSC_template_type_arg)) { |
| 2459 | const char *PrevSpec; |
| 2460 | unsigned DiagID; |
| 2461 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T, |
| 2462 | Actions.getASTContext().getPrintingPolicy()); |
| 2463 | DS.SetRangeEnd(Tok.getLocation()); |
| 2464 | ConsumeToken(); |
| 2465 | return false; |
| 2466 | } |
| 2467 | } |
| 2468 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2469 | // Otherwise, if we don't consume this token, we are going to emit an |
| 2470 | // error anyway. Try to recover from various common problems. Check |
| 2471 | // to see if this was a reference to a tag name without a tag specified. |
| 2472 | // 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] | 2473 | // |
| 2474 | // C++ doesn't need this, and isTagName doesn't take SS. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2475 | if (SS == nullptr) { |
| 2476 | const char *TagName = nullptr, *FixitTagName = nullptr; |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2477 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2478 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2479 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2480 | default: break; |
Argyrios Kyrtzidis | 1f32940 | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2481 | case DeclSpec::TST_enum: |
| 2482 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 2483 | case DeclSpec::TST_union: |
| 2484 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 2485 | case DeclSpec::TST_struct: |
| 2486 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2487 | case DeclSpec::TST_interface: |
| 2488 | TagName="__interface"; FixitTagName = "__interface "; |
| 2489 | TagKind=tok::kw___interface;break; |
Argyrios Kyrtzidis | 1f32940 | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2490 | case DeclSpec::TST_class: |
| 2491 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2492 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2493 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2494 | if (TagName) { |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2495 | IdentifierInfo *TokenName = Tok.getIdentifierInfo(); |
| 2496 | LookupResult R(Actions, TokenName, SourceLocation(), |
| 2497 | Sema::LookupOrdinaryName); |
| 2498 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2499 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2500 | << TokenName << TagName << getLangOpts().CPlusPlus |
| 2501 | << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName); |
| 2502 | |
| 2503 | if (Actions.LookupParsedName(R, getCurScope(), SS)) { |
| 2504 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); |
| 2505 | I != IEnd; ++I) |
Kaelyn Uhrain | 3fe3f85 | 2012-04-27 18:26:49 +0000 | [diff] [blame] | 2506 | Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2507 | << TokenName << TagName; |
| 2508 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2509 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2510 | // Parse this as a tag as if the missing tag were present. |
| 2511 | if (TagKind == tok::kw_enum) |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2512 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal); |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2513 | else |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2514 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2515 | /*EnteringContext*/ false, DSC_normal, Attrs); |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2516 | return true; |
| 2517 | } |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2518 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2519 | |
Richard Smith | fe904f0 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2520 | // Determine whether this identifier could plausibly be the name of something |
Richard Smith | edd124e | 2012-05-15 21:42:17 +0000 | [diff] [blame] | 2521 | // being declared (with a missing type). |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 2522 | if (!isTypeSpecifier(DSC) && |
Richard Smith | fe904f0 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2523 | (!SS || DSC == DSC_top_level || DSC == DSC_class)) { |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2524 | // Look ahead to the next token to try to figure out what this declaration |
| 2525 | // was supposed to be. |
| 2526 | switch (NextToken().getKind()) { |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2527 | case tok::l_paren: { |
| 2528 | // static x(4); // 'x' is not a type |
| 2529 | // x(int n); // 'x' is not a type |
| 2530 | // x (*p)[]; // 'x' is a type |
| 2531 | // |
Richard Smith | a0a5d50 | 2014-05-08 22:32:00 +0000 | [diff] [blame] | 2532 | // Since we're in an error case, we can afford to perform a tentative |
| 2533 | // parse to determine which case we're in. |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2534 | TentativeParsingAction PA(*this); |
| 2535 | ConsumeToken(); |
| 2536 | TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false); |
| 2537 | PA.Revert(); |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2538 | |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 2539 | if (TPR != TPResult::False) { |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2540 | // The identifier is followed by a parenthesized declarator. |
| 2541 | // It's supposed to be a type. |
| 2542 | break; |
| 2543 | } |
| 2544 | |
| 2545 | // If we're in a context where we could be declaring a constructor, |
| 2546 | // check whether this is a constructor declaration with a bogus name. |
| 2547 | if (DSC == DSC_class || (DSC == DSC_top_level && SS)) { |
| 2548 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2549 | if (Actions.isCurrentClassNameTypo(II, SS)) { |
| 2550 | Diag(Loc, diag::err_constructor_bad_name) |
| 2551 | << Tok.getIdentifierInfo() << II |
| 2552 | << FixItHint::CreateReplacement(Tok.getLocation(), II->getName()); |
| 2553 | Tok.setIdentifierInfo(II); |
| 2554 | } |
| 2555 | } |
| 2556 | // Fall through. |
Galina Kistanova | 7767425 | 2017-06-01 21:15:34 +0000 | [diff] [blame] | 2557 | LLVM_FALLTHROUGH; |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2558 | } |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2559 | case tok::comma: |
| 2560 | case tok::equal: |
| 2561 | case tok::kw_asm: |
| 2562 | case tok::l_brace: |
| 2563 | case tok::l_square: |
| 2564 | case tok::semi: |
| 2565 | // This looks like a variable or function declaration. The type is |
| 2566 | // probably missing. We're done parsing decl-specifiers. |
| 2567 | if (SS) |
| 2568 | AnnotateScopeToken(*SS, /*IsNewAnnotation*/false); |
| 2569 | return false; |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2570 | |
| 2571 | default: |
| 2572 | // This is probably supposed to be a type. This includes cases like: |
| 2573 | // int f(itn); |
| 2574 | // struct S { unsinged : 4; }; |
| 2575 | break; |
| 2576 | } |
| 2577 | } |
| 2578 | |
Reid Kleckner | c05ca5e | 2014-06-19 01:23:22 +0000 | [diff] [blame] | 2579 | // This is almost certainly an invalid type name. Let Sema emit a diagnostic |
| 2580 | // and attempt to recover. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2581 | ParsedType T; |
Kaelyn Uhrain | b5b17fe | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2582 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Richard Smith | 52f8d19 | 2017-05-10 21:32:16 +0000 | [diff] [blame] | 2583 | bool IsTemplateName = getLangOpts().CPlusPlus && NextToken().is(tok::less); |
Reid Kleckner | c05ca5e | 2014-06-19 01:23:22 +0000 | [diff] [blame] | 2584 | Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T, |
Richard Smith | 52f8d19 | 2017-05-10 21:32:16 +0000 | [diff] [blame] | 2585 | IsTemplateName); |
Reid Kleckner | c05ca5e | 2014-06-19 01:23:22 +0000 | [diff] [blame] | 2586 | if (T) { |
| 2587 | // The action has suggested that the type T could be used. Set that as |
| 2588 | // the type in the declaration specifiers, consume the would-be type |
| 2589 | // name token, and we're done. |
| 2590 | const char *PrevSpec; |
| 2591 | unsigned DiagID; |
| 2592 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T, |
| 2593 | Actions.getASTContext().getPrintingPolicy()); |
| 2594 | DS.SetRangeEnd(Tok.getLocation()); |
| 2595 | ConsumeToken(); |
| 2596 | // There may be other declaration specifiers after this. |
| 2597 | return true; |
| 2598 | } else if (II != Tok.getIdentifierInfo()) { |
| 2599 | // If no type was suggested, the correction is to a keyword |
| 2600 | Tok.setKind(II->getTokenID()); |
| 2601 | // There may be other declaration specifiers after this. |
| 2602 | return true; |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2603 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2604 | |
Reid Kleckner | c05ca5e | 2014-06-19 01:23:22 +0000 | [diff] [blame] | 2605 | // 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] | 2606 | DS.SetTypeSpecError(); |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2607 | DS.SetRangeEnd(Tok.getLocation()); |
| 2608 | ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2609 | |
Richard Smith | 52f8d19 | 2017-05-10 21:32:16 +0000 | [diff] [blame] | 2610 | // Eat any following template arguments. |
| 2611 | if (IsTemplateName) { |
| 2612 | SourceLocation LAngle, RAngle; |
| 2613 | TemplateArgList Args; |
| 2614 | ParseTemplateIdAfterTemplateName(true, LAngle, Args, RAngle); |
| 2615 | } |
| 2616 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2617 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 2618 | // avoid rippling error messages on subsequent uses of the same type, |
| 2619 | // could be useful if #include was forgotten. |
| 2620 | return false; |
| 2621 | } |
| 2622 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2623 | /// \brief Determine the declaration specifier context from the declarator |
| 2624 | /// context. |
| 2625 | /// |
| 2626 | /// \param Context the declarator context, which is one of the |
| 2627 | /// Declarator::TheContext enumerator values. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2628 | Parser::DeclSpecContext |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2629 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 2630 | if (Context == Declarator::MemberContext) |
| 2631 | return DSC_class; |
| 2632 | if (Context == Declarator::FileContext) |
| 2633 | return DSC_top_level; |
Akira Hatanaka | 12ddcee | 2017-06-26 18:46:12 +0000 | [diff] [blame] | 2634 | if (Context == Declarator::TemplateParamContext) |
| 2635 | return DSC_template_param; |
Reid Kleckner | df6e4a0 | 2014-06-06 22:36:36 +0000 | [diff] [blame] | 2636 | if (Context == Declarator::TemplateTypeArgContext) |
| 2637 | return DSC_template_type_arg; |
Richard Smith | 62dad82 | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 2638 | if (Context == Declarator::TrailingReturnContext) |
| 2639 | return DSC_trailing; |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 2640 | if (Context == Declarator::AliasDeclContext || |
| 2641 | Context == Declarator::AliasTemplateContext) |
| 2642 | return DSC_alias_declaration; |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2643 | return DSC_normal; |
| 2644 | } |
| 2645 | |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2646 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 2647 | /// |
| 2648 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 2649 | /// type. Ideally, the type should be propagated directly into Sema. |
| 2650 | /// |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2651 | /// [C11] type-id |
| 2652 | /// [C11] constant-expression |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2653 | /// [C++0x] type-id ...[opt] |
| 2654 | /// [C++0x] assignment-expression ...[opt] |
| 2655 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 2656 | SourceLocation &EllipsisLoc) { |
| 2657 | ExprResult ER; |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2658 | if (isTypeIdInParens()) { |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2659 | SourceLocation TypeLoc = Tok.getLocation(); |
| 2660 | ParsedType Ty = ParseTypeName().get(); |
| 2661 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2662 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 2663 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2664 | } else |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2665 | ER = ParseConstantExpression(); |
| 2666 | |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 2667 | if (getLangOpts().CPlusPlus11) |
| 2668 | TryConsumeToken(tok::ellipsis, EllipsisLoc); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2669 | |
| 2670 | return ER; |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 2674 | /// attribute to Attrs. |
| 2675 | /// |
| 2676 | /// alignment-specifier: |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2677 | /// [C11] '_Alignas' '(' type-id ')' |
| 2678 | /// [C11] '_Alignas' '(' constant-expression ')' |
Richard Smith | d11c7a1 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2679 | /// [C++11] 'alignas' '(' type-id ...[opt] ')' |
| 2680 | /// [C++11] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2681 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2682 | SourceLocation *EndLoc) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2683 | assert(Tok.isOneOf(tok::kw_alignas, tok::kw__Alignas) && |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2684 | "Not an alignment-specifier!"); |
| 2685 | |
Richard Smith | d11c7a1 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2686 | IdentifierInfo *KWName = Tok.getIdentifierInfo(); |
| 2687 | SourceLocation KWLoc = ConsumeToken(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2688 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2689 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 2690 | if (T.expectAndConsume()) |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2691 | return; |
| 2692 | |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2693 | SourceLocation EllipsisLoc; |
| 2694 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2695 | if (ArgExpr.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2696 | T.skipToEnd(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2697 | return; |
| 2698 | } |
| 2699 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2700 | T.consumeClose(); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2701 | if (EndLoc) |
| 2702 | *EndLoc = T.getCloseLocation(); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2703 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2704 | ArgsVector ArgExprs; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2705 | ArgExprs.push_back(ArgExpr.get()); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2706 | Attrs.addNew(KWName, KWLoc, nullptr, KWLoc, ArgExprs.data(), 1, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2707 | AttributeList::AS_Keyword, EllipsisLoc); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2708 | } |
| 2709 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2710 | /// Determine whether we're looking at something that might be a declarator |
| 2711 | /// in a simple-declaration. If it can't possibly be a declarator, maybe |
| 2712 | /// diagnose a missing semicolon after a prior tag definition in the decl |
| 2713 | /// specifier. |
| 2714 | /// |
| 2715 | /// \return \c true if an error occurred and this can't be any kind of |
| 2716 | /// declaration. |
| 2717 | bool |
| 2718 | Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS, |
| 2719 | DeclSpecContext DSContext, |
| 2720 | LateParsedAttrList *LateAttrs) { |
| 2721 | assert(DS.hasTagDefinition() && "shouldn't call this"); |
| 2722 | |
| 2723 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2724 | |
| 2725 | if (getLangOpts().CPlusPlus && |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2726 | Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype, |
| 2727 | tok::annot_template_id) && |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2728 | TryAnnotateCXXScopeToken(EnteringContext)) { |
| 2729 | SkipMalformedDecl(); |
| 2730 | return true; |
| 2731 | } |
| 2732 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2733 | bool HasScope = Tok.is(tok::annot_cxxscope); |
| 2734 | // Make a copy in case GetLookAheadToken invalidates the result of NextToken. |
| 2735 | Token AfterScope = HasScope ? NextToken() : Tok; |
| 2736 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2737 | // Determine whether the following tokens could possibly be a |
| 2738 | // declarator. |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2739 | bool MightBeDeclarator = true; |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2740 | if (Tok.isOneOf(tok::kw_typename, tok::annot_typename)) { |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2741 | // A declarator-id can't start with 'typename'. |
| 2742 | MightBeDeclarator = false; |
| 2743 | } else if (AfterScope.is(tok::annot_template_id)) { |
| 2744 | // If we have a type expressed as a template-id, this cannot be a |
| 2745 | // declarator-id (such a type cannot be redeclared in a simple-declaration). |
| 2746 | TemplateIdAnnotation *Annot = |
| 2747 | static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue()); |
| 2748 | if (Annot->Kind == TNK_Type_template) |
| 2749 | MightBeDeclarator = false; |
| 2750 | } else if (AfterScope.is(tok::identifier)) { |
| 2751 | const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken(); |
| 2752 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2753 | // These tokens cannot come after the declarator-id in a |
| 2754 | // simple-declaration, and are likely to come after a type-specifier. |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 2755 | if (Next.isOneOf(tok::star, tok::amp, tok::ampamp, tok::identifier, |
| 2756 | tok::annot_cxxscope, tok::coloncolon)) { |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2757 | // Missing a semicolon. |
| 2758 | MightBeDeclarator = false; |
| 2759 | } else if (HasScope) { |
| 2760 | // If the declarator-id has a scope specifier, it must redeclare a |
| 2761 | // previously-declared entity. If that's a type (and this is not a |
| 2762 | // typedef), that's an error. |
| 2763 | CXXScopeSpec SS; |
| 2764 | Actions.RestoreNestedNameSpecifierAnnotation( |
| 2765 | Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS); |
| 2766 | IdentifierInfo *Name = AfterScope.getIdentifierInfo(); |
| 2767 | Sema::NameClassification Classification = Actions.ClassifyName( |
| 2768 | getCurScope(), SS, Name, AfterScope.getLocation(), Next, |
| 2769 | /*IsAddressOfOperand*/false); |
| 2770 | switch (Classification.getKind()) { |
| 2771 | case Sema::NC_Error: |
| 2772 | SkipMalformedDecl(); |
| 2773 | return true; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2774 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2775 | case Sema::NC_Keyword: |
| 2776 | case Sema::NC_NestedNameSpecifier: |
| 2777 | llvm_unreachable("typo correction and nested name specifiers not " |
| 2778 | "possible here"); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2779 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2780 | case Sema::NC_Type: |
| 2781 | case Sema::NC_TypeTemplate: |
| 2782 | // Not a previously-declared non-type entity. |
| 2783 | MightBeDeclarator = false; |
| 2784 | break; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2785 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2786 | case Sema::NC_Unknown: |
| 2787 | case Sema::NC_Expression: |
| 2788 | case Sema::NC_VarTemplate: |
| 2789 | case Sema::NC_FunctionTemplate: |
| 2790 | // Might be a redeclaration of a prior entity. |
| 2791 | break; |
| 2792 | } |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2793 | } |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2796 | if (MightBeDeclarator) |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2797 | return false; |
| 2798 | |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2799 | const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy(); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2800 | Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()), |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 2801 | diag::err_expected_after) |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2802 | << DeclSpec::getSpecifierName(DS.getTypeSpecType(), PPol) << tok::semi; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2803 | |
| 2804 | // Try to recover from the typo, by dropping the tag definition and parsing |
| 2805 | // the problematic tokens as a type. |
| 2806 | // |
| 2807 | // FIXME: Split the DeclSpec into pieces for the standalone |
| 2808 | // declaration and pieces for the following declaration, instead |
| 2809 | // of assuming that all the other pieces attach to new declaration, |
| 2810 | // and call ParsedFreeStandingDeclSpec as appropriate. |
| 2811 | DS.ClearTypeSpecType(); |
| 2812 | ParsedTemplateInfo NotATemplate; |
| 2813 | ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs); |
| 2814 | return false; |
| 2815 | } |
| 2816 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2817 | /// ParseDeclarationSpecifiers |
| 2818 | /// declaration-specifiers: [C99 6.7] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2819 | /// storage-class-specifier declaration-specifiers[opt] |
| 2820 | /// type-specifier declaration-specifiers[opt] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2821 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2822 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 2823 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2824 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 2825 | /// [ObjC1] '__kindof' declaration-specifiers[opt] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2826 | /// |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2827 | /// storage-class-specifier: [C99 6.7.1] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 2828 | /// 'typedef' |
| 2829 | /// 'extern' |
| 2830 | /// 'static' |
| 2831 | /// 'auto' |
| 2832 | /// 'register' |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2833 | /// [C++] 'mutable' |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2834 | /// [C++11] 'thread_local' |
| 2835 | /// [C11] '_Thread_local' |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 2836 | /// [GNU] '__thread' |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2837 | /// function-specifier: [C99 6.7.4] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2838 | /// [C99] 'inline' |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2839 | /// [C++] 'virtual' |
| 2840 | /// [C++] 'explicit' |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2841 | /// [OpenCL] '__kernel' |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2842 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 39c2a8b | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2843 | /// 'constexpr': [C++0x dcl.constexpr] |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 2844 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2845 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2846 | AccessSpecifier AS, |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2847 | DeclSpecContext DSContext, |
| 2848 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2849 | if (DS.getSourceRange().isInvalid()) { |
David Majnemer | 24b2830 | 2014-07-26 05:41:31 +0000 | [diff] [blame] | 2850 | // Start the range at the current token but make the end of the range |
| 2851 | // invalid. This will make the entire range invalid unless we successfully |
| 2852 | // consume a token. |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2853 | DS.SetRangeStart(Tok.getLocation()); |
David Majnemer | 24b2830 | 2014-07-26 05:41:31 +0000 | [diff] [blame] | 2854 | DS.SetRangeEnd(SourceLocation()); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2855 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2856 | |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2857 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2858 | bool AttrsLastTime = false; |
| 2859 | ParsedAttributesWithRange attrs(AttrFactory); |
Benjamin Kramer | e481214 | 2015-03-12 14:28:38 +0000 | [diff] [blame] | 2860 | // We use Sema's policy to get bool macros right. |
Richard Smith | 301bc21 | 2016-05-19 01:39:10 +0000 | [diff] [blame] | 2861 | PrintingPolicy Policy = Actions.getPrintingPolicy(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2862 | while (1) { |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2863 | bool isInvalid = false; |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 2864 | bool isStorageClass = false; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2865 | const char *PrevSpec = nullptr; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2866 | unsigned DiagID = 0; |
| 2867 | |
David Majnemer | 51fd8a0 | 2015-07-22 23:46:18 +0000 | [diff] [blame] | 2868 | // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL |
| 2869 | // implementation for VS2013 uses _Atomic as an identifier for one of the |
| 2870 | // classes in <atomic>. |
| 2871 | // |
| 2872 | // A typedef declaration containing _Atomic<...> is among the places where |
| 2873 | // the class is used. If we are currently parsing such a declaration, treat |
| 2874 | // the token as an identifier. |
| 2875 | if (getLangOpts().MSVCCompat && Tok.is(tok::kw__Atomic) && |
| 2876 | DS.getStorageClassSpec() == clang::DeclSpec::SCS_typedef && |
| 2877 | !DS.hasTypeSpecifier() && GetLookAheadToken(1).is(tok::less)) |
| 2878 | Tok.setKind(tok::identifier); |
| 2879 | |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 2880 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2881 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2882 | switch (Tok.getKind()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2883 | default: |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2884 | DoneWithDeclSpec: |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2885 | if (!AttrsLastTime) |
| 2886 | ProhibitAttributes(attrs); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2887 | else { |
| 2888 | // Reject C++11 attributes that appertain to decl specifiers as |
| 2889 | // we don't support any C++11 attributes that appertain to decl |
| 2890 | // specifiers. This also conforms to what g++ 4.8 is doing. |
Richard Smith | 49cc1cc | 2016-08-18 21:59:42 +0000 | [diff] [blame] | 2891 | ProhibitCXX11Attributes(attrs, diag::err_attribute_not_type_attr); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2892 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2893 | DS.takeAttributesFrom(attrs); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2894 | } |
Peter Collingbourne | 70188b3 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 2895 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2896 | // If this is not a declaration specifier token, we're done reading decl |
| 2897 | // specifiers. First verify that DeclSpec's are consistent. |
Craig Topper | 2512241 | 2015-11-15 03:32:11 +0000 | [diff] [blame] | 2898 | DS.Finish(Actions, Policy); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2899 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2900 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2901 | case tok::l_square: |
| 2902 | case tok::kw_alignas: |
Richard Smith | 4cabd04 | 2013-02-22 09:15:49 +0000 | [diff] [blame] | 2903 | if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier()) |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2904 | goto DoneWithDeclSpec; |
| 2905 | |
| 2906 | ProhibitAttributes(attrs); |
| 2907 | // FIXME: It would be good to recover by accepting the attributes, |
| 2908 | // but attempting to do that now would cause serious |
| 2909 | // madness in terms of diagnostics. |
| 2910 | attrs.clear(); |
| 2911 | attrs.Range = SourceRange(); |
| 2912 | |
| 2913 | ParseCXX11Attributes(attrs); |
| 2914 | AttrsLastTime = true; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2915 | continue; |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2916 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2917 | case tok::code_completion: { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2918 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2919 | if (DS.hasTypeSpecifier()) { |
| 2920 | bool AllowNonIdentifiers |
| 2921 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 2922 | Scope::BlockScope | |
| 2923 | Scope::TemplateParamScope | |
| 2924 | Scope::FunctionPrototypeScope | |
| 2925 | Scope::AtCatchScope)) == 0; |
| 2926 | bool AllowNestedNameSpecifiers |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2927 | = DSContext == DSC_top_level || |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2928 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 2929 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2930 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2931 | AllowNonIdentifiers, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2932 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2933 | return cutOffParsing(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2936 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 2937 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 2938 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2939 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2940 | : Sema::PCC_Template; |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2941 | else if (DSContext == DSC_class) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2942 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | b6c6a58 | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 2943 | else if (CurParsedObjCImpl) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2944 | CCC = Sema::PCC_ObjCImplementation; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2945 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2946 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2947 | return cutOffParsing(); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2948 | } |
| 2949 | |
Chris Lattner | bd31aa3 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2950 | case tok::coloncolon: // ::foo::bar |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2951 | // C++ scope specifier. Annotate and loop, or bail out on error. |
Eli Friedman | 2a1d9a9 | 2013-08-15 23:59:20 +0000 | [diff] [blame] | 2952 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2953 | if (!DS.hasTypeSpecifier()) |
| 2954 | DS.SetTypeSpecError(); |
| 2955 | goto DoneWithDeclSpec; |
| 2956 | } |
John McCall | 8bc2a70 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 2957 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 2958 | goto DoneWithDeclSpec; |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2959 | continue; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2960 | |
| 2961 | case tok::annot_cxxscope: { |
Richard Smith | 3092a3b | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2962 | if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector()) |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2963 | goto DoneWithDeclSpec; |
| 2964 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2965 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 2966 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 2967 | Tok.getAnnotationRange(), |
| 2968 | SS); |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2969 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2970 | // We are looking for a qualified typename. |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2971 | Token Next = NextToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2972 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2973 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2974 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2975 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2976 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 2977 | // If this would be a valid constructor declaration with template |
| 2978 | // arguments, we will reject the attempt to form an invalid type-id |
| 2979 | // referring to the injected-class-name when we annotate the token, |
| 2980 | // per C++ [class.qual]p2. |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2981 | // |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 2982 | // To improve diagnostics for this case, parse the declaration as a |
| 2983 | // constructor (and reject the extra template arguments later). |
Argyrios Kyrtzidis | c0c5dd2 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2984 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
Dmitri Gribenko | d1c91f1 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2985 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2986 | TemplateId->Name && |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 2987 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS) && |
| 2988 | isConstructorDeclarator(/*Unqualified*/false)) { |
| 2989 | // The user meant this to be an out-of-line constructor |
| 2990 | // definition, but template arguments are not allowed |
| 2991 | // there. Just allow this as a constructor; we'll |
| 2992 | // complain about it later. |
| 2993 | goto DoneWithDeclSpec; |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2994 | } |
| 2995 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2996 | DS.getTypeSpecScope() = SS; |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 2997 | ConsumeAnnotationToken(); // The C++ scope. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2998 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2999 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 3000 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 3001 | continue; |
| 3002 | } |
| 3003 | |
Douglas Gregor | c5790df | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 3004 | if (Next.is(tok::annot_typename)) { |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 3005 | DS.getTypeSpecScope() = SS; |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3006 | ConsumeAnnotationToken(); // The C++ scope. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3007 | if (Tok.getAnnotationValue()) { |
| 3008 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 7743034 | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 3009 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3010 | Tok.getAnnotationEndLoc(), |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3011 | PrevSpec, DiagID, T, Policy); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 3012 | if (isInvalid) |
| 3013 | break; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3014 | } |
Douglas Gregor | c5790df | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 3015 | else |
| 3016 | DS.SetTypeSpecError(); |
| 3017 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3018 | ConsumeAnnotationToken(); // The typename |
Douglas Gregor | c5790df | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 3021 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3022 | goto DoneWithDeclSpec; |
| 3023 | |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3024 | // Check whether this is a constructor declaration. If we're in a |
| 3025 | // context where the identifier could be a class name, and it has the |
| 3026 | // shape of a constructor declaration, process it as one. |
Dmitri Gribenko | d1c91f1 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 3027 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3028 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3029 | &SS) && |
| 3030 | isConstructorDeclarator(/*Unqualified*/ false)) |
| 3031 | goto DoneWithDeclSpec; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3032 | |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 3033 | ParsedType TypeRep = |
| 3034 | Actions.getTypeName(*Next.getIdentifierInfo(), Next.getLocation(), |
| 3035 | getCurScope(), &SS, false, false, nullptr, |
| 3036 | /*IsCtorOrDtorName=*/false, |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 3037 | /*WantNonTrivialSourceInfo=*/true, |
| 3038 | isClassTemplateDeductionContext(DSContext)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3039 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 3040 | // If the referenced identifier is not a type, then this declspec is |
| 3041 | // erroneous: We already checked about that it has no type specifier, and |
| 3042 | // 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] | 3043 | // typename. |
David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 3044 | if (!TypeRep) { |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3045 | // Eat the scope spec so the identifier is current. |
| 3046 | ConsumeAnnotationToken(); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3047 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 3048 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) { |
| 3049 | if (!Attrs.empty()) { |
| 3050 | AttrsLastTime = true; |
| 3051 | attrs.takeAllFrom(Attrs); |
| 3052 | } |
| 3053 | continue; |
| 3054 | } |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3055 | goto DoneWithDeclSpec; |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 3056 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3057 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 3058 | DS.getTypeSpecScope() = SS; |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3059 | ConsumeAnnotationToken(); // The C++ scope. |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3060 | |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3061 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3062 | DiagID, TypeRep, Policy); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3063 | if (isInvalid) |
| 3064 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3065 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3066 | DS.SetRangeEnd(Tok.getLocation()); |
| 3067 | ConsumeToken(); // The typename. |
| 3068 | |
| 3069 | continue; |
| 3070 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3071 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3072 | case tok::annot_typename: { |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 3073 | // If we've previously seen a tag definition, we were almost surely |
| 3074 | // missing a semicolon after it. |
| 3075 | if (DS.hasTypeSpecifier() && DS.hasTagDefinition()) |
| 3076 | goto DoneWithDeclSpec; |
| 3077 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3078 | if (Tok.getAnnotationValue()) { |
| 3079 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 7f8bb36 | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 3080 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3081 | DiagID, T, Policy); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3082 | } else |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 3083 | DS.SetTypeSpecError(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3084 | |
Chris Lattner | 005fc1b | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 3085 | if (isInvalid) |
| 3086 | break; |
| 3087 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3088 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 3089 | ConsumeAnnotationToken(); // The typename |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3090 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3091 | continue; |
| 3092 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3093 | |
Douglas Gregor | 0687309 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 3094 | case tok::kw___is_signed: |
| 3095 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 3096 | // typically treats it as a trait. If we see __is_signed as it appears |
| 3097 | // in libstdc++, e.g., |
| 3098 | // |
| 3099 | // static const bool __is_signed; |
| 3100 | // |
| 3101 | // then treat __is_signed as an identifier rather than as a keyword. |
| 3102 | if (DS.getTypeSpecType() == TST_bool && |
| 3103 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
Alp Toker | 47642d2 | 2013-12-03 06:13:01 +0000 | [diff] [blame] | 3104 | DS.getStorageClassSpec() == DeclSpec::SCS_static) |
| 3105 | TryKeywordIdentFallback(true); |
Douglas Gregor | 0687309 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 3106 | |
| 3107 | // We're done with the declaration-specifiers. |
| 3108 | goto DoneWithDeclSpec; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3109 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3110 | // typedef-name |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 3111 | case tok::kw___super: |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3112 | case tok::kw_decltype: |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3113 | case tok::identifier: { |
Reid Kleckner | c582f01 | 2014-07-14 18:19:58 +0000 | [diff] [blame] | 3114 | // This identifier can only be a typedef name if we haven't already seen |
| 3115 | // a type-specifier. Without this check we misparse: |
| 3116 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 3117 | if (DS.hasTypeSpecifier()) |
| 3118 | goto DoneWithDeclSpec; |
| 3119 | |
Aaron Ballman | 52d0aaa | 2017-02-14 22:47:20 +0000 | [diff] [blame] | 3120 | // If the token is an identifier named "__declspec" and Microsoft |
| 3121 | // extensions are not enabled, it is likely that there will be cascading |
| 3122 | // parse errors if this really is a __declspec attribute. Attempt to |
| 3123 | // recognize that scenario and recover gracefully. |
| 3124 | if (!getLangOpts().DeclSpecKeyword && Tok.is(tok::identifier) && |
| 3125 | Tok.getIdentifierInfo()->getName().equals("__declspec")) { |
| 3126 | Diag(Loc, diag::err_ms_attributes_not_enabled); |
| 3127 | |
| 3128 | // The next token should be an open paren. If it is, eat the entire |
| 3129 | // attribute declaration and continue. |
| 3130 | if (NextToken().is(tok::l_paren)) { |
| 3131 | // Consume the __declspec identifier. |
Richard Trieu | ba05737 | 2017-02-14 23:56:55 +0000 | [diff] [blame] | 3132 | ConsumeToken(); |
Aaron Ballman | 52d0aaa | 2017-02-14 22:47:20 +0000 | [diff] [blame] | 3133 | |
| 3134 | // Eat the parens and everything between them. |
| 3135 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 3136 | if (T.consumeOpen()) { |
| 3137 | assert(false && "Not a left paren?"); |
| 3138 | return; |
| 3139 | } |
| 3140 | T.skipToEnd(); |
| 3141 | continue; |
| 3142 | } |
| 3143 | } |
| 3144 | |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 3145 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 3146 | // so handle it as such. This is important for ctor parsing. |
| 3147 | if (getLangOpts().CPlusPlus) { |
| 3148 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
| 3149 | DS.SetTypeSpecError(); |
| 3150 | goto DoneWithDeclSpec; |
| 3151 | } |
| 3152 | if (!Tok.is(tok::identifier)) |
| 3153 | continue; |
| 3154 | } |
| 3155 | |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3156 | // Check for need to substitute AltiVec keyword tokens. |
| 3157 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 3158 | break; |
| 3159 | |
Richard Smith | 3092a3b | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 3160 | // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not |
| 3161 | // allow the use of a typedef name as a type specifier. |
| 3162 | if (DS.isTypeAltiVecVector()) |
| 3163 | goto DoneWithDeclSpec; |
| 3164 | |
Douglas Gregor | 5c0870a | 2015-06-19 23:18:00 +0000 | [diff] [blame] | 3165 | if (DSContext == DSC_objc_method_result && isObjCInstancetype()) { |
| 3166 | ParsedType TypeRep = Actions.ActOnObjCInstanceType(Loc); |
| 3167 | assert(TypeRep); |
| 3168 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
| 3169 | DiagID, TypeRep, Policy); |
| 3170 | if (isInvalid) |
| 3171 | break; |
| 3172 | |
| 3173 | DS.SetRangeEnd(Loc); |
| 3174 | ConsumeToken(); |
| 3175 | continue; |
| 3176 | } |
| 3177 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 3178 | ParsedType TypeRep = Actions.getTypeName( |
| 3179 | *Tok.getIdentifierInfo(), Tok.getLocation(), getCurScope(), nullptr, |
| 3180 | false, false, nullptr, false, false, |
| 3181 | isClassTemplateDeductionContext(DSContext)); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3182 | |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 3183 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 3184 | // it must be an implicit int or an error. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3185 | if (!TypeRep) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3186 | ParsedAttributesWithRange Attrs(AttrFactory); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 3187 | if (ParseImplicitInt(DS, nullptr, TemplateInfo, AS, DSContext, Attrs)) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3188 | if (!Attrs.empty()) { |
| 3189 | AttrsLastTime = true; |
| 3190 | attrs.takeAllFrom(Attrs); |
| 3191 | } |
| 3192 | continue; |
| 3193 | } |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3194 | goto DoneWithDeclSpec; |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 3195 | } |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3196 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3197 | // If we're in a context where the identifier could be a class name, |
| 3198 | // check whether this is a constructor declaration. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3199 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3200 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 3201 | isConstructorDeclarator(/*Unqualified*/true)) |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3202 | goto DoneWithDeclSpec; |
| 3203 | |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 3204 | // Likewise, if this is a context where the identifier could be a template |
| 3205 | // name, check whether this is a deduction guide declaration. |
| 3206 | if (getLangOpts().CPlusPlus1z && |
| 3207 | (DSContext == DSC_class || DSContext == DSC_top_level) && |
| 3208 | Actions.isDeductionGuideName(getCurScope(), *Tok.getIdentifierInfo(), |
| 3209 | Tok.getLocation()) && |
| 3210 | isConstructorDeclarator(/*Unqualified*/ true, |
| 3211 | /*DeductionGuide*/ true)) |
| 3212 | goto DoneWithDeclSpec; |
| 3213 | |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 3214 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3215 | DiagID, TypeRep, Policy); |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3216 | if (isInvalid) |
| 3217 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3218 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3219 | DS.SetRangeEnd(Tok.getLocation()); |
| 3220 | ConsumeToken(); // The identifier |
| 3221 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3222 | // Objective-C supports type arguments and protocol references |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 3223 | // following an Objective-C object or object pointer |
| 3224 | // type. Handle either one of them. |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3225 | if (Tok.is(tok::less) && getLangOpts().ObjC1) { |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 3226 | SourceLocation NewEndLoc; |
| 3227 | TypeResult NewTypeRep = parseObjCTypeArgsAndProtocolQualifiers( |
| 3228 | Loc, TypeRep, /*consumeLastToken=*/true, |
| 3229 | NewEndLoc); |
| 3230 | if (NewTypeRep.isUsable()) { |
| 3231 | DS.UpdateTypeRep(NewTypeRep.get()); |
| 3232 | DS.SetRangeEnd(NewEndLoc); |
| 3233 | } |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3234 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3235 | |
Steve Naroff | cd5e782 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 3236 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 3237 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 3238 | continue; |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3239 | } |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3240 | |
| 3241 | // type-name |
| 3242 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | c0c5dd2 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 3243 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 3244 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3245 | // This template-id does not refer to a type name, so we're |
| 3246 | // done with the type-specifiers. |
| 3247 | goto DoneWithDeclSpec; |
| 3248 | } |
| 3249 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3250 | // If we're in a context where the template-id could be a |
| 3251 | // constructor name or specialization, check whether this is a |
| 3252 | // constructor declaration. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3253 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3254 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 3255 | isConstructorDeclarator(TemplateId->SS.isEmpty())) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3256 | goto DoneWithDeclSpec; |
| 3257 | |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3258 | // Turn the template-id annotation token into a type annotation |
| 3259 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 3260 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3261 | continue; |
| 3262 | } |
| 3263 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 3264 | // GNU attributes support. |
| 3265 | case tok::kw___attribute: |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 3266 | ParseGNUAttributes(DS.getAttributes(), nullptr, LateAttrs); |
Chris Lattner | b95cca0 | 2006-10-17 03:01:08 +0000 | [diff] [blame] | 3267 | continue; |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 3268 | |
| 3269 | // Microsoft declspec support. |
| 3270 | case tok::kw___declspec: |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 3271 | ParseMicrosoftDeclSpecs(DS.getAttributes()); |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 3272 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3273 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3274 | // Microsoft single token adornments. |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 3275 | case tok::kw___forceinline: { |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 3276 | isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID); |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 3277 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 3278 | SourceLocation AttrNameLoc = Tok.getLocation(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 3279 | DS.getAttributes().addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, |
| 3280 | nullptr, 0, AttributeList::AS_Keyword); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 3281 | break; |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 3282 | } |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3283 | |
Andrey Bokhanko | 45d4132 | 2016-05-11 18:38:21 +0000 | [diff] [blame] | 3284 | case tok::kw___unaligned: |
| 3285 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID, |
| 3286 | getLangOpts()); |
| 3287 | break; |
| 3288 | |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 3289 | case tok::kw___sptr: |
| 3290 | case tok::kw___uptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3291 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3292 | case tok::kw___ptr32: |
Steve Naroff | f9c29d4 | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 3293 | case tok::kw___w64: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3294 | case tok::kw___cdecl: |
| 3295 | case tok::kw___stdcall: |
| 3296 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3297 | case tok::kw___thiscall: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3298 | case tok::kw___regcall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 3299 | case tok::kw___vectorcall: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3300 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3301 | continue; |
| 3302 | |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3303 | // Borland single token adornments. |
| 3304 | case tok::kw___pascal: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3305 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3306 | continue; |
| 3307 | |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3308 | // OpenCL single token adornments. |
| 3309 | case tok::kw___kernel: |
Anastasia Stulova | 6bdbcbb | 2016-02-19 18:30:11 +0000 | [diff] [blame] | 3310 | ParseOpenCLKernelAttributes(DS.getAttributes()); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3311 | continue; |
| 3312 | |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 3313 | // Nullability type specifiers. |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 3314 | case tok::kw__Nonnull: |
| 3315 | case tok::kw__Nullable: |
| 3316 | case tok::kw__Null_unspecified: |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 3317 | ParseNullabilityTypeSpecifiers(DS.getAttributes()); |
| 3318 | continue; |
| 3319 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 3320 | // Objective-C 'kindof' types. |
| 3321 | case tok::kw___kindof: |
| 3322 | DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc, |
| 3323 | nullptr, 0, AttributeList::AS_Keyword); |
| 3324 | (void)ConsumeToken(); |
| 3325 | continue; |
| 3326 | |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3327 | // storage-class-specifier |
| 3328 | case tok::kw_typedef: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3329 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3330 | PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3331 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3332 | break; |
| 3333 | case tok::kw_extern: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3334 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3335 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3336 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3337 | PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3338 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3339 | break; |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 3340 | case tok::kw___private_extern__: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3341 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3342 | Loc, PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3343 | isStorageClass = true; |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 3344 | break; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3345 | case tok::kw_static: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3346 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3347 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3348 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3349 | PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3350 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3351 | break; |
| 3352 | case tok::kw_auto: |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3353 | if (getLangOpts().CPlusPlus11) { |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3354 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3355 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3356 | PrevSpec, DiagID, Policy); |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3357 | if (!isInvalid) |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 3358 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3359 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 3360 | } else |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3361 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3362 | DiagID, Policy); |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 3363 | } else |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3364 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3365 | PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3366 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3367 | break; |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 3368 | case tok::kw___auto_type: |
| 3369 | Diag(Tok, diag::ext_auto_type); |
| 3370 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto_type, Loc, PrevSpec, |
| 3371 | DiagID, Policy); |
| 3372 | break; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3373 | case tok::kw_register: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3374 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3375 | PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3376 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3377 | break; |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 3378 | case tok::kw_mutable: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3379 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3380 | PrevSpec, DiagID, Policy); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3381 | isStorageClass = true; |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 3382 | break; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3383 | case tok::kw___thread: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3384 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc, |
| 3385 | PrevSpec, DiagID); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3386 | isStorageClass = true; |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3387 | break; |
| 3388 | case tok::kw_thread_local: |
| 3389 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc, |
| 3390 | PrevSpec, DiagID); |
| 3391 | break; |
| 3392 | case tok::kw__Thread_local: |
| 3393 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local, |
| 3394 | Loc, PrevSpec, DiagID); |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3395 | isStorageClass = true; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3396 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3397 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3398 | // function-specifier |
| 3399 | case tok::kw_inline: |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 3400 | isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3401 | break; |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3402 | case tok::kw_virtual: |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 3403 | isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3404 | break; |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3405 | case tok::kw_explicit: |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 3406 | isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3407 | break; |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 3408 | case tok::kw__Noreturn: |
| 3409 | if (!getLangOpts().C11) |
| 3410 | Diag(Loc, diag::ext_c11_noreturn); |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 3411 | isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID); |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 3412 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3413 | |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 3414 | // alignment-specifier |
| 3415 | case tok::kw__Alignas: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3416 | if (!getLangOpts().C11) |
Jordan Rose | 58d5472 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 3417 | Diag(Tok, diag::ext_c11_alignment) << Tok.getName(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 3418 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 3419 | continue; |
| 3420 | |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 3421 | // friend |
| 3422 | case tok::kw_friend: |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3423 | if (DSContext == DSC_class) |
| 3424 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 3425 | else { |
| 3426 | PrevSpec = ""; // not actually used by the diagnostic |
| 3427 | DiagID = diag::err_friend_invalid_in_context; |
| 3428 | isInvalid = true; |
| 3429 | } |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 3430 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3431 | |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 3432 | // Modules |
| 3433 | case tok::kw___module_private__: |
| 3434 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 3435 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3436 | |
Sebastian Redl | 39c2a8b | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 3437 | // constexpr |
| 3438 | case tok::kw_constexpr: |
| 3439 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 3440 | break; |
| 3441 | |
Hubert Tong | 375f00a | 2015-06-30 12:14:52 +0000 | [diff] [blame] | 3442 | // concept |
| 3443 | case tok::kw_concept: |
| 3444 | isInvalid = DS.SetConceptSpec(Loc, PrevSpec, DiagID); |
| 3445 | break; |
| 3446 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3447 | // type-specifier |
| 3448 | case tok::kw_short: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3449 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3450 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3451 | break; |
| 3452 | case tok::kw_long: |
| 3453 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3454 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3455 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3456 | else |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3457 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3458 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3459 | break; |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3460 | case tok::kw___int64: |
| 3461 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3462 | DiagID, Policy); |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3463 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3464 | case tok::kw_signed: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3465 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 3466 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3467 | break; |
| 3468 | case tok::kw_unsigned: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3469 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 3470 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3471 | break; |
| 3472 | case tok::kw__Complex: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3473 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 3474 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3475 | break; |
| 3476 | case tok::kw__Imaginary: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3477 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 3478 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3479 | break; |
| 3480 | case tok::kw_void: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3481 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3482 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3483 | break; |
| 3484 | case tok::kw_char: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3485 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3486 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3487 | break; |
| 3488 | case tok::kw_int: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3489 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3490 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3491 | break; |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3492 | case tok::kw___int128: |
| 3493 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3494 | DiagID, Policy); |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3495 | break; |
| 3496 | case tok::kw_half: |
| 3497 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3498 | DiagID, Policy); |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3499 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3500 | case tok::kw_float: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3501 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3502 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3503 | break; |
| 3504 | case tok::kw_double: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3505 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3506 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3507 | break; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 3508 | case tok::kw___float128: |
| 3509 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float128, Loc, PrevSpec, |
| 3510 | DiagID, Policy); |
| 3511 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3512 | case tok::kw_wchar_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3513 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3514 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3515 | break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3516 | case tok::kw_char16_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3517 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3518 | DiagID, Policy); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3519 | break; |
| 3520 | case tok::kw_char32_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3521 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3522 | DiagID, Policy); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3523 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3524 | case tok::kw_bool: |
| 3525 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 20ee5ae | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3526 | if (Tok.is(tok::kw_bool) && |
| 3527 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 3528 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 3529 | PrevSpec = ""; // Not used by the diagnostic. |
| 3530 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | 2b05999 | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3531 | // For better error recovery. |
| 3532 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 20ee5ae | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3533 | isInvalid = true; |
| 3534 | } else { |
| 3535 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3536 | DiagID, Policy); |
Argyrios Kyrtzidis | 20ee5ae | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3537 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3538 | break; |
| 3539 | case tok::kw__Decimal32: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3540 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3541 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3542 | break; |
| 3543 | case tok::kw__Decimal64: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3544 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3545 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3546 | break; |
| 3547 | case tok::kw__Decimal128: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3548 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3549 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3550 | break; |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3551 | case tok::kw___vector: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3552 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy); |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3553 | break; |
| 3554 | case tok::kw___pixel: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3555 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy); |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3556 | break; |
Bill Seurer | cf2c96b | 2015-01-12 19:35:51 +0000 | [diff] [blame] | 3557 | case tok::kw___bool: |
| 3558 | isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy); |
| 3559 | break; |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3560 | case tok::kw_pipe: |
| 3561 | if (!getLangOpts().OpenCL || (getLangOpts().OpenCLVersion < 200)) { |
| 3562 | // OpenCL 2.0 defined this keyword. OpenCL 1.2 and earlier should |
| 3563 | // support the "pipe" word as identifier. |
| 3564 | Tok.getIdentifierInfo()->revertTokenIDToIdentifier(); |
| 3565 | goto DoneWithDeclSpec; |
| 3566 | } |
| 3567 | isInvalid = DS.SetTypePipe(true, Loc, PrevSpec, DiagID, Policy); |
| 3568 | break; |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 3569 | #define GENERIC_IMAGE_TYPE(ImgType, Id) \ |
| 3570 | case tok::kw_##ImgType##_t: \ |
| 3571 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_##ImgType##_t, Loc, PrevSpec, \ |
| 3572 | DiagID, Policy); \ |
| 3573 | break; |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 3574 | #include "clang/Basic/OpenCLImageTypes.def" |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3575 | case tok::kw___unknown_anytype: |
| 3576 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3577 | PrevSpec, DiagID, Policy); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3578 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3579 | |
| 3580 | // class-specifier: |
| 3581 | case tok::kw_class: |
| 3582 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3583 | case tok::kw___interface: |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3584 | case tok::kw_union: { |
| 3585 | tok::TokenKind Kind = Tok.getKind(); |
| 3586 | ConsumeToken(); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3587 | |
| 3588 | // These are attributes following class specifiers. |
| 3589 | // To produce better diagnostic, we parse them when |
| 3590 | // parsing class specifier. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3591 | ParsedAttributesWithRange Attributes(AttrFactory); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3592 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3593 | EnteringContext, DSContext, Attributes); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3594 | |
| 3595 | // If there are attributes following class specifier, |
| 3596 | // take them over and handle them here. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3597 | if (!Attributes.empty()) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3598 | AttrsLastTime = true; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3599 | attrs.takeAllFrom(Attributes); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3600 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3601 | continue; |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3602 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3603 | |
| 3604 | // enum-specifier: |
| 3605 | case tok::kw_enum: |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3606 | ConsumeToken(); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3607 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3608 | continue; |
| 3609 | |
| 3610 | // cv-qualifier: |
| 3611 | case tok::kw_const: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3612 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3613 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3614 | break; |
| 3615 | case tok::kw_volatile: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3616 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3617 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3618 | break; |
| 3619 | case tok::kw_restrict: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3620 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3621 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3622 | break; |
| 3623 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3624 | // C++ typename-specifier: |
| 3625 | case tok::kw_typename: |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3626 | if (TryAnnotateTypeOrScopeToken()) { |
| 3627 | DS.SetTypeSpecError(); |
| 3628 | goto DoneWithDeclSpec; |
| 3629 | } |
| 3630 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3631 | continue; |
| 3632 | break; |
| 3633 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3634 | // GNU typeof support. |
| 3635 | case tok::kw_typeof: |
| 3636 | ParseTypeofSpecifier(DS); |
| 3637 | continue; |
| 3638 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3639 | case tok::annot_decltype: |
Anders Carlsson | 74948d0 | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 3640 | ParseDecltypeSpecifier(DS); |
| 3641 | continue; |
| 3642 | |
David Majnemer | 15b311c | 2016-06-14 03:20:28 +0000 | [diff] [blame] | 3643 | case tok::annot_pragma_pack: |
| 3644 | HandlePragmaPack(); |
| 3645 | continue; |
| 3646 | |
| 3647 | case tok::annot_pragma_ms_pragma: |
| 3648 | HandlePragmaMSPragma(); |
| 3649 | continue; |
| 3650 | |
| 3651 | case tok::annot_pragma_ms_vtordisp: |
| 3652 | HandlePragmaMSVtorDisp(); |
| 3653 | continue; |
| 3654 | |
| 3655 | case tok::annot_pragma_ms_pointers_to_members: |
| 3656 | HandlePragmaMSPointersToMembers(); |
| 3657 | continue; |
| 3658 | |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3659 | case tok::kw___underlying_type: |
| 3660 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3661 | continue; |
| 3662 | |
| 3663 | case tok::kw__Atomic: |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3664 | // C11 6.7.2.4/4: |
| 3665 | // If the _Atomic keyword is immediately followed by a left parenthesis, |
| 3666 | // it is interpreted as a type specifier (with a type name), not as a |
| 3667 | // type qualifier. |
| 3668 | if (NextToken().is(tok::l_paren)) { |
| 3669 | ParseAtomicSpecifier(DS); |
| 3670 | continue; |
| 3671 | } |
| 3672 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 3673 | getLangOpts()); |
| 3674 | break; |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3675 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3676 | // OpenCL qualifiers: |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 3677 | case tok::kw___generic: |
| 3678 | // generic address space is introduced only in OpenCL v2.0 |
| 3679 | // see OpenCL C Spec v2.0 s6.5.5 |
| 3680 | if (Actions.getLangOpts().OpenCLVersion < 200) { |
| 3681 | DiagID = diag::err_opencl_unknown_type_specifier; |
| 3682 | PrevSpec = Tok.getIdentifierInfo()->getNameStart(); |
| 3683 | isInvalid = true; |
| 3684 | break; |
| 3685 | }; |
Galina Kistanova | 7767425 | 2017-06-01 21:15:34 +0000 | [diff] [blame] | 3686 | LLVM_FALLTHROUGH; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3687 | case tok::kw___private: |
| 3688 | case tok::kw___global: |
| 3689 | case tok::kw___local: |
| 3690 | case tok::kw___constant: |
| 3691 | case tok::kw___read_only: |
| 3692 | case tok::kw___write_only: |
| 3693 | case tok::kw___read_write: |
Aaron Ballman | 05d76ea | 2014-01-14 01:29:54 +0000 | [diff] [blame] | 3694 | ParseOpenCLQualifiers(DS.getAttributes()); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3695 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3696 | |
Steve Naroff | cfdf616 | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 3697 | case tok::less: |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3698 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3699 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 3700 | // but we support it. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3701 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3702 | goto DoneWithDeclSpec; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3703 | |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 3704 | SourceLocation StartLoc = Tok.getLocation(); |
| 3705 | SourceLocation EndLoc; |
| 3706 | TypeResult Type = parseObjCProtocolQualifierType(EndLoc); |
| 3707 | if (Type.isUsable()) { |
| 3708 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, StartLoc, |
| 3709 | PrevSpec, DiagID, Type.get(), |
| 3710 | Actions.getASTContext().getPrintingPolicy())) |
| 3711 | Diag(StartLoc, DiagID) << PrevSpec; |
| 3712 | |
| 3713 | DS.SetRangeEnd(EndLoc); |
| 3714 | } else { |
| 3715 | DS.SetTypeSpecError(); |
| 3716 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3717 | |
Douglas Gregor | 06e41ae | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 3718 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 3719 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 3720 | continue; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 3721 | } |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3722 | // If the specifier wasn't legal, issue a diagnostic. |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3723 | if (isInvalid) { |
| 3724 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3725 | assert(DiagID); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3726 | |
Douglas Gregor | a05f5ab | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 3727 | if (DiagID == diag::ext_duplicate_declspec) |
| 3728 | Diag(Tok, DiagID) |
| 3729 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
Anastasia Stulova | 43ab9a0 | 2016-05-12 16:28:25 +0000 | [diff] [blame] | 3730 | else if (DiagID == diag::err_opencl_unknown_type_specifier) { |
| 3731 | const int OpenCLVer = getLangOpts().OpenCLVersion; |
| 3732 | std::string VerSpec = llvm::to_string(OpenCLVer / 100) + |
| 3733 | std::string (".") + |
| 3734 | llvm::to_string((OpenCLVer % 100) / 10); |
| 3735 | Diag(Tok, DiagID) << VerSpec << PrevSpec << isStorageClass; |
| 3736 | } else |
Douglas Gregor | a05f5ab | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 3737 | Diag(Tok, DiagID) << PrevSpec; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3738 | } |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3739 | |
Chris Lattner | 2e23209 | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 3740 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | 2b05999 | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3741 | if (DiagID != diag::err_bool_redeclaration) |
| 3742 | ConsumeToken(); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3743 | |
| 3744 | AttrsLastTime = false; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 3745 | } |
| 3746 | } |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 3747 | |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3748 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 3749 | /// semicolon. |
| 3750 | /// |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3751 | /// struct-declaration: |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3752 | /// specifier-qualifier-list struct-declarator-list |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 3753 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3754 | /// [GNU] specifier-qualifier-list |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3755 | /// struct-declarator-list: |
| 3756 | /// struct-declarator |
| 3757 | /// struct-declarator-list ',' struct-declarator |
| 3758 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 3759 | /// struct-declarator: |
| 3760 | /// declarator |
| 3761 | /// [GNU] declarator attributes[opt] |
| 3762 | /// declarator[opt] ':' constant-expression |
| 3763 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 3764 | /// |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 3765 | void Parser::ParseStructDeclaration( |
| 3766 | ParsingDeclSpec &DS, |
| 3767 | llvm::function_ref<void(ParsingFieldDeclarator &)> FieldsCallback) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3768 | |
Chris Lattner | f02ef3e | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3769 | if (Tok.is(tok::kw___extension__)) { |
| 3770 | // __extension__ silences extension warnings in the subexpression. |
| 3771 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3772 | ConsumeToken(); |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 3773 | return ParseStructDeclaration(DS, FieldsCallback); |
Chris Lattner | f02ef3e | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3774 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3775 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3776 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3777 | ParseSpecifierQualifierList(DS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3778 | |
Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3779 | // If there are no declarators, this is a free-standing declaration |
| 3780 | // specifier. Let the actions module cope with it. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3781 | if (Tok.is(tok::semi)) { |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 3782 | RecordDecl *AnonRecord = nullptr; |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3783 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Nico Weber | 7b837f5 | 2016-01-28 19:25:00 +0000 | [diff] [blame] | 3784 | DS, AnonRecord); |
| 3785 | assert(!AnonRecord && "Did not expect anonymous struct or union here"); |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3786 | DS.complete(TheDecl); |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3787 | return; |
| 3788 | } |
| 3789 | |
| 3790 | // Read struct-declarators until we find the semicolon. |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3791 | bool FirstDeclarator = true; |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3792 | SourceLocation CommaLoc; |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3793 | while (1) { |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3794 | ParsingFieldDeclarator DeclaratorInfo(*this, DS); |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3795 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3796 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3797 | // Attributes are only allowed here on successive declarators. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3798 | if (!FirstDeclarator) |
| 3799 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3800 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3801 | /// struct-declarator: declarator |
| 3802 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | 17c3b1f | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3803 | if (Tok.isNot(tok::colon)) { |
| 3804 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 3805 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3806 | ParseDeclarator(DeclaratorInfo.D); |
Richard Smith | 3d1a94c | 2014-08-12 00:22:39 +0000 | [diff] [blame] | 3807 | } else |
| 3808 | DeclaratorInfo.D.SetIdentifier(nullptr, Tok.getLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3809 | |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 3810 | if (TryConsumeToken(tok::colon)) { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3811 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3812 | if (Res.isInvalid()) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3813 | SkipUntil(tok::semi, StopBeforeMatch); |
Chris Lattner | 32295d3 | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 3814 | else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3815 | DeclaratorInfo.BitfieldSize = Res.get(); |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3816 | } |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3817 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3818 | // If attributes exist after the declarator, parse them. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3819 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3820 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3821 | // We're done with this declarator; invoke the callback. |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 3822 | FieldsCallback(DeclaratorInfo); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3823 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3824 | // If we don't have a comma, it is either the end of the list (a ';') |
| 3825 | // or an error, bail out. |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 3826 | if (!TryConsumeToken(tok::comma, CommaLoc)) |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3827 | return; |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3828 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3829 | FirstDeclarator = false; |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3830 | } |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3831 | } |
| 3832 | |
| 3833 | /// ParseStructUnionBody |
| 3834 | /// struct-contents: |
| 3835 | /// struct-declaration-list |
| 3836 | /// [EXT] empty |
| 3837 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 3838 | /// struct-declaration-list: |
| 3839 | /// struct-declaration |
| 3840 | /// struct-declaration-list struct-declaration |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3841 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3842 | /// |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 3843 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3844 | unsigned TagType, Decl *TagDecl) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3845 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 3846 | "parsing struct/union body"); |
Andy Gibbs | 22e140b | 2013-04-03 09:31:19 +0000 | [diff] [blame] | 3847 | assert(!getLangOpts().CPlusPlus && "C++ declarations not supported"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3848 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3849 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3850 | if (T.consumeOpen()) |
| 3851 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3852 | |
Douglas Gregor | 658b955 | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 3853 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3854 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3855 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3856 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3857 | |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 3858 | // 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] | 3859 | while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) && |
| 3860 | Tok.isNot(tok::eof)) { |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3861 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3862 | |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 3863 | // Check for extraneous top-level semicolon. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3864 | if (Tok.is(tok::semi)) { |
Richard Smith | 87f5dc5 | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3865 | ConsumeExtraSemi(InsideStruct, TagType); |
Chris Lattner | 36e46a2 | 2007-06-09 05:49:55 +0000 | [diff] [blame] | 3866 | continue; |
| 3867 | } |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3868 | |
Andy Gibbs | c804e08 | 2013-04-03 09:46:04 +0000 | [diff] [blame] | 3869 | // Parse _Static_assert declaration. |
| 3870 | if (Tok.is(tok::kw__Static_assert)) { |
| 3871 | SourceLocation DeclEnd; |
| 3872 | ParseStaticAssertDeclaration(DeclEnd); |
| 3873 | continue; |
| 3874 | } |
| 3875 | |
Argyrios Kyrtzidis | 71c12fb | 2013-04-18 01:42:35 +0000 | [diff] [blame] | 3876 | if (Tok.is(tok::annot_pragma_pack)) { |
| 3877 | HandlePragmaPack(); |
| 3878 | continue; |
| 3879 | } |
| 3880 | |
| 3881 | if (Tok.is(tok::annot_pragma_align)) { |
| 3882 | HandlePragmaAlign(); |
| 3883 | continue; |
| 3884 | } |
| 3885 | |
Alexey Bataev | 4652e4b | 2015-08-12 07:10:54 +0000 | [diff] [blame] | 3886 | if (Tok.is(tok::annot_pragma_openmp)) { |
Alexey Bataev | 587e1de | 2016-03-30 10:43:55 +0000 | [diff] [blame] | 3887 | // Result can be ignored, because it must be always empty. |
| 3888 | AccessSpecifier AS = AS_none; |
| 3889 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 3890 | (void)ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs); |
Alexey Bataev | 4652e4b | 2015-08-12 07:10:54 +0000 | [diff] [blame] | 3891 | continue; |
| 3892 | } |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 3893 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3894 | if (!Tok.is(tok::at)) { |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 3895 | auto CFieldCallback = [&](ParsingFieldDeclarator &FD) { |
| 3896 | // Install the declarator into the current TagDecl. |
| 3897 | Decl *Field = |
| 3898 | Actions.ActOnField(getCurScope(), TagDecl, |
| 3899 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 3900 | FD.D, FD.BitfieldSize); |
| 3901 | FieldDecls.push_back(Field); |
| 3902 | FD.complete(Field); |
| 3903 | }; |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3904 | |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3905 | // Parse all the comma separated declarators. |
| 3906 | ParsingDeclSpec DS(*this); |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 3907 | ParseStructDeclaration(DS, CFieldCallback); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3908 | } else { // Handle @defs |
| 3909 | ConsumeToken(); |
| 3910 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 3911 | Diag(Tok, diag::err_unexpected_at); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3912 | SkipUntil(tok::semi); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3913 | continue; |
| 3914 | } |
| 3915 | ConsumeToken(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 3916 | ExpectAndConsume(tok::l_paren); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3917 | if (!Tok.is(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 3918 | Diag(Tok, diag::err_expected) << tok::identifier; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3919 | SkipUntil(tok::semi); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3920 | continue; |
| 3921 | } |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3922 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3923 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3924 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3925 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 3926 | ConsumeToken(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 3927 | ExpectAndConsume(tok::r_paren); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3928 | } |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 3929 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 3930 | if (TryConsumeToken(tok::semi)) |
| 3931 | continue; |
| 3932 | |
| 3933 | if (Tok.is(tok::r_brace)) { |
Chris Lattner | 245c533 | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3934 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Chris Lattner | 0c7e82d | 2007-06-09 05:54:40 +0000 | [diff] [blame] | 3935 | break; |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3936 | } |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 3937 | |
| 3938 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 3939 | // Skip to end of block or statement to avoid ext-warning on extra ';'. |
| 3940 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
| 3941 | // If we stopped at a ';', eat it. |
| 3942 | TryConsumeToken(tok::semi); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3943 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3944 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3945 | T.consumeClose(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3946 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3947 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3948 | // If attributes exist after struct contents, parse them. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3949 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 15619c7 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 3950 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3951 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 751c558 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 3952 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3953 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3954 | attrs.getList()); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3955 | StructScope.Exit(); |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 3956 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, T.getRange()); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3957 | } |
| 3958 | |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3959 | /// ParseEnumSpecifier |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 3960 | /// enum-specifier: [C99 6.7.2.2] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3961 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3962 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 3963 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3964 | /// '}' attributes[opt] |
Aaron Ballman | 9ecff02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 3965 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3966 | /// '}' |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3967 | /// 'enum' identifier |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 3968 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3969 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3970 | /// [C++11] enum-head '{' enumerator-list[opt] '}' |
| 3971 | /// [C++11] enum-head '{' enumerator-list ',' '}' |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3972 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3973 | /// enum-head: [C++11] |
| 3974 | /// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt] |
| 3975 | /// enum-key attribute-specifier-seq[opt] nested-name-specifier |
| 3976 | /// identifier enum-base[opt] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3977 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3978 | /// enum-key: [C++11] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3979 | /// 'enum' |
| 3980 | /// 'enum' 'class' |
| 3981 | /// 'enum' 'struct' |
| 3982 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3983 | /// enum-base: [C++11] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3984 | /// ':' type-specifier-seq |
| 3985 | /// |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3986 | /// [C++] elaborated-type-specifier: |
| 3987 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 3988 | /// |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3989 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | dc70c3a | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 3990 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3991 | AccessSpecifier AS, DeclSpecContext DSC) { |
Chris Lattner | ffbc271 | 2007-01-25 06:05:38 +0000 | [diff] [blame] | 3992 | // Parse the tag portion of this. |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3993 | if (Tok.is(tok::code_completion)) { |
| 3994 | // Code completion for an enum name. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3995 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3996 | return cutOffParsing(); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3997 | } |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3998 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3999 | // If attributes exist after tag, parse them. |
| 4000 | ParsedAttributesWithRange attrs(AttrFactory); |
| 4001 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4002 | MaybeParseCXX11Attributes(attrs); |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 4003 | MaybeParseMicrosoftDeclSpecs(attrs); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 4004 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4005 | SourceLocation ScopedEnumKWLoc; |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 4006 | bool IsScopedUsingClassTag = false; |
| 4007 | |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 4008 | // In C++11, recognize 'enum class' and 'enum struct'. |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 4009 | if (Tok.isOneOf(tok::kw_class, tok::kw_struct)) { |
Richard Trieu | d0d87b5 | 2013-04-23 02:47:36 +0000 | [diff] [blame] | 4010 | Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum |
| 4011 | : diag::ext_scoped_enum); |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 4012 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4013 | ScopedEnumKWLoc = ConsumeToken(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4014 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 4015 | // Attributes are not allowed between these keywords. Diagnose, |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 4016 | // but then just treat them like they appeared in the right place. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 4017 | ProhibitAttributes(attrs); |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 4018 | |
| 4019 | // They are allowed afterwards, though. |
| 4020 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4021 | MaybeParseCXX11Attributes(attrs); |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 4022 | MaybeParseMicrosoftDeclSpecs(attrs); |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 4023 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4024 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4025 | // C++11 [temp.explicit]p12: |
| 4026 | // The usual access controls do not apply to names used to specify |
| 4027 | // explicit instantiations. |
| 4028 | // We extend this to also cover explicit specializations. Note that |
| 4029 | // we don't suppress if this turns out to be an elaborated type |
| 4030 | // specifier. |
| 4031 | bool shouldDelayDiagsInTag = |
| 4032 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 4033 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 4034 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4035 | |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 4036 | // Enum definitions should not be parsed in a trailing-return-type. |
| 4037 | bool AllowDeclaration = DSC != DSC_trailing; |
| 4038 | |
| 4039 | bool AllowFixedUnderlyingType = AllowDeclaration && |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4040 | (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt || |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 4041 | getLangOpts().ObjC2); |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 4042 | |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 4043 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4044 | if (getLangOpts().CPlusPlus) { |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 4045 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 4046 | // if a fixed underlying type is allowed. |
| 4047 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4048 | |
Nico Weber | cfaa4cd | 2015-02-15 07:26:13 +0000 | [diff] [blame] | 4049 | CXXScopeSpec Spec; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 4050 | if (ParseOptionalCXXScopeSpecifier(Spec, nullptr, |
Richard Smith | 1d4b2e1 | 2013-04-01 21:43:41 +0000 | [diff] [blame] | 4051 | /*EnteringContext=*/true)) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4052 | return; |
| 4053 | |
Nico Weber | cfaa4cd | 2015-02-15 07:26:13 +0000 | [diff] [blame] | 4054 | if (Spec.isSet() && Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 4055 | Diag(Tok, diag::err_expected) << tok::identifier; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4056 | if (Tok.isNot(tok::l_brace)) { |
| 4057 | // Has no name and is not a definition. |
| 4058 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4059 | SkipUntil(tok::comma, StopAtSemi); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4060 | return; |
| 4061 | } |
| 4062 | } |
Nico Weber | cfaa4cd | 2015-02-15 07:26:13 +0000 | [diff] [blame] | 4063 | |
| 4064 | SS = Spec; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4065 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4066 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4067 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 4068 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 4069 | !(AllowFixedUnderlyingType && Tok.is(tok::colon))) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 4070 | Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4071 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4072 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4073 | SkipUntil(tok::comma, StopAtSemi); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 4074 | return; |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4075 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4076 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4077 | // If an identifier is present, consume and remember it. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 4078 | IdentifierInfo *Name = nullptr; |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4079 | SourceLocation NameLoc; |
| 4080 | if (Tok.is(tok::identifier)) { |
| 4081 | Name = Tok.getIdentifierInfo(); |
| 4082 | NameLoc = ConsumeToken(); |
| 4083 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4084 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4085 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4086 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 4087 | // declaration of a scoped enumeration. |
| 4088 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4089 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 4090 | IsScopedUsingClassTag = false; |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4091 | } |
| 4092 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4093 | // Okay, end the suppression area. We'll decide whether to emit the |
| 4094 | // diagnostics in a second. |
| 4095 | if (shouldDelayDiagsInTag) |
| 4096 | diagsFromTag.done(); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4097 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4098 | TypeResult BaseType; |
| 4099 | |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4100 | // Parse the fixed underlying type. |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 4101 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 4102 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4103 | bool PossibleBitfield = false; |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 4104 | if (CanBeBitfield) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4105 | // If we're in class scope, this can either be an enum declaration with |
| 4106 | // an underlying type, or a declaration of a bitfield member. We try to |
| 4107 | // use a simple disambiguation scheme first to catch the common cases |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4108 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 4109 | // anything that's a simple-type-specifier followed by '(' as an |
| 4110 | // expression. This suffices because function types are not valid |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4111 | // underlying types anyway. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4112 | EnterExpressionEvaluationContext Unevaluated( |
| 4113 | Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4114 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4115 | // 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] | 4116 | // bit-field. This is the common case. |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 4117 | if (TPR == TPResult::True) |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4118 | PossibleBitfield = true; |
| 4119 | // If the next token starts a type-specifier-seq, it may be either a |
| 4120 | // 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] | 4121 | // 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] | 4122 | // fixed underlying type. |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 4123 | else if (TPR == TPResult::False && |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4124 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 4125 | // Consume the ':'. |
| 4126 | ConsumeToken(); |
| 4127 | } else { |
| 4128 | // We have the start of a type-specifier-seq, so we have to perform |
| 4129 | // tentative parsing to determine whether we have an expression or a |
| 4130 | // type. |
| 4131 | TentativeParsingAction TPA(*this); |
| 4132 | |
| 4133 | // Consume the ':'. |
| 4134 | ConsumeToken(); |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 4135 | |
| 4136 | // If we see a type specifier followed by an open-brace, we have an |
| 4137 | // ambiguity between an underlying type and a C++11 braced |
| 4138 | // function-style cast. Resolve this by always treating it as an |
| 4139 | // underlying type. |
| 4140 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 4141 | // this case. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4142 | if ((getLangOpts().CPlusPlus && |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 4143 | isCXXDeclarationSpecifier(TPResult::True) != TPResult::True) || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4144 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4145 | // We'll parse this as a bitfield later. |
| 4146 | PossibleBitfield = true; |
| 4147 | TPA.Revert(); |
| 4148 | } else { |
| 4149 | // We have a type-specifier-seq. |
| 4150 | TPA.Commit(); |
| 4151 | } |
| 4152 | } |
| 4153 | } else { |
| 4154 | // Consume the ':'. |
| 4155 | ConsumeToken(); |
| 4156 | } |
| 4157 | |
| 4158 | if (!PossibleBitfield) { |
| 4159 | SourceRange Range; |
| 4160 | BaseType = ParseTypeName(&Range); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4161 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4162 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4163 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Eli Friedman | 0d0355ab | 2012-11-02 01:34:28 +0000 | [diff] [blame] | 4164 | } else if (!getLangOpts().ObjC2) { |
| 4165 | if (getLangOpts().CPlusPlus) |
| 4166 | Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range; |
| 4167 | else |
| 4168 | Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range; |
| 4169 | } |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 4170 | } |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4171 | } |
| 4172 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4173 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 4174 | // friend declaration, and cannot have an accompanying definition. If we have |
| 4175 | // 'enum foo;', then this is a forward declaration. If we have |
| 4176 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 4177 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 4178 | // |
| 4179 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 4180 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 4181 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 4182 | // |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4183 | Sema::TagUseKind TUK; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4184 | if (!AllowDeclaration) { |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 4185 | TUK = Sema::TUK_Reference; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4186 | } else if (Tok.is(tok::l_brace)) { |
| 4187 | if (DS.isFriendSpecified()) { |
| 4188 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
| 4189 | << SourceRange(DS.getFriendSpecLoc()); |
| 4190 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4191 | SkipUntil(tok::r_brace, StopAtSemi); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4192 | TUK = Sema::TUK_Friend; |
| 4193 | } else { |
| 4194 | TUK = Sema::TUK_Definition; |
| 4195 | } |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 4196 | } else if (!isTypeSpecifier(DSC) && |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 4197 | (Tok.is(tok::semi) || |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 4198 | (Tok.isAtStartOfLine() && |
| 4199 | !isValidAfterTypeSpecifier(CanBeBitfield)))) { |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 4200 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
| 4201 | if (Tok.isNot(tok::semi)) { |
| 4202 | // A semicolon was missing after this declaration. Diagnose and recover. |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 4203 | ExpectAndConsume(tok::semi, diag::err_expected_after, "enum"); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 4204 | PP.EnterToken(Tok); |
| 4205 | Tok.setKind(tok::semi); |
| 4206 | } |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4207 | } else { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4208 | TUK = Sema::TUK_Reference; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
| 4211 | // If this is an elaborated type specifier, and we delayed |
| 4212 | // diagnostics before, just merge them into the current pool. |
| 4213 | if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) { |
| 4214 | diagsFromTag.redelay(); |
| 4215 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4216 | |
| 4217 | MultiTemplateParamsArg TParams; |
Douglas Gregor | cbbf3e3 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 4218 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4219 | TUK != Sema::TUK_Reference) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4220 | if (!getLangOpts().CPlusPlus11 || !SS.isSet()) { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4221 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 4222 | Diag(Tok, diag::err_enum_template); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4223 | SkipUntil(tok::comma, StopAtSemi); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4224 | return; |
| 4225 | } |
| 4226 | |
| 4227 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 4228 | // Enumerations can't be explicitly instantiated. |
| 4229 | DS.SetTypeSpecError(); |
| 4230 | Diag(StartLoc, diag::err_explicit_instantiation_enum); |
| 4231 | return; |
| 4232 | } |
| 4233 | |
| 4234 | assert(TemplateInfo.TemplateParams && "no template parameters"); |
| 4235 | TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(), |
| 4236 | TemplateInfo.TemplateParams->size()); |
Douglas Gregor | cbbf3e3 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 4237 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4238 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 4239 | if (TUK == Sema::TUK_Reference) |
| 4240 | ProhibitAttributes(attrs); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4241 | |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 4242 | if (!Name && TUK != Sema::TUK_Definition) { |
| 4243 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4244 | |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 4245 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4246 | SkipUntil(tok::comma, StopAtSemi); |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 4247 | return; |
| 4248 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4249 | |
Nico Weber | 32a0fc7 | 2016-09-03 03:01:32 +0000 | [diff] [blame] | 4250 | stripTypeAttributesOffDeclSpec(attrs, DS, TUK); |
David Majnemer | 936b411 | 2015-04-19 07:53:29 +0000 | [diff] [blame] | 4251 | |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 4252 | Sema::SkipBodyInfo SkipBody; |
| 4253 | if (!Name && TUK == Sema::TUK_Definition && Tok.is(tok::l_brace) && |
| 4254 | NextToken().is(tok::identifier)) |
| 4255 | SkipBody = Actions.shouldSkipAnonEnumBody(getCurScope(), |
| 4256 | NextToken().getIdentifierInfo(), |
| 4257 | NextToken().getLocation()); |
| 4258 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 4259 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4260 | bool IsDependent = false; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 4261 | const char *PrevSpec = nullptr; |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4262 | unsigned DiagID; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4263 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4264 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4265 | AS, DS.getModulePrivateSpecLoc(), TParams, |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4266 | Owned, IsDependent, ScopedEnumKWLoc, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 4267 | IsScopedUsingClassTag, BaseType, |
Akira Hatanaka | 12ddcee | 2017-06-26 18:46:12 +0000 | [diff] [blame] | 4268 | DSC == DSC_type_specifier, |
| 4269 | DSC == DSC_template_param || |
| 4270 | DSC == DSC_template_type_arg, &SkipBody); |
Richard Smith | d9ba224 | 2015-05-07 03:54:19 +0000 | [diff] [blame] | 4271 | |
| 4272 | if (SkipBody.ShouldSkip) { |
| 4273 | assert(TUK == Sema::TUK_Definition && "can only skip a definition"); |
| 4274 | |
| 4275 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 4276 | T.consumeOpen(); |
| 4277 | T.skipToEnd(); |
| 4278 | |
| 4279 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 4280 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 4281 | PrevSpec, DiagID, TagDecl, Owned, |
| 4282 | Actions.getASTContext().getPrintingPolicy())) |
| 4283 | Diag(StartLoc, DiagID) << PrevSpec; |
| 4284 | return; |
| 4285 | } |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4286 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4287 | if (IsDependent) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4288 | // This enum has a dependent nested-name-specifier. Handle it as a |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4289 | // dependent tag. |
| 4290 | if (!Name) { |
| 4291 | DS.SetTypeSpecError(); |
| 4292 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 4293 | return; |
| 4294 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4295 | |
Nico Weber | 83ea012 | 2014-05-03 21:57:40 +0000 | [diff] [blame] | 4296 | TypeResult Type = Actions.ActOnDependentTag( |
| 4297 | getCurScope(), DeclSpec::TST_enum, TUK, SS, Name, StartLoc, NameLoc); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4298 | if (Type.isInvalid()) { |
| 4299 | DS.SetTypeSpecError(); |
| 4300 | return; |
| 4301 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4302 | |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 4303 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 4304 | NameLoc.isValid() ? NameLoc : StartLoc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 4305 | PrevSpec, DiagID, Type.get(), |
| 4306 | Actions.getASTContext().getPrintingPolicy())) |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4307 | Diag(StartLoc, DiagID) << PrevSpec; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4308 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4309 | return; |
| 4310 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4311 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4312 | if (!TagDecl) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4313 | // The action failed to produce an enumeration tag. If this is a |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4314 | // definition, consume the entire definition. |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 4315 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4316 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 4317 | SkipUntil(tok::r_brace, StopAtSemi); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4318 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4319 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 4320 | DS.SetTypeSpecError(); |
| 4321 | return; |
| 4322 | } |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 4323 | |
Bruno Cardoso Lopes | df0ee34 | 2017-07-01 00:06:47 +0000 | [diff] [blame] | 4324 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
| 4325 | Decl *D = SkipBody.CheckSameAsPrevious ? SkipBody.New : TagDecl; |
| 4326 | ParseEnumBody(StartLoc, D); |
| 4327 | if (SkipBody.CheckSameAsPrevious && |
| 4328 | !Actions.ActOnDuplicateDefinition(DS, TagDecl, SkipBody)) { |
| 4329 | DS.SetTypeSpecError(); |
| 4330 | return; |
| 4331 | } |
| 4332 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4333 | |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 4334 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 4335 | NameLoc.isValid() ? NameLoc : StartLoc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 4336 | PrevSpec, DiagID, TagDecl, Owned, |
| 4337 | Actions.getASTContext().getPrintingPolicy())) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4338 | Diag(StartLoc, DiagID) << PrevSpec; |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 4339 | } |
| 4340 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4341 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 4342 | /// enumerator-list: |
| 4343 | /// enumerator |
| 4344 | /// enumerator-list ',' enumerator |
| 4345 | /// enumerator: |
Aaron Ballman | 730476b | 2014-11-08 15:33:35 +0000 | [diff] [blame] | 4346 | /// enumeration-constant attributes[opt] |
| 4347 | /// enumeration-constant attributes[opt] '=' constant-expression |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4348 | /// enumeration-constant: |
| 4349 | /// identifier |
| 4350 | /// |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4351 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4352 | // Enter the scope of the enum body and start the definition. |
Hans Wennborg | fe78145 | 2014-06-17 00:00:18 +0000 | [diff] [blame] | 4353 | ParseScope EnumScope(this, Scope::DeclScope | Scope::EnumScope); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4354 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4355 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4356 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 4357 | T.consumeOpen(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4358 | |
Chris Lattner | 37256fb | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 4359 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4360 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 4361 | Diag(Tok, diag::err_empty_enum); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4362 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4363 | SmallVector<Decl *, 32> EnumConstantDecls; |
Jordan Rose | 60ac316 | 2015-04-30 17:20:30 +0000 | [diff] [blame] | 4364 | SmallVector<SuppressAccessChecks, 32> EnumAvailabilityDiags; |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4365 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 4366 | Decl *LastEnumConstDecl = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4367 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4368 | // Parse the enumerator-list. |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4369 | while (Tok.isNot(tok::r_brace)) { |
| 4370 | // Parse enumerator. If failed, try skipping till the start of the next |
| 4371 | // enumerator definition. |
| 4372 | if (Tok.isNot(tok::identifier)) { |
| 4373 | Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; |
| 4374 | if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) && |
| 4375 | TryConsumeToken(tok::comma)) |
| 4376 | continue; |
| 4377 | break; |
| 4378 | } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4379 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 4380 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4381 | |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 4382 | // If attributes exist after the enumerator, parse them. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 4383 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4384 | MaybeParseGNUAttributes(attrs); |
Aaron Ballman | 730476b | 2014-11-08 15:33:35 +0000 | [diff] [blame] | 4385 | ProhibitAttributes(attrs); // GNU-style attributes are prohibited. |
| 4386 | if (getLangOpts().CPlusPlus11 && isCXX11AttributeSpecifier()) { |
| 4387 | if (!getLangOpts().CPlusPlus1z) |
| 4388 | Diag(Tok.getLocation(), diag::warn_cxx14_compat_attribute) |
Aaron Ballman | c0ae7df | 2014-11-08 17:07:15 +0000 | [diff] [blame] | 4389 | << 1 /*enumerator*/; |
Aaron Ballman | 730476b | 2014-11-08 15:33:35 +0000 | [diff] [blame] | 4390 | ParseCXX11Attributes(attrs); |
| 4391 | } |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 4392 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4393 | SourceLocation EqualLoc; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4394 | ExprResult AssignedVal; |
Jordan Rose | 60ac316 | 2015-04-30 17:20:30 +0000 | [diff] [blame] | 4395 | EnumAvailabilityDiags.emplace_back(*this); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4396 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 4397 | if (TryConsumeToken(tok::equal, EqualLoc)) { |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 4398 | AssignedVal = ParseConstantExpression(); |
| 4399 | if (AssignedVal.isInvalid()) |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4400 | SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch); |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4401 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4402 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4403 | // Install the enumerator constant into EnumDecl. |
Bruno Cardoso Lopes | df0ee34 | 2017-07-01 00:06:47 +0000 | [diff] [blame] | 4404 | Decl *EnumConstDecl = Actions.ActOnEnumConstant( |
| 4405 | getCurScope(), EnumDecl, LastEnumConstDecl, IdentLoc, Ident, |
| 4406 | attrs.getList(), EqualLoc, AssignedVal.get()); |
Jordan Rose | 60ac316 | 2015-04-30 17:20:30 +0000 | [diff] [blame] | 4407 | EnumAvailabilityDiags.back().done(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4408 | |
Chris Lattner | 4ef4001 | 2007-06-11 01:28:17 +0000 | [diff] [blame] | 4409 | EnumConstantDecls.push_back(EnumConstDecl); |
| 4410 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4411 | |
Douglas Gregor | ce66d02 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 4412 | if (Tok.is(tok::identifier)) { |
| 4413 | // We're missing a comma between enumerators. |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 4414 | SourceLocation Loc = getEndOfPreviousToken(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4415 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
Douglas Gregor | ce66d02 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 4416 | << FixItHint::CreateInsertion(Loc, ", "); |
| 4417 | continue; |
| 4418 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4419 | |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4420 | // Emumerator definition must be finished, only comma or r_brace are |
| 4421 | // allowed here. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 4422 | SourceLocation CommaLoc; |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4423 | if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) { |
| 4424 | if (EqualLoc.isValid()) |
| 4425 | Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace |
| 4426 | << tok::comma; |
| 4427 | else |
| 4428 | Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator); |
| 4429 | if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) { |
| 4430 | if (TryConsumeToken(tok::comma, CommaLoc)) |
| 4431 | continue; |
| 4432 | } else { |
| 4433 | break; |
| 4434 | } |
| 4435 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4436 | |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4437 | // If comma is followed by r_brace, emit appropriate warning. |
| 4438 | if (Tok.is(tok::r_brace) && CommaLoc.isValid()) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4439 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11) |
Richard Smith | 87f5dc5 | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 4440 | Diag(CommaLoc, getLangOpts().CPlusPlus ? |
| 4441 | diag::ext_enumerator_list_comma_cxx : |
| 4442 | diag::ext_enumerator_list_comma_c) |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4443 | << FixItHint::CreateRemoval(CommaLoc); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4444 | else if (getLangOpts().CPlusPlus11) |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4445 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 4446 | << FixItHint::CreateRemoval(CommaLoc); |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 4447 | break; |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4448 | } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4449 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4450 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4451 | // Eat the }. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4452 | T.consumeClose(); |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4453 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4454 | // If attributes exist after the identifier list, parse them. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4455 | ParsedAttributes attrs(AttrFactory); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4456 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 4457 | |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 4458 | Actions.ActOnEnumBody(StartLoc, T.getRange(), |
Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 4459 | EnumDecl, EnumConstantDecls, |
| 4460 | getCurScope(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4461 | attrs.getList()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4462 | |
Jordan Rose | 60ac316 | 2015-04-30 17:20:30 +0000 | [diff] [blame] | 4463 | // Now handle enum constant availability diagnostics. |
| 4464 | assert(EnumConstantDecls.size() == EnumAvailabilityDiags.size()); |
| 4465 | for (size_t i = 0, e = EnumConstantDecls.size(); i != e; ++i) { |
| 4466 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
| 4467 | EnumAvailabilityDiags[i].redelay(); |
| 4468 | PD.complete(EnumConstantDecls[i]); |
| 4469 | } |
| 4470 | |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 4471 | EnumScope.Exit(); |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 4472 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, T.getRange()); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 4473 | |
| 4474 | // The next token must be valid after an enum definition. If not, a ';' |
| 4475 | // was probably forgotten. |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 4476 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
| 4477 | if (!isValidAfterTypeSpecifier(CanBeBitfield)) { |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 4478 | ExpectAndConsume(tok::semi, diag::err_expected_after, "enum"); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 4479 | // Push this token back into the preprocessor and change our current token |
| 4480 | // to ';' so that the rest of the code recovers as though there were an |
| 4481 | // ';' after the definition. |
| 4482 | PP.EnterToken(Tok); |
| 4483 | Tok.setKind(tok::semi); |
| 4484 | } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4485 | } |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 4486 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4487 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 4488 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 4489 | /// specifier or if we're not sure. |
| 4490 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 4491 | switch (Tok.getKind()) { |
| 4492 | default: return false; |
| 4493 | // type-specifiers |
| 4494 | case tok::kw_short: |
| 4495 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4496 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4497 | case tok::kw___int128: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4498 | case tok::kw_signed: |
| 4499 | case tok::kw_unsigned: |
| 4500 | case tok::kw__Complex: |
| 4501 | case tok::kw__Imaginary: |
| 4502 | case tok::kw_void: |
| 4503 | case tok::kw_char: |
| 4504 | case tok::kw_wchar_t: |
| 4505 | case tok::kw_char16_t: |
| 4506 | case tok::kw_char32_t: |
| 4507 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4508 | case tok::kw_half: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4509 | case tok::kw_float: |
| 4510 | case tok::kw_double: |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 4511 | case tok::kw___float128: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4512 | case tok::kw_bool: |
| 4513 | case tok::kw__Bool: |
| 4514 | case tok::kw__Decimal32: |
| 4515 | case tok::kw__Decimal64: |
| 4516 | case tok::kw__Decimal128: |
| 4517 | case tok::kw___vector: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 4518 | #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 4519 | #include "clang/Basic/OpenCLImageTypes.def" |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4520 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4521 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4522 | case tok::kw_class: |
| 4523 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4524 | case tok::kw___interface: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4525 | case tok::kw_union: |
| 4526 | // enum-specifier |
| 4527 | case tok::kw_enum: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4528 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4529 | // typedef-name |
| 4530 | case tok::annot_typename: |
| 4531 | return true; |
| 4532 | } |
| 4533 | } |
| 4534 | |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 4535 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4536 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4537 | bool Parser::isTypeSpecifierQualifier() { |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4538 | switch (Tok.getKind()) { |
| 4539 | default: return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4540 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4541 | case tok::identifier: // foo::bar |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4542 | if (TryAltiVecVectorToken()) |
| 4543 | return true; |
| 4544 | // Fall through. |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4545 | case tok::kw_typename: // typename T::type |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4546 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4547 | // recurse to handle whatever we get. |
| 4548 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4549 | return true; |
| 4550 | if (Tok.is(tok::identifier)) |
| 4551 | return false; |
| 4552 | return isTypeSpecifierQualifier(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4553 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4554 | case tok::coloncolon: // ::foo::bar |
| 4555 | if (NextToken().is(tok::kw_new) || // ::new |
| 4556 | NextToken().is(tok::kw_delete)) // ::delete |
| 4557 | return false; |
| 4558 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4559 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4560 | return true; |
| 4561 | return isTypeSpecifierQualifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4562 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 4563 | // GNU attributes support. |
| 4564 | case tok::kw___attribute: |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4565 | // GNU typeof support. |
| 4566 | case tok::kw_typeof: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4567 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4568 | // type-specifiers |
| 4569 | case tok::kw_short: |
| 4570 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4571 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4572 | case tok::kw___int128: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4573 | case tok::kw_signed: |
| 4574 | case tok::kw_unsigned: |
| 4575 | case tok::kw__Complex: |
| 4576 | case tok::kw__Imaginary: |
| 4577 | case tok::kw_void: |
| 4578 | case tok::kw_char: |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4579 | case tok::kw_wchar_t: |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4580 | case tok::kw_char16_t: |
| 4581 | case tok::kw_char32_t: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4582 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4583 | case tok::kw_half: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4584 | case tok::kw_float: |
| 4585 | case tok::kw_double: |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 4586 | case tok::kw___float128: |
Chris Lattner | bb31a42 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 4587 | case tok::kw_bool: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4588 | case tok::kw__Bool: |
| 4589 | case tok::kw__Decimal32: |
| 4590 | case tok::kw__Decimal64: |
| 4591 | case tok::kw__Decimal128: |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4592 | case tok::kw___vector: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 4593 | #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 4594 | #include "clang/Basic/OpenCLImageTypes.def" |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4595 | |
Chris Lattner | 861a226 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 4596 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4597 | case tok::kw_class: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4598 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4599 | case tok::kw___interface: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4600 | case tok::kw_union: |
| 4601 | // enum-specifier |
| 4602 | case tok::kw_enum: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4603 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4604 | // type-qualifier |
| 4605 | case tok::kw_const: |
| 4606 | case tok::kw_volatile: |
| 4607 | case tok::kw_restrict: |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4608 | |
John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4609 | // Debugger support. |
| 4610 | case tok::kw___unknown_anytype: |
| 4611 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4612 | // typedef-name |
Chris Lattner | a8a3f73 | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 4613 | case tok::annot_typename: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4614 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4615 | |
Chris Lattner | 409bf7d | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 4616 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4617 | case tok::less: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4618 | return getLangOpts().ObjC1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4619 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4620 | case tok::kw___cdecl: |
| 4621 | case tok::kw___stdcall: |
| 4622 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4623 | case tok::kw___thiscall: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4624 | case tok::kw___regcall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4625 | case tok::kw___vectorcall: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4626 | case tok::kw___w64: |
| 4627 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4628 | case tok::kw___ptr32: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4629 | case tok::kw___pascal: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4630 | case tok::kw___unaligned: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4631 | |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 4632 | case tok::kw__Nonnull: |
| 4633 | case tok::kw__Nullable: |
| 4634 | case tok::kw__Null_unspecified: |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 4635 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4636 | case tok::kw___kindof: |
| 4637 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4638 | case tok::kw___private: |
| 4639 | case tok::kw___local: |
| 4640 | case tok::kw___global: |
| 4641 | case tok::kw___constant: |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 4642 | case tok::kw___generic: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4643 | case tok::kw___read_only: |
| 4644 | case tok::kw___read_write: |
| 4645 | case tok::kw___write_only: |
| 4646 | |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4647 | return true; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4648 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4649 | // C11 _Atomic |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4650 | case tok::kw__Atomic: |
| 4651 | return true; |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4652 | } |
| 4653 | } |
| 4654 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4655 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 4656 | /// declaration specifier. |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4657 | /// |
| 4658 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 4659 | /// this check is to disambiguate between an expression and a declaration. |
| 4660 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4661 | switch (Tok.getKind()) { |
| 4662 | default: return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4663 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 4664 | case tok::kw_pipe: |
| 4665 | return getLangOpts().OpenCL && (getLangOpts().OpenCLVersion >= 200); |
| 4666 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4667 | case tok::identifier: // foo::bar |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4668 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4669 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4670 | return false; |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4671 | if (TryAltiVecVectorToken()) |
| 4672 | return true; |
| 4673 | // Fall through. |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4674 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4675 | case tok::kw_typename: // typename T::type |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4676 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4677 | // recurse to handle whatever we get. |
| 4678 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4679 | return true; |
| 4680 | if (Tok.is(tok::identifier)) |
| 4681 | return false; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4682 | |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4683 | // 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] | 4684 | // by an identifier and then either ':' or ']', in a place where an |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4685 | // expression is permitted, then this is probably a class message send |
| 4686 | // missing the initial '['. In this case, we won't consider this to be |
| 4687 | // the start of a declaration. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4688 | if (DisambiguatingWithExpression && |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4689 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 4690 | return false; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4691 | |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4692 | return isDeclarationSpecifier(); |
| 4693 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4694 | case tok::coloncolon: // ::foo::bar |
| 4695 | if (NextToken().is(tok::kw_new) || // ::new |
| 4696 | NextToken().is(tok::kw_delete)) // ::delete |
| 4697 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4698 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4699 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4700 | // recurse to handle whatever we get. |
| 4701 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4702 | return true; |
| 4703 | return isDeclarationSpecifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4704 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4705 | // storage-class-specifier |
| 4706 | case tok::kw_typedef: |
| 4707 | case tok::kw_extern: |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 4708 | case tok::kw___private_extern__: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4709 | case tok::kw_static: |
| 4710 | case tok::kw_auto: |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4711 | case tok::kw___auto_type: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4712 | case tok::kw_register: |
| 4713 | case tok::kw___thread: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4714 | case tok::kw_thread_local: |
| 4715 | case tok::kw__Thread_local: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4716 | |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 4717 | // Modules |
| 4718 | case tok::kw___module_private__: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4719 | |
John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4720 | // Debugger support |
| 4721 | case tok::kw___unknown_anytype: |
| 4722 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4723 | // type-specifiers |
| 4724 | case tok::kw_short: |
| 4725 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4726 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4727 | case tok::kw___int128: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4728 | case tok::kw_signed: |
| 4729 | case tok::kw_unsigned: |
| 4730 | case tok::kw__Complex: |
| 4731 | case tok::kw__Imaginary: |
| 4732 | case tok::kw_void: |
| 4733 | case tok::kw_char: |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4734 | case tok::kw_wchar_t: |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4735 | case tok::kw_char16_t: |
| 4736 | case tok::kw_char32_t: |
| 4737 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4738 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4739 | case tok::kw_half: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4740 | case tok::kw_float: |
| 4741 | case tok::kw_double: |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 4742 | case tok::kw___float128: |
Chris Lattner | bb31a42 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 4743 | case tok::kw_bool: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4744 | case tok::kw__Bool: |
| 4745 | case tok::kw__Decimal32: |
| 4746 | case tok::kw__Decimal64: |
| 4747 | case tok::kw__Decimal128: |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4748 | case tok::kw___vector: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4749 | |
Chris Lattner | 861a226 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 4750 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4751 | case tok::kw_class: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4752 | case tok::kw_struct: |
| 4753 | case tok::kw_union: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4754 | case tok::kw___interface: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4755 | // enum-specifier |
| 4756 | case tok::kw_enum: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4757 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4758 | // type-qualifier |
| 4759 | case tok::kw_const: |
| 4760 | case tok::kw_volatile: |
| 4761 | case tok::kw_restrict: |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4762 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4763 | // function-specifier |
| 4764 | case tok::kw_inline: |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4765 | case tok::kw_virtual: |
| 4766 | case tok::kw_explicit: |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4767 | case tok::kw__Noreturn: |
Chris Lattner | 7b20dc7 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4768 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 4769 | // alignment-specifier |
| 4770 | case tok::kw__Alignas: |
| 4771 | |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4772 | // friend keyword. |
| 4773 | case tok::kw_friend: |
| 4774 | |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 4775 | // static_assert-declaration |
| 4776 | case tok::kw__Static_assert: |
| 4777 | |
Chris Lattner | 599e47e | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4778 | // GNU typeof support. |
| 4779 | case tok::kw_typeof: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4780 | |
Chris Lattner | 599e47e | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4781 | // GNU attributes. |
Chris Lattner | 7b20dc7 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4782 | case tok::kw___attribute: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4783 | |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4784 | // C++11 decltype and constexpr. |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4785 | case tok::annot_decltype: |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4786 | case tok::kw_constexpr: |
Francois Pichet | e878cb6 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 4787 | |
Hubert Tong | 375f00a | 2015-06-30 12:14:52 +0000 | [diff] [blame] | 4788 | // C++ Concepts TS - concept |
| 4789 | case tok::kw_concept: |
| 4790 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4791 | // C11 _Atomic |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4792 | case tok::kw__Atomic: |
| 4793 | return true; |
| 4794 | |
Chris Lattner | 8b2ec16 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 4795 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4796 | case tok::less: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4797 | return getLangOpts().ObjC1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4798 | |
Douglas Gregor | 19b7acf | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 4799 | // typedef-name |
| 4800 | case tok::annot_typename: |
| 4801 | return !DisambiguatingWithExpression || |
| 4802 | !isStartOfObjCClassMessageMissingOpenBracket(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4803 | |
Steve Naroff | f192fab | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 4804 | case tok::kw___declspec: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4805 | case tok::kw___cdecl: |
| 4806 | case tok::kw___stdcall: |
| 4807 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4808 | case tok::kw___thiscall: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4809 | case tok::kw___regcall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4810 | case tok::kw___vectorcall: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4811 | case tok::kw___w64: |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4812 | case tok::kw___sptr: |
| 4813 | case tok::kw___uptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4814 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4815 | case tok::kw___ptr32: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4816 | case tok::kw___forceinline: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4817 | case tok::kw___pascal: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4818 | case tok::kw___unaligned: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4819 | |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 4820 | case tok::kw__Nonnull: |
| 4821 | case tok::kw__Nullable: |
| 4822 | case tok::kw__Null_unspecified: |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 4823 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4824 | case tok::kw___kindof: |
| 4825 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4826 | case tok::kw___private: |
| 4827 | case tok::kw___local: |
| 4828 | case tok::kw___global: |
| 4829 | case tok::kw___constant: |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 4830 | case tok::kw___generic: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4831 | case tok::kw___read_only: |
| 4832 | case tok::kw___read_write: |
| 4833 | case tok::kw___write_only: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 4834 | #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 4835 | #include "clang/Basic/OpenCLImageTypes.def" |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4836 | |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4837 | return true; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4838 | } |
| 4839 | } |
| 4840 | |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 4841 | bool Parser::isConstructorDeclarator(bool IsUnqualified, bool DeductionGuide) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4842 | TentativeParsingAction TPA(*this); |
| 4843 | |
| 4844 | // Parse the C++ scope specifier. |
| 4845 | CXXScopeSpec SS; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 4846 | if (ParseOptionalCXXScopeSpecifier(SS, nullptr, |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4847 | /*EnteringContext=*/true)) { |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4848 | TPA.Revert(); |
| 4849 | return false; |
| 4850 | } |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4851 | |
| 4852 | // Parse the constructor name. |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 4853 | if (Tok.is(tok::identifier)) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4854 | // We already know that we have a constructor name; just consume |
| 4855 | // the token. |
| 4856 | ConsumeToken(); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 4857 | } else if (Tok.is(tok::annot_template_id)) { |
| 4858 | ConsumeAnnotationToken(); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4859 | } else { |
| 4860 | TPA.Revert(); |
| 4861 | return false; |
| 4862 | } |
| 4863 | |
Richard Smith | 8f8697f | 2017-02-08 01:16:55 +0000 | [diff] [blame] | 4864 | // There may be attributes here, appertaining to the constructor name or type |
| 4865 | // we just stepped past. |
| 4866 | SkipCXX11Attributes(); |
| 4867 | |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4868 | // Current class name must be followed by a left parenthesis. |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4869 | if (Tok.isNot(tok::l_paren)) { |
| 4870 | TPA.Revert(); |
| 4871 | return false; |
| 4872 | } |
| 4873 | ConsumeParen(); |
| 4874 | |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4875 | // A right parenthesis, or ellipsis followed by a right parenthesis signals |
| 4876 | // that we have a constructor. |
| 4877 | if (Tok.is(tok::r_paren) || |
| 4878 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4879 | TPA.Revert(); |
| 4880 | return true; |
| 4881 | } |
| 4882 | |
Richard Smith | f216366 | 2013-09-06 00:12:20 +0000 | [diff] [blame] | 4883 | // A C++11 attribute here signals that we have a constructor, and is an |
| 4884 | // attribute on the first constructor parameter. |
| 4885 | if (getLangOpts().CPlusPlus11 && |
| 4886 | isCXX11AttributeSpecifier(/*Disambiguate*/ false, |
| 4887 | /*OuterMightBeMessageSend*/ true)) { |
| 4888 | TPA.Revert(); |
| 4889 | return true; |
| 4890 | } |
| 4891 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4892 | // If we need to, enter the specified scope. |
| 4893 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4894 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4895 | DeclScopeObj.EnterDeclaratorScope(); |
| 4896 | |
Francois Pichet | 79f3a87 | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4897 | // Optionally skip Microsoft attributes. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4898 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | 79f3a87 | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4899 | MaybeParseMicrosoftAttributes(Attrs); |
| 4900 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4901 | // Check whether the next token(s) are part of a declaration |
| 4902 | // specifier, in which case we have the start of a parameter and, |
| 4903 | // therefore, we know that this is a constructor. |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4904 | bool IsConstructor = false; |
| 4905 | if (isDeclarationSpecifier()) |
| 4906 | IsConstructor = true; |
| 4907 | else if (Tok.is(tok::identifier) || |
| 4908 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) { |
| 4909 | // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type. |
| 4910 | // This might be a parenthesized member name, but is more likely to |
| 4911 | // be a constructor declaration with an invalid argument type. Keep |
| 4912 | // looking. |
| 4913 | if (Tok.is(tok::annot_cxxscope)) |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 4914 | ConsumeAnnotationToken(); |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4915 | ConsumeToken(); |
| 4916 | |
| 4917 | // If this is not a constructor, we must be parsing a declarator, |
Richard Smith | 1453e31 | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4918 | // which must have one of the following syntactic forms (see the |
| 4919 | // grammar extract at the start of ParseDirectDeclarator): |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4920 | switch (Tok.getKind()) { |
| 4921 | case tok::l_paren: |
| 4922 | // C(X ( int)); |
| 4923 | case tok::l_square: |
| 4924 | // C(X [ 5]); |
| 4925 | // C(X [ [attribute]]); |
| 4926 | case tok::coloncolon: |
| 4927 | // C(X :: Y); |
| 4928 | // C(X :: *p); |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4929 | // Assume this isn't a constructor, rather than assuming it's a |
| 4930 | // constructor with an unnamed parameter of an ill-formed type. |
| 4931 | break; |
| 4932 | |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 4933 | case tok::r_paren: |
| 4934 | // C(X ) |
Richard Smith | 8f8697f | 2017-02-08 01:16:55 +0000 | [diff] [blame] | 4935 | |
| 4936 | // Skip past the right-paren and any following attributes to get to |
| 4937 | // the function body or trailing-return-type. |
| 4938 | ConsumeParen(); |
| 4939 | SkipCXX11Attributes(); |
| 4940 | |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 4941 | if (DeductionGuide) { |
| 4942 | // C(X) -> ... is a deduction guide. |
Richard Smith | 8f8697f | 2017-02-08 01:16:55 +0000 | [diff] [blame] | 4943 | IsConstructor = Tok.is(tok::arrow); |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 4944 | break; |
| 4945 | } |
Richard Smith | 8f8697f | 2017-02-08 01:16:55 +0000 | [diff] [blame] | 4946 | if (Tok.is(tok::colon) || Tok.is(tok::kw_try)) { |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 4947 | // Assume these were meant to be constructors: |
| 4948 | // C(X) : (the name of a bit-field cannot be parenthesized). |
| 4949 | // C(X) try (this is otherwise ill-formed). |
| 4950 | IsConstructor = true; |
| 4951 | } |
Richard Smith | 8f8697f | 2017-02-08 01:16:55 +0000 | [diff] [blame] | 4952 | if (Tok.is(tok::semi) || Tok.is(tok::l_brace)) { |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 4953 | // If we have a constructor name within the class definition, |
| 4954 | // assume these were meant to be constructors: |
| 4955 | // C(X) { |
| 4956 | // C(X) ; |
| 4957 | // ... because otherwise we would be declaring a non-static data |
| 4958 | // member that is ill-formed because it's of the same type as its |
| 4959 | // surrounding class. |
| 4960 | // |
| 4961 | // FIXME: We can actually do this whether or not the name is qualified, |
| 4962 | // 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] | 4963 | // a constructor name. |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 4964 | // currently, so we're somewhat conservative here. |
| 4965 | IsConstructor = IsUnqualified; |
| 4966 | } |
| 4967 | break; |
| 4968 | |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4969 | default: |
| 4970 | IsConstructor = true; |
| 4971 | break; |
| 4972 | } |
| 4973 | } |
| 4974 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4975 | TPA.Revert(); |
| 4976 | return IsConstructor; |
| 4977 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 4978 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4979 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4980 | /// type-qualifier-list: [C99 6.7.5] |
| 4981 | /// type-qualifier |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4982 | /// [vendor] attributes |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 4983 | /// [ only if AttrReqs & AR_VendorAttributesParsed ] |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4984 | /// type-qualifier-list type-qualifier |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4985 | /// [vendor] type-qualifier-list attributes |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 4986 | /// [ only if AttrReqs & AR_VendorAttributesParsed ] |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4987 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 4988 | /// [ only if AttReqs & AR_CXX11AttributesParsed ] |
| 4989 | /// Note: vendor can be GNU, MS, etc and can be explicitly controlled via |
| 4990 | /// AttrRequirements bitmask values. |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 4991 | void Parser::ParseTypeQualifierListOpt( |
| 4992 | DeclSpec &DS, unsigned AttrReqs, bool AtomicAllowed, |
| 4993 | bool IdentifierRequired, |
| 4994 | Optional<llvm::function_ref<void()>> CodeCompletionHandler) { |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 4995 | if (getLangOpts().CPlusPlus11 && (AttrReqs & AR_CXX11AttributesParsed) && |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4996 | isCXX11AttributeSpecifier()) { |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4997 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4998 | ParseCXX11Attributes(attrs); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4999 | DS.takeAttributesFrom(attrs); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5000 | } |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 5001 | |
| 5002 | SourceLocation EndLoc; |
| 5003 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5004 | while (1) { |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5005 | bool isInvalid = false; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5006 | const char *PrevSpec = nullptr; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5007 | unsigned DiagID = 0; |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 5008 | SourceLocation Loc = Tok.getLocation(); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 5009 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5010 | switch (Tok.getKind()) { |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 5011 | case tok::code_completion: |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 5012 | if (CodeCompletionHandler) |
| 5013 | (*CodeCompletionHandler)(); |
| 5014 | else |
| 5015 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 5016 | return cutOffParsing(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5017 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5018 | case tok::kw_const: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5019 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 5020 | getLangOpts()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 5021 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5022 | case tok::kw_volatile: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5023 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 5024 | getLangOpts()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 5025 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5026 | case tok::kw_restrict: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5027 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 5028 | getLangOpts()); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5029 | break; |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5030 | case tok::kw__Atomic: |
| 5031 | if (!AtomicAllowed) |
| 5032 | goto DoneWithTypeQuals; |
| 5033 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 5034 | getLangOpts()); |
| 5035 | break; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 5036 | |
| 5037 | // OpenCL qualifiers: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 5038 | case tok::kw___private: |
| 5039 | case tok::kw___global: |
| 5040 | case tok::kw___local: |
| 5041 | case tok::kw___constant: |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 5042 | case tok::kw___generic: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 5043 | case tok::kw___read_only: |
| 5044 | case tok::kw___write_only: |
| 5045 | case tok::kw___read_write: |
Aaron Ballman | 05d76ea | 2014-01-14 01:29:54 +0000 | [diff] [blame] | 5046 | ParseOpenCLQualifiers(DS.getAttributes()); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 5047 | break; |
| 5048 | |
Andrey Bokhanko | 45d4132 | 2016-05-11 18:38:21 +0000 | [diff] [blame] | 5049 | case tok::kw___unaligned: |
| 5050 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID, |
| 5051 | getLangOpts()); |
| 5052 | break; |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 5053 | case tok::kw___uptr: |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 5054 | // GNU libc headers in C mode use '__uptr' as an identifer which conflicts |
| 5055 | // with the MS modifier keyword. |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5056 | if ((AttrReqs & AR_DeclspecAttributesParsed) && !getLangOpts().CPlusPlus && |
Alp Toker | 47642d2 | 2013-12-03 06:13:01 +0000 | [diff] [blame] | 5057 | IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) { |
| 5058 | if (TryKeywordIdentFallback(false)) |
| 5059 | continue; |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 5060 | } |
Galina Kistanova | 7767425 | 2017-06-01 21:15:34 +0000 | [diff] [blame] | 5061 | LLVM_FALLTHROUGH; |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 5062 | case tok::kw___sptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 5063 | case tok::kw___w64: |
Steve Naroff | f9c29d4 | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 5064 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 5065 | case tok::kw___ptr32: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 5066 | case tok::kw___cdecl: |
| 5067 | case tok::kw___stdcall: |
| 5068 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 5069 | case tok::kw___thiscall: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 5070 | case tok::kw___regcall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 5071 | case tok::kw___vectorcall: |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5072 | if (AttrReqs & AR_DeclspecAttributesParsed) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5073 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 5074 | continue; |
| 5075 | } |
| 5076 | goto DoneWithTypeQuals; |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5077 | case tok::kw___pascal: |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5078 | if (AttrReqs & AR_VendorAttributesParsed) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5079 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5080 | continue; |
| 5081 | } |
| 5082 | goto DoneWithTypeQuals; |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 5083 | |
| 5084 | // Nullability type specifiers. |
Douglas Gregor | aea7afd | 2015-06-24 22:02:08 +0000 | [diff] [blame] | 5085 | case tok::kw__Nonnull: |
| 5086 | case tok::kw__Nullable: |
| 5087 | case tok::kw__Null_unspecified: |
Douglas Gregor | 261a89b | 2015-06-19 17:51:05 +0000 | [diff] [blame] | 5088 | ParseNullabilityTypeSpecifiers(DS.getAttributes()); |
| 5089 | continue; |
| 5090 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 5091 | // Objective-C 'kindof' types. |
| 5092 | case tok::kw___kindof: |
| 5093 | DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc, |
| 5094 | nullptr, 0, AttributeList::AS_Keyword); |
| 5095 | (void)ConsumeToken(); |
| 5096 | continue; |
| 5097 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 5098 | case tok::kw___attribute: |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5099 | if (AttrReqs & AR_GNUAttributesParsedAndRejected) |
| 5100 | // When GNU attributes are expressly forbidden, diagnose their usage. |
| 5101 | Diag(Tok, diag::err_attributes_not_allowed); |
| 5102 | |
| 5103 | // Parse the attributes even if they are rejected to ensure that error |
| 5104 | // recovery is graceful. |
| 5105 | if (AttrReqs & AR_GNUAttributesParsed || |
| 5106 | AttrReqs & AR_GNUAttributesParsedAndRejected) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5107 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5108 | continue; // do *not* consume the next token! |
| 5109 | } |
| 5110 | // otherwise, FALL THROUGH! |
Galina Kistanova | 7767425 | 2017-06-01 21:15:34 +0000 | [diff] [blame] | 5111 | LLVM_FALLTHROUGH; |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5112 | default: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 5113 | DoneWithTypeQuals: |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5114 | // If this is not a type-qualifier token, we're done reading type |
| 5115 | // qualifiers. First verify that DeclSpec's are consistent. |
Craig Topper | 2512241 | 2015-11-15 03:32:11 +0000 | [diff] [blame] | 5116 | DS.Finish(Actions, Actions.getASTContext().getPrintingPolicy()); |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 5117 | if (EndLoc.isValid()) |
| 5118 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5119 | return; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5120 | } |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5121 | |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 5122 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 5123 | if (isInvalid) { |
| 5124 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 5125 | Diag(Tok, DiagID) << PrevSpec; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 5126 | } |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 5127 | EndLoc = ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5128 | } |
| 5129 | } |
| 5130 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 5131 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 5132 | /// |
| 5133 | void Parser::ParseDeclarator(Declarator &D) { |
| 5134 | /// This implements the 'declarator' production in the C grammar, then checks |
| 5135 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5136 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 5137 | } |
| 5138 | |
Richard Smith | 0b350b9 | 2014-10-28 16:55:02 +0000 | [diff] [blame] | 5139 | static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang, |
| 5140 | unsigned TheContext) { |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5141 | if (Kind == tok::star || Kind == tok::caret) |
| 5142 | return true; |
| 5143 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5144 | if ((Kind == tok::kw_pipe) && Lang.OpenCL && (Lang.OpenCLVersion >= 200)) |
| 5145 | return true; |
| 5146 | |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5147 | if (!Lang.CPlusPlus) |
| 5148 | return false; |
| 5149 | |
Richard Smith | 0b350b9 | 2014-10-28 16:55:02 +0000 | [diff] [blame] | 5150 | if (Kind == tok::amp) |
| 5151 | return true; |
| 5152 | |
| 5153 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 5154 | // But we must not parse them in conversion-type-ids and new-type-ids, since |
| 5155 | // those can be legitimately followed by a && operator. |
| 5156 | // (The same thing can in theory happen after a trailing-return-type, but |
| 5157 | // since those are a C++11 feature, there is no rejects-valid issue there.) |
| 5158 | if (Kind == tok::ampamp) |
| 5159 | return Lang.CPlusPlus11 || (TheContext != Declarator::ConversionIdContext && |
| 5160 | TheContext != Declarator::CXXNewContext); |
| 5161 | |
| 5162 | return false; |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5163 | } |
| 5164 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5165 | // Indicates whether the given declarator is a pipe declarator. |
| 5166 | static bool isPipeDeclerator(const Declarator &D) { |
| 5167 | const unsigned NumTypes = D.getNumTypeObjects(); |
| 5168 | |
| 5169 | for (unsigned Idx = 0; Idx != NumTypes; ++Idx) |
| 5170 | if (DeclaratorChunk::Pipe == D.getTypeObject(Idx).Kind) |
| 5171 | return true; |
| 5172 | |
| 5173 | return false; |
| 5174 | } |
| 5175 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5176 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 5177 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 5178 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5179 | /// ptr-operator production. |
| 5180 | /// |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 5181 | /// 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] | 5182 | /// made to TryParseDeclarator and MightBeDeclarator, and possibly to |
| 5183 | /// isConstructorDeclarator. |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 5184 | /// |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5185 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 5186 | /// [C] pointer[opt] direct-declarator |
| 5187 | /// [C++] direct-declarator |
| 5188 | /// [C++] ptr-operator declarator |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 5189 | /// |
| 5190 | /// pointer: [C99 6.7.5] |
| 5191 | /// '*' type-qualifier-list[opt] |
| 5192 | /// '*' type-qualifier-list[opt] pointer |
| 5193 | /// |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5194 | /// ptr-operator: |
| 5195 | /// '*' cv-qualifier-seq[opt] |
| 5196 | /// '&' |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 5197 | /// [C++0x] '&&' |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5198 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 5199 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5200 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5201 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 5202 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 66a985d | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 5203 | if (Diags.hasAllExtensionsSilenced()) |
| 5204 | D.setExtension(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5205 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5206 | // C++ member pointers start with a '::' or a nested-name. |
| 5207 | // Member pointers get special handling, since there's no place for the |
| 5208 | // scope spec in the generic path below. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5209 | if (getLangOpts().CPlusPlus && |
Richard Smith | 83c2ecf | 2016-02-02 23:34:49 +0000 | [diff] [blame] | 5210 | (Tok.is(tok::coloncolon) || Tok.is(tok::kw_decltype) || |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 5211 | (Tok.is(tok::identifier) && |
| 5212 | (NextToken().is(tok::coloncolon) || NextToken().is(tok::less))) || |
Chris Lattner | 803802d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 5213 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 5214 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 5215 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5216 | CXXScopeSpec SS; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 5217 | ParseOptionalCXXScopeSpecifier(SS, nullptr, EnteringContext); |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 5218 | |
Jeffrey Yasskin | 4e150f8 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 5219 | if (SS.isNotEmpty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5220 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5221 | // The scope spec really belongs to the direct-declarator. |
Richard Smith | 5f044ad | 2013-01-08 22:43:49 +0000 | [diff] [blame] | 5222 | if (D.mayHaveIdentifier()) |
| 5223 | D.getCXXScopeSpec() = SS; |
| 5224 | else |
| 5225 | AnnotateScopeToken(SS, true); |
| 5226 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5227 | if (DirectDeclParser) |
| 5228 | (this->*DirectDeclParser)(D); |
| 5229 | return; |
| 5230 | } |
| 5231 | |
| 5232 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5233 | D.SetRangeEnd(Loc); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5234 | DeclSpec DS(AttrFactory); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5235 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5236 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5237 | |
| 5238 | // Recurse to parse whatever is left. |
| 5239 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 5240 | |
| 5241 | // Sema will have to catch (syntactically invalid) pointers into global |
| 5242 | // scope. It has to catch pointers into namespace scope anyway. |
| 5243 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
Richard Smith | a865a16 | 2014-12-19 02:07:47 +0000 | [diff] [blame] | 5244 | DS.getLocEnd()), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5245 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5246 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5247 | return; |
| 5248 | } |
| 5249 | } |
| 5250 | |
| 5251 | tok::TokenKind Kind = Tok.getKind(); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5252 | |
| 5253 | if (D.getDeclSpec().isTypeSpecPipe() && !isPipeDeclerator(D)) { |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 5254 | DeclSpec DS(AttrFactory); |
| 5255 | ParseTypeQualifierListOpt(DS); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5256 | |
| 5257 | D.AddTypeInfo( |
| 5258 | DeclaratorChunk::getPipe(DS.getTypeQualifiers(), DS.getPipeLoc()), |
| 5259 | DS.getAttributes(), SourceLocation()); |
| 5260 | } |
| 5261 | |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 5262 | // Not a pointer, C++ reference, or block. |
Richard Smith | 0b350b9 | 2014-10-28 16:55:02 +0000 | [diff] [blame] | 5263 | if (!isPtrOperatorToken(Kind, getLangOpts(), D.getContext())) { |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5264 | if (DirectDeclParser) |
| 5265 | (this->*DirectDeclParser)(D); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5266 | return; |
| 5267 | } |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5268 | |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 5269 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 5270 | // '&&' -> rvalue reference |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 5271 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5272 | D.SetRangeEnd(Loc); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5273 | |
Chris Lattner | 9eac931 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 5274 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 788404f | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 5275 | // Is a pointer. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5276 | DeclSpec DS(AttrFactory); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5277 | |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5278 | // GNU attributes are not allowed here in a new-type-id, but Declspec and |
| 5279 | // C++11 attributes are allowed. |
| 5280 | unsigned Reqs = AR_CXX11AttributesParsed | AR_DeclspecAttributesParsed | |
| 5281 | ((D.getContext() != Declarator::CXXNewContext) |
| 5282 | ? AR_GNUAttributesParsed |
| 5283 | : AR_GNUAttributesParsedAndRejected); |
| 5284 | ParseTypeQualifierListOpt(DS, Reqs, true, !D.mayOmitIdentifier()); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5285 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 5286 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5287 | // Recursively parse the declarator. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5288 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 5289 | if (Kind == tok::star) |
| 5290 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 5291 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | e71b378d | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 5292 | DS.getConstSpecLoc(), |
| 5293 | DS.getVolatileSpecLoc(), |
Benjamin Kramer | d0028dc | 2015-03-29 16:42:06 +0000 | [diff] [blame] | 5294 | DS.getRestrictSpecLoc(), |
Andrey Bokhanko | 45d4132 | 2016-05-11 18:38:21 +0000 | [diff] [blame] | 5295 | DS.getAtomicSpecLoc(), |
| 5296 | DS.getUnalignedSpecLoc()), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5297 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5298 | SourceLocation()); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 5299 | else |
| 5300 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5301 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5302 | Loc), |
| 5303 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5304 | SourceLocation()); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5305 | } else { |
| 5306 | // Is a reference |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5307 | DeclSpec DS(AttrFactory); |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 5308 | |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 5309 | // Complain about rvalue references in C++03, but then go on and build |
| 5310 | // the declarator. |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5311 | if (Kind == tok::ampamp) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5312 | Diag(Loc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5313 | diag::warn_cxx98_compat_rvalue_reference : |
| 5314 | diag::ext_rvalue_reference); |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 5315 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5316 | // GNU-style and C++11 attributes are allowed here, as is restrict. |
| 5317 | ParseTypeQualifierListOpt(DS); |
| 5318 | D.ExtendWithDeclSpec(DS); |
| 5319 | |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 5320 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 5321 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 5322 | // template type argument, in which case the cv-qualifiers are ignored. |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 5323 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 5324 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 5325 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 5326 | diag::err_invalid_reference_qualifier_application) << "const"; |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 5327 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 5328 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 5329 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5330 | // 'restrict' is permitted as an extension. |
| 5331 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
| 5332 | Diag(DS.getAtomicSpecLoc(), |
| 5333 | diag::err_invalid_reference_qualifier_application) << "_Atomic"; |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 5334 | } |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5335 | |
| 5336 | // Recursively parse the declarator. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5337 | ParseDeclaratorInternal(D, DirectDeclParser); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5338 | |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 5339 | if (D.getNumTypeObjects() > 0) { |
| 5340 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 5341 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 5342 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | ebad6a2 | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 5343 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 5344 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 5345 | << II; |
| 5346 | else |
| 5347 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 5348 | << "type name"; |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 5349 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5350 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 5351 | // can go ahead and build the (technically ill-formed) |
| 5352 | // declarator: reference collapsing will take care of it. |
| 5353 | } |
| 5354 | } |
| 5355 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5356 | // Remember that we parsed a reference type. |
Chris Lattner | 788404f | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 5357 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 5358 | Kind == tok::amp), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5359 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5360 | SourceLocation()); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 5361 | } |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 5362 | } |
| 5363 | |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5364 | // When correcting from misplaced brackets before the identifier, the location |
| 5365 | // is saved inside the declarator so that other diagnostic messages can use |
| 5366 | // them. This extracts and returns that location, or returns the provided |
| 5367 | // location if a stored location does not exist. |
| 5368 | static SourceLocation getMissingDeclaratorIdLoc(Declarator &D, |
| 5369 | SourceLocation Loc) { |
| 5370 | if (D.getName().StartLocation.isInvalid() && |
| 5371 | D.getName().EndLocation.isValid()) |
| 5372 | return D.getName().EndLocation; |
| 5373 | |
| 5374 | return Loc; |
| 5375 | } |
| 5376 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5377 | /// ParseDirectDeclarator |
| 5378 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5379 | /// [C99] identifier |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5380 | /// '(' declarator ')' |
| 5381 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5382 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 5383 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 5384 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 5385 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 5386 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5387 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 5388 | /// attribute-specifier-seq[opt] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5389 | /// direct-declarator '(' parameter-type-list ')' |
| 5390 | /// direct-declarator '(' identifier-list[opt] ')' |
| 5391 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 5392 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 22c40fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 5393 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 5394 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5395 | /// [C++11] direct-declarator '(' parameter-declaration-clause ')' |
| 5396 | /// attribute-specifier-seq[opt] cv-qualifier-seq[opt] |
| 5397 | /// ref-qualifier[opt] exception-specification[opt] |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 5398 | /// [C++] declarator-id |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5399 | /// [C++11] declarator-id attribute-specifier-seq[opt] |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5400 | /// |
| 5401 | /// declarator-id: [C++ 8] |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 5402 | /// '...'[opt] id-expression |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5403 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 5404 | /// |
| 5405 | /// id-expression: [C++ 5.1] |
| 5406 | /// unqualified-id |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5407 | /// qualified-id |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5408 | /// |
| 5409 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5410 | /// identifier |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 5411 | /// operator-function-id |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5412 | /// conversion-function-id |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5413 | /// '~' class-name |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 5414 | /// template-id |
Argyrios Kyrtzidis | e442635 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 5415 | /// |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 5416 | /// C++17 adds the following, which we also handle here: |
| 5417 | /// |
| 5418 | /// simple-declaration: |
| 5419 | /// <decl-spec> '[' identifier-list ']' brace-or-equal-initializer ';' |
| 5420 | /// |
Richard Smith | 1453e31 | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 5421 | /// Note, any additional constructs added here may need corresponding changes |
| 5422 | /// in isConstructorDeclarator. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5423 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5424 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 5425 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5426 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 5427 | // This might be a C++17 structured binding. |
| 5428 | if (Tok.is(tok::l_square) && !D.mayOmitIdentifier() && |
| 5429 | D.getCXXScopeSpec().isEmpty()) |
| 5430 | return ParseDecompositionDeclarator(D); |
| 5431 | |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 5432 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR inside a class, in |
| 5433 | // this context it is a bitfield. Also in range-based for statement colon |
| 5434 | // may delimit for-range-declaration. |
| 5435 | ColonProtectionRAIIObject X(*this, |
| 5436 | D.getContext() == Declarator::MemberContext || |
| 5437 | (D.getContext() == Declarator::ForContext && |
| 5438 | getLangOpts().CPlusPlus11)); |
| 5439 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5440 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5441 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 5442 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 5443 | D.getContext() == Declarator::MemberContext; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 5444 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), nullptr, |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 5445 | EnteringContext); |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 5446 | } |
| 5447 | |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5448 | if (D.getCXXScopeSpec().isValid()) { |
Richard Smith | 64e033f | 2015-01-15 00:48:52 +0000 | [diff] [blame] | 5449 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), |
| 5450 | D.getCXXScopeSpec())) |
John McCall | 2b058ef | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 5451 | // Change the declaration context for name lookup, until this function |
| 5452 | // is exited (and the declarator has been parsed). |
| 5453 | DeclScopeObj.EnterDeclaratorScope(); |
Alex Lorenz | e151f010 | 2016-12-07 10:24:44 +0000 | [diff] [blame] | 5454 | else if (getObjCDeclContext()) { |
| 5455 | // Ensure that we don't interpret the next token as an identifier when |
| 5456 | // dealing with declarations in an Objective-C container. |
| 5457 | D.SetIdentifier(nullptr, Tok.getLocation()); |
| 5458 | D.setInvalidType(true); |
| 5459 | ConsumeToken(); |
| 5460 | goto PastIdentifier; |
| 5461 | } |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5462 | } |
| 5463 | |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 5464 | // C++0x [dcl.fct]p14: |
Faisal Vali | 8435a69 | 2014-12-04 12:40:21 +0000 | [diff] [blame] | 5465 | // There is a syntactic ambiguity when an ellipsis occurs at the end of a |
| 5466 | // parameter-declaration-clause without a preceding comma. In this case, |
| 5467 | // the ellipsis is parsed as part of the abstract-declarator if the type |
| 5468 | // of the parameter either names a template parameter pack that has not |
| 5469 | // been expanded or contains auto; otherwise, it is parsed as part of the |
| 5470 | // parameter-declaration-clause. |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5471 | if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() && |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 5472 | !((D.getContext() == Declarator::PrototypeContext || |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5473 | D.getContext() == Declarator::LambdaExprParameterContext || |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 5474 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 5475 | NextToken().is(tok::r_paren) && |
Richard Smith | b19337f | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 5476 | !D.hasGroupingParens() && |
Faisal Vali | 8435a69 | 2014-12-04 12:40:21 +0000 | [diff] [blame] | 5477 | !Actions.containsUnexpandedParameterPacks(D) && |
| 5478 | D.getDeclSpec().getTypeSpecType() != TST_auto)) { |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5479 | SourceLocation EllipsisLoc = ConsumeToken(); |
Richard Smith | 0b350b9 | 2014-10-28 16:55:02 +0000 | [diff] [blame] | 5480 | if (isPtrOperatorToken(Tok.getKind(), getLangOpts(), D.getContext())) { |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5481 | // The ellipsis was put in the wrong place. Recover, and explain to |
| 5482 | // the user what they should have done. |
| 5483 | ParseDeclarator(D); |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 5484 | if (EllipsisLoc.isValid()) |
| 5485 | DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D); |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5486 | return; |
| 5487 | } else |
| 5488 | D.setEllipsisLoc(EllipsisLoc); |
| 5489 | |
| 5490 | // The ellipsis can't be followed by a parenthesized declarator. We |
| 5491 | // check for that in ParseParenDeclarator, after we have disambiguated |
| 5492 | // the l_paren token. |
| 5493 | } |
| 5494 | |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 5495 | if (Tok.isOneOf(tok::identifier, tok::kw_operator, tok::annot_template_id, |
| 5496 | tok::tilde)) { |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5497 | // We found something that indicates the start of an unqualified-id. |
| 5498 | // Parse that unqualified-id. |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 5499 | bool AllowConstructorName; |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5500 | bool AllowDeductionGuide; |
| 5501 | if (D.getDeclSpec().hasTypeSpecifier()) { |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 5502 | AllowConstructorName = false; |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5503 | AllowDeductionGuide = false; |
| 5504 | } else if (D.getCXXScopeSpec().isSet()) { |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 5505 | AllowConstructorName = |
| 5506 | (D.getContext() == Declarator::FileContext || |
Dmitri Gribenko | d1c91f1 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 5507 | D.getContext() == Declarator::MemberContext); |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5508 | AllowDeductionGuide = false; |
| 5509 | } else { |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 5510 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5511 | AllowDeductionGuide = |
| 5512 | (D.getContext() == Declarator::FileContext || |
| 5513 | D.getContext() == Declarator::MemberContext); |
| 5514 | } |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 5515 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 5516 | SourceLocation TemplateKWLoc; |
Richard Smith | 64e033f | 2015-01-15 00:48:52 +0000 | [diff] [blame] | 5517 | bool HadScope = D.getCXXScopeSpec().isValid(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5518 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 5519 | /*EnteringContext=*/true, |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 5520 | /*AllowDestructorName=*/true, AllowConstructorName, |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 5521 | AllowDeductionGuide, nullptr, TemplateKWLoc, |
| 5522 | D.getName()) || |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5523 | // Once we're past the identifier, if the scope was bad, mark the |
| 5524 | // whole declarator bad. |
| 5525 | D.getCXXScopeSpec().isInvalid()) { |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5526 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5527 | D.setInvalidType(true); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5528 | } else { |
Richard Smith | 64e033f | 2015-01-15 00:48:52 +0000 | [diff] [blame] | 5529 | // ParseUnqualifiedId might have parsed a scope specifier during error |
| 5530 | // recovery. If it did so, enter that scope. |
| 5531 | if (!HadScope && D.getCXXScopeSpec().isValid() && |
| 5532 | Actions.ShouldEnterDeclaratorScope(getCurScope(), |
| 5533 | D.getCXXScopeSpec())) |
| 5534 | DeclScopeObj.EnterDeclaratorScope(); |
| 5535 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5536 | // Parsed the unqualified-id; update range information and move along. |
| 5537 | if (D.getSourceRange().getBegin().isInvalid()) |
| 5538 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 5539 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5540 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5541 | goto PastIdentifier; |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5542 | } |
Richard Smith | d63db6e | 2015-12-19 02:40:19 +0000 | [diff] [blame] | 5543 | |
| 5544 | if (D.getCXXScopeSpec().isNotEmpty()) { |
| 5545 | // We have a scope specifier but no following unqualified-id. |
| 5546 | Diag(PP.getLocForEndOfToken(D.getCXXScopeSpec().getEndLoc()), |
| 5547 | diag::err_expected_unqualified_id) |
| 5548 | << /*C++*/1; |
| 5549 | D.SetIdentifier(nullptr, Tok.getLocation()); |
| 5550 | goto PastIdentifier; |
| 5551 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5552 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5553 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5554 | "There's a C++-specific check for tok::identifier above"); |
| 5555 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 5556 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
Richard Trieu | 2664dc1 | 2014-05-05 22:06:50 +0000 | [diff] [blame] | 5557 | D.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5558 | ConsumeToken(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5559 | goto PastIdentifier; |
Richard Smith | 74639b1 | 2017-05-19 01:54:59 +0000 | [diff] [blame] | 5560 | } else if (Tok.is(tok::identifier) && !D.mayHaveIdentifier()) { |
| 5561 | // We're not allowed an identifier here, but we got one. Try to figure out |
| 5562 | // if the user was trying to attach a name to the type, or whether the name |
| 5563 | // is some unrelated trailing syntax. |
| 5564 | bool DiagnoseIdentifier = false; |
| 5565 | if (D.hasGroupingParens()) |
| 5566 | // An identifier within parens is unlikely to be intended to be anything |
| 5567 | // other than a name being "declared". |
| 5568 | DiagnoseIdentifier = true; |
| 5569 | else if (D.getContext() == Declarator::TemplateTypeArgContext) |
| 5570 | // T<int N> is an accidental identifier; T<int N indicates a missing '>'. |
| 5571 | DiagnoseIdentifier = |
| 5572 | NextToken().isOneOf(tok::comma, tok::greater, tok::greatergreater); |
| 5573 | else if (D.getContext() == Declarator::AliasDeclContext || |
| 5574 | D.getContext() == Declarator::AliasTemplateContext) |
| 5575 | // The most likely error is that the ';' was forgotten. |
| 5576 | DiagnoseIdentifier = NextToken().isOneOf(tok::comma, tok::semi); |
| 5577 | else if (D.getContext() == Declarator::TrailingReturnContext && |
| 5578 | !isCXX11VirtSpecifier(Tok)) |
| 5579 | DiagnoseIdentifier = NextToken().isOneOf( |
| 5580 | tok::comma, tok::semi, tok::equal, tok::l_brace, tok::kw_try); |
| 5581 | if (DiagnoseIdentifier) { |
Richard Smith | f39720b | 2013-10-13 22:12:28 +0000 | [diff] [blame] | 5582 | Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id) |
| 5583 | << FixItHint::CreateRemoval(Tok.getLocation()); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5584 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Richard Smith | f39720b | 2013-10-13 22:12:28 +0000 | [diff] [blame] | 5585 | ConsumeToken(); |
| 5586 | goto PastIdentifier; |
| 5587 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5588 | } |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5589 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5590 | if (Tok.is(tok::l_paren)) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5591 | // direct-declarator: '(' declarator ')' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 5592 | // direct-declarator: '(' attributes declarator ')' |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5593 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 5594 | ParseParenDeclarator(D); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5595 | |
| 5596 | // If the declarator was parenthesized, we entered the declarator |
| 5597 | // scope when parsing the parenthesized declarator, then exited |
| 5598 | // the scope already. Re-enter the scope, if we need to. |
| 5599 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 5600 | // If there was an error parsing parenthesized declarator, declarator |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5601 | // scope may have been entered before. Don't do it again. |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 5602 | if (!D.isInvalidType() && |
Nico Weber | 6b05f38 | 2015-02-18 04:53:03 +0000 | [diff] [blame] | 5603 | Actions.ShouldEnterDeclaratorScope(getCurScope(), |
| 5604 | D.getCXXScopeSpec())) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5605 | // Change the declaration context for name lookup, until this function |
| 5606 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 5607 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5608 | } |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5609 | } else if (D.mayOmitIdentifier()) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5610 | // This could be something simple like "int" (in which case the declarator |
| 5611 | // portion is empty), if an abstract-declarator is allowed. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5612 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Richard Smith | b19337f | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 5613 | |
| 5614 | // The grammar for abstract-pack-declarator does not allow grouping parens. |
| 5615 | // FIXME: Revisit this once core issue 1488 is resolved. |
| 5616 | if (D.hasEllipsis() && D.hasGroupingParens()) |
| 5617 | Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()), |
| 5618 | diag::ext_abstract_pack_declarator_parens); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5619 | } else { |
David Blaikie | 5d577a2 | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 5620 | if (Tok.getKind() == tok::annot_pragma_parser_crash) |
David Blaikie | 5bd4c2a | 2012-08-21 18:56:49 +0000 | [diff] [blame] | 5621 | LLVM_BUILTIN_TRAP; |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5622 | if (Tok.is(tok::l_square)) |
| 5623 | return ParseMisplacedBracketDeclarator(D); |
| 5624 | if (D.getContext() == Declarator::MemberContext) { |
Alex Lorenz | f127821 | 2017-04-11 15:01:53 +0000 | [diff] [blame] | 5625 | // Objective-C++: Detect C++ keywords and try to prevent further errors by |
| 5626 | // treating these keyword as valid member names. |
| 5627 | if (getLangOpts().ObjC1 && getLangOpts().CPlusPlus && |
| 5628 | Tok.getIdentifierInfo() && |
| 5629 | Tok.getIdentifierInfo()->isCPlusPlusKeyword(getLangOpts())) { |
| 5630 | Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()), |
| 5631 | diag::err_expected_member_name_or_semi_objcxx_keyword) |
| 5632 | << Tok.getIdentifierInfo() |
| 5633 | << (D.getDeclSpec().isEmpty() ? SourceRange() |
| 5634 | : D.getDeclSpec().getSourceRange()); |
| 5635 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 5636 | D.SetRangeEnd(Tok.getLocation()); |
| 5637 | ConsumeToken(); |
| 5638 | goto PastIdentifier; |
| 5639 | } |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5640 | Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()), |
| 5641 | diag::err_expected_member_name_or_semi) |
Richard Trieu | a134240 | 2014-05-02 23:40:32 +0000 | [diff] [blame] | 5642 | << (D.getDeclSpec().isEmpty() ? SourceRange() |
| 5643 | : D.getDeclSpec().getSourceRange()); |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5644 | } else if (getLangOpts().CPlusPlus) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 5645 | if (Tok.isOneOf(tok::period, tok::arrow)) |
Richard Trieu | 9c67267 | 2013-01-26 02:31:38 +0000 | [diff] [blame] | 5646 | Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow); |
Richard Trieu | 2f58696 | 2013-09-05 02:31:33 +0000 | [diff] [blame] | 5647 | else { |
| 5648 | SourceLocation Loc = D.getCXXScopeSpec().getEndLoc(); |
| 5649 | if (Tok.isAtStartOfLine() && Loc.isValid()) |
| 5650 | Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id) |
| 5651 | << getLangOpts().CPlusPlus; |
| 5652 | else |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5653 | Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()), |
| 5654 | diag::err_expected_unqualified_id) |
Richard Trieu | 2f58696 | 2013-09-05 02:31:33 +0000 | [diff] [blame] | 5655 | << getLangOpts().CPlusPlus; |
| 5656 | } |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5657 | } else { |
| 5658 | Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()), |
| 5659 | diag::err_expected_either) |
| 5660 | << tok::identifier << tok::l_paren; |
| 5661 | } |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5662 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Chris Lattner | 8c5dd73 | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 5663 | D.setInvalidType(true); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5664 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5665 | |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5666 | PastIdentifier: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5667 | assert(D.isPastIdentifier() && |
| 5668 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5669 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5670 | // Don't parse attributes unless we have parsed an unparenthesized name. |
| 5671 | if (D.hasName() && !D.getNumTypeObjects()) |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5672 | MaybeParseCXX11Attributes(D); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5673 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5674 | while (1) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5675 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5676 | // Enter function-declaration scope, limiting any declarators to the |
| 5677 | // function prototype scope, including parameter declarators. |
| 5678 | ParseScope PrototypeScope(this, |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 5679 | Scope::FunctionPrototypeScope|Scope::DeclScope| |
| 5680 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 5681 | ? Scope::FunctionDeclarationScope : 0)); |
| 5682 | |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5683 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 5684 | // In such a case, check if we actually have a function declarator; if it |
| 5685 | // is not, the declarator has been fully parsed. |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5686 | bool IsAmbiguous = false; |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 5687 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 5688 | // The name of the declarator, if any, is tentatively declared within |
| 5689 | // a possible direct initializer. |
| 5690 | TentativelyDeclaredIdentifiers.push_back(D.getIdentifier()); |
| 5691 | bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous); |
| 5692 | TentativelyDeclaredIdentifiers.pop_back(); |
| 5693 | if (!IsFunctionDecl) |
| 5694 | break; |
| 5695 | } |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5696 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5697 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 5698 | T.consumeOpen(); |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5699 | ParseFunctionDeclarator(D, attrs, T, IsAmbiguous); |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5700 | PrototypeScope.Exit(); |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5701 | } else if (Tok.is(tok::l_square)) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5702 | ParseBracketDeclarator(D); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5703 | } else { |
| 5704 | break; |
| 5705 | } |
| 5706 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5707 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5708 | |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 5709 | void Parser::ParseDecompositionDeclarator(Declarator &D) { |
| 5710 | assert(Tok.is(tok::l_square)); |
| 5711 | |
| 5712 | // If this doesn't look like a structured binding, maybe it's a misplaced |
| 5713 | // array declarator. |
| 5714 | // FIXME: Consume the l_square first so we don't need extra lookahead for |
| 5715 | // this. |
| 5716 | if (!(NextToken().is(tok::identifier) && |
| 5717 | GetLookAheadToken(2).isOneOf(tok::comma, tok::r_square)) && |
| 5718 | !(NextToken().is(tok::r_square) && |
| 5719 | GetLookAheadToken(2).isOneOf(tok::equal, tok::l_brace))) |
| 5720 | return ParseMisplacedBracketDeclarator(D); |
| 5721 | |
| 5722 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 5723 | T.consumeOpen(); |
| 5724 | |
| 5725 | SmallVector<DecompositionDeclarator::Binding, 32> Bindings; |
| 5726 | while (Tok.isNot(tok::r_square)) { |
| 5727 | if (!Bindings.empty()) { |
| 5728 | if (Tok.is(tok::comma)) |
| 5729 | ConsumeToken(); |
| 5730 | else { |
| 5731 | if (Tok.is(tok::identifier)) { |
| 5732 | SourceLocation EndLoc = getEndOfPreviousToken(); |
| 5733 | Diag(EndLoc, diag::err_expected) |
| 5734 | << tok::comma << FixItHint::CreateInsertion(EndLoc, ","); |
| 5735 | } else { |
| 5736 | Diag(Tok, diag::err_expected_comma_or_rsquare); |
| 5737 | } |
| 5738 | |
| 5739 | SkipUntil(tok::r_square, tok::comma, tok::identifier, |
| 5740 | StopAtSemi | StopBeforeMatch); |
| 5741 | if (Tok.is(tok::comma)) |
| 5742 | ConsumeToken(); |
| 5743 | else if (Tok.isNot(tok::identifier)) |
| 5744 | break; |
| 5745 | } |
| 5746 | } |
| 5747 | |
| 5748 | if (Tok.isNot(tok::identifier)) { |
| 5749 | Diag(Tok, diag::err_expected) << tok::identifier; |
| 5750 | break; |
| 5751 | } |
| 5752 | |
| 5753 | Bindings.push_back({Tok.getIdentifierInfo(), Tok.getLocation()}); |
| 5754 | ConsumeToken(); |
| 5755 | } |
| 5756 | |
| 5757 | if (Tok.isNot(tok::r_square)) |
| 5758 | // We've already diagnosed a problem here. |
| 5759 | T.skipToEnd(); |
| 5760 | else { |
| 5761 | // C++17 does not allow the identifier-list in a structured binding |
| 5762 | // to be empty. |
| 5763 | if (Bindings.empty()) |
| 5764 | Diag(Tok.getLocation(), diag::ext_decomp_decl_empty); |
| 5765 | |
| 5766 | T.consumeClose(); |
| 5767 | } |
| 5768 | |
| 5769 | return D.setDecompositionBindings(T.getOpenLocation(), Bindings, |
| 5770 | T.getCloseLocation()); |
| 5771 | } |
| 5772 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5773 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 5774 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5775 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5776 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 5777 | /// |
| 5778 | /// direct-declarator: |
| 5779 | /// '(' declarator ')' |
| 5780 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5781 | /// direct-declarator '(' parameter-type-list ')' |
| 5782 | /// direct-declarator '(' identifier-list[opt] ')' |
| 5783 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 5784 | /// parameter-type-list[opt] ')' |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5785 | /// |
| 5786 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5787 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 5788 | T.consumeOpen(); |
| 5789 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5790 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5791 | |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5792 | // Eat any attributes before we look at whether this is a grouping or function |
| 5793 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 5794 | // the type being built up, for example: |
| 5795 | // int (__attribute__(()) *x)(long y) |
| 5796 | // If this ends up not being a grouping paren, the attribute applies to the |
| 5797 | // first argument, for example: |
| 5798 | // int (__attribute__(()) int x) |
| 5799 | // In either case, we need to eat any attributes to be able to determine what |
| 5800 | // sort of paren this is. |
| 5801 | // |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5802 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5803 | bool RequiresArg = false; |
| 5804 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5805 | ParseGNUAttributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5806 | |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5807 | // We require that the argument list (if this is a non-grouping paren) be |
| 5808 | // present even if the attribute list was empty. |
| 5809 | RequiresArg = true; |
| 5810 | } |
Chad Rosier | eea9ca7 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 5811 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 5812 | // Eat any Microsoft extensions. |
Chad Rosier | eea9ca7 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 5813 | ParseMicrosoftTypeAttributes(attrs); |
| 5814 | |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5815 | // Eat any Borland extensions. |
Ted Kremenek | 5eec2b0 | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 5816 | if (Tok.is(tok::kw___pascal)) |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5817 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5818 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5819 | // If we haven't past the identifier yet (or where the identifier would be |
| 5820 | // stored, if this is an abstract declarator), then this is probably just |
| 5821 | // grouping parens. However, if this could be an abstract-declarator, then |
| 5822 | // this could also be the start of function arguments (consider 'void()'). |
| 5823 | bool isGrouping; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5824 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5825 | if (!D.mayOmitIdentifier()) { |
| 5826 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 5827 | // paren, because we haven't seen the identifier yet. |
| 5828 | isGrouping = true; |
| 5829 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 5830 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) && |
| 5831 | NextToken().is(tok::r_paren)) || // C++ int(...) |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5832 | isDeclarationSpecifier() || // 'int(int)' is a function. |
| 5833 | isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function. |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5834 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 5835 | // considered to be a type, not a K&R identifier-list. |
| 5836 | isGrouping = false; |
| 5837 | } else { |
| 5838 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 5839 | isGrouping = true; |
| 5840 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5841 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5842 | // If this is a grouping paren, handle: |
| 5843 | // direct-declarator: '(' declarator ')' |
| 5844 | // direct-declarator: '(' attributes declarator ')' |
| 5845 | if (isGrouping) { |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5846 | SourceLocation EllipsisLoc = D.getEllipsisLoc(); |
| 5847 | D.setEllipsisLoc(SourceLocation()); |
| 5848 | |
Argyrios Kyrtzidis | 8ae3684 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 5849 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5850 | D.setGroupingParens(true); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5851 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5852 | // Match the ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5853 | T.consumeClose(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5854 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5855 | T.getCloseLocation()), |
| 5856 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 8ae3684 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 5857 | |
| 5858 | D.setGroupingParens(hadGroupingParens); |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5859 | |
| 5860 | // An ellipsis cannot be placed outside parentheses. |
| 5861 | if (EllipsisLoc.isValid()) |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 5862 | DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D); |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5863 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5864 | return; |
| 5865 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5866 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5867 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 5868 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5869 | // identifier (and remember where it would have been), then call into |
| 5870 | // ParseFunctionDeclarator to handle of argument list. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5871 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5872 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5873 | // Enter function-declaration scope, limiting any declarators to the |
| 5874 | // function prototype scope, including parameter declarators. |
| 5875 | ParseScope PrototypeScope(this, |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 5876 | Scope::FunctionPrototypeScope | Scope::DeclScope | |
| 5877 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 5878 | ? Scope::FunctionDeclarationScope : 0)); |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5879 | ParseFunctionDeclarator(D, attrs, T, false, RequiresArg); |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5880 | PrototypeScope.Exit(); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5881 | } |
| 5882 | |
| 5883 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 5884 | /// declarator D up to a paren, which indicates that we are parsing function |
| 5885 | /// arguments. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5886 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5887 | /// If FirstArgAttrs is non-null, then the caller parsed those arguments |
| 5888 | /// immediately after the open paren - they should be considered to be the |
| 5889 | /// first argument of a parameter. |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5890 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5891 | /// If RequiresArg is true, then the first argument of the function is required |
| 5892 | /// to be present and required to not be an identifier list. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5893 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5894 | /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt], |
| 5895 | /// (C++11) ref-qualifier[opt], exception-specification[opt], |
| 5896 | /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt]. |
| 5897 | /// |
| 5898 | /// [C++11] exception-specification: |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5899 | /// dynamic-exception-specification |
| 5900 | /// noexcept-specification |
| 5901 | /// |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5902 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5903 | ParsedAttributes &FirstArgAttrs, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5904 | BalancedDelimiterTracker &Tracker, |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5905 | bool IsAmbiguous, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5906 | bool RequiresArg) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5907 | assert(getCurScope()->isFunctionPrototypeScope() && |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5908 | "Should call from a Function scope"); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5909 | // lparen is already consumed! |
| 5910 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 5911 | |
| 5912 | // This should be true when the function has typed arguments. |
| 5913 | // Otherwise, it is treated as a K&R-style function. |
| 5914 | bool HasProto = false; |
| 5915 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5916 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5917 | // Remember where we see an ellipsis, if any. |
| 5918 | SourceLocation EllipsisLoc; |
| 5919 | |
| 5920 | DeclSpec DS(AttrFactory); |
| 5921 | bool RefQualifierIsLValueRef = true; |
| 5922 | SourceLocation RefQualifierLoc; |
Douglas Gregor | e248eea | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 5923 | SourceLocation ConstQualifierLoc; |
| 5924 | SourceLocation VolatileQualifierLoc; |
Hal Finkel | 23a0739 | 2014-10-20 17:32:04 +0000 | [diff] [blame] | 5925 | SourceLocation RestrictQualifierLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5926 | ExceptionSpecificationType ESpecType = EST_None; |
| 5927 | SourceRange ESpecRange; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5928 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 5929 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5930 | ExprResult NoexceptExpr; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 5931 | CachedTokens *ExceptionSpecTokens = nullptr; |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5932 | ParsedAttributes FnAttrs(AttrFactory); |
Richard Smith | 700537c | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 5933 | TypeResult TrailingReturnType; |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5934 | |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5935 | /* LocalEndLoc is the end location for the local FunctionTypeLoc. |
| 5936 | EndLoc is the end location for the function declarator. |
| 5937 | They differ for trailing return types. */ |
| 5938 | SourceLocation StartLoc, LocalEndLoc, EndLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5939 | SourceLocation LParenLoc, RParenLoc; |
| 5940 | LParenLoc = Tracker.getOpenLocation(); |
| 5941 | StartLoc = LParenLoc; |
| 5942 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5943 | if (isFunctionDeclaratorIdentifierList()) { |
| 5944 | if (RequiresArg) |
| 5945 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 5946 | |
| 5947 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 5948 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5949 | Tracker.consumeClose(); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5950 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5951 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5952 | EndLoc = RParenLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5953 | } else { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5954 | if (Tok.isNot(tok::r_paren)) |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5955 | ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, |
| 5956 | EllipsisLoc); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5957 | else if (RequiresArg) |
| 5958 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 5959 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5960 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5961 | |
| 5962 | // If we have the closing ')', eat it. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5963 | Tracker.consumeClose(); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5964 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5965 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5966 | EndLoc = RParenLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5967 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5968 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5969 | // FIXME: Accept these components in any order, and produce fixits to |
| 5970 | // 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] | 5971 | // with the pure-specifier in the same way. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5972 | |
| 5973 | // Parse cv-qualifier-seq[opt]. |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5974 | ParseTypeQualifierListOpt(DS, AR_NoAttributesParsed, |
Alex Lorenz | 8f4d399 | 2017-02-13 23:19:40 +0000 | [diff] [blame] | 5975 | /*AtomicAllowed*/ false, |
| 5976 | /*IdentifierRequired=*/false, |
| 5977 | llvm::function_ref<void()>([&]() { |
| 5978 | Actions.CodeCompleteFunctionQualifiers(DS, D); |
| 5979 | })); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5980 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
| 5981 | EndLoc = DS.getSourceRange().getEnd(); |
| 5982 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 5983 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
Hal Finkel | 23a0739 | 2014-10-20 17:32:04 +0000 | [diff] [blame] | 5984 | RestrictQualifierLoc = DS.getRestrictSpecLoc(); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5985 | } |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5986 | |
| 5987 | // Parse ref-qualifier[opt]. |
Ehsan Akhgari | c07d1e2 | 2015-03-25 00:53:33 +0000 | [diff] [blame] | 5988 | if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc)) |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5989 | EndLoc = RefQualifierLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5990 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5991 | // C++11 [expr.prim.general]p3: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5992 | // If a declaration declares a member function or member function |
| 5993 | // 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] | 5994 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5995 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5996 | // declarator. |
Richard Smith | ad1bbb9 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 5997 | // FIXME: currently, "static" case isn't handled correctly. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5998 | bool IsCXX11MemberFunction = |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5999 | getLangOpts().CPlusPlus11 && |
Richard Smith | 990a692 | 2014-01-17 21:01:18 +0000 | [diff] [blame] | 6000 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
Richard Smith | ad1bbb9 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 6001 | (D.getContext() == Declarator::MemberContext |
| 6002 | ? !D.getDeclSpec().isFriendSpecified() |
| 6003 | : D.getContext() == Declarator::FileContext && |
| 6004 | D.getCXXScopeSpec().isValid() && |
| 6005 | Actions.CurContext->isRecord()); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 6006 | Sema::CXXThisScopeRAII ThisScope(Actions, |
| 6007 | dyn_cast<CXXRecordDecl>(Actions.CurContext), |
Richard Smith | 01141a9 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 6008 | DS.getTypeQualifiers() | |
Richard Smith | 034185c | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 6009 | (D.getDeclSpec().isConstexprSpecified() && |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 6010 | !getLangOpts().CPlusPlus14 |
Richard Smith | 01141a9 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 6011 | ? Qualifiers::Const : 0), |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 6012 | IsCXX11MemberFunction); |
Richard Smith | 2331bbf | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 6013 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6014 | // Parse exception-specification[opt]. |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 6015 | bool Delayed = D.isFirstDeclarationOfMember() && |
Richard Smith | 3ef3e89 | 2014-11-20 22:32:11 +0000 | [diff] [blame] | 6016 | D.isFunctionDeclaratorAFunctionDeclaration(); |
| 6017 | if (Delayed && Actions.isLibstdcxxEagerExceptionSpecHack(D) && |
| 6018 | GetLookAheadToken(0).is(tok::kw_noexcept) && |
| 6019 | GetLookAheadToken(1).is(tok::l_paren) && |
| 6020 | GetLookAheadToken(2).is(tok::kw_noexcept) && |
| 6021 | GetLookAheadToken(3).is(tok::l_paren) && |
| 6022 | GetLookAheadToken(4).is(tok::identifier) && |
| 6023 | GetLookAheadToken(4).getIdentifierInfo()->isStr("swap")) { |
| 6024 | // HACK: We've got an exception-specification |
| 6025 | // noexcept(noexcept(swap(...))) |
| 6026 | // or |
| 6027 | // noexcept(noexcept(swap(...)) && noexcept(swap(...))) |
| 6028 | // on a 'swap' member function. This is a libstdc++ bug; the lookup |
| 6029 | // for 'swap' will only find the function we're currently declaring, |
| 6030 | // whereas it expects to find a non-member swap through ADL. Turn off |
| 6031 | // delayed parsing to give it a chance to find what it expects. |
| 6032 | Delayed = false; |
| 6033 | } |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 6034 | ESpecType = tryParseExceptionSpecification(Delayed, |
| 6035 | ESpecRange, |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 6036 | DynamicExceptions, |
| 6037 | DynamicExceptionRanges, |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 6038 | NoexceptExpr, |
| 6039 | ExceptionSpecTokens); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6040 | if (ESpecType != EST_None) |
| 6041 | EndLoc = ESpecRange.getEnd(); |
| 6042 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6043 | // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes |
| 6044 | // after the exception-specification. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 6045 | MaybeParseCXX11Attributes(FnAttrs); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6046 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6047 | // Parse trailing-return-type[opt]. |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 6048 | LocalEndLoc = EndLoc; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6049 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) { |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 6050 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 6051 | if (D.getDeclSpec().getTypeSpecType() == TST_auto) |
| 6052 | StartLoc = D.getDeclSpec().getTypeSpecTypeLoc(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 6053 | LocalEndLoc = Tok.getLocation(); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 6054 | SourceRange Range; |
Richard Smith | 700537c | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 6055 | TrailingReturnType = ParseTrailingReturnType(Range); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 6056 | EndLoc = Range.getEnd(); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6057 | } |
| 6058 | } |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6059 | } |
| 6060 | |
Reid Kleckner | 078aea9 | 2016-12-09 17:14:05 +0000 | [diff] [blame] | 6061 | // Collect non-parameter declarations from the prototype if this is a function |
| 6062 | // declaration. They will be moved into the scope of the function. Only do |
| 6063 | // this in C and not C++, where the decls will continue to live in the |
| 6064 | // surrounding context. |
| 6065 | SmallVector<NamedDecl *, 0> DeclsInPrototype; |
| 6066 | if (getCurScope()->getFlags() & Scope::FunctionDeclarationScope && |
| 6067 | !getLangOpts().CPlusPlus) { |
| 6068 | for (Decl *D : getCurScope()->decls()) { |
| 6069 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 6070 | if (!ND || isa<ParmVarDecl>(ND)) |
| 6071 | continue; |
| 6072 | DeclsInPrototype.push_back(ND); |
| 6073 | } |
| 6074 | } |
| 6075 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6076 | // Remember that we parsed a function type, and remember the attributes. |
| 6077 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 6078 | IsAmbiguous, |
| 6079 | LParenLoc, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6080 | ParamInfo.data(), ParamInfo.size(), |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 6081 | EllipsisLoc, RParenLoc, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6082 | DS.getTypeQualifiers(), |
| 6083 | RefQualifierIsLValueRef, |
Douglas Gregor | e248eea | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 6084 | RefQualifierLoc, ConstQualifierLoc, |
| 6085 | VolatileQualifierLoc, |
Hal Finkel | 23a0739 | 2014-10-20 17:32:04 +0000 | [diff] [blame] | 6086 | RestrictQualifierLoc, |
Douglas Gregor | ad69e65 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 6087 | /*MutableLoc=*/SourceLocation(), |
Nathan Wilson | e23a9a4 | 2015-08-26 04:19:36 +0000 | [diff] [blame] | 6088 | ESpecType, ESpecRange, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6089 | DynamicExceptions.data(), |
| 6090 | DynamicExceptionRanges.data(), |
| 6091 | DynamicExceptions.size(), |
| 6092 | NoexceptExpr.isUsable() ? |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6093 | NoexceptExpr.get() : nullptr, |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 6094 | ExceptionSpecTokens, |
Reid Kleckner | 078aea9 | 2016-12-09 17:14:05 +0000 | [diff] [blame] | 6095 | DeclsInPrototype, |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 6096 | StartLoc, LocalEndLoc, D, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6097 | TrailingReturnType), |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6098 | FnAttrs, EndLoc); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6099 | } |
| 6100 | |
Ehsan Akhgari | c07d1e2 | 2015-03-25 00:53:33 +0000 | [diff] [blame] | 6101 | /// ParseRefQualifier - Parses a member function ref-qualifier. Returns |
| 6102 | /// true if a ref-qualifier is found. |
| 6103 | bool Parser::ParseRefQualifier(bool &RefQualifierIsLValueRef, |
| 6104 | SourceLocation &RefQualifierLoc) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 6105 | if (Tok.isOneOf(tok::amp, tok::ampamp)) { |
Ehsan Akhgari | c07d1e2 | 2015-03-25 00:53:33 +0000 | [diff] [blame] | 6106 | Diag(Tok, getLangOpts().CPlusPlus11 ? |
| 6107 | diag::warn_cxx98_compat_ref_qualifier : |
| 6108 | diag::ext_ref_qualifier); |
| 6109 | |
| 6110 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 6111 | RefQualifierLoc = ConsumeToken(); |
| 6112 | return true; |
| 6113 | } |
| 6114 | return false; |
| 6115 | } |
| 6116 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6117 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 6118 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 6119 | /// |
| 6120 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 6121 | /// abstract-declarators. |
| 6122 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6123 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6124 | && Tok.is(tok::identifier) |
| 6125 | && !TryAltiVecVectorToken() |
| 6126 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 6127 | // 6.7.5.3p11. |
| 6128 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 6129 | // Identifier lists follow a really simple grammar: the identifiers can |
| 6130 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 6131 | // identifier lists are really rare in the brave new modern world, and |
| 6132 | // it is very common for someone to typo a type in a non-K&R style |
| 6133 | // list. If we are presented with something like: "void foo(intptr x, |
| 6134 | // float y)", we don't want to start parsing the function declarator as |
| 6135 | // though it is a K&R style declarator just because intptr is an |
| 6136 | // invalid type. |
| 6137 | // |
| 6138 | // To handle this, we check to see if the token after the first |
| 6139 | // identifier is a "," or ")". Only then do we parse it as an |
| 6140 | // identifier list. |
Bruno Cardoso Lopes | 218c874 | 2016-09-13 20:04:35 +0000 | [diff] [blame] | 6141 | && (!Tok.is(tok::eof) && |
| 6142 | (NextToken().is(tok::comma) || NextToken().is(tok::r_paren))); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6143 | } |
| 6144 | |
| 6145 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 6146 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 6147 | /// |
| 6148 | /// After returning, ParamInfo will hold the parsed parameters. |
| 6149 | /// |
| 6150 | /// identifier-list: [C99 6.7.5] |
| 6151 | /// identifier |
| 6152 | /// identifier-list ',' identifier |
| 6153 | /// |
| 6154 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 6155 | Declarator &D, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6156 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6157 | // If there was no identifier specified for the declarator, either we are in |
| 6158 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 6159 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 6160 | // diagnose this. |
| 6161 | if (!D.getIdentifier()) |
| 6162 | Diag(Tok, diag::ext_ident_list_in_param); |
| 6163 | |
| 6164 | // Maintain an efficient lookup of params we have seen so far. |
| 6165 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 6166 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6167 | do { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6168 | // If this isn't an identifier, report the error and skip until ')'. |
| 6169 | if (Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 6170 | Diag(Tok, diag::err_expected) << tok::identifier; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 6171 | SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6172 | // Forget we parsed anything. |
| 6173 | ParamInfo.clear(); |
| 6174 | return; |
| 6175 | } |
| 6176 | |
| 6177 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 6178 | |
| 6179 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 6180 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 6181 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 6182 | |
| 6183 | // Verify that the argument identifier has not already been mentioned. |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 6184 | if (!ParamsSoFar.insert(ParmII).second) { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6185 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 6186 | } else { |
| 6187 | // Remember this identifier in ParamInfo. |
| 6188 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 6189 | Tok.getLocation(), |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6190 | nullptr)); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6191 | } |
| 6192 | |
| 6193 | // Eat the identifier. |
| 6194 | ConsumeToken(); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6195 | // The list continues if we see a comma. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6196 | } while (TryConsumeToken(tok::comma)); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6197 | } |
| 6198 | |
| 6199 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 6200 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 6201 | /// identifier list. |
| 6202 | /// |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 6203 | /// D is the declarator being parsed. If FirstArgAttrs is non-null, then the |
| 6204 | /// caller parsed those arguments immediately after the open paren - they should |
| 6205 | /// be considered to be part of the first parameter. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6206 | /// |
| 6207 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 6208 | /// be the location of the ellipsis, if any was parsed. |
| 6209 | /// |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 6210 | /// parameter-type-list: [C99 6.7.5] |
| 6211 | /// parameter-list |
| 6212 | /// parameter-list ',' '...' |
Douglas Gregor | 9bfc2e5 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 6213 | /// [C++] parameter-list '...' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 6214 | /// |
| 6215 | /// parameter-list: [C99 6.7.5] |
| 6216 | /// parameter-declaration |
| 6217 | /// parameter-list ',' parameter-declaration |
| 6218 | /// |
| 6219 | /// parameter-declaration: [C99 6.7.5] |
| 6220 | /// declaration-specifiers declarator |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 6221 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 6222 | /// [C++11] initializer-clause |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 6223 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 6224 | /// declaration-specifiers abstract-declarator[opt] |
| 6225 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 6226 | /// '=' assignment-expression |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 6227 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 6228 | /// [C++11] attribute-specifier-seq parameter-declaration |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 6229 | /// |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6230 | void Parser::ParseParameterDeclarationClause( |
| 6231 | Declarator &D, |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 6232 | ParsedAttributes &FirstArgAttrs, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6233 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 6234 | SourceLocation &EllipsisLoc) { |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6235 | do { |
| 6236 | // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq |
| 6237 | // before deciding this was a parameter-declaration-clause. |
| 6238 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6239 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6240 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6241 | // Parse the declaration-specifiers. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 6242 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 6243 | DeclSpec DS(AttrFactory); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6244 | |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 6245 | // Parse any C++11 attributes. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 6246 | MaybeParseCXX11Attributes(DS.getAttributes()); |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 6247 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 6248 | // Skip any Microsoft attributes before a param. |
Chad Rosier | f8a2e70 | 2012-12-20 20:37:53 +0000 | [diff] [blame] | 6249 | MaybeParseMicrosoftAttributes(DS.getAttributes()); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 6250 | |
| 6251 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 6252 | |
| 6253 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 6254 | // 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] | 6255 | // 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] | 6256 | // get rid of a parameter (FirstArgAttrs) and this statement. It might be |
| 6257 | // too much hassle. |
| 6258 | DS.takeAttributesFrom(FirstArgAttrs); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 6259 | |
Chris Lattner | de39c3e | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 6260 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6261 | |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6262 | |
| 6263 | // Parse the declarator. This is "PrototypeContext" or |
| 6264 | // "LambdaExprParameterContext", because we must accept either |
| 6265 | // 'declarator' or 'abstract-declarator' here. |
| 6266 | Declarator ParmDeclarator(DS, |
| 6267 | D.getContext() == Declarator::LambdaExprContext ? |
| 6268 | Declarator::LambdaExprParameterContext : |
| 6269 | Declarator::PrototypeContext); |
| 6270 | ParseDeclarator(ParmDeclarator); |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6271 | |
| 6272 | // Parse GNU attributes, if present. |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6273 | MaybeParseGNUAttributes(ParmDeclarator); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6274 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6275 | // Remember this parsed parameter in ParamInfo. |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6276 | IdentifierInfo *ParmII = ParmDeclarator.getIdentifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6277 | |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6278 | // DefArgToks is used when the parsing of default arguments needs |
| 6279 | // to be delayed. |
Malcolm Parsons | ff0382c | 2016-11-17 17:52:58 +0000 | [diff] [blame] | 6280 | std::unique_ptr<CachedTokens> DefArgToks; |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6281 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6282 | // If no parameter was specified, verify that *something* was specified, |
| 6283 | // otherwise we have a missing type and identifier. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6284 | if (DS.isEmpty() && ParmDeclarator.getIdentifier() == nullptr && |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6285 | ParmDeclarator.getNumTypeObjects() == 0) { |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6286 | // Completely missing, emit error. |
| 6287 | Diag(DSStart, diag::err_missing_param); |
| 6288 | } else { |
| 6289 | // Otherwise, we have something. Add it and let semantic analysis try |
| 6290 | // 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] | 6291 | |
Richard Smith | 36ee9fb | 2014-08-11 23:30:23 +0000 | [diff] [blame] | 6292 | // Last chance to recover from a misplaced ellipsis in an attempted |
| 6293 | // parameter pack declaration. |
| 6294 | if (Tok.is(tok::ellipsis) && |
| 6295 | (NextToken().isNot(tok::r_paren) || |
| 6296 | (!ParmDeclarator.getEllipsisLoc().isValid() && |
| 6297 | !Actions.isUnexpandedParameterPackPermitted())) && |
| 6298 | Actions.containsUnexpandedParameterPacks(ParmDeclarator)) |
| 6299 | DiagnoseMisplacedEllipsisInDeclarator(ConsumeToken(), ParmDeclarator); |
| 6300 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6301 | // Inform the actions module about the parameter declarator, so it gets |
| 6302 | // added to the current scope. |
Richard Smith | 95e1fb0 | 2014-08-27 03:23:12 +0000 | [diff] [blame] | 6303 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator); |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 6304 | // Parse the default argument, if any. We parse the default |
| 6305 | // arguments in all dialects; the semantic analysis in |
| 6306 | // ActOnParamDefaultArgument will reject the default argument in |
| 6307 | // C. |
| 6308 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 6309 | SourceLocation EqualLoc = Tok.getLocation(); |
| 6310 | |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 6311 | // Parse the default argument |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6312 | if (D.getContext() == Declarator::MemberContext) { |
| 6313 | // If we're inside a class definition, cache the tokens |
| 6314 | // corresponding to the default argument. We'll actually parse |
| 6315 | // them when we see the end of the class definition. |
Malcolm Parsons | ff0382c | 2016-11-17 17:52:58 +0000 | [diff] [blame] | 6316 | DefArgToks.reset(new CachedTokens); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6317 | |
David Majnemer | 906ed27 | 2015-01-13 05:28:24 +0000 | [diff] [blame] | 6318 | SourceLocation ArgStartLoc = NextToken().getLocation(); |
Richard Smith | 1fff95c | 2013-09-12 23:28:08 +0000 | [diff] [blame] | 6319 | if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) { |
Malcolm Parsons | ff0382c | 2016-11-17 17:52:58 +0000 | [diff] [blame] | 6320 | DefArgToks.reset(); |
Serge Pavlov | b4b3578 | 2014-07-22 01:54:49 +0000 | [diff] [blame] | 6321 | Actions.ActOnParamDefaultArgumentError(Param, EqualLoc); |
Argyrios Kyrtzidis | 249179c | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 6322 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6323 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
David Majnemer | 906ed27 | 2015-01-13 05:28:24 +0000 | [diff] [blame] | 6324 | ArgStartLoc); |
Argyrios Kyrtzidis | 249179c | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 6325 | } |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 6326 | } else { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6327 | // Consume the '='. |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 6328 | ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6329 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 6330 | // The argument isn't actually potentially evaluated unless it is |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 6331 | // used. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 6332 | EnterExpressionEvaluationContext Eval( |
| 6333 | Actions, |
| 6334 | Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed, |
| 6335 | Param); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 6336 | |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 6337 | ExprResult DefArgResult; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6338 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
Sebastian Redl | 1678d5f | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 6339 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 6340 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 1678d5f | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 6341 | } else |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 6342 | DefArgResult = ParseAssignmentExpression(); |
Kaelyn Takata | b16e632 | 2014-11-20 22:06:40 +0000 | [diff] [blame] | 6343 | DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6344 | if (DefArgResult.isInvalid()) { |
Serge Pavlov | b4b3578 | 2014-07-22 01:54:49 +0000 | [diff] [blame] | 6345 | Actions.ActOnParamDefaultArgumentError(Param, EqualLoc); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 6346 | SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6347 | } else { |
| 6348 | // Inform the actions module about the default argument |
| 6349 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6350 | DefArgResult.get()); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6351 | } |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 6352 | } |
| 6353 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6354 | |
| 6355 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 6356 | ParmDeclarator.getIdentifierLoc(), |
Malcolm Parsons | ff0382c | 2016-11-17 17:52:58 +0000 | [diff] [blame] | 6357 | Param, std::move(DefArgToks))); |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 6358 | } |
| 6359 | |
Richard Smith | 36ee9fb | 2014-08-11 23:30:23 +0000 | [diff] [blame] | 6360 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) { |
| 6361 | if (!getLangOpts().CPlusPlus) { |
| 6362 | // We have ellipsis without a preceding ',', which is ill-formed |
| 6363 | // in C. Complain and provide the fix. |
| 6364 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
| 6365 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
| 6366 | } else if (ParmDeclarator.getEllipsisLoc().isValid() || |
| 6367 | Actions.containsUnexpandedParameterPacks(ParmDeclarator)) { |
| 6368 | // It looks like this was supposed to be a parameter pack. Warn and |
| 6369 | // point out where the ellipsis should have gone. |
| 6370 | SourceLocation ParmEllipsis = ParmDeclarator.getEllipsisLoc(); |
| 6371 | Diag(EllipsisLoc, diag::warn_misplaced_ellipsis_vararg) |
| 6372 | << ParmEllipsis.isValid() << ParmEllipsis; |
| 6373 | if (ParmEllipsis.isValid()) { |
| 6374 | Diag(ParmEllipsis, |
| 6375 | diag::note_misplaced_ellipsis_vararg_existing_ellipsis); |
| 6376 | } else { |
| 6377 | Diag(ParmDeclarator.getIdentifierLoc(), |
| 6378 | diag::note_misplaced_ellipsis_vararg_add_ellipsis) |
| 6379 | << FixItHint::CreateInsertion(ParmDeclarator.getIdentifierLoc(), |
| 6380 | "...") |
| 6381 | << !ParmDeclarator.hasName(); |
| 6382 | } |
| 6383 | Diag(EllipsisLoc, diag::note_misplaced_ellipsis_vararg_add_comma) |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6384 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Richard Smith | 36ee9fb | 2014-08-11 23:30:23 +0000 | [diff] [blame] | 6385 | } |
| 6386 | |
| 6387 | // We can't have any more parameters after an ellipsis. |
Douglas Gregor | 9bfc2e5 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 6388 | break; |
| 6389 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6390 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6391 | // If the next token is a comma, consume it and keep reading arguments. |
| 6392 | } while (TryConsumeToken(tok::comma)); |
Chris Lattner | 6c940e6 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 6393 | } |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 6394 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6395 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 6396 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 6397 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 6398 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 6399 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6400 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 6401 | /// attribute-specifier-seq[opt] |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6402 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 6403 | if (CheckProhibitedCXX11Attribute()) |
| 6404 | return; |
| 6405 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6406 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 6407 | T.consumeOpen(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6408 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6409 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 6410 | // This code does a fast path to handle some of the most obvious cases. |
| 6411 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6412 | T.consumeClose(); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 6413 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 6414 | MaybeParseCXX11Attributes(attrs); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 6415 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6416 | // Remember that we parsed the empty array type. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6417 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, nullptr, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6418 | T.getOpenLocation(), |
| 6419 | T.getCloseLocation()), |
| 6420 | attrs, T.getCloseLocation()); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6421 | return; |
| 6422 | } else if (Tok.getKind() == tok::numeric_constant && |
| 6423 | GetLookAheadToken(1).is(tok::r_square)) { |
| 6424 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 6425 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6426 | ConsumeToken(); |
| 6427 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6428 | T.consumeClose(); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 6429 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 6430 | MaybeParseCXX11Attributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6431 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6432 | // Remember that we parsed a array type, and remember its features. |
Nikola Smiljanic | c531dcc | 2013-01-11 08:33:05 +0000 | [diff] [blame] | 6433 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6434 | ExprRes.get(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6435 | T.getOpenLocation(), |
| 6436 | T.getCloseLocation()), |
| 6437 | attrs, T.getCloseLocation()); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6438 | return; |
Benjamin Kramer | 72dae62 | 2016-02-18 15:30:24 +0000 | [diff] [blame] | 6439 | } else if (Tok.getKind() == tok::code_completion) { |
| 6440 | Actions.CodeCompleteBracketDeclarator(getCurScope()); |
| 6441 | return cutOffParsing(); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6442 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6443 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6444 | // If valid, this location is the position where we read the 'static' keyword. |
| 6445 | SourceLocation StaticLoc; |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6446 | TryConsumeToken(tok::kw_static, StaticLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6447 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6448 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 6449 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 6450 | DeclSpec DS(AttrFactory); |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 6451 | ParseTypeQualifierListOpt(DS, AR_CXX11AttributesParsed); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6452 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6453 | // If we haven't already read 'static', check to see if there is one after the |
| 6454 | // type-qualifier-list. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 6455 | if (!StaticLoc.isValid()) |
| 6456 | TryConsumeToken(tok::kw_static, StaticLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6457 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6458 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6459 | bool isStar = false; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6460 | ExprResult NumElements; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6461 | |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 6462 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 6463 | // 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] | 6464 | // the token after the star is a ']'. Since stars in arrays are |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 6465 | // infrequent, use of lookahead is not costly here. |
| 6466 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | c439f0d | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 6467 | ConsumeToken(); // Eat the '*'. |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 6468 | |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 6469 | if (StaticLoc.isValid()) { |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 6470 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 6471 | StaticLoc = SourceLocation(); // Drop the static. |
| 6472 | } |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 6473 | isStar = true; |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 6474 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6475 | // Note, in C89, this production uses the constant-expr production instead |
| 6476 | // of assignment-expr. The only difference is that assignment-expr allows |
| 6477 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 6478 | // 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] | 6479 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6480 | // Parse the constant-expression or assignment-expression now (depending |
| 6481 | // on dialect). |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6482 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6483 | NumElements = ParseConstantExpression(); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 6484 | } else { |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 6485 | EnterExpressionEvaluationContext Unevaluated( |
| 6486 | Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Kaelyn Takata | 1586782 | 2014-11-21 18:48:04 +0000 | [diff] [blame] | 6487 | NumElements = |
| 6488 | Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression()); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 6489 | } |
David Majnemer | f9834d5 | 2014-08-08 07:21:18 +0000 | [diff] [blame] | 6490 | } else { |
| 6491 | if (StaticLoc.isValid()) { |
| 6492 | Diag(StaticLoc, diag::err_unspecified_size_with_static); |
| 6493 | StaticLoc = SourceLocation(); // Drop the static. |
| 6494 | } |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 6495 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6496 | |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 6497 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 6498 | if (NumElements.isInvalid()) { |
Chris Lattner | cd2a8c5 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 6499 | D.setInvalidType(true); |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 6500 | // If the expression was invalid, skip it. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 6501 | SkipUntil(tok::r_square, StopAtSemi); |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 6502 | return; |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6503 | } |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 6504 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6505 | T.consumeClose(); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 6506 | |
Jordan Rose | 303e2f1 | 2016-11-10 23:28:17 +0000 | [diff] [blame] | 6507 | MaybeParseCXX11Attributes(DS.getAttributes()); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 6508 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 6509 | // Remember that we parsed a array type, and remember its features. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 6510 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 6511 | StaticLoc.isValid(), isStar, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6512 | NumElements.get(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6513 | T.getOpenLocation(), |
| 6514 | T.getCloseLocation()), |
Jordan Rose | 303e2f1 | 2016-11-10 23:28:17 +0000 | [diff] [blame] | 6515 | DS.getAttributes(), T.getCloseLocation()); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 6516 | } |
| 6517 | |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 6518 | /// Diagnose brackets before an identifier. |
| 6519 | void Parser::ParseMisplacedBracketDeclarator(Declarator &D) { |
| 6520 | assert(Tok.is(tok::l_square) && "Missing opening bracket"); |
| 6521 | assert(!D.mayOmitIdentifier() && "Declarator cannot omit identifier"); |
| 6522 | |
| 6523 | SourceLocation StartBracketLoc = Tok.getLocation(); |
| 6524 | Declarator TempDeclarator(D.getDeclSpec(), D.getContext()); |
| 6525 | |
| 6526 | while (Tok.is(tok::l_square)) { |
| 6527 | ParseBracketDeclarator(TempDeclarator); |
| 6528 | } |
| 6529 | |
| 6530 | // Stuff the location of the start of the brackets into the Declarator. |
| 6531 | // The diagnostics from ParseDirectDeclarator will make more sense if |
| 6532 | // they use this location instead. |
| 6533 | if (Tok.is(tok::semi)) |
| 6534 | D.getName().EndLocation = StartBracketLoc; |
| 6535 | |
| 6536 | SourceLocation SuggestParenLoc = Tok.getLocation(); |
| 6537 | |
| 6538 | // Now that the brackets are removed, try parsing the declarator again. |
| 6539 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
| 6540 | |
| 6541 | // Something went wrong parsing the brackets, in which case, |
| 6542 | // ParseBracketDeclarator has emitted an error, and we don't need to emit |
| 6543 | // one here. |
| 6544 | if (TempDeclarator.getNumTypeObjects() == 0) |
| 6545 | return; |
| 6546 | |
| 6547 | // Determine if parens will need to be suggested in the diagnostic. |
| 6548 | bool NeedParens = false; |
| 6549 | if (D.getNumTypeObjects() != 0) { |
| 6550 | switch (D.getTypeObject(D.getNumTypeObjects() - 1).Kind) { |
| 6551 | case DeclaratorChunk::Pointer: |
| 6552 | case DeclaratorChunk::Reference: |
| 6553 | case DeclaratorChunk::BlockPointer: |
| 6554 | case DeclaratorChunk::MemberPointer: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 6555 | case DeclaratorChunk::Pipe: |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 6556 | NeedParens = true; |
| 6557 | break; |
| 6558 | case DeclaratorChunk::Array: |
| 6559 | case DeclaratorChunk::Function: |
| 6560 | case DeclaratorChunk::Paren: |
| 6561 | break; |
| 6562 | } |
| 6563 | } |
| 6564 | |
| 6565 | if (NeedParens) { |
| 6566 | // Create a DeclaratorChunk for the inserted parens. |
| 6567 | ParsedAttributes attrs(AttrFactory); |
| 6568 | SourceLocation EndLoc = PP.getLocForEndOfToken(D.getLocEnd()); |
| 6569 | D.AddTypeInfo(DeclaratorChunk::getParen(SuggestParenLoc, EndLoc), attrs, |
| 6570 | SourceLocation()); |
| 6571 | } |
| 6572 | |
| 6573 | // Adding back the bracket info to the end of the Declarator. |
| 6574 | for (unsigned i = 0, e = TempDeclarator.getNumTypeObjects(); i < e; ++i) { |
| 6575 | const DeclaratorChunk &Chunk = TempDeclarator.getTypeObject(i); |
| 6576 | ParsedAttributes attrs(AttrFactory); |
| 6577 | attrs.set(Chunk.Common.AttrList); |
| 6578 | D.AddTypeInfo(Chunk, attrs, SourceLocation()); |
| 6579 | } |
| 6580 | |
| 6581 | // The missing identifier would have been diagnosed in ParseDirectDeclarator. |
| 6582 | // If parentheses are required, always suggest them. |
| 6583 | if (!D.getIdentifier() && !NeedParens) |
| 6584 | return; |
| 6585 | |
| 6586 | SourceLocation EndBracketLoc = TempDeclarator.getLocEnd(); |
| 6587 | |
| 6588 | // Generate the move bracket error message. |
| 6589 | SourceRange BracketRange(StartBracketLoc, EndBracketLoc); |
| 6590 | SourceLocation EndLoc = PP.getLocForEndOfToken(D.getLocEnd()); |
| 6591 | |
| 6592 | if (NeedParens) { |
| 6593 | Diag(EndLoc, diag::err_brackets_go_after_unqualified_id) |
| 6594 | << getLangOpts().CPlusPlus |
| 6595 | << FixItHint::CreateInsertion(SuggestParenLoc, "(") |
| 6596 | << FixItHint::CreateInsertion(EndLoc, ")") |
| 6597 | << FixItHint::CreateInsertionFromRange( |
| 6598 | EndLoc, CharSourceRange(BracketRange, true)) |
| 6599 | << FixItHint::CreateRemoval(BracketRange); |
| 6600 | } else { |
| 6601 | Diag(EndLoc, diag::err_brackets_go_after_unqualified_id) |
| 6602 | << getLangOpts().CPlusPlus |
| 6603 | << FixItHint::CreateInsertionFromRange( |
| 6604 | EndLoc, CharSourceRange(BracketRange, true)) |
| 6605 | << FixItHint::CreateRemoval(BracketRange); |
| 6606 | } |
| 6607 | } |
| 6608 | |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 6609 | /// [GNU] typeof-specifier: |
| 6610 | /// typeof ( expressions ) |
| 6611 | /// typeof ( type-name ) |
| 6612 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 6613 | /// |
| 6614 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 6615 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6616 | Token OpTok = Tok; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 6617 | SourceLocation StartLoc = ConsumeToken(); |
| 6618 | |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 6619 | const bool hasParens = Tok.is(tok::l_paren); |
| 6620 | |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 6621 | EnterExpressionEvaluationContext Unevaluated( |
| 6622 | Actions, Sema::ExpressionEvaluationContext::Unevaluated, |
| 6623 | Sema::ReuseLambdaContextDecl); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 6624 | |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6625 | bool isCastExpr; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6626 | ParsedType CastTy; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6627 | SourceRange CastRange; |
Kaelyn Takata | 91126074 | 2014-12-19 01:28:40 +0000 | [diff] [blame] | 6628 | ExprResult Operand = Actions.CorrectDelayedTyposInExpr( |
| 6629 | ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, CastTy, CastRange)); |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 6630 | if (hasParens) |
| 6631 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6632 | |
| 6633 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 6634 | // FIXME: Not accurate, the range gets one token more than it should. |
| 6635 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6636 | else |
| 6637 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6638 | |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6639 | if (isCastExpr) { |
| 6640 | if (!CastTy) { |
| 6641 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 6642 | return; |
Douglas Gregor | 220cac5 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 6643 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 6644 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6645 | const char *PrevSpec = nullptr; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 6646 | unsigned DiagID; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6647 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 6648 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6649 | DiagID, CastTy, |
| 6650 | Actions.getASTContext().getPrintingPolicy())) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 6651 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 6652 | return; |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 6653 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 6654 | |
Hiroshi Inoue | 533777f | 2017-07-02 06:12:49 +0000 | [diff] [blame] | 6655 | // If we get here, the operand to the typeof was an expression. |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 6656 | if (Operand.isInvalid()) { |
| 6657 | DS.SetTypeSpecError(); |
Steve Naroff | 4bd2f71 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 6658 | return; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 6659 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 6660 | |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 6661 | // We might need to transform the operand if it is potentially evaluated. |
| 6662 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 6663 | if (Operand.isInvalid()) { |
| 6664 | DS.SetTypeSpecError(); |
| 6665 | return; |
| 6666 | } |
| 6667 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6668 | const char *PrevSpec = nullptr; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 6669 | unsigned DiagID; |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 6670 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 6671 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6672 | DiagID, Operand.get(), |
| 6673 | Actions.getASTContext().getPrintingPolicy())) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 6674 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 6675 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6676 | |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 6677 | /// [C11] atomic-specifier: |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6678 | /// _Atomic ( type-name ) |
| 6679 | /// |
| 6680 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 6681 | assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) && |
| 6682 | "Not an atomic specifier"); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6683 | |
| 6684 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6685 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 6686 | if (T.consumeOpen()) |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6687 | return; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6688 | |
| 6689 | TypeResult Result = ParseTypeName(); |
| 6690 | if (Result.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 6691 | SkipUntil(tok::r_paren, StopAtSemi); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6692 | return; |
| 6693 | } |
| 6694 | |
| 6695 | // Match the ')' |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6696 | T.consumeClose(); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6697 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6698 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6699 | return; |
| 6700 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 6701 | DS.setTypeofParensRange(T.getRange()); |
| 6702 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6703 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 6704 | const char *PrevSpec = nullptr; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6705 | unsigned DiagID; |
| 6706 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6707 | DiagID, Result.get(), |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6708 | Actions.getASTContext().getPrintingPolicy())) |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6709 | Diag(StartLoc, DiagID) << PrevSpec; |
| 6710 | } |
| 6711 | |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6712 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 6713 | /// from TryAltiVecVectorToken. |
| 6714 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 6715 | Token Next = NextToken(); |
| 6716 | switch (Next.getKind()) { |
| 6717 | default: return false; |
| 6718 | case tok::kw_short: |
| 6719 | case tok::kw_long: |
| 6720 | case tok::kw_signed: |
| 6721 | case tok::kw_unsigned: |
| 6722 | case tok::kw_void: |
| 6723 | case tok::kw_char: |
| 6724 | case tok::kw_int: |
| 6725 | case tok::kw_float: |
| 6726 | case tok::kw_double: |
| 6727 | case tok::kw_bool: |
Bill Seurer | cf2c96b | 2015-01-12 19:35:51 +0000 | [diff] [blame] | 6728 | case tok::kw___bool: |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6729 | case tok::kw___pixel: |
| 6730 | Tok.setKind(tok::kw___vector); |
| 6731 | return true; |
| 6732 | case tok::identifier: |
| 6733 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 6734 | Tok.setKind(tok::kw___vector); |
| 6735 | return true; |
| 6736 | } |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 6737 | if (Next.getIdentifierInfo() == Ident_bool) { |
| 6738 | Tok.setKind(tok::kw___vector); |
| 6739 | return true; |
| 6740 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6741 | return false; |
| 6742 | } |
| 6743 | } |
| 6744 | |
| 6745 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 6746 | const char *&PrevSpec, unsigned &DiagID, |
| 6747 | bool &isInvalid) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6748 | const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy(); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6749 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 6750 | Token Next = NextToken(); |
| 6751 | switch (Next.getKind()) { |
| 6752 | case tok::kw_short: |
| 6753 | case tok::kw_long: |
| 6754 | case tok::kw_signed: |
| 6755 | case tok::kw_unsigned: |
| 6756 | case tok::kw_void: |
| 6757 | case tok::kw_char: |
| 6758 | case tok::kw_int: |
| 6759 | case tok::kw_float: |
| 6760 | case tok::kw_double: |
| 6761 | case tok::kw_bool: |
Bill Seurer | cf2c96b | 2015-01-12 19:35:51 +0000 | [diff] [blame] | 6762 | case tok::kw___bool: |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6763 | case tok::kw___pixel: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6764 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6765 | return true; |
| 6766 | case tok::identifier: |
| 6767 | if (Next.getIdentifierInfo() == Ident_pixel) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6768 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6769 | return true; |
| 6770 | } |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 6771 | if (Next.getIdentifierInfo() == Ident_bool) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6772 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy); |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 6773 | return true; |
| 6774 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6775 | break; |
| 6776 | default: |
| 6777 | break; |
| 6778 | } |
Douglas Gregor | 9938e3b | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 6779 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6780 | DS.isTypeAltiVecVector()) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6781 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6782 | return true; |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 6783 | } else if ((Tok.getIdentifierInfo() == Ident_bool) && |
| 6784 | DS.isTypeAltiVecVector()) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 6785 | isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy); |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 6786 | return true; |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 6787 | } |
| 6788 | return false; |
| 6789 | } |