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" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 19 | #include "clang/Basic/CharInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/Parse/ParseDiagnostic.h" |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 21 | #include "clang/Sema/Lookup.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 22 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 23 | #include "clang/Sema/PrettyDeclStackTrace.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Sema/Scope.h" |
Chris Lattner | ad9ac94 | 2007-01-23 01:14:52 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallSet.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallString.h" |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringSwitch.h" |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | // C99 6.7: Declarations. |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 34 | /// ParseTypeName |
| 35 | /// type-name: [C99 6.7.6] |
| 36 | /// specifier-qualifier-list abstract-declarator[opt] |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 37 | /// |
| 38 | /// Called type-id in C++. |
Douglas Gregor | 205d5e3 | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 39 | TypeResult Parser::ParseTypeName(SourceRange *Range, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 40 | Declarator::TheContext Context, |
Richard Smith | cd1c055 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 41 | AccessSpecifier AS, |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 42 | Decl **OwnedType, |
| 43 | ParsedAttributes *Attrs) { |
Richard Smith | 62dad82 | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 44 | DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context); |
Richard Smith | 2f07ad5 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 45 | if (DSC == DSC_normal) |
| 46 | DSC = DSC_type_specifier; |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 47 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 48 | // Parse the common declaration-specifiers piece. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 49 | DeclSpec DS(AttrFactory); |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 50 | if (Attrs) |
| 51 | DS.addAttributes(Attrs->getList()); |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 52 | ParseSpecifierQualifierList(DS, AS, DSC); |
Richard Smith | cd1c055 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 53 | if (OwnedType) |
| 54 | *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0; |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 55 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 56 | // Parse the abstract-declarator, if present. |
Douglas Gregor | 205d5e3 | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 57 | Declarator DeclaratorInfo(DS, Context); |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 58 | ParseDeclarator(DeclaratorInfo); |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 59 | if (Range) |
| 60 | *Range = DeclaratorInfo.getSourceRange(); |
| 61 | |
Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 62 | if (DeclaratorInfo.isInvalidType()) |
Douglas Gregor | 220cac5 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 63 | return true; |
| 64 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 65 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 68 | |
| 69 | /// isAttributeLateParsed - Return true if the attribute has arguments that |
| 70 | /// require late parsing. |
| 71 | static bool isAttributeLateParsed(const IdentifierInfo &II) { |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 72 | #define CLANG_ATTR_LATE_PARSED_LIST |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 73 | return llvm::StringSwitch<bool>(II.getName()) |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 74 | #include "clang/Parse/AttrParserStringSwitches.inc" |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 75 | .Default(false); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 76 | #undef CLANG_ATTR_LATE_PARSED_LIST |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 79 | /// ParseGNUAttributes - Parse a non-empty attributes list. |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 80 | /// |
| 81 | /// [GNU] attributes: |
| 82 | /// attribute |
| 83 | /// attributes attribute |
| 84 | /// |
| 85 | /// [GNU] attribute: |
| 86 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 87 | /// |
| 88 | /// [GNU] attribute-list: |
| 89 | /// attrib |
| 90 | /// attribute_list ',' attrib |
| 91 | /// |
| 92 | /// [GNU] attrib: |
| 93 | /// empty |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 94 | /// attrib-name |
| 95 | /// attrib-name '(' identifier ')' |
| 96 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 97 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 98 | /// |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 99 | /// [GNU] attrib-name: |
| 100 | /// identifier |
| 101 | /// typespec |
| 102 | /// typequal |
| 103 | /// storageclass |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | /// |
Richard Smith | f7ca0c0 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 105 | /// Whether an attribute takes an 'identifier' is determined by the |
| 106 | /// attrib-name. GCC's behavior here is not worth imitating: |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 107 | /// |
Richard Smith | f7ca0c0 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 108 | /// * In C mode, if the attribute argument list starts with an identifier |
| 109 | /// followed by a ',' or an ')', and the identifier doesn't resolve to |
| 110 | /// a type, it is parsed as an identifier. If the attribute actually |
| 111 | /// wanted an expression, it's out of luck (but it turns out that no |
| 112 | /// attributes work that way, because C constant expressions are very |
| 113 | /// limited). |
| 114 | /// * In C++ mode, if the attribute argument list starts with an identifier, |
| 115 | /// and the attribute *wants* an identifier, it is parsed as an identifier. |
| 116 | /// At block scope, any additional tokens between the identifier and the |
| 117 | /// ',' or ')' are ignored, otherwise they produce a parse error. |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 118 | /// |
Richard Smith | f7ca0c0 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 119 | /// 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] | 120 | void Parser::ParseGNUAttributes(ParsedAttributes &attrs, |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 121 | SourceLocation *endLoc, |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 122 | LateParsedAttrList *LateAttrs, |
| 123 | Declarator *D) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 124 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 126 | while (Tok.is(tok::kw___attribute)) { |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 127 | ConsumeToken(); |
| 128 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 129 | "attribute")) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 130 | SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ; |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 131 | return; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 132 | } |
| 133 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 134 | SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ; |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 135 | return; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 136 | } |
| 137 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 138 | while (true) { |
| 139 | // Allow empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 140 | if (TryConsumeToken(tok::comma)) |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 141 | continue; |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 142 | |
| 143 | // Expect an identifier or declaration specifier (const, int, etc.) |
| 144 | if (Tok.isNot(tok::identifier) && !isDeclarationSpecifier()) |
| 145 | break; |
| 146 | |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 147 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 148 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 150 | if (Tok.isNot(tok::l_paren)) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 151 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 152 | AttributeList::AS_GNU); |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 153 | continue; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 154 | } |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 155 | |
| 156 | // Handle "parameterized" attributes |
| 157 | if (!LateAttrs || !isAttributeLateParsed(*AttrName)) { |
| 158 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc, 0, |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 159 | SourceLocation(), AttributeList::AS_GNU, D); |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 160 | continue; |
| 161 | } |
| 162 | |
| 163 | // Handle attributes with arguments that require late parsing. |
| 164 | LateParsedAttribute *LA = |
| 165 | new LateParsedAttribute(this, *AttrName, AttrNameLoc); |
| 166 | LateAttrs->push_back(LA); |
| 167 | |
| 168 | // Attributes in a class are parsed at the end of the class, along |
| 169 | // with other late-parsed declarations. |
| 170 | if (!ClassStack.empty() && !LateAttrs->parseSoon()) |
| 171 | getCurrentClass().LateParsedDeclarations.push_back(LA); |
| 172 | |
| 173 | // consume everything up to and including the matching right parens |
| 174 | ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false); |
| 175 | |
| 176 | Token Eof; |
| 177 | Eof.startToken(); |
| 178 | Eof.setLocation(Tok.getLocation()); |
| 179 | LA->Toks.push_back(Eof); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 180 | } |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 181 | |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 182 | if (ExpectAndConsume(tok::r_paren)) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 183 | SkipUntil(tok::r_paren, StopAtSemi); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 184 | SourceLocation Loc = Tok.getLocation(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 185 | if (ExpectAndConsume(tok::r_paren)) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 186 | SkipUntil(tok::r_paren, StopAtSemi); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 187 | if (endLoc) |
| 188 | *endLoc = Loc; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 189 | } |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 190 | } |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 191 | |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 192 | /// \brief Normalizes an attribute name by dropping prefixed and suffixed __. |
| 193 | static StringRef normalizeAttrName(StringRef Name) { |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 194 | if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__")) |
| 195 | Name = Name.drop_front(2).drop_back(2); |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 196 | return Name; |
| 197 | } |
| 198 | |
| 199 | /// \brief Determine whether the given attribute has an identifier argument. |
| 200 | static bool attributeHasIdentifierArg(const IdentifierInfo &II) { |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 201 | #define CLANG_ATTR_IDENTIFIER_ARG_LIST |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 202 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 203 | #include "clang/Parse/AttrParserStringSwitches.inc" |
Douglas Gregor | d2472d4 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 204 | .Default(false); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 205 | #undef CLANG_ATTR_IDENTIFIER_ARG_LIST |
Douglas Gregor | d2472d4 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 206 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 207 | |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 208 | /// \brief Determine whether the given attribute parses a type argument. |
| 209 | static bool attributeIsTypeArgAttr(const IdentifierInfo &II) { |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 210 | #define CLANG_ATTR_TYPE_ARG_LIST |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 211 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 212 | #include "clang/Parse/AttrParserStringSwitches.inc" |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 213 | .Default(false); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 214 | #undef CLANG_ATTR_TYPE_ARG_LIST |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Aaron Ballman | 15b27b9 | 2014-01-09 19:39:35 +0000 | [diff] [blame] | 217 | /// \brief Determine whether the given attribute requires parsing its arguments |
| 218 | /// in an unevaluated context or not. |
| 219 | static bool attributeParsedArgsUnevaluated(const IdentifierInfo &II) { |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 220 | #define CLANG_ATTR_ARG_CONTEXT_LIST |
Aaron Ballman | 15b27b9 | 2014-01-09 19:39:35 +0000 | [diff] [blame] | 221 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 222 | #include "clang/Parse/AttrParserStringSwitches.inc" |
Aaron Ballman | 15b27b9 | 2014-01-09 19:39:35 +0000 | [diff] [blame] | 223 | .Default(false); |
Aaron Ballman | 35db2b3 | 2014-01-29 22:13:45 +0000 | [diff] [blame] | 224 | #undef CLANG_ATTR_ARG_CONTEXT_LIST |
Aaron Ballman | 15b27b9 | 2014-01-09 19:39:35 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 227 | IdentifierLoc *Parser::ParseIdentifierLoc() { |
| 228 | assert(Tok.is(tok::identifier) && "expected an identifier"); |
| 229 | IdentifierLoc *IL = IdentifierLoc::create(Actions.Context, |
| 230 | Tok.getLocation(), |
| 231 | Tok.getIdentifierInfo()); |
| 232 | ConsumeToken(); |
| 233 | return IL; |
| 234 | } |
| 235 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 236 | void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName, |
| 237 | SourceLocation AttrNameLoc, |
| 238 | ParsedAttributes &Attrs, |
| 239 | SourceLocation *EndLoc) { |
| 240 | BalancedDelimiterTracker Parens(*this, tok::l_paren); |
| 241 | Parens.consumeOpen(); |
| 242 | |
| 243 | TypeResult T; |
| 244 | if (Tok.isNot(tok::r_paren)) |
| 245 | T = ParseTypeName(); |
| 246 | |
| 247 | if (Parens.consumeClose()) |
| 248 | return; |
| 249 | |
| 250 | if (T.isInvalid()) |
| 251 | return; |
| 252 | |
| 253 | if (T.isUsable()) |
| 254 | Attrs.addNewTypeAttr(&AttrName, |
| 255 | SourceRange(AttrNameLoc, Parens.getCloseLocation()), 0, |
| 256 | AttrNameLoc, T.get(), AttributeList::AS_GNU); |
| 257 | else |
| 258 | Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()), |
| 259 | 0, AttrNameLoc, 0, 0, AttributeList::AS_GNU); |
| 260 | } |
| 261 | |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 262 | /// Parse the arguments to a parameterized GNU attribute or |
| 263 | /// a C++11 attribute in "gnu" namespace. |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 264 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 265 | SourceLocation AttrNameLoc, |
| 266 | ParsedAttributes &Attrs, |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 267 | SourceLocation *EndLoc, |
| 268 | IdentifierInfo *ScopeName, |
| 269 | SourceLocation ScopeLoc, |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 270 | AttributeList::Syntax Syntax, |
| 271 | Declarator *D) { |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 272 | |
| 273 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 274 | |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 275 | AttributeList::Kind AttrKind = |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 276 | AttributeList::getKind(AttrName, ScopeName, Syntax); |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 277 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 278 | // Availability attributes have their own grammar. |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 279 | // FIXME: All these cases fail to pass in the syntax and scope, and might be |
| 280 | // written as C++11 gnu:: attributes. |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 281 | if (AttrKind == AttributeList::AT_Availability) { |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 282 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 283 | return; |
| 284 | } |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 285 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 286 | if (AttrKind == AttributeList::AT_ObjCBridgeRelated) { |
| 287 | ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 288 | return; |
| 289 | } |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 290 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 291 | // Type safety attributes have their own grammar. |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 292 | if (AttrKind == AttributeList::AT_TypeTagForDatatype) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 293 | ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 294 | return; |
| 295 | } |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 296 | |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 297 | // Some attributes expect solely a type parameter. |
| 298 | if (attributeIsTypeArgAttr(*AttrName)) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 299 | ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 300 | return; |
| 301 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 302 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 303 | // These may refer to the function arguments, but need to be parsed early to |
| 304 | // participate in determining whether it's a redeclaration. |
| 305 | llvm::OwningPtr<ParseScope> PrototypeScope; |
| 306 | if (AttrName->isStr("enable_if") && D && D->isFunctionDeclarator()) { |
| 307 | DeclaratorChunk::FunctionTypeInfo FTI = D->getFunctionTypeInfo(); |
| 308 | PrototypeScope.reset(new ParseScope(this, Scope::FunctionPrototypeScope | |
| 309 | Scope::FunctionDeclarationScope | |
| 310 | Scope::DeclScope)); |
Alp Toker | c535072 | 2014-02-26 22:27:52 +0000 | [diff] [blame] | 311 | for (unsigned i = 0; i != FTI.NumParams; ++i) { |
| 312 | ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 313 | Actions.ActOnReenterCXXMethodParameter(getCurScope(), Param); |
| 314 | } |
| 315 | } |
| 316 | |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 317 | // Ignore the left paren location for now. |
| 318 | ConsumeParen(); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 319 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 320 | ArgsVector ArgExprs; |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 321 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 322 | if (Tok.is(tok::identifier)) { |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 323 | // If this attribute wants an 'identifier' argument, make it so. |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 324 | bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName); |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 325 | |
| 326 | // If we don't know how to parse this attribute, but this is the only |
| 327 | // token in this argument, assume it's meant to be an identifier. |
Aaron Ballman | 6603747 | 2013-12-04 15:32:26 +0000 | [diff] [blame] | 328 | if (AttrKind == AttributeList::UnknownAttribute || |
| 329 | AttrKind == AttributeList::IgnoredAttribute) { |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 330 | const Token &Next = NextToken(); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 331 | IsIdentifierArg = Next.is(tok::r_paren) || Next.is(tok::comma); |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 332 | } |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 333 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 334 | if (IsIdentifierArg) |
| 335 | ArgExprs.push_back(ParseIdentifierLoc()); |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 338 | if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) { |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 339 | // Eat the comma. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 340 | if (!ArgExprs.empty()) |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 341 | ConsumeToken(); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 342 | |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 343 | // Parse the non-empty comma-separated list of expressions. |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 344 | do { |
Aaron Ballman | 7c1fcf8 | 2014-01-09 20:12:12 +0000 | [diff] [blame] | 345 | OwningPtr<EnterExpressionEvaluationContext> Unevaluated; |
| 346 | if (attributeParsedArgsUnevaluated(*AttrName)) |
| 347 | Unevaluated.reset(new EnterExpressionEvaluationContext(Actions, |
| 348 | Sema::Unevaluated)); |
| 349 | |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 350 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 351 | if (ArgExpr.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 352 | SkipUntil(tok::r_paren, StopAtSemi); |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 353 | return; |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 354 | } |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 355 | ArgExprs.push_back(ArgExpr.release()); |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 356 | // Eat the comma, move to the next argument |
| 357 | } while (TryConsumeToken(tok::comma)); |
Fariborz Jahanian | 6b70865 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 358 | } |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 359 | |
| 360 | SourceLocation RParen = Tok.getLocation(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 361 | if (!ExpectAndConsume(tok::r_paren)) { |
Michael Han | 360d225 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 362 | SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc; |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 363 | Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc, |
| 364 | ArgExprs.data(), ArgExprs.size(), Syntax); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 365 | } |
Aaron Ballman | 7c1fcf8 | 2014-01-09 20:12:12 +0000 | [diff] [blame] | 366 | |
| 367 | if (EndLoc) |
| 368 | *EndLoc = RParen; |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 371 | /// \brief Parses a single argument for a declspec, including the |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 372 | /// surrounding parens. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 373 | void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName, |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 374 | SourceLocation AttrNameLoc, |
| 375 | ParsedAttributes &Attrs) |
| 376 | { |
| 377 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 378 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 379 | AttrName->getNameStart(), tok::r_paren)) |
| 380 | return; |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 381 | |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 382 | ExprResult ArgExpr(ParseConstantExpression()); |
| 383 | if (ArgExpr.isInvalid()) { |
| 384 | T.skipToEnd(); |
| 385 | return; |
| 386 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 387 | ArgsUnion ExprList = ArgExpr.take(); |
| 388 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1, |
| 389 | AttributeList::AS_Declspec); |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 390 | |
| 391 | T.consumeClose(); |
| 392 | } |
| 393 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 394 | /// \brief Determines whether a declspec is a "simple" one requiring no |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 395 | /// arguments. |
| 396 | bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) { |
| 397 | return llvm::StringSwitch<bool>(Ident->getName()) |
| 398 | .Case("dllimport", true) |
| 399 | .Case("dllexport", true) |
| 400 | .Case("noreturn", true) |
| 401 | .Case("nothrow", true) |
| 402 | .Case("noinline", true) |
| 403 | .Case("naked", true) |
| 404 | .Case("appdomain", true) |
| 405 | .Case("process", true) |
| 406 | .Case("jitintrinsic", true) |
| 407 | .Case("noalias", true) |
| 408 | .Case("restrict", true) |
| 409 | .Case("novtable", true) |
| 410 | .Case("selectany", true) |
| 411 | .Case("thread", true) |
Aaron Ballman | 444eb6e | 2013-05-04 16:58:37 +0000 | [diff] [blame] | 412 | .Case("safebuffers", true ) |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 413 | .Default(false); |
| 414 | } |
| 415 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 416 | /// \brief Attempts to parse a declspec which is not simple (one that takes |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 417 | /// parameters). Will return false if we properly handled the declspec, or |
| 418 | /// true if it is an unknown declspec. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 419 | void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident, |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 420 | SourceLocation Loc, |
| 421 | ParsedAttributes &Attrs) { |
| 422 | // Try to handle the easy case first -- these declspecs all take a single |
| 423 | // parameter as their argument. |
| 424 | if (llvm::StringSwitch<bool>(Ident->getName()) |
| 425 | .Case("uuid", true) |
| 426 | .Case("align", true) |
| 427 | .Case("allocate", true) |
| 428 | .Default(false)) { |
| 429 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 430 | } else if (Ident->getName() == "deprecated") { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 431 | // The deprecated declspec has an optional single argument, so we will |
| 432 | // check for a l-paren to decide whether we should parse an argument or |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 433 | // not. |
| 434 | if (Tok.getKind() == tok::l_paren) |
| 435 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 436 | else |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 437 | Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec); |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 438 | } else if (Ident->getName() == "property") { |
| 439 | // 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] | 440 | // assignment expressions as a parameter, but the lhs of the assignment |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 441 | // must be named get or put. |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 442 | if (Tok.isNot(tok::l_paren)) { |
| 443 | Diag(Tok.getLocation(), diag::err_expected_lparen_after) |
| 444 | << Ident->getNameStart(); |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 445 | return; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 446 | } |
| 447 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 448 | T.expectAndConsume(diag::err_expected_lparen_after, |
| 449 | Ident->getNameStart(), tok::r_paren); |
| 450 | |
| 451 | enum AccessorKind { |
| 452 | AK_Invalid = -1, |
| 453 | AK_Put = 0, AK_Get = 1 // indices into AccessorNames |
| 454 | }; |
| 455 | IdentifierInfo *AccessorNames[] = { 0, 0 }; |
| 456 | bool HasInvalidAccessor = false; |
| 457 | |
| 458 | // Parse the accessor specifications. |
| 459 | while (true) { |
| 460 | // Stop if this doesn't look like an accessor spec. |
| 461 | if (!Tok.is(tok::identifier)) { |
| 462 | // If the user wrote a completely empty list, use a special diagnostic. |
| 463 | if (Tok.is(tok::r_paren) && !HasInvalidAccessor && |
| 464 | AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) { |
| 465 | Diag(Loc, diag::err_ms_property_no_getter_or_putter); |
| 466 | break; |
| 467 | } |
| 468 | |
| 469 | Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor); |
| 470 | break; |
| 471 | } |
| 472 | |
| 473 | AccessorKind Kind; |
| 474 | SourceLocation KindLoc = Tok.getLocation(); |
| 475 | StringRef KindStr = Tok.getIdentifierInfo()->getName(); |
| 476 | if (KindStr == "get") { |
| 477 | Kind = AK_Get; |
| 478 | } else if (KindStr == "put") { |
| 479 | Kind = AK_Put; |
| 480 | |
| 481 | // Recover from the common mistake of using 'set' instead of 'put'. |
| 482 | } else if (KindStr == "set") { |
| 483 | Diag(KindLoc, diag::err_ms_property_has_set_accessor) |
| 484 | << FixItHint::CreateReplacement(KindLoc, "put"); |
| 485 | Kind = AK_Put; |
| 486 | |
| 487 | // Handle the mistake of forgetting the accessor kind by skipping |
| 488 | // this accessor. |
| 489 | } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) { |
| 490 | Diag(KindLoc, diag::err_ms_property_missing_accessor_kind); |
| 491 | ConsumeToken(); |
| 492 | HasInvalidAccessor = true; |
| 493 | goto next_property_accessor; |
| 494 | |
| 495 | // Otherwise, complain about the unknown accessor kind. |
| 496 | } else { |
| 497 | Diag(KindLoc, diag::err_ms_property_unknown_accessor); |
| 498 | HasInvalidAccessor = true; |
| 499 | Kind = AK_Invalid; |
| 500 | |
| 501 | // Try to keep parsing unless it doesn't look like an accessor spec. |
| 502 | if (!NextToken().is(tok::equal)) break; |
| 503 | } |
| 504 | |
| 505 | // Consume the identifier. |
| 506 | ConsumeToken(); |
| 507 | |
| 508 | // Consume the '='. |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 509 | if (!TryConsumeToken(tok::equal)) { |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 510 | Diag(Tok.getLocation(), diag::err_ms_property_expected_equal) |
| 511 | << KindStr; |
| 512 | break; |
| 513 | } |
| 514 | |
| 515 | // Expect the method name. |
| 516 | if (!Tok.is(tok::identifier)) { |
| 517 | Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name); |
| 518 | break; |
| 519 | } |
| 520 | |
| 521 | if (Kind == AK_Invalid) { |
| 522 | // Just drop invalid accessors. |
| 523 | } else if (AccessorNames[Kind] != NULL) { |
| 524 | // Complain about the repeated accessor, ignore it, and keep parsing. |
| 525 | Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr; |
| 526 | } else { |
| 527 | AccessorNames[Kind] = Tok.getIdentifierInfo(); |
| 528 | } |
| 529 | ConsumeToken(); |
| 530 | |
| 531 | next_property_accessor: |
| 532 | // Keep processing accessors until we run out. |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 533 | if (TryConsumeToken(tok::comma)) |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 534 | continue; |
| 535 | |
| 536 | // If we run into the ')', stop without consuming it. |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 537 | if (Tok.is(tok::r_paren)) |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 538 | break; |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 539 | |
| 540 | Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen); |
| 541 | break; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | // Only add the property attribute if it was well-formed. |
| 545 | if (!HasInvalidAccessor) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 546 | Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(), |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 547 | AccessorNames[AK_Get], AccessorNames[AK_Put], |
| 548 | AttributeList::AS_Declspec); |
| 549 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 550 | T.skipToEnd(); |
| 551 | } else { |
| 552 | // We don't recognize this as a valid declspec, but instead of creating the |
| 553 | // attribute and allowing sema to warn about it, we will warn here instead. |
| 554 | // This is because some attributes have multiple spellings, but we need to |
| 555 | // disallow that for declspecs (such as align vs aligned). If we made the |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 556 | // attribute, we'd have to split the valid declspec spelling logic into |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 557 | // both locations. |
| 558 | Diag(Loc, diag::warn_ms_declspec_unknown) << Ident; |
| 559 | |
| 560 | // If there's an open paren, we should eat the open and close parens under |
| 561 | // the assumption that this unknown declspec has parameters. |
| 562 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 563 | if (!T.consumeOpen()) |
| 564 | T.skipToEnd(); |
| 565 | } |
| 566 | } |
| 567 | |
Eli Friedman | 06de2b5 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 568 | /// [MS] decl-specifier: |
| 569 | /// __declspec ( extended-decl-modifier-seq ) |
| 570 | /// |
| 571 | /// [MS] extended-decl-modifier-seq: |
| 572 | /// extended-decl-modifier[opt] |
| 573 | /// extended-decl-modifier extended-decl-modifier-seq |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 574 | void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) { |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 575 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | 06de2b5 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 576 | |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 577 | ConsumeToken(); |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 578 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 579 | if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec", |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 580 | tok::r_paren)) |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 581 | return; |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 582 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 583 | // An empty declspec is perfectly legal and should not warn. Additionally, |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 584 | // you can specify multiple attributes per declspec. |
| 585 | while (Tok.getKind() != tok::r_paren) { |
| 586 | // We expect either a well-known identifier or a generic string. Anything |
| 587 | // else is a malformed declspec. |
| 588 | bool IsString = Tok.getKind() == tok::string_literal ? true : false; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 589 | if (!IsString && Tok.getKind() != tok::identifier && |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 590 | Tok.getKind() != tok::kw_restrict) { |
| 591 | Diag(Tok, diag::err_ms_declspec_type); |
| 592 | T.skipToEnd(); |
| 593 | return; |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 594 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 595 | |
| 596 | IdentifierInfo *AttrName; |
| 597 | SourceLocation AttrNameLoc; |
| 598 | if (IsString) { |
| 599 | SmallString<8> StrBuffer; |
| 600 | bool Invalid = false; |
| 601 | StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid); |
| 602 | if (Invalid) { |
| 603 | T.skipToEnd(); |
| 604 | return; |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 605 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 606 | AttrName = PP.getIdentifierInfo(Str); |
| 607 | AttrNameLoc = ConsumeStringToken(); |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 608 | } else { |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 609 | AttrName = Tok.getIdentifierInfo(); |
| 610 | AttrNameLoc = ConsumeToken(); |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 611 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 612 | |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 613 | if (IsString || IsSimpleMicrosoftDeclSpec(AttrName)) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 614 | // If we have a generic string, we will allow it because there is no |
| 615 | // documented list of allowable string declspecs, but we know they exist |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 616 | // (for instance, SAL declspecs in older versions of MSVC). |
| 617 | // |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 618 | // Alternatively, if the identifier is a simple one, then it requires no |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 619 | // arguments and can be turned into an attribute directly. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 620 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 621 | AttributeList::AS_Declspec); |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 622 | else |
| 623 | ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs); |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 624 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 625 | T.consumeClose(); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 626 | } |
| 627 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 628 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 629 | // Treat these like attributes |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 630 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 631 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 632 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 633 | Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) || |
| 634 | Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) { |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 635 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 636 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 637 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 638 | AttributeList::AS_Keyword); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 639 | } |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 640 | } |
| 641 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 642 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 643 | // Treat these like attributes |
| 644 | while (Tok.is(tok::kw___pascal)) { |
| 645 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 646 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 647 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 648 | AttributeList::AS_Keyword); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 649 | } |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 652 | void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { |
| 653 | // Treat these like attributes |
| 654 | while (Tok.is(tok::kw___kernel)) { |
Richard Smith | 0cdcc98 | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 655 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 656 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 657 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 658 | AttributeList::AS_Keyword); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
Aaron Ballman | 05d76ea | 2014-01-14 01:29:54 +0000 | [diff] [blame] | 662 | void Parser::ParseOpenCLQualifiers(ParsedAttributes &Attrs) { |
| 663 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 664 | SourceLocation AttrNameLoc = Tok.getLocation(); |
Aaron Ballman | 2689133 | 2014-01-14 17:41:53 +0000 | [diff] [blame] | 665 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 666 | AttributeList::AS_Keyword); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 669 | /// \brief Parse a version number. |
| 670 | /// |
| 671 | /// version: |
| 672 | /// simple-integer |
| 673 | /// simple-integer ',' simple-integer |
| 674 | /// simple-integer ',' simple-integer ',' simple-integer |
| 675 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
| 676 | Range = Tok.getLocation(); |
| 677 | |
| 678 | if (!Tok.is(tok::numeric_constant)) { |
| 679 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 680 | SkipUntil(tok::comma, tok::r_paren, |
| 681 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 682 | return VersionTuple(); |
| 683 | } |
| 684 | |
| 685 | // Parse the major (and possibly minor and subminor) versions, which |
| 686 | // are stored in the numeric constant. We utilize a quirk of the |
| 687 | // lexer, which is that it handles something like 1.2.3 as a single |
| 688 | // numeric constant, rather than two separate tokens. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 689 | SmallString<512> Buffer; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 690 | Buffer.resize(Tok.getLength()+1); |
| 691 | const char *ThisTokBegin = &Buffer[0]; |
| 692 | |
| 693 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 694 | bool Invalid = false; |
| 695 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 696 | if (Invalid) |
| 697 | return VersionTuple(); |
| 698 | |
| 699 | // Parse the major version. |
| 700 | unsigned AfterMajor = 0; |
| 701 | unsigned Major = 0; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 702 | while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 703 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 704 | ++AfterMajor; |
| 705 | } |
| 706 | |
| 707 | if (AfterMajor == 0) { |
| 708 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 709 | SkipUntil(tok::comma, tok::r_paren, |
| 710 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 711 | return VersionTuple(); |
| 712 | } |
| 713 | |
| 714 | if (AfterMajor == ActualLength) { |
| 715 | ConsumeToken(); |
| 716 | |
| 717 | // We only had a single version component. |
| 718 | if (Major == 0) { |
| 719 | Diag(Tok, diag::err_zero_version); |
| 720 | return VersionTuple(); |
| 721 | } |
| 722 | |
| 723 | return VersionTuple(Major); |
| 724 | } |
| 725 | |
| 726 | if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) { |
| 727 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 728 | SkipUntil(tok::comma, tok::r_paren, |
| 729 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 730 | return VersionTuple(); |
| 731 | } |
| 732 | |
| 733 | // Parse the minor version. |
| 734 | unsigned AfterMinor = AfterMajor + 1; |
| 735 | unsigned Minor = 0; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 736 | while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 737 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 738 | ++AfterMinor; |
| 739 | } |
| 740 | |
| 741 | if (AfterMinor == ActualLength) { |
| 742 | ConsumeToken(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 743 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 744 | // We had major.minor. |
| 745 | if (Major == 0 && Minor == 0) { |
| 746 | Diag(Tok, diag::err_zero_version); |
| 747 | return VersionTuple(); |
| 748 | } |
| 749 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 750 | return VersionTuple(Major, Minor); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | // If what follows is not a '.', we have a problem. |
| 754 | if (ThisTokBegin[AfterMinor] != '.') { |
| 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); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 758 | return VersionTuple(); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | // Parse the subminor version. |
| 762 | unsigned AfterSubminor = AfterMinor + 1; |
| 763 | unsigned Subminor = 0; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 764 | while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 765 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 766 | ++AfterSubminor; |
| 767 | } |
| 768 | |
| 769 | if (AfterSubminor != ActualLength) { |
| 770 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 771 | SkipUntil(tok::comma, tok::r_paren, |
| 772 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 773 | return VersionTuple(); |
| 774 | } |
| 775 | ConsumeToken(); |
| 776 | return VersionTuple(Major, Minor, Subminor); |
| 777 | } |
| 778 | |
| 779 | /// \brief Parse the contents of the "availability" attribute. |
| 780 | /// |
| 781 | /// availability-attribute: |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 782 | /// 'availability' '(' platform ',' version-arg-list, opt-message')' |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 783 | /// |
| 784 | /// platform: |
| 785 | /// identifier |
| 786 | /// |
| 787 | /// version-arg-list: |
| 788 | /// version-arg |
| 789 | /// version-arg ',' version-arg-list |
| 790 | /// |
| 791 | /// version-arg: |
| 792 | /// 'introduced' '=' version |
| 793 | /// 'deprecated' '=' version |
Douglas Gregor | fdd417f | 2012-03-11 04:53:21 +0000 | [diff] [blame] | 794 | /// 'obsoleted' = version |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 795 | /// 'unavailable' |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 796 | /// opt-message: |
| 797 | /// 'message' '=' <string> |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 798 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 799 | SourceLocation AvailabilityLoc, |
| 800 | ParsedAttributes &attrs, |
| 801 | SourceLocation *endLoc) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 802 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 803 | AvailabilityChange Changes[Unknown]; |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 804 | ExprResult MessageExpr; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 805 | |
| 806 | // Opening '('. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 807 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 808 | if (T.consumeOpen()) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 809 | Diag(Tok, diag::err_expected) << tok::l_paren; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 810 | return; |
| 811 | } |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 812 | |
| 813 | // Parse the platform name, |
| 814 | if (Tok.isNot(tok::identifier)) { |
| 815 | Diag(Tok, diag::err_availability_expected_platform); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 816 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 817 | return; |
| 818 | } |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 819 | IdentifierLoc *Platform = ParseIdentifierLoc(); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 820 | |
| 821 | // Parse the ',' following the platform name. |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 822 | if (ExpectAndConsume(tok::comma)) { |
| 823 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 824 | return; |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 825 | } |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 826 | |
| 827 | // If we haven't grabbed the pointers for the identifiers |
| 828 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 829 | if (!Ident_introduced) { |
| 830 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 831 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 832 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 833 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 834 | Ident_message = PP.getIdentifierInfo("message"); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 838 | SourceLocation UnavailableLoc; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 839 | do { |
| 840 | if (Tok.isNot(tok::identifier)) { |
| 841 | Diag(Tok, diag::err_availability_expected_change); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 842 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 843 | return; |
| 844 | } |
| 845 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 846 | SourceLocation KeywordLoc = ConsumeToken(); |
| 847 | |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 848 | if (Keyword == Ident_unavailable) { |
| 849 | if (UnavailableLoc.isValid()) { |
| 850 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 851 | << Keyword << SourceRange(UnavailableLoc); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 852 | } |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 853 | UnavailableLoc = KeywordLoc; |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 854 | continue; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 857 | if (Tok.isNot(tok::equal)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 858 | Diag(Tok, diag::err_expected_after) << Keyword << tok::equal; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 859 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 860 | return; |
| 861 | } |
| 862 | ConsumeToken(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 863 | if (Keyword == Ident_message) { |
Benjamin Kramer | a9dfa92 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 864 | if (Tok.isNot(tok::string_literal)) { // Also reject wide string literals. |
Andy Gibbs | a8df57a | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 865 | Diag(Tok, diag::err_expected_string_literal) |
| 866 | << /*Source='availability attribute'*/2; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 867 | SkipUntil(tok::r_paren, StopAtSemi); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 868 | return; |
| 869 | } |
| 870 | MessageExpr = ParseStringLiteralExpression(); |
| 871 | break; |
| 872 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 873 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 874 | SourceRange VersionRange; |
| 875 | VersionTuple Version = ParseVersionTuple(VersionRange); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 876 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 877 | if (Version.empty()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 878 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 879 | return; |
| 880 | } |
| 881 | |
| 882 | unsigned Index; |
| 883 | if (Keyword == Ident_introduced) |
| 884 | Index = Introduced; |
| 885 | else if (Keyword == Ident_deprecated) |
| 886 | Index = Deprecated; |
| 887 | else if (Keyword == Ident_obsoleted) |
| 888 | Index = Obsoleted; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 889 | else |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 890 | Index = Unknown; |
| 891 | |
| 892 | if (Index < Unknown) { |
| 893 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 894 | Diag(KeywordLoc, diag::err_availability_redundant) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 895 | << Keyword |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 896 | << SourceRange(Changes[Index].KeywordLoc, |
| 897 | Changes[Index].VersionRange.getEnd()); |
| 898 | } |
| 899 | |
| 900 | Changes[Index].KeywordLoc = KeywordLoc; |
| 901 | Changes[Index].Version = Version; |
| 902 | Changes[Index].VersionRange = VersionRange; |
| 903 | } else { |
| 904 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 905 | << Keyword << VersionRange; |
| 906 | } |
| 907 | |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 908 | } while (TryConsumeToken(tok::comma)); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 909 | |
| 910 | // Closing ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 911 | if (T.consumeClose()) |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 912 | return; |
| 913 | |
| 914 | if (endLoc) |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 915 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 916 | |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 917 | // The 'unavailable' availability cannot be combined with any other |
| 918 | // availability changes. Make sure that hasn't happened. |
| 919 | if (UnavailableLoc.isValid()) { |
| 920 | bool Complained = false; |
| 921 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 922 | if (Changes[Index].KeywordLoc.isValid()) { |
| 923 | if (!Complained) { |
| 924 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 925 | << SourceRange(Changes[Index].KeywordLoc, |
| 926 | Changes[Index].VersionRange.getEnd()); |
| 927 | Complained = true; |
| 928 | } |
| 929 | |
| 930 | // Clear out the availability. |
| 931 | Changes[Index] = AvailabilityChange(); |
| 932 | } |
| 933 | } |
| 934 | } |
| 935 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 936 | // Record this attribute |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 937 | attrs.addNew(&Availability, |
| 938 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Fariborz Jahanian | 586be88 | 2012-01-23 23:38:32 +0000 | [diff] [blame] | 939 | 0, AvailabilityLoc, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 940 | Platform, |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 941 | Changes[Introduced], |
| 942 | Changes[Deprecated], |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 943 | Changes[Obsoleted], |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 944 | UnavailableLoc, MessageExpr.take(), |
Alexis Hunt | a0e54d4 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 945 | AttributeList::AS_GNU); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 948 | /// \brief Parse the contents of the "objc_bridge_related" attribute. |
| 949 | /// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')' |
| 950 | /// related_class: |
| 951 | /// Identifier |
| 952 | /// |
| 953 | /// opt-class_method: |
| 954 | /// Identifier: | <empty> |
| 955 | /// |
| 956 | /// opt-instance_method: |
| 957 | /// Identifier | <empty> |
| 958 | /// |
| 959 | void Parser::ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated, |
| 960 | SourceLocation ObjCBridgeRelatedLoc, |
| 961 | ParsedAttributes &attrs, |
| 962 | SourceLocation *endLoc) { |
| 963 | // Opening '('. |
| 964 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 965 | if (T.consumeOpen()) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 966 | Diag(Tok, diag::err_expected) << tok::l_paren; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 967 | return; |
| 968 | } |
| 969 | |
| 970 | // Parse the related class name. |
| 971 | if (Tok.isNot(tok::identifier)) { |
| 972 | Diag(Tok, diag::err_objcbridge_related_expected_related_class); |
| 973 | SkipUntil(tok::r_paren, StopAtSemi); |
| 974 | return; |
| 975 | } |
| 976 | IdentifierLoc *RelatedClass = ParseIdentifierLoc(); |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 977 | if (ExpectAndConsume(tok::comma)) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 978 | SkipUntil(tok::r_paren, StopAtSemi); |
| 979 | return; |
| 980 | } |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 981 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 982 | // Parse optional class method name. |
| 983 | IdentifierLoc *ClassMethod = 0; |
| 984 | if (Tok.is(tok::identifier)) { |
| 985 | ClassMethod = ParseIdentifierLoc(); |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 986 | if (!TryConsumeToken(tok::colon)) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 987 | Diag(Tok, diag::err_objcbridge_related_selector_name); |
| 988 | SkipUntil(tok::r_paren, StopAtSemi); |
| 989 | return; |
| 990 | } |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 991 | } |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 992 | if (!TryConsumeToken(tok::comma)) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 993 | if (Tok.is(tok::colon)) |
| 994 | Diag(Tok, diag::err_objcbridge_related_selector_name); |
| 995 | else |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 996 | Diag(Tok, diag::err_expected) << tok::comma; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 997 | SkipUntil(tok::r_paren, StopAtSemi); |
| 998 | return; |
| 999 | } |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1000 | |
| 1001 | // Parse optional instance method name. |
| 1002 | IdentifierLoc *InstanceMethod = 0; |
| 1003 | if (Tok.is(tok::identifier)) |
| 1004 | InstanceMethod = ParseIdentifierLoc(); |
| 1005 | else if (Tok.isNot(tok::r_paren)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1006 | Diag(Tok, diag::err_expected) << tok::r_paren; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1007 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1008 | return; |
| 1009 | } |
| 1010 | |
| 1011 | // Closing ')'. |
| 1012 | if (T.consumeClose()) |
| 1013 | return; |
| 1014 | |
| 1015 | if (endLoc) |
| 1016 | *endLoc = T.getCloseLocation(); |
| 1017 | |
| 1018 | // Record this attribute |
| 1019 | attrs.addNew(&ObjCBridgeRelated, |
| 1020 | SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()), |
| 1021 | 0, ObjCBridgeRelatedLoc, |
| 1022 | RelatedClass, |
| 1023 | ClassMethod, |
| 1024 | InstanceMethod, |
| 1025 | AttributeList::AS_GNU); |
| 1026 | |
| 1027 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1028 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1029 | // Late Parsed Attributes: |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1030 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 1031 | |
| 1032 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 1033 | |
| 1034 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 1035 | Self->ParseLexedAttributes(*Class); |
| 1036 | } |
| 1037 | |
| 1038 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1039 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 1043 | /// scope appropriately. |
| 1044 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 1045 | // Deal with templates |
| 1046 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 1047 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 1048 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 1049 | HasTemplateScope); |
| 1050 | if (HasTemplateScope) |
| 1051 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 1052 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1053 | // Set or update the scope flags. |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1054 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1055 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1056 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 1057 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 1058 | |
DeLesley Hutchins | 6f86004 | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1059 | // Enter the scope of nested classes |
| 1060 | if (!AlreadyHasClassScope) |
| 1061 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 1062 | Class.TagOrTemplate); |
Benjamin Kramer | 1d373c6 | 2012-05-17 12:01:52 +0000 | [diff] [blame] | 1063 | if (!Class.LateParsedDeclarations.empty()) { |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1064 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){ |
| 1065 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 1066 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1067 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1068 | |
DeLesley Hutchins | 6f86004 | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1069 | if (!AlreadyHasClassScope) |
| 1070 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 1071 | Class.TagOrTemplate); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1074 | |
| 1075 | /// \brief Parse all attributes in LAs, and attach them to Decl D. |
| 1076 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 1077 | bool EnterScope, bool OnDefinition) { |
DeLesley Hutchins | 66e300e | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1078 | assert(LAs.parseSoon() && |
| 1079 | "Attribute list should be marked for immediate parsing."); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1080 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 19c722d | 2012-08-15 22:41:04 +0000 | [diff] [blame] | 1081 | if (D) |
| 1082 | LAs[i]->addDecl(D); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1083 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
Benjamin Kramer | bafc49a | 2012-04-14 12:44:47 +0000 | [diff] [blame] | 1084 | delete LAs[i]; |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1085 | } |
| 1086 | LAs.clear(); |
| 1087 | } |
| 1088 | |
| 1089 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1090 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 1091 | /// This will be called at the end of parsing a class declaration |
| 1092 | /// for each LateParsedAttribute. We consume the saved tokens and |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1093 | /// create an attribute with the arguments filled in. We add this |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1094 | /// to the Attribute list for the decl. |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1095 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 1096 | bool EnterScope, bool OnDefinition) { |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1097 | // Save the current token position. |
| 1098 | SourceLocation OrigLoc = Tok.getLocation(); |
| 1099 | |
| 1100 | // Append the current token at the end of the new token stream so that it |
| 1101 | // doesn't get lost. |
| 1102 | LA.Toks.push_back(Tok); |
| 1103 | PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); |
| 1104 | // Consume the previously pushed token. |
Argyrios Kyrtzidis | c36633c | 2013-03-27 23:58:17 +0000 | [diff] [blame] | 1105 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1106 | |
| 1107 | ParsedAttributes Attrs(AttrFactory); |
| 1108 | SourceLocation endLoc; |
| 1109 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1110 | if (LA.Decls.size() > 0) { |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1111 | Decl *D = LA.Decls[0]; |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1112 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1113 | RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext()); |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1114 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1115 | // Allow 'this' within late-parsed attributes. |
Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 1116 | Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0, |
| 1117 | ND && ND->isCXXInstanceMember()); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1118 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1119 | if (LA.Decls.size() == 1) { |
| 1120 | // If the Decl is templatized, add template parameters to scope. |
| 1121 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 1122 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 1123 | if (HasTemplateScope) |
| 1124 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1125 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1126 | // If the Decl is on a function, add function parameters to the scope. |
| 1127 | bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
| 1128 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope); |
| 1129 | if (HasFunScope) |
| 1130 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1131 | |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1132 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1133 | 0, SourceLocation(), AttributeList::AS_GNU, 0); |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1134 | |
| 1135 | if (HasFunScope) { |
| 1136 | Actions.ActOnExitFunctionContext(); |
| 1137 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 1138 | } |
| 1139 | if (HasTemplateScope) { |
| 1140 | TempScope.Exit(); |
| 1141 | } |
| 1142 | } else { |
| 1143 | // If there are multiple decls, then the decl cannot be within the |
| 1144 | // function scope. |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1145 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1146 | 0, SourceLocation(), AttributeList::AS_GNU, 0); |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1147 | } |
DeLesley Hutchins | 71d6103 | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 1148 | } else { |
| 1149 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
Aaron Ballman | 9a99e0d | 2014-01-20 17:18:35 +0000 | [diff] [blame] | 1152 | const AttributeList *AL = Attrs.getList(); |
| 1153 | if (OnDefinition && AL && !AL->isCXX11Attribute() && |
Aaron Ballman | c669cc0 | 2014-01-27 22:10:04 +0000 | [diff] [blame] | 1154 | AL->isKnownToGCC()) |
Aaron Ballman | 9a99e0d | 2014-01-20 17:18:35 +0000 | [diff] [blame] | 1155 | Diag(Tok, diag::warn_attribute_on_function_definition) |
| 1156 | << &LA.AttrName; |
| 1157 | |
| 1158 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1159 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1160 | |
| 1161 | if (Tok.getLocation() != OrigLoc) { |
| 1162 | // Due to a parsing error, we either went over the cached tokens or |
| 1163 | // there are still cached tokens left, so we skip the leftover tokens. |
| 1164 | // Since this is an uncommon situation that should be avoided, use the |
| 1165 | // expensive isBeforeInTranslationUnit call. |
| 1166 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 1167 | OrigLoc)) |
| 1168 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
Douglas Gregor | 0cf55e9 | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 1169 | ConsumeAnyToken(); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1170 | } |
| 1171 | } |
| 1172 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1173 | void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName, |
| 1174 | SourceLocation AttrNameLoc, |
| 1175 | ParsedAttributes &Attrs, |
| 1176 | SourceLocation *EndLoc) { |
| 1177 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 1178 | |
| 1179 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1180 | T.consumeOpen(); |
| 1181 | |
| 1182 | if (Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1183 | Diag(Tok, diag::err_expected) << tok::identifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1184 | T.skipToEnd(); |
| 1185 | return; |
| 1186 | } |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 1187 | IdentifierLoc *ArgumentKind = ParseIdentifierLoc(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1188 | |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 1189 | if (ExpectAndConsume(tok::comma)) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1190 | T.skipToEnd(); |
| 1191 | return; |
| 1192 | } |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1193 | |
| 1194 | SourceRange MatchingCTypeRange; |
| 1195 | TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange); |
| 1196 | if (MatchingCType.isInvalid()) { |
| 1197 | T.skipToEnd(); |
| 1198 | return; |
| 1199 | } |
| 1200 | |
| 1201 | bool LayoutCompatible = false; |
| 1202 | bool MustBeNull = false; |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1203 | while (TryConsumeToken(tok::comma)) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1204 | if (Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1205 | Diag(Tok, diag::err_expected) << tok::identifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1206 | T.skipToEnd(); |
| 1207 | return; |
| 1208 | } |
| 1209 | IdentifierInfo *Flag = Tok.getIdentifierInfo(); |
| 1210 | if (Flag->isStr("layout_compatible")) |
| 1211 | LayoutCompatible = true; |
| 1212 | else if (Flag->isStr("must_be_null")) |
| 1213 | MustBeNull = true; |
| 1214 | else { |
| 1215 | Diag(Tok, diag::err_type_safety_unknown_flag) << Flag; |
| 1216 | T.skipToEnd(); |
| 1217 | return; |
| 1218 | } |
| 1219 | ConsumeToken(); // consume flag |
| 1220 | } |
| 1221 | |
| 1222 | if (!T.consumeClose()) { |
| 1223 | Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1224 | ArgumentKind, MatchingCType.release(), |
| 1225 | LayoutCompatible, MustBeNull, |
| 1226 | AttributeList::AS_GNU); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | if (EndLoc) |
| 1230 | *EndLoc = T.getCloseLocation(); |
| 1231 | } |
| 1232 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1233 | /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets |
| 1234 | /// of a C++11 attribute-specifier in a location where an attribute is not |
| 1235 | /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this |
| 1236 | /// situation. |
| 1237 | /// |
| 1238 | /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if |
| 1239 | /// this doesn't appear to actually be an attribute-specifier, and the caller |
| 1240 | /// should try to parse it. |
| 1241 | bool Parser::DiagnoseProhibitedCXX11Attribute() { |
| 1242 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)); |
| 1243 | |
| 1244 | switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) { |
| 1245 | case CAK_NotAttributeSpecifier: |
| 1246 | // No diagnostic: we're in Obj-C++11 and this is not actually an attribute. |
| 1247 | return false; |
| 1248 | |
| 1249 | case CAK_InvalidAttributeSpecifier: |
| 1250 | Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute); |
| 1251 | return false; |
| 1252 | |
| 1253 | case CAK_AttributeSpecifier: |
| 1254 | // Parse and discard the attributes. |
| 1255 | SourceLocation BeginLoc = ConsumeBracket(); |
| 1256 | ConsumeBracket(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1257 | SkipUntil(tok::r_square); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1258 | assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied"); |
| 1259 | SourceLocation EndLoc = ConsumeBracket(); |
| 1260 | Diag(BeginLoc, diag::err_attributes_not_allowed) |
| 1261 | << SourceRange(BeginLoc, EndLoc); |
| 1262 | return true; |
| 1263 | } |
Chandler Carruth | d8f7d38 | 2012-04-10 16:03:08 +0000 | [diff] [blame] | 1264 | llvm_unreachable("All cases handled above."); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
Richard Smith | 98155ad | 2013-02-20 01:17:14 +0000 | [diff] [blame] | 1267 | /// \brief We have found the opening square brackets of a C++11 |
| 1268 | /// attribute-specifier in a location where an attribute is not permitted, but |
| 1269 | /// we know where the attributes ought to be written. Parse them anyway, and |
| 1270 | /// provide a fixit moving them to the right place. |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1271 | void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs, |
| 1272 | SourceLocation CorrectLocation) { |
| 1273 | assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) || |
| 1274 | Tok.is(tok::kw_alignas)); |
| 1275 | |
| 1276 | // Consume the attributes. |
| 1277 | SourceLocation Loc = Tok.getLocation(); |
| 1278 | ParseCXX11Attributes(Attrs); |
| 1279 | CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true); |
| 1280 | |
| 1281 | Diag(Loc, diag::err_attributes_not_allowed) |
| 1282 | << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange) |
| 1283 | << FixItHint::CreateRemoval(AttrRange); |
| 1284 | } |
| 1285 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1286 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 1287 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 1288 | << attrs.Range; |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1291 | void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) { |
| 1292 | AttributeList *AttrList = attrs.getList(); |
| 1293 | while (AttrList) { |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1294 | if (AttrList->isCXX11Attribute()) { |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 1295 | Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr) |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1296 | << AttrList->getName(); |
| 1297 | AttrList->setInvalid(); |
| 1298 | } |
| 1299 | AttrList = AttrList->getNext(); |
| 1300 | } |
| 1301 | } |
| 1302 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1303 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 1304 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1305 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 1306 | /// location of the semicolon in DeclEnd. |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1307 | /// |
| 1308 | /// declaration: [C99 6.7] |
| 1309 | /// block-declaration -> |
| 1310 | /// simple-declaration |
| 1311 | /// others [FIXME] |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1312 | /// [C++] template-declaration |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1313 | /// [C++] namespace-definition |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1314 | /// [C++] using-directive |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1315 | /// [C++] using-declaration |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1316 | /// [C++11/C11] static_assert-declaration |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1317 | /// others... [FIXME] |
| 1318 | /// |
Fariborz Jahanian | 1db5c94 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1319 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 1320 | unsigned Context, |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1321 | SourceLocation &DeclEnd, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1322 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 355094e | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 1323 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | 59b7528 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 1324 | // Must temporarily exit the objective-c container scope for |
| 1325 | // parsing c none objective-c decls. |
| 1326 | ObjCDeclContextSwitch ObjCDC(*this); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1327 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1328 | Decl *SingleDecl = 0; |
Richard Smith | cd1c055 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1329 | Decl *OwnedType = 0; |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1330 | switch (Tok.getKind()) { |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1331 | case tok::kw_template: |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1332 | case tok::kw_export: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1333 | ProhibitAttributes(attrs); |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1334 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1335 | break; |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1336 | case tok::kw_inline: |
Sebastian Redl | 5a5f2c7 | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 1337 | // 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] | 1338 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1339 | ProhibitAttributes(attrs); |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1340 | SourceLocation InlineLoc = ConsumeToken(); |
| 1341 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 1342 | break; |
| 1343 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1344 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, |
Fariborz Jahanian | 1db5c94 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1345 | true); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1346 | case tok::kw_namespace: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1347 | ProhibitAttributes(attrs); |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1348 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1349 | break; |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1350 | case tok::kw_using: |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 1351 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
Richard Smith | cd1c055 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1352 | DeclEnd, attrs, &OwnedType); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1353 | break; |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1354 | case tok::kw_static_assert: |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1355 | case tok::kw__Static_assert: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1356 | ProhibitAttributes(attrs); |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1357 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1358 | break; |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1359 | default: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1360 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1361 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1362 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1363 | // 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] | 1364 | // single decl, convert it now. Alias declarations can also declare a type; |
| 1365 | // include that too if it is present. |
| 1366 | return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 1370 | /// declaration-specifiers init-declarator-list[opt] ';' |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1371 | /// [C++11] attribute-specifier-seq decl-specifier-seq[opt] |
| 1372 | /// init-declarator-list ';' |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1373 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 1374 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1375 | /// |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1376 | /// for-range-declaration: [C++11 6.5p1: stmt.ranged] |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1377 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1378 | /// |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1379 | /// 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] | 1380 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1381 | /// |
| 1382 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 1383 | /// of a simple-declaration. If we find that we are, we also parse the |
| 1384 | /// for-range-initializer, and place it here. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1385 | Parser::DeclGroupPtrTy |
| 1386 | Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context, |
| 1387 | SourceLocation &DeclEnd, |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1388 | ParsedAttributesWithRange &Attrs, |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1389 | bool RequireSemi, ForRangeInit *FRI) { |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1390 | // Parse the common declaration-specifiers piece. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1391 | ParsingDeclSpec DS(*this); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1392 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 1393 | DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context); |
| 1394 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext); |
| 1395 | |
| 1396 | // If we had a free-standing type definition with a missing semicolon, we |
| 1397 | // may get this far before the problem becomes obvious. |
| 1398 | if (DS.hasTagDefinition() && |
| 1399 | DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext)) |
| 1400 | return DeclGroupPtrTy(); |
Abramo Bagnara | 1cd8368 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 1401 | |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 1402 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 1403 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1404 | if (Tok.is(tok::semi)) { |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1405 | ProhibitAttributes(Attrs); |
Argyrios Kyrtzidis | fbb2bb5 | 2012-05-16 23:49:15 +0000 | [diff] [blame] | 1406 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 005fc1b | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1407 | if (RequireSemi) ConsumeToken(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1408 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1409 | DS); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1410 | DS.complete(TheDecl); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1411 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 1412 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1413 | |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1414 | DS.takeAttributesFrom(Attrs); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1415 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1416 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1417 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1418 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1419 | /// for a declarator. |
| 1420 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 1421 | switch (Tok.getKind()) { |
| 1422 | case tok::annot_cxxscope: |
| 1423 | case tok::annot_template_id: |
| 1424 | case tok::caret: |
| 1425 | case tok::code_completion: |
| 1426 | case tok::coloncolon: |
| 1427 | case tok::ellipsis: |
| 1428 | case tok::kw___attribute: |
| 1429 | case tok::kw_operator: |
| 1430 | case tok::l_paren: |
| 1431 | case tok::star: |
| 1432 | return true; |
| 1433 | |
| 1434 | case tok::amp: |
| 1435 | case tok::ampamp: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1436 | return getLangOpts().CPlusPlus; |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1437 | |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1438 | 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] | 1439 | return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 && |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1440 | NextToken().is(tok::l_square); |
| 1441 | |
| 1442 | 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] | 1443 | return Context == Declarator::MemberContext || getLangOpts().CPlusPlus; |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1444 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1445 | case tok::identifier: |
| 1446 | switch (NextToken().getKind()) { |
| 1447 | case tok::code_completion: |
| 1448 | case tok::coloncolon: |
| 1449 | case tok::comma: |
| 1450 | case tok::equal: |
| 1451 | case tok::equalequal: // Might be a typo for '='. |
| 1452 | case tok::kw_alignas: |
| 1453 | case tok::kw_asm: |
| 1454 | case tok::kw___attribute: |
| 1455 | case tok::l_brace: |
| 1456 | case tok::l_paren: |
| 1457 | case tok::l_square: |
| 1458 | case tok::less: |
| 1459 | case tok::r_brace: |
| 1460 | case tok::r_paren: |
| 1461 | case tok::r_square: |
| 1462 | case tok::semi: |
| 1463 | return true; |
| 1464 | |
| 1465 | case tok::colon: |
| 1466 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1467 | // and in block scope it's probably a label. Inside a class definition, |
| 1468 | // this is a bit-field. |
| 1469 | return Context == Declarator::MemberContext || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1470 | (getLangOpts().CPlusPlus && Context == Declarator::FileContext); |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1471 | |
| 1472 | case tok::identifier: // Possible virt-specifier. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1473 | return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken()); |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1474 | |
| 1475 | default: |
| 1476 | return false; |
| 1477 | } |
| 1478 | |
| 1479 | default: |
| 1480 | return false; |
| 1481 | } |
| 1482 | } |
| 1483 | |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1484 | /// Skip until we reach something which seems like a sensible place to pick |
| 1485 | /// up parsing after a malformed declaration. This will sometimes stop sooner |
| 1486 | /// than SkipUntil(tok::r_brace) would, but will never stop later. |
| 1487 | void Parser::SkipMalformedDecl() { |
| 1488 | while (true) { |
| 1489 | switch (Tok.getKind()) { |
| 1490 | case tok::l_brace: |
| 1491 | // Skip until matching }, then stop. We've probably skipped over |
| 1492 | // a malformed class or function definition or similar. |
| 1493 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1494 | SkipUntil(tok::r_brace); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1495 | if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) { |
| 1496 | // This declaration isn't over yet. Keep skipping. |
| 1497 | continue; |
| 1498 | } |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1499 | TryConsumeToken(tok::semi); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1500 | return; |
| 1501 | |
| 1502 | case tok::l_square: |
| 1503 | ConsumeBracket(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1504 | SkipUntil(tok::r_square); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1505 | continue; |
| 1506 | |
| 1507 | case tok::l_paren: |
| 1508 | ConsumeParen(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1509 | SkipUntil(tok::r_paren); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1510 | continue; |
| 1511 | |
| 1512 | case tok::r_brace: |
| 1513 | return; |
| 1514 | |
| 1515 | case tok::semi: |
| 1516 | ConsumeToken(); |
| 1517 | return; |
| 1518 | |
| 1519 | case tok::kw_inline: |
| 1520 | // 'inline namespace' at the start of a line is almost certainly |
Jordan Rose | 12e730c | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1521 | // a good place to pick back up parsing, except in an Objective-C |
| 1522 | // @interface context. |
| 1523 | if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) && |
| 1524 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1525 | return; |
| 1526 | break; |
| 1527 | |
| 1528 | case tok::kw_namespace: |
| 1529 | // 'namespace' at the start of a line is almost certainly a good |
Jordan Rose | 12e730c | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1530 | // place to pick back up parsing, except in an Objective-C |
| 1531 | // @interface context. |
| 1532 | if (Tok.isAtStartOfLine() && |
| 1533 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
| 1534 | return; |
| 1535 | break; |
| 1536 | |
| 1537 | case tok::at: |
| 1538 | // @end is very much like } in Objective-C contexts. |
| 1539 | if (NextToken().isObjCAtKeyword(tok::objc_end) && |
| 1540 | ParsingInObjCContainer) |
| 1541 | return; |
| 1542 | break; |
| 1543 | |
| 1544 | case tok::minus: |
| 1545 | case tok::plus: |
| 1546 | // - and + probably start new method declarations in Objective-C contexts. |
| 1547 | if (Tok.isAtStartOfLine() && ParsingInObjCContainer) |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1548 | return; |
| 1549 | break; |
| 1550 | |
| 1551 | case tok::eof: |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 1552 | case tok::annot_module_begin: |
| 1553 | case tok::annot_module_end: |
| 1554 | case tok::annot_module_include: |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1555 | return; |
| 1556 | |
| 1557 | default: |
| 1558 | break; |
| 1559 | } |
| 1560 | |
| 1561 | ConsumeAnyToken(); |
| 1562 | } |
| 1563 | } |
| 1564 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1565 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1566 | /// definition or a group of object declarations, actually parse the |
| 1567 | /// result. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1568 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1569 | unsigned Context, |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1570 | bool AllowFunctionDefinitions, |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1571 | SourceLocation *DeclEnd, |
| 1572 | ForRangeInit *FRI) { |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1573 | // Parse the first declarator. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1574 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1575 | ParseDeclarator(D); |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1576 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1577 | // Bail out if the first declarator didn't seem well-formed. |
| 1578 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1579 | SkipMalformedDecl(); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1580 | return DeclGroupPtrTy(); |
Chris Lattner | efb0f11 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1581 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1582 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1583 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1584 | // appropriate function scope after the function Decl has been constructed. |
DeLesley Hutchins | 66e300e | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1585 | // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList. |
| 1586 | LateParsedAttrList LateParsedAttrs(true); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1587 | if (D.isFunctionDeclarator()) |
| 1588 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1589 | |
Chris Lattner | dbb1e93 | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1590 | // 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] | 1591 | if (D.isFunctionDeclarator() && |
Chris Lattner | dbb1e93 | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1592 | // Look at the next token to make sure that this isn't a function |
| 1593 | // declaration. We have to check this because __attribute__ might be the |
| 1594 | // start of a function definition in GCC-extended K&R C. |
Fariborz Jahanian | 712bb81 | 2012-08-10 15:54:40 +0000 | [diff] [blame] | 1595 | !isDeclarationAfterDeclarator()) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1596 | |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1597 | if (AllowFunctionDefinitions) { |
| 1598 | if (isStartOfFunctionDefinition(D)) { |
| 1599 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1600 | Diag(Tok, diag::err_function_declared_typedef); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1601 | |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1602 | // Recover by treating the 'typedef' as spurious. |
| 1603 | DS.ClearStorageClassSpecs(); |
| 1604 | } |
| 1605 | |
| 1606 | Decl *TheDecl = |
| 1607 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
| 1608 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1611 | if (isDeclarationSpecifier()) { |
| 1612 | // If there is an invalid declaration specifier right after the function |
| 1613 | // prototype, then we must be in a missing semicolon case where this isn't |
| 1614 | // actually a body. Just fall through into the code that handles it as a |
| 1615 | // prototype, and let the top-level code handle the erroneous declspec |
| 1616 | // where it would otherwise expect a comma or semicolon. |
| 1617 | } else { |
| 1618 | Diag(Tok, diag::err_expected_fn_body); |
| 1619 | SkipUntil(tok::semi); |
| 1620 | return DeclGroupPtrTy(); |
| 1621 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1622 | } else { |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1623 | if (Tok.is(tok::l_brace)) { |
| 1624 | Diag(Tok, diag::err_function_definition_not_allowed); |
Serge Pavlov | 1de5151 | 2013-12-09 05:25:47 +0000 | [diff] [blame] | 1625 | SkipMalformedDecl(); |
| 1626 | return DeclGroupPtrTy(); |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1627 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1628 | } |
| 1629 | } |
| 1630 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1631 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1632 | return DeclGroupPtrTy(); |
| 1633 | |
| 1634 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1635 | // must parse and analyze the for-range-initializer before the declaration is |
| 1636 | // analyzed. |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1637 | // |
| 1638 | // Handle the Objective-C for-in loop variable similarly, although we |
| 1639 | // don't need to parse the container in advance. |
| 1640 | if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) { |
| 1641 | bool IsForRangeLoop = false; |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1642 | if (TryConsumeToken(tok::colon, FRI->ColonLoc)) { |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1643 | IsForRangeLoop = true; |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1644 | if (Tok.is(tok::l_brace)) |
| 1645 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1646 | else |
| 1647 | FRI->RangeExpr = ParseExpression(); |
| 1648 | } |
| 1649 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1650 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1651 | if (IsForRangeLoop) |
| 1652 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1653 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | cf6e0c8 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 1654 | D.complete(ThisDecl); |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1655 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1658 | SmallVector<Decl *, 8> DeclsInGroup; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1659 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1660 | if (LateParsedAttrs.size() > 0) |
| 1661 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1662 | D.complete(FirstDecl); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1663 | if (FirstDecl) |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1664 | DeclsInGroup.push_back(FirstDecl); |
| 1665 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1666 | bool ExpectSemi = Context != Declarator::ForContext; |
Fariborz Jahanian | 577574a | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 1667 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1668 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 1669 | // error, bail out. |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1670 | SourceLocation CommaLoc; |
| 1671 | while (TryConsumeToken(tok::comma, CommaLoc)) { |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1672 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 1673 | // This comma was followed by a line-break and something which can't be |
| 1674 | // the start of a declarator. The comma was probably a typo for a |
| 1675 | // semicolon. |
| 1676 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 1677 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 1678 | ExpectSemi = false; |
| 1679 | break; |
| 1680 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1681 | |
| 1682 | // Parse the next declarator. |
| 1683 | D.clear(); |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 1684 | D.setCommaLoc(CommaLoc); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1685 | |
| 1686 | // Accept attributes in an init-declarator. In the first declarator in a |
| 1687 | // declaration, these would be part of the declspec. In subsequent |
| 1688 | // declarators, they become part of the declarator itself, so that they |
| 1689 | // don't apply to declarators after *this* one. Examples: |
| 1690 | // short __attribute__((common)) var; -> declspec |
| 1691 | // short var __attribute__((common)); -> declarator |
| 1692 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1693 | MaybeParseGNUAttributes(D); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1694 | |
| 1695 | ParseDeclarator(D); |
Fariborz Jahanian | 372030b | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1696 | if (!D.isInvalidType()) { |
| 1697 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 1698 | D.complete(ThisDecl); |
| 1699 | if (ThisDecl) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1700 | DeclsInGroup.push_back(ThisDecl); |
Fariborz Jahanian | 372030b | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1701 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | if (DeclEnd) |
| 1705 | *DeclEnd = Tok.getLocation(); |
| 1706 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1707 | if (ExpectSemi && |
Chris Lattner | 02f1b61 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 1708 | ExpectAndConsumeSemi(Context == Declarator::FileContext |
| 1709 | ? diag::err_invalid_token_after_toplevel_declarator |
| 1710 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 1390134 | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1711 | // Okay, there was no semicolon and one was expected. If we see a |
| 1712 | // declaration specifier, just assume it was missing and continue parsing. |
| 1713 | // Otherwise things are very confused and we skip to recover. |
| 1714 | if (!isDeclarationSpecifier()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1715 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1716 | TryConsumeToken(tok::semi); |
Chris Lattner | 1390134 | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1717 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1720 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup); |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1723 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1724 | /// declarator. Returns true on an error. |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1725 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1726 | // If a simple-asm-expr is present, parse it. |
| 1727 | if (Tok.is(tok::kw_asm)) { |
| 1728 | SourceLocation Loc; |
| 1729 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1730 | if (AsmLabel.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1731 | SkipUntil(tok::semi, StopBeforeMatch); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1732 | return true; |
| 1733 | } |
| 1734 | |
| 1735 | D.setAsmLabel(AsmLabel.release()); |
| 1736 | D.SetRangeEnd(Loc); |
| 1737 | } |
| 1738 | |
| 1739 | MaybeParseGNUAttributes(D); |
| 1740 | return false; |
| 1741 | } |
| 1742 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1743 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1744 | /// declarator'. This method parses the remainder of the declaration |
| 1745 | /// (including any attributes or initializer, among other things) and |
| 1746 | /// finalizes the declaration. |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 1747 | /// |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 1748 | /// init-declarator: [C99 6.7] |
| 1749 | /// declarator |
| 1750 | /// declarator '=' initializer |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 1751 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1752 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1753 | /// [C++] declarator initializer[opt] |
| 1754 | /// |
| 1755 | /// [C++] initializer: |
| 1756 | /// [C++] '=' initializer-clause |
| 1757 | /// [C++] '(' expression-list ')' |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1758 | /// [C++0x] '=' 'default' [TODO] |
| 1759 | /// [C++0x] '=' 'delete' |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1760 | /// [C++0x] braced-init-list |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1761 | /// |
| 1762 | /// According to the standard grammar, =default and =delete are function |
| 1763 | /// definitions, but that definitely doesn't fit with the parser here. |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 1764 | /// |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1765 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1766 | const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1767 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1768 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1769 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1770 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1771 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1772 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1773 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 1774 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1775 | // Inform the current actions module that we just parsed this declarator. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1776 | Decl *ThisDecl = 0; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1777 | switch (TemplateInfo.Kind) { |
| 1778 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1779 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1780 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1781 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1782 | case ParsedTemplateInfo::Template: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1783 | case ParsedTemplateInfo::ExplicitSpecialization: { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1784 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1785 | *TemplateInfo.TemplateParams, |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1786 | D); |
Larisse Voufo | 833b05a | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 1787 | if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1788 | // Re-direct this decl to refer to the templated decl so that we can |
| 1789 | // initialize it. |
| 1790 | ThisDecl = VT->getTemplatedDecl(); |
| 1791 | break; |
| 1792 | } |
| 1793 | case ParsedTemplateInfo::ExplicitInstantiation: { |
| 1794 | if (Tok.is(tok::semi)) { |
| 1795 | DeclResult ThisRes = Actions.ActOnExplicitInstantiation( |
| 1796 | getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D); |
| 1797 | if (ThisRes.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1798 | SkipUntil(tok::semi, StopBeforeMatch); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1799 | return 0; |
| 1800 | } |
| 1801 | ThisDecl = ThisRes.get(); |
| 1802 | } else { |
| 1803 | // FIXME: This check should be for a variable template instantiation only. |
| 1804 | |
| 1805 | // Check that this is a valid instantiation |
| 1806 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 1807 | // If the declarator-id is not a template-id, issue a diagnostic and |
| 1808 | // recover by ignoring the 'template' keyword. |
| 1809 | Diag(Tok, diag::err_template_defn_explicit_instantiation) |
| 1810 | << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc); |
| 1811 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 1812 | } else { |
| 1813 | SourceLocation LAngleLoc = |
| 1814 | PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
| 1815 | Diag(D.getIdentifierLoc(), |
| 1816 | diag::err_explicit_instantiation_with_definition) |
| 1817 | << SourceRange(TemplateInfo.TemplateLoc) |
| 1818 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
| 1819 | |
| 1820 | // Recover as if it were an explicit specialization. |
| 1821 | TemplateParameterLists FakedParamLists; |
| 1822 | FakedParamLists.push_back(Actions.ActOnTemplateParameterList( |
| 1823 | 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0, |
| 1824 | LAngleLoc)); |
| 1825 | |
| 1826 | ThisDecl = |
| 1827 | Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D); |
| 1828 | } |
| 1829 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1830 | break; |
| 1831 | } |
| 1832 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1833 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 1834 | bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType(); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1835 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1836 | // Parse declarator '=' initializer. |
Richard Trieu | c64d323 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1837 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | 4972a6d | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 1838 | if (isTokenEqualOrEqualTypo()) { |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1839 | ConsumeToken(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1840 | |
Anders Carlsson | 991285e | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1841 | if (Tok.is(tok::kw_delete)) { |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1842 | if (D.isFunctionDeclarator()) |
| 1843 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1844 | << 1 /* delete */; |
| 1845 | else |
| 1846 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Alexis Hunt | 5dafebc | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1847 | } else if (Tok.is(tok::kw_default)) { |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1848 | if (D.isFunctionDeclarator()) |
Sebastian Redl | 46afb55 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 1849 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1850 | << 0 /* default */; |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1851 | else |
| 1852 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1853 | } else { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1854 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1855 | EnterScope(0); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1856 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1857 | } |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1858 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1859 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1860 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Peter Collingbourne | 6b4fdc2 | 2012-07-27 12:56:09 +0000 | [diff] [blame] | 1861 | Actions.FinalizeDeclaration(ThisDecl); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1862 | cutOffParsing(); |
| 1863 | return 0; |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1864 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1865 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1866 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1867 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1868 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1869 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1870 | ExitScope(); |
| 1871 | } |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1872 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1873 | if (Init.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1874 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 604c302 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1875 | Actions.ActOnInitializerError(ThisDecl); |
| 1876 | } else |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1877 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1878 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1879 | } |
| 1880 | } else if (Tok.is(tok::l_paren)) { |
| 1881 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1882 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1883 | T.consumeOpen(); |
| 1884 | |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1885 | ExprVector Exprs; |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1886 | CommaLocsTy CommaLocs; |
| 1887 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1888 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1889 | EnterScope(0); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1890 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1893 | if (ParseExpressionList(Exprs, CommaLocs)) { |
David Blaikie | eae0411 | 2012-10-10 23:15:05 +0000 | [diff] [blame] | 1894 | Actions.ActOnInitializerError(ThisDecl); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1895 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1896 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1897 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1898 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1899 | ExitScope(); |
| 1900 | } |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1901 | } else { |
| 1902 | // Match the ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1903 | T.consumeClose(); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1904 | |
| 1905 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1906 | "Unexpected number of commas!"); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1907 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1908 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1909 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1910 | ExitScope(); |
| 1911 | } |
| 1912 | |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1913 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 1914 | T.getCloseLocation(), |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1915 | Exprs); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1916 | Actions.AddInitializerToDecl(ThisDecl, Initializer.take(), |
| 1917 | /*DirectInit=*/true, TypeContainsAuto); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1918 | } |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1919 | } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) && |
Fariborz Jahanian | 8be1ecd | 2012-07-03 23:22:13 +0000 | [diff] [blame] | 1920 | (!CurParsedObjCImpl || !D.isFunctionDeclarator())) { |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1921 | // Parse C++0x braced-init-list. |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1922 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 1923 | |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1924 | if (D.getCXXScopeSpec().isSet()) { |
| 1925 | EnterScope(0); |
| 1926 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 1927 | } |
| 1928 | |
| 1929 | ExprResult Init(ParseBraceInitializer()); |
| 1930 | |
| 1931 | if (D.getCXXScopeSpec().isSet()) { |
| 1932 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 1933 | ExitScope(); |
| 1934 | } |
| 1935 | |
| 1936 | if (Init.isInvalid()) { |
| 1937 | Actions.ActOnInitializerError(ThisDecl); |
| 1938 | } else |
| 1939 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1940 | /*DirectInit=*/true, TypeContainsAuto); |
| 1941 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1942 | } else { |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1943 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1946 | Actions.FinalizeDeclaration(ThisDecl); |
| 1947 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1948 | return ThisDecl; |
| 1949 | } |
| 1950 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 1951 | /// ParseSpecifierQualifierList |
| 1952 | /// specifier-qualifier-list: |
| 1953 | /// type-specifier specifier-qualifier-list[opt] |
| 1954 | /// type-qualifier specifier-qualifier-list[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 1955 | /// [GNU] attributes specifier-qualifier-list[opt] |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 1956 | /// |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1957 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 1958 | DeclSpecContext DSC) { |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 1959 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 1960 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1961 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1962 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1963 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 1964 | // Validate declspec for type-name. |
| 1965 | unsigned Specs = DS.getParsedSpecifiers(); |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 1966 | if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) { |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1967 | Diag(Tok, diag::err_expected_type); |
| 1968 | DS.SetTypeSpecError(); |
| 1969 | } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
| 1970 | !DS.hasAttributes()) { |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 1971 | Diag(Tok, diag::err_typename_requires_specqual); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1972 | if (!DS.hasTypeSpecifier()) |
| 1973 | DS.SetTypeSpecError(); |
| 1974 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1975 | |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 1976 | // Issue diagnostic and remove storage class if present. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 1977 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 1978 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1979 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 1980 | else |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 1981 | Diag(DS.getThreadStorageClassSpecLoc(), |
| 1982 | diag::err_typename_invalid_storageclass); |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 1983 | DS.ClearStorageClassSpecs(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 1984 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1985 | |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 1986 | // Issue diagnostic and remove function specfier if present. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 1987 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1988 | if (DS.isInlineSpecified()) |
| 1989 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1990 | if (DS.isVirtualSpecified()) |
| 1991 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1992 | if (DS.isExplicitSpecified()) |
| 1993 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 1994 | DS.ClearFunctionSpecs(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 1995 | } |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1996 | |
| 1997 | // Issue diagnostic and remove constexpr specfier if present. |
| 1998 | if (DS.isConstexprSpecified()) { |
| 1999 | Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr); |
| 2000 | DS.ClearConstexprSpec(); |
| 2001 | } |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2002 | } |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 2003 | |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2004 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 2005 | /// specified token is valid after the identifier in a declarator which |
| 2006 | /// immediately follows the declspec. For example, these things are valid: |
| 2007 | /// |
| 2008 | /// int x [ 4]; // direct-declarator |
| 2009 | /// int x ( int y); // direct-declarator |
| 2010 | /// int(int x ) // direct-declarator |
| 2011 | /// int x ; // simple-declaration |
| 2012 | /// int x = 17; // init-declarator-list |
| 2013 | /// int x , y; // init-declarator-list |
| 2014 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | a723ba9 | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 2015 | /// int x : 4; // struct-declarator |
Chris Lattner | 2b988c1 | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 2016 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2017 | /// |
| 2018 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 2019 | /// ')' happens to be valid anyway). |
| 2020 | /// int (x) |
| 2021 | /// |
| 2022 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 2023 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 2024 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | a723ba9 | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 2025 | 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] | 2026 | } |
| 2027 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2028 | |
| 2029 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 2030 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 2031 | /// the declspec has no type specifier. In this case, the declspec is either |
| 2032 | /// malformed or is "implicit int" (in K&R and C89). |
| 2033 | /// |
| 2034 | /// This method handles diagnosing this prettily and returns false if the |
| 2035 | /// declspec is done being processed. If it recovers and thinks there may be |
| 2036 | /// other pieces of declspec after it, it returns true. |
| 2037 | /// |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2038 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2039 | const ParsedTemplateInfo &TemplateInfo, |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2040 | AccessSpecifier AS, DeclSpecContext DSC, |
| 2041 | ParsedAttributesWithRange &Attrs) { |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2042 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2043 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2044 | SourceLocation Loc = Tok.getLocation(); |
| 2045 | // If we see an identifier that is not a type name, we normally would |
| 2046 | // parse it as the identifer being declared. However, when a typename |
| 2047 | // is typo'd or the definition is not included, this will incorrectly |
| 2048 | // parse the typename as the identifier name and fall over misparsing |
| 2049 | // later parts of the diagnostic. |
| 2050 | // |
| 2051 | // As such, we try to do some look-ahead in cases where this would |
| 2052 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 2053 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 2054 | // an identifier with implicit int, we'd get a parse error because the |
| 2055 | // next token is obviously invalid for a type. Parse these as a case |
| 2056 | // with an invalid type specifier. |
| 2057 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2058 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2059 | // 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] | 2060 | // error, do lookahead to try to do better recovery. This never applies |
| 2061 | // within a type specifier. Outside of C++, we allow this even if the |
| 2062 | // language doesn't "officially" support implicit int -- we support |
Richard Smith | 3b87038 | 2013-04-30 22:43:51 +0000 | [diff] [blame] | 2063 | // implicit int as an extension in C99 and C11. |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 2064 | if (!isTypeSpecifier(DSC) && !getLangOpts().CPlusPlus && |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2065 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2066 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 2067 | // we just avoid eating the identifier, so it will be parsed as the |
| 2068 | // identifier in the declarator. |
| 2069 | return false; |
| 2070 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2071 | |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2072 | if (getLangOpts().CPlusPlus && |
| 2073 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| 2074 | // Don't require a type specifier if we have the 'auto' storage class |
| 2075 | // specifier in C++98 -- we'll promote it to a type specifier. |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2076 | if (SS) |
| 2077 | AnnotateScopeToken(*SS, /*IsNewAnnotation*/false); |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2078 | return false; |
| 2079 | } |
| 2080 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2081 | // Otherwise, if we don't consume this token, we are going to emit an |
| 2082 | // error anyway. Try to recover from various common problems. Check |
| 2083 | // to see if this was a reference to a tag name without a tag specified. |
| 2084 | // 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] | 2085 | // |
| 2086 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 2087 | if (SS == 0) { |
Argyrios Kyrtzidis | 1f32940 | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2088 | const char *TagName = 0, *FixitTagName = 0; |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2089 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2090 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2091 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2092 | default: break; |
Argyrios Kyrtzidis | 1f32940 | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2093 | case DeclSpec::TST_enum: |
| 2094 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 2095 | case DeclSpec::TST_union: |
| 2096 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 2097 | case DeclSpec::TST_struct: |
| 2098 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2099 | case DeclSpec::TST_interface: |
| 2100 | TagName="__interface"; FixitTagName = "__interface "; |
| 2101 | TagKind=tok::kw___interface;break; |
Argyrios Kyrtzidis | 1f32940 | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2102 | case DeclSpec::TST_class: |
| 2103 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2104 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2105 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2106 | if (TagName) { |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2107 | IdentifierInfo *TokenName = Tok.getIdentifierInfo(); |
| 2108 | LookupResult R(Actions, TokenName, SourceLocation(), |
| 2109 | Sema::LookupOrdinaryName); |
| 2110 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2111 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2112 | << TokenName << TagName << getLangOpts().CPlusPlus |
| 2113 | << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName); |
| 2114 | |
| 2115 | if (Actions.LookupParsedName(R, getCurScope(), SS)) { |
| 2116 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); |
| 2117 | I != IEnd; ++I) |
Kaelyn Uhrain | 3fe3f85 | 2012-04-27 18:26:49 +0000 | [diff] [blame] | 2118 | Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2119 | << TokenName << TagName; |
| 2120 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2121 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2122 | // Parse this as a tag as if the missing tag were present. |
| 2123 | if (TagKind == tok::kw_enum) |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2124 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal); |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2125 | else |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2126 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2127 | /*EnteringContext*/ false, DSC_normal, Attrs); |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2128 | return true; |
| 2129 | } |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2130 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2131 | |
Richard Smith | fe904f0 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2132 | // Determine whether this identifier could plausibly be the name of something |
Richard Smith | edd124e | 2012-05-15 21:42:17 +0000 | [diff] [blame] | 2133 | // being declared (with a missing type). |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 2134 | if (!isTypeSpecifier(DSC) && |
Richard Smith | fe904f0 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2135 | (!SS || DSC == DSC_top_level || DSC == DSC_class)) { |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2136 | // Look ahead to the next token to try to figure out what this declaration |
| 2137 | // was supposed to be. |
| 2138 | switch (NextToken().getKind()) { |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2139 | case tok::l_paren: { |
| 2140 | // static x(4); // 'x' is not a type |
| 2141 | // x(int n); // 'x' is not a type |
| 2142 | // x (*p)[]; // 'x' is a type |
| 2143 | // |
| 2144 | // Since we're in an error case (or the rare 'implicit int in C++' MS |
| 2145 | // extension), we can afford to perform a tentative parse to determine |
| 2146 | // which case we're in. |
| 2147 | TentativeParsingAction PA(*this); |
| 2148 | ConsumeToken(); |
| 2149 | TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false); |
| 2150 | PA.Revert(); |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2151 | |
| 2152 | if (TPR != TPResult::False()) { |
| 2153 | // The identifier is followed by a parenthesized declarator. |
| 2154 | // It's supposed to be a type. |
| 2155 | break; |
| 2156 | } |
| 2157 | |
| 2158 | // If we're in a context where we could be declaring a constructor, |
| 2159 | // check whether this is a constructor declaration with a bogus name. |
| 2160 | if (DSC == DSC_class || (DSC == DSC_top_level && SS)) { |
| 2161 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2162 | if (Actions.isCurrentClassNameTypo(II, SS)) { |
| 2163 | Diag(Loc, diag::err_constructor_bad_name) |
| 2164 | << Tok.getIdentifierInfo() << II |
| 2165 | << FixItHint::CreateReplacement(Tok.getLocation(), II->getName()); |
| 2166 | Tok.setIdentifierInfo(II); |
| 2167 | } |
| 2168 | } |
| 2169 | // Fall through. |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2170 | } |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2171 | case tok::comma: |
| 2172 | case tok::equal: |
| 2173 | case tok::kw_asm: |
| 2174 | case tok::l_brace: |
| 2175 | case tok::l_square: |
| 2176 | case tok::semi: |
| 2177 | // This looks like a variable or function declaration. The type is |
| 2178 | // probably missing. We're done parsing decl-specifiers. |
| 2179 | if (SS) |
| 2180 | AnnotateScopeToken(*SS, /*IsNewAnnotation*/false); |
| 2181 | return false; |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2182 | |
| 2183 | default: |
| 2184 | // This is probably supposed to be a type. This includes cases like: |
| 2185 | // int f(itn); |
| 2186 | // struct S { unsinged : 4; }; |
| 2187 | break; |
| 2188 | } |
| 2189 | } |
| 2190 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2191 | // This is almost certainly an invalid type name. Let the action emit a |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2192 | // diagnostic and attempt to recover. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2193 | ParsedType T; |
Kaelyn Uhrain | b5b17fe | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2194 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Kaelyn Uhrain | 67b44c9 | 2014-02-13 20:14:07 +0000 | [diff] [blame] | 2195 | if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T, |
| 2196 | getLangOpts().CPlusPlus && |
| 2197 | NextToken().is(tok::less))) { |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2198 | // The action emitted a diagnostic, so we don't have to. |
| 2199 | if (T) { |
| 2200 | // The action has suggested that the type T could be used. Set that as |
| 2201 | // the type in the declaration specifiers, consume the would-be type |
| 2202 | // name token, and we're done. |
| 2203 | const char *PrevSpec; |
| 2204 | unsigned DiagID; |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2205 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T, |
| 2206 | Actions.getASTContext().getPrintingPolicy()); |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2207 | DS.SetRangeEnd(Tok.getLocation()); |
| 2208 | ConsumeToken(); |
Kaelyn Uhrain | b5b17fe | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2209 | // There may be other declaration specifiers after this. |
| 2210 | return true; |
| 2211 | } else if (II != Tok.getIdentifierInfo()) { |
| 2212 | // If no type was suggested, the correction is to a keyword |
| 2213 | Tok.setKind(II->getTokenID()); |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2214 | // There may be other declaration specifiers after this. |
| 2215 | return true; |
| 2216 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2217 | |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2218 | // Fall through; the action had no suggestion for us. |
| 2219 | } else { |
| 2220 | // The action did not emit a diagnostic, so emit one now. |
| 2221 | SourceRange R; |
| 2222 | if (SS) R = SS->getRange(); |
| 2223 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 2224 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2225 | |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2226 | // Mark this as an error. |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2227 | DS.SetTypeSpecError(); |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2228 | DS.SetRangeEnd(Tok.getLocation()); |
| 2229 | ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2230 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2231 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 2232 | // avoid rippling error messages on subsequent uses of the same type, |
| 2233 | // could be useful if #include was forgotten. |
| 2234 | return false; |
| 2235 | } |
| 2236 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2237 | /// \brief Determine the declaration specifier context from the declarator |
| 2238 | /// context. |
| 2239 | /// |
| 2240 | /// \param Context the declarator context, which is one of the |
| 2241 | /// Declarator::TheContext enumerator values. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2242 | Parser::DeclSpecContext |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2243 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 2244 | if (Context == Declarator::MemberContext) |
| 2245 | return DSC_class; |
| 2246 | if (Context == Declarator::FileContext) |
| 2247 | return DSC_top_level; |
Richard Smith | 62dad82 | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 2248 | if (Context == Declarator::TrailingReturnContext) |
| 2249 | return DSC_trailing; |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 2250 | if (Context == Declarator::AliasDeclContext || |
| 2251 | Context == Declarator::AliasTemplateContext) |
| 2252 | return DSC_alias_declaration; |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2253 | return DSC_normal; |
| 2254 | } |
| 2255 | |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2256 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 2257 | /// |
| 2258 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 2259 | /// type. Ideally, the type should be propagated directly into Sema. |
| 2260 | /// |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2261 | /// [C11] type-id |
| 2262 | /// [C11] constant-expression |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2263 | /// [C++0x] type-id ...[opt] |
| 2264 | /// [C++0x] assignment-expression ...[opt] |
| 2265 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 2266 | SourceLocation &EllipsisLoc) { |
| 2267 | ExprResult ER; |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2268 | if (isTypeIdInParens()) { |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2269 | SourceLocation TypeLoc = Tok.getLocation(); |
| 2270 | ParsedType Ty = ParseTypeName().get(); |
| 2271 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2272 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 2273 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2274 | } else |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2275 | ER = ParseConstantExpression(); |
| 2276 | |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 2277 | if (getLangOpts().CPlusPlus11) |
| 2278 | TryConsumeToken(tok::ellipsis, EllipsisLoc); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2279 | |
| 2280 | return ER; |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 2284 | /// attribute to Attrs. |
| 2285 | /// |
| 2286 | /// alignment-specifier: |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2287 | /// [C11] '_Alignas' '(' type-id ')' |
| 2288 | /// [C11] '_Alignas' '(' constant-expression ')' |
Richard Smith | d11c7a1 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2289 | /// [C++11] 'alignas' '(' type-id ...[opt] ')' |
| 2290 | /// [C++11] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2291 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2292 | SourceLocation *EndLoc) { |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2293 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 2294 | "Not an alignment-specifier!"); |
| 2295 | |
Richard Smith | d11c7a1 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2296 | IdentifierInfo *KWName = Tok.getIdentifierInfo(); |
| 2297 | SourceLocation KWLoc = ConsumeToken(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2298 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2299 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 2300 | if (T.expectAndConsume()) |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2301 | return; |
| 2302 | |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2303 | SourceLocation EllipsisLoc; |
| 2304 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2305 | if (ArgExpr.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2306 | T.skipToEnd(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2307 | return; |
| 2308 | } |
| 2309 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2310 | T.consumeClose(); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2311 | if (EndLoc) |
| 2312 | *EndLoc = T.getCloseLocation(); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2313 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2314 | ArgsVector ArgExprs; |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2315 | ArgExprs.push_back(ArgExpr.release()); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2316 | Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1, |
| 2317 | AttributeList::AS_Keyword, EllipsisLoc); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2318 | } |
| 2319 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2320 | /// Determine whether we're looking at something that might be a declarator |
| 2321 | /// in a simple-declaration. If it can't possibly be a declarator, maybe |
| 2322 | /// diagnose a missing semicolon after a prior tag definition in the decl |
| 2323 | /// specifier. |
| 2324 | /// |
| 2325 | /// \return \c true if an error occurred and this can't be any kind of |
| 2326 | /// declaration. |
| 2327 | bool |
| 2328 | Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS, |
| 2329 | DeclSpecContext DSContext, |
| 2330 | LateParsedAttrList *LateAttrs) { |
| 2331 | assert(DS.hasTagDefinition() && "shouldn't call this"); |
| 2332 | |
| 2333 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2334 | |
| 2335 | if (getLangOpts().CPlusPlus && |
| 2336 | (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) || |
| 2337 | Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) && |
| 2338 | TryAnnotateCXXScopeToken(EnteringContext)) { |
| 2339 | SkipMalformedDecl(); |
| 2340 | return true; |
| 2341 | } |
| 2342 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2343 | bool HasScope = Tok.is(tok::annot_cxxscope); |
| 2344 | // Make a copy in case GetLookAheadToken invalidates the result of NextToken. |
| 2345 | Token AfterScope = HasScope ? NextToken() : Tok; |
| 2346 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2347 | // Determine whether the following tokens could possibly be a |
| 2348 | // declarator. |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2349 | bool MightBeDeclarator = true; |
| 2350 | if (Tok.is(tok::kw_typename) || Tok.is(tok::annot_typename)) { |
| 2351 | // A declarator-id can't start with 'typename'. |
| 2352 | MightBeDeclarator = false; |
| 2353 | } else if (AfterScope.is(tok::annot_template_id)) { |
| 2354 | // If we have a type expressed as a template-id, this cannot be a |
| 2355 | // declarator-id (such a type cannot be redeclared in a simple-declaration). |
| 2356 | TemplateIdAnnotation *Annot = |
| 2357 | static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue()); |
| 2358 | if (Annot->Kind == TNK_Type_template) |
| 2359 | MightBeDeclarator = false; |
| 2360 | } else if (AfterScope.is(tok::identifier)) { |
| 2361 | const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken(); |
| 2362 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2363 | // These tokens cannot come after the declarator-id in a |
| 2364 | // simple-declaration, and are likely to come after a type-specifier. |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2365 | if (Next.is(tok::star) || Next.is(tok::amp) || Next.is(tok::ampamp) || |
| 2366 | Next.is(tok::identifier) || Next.is(tok::annot_cxxscope) || |
| 2367 | Next.is(tok::coloncolon)) { |
| 2368 | // Missing a semicolon. |
| 2369 | MightBeDeclarator = false; |
| 2370 | } else if (HasScope) { |
| 2371 | // If the declarator-id has a scope specifier, it must redeclare a |
| 2372 | // previously-declared entity. If that's a type (and this is not a |
| 2373 | // typedef), that's an error. |
| 2374 | CXXScopeSpec SS; |
| 2375 | Actions.RestoreNestedNameSpecifierAnnotation( |
| 2376 | Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS); |
| 2377 | IdentifierInfo *Name = AfterScope.getIdentifierInfo(); |
| 2378 | Sema::NameClassification Classification = Actions.ClassifyName( |
| 2379 | getCurScope(), SS, Name, AfterScope.getLocation(), Next, |
| 2380 | /*IsAddressOfOperand*/false); |
| 2381 | switch (Classification.getKind()) { |
| 2382 | case Sema::NC_Error: |
| 2383 | SkipMalformedDecl(); |
| 2384 | return true; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2385 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2386 | case Sema::NC_Keyword: |
| 2387 | case Sema::NC_NestedNameSpecifier: |
| 2388 | llvm_unreachable("typo correction and nested name specifiers not " |
| 2389 | "possible here"); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2390 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2391 | case Sema::NC_Type: |
| 2392 | case Sema::NC_TypeTemplate: |
| 2393 | // Not a previously-declared non-type entity. |
| 2394 | MightBeDeclarator = false; |
| 2395 | break; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2396 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2397 | case Sema::NC_Unknown: |
| 2398 | case Sema::NC_Expression: |
| 2399 | case Sema::NC_VarTemplate: |
| 2400 | case Sema::NC_FunctionTemplate: |
| 2401 | // Might be a redeclaration of a prior entity. |
| 2402 | break; |
| 2403 | } |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2404 | } |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2405 | } |
| 2406 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2407 | if (MightBeDeclarator) |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2408 | return false; |
| 2409 | |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2410 | const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy(); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2411 | Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()), |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 2412 | diag::err_expected_after) |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2413 | << DeclSpec::getSpecifierName(DS.getTypeSpecType(), PPol) << tok::semi; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2414 | |
| 2415 | // Try to recover from the typo, by dropping the tag definition and parsing |
| 2416 | // the problematic tokens as a type. |
| 2417 | // |
| 2418 | // FIXME: Split the DeclSpec into pieces for the standalone |
| 2419 | // declaration and pieces for the following declaration, instead |
| 2420 | // of assuming that all the other pieces attach to new declaration, |
| 2421 | // and call ParsedFreeStandingDeclSpec as appropriate. |
| 2422 | DS.ClearTypeSpecType(); |
| 2423 | ParsedTemplateInfo NotATemplate; |
| 2424 | ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs); |
| 2425 | return false; |
| 2426 | } |
| 2427 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2428 | /// ParseDeclarationSpecifiers |
| 2429 | /// declaration-specifiers: [C99 6.7] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2430 | /// storage-class-specifier declaration-specifiers[opt] |
| 2431 | /// type-specifier declaration-specifiers[opt] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2432 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2433 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 2434 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2435 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2436 | /// |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2437 | /// storage-class-specifier: [C99 6.7.1] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 2438 | /// 'typedef' |
| 2439 | /// 'extern' |
| 2440 | /// 'static' |
| 2441 | /// 'auto' |
| 2442 | /// 'register' |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2443 | /// [C++] 'mutable' |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2444 | /// [C++11] 'thread_local' |
| 2445 | /// [C11] '_Thread_local' |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 2446 | /// [GNU] '__thread' |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2447 | /// function-specifier: [C99 6.7.4] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2448 | /// [C99] 'inline' |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2449 | /// [C++] 'virtual' |
| 2450 | /// [C++] 'explicit' |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2451 | /// [OpenCL] '__kernel' |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2452 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 39c2a8b | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2453 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2454 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2455 | /// |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 2456 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2457 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2458 | AccessSpecifier AS, |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2459 | DeclSpecContext DSContext, |
| 2460 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2461 | if (DS.getSourceRange().isInvalid()) { |
| 2462 | DS.SetRangeStart(Tok.getLocation()); |
| 2463 | DS.SetRangeEnd(Tok.getLocation()); |
| 2464 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2465 | |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2466 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2467 | bool AttrsLastTime = false; |
| 2468 | ParsedAttributesWithRange attrs(AttrFactory); |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2469 | const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2470 | while (1) { |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2471 | bool isInvalid = false; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2472 | const char *PrevSpec = 0; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2473 | unsigned DiagID = 0; |
| 2474 | |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 2475 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2476 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2477 | switch (Tok.getKind()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2478 | default: |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2479 | DoneWithDeclSpec: |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2480 | if (!AttrsLastTime) |
| 2481 | ProhibitAttributes(attrs); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2482 | else { |
| 2483 | // Reject C++11 attributes that appertain to decl specifiers as |
| 2484 | // we don't support any C++11 attributes that appertain to decl |
| 2485 | // specifiers. This also conforms to what g++ 4.8 is doing. |
| 2486 | ProhibitCXX11Attributes(attrs); |
| 2487 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2488 | DS.takeAttributesFrom(attrs); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2489 | } |
Peter Collingbourne | 70188b3 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 2490 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2491 | // If this is not a declaration specifier token, we're done reading decl |
| 2492 | // specifiers. First verify that DeclSpec's are consistent. |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2493 | DS.Finish(Diags, PP, Policy); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2494 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2495 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2496 | case tok::l_square: |
| 2497 | case tok::kw_alignas: |
Richard Smith | 4cabd04 | 2013-02-22 09:15:49 +0000 | [diff] [blame] | 2498 | if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier()) |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2499 | goto DoneWithDeclSpec; |
| 2500 | |
| 2501 | ProhibitAttributes(attrs); |
| 2502 | // FIXME: It would be good to recover by accepting the attributes, |
| 2503 | // but attempting to do that now would cause serious |
| 2504 | // madness in terms of diagnostics. |
| 2505 | attrs.clear(); |
| 2506 | attrs.Range = SourceRange(); |
| 2507 | |
| 2508 | ParseCXX11Attributes(attrs); |
| 2509 | AttrsLastTime = true; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2510 | continue; |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2511 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2512 | case tok::code_completion: { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2513 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2514 | if (DS.hasTypeSpecifier()) { |
| 2515 | bool AllowNonIdentifiers |
| 2516 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 2517 | Scope::BlockScope | |
| 2518 | Scope::TemplateParamScope | |
| 2519 | Scope::FunctionPrototypeScope | |
| 2520 | Scope::AtCatchScope)) == 0; |
| 2521 | bool AllowNestedNameSpecifiers |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2522 | = DSContext == DSC_top_level || |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2523 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 2524 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2525 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2526 | AllowNonIdentifiers, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2527 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2528 | return cutOffParsing(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2529 | } |
| 2530 | |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2531 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 2532 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 2533 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2534 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2535 | : Sema::PCC_Template; |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2536 | else if (DSContext == DSC_class) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2537 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | b6c6a58 | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 2538 | else if (CurParsedObjCImpl) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2539 | CCC = Sema::PCC_ObjCImplementation; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2540 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2541 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2542 | return cutOffParsing(); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2543 | } |
| 2544 | |
Chris Lattner | bd31aa3 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2545 | case tok::coloncolon: // ::foo::bar |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2546 | // C++ scope specifier. Annotate and loop, or bail out on error. |
Eli Friedman | 2a1d9a9 | 2013-08-15 23:59:20 +0000 | [diff] [blame] | 2547 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2548 | if (!DS.hasTypeSpecifier()) |
| 2549 | DS.SetTypeSpecError(); |
| 2550 | goto DoneWithDeclSpec; |
| 2551 | } |
John McCall | 8bc2a70 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 2552 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 2553 | goto DoneWithDeclSpec; |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2554 | continue; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2555 | |
| 2556 | case tok::annot_cxxscope: { |
Richard Smith | 3092a3b | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2557 | if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector()) |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2558 | goto DoneWithDeclSpec; |
| 2559 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2560 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 2561 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 2562 | Tok.getAnnotationRange(), |
| 2563 | SS); |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2564 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2565 | // We are looking for a qualified typename. |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2566 | Token Next = NextToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2567 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2568 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2569 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2570 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2571 | |
| 2572 | // C++ [class.qual]p2: |
| 2573 | // In a lookup in which the constructor is an acceptable lookup |
| 2574 | // result and the nested-name-specifier nominates a class C: |
| 2575 | // |
| 2576 | // - if the name specified after the |
| 2577 | // nested-name-specifier, when looked up in C, is the |
| 2578 | // injected-class-name of C (Clause 9), or |
| 2579 | // |
| 2580 | // - if the name specified after the nested-name-specifier |
| 2581 | // is the same as the identifier or the |
| 2582 | // simple-template-id's template-name in the last |
| 2583 | // component of the nested-name-specifier, |
| 2584 | // |
| 2585 | // the name is instead considered to name the constructor of |
| 2586 | // class C. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2587 | // |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2588 | // Thus, if the template-name is actually the constructor |
| 2589 | // name, then the code is ill-formed; this interpretation is |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2590 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | c0c5dd2 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2591 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
Dmitri Gribenko | d1c91f1 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2592 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2593 | TemplateId->Name && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2594 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 2595 | if (isConstructorDeclarator(/*Unqualified*/false)) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2596 | // The user meant this to be an out-of-line constructor |
| 2597 | // definition, but template arguments are not allowed |
| 2598 | // there. Just allow this as a constructor; we'll |
| 2599 | // complain about it later. |
| 2600 | goto DoneWithDeclSpec; |
| 2601 | } |
| 2602 | |
| 2603 | // The user meant this to name a type, but it actually names |
| 2604 | // a constructor with some extraneous template |
| 2605 | // arguments. Complain, then parse it as a type as the user |
| 2606 | // intended. |
| 2607 | Diag(TemplateId->TemplateNameLoc, |
| 2608 | diag::err_out_of_line_template_id_names_constructor) |
| 2609 | << TemplateId->Name; |
| 2610 | } |
| 2611 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2612 | DS.getTypeSpecScope() = SS; |
| 2613 | ConsumeToken(); // The C++ scope. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2614 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2615 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2616 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2617 | continue; |
| 2618 | } |
| 2619 | |
Douglas Gregor | c5790df | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2620 | if (Next.is(tok::annot_typename)) { |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2621 | DS.getTypeSpecScope() = SS; |
| 2622 | ConsumeToken(); // The C++ scope. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2623 | if (Tok.getAnnotationValue()) { |
| 2624 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 7743034 | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2625 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2626 | Tok.getAnnotationEndLoc(), |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2627 | PrevSpec, DiagID, T, Policy); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2628 | if (isInvalid) |
| 2629 | break; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2630 | } |
Douglas Gregor | c5790df | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2631 | else |
| 2632 | DS.SetTypeSpecError(); |
| 2633 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2634 | ConsumeToken(); // The typename |
| 2635 | } |
| 2636 | |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2637 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2638 | goto DoneWithDeclSpec; |
| 2639 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2640 | // If we're in a context where the identifier could be a class name, |
| 2641 | // check whether this is a constructor declaration. |
Dmitri Gribenko | d1c91f1 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2642 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2643 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2644 | &SS)) { |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 2645 | if (isConstructorDeclarator(/*Unqualified*/false)) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2646 | goto DoneWithDeclSpec; |
| 2647 | |
| 2648 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 2649 | // of the class is qualified in a context where it could name |
| 2650 | // a constructor, its a constructor name. However, we've |
| 2651 | // looked at the declarator, and the user probably meant this |
| 2652 | // to be a type. Complain that it isn't supposed to be treated |
| 2653 | // as a type, then proceed to parse it as a type. |
| 2654 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 2655 | << Next.getIdentifierInfo(); |
| 2656 | } |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2657 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2658 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 2659 | Next.getLocation(), |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2660 | getCurScope(), &SS, |
| 2661 | false, false, ParsedType(), |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2662 | /*IsCtorOrDtorName=*/false, |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2663 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2664 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2665 | // If the referenced identifier is not a type, then this declspec is |
| 2666 | // erroneous: We already checked about that it has no type specifier, and |
| 2667 | // 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] | 2668 | // typename. |
David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 2669 | if (!TypeRep) { |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2670 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2671 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2672 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) { |
| 2673 | if (!Attrs.empty()) { |
| 2674 | AttrsLastTime = true; |
| 2675 | attrs.takeAllFrom(Attrs); |
| 2676 | } |
| 2677 | continue; |
| 2678 | } |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2679 | goto DoneWithDeclSpec; |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2680 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2681 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2682 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2683 | ConsumeToken(); // The C++ scope. |
| 2684 | |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2685 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2686 | DiagID, TypeRep, Policy); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2687 | if (isInvalid) |
| 2688 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2689 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2690 | DS.SetRangeEnd(Tok.getLocation()); |
| 2691 | ConsumeToken(); // The typename. |
| 2692 | |
| 2693 | continue; |
| 2694 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2695 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2696 | case tok::annot_typename: { |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2697 | // If we've previously seen a tag definition, we were almost surely |
| 2698 | // missing a semicolon after it. |
| 2699 | if (DS.hasTypeSpecifier() && DS.hasTagDefinition()) |
| 2700 | goto DoneWithDeclSpec; |
| 2701 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2702 | if (Tok.getAnnotationValue()) { |
| 2703 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 7f8bb36 | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 2704 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2705 | DiagID, T, Policy); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2706 | } else |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2707 | DS.SetTypeSpecError(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2708 | |
Chris Lattner | 005fc1b | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 2709 | if (isInvalid) |
| 2710 | break; |
| 2711 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2712 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2713 | ConsumeToken(); // The typename |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2714 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2715 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2716 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2717 | // Objective-C interface. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2718 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 06e41ae | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2719 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2720 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2721 | continue; |
| 2722 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2723 | |
Douglas Gregor | 0687309 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2724 | case tok::kw___is_signed: |
| 2725 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 2726 | // typically treats it as a trait. If we see __is_signed as it appears |
| 2727 | // in libstdc++, e.g., |
| 2728 | // |
| 2729 | // static const bool __is_signed; |
| 2730 | // |
| 2731 | // then treat __is_signed as an identifier rather than as a keyword. |
| 2732 | if (DS.getTypeSpecType() == TST_bool && |
| 2733 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
Alp Toker | 47642d2 | 2013-12-03 06:13:01 +0000 | [diff] [blame] | 2734 | DS.getStorageClassSpec() == DeclSpec::SCS_static) |
| 2735 | TryKeywordIdentFallback(true); |
Douglas Gregor | 0687309 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2736 | |
| 2737 | // We're done with the declaration-specifiers. |
| 2738 | goto DoneWithDeclSpec; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2739 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2740 | // typedef-name |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2741 | case tok::kw_decltype: |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2742 | case tok::identifier: { |
Chris Lattner | bd31aa3 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2743 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 2744 | // so handle it as such. This is important for ctor parsing. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2745 | if (getLangOpts().CPlusPlus) { |
Eli Friedman | 2a1d9a9 | 2013-08-15 23:59:20 +0000 | [diff] [blame] | 2746 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2747 | if (!DS.hasTypeSpecifier()) |
| 2748 | DS.SetTypeSpecError(); |
| 2749 | goto DoneWithDeclSpec; |
| 2750 | } |
| 2751 | if (!Tok.is(tok::identifier)) |
| 2752 | continue; |
| 2753 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2754 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2755 | // This identifier can only be a typedef name if we haven't already seen |
| 2756 | // a type-specifier. Without this check we misparse: |
| 2757 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 2758 | if (DS.hasTypeSpecifier()) |
| 2759 | goto DoneWithDeclSpec; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2760 | |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2761 | // Check for need to substitute AltiVec keyword tokens. |
| 2762 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2763 | break; |
| 2764 | |
Richard Smith | 3092a3b | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2765 | // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not |
| 2766 | // allow the use of a typedef name as a type specifier. |
| 2767 | if (DS.isTypeAltiVecVector()) |
| 2768 | goto DoneWithDeclSpec; |
| 2769 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2770 | ParsedType TypeRep = |
| 2771 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 2772 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2773 | |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2774 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 2775 | // it must be an implicit int or an error. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2776 | if (!TypeRep) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2777 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2778 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) { |
| 2779 | if (!Attrs.empty()) { |
| 2780 | AttrsLastTime = true; |
| 2781 | attrs.takeAllFrom(Attrs); |
| 2782 | } |
| 2783 | continue; |
| 2784 | } |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2785 | goto DoneWithDeclSpec; |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2786 | } |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2787 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2788 | // If we're in a context where the identifier could be a class name, |
| 2789 | // check whether this is a constructor declaration. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2790 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2791 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 2792 | isConstructorDeclarator(/*Unqualified*/true)) |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2793 | goto DoneWithDeclSpec; |
| 2794 | |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2795 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2796 | DiagID, TypeRep, Policy); |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2797 | if (isInvalid) |
| 2798 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2799 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2800 | DS.SetRangeEnd(Tok.getLocation()); |
| 2801 | ConsumeToken(); // The identifier |
| 2802 | |
| 2803 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2804 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2805 | // Objective-C interface. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2806 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 06e41ae | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2807 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2808 | |
Steve Naroff | cd5e782 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 2809 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2810 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2811 | continue; |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2812 | } |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2813 | |
| 2814 | // type-name |
| 2815 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | c0c5dd2 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2816 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2817 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2818 | // This template-id does not refer to a type name, so we're |
| 2819 | // done with the type-specifiers. |
| 2820 | goto DoneWithDeclSpec; |
| 2821 | } |
| 2822 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2823 | // If we're in a context where the template-id could be a |
| 2824 | // constructor name or specialization, check whether this is a |
| 2825 | // constructor declaration. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2826 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2827 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 2828 | isConstructorDeclarator(TemplateId->SS.isEmpty())) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2829 | goto DoneWithDeclSpec; |
| 2830 | |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2831 | // Turn the template-id annotation token into a type annotation |
| 2832 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2833 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2834 | continue; |
| 2835 | } |
| 2836 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 2837 | // GNU attributes support. |
| 2838 | case tok::kw___attribute: |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2839 | ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs); |
Chris Lattner | b95cca0 | 2006-10-17 03:01:08 +0000 | [diff] [blame] | 2840 | continue; |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2841 | |
| 2842 | // Microsoft declspec support. |
| 2843 | case tok::kw___declspec: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2844 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2845 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2846 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2847 | // Microsoft single token adornments. |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2848 | case tok::kw___forceinline: { |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2849 | isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID); |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2850 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2851 | SourceLocation AttrNameLoc = Tok.getLocation(); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2852 | DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
Aaron Ballman | 3fe6ed5 | 2014-01-13 21:40:16 +0000 | [diff] [blame] | 2853 | AttributeList::AS_Keyword); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2854 | break; |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2855 | } |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2856 | |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 2857 | case tok::kw___sptr: |
| 2858 | case tok::kw___uptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2859 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2860 | case tok::kw___ptr32: |
Steve Naroff | f9c29d4 | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2861 | case tok::kw___w64: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2862 | case tok::kw___cdecl: |
| 2863 | case tok::kw___stdcall: |
| 2864 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2865 | case tok::kw___thiscall: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2866 | case tok::kw___unaligned: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2867 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2868 | continue; |
| 2869 | |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2870 | // Borland single token adornments. |
| 2871 | case tok::kw___pascal: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2872 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2873 | continue; |
| 2874 | |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2875 | // OpenCL single token adornments. |
| 2876 | case tok::kw___kernel: |
| 2877 | ParseOpenCLAttributes(DS.getAttributes()); |
| 2878 | continue; |
| 2879 | |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2880 | // storage-class-specifier |
| 2881 | case tok::kw_typedef: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2882 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2883 | PrevSpec, DiagID, Policy); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2884 | break; |
| 2885 | case tok::kw_extern: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2886 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2887 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2888 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2889 | PrevSpec, DiagID, Policy); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2890 | break; |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2891 | case tok::kw___private_extern__: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2892 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2893 | Loc, PrevSpec, DiagID, Policy); |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2894 | break; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2895 | case tok::kw_static: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2896 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2897 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2898 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2899 | PrevSpec, DiagID, Policy); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2900 | break; |
| 2901 | case tok::kw_auto: |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2902 | if (getLangOpts().CPlusPlus11) { |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2903 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2904 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2905 | PrevSpec, DiagID, Policy); |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2906 | if (!isInvalid) |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2907 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2908 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2909 | } else |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2910 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2911 | DiagID, Policy); |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2912 | } else |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2913 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2914 | PrevSpec, DiagID, Policy); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2915 | break; |
| 2916 | case tok::kw_register: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2917 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2918 | PrevSpec, DiagID, Policy); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2919 | break; |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2920 | case tok::kw_mutable: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2921 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2922 | PrevSpec, DiagID, Policy); |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2923 | break; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2924 | case tok::kw___thread: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2925 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc, |
| 2926 | PrevSpec, DiagID); |
| 2927 | break; |
| 2928 | case tok::kw_thread_local: |
| 2929 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc, |
| 2930 | PrevSpec, DiagID); |
| 2931 | break; |
| 2932 | case tok::kw__Thread_local: |
| 2933 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local, |
| 2934 | Loc, PrevSpec, DiagID); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2935 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2936 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2937 | // function-specifier |
| 2938 | case tok::kw_inline: |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2939 | isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2940 | break; |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2941 | case tok::kw_virtual: |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2942 | isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2943 | break; |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2944 | case tok::kw_explicit: |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2945 | isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2946 | break; |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 2947 | case tok::kw__Noreturn: |
| 2948 | if (!getLangOpts().C11) |
| 2949 | Diag(Loc, diag::ext_c11_noreturn); |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2950 | isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID); |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 2951 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2952 | |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2953 | // alignment-specifier |
| 2954 | case tok::kw__Alignas: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2955 | if (!getLangOpts().C11) |
Jordan Rose | 58d5472 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2956 | Diag(Tok, diag::ext_c11_alignment) << Tok.getName(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2957 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 2958 | continue; |
| 2959 | |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2960 | // friend |
| 2961 | case tok::kw_friend: |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2962 | if (DSContext == DSC_class) |
| 2963 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 2964 | else { |
| 2965 | PrevSpec = ""; // not actually used by the diagnostic |
| 2966 | DiagID = diag::err_friend_invalid_in_context; |
| 2967 | isInvalid = true; |
| 2968 | } |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2969 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2970 | |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2971 | // Modules |
| 2972 | case tok::kw___module_private__: |
| 2973 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 2974 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2975 | |
Sebastian Redl | 39c2a8b | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2976 | // constexpr |
| 2977 | case tok::kw_constexpr: |
| 2978 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 2979 | break; |
| 2980 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2981 | // type-specifier |
| 2982 | case tok::kw_short: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2983 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2984 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2985 | break; |
| 2986 | case tok::kw_long: |
| 2987 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2988 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2989 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2990 | else |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2991 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2992 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2993 | break; |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2994 | case tok::kw___int64: |
| 2995 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2996 | DiagID, Policy); |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2997 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2998 | case tok::kw_signed: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2999 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 3000 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3001 | break; |
| 3002 | case tok::kw_unsigned: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3003 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 3004 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3005 | break; |
| 3006 | case tok::kw__Complex: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3007 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 3008 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3009 | break; |
| 3010 | case tok::kw__Imaginary: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3011 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 3012 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3013 | break; |
| 3014 | case tok::kw_void: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3015 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3016 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3017 | break; |
| 3018 | case tok::kw_char: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3019 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3020 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3021 | break; |
| 3022 | case tok::kw_int: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3023 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, 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; |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3026 | case tok::kw___int128: |
| 3027 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3028 | DiagID, Policy); |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3029 | break; |
| 3030 | case tok::kw_half: |
| 3031 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3032 | DiagID, Policy); |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3033 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3034 | case tok::kw_float: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3035 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3036 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3037 | break; |
| 3038 | case tok::kw_double: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3039 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3040 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3041 | break; |
| 3042 | case tok::kw_wchar_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3043 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3044 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3045 | break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3046 | case tok::kw_char16_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3047 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3048 | DiagID, Policy); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3049 | break; |
| 3050 | case tok::kw_char32_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3051 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3052 | DiagID, Policy); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3053 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3054 | case tok::kw_bool: |
| 3055 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 20ee5ae | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3056 | if (Tok.is(tok::kw_bool) && |
| 3057 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 3058 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 3059 | PrevSpec = ""; // Not used by the diagnostic. |
| 3060 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | 2b05999 | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3061 | // For better error recovery. |
| 3062 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 20ee5ae | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3063 | isInvalid = true; |
| 3064 | } else { |
| 3065 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3066 | DiagID, Policy); |
Argyrios Kyrtzidis | 20ee5ae | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3067 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3068 | break; |
| 3069 | case tok::kw__Decimal32: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3070 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3071 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3072 | break; |
| 3073 | case tok::kw__Decimal64: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3074 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3075 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3076 | break; |
| 3077 | case tok::kw__Decimal128: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3078 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3079 | DiagID, Policy); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3080 | break; |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3081 | case tok::kw___vector: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3082 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy); |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3083 | break; |
| 3084 | case tok::kw___pixel: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3085 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy); |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3086 | break; |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3087 | case tok::kw___unknown_anytype: |
| 3088 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3089 | PrevSpec, DiagID, Policy); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3090 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3091 | |
| 3092 | // class-specifier: |
| 3093 | case tok::kw_class: |
| 3094 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3095 | case tok::kw___interface: |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3096 | case tok::kw_union: { |
| 3097 | tok::TokenKind Kind = Tok.getKind(); |
| 3098 | ConsumeToken(); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3099 | |
| 3100 | // These are attributes following class specifiers. |
| 3101 | // To produce better diagnostic, we parse them when |
| 3102 | // parsing class specifier. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3103 | ParsedAttributesWithRange Attributes(AttrFactory); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3104 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3105 | EnteringContext, DSContext, Attributes); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3106 | |
| 3107 | // If there are attributes following class specifier, |
| 3108 | // take them over and handle them here. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3109 | if (!Attributes.empty()) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3110 | AttrsLastTime = true; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3111 | attrs.takeAllFrom(Attributes); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3112 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3113 | continue; |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3114 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3115 | |
| 3116 | // enum-specifier: |
| 3117 | case tok::kw_enum: |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3118 | ConsumeToken(); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3119 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3120 | continue; |
| 3121 | |
| 3122 | // cv-qualifier: |
| 3123 | case tok::kw_const: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3124 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3125 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3126 | break; |
| 3127 | case tok::kw_volatile: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3128 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3129 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3130 | break; |
| 3131 | case tok::kw_restrict: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3132 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3133 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3134 | break; |
| 3135 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3136 | // C++ typename-specifier: |
| 3137 | case tok::kw_typename: |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3138 | if (TryAnnotateTypeOrScopeToken()) { |
| 3139 | DS.SetTypeSpecError(); |
| 3140 | goto DoneWithDeclSpec; |
| 3141 | } |
| 3142 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3143 | continue; |
| 3144 | break; |
| 3145 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3146 | // GNU typeof support. |
| 3147 | case tok::kw_typeof: |
| 3148 | ParseTypeofSpecifier(DS); |
| 3149 | continue; |
| 3150 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3151 | case tok::annot_decltype: |
Anders Carlsson | 74948d0 | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 3152 | ParseDecltypeSpecifier(DS); |
| 3153 | continue; |
| 3154 | |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3155 | case tok::kw___underlying_type: |
| 3156 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3157 | continue; |
| 3158 | |
| 3159 | case tok::kw__Atomic: |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3160 | // C11 6.7.2.4/4: |
| 3161 | // If the _Atomic keyword is immediately followed by a left parenthesis, |
| 3162 | // it is interpreted as a type specifier (with a type name), not as a |
| 3163 | // type qualifier. |
| 3164 | if (NextToken().is(tok::l_paren)) { |
| 3165 | ParseAtomicSpecifier(DS); |
| 3166 | continue; |
| 3167 | } |
| 3168 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 3169 | getLangOpts()); |
| 3170 | break; |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3171 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3172 | // OpenCL qualifiers: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3173 | case tok::kw___private: |
| 3174 | case tok::kw___global: |
| 3175 | case tok::kw___local: |
| 3176 | case tok::kw___constant: |
| 3177 | case tok::kw___read_only: |
| 3178 | case tok::kw___write_only: |
| 3179 | case tok::kw___read_write: |
Aaron Ballman | 05d76ea | 2014-01-14 01:29:54 +0000 | [diff] [blame] | 3180 | ParseOpenCLQualifiers(DS.getAttributes()); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3181 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3182 | |
Steve Naroff | cfdf616 | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 3183 | case tok::less: |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3184 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3185 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 3186 | // but we support it. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3187 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3188 | goto DoneWithDeclSpec; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3189 | |
Douglas Gregor | 3a001f4 | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 3190 | if (!ParseObjCProtocolQualifiers(DS)) |
| 3191 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 3192 | << FixItHint::CreateInsertion(Loc, "id") |
| 3193 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3194 | |
Douglas Gregor | 06e41ae | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 3195 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 3196 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 3197 | continue; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 3198 | } |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3199 | // If the specifier wasn't legal, issue a diagnostic. |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3200 | if (isInvalid) { |
| 3201 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3202 | assert(DiagID); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3203 | |
Douglas Gregor | a05f5ab | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 3204 | if (DiagID == diag::ext_duplicate_declspec) |
| 3205 | Diag(Tok, DiagID) |
| 3206 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 3207 | else |
| 3208 | Diag(Tok, DiagID) << PrevSpec; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3209 | } |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3210 | |
Chris Lattner | 2e23209 | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 3211 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | 2b05999 | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3212 | if (DiagID != diag::err_bool_redeclaration) |
| 3213 | ConsumeToken(); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3214 | |
| 3215 | AttrsLastTime = false; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 3216 | } |
| 3217 | } |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 3218 | |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3219 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 3220 | /// semicolon. |
| 3221 | /// |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3222 | /// struct-declaration: |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3223 | /// specifier-qualifier-list struct-declarator-list |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 3224 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3225 | /// [GNU] specifier-qualifier-list |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3226 | /// struct-declarator-list: |
| 3227 | /// struct-declarator |
| 3228 | /// struct-declarator-list ',' struct-declarator |
| 3229 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 3230 | /// struct-declarator: |
| 3231 | /// declarator |
| 3232 | /// [GNU] declarator attributes[opt] |
| 3233 | /// declarator[opt] ':' constant-expression |
| 3234 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 3235 | /// |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3236 | void Parser:: |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3237 | ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3238 | |
Chris Lattner | f02ef3e | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3239 | if (Tok.is(tok::kw___extension__)) { |
| 3240 | // __extension__ silences extension warnings in the subexpression. |
| 3241 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3242 | ConsumeToken(); |
Chris Lattner | f02ef3e | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3243 | return ParseStructDeclaration(DS, Fields); |
| 3244 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3245 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3246 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3247 | ParseSpecifierQualifierList(DS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3248 | |
Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3249 | // If there are no declarators, this is a free-standing declaration |
| 3250 | // specifier. Let the actions module cope with it. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3251 | if (Tok.is(tok::semi)) { |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3252 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
| 3253 | DS); |
| 3254 | DS.complete(TheDecl); |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3255 | return; |
| 3256 | } |
| 3257 | |
| 3258 | // Read struct-declarators until we find the semicolon. |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3259 | bool FirstDeclarator = true; |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3260 | SourceLocation CommaLoc; |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3261 | while (1) { |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3262 | ParsingFieldDeclarator DeclaratorInfo(*this, DS); |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3263 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3264 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3265 | // Attributes are only allowed here on successive declarators. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3266 | if (!FirstDeclarator) |
| 3267 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3268 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3269 | /// struct-declarator: declarator |
| 3270 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | 17c3b1f | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3271 | if (Tok.isNot(tok::colon)) { |
| 3272 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 3273 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3274 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | 17c3b1f | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3275 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3276 | |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 3277 | if (TryConsumeToken(tok::colon)) { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3278 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3279 | if (Res.isInvalid()) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3280 | SkipUntil(tok::semi, StopBeforeMatch); |
Chris Lattner | 32295d3 | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 3281 | else |
Sebastian Redl | d9f7b1c | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 3282 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3283 | } |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3284 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3285 | // If attributes exist after the declarator, parse them. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3286 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3287 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3288 | // We're done with this declarator; invoke the callback. |
Eli Friedman | ba01f2b | 2012-08-08 23:35:12 +0000 | [diff] [blame] | 3289 | Fields.invoke(DeclaratorInfo); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3290 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3291 | // If we don't have a comma, it is either the end of the list (a ';') |
| 3292 | // or an error, bail out. |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 3293 | if (!TryConsumeToken(tok::comma, CommaLoc)) |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3294 | return; |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3295 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3296 | FirstDeclarator = false; |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3297 | } |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3298 | } |
| 3299 | |
| 3300 | /// ParseStructUnionBody |
| 3301 | /// struct-contents: |
| 3302 | /// struct-declaration-list |
| 3303 | /// [EXT] empty |
| 3304 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 3305 | /// struct-declaration-list: |
| 3306 | /// struct-declaration |
| 3307 | /// struct-declaration-list struct-declaration |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3308 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3309 | /// |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 3310 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3311 | unsigned TagType, Decl *TagDecl) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3312 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 3313 | "parsing struct/union body"); |
Andy Gibbs | 22e140b | 2013-04-03 09:31:19 +0000 | [diff] [blame] | 3314 | assert(!getLangOpts().CPlusPlus && "C++ declarations not supported"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3315 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3316 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3317 | if (T.consumeOpen()) |
| 3318 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3319 | |
Douglas Gregor | 658b955 | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 3320 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3321 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3322 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3323 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3324 | |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 3325 | // 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] | 3326 | while (Tok.isNot(tok::r_brace) && !isEofOrEom()) { |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3327 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3328 | |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 3329 | // Check for extraneous top-level semicolon. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3330 | if (Tok.is(tok::semi)) { |
Richard Smith | 87f5dc5 | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3331 | ConsumeExtraSemi(InsideStruct, TagType); |
Chris Lattner | 36e46a2 | 2007-06-09 05:49:55 +0000 | [diff] [blame] | 3332 | continue; |
| 3333 | } |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3334 | |
Andy Gibbs | c804e08 | 2013-04-03 09:46:04 +0000 | [diff] [blame] | 3335 | // Parse _Static_assert declaration. |
| 3336 | if (Tok.is(tok::kw__Static_assert)) { |
| 3337 | SourceLocation DeclEnd; |
| 3338 | ParseStaticAssertDeclaration(DeclEnd); |
| 3339 | continue; |
| 3340 | } |
| 3341 | |
Argyrios Kyrtzidis | 71c12fb | 2013-04-18 01:42:35 +0000 | [diff] [blame] | 3342 | if (Tok.is(tok::annot_pragma_pack)) { |
| 3343 | HandlePragmaPack(); |
| 3344 | continue; |
| 3345 | } |
| 3346 | |
| 3347 | if (Tok.is(tok::annot_pragma_align)) { |
| 3348 | HandlePragmaAlign(); |
| 3349 | continue; |
| 3350 | } |
| 3351 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3352 | if (!Tok.is(tok::at)) { |
| 3353 | struct CFieldCallback : FieldCallback { |
| 3354 | Parser &P; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3355 | Decl *TagDecl; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3356 | SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3357 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3358 | CFieldCallback(Parser &P, Decl *TagDecl, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3359 | SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3360 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 3361 | |
Eli Friedman | 934dbbf | 2012-08-08 23:53:27 +0000 | [diff] [blame] | 3362 | void invoke(ParsingFieldDeclarator &FD) { |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3363 | // Install the declarator into the current TagDecl. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3364 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 5e6253b | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 3365 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 3366 | FD.D, FD.BitfieldSize); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3367 | FieldDecls.push_back(Field); |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3368 | FD.complete(Field); |
Douglas Gregor | 66a985d | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 3369 | } |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3370 | } Callback(*this, TagDecl, FieldDecls); |
| 3371 | |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3372 | // Parse all the comma separated declarators. |
| 3373 | ParsingDeclSpec DS(*this); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3374 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3375 | } else { // Handle @defs |
| 3376 | ConsumeToken(); |
| 3377 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 3378 | Diag(Tok, diag::err_unexpected_at); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3379 | SkipUntil(tok::semi); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3380 | continue; |
| 3381 | } |
| 3382 | ConsumeToken(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 3383 | ExpectAndConsume(tok::l_paren); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3384 | if (!Tok.is(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 3385 | Diag(Tok, diag::err_expected) << tok::identifier; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3386 | SkipUntil(tok::semi); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3387 | continue; |
| 3388 | } |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3389 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3390 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3391 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3392 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 3393 | ConsumeToken(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 3394 | ExpectAndConsume(tok::r_paren); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3395 | } |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 3396 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 3397 | if (TryConsumeToken(tok::semi)) |
| 3398 | continue; |
| 3399 | |
| 3400 | if (Tok.is(tok::r_brace)) { |
Chris Lattner | 245c533 | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3401 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Chris Lattner | 0c7e82d | 2007-06-09 05:54:40 +0000 | [diff] [blame] | 3402 | break; |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3403 | } |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 3404 | |
| 3405 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 3406 | // Skip to end of block or statement to avoid ext-warning on extra ';'. |
| 3407 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
| 3408 | // If we stopped at a ';', eat it. |
| 3409 | TryConsumeToken(tok::semi); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3410 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3411 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3412 | T.consumeClose(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3413 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3414 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3415 | // If attributes exist after struct contents, parse them. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3416 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 15619c7 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 3417 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3418 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 751c558 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 3419 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3420 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3421 | attrs.getList()); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3422 | StructScope.Exit(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3423 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 3424 | T.getCloseLocation()); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3425 | } |
| 3426 | |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3427 | /// ParseEnumSpecifier |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 3428 | /// enum-specifier: [C99 6.7.2.2] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3429 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3430 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 3431 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3432 | /// '}' attributes[opt] |
Aaron Ballman | 9ecff02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 3433 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3434 | /// '}' |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3435 | /// 'enum' identifier |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 3436 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3437 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3438 | /// [C++11] enum-head '{' enumerator-list[opt] '}' |
| 3439 | /// [C++11] enum-head '{' enumerator-list ',' '}' |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3440 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3441 | /// enum-head: [C++11] |
| 3442 | /// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt] |
| 3443 | /// enum-key attribute-specifier-seq[opt] nested-name-specifier |
| 3444 | /// identifier enum-base[opt] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3445 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3446 | /// enum-key: [C++11] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3447 | /// 'enum' |
| 3448 | /// 'enum' 'class' |
| 3449 | /// 'enum' 'struct' |
| 3450 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3451 | /// enum-base: [C++11] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3452 | /// ':' type-specifier-seq |
| 3453 | /// |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3454 | /// [C++] elaborated-type-specifier: |
| 3455 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 3456 | /// |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3457 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | dc70c3a | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 3458 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3459 | AccessSpecifier AS, DeclSpecContext DSC) { |
Chris Lattner | ffbc271 | 2007-01-25 06:05:38 +0000 | [diff] [blame] | 3460 | // Parse the tag portion of this. |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3461 | if (Tok.is(tok::code_completion)) { |
| 3462 | // Code completion for an enum name. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3463 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3464 | return cutOffParsing(); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3465 | } |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3466 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3467 | // If attributes exist after tag, parse them. |
| 3468 | ParsedAttributesWithRange attrs(AttrFactory); |
| 3469 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3470 | MaybeParseCXX11Attributes(attrs); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3471 | |
| 3472 | // If declspecs exist after tag, parse them. |
| 3473 | while (Tok.is(tok::kw___declspec)) |
| 3474 | ParseMicrosoftDeclSpec(attrs); |
| 3475 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3476 | SourceLocation ScopedEnumKWLoc; |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3477 | bool IsScopedUsingClassTag = false; |
| 3478 | |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3479 | // In C++11, recognize 'enum class' and 'enum struct'. |
Richard Trieu | d0d87b5 | 2013-04-23 02:47:36 +0000 | [diff] [blame] | 3480 | if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) { |
| 3481 | Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum |
| 3482 | : diag::ext_scoped_enum); |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3483 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3484 | ScopedEnumKWLoc = ConsumeToken(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3485 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3486 | // Attributes are not allowed between these keywords. Diagnose, |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3487 | // but then just treat them like they appeared in the right place. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3488 | ProhibitAttributes(attrs); |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3489 | |
| 3490 | // They are allowed afterwards, though. |
| 3491 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3492 | MaybeParseCXX11Attributes(attrs); |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3493 | while (Tok.is(tok::kw___declspec)) |
| 3494 | ParseMicrosoftDeclSpec(attrs); |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3495 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3496 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3497 | // C++11 [temp.explicit]p12: |
| 3498 | // The usual access controls do not apply to names used to specify |
| 3499 | // explicit instantiations. |
| 3500 | // We extend this to also cover explicit specializations. Note that |
| 3501 | // we don't suppress if this turns out to be an elaborated type |
| 3502 | // specifier. |
| 3503 | bool shouldDelayDiagsInTag = |
| 3504 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 3505 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 3506 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3507 | |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3508 | // Enum definitions should not be parsed in a trailing-return-type. |
| 3509 | bool AllowDeclaration = DSC != DSC_trailing; |
| 3510 | |
| 3511 | bool AllowFixedUnderlyingType = AllowDeclaration && |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3512 | (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt || |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3513 | getLangOpts().ObjC2); |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3514 | |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3515 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3516 | if (getLangOpts().CPlusPlus) { |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3517 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 3518 | // if a fixed underlying type is allowed. |
| 3519 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3520 | |
| 3521 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Richard Smith | 1d4b2e1 | 2013-04-01 21:43:41 +0000 | [diff] [blame] | 3522 | /*EnteringContext=*/true)) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3523 | return; |
| 3524 | |
| 3525 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 3526 | Diag(Tok, diag::err_expected) << tok::identifier; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3527 | if (Tok.isNot(tok::l_brace)) { |
| 3528 | // Has no name and is not a definition. |
| 3529 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3530 | SkipUntil(tok::comma, StopAtSemi); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3531 | return; |
| 3532 | } |
| 3533 | } |
| 3534 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3535 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3536 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3537 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3538 | !(AllowFixedUnderlyingType && Tok.is(tok::colon))) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 3539 | Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3540 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3541 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3542 | SkipUntil(tok::comma, StopAtSemi); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3543 | return; |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3544 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3545 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3546 | // If an identifier is present, consume and remember it. |
| 3547 | IdentifierInfo *Name = 0; |
| 3548 | SourceLocation NameLoc; |
| 3549 | if (Tok.is(tok::identifier)) { |
| 3550 | Name = Tok.getIdentifierInfo(); |
| 3551 | NameLoc = ConsumeToken(); |
| 3552 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3553 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3554 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3555 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 3556 | // declaration of a scoped enumeration. |
| 3557 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3558 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3559 | IsScopedUsingClassTag = false; |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3560 | } |
| 3561 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3562 | // Okay, end the suppression area. We'll decide whether to emit the |
| 3563 | // diagnostics in a second. |
| 3564 | if (shouldDelayDiagsInTag) |
| 3565 | diagsFromTag.done(); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3566 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3567 | TypeResult BaseType; |
| 3568 | |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3569 | // Parse the fixed underlying type. |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3570 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3571 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3572 | bool PossibleBitfield = false; |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3573 | if (CanBeBitfield) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3574 | // If we're in class scope, this can either be an enum declaration with |
| 3575 | // an underlying type, or a declaration of a bitfield member. We try to |
| 3576 | // use a simple disambiguation scheme first to catch the common cases |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3577 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 3578 | // anything that's a simple-type-specifier followed by '(' as an |
| 3579 | // expression. This suffices because function types are not valid |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3580 | // underlying types anyway. |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 3581 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 3582 | Sema::ConstantEvaluated); |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3583 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3584 | // 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] | 3585 | // bit-field. This is the common case. |
| 3586 | if (TPR == TPResult::True()) |
| 3587 | PossibleBitfield = true; |
| 3588 | // If the next token starts a type-specifier-seq, it may be either a |
| 3589 | // 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] | 3590 | // 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] | 3591 | // fixed underlying type. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3592 | else if (TPR == TPResult::False() && |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3593 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 3594 | // Consume the ':'. |
| 3595 | ConsumeToken(); |
| 3596 | } else { |
| 3597 | // We have the start of a type-specifier-seq, so we have to perform |
| 3598 | // tentative parsing to determine whether we have an expression or a |
| 3599 | // type. |
| 3600 | TentativeParsingAction TPA(*this); |
| 3601 | |
| 3602 | // Consume the ':'. |
| 3603 | ConsumeToken(); |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3604 | |
| 3605 | // If we see a type specifier followed by an open-brace, we have an |
| 3606 | // ambiguity between an underlying type and a C++11 braced |
| 3607 | // function-style cast. Resolve this by always treating it as an |
| 3608 | // underlying type. |
| 3609 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 3610 | // this case. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3611 | if ((getLangOpts().CPlusPlus && |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3612 | isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3613 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3614 | // We'll parse this as a bitfield later. |
| 3615 | PossibleBitfield = true; |
| 3616 | TPA.Revert(); |
| 3617 | } else { |
| 3618 | // We have a type-specifier-seq. |
| 3619 | TPA.Commit(); |
| 3620 | } |
| 3621 | } |
| 3622 | } else { |
| 3623 | // Consume the ':'. |
| 3624 | ConsumeToken(); |
| 3625 | } |
| 3626 | |
| 3627 | if (!PossibleBitfield) { |
| 3628 | SourceRange Range; |
| 3629 | BaseType = ParseTypeName(&Range); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3630 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3631 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3632 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Eli Friedman | 0d0355ab | 2012-11-02 01:34:28 +0000 | [diff] [blame] | 3633 | } else if (!getLangOpts().ObjC2) { |
| 3634 | if (getLangOpts().CPlusPlus) |
| 3635 | Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range; |
| 3636 | else |
| 3637 | Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range; |
| 3638 | } |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3639 | } |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3640 | } |
| 3641 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3642 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 3643 | // friend declaration, and cannot have an accompanying definition. If we have |
| 3644 | // 'enum foo;', then this is a forward declaration. If we have |
| 3645 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 3646 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3647 | // |
| 3648 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 3649 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 3650 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 3651 | // |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3652 | Sema::TagUseKind TUK; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3653 | if (!AllowDeclaration) { |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3654 | TUK = Sema::TUK_Reference; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3655 | } else if (Tok.is(tok::l_brace)) { |
| 3656 | if (DS.isFriendSpecified()) { |
| 3657 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
| 3658 | << SourceRange(DS.getFriendSpecLoc()); |
| 3659 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3660 | SkipUntil(tok::r_brace, StopAtSemi); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3661 | TUK = Sema::TUK_Friend; |
| 3662 | } else { |
| 3663 | TUK = Sema::TUK_Definition; |
| 3664 | } |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 3665 | } else if (!isTypeSpecifier(DSC) && |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3666 | (Tok.is(tok::semi) || |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3667 | (Tok.isAtStartOfLine() && |
| 3668 | !isValidAfterTypeSpecifier(CanBeBitfield)))) { |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3669 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
| 3670 | if (Tok.isNot(tok::semi)) { |
| 3671 | // A semicolon was missing after this declaration. Diagnose and recover. |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 3672 | ExpectAndConsume(tok::semi, diag::err_expected_after, "enum"); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3673 | PP.EnterToken(Tok); |
| 3674 | Tok.setKind(tok::semi); |
| 3675 | } |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3676 | } else { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3677 | TUK = Sema::TUK_Reference; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3678 | } |
| 3679 | |
| 3680 | // If this is an elaborated type specifier, and we delayed |
| 3681 | // diagnostics before, just merge them into the current pool. |
| 3682 | if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) { |
| 3683 | diagsFromTag.redelay(); |
| 3684 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3685 | |
| 3686 | MultiTemplateParamsArg TParams; |
Douglas Gregor | cbbf3e3 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3687 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3688 | TUK != Sema::TUK_Reference) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3689 | if (!getLangOpts().CPlusPlus11 || !SS.isSet()) { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3690 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3691 | Diag(Tok, diag::err_enum_template); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3692 | SkipUntil(tok::comma, StopAtSemi); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3693 | return; |
| 3694 | } |
| 3695 | |
| 3696 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 3697 | // Enumerations can't be explicitly instantiated. |
| 3698 | DS.SetTypeSpecError(); |
| 3699 | Diag(StartLoc, diag::err_explicit_instantiation_enum); |
| 3700 | return; |
| 3701 | } |
| 3702 | |
| 3703 | assert(TemplateInfo.TemplateParams && "no template parameters"); |
| 3704 | TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(), |
| 3705 | TemplateInfo.TemplateParams->size()); |
Douglas Gregor | cbbf3e3 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3706 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3707 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3708 | if (TUK == Sema::TUK_Reference) |
| 3709 | ProhibitAttributes(attrs); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3710 | |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3711 | if (!Name && TUK != Sema::TUK_Definition) { |
| 3712 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3713 | |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3714 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3715 | SkipUntil(tok::comma, StopAtSemi); |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3716 | return; |
| 3717 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3718 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3719 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3720 | bool IsDependent = false; |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3721 | const char *PrevSpec = 0; |
| 3722 | unsigned DiagID; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3723 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3724 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3725 | AS, DS.getModulePrivateSpecLoc(), TParams, |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3726 | Owned, IsDependent, ScopedEnumKWLoc, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 3727 | IsScopedUsingClassTag, BaseType, |
| 3728 | DSC == DSC_type_specifier); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3729 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3730 | if (IsDependent) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3731 | // This enum has a dependent nested-name-specifier. Handle it as a |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3732 | // dependent tag. |
| 3733 | if (!Name) { |
| 3734 | DS.SetTypeSpecError(); |
| 3735 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 3736 | return; |
| 3737 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3738 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3739 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3740 | TUK, SS, Name, StartLoc, |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3741 | NameLoc); |
| 3742 | if (Type.isInvalid()) { |
| 3743 | DS.SetTypeSpecError(); |
| 3744 | return; |
| 3745 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3746 | |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3747 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 3748 | NameLoc.isValid() ? NameLoc : StartLoc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3749 | PrevSpec, DiagID, Type.get(), |
| 3750 | Actions.getASTContext().getPrintingPolicy())) |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3751 | Diag(StartLoc, DiagID) << PrevSpec; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3752 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3753 | return; |
| 3754 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3755 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3756 | if (!TagDecl) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3757 | // The action failed to produce an enumeration tag. If this is a |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3758 | // definition, consume the entire definition. |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3759 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3760 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3761 | SkipUntil(tok::r_brace, StopAtSemi); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3762 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3763 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3764 | DS.SetTypeSpecError(); |
| 3765 | return; |
| 3766 | } |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3767 | |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3768 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3769 | ParseEnumBody(StartLoc, TagDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3770 | |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3771 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 3772 | NameLoc.isValid() ? NameLoc : StartLoc, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 3773 | PrevSpec, DiagID, TagDecl, Owned, |
| 3774 | Actions.getASTContext().getPrintingPolicy())) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3775 | Diag(StartLoc, DiagID) << PrevSpec; |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3776 | } |
| 3777 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3778 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 3779 | /// enumerator-list: |
| 3780 | /// enumerator |
| 3781 | /// enumerator-list ',' enumerator |
| 3782 | /// enumerator: |
| 3783 | /// enumeration-constant |
| 3784 | /// enumeration-constant '=' constant-expression |
| 3785 | /// enumeration-constant: |
| 3786 | /// identifier |
| 3787 | /// |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3788 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3789 | // Enter the scope of the enum body and start the definition. |
| 3790 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3791 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3792 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3793 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3794 | T.consumeOpen(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3795 | |
Chris Lattner | 37256fb | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 3796 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3797 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 6e81492 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 3798 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3799 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3800 | SmallVector<Decl *, 32> EnumConstantDecls; |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3801 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3802 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3803 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3804 | // Parse the enumerator-list. |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 3805 | while (Tok.isNot(tok::r_brace)) { |
| 3806 | // Parse enumerator. If failed, try skipping till the start of the next |
| 3807 | // enumerator definition. |
| 3808 | if (Tok.isNot(tok::identifier)) { |
| 3809 | Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; |
| 3810 | if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) && |
| 3811 | TryConsumeToken(tok::comma)) |
| 3812 | continue; |
| 3813 | break; |
| 3814 | } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3815 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 3816 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3817 | |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3818 | // If attributes exist after the enumerator, parse them. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3819 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3820 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3821 | MaybeParseCXX11Attributes(attrs); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3822 | ProhibitAttributes(attrs); |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3823 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3824 | SourceLocation EqualLoc; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3825 | ExprResult AssignedVal; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 3826 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3827 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 3828 | if (TryConsumeToken(tok::equal, EqualLoc)) { |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3829 | AssignedVal = ParseConstantExpression(); |
| 3830 | if (AssignedVal.isInvalid()) |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 3831 | SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch); |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3832 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3833 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3834 | // Install the enumerator constant into EnumDecl. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3835 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 3836 | LastEnumConstDecl, |
| 3837 | IdentLoc, Ident, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3838 | attrs.getList(), EqualLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3839 | AssignedVal.release()); |
Fariborz Jahanian | 329b351 | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3840 | PD.complete(EnumConstDecl); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3841 | |
Chris Lattner | 4ef4001 | 2007-06-11 01:28:17 +0000 | [diff] [blame] | 3842 | EnumConstantDecls.push_back(EnumConstDecl); |
| 3843 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3844 | |
Douglas Gregor | ce66d02 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3845 | if (Tok.is(tok::identifier)) { |
| 3846 | // We're missing a comma between enumerators. |
| 3847 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3848 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
Douglas Gregor | ce66d02 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3849 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3850 | continue; |
| 3851 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3852 | |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 3853 | // Emumerator definition must be finished, only comma or r_brace are |
| 3854 | // allowed here. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 3855 | SourceLocation CommaLoc; |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 3856 | if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) { |
| 3857 | if (EqualLoc.isValid()) |
| 3858 | Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace |
| 3859 | << tok::comma; |
| 3860 | else |
| 3861 | Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator); |
| 3862 | if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) { |
| 3863 | if (TryConsumeToken(tok::comma, CommaLoc)) |
| 3864 | continue; |
| 3865 | } else { |
| 3866 | break; |
| 3867 | } |
| 3868 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3869 | |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 3870 | // If comma is followed by r_brace, emit appropriate warning. |
| 3871 | if (Tok.is(tok::r_brace) && CommaLoc.isValid()) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3872 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11) |
Richard Smith | 87f5dc5 | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3873 | Diag(CommaLoc, getLangOpts().CPlusPlus ? |
| 3874 | diag::ext_enumerator_list_comma_cxx : |
| 3875 | diag::ext_enumerator_list_comma_c) |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3876 | << FixItHint::CreateRemoval(CommaLoc); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3877 | else if (getLangOpts().CPlusPlus11) |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3878 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 3879 | << FixItHint::CreateRemoval(CommaLoc); |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 3880 | break; |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3881 | } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3882 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3883 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3884 | // Eat the }. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3885 | T.consumeClose(); |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3886 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3887 | // If attributes exist after the identifier list, parse them. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3888 | ParsedAttributes attrs(AttrFactory); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3889 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3890 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3891 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 3892 | EnumDecl, EnumConstantDecls, |
| 3893 | getCurScope(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3894 | attrs.getList()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3895 | |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3896 | EnumScope.Exit(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3897 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 3898 | T.getCloseLocation()); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3899 | |
| 3900 | // The next token must be valid after an enum definition. If not, a ';' |
| 3901 | // was probably forgotten. |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3902 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
| 3903 | if (!isValidAfterTypeSpecifier(CanBeBitfield)) { |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 3904 | ExpectAndConsume(tok::semi, diag::err_expected_after, "enum"); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3905 | // Push this token back into the preprocessor and change our current token |
| 3906 | // to ';' so that the rest of the code recovers as though there were an |
| 3907 | // ';' after the definition. |
| 3908 | PP.EnterToken(Tok); |
| 3909 | Tok.setKind(tok::semi); |
| 3910 | } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3911 | } |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3912 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 3913 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3914 | /// start of a type-qualifier-list. |
| 3915 | bool Parser::isTypeQualifier() const { |
| 3916 | switch (Tok.getKind()) { |
| 3917 | default: return false; |
Alp Toker | de50ff3 | 2013-12-17 18:17:46 +0000 | [diff] [blame] | 3918 | // type-qualifier |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3919 | case tok::kw_const: |
| 3920 | case tok::kw_volatile: |
| 3921 | case tok::kw_restrict: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3922 | case tok::kw___private: |
| 3923 | case tok::kw___local: |
| 3924 | case tok::kw___global: |
| 3925 | case tok::kw___constant: |
| 3926 | case tok::kw___read_only: |
| 3927 | case tok::kw___read_write: |
| 3928 | case tok::kw___write_only: |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3929 | return true; |
| 3930 | } |
| 3931 | } |
| 3932 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3933 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 3934 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 3935 | /// specifier or if we're not sure. |
| 3936 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 3937 | switch (Tok.getKind()) { |
| 3938 | default: return false; |
| 3939 | // type-specifiers |
| 3940 | case tok::kw_short: |
| 3941 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3942 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3943 | case tok::kw___int128: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3944 | case tok::kw_signed: |
| 3945 | case tok::kw_unsigned: |
| 3946 | case tok::kw__Complex: |
| 3947 | case tok::kw__Imaginary: |
| 3948 | case tok::kw_void: |
| 3949 | case tok::kw_char: |
| 3950 | case tok::kw_wchar_t: |
| 3951 | case tok::kw_char16_t: |
| 3952 | case tok::kw_char32_t: |
| 3953 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3954 | case tok::kw_half: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3955 | case tok::kw_float: |
| 3956 | case tok::kw_double: |
| 3957 | case tok::kw_bool: |
| 3958 | case tok::kw__Bool: |
| 3959 | case tok::kw__Decimal32: |
| 3960 | case tok::kw__Decimal64: |
| 3961 | case tok::kw__Decimal128: |
| 3962 | case tok::kw___vector: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3963 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3964 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3965 | case tok::kw_class: |
| 3966 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3967 | case tok::kw___interface: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3968 | case tok::kw_union: |
| 3969 | // enum-specifier |
| 3970 | case tok::kw_enum: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3971 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3972 | // typedef-name |
| 3973 | case tok::annot_typename: |
| 3974 | return true; |
| 3975 | } |
| 3976 | } |
| 3977 | |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3978 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 3979 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3980 | bool Parser::isTypeSpecifierQualifier() { |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 3981 | switch (Tok.getKind()) { |
| 3982 | default: return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3983 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3984 | case tok::identifier: // foo::bar |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3985 | if (TryAltiVecVectorToken()) |
| 3986 | return true; |
| 3987 | // Fall through. |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3988 | case tok::kw_typename: // typename T::type |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3989 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3990 | // recurse to handle whatever we get. |
| 3991 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3992 | return true; |
| 3993 | if (Tok.is(tok::identifier)) |
| 3994 | return false; |
| 3995 | return isTypeSpecifierQualifier(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3996 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3997 | case tok::coloncolon: // ::foo::bar |
| 3998 | if (NextToken().is(tok::kw_new) || // ::new |
| 3999 | NextToken().is(tok::kw_delete)) // ::delete |
| 4000 | return false; |
| 4001 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4002 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4003 | return true; |
| 4004 | return isTypeSpecifierQualifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4005 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 4006 | // GNU attributes support. |
| 4007 | case tok::kw___attribute: |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4008 | // GNU typeof support. |
| 4009 | case tok::kw_typeof: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4010 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4011 | // type-specifiers |
| 4012 | case tok::kw_short: |
| 4013 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4014 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4015 | case tok::kw___int128: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4016 | case tok::kw_signed: |
| 4017 | case tok::kw_unsigned: |
| 4018 | case tok::kw__Complex: |
| 4019 | case tok::kw__Imaginary: |
| 4020 | case tok::kw_void: |
| 4021 | case tok::kw_char: |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4022 | case tok::kw_wchar_t: |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4023 | case tok::kw_char16_t: |
| 4024 | case tok::kw_char32_t: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4025 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4026 | case tok::kw_half: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4027 | case tok::kw_float: |
| 4028 | case tok::kw_double: |
Chris Lattner | bb31a42 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 4029 | case tok::kw_bool: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4030 | case tok::kw__Bool: |
| 4031 | case tok::kw__Decimal32: |
| 4032 | case tok::kw__Decimal64: |
| 4033 | case tok::kw__Decimal128: |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4034 | case tok::kw___vector: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4035 | |
Chris Lattner | 861a226 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 4036 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4037 | case tok::kw_class: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4038 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4039 | case tok::kw___interface: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4040 | case tok::kw_union: |
| 4041 | // enum-specifier |
| 4042 | case tok::kw_enum: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4043 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4044 | // type-qualifier |
| 4045 | case tok::kw_const: |
| 4046 | case tok::kw_volatile: |
| 4047 | case tok::kw_restrict: |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4048 | |
John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4049 | // Debugger support. |
| 4050 | case tok::kw___unknown_anytype: |
| 4051 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4052 | // typedef-name |
Chris Lattner | a8a3f73 | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 4053 | case tok::annot_typename: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4054 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4055 | |
Chris Lattner | 409bf7d | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 4056 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4057 | case tok::less: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4058 | return getLangOpts().ObjC1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4059 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4060 | case tok::kw___cdecl: |
| 4061 | case tok::kw___stdcall: |
| 4062 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4063 | case tok::kw___thiscall: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4064 | case tok::kw___w64: |
| 4065 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4066 | case tok::kw___ptr32: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4067 | case tok::kw___pascal: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4068 | case tok::kw___unaligned: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4069 | |
| 4070 | case tok::kw___private: |
| 4071 | case tok::kw___local: |
| 4072 | case tok::kw___global: |
| 4073 | case tok::kw___constant: |
| 4074 | case tok::kw___read_only: |
| 4075 | case tok::kw___read_write: |
| 4076 | case tok::kw___write_only: |
| 4077 | |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4078 | return true; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4079 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4080 | // C11 _Atomic |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4081 | case tok::kw__Atomic: |
| 4082 | return true; |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4083 | } |
| 4084 | } |
| 4085 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4086 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 4087 | /// declaration specifier. |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4088 | /// |
| 4089 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 4090 | /// this check is to disambiguate between an expression and a declaration. |
| 4091 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4092 | switch (Tok.getKind()) { |
| 4093 | default: return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4094 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4095 | case tok::identifier: // foo::bar |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4096 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4097 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4098 | return false; |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4099 | if (TryAltiVecVectorToken()) |
| 4100 | return true; |
| 4101 | // Fall through. |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4102 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4103 | case tok::kw_typename: // typename T::type |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4104 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4105 | // recurse to handle whatever we get. |
| 4106 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4107 | return true; |
| 4108 | if (Tok.is(tok::identifier)) |
| 4109 | return false; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4110 | |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4111 | // 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] | 4112 | // by an identifier and then either ':' or ']', in a place where an |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4113 | // expression is permitted, then this is probably a class message send |
| 4114 | // missing the initial '['. In this case, we won't consider this to be |
| 4115 | // the start of a declaration. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4116 | if (DisambiguatingWithExpression && |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4117 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 4118 | return false; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4119 | |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4120 | return isDeclarationSpecifier(); |
| 4121 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4122 | case tok::coloncolon: // ::foo::bar |
| 4123 | if (NextToken().is(tok::kw_new) || // ::new |
| 4124 | NextToken().is(tok::kw_delete)) // ::delete |
| 4125 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4126 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4127 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4128 | // recurse to handle whatever we get. |
| 4129 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4130 | return true; |
| 4131 | return isDeclarationSpecifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4132 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4133 | // storage-class-specifier |
| 4134 | case tok::kw_typedef: |
| 4135 | case tok::kw_extern: |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 4136 | case tok::kw___private_extern__: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4137 | case tok::kw_static: |
| 4138 | case tok::kw_auto: |
| 4139 | case tok::kw_register: |
| 4140 | case tok::kw___thread: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4141 | case tok::kw_thread_local: |
| 4142 | case tok::kw__Thread_local: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4143 | |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 4144 | // Modules |
| 4145 | case tok::kw___module_private__: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4146 | |
John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4147 | // Debugger support |
| 4148 | case tok::kw___unknown_anytype: |
| 4149 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4150 | // type-specifiers |
| 4151 | case tok::kw_short: |
| 4152 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4153 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4154 | case tok::kw___int128: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4155 | case tok::kw_signed: |
| 4156 | case tok::kw_unsigned: |
| 4157 | case tok::kw__Complex: |
| 4158 | case tok::kw__Imaginary: |
| 4159 | case tok::kw_void: |
| 4160 | case tok::kw_char: |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4161 | case tok::kw_wchar_t: |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4162 | case tok::kw_char16_t: |
| 4163 | case tok::kw_char32_t: |
| 4164 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4165 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4166 | case tok::kw_half: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4167 | case tok::kw_float: |
| 4168 | case tok::kw_double: |
Chris Lattner | bb31a42 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 4169 | case tok::kw_bool: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4170 | case tok::kw__Bool: |
| 4171 | case tok::kw__Decimal32: |
| 4172 | case tok::kw__Decimal64: |
| 4173 | case tok::kw__Decimal128: |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4174 | case tok::kw___vector: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4175 | |
Chris Lattner | 861a226 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 4176 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4177 | case tok::kw_class: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4178 | case tok::kw_struct: |
| 4179 | case tok::kw_union: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4180 | case tok::kw___interface: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4181 | // enum-specifier |
| 4182 | case tok::kw_enum: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4183 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4184 | // type-qualifier |
| 4185 | case tok::kw_const: |
| 4186 | case tok::kw_volatile: |
| 4187 | case tok::kw_restrict: |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4188 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4189 | // function-specifier |
| 4190 | case tok::kw_inline: |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4191 | case tok::kw_virtual: |
| 4192 | case tok::kw_explicit: |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4193 | case tok::kw__Noreturn: |
Chris Lattner | 7b20dc7 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4194 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 4195 | // alignment-specifier |
| 4196 | case tok::kw__Alignas: |
| 4197 | |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4198 | // friend keyword. |
| 4199 | case tok::kw_friend: |
| 4200 | |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 4201 | // static_assert-declaration |
| 4202 | case tok::kw__Static_assert: |
| 4203 | |
Chris Lattner | 599e47e | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4204 | // GNU typeof support. |
| 4205 | case tok::kw_typeof: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4206 | |
Chris Lattner | 599e47e | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4207 | // GNU attributes. |
Chris Lattner | 7b20dc7 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4208 | case tok::kw___attribute: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4209 | |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4210 | // C++11 decltype and constexpr. |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4211 | case tok::annot_decltype: |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4212 | case tok::kw_constexpr: |
Francois Pichet | e878cb6 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 4213 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4214 | // C11 _Atomic |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4215 | case tok::kw__Atomic: |
| 4216 | return true; |
| 4217 | |
Chris Lattner | 8b2ec16 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 4218 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4219 | case tok::less: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4220 | return getLangOpts().ObjC1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4221 | |
Douglas Gregor | 19b7acf | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 4222 | // typedef-name |
| 4223 | case tok::annot_typename: |
| 4224 | return !DisambiguatingWithExpression || |
| 4225 | !isStartOfObjCClassMessageMissingOpenBracket(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4226 | |
Steve Naroff | f192fab | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 4227 | case tok::kw___declspec: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4228 | case tok::kw___cdecl: |
| 4229 | case tok::kw___stdcall: |
| 4230 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4231 | case tok::kw___thiscall: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4232 | case tok::kw___w64: |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4233 | case tok::kw___sptr: |
| 4234 | case tok::kw___uptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4235 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4236 | case tok::kw___ptr32: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4237 | case tok::kw___forceinline: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4238 | case tok::kw___pascal: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4239 | case tok::kw___unaligned: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4240 | |
| 4241 | case tok::kw___private: |
| 4242 | case tok::kw___local: |
| 4243 | case tok::kw___global: |
| 4244 | case tok::kw___constant: |
| 4245 | case tok::kw___read_only: |
| 4246 | case tok::kw___read_write: |
| 4247 | case tok::kw___write_only: |
| 4248 | |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4249 | return true; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4250 | } |
| 4251 | } |
| 4252 | |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 4253 | bool Parser::isConstructorDeclarator(bool IsUnqualified) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4254 | TentativeParsingAction TPA(*this); |
| 4255 | |
| 4256 | // Parse the C++ scope specifier. |
| 4257 | CXXScopeSpec SS; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4258 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4259 | /*EnteringContext=*/true)) { |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4260 | TPA.Revert(); |
| 4261 | return false; |
| 4262 | } |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4263 | |
| 4264 | // Parse the constructor name. |
| 4265 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 4266 | // We already know that we have a constructor name; just consume |
| 4267 | // the token. |
| 4268 | ConsumeToken(); |
| 4269 | } else { |
| 4270 | TPA.Revert(); |
| 4271 | return false; |
| 4272 | } |
| 4273 | |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4274 | // Current class name must be followed by a left parenthesis. |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4275 | if (Tok.isNot(tok::l_paren)) { |
| 4276 | TPA.Revert(); |
| 4277 | return false; |
| 4278 | } |
| 4279 | ConsumeParen(); |
| 4280 | |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4281 | // A right parenthesis, or ellipsis followed by a right parenthesis signals |
| 4282 | // that we have a constructor. |
| 4283 | if (Tok.is(tok::r_paren) || |
| 4284 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4285 | TPA.Revert(); |
| 4286 | return true; |
| 4287 | } |
| 4288 | |
Richard Smith | f216366 | 2013-09-06 00:12:20 +0000 | [diff] [blame] | 4289 | // A C++11 attribute here signals that we have a constructor, and is an |
| 4290 | // attribute on the first constructor parameter. |
| 4291 | if (getLangOpts().CPlusPlus11 && |
| 4292 | isCXX11AttributeSpecifier(/*Disambiguate*/ false, |
| 4293 | /*OuterMightBeMessageSend*/ true)) { |
| 4294 | TPA.Revert(); |
| 4295 | return true; |
| 4296 | } |
| 4297 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4298 | // If we need to, enter the specified scope. |
| 4299 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4300 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4301 | DeclScopeObj.EnterDeclaratorScope(); |
| 4302 | |
Francois Pichet | 79f3a87 | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4303 | // Optionally skip Microsoft attributes. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4304 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | 79f3a87 | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4305 | MaybeParseMicrosoftAttributes(Attrs); |
| 4306 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4307 | // Check whether the next token(s) are part of a declaration |
| 4308 | // specifier, in which case we have the start of a parameter and, |
| 4309 | // therefore, we know that this is a constructor. |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4310 | bool IsConstructor = false; |
| 4311 | if (isDeclarationSpecifier()) |
| 4312 | IsConstructor = true; |
| 4313 | else if (Tok.is(tok::identifier) || |
| 4314 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) { |
| 4315 | // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type. |
| 4316 | // This might be a parenthesized member name, but is more likely to |
| 4317 | // be a constructor declaration with an invalid argument type. Keep |
| 4318 | // looking. |
| 4319 | if (Tok.is(tok::annot_cxxscope)) |
| 4320 | ConsumeToken(); |
| 4321 | ConsumeToken(); |
| 4322 | |
| 4323 | // If this is not a constructor, we must be parsing a declarator, |
Richard Smith | 1453e31 | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4324 | // which must have one of the following syntactic forms (see the |
| 4325 | // grammar extract at the start of ParseDirectDeclarator): |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4326 | switch (Tok.getKind()) { |
| 4327 | case tok::l_paren: |
| 4328 | // C(X ( int)); |
| 4329 | case tok::l_square: |
| 4330 | // C(X [ 5]); |
| 4331 | // C(X [ [attribute]]); |
| 4332 | case tok::coloncolon: |
| 4333 | // C(X :: Y); |
| 4334 | // C(X :: *p); |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4335 | // Assume this isn't a constructor, rather than assuming it's a |
| 4336 | // constructor with an unnamed parameter of an ill-formed type. |
| 4337 | break; |
| 4338 | |
Richard Smith | 446161b | 2014-03-03 21:12:53 +0000 | [diff] [blame] | 4339 | case tok::r_paren: |
| 4340 | // C(X ) |
| 4341 | if (NextToken().is(tok::colon) || NextToken().is(tok::kw_try)) { |
| 4342 | // Assume these were meant to be constructors: |
| 4343 | // C(X) : (the name of a bit-field cannot be parenthesized). |
| 4344 | // C(X) try (this is otherwise ill-formed). |
| 4345 | IsConstructor = true; |
| 4346 | } |
| 4347 | if (NextToken().is(tok::semi) || NextToken().is(tok::l_brace)) { |
| 4348 | // If we have a constructor name within the class definition, |
| 4349 | // assume these were meant to be constructors: |
| 4350 | // C(X) { |
| 4351 | // C(X) ; |
| 4352 | // ... because otherwise we would be declaring a non-static data |
| 4353 | // member that is ill-formed because it's of the same type as its |
| 4354 | // surrounding class. |
| 4355 | // |
| 4356 | // FIXME: We can actually do this whether or not the name is qualified, |
| 4357 | // because if it is qualified in this context it must be being used as |
| 4358 | // a constructor name. However, we do not implement that rule correctly |
| 4359 | // currently, so we're somewhat conservative here. |
| 4360 | IsConstructor = IsUnqualified; |
| 4361 | } |
| 4362 | break; |
| 4363 | |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4364 | default: |
| 4365 | IsConstructor = true; |
| 4366 | break; |
| 4367 | } |
| 4368 | } |
| 4369 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4370 | TPA.Revert(); |
| 4371 | return IsConstructor; |
| 4372 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 4373 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4374 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4375 | /// type-qualifier-list: [C99 6.7.5] |
| 4376 | /// type-qualifier |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4377 | /// [vendor] attributes |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4378 | /// [ only if VendorAttributesAllowed=true ] |
| 4379 | /// type-qualifier-list type-qualifier |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4380 | /// [vendor] type-qualifier-list attributes |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4381 | /// [ only if VendorAttributesAllowed=true ] |
| 4382 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4383 | /// [ only if CXX11AttributesAllowed=true ] |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4384 | /// Note: vendor can be GNU, MS, etc. |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4385 | /// |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4386 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 4387 | bool VendorAttributesAllowed, |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4388 | bool CXX11AttributesAllowed, |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 4389 | bool AtomicAllowed, |
| 4390 | bool IdentifierRequired) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4391 | if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed && |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4392 | isCXX11AttributeSpecifier()) { |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4393 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4394 | ParseCXX11Attributes(attrs); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4395 | DS.takeAttributesFrom(attrs); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4396 | } |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4397 | |
| 4398 | SourceLocation EndLoc; |
| 4399 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4400 | while (1) { |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4401 | bool isInvalid = false; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 4402 | const char *PrevSpec = 0; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4403 | unsigned DiagID = 0; |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 4404 | SourceLocation Loc = Tok.getLocation(); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 4405 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4406 | switch (Tok.getKind()) { |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4407 | case tok::code_completion: |
| 4408 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 4409 | return cutOffParsing(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4410 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4411 | case tok::kw_const: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4412 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4413 | getLangOpts()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 4414 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4415 | case tok::kw_volatile: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4416 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4417 | getLangOpts()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 4418 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4419 | case tok::kw_restrict: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4420 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4421 | getLangOpts()); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4422 | break; |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4423 | case tok::kw__Atomic: |
| 4424 | if (!AtomicAllowed) |
| 4425 | goto DoneWithTypeQuals; |
| 4426 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 4427 | getLangOpts()); |
| 4428 | break; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4429 | |
| 4430 | // OpenCL qualifiers: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4431 | case tok::kw___private: |
| 4432 | case tok::kw___global: |
| 4433 | case tok::kw___local: |
| 4434 | case tok::kw___constant: |
| 4435 | case tok::kw___read_only: |
| 4436 | case tok::kw___write_only: |
| 4437 | case tok::kw___read_write: |
Aaron Ballman | 05d76ea | 2014-01-14 01:29:54 +0000 | [diff] [blame] | 4438 | ParseOpenCLQualifiers(DS.getAttributes()); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4439 | break; |
| 4440 | |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4441 | case tok::kw___uptr: |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 4442 | // GNU libc headers in C mode use '__uptr' as an identifer which conflicts |
| 4443 | // with the MS modifier keyword. |
| 4444 | if (VendorAttributesAllowed && !getLangOpts().CPlusPlus && |
Alp Toker | 47642d2 | 2013-12-03 06:13:01 +0000 | [diff] [blame] | 4445 | IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) { |
| 4446 | if (TryKeywordIdentFallback(false)) |
| 4447 | continue; |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 4448 | } |
| 4449 | case tok::kw___sptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4450 | case tok::kw___w64: |
Steve Naroff | f9c29d4 | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 4451 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4452 | case tok::kw___ptr32: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4453 | case tok::kw___cdecl: |
| 4454 | case tok::kw___stdcall: |
| 4455 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4456 | case tok::kw___thiscall: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4457 | case tok::kw___unaligned: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4458 | if (VendorAttributesAllowed) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4459 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4460 | continue; |
| 4461 | } |
| 4462 | goto DoneWithTypeQuals; |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4463 | case tok::kw___pascal: |
| 4464 | if (VendorAttributesAllowed) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4465 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4466 | continue; |
| 4467 | } |
| 4468 | goto DoneWithTypeQuals; |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 4469 | case tok::kw___attribute: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4470 | if (VendorAttributesAllowed) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4471 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4472 | continue; // do *not* consume the next token! |
| 4473 | } |
| 4474 | // otherwise, FALL THROUGH! |
| 4475 | default: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4476 | DoneWithTypeQuals: |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4477 | // If this is not a type-qualifier token, we're done reading type |
| 4478 | // qualifiers. First verify that DeclSpec's are consistent. |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 4479 | DS.Finish(Diags, PP, Actions.getASTContext().getPrintingPolicy()); |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4480 | if (EndLoc.isValid()) |
| 4481 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4482 | return; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4483 | } |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4484 | |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 4485 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 4486 | if (isInvalid) { |
| 4487 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4488 | Diag(Tok, DiagID) << PrevSpec; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 4489 | } |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4490 | EndLoc = ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4491 | } |
| 4492 | } |
| 4493 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 4494 | |
| 4495 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 4496 | /// |
| 4497 | void Parser::ParseDeclarator(Declarator &D) { |
| 4498 | /// This implements the 'declarator' production in the C grammar, then checks |
| 4499 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4500 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 4501 | } |
| 4502 | |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4503 | static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) { |
| 4504 | if (Kind == tok::star || Kind == tok::caret) |
| 4505 | return true; |
| 4506 | |
| 4507 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 4508 | if (!Lang.CPlusPlus) |
| 4509 | return false; |
| 4510 | |
| 4511 | return Kind == tok::amp || Kind == tok::ampamp; |
| 4512 | } |
| 4513 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4514 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 4515 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 4516 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4517 | /// ptr-operator production. |
| 4518 | /// |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4519 | /// 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] | 4520 | /// made to TryParseDeclarator and MightBeDeclarator, and possibly to |
| 4521 | /// isConstructorDeclarator. |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4522 | /// |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4523 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 4524 | /// [C] pointer[opt] direct-declarator |
| 4525 | /// [C++] direct-declarator |
| 4526 | /// [C++] ptr-operator declarator |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 4527 | /// |
| 4528 | /// pointer: [C99 6.7.5] |
| 4529 | /// '*' type-qualifier-list[opt] |
| 4530 | /// '*' type-qualifier-list[opt] pointer |
| 4531 | /// |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4532 | /// ptr-operator: |
| 4533 | /// '*' cv-qualifier-seq[opt] |
| 4534 | /// '&' |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4535 | /// [C++0x] '&&' |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4536 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4537 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4538 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4539 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 4540 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 66a985d | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 4541 | if (Diags.hasAllExtensionsSilenced()) |
| 4542 | D.setExtension(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4543 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4544 | // C++ member pointers start with a '::' or a nested-name. |
| 4545 | // Member pointers get special handling, since there's no place for the |
| 4546 | // scope spec in the generic path below. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4547 | if (getLangOpts().CPlusPlus && |
Chris Lattner | 803802d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 4548 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 4549 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4550 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4551 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4552 | CXXScopeSpec SS; |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4553 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext); |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4554 | |
Jeffrey Yasskin | 4e150f8 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 4555 | if (SS.isNotEmpty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4556 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4557 | // The scope spec really belongs to the direct-declarator. |
Richard Smith | 5f044ad | 2013-01-08 22:43:49 +0000 | [diff] [blame] | 4558 | if (D.mayHaveIdentifier()) |
| 4559 | D.getCXXScopeSpec() = SS; |
| 4560 | else |
| 4561 | AnnotateScopeToken(SS, true); |
| 4562 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4563 | if (DirectDeclParser) |
| 4564 | (this->*DirectDeclParser)(D); |
| 4565 | return; |
| 4566 | } |
| 4567 | |
| 4568 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4569 | D.SetRangeEnd(Loc); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4570 | DeclSpec DS(AttrFactory); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4571 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4572 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4573 | |
| 4574 | // Recurse to parse whatever is left. |
| 4575 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 4576 | |
| 4577 | // Sema will have to catch (syntactically invalid) pointers into global |
| 4578 | // scope. It has to catch pointers into namespace scope anyway. |
| 4579 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4580 | Loc), |
| 4581 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4582 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4583 | return; |
| 4584 | } |
| 4585 | } |
| 4586 | |
| 4587 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4588 | // Not a pointer, C++ reference, or block. |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4589 | if (!isPtrOperatorToken(Kind, getLangOpts())) { |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4590 | if (DirectDeclParser) |
| 4591 | (this->*DirectDeclParser)(D); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4592 | return; |
| 4593 | } |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4594 | |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4595 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 4596 | // '&&' -> rvalue reference |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4597 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4598 | D.SetRangeEnd(Loc); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4599 | |
Chris Lattner | 9eac931 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 4600 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 788404f | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4601 | // Is a pointer. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4602 | DeclSpec DS(AttrFactory); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4603 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4604 | // FIXME: GNU attributes are not allowed here in a new-type-id. |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 4605 | ParseTypeQualifierListOpt(DS, true, true, true, !D.mayOmitIdentifier()); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4606 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4607 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4608 | // Recursively parse the declarator. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4609 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4610 | if (Kind == tok::star) |
| 4611 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 4612 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | e71b378d | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 4613 | DS.getConstSpecLoc(), |
| 4614 | DS.getVolatileSpecLoc(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4615 | DS.getRestrictSpecLoc()), |
| 4616 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4617 | SourceLocation()); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4618 | else |
| 4619 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4620 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4621 | Loc), |
| 4622 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4623 | SourceLocation()); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4624 | } else { |
| 4625 | // Is a reference |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4626 | DeclSpec DS(AttrFactory); |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 4627 | |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4628 | // Complain about rvalue references in C++03, but then go on and build |
| 4629 | // the declarator. |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4630 | if (Kind == tok::ampamp) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4631 | Diag(Loc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4632 | diag::warn_cxx98_compat_rvalue_reference : |
| 4633 | diag::ext_rvalue_reference); |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4634 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4635 | // GNU-style and C++11 attributes are allowed here, as is restrict. |
| 4636 | ParseTypeQualifierListOpt(DS); |
| 4637 | D.ExtendWithDeclSpec(DS); |
| 4638 | |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 4639 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 4640 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 4641 | // template type argument, in which case the cv-qualifiers are ignored. |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 4642 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 4643 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 4644 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4645 | diag::err_invalid_reference_qualifier_application) << "const"; |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 4646 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 4647 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4648 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4649 | // 'restrict' is permitted as an extension. |
| 4650 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
| 4651 | Diag(DS.getAtomicSpecLoc(), |
| 4652 | diag::err_invalid_reference_qualifier_application) << "_Atomic"; |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 4653 | } |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4654 | |
| 4655 | // Recursively parse the declarator. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4656 | ParseDeclaratorInternal(D, DirectDeclParser); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4657 | |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4658 | if (D.getNumTypeObjects() > 0) { |
| 4659 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 4660 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 4661 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | ebad6a2 | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 4662 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 4663 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4664 | << II; |
| 4665 | else |
| 4666 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4667 | << "type name"; |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4668 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4669 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4670 | // can go ahead and build the (technically ill-formed) |
| 4671 | // declarator: reference collapsing will take care of it. |
| 4672 | } |
| 4673 | } |
| 4674 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4675 | // Remember that we parsed a reference type. |
Chris Lattner | 788404f | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4676 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4677 | Kind == tok::amp), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4678 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4679 | SourceLocation()); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4680 | } |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 4681 | } |
| 4682 | |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4683 | static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D, |
| 4684 | SourceLocation EllipsisLoc) { |
| 4685 | if (EllipsisLoc.isValid()) { |
| 4686 | FixItHint Insertion; |
| 4687 | if (!D.getEllipsisLoc().isValid()) { |
| 4688 | Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "..."); |
| 4689 | D.setEllipsisLoc(EllipsisLoc); |
| 4690 | } |
| 4691 | P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration) |
| 4692 | << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName(); |
| 4693 | } |
| 4694 | } |
| 4695 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4696 | /// ParseDirectDeclarator |
| 4697 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4698 | /// [C99] identifier |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4699 | /// '(' declarator ')' |
| 4700 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 4701 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4702 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4703 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4704 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4705 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4706 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 4707 | /// attribute-specifier-seq[opt] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4708 | /// direct-declarator '(' parameter-type-list ')' |
| 4709 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4710 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4711 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 22c40fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 4712 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 4713 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4714 | /// [C++11] direct-declarator '(' parameter-declaration-clause ')' |
| 4715 | /// attribute-specifier-seq[opt] cv-qualifier-seq[opt] |
| 4716 | /// ref-qualifier[opt] exception-specification[opt] |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4717 | /// [C++] declarator-id |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4718 | /// [C++11] declarator-id attribute-specifier-seq[opt] |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4719 | /// |
| 4720 | /// declarator-id: [C++ 8] |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4721 | /// '...'[opt] id-expression |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4722 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 4723 | /// |
| 4724 | /// id-expression: [C++ 5.1] |
| 4725 | /// unqualified-id |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4726 | /// qualified-id |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4727 | /// |
| 4728 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4729 | /// identifier |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4730 | /// operator-function-id |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4731 | /// conversion-function-id |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4732 | /// '~' class-name |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 4733 | /// template-id |
Argyrios Kyrtzidis | e442635 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 4734 | /// |
Richard Smith | 1453e31 | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4735 | /// Note, any additional constructs added here may need corresponding changes |
| 4736 | /// in isConstructorDeclarator. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4737 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4738 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4739 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4740 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4741 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4742 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4743 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4744 | D.getContext() == Declarator::MemberContext; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4745 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4746 | EnteringContext); |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4747 | } |
| 4748 | |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4749 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4750 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | 2b058ef | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 4751 | // Change the declaration context for name lookup, until this function |
| 4752 | // is exited (and the declarator has been parsed). |
| 4753 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4754 | } |
| 4755 | |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4756 | // C++0x [dcl.fct]p14: |
| 4757 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4758 | // of a parameter-declaration-clause without a preceding comma. In |
| 4759 | // this case, the ellipsis is parsed as part of the |
| 4760 | // abstract-declarator if the type of the parameter names a template |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4761 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 4762 | // as part of the parameter-declaration-clause. |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4763 | if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() && |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4764 | !((D.getContext() == Declarator::PrototypeContext || |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4765 | D.getContext() == Declarator::LambdaExprParameterContext || |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4766 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4767 | NextToken().is(tok::r_paren) && |
Richard Smith | b19337f | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4768 | !D.hasGroupingParens() && |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4769 | !Actions.containsUnexpandedParameterPacks(D))) { |
| 4770 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 4771 | if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) { |
| 4772 | // The ellipsis was put in the wrong place. Recover, and explain to |
| 4773 | // the user what they should have done. |
| 4774 | ParseDeclarator(D); |
| 4775 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4776 | return; |
| 4777 | } else |
| 4778 | D.setEllipsisLoc(EllipsisLoc); |
| 4779 | |
| 4780 | // The ellipsis can't be followed by a parenthesized declarator. We |
| 4781 | // check for that in ParseParenDeclarator, after we have disambiguated |
| 4782 | // the l_paren token. |
| 4783 | } |
| 4784 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4785 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 4786 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 4787 | // We found something that indicates the start of an unqualified-id. |
| 4788 | // Parse that unqualified-id. |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4789 | bool AllowConstructorName; |
| 4790 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 4791 | AllowConstructorName = false; |
| 4792 | else if (D.getCXXScopeSpec().isSet()) |
| 4793 | AllowConstructorName = |
| 4794 | (D.getContext() == Declarator::FileContext || |
Dmitri Gribenko | d1c91f1 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 4795 | D.getContext() == Declarator::MemberContext); |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4796 | else |
| 4797 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 4798 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4799 | SourceLocation TemplateKWLoc; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4800 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 4801 | /*EnteringContext=*/true, |
| 4802 | /*AllowDestructorName=*/true, |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4803 | AllowConstructorName, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4804 | ParsedType(), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4805 | TemplateKWLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4806 | D.getName()) || |
| 4807 | // Once we're past the identifier, if the scope was bad, mark the |
| 4808 | // whole declarator bad. |
| 4809 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4810 | D.SetIdentifier(0, Tok.getLocation()); |
| 4811 | D.setInvalidType(true); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4812 | } else { |
| 4813 | // Parsed the unqualified-id; update range information and move along. |
| 4814 | if (D.getSourceRange().getBegin().isInvalid()) |
| 4815 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 4816 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4817 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4818 | goto PastIdentifier; |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4819 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4820 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4821 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4822 | "There's a C++-specific check for tok::identifier above"); |
| 4823 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 4824 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 4825 | ConsumeToken(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4826 | goto PastIdentifier; |
Richard Smith | 9ce302e | 2013-07-11 05:10:21 +0000 | [diff] [blame] | 4827 | } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) { |
Richard Smith | f39720b | 2013-10-13 22:12:28 +0000 | [diff] [blame] | 4828 | // A virt-specifier isn't treated as an identifier if it appears after a |
| 4829 | // trailing-return-type. |
| 4830 | if (D.getContext() != Declarator::TrailingReturnContext || |
| 4831 | !isCXX11VirtSpecifier(Tok)) { |
| 4832 | Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id) |
| 4833 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 4834 | D.SetIdentifier(0, Tok.getLocation()); |
| 4835 | ConsumeToken(); |
| 4836 | goto PastIdentifier; |
| 4837 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4838 | } |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4839 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4840 | if (Tok.is(tok::l_paren)) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4841 | // direct-declarator: '(' declarator ')' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 4842 | // direct-declarator: '(' attributes declarator ')' |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4843 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 4844 | ParseParenDeclarator(D); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4845 | |
| 4846 | // If the declarator was parenthesized, we entered the declarator |
| 4847 | // scope when parsing the parenthesized declarator, then exited |
| 4848 | // the scope already. Re-enter the scope, if we need to. |
| 4849 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4850 | // If there was an error parsing parenthesized declarator, declarator |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4851 | // scope may have been entered before. Don't do it again. |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4852 | if (!D.isInvalidType() && |
| 4853 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4854 | // Change the declaration context for name lookup, until this function |
| 4855 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4856 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4857 | } |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4858 | } else if (D.mayOmitIdentifier()) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4859 | // This could be something simple like "int" (in which case the declarator |
| 4860 | // portion is empty), if an abstract-declarator is allowed. |
| 4861 | D.SetIdentifier(0, Tok.getLocation()); |
Richard Smith | b19337f | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4862 | |
| 4863 | // The grammar for abstract-pack-declarator does not allow grouping parens. |
| 4864 | // FIXME: Revisit this once core issue 1488 is resolved. |
| 4865 | if (D.hasEllipsis() && D.hasGroupingParens()) |
| 4866 | Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()), |
| 4867 | diag::ext_abstract_pack_declarator_parens); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4868 | } else { |
David Blaikie | 5d577a2 | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 4869 | if (Tok.getKind() == tok::annot_pragma_parser_crash) |
David Blaikie | 5bd4c2a | 2012-08-21 18:56:49 +0000 | [diff] [blame] | 4870 | LLVM_BUILTIN_TRAP; |
Douglas Gregor | d9f92e2 | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 4871 | if (D.getContext() == Declarator::MemberContext) |
| 4872 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 4873 | << D.getDeclSpec().getSourceRange(); |
Richard Trieu | 9c67267 | 2013-01-26 02:31:38 +0000 | [diff] [blame] | 4874 | else if (getLangOpts().CPlusPlus) { |
| 4875 | if (Tok.is(tok::period) || Tok.is(tok::arrow)) |
| 4876 | Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow); |
Richard Trieu | 2f58696 | 2013-09-05 02:31:33 +0000 | [diff] [blame] | 4877 | else { |
| 4878 | SourceLocation Loc = D.getCXXScopeSpec().getEndLoc(); |
| 4879 | if (Tok.isAtStartOfLine() && Loc.isValid()) |
| 4880 | Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id) |
| 4881 | << getLangOpts().CPlusPlus; |
| 4882 | else |
| 4883 | Diag(Tok, diag::err_expected_unqualified_id) |
| 4884 | << getLangOpts().CPlusPlus; |
| 4885 | } |
Richard Trieu | 9c67267 | 2013-01-26 02:31:38 +0000 | [diff] [blame] | 4886 | } else |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 4887 | Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_paren; |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 4888 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 8c5dd73 | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 4889 | D.setInvalidType(true); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4890 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4891 | |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4892 | PastIdentifier: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4893 | assert(D.isPastIdentifier() && |
| 4894 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4895 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4896 | // Don't parse attributes unless we have parsed an unparenthesized name. |
| 4897 | if (D.hasName() && !D.getNumTypeObjects()) |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4898 | MaybeParseCXX11Attributes(D); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4899 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4900 | while (1) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4901 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4902 | // Enter function-declaration scope, limiting any declarators to the |
| 4903 | // function prototype scope, including parameter declarators. |
| 4904 | ParseScope PrototypeScope(this, |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4905 | Scope::FunctionPrototypeScope|Scope::DeclScope| |
| 4906 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 4907 | ? Scope::FunctionDeclarationScope : 0)); |
| 4908 | |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4909 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 4910 | // In such a case, check if we actually have a function declarator; if it |
| 4911 | // is not, the declarator has been fully parsed. |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4912 | bool IsAmbiguous = false; |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 4913 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 4914 | // The name of the declarator, if any, is tentatively declared within |
| 4915 | // a possible direct initializer. |
| 4916 | TentativelyDeclaredIdentifiers.push_back(D.getIdentifier()); |
| 4917 | bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous); |
| 4918 | TentativelyDeclaredIdentifiers.pop_back(); |
| 4919 | if (!IsFunctionDecl) |
| 4920 | break; |
| 4921 | } |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4922 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4923 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4924 | T.consumeOpen(); |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4925 | ParseFunctionDeclarator(D, attrs, T, IsAmbiguous); |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4926 | PrototypeScope.Exit(); |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4927 | } else if (Tok.is(tok::l_square)) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 4928 | ParseBracketDeclarator(D); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4929 | } else { |
| 4930 | break; |
| 4931 | } |
| 4932 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4933 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4934 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4935 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 4936 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4937 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4938 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 4939 | /// |
| 4940 | /// direct-declarator: |
| 4941 | /// '(' declarator ')' |
| 4942 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4943 | /// direct-declarator '(' parameter-type-list ')' |
| 4944 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4945 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4946 | /// parameter-type-list[opt] ')' |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4947 | /// |
| 4948 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4949 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4950 | T.consumeOpen(); |
| 4951 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4952 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4953 | |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4954 | // Eat any attributes before we look at whether this is a grouping or function |
| 4955 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 4956 | // the type being built up, for example: |
| 4957 | // int (__attribute__(()) *x)(long y) |
| 4958 | // If this ends up not being a grouping paren, the attribute applies to the |
| 4959 | // first argument, for example: |
| 4960 | // int (__attribute__(()) int x) |
| 4961 | // In either case, we need to eat any attributes to be able to determine what |
| 4962 | // sort of paren this is. |
| 4963 | // |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4964 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4965 | bool RequiresArg = false; |
| 4966 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4967 | ParseGNUAttributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4968 | |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4969 | // We require that the argument list (if this is a non-grouping paren) be |
| 4970 | // present even if the attribute list was empty. |
| 4971 | RequiresArg = true; |
| 4972 | } |
Chad Rosier | eea9ca7 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 4973 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4974 | // Eat any Microsoft extensions. |
Chad Rosier | eea9ca7 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 4975 | ParseMicrosoftTypeAttributes(attrs); |
| 4976 | |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4977 | // Eat any Borland extensions. |
Ted Kremenek | 5eec2b0 | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 4978 | if (Tok.is(tok::kw___pascal)) |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4979 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4980 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4981 | // If we haven't past the identifier yet (or where the identifier would be |
| 4982 | // stored, if this is an abstract declarator), then this is probably just |
| 4983 | // grouping parens. However, if this could be an abstract-declarator, then |
| 4984 | // this could also be the start of function arguments (consider 'void()'). |
| 4985 | bool isGrouping; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4986 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4987 | if (!D.mayOmitIdentifier()) { |
| 4988 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 4989 | // paren, because we haven't seen the identifier yet. |
| 4990 | isGrouping = true; |
| 4991 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4992 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) && |
| 4993 | NextToken().is(tok::r_paren)) || // C++ int(...) |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4994 | isDeclarationSpecifier() || // 'int(int)' is a function. |
| 4995 | isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function. |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4996 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 4997 | // considered to be a type, not a K&R identifier-list. |
| 4998 | isGrouping = false; |
| 4999 | } else { |
| 5000 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 5001 | isGrouping = true; |
| 5002 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5003 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5004 | // If this is a grouping paren, handle: |
| 5005 | // direct-declarator: '(' declarator ')' |
| 5006 | // direct-declarator: '(' attributes declarator ')' |
| 5007 | if (isGrouping) { |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5008 | SourceLocation EllipsisLoc = D.getEllipsisLoc(); |
| 5009 | D.setEllipsisLoc(SourceLocation()); |
| 5010 | |
Argyrios Kyrtzidis | 8ae3684 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 5011 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5012 | D.setGroupingParens(true); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5013 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5014 | // Match the ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5015 | T.consumeClose(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5016 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5017 | T.getCloseLocation()), |
| 5018 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 8ae3684 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 5019 | |
| 5020 | D.setGroupingParens(hadGroupingParens); |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5021 | |
| 5022 | // An ellipsis cannot be placed outside parentheses. |
| 5023 | if (EllipsisLoc.isValid()) |
| 5024 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 5025 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5026 | return; |
| 5027 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5028 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5029 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 5030 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5031 | // identifier (and remember where it would have been), then call into |
| 5032 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5033 | D.SetIdentifier(0, Tok.getLocation()); |
| 5034 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5035 | // Enter function-declaration scope, limiting any declarators to the |
| 5036 | // function prototype scope, including parameter declarators. |
| 5037 | ParseScope PrototypeScope(this, |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 5038 | Scope::FunctionPrototypeScope | Scope::DeclScope | |
| 5039 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 5040 | ? Scope::FunctionDeclarationScope : 0)); |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5041 | ParseFunctionDeclarator(D, attrs, T, false, RequiresArg); |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5042 | PrototypeScope.Exit(); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5043 | } |
| 5044 | |
| 5045 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 5046 | /// declarator D up to a paren, which indicates that we are parsing function |
| 5047 | /// arguments. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5048 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5049 | /// If FirstArgAttrs is non-null, then the caller parsed those arguments |
| 5050 | /// immediately after the open paren - they should be considered to be the |
| 5051 | /// first argument of a parameter. |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5052 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5053 | /// If RequiresArg is true, then the first argument of the function is required |
| 5054 | /// to be present and required to not be an identifier list. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5055 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5056 | /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt], |
| 5057 | /// (C++11) ref-qualifier[opt], exception-specification[opt], |
| 5058 | /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt]. |
| 5059 | /// |
| 5060 | /// [C++11] exception-specification: |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5061 | /// dynamic-exception-specification |
| 5062 | /// noexcept-specification |
| 5063 | /// |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5064 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5065 | ParsedAttributes &FirstArgAttrs, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5066 | BalancedDelimiterTracker &Tracker, |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5067 | bool IsAmbiguous, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5068 | bool RequiresArg) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5069 | assert(getCurScope()->isFunctionPrototypeScope() && |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5070 | "Should call from a Function scope"); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5071 | // lparen is already consumed! |
| 5072 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 5073 | |
| 5074 | // This should be true when the function has typed arguments. |
| 5075 | // Otherwise, it is treated as a K&R-style function. |
| 5076 | bool HasProto = false; |
| 5077 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5078 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5079 | // Remember where we see an ellipsis, if any. |
| 5080 | SourceLocation EllipsisLoc; |
| 5081 | |
| 5082 | DeclSpec DS(AttrFactory); |
| 5083 | bool RefQualifierIsLValueRef = true; |
| 5084 | SourceLocation RefQualifierLoc; |
Douglas Gregor | e248eea | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 5085 | SourceLocation ConstQualifierLoc; |
| 5086 | SourceLocation VolatileQualifierLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5087 | ExceptionSpecificationType ESpecType = EST_None; |
| 5088 | SourceRange ESpecRange; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5089 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 5090 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5091 | ExprResult NoexceptExpr; |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5092 | ParsedAttributes FnAttrs(AttrFactory); |
Richard Smith | 700537c | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 5093 | TypeResult TrailingReturnType; |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5094 | |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5095 | /* LocalEndLoc is the end location for the local FunctionTypeLoc. |
| 5096 | EndLoc is the end location for the function declarator. |
| 5097 | They differ for trailing return types. */ |
| 5098 | SourceLocation StartLoc, LocalEndLoc, EndLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5099 | SourceLocation LParenLoc, RParenLoc; |
| 5100 | LParenLoc = Tracker.getOpenLocation(); |
| 5101 | StartLoc = LParenLoc; |
| 5102 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5103 | if (isFunctionDeclaratorIdentifierList()) { |
| 5104 | if (RequiresArg) |
| 5105 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 5106 | |
| 5107 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 5108 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5109 | Tracker.consumeClose(); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5110 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5111 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5112 | EndLoc = RParenLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5113 | } else { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5114 | if (Tok.isNot(tok::r_paren)) |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5115 | ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, |
| 5116 | EllipsisLoc); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5117 | else if (RequiresArg) |
| 5118 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 5119 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5120 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5121 | |
| 5122 | // If we have the closing ')', eat it. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5123 | Tracker.consumeClose(); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5124 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5125 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5126 | EndLoc = RParenLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5127 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5128 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5129 | // FIXME: Accept these components in any order, and produce fixits to |
| 5130 | // correct the order if the user gets it wrong. Ideally we should deal |
| 5131 | // with the virt-specifier-seq and pure-specifier in the same way. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5132 | |
| 5133 | // Parse cv-qualifier-seq[opt]. |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5134 | ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false, |
| 5135 | /*CXX11AttributesAllowed*/ false, |
| 5136 | /*AtomicAllowed*/ false); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5137 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
| 5138 | EndLoc = DS.getSourceRange().getEnd(); |
| 5139 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 5140 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
| 5141 | } |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5142 | |
| 5143 | // Parse ref-qualifier[opt]. |
| 5144 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5145 | Diag(Tok, getLangOpts().CPlusPlus11 ? |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5146 | diag::warn_cxx98_compat_ref_qualifier : |
| 5147 | diag::ext_ref_qualifier); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5148 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5149 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 5150 | RefQualifierLoc = ConsumeToken(); |
| 5151 | EndLoc = RefQualifierLoc; |
| 5152 | } |
| 5153 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5154 | // C++11 [expr.prim.general]p3: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5155 | // If a declaration declares a member function or member function |
| 5156 | // 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] | 5157 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5158 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5159 | // declarator. |
Richard Smith | ad1bbb9 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 5160 | // FIXME: currently, "static" case isn't handled correctly. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5161 | bool IsCXX11MemberFunction = |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5162 | getLangOpts().CPlusPlus11 && |
Richard Smith | 990a692 | 2014-01-17 21:01:18 +0000 | [diff] [blame] | 5163 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
Richard Smith | ad1bbb9 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 5164 | (D.getContext() == Declarator::MemberContext |
| 5165 | ? !D.getDeclSpec().isFriendSpecified() |
| 5166 | : D.getContext() == Declarator::FileContext && |
| 5167 | D.getCXXScopeSpec().isValid() && |
| 5168 | Actions.CurContext->isRecord()); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5169 | Sema::CXXThisScopeRAII ThisScope(Actions, |
| 5170 | dyn_cast<CXXRecordDecl>(Actions.CurContext), |
Richard Smith | 01141a9 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 5171 | DS.getTypeQualifiers() | |
Richard Smith | 034185c | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 5172 | (D.getDeclSpec().isConstexprSpecified() && |
| 5173 | !getLangOpts().CPlusPlus1y |
Richard Smith | 01141a9 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 5174 | ? Qualifiers::Const : 0), |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5175 | IsCXX11MemberFunction); |
Richard Smith | 2331bbf | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5176 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5177 | // Parse exception-specification[opt]. |
Richard Smith | 2331bbf | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5178 | ESpecType = tryParseExceptionSpecification(ESpecRange, |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 5179 | DynamicExceptions, |
| 5180 | DynamicExceptionRanges, |
Richard Smith | 2331bbf | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5181 | NoexceptExpr); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5182 | if (ESpecType != EST_None) |
| 5183 | EndLoc = ESpecRange.getEnd(); |
| 5184 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5185 | // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes |
| 5186 | // after the exception-specification. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5187 | MaybeParseCXX11Attributes(FnAttrs); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5188 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5189 | // Parse trailing-return-type[opt]. |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5190 | LocalEndLoc = EndLoc; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5191 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) { |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5192 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5193 | if (D.getDeclSpec().getTypeSpecType() == TST_auto) |
| 5194 | StartLoc = D.getDeclSpec().getTypeSpecTypeLoc(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5195 | LocalEndLoc = Tok.getLocation(); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 5196 | SourceRange Range; |
Richard Smith | 700537c | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 5197 | TrailingReturnType = ParseTrailingReturnType(Range); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5198 | EndLoc = Range.getEnd(); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5199 | } |
| 5200 | } |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5201 | } |
| 5202 | |
| 5203 | // Remember that we parsed a function type, and remember the attributes. |
| 5204 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5205 | IsAmbiguous, |
| 5206 | LParenLoc, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5207 | ParamInfo.data(), ParamInfo.size(), |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5208 | EllipsisLoc, RParenLoc, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5209 | DS.getTypeQualifiers(), |
| 5210 | RefQualifierIsLValueRef, |
Douglas Gregor | e248eea | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 5211 | RefQualifierLoc, ConstQualifierLoc, |
| 5212 | VolatileQualifierLoc, |
Douglas Gregor | ad69e65 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 5213 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5214 | ESpecType, ESpecRange.getBegin(), |
| 5215 | DynamicExceptions.data(), |
| 5216 | DynamicExceptionRanges.data(), |
| 5217 | DynamicExceptions.size(), |
| 5218 | NoexceptExpr.isUsable() ? |
| 5219 | NoexceptExpr.get() : 0, |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5220 | StartLoc, LocalEndLoc, D, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5221 | TrailingReturnType), |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5222 | FnAttrs, EndLoc); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5223 | } |
| 5224 | |
| 5225 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 5226 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 5227 | /// |
| 5228 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 5229 | /// abstract-declarators. |
| 5230 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5231 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5232 | && Tok.is(tok::identifier) |
| 5233 | && !TryAltiVecVectorToken() |
| 5234 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 5235 | // 6.7.5.3p11. |
| 5236 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 5237 | // Identifier lists follow a really simple grammar: the identifiers can |
| 5238 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 5239 | // identifier lists are really rare in the brave new modern world, and |
| 5240 | // it is very common for someone to typo a type in a non-K&R style |
| 5241 | // list. If we are presented with something like: "void foo(intptr x, |
| 5242 | // float y)", we don't want to start parsing the function declarator as |
| 5243 | // though it is a K&R style declarator just because intptr is an |
| 5244 | // invalid type. |
| 5245 | // |
| 5246 | // To handle this, we check to see if the token after the first |
| 5247 | // identifier is a "," or ")". Only then do we parse it as an |
| 5248 | // identifier list. |
| 5249 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 5250 | } |
| 5251 | |
| 5252 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 5253 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 5254 | /// |
| 5255 | /// After returning, ParamInfo will hold the parsed parameters. |
| 5256 | /// |
| 5257 | /// identifier-list: [C99 6.7.5] |
| 5258 | /// identifier |
| 5259 | /// identifier-list ',' identifier |
| 5260 | /// |
| 5261 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 5262 | Declarator &D, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5263 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5264 | // If there was no identifier specified for the declarator, either we are in |
| 5265 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 5266 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 5267 | // diagnose this. |
| 5268 | if (!D.getIdentifier()) |
| 5269 | Diag(Tok, diag::ext_ident_list_in_param); |
| 5270 | |
| 5271 | // Maintain an efficient lookup of params we have seen so far. |
| 5272 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 5273 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 5274 | do { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5275 | // If this isn't an identifier, report the error and skip until ')'. |
| 5276 | if (Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 5277 | Diag(Tok, diag::err_expected) << tok::identifier; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5278 | SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5279 | // Forget we parsed anything. |
| 5280 | ParamInfo.clear(); |
| 5281 | return; |
| 5282 | } |
| 5283 | |
| 5284 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 5285 | |
| 5286 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 5287 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 5288 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 5289 | |
| 5290 | // Verify that the argument identifier has not already been mentioned. |
| 5291 | if (!ParamsSoFar.insert(ParmII)) { |
| 5292 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 5293 | } else { |
| 5294 | // Remember this identifier in ParamInfo. |
| 5295 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 5296 | Tok.getLocation(), |
| 5297 | 0)); |
| 5298 | } |
| 5299 | |
| 5300 | // Eat the identifier. |
| 5301 | ConsumeToken(); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5302 | // The list continues if we see a comma. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 5303 | } while (TryConsumeToken(tok::comma)); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5304 | } |
| 5305 | |
| 5306 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 5307 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 5308 | /// identifier list. |
| 5309 | /// |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5310 | /// D is the declarator being parsed. If FirstArgAttrs is non-null, then the |
| 5311 | /// caller parsed those arguments immediately after the open paren - they should |
| 5312 | /// be considered to be part of the first parameter. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5313 | /// |
| 5314 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 5315 | /// be the location of the ellipsis, if any was parsed. |
| 5316 | /// |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5317 | /// parameter-type-list: [C99 6.7.5] |
| 5318 | /// parameter-list |
| 5319 | /// parameter-list ',' '...' |
Douglas Gregor | 9bfc2e5 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5320 | /// [C++] parameter-list '...' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5321 | /// |
| 5322 | /// parameter-list: [C99 6.7.5] |
| 5323 | /// parameter-declaration |
| 5324 | /// parameter-list ',' parameter-declaration |
| 5325 | /// |
| 5326 | /// parameter-declaration: [C99 6.7.5] |
| 5327 | /// declaration-specifiers declarator |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5328 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5329 | /// [C++11] initializer-clause |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 5330 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 5331 | /// declaration-specifiers abstract-declarator[opt] |
| 5332 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 5333 | /// '=' assignment-expression |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 5334 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5335 | /// [C++11] attribute-specifier-seq parameter-declaration |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5336 | /// |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5337 | void Parser::ParseParameterDeclarationClause( |
| 5338 | Declarator &D, |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5339 | ParsedAttributes &FirstArgAttrs, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5340 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5341 | SourceLocation &EllipsisLoc) { |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 5342 | do { |
| 5343 | // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq |
| 5344 | // before deciding this was a parameter-declaration-clause. |
| 5345 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5346 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5347 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5348 | // Parse the declaration-specifiers. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5349 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5350 | DeclSpec DS(AttrFactory); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5351 | |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5352 | // Parse any C++11 attributes. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5353 | MaybeParseCXX11Attributes(DS.getAttributes()); |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5354 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5355 | // Skip any Microsoft attributes before a param. |
Chad Rosier | f8a2e70 | 2012-12-20 20:37:53 +0000 | [diff] [blame] | 5356 | MaybeParseMicrosoftAttributes(DS.getAttributes()); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5357 | |
| 5358 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5359 | |
| 5360 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5361 | // 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] | 5362 | // 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] | 5363 | // get rid of a parameter (FirstArgAttrs) and this statement. It might be |
| 5364 | // too much hassle. |
| 5365 | DS.takeAttributesFrom(FirstArgAttrs); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5366 | |
Chris Lattner | de39c3e | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 5367 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5368 | |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5369 | |
| 5370 | // Parse the declarator. This is "PrototypeContext" or |
| 5371 | // "LambdaExprParameterContext", because we must accept either |
| 5372 | // 'declarator' or 'abstract-declarator' here. |
| 5373 | Declarator ParmDeclarator(DS, |
| 5374 | D.getContext() == Declarator::LambdaExprContext ? |
| 5375 | Declarator::LambdaExprParameterContext : |
| 5376 | Declarator::PrototypeContext); |
| 5377 | ParseDeclarator(ParmDeclarator); |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5378 | |
| 5379 | // Parse GNU attributes, if present. |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5380 | MaybeParseGNUAttributes(ParmDeclarator); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5381 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5382 | // Remember this parsed parameter in ParamInfo. |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5383 | IdentifierInfo *ParmII = ParmDeclarator.getIdentifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5384 | |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5385 | // DefArgToks is used when the parsing of default arguments needs |
| 5386 | // to be delayed. |
| 5387 | CachedTokens *DefArgToks = 0; |
| 5388 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5389 | // If no parameter was specified, verify that *something* was specified, |
| 5390 | // otherwise we have a missing type and identifier. |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5391 | if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 && |
| 5392 | ParmDeclarator.getNumTypeObjects() == 0) { |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5393 | // Completely missing, emit error. |
| 5394 | Diag(DSStart, diag::err_missing_param); |
| 5395 | } else { |
| 5396 | // Otherwise, we have something. Add it and let semantic analysis try |
| 5397 | // 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] | 5398 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5399 | // Inform the actions module about the parameter declarator, so it gets |
| 5400 | // added to the current scope. |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5401 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), |
| 5402 | ParmDeclarator); |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5403 | // Parse the default argument, if any. We parse the default |
| 5404 | // arguments in all dialects; the semantic analysis in |
| 5405 | // ActOnParamDefaultArgument will reject the default argument in |
| 5406 | // C. |
| 5407 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5408 | SourceLocation EqualLoc = Tok.getLocation(); |
| 5409 | |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5410 | // Parse the default argument |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5411 | if (D.getContext() == Declarator::MemberContext) { |
| 5412 | // If we're inside a class definition, cache the tokens |
| 5413 | // corresponding to the default argument. We'll actually parse |
| 5414 | // them when we see the end of the class definition. |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5415 | // FIXME: Can we use a smart pointer for Toks? |
| 5416 | DefArgToks = new CachedTokens; |
| 5417 | |
Richard Smith | 1fff95c | 2013-09-12 23:28:08 +0000 | [diff] [blame] | 5418 | if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5419 | delete DefArgToks; |
| 5420 | DefArgToks = 0; |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5421 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 249179c | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5422 | } else { |
| 5423 | // Mark the end of the default argument so that we know when to |
| 5424 | // stop when we parse it later on. |
| 5425 | Token DefArgEnd; |
| 5426 | DefArgEnd.startToken(); |
| 5427 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 5428 | DefArgEnd.setLocation(Tok.getLocation()); |
| 5429 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5430 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 5431 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 249179c | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5432 | } |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5433 | } else { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5434 | // Consume the '='. |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5435 | ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5436 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5437 | // The argument isn't actually potentially evaluated unless it is |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5438 | // used. |
| 5439 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | 7fcbd90 | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 5440 | Sema::PotentiallyEvaluatedIfUsed, |
| 5441 | Param); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5442 | |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5443 | ExprResult DefArgResult; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5444 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
Sebastian Redl | 1678d5f | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5445 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5446 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 1678d5f | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5447 | } else |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5448 | DefArgResult = ParseAssignmentExpression(); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5449 | if (DefArgResult.isInvalid()) { |
| 5450 | Actions.ActOnParamDefaultArgumentError(Param); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5451 | SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5452 | } else { |
| 5453 | // Inform the actions module about the default argument |
| 5454 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5455 | DefArgResult.take()); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5456 | } |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5457 | } |
| 5458 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5459 | |
| 5460 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5461 | ParmDeclarator.getIdentifierLoc(), |
| 5462 | Param, DefArgToks)); |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5463 | } |
| 5464 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 5465 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc) && |
| 5466 | !getLangOpts().CPlusPlus) { |
| 5467 | // We have ellipsis without a preceding ',', which is ill-formed |
| 5468 | // in C. Complain and provide the fix. |
| 5469 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
| 5470 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | 9bfc2e5 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5471 | break; |
| 5472 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5473 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 5474 | // If the next token is a comma, consume it and keep reading arguments. |
| 5475 | } while (TryConsumeToken(tok::comma)); |
Chris Lattner | 6c940e6 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 5476 | } |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5477 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5478 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 5479 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 5480 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 5481 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 5482 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5483 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 5484 | /// attribute-specifier-seq[opt] |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5485 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5486 | if (CheckProhibitedCXX11Attribute()) |
| 5487 | return; |
| 5488 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5489 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 5490 | T.consumeOpen(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5491 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5492 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 5493 | // This code does a fast path to handle some of the most obvious cases. |
| 5494 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5495 | T.consumeClose(); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5496 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5497 | MaybeParseCXX11Attributes(attrs); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5498 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5499 | // Remember that we parsed the empty array type. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5500 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5501 | T.getOpenLocation(), |
| 5502 | T.getCloseLocation()), |
| 5503 | attrs, T.getCloseLocation()); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5504 | return; |
| 5505 | } else if (Tok.getKind() == tok::numeric_constant && |
| 5506 | GetLookAheadToken(1).is(tok::r_square)) { |
| 5507 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5508 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5509 | ConsumeToken(); |
| 5510 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5511 | T.consumeClose(); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5512 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5513 | MaybeParseCXX11Attributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5514 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5515 | // Remember that we parsed a array type, and remember its features. |
Nikola Smiljanic | c531dcc | 2013-01-11 08:33:05 +0000 | [diff] [blame] | 5516 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5517 | ExprRes.release(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5518 | T.getOpenLocation(), |
| 5519 | T.getCloseLocation()), |
| 5520 | attrs, T.getCloseLocation()); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5521 | return; |
| 5522 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5523 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5524 | // If valid, this location is the position where we read the 'static' keyword. |
| 5525 | SourceLocation StaticLoc; |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 5526 | TryConsumeToken(tok::kw_static, StaticLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5527 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5528 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5529 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5530 | DeclSpec DS(AttrFactory); |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5531 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5532 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5533 | // If we haven't already read 'static', check to see if there is one after the |
| 5534 | // type-qualifier-list. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 5535 | if (!StaticLoc.isValid()) |
| 5536 | TryConsumeToken(tok::kw_static, StaticLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5537 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5538 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5539 | bool isStar = false; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5540 | ExprResult NumElements; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5541 | |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5542 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 5543 | // 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] | 5544 | // the token after the star is a ']'. Since stars in arrays are |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5545 | // infrequent, use of lookahead is not costly here. |
| 5546 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | c439f0d | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 5547 | ConsumeToken(); // Eat the '*'. |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 5548 | |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5549 | if (StaticLoc.isValid()) { |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5550 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5551 | StaticLoc = SourceLocation(); // Drop the static. |
| 5552 | } |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5553 | isStar = true; |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5554 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5555 | // Note, in C89, this production uses the constant-expr production instead |
| 5556 | // of assignment-expr. The only difference is that assignment-expr allows |
| 5557 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 5558 | // 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] | 5559 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5560 | // Parse the constant-expression or assignment-expression now (depending |
| 5561 | // on dialect). |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5562 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5563 | NumElements = ParseConstantExpression(); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5564 | } else { |
| 5565 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 5566 | Sema::ConstantEvaluated); |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5567 | NumElements = ParseAssignmentExpression(); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5568 | } |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 5569 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5570 | |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 5571 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 5572 | if (NumElements.isInvalid()) { |
Chris Lattner | cd2a8c5 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 5573 | D.setInvalidType(true); |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 5574 | // If the expression was invalid, skip it. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5575 | SkipUntil(tok::r_square, StopAtSemi); |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 5576 | return; |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5577 | } |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5578 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5579 | T.consumeClose(); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5580 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5581 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5582 | MaybeParseCXX11Attributes(attrs); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5583 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5584 | // Remember that we parsed a array type, and remember its features. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5585 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 5586 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5587 | NumElements.release(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5588 | T.getOpenLocation(), |
| 5589 | T.getCloseLocation()), |
| 5590 | attrs, T.getCloseLocation()); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5591 | } |
| 5592 | |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5593 | /// [GNU] typeof-specifier: |
| 5594 | /// typeof ( expressions ) |
| 5595 | /// typeof ( type-name ) |
| 5596 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5597 | /// |
| 5598 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5599 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5600 | Token OpTok = Tok; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5601 | SourceLocation StartLoc = ConsumeToken(); |
| 5602 | |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5603 | const bool hasParens = Tok.is(tok::l_paren); |
| 5604 | |
Eli Friedman | 15681d6 | 2012-09-26 04:34:21 +0000 | [diff] [blame] | 5605 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated, |
| 5606 | Sema::ReuseLambdaContextDecl); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5607 | |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5608 | bool isCastExpr; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5609 | ParsedType CastTy; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5610 | SourceRange CastRange; |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5611 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 5612 | CastTy, CastRange); |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5613 | if (hasParens) |
| 5614 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5615 | |
| 5616 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5617 | // FIXME: Not accurate, the range gets one token more than it should. |
| 5618 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5619 | else |
| 5620 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5621 | |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5622 | if (isCastExpr) { |
| 5623 | if (!CastTy) { |
| 5624 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5625 | return; |
Douglas Gregor | 220cac5 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 5626 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5627 | |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5628 | const char *PrevSpec = 0; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5629 | unsigned DiagID; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5630 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5631 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 5632 | DiagID, CastTy, |
| 5633 | Actions.getASTContext().getPrintingPolicy())) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5634 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5635 | return; |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5636 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5637 | |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5638 | // If we get here, the operand to the typeof was an expresion. |
| 5639 | if (Operand.isInvalid()) { |
| 5640 | DS.SetTypeSpecError(); |
Steve Naroff | 4bd2f71 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 5641 | return; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5642 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5643 | |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5644 | // We might need to transform the operand if it is potentially evaluated. |
| 5645 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 5646 | if (Operand.isInvalid()) { |
| 5647 | DS.SetTypeSpecError(); |
| 5648 | return; |
| 5649 | } |
| 5650 | |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5651 | const char *PrevSpec = 0; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5652 | unsigned DiagID; |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5653 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5654 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 5655 | DiagID, Operand.get(), |
| 5656 | Actions.getASTContext().getPrintingPolicy())) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5657 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5658 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5659 | |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 5660 | /// [C11] atomic-specifier: |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5661 | /// _Atomic ( type-name ) |
| 5662 | /// |
| 5663 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5664 | assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) && |
| 5665 | "Not an atomic specifier"); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5666 | |
| 5667 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5668 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5669 | if (T.consumeOpen()) |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5670 | return; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5671 | |
| 5672 | TypeResult Result = ParseTypeName(); |
| 5673 | if (Result.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5674 | SkipUntil(tok::r_paren, StopAtSemi); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5675 | return; |
| 5676 | } |
| 5677 | |
| 5678 | // Match the ')' |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5679 | T.consumeClose(); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5680 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5681 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5682 | return; |
| 5683 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5684 | DS.setTypeofParensRange(T.getRange()); |
| 5685 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5686 | |
| 5687 | const char *PrevSpec = 0; |
| 5688 | unsigned DiagID; |
| 5689 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 5690 | DiagID, Result.release(), |
| 5691 | Actions.getASTContext().getPrintingPolicy())) |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5692 | Diag(StartLoc, DiagID) << PrevSpec; |
| 5693 | } |
| 5694 | |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5695 | |
| 5696 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 5697 | /// from TryAltiVecVectorToken. |
| 5698 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 5699 | Token Next = NextToken(); |
| 5700 | switch (Next.getKind()) { |
| 5701 | default: return false; |
| 5702 | case tok::kw_short: |
| 5703 | case tok::kw_long: |
| 5704 | case tok::kw_signed: |
| 5705 | case tok::kw_unsigned: |
| 5706 | case tok::kw_void: |
| 5707 | case tok::kw_char: |
| 5708 | case tok::kw_int: |
| 5709 | case tok::kw_float: |
| 5710 | case tok::kw_double: |
| 5711 | case tok::kw_bool: |
| 5712 | case tok::kw___pixel: |
| 5713 | Tok.setKind(tok::kw___vector); |
| 5714 | return true; |
| 5715 | case tok::identifier: |
| 5716 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5717 | Tok.setKind(tok::kw___vector); |
| 5718 | return true; |
| 5719 | } |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5720 | if (Next.getIdentifierInfo() == Ident_bool) { |
| 5721 | Tok.setKind(tok::kw___vector); |
| 5722 | return true; |
| 5723 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5724 | return false; |
| 5725 | } |
| 5726 | } |
| 5727 | |
| 5728 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 5729 | const char *&PrevSpec, unsigned &DiagID, |
| 5730 | bool &isInvalid) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 5731 | const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy(); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5732 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 5733 | Token Next = NextToken(); |
| 5734 | switch (Next.getKind()) { |
| 5735 | case tok::kw_short: |
| 5736 | case tok::kw_long: |
| 5737 | case tok::kw_signed: |
| 5738 | case tok::kw_unsigned: |
| 5739 | case tok::kw_void: |
| 5740 | case tok::kw_char: |
| 5741 | case tok::kw_int: |
| 5742 | case tok::kw_float: |
| 5743 | case tok::kw_double: |
| 5744 | case tok::kw_bool: |
| 5745 | case tok::kw___pixel: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 5746 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5747 | return true; |
| 5748 | case tok::identifier: |
| 5749 | if (Next.getIdentifierInfo() == Ident_pixel) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 5750 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5751 | return true; |
| 5752 | } |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5753 | if (Next.getIdentifierInfo() == Ident_bool) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 5754 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy); |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5755 | return true; |
| 5756 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5757 | break; |
| 5758 | default: |
| 5759 | break; |
| 5760 | } |
Douglas Gregor | 9938e3b | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 5761 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5762 | DS.isTypeAltiVecVector()) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 5763 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy); |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5764 | return true; |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5765 | } else if ((Tok.getIdentifierInfo() == Ident_bool) && |
| 5766 | DS.isTypeAltiVecVector()) { |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 5767 | isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy); |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5768 | return true; |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5769 | } |
| 5770 | return false; |
| 5771 | } |