Chris Lattner | 7ad0fbe | 2006-11-05 07:46:30 +0000 | [diff] [blame] | 1 | //===--- ParseDecl.cpp - Declaration Parsing ------------------------------===// |
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" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "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" |
Chris Lattner | ad9ac94 | 2007-01-23 01:14:52 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallSet.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallString.h" |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/StringSwitch.h" |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | // C99 6.7: Declarations. |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 36 | /// ParseTypeName |
| 37 | /// type-name: [C99 6.7.6] |
| 38 | /// specifier-qualifier-list abstract-declarator[opt] |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 39 | /// |
| 40 | /// Called type-id in C++. |
Douglas Gregor | 205d5e3 | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 41 | TypeResult Parser::ParseTypeName(SourceRange *Range, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 42 | Declarator::TheContext Context, |
Richard Smith | cd1c055 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 43 | AccessSpecifier AS, |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 44 | Decl **OwnedType, |
| 45 | ParsedAttributes *Attrs) { |
Richard Smith | 62dad82 | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 46 | DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context); |
Richard Smith | 2f07ad5 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 47 | if (DSC == DSC_normal) |
| 48 | DSC = DSC_type_specifier; |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 49 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 50 | // Parse the common declaration-specifiers piece. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 51 | DeclSpec DS(AttrFactory); |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 52 | if (Attrs) |
| 53 | DS.addAttributes(Attrs->getList()); |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 54 | ParseSpecifierQualifierList(DS, AS, DSC); |
Richard Smith | cd1c055 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 55 | if (OwnedType) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 56 | *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : nullptr; |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 57 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 58 | // Parse the abstract-declarator, if present. |
Douglas Gregor | 205d5e3 | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 59 | Declarator DeclaratorInfo(DS, Context); |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 60 | ParseDeclarator(DeclaratorInfo); |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 61 | if (Range) |
| 62 | *Range = DeclaratorInfo.getSourceRange(); |
| 63 | |
Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 64 | if (DeclaratorInfo.isInvalidType()) |
Douglas Gregor | 220cac5 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 65 | return true; |
| 66 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 67 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 70 | |
| 71 | /// isAttributeLateParsed - Return true if the attribute has arguments that |
| 72 | /// require late parsing. |
| 73 | static bool isAttributeLateParsed(const IdentifierInfo &II) { |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 74 | #define CLANG_ATTR_LATE_PARSED_LIST |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 75 | return llvm::StringSwitch<bool>(II.getName()) |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 76 | #include "clang/Parse/AttrParserStringSwitches.inc" |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 77 | .Default(false); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 78 | #undef CLANG_ATTR_LATE_PARSED_LIST |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 81 | /// ParseGNUAttributes - Parse a non-empty attributes list. |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 82 | /// |
| 83 | /// [GNU] attributes: |
| 84 | /// attribute |
| 85 | /// attributes attribute |
| 86 | /// |
| 87 | /// [GNU] attribute: |
| 88 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 89 | /// |
| 90 | /// [GNU] attribute-list: |
| 91 | /// attrib |
| 92 | /// attribute_list ',' attrib |
| 93 | /// |
| 94 | /// [GNU] attrib: |
| 95 | /// empty |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 96 | /// attrib-name |
| 97 | /// attrib-name '(' identifier ')' |
| 98 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 99 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 100 | /// |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 101 | /// [GNU] attrib-name: |
| 102 | /// identifier |
| 103 | /// typespec |
| 104 | /// typequal |
| 105 | /// storageclass |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | /// |
Richard Smith | f7ca0c0 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 107 | /// Whether an attribute takes an 'identifier' is determined by the |
| 108 | /// attrib-name. GCC's behavior here is not worth imitating: |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 109 | /// |
Richard Smith | f7ca0c0 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 110 | /// * In C mode, if the attribute argument list starts with an identifier |
| 111 | /// followed by a ',' or an ')', and the identifier doesn't resolve to |
| 112 | /// a type, it is parsed as an identifier. If the attribute actually |
| 113 | /// wanted an expression, it's out of luck (but it turns out that no |
| 114 | /// attributes work that way, because C constant expressions are very |
| 115 | /// limited). |
| 116 | /// * In C++ mode, if the attribute argument list starts with an identifier, |
| 117 | /// and the attribute *wants* an identifier, it is parsed as an identifier. |
| 118 | /// At block scope, any additional tokens between the identifier and the |
| 119 | /// ',' or ')' are ignored, otherwise they produce a parse error. |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 120 | /// |
Richard Smith | f7ca0c0 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 121 | /// 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] | 122 | void Parser::ParseGNUAttributes(ParsedAttributes &attrs, |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 123 | SourceLocation *endLoc, |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 124 | LateParsedAttrList *LateAttrs, |
| 125 | Declarator *D) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 126 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 128 | while (Tok.is(tok::kw___attribute)) { |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 129 | ConsumeToken(); |
| 130 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 131 | "attribute")) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 132 | SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ; |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 133 | return; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 134 | } |
| 135 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 136 | SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ; |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 137 | return; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 138 | } |
| 139 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 140 | while (true) { |
| 141 | // Allow empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 142 | if (TryConsumeToken(tok::comma)) |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 143 | continue; |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 144 | |
| 145 | // Expect an identifier or declaration specifier (const, int, etc.) |
| 146 | if (Tok.isNot(tok::identifier) && !isDeclarationSpecifier()) |
| 147 | break; |
| 148 | |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 149 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 150 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 152 | if (Tok.isNot(tok::l_paren)) { |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 153 | attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 154 | AttributeList::AS_GNU); |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 155 | continue; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 156 | } |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 157 | |
| 158 | // Handle "parameterized" attributes |
| 159 | if (!LateAttrs || !isAttributeLateParsed(*AttrName)) { |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 160 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc, nullptr, |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 161 | SourceLocation(), AttributeList::AS_GNU, D); |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 162 | continue; |
| 163 | } |
| 164 | |
| 165 | // Handle attributes with arguments that require late parsing. |
| 166 | LateParsedAttribute *LA = |
| 167 | new LateParsedAttribute(this, *AttrName, AttrNameLoc); |
| 168 | LateAttrs->push_back(LA); |
| 169 | |
| 170 | // Attributes in a class are parsed at the end of the class, along |
| 171 | // with other late-parsed declarations. |
| 172 | if (!ClassStack.empty() && !LateAttrs->parseSoon()) |
| 173 | getCurrentClass().LateParsedDeclarations.push_back(LA); |
| 174 | |
| 175 | // consume everything up to and including the matching right parens |
| 176 | ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false); |
| 177 | |
| 178 | Token Eof; |
| 179 | Eof.startToken(); |
| 180 | Eof.setLocation(Tok.getLocation()); |
| 181 | LA->Toks.push_back(Eof); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 182 | } |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 183 | |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 184 | if (ExpectAndConsume(tok::r_paren)) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 185 | SkipUntil(tok::r_paren, StopAtSemi); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 186 | SourceLocation Loc = Tok.getLocation(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 187 | if (ExpectAndConsume(tok::r_paren)) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 188 | SkipUntil(tok::r_paren, StopAtSemi); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 189 | if (endLoc) |
| 190 | *endLoc = Loc; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 191 | } |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 192 | } |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 193 | |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 194 | /// \brief Normalizes an attribute name by dropping prefixed and suffixed __. |
| 195 | static StringRef normalizeAttrName(StringRef Name) { |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 196 | if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__")) |
| 197 | Name = Name.drop_front(2).drop_back(2); |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 198 | return Name; |
| 199 | } |
| 200 | |
| 201 | /// \brief Determine whether the given attribute has an identifier argument. |
| 202 | static bool attributeHasIdentifierArg(const IdentifierInfo &II) { |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 203 | #define CLANG_ATTR_IDENTIFIER_ARG_LIST |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 204 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 205 | #include "clang/Parse/AttrParserStringSwitches.inc" |
Douglas Gregor | d2472d4 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 206 | .Default(false); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 207 | #undef CLANG_ATTR_IDENTIFIER_ARG_LIST |
Douglas Gregor | d2472d4 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 208 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 209 | |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 210 | /// \brief Determine whether the given attribute parses a type argument. |
| 211 | static bool attributeIsTypeArgAttr(const IdentifierInfo &II) { |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 212 | #define CLANG_ATTR_TYPE_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" |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 215 | .Default(false); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 216 | #undef CLANG_ATTR_TYPE_ARG_LIST |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Aaron Ballman | 15b27b9 | 2014-01-09 19:39:35 +0000 | [diff] [blame] | 219 | /// \brief Determine whether the given attribute requires parsing its arguments |
| 220 | /// in an unevaluated context or not. |
| 221 | static bool attributeParsedArgsUnevaluated(const IdentifierInfo &II) { |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 222 | #define CLANG_ATTR_ARG_CONTEXT_LIST |
Aaron Ballman | 15b27b9 | 2014-01-09 19:39:35 +0000 | [diff] [blame] | 223 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 224 | #include "clang/Parse/AttrParserStringSwitches.inc" |
Aaron Ballman | 15b27b9 | 2014-01-09 19:39:35 +0000 | [diff] [blame] | 225 | .Default(false); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 226 | #undef CLANG_ATTR_ARG_CONTEXT_LIST |
Aaron Ballman | 15b27b9 | 2014-01-09 19:39:35 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 229 | IdentifierLoc *Parser::ParseIdentifierLoc() { |
| 230 | assert(Tok.is(tok::identifier) && "expected an identifier"); |
| 231 | IdentifierLoc *IL = IdentifierLoc::create(Actions.Context, |
| 232 | Tok.getLocation(), |
| 233 | Tok.getIdentifierInfo()); |
| 234 | ConsumeToken(); |
| 235 | return IL; |
| 236 | } |
| 237 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 238 | void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName, |
| 239 | SourceLocation AttrNameLoc, |
| 240 | ParsedAttributes &Attrs, |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 241 | SourceLocation *EndLoc, |
| 242 | IdentifierInfo *ScopeName, |
| 243 | SourceLocation ScopeLoc, |
| 244 | AttributeList::Syntax Syntax) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 245 | BalancedDelimiterTracker Parens(*this, tok::l_paren); |
| 246 | Parens.consumeOpen(); |
| 247 | |
| 248 | TypeResult T; |
| 249 | if (Tok.isNot(tok::r_paren)) |
| 250 | T = ParseTypeName(); |
| 251 | |
| 252 | if (Parens.consumeClose()) |
| 253 | return; |
| 254 | |
| 255 | if (T.isInvalid()) |
| 256 | return; |
| 257 | |
| 258 | if (T.isUsable()) |
| 259 | Attrs.addNewTypeAttr(&AttrName, |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 260 | SourceRange(AttrNameLoc, Parens.getCloseLocation()), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 261 | ScopeName, ScopeLoc, T.get(), Syntax); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 262 | else |
| 263 | Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 264 | ScopeName, ScopeLoc, nullptr, 0, Syntax); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Aaron Ballman | 35f9421 | 2014-04-14 16:03:22 +0000 | [diff] [blame] | 267 | unsigned Parser::ParseAttributeArgsCommon( |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 268 | IdentifierInfo *AttrName, SourceLocation AttrNameLoc, |
| 269 | ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName, |
| 270 | SourceLocation ScopeLoc, AttributeList::Syntax Syntax) { |
| 271 | // Ignore the left paren location for now. |
| 272 | ConsumeParen(); |
| 273 | |
| 274 | ArgsVector ArgExprs; |
| 275 | if (Tok.is(tok::identifier)) { |
| 276 | // If this attribute wants an 'identifier' argument, make it so. |
| 277 | bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName); |
| 278 | AttributeList::Kind AttrKind = |
| 279 | AttributeList::getKind(AttrName, ScopeName, Syntax); |
| 280 | |
| 281 | // If we don't know how to parse this attribute, but this is the only |
| 282 | // token in this argument, assume it's meant to be an identifier. |
| 283 | if (AttrKind == AttributeList::UnknownAttribute || |
| 284 | AttrKind == AttributeList::IgnoredAttribute) { |
| 285 | const Token &Next = NextToken(); |
| 286 | IsIdentifierArg = Next.is(tok::r_paren) || Next.is(tok::comma); |
| 287 | } |
| 288 | |
| 289 | if (IsIdentifierArg) |
| 290 | ArgExprs.push_back(ParseIdentifierLoc()); |
| 291 | } |
| 292 | |
| 293 | if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) { |
| 294 | // Eat the comma. |
| 295 | if (!ArgExprs.empty()) |
| 296 | ConsumeToken(); |
| 297 | |
| 298 | // Parse the non-empty comma-separated list of expressions. |
| 299 | do { |
| 300 | std::unique_ptr<EnterExpressionEvaluationContext> Unevaluated; |
| 301 | if (attributeParsedArgsUnevaluated(*AttrName)) |
| 302 | Unevaluated.reset( |
| 303 | new EnterExpressionEvaluationContext(Actions, Sema::Unevaluated)); |
| 304 | |
| 305 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 306 | if (ArgExpr.isInvalid()) { |
| 307 | SkipUntil(tok::r_paren, StopAtSemi); |
Aaron Ballman | 35f9421 | 2014-04-14 16:03:22 +0000 | [diff] [blame] | 308 | return 0; |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 309 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 310 | ArgExprs.push_back(ArgExpr.get()); |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 311 | // Eat the comma, move to the next argument |
| 312 | } while (TryConsumeToken(tok::comma)); |
| 313 | } |
| 314 | |
| 315 | SourceLocation RParen = Tok.getLocation(); |
| 316 | if (!ExpectAndConsume(tok::r_paren)) { |
| 317 | SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc; |
| 318 | Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc, |
| 319 | ArgExprs.data(), ArgExprs.size(), Syntax); |
| 320 | } |
| 321 | |
| 322 | if (EndLoc) |
| 323 | *EndLoc = RParen; |
Aaron Ballman | 35f9421 | 2014-04-14 16:03:22 +0000 | [diff] [blame] | 324 | |
| 325 | return static_cast<unsigned>(ArgExprs.size()); |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 328 | /// Parse the arguments to a parameterized GNU attribute or |
| 329 | /// a C++11 attribute in "gnu" namespace. |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 330 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 331 | SourceLocation AttrNameLoc, |
| 332 | ParsedAttributes &Attrs, |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 333 | SourceLocation *EndLoc, |
| 334 | IdentifierInfo *ScopeName, |
| 335 | SourceLocation ScopeLoc, |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 336 | AttributeList::Syntax Syntax, |
| 337 | Declarator *D) { |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 338 | |
| 339 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 340 | |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 341 | AttributeList::Kind AttrKind = |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 342 | AttributeList::getKind(AttrName, ScopeName, Syntax); |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 343 | |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 344 | if (AttrKind == AttributeList::AT_Availability) { |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 345 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName, |
| 346 | ScopeLoc, Syntax); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 347 | return; |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 348 | } else if (AttrKind == AttributeList::AT_ObjCBridgeRelated) { |
| 349 | ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, |
| 350 | ScopeName, ScopeLoc, Syntax); |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 351 | return; |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 352 | } else if (AttrKind == AttributeList::AT_TypeTagForDatatype) { |
| 353 | ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, |
| 354 | ScopeName, ScopeLoc, Syntax); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 355 | return; |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 356 | } else if (attributeIsTypeArgAttr(*AttrName)) { |
| 357 | ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName, |
| 358 | ScopeLoc, Syntax); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 359 | return; |
| 360 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 361 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 362 | // These may refer to the function arguments, but need to be parsed early to |
| 363 | // participate in determining whether it's a redeclaration. |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 364 | std::unique_ptr<ParseScope> PrototypeScope; |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 365 | if (AttrName->isStr("enable_if") && D && D->isFunctionDeclarator()) { |
| 366 | DeclaratorChunk::FunctionTypeInfo FTI = D->getFunctionTypeInfo(); |
| 367 | PrototypeScope.reset(new ParseScope(this, Scope::FunctionPrototypeScope | |
| 368 | Scope::FunctionDeclarationScope | |
| 369 | Scope::DeclScope)); |
Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 370 | for (unsigned i = 0; i != FTI.NumParams; ++i) { |
| 371 | ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 372 | Actions.ActOnReenterCXXMethodParameter(getCurScope(), Param); |
| 373 | } |
| 374 | } |
| 375 | |
Aaron Ballman | b8e2039 | 2014-03-31 17:32:39 +0000 | [diff] [blame] | 376 | ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName, |
| 377 | ScopeLoc, Syntax); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 380 | bool Parser::ParseMicrosoftDeclSpecArgs(IdentifierInfo *AttrName, |
| 381 | SourceLocation AttrNameLoc, |
| 382 | ParsedAttributes &Attrs) { |
| 383 | // If the attribute isn't known, we will not attempt to parse any |
| 384 | // arguments. |
| 385 | if (!hasAttribute(AttrSyntax::Declspec, nullptr, AttrName, |
| 386 | getTargetInfo().getTriple(), getLangOpts())) { |
| 387 | // Eat the left paren, then skip to the ending right paren. |
| 388 | ConsumeParen(); |
| 389 | SkipUntil(tok::r_paren); |
| 390 | return false; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 391 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 392 | |
Aaron Ballman | 95d5703 | 2014-04-14 16:44:26 +0000 | [diff] [blame] | 393 | SourceLocation OpenParenLoc = Tok.getLocation(); |
| 394 | |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 395 | if (AttrName->getName() == "property") { |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 396 | // 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] | 397 | // assignment expressions as a parameter, but the lhs of the assignment |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 398 | // must be named get or put. |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 399 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 400 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 401 | T.expectAndConsume(diag::err_expected_lparen_after, |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 402 | AttrName->getNameStart(), tok::r_paren); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 403 | |
| 404 | enum AccessorKind { |
| 405 | AK_Invalid = -1, |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 406 | AK_Put = 0, |
| 407 | AK_Get = 1 // indices into AccessorNames |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 408 | }; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 409 | IdentifierInfo *AccessorNames[] = {nullptr, nullptr}; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 410 | bool HasInvalidAccessor = false; |
| 411 | |
| 412 | // Parse the accessor specifications. |
| 413 | while (true) { |
| 414 | // Stop if this doesn't look like an accessor spec. |
| 415 | if (!Tok.is(tok::identifier)) { |
| 416 | // If the user wrote a completely empty list, use a special diagnostic. |
| 417 | if (Tok.is(tok::r_paren) && !HasInvalidAccessor && |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 418 | AccessorNames[AK_Put] == nullptr && |
| 419 | AccessorNames[AK_Get] == nullptr) { |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 420 | Diag(AttrNameLoc, diag::err_ms_property_no_getter_or_putter); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 421 | break; |
| 422 | } |
| 423 | |
| 424 | Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor); |
| 425 | break; |
| 426 | } |
| 427 | |
| 428 | AccessorKind Kind; |
| 429 | SourceLocation KindLoc = Tok.getLocation(); |
| 430 | StringRef KindStr = Tok.getIdentifierInfo()->getName(); |
| 431 | if (KindStr == "get") { |
| 432 | Kind = AK_Get; |
| 433 | } else if (KindStr == "put") { |
| 434 | Kind = AK_Put; |
| 435 | |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 436 | // Recover from the common mistake of using 'set' instead of 'put'. |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 437 | } else if (KindStr == "set") { |
| 438 | Diag(KindLoc, diag::err_ms_property_has_set_accessor) |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 439 | << FixItHint::CreateReplacement(KindLoc, "put"); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 440 | Kind = AK_Put; |
| 441 | |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 442 | // Handle the mistake of forgetting the accessor kind by skipping |
| 443 | // this accessor. |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 444 | } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) { |
| 445 | Diag(KindLoc, diag::err_ms_property_missing_accessor_kind); |
| 446 | ConsumeToken(); |
| 447 | HasInvalidAccessor = true; |
| 448 | goto next_property_accessor; |
| 449 | |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 450 | // Otherwise, complain about the unknown accessor kind. |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 451 | } else { |
| 452 | Diag(KindLoc, diag::err_ms_property_unknown_accessor); |
| 453 | HasInvalidAccessor = true; |
| 454 | Kind = AK_Invalid; |
| 455 | |
| 456 | // 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] | 457 | if (!NextToken().is(tok::equal)) |
| 458 | break; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | // Consume the identifier. |
| 462 | ConsumeToken(); |
| 463 | |
| 464 | // Consume the '='. |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 465 | if (!TryConsumeToken(tok::equal)) { |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 466 | Diag(Tok.getLocation(), diag::err_ms_property_expected_equal) |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 467 | << KindStr; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 468 | break; |
| 469 | } |
| 470 | |
| 471 | // Expect the method name. |
| 472 | if (!Tok.is(tok::identifier)) { |
| 473 | Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name); |
| 474 | break; |
| 475 | } |
| 476 | |
| 477 | if (Kind == AK_Invalid) { |
| 478 | // Just drop invalid accessors. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 479 | } else if (AccessorNames[Kind] != nullptr) { |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 480 | // Complain about the repeated accessor, ignore it, and keep parsing. |
| 481 | Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr; |
| 482 | } else { |
| 483 | AccessorNames[Kind] = Tok.getIdentifierInfo(); |
| 484 | } |
| 485 | ConsumeToken(); |
| 486 | |
| 487 | next_property_accessor: |
| 488 | // Keep processing accessors until we run out. |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 489 | if (TryConsumeToken(tok::comma)) |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 490 | continue; |
| 491 | |
| 492 | // If we run into the ')', stop without consuming it. |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 493 | if (Tok.is(tok::r_paren)) |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 494 | break; |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 495 | |
| 496 | Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen); |
| 497 | break; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | // Only add the property attribute if it was well-formed. |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 501 | if (!HasInvalidAccessor) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 502 | Attrs.addNewPropertyAttr(AttrName, AttrNameLoc, nullptr, SourceLocation(), |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 503 | AccessorNames[AK_Get], AccessorNames[AK_Put], |
| 504 | AttributeList::AS_Declspec); |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 505 | T.skipToEnd(); |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 506 | return !HasInvalidAccessor; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 507 | } |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 508 | |
Aaron Ballman | 95d5703 | 2014-04-14 16:44:26 +0000 | [diff] [blame] | 509 | unsigned NumArgs = |
| 510 | ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, nullptr, nullptr, |
| 511 | SourceLocation(), AttributeList::AS_Declspec); |
| 512 | |
| 513 | // If this attribute's args were parsed, and it was expected to have |
| 514 | // arguments but none were provided, emit a diagnostic. |
| 515 | const AttributeList *Attr = Attrs.getList(); |
| 516 | if (Attr && Attr->getMaxArgs() && !NumArgs) { |
Aaron Ballman | ef5d94c | 2014-04-15 00:36:39 +0000 | [diff] [blame] | 517 | Diag(OpenParenLoc, diag::err_attribute_requires_arguments) << AttrName; |
Aaron Ballman | 95d5703 | 2014-04-14 16:44:26 +0000 | [diff] [blame] | 518 | return false; |
| 519 | } |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 520 | return true; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Eli Friedman | 06de2b5 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 523 | /// [MS] decl-specifier: |
| 524 | /// __declspec ( extended-decl-modifier-seq ) |
| 525 | /// |
| 526 | /// [MS] extended-decl-modifier-seq: |
| 527 | /// extended-decl-modifier[opt] |
| 528 | /// extended-decl-modifier extended-decl-modifier-seq |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 529 | void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) { |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 530 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | 06de2b5 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 531 | |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 532 | ConsumeToken(); |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 533 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 534 | if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec", |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 535 | tok::r_paren)) |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 536 | return; |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 537 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 538 | // An empty declspec is perfectly legal and should not warn. Additionally, |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 539 | // you can specify multiple attributes per declspec. |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 540 | while (Tok.isNot(tok::r_paren)) { |
| 541 | // Attribute not present. |
| 542 | if (TryConsumeToken(tok::comma)) |
| 543 | continue; |
| 544 | |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 545 | // We expect either a well-known identifier or a generic string. Anything |
| 546 | // else is a malformed declspec. |
| 547 | bool IsString = Tok.getKind() == tok::string_literal ? true : false; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 548 | if (!IsString && Tok.getKind() != tok::identifier && |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 549 | Tok.getKind() != tok::kw_restrict) { |
| 550 | Diag(Tok, diag::err_ms_declspec_type); |
| 551 | T.skipToEnd(); |
| 552 | return; |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 553 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 554 | |
| 555 | IdentifierInfo *AttrName; |
| 556 | SourceLocation AttrNameLoc; |
| 557 | if (IsString) { |
| 558 | SmallString<8> StrBuffer; |
| 559 | bool Invalid = false; |
| 560 | StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid); |
| 561 | if (Invalid) { |
| 562 | T.skipToEnd(); |
| 563 | return; |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 564 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 565 | AttrName = PP.getIdentifierInfo(Str); |
| 566 | AttrNameLoc = ConsumeStringToken(); |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 567 | } else { |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 568 | AttrName = Tok.getIdentifierInfo(); |
| 569 | AttrNameLoc = ConsumeToken(); |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 570 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 571 | |
Aaron Ballman | fdd783a | 2014-03-31 18:18:43 +0000 | [diff] [blame] | 572 | bool AttrHandled = false; |
| 573 | |
| 574 | // Parse attribute arguments. |
| 575 | if (Tok.is(tok::l_paren)) |
| 576 | AttrHandled = ParseMicrosoftDeclSpecArgs(AttrName, AttrNameLoc, Attrs); |
| 577 | else if (AttrName->getName() == "property") |
| 578 | // The property attribute must have an argument list. |
| 579 | Diag(Tok.getLocation(), diag::err_expected_lparen_after) |
| 580 | << AttrName->getName(); |
| 581 | |
| 582 | if (!AttrHandled) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 583 | Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 584 | AttributeList::AS_Declspec); |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 585 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 586 | T.consumeClose(); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 587 | } |
| 588 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 589 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 590 | // Treat these like attributes |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 591 | while (true) { |
| 592 | switch (Tok.getKind()) { |
| 593 | case tok::kw___fastcall: |
| 594 | case tok::kw___stdcall: |
| 595 | case tok::kw___thiscall: |
| 596 | case tok::kw___cdecl: |
| 597 | case tok::kw___vectorcall: |
| 598 | case tok::kw___ptr64: |
| 599 | case tok::kw___w64: |
| 600 | case tok::kw___ptr32: |
| 601 | case tok::kw___unaligned: |
| 602 | case tok::kw___sptr: |
| 603 | case tok::kw___uptr: { |
| 604 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 605 | SourceLocation AttrNameLoc = ConsumeToken(); |
| 606 | attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
| 607 | AttributeList::AS_Keyword); |
| 608 | break; |
| 609 | } |
| 610 | default: |
| 611 | return; |
| 612 | } |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 613 | } |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 614 | } |
| 615 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 616 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 617 | // Treat these like attributes |
| 618 | while (Tok.is(tok::kw___pascal)) { |
| 619 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 620 | SourceLocation AttrNameLoc = ConsumeToken(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 621 | attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 622 | AttributeList::AS_Keyword); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 623 | } |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 626 | void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { |
| 627 | // Treat these like attributes |
| 628 | while (Tok.is(tok::kw___kernel)) { |
Richard Smith | 0cdcc98 | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 629 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 630 | SourceLocation AttrNameLoc = ConsumeToken(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 631 | attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 632 | AttributeList::AS_Keyword); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
Aaron Ballman | 05d76ea | 2014-01-14 01:29:54 +0000 | [diff] [blame] | 636 | void Parser::ParseOpenCLQualifiers(ParsedAttributes &Attrs) { |
| 637 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 638 | SourceLocation AttrNameLoc = Tok.getLocation(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 639 | Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, |
Aaron Ballman | 2689133 | 2014-01-14 17:41:53 +0000 | [diff] [blame] | 640 | AttributeList::AS_Keyword); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Fariborz Jahanian | dbc956d | 2014-10-02 16:39:45 +0000 | [diff] [blame] | 643 | static bool VersionNumberSeparator(const char Separator) { |
| 644 | return (Separator == '.' || Separator == '_'); |
| 645 | } |
| 646 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 647 | /// \brief Parse a version number. |
| 648 | /// |
| 649 | /// version: |
| 650 | /// simple-integer |
| 651 | /// simple-integer ',' simple-integer |
| 652 | /// simple-integer ',' simple-integer ',' simple-integer |
| 653 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
| 654 | Range = Tok.getLocation(); |
| 655 | |
| 656 | if (!Tok.is(tok::numeric_constant)) { |
| 657 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 658 | SkipUntil(tok::comma, tok::r_paren, |
| 659 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 660 | return VersionTuple(); |
| 661 | } |
| 662 | |
| 663 | // Parse the major (and possibly minor and subminor) versions, which |
| 664 | // are stored in the numeric constant. We utilize a quirk of the |
| 665 | // lexer, which is that it handles something like 1.2.3 as a single |
| 666 | // numeric constant, rather than two separate tokens. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 667 | SmallString<512> Buffer; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 668 | Buffer.resize(Tok.getLength()+1); |
| 669 | const char *ThisTokBegin = &Buffer[0]; |
| 670 | |
| 671 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 672 | bool Invalid = false; |
| 673 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 674 | if (Invalid) |
| 675 | return VersionTuple(); |
| 676 | |
| 677 | // Parse the major version. |
| 678 | unsigned AfterMajor = 0; |
| 679 | unsigned Major = 0; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 680 | while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 681 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 682 | ++AfterMajor; |
| 683 | } |
| 684 | |
| 685 | if (AfterMajor == 0) { |
| 686 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 687 | SkipUntil(tok::comma, tok::r_paren, |
| 688 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 689 | return VersionTuple(); |
| 690 | } |
| 691 | |
| 692 | if (AfterMajor == ActualLength) { |
| 693 | ConsumeToken(); |
| 694 | |
| 695 | // We only had a single version component. |
| 696 | if (Major == 0) { |
| 697 | Diag(Tok, diag::err_zero_version); |
| 698 | return VersionTuple(); |
| 699 | } |
| 700 | |
| 701 | return VersionTuple(Major); |
| 702 | } |
| 703 | |
Fariborz Jahanian | ce72e63 | 2014-10-02 17:57:26 +0000 | [diff] [blame] | 704 | const char AfterMajorSeparator = ThisTokBegin[AfterMajor]; |
| 705 | if (!VersionNumberSeparator(AfterMajorSeparator) |
Fariborz Jahanian | dbc956d | 2014-10-02 16:39:45 +0000 | [diff] [blame] | 706 | || (AfterMajor + 1 == ActualLength)) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 707 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 708 | SkipUntil(tok::comma, tok::r_paren, |
| 709 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 710 | return VersionTuple(); |
| 711 | } |
| 712 | |
| 713 | // Parse the minor version. |
| 714 | unsigned AfterMinor = AfterMajor + 1; |
| 715 | unsigned Minor = 0; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 716 | while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 717 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 718 | ++AfterMinor; |
| 719 | } |
| 720 | |
| 721 | if (AfterMinor == ActualLength) { |
| 722 | ConsumeToken(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 723 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 724 | // We had major.minor. |
| 725 | if (Major == 0 && Minor == 0) { |
| 726 | Diag(Tok, diag::err_zero_version); |
| 727 | return VersionTuple(); |
| 728 | } |
| 729 | |
Fariborz Jahanian | 2618dba | 2014-10-06 16:46:02 +0000 | [diff] [blame] | 730 | return VersionTuple(Major, Minor, (AfterMajorSeparator == '_')); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Fariborz Jahanian | ce72e63 | 2014-10-02 17:57:26 +0000 | [diff] [blame] | 733 | const char AfterMinorSeparator = ThisTokBegin[AfterMinor]; |
Fariborz Jahanian | dbc956d | 2014-10-02 16:39:45 +0000 | [diff] [blame] | 734 | // If what follows is not a '.' or '_', we have a problem. |
Fariborz Jahanian | ce72e63 | 2014-10-02 17:57:26 +0000 | [diff] [blame] | 735 | if (!VersionNumberSeparator(AfterMinorSeparator)) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 736 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 737 | SkipUntil(tok::comma, tok::r_paren, |
| 738 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 739 | return VersionTuple(); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 740 | } |
Fariborz Jahanian | ce72e63 | 2014-10-02 17:57:26 +0000 | [diff] [blame] | 741 | |
Fariborz Jahanian | b616161 | 2014-10-03 17:21:12 +0000 | [diff] [blame] | 742 | // Warn if separators, be it '.' or '_', do not match. |
Fariborz Jahanian | ce72e63 | 2014-10-02 17:57:26 +0000 | [diff] [blame] | 743 | if (AfterMajorSeparator != AfterMinorSeparator) |
| 744 | Diag(Tok, diag::warn_expected_consistent_version_separator); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 745 | |
| 746 | // Parse the subminor version. |
| 747 | unsigned AfterSubminor = AfterMinor + 1; |
| 748 | unsigned Subminor = 0; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 749 | while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 750 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 751 | ++AfterSubminor; |
| 752 | } |
| 753 | |
| 754 | if (AfterSubminor != ActualLength) { |
| 755 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 756 | SkipUntil(tok::comma, tok::r_paren, |
| 757 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 758 | return VersionTuple(); |
| 759 | } |
| 760 | ConsumeToken(); |
Fariborz Jahanian | 2618dba | 2014-10-06 16:46:02 +0000 | [diff] [blame] | 761 | return VersionTuple(Major, Minor, Subminor, (AfterMajorSeparator == '_')); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | /// \brief Parse the contents of the "availability" attribute. |
| 765 | /// |
| 766 | /// availability-attribute: |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 767 | /// 'availability' '(' platform ',' version-arg-list, opt-message')' |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 768 | /// |
| 769 | /// platform: |
| 770 | /// identifier |
| 771 | /// |
| 772 | /// version-arg-list: |
| 773 | /// version-arg |
| 774 | /// version-arg ',' version-arg-list |
| 775 | /// |
| 776 | /// version-arg: |
| 777 | /// 'introduced' '=' version |
| 778 | /// 'deprecated' '=' version |
Douglas Gregor | fdd417f | 2012-03-11 04:53:21 +0000 | [diff] [blame] | 779 | /// 'obsoleted' = version |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 780 | /// 'unavailable' |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 781 | /// opt-message: |
| 782 | /// 'message' '=' <string> |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 783 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 784 | SourceLocation AvailabilityLoc, |
| 785 | ParsedAttributes &attrs, |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 786 | SourceLocation *endLoc, |
| 787 | IdentifierInfo *ScopeName, |
| 788 | SourceLocation ScopeLoc, |
| 789 | AttributeList::Syntax Syntax) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 790 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 791 | AvailabilityChange Changes[Unknown]; |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 792 | ExprResult MessageExpr; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 793 | |
| 794 | // Opening '('. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 795 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 796 | if (T.consumeOpen()) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 797 | Diag(Tok, diag::err_expected) << tok::l_paren; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 798 | return; |
| 799 | } |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 800 | |
| 801 | // Parse the platform name, |
| 802 | if (Tok.isNot(tok::identifier)) { |
| 803 | Diag(Tok, diag::err_availability_expected_platform); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 804 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 805 | return; |
| 806 | } |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 807 | IdentifierLoc *Platform = ParseIdentifierLoc(); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 808 | |
| 809 | // Parse the ',' following the platform name. |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 810 | if (ExpectAndConsume(tok::comma)) { |
| 811 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 812 | return; |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 813 | } |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 814 | |
| 815 | // If we haven't grabbed the pointers for the identifiers |
| 816 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 817 | if (!Ident_introduced) { |
| 818 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 819 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 820 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 821 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 822 | Ident_message = PP.getIdentifierInfo("message"); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 826 | SourceLocation UnavailableLoc; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 827 | do { |
| 828 | if (Tok.isNot(tok::identifier)) { |
| 829 | Diag(Tok, diag::err_availability_expected_change); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 830 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 831 | return; |
| 832 | } |
| 833 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 834 | SourceLocation KeywordLoc = ConsumeToken(); |
| 835 | |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 836 | if (Keyword == Ident_unavailable) { |
| 837 | if (UnavailableLoc.isValid()) { |
| 838 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 839 | << Keyword << SourceRange(UnavailableLoc); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 840 | } |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 841 | UnavailableLoc = KeywordLoc; |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 842 | continue; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 845 | if (Tok.isNot(tok::equal)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 846 | Diag(Tok, diag::err_expected_after) << Keyword << tok::equal; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 847 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 848 | return; |
| 849 | } |
| 850 | ConsumeToken(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 851 | if (Keyword == Ident_message) { |
David Majnemer | 2e49830 | 2014-07-18 05:43:12 +0000 | [diff] [blame] | 852 | if (Tok.isNot(tok::string_literal)) { |
Andy Gibbs | a8df57a | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 853 | Diag(Tok, diag::err_expected_string_literal) |
| 854 | << /*Source='availability attribute'*/2; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 855 | SkipUntil(tok::r_paren, StopAtSemi); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 856 | return; |
| 857 | } |
| 858 | MessageExpr = ParseStringLiteralExpression(); |
David Majnemer | 2e49830 | 2014-07-18 05:43:12 +0000 | [diff] [blame] | 859 | // Also reject wide string literals. |
| 860 | if (StringLiteral *MessageStringLiteral = |
| 861 | cast_or_null<StringLiteral>(MessageExpr.get())) { |
| 862 | if (MessageStringLiteral->getCharByteWidth() != 1) { |
| 863 | Diag(MessageStringLiteral->getSourceRange().getBegin(), |
| 864 | diag::err_expected_string_literal) |
| 865 | << /*Source='availability attribute'*/ 2; |
| 866 | SkipUntil(tok::r_paren, StopAtSemi); |
| 867 | return; |
| 868 | } |
| 869 | } |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 870 | break; |
| 871 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 872 | |
Fariborz Jahanian | 5a29e6a | 2014-11-05 23:58:55 +0000 | [diff] [blame] | 873 | // Special handling of 'NA' only when applied to introduced or |
| 874 | // deprecated. |
| 875 | if ((Keyword == Ident_introduced || Keyword == Ident_deprecated) && |
| 876 | Tok.is(tok::identifier)) { |
| 877 | IdentifierInfo *NA = Tok.getIdentifierInfo(); |
| 878 | if (NA->getName() == "NA") { |
| 879 | ConsumeToken(); |
| 880 | if (Keyword == Ident_introduced) |
| 881 | UnavailableLoc = KeywordLoc; |
| 882 | continue; |
| 883 | } |
| 884 | } |
| 885 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 886 | SourceRange VersionRange; |
| 887 | VersionTuple Version = ParseVersionTuple(VersionRange); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 888 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 889 | if (Version.empty()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 890 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 891 | return; |
| 892 | } |
| 893 | |
| 894 | unsigned Index; |
| 895 | if (Keyword == Ident_introduced) |
| 896 | Index = Introduced; |
| 897 | else if (Keyword == Ident_deprecated) |
| 898 | Index = Deprecated; |
| 899 | else if (Keyword == Ident_obsoleted) |
| 900 | Index = Obsoleted; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 901 | else |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 902 | Index = Unknown; |
| 903 | |
| 904 | if (Index < Unknown) { |
| 905 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 906 | Diag(KeywordLoc, diag::err_availability_redundant) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 907 | << Keyword |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 908 | << SourceRange(Changes[Index].KeywordLoc, |
| 909 | Changes[Index].VersionRange.getEnd()); |
| 910 | } |
| 911 | |
| 912 | Changes[Index].KeywordLoc = KeywordLoc; |
| 913 | Changes[Index].Version = Version; |
| 914 | Changes[Index].VersionRange = VersionRange; |
| 915 | } else { |
| 916 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 917 | << Keyword << VersionRange; |
| 918 | } |
| 919 | |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 920 | } while (TryConsumeToken(tok::comma)); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 921 | |
| 922 | // Closing ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 923 | if (T.consumeClose()) |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 924 | return; |
| 925 | |
| 926 | if (endLoc) |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 927 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 928 | |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 929 | // The 'unavailable' availability cannot be combined with any other |
| 930 | // availability changes. Make sure that hasn't happened. |
| 931 | if (UnavailableLoc.isValid()) { |
| 932 | bool Complained = false; |
| 933 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 934 | if (Changes[Index].KeywordLoc.isValid()) { |
| 935 | if (!Complained) { |
| 936 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 937 | << SourceRange(Changes[Index].KeywordLoc, |
| 938 | Changes[Index].VersionRange.getEnd()); |
| 939 | Complained = true; |
| 940 | } |
| 941 | |
| 942 | // Clear out the availability. |
| 943 | Changes[Index] = AvailabilityChange(); |
| 944 | } |
| 945 | } |
| 946 | } |
| 947 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 948 | // Record this attribute |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 949 | attrs.addNew(&Availability, |
| 950 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 951 | ScopeName, ScopeLoc, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 952 | Platform, |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 953 | Changes[Introduced], |
| 954 | Changes[Deprecated], |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 955 | Changes[Obsoleted], |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 956 | UnavailableLoc, MessageExpr.get(), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 957 | Syntax); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 960 | /// \brief Parse the contents of the "objc_bridge_related" attribute. |
| 961 | /// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')' |
| 962 | /// related_class: |
| 963 | /// Identifier |
| 964 | /// |
| 965 | /// opt-class_method: |
| 966 | /// Identifier: | <empty> |
| 967 | /// |
| 968 | /// opt-instance_method: |
| 969 | /// Identifier | <empty> |
| 970 | /// |
| 971 | void Parser::ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated, |
| 972 | SourceLocation ObjCBridgeRelatedLoc, |
| 973 | ParsedAttributes &attrs, |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 974 | SourceLocation *endLoc, |
| 975 | IdentifierInfo *ScopeName, |
| 976 | SourceLocation ScopeLoc, |
| 977 | AttributeList::Syntax Syntax) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 978 | // Opening '('. |
| 979 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 980 | if (T.consumeOpen()) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 981 | Diag(Tok, diag::err_expected) << tok::l_paren; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 982 | return; |
| 983 | } |
| 984 | |
| 985 | // Parse the related class name. |
| 986 | if (Tok.isNot(tok::identifier)) { |
| 987 | Diag(Tok, diag::err_objcbridge_related_expected_related_class); |
| 988 | SkipUntil(tok::r_paren, StopAtSemi); |
| 989 | return; |
| 990 | } |
| 991 | IdentifierLoc *RelatedClass = ParseIdentifierLoc(); |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 992 | if (ExpectAndConsume(tok::comma)) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 993 | SkipUntil(tok::r_paren, StopAtSemi); |
| 994 | return; |
| 995 | } |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 996 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 997 | // Parse optional class method name. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 998 | IdentifierLoc *ClassMethod = nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 999 | if (Tok.is(tok::identifier)) { |
| 1000 | ClassMethod = ParseIdentifierLoc(); |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1001 | if (!TryConsumeToken(tok::colon)) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1002 | Diag(Tok, diag::err_objcbridge_related_selector_name); |
| 1003 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1004 | return; |
| 1005 | } |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1006 | } |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1007 | if (!TryConsumeToken(tok::comma)) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1008 | if (Tok.is(tok::colon)) |
| 1009 | Diag(Tok, diag::err_objcbridge_related_selector_name); |
| 1010 | else |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1011 | Diag(Tok, diag::err_expected) << tok::comma; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1012 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1013 | return; |
| 1014 | } |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1015 | |
| 1016 | // Parse optional instance method name. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1017 | IdentifierLoc *InstanceMethod = nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1018 | if (Tok.is(tok::identifier)) |
| 1019 | InstanceMethod = ParseIdentifierLoc(); |
| 1020 | else if (Tok.isNot(tok::r_paren)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1021 | Diag(Tok, diag::err_expected) << tok::r_paren; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1022 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1023 | return; |
| 1024 | } |
| 1025 | |
| 1026 | // Closing ')'. |
| 1027 | if (T.consumeClose()) |
| 1028 | return; |
| 1029 | |
| 1030 | if (endLoc) |
| 1031 | *endLoc = T.getCloseLocation(); |
| 1032 | |
| 1033 | // Record this attribute |
| 1034 | attrs.addNew(&ObjCBridgeRelated, |
| 1035 | SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1036 | ScopeName, ScopeLoc, |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1037 | RelatedClass, |
| 1038 | ClassMethod, |
| 1039 | InstanceMethod, |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1040 | Syntax); |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1041 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1042 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1043 | // Late Parsed Attributes: |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1044 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 1045 | |
| 1046 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 1047 | |
| 1048 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 1049 | Self->ParseLexedAttributes(*Class); |
| 1050 | } |
| 1051 | |
| 1052 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1053 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 1057 | /// scope appropriately. |
| 1058 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 1059 | // Deal with templates |
| 1060 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 1061 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 1062 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 1063 | HasTemplateScope); |
| 1064 | if (HasTemplateScope) |
| 1065 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 1066 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1067 | // Set or update the scope flags. |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1068 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1069 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1070 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 1071 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 1072 | |
DeLesley Hutchins | 6f86004 | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1073 | // Enter the scope of nested classes |
| 1074 | if (!AlreadyHasClassScope) |
| 1075 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 1076 | Class.TagOrTemplate); |
Benjamin Kramer | 1d373c6 | 2012-05-17 12:01:52 +0000 | [diff] [blame] | 1077 | if (!Class.LateParsedDeclarations.empty()) { |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1078 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){ |
| 1079 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 1080 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1081 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1082 | |
DeLesley Hutchins | 6f86004 | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1083 | if (!AlreadyHasClassScope) |
| 1084 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 1085 | Class.TagOrTemplate); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1088 | |
| 1089 | /// \brief Parse all attributes in LAs, and attach them to Decl D. |
| 1090 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 1091 | bool EnterScope, bool OnDefinition) { |
DeLesley Hutchins | 66e300e | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1092 | assert(LAs.parseSoon() && |
| 1093 | "Attribute list should be marked for immediate parsing."); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1094 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 19c722d | 2012-08-15 22:41:04 +0000 | [diff] [blame] | 1095 | if (D) |
| 1096 | LAs[i]->addDecl(D); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1097 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
Benjamin Kramer | bafc49a | 2012-04-14 12:44:47 +0000 | [diff] [blame] | 1098 | delete LAs[i]; |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1099 | } |
| 1100 | LAs.clear(); |
| 1101 | } |
| 1102 | |
| 1103 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1104 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 1105 | /// This will be called at the end of parsing a class declaration |
| 1106 | /// for each LateParsedAttribute. We consume the saved tokens and |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1107 | /// create an attribute with the arguments filled in. We add this |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1108 | /// to the Attribute list for the decl. |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1109 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 1110 | bool EnterScope, bool OnDefinition) { |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1111 | // Save the current token position. |
| 1112 | SourceLocation OrigLoc = Tok.getLocation(); |
| 1113 | |
| 1114 | // Append the current token at the end of the new token stream so that it |
| 1115 | // doesn't get lost. |
| 1116 | LA.Toks.push_back(Tok); |
| 1117 | PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); |
| 1118 | // Consume the previously pushed token. |
Argyrios Kyrtzidis | c36633c | 2013-03-27 23:58:17 +0000 | [diff] [blame] | 1119 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1120 | |
| 1121 | ParsedAttributes Attrs(AttrFactory); |
| 1122 | SourceLocation endLoc; |
| 1123 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1124 | if (LA.Decls.size() > 0) { |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1125 | Decl *D = LA.Decls[0]; |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1126 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1127 | RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext()); |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1128 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1129 | // Allow 'this' within late-parsed attributes. |
Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 1130 | Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0, |
| 1131 | ND && ND->isCXXInstanceMember()); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1132 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1133 | if (LA.Decls.size() == 1) { |
| 1134 | // If the Decl is templatized, add template parameters to scope. |
| 1135 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 1136 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 1137 | if (HasTemplateScope) |
| 1138 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1139 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1140 | // If the Decl is on a function, add function parameters to the scope. |
| 1141 | bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
| 1142 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope); |
| 1143 | if (HasFunScope) |
| 1144 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1145 | |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1146 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1147 | nullptr, SourceLocation(), AttributeList::AS_GNU, |
| 1148 | nullptr); |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1149 | |
| 1150 | if (HasFunScope) { |
| 1151 | Actions.ActOnExitFunctionContext(); |
| 1152 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 1153 | } |
| 1154 | if (HasTemplateScope) { |
| 1155 | TempScope.Exit(); |
| 1156 | } |
| 1157 | } else { |
| 1158 | // If there are multiple decls, then the decl cannot be within the |
| 1159 | // function scope. |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1160 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1161 | nullptr, SourceLocation(), AttributeList::AS_GNU, |
| 1162 | nullptr); |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1163 | } |
DeLesley Hutchins | 71d6103 | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 1164 | } else { |
| 1165 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Aaron Ballman | 9a99e0d | 2014-01-20 17:18:35 +0000 | [diff] [blame] | 1168 | const AttributeList *AL = Attrs.getList(); |
| 1169 | if (OnDefinition && AL && !AL->isCXX11Attribute() && |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1170 | AL->isKnownToGCC()) |
Aaron Ballman | 9a99e0d | 2014-01-20 17:18:35 +0000 | [diff] [blame] | 1171 | Diag(Tok, diag::warn_attribute_on_function_definition) |
| 1172 | << &LA.AttrName; |
| 1173 | |
| 1174 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1175 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1176 | |
| 1177 | if (Tok.getLocation() != OrigLoc) { |
| 1178 | // Due to a parsing error, we either went over the cached tokens or |
| 1179 | // there are still cached tokens left, so we skip the leftover tokens. |
| 1180 | // Since this is an uncommon situation that should be avoided, use the |
| 1181 | // expensive isBeforeInTranslationUnit call. |
| 1182 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 1183 | OrigLoc)) |
| 1184 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
Douglas Gregor | 0cf55e9 | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 1185 | ConsumeAnyToken(); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1186 | } |
| 1187 | } |
| 1188 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1189 | void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName, |
| 1190 | SourceLocation AttrNameLoc, |
| 1191 | ParsedAttributes &Attrs, |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1192 | SourceLocation *EndLoc, |
| 1193 | IdentifierInfo *ScopeName, |
| 1194 | SourceLocation ScopeLoc, |
| 1195 | AttributeList::Syntax Syntax) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1196 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 1197 | |
| 1198 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1199 | T.consumeOpen(); |
| 1200 | |
| 1201 | if (Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1202 | Diag(Tok, diag::err_expected) << tok::identifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1203 | T.skipToEnd(); |
| 1204 | return; |
| 1205 | } |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 1206 | IdentifierLoc *ArgumentKind = ParseIdentifierLoc(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1207 | |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 1208 | if (ExpectAndConsume(tok::comma)) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1209 | T.skipToEnd(); |
| 1210 | return; |
| 1211 | } |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1212 | |
| 1213 | SourceRange MatchingCTypeRange; |
| 1214 | TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange); |
| 1215 | if (MatchingCType.isInvalid()) { |
| 1216 | T.skipToEnd(); |
| 1217 | return; |
| 1218 | } |
| 1219 | |
| 1220 | bool LayoutCompatible = false; |
| 1221 | bool MustBeNull = false; |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1222 | while (TryConsumeToken(tok::comma)) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1223 | if (Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1224 | Diag(Tok, diag::err_expected) << tok::identifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1225 | T.skipToEnd(); |
| 1226 | return; |
| 1227 | } |
| 1228 | IdentifierInfo *Flag = Tok.getIdentifierInfo(); |
| 1229 | if (Flag->isStr("layout_compatible")) |
| 1230 | LayoutCompatible = true; |
| 1231 | else if (Flag->isStr("must_be_null")) |
| 1232 | MustBeNull = true; |
| 1233 | else { |
| 1234 | Diag(Tok, diag::err_type_safety_unknown_flag) << Flag; |
| 1235 | T.skipToEnd(); |
| 1236 | return; |
| 1237 | } |
| 1238 | ConsumeToken(); // consume flag |
| 1239 | } |
| 1240 | |
| 1241 | if (!T.consumeClose()) { |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1242 | Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, ScopeName, ScopeLoc, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1243 | ArgumentKind, MatchingCType.get(), |
Aaron Ballman | 80f1529c | 2014-07-16 20:21:50 +0000 | [diff] [blame] | 1244 | LayoutCompatible, MustBeNull, Syntax); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | if (EndLoc) |
| 1248 | *EndLoc = T.getCloseLocation(); |
| 1249 | } |
| 1250 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1251 | /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets |
| 1252 | /// of a C++11 attribute-specifier in a location where an attribute is not |
| 1253 | /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this |
| 1254 | /// situation. |
| 1255 | /// |
| 1256 | /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if |
| 1257 | /// this doesn't appear to actually be an attribute-specifier, and the caller |
| 1258 | /// should try to parse it. |
| 1259 | bool Parser::DiagnoseProhibitedCXX11Attribute() { |
| 1260 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)); |
| 1261 | |
| 1262 | switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) { |
| 1263 | case CAK_NotAttributeSpecifier: |
| 1264 | // No diagnostic: we're in Obj-C++11 and this is not actually an attribute. |
| 1265 | return false; |
| 1266 | |
| 1267 | case CAK_InvalidAttributeSpecifier: |
| 1268 | Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute); |
| 1269 | return false; |
| 1270 | |
| 1271 | case CAK_AttributeSpecifier: |
| 1272 | // Parse and discard the attributes. |
| 1273 | SourceLocation BeginLoc = ConsumeBracket(); |
| 1274 | ConsumeBracket(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1275 | SkipUntil(tok::r_square); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1276 | assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied"); |
| 1277 | SourceLocation EndLoc = ConsumeBracket(); |
| 1278 | Diag(BeginLoc, diag::err_attributes_not_allowed) |
| 1279 | << SourceRange(BeginLoc, EndLoc); |
| 1280 | return true; |
| 1281 | } |
Chandler Carruth | d8f7d38 | 2012-04-10 16:03:08 +0000 | [diff] [blame] | 1282 | llvm_unreachable("All cases handled above."); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
Richard Smith | 98155ad | 2013-02-20 01:17:14 +0000 | [diff] [blame] | 1285 | /// \brief We have found the opening square brackets of a C++11 |
| 1286 | /// attribute-specifier in a location where an attribute is not permitted, but |
| 1287 | /// we know where the attributes ought to be written. Parse them anyway, and |
| 1288 | /// provide a fixit moving them to the right place. |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1289 | void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs, |
| 1290 | SourceLocation CorrectLocation) { |
| 1291 | assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) || |
| 1292 | Tok.is(tok::kw_alignas)); |
| 1293 | |
| 1294 | // Consume the attributes. |
| 1295 | SourceLocation Loc = Tok.getLocation(); |
| 1296 | ParseCXX11Attributes(Attrs); |
| 1297 | CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true); |
| 1298 | |
| 1299 | Diag(Loc, diag::err_attributes_not_allowed) |
| 1300 | << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange) |
| 1301 | << FixItHint::CreateRemoval(AttrRange); |
| 1302 | } |
| 1303 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1304 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 1305 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 1306 | << attrs.Range; |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1309 | void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) { |
| 1310 | AttributeList *AttrList = attrs.getList(); |
| 1311 | while (AttrList) { |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1312 | if (AttrList->isCXX11Attribute()) { |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 1313 | Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr) |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1314 | << AttrList->getName(); |
| 1315 | AttrList->setInvalid(); |
| 1316 | } |
| 1317 | AttrList = AttrList->getNext(); |
| 1318 | } |
| 1319 | } |
| 1320 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1321 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 1322 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1323 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 1324 | /// location of the semicolon in DeclEnd. |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1325 | /// |
| 1326 | /// declaration: [C99 6.7] |
| 1327 | /// block-declaration -> |
| 1328 | /// simple-declaration |
| 1329 | /// others [FIXME] |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1330 | /// [C++] template-declaration |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1331 | /// [C++] namespace-definition |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1332 | /// [C++] using-directive |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1333 | /// [C++] using-declaration |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1334 | /// [C++11/C11] static_assert-declaration |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1335 | /// others... [FIXME] |
| 1336 | /// |
Rafael Espindola | 1bd906d | 2014-10-22 14:27:08 +0000 | [diff] [blame] | 1337 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context, |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1338 | SourceLocation &DeclEnd, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1339 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 355094e | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 1340 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | 59b7528 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 1341 | // Must temporarily exit the objective-c container scope for |
| 1342 | // parsing c none objective-c decls. |
| 1343 | ObjCDeclContextSwitch ObjCDC(*this); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1344 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1345 | Decl *SingleDecl = nullptr; |
| 1346 | Decl *OwnedType = nullptr; |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1347 | switch (Tok.getKind()) { |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1348 | case tok::kw_template: |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1349 | case tok::kw_export: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1350 | ProhibitAttributes(attrs); |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1351 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1352 | break; |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1353 | case tok::kw_inline: |
Sebastian Redl | 5a5f2c7 | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 1354 | // 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] | 1355 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1356 | ProhibitAttributes(attrs); |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1357 | SourceLocation InlineLoc = ConsumeToken(); |
| 1358 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 1359 | break; |
| 1360 | } |
Rafael Espindola | 1bd906d | 2014-10-22 14:27:08 +0000 | [diff] [blame] | 1361 | return ParseSimpleDeclaration(Context, DeclEnd, attrs, |
Fariborz Jahanian | 1db5c94 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1362 | true); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1363 | case tok::kw_namespace: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1364 | ProhibitAttributes(attrs); |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1365 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1366 | break; |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1367 | case tok::kw_using: |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 1368 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
Richard Smith | cd1c055 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1369 | DeclEnd, attrs, &OwnedType); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1370 | break; |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1371 | case tok::kw_static_assert: |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1372 | case tok::kw__Static_assert: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1373 | ProhibitAttributes(attrs); |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1374 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1375 | break; |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1376 | default: |
Rafael Espindola | 1bd906d | 2014-10-22 14:27:08 +0000 | [diff] [blame] | 1377 | return ParseSimpleDeclaration(Context, DeclEnd, attrs, true); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1378 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1379 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1380 | // This routine returns a DeclGroup, if the thing we parsed only contains a |
Richard Smith | cd1c055 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1381 | // single decl, convert it now. Alias declarations can also declare a type; |
| 1382 | // include that too if it is present. |
| 1383 | return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 1387 | /// declaration-specifiers init-declarator-list[opt] ';' |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1388 | /// [C++11] attribute-specifier-seq decl-specifier-seq[opt] |
| 1389 | /// init-declarator-list ';' |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1390 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 1391 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1392 | /// |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1393 | /// for-range-declaration: [C++11 6.5p1: stmt.ranged] |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1394 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1395 | /// |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1396 | /// 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] | 1397 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1398 | /// |
| 1399 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 1400 | /// of a simple-declaration. If we find that we are, we also parse the |
| 1401 | /// for-range-initializer, and place it here. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1402 | Parser::DeclGroupPtrTy |
Rafael Espindola | 1bd906d | 2014-10-22 14:27:08 +0000 | [diff] [blame] | 1403 | Parser::ParseSimpleDeclaration(unsigned Context, |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1404 | SourceLocation &DeclEnd, |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1405 | ParsedAttributesWithRange &Attrs, |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1406 | bool RequireSemi, ForRangeInit *FRI) { |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1407 | // Parse the common declaration-specifiers piece. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1408 | ParsingDeclSpec DS(*this); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1409 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 1410 | DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context); |
| 1411 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext); |
| 1412 | |
| 1413 | // If we had a free-standing type definition with a missing semicolon, we |
| 1414 | // may get this far before the problem becomes obvious. |
| 1415 | if (DS.hasTagDefinition() && |
| 1416 | DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext)) |
| 1417 | return DeclGroupPtrTy(); |
Abramo Bagnara | 1cd8368 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 1418 | |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 1419 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 1420 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1421 | if (Tok.is(tok::semi)) { |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1422 | ProhibitAttributes(Attrs); |
Argyrios Kyrtzidis | fbb2bb5 | 2012-05-16 23:49:15 +0000 | [diff] [blame] | 1423 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 005fc1b | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1424 | if (RequireSemi) ConsumeToken(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1425 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1426 | DS); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1427 | DS.complete(TheDecl); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1428 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 1429 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1430 | |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1431 | DS.takeAttributesFrom(Attrs); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1432 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1433 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1434 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1435 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1436 | /// for a declarator. |
| 1437 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 1438 | switch (Tok.getKind()) { |
| 1439 | case tok::annot_cxxscope: |
| 1440 | case tok::annot_template_id: |
| 1441 | case tok::caret: |
| 1442 | case tok::code_completion: |
| 1443 | case tok::coloncolon: |
| 1444 | case tok::ellipsis: |
| 1445 | case tok::kw___attribute: |
| 1446 | case tok::kw_operator: |
| 1447 | case tok::l_paren: |
| 1448 | case tok::star: |
| 1449 | return true; |
| 1450 | |
| 1451 | case tok::amp: |
| 1452 | case tok::ampamp: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1453 | return getLangOpts().CPlusPlus; |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1454 | |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1455 | 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] | 1456 | return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 && |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1457 | NextToken().is(tok::l_square); |
| 1458 | |
| 1459 | 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] | 1460 | return Context == Declarator::MemberContext || getLangOpts().CPlusPlus; |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1461 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1462 | case tok::identifier: |
| 1463 | switch (NextToken().getKind()) { |
| 1464 | case tok::code_completion: |
| 1465 | case tok::coloncolon: |
| 1466 | case tok::comma: |
| 1467 | case tok::equal: |
| 1468 | case tok::equalequal: // Might be a typo for '='. |
| 1469 | case tok::kw_alignas: |
| 1470 | case tok::kw_asm: |
| 1471 | case tok::kw___attribute: |
| 1472 | case tok::l_brace: |
| 1473 | case tok::l_paren: |
| 1474 | case tok::l_square: |
| 1475 | case tok::less: |
| 1476 | case tok::r_brace: |
| 1477 | case tok::r_paren: |
| 1478 | case tok::r_square: |
| 1479 | case tok::semi: |
| 1480 | return true; |
| 1481 | |
| 1482 | case tok::colon: |
| 1483 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1484 | // and in block scope it's probably a label. Inside a class definition, |
| 1485 | // this is a bit-field. |
| 1486 | return Context == Declarator::MemberContext || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1487 | (getLangOpts().CPlusPlus && Context == Declarator::FileContext); |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1488 | |
| 1489 | case tok::identifier: // Possible virt-specifier. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1490 | return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken()); |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1491 | |
| 1492 | default: |
| 1493 | return false; |
| 1494 | } |
| 1495 | |
| 1496 | default: |
| 1497 | return false; |
| 1498 | } |
| 1499 | } |
| 1500 | |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1501 | /// Skip until we reach something which seems like a sensible place to pick |
| 1502 | /// up parsing after a malformed declaration. This will sometimes stop sooner |
| 1503 | /// than SkipUntil(tok::r_brace) would, but will never stop later. |
| 1504 | void Parser::SkipMalformedDecl() { |
| 1505 | while (true) { |
| 1506 | switch (Tok.getKind()) { |
| 1507 | case tok::l_brace: |
| 1508 | // Skip until matching }, then stop. We've probably skipped over |
| 1509 | // a malformed class or function definition or similar. |
| 1510 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1511 | SkipUntil(tok::r_brace); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1512 | if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) { |
| 1513 | // This declaration isn't over yet. Keep skipping. |
| 1514 | continue; |
| 1515 | } |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1516 | TryConsumeToken(tok::semi); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1517 | return; |
| 1518 | |
| 1519 | case tok::l_square: |
| 1520 | ConsumeBracket(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1521 | SkipUntil(tok::r_square); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1522 | continue; |
| 1523 | |
| 1524 | case tok::l_paren: |
| 1525 | ConsumeParen(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1526 | SkipUntil(tok::r_paren); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1527 | continue; |
| 1528 | |
| 1529 | case tok::r_brace: |
| 1530 | return; |
| 1531 | |
| 1532 | case tok::semi: |
| 1533 | ConsumeToken(); |
| 1534 | return; |
| 1535 | |
| 1536 | case tok::kw_inline: |
| 1537 | // 'inline namespace' at the start of a line is almost certainly |
Jordan Rose | 12e730c | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1538 | // a good place to pick back up parsing, except in an Objective-C |
| 1539 | // @interface context. |
| 1540 | if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) && |
| 1541 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1542 | return; |
| 1543 | break; |
| 1544 | |
| 1545 | case tok::kw_namespace: |
| 1546 | // 'namespace' at the start of a line is almost certainly a good |
Jordan Rose | 12e730c | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1547 | // place to pick back up parsing, except in an Objective-C |
| 1548 | // @interface context. |
| 1549 | if (Tok.isAtStartOfLine() && |
| 1550 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
| 1551 | return; |
| 1552 | break; |
| 1553 | |
| 1554 | case tok::at: |
| 1555 | // @end is very much like } in Objective-C contexts. |
| 1556 | if (NextToken().isObjCAtKeyword(tok::objc_end) && |
| 1557 | ParsingInObjCContainer) |
| 1558 | return; |
| 1559 | break; |
| 1560 | |
| 1561 | case tok::minus: |
| 1562 | case tok::plus: |
| 1563 | // - and + probably start new method declarations in Objective-C contexts. |
| 1564 | if (Tok.isAtStartOfLine() && ParsingInObjCContainer) |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1565 | return; |
| 1566 | break; |
| 1567 | |
| 1568 | case tok::eof: |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 1569 | case tok::annot_module_begin: |
| 1570 | case tok::annot_module_end: |
| 1571 | case tok::annot_module_include: |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1572 | return; |
| 1573 | |
| 1574 | default: |
| 1575 | break; |
| 1576 | } |
| 1577 | |
| 1578 | ConsumeAnyToken(); |
| 1579 | } |
| 1580 | } |
| 1581 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1582 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1583 | /// definition or a group of object declarations, actually parse the |
| 1584 | /// result. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1585 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1586 | unsigned Context, |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1587 | bool AllowFunctionDefinitions, |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1588 | SourceLocation *DeclEnd, |
| 1589 | ForRangeInit *FRI) { |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1590 | // Parse the first declarator. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1591 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1592 | ParseDeclarator(D); |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1593 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1594 | // Bail out if the first declarator didn't seem well-formed. |
| 1595 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1596 | SkipMalformedDecl(); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1597 | return DeclGroupPtrTy(); |
Chris Lattner | efb0f11 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1598 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1599 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1600 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1601 | // appropriate function scope after the function Decl has been constructed. |
DeLesley Hutchins | 66e300e | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1602 | // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList. |
| 1603 | LateParsedAttrList LateParsedAttrs(true); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1604 | if (D.isFunctionDeclarator()) |
| 1605 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1606 | |
Chris Lattner | dbb1e93 | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1607 | // 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] | 1608 | if (D.isFunctionDeclarator() && |
Chris Lattner | dbb1e93 | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1609 | // Look at the next token to make sure that this isn't a function |
| 1610 | // declaration. We have to check this because __attribute__ might be the |
| 1611 | // start of a function definition in GCC-extended K&R C. |
Fariborz Jahanian | 712bb81 | 2012-08-10 15:54:40 +0000 | [diff] [blame] | 1612 | !isDeclarationAfterDeclarator()) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1613 | |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1614 | if (AllowFunctionDefinitions) { |
| 1615 | if (isStartOfFunctionDefinition(D)) { |
| 1616 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1617 | Diag(Tok, diag::err_function_declared_typedef); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1618 | |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1619 | // Recover by treating the 'typedef' as spurious. |
| 1620 | DS.ClearStorageClassSpecs(); |
| 1621 | } |
| 1622 | |
| 1623 | Decl *TheDecl = |
| 1624 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
| 1625 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1628 | if (isDeclarationSpecifier()) { |
| 1629 | // If there is an invalid declaration specifier right after the function |
| 1630 | // prototype, then we must be in a missing semicolon case where this isn't |
| 1631 | // actually a body. Just fall through into the code that handles it as a |
| 1632 | // prototype, and let the top-level code handle the erroneous declspec |
| 1633 | // where it would otherwise expect a comma or semicolon. |
| 1634 | } else { |
| 1635 | Diag(Tok, diag::err_expected_fn_body); |
| 1636 | SkipUntil(tok::semi); |
| 1637 | return DeclGroupPtrTy(); |
| 1638 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1639 | } else { |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1640 | if (Tok.is(tok::l_brace)) { |
| 1641 | Diag(Tok, diag::err_function_definition_not_allowed); |
Serge Pavlov | 1de5151 | 2013-12-09 05:25:47 +0000 | [diff] [blame] | 1642 | SkipMalformedDecl(); |
| 1643 | return DeclGroupPtrTy(); |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1644 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1645 | } |
| 1646 | } |
| 1647 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1648 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1649 | return DeclGroupPtrTy(); |
| 1650 | |
| 1651 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1652 | // must parse and analyze the for-range-initializer before the declaration is |
| 1653 | // analyzed. |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1654 | // |
| 1655 | // Handle the Objective-C for-in loop variable similarly, although we |
| 1656 | // don't need to parse the container in advance. |
| 1657 | if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) { |
| 1658 | bool IsForRangeLoop = false; |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1659 | if (TryConsumeToken(tok::colon, FRI->ColonLoc)) { |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1660 | IsForRangeLoop = true; |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1661 | if (Tok.is(tok::l_brace)) |
| 1662 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1663 | else |
| 1664 | FRI->RangeExpr = ParseExpression(); |
| 1665 | } |
| 1666 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1667 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1668 | if (IsForRangeLoop) |
| 1669 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1670 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | cf6e0c8 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 1671 | D.complete(ThisDecl); |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1672 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1675 | SmallVector<Decl *, 8> DeclsInGroup; |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 1676 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes( |
| 1677 | D, ParsedTemplateInfo(), FRI); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1678 | if (LateParsedAttrs.size() > 0) |
| 1679 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1680 | D.complete(FirstDecl); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1681 | if (FirstDecl) |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1682 | DeclsInGroup.push_back(FirstDecl); |
| 1683 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1684 | bool ExpectSemi = Context != Declarator::ForContext; |
Fariborz Jahanian | 577574a | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 1685 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1686 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 1687 | // error, bail out. |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1688 | SourceLocation CommaLoc; |
| 1689 | while (TryConsumeToken(tok::comma, CommaLoc)) { |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1690 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 1691 | // This comma was followed by a line-break and something which can't be |
| 1692 | // the start of a declarator. The comma was probably a typo for a |
| 1693 | // semicolon. |
| 1694 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 1695 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 1696 | ExpectSemi = false; |
| 1697 | break; |
| 1698 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1699 | |
| 1700 | // Parse the next declarator. |
| 1701 | D.clear(); |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 1702 | D.setCommaLoc(CommaLoc); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1703 | |
| 1704 | // Accept attributes in an init-declarator. In the first declarator in a |
| 1705 | // declaration, these would be part of the declspec. In subsequent |
| 1706 | // declarators, they become part of the declarator itself, so that they |
| 1707 | // don't apply to declarators after *this* one. Examples: |
| 1708 | // short __attribute__((common)) var; -> declspec |
| 1709 | // short var __attribute__((common)); -> declarator |
| 1710 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1711 | MaybeParseGNUAttributes(D); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1712 | |
| 1713 | ParseDeclarator(D); |
Fariborz Jahanian | 372030b | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1714 | if (!D.isInvalidType()) { |
| 1715 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 1716 | D.complete(ThisDecl); |
| 1717 | if (ThisDecl) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1718 | DeclsInGroup.push_back(ThisDecl); |
Fariborz Jahanian | 372030b | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1719 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | if (DeclEnd) |
| 1723 | *DeclEnd = Tok.getLocation(); |
| 1724 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1725 | if (ExpectSemi && |
Chris Lattner | 02f1b61 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 1726 | ExpectAndConsumeSemi(Context == Declarator::FileContext |
| 1727 | ? diag::err_invalid_token_after_toplevel_declarator |
| 1728 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 1390134 | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1729 | // Okay, there was no semicolon and one was expected. If we see a |
| 1730 | // declaration specifier, just assume it was missing and continue parsing. |
| 1731 | // Otherwise things are very confused and we skip to recover. |
| 1732 | if (!isDeclarationSpecifier()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1733 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1734 | TryConsumeToken(tok::semi); |
Chris Lattner | 1390134 | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1735 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1738 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup); |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1741 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1742 | /// declarator. Returns true on an error. |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1743 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1744 | // If a simple-asm-expr is present, parse it. |
| 1745 | if (Tok.is(tok::kw_asm)) { |
| 1746 | SourceLocation Loc; |
| 1747 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1748 | if (AsmLabel.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1749 | SkipUntil(tok::semi, StopBeforeMatch); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1750 | return true; |
| 1751 | } |
| 1752 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1753 | D.setAsmLabel(AsmLabel.get()); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1754 | D.SetRangeEnd(Loc); |
| 1755 | } |
| 1756 | |
| 1757 | MaybeParseGNUAttributes(D); |
| 1758 | return false; |
| 1759 | } |
| 1760 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1761 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1762 | /// declarator'. This method parses the remainder of the declaration |
| 1763 | /// (including any attributes or initializer, among other things) and |
| 1764 | /// finalizes the declaration. |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 1765 | /// |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 1766 | /// init-declarator: [C99 6.7] |
| 1767 | /// declarator |
| 1768 | /// declarator '=' initializer |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 1769 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1770 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1771 | /// [C++] declarator initializer[opt] |
| 1772 | /// |
| 1773 | /// [C++] initializer: |
| 1774 | /// [C++] '=' initializer-clause |
| 1775 | /// [C++] '(' expression-list ')' |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1776 | /// [C++0x] '=' 'default' [TODO] |
| 1777 | /// [C++0x] '=' 'delete' |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1778 | /// [C++0x] braced-init-list |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1779 | /// |
| 1780 | /// According to the standard grammar, =default and =delete are function |
| 1781 | /// definitions, but that definitely doesn't fit with the parser here. |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 1782 | /// |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 1783 | Decl *Parser::ParseDeclarationAfterDeclarator( |
| 1784 | Declarator &D, const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1785 | if (ParseAsmAttributesAfterDeclarator(D)) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1786 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1787 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1788 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1789 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1790 | |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 1791 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes( |
| 1792 | Declarator &D, const ParsedTemplateInfo &TemplateInfo, ForRangeInit *FRI) { |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1793 | // Inform the current actions module that we just parsed this declarator. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1794 | Decl *ThisDecl = nullptr; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1795 | switch (TemplateInfo.Kind) { |
| 1796 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1797 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1798 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1799 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1800 | case ParsedTemplateInfo::Template: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1801 | case ParsedTemplateInfo::ExplicitSpecialization: { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1802 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1803 | *TemplateInfo.TemplateParams, |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1804 | D); |
Larisse Voufo | 833b05a | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 1805 | if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1806 | // Re-direct this decl to refer to the templated decl so that we can |
| 1807 | // initialize it. |
| 1808 | ThisDecl = VT->getTemplatedDecl(); |
| 1809 | break; |
| 1810 | } |
| 1811 | case ParsedTemplateInfo::ExplicitInstantiation: { |
| 1812 | if (Tok.is(tok::semi)) { |
| 1813 | DeclResult ThisRes = Actions.ActOnExplicitInstantiation( |
| 1814 | getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D); |
| 1815 | if (ThisRes.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1816 | SkipUntil(tok::semi, StopBeforeMatch); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1817 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1818 | } |
| 1819 | ThisDecl = ThisRes.get(); |
| 1820 | } else { |
| 1821 | // FIXME: This check should be for a variable template instantiation only. |
| 1822 | |
| 1823 | // Check that this is a valid instantiation |
| 1824 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 1825 | // If the declarator-id is not a template-id, issue a diagnostic and |
| 1826 | // recover by ignoring the 'template' keyword. |
| 1827 | Diag(Tok, diag::err_template_defn_explicit_instantiation) |
| 1828 | << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc); |
| 1829 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 1830 | } else { |
| 1831 | SourceLocation LAngleLoc = |
| 1832 | PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
| 1833 | Diag(D.getIdentifierLoc(), |
| 1834 | diag::err_explicit_instantiation_with_definition) |
| 1835 | << SourceRange(TemplateInfo.TemplateLoc) |
| 1836 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
| 1837 | |
| 1838 | // Recover as if it were an explicit specialization. |
| 1839 | TemplateParameterLists FakedParamLists; |
| 1840 | FakedParamLists.push_back(Actions.ActOnTemplateParameterList( |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1841 | 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, nullptr, |
| 1842 | 0, LAngleLoc)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1843 | |
| 1844 | ThisDecl = |
| 1845 | Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D); |
| 1846 | } |
| 1847 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1848 | break; |
| 1849 | } |
| 1850 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1851 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 1852 | bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType(); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1853 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1854 | // Parse declarator '=' initializer. |
Richard Trieu | c64d323 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1855 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | 4972a6d | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 1856 | if (isTokenEqualOrEqualTypo()) { |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 1857 | SourceLocation EqualLoc = ConsumeToken(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1858 | |
Anders Carlsson | 991285e | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1859 | if (Tok.is(tok::kw_delete)) { |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1860 | if (D.isFunctionDeclarator()) |
| 1861 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1862 | << 1 /* delete */; |
| 1863 | else |
| 1864 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Alexis Hunt | 5dafebc | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1865 | } else if (Tok.is(tok::kw_default)) { |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1866 | if (D.isFunctionDeclarator()) |
Sebastian Redl | 46afb55 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 1867 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1868 | << 0 /* default */; |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1869 | else |
| 1870 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1871 | } else { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1872 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1873 | EnterScope(0); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1874 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1875 | } |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1876 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1877 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1878 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Peter Collingbourne | 6b4fdc2 | 2012-07-27 12:56:09 +0000 | [diff] [blame] | 1879 | Actions.FinalizeDeclaration(ThisDecl); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1880 | cutOffParsing(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1881 | return nullptr; |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1882 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1883 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1884 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1885 | |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 1886 | // If this is the only decl in (possibly) range based for statement, |
| 1887 | // our best guess is that the user meant ':' instead of '='. |
| 1888 | if (Tok.is(tok::r_paren) && FRI && D.isFirstDeclarator()) { |
| 1889 | Diag(EqualLoc, diag::err_single_decl_assign_in_for_range) |
| 1890 | << FixItHint::CreateReplacement(EqualLoc, ":"); |
| 1891 | // We are trying to stop parser from looking for ';' in this for |
| 1892 | // statement, therefore preventing spurious errors to be issued. |
| 1893 | FRI->ColonLoc = EqualLoc; |
| 1894 | Init = ExprError(); |
| 1895 | FRI->RangeExpr = Init; |
| 1896 | } |
| 1897 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1898 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1899 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1900 | ExitScope(); |
| 1901 | } |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1902 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1903 | if (Init.isInvalid()) { |
Ismail Pazarbasi | 49ff754 | 2014-05-08 11:28:25 +0000 | [diff] [blame] | 1904 | SmallVector<tok::TokenKind, 2> StopTokens; |
| 1905 | StopTokens.push_back(tok::comma); |
| 1906 | if (D.getContext() == Declarator::ForContext) |
| 1907 | StopTokens.push_back(tok::r_paren); |
| 1908 | SkipUntil(StopTokens, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 604c302 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1909 | Actions.ActOnInitializerError(ThisDecl); |
| 1910 | } else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1911 | Actions.AddInitializerToDecl(ThisDecl, Init.get(), |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1912 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1913 | } |
| 1914 | } else if (Tok.is(tok::l_paren)) { |
| 1915 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1916 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1917 | T.consumeOpen(); |
| 1918 | |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1919 | ExprVector Exprs; |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1920 | CommaLocsTy CommaLocs; |
| 1921 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1922 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1923 | EnterScope(0); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1924 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1927 | if (ParseExpressionList(Exprs, CommaLocs)) { |
David Blaikie | eae0411 | 2012-10-10 23:15:05 +0000 | [diff] [blame] | 1928 | Actions.ActOnInitializerError(ThisDecl); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1929 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1930 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1931 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1932 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1933 | ExitScope(); |
| 1934 | } |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1935 | } else { |
| 1936 | // Match the ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1937 | T.consumeClose(); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1938 | |
| 1939 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1940 | "Unexpected number of commas!"); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1941 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1942 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1943 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1944 | ExitScope(); |
| 1945 | } |
| 1946 | |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1947 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 1948 | T.getCloseLocation(), |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1949 | Exprs); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1950 | Actions.AddInitializerToDecl(ThisDecl, Initializer.get(), |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1951 | /*DirectInit=*/true, TypeContainsAuto); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1952 | } |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1953 | } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) && |
Fariborz Jahanian | 8be1ecd | 2012-07-03 23:22:13 +0000 | [diff] [blame] | 1954 | (!CurParsedObjCImpl || !D.isFunctionDeclarator())) { |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1955 | // Parse C++0x braced-init-list. |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1956 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 1957 | |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1958 | if (D.getCXXScopeSpec().isSet()) { |
| 1959 | EnterScope(0); |
| 1960 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 1961 | } |
| 1962 | |
| 1963 | ExprResult Init(ParseBraceInitializer()); |
| 1964 | |
| 1965 | if (D.getCXXScopeSpec().isSet()) { |
| 1966 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 1967 | ExitScope(); |
| 1968 | } |
| 1969 | |
| 1970 | if (Init.isInvalid()) { |
| 1971 | Actions.ActOnInitializerError(ThisDecl); |
| 1972 | } else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1973 | Actions.AddInitializerToDecl(ThisDecl, Init.get(), |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1974 | /*DirectInit=*/true, TypeContainsAuto); |
| 1975 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1976 | } else { |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1977 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1980 | Actions.FinalizeDeclaration(ThisDecl); |
| 1981 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1982 | return ThisDecl; |
| 1983 | } |
| 1984 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 1985 | /// ParseSpecifierQualifierList |
| 1986 | /// specifier-qualifier-list: |
| 1987 | /// type-specifier specifier-qualifier-list[opt] |
| 1988 | /// type-qualifier specifier-qualifier-list[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 1989 | /// [GNU] attributes specifier-qualifier-list[opt] |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 1990 | /// |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1991 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 1992 | DeclSpecContext DSC) { |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 1993 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 1994 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1995 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1996 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1997 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 1998 | // Validate declspec for type-name. |
| 1999 | unsigned Specs = DS.getParsedSpecifiers(); |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 2000 | if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) { |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2001 | Diag(Tok, diag::err_expected_type); |
| 2002 | DS.SetTypeSpecError(); |
| 2003 | } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
| 2004 | !DS.hasAttributes()) { |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2005 | Diag(Tok, diag::err_typename_requires_specqual); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2006 | if (!DS.hasTypeSpecifier()) |
| 2007 | DS.SetTypeSpecError(); |
| 2008 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 2010 | // Issue diagnostic and remove storage class if present. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2011 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 2012 | if (DS.getStorageClassSpecLoc().isValid()) |
| 2013 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 2014 | else |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2015 | Diag(DS.getThreadStorageClassSpecLoc(), |
| 2016 | diag::err_typename_invalid_storageclass); |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 2017 | DS.ClearStorageClassSpecs(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2018 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2019 | |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 2020 | // Issue diagnostic and remove function specfier if present. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2021 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2022 | if (DS.isInlineSpecified()) |
| 2023 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 2024 | if (DS.isVirtualSpecified()) |
| 2025 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 2026 | if (DS.isExplicitSpecified()) |
| 2027 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 2028 | DS.ClearFunctionSpecs(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2029 | } |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2030 | |
| 2031 | // Issue diagnostic and remove constexpr specfier if present. |
| 2032 | if (DS.isConstexprSpecified()) { |
| 2033 | Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr); |
| 2034 | DS.ClearConstexprSpec(); |
| 2035 | } |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2036 | } |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 2037 | |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2038 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 2039 | /// specified token is valid after the identifier in a declarator which |
| 2040 | /// immediately follows the declspec. For example, these things are valid: |
| 2041 | /// |
| 2042 | /// int x [ 4]; // direct-declarator |
| 2043 | /// int x ( int y); // direct-declarator |
| 2044 | /// int(int x ) // direct-declarator |
| 2045 | /// int x ; // simple-declaration |
| 2046 | /// int x = 17; // init-declarator-list |
| 2047 | /// int x , y; // init-declarator-list |
| 2048 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | a723ba9 | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 2049 | /// int x : 4; // struct-declarator |
Chris Lattner | 2b988c1 | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 2050 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2051 | /// |
| 2052 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 2053 | /// ')' happens to be valid anyway). |
| 2054 | /// int (x) |
| 2055 | /// |
| 2056 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 2057 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 2058 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | a723ba9 | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 2059 | T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon); |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2060 | } |
| 2061 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2062 | |
| 2063 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 2064 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 2065 | /// the declspec has no type specifier. In this case, the declspec is either |
| 2066 | /// malformed or is "implicit int" (in K&R and C89). |
| 2067 | /// |
| 2068 | /// This method handles diagnosing this prettily and returns false if the |
| 2069 | /// declspec is done being processed. If it recovers and thinks there may be |
| 2070 | /// other pieces of declspec after it, it returns true. |
| 2071 | /// |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2072 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2073 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | a0a5d50 | 2014-05-08 22:32:00 +0000 | [diff] [blame] | 2074 | AccessSpecifier AS, DeclSpecContext DSC, |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2075 | ParsedAttributesWithRange &Attrs) { |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2076 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2077 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2078 | SourceLocation Loc = Tok.getLocation(); |
| 2079 | // If we see an identifier that is not a type name, we normally would |
| 2080 | // parse it as the identifer being declared. However, when a typename |
| 2081 | // is typo'd or the definition is not included, this will incorrectly |
| 2082 | // parse the typename as the identifier name and fall over misparsing |
| 2083 | // later parts of the diagnostic. |
| 2084 | // |
| 2085 | // As such, we try to do some look-ahead in cases where this would |
| 2086 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 2087 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 2088 | // an identifier with implicit int, we'd get a parse error because the |
| 2089 | // next token is obviously invalid for a type. Parse these as a case |
| 2090 | // with an invalid type specifier. |
| 2091 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2092 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2093 | // 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] | 2094 | // error, do lookahead to try to do better recovery. This never applies |
| 2095 | // within a type specifier. Outside of C++, we allow this even if the |
| 2096 | // language doesn't "officially" support implicit int -- we support |
Richard Smith | 3b87038 | 2013-04-30 22:43:51 +0000 | [diff] [blame] | 2097 | // implicit int as an extension in C99 and C11. |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 2098 | if (!isTypeSpecifier(DSC) && !getLangOpts().CPlusPlus && |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2099 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2100 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 2101 | // we just avoid eating the identifier, so it will be parsed as the |
| 2102 | // identifier in the declarator. |
| 2103 | return false; |
| 2104 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2105 | |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2106 | if (getLangOpts().CPlusPlus && |
| 2107 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| 2108 | // Don't require a type specifier if we have the 'auto' storage class |
| 2109 | // specifier in C++98 -- we'll promote it to a type specifier. |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2110 | if (SS) |
| 2111 | AnnotateScopeToken(*SS, /*IsNewAnnotation*/false); |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2112 | return false; |
| 2113 | } |
| 2114 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2115 | // Otherwise, if we don't consume this token, we are going to emit an |
| 2116 | // error anyway. Try to recover from various common problems. Check |
| 2117 | // to see if this was a reference to a tag name without a tag specified. |
| 2118 | // 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] | 2119 | // |
| 2120 | // C++ doesn't need this, and isTagName doesn't take SS. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2121 | if (SS == nullptr) { |
| 2122 | const char *TagName = nullptr, *FixitTagName = nullptr; |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2123 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2124 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2125 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2126 | default: break; |
Argyrios Kyrtzidis | 1f32940 | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2127 | case DeclSpec::TST_enum: |
| 2128 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 2129 | case DeclSpec::TST_union: |
| 2130 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 2131 | case DeclSpec::TST_struct: |
| 2132 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2133 | case DeclSpec::TST_interface: |
| 2134 | TagName="__interface"; FixitTagName = "__interface "; |
| 2135 | TagKind=tok::kw___interface;break; |
Argyrios Kyrtzidis | 1f32940 | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2136 | case DeclSpec::TST_class: |
| 2137 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2138 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2139 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2140 | if (TagName) { |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2141 | IdentifierInfo *TokenName = Tok.getIdentifierInfo(); |
| 2142 | LookupResult R(Actions, TokenName, SourceLocation(), |
| 2143 | Sema::LookupOrdinaryName); |
| 2144 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2145 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2146 | << TokenName << TagName << getLangOpts().CPlusPlus |
| 2147 | << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName); |
| 2148 | |
| 2149 | if (Actions.LookupParsedName(R, getCurScope(), SS)) { |
| 2150 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); |
| 2151 | I != IEnd; ++I) |
Kaelyn Uhrain | 3fe3f85 | 2012-04-27 18:26:49 +0000 | [diff] [blame] | 2152 | Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2153 | << TokenName << TagName; |
| 2154 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2155 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2156 | // Parse this as a tag as if the missing tag were present. |
| 2157 | if (TagKind == tok::kw_enum) |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2158 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal); |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2159 | else |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2160 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2161 | /*EnteringContext*/ false, DSC_normal, Attrs); |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2162 | return true; |
| 2163 | } |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2164 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2165 | |
Richard Smith | fe904f0 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2166 | // Determine whether this identifier could plausibly be the name of something |
Richard Smith | edd124e | 2012-05-15 21:42:17 +0000 | [diff] [blame] | 2167 | // being declared (with a missing type). |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 2168 | if (!isTypeSpecifier(DSC) && |
Richard Smith | fe904f0 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2169 | (!SS || DSC == DSC_top_level || DSC == DSC_class)) { |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2170 | // Look ahead to the next token to try to figure out what this declaration |
| 2171 | // was supposed to be. |
| 2172 | switch (NextToken().getKind()) { |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2173 | case tok::l_paren: { |
| 2174 | // static x(4); // 'x' is not a type |
| 2175 | // x(int n); // 'x' is not a type |
| 2176 | // x (*p)[]; // 'x' is a type |
| 2177 | // |
Richard Smith | a0a5d50 | 2014-05-08 22:32:00 +0000 | [diff] [blame] | 2178 | // Since we're in an error case, we can afford to perform a tentative |
| 2179 | // parse to determine which case we're in. |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2180 | TentativeParsingAction PA(*this); |
| 2181 | ConsumeToken(); |
| 2182 | TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false); |
| 2183 | PA.Revert(); |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2184 | |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 2185 | if (TPR != TPResult::False) { |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2186 | // The identifier is followed by a parenthesized declarator. |
| 2187 | // It's supposed to be a type. |
| 2188 | break; |
| 2189 | } |
| 2190 | |
| 2191 | // If we're in a context where we could be declaring a constructor, |
| 2192 | // check whether this is a constructor declaration with a bogus name. |
| 2193 | if (DSC == DSC_class || (DSC == DSC_top_level && SS)) { |
| 2194 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2195 | if (Actions.isCurrentClassNameTypo(II, SS)) { |
| 2196 | Diag(Loc, diag::err_constructor_bad_name) |
| 2197 | << Tok.getIdentifierInfo() << II |
| 2198 | << FixItHint::CreateReplacement(Tok.getLocation(), II->getName()); |
| 2199 | Tok.setIdentifierInfo(II); |
| 2200 | } |
| 2201 | } |
| 2202 | // Fall through. |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2203 | } |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2204 | case tok::comma: |
| 2205 | case tok::equal: |
| 2206 | case tok::kw_asm: |
| 2207 | case tok::l_brace: |
| 2208 | case tok::l_square: |
| 2209 | case tok::semi: |
| 2210 | // This looks like a variable or function declaration. The type is |
| 2211 | // probably missing. We're done parsing decl-specifiers. |
| 2212 | if (SS) |
| 2213 | AnnotateScopeToken(*SS, /*IsNewAnnotation*/false); |
| 2214 | return false; |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2215 | |
| 2216 | default: |
| 2217 | // This is probably supposed to be a type. This includes cases like: |
| 2218 | // int f(itn); |
| 2219 | // struct S { unsinged : 4; }; |
| 2220 | break; |
| 2221 | } |
| 2222 | } |
| 2223 | |
Reid Kleckner | c05ca5e | 2014-06-19 01:23:22 +0000 | [diff] [blame] | 2224 | // This is almost certainly an invalid type name. Let Sema emit a diagnostic |
| 2225 | // and attempt to recover. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2226 | ParsedType T; |
Kaelyn Uhrain | b5b17fe | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2227 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Reid Kleckner | c05ca5e | 2014-06-19 01:23:22 +0000 | [diff] [blame] | 2228 | Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T, |
| 2229 | getLangOpts().CPlusPlus && |
| 2230 | NextToken().is(tok::less)); |
| 2231 | if (T) { |
| 2232 | // The action has suggested that the type T could be used. Set that as |
| 2233 | // the type in the declaration specifiers, consume the would-be type |
| 2234 | // name token, and we're done. |
| 2235 | const char *PrevSpec; |
| 2236 | unsigned DiagID; |
| 2237 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T, |
| 2238 | Actions.getASTContext().getPrintingPolicy()); |
| 2239 | DS.SetRangeEnd(Tok.getLocation()); |
| 2240 | ConsumeToken(); |
| 2241 | // There may be other declaration specifiers after this. |
| 2242 | return true; |
| 2243 | } else if (II != Tok.getIdentifierInfo()) { |
| 2244 | // If no type was suggested, the correction is to a keyword |
| 2245 | Tok.setKind(II->getTokenID()); |
| 2246 | // There may be other declaration specifiers after this. |
| 2247 | return true; |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2248 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2249 | |
Reid Kleckner | c05ca5e | 2014-06-19 01:23:22 +0000 | [diff] [blame] | 2250 | // 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] | 2251 | DS.SetTypeSpecError(); |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2252 | DS.SetRangeEnd(Tok.getLocation()); |
| 2253 | ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2254 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2255 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 2256 | // avoid rippling error messages on subsequent uses of the same type, |
| 2257 | // could be useful if #include was forgotten. |
| 2258 | return false; |
| 2259 | } |
| 2260 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2261 | /// \brief Determine the declaration specifier context from the declarator |
| 2262 | /// context. |
| 2263 | /// |
| 2264 | /// \param Context the declarator context, which is one of the |
| 2265 | /// Declarator::TheContext enumerator values. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2266 | Parser::DeclSpecContext |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2267 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 2268 | if (Context == Declarator::MemberContext) |
| 2269 | return DSC_class; |
| 2270 | if (Context == Declarator::FileContext) |
| 2271 | return DSC_top_level; |
Reid Kleckner | df6e4a0 | 2014-06-06 22:36:36 +0000 | [diff] [blame] | 2272 | if (Context == Declarator::TemplateTypeArgContext) |
| 2273 | return DSC_template_type_arg; |
Richard Smith | 62dad82 | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 2274 | if (Context == Declarator::TrailingReturnContext) |
| 2275 | return DSC_trailing; |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 2276 | if (Context == Declarator::AliasDeclContext || |
| 2277 | Context == Declarator::AliasTemplateContext) |
| 2278 | return DSC_alias_declaration; |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2279 | return DSC_normal; |
| 2280 | } |
| 2281 | |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2282 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 2283 | /// |
| 2284 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 2285 | /// type. Ideally, the type should be propagated directly into Sema. |
| 2286 | /// |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2287 | /// [C11] type-id |
| 2288 | /// [C11] constant-expression |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2289 | /// [C++0x] type-id ...[opt] |
| 2290 | /// [C++0x] assignment-expression ...[opt] |
| 2291 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 2292 | SourceLocation &EllipsisLoc) { |
| 2293 | ExprResult ER; |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2294 | if (isTypeIdInParens()) { |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2295 | SourceLocation TypeLoc = Tok.getLocation(); |
| 2296 | ParsedType Ty = ParseTypeName().get(); |
| 2297 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2298 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 2299 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2300 | } else |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2301 | ER = ParseConstantExpression(); |
| 2302 | |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 2303 | if (getLangOpts().CPlusPlus11) |
| 2304 | TryConsumeToken(tok::ellipsis, EllipsisLoc); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2305 | |
| 2306 | return ER; |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
| 2309 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 2310 | /// attribute to Attrs. |
| 2311 | /// |
| 2312 | /// alignment-specifier: |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2313 | /// [C11] '_Alignas' '(' type-id ')' |
| 2314 | /// [C11] '_Alignas' '(' constant-expression ')' |
Richard Smith | d11c7a1 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2315 | /// [C++11] 'alignas' '(' type-id ...[opt] ')' |
| 2316 | /// [C++11] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2317 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2318 | SourceLocation *EndLoc) { |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2319 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 2320 | "Not an alignment-specifier!"); |
| 2321 | |
Richard Smith | d11c7a1 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2322 | IdentifierInfo *KWName = Tok.getIdentifierInfo(); |
| 2323 | SourceLocation KWLoc = ConsumeToken(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2324 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2325 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 2326 | if (T.expectAndConsume()) |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2327 | return; |
| 2328 | |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2329 | SourceLocation EllipsisLoc; |
| 2330 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2331 | if (ArgExpr.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2332 | T.skipToEnd(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2333 | return; |
| 2334 | } |
| 2335 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2336 | T.consumeClose(); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2337 | if (EndLoc) |
| 2338 | *EndLoc = T.getCloseLocation(); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2339 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2340 | ArgsVector ArgExprs; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2341 | ArgExprs.push_back(ArgExpr.get()); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2342 | Attrs.addNew(KWName, KWLoc, nullptr, KWLoc, ArgExprs.data(), 1, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2343 | AttributeList::AS_Keyword, EllipsisLoc); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2344 | } |
| 2345 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2346 | /// Determine whether we're looking at something that might be a declarator |
| 2347 | /// in a simple-declaration. If it can't possibly be a declarator, maybe |
| 2348 | /// diagnose a missing semicolon after a prior tag definition in the decl |
| 2349 | /// specifier. |
| 2350 | /// |
| 2351 | /// \return \c true if an error occurred and this can't be any kind of |
| 2352 | /// declaration. |
| 2353 | bool |
| 2354 | Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS, |
| 2355 | DeclSpecContext DSContext, |
| 2356 | LateParsedAttrList *LateAttrs) { |
| 2357 | assert(DS.hasTagDefinition() && "shouldn't call this"); |
| 2358 | |
| 2359 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2360 | |
| 2361 | if (getLangOpts().CPlusPlus && |
| 2362 | (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) || |
| 2363 | Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) && |
| 2364 | TryAnnotateCXXScopeToken(EnteringContext)) { |
| 2365 | SkipMalformedDecl(); |
| 2366 | return true; |
| 2367 | } |
| 2368 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2369 | bool HasScope = Tok.is(tok::annot_cxxscope); |
| 2370 | // Make a copy in case GetLookAheadToken invalidates the result of NextToken. |
| 2371 | Token AfterScope = HasScope ? NextToken() : Tok; |
| 2372 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2373 | // Determine whether the following tokens could possibly be a |
| 2374 | // declarator. |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2375 | bool MightBeDeclarator = true; |
| 2376 | if (Tok.is(tok::kw_typename) || Tok.is(tok::annot_typename)) { |
| 2377 | // A declarator-id can't start with 'typename'. |
| 2378 | MightBeDeclarator = false; |
| 2379 | } else if (AfterScope.is(tok::annot_template_id)) { |
| 2380 | // If we have a type expressed as a template-id, this cannot be a |
| 2381 | // declarator-id (such a type cannot be redeclared in a simple-declaration). |
| 2382 | TemplateIdAnnotation *Annot = |
| 2383 | static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue()); |
| 2384 | if (Annot->Kind == TNK_Type_template) |
| 2385 | MightBeDeclarator = false; |
| 2386 | } else if (AfterScope.is(tok::identifier)) { |
| 2387 | const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken(); |
| 2388 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2389 | // These tokens cannot come after the declarator-id in a |
| 2390 | // simple-declaration, and are likely to come after a type-specifier. |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2391 | if (Next.is(tok::star) || Next.is(tok::amp) || Next.is(tok::ampamp) || |
| 2392 | Next.is(tok::identifier) || Next.is(tok::annot_cxxscope) || |
| 2393 | Next.is(tok::coloncolon)) { |
| 2394 | // Missing a semicolon. |
| 2395 | MightBeDeclarator = false; |
| 2396 | } else if (HasScope) { |
| 2397 | // If the declarator-id has a scope specifier, it must redeclare a |
| 2398 | // previously-declared entity. If that's a type (and this is not a |
| 2399 | // typedef), that's an error. |
| 2400 | CXXScopeSpec SS; |
| 2401 | Actions.RestoreNestedNameSpecifierAnnotation( |
| 2402 | Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS); |
| 2403 | IdentifierInfo *Name = AfterScope.getIdentifierInfo(); |
| 2404 | Sema::NameClassification Classification = Actions.ClassifyName( |
| 2405 | getCurScope(), SS, Name, AfterScope.getLocation(), Next, |
| 2406 | /*IsAddressOfOperand*/false); |
| 2407 | switch (Classification.getKind()) { |
| 2408 | case Sema::NC_Error: |
| 2409 | SkipMalformedDecl(); |
| 2410 | return true; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2411 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2412 | case Sema::NC_Keyword: |
| 2413 | case Sema::NC_NestedNameSpecifier: |
| 2414 | llvm_unreachable("typo correction and nested name specifiers not " |
| 2415 | "possible here"); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2416 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2417 | case Sema::NC_Type: |
| 2418 | case Sema::NC_TypeTemplate: |
| 2419 | // Not a previously-declared non-type entity. |
| 2420 | MightBeDeclarator = false; |
| 2421 | break; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2422 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2423 | case Sema::NC_Unknown: |
| 2424 | case Sema::NC_Expression: |
| 2425 | case Sema::NC_VarTemplate: |
| 2426 | case Sema::NC_FunctionTemplate: |
| 2427 | // Might be a redeclaration of a prior entity. |
| 2428 | break; |
| 2429 | } |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2430 | } |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2431 | } |
| 2432 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2433 | if (MightBeDeclarator) |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2434 | return false; |
| 2435 | |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2436 | const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy(); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2437 | Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()), |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 2438 | diag::err_expected_after) |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2439 | << DeclSpec::getSpecifierName(DS.getTypeSpecType(), PPol) << tok::semi; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2440 | |
| 2441 | // Try to recover from the typo, by dropping the tag definition and parsing |
| 2442 | // the problematic tokens as a type. |
| 2443 | // |
| 2444 | // FIXME: Split the DeclSpec into pieces for the standalone |
| 2445 | // declaration and pieces for the following declaration, instead |
| 2446 | // of assuming that all the other pieces attach to new declaration, |
| 2447 | // and call ParsedFreeStandingDeclSpec as appropriate. |
| 2448 | DS.ClearTypeSpecType(); |
| 2449 | ParsedTemplateInfo NotATemplate; |
| 2450 | ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs); |
| 2451 | return false; |
| 2452 | } |
| 2453 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2454 | /// ParseDeclarationSpecifiers |
| 2455 | /// declaration-specifiers: [C99 6.7] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2456 | /// storage-class-specifier declaration-specifiers[opt] |
| 2457 | /// type-specifier declaration-specifiers[opt] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2458 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2459 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 2460 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2461 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2462 | /// |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2463 | /// storage-class-specifier: [C99 6.7.1] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 2464 | /// 'typedef' |
| 2465 | /// 'extern' |
| 2466 | /// 'static' |
| 2467 | /// 'auto' |
| 2468 | /// 'register' |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2469 | /// [C++] 'mutable' |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2470 | /// [C++11] 'thread_local' |
| 2471 | /// [C11] '_Thread_local' |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 2472 | /// [GNU] '__thread' |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2473 | /// function-specifier: [C99 6.7.4] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2474 | /// [C99] 'inline' |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2475 | /// [C++] 'virtual' |
| 2476 | /// [C++] 'explicit' |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2477 | /// [OpenCL] '__kernel' |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2478 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 39c2a8b | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2479 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2480 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2481 | /// |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 2482 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2483 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2484 | AccessSpecifier AS, |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2485 | DeclSpecContext DSContext, |
| 2486 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2487 | if (DS.getSourceRange().isInvalid()) { |
David Majnemer | 24b2830 | 2014-07-26 05:41:31 +0000 | [diff] [blame] | 2488 | // Start the range at the current token but make the end of the range |
| 2489 | // invalid. This will make the entire range invalid unless we successfully |
| 2490 | // consume a token. |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2491 | DS.SetRangeStart(Tok.getLocation()); |
David Majnemer | 24b2830 | 2014-07-26 05:41:31 +0000 | [diff] [blame] | 2492 | DS.SetRangeEnd(SourceLocation()); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2493 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2494 | |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2495 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2496 | bool AttrsLastTime = false; |
| 2497 | ParsedAttributesWithRange attrs(AttrFactory); |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2498 | const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2499 | while (1) { |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2500 | bool isInvalid = false; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2501 | const char *PrevSpec = nullptr; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2502 | unsigned DiagID = 0; |
| 2503 | |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 2504 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2505 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2506 | switch (Tok.getKind()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2507 | default: |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2508 | DoneWithDeclSpec: |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2509 | if (!AttrsLastTime) |
| 2510 | ProhibitAttributes(attrs); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2511 | else { |
| 2512 | // Reject C++11 attributes that appertain to decl specifiers as |
| 2513 | // we don't support any C++11 attributes that appertain to decl |
| 2514 | // specifiers. This also conforms to what g++ 4.8 is doing. |
| 2515 | ProhibitCXX11Attributes(attrs); |
| 2516 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2517 | DS.takeAttributesFrom(attrs); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2518 | } |
Peter Collingbourne | 70188b3 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 2519 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2520 | // If this is not a declaration specifier token, we're done reading decl |
| 2521 | // specifiers. First verify that DeclSpec's are consistent. |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2522 | DS.Finish(Diags, PP, Policy); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2523 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2524 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2525 | case tok::l_square: |
| 2526 | case tok::kw_alignas: |
Richard Smith | 4cabd04 | 2013-02-22 09:15:49 +0000 | [diff] [blame] | 2527 | if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier()) |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2528 | goto DoneWithDeclSpec; |
| 2529 | |
| 2530 | ProhibitAttributes(attrs); |
| 2531 | // FIXME: It would be good to recover by accepting the attributes, |
| 2532 | // but attempting to do that now would cause serious |
| 2533 | // madness in terms of diagnostics. |
| 2534 | attrs.clear(); |
| 2535 | attrs.Range = SourceRange(); |
| 2536 | |
| 2537 | ParseCXX11Attributes(attrs); |
| 2538 | AttrsLastTime = true; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2539 | continue; |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2540 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2541 | case tok::code_completion: { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2542 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2543 | if (DS.hasTypeSpecifier()) { |
| 2544 | bool AllowNonIdentifiers |
| 2545 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 2546 | Scope::BlockScope | |
| 2547 | Scope::TemplateParamScope | |
| 2548 | Scope::FunctionPrototypeScope | |
| 2549 | Scope::AtCatchScope)) == 0; |
| 2550 | bool AllowNestedNameSpecifiers |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2551 | = DSContext == DSC_top_level || |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2552 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 2553 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2554 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2555 | AllowNonIdentifiers, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2556 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2557 | return cutOffParsing(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2558 | } |
| 2559 | |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2560 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 2561 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 2562 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2563 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2564 | : Sema::PCC_Template; |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2565 | else if (DSContext == DSC_class) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2566 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | b6c6a58 | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 2567 | else if (CurParsedObjCImpl) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2568 | CCC = Sema::PCC_ObjCImplementation; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2569 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2570 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2571 | return cutOffParsing(); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2572 | } |
| 2573 | |
Chris Lattner | bd31aa3 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2574 | case tok::coloncolon: // ::foo::bar |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2575 | // C++ scope specifier. Annotate and loop, or bail out on error. |
Eli Friedman | 2a1d9a9 | 2013-08-15 23:59:20 +0000 | [diff] [blame] | 2576 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2577 | if (!DS.hasTypeSpecifier()) |
| 2578 | DS.SetTypeSpecError(); |
| 2579 | goto DoneWithDeclSpec; |
| 2580 | } |
John McCall | 8bc2a70 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 2581 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 2582 | goto DoneWithDeclSpec; |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2583 | continue; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2584 | |
| 2585 | case tok::annot_cxxscope: { |
Richard Smith | 3092a3b | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2586 | if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector()) |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2587 | goto DoneWithDeclSpec; |
| 2588 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2589 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 2590 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 2591 | Tok.getAnnotationRange(), |
| 2592 | SS); |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2593 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2594 | // We are looking for a qualified typename. |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2595 | Token Next = NextToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2596 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2597 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2598 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2599 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2600 | |
| 2601 | // C++ [class.qual]p2: |
| 2602 | // In a lookup in which the constructor is an acceptable lookup |
| 2603 | // result and the nested-name-specifier nominates a class C: |
| 2604 | // |
| 2605 | // - if the name specified after the |
| 2606 | // nested-name-specifier, when looked up in C, is the |
| 2607 | // injected-class-name of C (Clause 9), or |
| 2608 | // |
| 2609 | // - if the name specified after the nested-name-specifier |
| 2610 | // is the same as the identifier or the |
| 2611 | // simple-template-id's template-name in the last |
| 2612 | // component of the nested-name-specifier, |
| 2613 | // |
| 2614 | // the name is instead considered to name the constructor of |
| 2615 | // class C. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2616 | // |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2617 | // Thus, if the template-name is actually the constructor |
| 2618 | // name, then the code is ill-formed; this interpretation is |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2619 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | c0c5dd2 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2620 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
Dmitri Gribenko | d1c91f1 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2621 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2622 | TemplateId->Name && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2623 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 2624 | if (isConstructorDeclarator(/*Unqualified*/false)) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2625 | // The user meant this to be an out-of-line constructor |
| 2626 | // definition, but template arguments are not allowed |
| 2627 | // there. Just allow this as a constructor; we'll |
| 2628 | // complain about it later. |
| 2629 | goto DoneWithDeclSpec; |
| 2630 | } |
| 2631 | |
| 2632 | // The user meant this to name a type, but it actually names |
| 2633 | // a constructor with some extraneous template |
| 2634 | // arguments. Complain, then parse it as a type as the user |
| 2635 | // intended. |
| 2636 | Diag(TemplateId->TemplateNameLoc, |
| 2637 | diag::err_out_of_line_template_id_names_constructor) |
| 2638 | << TemplateId->Name; |
| 2639 | } |
| 2640 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2641 | DS.getTypeSpecScope() = SS; |
| 2642 | ConsumeToken(); // The C++ scope. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2643 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2644 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2645 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2646 | continue; |
| 2647 | } |
| 2648 | |
Douglas Gregor | c5790df | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2649 | if (Next.is(tok::annot_typename)) { |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2650 | DS.getTypeSpecScope() = SS; |
| 2651 | ConsumeToken(); // The C++ scope. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2652 | if (Tok.getAnnotationValue()) { |
| 2653 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 7743034 | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2654 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2655 | Tok.getAnnotationEndLoc(), |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2656 | PrevSpec, DiagID, T, Policy); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2657 | if (isInvalid) |
| 2658 | break; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2659 | } |
Douglas Gregor | c5790df | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2660 | else |
| 2661 | DS.SetTypeSpecError(); |
| 2662 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2663 | ConsumeToken(); // The typename |
| 2664 | } |
| 2665 | |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2666 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2667 | goto DoneWithDeclSpec; |
| 2668 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2669 | // If we're in a context where the identifier could be a class name, |
| 2670 | // check whether this is a constructor declaration. |
Dmitri Gribenko | d1c91f1 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2671 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2672 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2673 | &SS)) { |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 2674 | if (isConstructorDeclarator(/*Unqualified*/false)) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2675 | goto DoneWithDeclSpec; |
| 2676 | |
| 2677 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 2678 | // of the class is qualified in a context where it could name |
| 2679 | // a constructor, its a constructor name. However, we've |
| 2680 | // looked at the declarator, and the user probably meant this |
| 2681 | // to be a type. Complain that it isn't supposed to be treated |
| 2682 | // as a type, then proceed to parse it as a type. |
| 2683 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 2684 | << Next.getIdentifierInfo(); |
| 2685 | } |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2686 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2687 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 2688 | Next.getLocation(), |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2689 | getCurScope(), &SS, |
| 2690 | false, false, ParsedType(), |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2691 | /*IsCtorOrDtorName=*/false, |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2692 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2693 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2694 | // If the referenced identifier is not a type, then this declspec is |
| 2695 | // erroneous: We already checked about that it has no type specifier, and |
| 2696 | // 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] | 2697 | // typename. |
David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 2698 | if (!TypeRep) { |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2699 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2700 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2701 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) { |
| 2702 | if (!Attrs.empty()) { |
| 2703 | AttrsLastTime = true; |
| 2704 | attrs.takeAllFrom(Attrs); |
| 2705 | } |
| 2706 | continue; |
| 2707 | } |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2708 | goto DoneWithDeclSpec; |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2709 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2710 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2711 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2712 | ConsumeToken(); // The C++ scope. |
| 2713 | |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2714 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2715 | DiagID, TypeRep, Policy); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2716 | if (isInvalid) |
| 2717 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2718 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2719 | DS.SetRangeEnd(Tok.getLocation()); |
| 2720 | ConsumeToken(); // The typename. |
| 2721 | |
| 2722 | continue; |
| 2723 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2724 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2725 | case tok::annot_typename: { |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2726 | // If we've previously seen a tag definition, we were almost surely |
| 2727 | // missing a semicolon after it. |
| 2728 | if (DS.hasTypeSpecifier() && DS.hasTagDefinition()) |
| 2729 | goto DoneWithDeclSpec; |
| 2730 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2731 | if (Tok.getAnnotationValue()) { |
| 2732 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 7f8bb36 | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 2733 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2734 | DiagID, T, Policy); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2735 | } else |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2736 | DS.SetTypeSpecError(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2737 | |
Chris Lattner | 005fc1b | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 2738 | if (isInvalid) |
| 2739 | break; |
| 2740 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2741 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2742 | ConsumeToken(); // The typename |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2743 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2744 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2745 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2746 | // Objective-C interface. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2747 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 06e41ae | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2748 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2749 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2750 | continue; |
| 2751 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2752 | |
Douglas Gregor | 0687309 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2753 | case tok::kw___is_signed: |
| 2754 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 2755 | // typically treats it as a trait. If we see __is_signed as it appears |
| 2756 | // in libstdc++, e.g., |
| 2757 | // |
| 2758 | // static const bool __is_signed; |
| 2759 | // |
| 2760 | // then treat __is_signed as an identifier rather than as a keyword. |
| 2761 | if (DS.getTypeSpecType() == TST_bool && |
| 2762 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
Alp Toker | 47642d2 | 2013-12-03 06:13:01 +0000 | [diff] [blame] | 2763 | DS.getStorageClassSpec() == DeclSpec::SCS_static) |
| 2764 | TryKeywordIdentFallback(true); |
Douglas Gregor | 0687309 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2765 | |
| 2766 | // We're done with the declaration-specifiers. |
| 2767 | goto DoneWithDeclSpec; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2768 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2769 | // typedef-name |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 2770 | case tok::kw___super: |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2771 | case tok::kw_decltype: |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2772 | case tok::identifier: { |
Reid Kleckner | c582f01 | 2014-07-14 18:19:58 +0000 | [diff] [blame] | 2773 | // This identifier can only be a typedef name if we haven't already seen |
| 2774 | // a type-specifier. Without this check we misparse: |
| 2775 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 2776 | if (DS.hasTypeSpecifier()) |
| 2777 | goto DoneWithDeclSpec; |
| 2778 | |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 2779 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 2780 | // so handle it as such. This is important for ctor parsing. |
| 2781 | if (getLangOpts().CPlusPlus) { |
| 2782 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
| 2783 | DS.SetTypeSpecError(); |
| 2784 | goto DoneWithDeclSpec; |
| 2785 | } |
| 2786 | if (!Tok.is(tok::identifier)) |
| 2787 | continue; |
| 2788 | } |
| 2789 | |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2790 | // Check for need to substitute AltiVec keyword tokens. |
| 2791 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2792 | break; |
| 2793 | |
Richard Smith | 3092a3b | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2794 | // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not |
| 2795 | // allow the use of a typedef name as a type specifier. |
| 2796 | if (DS.isTypeAltiVecVector()) |
| 2797 | goto DoneWithDeclSpec; |
| 2798 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2799 | ParsedType TypeRep = |
| 2800 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 2801 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2802 | |
Reid Kleckner | df6e4a0 | 2014-06-06 22:36:36 +0000 | [diff] [blame] | 2803 | // MSVC: If we weren't able to parse a default template argument, and it's |
| 2804 | // just a simple identifier, create a DependentNameType. This will allow us |
| 2805 | // to defer the name lookup to template instantiation time, as long we forge a |
| 2806 | // NestedNameSpecifier for the current context. |
| 2807 | if (!TypeRep && DSContext == DSC_template_type_arg && |
| 2808 | getLangOpts().MSVCCompat && getCurScope()->isTemplateParamScope()) { |
| 2809 | TypeRep = Actions.ActOnDelayedDefaultTemplateArg( |
| 2810 | *Tok.getIdentifierInfo(), Tok.getLocation()); |
| 2811 | } |
| 2812 | |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2813 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 2814 | // it must be an implicit int or an error. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2815 | if (!TypeRep) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2816 | ParsedAttributesWithRange Attrs(AttrFactory); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2817 | if (ParseImplicitInt(DS, nullptr, TemplateInfo, AS, DSContext, Attrs)) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2818 | if (!Attrs.empty()) { |
| 2819 | AttrsLastTime = true; |
| 2820 | attrs.takeAllFrom(Attrs); |
| 2821 | } |
| 2822 | continue; |
| 2823 | } |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2824 | goto DoneWithDeclSpec; |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2825 | } |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2826 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2827 | // If we're in a context where the identifier could be a class name, |
| 2828 | // check whether this is a constructor declaration. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2829 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2830 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 2831 | isConstructorDeclarator(/*Unqualified*/true)) |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2832 | goto DoneWithDeclSpec; |
| 2833 | |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2834 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2835 | DiagID, TypeRep, Policy); |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2836 | if (isInvalid) |
| 2837 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2838 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2839 | DS.SetRangeEnd(Tok.getLocation()); |
| 2840 | ConsumeToken(); // The identifier |
| 2841 | |
| 2842 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2843 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2844 | // Objective-C interface. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2845 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 06e41ae | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2846 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2847 | |
Steve Naroff | cd5e782 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 2848 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2849 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2850 | continue; |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2851 | } |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2852 | |
| 2853 | // type-name |
| 2854 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | c0c5dd2 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2855 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2856 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2857 | // This template-id does not refer to a type name, so we're |
| 2858 | // done with the type-specifiers. |
| 2859 | goto DoneWithDeclSpec; |
| 2860 | } |
| 2861 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2862 | // If we're in a context where the template-id could be a |
| 2863 | // constructor name or specialization, check whether this is a |
| 2864 | // constructor declaration. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2865 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2866 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 2867 | isConstructorDeclarator(TemplateId->SS.isEmpty())) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2868 | goto DoneWithDeclSpec; |
| 2869 | |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2870 | // Turn the template-id annotation token into a type annotation |
| 2871 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2872 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2873 | continue; |
| 2874 | } |
| 2875 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 2876 | // GNU attributes support. |
| 2877 | case tok::kw___attribute: |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2878 | ParseGNUAttributes(DS.getAttributes(), nullptr, LateAttrs); |
Chris Lattner | b95cca0 | 2006-10-17 03:01:08 +0000 | [diff] [blame] | 2879 | continue; |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2880 | |
| 2881 | // Microsoft declspec support. |
| 2882 | case tok::kw___declspec: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2883 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2884 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2885 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2886 | // Microsoft single token adornments. |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2887 | case tok::kw___forceinline: { |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2888 | isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID); |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2889 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2890 | SourceLocation AttrNameLoc = Tok.getLocation(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2891 | DS.getAttributes().addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, |
| 2892 | nullptr, 0, AttributeList::AS_Keyword); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2893 | break; |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2894 | } |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2895 | |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 2896 | case tok::kw___sptr: |
| 2897 | case tok::kw___uptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2898 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2899 | case tok::kw___ptr32: |
Steve Naroff | f9c29d4 | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2900 | case tok::kw___w64: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2901 | case tok::kw___cdecl: |
| 2902 | case tok::kw___stdcall: |
| 2903 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2904 | case tok::kw___thiscall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 2905 | case tok::kw___vectorcall: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2906 | case tok::kw___unaligned: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2907 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2908 | continue; |
| 2909 | |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2910 | // Borland single token adornments. |
| 2911 | case tok::kw___pascal: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2912 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2913 | continue; |
| 2914 | |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2915 | // OpenCL single token adornments. |
| 2916 | case tok::kw___kernel: |
| 2917 | ParseOpenCLAttributes(DS.getAttributes()); |
| 2918 | continue; |
| 2919 | |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2920 | // storage-class-specifier |
| 2921 | case tok::kw_typedef: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2922 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2923 | PrevSpec, DiagID, Policy); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2924 | break; |
| 2925 | case tok::kw_extern: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2926 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2927 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2928 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2929 | PrevSpec, DiagID, Policy); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2930 | break; |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2931 | case tok::kw___private_extern__: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2932 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2933 | Loc, PrevSpec, DiagID, Policy); |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2934 | break; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2935 | case tok::kw_static: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2936 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2937 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2938 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2939 | PrevSpec, DiagID, Policy); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2940 | break; |
| 2941 | case tok::kw_auto: |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2942 | if (getLangOpts().CPlusPlus11) { |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2943 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2944 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2945 | PrevSpec, DiagID, Policy); |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2946 | if (!isInvalid) |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2947 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2948 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2949 | } else |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2950 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2951 | DiagID, Policy); |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2952 | } else |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2953 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2954 | PrevSpec, DiagID, Policy); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2955 | break; |
| 2956 | case tok::kw_register: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2957 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2958 | PrevSpec, DiagID, Policy); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2959 | break; |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2960 | case tok::kw_mutable: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2961 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2962 | PrevSpec, DiagID, Policy); |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2963 | break; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2964 | case tok::kw___thread: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2965 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc, |
| 2966 | PrevSpec, DiagID); |
| 2967 | break; |
| 2968 | case tok::kw_thread_local: |
| 2969 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc, |
| 2970 | PrevSpec, DiagID); |
| 2971 | break; |
| 2972 | case tok::kw__Thread_local: |
| 2973 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local, |
| 2974 | Loc, PrevSpec, DiagID); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2975 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2976 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2977 | // function-specifier |
| 2978 | case tok::kw_inline: |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2979 | isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2980 | break; |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2981 | case tok::kw_virtual: |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2982 | isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2983 | break; |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2984 | case tok::kw_explicit: |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2985 | isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2986 | break; |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 2987 | case tok::kw__Noreturn: |
| 2988 | if (!getLangOpts().C11) |
| 2989 | Diag(Loc, diag::ext_c11_noreturn); |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2990 | isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID); |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 2991 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2992 | |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2993 | // alignment-specifier |
| 2994 | case tok::kw__Alignas: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2995 | if (!getLangOpts().C11) |
Jordan Rose | 58d5472 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2996 | Diag(Tok, diag::ext_c11_alignment) << Tok.getName(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2997 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 2998 | continue; |
| 2999 | |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 3000 | // friend |
| 3001 | case tok::kw_friend: |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3002 | if (DSContext == DSC_class) |
| 3003 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 3004 | else { |
| 3005 | PrevSpec = ""; // not actually used by the diagnostic |
| 3006 | DiagID = diag::err_friend_invalid_in_context; |
| 3007 | isInvalid = true; |
| 3008 | } |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 3009 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3010 | |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 3011 | // Modules |
| 3012 | case tok::kw___module_private__: |
| 3013 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 3014 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3015 | |
Sebastian Redl | 39c2a8b | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 3016 | // constexpr |
| 3017 | case tok::kw_constexpr: |
| 3018 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 3019 | break; |
| 3020 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3021 | // type-specifier |
| 3022 | case tok::kw_short: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3023 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3024 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3025 | break; |
| 3026 | case tok::kw_long: |
| 3027 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3028 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3029 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3030 | else |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3031 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3032 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3033 | break; |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3034 | case tok::kw___int64: |
| 3035 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3036 | DiagID, Policy); |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3037 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3038 | case tok::kw_signed: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3039 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 3040 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3041 | break; |
| 3042 | case tok::kw_unsigned: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3043 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 3044 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3045 | break; |
| 3046 | case tok::kw__Complex: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3047 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 3048 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3049 | break; |
| 3050 | case tok::kw__Imaginary: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3051 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 3052 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3053 | break; |
| 3054 | case tok::kw_void: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3055 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3056 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3057 | break; |
| 3058 | case tok::kw_char: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3059 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3060 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3061 | break; |
| 3062 | case tok::kw_int: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3063 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3064 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3065 | break; |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3066 | case tok::kw___int128: |
| 3067 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3068 | DiagID, Policy); |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3069 | break; |
| 3070 | case tok::kw_half: |
| 3071 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3072 | DiagID, Policy); |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3073 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3074 | case tok::kw_float: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3075 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3076 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3077 | break; |
| 3078 | case tok::kw_double: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3079 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3080 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3081 | break; |
| 3082 | case tok::kw_wchar_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3083 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3084 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3085 | break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3086 | case tok::kw_char16_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3087 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3088 | DiagID, Policy); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3089 | break; |
| 3090 | case tok::kw_char32_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3091 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3092 | DiagID, Policy); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3093 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3094 | case tok::kw_bool: |
| 3095 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 20ee5ae | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3096 | if (Tok.is(tok::kw_bool) && |
| 3097 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 3098 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 3099 | PrevSpec = ""; // Not used by the diagnostic. |
| 3100 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | 2b05999 | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3101 | // For better error recovery. |
| 3102 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 20ee5ae | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3103 | isInvalid = true; |
| 3104 | } else { |
| 3105 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3106 | DiagID, Policy); |
Argyrios Kyrtzidis | 20ee5ae | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3107 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3108 | break; |
| 3109 | case tok::kw__Decimal32: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3110 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3111 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3112 | break; |
| 3113 | case tok::kw__Decimal64: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3114 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3115 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3116 | break; |
| 3117 | case tok::kw__Decimal128: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3118 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3119 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3120 | break; |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3121 | case tok::kw___vector: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3122 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy); |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3123 | break; |
| 3124 | case tok::kw___pixel: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3125 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy); |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3126 | break; |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3127 | case tok::kw___unknown_anytype: |
| 3128 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3129 | PrevSpec, DiagID, Policy); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3130 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3131 | |
| 3132 | // class-specifier: |
| 3133 | case tok::kw_class: |
| 3134 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3135 | case tok::kw___interface: |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3136 | case tok::kw_union: { |
| 3137 | tok::TokenKind Kind = Tok.getKind(); |
| 3138 | ConsumeToken(); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3139 | |
| 3140 | // These are attributes following class specifiers. |
| 3141 | // To produce better diagnostic, we parse them when |
| 3142 | // parsing class specifier. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3143 | ParsedAttributesWithRange Attributes(AttrFactory); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3144 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3145 | EnteringContext, DSContext, Attributes); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3146 | |
| 3147 | // If there are attributes following class specifier, |
| 3148 | // take them over and handle them here. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3149 | if (!Attributes.empty()) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3150 | AttrsLastTime = true; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3151 | attrs.takeAllFrom(Attributes); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3152 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3153 | continue; |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3154 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3155 | |
| 3156 | // enum-specifier: |
| 3157 | case tok::kw_enum: |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3158 | ConsumeToken(); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3159 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3160 | continue; |
| 3161 | |
| 3162 | // cv-qualifier: |
| 3163 | case tok::kw_const: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3164 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3165 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3166 | break; |
| 3167 | case tok::kw_volatile: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3168 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3169 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3170 | break; |
| 3171 | case tok::kw_restrict: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3172 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3173 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3174 | break; |
| 3175 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3176 | // C++ typename-specifier: |
| 3177 | case tok::kw_typename: |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3178 | if (TryAnnotateTypeOrScopeToken()) { |
| 3179 | DS.SetTypeSpecError(); |
| 3180 | goto DoneWithDeclSpec; |
| 3181 | } |
| 3182 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3183 | continue; |
| 3184 | break; |
| 3185 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3186 | // GNU typeof support. |
| 3187 | case tok::kw_typeof: |
| 3188 | ParseTypeofSpecifier(DS); |
| 3189 | continue; |
| 3190 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3191 | case tok::annot_decltype: |
Anders Carlsson | 74948d0 | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 3192 | ParseDecltypeSpecifier(DS); |
| 3193 | continue; |
| 3194 | |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3195 | case tok::kw___underlying_type: |
| 3196 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3197 | continue; |
| 3198 | |
| 3199 | case tok::kw__Atomic: |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3200 | // C11 6.7.2.4/4: |
| 3201 | // If the _Atomic keyword is immediately followed by a left parenthesis, |
| 3202 | // it is interpreted as a type specifier (with a type name), not as a |
| 3203 | // type qualifier. |
| 3204 | if (NextToken().is(tok::l_paren)) { |
| 3205 | ParseAtomicSpecifier(DS); |
| 3206 | continue; |
| 3207 | } |
| 3208 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 3209 | getLangOpts()); |
| 3210 | break; |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3211 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3212 | // OpenCL qualifiers: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3213 | case tok::kw___private: |
| 3214 | case tok::kw___global: |
| 3215 | case tok::kw___local: |
| 3216 | case tok::kw___constant: |
| 3217 | case tok::kw___read_only: |
| 3218 | case tok::kw___write_only: |
| 3219 | case tok::kw___read_write: |
Aaron Ballman | 05d76ea | 2014-01-14 01:29:54 +0000 | [diff] [blame] | 3220 | ParseOpenCLQualifiers(DS.getAttributes()); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3221 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3222 | |
Steve Naroff | cfdf616 | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 3223 | case tok::less: |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3224 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3225 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 3226 | // but we support it. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3227 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3228 | goto DoneWithDeclSpec; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3229 | |
Douglas Gregor | 3a001f4 | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 3230 | if (!ParseObjCProtocolQualifiers(DS)) |
| 3231 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 3232 | << FixItHint::CreateInsertion(Loc, "id") |
| 3233 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3234 | |
Douglas Gregor | 06e41ae | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 3235 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 3236 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 3237 | continue; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 3238 | } |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3239 | // If the specifier wasn't legal, issue a diagnostic. |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3240 | if (isInvalid) { |
| 3241 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3242 | assert(DiagID); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3243 | |
Douglas Gregor | a05f5ab | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 3244 | if (DiagID == diag::ext_duplicate_declspec) |
| 3245 | Diag(Tok, DiagID) |
| 3246 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 3247 | else |
| 3248 | Diag(Tok, DiagID) << PrevSpec; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3249 | } |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3250 | |
Chris Lattner | 2e23209 | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 3251 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | 2b05999 | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3252 | if (DiagID != diag::err_bool_redeclaration) |
| 3253 | ConsumeToken(); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3254 | |
| 3255 | AttrsLastTime = false; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 3256 | } |
| 3257 | } |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 3258 | |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3259 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 3260 | /// semicolon. |
| 3261 | /// |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3262 | /// struct-declaration: |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3263 | /// specifier-qualifier-list struct-declarator-list |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 3264 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3265 | /// [GNU] specifier-qualifier-list |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3266 | /// struct-declarator-list: |
| 3267 | /// struct-declarator |
| 3268 | /// struct-declarator-list ',' struct-declarator |
| 3269 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 3270 | /// struct-declarator: |
| 3271 | /// declarator |
| 3272 | /// [GNU] declarator attributes[opt] |
| 3273 | /// declarator[opt] ':' constant-expression |
| 3274 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 3275 | /// |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 3276 | void Parser::ParseStructDeclaration( |
| 3277 | ParsingDeclSpec &DS, |
| 3278 | llvm::function_ref<void(ParsingFieldDeclarator &)> FieldsCallback) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3279 | |
Chris Lattner | f02ef3e | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3280 | if (Tok.is(tok::kw___extension__)) { |
| 3281 | // __extension__ silences extension warnings in the subexpression. |
| 3282 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3283 | ConsumeToken(); |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 3284 | return ParseStructDeclaration(DS, FieldsCallback); |
Chris Lattner | f02ef3e | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3285 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3286 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3287 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3288 | ParseSpecifierQualifierList(DS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3289 | |
Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3290 | // If there are no declarators, this is a free-standing declaration |
| 3291 | // specifier. Let the actions module cope with it. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3292 | if (Tok.is(tok::semi)) { |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3293 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
| 3294 | DS); |
| 3295 | DS.complete(TheDecl); |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3296 | return; |
| 3297 | } |
| 3298 | |
| 3299 | // Read struct-declarators until we find the semicolon. |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3300 | bool FirstDeclarator = true; |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3301 | SourceLocation CommaLoc; |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3302 | while (1) { |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3303 | ParsingFieldDeclarator DeclaratorInfo(*this, DS); |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3304 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3305 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3306 | // Attributes are only allowed here on successive declarators. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3307 | if (!FirstDeclarator) |
| 3308 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3309 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3310 | /// struct-declarator: declarator |
| 3311 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | 17c3b1f | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3312 | if (Tok.isNot(tok::colon)) { |
| 3313 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 3314 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3315 | ParseDeclarator(DeclaratorInfo.D); |
Richard Smith | 3d1a94c | 2014-08-12 00:22:39 +0000 | [diff] [blame] | 3316 | } else |
| 3317 | DeclaratorInfo.D.SetIdentifier(nullptr, Tok.getLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3318 | |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 3319 | if (TryConsumeToken(tok::colon)) { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3320 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3321 | if (Res.isInvalid()) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3322 | SkipUntil(tok::semi, StopBeforeMatch); |
Chris Lattner | 32295d3 | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 3323 | else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3324 | DeclaratorInfo.BitfieldSize = Res.get(); |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3325 | } |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3326 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3327 | // If attributes exist after the declarator, parse them. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3328 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3329 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3330 | // We're done with this declarator; invoke the callback. |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 3331 | FieldsCallback(DeclaratorInfo); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3332 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3333 | // If we don't have a comma, it is either the end of the list (a ';') |
| 3334 | // or an error, bail out. |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 3335 | if (!TryConsumeToken(tok::comma, CommaLoc)) |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3336 | return; |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3337 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3338 | FirstDeclarator = false; |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3339 | } |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
| 3342 | /// ParseStructUnionBody |
| 3343 | /// struct-contents: |
| 3344 | /// struct-declaration-list |
| 3345 | /// [EXT] empty |
| 3346 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 3347 | /// struct-declaration-list: |
| 3348 | /// struct-declaration |
| 3349 | /// struct-declaration-list struct-declaration |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3350 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3351 | /// |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 3352 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3353 | unsigned TagType, Decl *TagDecl) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3354 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 3355 | "parsing struct/union body"); |
Andy Gibbs | 22e140b | 2013-04-03 09:31:19 +0000 | [diff] [blame] | 3356 | assert(!getLangOpts().CPlusPlus && "C++ declarations not supported"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3357 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3358 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3359 | if (T.consumeOpen()) |
| 3360 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3361 | |
Douglas Gregor | 658b955 | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 3362 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3363 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3364 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3365 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3366 | |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 3367 | // While we still have something to read, read the declarations in the struct. |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 3368 | while (Tok.isNot(tok::r_brace) && !isEofOrEom()) { |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3369 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3370 | |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 3371 | // Check for extraneous top-level semicolon. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3372 | if (Tok.is(tok::semi)) { |
Richard Smith | 87f5dc5 | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3373 | ConsumeExtraSemi(InsideStruct, TagType); |
Chris Lattner | 36e46a2 | 2007-06-09 05:49:55 +0000 | [diff] [blame] | 3374 | continue; |
| 3375 | } |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3376 | |
Andy Gibbs | c804e08 | 2013-04-03 09:46:04 +0000 | [diff] [blame] | 3377 | // Parse _Static_assert declaration. |
| 3378 | if (Tok.is(tok::kw__Static_assert)) { |
| 3379 | SourceLocation DeclEnd; |
| 3380 | ParseStaticAssertDeclaration(DeclEnd); |
| 3381 | continue; |
| 3382 | } |
| 3383 | |
Argyrios Kyrtzidis | 71c12fb | 2013-04-18 01:42:35 +0000 | [diff] [blame] | 3384 | if (Tok.is(tok::annot_pragma_pack)) { |
| 3385 | HandlePragmaPack(); |
| 3386 | continue; |
| 3387 | } |
| 3388 | |
| 3389 | if (Tok.is(tok::annot_pragma_align)) { |
| 3390 | HandlePragmaAlign(); |
| 3391 | continue; |
| 3392 | } |
| 3393 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3394 | if (!Tok.is(tok::at)) { |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 3395 | auto CFieldCallback = [&](ParsingFieldDeclarator &FD) { |
| 3396 | // Install the declarator into the current TagDecl. |
| 3397 | Decl *Field = |
| 3398 | Actions.ActOnField(getCurScope(), TagDecl, |
| 3399 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 3400 | FD.D, FD.BitfieldSize); |
| 3401 | FieldDecls.push_back(Field); |
| 3402 | FD.complete(Field); |
| 3403 | }; |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3404 | |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3405 | // Parse all the comma separated declarators. |
| 3406 | ParsingDeclSpec DS(*this); |
Benjamin Kramer | a39beb9 | 2014-09-03 11:06:10 +0000 | [diff] [blame] | 3407 | ParseStructDeclaration(DS, CFieldCallback); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3408 | } else { // Handle @defs |
| 3409 | ConsumeToken(); |
| 3410 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 3411 | Diag(Tok, diag::err_unexpected_at); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3412 | SkipUntil(tok::semi); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3413 | continue; |
| 3414 | } |
| 3415 | ConsumeToken(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 3416 | ExpectAndConsume(tok::l_paren); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3417 | if (!Tok.is(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 3418 | Diag(Tok, diag::err_expected) << tok::identifier; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3419 | SkipUntil(tok::semi); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3420 | continue; |
| 3421 | } |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3422 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3423 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3424 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3425 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 3426 | ConsumeToken(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 3427 | ExpectAndConsume(tok::r_paren); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3428 | } |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 3429 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 3430 | if (TryConsumeToken(tok::semi)) |
| 3431 | continue; |
| 3432 | |
| 3433 | if (Tok.is(tok::r_brace)) { |
Chris Lattner | 245c533 | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3434 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Chris Lattner | 0c7e82d | 2007-06-09 05:54:40 +0000 | [diff] [blame] | 3435 | break; |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3436 | } |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 3437 | |
| 3438 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 3439 | // Skip to end of block or statement to avoid ext-warning on extra ';'. |
| 3440 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
| 3441 | // If we stopped at a ';', eat it. |
| 3442 | TryConsumeToken(tok::semi); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3443 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3444 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3445 | T.consumeClose(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3446 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3447 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3448 | // If attributes exist after struct contents, parse them. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3449 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 15619c7 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 3450 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3451 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 751c558 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 3452 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3453 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3454 | attrs.getList()); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3455 | StructScope.Exit(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3456 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 3457 | T.getCloseLocation()); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3458 | } |
| 3459 | |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3460 | /// ParseEnumSpecifier |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 3461 | /// enum-specifier: [C99 6.7.2.2] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3462 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3463 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 3464 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3465 | /// '}' attributes[opt] |
Aaron Ballman | 9ecff02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 3466 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3467 | /// '}' |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3468 | /// 'enum' identifier |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 3469 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3470 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3471 | /// [C++11] enum-head '{' enumerator-list[opt] '}' |
| 3472 | /// [C++11] enum-head '{' enumerator-list ',' '}' |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3473 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3474 | /// enum-head: [C++11] |
| 3475 | /// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt] |
| 3476 | /// enum-key attribute-specifier-seq[opt] nested-name-specifier |
| 3477 | /// identifier enum-base[opt] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3478 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3479 | /// enum-key: [C++11] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3480 | /// 'enum' |
| 3481 | /// 'enum' 'class' |
| 3482 | /// 'enum' 'struct' |
| 3483 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3484 | /// enum-base: [C++11] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3485 | /// ':' type-specifier-seq |
| 3486 | /// |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3487 | /// [C++] elaborated-type-specifier: |
| 3488 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 3489 | /// |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3490 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | dc70c3a | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 3491 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3492 | AccessSpecifier AS, DeclSpecContext DSC) { |
Chris Lattner | ffbc271 | 2007-01-25 06:05:38 +0000 | [diff] [blame] | 3493 | // Parse the tag portion of this. |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3494 | if (Tok.is(tok::code_completion)) { |
| 3495 | // Code completion for an enum name. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3496 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3497 | return cutOffParsing(); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3498 | } |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3499 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3500 | // If attributes exist after tag, parse them. |
| 3501 | ParsedAttributesWithRange attrs(AttrFactory); |
| 3502 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3503 | MaybeParseCXX11Attributes(attrs); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3504 | |
| 3505 | // If declspecs exist after tag, parse them. |
| 3506 | while (Tok.is(tok::kw___declspec)) |
| 3507 | ParseMicrosoftDeclSpec(attrs); |
| 3508 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3509 | SourceLocation ScopedEnumKWLoc; |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3510 | bool IsScopedUsingClassTag = false; |
| 3511 | |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3512 | // In C++11, recognize 'enum class' and 'enum struct'. |
Richard Trieu | d0d87b5 | 2013-04-23 02:47:36 +0000 | [diff] [blame] | 3513 | if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) { |
| 3514 | Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum |
| 3515 | : diag::ext_scoped_enum); |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3516 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3517 | ScopedEnumKWLoc = ConsumeToken(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3518 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3519 | // Attributes are not allowed between these keywords. Diagnose, |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3520 | // but then just treat them like they appeared in the right place. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3521 | ProhibitAttributes(attrs); |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3522 | |
| 3523 | // They are allowed afterwards, though. |
| 3524 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3525 | MaybeParseCXX11Attributes(attrs); |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3526 | while (Tok.is(tok::kw___declspec)) |
| 3527 | ParseMicrosoftDeclSpec(attrs); |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3528 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3529 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3530 | // C++11 [temp.explicit]p12: |
| 3531 | // The usual access controls do not apply to names used to specify |
| 3532 | // explicit instantiations. |
| 3533 | // We extend this to also cover explicit specializations. Note that |
| 3534 | // we don't suppress if this turns out to be an elaborated type |
| 3535 | // specifier. |
| 3536 | bool shouldDelayDiagsInTag = |
| 3537 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 3538 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 3539 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3540 | |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3541 | // Enum definitions should not be parsed in a trailing-return-type. |
| 3542 | bool AllowDeclaration = DSC != DSC_trailing; |
| 3543 | |
| 3544 | bool AllowFixedUnderlyingType = AllowDeclaration && |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3545 | (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt || |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3546 | getLangOpts().ObjC2); |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3547 | |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3548 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3549 | if (getLangOpts().CPlusPlus) { |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3550 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 3551 | // if a fixed underlying type is allowed. |
| 3552 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3553 | |
| 3554 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Richard Smith | 1d4b2e1 | 2013-04-01 21:43:41 +0000 | [diff] [blame] | 3555 | /*EnteringContext=*/true)) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3556 | return; |
| 3557 | |
| 3558 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 3559 | Diag(Tok, diag::err_expected) << tok::identifier; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3560 | if (Tok.isNot(tok::l_brace)) { |
| 3561 | // Has no name and is not a definition. |
| 3562 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3563 | SkipUntil(tok::comma, StopAtSemi); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3564 | return; |
| 3565 | } |
| 3566 | } |
| 3567 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3568 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3569 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3570 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3571 | !(AllowFixedUnderlyingType && Tok.is(tok::colon))) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 3572 | Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3573 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3574 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3575 | SkipUntil(tok::comma, StopAtSemi); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3576 | return; |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3577 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3578 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3579 | // If an identifier is present, consume and remember it. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 3580 | IdentifierInfo *Name = nullptr; |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3581 | SourceLocation NameLoc; |
| 3582 | if (Tok.is(tok::identifier)) { |
| 3583 | Name = Tok.getIdentifierInfo(); |
| 3584 | NameLoc = ConsumeToken(); |
| 3585 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3586 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3587 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3588 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 3589 | // declaration of a scoped enumeration. |
| 3590 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3591 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3592 | IsScopedUsingClassTag = false; |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3593 | } |
| 3594 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3595 | // Okay, end the suppression area. We'll decide whether to emit the |
| 3596 | // diagnostics in a second. |
| 3597 | if (shouldDelayDiagsInTag) |
| 3598 | diagsFromTag.done(); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3599 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3600 | TypeResult BaseType; |
| 3601 | |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3602 | // Parse the fixed underlying type. |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3603 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3604 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3605 | bool PossibleBitfield = false; |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3606 | if (CanBeBitfield) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3607 | // If we're in class scope, this can either be an enum declaration with |
| 3608 | // an underlying type, or a declaration of a bitfield member. We try to |
| 3609 | // use a simple disambiguation scheme first to catch the common cases |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3610 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 3611 | // anything that's a simple-type-specifier followed by '(' as an |
| 3612 | // expression. This suffices because function types are not valid |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3613 | // underlying types anyway. |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 3614 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 3615 | Sema::ConstantEvaluated); |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3616 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3617 | // 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] | 3618 | // bit-field. This is the common case. |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 3619 | if (TPR == TPResult::True) |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3620 | PossibleBitfield = true; |
| 3621 | // If the next token starts a type-specifier-seq, it may be either a |
| 3622 | // 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] | 3623 | // 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] | 3624 | // fixed underlying type. |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 3625 | else if (TPR == TPResult::False && |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3626 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 3627 | // Consume the ':'. |
| 3628 | ConsumeToken(); |
| 3629 | } else { |
| 3630 | // We have the start of a type-specifier-seq, so we have to perform |
| 3631 | // tentative parsing to determine whether we have an expression or a |
| 3632 | // type. |
| 3633 | TentativeParsingAction TPA(*this); |
| 3634 | |
| 3635 | // Consume the ':'. |
| 3636 | ConsumeToken(); |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3637 | |
| 3638 | // If we see a type specifier followed by an open-brace, we have an |
| 3639 | // ambiguity between an underlying type and a C++11 braced |
| 3640 | // function-style cast. Resolve this by always treating it as an |
| 3641 | // underlying type. |
| 3642 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 3643 | // this case. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3644 | if ((getLangOpts().CPlusPlus && |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 3645 | isCXXDeclarationSpecifier(TPResult::True) != TPResult::True) || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3646 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3647 | // We'll parse this as a bitfield later. |
| 3648 | PossibleBitfield = true; |
| 3649 | TPA.Revert(); |
| 3650 | } else { |
| 3651 | // We have a type-specifier-seq. |
| 3652 | TPA.Commit(); |
| 3653 | } |
| 3654 | } |
| 3655 | } else { |
| 3656 | // Consume the ':'. |
| 3657 | ConsumeToken(); |
| 3658 | } |
| 3659 | |
| 3660 | if (!PossibleBitfield) { |
| 3661 | SourceRange Range; |
| 3662 | BaseType = ParseTypeName(&Range); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3663 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3664 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3665 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Eli Friedman | 0d0355ab | 2012-11-02 01:34:28 +0000 | [diff] [blame] | 3666 | } else if (!getLangOpts().ObjC2) { |
| 3667 | if (getLangOpts().CPlusPlus) |
| 3668 | Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range; |
| 3669 | else |
| 3670 | Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range; |
| 3671 | } |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3672 | } |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3673 | } |
| 3674 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3675 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 3676 | // friend declaration, and cannot have an accompanying definition. If we have |
| 3677 | // 'enum foo;', then this is a forward declaration. If we have |
| 3678 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 3679 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3680 | // |
| 3681 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 3682 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 3683 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 3684 | // |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3685 | Sema::TagUseKind TUK; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3686 | if (!AllowDeclaration) { |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3687 | TUK = Sema::TUK_Reference; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3688 | } else if (Tok.is(tok::l_brace)) { |
| 3689 | if (DS.isFriendSpecified()) { |
| 3690 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
| 3691 | << SourceRange(DS.getFriendSpecLoc()); |
| 3692 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3693 | SkipUntil(tok::r_brace, StopAtSemi); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3694 | TUK = Sema::TUK_Friend; |
| 3695 | } else { |
| 3696 | TUK = Sema::TUK_Definition; |
| 3697 | } |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 3698 | } else if (!isTypeSpecifier(DSC) && |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3699 | (Tok.is(tok::semi) || |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3700 | (Tok.isAtStartOfLine() && |
| 3701 | !isValidAfterTypeSpecifier(CanBeBitfield)))) { |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3702 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
| 3703 | if (Tok.isNot(tok::semi)) { |
| 3704 | // A semicolon was missing after this declaration. Diagnose and recover. |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 3705 | ExpectAndConsume(tok::semi, diag::err_expected_after, "enum"); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3706 | PP.EnterToken(Tok); |
| 3707 | Tok.setKind(tok::semi); |
| 3708 | } |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3709 | } else { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3710 | TUK = Sema::TUK_Reference; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3711 | } |
| 3712 | |
| 3713 | // If this is an elaborated type specifier, and we delayed |
| 3714 | // diagnostics before, just merge them into the current pool. |
| 3715 | if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) { |
| 3716 | diagsFromTag.redelay(); |
| 3717 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3718 | |
| 3719 | MultiTemplateParamsArg TParams; |
Douglas Gregor | cbbf3e3 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3720 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3721 | TUK != Sema::TUK_Reference) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3722 | if (!getLangOpts().CPlusPlus11 || !SS.isSet()) { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3723 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3724 | Diag(Tok, diag::err_enum_template); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3725 | SkipUntil(tok::comma, StopAtSemi); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3726 | return; |
| 3727 | } |
| 3728 | |
| 3729 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 3730 | // Enumerations can't be explicitly instantiated. |
| 3731 | DS.SetTypeSpecError(); |
| 3732 | Diag(StartLoc, diag::err_explicit_instantiation_enum); |
| 3733 | return; |
| 3734 | } |
| 3735 | |
| 3736 | assert(TemplateInfo.TemplateParams && "no template parameters"); |
| 3737 | TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(), |
| 3738 | TemplateInfo.TemplateParams->size()); |
Douglas Gregor | cbbf3e3 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3739 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3740 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3741 | if (TUK == Sema::TUK_Reference) |
| 3742 | ProhibitAttributes(attrs); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3743 | |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3744 | if (!Name && TUK != Sema::TUK_Definition) { |
| 3745 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3746 | |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3747 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3748 | SkipUntil(tok::comma, StopAtSemi); |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3749 | return; |
| 3750 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3751 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3752 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3753 | bool IsDependent = false; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 3754 | const char *PrevSpec = nullptr; |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3755 | unsigned DiagID; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3756 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3757 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3758 | AS, DS.getModulePrivateSpecLoc(), TParams, |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3759 | Owned, IsDependent, ScopedEnumKWLoc, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 3760 | IsScopedUsingClassTag, BaseType, |
| 3761 | DSC == DSC_type_specifier); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3762 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3763 | if (IsDependent) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3764 | // This enum has a dependent nested-name-specifier. Handle it as a |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3765 | // dependent tag. |
| 3766 | if (!Name) { |
| 3767 | DS.SetTypeSpecError(); |
| 3768 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 3769 | return; |
| 3770 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3771 | |
Nico Weber | 83ea012 | 2014-05-03 21:57:40 +0000 | [diff] [blame] | 3772 | TypeResult Type = Actions.ActOnDependentTag( |
| 3773 | getCurScope(), DeclSpec::TST_enum, TUK, SS, Name, StartLoc, NameLoc); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3774 | if (Type.isInvalid()) { |
| 3775 | DS.SetTypeSpecError(); |
| 3776 | return; |
| 3777 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3778 | |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3779 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 3780 | NameLoc.isValid() ? NameLoc : StartLoc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3781 | PrevSpec, DiagID, Type.get(), |
| 3782 | Actions.getASTContext().getPrintingPolicy())) |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3783 | Diag(StartLoc, DiagID) << PrevSpec; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3784 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3785 | return; |
| 3786 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3787 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3788 | if (!TagDecl) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3789 | // The action failed to produce an enumeration tag. If this is a |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3790 | // definition, consume the entire definition. |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3791 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3792 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3793 | SkipUntil(tok::r_brace, StopAtSemi); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3794 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3795 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3796 | DS.SetTypeSpecError(); |
| 3797 | return; |
| 3798 | } |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3799 | |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3800 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3801 | ParseEnumBody(StartLoc, TagDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3802 | |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3803 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 3804 | NameLoc.isValid() ? NameLoc : StartLoc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3805 | PrevSpec, DiagID, TagDecl, Owned, |
| 3806 | Actions.getASTContext().getPrintingPolicy())) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3807 | Diag(StartLoc, DiagID) << PrevSpec; |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3808 | } |
| 3809 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3810 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 3811 | /// enumerator-list: |
| 3812 | /// enumerator |
| 3813 | /// enumerator-list ',' enumerator |
| 3814 | /// enumerator: |
Aaron Ballman | 730476b | 2014-11-08 15:33:35 +0000 | [diff] [blame^] | 3815 | /// enumeration-constant attributes[opt] |
| 3816 | /// enumeration-constant attributes[opt] '=' constant-expression |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3817 | /// enumeration-constant: |
| 3818 | /// identifier |
| 3819 | /// |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3820 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3821 | // Enter the scope of the enum body and start the definition. |
Hans Wennborg | fe78145 | 2014-06-17 00:00:18 +0000 | [diff] [blame] | 3822 | ParseScope EnumScope(this, Scope::DeclScope | Scope::EnumScope); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3823 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3824 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3825 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3826 | T.consumeOpen(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3827 | |
Chris Lattner | 37256fb | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 3828 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3829 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 6e81492 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 3830 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3831 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3832 | SmallVector<Decl *, 32> EnumConstantDecls; |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3833 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 3834 | Decl *LastEnumConstDecl = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3835 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3836 | // Parse the enumerator-list. |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 3837 | while (Tok.isNot(tok::r_brace)) { |
| 3838 | // Parse enumerator. If failed, try skipping till the start of the next |
| 3839 | // enumerator definition. |
| 3840 | if (Tok.isNot(tok::identifier)) { |
| 3841 | Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; |
| 3842 | if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) && |
| 3843 | TryConsumeToken(tok::comma)) |
| 3844 | continue; |
| 3845 | break; |
| 3846 | } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3847 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 3848 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3849 | |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3850 | // If attributes exist after the enumerator, parse them. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3851 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3852 | MaybeParseGNUAttributes(attrs); |
Aaron Ballman | 730476b | 2014-11-08 15:33:35 +0000 | [diff] [blame^] | 3853 | ProhibitAttributes(attrs); // GNU-style attributes are prohibited. |
| 3854 | if (getLangOpts().CPlusPlus11 && isCXX11AttributeSpecifier()) { |
| 3855 | if (!getLangOpts().CPlusPlus1z) |
| 3856 | Diag(Tok.getLocation(), diag::warn_cxx14_compat_attribute) |
| 3857 | << "enumerator"; |
| 3858 | ParseCXX11Attributes(attrs); |
| 3859 | } |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3860 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3861 | SourceLocation EqualLoc; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3862 | ExprResult AssignedVal; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 3863 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3864 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 3865 | if (TryConsumeToken(tok::equal, EqualLoc)) { |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3866 | AssignedVal = ParseConstantExpression(); |
| 3867 | if (AssignedVal.isInvalid()) |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 3868 | SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch); |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3869 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3870 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3871 | // Install the enumerator constant into EnumDecl. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3872 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 3873 | LastEnumConstDecl, |
| 3874 | IdentLoc, Ident, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3875 | attrs.getList(), EqualLoc, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3876 | AssignedVal.get()); |
Fariborz Jahanian | 329b351 | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3877 | PD.complete(EnumConstDecl); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3878 | |
Chris Lattner | 4ef4001 | 2007-06-11 01:28:17 +0000 | [diff] [blame] | 3879 | EnumConstantDecls.push_back(EnumConstDecl); |
| 3880 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3881 | |
Douglas Gregor | ce66d02 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3882 | if (Tok.is(tok::identifier)) { |
| 3883 | // We're missing a comma between enumerators. |
| 3884 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3885 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
Douglas Gregor | ce66d02 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3886 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3887 | continue; |
| 3888 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3889 | |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 3890 | // Emumerator definition must be finished, only comma or r_brace are |
| 3891 | // allowed here. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 3892 | SourceLocation CommaLoc; |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 3893 | if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) { |
| 3894 | if (EqualLoc.isValid()) |
| 3895 | Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace |
| 3896 | << tok::comma; |
| 3897 | else |
| 3898 | Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator); |
| 3899 | if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) { |
| 3900 | if (TryConsumeToken(tok::comma, CommaLoc)) |
| 3901 | continue; |
| 3902 | } else { |
| 3903 | break; |
| 3904 | } |
| 3905 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3906 | |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 3907 | // If comma is followed by r_brace, emit appropriate warning. |
| 3908 | if (Tok.is(tok::r_brace) && CommaLoc.isValid()) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3909 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11) |
Richard Smith | 87f5dc5 | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3910 | Diag(CommaLoc, getLangOpts().CPlusPlus ? |
| 3911 | diag::ext_enumerator_list_comma_cxx : |
| 3912 | diag::ext_enumerator_list_comma_c) |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3913 | << FixItHint::CreateRemoval(CommaLoc); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3914 | else if (getLangOpts().CPlusPlus11) |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3915 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 3916 | << FixItHint::CreateRemoval(CommaLoc); |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 3917 | break; |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3918 | } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3919 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3920 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3921 | // Eat the }. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3922 | T.consumeClose(); |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3923 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3924 | // If attributes exist after the identifier list, parse them. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3925 | ParsedAttributes attrs(AttrFactory); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3926 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3927 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3928 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 3929 | EnumDecl, EnumConstantDecls, |
| 3930 | getCurScope(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3931 | attrs.getList()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3932 | |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3933 | EnumScope.Exit(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3934 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 3935 | T.getCloseLocation()); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3936 | |
| 3937 | // The next token must be valid after an enum definition. If not, a ';' |
| 3938 | // was probably forgotten. |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3939 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
| 3940 | if (!isValidAfterTypeSpecifier(CanBeBitfield)) { |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 3941 | ExpectAndConsume(tok::semi, diag::err_expected_after, "enum"); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3942 | // Push this token back into the preprocessor and change our current token |
| 3943 | // to ';' so that the rest of the code recovers as though there were an |
| 3944 | // ';' after the definition. |
| 3945 | PP.EnterToken(Tok); |
| 3946 | Tok.setKind(tok::semi); |
| 3947 | } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3948 | } |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3949 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 3950 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3951 | /// start of a type-qualifier-list. |
| 3952 | bool Parser::isTypeQualifier() const { |
| 3953 | switch (Tok.getKind()) { |
| 3954 | default: return false; |
Alp Toker | de50ff3 | 2013-12-17 18:17:46 +0000 | [diff] [blame] | 3955 | // type-qualifier |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3956 | case tok::kw_const: |
| 3957 | case tok::kw_volatile: |
| 3958 | case tok::kw_restrict: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3959 | case tok::kw___private: |
| 3960 | case tok::kw___local: |
| 3961 | case tok::kw___global: |
| 3962 | case tok::kw___constant: |
| 3963 | case tok::kw___read_only: |
| 3964 | case tok::kw___read_write: |
| 3965 | case tok::kw___write_only: |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3966 | return true; |
| 3967 | } |
| 3968 | } |
| 3969 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3970 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 3971 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 3972 | /// specifier or if we're not sure. |
| 3973 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 3974 | switch (Tok.getKind()) { |
| 3975 | default: return false; |
| 3976 | // type-specifiers |
| 3977 | case tok::kw_short: |
| 3978 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3979 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3980 | case tok::kw___int128: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3981 | case tok::kw_signed: |
| 3982 | case tok::kw_unsigned: |
| 3983 | case tok::kw__Complex: |
| 3984 | case tok::kw__Imaginary: |
| 3985 | case tok::kw_void: |
| 3986 | case tok::kw_char: |
| 3987 | case tok::kw_wchar_t: |
| 3988 | case tok::kw_char16_t: |
| 3989 | case tok::kw_char32_t: |
| 3990 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3991 | case tok::kw_half: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3992 | case tok::kw_float: |
| 3993 | case tok::kw_double: |
| 3994 | case tok::kw_bool: |
| 3995 | case tok::kw__Bool: |
| 3996 | case tok::kw__Decimal32: |
| 3997 | case tok::kw__Decimal64: |
| 3998 | case tok::kw__Decimal128: |
| 3999 | case tok::kw___vector: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4000 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4001 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4002 | case tok::kw_class: |
| 4003 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4004 | case tok::kw___interface: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4005 | case tok::kw_union: |
| 4006 | // enum-specifier |
| 4007 | case tok::kw_enum: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4008 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4009 | // typedef-name |
| 4010 | case tok::annot_typename: |
| 4011 | return true; |
| 4012 | } |
| 4013 | } |
| 4014 | |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 4015 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4016 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4017 | bool Parser::isTypeSpecifierQualifier() { |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4018 | switch (Tok.getKind()) { |
| 4019 | default: return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4020 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4021 | case tok::identifier: // foo::bar |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4022 | if (TryAltiVecVectorToken()) |
| 4023 | return true; |
| 4024 | // Fall through. |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4025 | case tok::kw_typename: // typename T::type |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4026 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4027 | // recurse to handle whatever we get. |
| 4028 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4029 | return true; |
| 4030 | if (Tok.is(tok::identifier)) |
| 4031 | return false; |
| 4032 | return isTypeSpecifierQualifier(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4033 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4034 | case tok::coloncolon: // ::foo::bar |
| 4035 | if (NextToken().is(tok::kw_new) || // ::new |
| 4036 | NextToken().is(tok::kw_delete)) // ::delete |
| 4037 | return false; |
| 4038 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4039 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4040 | return true; |
| 4041 | return isTypeSpecifierQualifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4042 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 4043 | // GNU attributes support. |
| 4044 | case tok::kw___attribute: |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4045 | // GNU typeof support. |
| 4046 | case tok::kw_typeof: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4047 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4048 | // type-specifiers |
| 4049 | case tok::kw_short: |
| 4050 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4051 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4052 | case tok::kw___int128: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4053 | case tok::kw_signed: |
| 4054 | case tok::kw_unsigned: |
| 4055 | case tok::kw__Complex: |
| 4056 | case tok::kw__Imaginary: |
| 4057 | case tok::kw_void: |
| 4058 | case tok::kw_char: |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4059 | case tok::kw_wchar_t: |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4060 | case tok::kw_char16_t: |
| 4061 | case tok::kw_char32_t: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4062 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4063 | case tok::kw_half: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4064 | case tok::kw_float: |
| 4065 | case tok::kw_double: |
Chris Lattner | bb31a42 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 4066 | case tok::kw_bool: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4067 | case tok::kw__Bool: |
| 4068 | case tok::kw__Decimal32: |
| 4069 | case tok::kw__Decimal64: |
| 4070 | case tok::kw__Decimal128: |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4071 | case tok::kw___vector: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4072 | |
Chris Lattner | 861a226 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 4073 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4074 | case tok::kw_class: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4075 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4076 | case tok::kw___interface: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4077 | case tok::kw_union: |
| 4078 | // enum-specifier |
| 4079 | case tok::kw_enum: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4080 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4081 | // type-qualifier |
| 4082 | case tok::kw_const: |
| 4083 | case tok::kw_volatile: |
| 4084 | case tok::kw_restrict: |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4085 | |
John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4086 | // Debugger support. |
| 4087 | case tok::kw___unknown_anytype: |
| 4088 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4089 | // typedef-name |
Chris Lattner | a8a3f73 | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 4090 | case tok::annot_typename: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4091 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4092 | |
Chris Lattner | 409bf7d | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 4093 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4094 | case tok::less: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4095 | return getLangOpts().ObjC1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4096 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4097 | case tok::kw___cdecl: |
| 4098 | case tok::kw___stdcall: |
| 4099 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4100 | case tok::kw___thiscall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4101 | case tok::kw___vectorcall: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4102 | case tok::kw___w64: |
| 4103 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4104 | case tok::kw___ptr32: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4105 | case tok::kw___pascal: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4106 | case tok::kw___unaligned: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4107 | |
| 4108 | case tok::kw___private: |
| 4109 | case tok::kw___local: |
| 4110 | case tok::kw___global: |
| 4111 | case tok::kw___constant: |
| 4112 | case tok::kw___read_only: |
| 4113 | case tok::kw___read_write: |
| 4114 | case tok::kw___write_only: |
| 4115 | |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4116 | return true; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4117 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4118 | // C11 _Atomic |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4119 | case tok::kw__Atomic: |
| 4120 | return true; |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4121 | } |
| 4122 | } |
| 4123 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4124 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 4125 | /// declaration specifier. |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4126 | /// |
| 4127 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 4128 | /// this check is to disambiguate between an expression and a declaration. |
| 4129 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4130 | switch (Tok.getKind()) { |
| 4131 | default: return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4132 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4133 | case tok::identifier: // foo::bar |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4134 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4135 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4136 | return false; |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4137 | if (TryAltiVecVectorToken()) |
| 4138 | return true; |
| 4139 | // Fall through. |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4140 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4141 | case tok::kw_typename: // typename T::type |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4142 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4143 | // recurse to handle whatever we get. |
| 4144 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4145 | return true; |
| 4146 | if (Tok.is(tok::identifier)) |
| 4147 | return false; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4148 | |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4149 | // 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] | 4150 | // by an identifier and then either ':' or ']', in a place where an |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4151 | // expression is permitted, then this is probably a class message send |
| 4152 | // missing the initial '['. In this case, we won't consider this to be |
| 4153 | // the start of a declaration. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4154 | if (DisambiguatingWithExpression && |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4155 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 4156 | return false; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4157 | |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4158 | return isDeclarationSpecifier(); |
| 4159 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4160 | case tok::coloncolon: // ::foo::bar |
| 4161 | if (NextToken().is(tok::kw_new) || // ::new |
| 4162 | NextToken().is(tok::kw_delete)) // ::delete |
| 4163 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4164 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4165 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4166 | // recurse to handle whatever we get. |
| 4167 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4168 | return true; |
| 4169 | return isDeclarationSpecifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4170 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4171 | // storage-class-specifier |
| 4172 | case tok::kw_typedef: |
| 4173 | case tok::kw_extern: |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 4174 | case tok::kw___private_extern__: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4175 | case tok::kw_static: |
| 4176 | case tok::kw_auto: |
| 4177 | case tok::kw_register: |
| 4178 | case tok::kw___thread: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4179 | case tok::kw_thread_local: |
| 4180 | case tok::kw__Thread_local: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4181 | |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 4182 | // Modules |
| 4183 | case tok::kw___module_private__: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4184 | |
John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4185 | // Debugger support |
| 4186 | case tok::kw___unknown_anytype: |
| 4187 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4188 | // type-specifiers |
| 4189 | case tok::kw_short: |
| 4190 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4191 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4192 | case tok::kw___int128: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4193 | case tok::kw_signed: |
| 4194 | case tok::kw_unsigned: |
| 4195 | case tok::kw__Complex: |
| 4196 | case tok::kw__Imaginary: |
| 4197 | case tok::kw_void: |
| 4198 | case tok::kw_char: |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4199 | case tok::kw_wchar_t: |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4200 | case tok::kw_char16_t: |
| 4201 | case tok::kw_char32_t: |
| 4202 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4203 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4204 | case tok::kw_half: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4205 | case tok::kw_float: |
| 4206 | case tok::kw_double: |
Chris Lattner | bb31a42 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 4207 | case tok::kw_bool: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4208 | case tok::kw__Bool: |
| 4209 | case tok::kw__Decimal32: |
| 4210 | case tok::kw__Decimal64: |
| 4211 | case tok::kw__Decimal128: |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4212 | case tok::kw___vector: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4213 | |
Chris Lattner | 861a226 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 4214 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4215 | case tok::kw_class: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4216 | case tok::kw_struct: |
| 4217 | case tok::kw_union: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4218 | case tok::kw___interface: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4219 | // enum-specifier |
| 4220 | case tok::kw_enum: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4221 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4222 | // type-qualifier |
| 4223 | case tok::kw_const: |
| 4224 | case tok::kw_volatile: |
| 4225 | case tok::kw_restrict: |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4226 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4227 | // function-specifier |
| 4228 | case tok::kw_inline: |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4229 | case tok::kw_virtual: |
| 4230 | case tok::kw_explicit: |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4231 | case tok::kw__Noreturn: |
Chris Lattner | 7b20dc7 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4232 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 4233 | // alignment-specifier |
| 4234 | case tok::kw__Alignas: |
| 4235 | |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4236 | // friend keyword. |
| 4237 | case tok::kw_friend: |
| 4238 | |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 4239 | // static_assert-declaration |
| 4240 | case tok::kw__Static_assert: |
| 4241 | |
Chris Lattner | 599e47e | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4242 | // GNU typeof support. |
| 4243 | case tok::kw_typeof: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4244 | |
Chris Lattner | 599e47e | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4245 | // GNU attributes. |
Chris Lattner | 7b20dc7 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4246 | case tok::kw___attribute: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4247 | |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4248 | // C++11 decltype and constexpr. |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4249 | case tok::annot_decltype: |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4250 | case tok::kw_constexpr: |
Francois Pichet | e878cb6 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 4251 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4252 | // C11 _Atomic |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4253 | case tok::kw__Atomic: |
| 4254 | return true; |
| 4255 | |
Chris Lattner | 8b2ec16 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 4256 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4257 | case tok::less: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4258 | return getLangOpts().ObjC1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4259 | |
Douglas Gregor | 19b7acf | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 4260 | // typedef-name |
| 4261 | case tok::annot_typename: |
| 4262 | return !DisambiguatingWithExpression || |
| 4263 | !isStartOfObjCClassMessageMissingOpenBracket(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4264 | |
Steve Naroff | f192fab | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 4265 | case tok::kw___declspec: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4266 | case tok::kw___cdecl: |
| 4267 | case tok::kw___stdcall: |
| 4268 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4269 | case tok::kw___thiscall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4270 | case tok::kw___vectorcall: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4271 | case tok::kw___w64: |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4272 | case tok::kw___sptr: |
| 4273 | case tok::kw___uptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4274 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4275 | case tok::kw___ptr32: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4276 | case tok::kw___forceinline: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4277 | case tok::kw___pascal: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4278 | case tok::kw___unaligned: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4279 | |
| 4280 | case tok::kw___private: |
| 4281 | case tok::kw___local: |
| 4282 | case tok::kw___global: |
| 4283 | case tok::kw___constant: |
| 4284 | case tok::kw___read_only: |
| 4285 | case tok::kw___read_write: |
| 4286 | case tok::kw___write_only: |
| 4287 | |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4288 | return true; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4289 | } |
| 4290 | } |
| 4291 | |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 4292 | bool Parser::isConstructorDeclarator(bool IsUnqualified) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4293 | TentativeParsingAction TPA(*this); |
| 4294 | |
| 4295 | // Parse the C++ scope specifier. |
| 4296 | CXXScopeSpec SS; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4297 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4298 | /*EnteringContext=*/true)) { |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4299 | TPA.Revert(); |
| 4300 | return false; |
| 4301 | } |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4302 | |
| 4303 | // Parse the constructor name. |
| 4304 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 4305 | // We already know that we have a constructor name; just consume |
| 4306 | // the token. |
| 4307 | ConsumeToken(); |
| 4308 | } else { |
| 4309 | TPA.Revert(); |
| 4310 | return false; |
| 4311 | } |
| 4312 | |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4313 | // Current class name must be followed by a left parenthesis. |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4314 | if (Tok.isNot(tok::l_paren)) { |
| 4315 | TPA.Revert(); |
| 4316 | return false; |
| 4317 | } |
| 4318 | ConsumeParen(); |
| 4319 | |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4320 | // A right parenthesis, or ellipsis followed by a right parenthesis signals |
| 4321 | // that we have a constructor. |
| 4322 | if (Tok.is(tok::r_paren) || |
| 4323 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4324 | TPA.Revert(); |
| 4325 | return true; |
| 4326 | } |
| 4327 | |
Richard Smith | f216366 | 2013-09-06 00:12:20 +0000 | [diff] [blame] | 4328 | // A C++11 attribute here signals that we have a constructor, and is an |
| 4329 | // attribute on the first constructor parameter. |
| 4330 | if (getLangOpts().CPlusPlus11 && |
| 4331 | isCXX11AttributeSpecifier(/*Disambiguate*/ false, |
| 4332 | /*OuterMightBeMessageSend*/ true)) { |
| 4333 | TPA.Revert(); |
| 4334 | return true; |
| 4335 | } |
| 4336 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4337 | // If we need to, enter the specified scope. |
| 4338 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4339 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4340 | DeclScopeObj.EnterDeclaratorScope(); |
| 4341 | |
Francois Pichet | 79f3a87 | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4342 | // Optionally skip Microsoft attributes. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4343 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | 79f3a87 | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4344 | MaybeParseMicrosoftAttributes(Attrs); |
| 4345 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4346 | // Check whether the next token(s) are part of a declaration |
| 4347 | // specifier, in which case we have the start of a parameter and, |
| 4348 | // therefore, we know that this is a constructor. |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4349 | bool IsConstructor = false; |
| 4350 | if (isDeclarationSpecifier()) |
| 4351 | IsConstructor = true; |
| 4352 | else if (Tok.is(tok::identifier) || |
| 4353 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) { |
| 4354 | // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type. |
| 4355 | // This might be a parenthesized member name, but is more likely to |
| 4356 | // be a constructor declaration with an invalid argument type. Keep |
| 4357 | // looking. |
| 4358 | if (Tok.is(tok::annot_cxxscope)) |
| 4359 | ConsumeToken(); |
| 4360 | ConsumeToken(); |
| 4361 | |
| 4362 | // If this is not a constructor, we must be parsing a declarator, |
Richard Smith | 1453e31 | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4363 | // which must have one of the following syntactic forms (see the |
| 4364 | // grammar extract at the start of ParseDirectDeclarator): |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4365 | switch (Tok.getKind()) { |
| 4366 | case tok::l_paren: |
| 4367 | // C(X ( int)); |
| 4368 | case tok::l_square: |
| 4369 | // C(X [ 5]); |
| 4370 | // C(X [ [attribute]]); |
| 4371 | case tok::coloncolon: |
| 4372 | // C(X :: Y); |
| 4373 | // C(X :: *p); |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4374 | // Assume this isn't a constructor, rather than assuming it's a |
| 4375 | // constructor with an unnamed parameter of an ill-formed type. |
| 4376 | break; |
| 4377 | |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 4378 | case tok::r_paren: |
| 4379 | // C(X ) |
| 4380 | if (NextToken().is(tok::colon) || NextToken().is(tok::kw_try)) { |
| 4381 | // Assume these were meant to be constructors: |
| 4382 | // C(X) : (the name of a bit-field cannot be parenthesized). |
| 4383 | // C(X) try (this is otherwise ill-formed). |
| 4384 | IsConstructor = true; |
| 4385 | } |
| 4386 | if (NextToken().is(tok::semi) || NextToken().is(tok::l_brace)) { |
| 4387 | // If we have a constructor name within the class definition, |
| 4388 | // assume these were meant to be constructors: |
| 4389 | // C(X) { |
| 4390 | // C(X) ; |
| 4391 | // ... because otherwise we would be declaring a non-static data |
| 4392 | // member that is ill-formed because it's of the same type as its |
| 4393 | // surrounding class. |
| 4394 | // |
| 4395 | // FIXME: We can actually do this whether or not the name is qualified, |
| 4396 | // because if it is qualified in this context it must be being used as |
| 4397 | // a constructor name. However, we do not implement that rule correctly |
| 4398 | // currently, so we're somewhat conservative here. |
| 4399 | IsConstructor = IsUnqualified; |
| 4400 | } |
| 4401 | break; |
| 4402 | |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4403 | default: |
| 4404 | IsConstructor = true; |
| 4405 | break; |
| 4406 | } |
| 4407 | } |
| 4408 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4409 | TPA.Revert(); |
| 4410 | return IsConstructor; |
| 4411 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 4412 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4413 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4414 | /// type-qualifier-list: [C99 6.7.5] |
| 4415 | /// type-qualifier |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4416 | /// [vendor] attributes |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 4417 | /// [ only if AttrReqs & AR_VendorAttributesParsed ] |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4418 | /// type-qualifier-list type-qualifier |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4419 | /// [vendor] type-qualifier-list attributes |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 4420 | /// [ only if AttrReqs & AR_VendorAttributesParsed ] |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4421 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 4422 | /// [ only if AttReqs & AR_CXX11AttributesParsed ] |
| 4423 | /// Note: vendor can be GNU, MS, etc and can be explicitly controlled via |
| 4424 | /// AttrRequirements bitmask values. |
| 4425 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, unsigned AttrReqs, |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 4426 | bool AtomicAllowed, |
| 4427 | bool IdentifierRequired) { |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 4428 | if (getLangOpts().CPlusPlus11 && (AttrReqs & AR_CXX11AttributesParsed) && |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4429 | isCXX11AttributeSpecifier()) { |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4430 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4431 | ParseCXX11Attributes(attrs); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4432 | DS.takeAttributesFrom(attrs); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4433 | } |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4434 | |
| 4435 | SourceLocation EndLoc; |
| 4436 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4437 | while (1) { |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4438 | bool isInvalid = false; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 4439 | const char *PrevSpec = nullptr; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4440 | unsigned DiagID = 0; |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 4441 | SourceLocation Loc = Tok.getLocation(); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 4442 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4443 | switch (Tok.getKind()) { |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4444 | case tok::code_completion: |
| 4445 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 4446 | return cutOffParsing(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4447 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4448 | case tok::kw_const: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4449 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4450 | getLangOpts()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 4451 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4452 | case tok::kw_volatile: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4453 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4454 | getLangOpts()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 4455 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4456 | case tok::kw_restrict: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4457 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4458 | getLangOpts()); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4459 | break; |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4460 | case tok::kw__Atomic: |
| 4461 | if (!AtomicAllowed) |
| 4462 | goto DoneWithTypeQuals; |
| 4463 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 4464 | getLangOpts()); |
| 4465 | break; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4466 | |
| 4467 | // OpenCL qualifiers: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4468 | case tok::kw___private: |
| 4469 | case tok::kw___global: |
| 4470 | case tok::kw___local: |
| 4471 | case tok::kw___constant: |
| 4472 | case tok::kw___read_only: |
| 4473 | case tok::kw___write_only: |
| 4474 | case tok::kw___read_write: |
Aaron Ballman | 05d76ea | 2014-01-14 01:29:54 +0000 | [diff] [blame] | 4475 | ParseOpenCLQualifiers(DS.getAttributes()); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4476 | break; |
| 4477 | |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4478 | case tok::kw___uptr: |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 4479 | // GNU libc headers in C mode use '__uptr' as an identifer which conflicts |
| 4480 | // with the MS modifier keyword. |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 4481 | if ((AttrReqs & AR_DeclspecAttributesParsed) && !getLangOpts().CPlusPlus && |
Alp Toker | 47642d2 | 2013-12-03 06:13:01 +0000 | [diff] [blame] | 4482 | IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) { |
| 4483 | if (TryKeywordIdentFallback(false)) |
| 4484 | continue; |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 4485 | } |
| 4486 | case tok::kw___sptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4487 | case tok::kw___w64: |
Steve Naroff | f9c29d4 | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 4488 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4489 | case tok::kw___ptr32: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4490 | case tok::kw___cdecl: |
| 4491 | case tok::kw___stdcall: |
| 4492 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4493 | case tok::kw___thiscall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4494 | case tok::kw___vectorcall: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4495 | case tok::kw___unaligned: |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 4496 | if (AttrReqs & AR_DeclspecAttributesParsed) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4497 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4498 | continue; |
| 4499 | } |
| 4500 | goto DoneWithTypeQuals; |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4501 | case tok::kw___pascal: |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 4502 | if (AttrReqs & AR_VendorAttributesParsed) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4503 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4504 | continue; |
| 4505 | } |
| 4506 | goto DoneWithTypeQuals; |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 4507 | case tok::kw___attribute: |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 4508 | if (AttrReqs & AR_GNUAttributesParsedAndRejected) |
| 4509 | // When GNU attributes are expressly forbidden, diagnose their usage. |
| 4510 | Diag(Tok, diag::err_attributes_not_allowed); |
| 4511 | |
| 4512 | // Parse the attributes even if they are rejected to ensure that error |
| 4513 | // recovery is graceful. |
| 4514 | if (AttrReqs & AR_GNUAttributesParsed || |
| 4515 | AttrReqs & AR_GNUAttributesParsedAndRejected) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4516 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4517 | continue; // do *not* consume the next token! |
| 4518 | } |
| 4519 | // otherwise, FALL THROUGH! |
| 4520 | default: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4521 | DoneWithTypeQuals: |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4522 | // If this is not a type-qualifier token, we're done reading type |
| 4523 | // qualifiers. First verify that DeclSpec's are consistent. |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 4524 | DS.Finish(Diags, PP, Actions.getASTContext().getPrintingPolicy()); |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4525 | if (EndLoc.isValid()) |
| 4526 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4527 | return; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4528 | } |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4529 | |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 4530 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 4531 | if (isInvalid) { |
| 4532 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4533 | Diag(Tok, DiagID) << PrevSpec; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 4534 | } |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4535 | EndLoc = ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4536 | } |
| 4537 | } |
| 4538 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 4539 | |
| 4540 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 4541 | /// |
| 4542 | void Parser::ParseDeclarator(Declarator &D) { |
| 4543 | /// This implements the 'declarator' production in the C grammar, then checks |
| 4544 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4545 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 4546 | } |
| 4547 | |
Richard Smith | 0b350b9 | 2014-10-28 16:55:02 +0000 | [diff] [blame] | 4548 | static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang, |
| 4549 | unsigned TheContext) { |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4550 | if (Kind == tok::star || Kind == tok::caret) |
| 4551 | return true; |
| 4552 | |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4553 | if (!Lang.CPlusPlus) |
| 4554 | return false; |
| 4555 | |
Richard Smith | 0b350b9 | 2014-10-28 16:55:02 +0000 | [diff] [blame] | 4556 | if (Kind == tok::amp) |
| 4557 | return true; |
| 4558 | |
| 4559 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 4560 | // But we must not parse them in conversion-type-ids and new-type-ids, since |
| 4561 | // those can be legitimately followed by a && operator. |
| 4562 | // (The same thing can in theory happen after a trailing-return-type, but |
| 4563 | // since those are a C++11 feature, there is no rejects-valid issue there.) |
| 4564 | if (Kind == tok::ampamp) |
| 4565 | return Lang.CPlusPlus11 || (TheContext != Declarator::ConversionIdContext && |
| 4566 | TheContext != Declarator::CXXNewContext); |
| 4567 | |
| 4568 | return false; |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4569 | } |
| 4570 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4571 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 4572 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 4573 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4574 | /// ptr-operator production. |
| 4575 | /// |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4576 | /// 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] | 4577 | /// made to TryParseDeclarator and MightBeDeclarator, and possibly to |
| 4578 | /// isConstructorDeclarator. |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4579 | /// |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4580 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 4581 | /// [C] pointer[opt] direct-declarator |
| 4582 | /// [C++] direct-declarator |
| 4583 | /// [C++] ptr-operator declarator |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 4584 | /// |
| 4585 | /// pointer: [C99 6.7.5] |
| 4586 | /// '*' type-qualifier-list[opt] |
| 4587 | /// '*' type-qualifier-list[opt] pointer |
| 4588 | /// |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4589 | /// ptr-operator: |
| 4590 | /// '*' cv-qualifier-seq[opt] |
| 4591 | /// '&' |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4592 | /// [C++0x] '&&' |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4593 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4594 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4595 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4596 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 4597 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 66a985d | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 4598 | if (Diags.hasAllExtensionsSilenced()) |
| 4599 | D.setExtension(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4600 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4601 | // C++ member pointers start with a '::' or a nested-name. |
| 4602 | // Member pointers get special handling, since there's no place for the |
| 4603 | // scope spec in the generic path below. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4604 | if (getLangOpts().CPlusPlus && |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 4605 | (Tok.is(tok::coloncolon) || |
| 4606 | (Tok.is(tok::identifier) && |
| 4607 | (NextToken().is(tok::coloncolon) || NextToken().is(tok::less))) || |
Chris Lattner | 803802d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 4608 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4609 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4610 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4611 | CXXScopeSpec SS; |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4612 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext); |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4613 | |
Jeffrey Yasskin | 4e150f8 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 4614 | if (SS.isNotEmpty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4615 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4616 | // The scope spec really belongs to the direct-declarator. |
Richard Smith | 5f044ad | 2013-01-08 22:43:49 +0000 | [diff] [blame] | 4617 | if (D.mayHaveIdentifier()) |
| 4618 | D.getCXXScopeSpec() = SS; |
| 4619 | else |
| 4620 | AnnotateScopeToken(SS, true); |
| 4621 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4622 | if (DirectDeclParser) |
| 4623 | (this->*DirectDeclParser)(D); |
| 4624 | return; |
| 4625 | } |
| 4626 | |
| 4627 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4628 | D.SetRangeEnd(Loc); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4629 | DeclSpec DS(AttrFactory); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4630 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4631 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4632 | |
| 4633 | // Recurse to parse whatever is left. |
| 4634 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 4635 | |
| 4636 | // Sema will have to catch (syntactically invalid) pointers into global |
| 4637 | // scope. It has to catch pointers into namespace scope anyway. |
| 4638 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4639 | Loc), |
| 4640 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4641 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4642 | return; |
| 4643 | } |
| 4644 | } |
| 4645 | |
| 4646 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4647 | // Not a pointer, C++ reference, or block. |
Richard Smith | 0b350b9 | 2014-10-28 16:55:02 +0000 | [diff] [blame] | 4648 | if (!isPtrOperatorToken(Kind, getLangOpts(), D.getContext())) { |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4649 | if (DirectDeclParser) |
| 4650 | (this->*DirectDeclParser)(D); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4651 | return; |
| 4652 | } |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4653 | |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4654 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 4655 | // '&&' -> rvalue reference |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4656 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4657 | D.SetRangeEnd(Loc); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4658 | |
Chris Lattner | 9eac931 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 4659 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 788404f | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4660 | // Is a pointer. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4661 | DeclSpec DS(AttrFactory); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4662 | |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 4663 | // GNU attributes are not allowed here in a new-type-id, but Declspec and |
| 4664 | // C++11 attributes are allowed. |
| 4665 | unsigned Reqs = AR_CXX11AttributesParsed | AR_DeclspecAttributesParsed | |
| 4666 | ((D.getContext() != Declarator::CXXNewContext) |
| 4667 | ? AR_GNUAttributesParsed |
| 4668 | : AR_GNUAttributesParsedAndRejected); |
| 4669 | ParseTypeQualifierListOpt(DS, Reqs, true, !D.mayOmitIdentifier()); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4670 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4671 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4672 | // Recursively parse the declarator. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4673 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4674 | if (Kind == tok::star) |
| 4675 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 4676 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | e71b378d | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 4677 | DS.getConstSpecLoc(), |
| 4678 | DS.getVolatileSpecLoc(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4679 | DS.getRestrictSpecLoc()), |
| 4680 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4681 | SourceLocation()); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4682 | else |
| 4683 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4684 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4685 | Loc), |
| 4686 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4687 | SourceLocation()); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4688 | } else { |
| 4689 | // Is a reference |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4690 | DeclSpec DS(AttrFactory); |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 4691 | |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4692 | // Complain about rvalue references in C++03, but then go on and build |
| 4693 | // the declarator. |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4694 | if (Kind == tok::ampamp) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4695 | Diag(Loc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4696 | diag::warn_cxx98_compat_rvalue_reference : |
| 4697 | diag::ext_rvalue_reference); |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4698 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4699 | // GNU-style and C++11 attributes are allowed here, as is restrict. |
| 4700 | ParseTypeQualifierListOpt(DS); |
| 4701 | D.ExtendWithDeclSpec(DS); |
| 4702 | |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 4703 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 4704 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 4705 | // template type argument, in which case the cv-qualifiers are ignored. |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 4706 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 4707 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 4708 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4709 | diag::err_invalid_reference_qualifier_application) << "const"; |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 4710 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 4711 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4712 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4713 | // 'restrict' is permitted as an extension. |
| 4714 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
| 4715 | Diag(DS.getAtomicSpecLoc(), |
| 4716 | diag::err_invalid_reference_qualifier_application) << "_Atomic"; |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 4717 | } |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4718 | |
| 4719 | // Recursively parse the declarator. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4720 | ParseDeclaratorInternal(D, DirectDeclParser); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4721 | |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4722 | if (D.getNumTypeObjects() > 0) { |
| 4723 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 4724 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 4725 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | ebad6a2 | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 4726 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 4727 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4728 | << II; |
| 4729 | else |
| 4730 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4731 | << "type name"; |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4732 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4733 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4734 | // can go ahead and build the (technically ill-formed) |
| 4735 | // declarator: reference collapsing will take care of it. |
| 4736 | } |
| 4737 | } |
| 4738 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4739 | // Remember that we parsed a reference type. |
Chris Lattner | 788404f | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4740 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4741 | Kind == tok::amp), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4742 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4743 | SourceLocation()); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4744 | } |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 4745 | } |
| 4746 | |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 4747 | // When correcting from misplaced brackets before the identifier, the location |
| 4748 | // is saved inside the declarator so that other diagnostic messages can use |
| 4749 | // them. This extracts and returns that location, or returns the provided |
| 4750 | // location if a stored location does not exist. |
| 4751 | static SourceLocation getMissingDeclaratorIdLoc(Declarator &D, |
| 4752 | SourceLocation Loc) { |
| 4753 | if (D.getName().StartLocation.isInvalid() && |
| 4754 | D.getName().EndLocation.isValid()) |
| 4755 | return D.getName().EndLocation; |
| 4756 | |
| 4757 | return Loc; |
| 4758 | } |
| 4759 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4760 | /// ParseDirectDeclarator |
| 4761 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4762 | /// [C99] identifier |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4763 | /// '(' declarator ')' |
| 4764 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 4765 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4766 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4767 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4768 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4769 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4770 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 4771 | /// attribute-specifier-seq[opt] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4772 | /// direct-declarator '(' parameter-type-list ')' |
| 4773 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4774 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4775 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 22c40fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 4776 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 4777 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4778 | /// [C++11] direct-declarator '(' parameter-declaration-clause ')' |
| 4779 | /// attribute-specifier-seq[opt] cv-qualifier-seq[opt] |
| 4780 | /// ref-qualifier[opt] exception-specification[opt] |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4781 | /// [C++] declarator-id |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4782 | /// [C++11] declarator-id attribute-specifier-seq[opt] |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4783 | /// |
| 4784 | /// declarator-id: [C++ 8] |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4785 | /// '...'[opt] id-expression |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4786 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 4787 | /// |
| 4788 | /// id-expression: [C++ 5.1] |
| 4789 | /// unqualified-id |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4790 | /// qualified-id |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4791 | /// |
| 4792 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4793 | /// identifier |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4794 | /// operator-function-id |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4795 | /// conversion-function-id |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4796 | /// '~' class-name |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 4797 | /// template-id |
Argyrios Kyrtzidis | e442635 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 4798 | /// |
Richard Smith | 1453e31 | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4799 | /// Note, any additional constructs added here may need corresponding changes |
| 4800 | /// in isConstructorDeclarator. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4801 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4802 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4803 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4804 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Serge Pavlov | 458ea76 | 2014-07-16 05:16:52 +0000 | [diff] [blame] | 4805 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR inside a class, in |
| 4806 | // this context it is a bitfield. Also in range-based for statement colon |
| 4807 | // may delimit for-range-declaration. |
| 4808 | ColonProtectionRAIIObject X(*this, |
| 4809 | D.getContext() == Declarator::MemberContext || |
| 4810 | (D.getContext() == Declarator::ForContext && |
| 4811 | getLangOpts().CPlusPlus11)); |
| 4812 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4813 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4814 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4815 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4816 | D.getContext() == Declarator::MemberContext; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4817 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4818 | EnteringContext); |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4819 | } |
| 4820 | |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4821 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4822 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | 2b058ef | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 4823 | // Change the declaration context for name lookup, until this function |
| 4824 | // is exited (and the declarator has been parsed). |
| 4825 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4826 | } |
| 4827 | |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4828 | // C++0x [dcl.fct]p14: |
| 4829 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4830 | // of a parameter-declaration-clause without a preceding comma. In |
| 4831 | // this case, the ellipsis is parsed as part of the |
| 4832 | // abstract-declarator if the type of the parameter names a template |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4833 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 4834 | // as part of the parameter-declaration-clause. |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4835 | if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() && |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4836 | !((D.getContext() == Declarator::PrototypeContext || |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4837 | D.getContext() == Declarator::LambdaExprParameterContext || |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4838 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4839 | NextToken().is(tok::r_paren) && |
Richard Smith | b19337f | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4840 | !D.hasGroupingParens() && |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4841 | !Actions.containsUnexpandedParameterPacks(D))) { |
| 4842 | SourceLocation EllipsisLoc = ConsumeToken(); |
Richard Smith | 0b350b9 | 2014-10-28 16:55:02 +0000 | [diff] [blame] | 4843 | if (isPtrOperatorToken(Tok.getKind(), getLangOpts(), D.getContext())) { |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4844 | // The ellipsis was put in the wrong place. Recover, and explain to |
| 4845 | // the user what they should have done. |
| 4846 | ParseDeclarator(D); |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 4847 | if (EllipsisLoc.isValid()) |
| 4848 | DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D); |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4849 | return; |
| 4850 | } else |
| 4851 | D.setEllipsisLoc(EllipsisLoc); |
| 4852 | |
| 4853 | // The ellipsis can't be followed by a parenthesized declarator. We |
| 4854 | // check for that in ParseParenDeclarator, after we have disambiguated |
| 4855 | // the l_paren token. |
| 4856 | } |
| 4857 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4858 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 4859 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 4860 | // We found something that indicates the start of an unqualified-id. |
| 4861 | // Parse that unqualified-id. |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4862 | bool AllowConstructorName; |
| 4863 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 4864 | AllowConstructorName = false; |
| 4865 | else if (D.getCXXScopeSpec().isSet()) |
| 4866 | AllowConstructorName = |
| 4867 | (D.getContext() == Declarator::FileContext || |
Dmitri Gribenko | d1c91f1 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 4868 | D.getContext() == Declarator::MemberContext); |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4869 | else |
| 4870 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 4871 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4872 | SourceLocation TemplateKWLoc; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4873 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 4874 | /*EnteringContext=*/true, |
| 4875 | /*AllowDestructorName=*/true, |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4876 | AllowConstructorName, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4877 | ParsedType(), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4878 | TemplateKWLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4879 | D.getName()) || |
| 4880 | // Once we're past the identifier, if the scope was bad, mark the |
| 4881 | // whole declarator bad. |
| 4882 | D.getCXXScopeSpec().isInvalid()) { |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 4883 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4884 | D.setInvalidType(true); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4885 | } else { |
| 4886 | // Parsed the unqualified-id; update range information and move along. |
| 4887 | if (D.getSourceRange().getBegin().isInvalid()) |
| 4888 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 4889 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4890 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4891 | goto PastIdentifier; |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4892 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4893 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4894 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4895 | "There's a C++-specific check for tok::identifier above"); |
| 4896 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 4897 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
Richard Trieu | 2664dc1 | 2014-05-05 22:06:50 +0000 | [diff] [blame] | 4898 | D.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4899 | ConsumeToken(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4900 | goto PastIdentifier; |
Richard Smith | 9ce302e | 2013-07-11 05:10:21 +0000 | [diff] [blame] | 4901 | } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) { |
Richard Smith | f39720b | 2013-10-13 22:12:28 +0000 | [diff] [blame] | 4902 | // A virt-specifier isn't treated as an identifier if it appears after a |
| 4903 | // trailing-return-type. |
| 4904 | if (D.getContext() != Declarator::TrailingReturnContext || |
| 4905 | !isCXX11VirtSpecifier(Tok)) { |
| 4906 | Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id) |
| 4907 | << FixItHint::CreateRemoval(Tok.getLocation()); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 4908 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Richard Smith | f39720b | 2013-10-13 22:12:28 +0000 | [diff] [blame] | 4909 | ConsumeToken(); |
| 4910 | goto PastIdentifier; |
| 4911 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4912 | } |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4913 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4914 | if (Tok.is(tok::l_paren)) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4915 | // direct-declarator: '(' declarator ')' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 4916 | // direct-declarator: '(' attributes declarator ')' |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4917 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 4918 | ParseParenDeclarator(D); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4919 | |
| 4920 | // If the declarator was parenthesized, we entered the declarator |
| 4921 | // scope when parsing the parenthesized declarator, then exited |
| 4922 | // the scope already. Re-enter the scope, if we need to. |
| 4923 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4924 | // If there was an error parsing parenthesized declarator, declarator |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4925 | // scope may have been entered before. Don't do it again. |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4926 | if (!D.isInvalidType() && |
| 4927 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4928 | // Change the declaration context for name lookup, until this function |
| 4929 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4930 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4931 | } |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4932 | } else if (D.mayOmitIdentifier()) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4933 | // This could be something simple like "int" (in which case the declarator |
| 4934 | // portion is empty), if an abstract-declarator is allowed. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 4935 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Richard Smith | b19337f | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4936 | |
| 4937 | // The grammar for abstract-pack-declarator does not allow grouping parens. |
| 4938 | // FIXME: Revisit this once core issue 1488 is resolved. |
| 4939 | if (D.hasEllipsis() && D.hasGroupingParens()) |
| 4940 | Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()), |
| 4941 | diag::ext_abstract_pack_declarator_parens); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4942 | } else { |
David Blaikie | 5d577a2 | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 4943 | if (Tok.getKind() == tok::annot_pragma_parser_crash) |
David Blaikie | 5bd4c2a | 2012-08-21 18:56:49 +0000 | [diff] [blame] | 4944 | LLVM_BUILTIN_TRAP; |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 4945 | if (Tok.is(tok::l_square)) |
| 4946 | return ParseMisplacedBracketDeclarator(D); |
| 4947 | if (D.getContext() == Declarator::MemberContext) { |
| 4948 | Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()), |
| 4949 | diag::err_expected_member_name_or_semi) |
Richard Trieu | a134240 | 2014-05-02 23:40:32 +0000 | [diff] [blame] | 4950 | << (D.getDeclSpec().isEmpty() ? SourceRange() |
| 4951 | : D.getDeclSpec().getSourceRange()); |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 4952 | } else if (getLangOpts().CPlusPlus) { |
Richard Trieu | 9c67267 | 2013-01-26 02:31:38 +0000 | [diff] [blame] | 4953 | if (Tok.is(tok::period) || Tok.is(tok::arrow)) |
| 4954 | Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow); |
Richard Trieu | 2f58696 | 2013-09-05 02:31:33 +0000 | [diff] [blame] | 4955 | else { |
| 4956 | SourceLocation Loc = D.getCXXScopeSpec().getEndLoc(); |
| 4957 | if (Tok.isAtStartOfLine() && Loc.isValid()) |
| 4958 | Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id) |
| 4959 | << getLangOpts().CPlusPlus; |
| 4960 | else |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 4961 | Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()), |
| 4962 | diag::err_expected_unqualified_id) |
Richard Trieu | 2f58696 | 2013-09-05 02:31:33 +0000 | [diff] [blame] | 4963 | << getLangOpts().CPlusPlus; |
| 4964 | } |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 4965 | } else { |
| 4966 | Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()), |
| 4967 | diag::err_expected_either) |
| 4968 | << tok::identifier << tok::l_paren; |
| 4969 | } |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 4970 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Chris Lattner | 8c5dd73 | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 4971 | D.setInvalidType(true); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4972 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4973 | |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4974 | PastIdentifier: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4975 | assert(D.isPastIdentifier() && |
| 4976 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4977 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4978 | // Don't parse attributes unless we have parsed an unparenthesized name. |
| 4979 | if (D.hasName() && !D.getNumTypeObjects()) |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4980 | MaybeParseCXX11Attributes(D); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4981 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4982 | while (1) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4983 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4984 | // Enter function-declaration scope, limiting any declarators to the |
| 4985 | // function prototype scope, including parameter declarators. |
| 4986 | ParseScope PrototypeScope(this, |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4987 | Scope::FunctionPrototypeScope|Scope::DeclScope| |
| 4988 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 4989 | ? Scope::FunctionDeclarationScope : 0)); |
| 4990 | |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4991 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 4992 | // In such a case, check if we actually have a function declarator; if it |
| 4993 | // is not, the declarator has been fully parsed. |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4994 | bool IsAmbiguous = false; |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 4995 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 4996 | // The name of the declarator, if any, is tentatively declared within |
| 4997 | // a possible direct initializer. |
| 4998 | TentativelyDeclaredIdentifiers.push_back(D.getIdentifier()); |
| 4999 | bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous); |
| 5000 | TentativelyDeclaredIdentifiers.pop_back(); |
| 5001 | if (!IsFunctionDecl) |
| 5002 | break; |
| 5003 | } |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5004 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5005 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 5006 | T.consumeOpen(); |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5007 | ParseFunctionDeclarator(D, attrs, T, IsAmbiguous); |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5008 | PrototypeScope.Exit(); |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5009 | } else if (Tok.is(tok::l_square)) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5010 | ParseBracketDeclarator(D); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5011 | } else { |
| 5012 | break; |
| 5013 | } |
| 5014 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5015 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5016 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5017 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 5018 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5019 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5020 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 5021 | /// |
| 5022 | /// direct-declarator: |
| 5023 | /// '(' declarator ')' |
| 5024 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5025 | /// direct-declarator '(' parameter-type-list ')' |
| 5026 | /// direct-declarator '(' identifier-list[opt] ')' |
| 5027 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 5028 | /// parameter-type-list[opt] ')' |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5029 | /// |
| 5030 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5031 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 5032 | T.consumeOpen(); |
| 5033 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5034 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5035 | |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5036 | // Eat any attributes before we look at whether this is a grouping or function |
| 5037 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 5038 | // the type being built up, for example: |
| 5039 | // int (__attribute__(()) *x)(long y) |
| 5040 | // If this ends up not being a grouping paren, the attribute applies to the |
| 5041 | // first argument, for example: |
| 5042 | // int (__attribute__(()) int x) |
| 5043 | // In either case, we need to eat any attributes to be able to determine what |
| 5044 | // sort of paren this is. |
| 5045 | // |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5046 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5047 | bool RequiresArg = false; |
| 5048 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5049 | ParseGNUAttributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5050 | |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5051 | // We require that the argument list (if this is a non-grouping paren) be |
| 5052 | // present even if the attribute list was empty. |
| 5053 | RequiresArg = true; |
| 5054 | } |
Chad Rosier | eea9ca7 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 5055 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 5056 | // Eat any Microsoft extensions. |
Chad Rosier | eea9ca7 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 5057 | ParseMicrosoftTypeAttributes(attrs); |
| 5058 | |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5059 | // Eat any Borland extensions. |
Ted Kremenek | 5eec2b0 | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 5060 | if (Tok.is(tok::kw___pascal)) |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5061 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5062 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5063 | // If we haven't past the identifier yet (or where the identifier would be |
| 5064 | // stored, if this is an abstract declarator), then this is probably just |
| 5065 | // grouping parens. However, if this could be an abstract-declarator, then |
| 5066 | // this could also be the start of function arguments (consider 'void()'). |
| 5067 | bool isGrouping; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5068 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5069 | if (!D.mayOmitIdentifier()) { |
| 5070 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 5071 | // paren, because we haven't seen the identifier yet. |
| 5072 | isGrouping = true; |
| 5073 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 5074 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) && |
| 5075 | NextToken().is(tok::r_paren)) || // C++ int(...) |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5076 | isDeclarationSpecifier() || // 'int(int)' is a function. |
| 5077 | isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function. |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5078 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 5079 | // considered to be a type, not a K&R identifier-list. |
| 5080 | isGrouping = false; |
| 5081 | } else { |
| 5082 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 5083 | isGrouping = true; |
| 5084 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5085 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5086 | // If this is a grouping paren, handle: |
| 5087 | // direct-declarator: '(' declarator ')' |
| 5088 | // direct-declarator: '(' attributes declarator ')' |
| 5089 | if (isGrouping) { |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5090 | SourceLocation EllipsisLoc = D.getEllipsisLoc(); |
| 5091 | D.setEllipsisLoc(SourceLocation()); |
| 5092 | |
Argyrios Kyrtzidis | 8ae3684 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 5093 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5094 | D.setGroupingParens(true); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5095 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5096 | // Match the ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5097 | T.consumeClose(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5098 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5099 | T.getCloseLocation()), |
| 5100 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 8ae3684 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 5101 | |
| 5102 | D.setGroupingParens(hadGroupingParens); |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5103 | |
| 5104 | // An ellipsis cannot be placed outside parentheses. |
| 5105 | if (EllipsisLoc.isValid()) |
Nikola Smiljanic | 69fdc9f | 2014-06-06 02:58:59 +0000 | [diff] [blame] | 5106 | DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D); |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5107 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5108 | return; |
| 5109 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5110 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5111 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 5112 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5113 | // identifier (and remember where it would have been), then call into |
| 5114 | // ParseFunctionDeclarator to handle of argument list. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5115 | D.SetIdentifier(nullptr, Tok.getLocation()); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5116 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5117 | // Enter function-declaration scope, limiting any declarators to the |
| 5118 | // function prototype scope, including parameter declarators. |
| 5119 | ParseScope PrototypeScope(this, |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 5120 | Scope::FunctionPrototypeScope | Scope::DeclScope | |
| 5121 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 5122 | ? Scope::FunctionDeclarationScope : 0)); |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5123 | ParseFunctionDeclarator(D, attrs, T, false, RequiresArg); |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5124 | PrototypeScope.Exit(); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5125 | } |
| 5126 | |
| 5127 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 5128 | /// declarator D up to a paren, which indicates that we are parsing function |
| 5129 | /// arguments. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5130 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5131 | /// If FirstArgAttrs is non-null, then the caller parsed those arguments |
| 5132 | /// immediately after the open paren - they should be considered to be the |
| 5133 | /// first argument of a parameter. |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5134 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5135 | /// If RequiresArg is true, then the first argument of the function is required |
| 5136 | /// to be present and required to not be an identifier list. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5137 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5138 | /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt], |
| 5139 | /// (C++11) ref-qualifier[opt], exception-specification[opt], |
| 5140 | /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt]. |
| 5141 | /// |
| 5142 | /// [C++11] exception-specification: |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5143 | /// dynamic-exception-specification |
| 5144 | /// noexcept-specification |
| 5145 | /// |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5146 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5147 | ParsedAttributes &FirstArgAttrs, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5148 | BalancedDelimiterTracker &Tracker, |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5149 | bool IsAmbiguous, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5150 | bool RequiresArg) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5151 | assert(getCurScope()->isFunctionPrototypeScope() && |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5152 | "Should call from a Function scope"); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5153 | // lparen is already consumed! |
| 5154 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 5155 | |
| 5156 | // This should be true when the function has typed arguments. |
| 5157 | // Otherwise, it is treated as a K&R-style function. |
| 5158 | bool HasProto = false; |
| 5159 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5160 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5161 | // Remember where we see an ellipsis, if any. |
| 5162 | SourceLocation EllipsisLoc; |
| 5163 | |
| 5164 | DeclSpec DS(AttrFactory); |
| 5165 | bool RefQualifierIsLValueRef = true; |
| 5166 | SourceLocation RefQualifierLoc; |
Douglas Gregor | e248eea | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 5167 | SourceLocation ConstQualifierLoc; |
| 5168 | SourceLocation VolatileQualifierLoc; |
Hal Finkel | 23a0739 | 2014-10-20 17:32:04 +0000 | [diff] [blame] | 5169 | SourceLocation RestrictQualifierLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5170 | ExceptionSpecificationType ESpecType = EST_None; |
| 5171 | SourceRange ESpecRange; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5172 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 5173 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5174 | ExprResult NoexceptExpr; |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5175 | ParsedAttributes FnAttrs(AttrFactory); |
Richard Smith | 700537c | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 5176 | TypeResult TrailingReturnType; |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5177 | |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5178 | /* LocalEndLoc is the end location for the local FunctionTypeLoc. |
| 5179 | EndLoc is the end location for the function declarator. |
| 5180 | They differ for trailing return types. */ |
| 5181 | SourceLocation StartLoc, LocalEndLoc, EndLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5182 | SourceLocation LParenLoc, RParenLoc; |
| 5183 | LParenLoc = Tracker.getOpenLocation(); |
| 5184 | StartLoc = LParenLoc; |
| 5185 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5186 | if (isFunctionDeclaratorIdentifierList()) { |
| 5187 | if (RequiresArg) |
| 5188 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 5189 | |
| 5190 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 5191 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5192 | Tracker.consumeClose(); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5193 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5194 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5195 | EndLoc = RParenLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5196 | } else { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5197 | if (Tok.isNot(tok::r_paren)) |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5198 | ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, |
| 5199 | EllipsisLoc); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5200 | else if (RequiresArg) |
| 5201 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 5202 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5203 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5204 | |
| 5205 | // If we have the closing ')', eat it. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5206 | Tracker.consumeClose(); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5207 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5208 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5209 | EndLoc = RParenLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5210 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5211 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5212 | // FIXME: Accept these components in any order, and produce fixits to |
| 5213 | // correct the order if the user gets it wrong. Ideally we should deal |
| 5214 | // with the virt-specifier-seq and pure-specifier in the same way. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5215 | |
| 5216 | // Parse cv-qualifier-seq[opt]. |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5217 | ParseTypeQualifierListOpt(DS, AR_NoAttributesParsed, |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5218 | /*AtomicAllowed*/ false); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5219 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
| 5220 | EndLoc = DS.getSourceRange().getEnd(); |
| 5221 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 5222 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
Hal Finkel | 23a0739 | 2014-10-20 17:32:04 +0000 | [diff] [blame] | 5223 | RestrictQualifierLoc = DS.getRestrictSpecLoc(); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5224 | } |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5225 | |
| 5226 | // Parse ref-qualifier[opt]. |
| 5227 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5228 | Diag(Tok, getLangOpts().CPlusPlus11 ? |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5229 | diag::warn_cxx98_compat_ref_qualifier : |
| 5230 | diag::ext_ref_qualifier); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5231 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5232 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 5233 | RefQualifierLoc = ConsumeToken(); |
| 5234 | EndLoc = RefQualifierLoc; |
| 5235 | } |
| 5236 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5237 | // C++11 [expr.prim.general]p3: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5238 | // If a declaration declares a member function or member function |
| 5239 | // 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] | 5240 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5241 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5242 | // declarator. |
Richard Smith | ad1bbb9 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 5243 | // FIXME: currently, "static" case isn't handled correctly. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5244 | bool IsCXX11MemberFunction = |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5245 | getLangOpts().CPlusPlus11 && |
Richard Smith | 990a692 | 2014-01-17 21:01:18 +0000 | [diff] [blame] | 5246 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
Richard Smith | ad1bbb9 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 5247 | (D.getContext() == Declarator::MemberContext |
| 5248 | ? !D.getDeclSpec().isFriendSpecified() |
| 5249 | : D.getContext() == Declarator::FileContext && |
| 5250 | D.getCXXScopeSpec().isValid() && |
| 5251 | Actions.CurContext->isRecord()); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5252 | Sema::CXXThisScopeRAII ThisScope(Actions, |
| 5253 | dyn_cast<CXXRecordDecl>(Actions.CurContext), |
Richard Smith | 01141a9 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 5254 | DS.getTypeQualifiers() | |
Richard Smith | 034185c | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 5255 | (D.getDeclSpec().isConstexprSpecified() && |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 5256 | !getLangOpts().CPlusPlus14 |
Richard Smith | 01141a9 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 5257 | ? Qualifiers::Const : 0), |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5258 | IsCXX11MemberFunction); |
Richard Smith | 2331bbf | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5259 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5260 | // Parse exception-specification[opt]. |
Richard Smith | 2331bbf | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5261 | ESpecType = tryParseExceptionSpecification(ESpecRange, |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 5262 | DynamicExceptions, |
| 5263 | DynamicExceptionRanges, |
Richard Smith | 2331bbf | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5264 | NoexceptExpr); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5265 | if (ESpecType != EST_None) |
| 5266 | EndLoc = ESpecRange.getEnd(); |
| 5267 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5268 | // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes |
| 5269 | // after the exception-specification. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5270 | MaybeParseCXX11Attributes(FnAttrs); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5271 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5272 | // Parse trailing-return-type[opt]. |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5273 | LocalEndLoc = EndLoc; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5274 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) { |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5275 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5276 | if (D.getDeclSpec().getTypeSpecType() == TST_auto) |
| 5277 | StartLoc = D.getDeclSpec().getTypeSpecTypeLoc(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5278 | LocalEndLoc = Tok.getLocation(); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 5279 | SourceRange Range; |
Richard Smith | 700537c | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 5280 | TrailingReturnType = ParseTrailingReturnType(Range); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5281 | EndLoc = Range.getEnd(); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5282 | } |
| 5283 | } |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5284 | } |
| 5285 | |
| 5286 | // Remember that we parsed a function type, and remember the attributes. |
| 5287 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5288 | IsAmbiguous, |
| 5289 | LParenLoc, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5290 | ParamInfo.data(), ParamInfo.size(), |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5291 | EllipsisLoc, RParenLoc, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5292 | DS.getTypeQualifiers(), |
| 5293 | RefQualifierIsLValueRef, |
Douglas Gregor | e248eea | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 5294 | RefQualifierLoc, ConstQualifierLoc, |
| 5295 | VolatileQualifierLoc, |
Hal Finkel | 23a0739 | 2014-10-20 17:32:04 +0000 | [diff] [blame] | 5296 | RestrictQualifierLoc, |
Douglas Gregor | ad69e65 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 5297 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5298 | ESpecType, ESpecRange.getBegin(), |
| 5299 | DynamicExceptions.data(), |
| 5300 | DynamicExceptionRanges.data(), |
| 5301 | DynamicExceptions.size(), |
| 5302 | NoexceptExpr.isUsable() ? |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5303 | NoexceptExpr.get() : nullptr, |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5304 | StartLoc, LocalEndLoc, D, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5305 | TrailingReturnType), |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5306 | FnAttrs, EndLoc); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5307 | } |
| 5308 | |
| 5309 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 5310 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 5311 | /// |
| 5312 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 5313 | /// abstract-declarators. |
| 5314 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5315 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5316 | && Tok.is(tok::identifier) |
| 5317 | && !TryAltiVecVectorToken() |
| 5318 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 5319 | // 6.7.5.3p11. |
| 5320 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 5321 | // Identifier lists follow a really simple grammar: the identifiers can |
| 5322 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 5323 | // identifier lists are really rare in the brave new modern world, and |
| 5324 | // it is very common for someone to typo a type in a non-K&R style |
| 5325 | // list. If we are presented with something like: "void foo(intptr x, |
| 5326 | // float y)", we don't want to start parsing the function declarator as |
| 5327 | // though it is a K&R style declarator just because intptr is an |
| 5328 | // invalid type. |
| 5329 | // |
| 5330 | // To handle this, we check to see if the token after the first |
| 5331 | // identifier is a "," or ")". Only then do we parse it as an |
| 5332 | // identifier list. |
| 5333 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 5334 | } |
| 5335 | |
| 5336 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 5337 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 5338 | /// |
| 5339 | /// After returning, ParamInfo will hold the parsed parameters. |
| 5340 | /// |
| 5341 | /// identifier-list: [C99 6.7.5] |
| 5342 | /// identifier |
| 5343 | /// identifier-list ',' identifier |
| 5344 | /// |
| 5345 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 5346 | Declarator &D, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5347 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5348 | // If there was no identifier specified for the declarator, either we are in |
| 5349 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 5350 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 5351 | // diagnose this. |
| 5352 | if (!D.getIdentifier()) |
| 5353 | Diag(Tok, diag::ext_ident_list_in_param); |
| 5354 | |
| 5355 | // Maintain an efficient lookup of params we have seen so far. |
| 5356 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 5357 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 5358 | do { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5359 | // If this isn't an identifier, report the error and skip until ')'. |
| 5360 | if (Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 5361 | Diag(Tok, diag::err_expected) << tok::identifier; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5362 | SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5363 | // Forget we parsed anything. |
| 5364 | ParamInfo.clear(); |
| 5365 | return; |
| 5366 | } |
| 5367 | |
| 5368 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 5369 | |
| 5370 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 5371 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 5372 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 5373 | |
| 5374 | // Verify that the argument identifier has not already been mentioned. |
| 5375 | if (!ParamsSoFar.insert(ParmII)) { |
| 5376 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 5377 | } else { |
| 5378 | // Remember this identifier in ParamInfo. |
| 5379 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 5380 | Tok.getLocation(), |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5381 | nullptr)); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5382 | } |
| 5383 | |
| 5384 | // Eat the identifier. |
| 5385 | ConsumeToken(); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5386 | // The list continues if we see a comma. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 5387 | } while (TryConsumeToken(tok::comma)); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5388 | } |
| 5389 | |
| 5390 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 5391 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 5392 | /// identifier list. |
| 5393 | /// |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5394 | /// D is the declarator being parsed. If FirstArgAttrs is non-null, then the |
| 5395 | /// caller parsed those arguments immediately after the open paren - they should |
| 5396 | /// be considered to be part of the first parameter. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5397 | /// |
| 5398 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 5399 | /// be the location of the ellipsis, if any was parsed. |
| 5400 | /// |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5401 | /// parameter-type-list: [C99 6.7.5] |
| 5402 | /// parameter-list |
| 5403 | /// parameter-list ',' '...' |
Douglas Gregor | 9bfc2e5 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5404 | /// [C++] parameter-list '...' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5405 | /// |
| 5406 | /// parameter-list: [C99 6.7.5] |
| 5407 | /// parameter-declaration |
| 5408 | /// parameter-list ',' parameter-declaration |
| 5409 | /// |
| 5410 | /// parameter-declaration: [C99 6.7.5] |
| 5411 | /// declaration-specifiers declarator |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5412 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5413 | /// [C++11] initializer-clause |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 5414 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 5415 | /// declaration-specifiers abstract-declarator[opt] |
| 5416 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 5417 | /// '=' assignment-expression |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 5418 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5419 | /// [C++11] attribute-specifier-seq parameter-declaration |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5420 | /// |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5421 | void Parser::ParseParameterDeclarationClause( |
| 5422 | Declarator &D, |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5423 | ParsedAttributes &FirstArgAttrs, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5424 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5425 | SourceLocation &EllipsisLoc) { |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 5426 | do { |
| 5427 | // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq |
| 5428 | // before deciding this was a parameter-declaration-clause. |
| 5429 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5430 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5431 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5432 | // Parse the declaration-specifiers. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5433 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5434 | DeclSpec DS(AttrFactory); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5435 | |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5436 | // Parse any C++11 attributes. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5437 | MaybeParseCXX11Attributes(DS.getAttributes()); |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5438 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5439 | // Skip any Microsoft attributes before a param. |
Chad Rosier | f8a2e70 | 2012-12-20 20:37:53 +0000 | [diff] [blame] | 5440 | MaybeParseMicrosoftAttributes(DS.getAttributes()); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5441 | |
| 5442 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5443 | |
| 5444 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5445 | // 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] | 5446 | // 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] | 5447 | // get rid of a parameter (FirstArgAttrs) and this statement. It might be |
| 5448 | // too much hassle. |
| 5449 | DS.takeAttributesFrom(FirstArgAttrs); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5450 | |
Chris Lattner | de39c3e | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 5451 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5452 | |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5453 | |
| 5454 | // Parse the declarator. This is "PrototypeContext" or |
| 5455 | // "LambdaExprParameterContext", because we must accept either |
| 5456 | // 'declarator' or 'abstract-declarator' here. |
| 5457 | Declarator ParmDeclarator(DS, |
| 5458 | D.getContext() == Declarator::LambdaExprContext ? |
| 5459 | Declarator::LambdaExprParameterContext : |
| 5460 | Declarator::PrototypeContext); |
| 5461 | ParseDeclarator(ParmDeclarator); |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5462 | |
| 5463 | // Parse GNU attributes, if present. |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5464 | MaybeParseGNUAttributes(ParmDeclarator); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5465 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5466 | // Remember this parsed parameter in ParamInfo. |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5467 | IdentifierInfo *ParmII = ParmDeclarator.getIdentifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5468 | |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5469 | // DefArgToks is used when the parsing of default arguments needs |
| 5470 | // to be delayed. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5471 | CachedTokens *DefArgToks = nullptr; |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5472 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5473 | // If no parameter was specified, verify that *something* was specified, |
| 5474 | // otherwise we have a missing type and identifier. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5475 | if (DS.isEmpty() && ParmDeclarator.getIdentifier() == nullptr && |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5476 | ParmDeclarator.getNumTypeObjects() == 0) { |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5477 | // Completely missing, emit error. |
| 5478 | Diag(DSStart, diag::err_missing_param); |
| 5479 | } else { |
| 5480 | // Otherwise, we have something. Add it and let semantic analysis try |
| 5481 | // 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] | 5482 | |
Richard Smith | 36ee9fb | 2014-08-11 23:30:23 +0000 | [diff] [blame] | 5483 | // Last chance to recover from a misplaced ellipsis in an attempted |
| 5484 | // parameter pack declaration. |
| 5485 | if (Tok.is(tok::ellipsis) && |
| 5486 | (NextToken().isNot(tok::r_paren) || |
| 5487 | (!ParmDeclarator.getEllipsisLoc().isValid() && |
| 5488 | !Actions.isUnexpandedParameterPackPermitted())) && |
| 5489 | Actions.containsUnexpandedParameterPacks(ParmDeclarator)) |
| 5490 | DiagnoseMisplacedEllipsisInDeclarator(ConsumeToken(), ParmDeclarator); |
| 5491 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5492 | // Inform the actions module about the parameter declarator, so it gets |
| 5493 | // added to the current scope. |
Richard Smith | 95e1fb0 | 2014-08-27 03:23:12 +0000 | [diff] [blame] | 5494 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator); |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5495 | // Parse the default argument, if any. We parse the default |
| 5496 | // arguments in all dialects; the semantic analysis in |
| 5497 | // ActOnParamDefaultArgument will reject the default argument in |
| 5498 | // C. |
| 5499 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5500 | SourceLocation EqualLoc = Tok.getLocation(); |
| 5501 | |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5502 | // Parse the default argument |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5503 | if (D.getContext() == Declarator::MemberContext) { |
| 5504 | // If we're inside a class definition, cache the tokens |
| 5505 | // corresponding to the default argument. We'll actually parse |
| 5506 | // them when we see the end of the class definition. |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5507 | // FIXME: Can we use a smart pointer for Toks? |
| 5508 | DefArgToks = new CachedTokens; |
| 5509 | |
Richard Smith | 1fff95c | 2013-09-12 23:28:08 +0000 | [diff] [blame] | 5510 | if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5511 | delete DefArgToks; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5512 | DefArgToks = nullptr; |
Serge Pavlov | b4b3578 | 2014-07-22 01:54:49 +0000 | [diff] [blame] | 5513 | Actions.ActOnParamDefaultArgumentError(Param, EqualLoc); |
Argyrios Kyrtzidis | 249179c | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5514 | } else { |
| 5515 | // Mark the end of the default argument so that we know when to |
| 5516 | // stop when we parse it later on. |
| 5517 | Token DefArgEnd; |
| 5518 | DefArgEnd.startToken(); |
| 5519 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 5520 | DefArgEnd.setLocation(Tok.getLocation()); |
| 5521 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5522 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 5523 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 249179c | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5524 | } |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5525 | } else { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5526 | // Consume the '='. |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5527 | ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5528 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5529 | // The argument isn't actually potentially evaluated unless it is |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5530 | // used. |
| 5531 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | 7fcbd90 | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 5532 | Sema::PotentiallyEvaluatedIfUsed, |
| 5533 | Param); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5534 | |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5535 | ExprResult DefArgResult; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5536 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
Sebastian Redl | 1678d5f | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5537 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5538 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 1678d5f | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5539 | } else |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5540 | DefArgResult = ParseAssignmentExpression(); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5541 | if (DefArgResult.isInvalid()) { |
Serge Pavlov | b4b3578 | 2014-07-22 01:54:49 +0000 | [diff] [blame] | 5542 | Actions.ActOnParamDefaultArgumentError(Param, EqualLoc); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5543 | SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5544 | } else { |
| 5545 | // Inform the actions module about the default argument |
| 5546 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5547 | DefArgResult.get()); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5548 | } |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5549 | } |
| 5550 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5551 | |
| 5552 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5553 | ParmDeclarator.getIdentifierLoc(), |
| 5554 | Param, DefArgToks)); |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5555 | } |
| 5556 | |
Richard Smith | 36ee9fb | 2014-08-11 23:30:23 +0000 | [diff] [blame] | 5557 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) { |
| 5558 | if (!getLangOpts().CPlusPlus) { |
| 5559 | // We have ellipsis without a preceding ',', which is ill-formed |
| 5560 | // in C. Complain and provide the fix. |
| 5561 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
| 5562 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
| 5563 | } else if (ParmDeclarator.getEllipsisLoc().isValid() || |
| 5564 | Actions.containsUnexpandedParameterPacks(ParmDeclarator)) { |
| 5565 | // It looks like this was supposed to be a parameter pack. Warn and |
| 5566 | // point out where the ellipsis should have gone. |
| 5567 | SourceLocation ParmEllipsis = ParmDeclarator.getEllipsisLoc(); |
| 5568 | Diag(EllipsisLoc, diag::warn_misplaced_ellipsis_vararg) |
| 5569 | << ParmEllipsis.isValid() << ParmEllipsis; |
| 5570 | if (ParmEllipsis.isValid()) { |
| 5571 | Diag(ParmEllipsis, |
| 5572 | diag::note_misplaced_ellipsis_vararg_existing_ellipsis); |
| 5573 | } else { |
| 5574 | Diag(ParmDeclarator.getIdentifierLoc(), |
| 5575 | diag::note_misplaced_ellipsis_vararg_add_ellipsis) |
| 5576 | << FixItHint::CreateInsertion(ParmDeclarator.getIdentifierLoc(), |
| 5577 | "...") |
| 5578 | << !ParmDeclarator.hasName(); |
| 5579 | } |
| 5580 | Diag(EllipsisLoc, diag::note_misplaced_ellipsis_vararg_add_comma) |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 5581 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Richard Smith | 36ee9fb | 2014-08-11 23:30:23 +0000 | [diff] [blame] | 5582 | } |
| 5583 | |
| 5584 | // We can't have any more parameters after an ellipsis. |
Douglas Gregor | 9bfc2e5 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5585 | break; |
| 5586 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5587 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 5588 | // If the next token is a comma, consume it and keep reading arguments. |
| 5589 | } while (TryConsumeToken(tok::comma)); |
Chris Lattner | 6c940e6 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 5590 | } |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5591 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5592 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 5593 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 5594 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 5595 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 5596 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5597 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 5598 | /// attribute-specifier-seq[opt] |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5599 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5600 | if (CheckProhibitedCXX11Attribute()) |
| 5601 | return; |
| 5602 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5603 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 5604 | T.consumeOpen(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5605 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5606 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 5607 | // This code does a fast path to handle some of the most obvious cases. |
| 5608 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5609 | T.consumeClose(); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5610 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5611 | MaybeParseCXX11Attributes(attrs); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5612 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5613 | // Remember that we parsed the empty array type. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5614 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, nullptr, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5615 | T.getOpenLocation(), |
| 5616 | T.getCloseLocation()), |
| 5617 | attrs, T.getCloseLocation()); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5618 | return; |
| 5619 | } else if (Tok.getKind() == tok::numeric_constant && |
| 5620 | GetLookAheadToken(1).is(tok::r_square)) { |
| 5621 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5622 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5623 | ConsumeToken(); |
| 5624 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5625 | T.consumeClose(); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5626 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5627 | MaybeParseCXX11Attributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5628 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5629 | // Remember that we parsed a array type, and remember its features. |
Nikola Smiljanic | c531dcc | 2013-01-11 08:33:05 +0000 | [diff] [blame] | 5630 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5631 | ExprRes.get(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5632 | T.getOpenLocation(), |
| 5633 | T.getCloseLocation()), |
| 5634 | attrs, T.getCloseLocation()); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5635 | return; |
| 5636 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5637 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5638 | // If valid, this location is the position where we read the 'static' keyword. |
| 5639 | SourceLocation StaticLoc; |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 5640 | TryConsumeToken(tok::kw_static, StaticLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5641 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5642 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5643 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5644 | DeclSpec DS(AttrFactory); |
Aaron Ballman | 08b0659 | 2014-07-22 12:44:22 +0000 | [diff] [blame] | 5645 | ParseTypeQualifierListOpt(DS, AR_CXX11AttributesParsed); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5646 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5647 | // If we haven't already read 'static', check to see if there is one after the |
| 5648 | // type-qualifier-list. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 5649 | if (!StaticLoc.isValid()) |
| 5650 | TryConsumeToken(tok::kw_static, StaticLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5651 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5652 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5653 | bool isStar = false; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5654 | ExprResult NumElements; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5655 | |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5656 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 5657 | // 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] | 5658 | // the token after the star is a ']'. Since stars in arrays are |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5659 | // infrequent, use of lookahead is not costly here. |
| 5660 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | c439f0d | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 5661 | ConsumeToken(); // Eat the '*'. |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 5662 | |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5663 | if (StaticLoc.isValid()) { |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5664 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5665 | StaticLoc = SourceLocation(); // Drop the static. |
| 5666 | } |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5667 | isStar = true; |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5668 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5669 | // Note, in C89, this production uses the constant-expr production instead |
| 5670 | // of assignment-expr. The only difference is that assignment-expr allows |
| 5671 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 5672 | // 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] | 5673 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5674 | // Parse the constant-expression or assignment-expression now (depending |
| 5675 | // on dialect). |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5676 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5677 | NumElements = ParseConstantExpression(); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5678 | } else { |
| 5679 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 5680 | Sema::ConstantEvaluated); |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5681 | NumElements = ParseAssignmentExpression(); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5682 | } |
David Majnemer | f9834d5 | 2014-08-08 07:21:18 +0000 | [diff] [blame] | 5683 | } else { |
| 5684 | if (StaticLoc.isValid()) { |
| 5685 | Diag(StaticLoc, diag::err_unspecified_size_with_static); |
| 5686 | StaticLoc = SourceLocation(); // Drop the static. |
| 5687 | } |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 5688 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5689 | |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 5690 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 5691 | if (NumElements.isInvalid()) { |
Chris Lattner | cd2a8c5 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 5692 | D.setInvalidType(true); |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 5693 | // If the expression was invalid, skip it. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5694 | SkipUntil(tok::r_square, StopAtSemi); |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 5695 | return; |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5696 | } |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5697 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5698 | T.consumeClose(); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5699 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5700 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5701 | MaybeParseCXX11Attributes(attrs); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5702 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5703 | // Remember that we parsed a array type, and remember its features. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5704 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 5705 | StaticLoc.isValid(), isStar, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5706 | NumElements.get(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5707 | T.getOpenLocation(), |
| 5708 | T.getCloseLocation()), |
| 5709 | attrs, T.getCloseLocation()); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5710 | } |
| 5711 | |
Richard Trieu | f4b81d0 | 2014-06-24 23:14:24 +0000 | [diff] [blame] | 5712 | /// Diagnose brackets before an identifier. |
| 5713 | void Parser::ParseMisplacedBracketDeclarator(Declarator &D) { |
| 5714 | assert(Tok.is(tok::l_square) && "Missing opening bracket"); |
| 5715 | assert(!D.mayOmitIdentifier() && "Declarator cannot omit identifier"); |
| 5716 | |
| 5717 | SourceLocation StartBracketLoc = Tok.getLocation(); |
| 5718 | Declarator TempDeclarator(D.getDeclSpec(), D.getContext()); |
| 5719 | |
| 5720 | while (Tok.is(tok::l_square)) { |
| 5721 | ParseBracketDeclarator(TempDeclarator); |
| 5722 | } |
| 5723 | |
| 5724 | // Stuff the location of the start of the brackets into the Declarator. |
| 5725 | // The diagnostics from ParseDirectDeclarator will make more sense if |
| 5726 | // they use this location instead. |
| 5727 | if (Tok.is(tok::semi)) |
| 5728 | D.getName().EndLocation = StartBracketLoc; |
| 5729 | |
| 5730 | SourceLocation SuggestParenLoc = Tok.getLocation(); |
| 5731 | |
| 5732 | // Now that the brackets are removed, try parsing the declarator again. |
| 5733 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
| 5734 | |
| 5735 | // Something went wrong parsing the brackets, in which case, |
| 5736 | // ParseBracketDeclarator has emitted an error, and we don't need to emit |
| 5737 | // one here. |
| 5738 | if (TempDeclarator.getNumTypeObjects() == 0) |
| 5739 | return; |
| 5740 | |
| 5741 | // Determine if parens will need to be suggested in the diagnostic. |
| 5742 | bool NeedParens = false; |
| 5743 | if (D.getNumTypeObjects() != 0) { |
| 5744 | switch (D.getTypeObject(D.getNumTypeObjects() - 1).Kind) { |
| 5745 | case DeclaratorChunk::Pointer: |
| 5746 | case DeclaratorChunk::Reference: |
| 5747 | case DeclaratorChunk::BlockPointer: |
| 5748 | case DeclaratorChunk::MemberPointer: |
| 5749 | NeedParens = true; |
| 5750 | break; |
| 5751 | case DeclaratorChunk::Array: |
| 5752 | case DeclaratorChunk::Function: |
| 5753 | case DeclaratorChunk::Paren: |
| 5754 | break; |
| 5755 | } |
| 5756 | } |
| 5757 | |
| 5758 | if (NeedParens) { |
| 5759 | // Create a DeclaratorChunk for the inserted parens. |
| 5760 | ParsedAttributes attrs(AttrFactory); |
| 5761 | SourceLocation EndLoc = PP.getLocForEndOfToken(D.getLocEnd()); |
| 5762 | D.AddTypeInfo(DeclaratorChunk::getParen(SuggestParenLoc, EndLoc), attrs, |
| 5763 | SourceLocation()); |
| 5764 | } |
| 5765 | |
| 5766 | // Adding back the bracket info to the end of the Declarator. |
| 5767 | for (unsigned i = 0, e = TempDeclarator.getNumTypeObjects(); i < e; ++i) { |
| 5768 | const DeclaratorChunk &Chunk = TempDeclarator.getTypeObject(i); |
| 5769 | ParsedAttributes attrs(AttrFactory); |
| 5770 | attrs.set(Chunk.Common.AttrList); |
| 5771 | D.AddTypeInfo(Chunk, attrs, SourceLocation()); |
| 5772 | } |
| 5773 | |
| 5774 | // The missing identifier would have been diagnosed in ParseDirectDeclarator. |
| 5775 | // If parentheses are required, always suggest them. |
| 5776 | if (!D.getIdentifier() && !NeedParens) |
| 5777 | return; |
| 5778 | |
| 5779 | SourceLocation EndBracketLoc = TempDeclarator.getLocEnd(); |
| 5780 | |
| 5781 | // Generate the move bracket error message. |
| 5782 | SourceRange BracketRange(StartBracketLoc, EndBracketLoc); |
| 5783 | SourceLocation EndLoc = PP.getLocForEndOfToken(D.getLocEnd()); |
| 5784 | |
| 5785 | if (NeedParens) { |
| 5786 | Diag(EndLoc, diag::err_brackets_go_after_unqualified_id) |
| 5787 | << getLangOpts().CPlusPlus |
| 5788 | << FixItHint::CreateInsertion(SuggestParenLoc, "(") |
| 5789 | << FixItHint::CreateInsertion(EndLoc, ")") |
| 5790 | << FixItHint::CreateInsertionFromRange( |
| 5791 | EndLoc, CharSourceRange(BracketRange, true)) |
| 5792 | << FixItHint::CreateRemoval(BracketRange); |
| 5793 | } else { |
| 5794 | Diag(EndLoc, diag::err_brackets_go_after_unqualified_id) |
| 5795 | << getLangOpts().CPlusPlus |
| 5796 | << FixItHint::CreateInsertionFromRange( |
| 5797 | EndLoc, CharSourceRange(BracketRange, true)) |
| 5798 | << FixItHint::CreateRemoval(BracketRange); |
| 5799 | } |
| 5800 | } |
| 5801 | |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5802 | /// [GNU] typeof-specifier: |
| 5803 | /// typeof ( expressions ) |
| 5804 | /// typeof ( type-name ) |
| 5805 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5806 | /// |
| 5807 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5808 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5809 | Token OpTok = Tok; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5810 | SourceLocation StartLoc = ConsumeToken(); |
| 5811 | |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5812 | const bool hasParens = Tok.is(tok::l_paren); |
| 5813 | |
Eli Friedman | 15681d6 | 2012-09-26 04:34:21 +0000 | [diff] [blame] | 5814 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated, |
| 5815 | Sema::ReuseLambdaContextDecl); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5816 | |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5817 | bool isCastExpr; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5818 | ParsedType CastTy; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5819 | SourceRange CastRange; |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5820 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 5821 | CastTy, CastRange); |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5822 | if (hasParens) |
| 5823 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5824 | |
| 5825 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5826 | // FIXME: Not accurate, the range gets one token more than it should. |
| 5827 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5828 | else |
| 5829 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5830 | |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5831 | if (isCastExpr) { |
| 5832 | if (!CastTy) { |
| 5833 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5834 | return; |
Douglas Gregor | 220cac5 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 5835 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5836 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5837 | const char *PrevSpec = nullptr; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5838 | unsigned DiagID; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5839 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5840 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 5841 | DiagID, CastTy, |
| 5842 | Actions.getASTContext().getPrintingPolicy())) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5843 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5844 | return; |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5845 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5846 | |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5847 | // If we get here, the operand to the typeof was an expresion. |
| 5848 | if (Operand.isInvalid()) { |
| 5849 | DS.SetTypeSpecError(); |
Steve Naroff | 4bd2f71 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 5850 | return; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5851 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5852 | |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5853 | // We might need to transform the operand if it is potentially evaluated. |
| 5854 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 5855 | if (Operand.isInvalid()) { |
| 5856 | DS.SetTypeSpecError(); |
| 5857 | return; |
| 5858 | } |
| 5859 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5860 | const char *PrevSpec = nullptr; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5861 | unsigned DiagID; |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5862 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5863 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 5864 | DiagID, Operand.get(), |
| 5865 | Actions.getASTContext().getPrintingPolicy())) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5866 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5867 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5868 | |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 5869 | /// [C11] atomic-specifier: |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5870 | /// _Atomic ( type-name ) |
| 5871 | /// |
| 5872 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5873 | assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) && |
| 5874 | "Not an atomic specifier"); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5875 | |
| 5876 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5877 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5878 | if (T.consumeOpen()) |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5879 | return; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5880 | |
| 5881 | TypeResult Result = ParseTypeName(); |
| 5882 | if (Result.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5883 | SkipUntil(tok::r_paren, StopAtSemi); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5884 | return; |
| 5885 | } |
| 5886 | |
| 5887 | // Match the ')' |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5888 | T.consumeClose(); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5889 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5890 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5891 | return; |
| 5892 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5893 | DS.setTypeofParensRange(T.getRange()); |
| 5894 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5895 | |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 5896 | const char *PrevSpec = nullptr; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5897 | unsigned DiagID; |
| 5898 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5899 | DiagID, Result.get(), |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 5900 | Actions.getASTContext().getPrintingPolicy())) |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5901 | Diag(StartLoc, DiagID) << PrevSpec; |
| 5902 | } |
| 5903 | |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5904 | |
| 5905 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 5906 | /// from TryAltiVecVectorToken. |
| 5907 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 5908 | Token Next = NextToken(); |
| 5909 | switch (Next.getKind()) { |
| 5910 | default: return false; |
| 5911 | case tok::kw_short: |
| 5912 | case tok::kw_long: |
| 5913 | case tok::kw_signed: |
| 5914 | case tok::kw_unsigned: |
| 5915 | case tok::kw_void: |
| 5916 | case tok::kw_char: |
| 5917 | case tok::kw_int: |
| 5918 | case tok::kw_float: |
| 5919 | case tok::kw_double: |
| 5920 | case tok::kw_bool: |
| 5921 | case tok::kw___pixel: |
| 5922 | Tok.setKind(tok::kw___vector); |
| 5923 | return true; |
| 5924 | case tok::identifier: |
| 5925 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5926 | Tok.setKind(tok::kw___vector); |
| 5927 | return true; |
| 5928 | } |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5929 | if (Next.getIdentifierInfo() == Ident_bool) { |
| 5930 | Tok.setKind(tok::kw___vector); |
| 5931 | return true; |
| 5932 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5933 | return false; |
| 5934 | } |
| 5935 | } |
| 5936 | |
| 5937 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 5938 | const char *&PrevSpec, unsigned &DiagID, |
| 5939 | bool &isInvalid) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 5940 | const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy(); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5941 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 5942 | Token Next = NextToken(); |
| 5943 | switch (Next.getKind()) { |
| 5944 | case tok::kw_short: |
| 5945 | case tok::kw_long: |
| 5946 | case tok::kw_signed: |
| 5947 | case tok::kw_unsigned: |
| 5948 | case tok::kw_void: |
| 5949 | case tok::kw_char: |
| 5950 | case tok::kw_int: |
| 5951 | case tok::kw_float: |
| 5952 | case tok::kw_double: |
| 5953 | case tok::kw_bool: |
| 5954 | case tok::kw___pixel: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 5955 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5956 | return true; |
| 5957 | case tok::identifier: |
| 5958 | if (Next.getIdentifierInfo() == Ident_pixel) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 5959 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5960 | return true; |
| 5961 | } |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5962 | if (Next.getIdentifierInfo() == Ident_bool) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 5963 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy); |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5964 | return true; |
| 5965 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5966 | break; |
| 5967 | default: |
| 5968 | break; |
| 5969 | } |
Douglas Gregor | 9938e3b | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 5970 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5971 | DS.isTypeAltiVecVector()) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 5972 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5973 | return true; |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5974 | } else if ((Tok.getIdentifierInfo() == Ident_bool) && |
| 5975 | DS.isTypeAltiVecVector()) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 5976 | isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy); |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5977 | return true; |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5978 | } |
| 5979 | return false; |
| 5980 | } |