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" |
Larisse Voufo | 725de3e | 2013-06-21 00:08:46 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclTemplate.h" |
Benjamin Kramer | d7d2b1f | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 17 | #include "clang/Basic/AddressSpaces.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 18 | #include "clang/Basic/CharInfo.h" |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 19 | #include "clang/Basic/OpenCL.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) { |
| 72 | return llvm::StringSwitch<bool>(II.getName()) |
| 73 | #include "clang/Parse/AttrLateParsed.inc" |
| 74 | .Default(false); |
| 75 | } |
| 76 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 77 | /// ParseGNUAttributes - Parse a non-empty attributes list. |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 78 | /// |
| 79 | /// [GNU] attributes: |
| 80 | /// attribute |
| 81 | /// attributes attribute |
| 82 | /// |
| 83 | /// [GNU] attribute: |
| 84 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 85 | /// |
| 86 | /// [GNU] attribute-list: |
| 87 | /// attrib |
| 88 | /// attribute_list ',' attrib |
| 89 | /// |
| 90 | /// [GNU] attrib: |
| 91 | /// empty |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 92 | /// attrib-name |
| 93 | /// attrib-name '(' identifier ')' |
| 94 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 95 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 96 | /// |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 97 | /// [GNU] attrib-name: |
| 98 | /// identifier |
| 99 | /// typespec |
| 100 | /// typequal |
| 101 | /// storageclass |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | /// |
Richard Smith | f7ca0c0 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 103 | /// Whether an attribute takes an 'identifier' is determined by the |
| 104 | /// attrib-name. GCC's behavior here is not worth imitating: |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 105 | /// |
Richard Smith | f7ca0c0 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 106 | /// * In C mode, if the attribute argument list starts with an identifier |
| 107 | /// followed by a ',' or an ')', and the identifier doesn't resolve to |
| 108 | /// a type, it is parsed as an identifier. If the attribute actually |
| 109 | /// wanted an expression, it's out of luck (but it turns out that no |
| 110 | /// attributes work that way, because C constant expressions are very |
| 111 | /// limited). |
| 112 | /// * In C++ mode, if the attribute argument list starts with an identifier, |
| 113 | /// and the attribute *wants* an identifier, it is parsed as an identifier. |
| 114 | /// At block scope, any additional tokens between the identifier and the |
| 115 | /// ',' or ')' are ignored, otherwise they produce a parse error. |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 116 | /// |
Richard Smith | f7ca0c0 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 117 | /// 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] | 118 | void Parser::ParseGNUAttributes(ParsedAttributes &attrs, |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 119 | SourceLocation *endLoc, |
| 120 | LateParsedAttrList *LateAttrs) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 121 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 123 | while (Tok.is(tok::kw___attribute)) { |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 124 | ConsumeToken(); |
| 125 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 126 | "attribute")) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 127 | SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ; |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 128 | return; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 129 | } |
| 130 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 131 | SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ; |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 132 | return; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 133 | } |
| 134 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 135 | while (true) { |
| 136 | // Allow empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 137 | if (TryConsumeToken(tok::comma)) |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 138 | continue; |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 139 | |
| 140 | // Expect an identifier or declaration specifier (const, int, etc.) |
| 141 | if (Tok.isNot(tok::identifier) && !isDeclarationSpecifier()) |
| 142 | break; |
| 143 | |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 144 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 145 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 147 | if (Tok.isNot(tok::l_paren)) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 148 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 149 | AttributeList::AS_GNU); |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 150 | continue; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 151 | } |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 152 | |
| 153 | // Handle "parameterized" attributes |
| 154 | if (!LateAttrs || !isAttributeLateParsed(*AttrName)) { |
| 155 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc, 0, |
| 156 | SourceLocation(), AttributeList::AS_GNU); |
| 157 | continue; |
| 158 | } |
| 159 | |
| 160 | // Handle attributes with arguments that require late parsing. |
| 161 | LateParsedAttribute *LA = |
| 162 | new LateParsedAttribute(this, *AttrName, AttrNameLoc); |
| 163 | LateAttrs->push_back(LA); |
| 164 | |
| 165 | // Attributes in a class are parsed at the end of the class, along |
| 166 | // with other late-parsed declarations. |
| 167 | if (!ClassStack.empty() && !LateAttrs->parseSoon()) |
| 168 | getCurrentClass().LateParsedDeclarations.push_back(LA); |
| 169 | |
| 170 | // consume everything up to and including the matching right parens |
| 171 | ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false); |
| 172 | |
| 173 | Token Eof; |
| 174 | Eof.startToken(); |
| 175 | Eof.setLocation(Tok.getLocation()); |
| 176 | LA->Toks.push_back(Eof); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 177 | } |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 178 | |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 179 | if (ExpectAndConsume(tok::r_paren)) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 180 | SkipUntil(tok::r_paren, StopAtSemi); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 181 | SourceLocation Loc = Tok.getLocation(); |
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); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 184 | if (endLoc) |
| 185 | *endLoc = Loc; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 186 | } |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 187 | } |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 188 | |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 189 | /// \brief Normalizes an attribute name by dropping prefixed and suffixed __. |
| 190 | static StringRef normalizeAttrName(StringRef Name) { |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 191 | if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__")) |
| 192 | Name = Name.drop_front(2).drop_back(2); |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 193 | return Name; |
| 194 | } |
| 195 | |
| 196 | /// \brief Determine whether the given attribute has an identifier argument. |
| 197 | static bool attributeHasIdentifierArg(const IdentifierInfo &II) { |
| 198 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 199 | #include "clang/Parse/AttrIdentifierArg.inc" |
Douglas Gregor | d2472d4 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 200 | .Default(false); |
| 201 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 202 | |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 203 | /// \brief Determine whether the given attribute parses a type argument. |
| 204 | static bool attributeIsTypeArgAttr(const IdentifierInfo &II) { |
| 205 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
| 206 | #include "clang/Parse/AttrTypeArg.inc" |
| 207 | .Default(false); |
| 208 | } |
| 209 | |
Aaron Ballman | 15b27b9 | 2014-01-09 19:39:35 +0000 | [diff] [blame] | 210 | /// \brief Determine whether the given attribute requires parsing its arguments |
| 211 | /// in an unevaluated context or not. |
| 212 | static bool attributeParsedArgsUnevaluated(const IdentifierInfo &II) { |
| 213 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
| 214 | #include "clang/Parse/AttrArgContext.inc" |
| 215 | .Default(false); |
| 216 | } |
| 217 | |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 218 | IdentifierLoc *Parser::ParseIdentifierLoc() { |
| 219 | assert(Tok.is(tok::identifier) && "expected an identifier"); |
| 220 | IdentifierLoc *IL = IdentifierLoc::create(Actions.Context, |
| 221 | Tok.getLocation(), |
| 222 | Tok.getIdentifierInfo()); |
| 223 | ConsumeToken(); |
| 224 | return IL; |
| 225 | } |
| 226 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 227 | void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName, |
| 228 | SourceLocation AttrNameLoc, |
| 229 | ParsedAttributes &Attrs, |
| 230 | SourceLocation *EndLoc) { |
| 231 | BalancedDelimiterTracker Parens(*this, tok::l_paren); |
| 232 | Parens.consumeOpen(); |
| 233 | |
| 234 | TypeResult T; |
| 235 | if (Tok.isNot(tok::r_paren)) |
| 236 | T = ParseTypeName(); |
| 237 | |
| 238 | if (Parens.consumeClose()) |
| 239 | return; |
| 240 | |
| 241 | if (T.isInvalid()) |
| 242 | return; |
| 243 | |
| 244 | if (T.isUsable()) |
| 245 | Attrs.addNewTypeAttr(&AttrName, |
| 246 | SourceRange(AttrNameLoc, Parens.getCloseLocation()), 0, |
| 247 | AttrNameLoc, T.get(), AttributeList::AS_GNU); |
| 248 | else |
| 249 | Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()), |
| 250 | 0, AttrNameLoc, 0, 0, AttributeList::AS_GNU); |
| 251 | } |
| 252 | |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 253 | /// Parse the arguments to a parameterized GNU attribute or |
| 254 | /// a C++11 attribute in "gnu" namespace. |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 255 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 256 | SourceLocation AttrNameLoc, |
| 257 | ParsedAttributes &Attrs, |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 258 | SourceLocation *EndLoc, |
| 259 | IdentifierInfo *ScopeName, |
| 260 | SourceLocation ScopeLoc, |
| 261 | AttributeList::Syntax Syntax) { |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 262 | |
| 263 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 264 | |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 265 | AttributeList::Kind AttrKind = |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 266 | AttributeList::getKind(AttrName, ScopeName, Syntax); |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 267 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 268 | // Availability attributes have their own grammar. |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 269 | // FIXME: All these cases fail to pass in the syntax and scope, and might be |
| 270 | // written as C++11 gnu:: attributes. |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 271 | if (AttrKind == AttributeList::AT_Availability) { |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 272 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 273 | return; |
| 274 | } |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 275 | |
| 276 | if (AttrKind == AttributeList::AT_ObjCBridgeRelated) { |
| 277 | ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 278 | return; |
| 279 | } |
| 280 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 281 | // Type safety attributes have their own grammar. |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 282 | if (AttrKind == AttributeList::AT_TypeTagForDatatype) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 283 | ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 284 | return; |
| 285 | } |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 286 | // Some attributes expect solely a type parameter. |
| 287 | if (attributeIsTypeArgAttr(*AttrName)) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 288 | ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 289 | return; |
| 290 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 291 | |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 292 | // Ignore the left paren location for now. |
| 293 | ConsumeParen(); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 294 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 295 | ArgsVector ArgExprs; |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 296 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 297 | if (Tok.is(tok::identifier)) { |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 298 | // If this attribute wants an 'identifier' argument, make it so. |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 299 | bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName); |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 300 | |
| 301 | // If we don't know how to parse this attribute, but this is the only |
| 302 | // token in this argument, assume it's meant to be an identifier. |
Aaron Ballman | 6603747 | 2013-12-04 15:32:26 +0000 | [diff] [blame] | 303 | if (AttrKind == AttributeList::UnknownAttribute || |
| 304 | AttrKind == AttributeList::IgnoredAttribute) { |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 305 | const Token &Next = NextToken(); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 306 | IsIdentifierArg = Next.is(tok::r_paren) || Next.is(tok::comma); |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 307 | } |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 308 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 309 | if (IsIdentifierArg) |
| 310 | ArgExprs.push_back(ParseIdentifierLoc()); |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 313 | if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) { |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 314 | // Eat the comma. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 315 | if (!ArgExprs.empty()) |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 316 | ConsumeToken(); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 317 | |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 318 | // Parse the non-empty comma-separated list of expressions. |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 319 | do { |
Aaron Ballman | 7c1fcf8 | 2014-01-09 20:12:12 +0000 | [diff] [blame] | 320 | OwningPtr<EnterExpressionEvaluationContext> Unevaluated; |
| 321 | if (attributeParsedArgsUnevaluated(*AttrName)) |
| 322 | Unevaluated.reset(new EnterExpressionEvaluationContext(Actions, |
| 323 | Sema::Unevaluated)); |
| 324 | |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 325 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 326 | if (ArgExpr.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 327 | SkipUntil(tok::r_paren, StopAtSemi); |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 328 | return; |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 329 | } |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 330 | ArgExprs.push_back(ArgExpr.release()); |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 331 | // Eat the comma, move to the next argument |
| 332 | } while (TryConsumeToken(tok::comma)); |
Fariborz Jahanian | 6b70865 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 333 | } |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 334 | |
| 335 | SourceLocation RParen = Tok.getLocation(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 336 | if (!ExpectAndConsume(tok::r_paren)) { |
Michael Han | 360d225 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 337 | SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc; |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 338 | Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc, |
| 339 | ArgExprs.data(), ArgExprs.size(), Syntax); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 340 | } |
Aaron Ballman | 7c1fcf8 | 2014-01-09 20:12:12 +0000 | [diff] [blame] | 341 | |
| 342 | if (EndLoc) |
| 343 | *EndLoc = RParen; |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 346 | /// \brief Parses a single argument for a declspec, including the |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 347 | /// surrounding parens. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 348 | void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName, |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 349 | SourceLocation AttrNameLoc, |
| 350 | ParsedAttributes &Attrs) |
| 351 | { |
| 352 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 353 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 354 | AttrName->getNameStart(), tok::r_paren)) |
| 355 | return; |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 356 | |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 357 | ExprResult ArgExpr(ParseConstantExpression()); |
| 358 | if (ArgExpr.isInvalid()) { |
| 359 | T.skipToEnd(); |
| 360 | return; |
| 361 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 362 | ArgsUnion ExprList = ArgExpr.take(); |
| 363 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1, |
| 364 | AttributeList::AS_Declspec); |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 365 | |
| 366 | T.consumeClose(); |
| 367 | } |
| 368 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 369 | /// \brief Determines whether a declspec is a "simple" one requiring no |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 370 | /// arguments. |
| 371 | bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) { |
| 372 | return llvm::StringSwitch<bool>(Ident->getName()) |
| 373 | .Case("dllimport", true) |
| 374 | .Case("dllexport", true) |
| 375 | .Case("noreturn", true) |
| 376 | .Case("nothrow", true) |
| 377 | .Case("noinline", true) |
| 378 | .Case("naked", true) |
| 379 | .Case("appdomain", true) |
| 380 | .Case("process", true) |
| 381 | .Case("jitintrinsic", true) |
| 382 | .Case("noalias", true) |
| 383 | .Case("restrict", true) |
| 384 | .Case("novtable", true) |
| 385 | .Case("selectany", true) |
| 386 | .Case("thread", true) |
Aaron Ballman | 444eb6e | 2013-05-04 16:58:37 +0000 | [diff] [blame] | 387 | .Case("safebuffers", true ) |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 388 | .Default(false); |
| 389 | } |
| 390 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 391 | /// \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] | 392 | /// parameters). Will return false if we properly handled the declspec, or |
| 393 | /// true if it is an unknown declspec. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 394 | void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident, |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 395 | SourceLocation Loc, |
| 396 | ParsedAttributes &Attrs) { |
| 397 | // Try to handle the easy case first -- these declspecs all take a single |
| 398 | // parameter as their argument. |
| 399 | if (llvm::StringSwitch<bool>(Ident->getName()) |
| 400 | .Case("uuid", true) |
| 401 | .Case("align", true) |
| 402 | .Case("allocate", true) |
| 403 | .Default(false)) { |
| 404 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 405 | } else if (Ident->getName() == "deprecated") { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 406 | // The deprecated declspec has an optional single argument, so we will |
| 407 | // 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] | 408 | // not. |
| 409 | if (Tok.getKind() == tok::l_paren) |
| 410 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 411 | else |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 412 | Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec); |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 413 | } else if (Ident->getName() == "property") { |
| 414 | // 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] | 415 | // assignment expressions as a parameter, but the lhs of the assignment |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 416 | // must be named get or put. |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 417 | if (Tok.isNot(tok::l_paren)) { |
| 418 | Diag(Tok.getLocation(), diag::err_expected_lparen_after) |
| 419 | << Ident->getNameStart(); |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 420 | return; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 421 | } |
| 422 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 423 | T.expectAndConsume(diag::err_expected_lparen_after, |
| 424 | Ident->getNameStart(), tok::r_paren); |
| 425 | |
| 426 | enum AccessorKind { |
| 427 | AK_Invalid = -1, |
| 428 | AK_Put = 0, AK_Get = 1 // indices into AccessorNames |
| 429 | }; |
| 430 | IdentifierInfo *AccessorNames[] = { 0, 0 }; |
| 431 | bool HasInvalidAccessor = false; |
| 432 | |
| 433 | // Parse the accessor specifications. |
| 434 | while (true) { |
| 435 | // Stop if this doesn't look like an accessor spec. |
| 436 | if (!Tok.is(tok::identifier)) { |
| 437 | // If the user wrote a completely empty list, use a special diagnostic. |
| 438 | if (Tok.is(tok::r_paren) && !HasInvalidAccessor && |
| 439 | AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) { |
| 440 | Diag(Loc, diag::err_ms_property_no_getter_or_putter); |
| 441 | break; |
| 442 | } |
| 443 | |
| 444 | Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor); |
| 445 | break; |
| 446 | } |
| 447 | |
| 448 | AccessorKind Kind; |
| 449 | SourceLocation KindLoc = Tok.getLocation(); |
| 450 | StringRef KindStr = Tok.getIdentifierInfo()->getName(); |
| 451 | if (KindStr == "get") { |
| 452 | Kind = AK_Get; |
| 453 | } else if (KindStr == "put") { |
| 454 | Kind = AK_Put; |
| 455 | |
| 456 | // Recover from the common mistake of using 'set' instead of 'put'. |
| 457 | } else if (KindStr == "set") { |
| 458 | Diag(KindLoc, diag::err_ms_property_has_set_accessor) |
| 459 | << FixItHint::CreateReplacement(KindLoc, "put"); |
| 460 | Kind = AK_Put; |
| 461 | |
| 462 | // Handle the mistake of forgetting the accessor kind by skipping |
| 463 | // this accessor. |
| 464 | } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) { |
| 465 | Diag(KindLoc, diag::err_ms_property_missing_accessor_kind); |
| 466 | ConsumeToken(); |
| 467 | HasInvalidAccessor = true; |
| 468 | goto next_property_accessor; |
| 469 | |
| 470 | // Otherwise, complain about the unknown accessor kind. |
| 471 | } else { |
| 472 | Diag(KindLoc, diag::err_ms_property_unknown_accessor); |
| 473 | HasInvalidAccessor = true; |
| 474 | Kind = AK_Invalid; |
| 475 | |
| 476 | // Try to keep parsing unless it doesn't look like an accessor spec. |
| 477 | if (!NextToken().is(tok::equal)) break; |
| 478 | } |
| 479 | |
| 480 | // Consume the identifier. |
| 481 | ConsumeToken(); |
| 482 | |
| 483 | // Consume the '='. |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 484 | if (!TryConsumeToken(tok::equal)) { |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 485 | Diag(Tok.getLocation(), diag::err_ms_property_expected_equal) |
| 486 | << KindStr; |
| 487 | break; |
| 488 | } |
| 489 | |
| 490 | // Expect the method name. |
| 491 | if (!Tok.is(tok::identifier)) { |
| 492 | Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name); |
| 493 | break; |
| 494 | } |
| 495 | |
| 496 | if (Kind == AK_Invalid) { |
| 497 | // Just drop invalid accessors. |
| 498 | } else if (AccessorNames[Kind] != NULL) { |
| 499 | // Complain about the repeated accessor, ignore it, and keep parsing. |
| 500 | Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr; |
| 501 | } else { |
| 502 | AccessorNames[Kind] = Tok.getIdentifierInfo(); |
| 503 | } |
| 504 | ConsumeToken(); |
| 505 | |
| 506 | next_property_accessor: |
| 507 | // Keep processing accessors until we run out. |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 508 | if (TryConsumeToken(tok::comma)) |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 509 | continue; |
| 510 | |
| 511 | // If we run into the ')', stop without consuming it. |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 512 | if (Tok.is(tok::r_paren)) |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 513 | break; |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 514 | |
| 515 | Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen); |
| 516 | break; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | // Only add the property attribute if it was well-formed. |
| 520 | if (!HasInvalidAccessor) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 521 | Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(), |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 522 | AccessorNames[AK_Get], AccessorNames[AK_Put], |
| 523 | AttributeList::AS_Declspec); |
| 524 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 525 | T.skipToEnd(); |
| 526 | } else { |
| 527 | // We don't recognize this as a valid declspec, but instead of creating the |
| 528 | // attribute and allowing sema to warn about it, we will warn here instead. |
| 529 | // This is because some attributes have multiple spellings, but we need to |
| 530 | // 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] | 531 | // attribute, we'd have to split the valid declspec spelling logic into |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 532 | // both locations. |
| 533 | Diag(Loc, diag::warn_ms_declspec_unknown) << Ident; |
| 534 | |
| 535 | // If there's an open paren, we should eat the open and close parens under |
| 536 | // the assumption that this unknown declspec has parameters. |
| 537 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 538 | if (!T.consumeOpen()) |
| 539 | T.skipToEnd(); |
| 540 | } |
| 541 | } |
| 542 | |
Eli Friedman | 06de2b5 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 543 | /// [MS] decl-specifier: |
| 544 | /// __declspec ( extended-decl-modifier-seq ) |
| 545 | /// |
| 546 | /// [MS] extended-decl-modifier-seq: |
| 547 | /// extended-decl-modifier[opt] |
| 548 | /// extended-decl-modifier extended-decl-modifier-seq |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 549 | void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) { |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 550 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | 06de2b5 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 551 | |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 552 | ConsumeToken(); |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 553 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 554 | if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec", |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 555 | tok::r_paren)) |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 556 | return; |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 557 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 558 | // An empty declspec is perfectly legal and should not warn. Additionally, |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 559 | // you can specify multiple attributes per declspec. |
| 560 | while (Tok.getKind() != tok::r_paren) { |
| 561 | // We expect either a well-known identifier or a generic string. Anything |
| 562 | // else is a malformed declspec. |
| 563 | bool IsString = Tok.getKind() == tok::string_literal ? true : false; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 564 | if (!IsString && Tok.getKind() != tok::identifier && |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 565 | Tok.getKind() != tok::kw_restrict) { |
| 566 | Diag(Tok, diag::err_ms_declspec_type); |
| 567 | T.skipToEnd(); |
| 568 | return; |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 569 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 570 | |
| 571 | IdentifierInfo *AttrName; |
| 572 | SourceLocation AttrNameLoc; |
| 573 | if (IsString) { |
| 574 | SmallString<8> StrBuffer; |
| 575 | bool Invalid = false; |
| 576 | StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid); |
| 577 | if (Invalid) { |
| 578 | T.skipToEnd(); |
| 579 | return; |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 580 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 581 | AttrName = PP.getIdentifierInfo(Str); |
| 582 | AttrNameLoc = ConsumeStringToken(); |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 583 | } else { |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 584 | AttrName = Tok.getIdentifierInfo(); |
| 585 | AttrNameLoc = ConsumeToken(); |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 586 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 587 | |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 588 | if (IsString || IsSimpleMicrosoftDeclSpec(AttrName)) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 589 | // If we have a generic string, we will allow it because there is no |
| 590 | // documented list of allowable string declspecs, but we know they exist |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 591 | // (for instance, SAL declspecs in older versions of MSVC). |
| 592 | // |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 593 | // Alternatively, if the identifier is a simple one, then it requires no |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 594 | // arguments and can be turned into an attribute directly. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 595 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 596 | AttributeList::AS_Declspec); |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 597 | else |
| 598 | ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs); |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 599 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 600 | T.consumeClose(); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 601 | } |
| 602 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 603 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 604 | // Treat these like attributes |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 605 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 606 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 607 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 608 | Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) || |
| 609 | Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) { |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 610 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 611 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 612 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 613 | AttributeList::AS_Keyword); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 614 | } |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 615 | } |
| 616 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 617 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 618 | // Treat these like attributes |
| 619 | while (Tok.is(tok::kw___pascal)) { |
| 620 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 621 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 622 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 623 | AttributeList::AS_Keyword); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 624 | } |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 627 | void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { |
| 628 | // Treat these like attributes |
| 629 | while (Tok.is(tok::kw___kernel)) { |
Richard Smith | 0cdcc98 | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 630 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 631 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 632 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 633 | AttributeList::AS_Keyword); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 637 | void Parser::ParseOpenCLQualifiers(DeclSpec &DS) { |
Richard Smith | 0cdcc98 | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 638 | // FIXME: The mapping from attribute spelling to semantics should be |
| 639 | // performed in Sema, not here. |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 640 | SourceLocation Loc = Tok.getLocation(); |
| 641 | switch(Tok.getKind()) { |
| 642 | // OpenCL qualifiers: |
| 643 | case tok::kw___private: |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 644 | DS.getAttributes().addNewInteger( |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 645 | Actions.getASTContext(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 646 | PP.getIdentifierInfo("address_space"), Loc, 0); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 647 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 648 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 649 | case tok::kw___global: |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 650 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 651 | Actions.getASTContext(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 652 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 653 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 654 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 655 | case tok::kw___local: |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 656 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 657 | Actions.getASTContext(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 658 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 659 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 660 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 661 | case tok::kw___constant: |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 662 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 663 | Actions.getASTContext(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 664 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 665 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 666 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 667 | case tok::kw___read_only: |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 668 | DS.getAttributes().addNewInteger( |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 669 | Actions.getASTContext(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 670 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 671 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 672 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 673 | case tok::kw___write_only: |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 674 | DS.getAttributes().addNewInteger( |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 675 | Actions.getASTContext(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 676 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 677 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 678 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 679 | case tok::kw___read_write: |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 680 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 681 | Actions.getASTContext(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 682 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 683 | break; |
| 684 | default: break; |
| 685 | } |
| 686 | } |
| 687 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 688 | /// \brief Parse a version number. |
| 689 | /// |
| 690 | /// version: |
| 691 | /// simple-integer |
| 692 | /// simple-integer ',' simple-integer |
| 693 | /// simple-integer ',' simple-integer ',' simple-integer |
| 694 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
| 695 | Range = Tok.getLocation(); |
| 696 | |
| 697 | if (!Tok.is(tok::numeric_constant)) { |
| 698 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 699 | SkipUntil(tok::comma, tok::r_paren, |
| 700 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 701 | return VersionTuple(); |
| 702 | } |
| 703 | |
| 704 | // Parse the major (and possibly minor and subminor) versions, which |
| 705 | // are stored in the numeric constant. We utilize a quirk of the |
| 706 | // lexer, which is that it handles something like 1.2.3 as a single |
| 707 | // numeric constant, rather than two separate tokens. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 708 | SmallString<512> Buffer; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 709 | Buffer.resize(Tok.getLength()+1); |
| 710 | const char *ThisTokBegin = &Buffer[0]; |
| 711 | |
| 712 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 713 | bool Invalid = false; |
| 714 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 715 | if (Invalid) |
| 716 | return VersionTuple(); |
| 717 | |
| 718 | // Parse the major version. |
| 719 | unsigned AfterMajor = 0; |
| 720 | unsigned Major = 0; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 721 | while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 722 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 723 | ++AfterMajor; |
| 724 | } |
| 725 | |
| 726 | if (AfterMajor == 0) { |
| 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 | if (AfterMajor == ActualLength) { |
| 734 | ConsumeToken(); |
| 735 | |
| 736 | // We only had a single version component. |
| 737 | if (Major == 0) { |
| 738 | Diag(Tok, diag::err_zero_version); |
| 739 | return VersionTuple(); |
| 740 | } |
| 741 | |
| 742 | return VersionTuple(Major); |
| 743 | } |
| 744 | |
| 745 | if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) { |
| 746 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 747 | SkipUntil(tok::comma, tok::r_paren, |
| 748 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 749 | return VersionTuple(); |
| 750 | } |
| 751 | |
| 752 | // Parse the minor version. |
| 753 | unsigned AfterMinor = AfterMajor + 1; |
| 754 | unsigned Minor = 0; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 755 | while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 756 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 757 | ++AfterMinor; |
| 758 | } |
| 759 | |
| 760 | if (AfterMinor == ActualLength) { |
| 761 | ConsumeToken(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 762 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 763 | // We had major.minor. |
| 764 | if (Major == 0 && Minor == 0) { |
| 765 | Diag(Tok, diag::err_zero_version); |
| 766 | return VersionTuple(); |
| 767 | } |
| 768 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 769 | return VersionTuple(Major, Minor); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | // If what follows is not a '.', we have a problem. |
| 773 | if (ThisTokBegin[AfterMinor] != '.') { |
| 774 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 775 | SkipUntil(tok::comma, tok::r_paren, |
| 776 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 777 | return VersionTuple(); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | // Parse the subminor version. |
| 781 | unsigned AfterSubminor = AfterMinor + 1; |
| 782 | unsigned Subminor = 0; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 783 | while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 784 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 785 | ++AfterSubminor; |
| 786 | } |
| 787 | |
| 788 | if (AfterSubminor != ActualLength) { |
| 789 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 790 | SkipUntil(tok::comma, tok::r_paren, |
| 791 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 792 | return VersionTuple(); |
| 793 | } |
| 794 | ConsumeToken(); |
| 795 | return VersionTuple(Major, Minor, Subminor); |
| 796 | } |
| 797 | |
| 798 | /// \brief Parse the contents of the "availability" attribute. |
| 799 | /// |
| 800 | /// availability-attribute: |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 801 | /// 'availability' '(' platform ',' version-arg-list, opt-message')' |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 802 | /// |
| 803 | /// platform: |
| 804 | /// identifier |
| 805 | /// |
| 806 | /// version-arg-list: |
| 807 | /// version-arg |
| 808 | /// version-arg ',' version-arg-list |
| 809 | /// |
| 810 | /// version-arg: |
| 811 | /// 'introduced' '=' version |
| 812 | /// 'deprecated' '=' version |
Douglas Gregor | fdd417f | 2012-03-11 04:53:21 +0000 | [diff] [blame] | 813 | /// 'obsoleted' = version |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 814 | /// 'unavailable' |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 815 | /// opt-message: |
| 816 | /// 'message' '=' <string> |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 817 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 818 | SourceLocation AvailabilityLoc, |
| 819 | ParsedAttributes &attrs, |
| 820 | SourceLocation *endLoc) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 821 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 822 | AvailabilityChange Changes[Unknown]; |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 823 | ExprResult MessageExpr; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 824 | |
| 825 | // Opening '('. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 826 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 827 | if (T.consumeOpen()) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 828 | Diag(Tok, diag::err_expected) << tok::l_paren; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 829 | return; |
| 830 | } |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 831 | |
| 832 | // Parse the platform name, |
| 833 | if (Tok.isNot(tok::identifier)) { |
| 834 | Diag(Tok, diag::err_availability_expected_platform); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 835 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 836 | return; |
| 837 | } |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 838 | IdentifierLoc *Platform = ParseIdentifierLoc(); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 839 | |
| 840 | // Parse the ',' following the platform name. |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 841 | if (ExpectAndConsume(tok::comma)) { |
| 842 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 843 | return; |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 844 | } |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 845 | |
| 846 | // If we haven't grabbed the pointers for the identifiers |
| 847 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 848 | if (!Ident_introduced) { |
| 849 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 850 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 851 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 852 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 853 | Ident_message = PP.getIdentifierInfo("message"); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 857 | SourceLocation UnavailableLoc; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 858 | do { |
| 859 | if (Tok.isNot(tok::identifier)) { |
| 860 | Diag(Tok, diag::err_availability_expected_change); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 861 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 862 | return; |
| 863 | } |
| 864 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 865 | SourceLocation KeywordLoc = ConsumeToken(); |
| 866 | |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 867 | if (Keyword == Ident_unavailable) { |
| 868 | if (UnavailableLoc.isValid()) { |
| 869 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 870 | << Keyword << SourceRange(UnavailableLoc); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 871 | } |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 872 | UnavailableLoc = KeywordLoc; |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 873 | continue; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 876 | if (Tok.isNot(tok::equal)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 877 | Diag(Tok, diag::err_expected_after) << Keyword << tok::equal; |
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 | ConsumeToken(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 882 | if (Keyword == Ident_message) { |
Benjamin Kramer | a9dfa92 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 883 | if (Tok.isNot(tok::string_literal)) { // Also reject wide string literals. |
Andy Gibbs | a8df57a | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 884 | Diag(Tok, diag::err_expected_string_literal) |
| 885 | << /*Source='availability attribute'*/2; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 886 | SkipUntil(tok::r_paren, StopAtSemi); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 887 | return; |
| 888 | } |
| 889 | MessageExpr = ParseStringLiteralExpression(); |
| 890 | break; |
| 891 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 892 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 893 | SourceRange VersionRange; |
| 894 | VersionTuple Version = ParseVersionTuple(VersionRange); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 895 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 896 | if (Version.empty()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 897 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 898 | return; |
| 899 | } |
| 900 | |
| 901 | unsigned Index; |
| 902 | if (Keyword == Ident_introduced) |
| 903 | Index = Introduced; |
| 904 | else if (Keyword == Ident_deprecated) |
| 905 | Index = Deprecated; |
| 906 | else if (Keyword == Ident_obsoleted) |
| 907 | Index = Obsoleted; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 908 | else |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 909 | Index = Unknown; |
| 910 | |
| 911 | if (Index < Unknown) { |
| 912 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 913 | Diag(KeywordLoc, diag::err_availability_redundant) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 914 | << Keyword |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 915 | << SourceRange(Changes[Index].KeywordLoc, |
| 916 | Changes[Index].VersionRange.getEnd()); |
| 917 | } |
| 918 | |
| 919 | Changes[Index].KeywordLoc = KeywordLoc; |
| 920 | Changes[Index].Version = Version; |
| 921 | Changes[Index].VersionRange = VersionRange; |
| 922 | } else { |
| 923 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 924 | << Keyword << VersionRange; |
| 925 | } |
| 926 | |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 927 | } while (TryConsumeToken(tok::comma)); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 928 | |
| 929 | // Closing ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 930 | if (T.consumeClose()) |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 931 | return; |
| 932 | |
| 933 | if (endLoc) |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 934 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 935 | |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 936 | // The 'unavailable' availability cannot be combined with any other |
| 937 | // availability changes. Make sure that hasn't happened. |
| 938 | if (UnavailableLoc.isValid()) { |
| 939 | bool Complained = false; |
| 940 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 941 | if (Changes[Index].KeywordLoc.isValid()) { |
| 942 | if (!Complained) { |
| 943 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 944 | << SourceRange(Changes[Index].KeywordLoc, |
| 945 | Changes[Index].VersionRange.getEnd()); |
| 946 | Complained = true; |
| 947 | } |
| 948 | |
| 949 | // Clear out the availability. |
| 950 | Changes[Index] = AvailabilityChange(); |
| 951 | } |
| 952 | } |
| 953 | } |
| 954 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 955 | // Record this attribute |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 956 | attrs.addNew(&Availability, |
| 957 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Fariborz Jahanian | 586be88 | 2012-01-23 23:38:32 +0000 | [diff] [blame] | 958 | 0, AvailabilityLoc, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 959 | Platform, |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 960 | Changes[Introduced], |
| 961 | Changes[Deprecated], |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 962 | Changes[Obsoleted], |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 963 | UnavailableLoc, MessageExpr.take(), |
Alexis Hunt | a0e54d4 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 964 | AttributeList::AS_GNU); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 967 | /// \brief Parse the contents of the "objc_bridge_related" attribute. |
| 968 | /// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')' |
| 969 | /// related_class: |
| 970 | /// Identifier |
| 971 | /// |
| 972 | /// opt-class_method: |
| 973 | /// Identifier: | <empty> |
| 974 | /// |
| 975 | /// opt-instance_method: |
| 976 | /// Identifier | <empty> |
| 977 | /// |
| 978 | void Parser::ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated, |
| 979 | SourceLocation ObjCBridgeRelatedLoc, |
| 980 | ParsedAttributes &attrs, |
| 981 | SourceLocation *endLoc) { |
| 982 | // Opening '('. |
| 983 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 984 | if (T.consumeOpen()) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 985 | Diag(Tok, diag::err_expected) << tok::l_paren; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 986 | return; |
| 987 | } |
| 988 | |
| 989 | // Parse the related class name. |
| 990 | if (Tok.isNot(tok::identifier)) { |
| 991 | Diag(Tok, diag::err_objcbridge_related_expected_related_class); |
| 992 | SkipUntil(tok::r_paren, StopAtSemi); |
| 993 | return; |
| 994 | } |
| 995 | IdentifierLoc *RelatedClass = ParseIdentifierLoc(); |
Alp Toker | 9765056 | 2014-01-10 11:19:30 +0000 | [diff] [blame] | 996 | if (ExpectAndConsume(tok::comma)) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 997 | SkipUntil(tok::r_paren, StopAtSemi); |
| 998 | return; |
| 999 | } |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1000 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1001 | // Parse optional class method name. |
| 1002 | IdentifierLoc *ClassMethod = 0; |
| 1003 | if (Tok.is(tok::identifier)) { |
| 1004 | ClassMethod = ParseIdentifierLoc(); |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1005 | if (!TryConsumeToken(tok::colon)) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1006 | Diag(Tok, diag::err_objcbridge_related_selector_name); |
| 1007 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1008 | return; |
| 1009 | } |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1010 | } |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1011 | if (!TryConsumeToken(tok::comma)) { |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1012 | if (Tok.is(tok::colon)) |
| 1013 | Diag(Tok, diag::err_objcbridge_related_selector_name); |
| 1014 | else |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1015 | Diag(Tok, diag::err_expected) << tok::comma; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1016 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1017 | return; |
| 1018 | } |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1019 | |
| 1020 | // Parse optional instance method name. |
| 1021 | IdentifierLoc *InstanceMethod = 0; |
| 1022 | if (Tok.is(tok::identifier)) |
| 1023 | InstanceMethod = ParseIdentifierLoc(); |
| 1024 | else if (Tok.isNot(tok::r_paren)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1025 | Diag(Tok, diag::err_expected) << tok::r_paren; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 1026 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1027 | return; |
| 1028 | } |
| 1029 | |
| 1030 | // Closing ')'. |
| 1031 | if (T.consumeClose()) |
| 1032 | return; |
| 1033 | |
| 1034 | if (endLoc) |
| 1035 | *endLoc = T.getCloseLocation(); |
| 1036 | |
| 1037 | // Record this attribute |
| 1038 | attrs.addNew(&ObjCBridgeRelated, |
| 1039 | SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()), |
| 1040 | 0, ObjCBridgeRelatedLoc, |
| 1041 | RelatedClass, |
| 1042 | ClassMethod, |
| 1043 | InstanceMethod, |
| 1044 | AttributeList::AS_GNU); |
| 1045 | |
| 1046 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1047 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1048 | // Late Parsed Attributes: |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1049 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 1050 | |
| 1051 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 1052 | |
| 1053 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 1054 | Self->ParseLexedAttributes(*Class); |
| 1055 | } |
| 1056 | |
| 1057 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1058 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 1062 | /// scope appropriately. |
| 1063 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 1064 | // Deal with templates |
| 1065 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 1066 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 1067 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 1068 | HasTemplateScope); |
| 1069 | if (HasTemplateScope) |
| 1070 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 1071 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1072 | // Set or update the scope flags. |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1073 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1074 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1075 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 1076 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 1077 | |
DeLesley Hutchins | 6f86004 | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1078 | // Enter the scope of nested classes |
| 1079 | if (!AlreadyHasClassScope) |
| 1080 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 1081 | Class.TagOrTemplate); |
Benjamin Kramer | 1d373c6 | 2012-05-17 12:01:52 +0000 | [diff] [blame] | 1082 | if (!Class.LateParsedDeclarations.empty()) { |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1083 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){ |
| 1084 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 1085 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1086 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1087 | |
DeLesley Hutchins | 6f86004 | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1088 | if (!AlreadyHasClassScope) |
| 1089 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 1090 | Class.TagOrTemplate); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1093 | |
| 1094 | /// \brief Parse all attributes in LAs, and attach them to Decl D. |
| 1095 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 1096 | bool EnterScope, bool OnDefinition) { |
DeLesley Hutchins | 66e300e | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1097 | assert(LAs.parseSoon() && |
| 1098 | "Attribute list should be marked for immediate parsing."); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1099 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 19c722d | 2012-08-15 22:41:04 +0000 | [diff] [blame] | 1100 | if (D) |
| 1101 | LAs[i]->addDecl(D); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1102 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
Benjamin Kramer | bafc49a | 2012-04-14 12:44:47 +0000 | [diff] [blame] | 1103 | delete LAs[i]; |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1104 | } |
| 1105 | LAs.clear(); |
| 1106 | } |
| 1107 | |
| 1108 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1109 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 1110 | /// This will be called at the end of parsing a class declaration |
| 1111 | /// for each LateParsedAttribute. We consume the saved tokens and |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1112 | /// create an attribute with the arguments filled in. We add this |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1113 | /// to the Attribute list for the decl. |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1114 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 1115 | bool EnterScope, bool OnDefinition) { |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1116 | // Save the current token position. |
| 1117 | SourceLocation OrigLoc = Tok.getLocation(); |
| 1118 | |
| 1119 | // Append the current token at the end of the new token stream so that it |
| 1120 | // doesn't get lost. |
| 1121 | LA.Toks.push_back(Tok); |
| 1122 | PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); |
| 1123 | // Consume the previously pushed token. |
Argyrios Kyrtzidis | c36633c | 2013-03-27 23:58:17 +0000 | [diff] [blame] | 1124 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1125 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1126 | if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) { |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1127 | // FIXME: Do not warn on C++11 attributes, once we start supporting |
| 1128 | // them here. |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1129 | Diag(Tok, diag::warn_attribute_on_function_definition) |
Aaron Ballman | 6d80b3c | 2014-01-02 18:10:17 +0000 | [diff] [blame] | 1130 | << &LA.AttrName; |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1133 | ParsedAttributes Attrs(AttrFactory); |
| 1134 | SourceLocation endLoc; |
| 1135 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1136 | if (LA.Decls.size() > 0) { |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1137 | Decl *D = LA.Decls[0]; |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1138 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1139 | RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext()); |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1140 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1141 | // Allow 'this' within late-parsed attributes. |
Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 1142 | Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0, |
| 1143 | ND && ND->isCXXInstanceMember()); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1144 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1145 | if (LA.Decls.size() == 1) { |
| 1146 | // If the Decl is templatized, add template parameters to scope. |
| 1147 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 1148 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 1149 | if (HasTemplateScope) |
| 1150 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1151 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1152 | // If the Decl is on a function, add function parameters to the scope. |
| 1153 | bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
| 1154 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope); |
| 1155 | if (HasFunScope) |
| 1156 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1157 | |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1158 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Michael Han | 360d225 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 1159 | 0, SourceLocation(), AttributeList::AS_GNU); |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1160 | |
| 1161 | if (HasFunScope) { |
| 1162 | Actions.ActOnExitFunctionContext(); |
| 1163 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 1164 | } |
| 1165 | if (HasTemplateScope) { |
| 1166 | TempScope.Exit(); |
| 1167 | } |
| 1168 | } else { |
| 1169 | // If there are multiple decls, then the decl cannot be within the |
| 1170 | // function scope. |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1171 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Michael Han | 360d225 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 1172 | 0, SourceLocation(), AttributeList::AS_GNU); |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1173 | } |
DeLesley Hutchins | 71d6103 | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 1174 | } else { |
| 1175 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1178 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) { |
| 1179 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
| 1180 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1181 | |
| 1182 | if (Tok.getLocation() != OrigLoc) { |
| 1183 | // Due to a parsing error, we either went over the cached tokens or |
| 1184 | // there are still cached tokens left, so we skip the leftover tokens. |
| 1185 | // Since this is an uncommon situation that should be avoided, use the |
| 1186 | // expensive isBeforeInTranslationUnit call. |
| 1187 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 1188 | OrigLoc)) |
| 1189 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
Douglas Gregor | 0cf55e9 | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 1190 | ConsumeAnyToken(); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1191 | } |
| 1192 | } |
| 1193 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1194 | /// \brief Wrapper around a case statement checking if AttrName is |
| 1195 | /// one of the thread safety attributes |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1196 | bool Parser::IsThreadSafetyAttribute(StringRef AttrName) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1197 | return llvm::StringSwitch<bool>(AttrName) |
| 1198 | .Case("guarded_by", true) |
| 1199 | .Case("guarded_var", true) |
| 1200 | .Case("pt_guarded_by", true) |
| 1201 | .Case("pt_guarded_var", true) |
| 1202 | .Case("lockable", true) |
| 1203 | .Case("scoped_lockable", true) |
| 1204 | .Case("no_thread_safety_analysis", true) |
| 1205 | .Case("acquired_after", true) |
| 1206 | .Case("acquired_before", true) |
| 1207 | .Case("exclusive_lock_function", true) |
| 1208 | .Case("shared_lock_function", true) |
| 1209 | .Case("exclusive_trylock_function", true) |
| 1210 | .Case("shared_trylock_function", true) |
| 1211 | .Case("unlock_function", true) |
| 1212 | .Case("lock_returned", true) |
| 1213 | .Case("locks_excluded", true) |
| 1214 | .Case("exclusive_locks_required", true) |
| 1215 | .Case("shared_locks_required", true) |
| 1216 | .Default(false); |
| 1217 | } |
| 1218 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1219 | void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName, |
| 1220 | SourceLocation AttrNameLoc, |
| 1221 | ParsedAttributes &Attrs, |
| 1222 | SourceLocation *EndLoc) { |
| 1223 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 1224 | |
| 1225 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1226 | T.consumeOpen(); |
| 1227 | |
| 1228 | if (Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1229 | Diag(Tok, diag::err_expected) << tok::identifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1230 | T.skipToEnd(); |
| 1231 | return; |
| 1232 | } |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 1233 | IdentifierLoc *ArgumentKind = ParseIdentifierLoc(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1234 | |
Alp Toker | 094e521 | 2014-01-05 03:27:11 +0000 | [diff] [blame] | 1235 | if (ExpectAndConsume(tok::comma)) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1236 | T.skipToEnd(); |
| 1237 | return; |
| 1238 | } |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1239 | |
| 1240 | SourceRange MatchingCTypeRange; |
| 1241 | TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange); |
| 1242 | if (MatchingCType.isInvalid()) { |
| 1243 | T.skipToEnd(); |
| 1244 | return; |
| 1245 | } |
| 1246 | |
| 1247 | bool LayoutCompatible = false; |
| 1248 | bool MustBeNull = false; |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1249 | while (TryConsumeToken(tok::comma)) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1250 | if (Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1251 | Diag(Tok, diag::err_expected) << tok::identifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1252 | T.skipToEnd(); |
| 1253 | return; |
| 1254 | } |
| 1255 | IdentifierInfo *Flag = Tok.getIdentifierInfo(); |
| 1256 | if (Flag->isStr("layout_compatible")) |
| 1257 | LayoutCompatible = true; |
| 1258 | else if (Flag->isStr("must_be_null")) |
| 1259 | MustBeNull = true; |
| 1260 | else { |
| 1261 | Diag(Tok, diag::err_type_safety_unknown_flag) << Flag; |
| 1262 | T.skipToEnd(); |
| 1263 | return; |
| 1264 | } |
| 1265 | ConsumeToken(); // consume flag |
| 1266 | } |
| 1267 | |
| 1268 | if (!T.consumeClose()) { |
| 1269 | Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1270 | ArgumentKind, MatchingCType.release(), |
| 1271 | LayoutCompatible, MustBeNull, |
| 1272 | AttributeList::AS_GNU); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | if (EndLoc) |
| 1276 | *EndLoc = T.getCloseLocation(); |
| 1277 | } |
| 1278 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1279 | /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets |
| 1280 | /// of a C++11 attribute-specifier in a location where an attribute is not |
| 1281 | /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this |
| 1282 | /// situation. |
| 1283 | /// |
| 1284 | /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if |
| 1285 | /// this doesn't appear to actually be an attribute-specifier, and the caller |
| 1286 | /// should try to parse it. |
| 1287 | bool Parser::DiagnoseProhibitedCXX11Attribute() { |
| 1288 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)); |
| 1289 | |
| 1290 | switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) { |
| 1291 | case CAK_NotAttributeSpecifier: |
| 1292 | // No diagnostic: we're in Obj-C++11 and this is not actually an attribute. |
| 1293 | return false; |
| 1294 | |
| 1295 | case CAK_InvalidAttributeSpecifier: |
| 1296 | Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute); |
| 1297 | return false; |
| 1298 | |
| 1299 | case CAK_AttributeSpecifier: |
| 1300 | // Parse and discard the attributes. |
| 1301 | SourceLocation BeginLoc = ConsumeBracket(); |
| 1302 | ConsumeBracket(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1303 | SkipUntil(tok::r_square); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1304 | assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied"); |
| 1305 | SourceLocation EndLoc = ConsumeBracket(); |
| 1306 | Diag(BeginLoc, diag::err_attributes_not_allowed) |
| 1307 | << SourceRange(BeginLoc, EndLoc); |
| 1308 | return true; |
| 1309 | } |
Chandler Carruth | d8f7d38 | 2012-04-10 16:03:08 +0000 | [diff] [blame] | 1310 | llvm_unreachable("All cases handled above."); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
Richard Smith | 98155ad | 2013-02-20 01:17:14 +0000 | [diff] [blame] | 1313 | /// \brief We have found the opening square brackets of a C++11 |
| 1314 | /// attribute-specifier in a location where an attribute is not permitted, but |
| 1315 | /// we know where the attributes ought to be written. Parse them anyway, and |
| 1316 | /// provide a fixit moving them to the right place. |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1317 | void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs, |
| 1318 | SourceLocation CorrectLocation) { |
| 1319 | assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) || |
| 1320 | Tok.is(tok::kw_alignas)); |
| 1321 | |
| 1322 | // Consume the attributes. |
| 1323 | SourceLocation Loc = Tok.getLocation(); |
| 1324 | ParseCXX11Attributes(Attrs); |
| 1325 | CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true); |
| 1326 | |
| 1327 | Diag(Loc, diag::err_attributes_not_allowed) |
| 1328 | << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange) |
| 1329 | << FixItHint::CreateRemoval(AttrRange); |
| 1330 | } |
| 1331 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1332 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 1333 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 1334 | << attrs.Range; |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1337 | void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) { |
| 1338 | AttributeList *AttrList = attrs.getList(); |
| 1339 | while (AttrList) { |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1340 | if (AttrList->isCXX11Attribute()) { |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 1341 | Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr) |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1342 | << AttrList->getName(); |
| 1343 | AttrList->setInvalid(); |
| 1344 | } |
| 1345 | AttrList = AttrList->getNext(); |
| 1346 | } |
| 1347 | } |
| 1348 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1349 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 1350 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1351 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 1352 | /// location of the semicolon in DeclEnd. |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1353 | /// |
| 1354 | /// declaration: [C99 6.7] |
| 1355 | /// block-declaration -> |
| 1356 | /// simple-declaration |
| 1357 | /// others [FIXME] |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1358 | /// [C++] template-declaration |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1359 | /// [C++] namespace-definition |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1360 | /// [C++] using-directive |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1361 | /// [C++] using-declaration |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1362 | /// [C++11/C11] static_assert-declaration |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1363 | /// others... [FIXME] |
| 1364 | /// |
Fariborz Jahanian | 1db5c94 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1365 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 1366 | unsigned Context, |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1367 | SourceLocation &DeclEnd, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1368 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 355094e | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 1369 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | 59b7528 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 1370 | // Must temporarily exit the objective-c container scope for |
| 1371 | // parsing c none objective-c decls. |
| 1372 | ObjCDeclContextSwitch ObjCDC(*this); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1373 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1374 | Decl *SingleDecl = 0; |
Richard Smith | cd1c055 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1375 | Decl *OwnedType = 0; |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1376 | switch (Tok.getKind()) { |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1377 | case tok::kw_template: |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1378 | case tok::kw_export: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1379 | ProhibitAttributes(attrs); |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1380 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1381 | break; |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1382 | case tok::kw_inline: |
Sebastian Redl | 5a5f2c7 | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 1383 | // 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] | 1384 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1385 | ProhibitAttributes(attrs); |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1386 | SourceLocation InlineLoc = ConsumeToken(); |
| 1387 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 1388 | break; |
| 1389 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1390 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, |
Fariborz Jahanian | 1db5c94 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1391 | true); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1392 | case tok::kw_namespace: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1393 | ProhibitAttributes(attrs); |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1394 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1395 | break; |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1396 | case tok::kw_using: |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 1397 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
Richard Smith | cd1c055 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1398 | DeclEnd, attrs, &OwnedType); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1399 | break; |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1400 | case tok::kw_static_assert: |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1401 | case tok::kw__Static_assert: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1402 | ProhibitAttributes(attrs); |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1403 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1404 | break; |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1405 | default: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1406 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1407 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1408 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1409 | // 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] | 1410 | // single decl, convert it now. Alias declarations can also declare a type; |
| 1411 | // include that too if it is present. |
| 1412 | return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 1416 | /// declaration-specifiers init-declarator-list[opt] ';' |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1417 | /// [C++11] attribute-specifier-seq decl-specifier-seq[opt] |
| 1418 | /// init-declarator-list ';' |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1419 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 1420 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1421 | /// |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1422 | /// for-range-declaration: [C++11 6.5p1: stmt.ranged] |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1423 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1424 | /// |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1425 | /// 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] | 1426 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1427 | /// |
| 1428 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 1429 | /// of a simple-declaration. If we find that we are, we also parse the |
| 1430 | /// for-range-initializer, and place it here. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1431 | Parser::DeclGroupPtrTy |
| 1432 | Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context, |
| 1433 | SourceLocation &DeclEnd, |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1434 | ParsedAttributesWithRange &Attrs, |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1435 | bool RequireSemi, ForRangeInit *FRI) { |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1436 | // Parse the common declaration-specifiers piece. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1437 | ParsingDeclSpec DS(*this); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1438 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 1439 | DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context); |
| 1440 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext); |
| 1441 | |
| 1442 | // If we had a free-standing type definition with a missing semicolon, we |
| 1443 | // may get this far before the problem becomes obvious. |
| 1444 | if (DS.hasTagDefinition() && |
| 1445 | DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext)) |
| 1446 | return DeclGroupPtrTy(); |
Abramo Bagnara | 1cd8368 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 1447 | |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 1448 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 1449 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1450 | if (Tok.is(tok::semi)) { |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1451 | ProhibitAttributes(Attrs); |
Argyrios Kyrtzidis | fbb2bb5 | 2012-05-16 23:49:15 +0000 | [diff] [blame] | 1452 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 005fc1b | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1453 | if (RequireSemi) ConsumeToken(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1454 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1455 | DS); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1456 | DS.complete(TheDecl); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1457 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 1458 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1459 | |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1460 | DS.takeAttributesFrom(Attrs); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1461 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1462 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1464 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1465 | /// for a declarator. |
| 1466 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 1467 | switch (Tok.getKind()) { |
| 1468 | case tok::annot_cxxscope: |
| 1469 | case tok::annot_template_id: |
| 1470 | case tok::caret: |
| 1471 | case tok::code_completion: |
| 1472 | case tok::coloncolon: |
| 1473 | case tok::ellipsis: |
| 1474 | case tok::kw___attribute: |
| 1475 | case tok::kw_operator: |
| 1476 | case tok::l_paren: |
| 1477 | case tok::star: |
| 1478 | return true; |
| 1479 | |
| 1480 | case tok::amp: |
| 1481 | case tok::ampamp: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1482 | return getLangOpts().CPlusPlus; |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1483 | |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1484 | 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] | 1485 | return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 && |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1486 | NextToken().is(tok::l_square); |
| 1487 | |
| 1488 | 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] | 1489 | return Context == Declarator::MemberContext || getLangOpts().CPlusPlus; |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1490 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1491 | case tok::identifier: |
| 1492 | switch (NextToken().getKind()) { |
| 1493 | case tok::code_completion: |
| 1494 | case tok::coloncolon: |
| 1495 | case tok::comma: |
| 1496 | case tok::equal: |
| 1497 | case tok::equalequal: // Might be a typo for '='. |
| 1498 | case tok::kw_alignas: |
| 1499 | case tok::kw_asm: |
| 1500 | case tok::kw___attribute: |
| 1501 | case tok::l_brace: |
| 1502 | case tok::l_paren: |
| 1503 | case tok::l_square: |
| 1504 | case tok::less: |
| 1505 | case tok::r_brace: |
| 1506 | case tok::r_paren: |
| 1507 | case tok::r_square: |
| 1508 | case tok::semi: |
| 1509 | return true; |
| 1510 | |
| 1511 | case tok::colon: |
| 1512 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1513 | // and in block scope it's probably a label. Inside a class definition, |
| 1514 | // this is a bit-field. |
| 1515 | return Context == Declarator::MemberContext || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1516 | (getLangOpts().CPlusPlus && Context == Declarator::FileContext); |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1517 | |
| 1518 | case tok::identifier: // Possible virt-specifier. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1519 | return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken()); |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1520 | |
| 1521 | default: |
| 1522 | return false; |
| 1523 | } |
| 1524 | |
| 1525 | default: |
| 1526 | return false; |
| 1527 | } |
| 1528 | } |
| 1529 | |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1530 | /// Skip until we reach something which seems like a sensible place to pick |
| 1531 | /// up parsing after a malformed declaration. This will sometimes stop sooner |
| 1532 | /// than SkipUntil(tok::r_brace) would, but will never stop later. |
| 1533 | void Parser::SkipMalformedDecl() { |
| 1534 | while (true) { |
| 1535 | switch (Tok.getKind()) { |
| 1536 | case tok::l_brace: |
| 1537 | // Skip until matching }, then stop. We've probably skipped over |
| 1538 | // a malformed class or function definition or similar. |
| 1539 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1540 | SkipUntil(tok::r_brace); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1541 | if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) { |
| 1542 | // This declaration isn't over yet. Keep skipping. |
| 1543 | continue; |
| 1544 | } |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1545 | TryConsumeToken(tok::semi); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1546 | return; |
| 1547 | |
| 1548 | case tok::l_square: |
| 1549 | ConsumeBracket(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1550 | SkipUntil(tok::r_square); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1551 | continue; |
| 1552 | |
| 1553 | case tok::l_paren: |
| 1554 | ConsumeParen(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1555 | SkipUntil(tok::r_paren); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1556 | continue; |
| 1557 | |
| 1558 | case tok::r_brace: |
| 1559 | return; |
| 1560 | |
| 1561 | case tok::semi: |
| 1562 | ConsumeToken(); |
| 1563 | return; |
| 1564 | |
| 1565 | case tok::kw_inline: |
| 1566 | // 'inline namespace' at the start of a line is almost certainly |
Jordan Rose | 12e730c | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1567 | // a good place to pick back up parsing, except in an Objective-C |
| 1568 | // @interface context. |
| 1569 | if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) && |
| 1570 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1571 | return; |
| 1572 | break; |
| 1573 | |
| 1574 | case tok::kw_namespace: |
| 1575 | // 'namespace' at the start of a line is almost certainly a good |
Jordan Rose | 12e730c | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1576 | // place to pick back up parsing, except in an Objective-C |
| 1577 | // @interface context. |
| 1578 | if (Tok.isAtStartOfLine() && |
| 1579 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
| 1580 | return; |
| 1581 | break; |
| 1582 | |
| 1583 | case tok::at: |
| 1584 | // @end is very much like } in Objective-C contexts. |
| 1585 | if (NextToken().isObjCAtKeyword(tok::objc_end) && |
| 1586 | ParsingInObjCContainer) |
| 1587 | return; |
| 1588 | break; |
| 1589 | |
| 1590 | case tok::minus: |
| 1591 | case tok::plus: |
| 1592 | // - and + probably start new method declarations in Objective-C contexts. |
| 1593 | if (Tok.isAtStartOfLine() && ParsingInObjCContainer) |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1594 | return; |
| 1595 | break; |
| 1596 | |
| 1597 | case tok::eof: |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 1598 | case tok::annot_module_begin: |
| 1599 | case tok::annot_module_end: |
| 1600 | case tok::annot_module_include: |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1601 | return; |
| 1602 | |
| 1603 | default: |
| 1604 | break; |
| 1605 | } |
| 1606 | |
| 1607 | ConsumeAnyToken(); |
| 1608 | } |
| 1609 | } |
| 1610 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1611 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1612 | /// definition or a group of object declarations, actually parse the |
| 1613 | /// result. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1614 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1615 | unsigned Context, |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1616 | bool AllowFunctionDefinitions, |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1617 | SourceLocation *DeclEnd, |
| 1618 | ForRangeInit *FRI) { |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1619 | // Parse the first declarator. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1620 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1621 | ParseDeclarator(D); |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1622 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1623 | // Bail out if the first declarator didn't seem well-formed. |
| 1624 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1625 | SkipMalformedDecl(); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1626 | return DeclGroupPtrTy(); |
Chris Lattner | efb0f11 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1627 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1628 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1629 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1630 | // appropriate function scope after the function Decl has been constructed. |
DeLesley Hutchins | 66e300e | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1631 | // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList. |
| 1632 | LateParsedAttrList LateParsedAttrs(true); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1633 | if (D.isFunctionDeclarator()) |
| 1634 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1635 | |
Chris Lattner | dbb1e93 | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1636 | // 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] | 1637 | if (D.isFunctionDeclarator() && |
Chris Lattner | dbb1e93 | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1638 | // Look at the next token to make sure that this isn't a function |
| 1639 | // declaration. We have to check this because __attribute__ might be the |
| 1640 | // start of a function definition in GCC-extended K&R C. |
Fariborz Jahanian | 712bb81 | 2012-08-10 15:54:40 +0000 | [diff] [blame] | 1641 | !isDeclarationAfterDeclarator()) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1642 | |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1643 | if (AllowFunctionDefinitions) { |
| 1644 | if (isStartOfFunctionDefinition(D)) { |
| 1645 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1646 | Diag(Tok, diag::err_function_declared_typedef); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1647 | |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1648 | // Recover by treating the 'typedef' as spurious. |
| 1649 | DS.ClearStorageClassSpecs(); |
| 1650 | } |
| 1651 | |
| 1652 | Decl *TheDecl = |
| 1653 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
| 1654 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1657 | if (isDeclarationSpecifier()) { |
| 1658 | // If there is an invalid declaration specifier right after the function |
| 1659 | // prototype, then we must be in a missing semicolon case where this isn't |
| 1660 | // actually a body. Just fall through into the code that handles it as a |
| 1661 | // prototype, and let the top-level code handle the erroneous declspec |
| 1662 | // where it would otherwise expect a comma or semicolon. |
| 1663 | } else { |
| 1664 | Diag(Tok, diag::err_expected_fn_body); |
| 1665 | SkipUntil(tok::semi); |
| 1666 | return DeclGroupPtrTy(); |
| 1667 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1668 | } else { |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1669 | if (Tok.is(tok::l_brace)) { |
| 1670 | Diag(Tok, diag::err_function_definition_not_allowed); |
Serge Pavlov | 1de5151 | 2013-12-09 05:25:47 +0000 | [diff] [blame] | 1671 | SkipMalformedDecl(); |
| 1672 | return DeclGroupPtrTy(); |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1673 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1674 | } |
| 1675 | } |
| 1676 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1677 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1678 | return DeclGroupPtrTy(); |
| 1679 | |
| 1680 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1681 | // must parse and analyze the for-range-initializer before the declaration is |
| 1682 | // analyzed. |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1683 | // |
| 1684 | // Handle the Objective-C for-in loop variable similarly, although we |
| 1685 | // don't need to parse the container in advance. |
| 1686 | if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) { |
| 1687 | bool IsForRangeLoop = false; |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1688 | if (TryConsumeToken(tok::colon, FRI->ColonLoc)) { |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1689 | IsForRangeLoop = true; |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1690 | if (Tok.is(tok::l_brace)) |
| 1691 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1692 | else |
| 1693 | FRI->RangeExpr = ParseExpression(); |
| 1694 | } |
| 1695 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1696 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1697 | if (IsForRangeLoop) |
| 1698 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1699 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | cf6e0c8 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 1700 | D.complete(ThisDecl); |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1701 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1704 | SmallVector<Decl *, 8> DeclsInGroup; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1705 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1706 | if (LateParsedAttrs.size() > 0) |
| 1707 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1708 | D.complete(FirstDecl); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1709 | if (FirstDecl) |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1710 | DeclsInGroup.push_back(FirstDecl); |
| 1711 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1712 | bool ExpectSemi = Context != Declarator::ForContext; |
Fariborz Jahanian | 577574a | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 1713 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1714 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 1715 | // error, bail out. |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1716 | SourceLocation CommaLoc; |
| 1717 | while (TryConsumeToken(tok::comma, CommaLoc)) { |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1718 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 1719 | // This comma was followed by a line-break and something which can't be |
| 1720 | // the start of a declarator. The comma was probably a typo for a |
| 1721 | // semicolon. |
| 1722 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 1723 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 1724 | ExpectSemi = false; |
| 1725 | break; |
| 1726 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1727 | |
| 1728 | // Parse the next declarator. |
| 1729 | D.clear(); |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 1730 | D.setCommaLoc(CommaLoc); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1731 | |
| 1732 | // Accept attributes in an init-declarator. In the first declarator in a |
| 1733 | // declaration, these would be part of the declspec. In subsequent |
| 1734 | // declarators, they become part of the declarator itself, so that they |
| 1735 | // don't apply to declarators after *this* one. Examples: |
| 1736 | // short __attribute__((common)) var; -> declspec |
| 1737 | // short var __attribute__((common)); -> declarator |
| 1738 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1739 | MaybeParseGNUAttributes(D); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1740 | |
| 1741 | ParseDeclarator(D); |
Fariborz Jahanian | 372030b | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1742 | if (!D.isInvalidType()) { |
| 1743 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 1744 | D.complete(ThisDecl); |
| 1745 | if (ThisDecl) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1746 | DeclsInGroup.push_back(ThisDecl); |
Fariborz Jahanian | 372030b | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1747 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
| 1750 | if (DeclEnd) |
| 1751 | *DeclEnd = Tok.getLocation(); |
| 1752 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1753 | if (ExpectSemi && |
Chris Lattner | 02f1b61 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 1754 | ExpectAndConsumeSemi(Context == Declarator::FileContext |
| 1755 | ? diag::err_invalid_token_after_toplevel_declarator |
| 1756 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 1390134 | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1757 | // Okay, there was no semicolon and one was expected. If we see a |
| 1758 | // declaration specifier, just assume it was missing and continue parsing. |
| 1759 | // Otherwise things are very confused and we skip to recover. |
| 1760 | if (!isDeclarationSpecifier()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1761 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 1762 | TryConsumeToken(tok::semi); |
Chris Lattner | 1390134 | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1763 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1766 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup); |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1769 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1770 | /// declarator. Returns true on an error. |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1771 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1772 | // If a simple-asm-expr is present, parse it. |
| 1773 | if (Tok.is(tok::kw_asm)) { |
| 1774 | SourceLocation Loc; |
| 1775 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1776 | if (AsmLabel.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1777 | SkipUntil(tok::semi, StopBeforeMatch); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1778 | return true; |
| 1779 | } |
| 1780 | |
| 1781 | D.setAsmLabel(AsmLabel.release()); |
| 1782 | D.SetRangeEnd(Loc); |
| 1783 | } |
| 1784 | |
| 1785 | MaybeParseGNUAttributes(D); |
| 1786 | return false; |
| 1787 | } |
| 1788 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1789 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1790 | /// declarator'. This method parses the remainder of the declaration |
| 1791 | /// (including any attributes or initializer, among other things) and |
| 1792 | /// finalizes the declaration. |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 1793 | /// |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 1794 | /// init-declarator: [C99 6.7] |
| 1795 | /// declarator |
| 1796 | /// declarator '=' initializer |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 1797 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1798 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1799 | /// [C++] declarator initializer[opt] |
| 1800 | /// |
| 1801 | /// [C++] initializer: |
| 1802 | /// [C++] '=' initializer-clause |
| 1803 | /// [C++] '(' expression-list ')' |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1804 | /// [C++0x] '=' 'default' [TODO] |
| 1805 | /// [C++0x] '=' 'delete' |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1806 | /// [C++0x] braced-init-list |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1807 | /// |
| 1808 | /// According to the standard grammar, =default and =delete are function |
| 1809 | /// definitions, but that definitely doesn't fit with the parser here. |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 1810 | /// |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1811 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1812 | const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1813 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1814 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1815 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1816 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1817 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1819 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 1820 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1821 | // Inform the current actions module that we just parsed this declarator. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1822 | Decl *ThisDecl = 0; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1823 | switch (TemplateInfo.Kind) { |
| 1824 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1825 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1826 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1827 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1828 | case ParsedTemplateInfo::Template: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1829 | case ParsedTemplateInfo::ExplicitSpecialization: { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1830 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1831 | *TemplateInfo.TemplateParams, |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1832 | D); |
Larisse Voufo | 833b05a | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 1833 | if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1834 | // Re-direct this decl to refer to the templated decl so that we can |
| 1835 | // initialize it. |
| 1836 | ThisDecl = VT->getTemplatedDecl(); |
| 1837 | break; |
| 1838 | } |
| 1839 | case ParsedTemplateInfo::ExplicitInstantiation: { |
| 1840 | if (Tok.is(tok::semi)) { |
| 1841 | DeclResult ThisRes = Actions.ActOnExplicitInstantiation( |
| 1842 | getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D); |
| 1843 | if (ThisRes.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1844 | SkipUntil(tok::semi, StopBeforeMatch); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1845 | return 0; |
| 1846 | } |
| 1847 | ThisDecl = ThisRes.get(); |
| 1848 | } else { |
| 1849 | // FIXME: This check should be for a variable template instantiation only. |
| 1850 | |
| 1851 | // Check that this is a valid instantiation |
| 1852 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 1853 | // If the declarator-id is not a template-id, issue a diagnostic and |
| 1854 | // recover by ignoring the 'template' keyword. |
| 1855 | Diag(Tok, diag::err_template_defn_explicit_instantiation) |
| 1856 | << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc); |
| 1857 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 1858 | } else { |
| 1859 | SourceLocation LAngleLoc = |
| 1860 | PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
| 1861 | Diag(D.getIdentifierLoc(), |
| 1862 | diag::err_explicit_instantiation_with_definition) |
| 1863 | << SourceRange(TemplateInfo.TemplateLoc) |
| 1864 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
| 1865 | |
| 1866 | // Recover as if it were an explicit specialization. |
| 1867 | TemplateParameterLists FakedParamLists; |
| 1868 | FakedParamLists.push_back(Actions.ActOnTemplateParameterList( |
| 1869 | 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0, |
| 1870 | LAngleLoc)); |
| 1871 | |
| 1872 | ThisDecl = |
| 1873 | Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D); |
| 1874 | } |
| 1875 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1876 | break; |
| 1877 | } |
| 1878 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1879 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 1880 | bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType(); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1881 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1882 | // Parse declarator '=' initializer. |
Richard Trieu | c64d323 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1883 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | 4972a6d | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 1884 | if (isTokenEqualOrEqualTypo()) { |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1885 | ConsumeToken(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1886 | |
Anders Carlsson | 991285e | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1887 | if (Tok.is(tok::kw_delete)) { |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1888 | if (D.isFunctionDeclarator()) |
| 1889 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1890 | << 1 /* delete */; |
| 1891 | else |
| 1892 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Alexis Hunt | 5dafebc | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1893 | } else if (Tok.is(tok::kw_default)) { |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1894 | if (D.isFunctionDeclarator()) |
Sebastian Redl | 46afb55 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 1895 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1896 | << 0 /* default */; |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1897 | else |
| 1898 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1899 | } else { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1900 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1901 | EnterScope(0); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1902 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1903 | } |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1904 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1905 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1906 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Peter Collingbourne | 6b4fdc2 | 2012-07-27 12:56:09 +0000 | [diff] [blame] | 1907 | Actions.FinalizeDeclaration(ThisDecl); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1908 | cutOffParsing(); |
| 1909 | return 0; |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1910 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1911 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1912 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1913 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1914 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1915 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1916 | ExitScope(); |
| 1917 | } |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1918 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1919 | if (Init.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1920 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 604c302 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1921 | Actions.ActOnInitializerError(ThisDecl); |
| 1922 | } else |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1923 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1924 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1925 | } |
| 1926 | } else if (Tok.is(tok::l_paren)) { |
| 1927 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1928 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1929 | T.consumeOpen(); |
| 1930 | |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1931 | ExprVector Exprs; |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1932 | CommaLocsTy CommaLocs; |
| 1933 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1934 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1935 | EnterScope(0); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1936 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1937 | } |
| 1938 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1939 | if (ParseExpressionList(Exprs, CommaLocs)) { |
David Blaikie | eae0411 | 2012-10-10 23:15:05 +0000 | [diff] [blame] | 1940 | Actions.ActOnInitializerError(ThisDecl); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1941 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1942 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1943 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1944 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1945 | ExitScope(); |
| 1946 | } |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1947 | } else { |
| 1948 | // Match the ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1949 | T.consumeClose(); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1950 | |
| 1951 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1952 | "Unexpected number of commas!"); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1953 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1954 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1955 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1956 | ExitScope(); |
| 1957 | } |
| 1958 | |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1959 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 1960 | T.getCloseLocation(), |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1961 | Exprs); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1962 | Actions.AddInitializerToDecl(ThisDecl, Initializer.take(), |
| 1963 | /*DirectInit=*/true, TypeContainsAuto); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1964 | } |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1965 | } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) && |
Fariborz Jahanian | 8be1ecd | 2012-07-03 23:22:13 +0000 | [diff] [blame] | 1966 | (!CurParsedObjCImpl || !D.isFunctionDeclarator())) { |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1967 | // Parse C++0x braced-init-list. |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1968 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 1969 | |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1970 | if (D.getCXXScopeSpec().isSet()) { |
| 1971 | EnterScope(0); |
| 1972 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 1973 | } |
| 1974 | |
| 1975 | ExprResult Init(ParseBraceInitializer()); |
| 1976 | |
| 1977 | if (D.getCXXScopeSpec().isSet()) { |
| 1978 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 1979 | ExitScope(); |
| 1980 | } |
| 1981 | |
| 1982 | if (Init.isInvalid()) { |
| 1983 | Actions.ActOnInitializerError(ThisDecl); |
| 1984 | } else |
| 1985 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1986 | /*DirectInit=*/true, TypeContainsAuto); |
| 1987 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1988 | } else { |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1989 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1992 | Actions.FinalizeDeclaration(ThisDecl); |
| 1993 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1994 | return ThisDecl; |
| 1995 | } |
| 1996 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 1997 | /// ParseSpecifierQualifierList |
| 1998 | /// specifier-qualifier-list: |
| 1999 | /// type-specifier specifier-qualifier-list[opt] |
| 2000 | /// type-qualifier specifier-qualifier-list[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 2001 | /// [GNU] attributes specifier-qualifier-list[opt] |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2002 | /// |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2003 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 2004 | DeclSpecContext DSC) { |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2005 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 2006 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2007 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2008 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2010 | // Validate declspec for type-name. |
| 2011 | unsigned Specs = DS.getParsedSpecifiers(); |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 2012 | if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) { |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2013 | Diag(Tok, diag::err_expected_type); |
| 2014 | DS.SetTypeSpecError(); |
| 2015 | } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
| 2016 | !DS.hasAttributes()) { |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2017 | Diag(Tok, diag::err_typename_requires_specqual); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2018 | if (!DS.hasTypeSpecifier()) |
| 2019 | DS.SetTypeSpecError(); |
| 2020 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2021 | |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 2022 | // Issue diagnostic and remove storage class if present. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2023 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 2024 | if (DS.getStorageClassSpecLoc().isValid()) |
| 2025 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 2026 | else |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2027 | Diag(DS.getThreadStorageClassSpecLoc(), |
| 2028 | diag::err_typename_invalid_storageclass); |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 2029 | DS.ClearStorageClassSpecs(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2030 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2031 | |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 2032 | // Issue diagnostic and remove function specfier if present. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2033 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2034 | if (DS.isInlineSpecified()) |
| 2035 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 2036 | if (DS.isVirtualSpecified()) |
| 2037 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 2038 | if (DS.isExplicitSpecified()) |
| 2039 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 2040 | DS.ClearFunctionSpecs(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2041 | } |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2042 | |
| 2043 | // Issue diagnostic and remove constexpr specfier if present. |
| 2044 | if (DS.isConstexprSpecified()) { |
| 2045 | Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr); |
| 2046 | DS.ClearConstexprSpec(); |
| 2047 | } |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2048 | } |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 2049 | |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2050 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 2051 | /// specified token is valid after the identifier in a declarator which |
| 2052 | /// immediately follows the declspec. For example, these things are valid: |
| 2053 | /// |
| 2054 | /// int x [ 4]; // direct-declarator |
| 2055 | /// int x ( int y); // direct-declarator |
| 2056 | /// int(int x ) // direct-declarator |
| 2057 | /// int x ; // simple-declaration |
| 2058 | /// int x = 17; // init-declarator-list |
| 2059 | /// int x , y; // init-declarator-list |
| 2060 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | a723ba9 | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 2061 | /// int x : 4; // struct-declarator |
Chris Lattner | 2b988c1 | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 2062 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2063 | /// |
| 2064 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 2065 | /// ')' happens to be valid anyway). |
| 2066 | /// int (x) |
| 2067 | /// |
| 2068 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 2069 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 2070 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | a723ba9 | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 2071 | 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] | 2072 | } |
| 2073 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2074 | |
| 2075 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 2076 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 2077 | /// the declspec has no type specifier. In this case, the declspec is either |
| 2078 | /// malformed or is "implicit int" (in K&R and C89). |
| 2079 | /// |
| 2080 | /// This method handles diagnosing this prettily and returns false if the |
| 2081 | /// declspec is done being processed. If it recovers and thinks there may be |
| 2082 | /// other pieces of declspec after it, it returns true. |
| 2083 | /// |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2084 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2085 | const ParsedTemplateInfo &TemplateInfo, |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2086 | AccessSpecifier AS, DeclSpecContext DSC, |
| 2087 | ParsedAttributesWithRange &Attrs) { |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2088 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2089 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2090 | SourceLocation Loc = Tok.getLocation(); |
| 2091 | // If we see an identifier that is not a type name, we normally would |
| 2092 | // parse it as the identifer being declared. However, when a typename |
| 2093 | // is typo'd or the definition is not included, this will incorrectly |
| 2094 | // parse the typename as the identifier name and fall over misparsing |
| 2095 | // later parts of the diagnostic. |
| 2096 | // |
| 2097 | // As such, we try to do some look-ahead in cases where this would |
| 2098 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 2099 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 2100 | // an identifier with implicit int, we'd get a parse error because the |
| 2101 | // next token is obviously invalid for a type. Parse these as a case |
| 2102 | // with an invalid type specifier. |
| 2103 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2104 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2105 | // 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] | 2106 | // error, do lookahead to try to do better recovery. This never applies |
| 2107 | // within a type specifier. Outside of C++, we allow this even if the |
| 2108 | // language doesn't "officially" support implicit int -- we support |
Richard Smith | 3b87038 | 2013-04-30 22:43:51 +0000 | [diff] [blame] | 2109 | // implicit int as an extension in C99 and C11. |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 2110 | if (!isTypeSpecifier(DSC) && !getLangOpts().CPlusPlus && |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2111 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2112 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 2113 | // we just avoid eating the identifier, so it will be parsed as the |
| 2114 | // identifier in the declarator. |
| 2115 | return false; |
| 2116 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2117 | |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2118 | if (getLangOpts().CPlusPlus && |
| 2119 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| 2120 | // Don't require a type specifier if we have the 'auto' storage class |
| 2121 | // specifier in C++98 -- we'll promote it to a type specifier. |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2122 | if (SS) |
| 2123 | AnnotateScopeToken(*SS, /*IsNewAnnotation*/false); |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2124 | return false; |
| 2125 | } |
| 2126 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2127 | // Otherwise, if we don't consume this token, we are going to emit an |
| 2128 | // error anyway. Try to recover from various common problems. Check |
| 2129 | // to see if this was a reference to a tag name without a tag specified. |
| 2130 | // 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] | 2131 | // |
| 2132 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 2133 | if (SS == 0) { |
Argyrios Kyrtzidis | 1f32940 | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2134 | const char *TagName = 0, *FixitTagName = 0; |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2135 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2136 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2137 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2138 | default: break; |
Argyrios Kyrtzidis | 1f32940 | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2139 | case DeclSpec::TST_enum: |
| 2140 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 2141 | case DeclSpec::TST_union: |
| 2142 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 2143 | case DeclSpec::TST_struct: |
| 2144 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2145 | case DeclSpec::TST_interface: |
| 2146 | TagName="__interface"; FixitTagName = "__interface "; |
| 2147 | TagKind=tok::kw___interface;break; |
Argyrios Kyrtzidis | 1f32940 | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2148 | case DeclSpec::TST_class: |
| 2149 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2150 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2151 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2152 | if (TagName) { |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2153 | IdentifierInfo *TokenName = Tok.getIdentifierInfo(); |
| 2154 | LookupResult R(Actions, TokenName, SourceLocation(), |
| 2155 | Sema::LookupOrdinaryName); |
| 2156 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2157 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2158 | << TokenName << TagName << getLangOpts().CPlusPlus |
| 2159 | << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName); |
| 2160 | |
| 2161 | if (Actions.LookupParsedName(R, getCurScope(), SS)) { |
| 2162 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); |
| 2163 | I != IEnd; ++I) |
Kaelyn Uhrain | 3fe3f85 | 2012-04-27 18:26:49 +0000 | [diff] [blame] | 2164 | Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2165 | << TokenName << TagName; |
| 2166 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2167 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2168 | // Parse this as a tag as if the missing tag were present. |
| 2169 | if (TagKind == tok::kw_enum) |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2170 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal); |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2171 | else |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2172 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2173 | /*EnteringContext*/ false, DSC_normal, Attrs); |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2174 | return true; |
| 2175 | } |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2176 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2177 | |
Richard Smith | fe904f0 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2178 | // Determine whether this identifier could plausibly be the name of something |
Richard Smith | edd124e | 2012-05-15 21:42:17 +0000 | [diff] [blame] | 2179 | // being declared (with a missing type). |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 2180 | if (!isTypeSpecifier(DSC) && |
Richard Smith | fe904f0 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2181 | (!SS || DSC == DSC_top_level || DSC == DSC_class)) { |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2182 | // Look ahead to the next token to try to figure out what this declaration |
| 2183 | // was supposed to be. |
| 2184 | switch (NextToken().getKind()) { |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2185 | case tok::l_paren: { |
| 2186 | // static x(4); // 'x' is not a type |
| 2187 | // x(int n); // 'x' is not a type |
| 2188 | // x (*p)[]; // 'x' is a type |
| 2189 | // |
| 2190 | // Since we're in an error case (or the rare 'implicit int in C++' MS |
| 2191 | // extension), we can afford to perform a tentative parse to determine |
| 2192 | // which case we're in. |
| 2193 | TentativeParsingAction PA(*this); |
| 2194 | ConsumeToken(); |
| 2195 | TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false); |
| 2196 | PA.Revert(); |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2197 | |
| 2198 | if (TPR != TPResult::False()) { |
| 2199 | // The identifier is followed by a parenthesized declarator. |
| 2200 | // It's supposed to be a type. |
| 2201 | break; |
| 2202 | } |
| 2203 | |
| 2204 | // If we're in a context where we could be declaring a constructor, |
| 2205 | // check whether this is a constructor declaration with a bogus name. |
| 2206 | if (DSC == DSC_class || (DSC == DSC_top_level && SS)) { |
| 2207 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2208 | if (Actions.isCurrentClassNameTypo(II, SS)) { |
| 2209 | Diag(Loc, diag::err_constructor_bad_name) |
| 2210 | << Tok.getIdentifierInfo() << II |
| 2211 | << FixItHint::CreateReplacement(Tok.getLocation(), II->getName()); |
| 2212 | Tok.setIdentifierInfo(II); |
| 2213 | } |
| 2214 | } |
| 2215 | // Fall through. |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2216 | } |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2217 | case tok::comma: |
| 2218 | case tok::equal: |
| 2219 | case tok::kw_asm: |
| 2220 | case tok::l_brace: |
| 2221 | case tok::l_square: |
| 2222 | case tok::semi: |
| 2223 | // This looks like a variable or function declaration. The type is |
| 2224 | // probably missing. We're done parsing decl-specifiers. |
| 2225 | if (SS) |
| 2226 | AnnotateScopeToken(*SS, /*IsNewAnnotation*/false); |
| 2227 | return false; |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2228 | |
| 2229 | default: |
| 2230 | // This is probably supposed to be a type. This includes cases like: |
| 2231 | // int f(itn); |
| 2232 | // struct S { unsinged : 4; }; |
| 2233 | break; |
| 2234 | } |
| 2235 | } |
| 2236 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2237 | // 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] | 2238 | // diagnostic and attempt to recover. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2239 | ParsedType T; |
Kaelyn Uhrain | b5b17fe | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2240 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2241 | if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) { |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2242 | // The action emitted a diagnostic, so we don't have to. |
| 2243 | if (T) { |
| 2244 | // The action has suggested that the type T could be used. Set that as |
| 2245 | // the type in the declaration specifiers, consume the would-be type |
| 2246 | // name token, and we're done. |
| 2247 | const char *PrevSpec; |
| 2248 | unsigned DiagID; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2249 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2250 | DS.SetRangeEnd(Tok.getLocation()); |
| 2251 | ConsumeToken(); |
Kaelyn Uhrain | b5b17fe | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2252 | // There may be other declaration specifiers after this. |
| 2253 | return true; |
| 2254 | } else if (II != Tok.getIdentifierInfo()) { |
| 2255 | // If no type was suggested, the correction is to a keyword |
| 2256 | Tok.setKind(II->getTokenID()); |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2257 | // There may be other declaration specifiers after this. |
| 2258 | return true; |
| 2259 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2260 | |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2261 | // Fall through; the action had no suggestion for us. |
| 2262 | } else { |
| 2263 | // The action did not emit a diagnostic, so emit one now. |
| 2264 | SourceRange R; |
| 2265 | if (SS) R = SS->getRange(); |
| 2266 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 2267 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2268 | |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2269 | // Mark this as an error. |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2270 | DS.SetTypeSpecError(); |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2271 | DS.SetRangeEnd(Tok.getLocation()); |
| 2272 | ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2273 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2274 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 2275 | // avoid rippling error messages on subsequent uses of the same type, |
| 2276 | // could be useful if #include was forgotten. |
| 2277 | return false; |
| 2278 | } |
| 2279 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2280 | /// \brief Determine the declaration specifier context from the declarator |
| 2281 | /// context. |
| 2282 | /// |
| 2283 | /// \param Context the declarator context, which is one of the |
| 2284 | /// Declarator::TheContext enumerator values. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2285 | Parser::DeclSpecContext |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2286 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 2287 | if (Context == Declarator::MemberContext) |
| 2288 | return DSC_class; |
| 2289 | if (Context == Declarator::FileContext) |
| 2290 | return DSC_top_level; |
Richard Smith | 62dad82 | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 2291 | if (Context == Declarator::TrailingReturnContext) |
| 2292 | return DSC_trailing; |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 2293 | if (Context == Declarator::AliasDeclContext || |
| 2294 | Context == Declarator::AliasTemplateContext) |
| 2295 | return DSC_alias_declaration; |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2296 | return DSC_normal; |
| 2297 | } |
| 2298 | |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2299 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 2300 | /// |
| 2301 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 2302 | /// type. Ideally, the type should be propagated directly into Sema. |
| 2303 | /// |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2304 | /// [C11] type-id |
| 2305 | /// [C11] constant-expression |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2306 | /// [C++0x] type-id ...[opt] |
| 2307 | /// [C++0x] assignment-expression ...[opt] |
| 2308 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 2309 | SourceLocation &EllipsisLoc) { |
| 2310 | ExprResult ER; |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2311 | if (isTypeIdInParens()) { |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2312 | SourceLocation TypeLoc = Tok.getLocation(); |
| 2313 | ParsedType Ty = ParseTypeName().get(); |
| 2314 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2315 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 2316 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2317 | } else |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2318 | ER = ParseConstantExpression(); |
| 2319 | |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 2320 | if (getLangOpts().CPlusPlus11) |
| 2321 | TryConsumeToken(tok::ellipsis, EllipsisLoc); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2322 | |
| 2323 | return ER; |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2324 | } |
| 2325 | |
| 2326 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 2327 | /// attribute to Attrs. |
| 2328 | /// |
| 2329 | /// alignment-specifier: |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2330 | /// [C11] '_Alignas' '(' type-id ')' |
| 2331 | /// [C11] '_Alignas' '(' constant-expression ')' |
Richard Smith | d11c7a1 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2332 | /// [C++11] 'alignas' '(' type-id ...[opt] ')' |
| 2333 | /// [C++11] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2334 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2335 | SourceLocation *EndLoc) { |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2336 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 2337 | "Not an alignment-specifier!"); |
| 2338 | |
Richard Smith | d11c7a1 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2339 | IdentifierInfo *KWName = Tok.getIdentifierInfo(); |
| 2340 | SourceLocation KWLoc = ConsumeToken(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2341 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2342 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 2343 | if (T.expectAndConsume()) |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2344 | return; |
| 2345 | |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2346 | SourceLocation EllipsisLoc; |
| 2347 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2348 | if (ArgExpr.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2349 | T.skipToEnd(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2350 | return; |
| 2351 | } |
| 2352 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2353 | T.consumeClose(); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2354 | if (EndLoc) |
| 2355 | *EndLoc = T.getCloseLocation(); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2356 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2357 | ArgsVector ArgExprs; |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2358 | ArgExprs.push_back(ArgExpr.release()); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2359 | Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1, |
| 2360 | AttributeList::AS_Keyword, EllipsisLoc); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2363 | /// Determine whether we're looking at something that might be a declarator |
| 2364 | /// in a simple-declaration. If it can't possibly be a declarator, maybe |
| 2365 | /// diagnose a missing semicolon after a prior tag definition in the decl |
| 2366 | /// specifier. |
| 2367 | /// |
| 2368 | /// \return \c true if an error occurred and this can't be any kind of |
| 2369 | /// declaration. |
| 2370 | bool |
| 2371 | Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS, |
| 2372 | DeclSpecContext DSContext, |
| 2373 | LateParsedAttrList *LateAttrs) { |
| 2374 | assert(DS.hasTagDefinition() && "shouldn't call this"); |
| 2375 | |
| 2376 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2377 | |
| 2378 | if (getLangOpts().CPlusPlus && |
| 2379 | (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) || |
| 2380 | Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) && |
| 2381 | TryAnnotateCXXScopeToken(EnteringContext)) { |
| 2382 | SkipMalformedDecl(); |
| 2383 | return true; |
| 2384 | } |
| 2385 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2386 | bool HasScope = Tok.is(tok::annot_cxxscope); |
| 2387 | // Make a copy in case GetLookAheadToken invalidates the result of NextToken. |
| 2388 | Token AfterScope = HasScope ? NextToken() : Tok; |
| 2389 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2390 | // Determine whether the following tokens could possibly be a |
| 2391 | // declarator. |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2392 | bool MightBeDeclarator = true; |
| 2393 | if (Tok.is(tok::kw_typename) || Tok.is(tok::annot_typename)) { |
| 2394 | // A declarator-id can't start with 'typename'. |
| 2395 | MightBeDeclarator = false; |
| 2396 | } else if (AfterScope.is(tok::annot_template_id)) { |
| 2397 | // If we have a type expressed as a template-id, this cannot be a |
| 2398 | // declarator-id (such a type cannot be redeclared in a simple-declaration). |
| 2399 | TemplateIdAnnotation *Annot = |
| 2400 | static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue()); |
| 2401 | if (Annot->Kind == TNK_Type_template) |
| 2402 | MightBeDeclarator = false; |
| 2403 | } else if (AfterScope.is(tok::identifier)) { |
| 2404 | const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken(); |
| 2405 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2406 | // These tokens cannot come after the declarator-id in a |
| 2407 | // simple-declaration, and are likely to come after a type-specifier. |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2408 | if (Next.is(tok::star) || Next.is(tok::amp) || Next.is(tok::ampamp) || |
| 2409 | Next.is(tok::identifier) || Next.is(tok::annot_cxxscope) || |
| 2410 | Next.is(tok::coloncolon)) { |
| 2411 | // Missing a semicolon. |
| 2412 | MightBeDeclarator = false; |
| 2413 | } else if (HasScope) { |
| 2414 | // If the declarator-id has a scope specifier, it must redeclare a |
| 2415 | // previously-declared entity. If that's a type (and this is not a |
| 2416 | // typedef), that's an error. |
| 2417 | CXXScopeSpec SS; |
| 2418 | Actions.RestoreNestedNameSpecifierAnnotation( |
| 2419 | Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS); |
| 2420 | IdentifierInfo *Name = AfterScope.getIdentifierInfo(); |
| 2421 | Sema::NameClassification Classification = Actions.ClassifyName( |
| 2422 | getCurScope(), SS, Name, AfterScope.getLocation(), Next, |
| 2423 | /*IsAddressOfOperand*/false); |
| 2424 | switch (Classification.getKind()) { |
| 2425 | case Sema::NC_Error: |
| 2426 | SkipMalformedDecl(); |
| 2427 | return true; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2428 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2429 | case Sema::NC_Keyword: |
| 2430 | case Sema::NC_NestedNameSpecifier: |
| 2431 | llvm_unreachable("typo correction and nested name specifiers not " |
| 2432 | "possible here"); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2433 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2434 | case Sema::NC_Type: |
| 2435 | case Sema::NC_TypeTemplate: |
| 2436 | // Not a previously-declared non-type entity. |
| 2437 | MightBeDeclarator = false; |
| 2438 | break; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2439 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2440 | case Sema::NC_Unknown: |
| 2441 | case Sema::NC_Expression: |
| 2442 | case Sema::NC_VarTemplate: |
| 2443 | case Sema::NC_FunctionTemplate: |
| 2444 | // Might be a redeclaration of a prior entity. |
| 2445 | break; |
| 2446 | } |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2447 | } |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2448 | } |
| 2449 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2450 | if (MightBeDeclarator) |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2451 | return false; |
| 2452 | |
| 2453 | Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()), |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 2454 | diag::err_expected_after) |
| 2455 | << DeclSpec::getSpecifierName(DS.getTypeSpecType()) << tok::semi; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2456 | |
| 2457 | // Try to recover from the typo, by dropping the tag definition and parsing |
| 2458 | // the problematic tokens as a type. |
| 2459 | // |
| 2460 | // FIXME: Split the DeclSpec into pieces for the standalone |
| 2461 | // declaration and pieces for the following declaration, instead |
| 2462 | // of assuming that all the other pieces attach to new declaration, |
| 2463 | // and call ParsedFreeStandingDeclSpec as appropriate. |
| 2464 | DS.ClearTypeSpecType(); |
| 2465 | ParsedTemplateInfo NotATemplate; |
| 2466 | ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs); |
| 2467 | return false; |
| 2468 | } |
| 2469 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2470 | /// ParseDeclarationSpecifiers |
| 2471 | /// declaration-specifiers: [C99 6.7] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2472 | /// storage-class-specifier declaration-specifiers[opt] |
| 2473 | /// type-specifier declaration-specifiers[opt] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2474 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2475 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 2476 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2477 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2478 | /// |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2479 | /// storage-class-specifier: [C99 6.7.1] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 2480 | /// 'typedef' |
| 2481 | /// 'extern' |
| 2482 | /// 'static' |
| 2483 | /// 'auto' |
| 2484 | /// 'register' |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2485 | /// [C++] 'mutable' |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2486 | /// [C++11] 'thread_local' |
| 2487 | /// [C11] '_Thread_local' |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 2488 | /// [GNU] '__thread' |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2489 | /// function-specifier: [C99 6.7.4] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2490 | /// [C99] 'inline' |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2491 | /// [C++] 'virtual' |
| 2492 | /// [C++] 'explicit' |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2493 | /// [OpenCL] '__kernel' |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2494 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 39c2a8b | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2495 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2496 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2497 | /// |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 2498 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2499 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2500 | AccessSpecifier AS, |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2501 | DeclSpecContext DSContext, |
| 2502 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2503 | if (DS.getSourceRange().isInvalid()) { |
| 2504 | DS.SetRangeStart(Tok.getLocation()); |
| 2505 | DS.SetRangeEnd(Tok.getLocation()); |
| 2506 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2507 | |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2508 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2509 | bool AttrsLastTime = false; |
| 2510 | ParsedAttributesWithRange attrs(AttrFactory); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2511 | while (1) { |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2512 | bool isInvalid = false; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2513 | const char *PrevSpec = 0; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2514 | unsigned DiagID = 0; |
| 2515 | |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 2516 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2517 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2518 | switch (Tok.getKind()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2519 | default: |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2520 | DoneWithDeclSpec: |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2521 | if (!AttrsLastTime) |
| 2522 | ProhibitAttributes(attrs); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2523 | else { |
| 2524 | // Reject C++11 attributes that appertain to decl specifiers as |
| 2525 | // we don't support any C++11 attributes that appertain to decl |
| 2526 | // specifiers. This also conforms to what g++ 4.8 is doing. |
| 2527 | ProhibitCXX11Attributes(attrs); |
| 2528 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2529 | DS.takeAttributesFrom(attrs); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2530 | } |
Peter Collingbourne | 70188b3 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 2531 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2532 | // If this is not a declaration specifier token, we're done reading decl |
| 2533 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | e3e01a2 | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2534 | DS.Finish(Diags, PP); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2535 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2536 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2537 | case tok::l_square: |
| 2538 | case tok::kw_alignas: |
Richard Smith | 4cabd04 | 2013-02-22 09:15:49 +0000 | [diff] [blame] | 2539 | if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier()) |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2540 | goto DoneWithDeclSpec; |
| 2541 | |
| 2542 | ProhibitAttributes(attrs); |
| 2543 | // FIXME: It would be good to recover by accepting the attributes, |
| 2544 | // but attempting to do that now would cause serious |
| 2545 | // madness in terms of diagnostics. |
| 2546 | attrs.clear(); |
| 2547 | attrs.Range = SourceRange(); |
| 2548 | |
| 2549 | ParseCXX11Attributes(attrs); |
| 2550 | AttrsLastTime = true; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2551 | continue; |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2552 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2553 | case tok::code_completion: { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2554 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2555 | if (DS.hasTypeSpecifier()) { |
| 2556 | bool AllowNonIdentifiers |
| 2557 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 2558 | Scope::BlockScope | |
| 2559 | Scope::TemplateParamScope | |
| 2560 | Scope::FunctionPrototypeScope | |
| 2561 | Scope::AtCatchScope)) == 0; |
| 2562 | bool AllowNestedNameSpecifiers |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2563 | = DSContext == DSC_top_level || |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2564 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 2565 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2566 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2567 | AllowNonIdentifiers, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2568 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2569 | return cutOffParsing(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2572 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 2573 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 2574 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2575 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2576 | : Sema::PCC_Template; |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2577 | else if (DSContext == DSC_class) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2578 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | b6c6a58 | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 2579 | else if (CurParsedObjCImpl) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2580 | CCC = Sema::PCC_ObjCImplementation; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2581 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2582 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2583 | return cutOffParsing(); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
Chris Lattner | bd31aa3 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2586 | case tok::coloncolon: // ::foo::bar |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2587 | // C++ scope specifier. Annotate and loop, or bail out on error. |
Eli Friedman | 2a1d9a9 | 2013-08-15 23:59:20 +0000 | [diff] [blame] | 2588 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2589 | if (!DS.hasTypeSpecifier()) |
| 2590 | DS.SetTypeSpecError(); |
| 2591 | goto DoneWithDeclSpec; |
| 2592 | } |
John McCall | 8bc2a70 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 2593 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 2594 | goto DoneWithDeclSpec; |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2595 | continue; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2596 | |
| 2597 | case tok::annot_cxxscope: { |
Richard Smith | 3092a3b | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2598 | if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector()) |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2599 | goto DoneWithDeclSpec; |
| 2600 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2601 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 2602 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 2603 | Tok.getAnnotationRange(), |
| 2604 | SS); |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2605 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2606 | // We are looking for a qualified typename. |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2607 | Token Next = NextToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2608 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2609 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2610 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2611 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2612 | |
| 2613 | // C++ [class.qual]p2: |
| 2614 | // In a lookup in which the constructor is an acceptable lookup |
| 2615 | // result and the nested-name-specifier nominates a class C: |
| 2616 | // |
| 2617 | // - if the name specified after the |
| 2618 | // nested-name-specifier, when looked up in C, is the |
| 2619 | // injected-class-name of C (Clause 9), or |
| 2620 | // |
| 2621 | // - if the name specified after the nested-name-specifier |
| 2622 | // is the same as the identifier or the |
| 2623 | // simple-template-id's template-name in the last |
| 2624 | // component of the nested-name-specifier, |
| 2625 | // |
| 2626 | // the name is instead considered to name the constructor of |
| 2627 | // class C. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2628 | // |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2629 | // Thus, if the template-name is actually the constructor |
| 2630 | // name, then the code is ill-formed; this interpretation is |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2631 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | c0c5dd2 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2632 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
Dmitri Gribenko | d1c91f1 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2633 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2634 | TemplateId->Name && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2635 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2636 | if (isConstructorDeclarator()) { |
| 2637 | // The user meant this to be an out-of-line constructor |
| 2638 | // definition, but template arguments are not allowed |
| 2639 | // there. Just allow this as a constructor; we'll |
| 2640 | // complain about it later. |
| 2641 | goto DoneWithDeclSpec; |
| 2642 | } |
| 2643 | |
| 2644 | // The user meant this to name a type, but it actually names |
| 2645 | // a constructor with some extraneous template |
| 2646 | // arguments. Complain, then parse it as a type as the user |
| 2647 | // intended. |
| 2648 | Diag(TemplateId->TemplateNameLoc, |
| 2649 | diag::err_out_of_line_template_id_names_constructor) |
| 2650 | << TemplateId->Name; |
| 2651 | } |
| 2652 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2653 | DS.getTypeSpecScope() = SS; |
| 2654 | ConsumeToken(); // The C++ scope. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2655 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2656 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2657 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2658 | continue; |
| 2659 | } |
| 2660 | |
Douglas Gregor | c5790df | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2661 | if (Next.is(tok::annot_typename)) { |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2662 | DS.getTypeSpecScope() = SS; |
| 2663 | ConsumeToken(); // The C++ scope. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2664 | if (Tok.getAnnotationValue()) { |
| 2665 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 7743034 | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2666 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2667 | Tok.getAnnotationEndLoc(), |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2668 | PrevSpec, DiagID, T); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2669 | if (isInvalid) |
| 2670 | break; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2671 | } |
Douglas Gregor | c5790df | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2672 | else |
| 2673 | DS.SetTypeSpecError(); |
| 2674 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2675 | ConsumeToken(); // The typename |
| 2676 | } |
| 2677 | |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2678 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2679 | goto DoneWithDeclSpec; |
| 2680 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2681 | // If we're in a context where the identifier could be a class name, |
| 2682 | // check whether this is a constructor declaration. |
Dmitri Gribenko | d1c91f1 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2683 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2684 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2685 | &SS)) { |
| 2686 | if (isConstructorDeclarator()) |
| 2687 | goto DoneWithDeclSpec; |
| 2688 | |
| 2689 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 2690 | // of the class is qualified in a context where it could name |
| 2691 | // a constructor, its a constructor name. However, we've |
| 2692 | // looked at the declarator, and the user probably meant this |
| 2693 | // to be a type. Complain that it isn't supposed to be treated |
| 2694 | // as a type, then proceed to parse it as a type. |
| 2695 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 2696 | << Next.getIdentifierInfo(); |
| 2697 | } |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2698 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2699 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 2700 | Next.getLocation(), |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2701 | getCurScope(), &SS, |
| 2702 | false, false, ParsedType(), |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2703 | /*IsCtorOrDtorName=*/false, |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2704 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2705 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2706 | // If the referenced identifier is not a type, then this declspec is |
| 2707 | // erroneous: We already checked about that it has no type specifier, and |
| 2708 | // 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] | 2709 | // typename. |
David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 2710 | if (!TypeRep) { |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2711 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2712 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2713 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) { |
| 2714 | if (!Attrs.empty()) { |
| 2715 | AttrsLastTime = true; |
| 2716 | attrs.takeAllFrom(Attrs); |
| 2717 | } |
| 2718 | continue; |
| 2719 | } |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2720 | goto DoneWithDeclSpec; |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2721 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2722 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2723 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2724 | ConsumeToken(); // The C++ scope. |
| 2725 | |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2726 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2727 | DiagID, TypeRep); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2728 | if (isInvalid) |
| 2729 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2730 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2731 | DS.SetRangeEnd(Tok.getLocation()); |
| 2732 | ConsumeToken(); // The typename. |
| 2733 | |
| 2734 | continue; |
| 2735 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2736 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2737 | case tok::annot_typename: { |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2738 | // If we've previously seen a tag definition, we were almost surely |
| 2739 | // missing a semicolon after it. |
| 2740 | if (DS.hasTypeSpecifier() && DS.hasTagDefinition()) |
| 2741 | goto DoneWithDeclSpec; |
| 2742 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2743 | if (Tok.getAnnotationValue()) { |
| 2744 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 7f8bb36 | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 2745 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2746 | DiagID, T); |
| 2747 | } else |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2748 | DS.SetTypeSpecError(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2749 | |
Chris Lattner | 005fc1b | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 2750 | if (isInvalid) |
| 2751 | break; |
| 2752 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2753 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2754 | ConsumeToken(); // The typename |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2755 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2756 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2757 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2758 | // Objective-C interface. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2759 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 06e41ae | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2760 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2761 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2762 | continue; |
| 2763 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2764 | |
Douglas Gregor | 0687309 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2765 | case tok::kw___is_signed: |
| 2766 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 2767 | // typically treats it as a trait. If we see __is_signed as it appears |
| 2768 | // in libstdc++, e.g., |
| 2769 | // |
| 2770 | // static const bool __is_signed; |
| 2771 | // |
| 2772 | // then treat __is_signed as an identifier rather than as a keyword. |
| 2773 | if (DS.getTypeSpecType() == TST_bool && |
| 2774 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
Alp Toker | 47642d2 | 2013-12-03 06:13:01 +0000 | [diff] [blame] | 2775 | DS.getStorageClassSpec() == DeclSpec::SCS_static) |
| 2776 | TryKeywordIdentFallback(true); |
Douglas Gregor | 0687309 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2777 | |
| 2778 | // We're done with the declaration-specifiers. |
| 2779 | goto DoneWithDeclSpec; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2780 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2781 | // typedef-name |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2782 | case tok::kw_decltype: |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2783 | case tok::identifier: { |
Chris Lattner | bd31aa3 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2784 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 2785 | // so handle it as such. This is important for ctor parsing. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2786 | if (getLangOpts().CPlusPlus) { |
Eli Friedman | 2a1d9a9 | 2013-08-15 23:59:20 +0000 | [diff] [blame] | 2787 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2788 | if (!DS.hasTypeSpecifier()) |
| 2789 | DS.SetTypeSpecError(); |
| 2790 | goto DoneWithDeclSpec; |
| 2791 | } |
| 2792 | if (!Tok.is(tok::identifier)) |
| 2793 | continue; |
| 2794 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2795 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2796 | // This identifier can only be a typedef name if we haven't already seen |
| 2797 | // a type-specifier. Without this check we misparse: |
| 2798 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 2799 | if (DS.hasTypeSpecifier()) |
| 2800 | goto DoneWithDeclSpec; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2801 | |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2802 | // Check for need to substitute AltiVec keyword tokens. |
| 2803 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2804 | break; |
| 2805 | |
Richard Smith | 3092a3b | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2806 | // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not |
| 2807 | // allow the use of a typedef name as a type specifier. |
| 2808 | if (DS.isTypeAltiVecVector()) |
| 2809 | goto DoneWithDeclSpec; |
| 2810 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2811 | ParsedType TypeRep = |
| 2812 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 2813 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2814 | |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2815 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 2816 | // it must be an implicit int or an error. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2817 | if (!TypeRep) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2818 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2819 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) { |
| 2820 | if (!Attrs.empty()) { |
| 2821 | AttrsLastTime = true; |
| 2822 | attrs.takeAllFrom(Attrs); |
| 2823 | } |
| 2824 | continue; |
| 2825 | } |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2826 | goto DoneWithDeclSpec; |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2827 | } |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2828 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2829 | // If we're in a context where the identifier could be a class name, |
| 2830 | // check whether this is a constructor declaration. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2831 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2832 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2833 | isConstructorDeclarator()) |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2834 | goto DoneWithDeclSpec; |
| 2835 | |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2836 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2837 | DiagID, TypeRep); |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2838 | if (isInvalid) |
| 2839 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2840 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2841 | DS.SetRangeEnd(Tok.getLocation()); |
| 2842 | ConsumeToken(); // The identifier |
| 2843 | |
| 2844 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2845 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2846 | // Objective-C interface. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2847 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 06e41ae | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2848 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2849 | |
Steve Naroff | cd5e782 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 2850 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2851 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2852 | continue; |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2853 | } |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2854 | |
| 2855 | // type-name |
| 2856 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | c0c5dd2 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2857 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2858 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2859 | // This template-id does not refer to a type name, so we're |
| 2860 | // done with the type-specifiers. |
| 2861 | goto DoneWithDeclSpec; |
| 2862 | } |
| 2863 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2864 | // If we're in a context where the template-id could be a |
| 2865 | // constructor name or specialization, check whether this is a |
| 2866 | // constructor declaration. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2867 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2868 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2869 | isConstructorDeclarator()) |
| 2870 | goto DoneWithDeclSpec; |
| 2871 | |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2872 | // Turn the template-id annotation token into a type annotation |
| 2873 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2874 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2875 | continue; |
| 2876 | } |
| 2877 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 2878 | // GNU attributes support. |
| 2879 | case tok::kw___attribute: |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2880 | ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs); |
Chris Lattner | b95cca0 | 2006-10-17 03:01:08 +0000 | [diff] [blame] | 2881 | continue; |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2882 | |
| 2883 | // Microsoft declspec support. |
| 2884 | case tok::kw___declspec: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2885 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2886 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2887 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2888 | // Microsoft single token adornments. |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2889 | case tok::kw___forceinline: { |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2890 | isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID); |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2891 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2892 | SourceLocation AttrNameLoc = Tok.getLocation(); |
Alexis Hunt | a0e54d4 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 2893 | // FIXME: This does not work correctly if it is set to be a declspec |
| 2894 | // attribute, and a GNU attribute is simply incorrect. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2895 | DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 2896 | AttributeList::AS_GNU); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2897 | break; |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2898 | } |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2899 | |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 2900 | case tok::kw___sptr: |
| 2901 | case tok::kw___uptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2902 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2903 | case tok::kw___ptr32: |
Steve Naroff | f9c29d4 | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2904 | case tok::kw___w64: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2905 | case tok::kw___cdecl: |
| 2906 | case tok::kw___stdcall: |
| 2907 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2908 | case tok::kw___thiscall: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2909 | case tok::kw___unaligned: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2910 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2911 | continue; |
| 2912 | |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2913 | // Borland single token adornments. |
| 2914 | case tok::kw___pascal: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2915 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2916 | continue; |
| 2917 | |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2918 | // OpenCL single token adornments. |
| 2919 | case tok::kw___kernel: |
| 2920 | ParseOpenCLAttributes(DS.getAttributes()); |
| 2921 | continue; |
| 2922 | |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2923 | // storage-class-specifier |
| 2924 | case tok::kw_typedef: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2925 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
| 2926 | PrevSpec, DiagID); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2927 | break; |
| 2928 | case tok::kw_extern: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2929 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2930 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2931 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
| 2932 | PrevSpec, DiagID); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2933 | break; |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2934 | case tok::kw___private_extern__: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2935 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
| 2936 | Loc, PrevSpec, DiagID); |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2937 | break; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2938 | case tok::kw_static: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2939 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2940 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2941 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
| 2942 | PrevSpec, DiagID); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2943 | break; |
| 2944 | case tok::kw_auto: |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2945 | if (getLangOpts().CPlusPlus11) { |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2946 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2947 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2948 | PrevSpec, DiagID); |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2949 | if (!isInvalid) |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2950 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2951 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2952 | } else |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2953 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 2954 | DiagID); |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2955 | } else |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2956 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2957 | PrevSpec, DiagID); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2958 | break; |
| 2959 | case tok::kw_register: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2960 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
| 2961 | PrevSpec, DiagID); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2962 | break; |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2963 | case tok::kw_mutable: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2964 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
| 2965 | PrevSpec, DiagID); |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2966 | break; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2967 | case tok::kw___thread: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2968 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc, |
| 2969 | PrevSpec, DiagID); |
| 2970 | break; |
| 2971 | case tok::kw_thread_local: |
| 2972 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc, |
| 2973 | PrevSpec, DiagID); |
| 2974 | break; |
| 2975 | case tok::kw__Thread_local: |
| 2976 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local, |
| 2977 | Loc, PrevSpec, DiagID); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2978 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2979 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2980 | // function-specifier |
| 2981 | case tok::kw_inline: |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2982 | isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2983 | break; |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2984 | case tok::kw_virtual: |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2985 | isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2986 | break; |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2987 | case tok::kw_explicit: |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2988 | isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2989 | break; |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 2990 | case tok::kw__Noreturn: |
| 2991 | if (!getLangOpts().C11) |
| 2992 | Diag(Loc, diag::ext_c11_noreturn); |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2993 | isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID); |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 2994 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2995 | |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2996 | // alignment-specifier |
| 2997 | case tok::kw__Alignas: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2998 | if (!getLangOpts().C11) |
Jordan Rose | 58d5472 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2999 | Diag(Tok, diag::ext_c11_alignment) << Tok.getName(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 3000 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 3001 | continue; |
| 3002 | |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 3003 | // friend |
| 3004 | case tok::kw_friend: |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3005 | if (DSContext == DSC_class) |
| 3006 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 3007 | else { |
| 3008 | PrevSpec = ""; // not actually used by the diagnostic |
| 3009 | DiagID = diag::err_friend_invalid_in_context; |
| 3010 | isInvalid = true; |
| 3011 | } |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 3012 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3013 | |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 3014 | // Modules |
| 3015 | case tok::kw___module_private__: |
| 3016 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 3017 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3018 | |
Sebastian Redl | 39c2a8b | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 3019 | // constexpr |
| 3020 | case tok::kw_constexpr: |
| 3021 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 3022 | break; |
| 3023 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3024 | // type-specifier |
| 3025 | case tok::kw_short: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3026 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 3027 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3028 | break; |
| 3029 | case tok::kw_long: |
| 3030 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3031 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 3032 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3033 | else |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3034 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 3035 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3036 | break; |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3037 | case tok::kw___int64: |
| 3038 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 3039 | DiagID); |
| 3040 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3041 | case tok::kw_signed: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3042 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 3043 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3044 | break; |
| 3045 | case tok::kw_unsigned: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3046 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 3047 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3048 | break; |
| 3049 | case tok::kw__Complex: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3050 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 3051 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3052 | break; |
| 3053 | case tok::kw__Imaginary: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3054 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 3055 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3056 | break; |
| 3057 | case tok::kw_void: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3058 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 3059 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3060 | break; |
| 3061 | case tok::kw_char: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3062 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 3063 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3064 | break; |
| 3065 | case tok::kw_int: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3066 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 3067 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3068 | break; |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3069 | case tok::kw___int128: |
| 3070 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, |
| 3071 | DiagID); |
| 3072 | break; |
| 3073 | case tok::kw_half: |
| 3074 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
| 3075 | DiagID); |
| 3076 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3077 | case tok::kw_float: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3078 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 3079 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3080 | break; |
| 3081 | case tok::kw_double: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3082 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 3083 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3084 | break; |
| 3085 | case tok::kw_wchar_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3086 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 3087 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3088 | break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3089 | case tok::kw_char16_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3090 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 3091 | DiagID); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3092 | break; |
| 3093 | case tok::kw_char32_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3094 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 3095 | DiagID); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3096 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3097 | case tok::kw_bool: |
| 3098 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 20ee5ae | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3099 | if (Tok.is(tok::kw_bool) && |
| 3100 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 3101 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 3102 | PrevSpec = ""; // Not used by the diagnostic. |
| 3103 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | 2b05999 | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3104 | // For better error recovery. |
| 3105 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 20ee5ae | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3106 | isInvalid = true; |
| 3107 | } else { |
| 3108 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 3109 | DiagID); |
| 3110 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3111 | break; |
| 3112 | case tok::kw__Decimal32: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3113 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 3114 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3115 | break; |
| 3116 | case tok::kw__Decimal64: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3117 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 3118 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3119 | break; |
| 3120 | case tok::kw__Decimal128: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3121 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 3122 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3123 | break; |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3124 | case tok::kw___vector: |
| 3125 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 3126 | break; |
| 3127 | case tok::kw___pixel: |
| 3128 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 3129 | break; |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3130 | case tok::kw___unknown_anytype: |
| 3131 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
| 3132 | PrevSpec, DiagID); |
| 3133 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3134 | |
| 3135 | // class-specifier: |
| 3136 | case tok::kw_class: |
| 3137 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3138 | case tok::kw___interface: |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3139 | case tok::kw_union: { |
| 3140 | tok::TokenKind Kind = Tok.getKind(); |
| 3141 | ConsumeToken(); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3142 | |
| 3143 | // These are attributes following class specifiers. |
| 3144 | // To produce better diagnostic, we parse them when |
| 3145 | // parsing class specifier. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3146 | ParsedAttributesWithRange Attributes(AttrFactory); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3147 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3148 | EnteringContext, DSContext, Attributes); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3149 | |
| 3150 | // If there are attributes following class specifier, |
| 3151 | // take them over and handle them here. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3152 | if (!Attributes.empty()) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3153 | AttrsLastTime = true; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3154 | attrs.takeAllFrom(Attributes); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3155 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3156 | continue; |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3157 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3158 | |
| 3159 | // enum-specifier: |
| 3160 | case tok::kw_enum: |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3161 | ConsumeToken(); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3162 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3163 | continue; |
| 3164 | |
| 3165 | // cv-qualifier: |
| 3166 | case tok::kw_const: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3167 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3168 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3169 | break; |
| 3170 | case tok::kw_volatile: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3171 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3172 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3173 | break; |
| 3174 | case tok::kw_restrict: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3175 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3176 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3177 | break; |
| 3178 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3179 | // C++ typename-specifier: |
| 3180 | case tok::kw_typename: |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3181 | if (TryAnnotateTypeOrScopeToken()) { |
| 3182 | DS.SetTypeSpecError(); |
| 3183 | goto DoneWithDeclSpec; |
| 3184 | } |
| 3185 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3186 | continue; |
| 3187 | break; |
| 3188 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3189 | // GNU typeof support. |
| 3190 | case tok::kw_typeof: |
| 3191 | ParseTypeofSpecifier(DS); |
| 3192 | continue; |
| 3193 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3194 | case tok::annot_decltype: |
Anders Carlsson | 74948d0 | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 3195 | ParseDecltypeSpecifier(DS); |
| 3196 | continue; |
| 3197 | |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3198 | case tok::kw___underlying_type: |
| 3199 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3200 | continue; |
| 3201 | |
| 3202 | case tok::kw__Atomic: |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3203 | // C11 6.7.2.4/4: |
| 3204 | // If the _Atomic keyword is immediately followed by a left parenthesis, |
| 3205 | // it is interpreted as a type specifier (with a type name), not as a |
| 3206 | // type qualifier. |
| 3207 | if (NextToken().is(tok::l_paren)) { |
| 3208 | ParseAtomicSpecifier(DS); |
| 3209 | continue; |
| 3210 | } |
| 3211 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 3212 | getLangOpts()); |
| 3213 | break; |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3214 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3215 | // OpenCL qualifiers: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3216 | case tok::kw___private: |
| 3217 | case tok::kw___global: |
| 3218 | case tok::kw___local: |
| 3219 | case tok::kw___constant: |
| 3220 | case tok::kw___read_only: |
| 3221 | case tok::kw___write_only: |
| 3222 | case tok::kw___read_write: |
| 3223 | ParseOpenCLQualifiers(DS); |
| 3224 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3225 | |
Steve Naroff | cfdf616 | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 3226 | case tok::less: |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3227 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3228 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 3229 | // but we support it. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3230 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3231 | goto DoneWithDeclSpec; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3232 | |
Douglas Gregor | 3a001f4 | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 3233 | if (!ParseObjCProtocolQualifiers(DS)) |
| 3234 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 3235 | << FixItHint::CreateInsertion(Loc, "id") |
| 3236 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3237 | |
Douglas Gregor | 06e41ae | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 3238 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 3239 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 3240 | continue; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 3241 | } |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3242 | // If the specifier wasn't legal, issue a diagnostic. |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3243 | if (isInvalid) { |
| 3244 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3245 | assert(DiagID); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3246 | |
Douglas Gregor | a05f5ab | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 3247 | if (DiagID == diag::ext_duplicate_declspec) |
| 3248 | Diag(Tok, DiagID) |
| 3249 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 3250 | else |
| 3251 | Diag(Tok, DiagID) << PrevSpec; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3252 | } |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3253 | |
Chris Lattner | 2e23209 | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 3254 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | 2b05999 | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3255 | if (DiagID != diag::err_bool_redeclaration) |
| 3256 | ConsumeToken(); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3257 | |
| 3258 | AttrsLastTime = false; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 3259 | } |
| 3260 | } |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 3261 | |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3262 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 3263 | /// semicolon. |
| 3264 | /// |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3265 | /// struct-declaration: |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3266 | /// specifier-qualifier-list struct-declarator-list |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 3267 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3268 | /// [GNU] specifier-qualifier-list |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3269 | /// struct-declarator-list: |
| 3270 | /// struct-declarator |
| 3271 | /// struct-declarator-list ',' struct-declarator |
| 3272 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 3273 | /// struct-declarator: |
| 3274 | /// declarator |
| 3275 | /// [GNU] declarator attributes[opt] |
| 3276 | /// declarator[opt] ':' constant-expression |
| 3277 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 3278 | /// |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3279 | void Parser:: |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3280 | ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3281 | |
Chris Lattner | f02ef3e | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3282 | if (Tok.is(tok::kw___extension__)) { |
| 3283 | // __extension__ silences extension warnings in the subexpression. |
| 3284 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3285 | ConsumeToken(); |
Chris Lattner | f02ef3e | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3286 | return ParseStructDeclaration(DS, Fields); |
| 3287 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3288 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3289 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3290 | ParseSpecifierQualifierList(DS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3291 | |
Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3292 | // If there are no declarators, this is a free-standing declaration |
| 3293 | // specifier. Let the actions module cope with it. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3294 | if (Tok.is(tok::semi)) { |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3295 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
| 3296 | DS); |
| 3297 | DS.complete(TheDecl); |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3298 | return; |
| 3299 | } |
| 3300 | |
| 3301 | // Read struct-declarators until we find the semicolon. |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3302 | bool FirstDeclarator = true; |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3303 | SourceLocation CommaLoc; |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3304 | while (1) { |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3305 | ParsingFieldDeclarator DeclaratorInfo(*this, DS); |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3306 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3307 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3308 | // Attributes are only allowed here on successive declarators. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3309 | if (!FirstDeclarator) |
| 3310 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3311 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3312 | /// struct-declarator: declarator |
| 3313 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | 17c3b1f | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3314 | if (Tok.isNot(tok::colon)) { |
| 3315 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 3316 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3317 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | 17c3b1f | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3318 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3319 | |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 3320 | if (TryConsumeToken(tok::colon)) { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3321 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3322 | if (Res.isInvalid()) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3323 | SkipUntil(tok::semi, StopBeforeMatch); |
Chris Lattner | 32295d3 | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 3324 | else |
Sebastian Redl | d9f7b1c | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 3325 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3326 | } |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3327 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3328 | // If attributes exist after the declarator, parse them. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3329 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3330 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3331 | // We're done with this declarator; invoke the callback. |
Eli Friedman | ba01f2b | 2012-08-08 23:35:12 +0000 | [diff] [blame] | 3332 | Fields.invoke(DeclaratorInfo); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3333 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3334 | // If we don't have a comma, it is either the end of the list (a ';') |
| 3335 | // or an error, bail out. |
Alp Toker | 8fbec67 | 2013-12-17 23:29:36 +0000 | [diff] [blame] | 3336 | if (!TryConsumeToken(tok::comma, CommaLoc)) |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3337 | return; |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3338 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3339 | FirstDeclarator = false; |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3340 | } |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3341 | } |
| 3342 | |
| 3343 | /// ParseStructUnionBody |
| 3344 | /// struct-contents: |
| 3345 | /// struct-declaration-list |
| 3346 | /// [EXT] empty |
| 3347 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 3348 | /// struct-declaration-list: |
| 3349 | /// struct-declaration |
| 3350 | /// struct-declaration-list struct-declaration |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3351 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3352 | /// |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 3353 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3354 | unsigned TagType, Decl *TagDecl) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3355 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 3356 | "parsing struct/union body"); |
Andy Gibbs | 22e140b | 2013-04-03 09:31:19 +0000 | [diff] [blame] | 3357 | assert(!getLangOpts().CPlusPlus && "C++ declarations not supported"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3358 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3359 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3360 | if (T.consumeOpen()) |
| 3361 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3362 | |
Douglas Gregor | 658b955 | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 3363 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3364 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3365 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3366 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3367 | |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 3368 | // 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] | 3369 | while (Tok.isNot(tok::r_brace) && !isEofOrEom()) { |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3370 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3371 | |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 3372 | // Check for extraneous top-level semicolon. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3373 | if (Tok.is(tok::semi)) { |
Richard Smith | 87f5dc5 | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3374 | ConsumeExtraSemi(InsideStruct, TagType); |
Chris Lattner | 36e46a2 | 2007-06-09 05:49:55 +0000 | [diff] [blame] | 3375 | continue; |
| 3376 | } |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3377 | |
Andy Gibbs | c804e08 | 2013-04-03 09:46:04 +0000 | [diff] [blame] | 3378 | // Parse _Static_assert declaration. |
| 3379 | if (Tok.is(tok::kw__Static_assert)) { |
| 3380 | SourceLocation DeclEnd; |
| 3381 | ParseStaticAssertDeclaration(DeclEnd); |
| 3382 | continue; |
| 3383 | } |
| 3384 | |
Argyrios Kyrtzidis | 71c12fb | 2013-04-18 01:42:35 +0000 | [diff] [blame] | 3385 | if (Tok.is(tok::annot_pragma_pack)) { |
| 3386 | HandlePragmaPack(); |
| 3387 | continue; |
| 3388 | } |
| 3389 | |
| 3390 | if (Tok.is(tok::annot_pragma_align)) { |
| 3391 | HandlePragmaAlign(); |
| 3392 | continue; |
| 3393 | } |
| 3394 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3395 | if (!Tok.is(tok::at)) { |
| 3396 | struct CFieldCallback : FieldCallback { |
| 3397 | Parser &P; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3398 | Decl *TagDecl; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3399 | SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3400 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3401 | CFieldCallback(Parser &P, Decl *TagDecl, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3402 | SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3403 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 3404 | |
Eli Friedman | 934dbbf | 2012-08-08 23:53:27 +0000 | [diff] [blame] | 3405 | void invoke(ParsingFieldDeclarator &FD) { |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3406 | // Install the declarator into the current TagDecl. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3407 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 5e6253b | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 3408 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 3409 | FD.D, FD.BitfieldSize); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3410 | FieldDecls.push_back(Field); |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3411 | FD.complete(Field); |
Douglas Gregor | 66a985d | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 3412 | } |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3413 | } Callback(*this, TagDecl, FieldDecls); |
| 3414 | |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3415 | // Parse all the comma separated declarators. |
| 3416 | ParsingDeclSpec DS(*this); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3417 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3418 | } else { // Handle @defs |
| 3419 | ConsumeToken(); |
| 3420 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 3421 | Diag(Tok, diag::err_unexpected_at); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3422 | SkipUntil(tok::semi); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3423 | continue; |
| 3424 | } |
| 3425 | ConsumeToken(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 3426 | ExpectAndConsume(tok::l_paren); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3427 | if (!Tok.is(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 3428 | Diag(Tok, diag::err_expected) << tok::identifier; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3429 | SkipUntil(tok::semi); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3430 | continue; |
| 3431 | } |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3432 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3433 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3434 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3435 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 3436 | ConsumeToken(); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 3437 | ExpectAndConsume(tok::r_paren); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3438 | } |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 3439 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 3440 | if (TryConsumeToken(tok::semi)) |
| 3441 | continue; |
| 3442 | |
| 3443 | if (Tok.is(tok::r_brace)) { |
Chris Lattner | 245c533 | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3444 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Chris Lattner | 0c7e82d | 2007-06-09 05:54:40 +0000 | [diff] [blame] | 3445 | break; |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3446 | } |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 3447 | |
| 3448 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 3449 | // Skip to end of block or statement to avoid ext-warning on extra ';'. |
| 3450 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
| 3451 | // If we stopped at a ';', eat it. |
| 3452 | TryConsumeToken(tok::semi); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3453 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3454 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3455 | T.consumeClose(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3456 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3457 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3458 | // If attributes exist after struct contents, parse them. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3459 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 15619c7 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 3460 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3461 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 751c558 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 3462 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3463 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3464 | attrs.getList()); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3465 | StructScope.Exit(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3466 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 3467 | T.getCloseLocation()); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3468 | } |
| 3469 | |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3470 | /// ParseEnumSpecifier |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 3471 | /// enum-specifier: [C99 6.7.2.2] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3472 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3473 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 3474 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3475 | /// '}' attributes[opt] |
Aaron Ballman | 9ecff02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 3476 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3477 | /// '}' |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3478 | /// 'enum' identifier |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 3479 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3480 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3481 | /// [C++11] enum-head '{' enumerator-list[opt] '}' |
| 3482 | /// [C++11] enum-head '{' enumerator-list ',' '}' |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3483 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3484 | /// enum-head: [C++11] |
| 3485 | /// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt] |
| 3486 | /// enum-key attribute-specifier-seq[opt] nested-name-specifier |
| 3487 | /// identifier enum-base[opt] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3488 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3489 | /// enum-key: [C++11] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3490 | /// 'enum' |
| 3491 | /// 'enum' 'class' |
| 3492 | /// 'enum' 'struct' |
| 3493 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3494 | /// enum-base: [C++11] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3495 | /// ':' type-specifier-seq |
| 3496 | /// |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3497 | /// [C++] elaborated-type-specifier: |
| 3498 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 3499 | /// |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3500 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | dc70c3a | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 3501 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3502 | AccessSpecifier AS, DeclSpecContext DSC) { |
Chris Lattner | ffbc271 | 2007-01-25 06:05:38 +0000 | [diff] [blame] | 3503 | // Parse the tag portion of this. |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3504 | if (Tok.is(tok::code_completion)) { |
| 3505 | // Code completion for an enum name. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3506 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3507 | return cutOffParsing(); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3508 | } |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3509 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3510 | // If attributes exist after tag, parse them. |
| 3511 | ParsedAttributesWithRange attrs(AttrFactory); |
| 3512 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3513 | MaybeParseCXX11Attributes(attrs); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3514 | |
| 3515 | // If declspecs exist after tag, parse them. |
| 3516 | while (Tok.is(tok::kw___declspec)) |
| 3517 | ParseMicrosoftDeclSpec(attrs); |
| 3518 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3519 | SourceLocation ScopedEnumKWLoc; |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3520 | bool IsScopedUsingClassTag = false; |
| 3521 | |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3522 | // In C++11, recognize 'enum class' and 'enum struct'. |
Richard Trieu | d0d87b5 | 2013-04-23 02:47:36 +0000 | [diff] [blame] | 3523 | if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) { |
| 3524 | Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum |
| 3525 | : diag::ext_scoped_enum); |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3526 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3527 | ScopedEnumKWLoc = ConsumeToken(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3528 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3529 | // Attributes are not allowed between these keywords. Diagnose, |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3530 | // but then just treat them like they appeared in the right place. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3531 | ProhibitAttributes(attrs); |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3532 | |
| 3533 | // They are allowed afterwards, though. |
| 3534 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3535 | MaybeParseCXX11Attributes(attrs); |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3536 | while (Tok.is(tok::kw___declspec)) |
| 3537 | ParseMicrosoftDeclSpec(attrs); |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3538 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3539 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3540 | // C++11 [temp.explicit]p12: |
| 3541 | // The usual access controls do not apply to names used to specify |
| 3542 | // explicit instantiations. |
| 3543 | // We extend this to also cover explicit specializations. Note that |
| 3544 | // we don't suppress if this turns out to be an elaborated type |
| 3545 | // specifier. |
| 3546 | bool shouldDelayDiagsInTag = |
| 3547 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 3548 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 3549 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3550 | |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3551 | // Enum definitions should not be parsed in a trailing-return-type. |
| 3552 | bool AllowDeclaration = DSC != DSC_trailing; |
| 3553 | |
| 3554 | bool AllowFixedUnderlyingType = AllowDeclaration && |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3555 | (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt || |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3556 | getLangOpts().ObjC2); |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3557 | |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3558 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3559 | if (getLangOpts().CPlusPlus) { |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3560 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 3561 | // if a fixed underlying type is allowed. |
| 3562 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3563 | |
| 3564 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Richard Smith | 1d4b2e1 | 2013-04-01 21:43:41 +0000 | [diff] [blame] | 3565 | /*EnteringContext=*/true)) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3566 | return; |
| 3567 | |
| 3568 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 3569 | Diag(Tok, diag::err_expected) << tok::identifier; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3570 | if (Tok.isNot(tok::l_brace)) { |
| 3571 | // Has no name and is not a definition. |
| 3572 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3573 | SkipUntil(tok::comma, StopAtSemi); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3574 | return; |
| 3575 | } |
| 3576 | } |
| 3577 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3578 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3579 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3580 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3581 | !(AllowFixedUnderlyingType && Tok.is(tok::colon))) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 3582 | Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3583 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3584 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3585 | SkipUntil(tok::comma, StopAtSemi); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3586 | return; |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3587 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3588 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3589 | // If an identifier is present, consume and remember it. |
| 3590 | IdentifierInfo *Name = 0; |
| 3591 | SourceLocation NameLoc; |
| 3592 | if (Tok.is(tok::identifier)) { |
| 3593 | Name = Tok.getIdentifierInfo(); |
| 3594 | NameLoc = ConsumeToken(); |
| 3595 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3596 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3597 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3598 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 3599 | // declaration of a scoped enumeration. |
| 3600 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3601 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3602 | IsScopedUsingClassTag = false; |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3603 | } |
| 3604 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3605 | // Okay, end the suppression area. We'll decide whether to emit the |
| 3606 | // diagnostics in a second. |
| 3607 | if (shouldDelayDiagsInTag) |
| 3608 | diagsFromTag.done(); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3609 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3610 | TypeResult BaseType; |
| 3611 | |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3612 | // Parse the fixed underlying type. |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3613 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3614 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3615 | bool PossibleBitfield = false; |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3616 | if (CanBeBitfield) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3617 | // If we're in class scope, this can either be an enum declaration with |
| 3618 | // an underlying type, or a declaration of a bitfield member. We try to |
| 3619 | // use a simple disambiguation scheme first to catch the common cases |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3620 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 3621 | // anything that's a simple-type-specifier followed by '(' as an |
| 3622 | // expression. This suffices because function types are not valid |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3623 | // underlying types anyway. |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 3624 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 3625 | Sema::ConstantEvaluated); |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3626 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3627 | // 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] | 3628 | // bit-field. This is the common case. |
| 3629 | if (TPR == TPResult::True()) |
| 3630 | PossibleBitfield = true; |
| 3631 | // If the next token starts a type-specifier-seq, it may be either a |
| 3632 | // 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] | 3633 | // 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] | 3634 | // fixed underlying type. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3635 | else if (TPR == TPResult::False() && |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3636 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 3637 | // Consume the ':'. |
| 3638 | ConsumeToken(); |
| 3639 | } else { |
| 3640 | // We have the start of a type-specifier-seq, so we have to perform |
| 3641 | // tentative parsing to determine whether we have an expression or a |
| 3642 | // type. |
| 3643 | TentativeParsingAction TPA(*this); |
| 3644 | |
| 3645 | // Consume the ':'. |
| 3646 | ConsumeToken(); |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3647 | |
| 3648 | // If we see a type specifier followed by an open-brace, we have an |
| 3649 | // ambiguity between an underlying type and a C++11 braced |
| 3650 | // function-style cast. Resolve this by always treating it as an |
| 3651 | // underlying type. |
| 3652 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 3653 | // this case. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3654 | if ((getLangOpts().CPlusPlus && |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3655 | isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3656 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3657 | // We'll parse this as a bitfield later. |
| 3658 | PossibleBitfield = true; |
| 3659 | TPA.Revert(); |
| 3660 | } else { |
| 3661 | // We have a type-specifier-seq. |
| 3662 | TPA.Commit(); |
| 3663 | } |
| 3664 | } |
| 3665 | } else { |
| 3666 | // Consume the ':'. |
| 3667 | ConsumeToken(); |
| 3668 | } |
| 3669 | |
| 3670 | if (!PossibleBitfield) { |
| 3671 | SourceRange Range; |
| 3672 | BaseType = ParseTypeName(&Range); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3673 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3674 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3675 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Eli Friedman | 0d0355ab | 2012-11-02 01:34:28 +0000 | [diff] [blame] | 3676 | } else if (!getLangOpts().ObjC2) { |
| 3677 | if (getLangOpts().CPlusPlus) |
| 3678 | Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range; |
| 3679 | else |
| 3680 | Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range; |
| 3681 | } |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3682 | } |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3683 | } |
| 3684 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3685 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 3686 | // friend declaration, and cannot have an accompanying definition. If we have |
| 3687 | // 'enum foo;', then this is a forward declaration. If we have |
| 3688 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 3689 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3690 | // |
| 3691 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 3692 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 3693 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 3694 | // |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3695 | Sema::TagUseKind TUK; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3696 | if (!AllowDeclaration) { |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3697 | TUK = Sema::TUK_Reference; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3698 | } else if (Tok.is(tok::l_brace)) { |
| 3699 | if (DS.isFriendSpecified()) { |
| 3700 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
| 3701 | << SourceRange(DS.getFriendSpecLoc()); |
| 3702 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3703 | SkipUntil(tok::r_brace, StopAtSemi); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3704 | TUK = Sema::TUK_Friend; |
| 3705 | } else { |
| 3706 | TUK = Sema::TUK_Definition; |
| 3707 | } |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 3708 | } else if (!isTypeSpecifier(DSC) && |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3709 | (Tok.is(tok::semi) || |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3710 | (Tok.isAtStartOfLine() && |
| 3711 | !isValidAfterTypeSpecifier(CanBeBitfield)))) { |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3712 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
| 3713 | if (Tok.isNot(tok::semi)) { |
| 3714 | // A semicolon was missing after this declaration. Diagnose and recover. |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 3715 | ExpectAndConsume(tok::semi, diag::err_expected_after, "enum"); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3716 | PP.EnterToken(Tok); |
| 3717 | Tok.setKind(tok::semi); |
| 3718 | } |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3719 | } else { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3720 | TUK = Sema::TUK_Reference; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3721 | } |
| 3722 | |
| 3723 | // If this is an elaborated type specifier, and we delayed |
| 3724 | // diagnostics before, just merge them into the current pool. |
| 3725 | if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) { |
| 3726 | diagsFromTag.redelay(); |
| 3727 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3728 | |
| 3729 | MultiTemplateParamsArg TParams; |
Douglas Gregor | cbbf3e3 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3730 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3731 | TUK != Sema::TUK_Reference) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3732 | if (!getLangOpts().CPlusPlus11 || !SS.isSet()) { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3733 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3734 | Diag(Tok, diag::err_enum_template); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3735 | SkipUntil(tok::comma, StopAtSemi); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3736 | return; |
| 3737 | } |
| 3738 | |
| 3739 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 3740 | // Enumerations can't be explicitly instantiated. |
| 3741 | DS.SetTypeSpecError(); |
| 3742 | Diag(StartLoc, diag::err_explicit_instantiation_enum); |
| 3743 | return; |
| 3744 | } |
| 3745 | |
| 3746 | assert(TemplateInfo.TemplateParams && "no template parameters"); |
| 3747 | TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(), |
| 3748 | TemplateInfo.TemplateParams->size()); |
Douglas Gregor | cbbf3e3 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3749 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3750 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3751 | if (TUK == Sema::TUK_Reference) |
| 3752 | ProhibitAttributes(attrs); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3753 | |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3754 | if (!Name && TUK != Sema::TUK_Definition) { |
| 3755 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3756 | |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3757 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3758 | SkipUntil(tok::comma, StopAtSemi); |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3759 | return; |
| 3760 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3761 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3762 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3763 | bool IsDependent = false; |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3764 | const char *PrevSpec = 0; |
| 3765 | unsigned DiagID; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3766 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3767 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3768 | AS, DS.getModulePrivateSpecLoc(), TParams, |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3769 | Owned, IsDependent, ScopedEnumKWLoc, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 3770 | IsScopedUsingClassTag, BaseType, |
| 3771 | DSC == DSC_type_specifier); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3772 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3773 | if (IsDependent) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3774 | // This enum has a dependent nested-name-specifier. Handle it as a |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3775 | // dependent tag. |
| 3776 | if (!Name) { |
| 3777 | DS.SetTypeSpecError(); |
| 3778 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 3779 | return; |
| 3780 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3781 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3782 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3783 | TUK, SS, Name, StartLoc, |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3784 | NameLoc); |
| 3785 | if (Type.isInvalid()) { |
| 3786 | DS.SetTypeSpecError(); |
| 3787 | return; |
| 3788 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3789 | |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3790 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 3791 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3792 | PrevSpec, DiagID, Type.get())) |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3793 | Diag(StartLoc, DiagID) << PrevSpec; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3794 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3795 | return; |
| 3796 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3797 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3798 | if (!TagDecl) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3799 | // The action failed to produce an enumeration tag. If this is a |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3800 | // definition, consume the entire definition. |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3801 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3802 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3803 | SkipUntil(tok::r_brace, StopAtSemi); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3804 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3805 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3806 | DS.SetTypeSpecError(); |
| 3807 | return; |
| 3808 | } |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3809 | |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3810 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3811 | ParseEnumBody(StartLoc, TagDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3812 | |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3813 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 3814 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3815 | PrevSpec, DiagID, TagDecl, Owned)) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3816 | Diag(StartLoc, DiagID) << PrevSpec; |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3817 | } |
| 3818 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3819 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 3820 | /// enumerator-list: |
| 3821 | /// enumerator |
| 3822 | /// enumerator-list ',' enumerator |
| 3823 | /// enumerator: |
| 3824 | /// enumeration-constant |
| 3825 | /// enumeration-constant '=' constant-expression |
| 3826 | /// enumeration-constant: |
| 3827 | /// identifier |
| 3828 | /// |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3829 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3830 | // Enter the scope of the enum body and start the definition. |
| 3831 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3832 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3833 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3834 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3835 | T.consumeOpen(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3836 | |
Chris Lattner | 37256fb | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 3837 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3838 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 6e81492 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 3839 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3840 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3841 | SmallVector<Decl *, 32> EnumConstantDecls; |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3842 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3843 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3844 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3845 | // Parse the enumerator-list. |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 3846 | while (Tok.isNot(tok::r_brace)) { |
| 3847 | // Parse enumerator. If failed, try skipping till the start of the next |
| 3848 | // enumerator definition. |
| 3849 | if (Tok.isNot(tok::identifier)) { |
| 3850 | Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; |
| 3851 | if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) && |
| 3852 | TryConsumeToken(tok::comma)) |
| 3853 | continue; |
| 3854 | break; |
| 3855 | } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3856 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 3857 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3858 | |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3859 | // If attributes exist after the enumerator, parse them. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3860 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3861 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3862 | MaybeParseCXX11Attributes(attrs); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3863 | ProhibitAttributes(attrs); |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3864 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3865 | SourceLocation EqualLoc; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3866 | ExprResult AssignedVal; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 3867 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3868 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 3869 | if (TryConsumeToken(tok::equal, EqualLoc)) { |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3870 | AssignedVal = ParseConstantExpression(); |
| 3871 | if (AssignedVal.isInvalid()) |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 3872 | SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch); |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3873 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3874 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3875 | // Install the enumerator constant into EnumDecl. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3876 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 3877 | LastEnumConstDecl, |
| 3878 | IdentLoc, Ident, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3879 | attrs.getList(), EqualLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3880 | AssignedVal.release()); |
Fariborz Jahanian | 329b351 | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3881 | PD.complete(EnumConstDecl); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3882 | |
Chris Lattner | 4ef4001 | 2007-06-11 01:28:17 +0000 | [diff] [blame] | 3883 | EnumConstantDecls.push_back(EnumConstDecl); |
| 3884 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3885 | |
Douglas Gregor | ce66d02 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3886 | if (Tok.is(tok::identifier)) { |
| 3887 | // We're missing a comma between enumerators. |
| 3888 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3889 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
Douglas Gregor | ce66d02 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3890 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3891 | continue; |
| 3892 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3893 | |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 3894 | // Emumerator definition must be finished, only comma or r_brace are |
| 3895 | // allowed here. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 3896 | SourceLocation CommaLoc; |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 3897 | if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) { |
| 3898 | if (EqualLoc.isValid()) |
| 3899 | Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace |
| 3900 | << tok::comma; |
| 3901 | else |
| 3902 | Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator); |
| 3903 | if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) { |
| 3904 | if (TryConsumeToken(tok::comma, CommaLoc)) |
| 3905 | continue; |
| 3906 | } else { |
| 3907 | break; |
| 3908 | } |
| 3909 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3910 | |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 3911 | // If comma is followed by r_brace, emit appropriate warning. |
| 3912 | if (Tok.is(tok::r_brace) && CommaLoc.isValid()) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3913 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11) |
Richard Smith | 87f5dc5 | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3914 | Diag(CommaLoc, getLangOpts().CPlusPlus ? |
| 3915 | diag::ext_enumerator_list_comma_cxx : |
| 3916 | diag::ext_enumerator_list_comma_c) |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3917 | << FixItHint::CreateRemoval(CommaLoc); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3918 | else if (getLangOpts().CPlusPlus11) |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3919 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 3920 | << FixItHint::CreateRemoval(CommaLoc); |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 3921 | break; |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3922 | } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3923 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3924 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3925 | // Eat the }. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3926 | T.consumeClose(); |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3927 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3928 | // If attributes exist after the identifier list, parse them. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3929 | ParsedAttributes attrs(AttrFactory); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3930 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3931 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3932 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 3933 | EnumDecl, EnumConstantDecls, |
| 3934 | getCurScope(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3935 | attrs.getList()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3936 | |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3937 | EnumScope.Exit(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3938 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 3939 | T.getCloseLocation()); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3940 | |
| 3941 | // The next token must be valid after an enum definition. If not, a ';' |
| 3942 | // was probably forgotten. |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3943 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
| 3944 | if (!isValidAfterTypeSpecifier(CanBeBitfield)) { |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 3945 | ExpectAndConsume(tok::semi, diag::err_expected_after, "enum"); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3946 | // Push this token back into the preprocessor and change our current token |
| 3947 | // to ';' so that the rest of the code recovers as though there were an |
| 3948 | // ';' after the definition. |
| 3949 | PP.EnterToken(Tok); |
| 3950 | Tok.setKind(tok::semi); |
| 3951 | } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3952 | } |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3953 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 3954 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3955 | /// start of a type-qualifier-list. |
| 3956 | bool Parser::isTypeQualifier() const { |
| 3957 | switch (Tok.getKind()) { |
| 3958 | default: return false; |
Alp Toker | de50ff3 | 2013-12-17 18:17:46 +0000 | [diff] [blame] | 3959 | // type-qualifier |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3960 | case tok::kw_const: |
| 3961 | case tok::kw_volatile: |
| 3962 | case tok::kw_restrict: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3963 | case tok::kw___private: |
| 3964 | case tok::kw___local: |
| 3965 | case tok::kw___global: |
| 3966 | case tok::kw___constant: |
| 3967 | case tok::kw___read_only: |
| 3968 | case tok::kw___read_write: |
| 3969 | case tok::kw___write_only: |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3970 | return true; |
| 3971 | } |
| 3972 | } |
| 3973 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3974 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 3975 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 3976 | /// specifier or if we're not sure. |
| 3977 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 3978 | switch (Tok.getKind()) { |
| 3979 | default: return false; |
| 3980 | // type-specifiers |
| 3981 | case tok::kw_short: |
| 3982 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3983 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3984 | case tok::kw___int128: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3985 | case tok::kw_signed: |
| 3986 | case tok::kw_unsigned: |
| 3987 | case tok::kw__Complex: |
| 3988 | case tok::kw__Imaginary: |
| 3989 | case tok::kw_void: |
| 3990 | case tok::kw_char: |
| 3991 | case tok::kw_wchar_t: |
| 3992 | case tok::kw_char16_t: |
| 3993 | case tok::kw_char32_t: |
| 3994 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3995 | case tok::kw_half: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3996 | case tok::kw_float: |
| 3997 | case tok::kw_double: |
| 3998 | case tok::kw_bool: |
| 3999 | case tok::kw__Bool: |
| 4000 | case tok::kw__Decimal32: |
| 4001 | case tok::kw__Decimal64: |
| 4002 | case tok::kw__Decimal128: |
| 4003 | case tok::kw___vector: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4004 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4005 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4006 | case tok::kw_class: |
| 4007 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4008 | case tok::kw___interface: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4009 | case tok::kw_union: |
| 4010 | // enum-specifier |
| 4011 | case tok::kw_enum: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4012 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4013 | // typedef-name |
| 4014 | case tok::annot_typename: |
| 4015 | return true; |
| 4016 | } |
| 4017 | } |
| 4018 | |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 4019 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4020 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4021 | bool Parser::isTypeSpecifierQualifier() { |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4022 | switch (Tok.getKind()) { |
| 4023 | default: return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4024 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4025 | case tok::identifier: // foo::bar |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4026 | if (TryAltiVecVectorToken()) |
| 4027 | return true; |
| 4028 | // Fall through. |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4029 | case tok::kw_typename: // typename T::type |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4030 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4031 | // recurse to handle whatever we get. |
| 4032 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4033 | return true; |
| 4034 | if (Tok.is(tok::identifier)) |
| 4035 | return false; |
| 4036 | return isTypeSpecifierQualifier(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4037 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4038 | case tok::coloncolon: // ::foo::bar |
| 4039 | if (NextToken().is(tok::kw_new) || // ::new |
| 4040 | NextToken().is(tok::kw_delete)) // ::delete |
| 4041 | return false; |
| 4042 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4043 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4044 | return true; |
| 4045 | return isTypeSpecifierQualifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4046 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 4047 | // GNU attributes support. |
| 4048 | case tok::kw___attribute: |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4049 | // GNU typeof support. |
| 4050 | case tok::kw_typeof: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4051 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4052 | // type-specifiers |
| 4053 | case tok::kw_short: |
| 4054 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4055 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4056 | case tok::kw___int128: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4057 | case tok::kw_signed: |
| 4058 | case tok::kw_unsigned: |
| 4059 | case tok::kw__Complex: |
| 4060 | case tok::kw__Imaginary: |
| 4061 | case tok::kw_void: |
| 4062 | case tok::kw_char: |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4063 | case tok::kw_wchar_t: |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4064 | case tok::kw_char16_t: |
| 4065 | case tok::kw_char32_t: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4066 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4067 | case tok::kw_half: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4068 | case tok::kw_float: |
| 4069 | case tok::kw_double: |
Chris Lattner | bb31a42 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 4070 | case tok::kw_bool: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4071 | case tok::kw__Bool: |
| 4072 | case tok::kw__Decimal32: |
| 4073 | case tok::kw__Decimal64: |
| 4074 | case tok::kw__Decimal128: |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4075 | case tok::kw___vector: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4076 | |
Chris Lattner | 861a226 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 4077 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4078 | case tok::kw_class: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4079 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4080 | case tok::kw___interface: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4081 | case tok::kw_union: |
| 4082 | // enum-specifier |
| 4083 | case tok::kw_enum: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4084 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4085 | // type-qualifier |
| 4086 | case tok::kw_const: |
| 4087 | case tok::kw_volatile: |
| 4088 | case tok::kw_restrict: |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4089 | |
John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4090 | // Debugger support. |
| 4091 | case tok::kw___unknown_anytype: |
| 4092 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4093 | // typedef-name |
Chris Lattner | a8a3f73 | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 4094 | case tok::annot_typename: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4095 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4096 | |
Chris Lattner | 409bf7d | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 4097 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4098 | case tok::less: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4099 | return getLangOpts().ObjC1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4100 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4101 | case tok::kw___cdecl: |
| 4102 | case tok::kw___stdcall: |
| 4103 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4104 | case tok::kw___thiscall: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4105 | case tok::kw___w64: |
| 4106 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4107 | case tok::kw___ptr32: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4108 | case tok::kw___pascal: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4109 | case tok::kw___unaligned: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4110 | |
| 4111 | case tok::kw___private: |
| 4112 | case tok::kw___local: |
| 4113 | case tok::kw___global: |
| 4114 | case tok::kw___constant: |
| 4115 | case tok::kw___read_only: |
| 4116 | case tok::kw___read_write: |
| 4117 | case tok::kw___write_only: |
| 4118 | |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4119 | return true; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4120 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4121 | // C11 _Atomic |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4122 | case tok::kw__Atomic: |
| 4123 | return true; |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4124 | } |
| 4125 | } |
| 4126 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4127 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 4128 | /// declaration specifier. |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4129 | /// |
| 4130 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 4131 | /// this check is to disambiguate between an expression and a declaration. |
| 4132 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4133 | switch (Tok.getKind()) { |
| 4134 | default: return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4135 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4136 | case tok::identifier: // foo::bar |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4137 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4138 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4139 | return false; |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4140 | if (TryAltiVecVectorToken()) |
| 4141 | return true; |
| 4142 | // Fall through. |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4143 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4144 | case tok::kw_typename: // typename T::type |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4145 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4146 | // recurse to handle whatever we get. |
| 4147 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4148 | return true; |
| 4149 | if (Tok.is(tok::identifier)) |
| 4150 | return false; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4151 | |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4152 | // 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] | 4153 | // by an identifier and then either ':' or ']', in a place where an |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4154 | // expression is permitted, then this is probably a class message send |
| 4155 | // missing the initial '['. In this case, we won't consider this to be |
| 4156 | // the start of a declaration. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4157 | if (DisambiguatingWithExpression && |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4158 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 4159 | return false; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4160 | |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4161 | return isDeclarationSpecifier(); |
| 4162 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4163 | case tok::coloncolon: // ::foo::bar |
| 4164 | if (NextToken().is(tok::kw_new) || // ::new |
| 4165 | NextToken().is(tok::kw_delete)) // ::delete |
| 4166 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4167 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4168 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4169 | // recurse to handle whatever we get. |
| 4170 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4171 | return true; |
| 4172 | return isDeclarationSpecifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4173 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4174 | // storage-class-specifier |
| 4175 | case tok::kw_typedef: |
| 4176 | case tok::kw_extern: |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 4177 | case tok::kw___private_extern__: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4178 | case tok::kw_static: |
| 4179 | case tok::kw_auto: |
| 4180 | case tok::kw_register: |
| 4181 | case tok::kw___thread: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4182 | case tok::kw_thread_local: |
| 4183 | case tok::kw__Thread_local: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4184 | |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 4185 | // Modules |
| 4186 | case tok::kw___module_private__: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4187 | |
John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4188 | // Debugger support |
| 4189 | case tok::kw___unknown_anytype: |
| 4190 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4191 | // type-specifiers |
| 4192 | case tok::kw_short: |
| 4193 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4194 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4195 | case tok::kw___int128: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4196 | case tok::kw_signed: |
| 4197 | case tok::kw_unsigned: |
| 4198 | case tok::kw__Complex: |
| 4199 | case tok::kw__Imaginary: |
| 4200 | case tok::kw_void: |
| 4201 | case tok::kw_char: |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4202 | case tok::kw_wchar_t: |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4203 | case tok::kw_char16_t: |
| 4204 | case tok::kw_char32_t: |
| 4205 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4206 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4207 | case tok::kw_half: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4208 | case tok::kw_float: |
| 4209 | case tok::kw_double: |
Chris Lattner | bb31a42 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 4210 | case tok::kw_bool: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4211 | case tok::kw__Bool: |
| 4212 | case tok::kw__Decimal32: |
| 4213 | case tok::kw__Decimal64: |
| 4214 | case tok::kw__Decimal128: |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4215 | case tok::kw___vector: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4216 | |
Chris Lattner | 861a226 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 4217 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4218 | case tok::kw_class: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4219 | case tok::kw_struct: |
| 4220 | case tok::kw_union: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4221 | case tok::kw___interface: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4222 | // enum-specifier |
| 4223 | case tok::kw_enum: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4224 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4225 | // type-qualifier |
| 4226 | case tok::kw_const: |
| 4227 | case tok::kw_volatile: |
| 4228 | case tok::kw_restrict: |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4229 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4230 | // function-specifier |
| 4231 | case tok::kw_inline: |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4232 | case tok::kw_virtual: |
| 4233 | case tok::kw_explicit: |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4234 | case tok::kw__Noreturn: |
Chris Lattner | 7b20dc7 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4235 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 4236 | // alignment-specifier |
| 4237 | case tok::kw__Alignas: |
| 4238 | |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4239 | // friend keyword. |
| 4240 | case tok::kw_friend: |
| 4241 | |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 4242 | // static_assert-declaration |
| 4243 | case tok::kw__Static_assert: |
| 4244 | |
Chris Lattner | 599e47e | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4245 | // GNU typeof support. |
| 4246 | case tok::kw_typeof: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4247 | |
Chris Lattner | 599e47e | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4248 | // GNU attributes. |
Chris Lattner | 7b20dc7 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4249 | case tok::kw___attribute: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4250 | |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4251 | // C++11 decltype and constexpr. |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4252 | case tok::annot_decltype: |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4253 | case tok::kw_constexpr: |
Francois Pichet | e878cb6 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 4254 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4255 | // C11 _Atomic |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4256 | case tok::kw__Atomic: |
| 4257 | return true; |
| 4258 | |
Chris Lattner | 8b2ec16 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 4259 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4260 | case tok::less: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4261 | return getLangOpts().ObjC1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4262 | |
Douglas Gregor | 19b7acf | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 4263 | // typedef-name |
| 4264 | case tok::annot_typename: |
| 4265 | return !DisambiguatingWithExpression || |
| 4266 | !isStartOfObjCClassMessageMissingOpenBracket(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4267 | |
Steve Naroff | f192fab | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 4268 | case tok::kw___declspec: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4269 | case tok::kw___cdecl: |
| 4270 | case tok::kw___stdcall: |
| 4271 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4272 | case tok::kw___thiscall: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4273 | case tok::kw___w64: |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4274 | case tok::kw___sptr: |
| 4275 | case tok::kw___uptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4276 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4277 | case tok::kw___ptr32: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4278 | case tok::kw___forceinline: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4279 | case tok::kw___pascal: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4280 | case tok::kw___unaligned: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4281 | |
| 4282 | case tok::kw___private: |
| 4283 | case tok::kw___local: |
| 4284 | case tok::kw___global: |
| 4285 | case tok::kw___constant: |
| 4286 | case tok::kw___read_only: |
| 4287 | case tok::kw___read_write: |
| 4288 | case tok::kw___write_only: |
| 4289 | |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4290 | return true; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4291 | } |
| 4292 | } |
| 4293 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4294 | bool Parser::isConstructorDeclarator() { |
| 4295 | TentativeParsingAction TPA(*this); |
| 4296 | |
| 4297 | // Parse the C++ scope specifier. |
| 4298 | CXXScopeSpec SS; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4299 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4300 | /*EnteringContext=*/true)) { |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4301 | TPA.Revert(); |
| 4302 | return false; |
| 4303 | } |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4304 | |
| 4305 | // Parse the constructor name. |
| 4306 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 4307 | // We already know that we have a constructor name; just consume |
| 4308 | // the token. |
| 4309 | ConsumeToken(); |
| 4310 | } else { |
| 4311 | TPA.Revert(); |
| 4312 | return false; |
| 4313 | } |
| 4314 | |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4315 | // Current class name must be followed by a left parenthesis. |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4316 | if (Tok.isNot(tok::l_paren)) { |
| 4317 | TPA.Revert(); |
| 4318 | return false; |
| 4319 | } |
| 4320 | ConsumeParen(); |
| 4321 | |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4322 | // A right parenthesis, or ellipsis followed by a right parenthesis signals |
| 4323 | // that we have a constructor. |
| 4324 | if (Tok.is(tok::r_paren) || |
| 4325 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4326 | TPA.Revert(); |
| 4327 | return true; |
| 4328 | } |
| 4329 | |
Richard Smith | f216366 | 2013-09-06 00:12:20 +0000 | [diff] [blame] | 4330 | // A C++11 attribute here signals that we have a constructor, and is an |
| 4331 | // attribute on the first constructor parameter. |
| 4332 | if (getLangOpts().CPlusPlus11 && |
| 4333 | isCXX11AttributeSpecifier(/*Disambiguate*/ false, |
| 4334 | /*OuterMightBeMessageSend*/ true)) { |
| 4335 | TPA.Revert(); |
| 4336 | return true; |
| 4337 | } |
| 4338 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4339 | // If we need to, enter the specified scope. |
| 4340 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4341 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4342 | DeclScopeObj.EnterDeclaratorScope(); |
| 4343 | |
Francois Pichet | 79f3a87 | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4344 | // Optionally skip Microsoft attributes. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4345 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | 79f3a87 | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4346 | MaybeParseMicrosoftAttributes(Attrs); |
| 4347 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4348 | // Check whether the next token(s) are part of a declaration |
| 4349 | // specifier, in which case we have the start of a parameter and, |
| 4350 | // therefore, we know that this is a constructor. |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4351 | bool IsConstructor = false; |
| 4352 | if (isDeclarationSpecifier()) |
| 4353 | IsConstructor = true; |
| 4354 | else if (Tok.is(tok::identifier) || |
| 4355 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) { |
| 4356 | // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type. |
| 4357 | // This might be a parenthesized member name, but is more likely to |
| 4358 | // be a constructor declaration with an invalid argument type. Keep |
| 4359 | // looking. |
| 4360 | if (Tok.is(tok::annot_cxxscope)) |
| 4361 | ConsumeToken(); |
| 4362 | ConsumeToken(); |
| 4363 | |
| 4364 | // If this is not a constructor, we must be parsing a declarator, |
Richard Smith | 1453e31 | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4365 | // which must have one of the following syntactic forms (see the |
| 4366 | // grammar extract at the start of ParseDirectDeclarator): |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4367 | switch (Tok.getKind()) { |
| 4368 | case tok::l_paren: |
| 4369 | // C(X ( int)); |
| 4370 | case tok::l_square: |
| 4371 | // C(X [ 5]); |
| 4372 | // C(X [ [attribute]]); |
| 4373 | case tok::coloncolon: |
| 4374 | // C(X :: Y); |
| 4375 | // C(X :: *p); |
| 4376 | case tok::r_paren: |
| 4377 | // C(X ) |
| 4378 | // Assume this isn't a constructor, rather than assuming it's a |
| 4379 | // constructor with an unnamed parameter of an ill-formed type. |
| 4380 | break; |
| 4381 | |
| 4382 | default: |
| 4383 | IsConstructor = true; |
| 4384 | break; |
| 4385 | } |
| 4386 | } |
| 4387 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4388 | TPA.Revert(); |
| 4389 | return IsConstructor; |
| 4390 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 4391 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4392 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4393 | /// type-qualifier-list: [C99 6.7.5] |
| 4394 | /// type-qualifier |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4395 | /// [vendor] attributes |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4396 | /// [ only if VendorAttributesAllowed=true ] |
| 4397 | /// type-qualifier-list type-qualifier |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4398 | /// [vendor] type-qualifier-list attributes |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4399 | /// [ only if VendorAttributesAllowed=true ] |
| 4400 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4401 | /// [ only if CXX11AttributesAllowed=true ] |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4402 | /// Note: vendor can be GNU, MS, etc. |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4403 | /// |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4404 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 4405 | bool VendorAttributesAllowed, |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4406 | bool CXX11AttributesAllowed, |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 4407 | bool AtomicAllowed, |
| 4408 | bool IdentifierRequired) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4409 | if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed && |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4410 | isCXX11AttributeSpecifier()) { |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4411 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4412 | ParseCXX11Attributes(attrs); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4413 | DS.takeAttributesFrom(attrs); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4414 | } |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4415 | |
| 4416 | SourceLocation EndLoc; |
| 4417 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4418 | while (1) { |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4419 | bool isInvalid = false; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 4420 | const char *PrevSpec = 0; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4421 | unsigned DiagID = 0; |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 4422 | SourceLocation Loc = Tok.getLocation(); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 4423 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4424 | switch (Tok.getKind()) { |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4425 | case tok::code_completion: |
| 4426 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 4427 | return cutOffParsing(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4428 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4429 | case tok::kw_const: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4430 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4431 | getLangOpts()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 4432 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4433 | case tok::kw_volatile: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4434 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4435 | getLangOpts()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 4436 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4437 | case tok::kw_restrict: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4438 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4439 | getLangOpts()); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4440 | break; |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4441 | case tok::kw__Atomic: |
| 4442 | if (!AtomicAllowed) |
| 4443 | goto DoneWithTypeQuals; |
| 4444 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 4445 | getLangOpts()); |
| 4446 | break; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4447 | |
| 4448 | // OpenCL qualifiers: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4449 | case tok::kw___private: |
| 4450 | case tok::kw___global: |
| 4451 | case tok::kw___local: |
| 4452 | case tok::kw___constant: |
| 4453 | case tok::kw___read_only: |
| 4454 | case tok::kw___write_only: |
| 4455 | case tok::kw___read_write: |
| 4456 | ParseOpenCLQualifiers(DS); |
| 4457 | break; |
| 4458 | |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4459 | case tok::kw___uptr: |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 4460 | // GNU libc headers in C mode use '__uptr' as an identifer which conflicts |
| 4461 | // with the MS modifier keyword. |
| 4462 | if (VendorAttributesAllowed && !getLangOpts().CPlusPlus && |
Alp Toker | 47642d2 | 2013-12-03 06:13:01 +0000 | [diff] [blame] | 4463 | IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) { |
| 4464 | if (TryKeywordIdentFallback(false)) |
| 4465 | continue; |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 4466 | } |
| 4467 | case tok::kw___sptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4468 | case tok::kw___w64: |
Steve Naroff | f9c29d4 | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 4469 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4470 | case tok::kw___ptr32: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4471 | case tok::kw___cdecl: |
| 4472 | case tok::kw___stdcall: |
| 4473 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4474 | case tok::kw___thiscall: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4475 | case tok::kw___unaligned: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4476 | if (VendorAttributesAllowed) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4477 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4478 | continue; |
| 4479 | } |
| 4480 | goto DoneWithTypeQuals; |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4481 | case tok::kw___pascal: |
| 4482 | if (VendorAttributesAllowed) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4483 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4484 | continue; |
| 4485 | } |
| 4486 | goto DoneWithTypeQuals; |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 4487 | case tok::kw___attribute: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4488 | if (VendorAttributesAllowed) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4489 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4490 | continue; // do *not* consume the next token! |
| 4491 | } |
| 4492 | // otherwise, FALL THROUGH! |
| 4493 | default: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4494 | DoneWithTypeQuals: |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4495 | // If this is not a type-qualifier token, we're done reading type |
| 4496 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | e3e01a2 | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 4497 | DS.Finish(Diags, PP); |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4498 | if (EndLoc.isValid()) |
| 4499 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4500 | return; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4501 | } |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4502 | |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 4503 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 4504 | if (isInvalid) { |
| 4505 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4506 | Diag(Tok, DiagID) << PrevSpec; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 4507 | } |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4508 | EndLoc = ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4509 | } |
| 4510 | } |
| 4511 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 4512 | |
| 4513 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 4514 | /// |
| 4515 | void Parser::ParseDeclarator(Declarator &D) { |
| 4516 | /// This implements the 'declarator' production in the C grammar, then checks |
| 4517 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4518 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 4519 | } |
| 4520 | |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4521 | static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) { |
| 4522 | if (Kind == tok::star || Kind == tok::caret) |
| 4523 | return true; |
| 4524 | |
| 4525 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 4526 | if (!Lang.CPlusPlus) |
| 4527 | return false; |
| 4528 | |
| 4529 | return Kind == tok::amp || Kind == tok::ampamp; |
| 4530 | } |
| 4531 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4532 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 4533 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 4534 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4535 | /// ptr-operator production. |
| 4536 | /// |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4537 | /// 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] | 4538 | /// made to TryParseDeclarator and MightBeDeclarator, and possibly to |
| 4539 | /// isConstructorDeclarator. |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4540 | /// |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4541 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 4542 | /// [C] pointer[opt] direct-declarator |
| 4543 | /// [C++] direct-declarator |
| 4544 | /// [C++] ptr-operator declarator |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 4545 | /// |
| 4546 | /// pointer: [C99 6.7.5] |
| 4547 | /// '*' type-qualifier-list[opt] |
| 4548 | /// '*' type-qualifier-list[opt] pointer |
| 4549 | /// |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4550 | /// ptr-operator: |
| 4551 | /// '*' cv-qualifier-seq[opt] |
| 4552 | /// '&' |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4553 | /// [C++0x] '&&' |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4554 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4555 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4556 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4557 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 4558 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 66a985d | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 4559 | if (Diags.hasAllExtensionsSilenced()) |
| 4560 | D.setExtension(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4561 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4562 | // C++ member pointers start with a '::' or a nested-name. |
| 4563 | // Member pointers get special handling, since there's no place for the |
| 4564 | // scope spec in the generic path below. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4565 | if (getLangOpts().CPlusPlus && |
Chris Lattner | 803802d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 4566 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 4567 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4568 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4569 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4570 | CXXScopeSpec SS; |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4571 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext); |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4572 | |
Jeffrey Yasskin | 4e150f8 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 4573 | if (SS.isNotEmpty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4574 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4575 | // The scope spec really belongs to the direct-declarator. |
Richard Smith | 5f044ad | 2013-01-08 22:43:49 +0000 | [diff] [blame] | 4576 | if (D.mayHaveIdentifier()) |
| 4577 | D.getCXXScopeSpec() = SS; |
| 4578 | else |
| 4579 | AnnotateScopeToken(SS, true); |
| 4580 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4581 | if (DirectDeclParser) |
| 4582 | (this->*DirectDeclParser)(D); |
| 4583 | return; |
| 4584 | } |
| 4585 | |
| 4586 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4587 | D.SetRangeEnd(Loc); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4588 | DeclSpec DS(AttrFactory); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4589 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4590 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4591 | |
| 4592 | // Recurse to parse whatever is left. |
| 4593 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 4594 | |
| 4595 | // Sema will have to catch (syntactically invalid) pointers into global |
| 4596 | // scope. It has to catch pointers into namespace scope anyway. |
| 4597 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4598 | Loc), |
| 4599 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4600 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4601 | return; |
| 4602 | } |
| 4603 | } |
| 4604 | |
| 4605 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4606 | // Not a pointer, C++ reference, or block. |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4607 | if (!isPtrOperatorToken(Kind, getLangOpts())) { |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4608 | if (DirectDeclParser) |
| 4609 | (this->*DirectDeclParser)(D); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4610 | return; |
| 4611 | } |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4612 | |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4613 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 4614 | // '&&' -> rvalue reference |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4615 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4616 | D.SetRangeEnd(Loc); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4617 | |
Chris Lattner | 9eac931 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 4618 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 788404f | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4619 | // Is a pointer. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4620 | DeclSpec DS(AttrFactory); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4621 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4622 | // FIXME: GNU attributes are not allowed here in a new-type-id. |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 4623 | ParseTypeQualifierListOpt(DS, true, true, true, !D.mayOmitIdentifier()); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4624 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4625 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4626 | // Recursively parse the declarator. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4627 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4628 | if (Kind == tok::star) |
| 4629 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 4630 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | e71b378d | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 4631 | DS.getConstSpecLoc(), |
| 4632 | DS.getVolatileSpecLoc(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4633 | DS.getRestrictSpecLoc()), |
| 4634 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4635 | SourceLocation()); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4636 | else |
| 4637 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4638 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4639 | Loc), |
| 4640 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4641 | SourceLocation()); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4642 | } else { |
| 4643 | // Is a reference |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4644 | DeclSpec DS(AttrFactory); |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 4645 | |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4646 | // Complain about rvalue references in C++03, but then go on and build |
| 4647 | // the declarator. |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4648 | if (Kind == tok::ampamp) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4649 | Diag(Loc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4650 | diag::warn_cxx98_compat_rvalue_reference : |
| 4651 | diag::ext_rvalue_reference); |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4652 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4653 | // GNU-style and C++11 attributes are allowed here, as is restrict. |
| 4654 | ParseTypeQualifierListOpt(DS); |
| 4655 | D.ExtendWithDeclSpec(DS); |
| 4656 | |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 4657 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 4658 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 4659 | // template type argument, in which case the cv-qualifiers are ignored. |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 4660 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 4661 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 4662 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4663 | diag::err_invalid_reference_qualifier_application) << "const"; |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 4664 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 4665 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4666 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4667 | // 'restrict' is permitted as an extension. |
| 4668 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
| 4669 | Diag(DS.getAtomicSpecLoc(), |
| 4670 | diag::err_invalid_reference_qualifier_application) << "_Atomic"; |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 4671 | } |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4672 | |
| 4673 | // Recursively parse the declarator. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4674 | ParseDeclaratorInternal(D, DirectDeclParser); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4675 | |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4676 | if (D.getNumTypeObjects() > 0) { |
| 4677 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 4678 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 4679 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | ebad6a2 | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 4680 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 4681 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4682 | << II; |
| 4683 | else |
| 4684 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4685 | << "type name"; |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4686 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4687 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4688 | // can go ahead and build the (technically ill-formed) |
| 4689 | // declarator: reference collapsing will take care of it. |
| 4690 | } |
| 4691 | } |
| 4692 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4693 | // Remember that we parsed a reference type. |
Chris Lattner | 788404f | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4694 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4695 | Kind == tok::amp), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4696 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4697 | SourceLocation()); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4698 | } |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 4699 | } |
| 4700 | |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4701 | static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D, |
| 4702 | SourceLocation EllipsisLoc) { |
| 4703 | if (EllipsisLoc.isValid()) { |
| 4704 | FixItHint Insertion; |
| 4705 | if (!D.getEllipsisLoc().isValid()) { |
| 4706 | Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "..."); |
| 4707 | D.setEllipsisLoc(EllipsisLoc); |
| 4708 | } |
| 4709 | P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration) |
| 4710 | << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName(); |
| 4711 | } |
| 4712 | } |
| 4713 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4714 | /// ParseDirectDeclarator |
| 4715 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4716 | /// [C99] identifier |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4717 | /// '(' declarator ')' |
| 4718 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 4719 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4720 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4721 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4722 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4723 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4724 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 4725 | /// attribute-specifier-seq[opt] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4726 | /// direct-declarator '(' parameter-type-list ')' |
| 4727 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4728 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4729 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 22c40fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 4730 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 4731 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4732 | /// [C++11] direct-declarator '(' parameter-declaration-clause ')' |
| 4733 | /// attribute-specifier-seq[opt] cv-qualifier-seq[opt] |
| 4734 | /// ref-qualifier[opt] exception-specification[opt] |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4735 | /// [C++] declarator-id |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4736 | /// [C++11] declarator-id attribute-specifier-seq[opt] |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4737 | /// |
| 4738 | /// declarator-id: [C++ 8] |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4739 | /// '...'[opt] id-expression |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4740 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 4741 | /// |
| 4742 | /// id-expression: [C++ 5.1] |
| 4743 | /// unqualified-id |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4744 | /// qualified-id |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4745 | /// |
| 4746 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4747 | /// identifier |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4748 | /// operator-function-id |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4749 | /// conversion-function-id |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4750 | /// '~' class-name |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 4751 | /// template-id |
Argyrios Kyrtzidis | e442635 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 4752 | /// |
Richard Smith | 1453e31 | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4753 | /// Note, any additional constructs added here may need corresponding changes |
| 4754 | /// in isConstructorDeclarator. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4755 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4756 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4757 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4758 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4759 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4760 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4761 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4762 | D.getContext() == Declarator::MemberContext; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4763 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4764 | EnteringContext); |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4765 | } |
| 4766 | |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4767 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4768 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | 2b058ef | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 4769 | // Change the declaration context for name lookup, until this function |
| 4770 | // is exited (and the declarator has been parsed). |
| 4771 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4772 | } |
| 4773 | |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4774 | // C++0x [dcl.fct]p14: |
| 4775 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4776 | // of a parameter-declaration-clause without a preceding comma. In |
| 4777 | // this case, the ellipsis is parsed as part of the |
| 4778 | // abstract-declarator if the type of the parameter names a template |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4779 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 4780 | // as part of the parameter-declaration-clause. |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4781 | if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() && |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4782 | !((D.getContext() == Declarator::PrototypeContext || |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4783 | D.getContext() == Declarator::LambdaExprParameterContext || |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4784 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4785 | NextToken().is(tok::r_paren) && |
Richard Smith | b19337f | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4786 | !D.hasGroupingParens() && |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4787 | !Actions.containsUnexpandedParameterPacks(D))) { |
| 4788 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 4789 | if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) { |
| 4790 | // The ellipsis was put in the wrong place. Recover, and explain to |
| 4791 | // the user what they should have done. |
| 4792 | ParseDeclarator(D); |
| 4793 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4794 | return; |
| 4795 | } else |
| 4796 | D.setEllipsisLoc(EllipsisLoc); |
| 4797 | |
| 4798 | // The ellipsis can't be followed by a parenthesized declarator. We |
| 4799 | // check for that in ParseParenDeclarator, after we have disambiguated |
| 4800 | // the l_paren token. |
| 4801 | } |
| 4802 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4803 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 4804 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 4805 | // We found something that indicates the start of an unqualified-id. |
| 4806 | // Parse that unqualified-id. |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4807 | bool AllowConstructorName; |
| 4808 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 4809 | AllowConstructorName = false; |
| 4810 | else if (D.getCXXScopeSpec().isSet()) |
| 4811 | AllowConstructorName = |
| 4812 | (D.getContext() == Declarator::FileContext || |
Dmitri Gribenko | d1c91f1 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 4813 | D.getContext() == Declarator::MemberContext); |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4814 | else |
| 4815 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 4816 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4817 | SourceLocation TemplateKWLoc; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4818 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 4819 | /*EnteringContext=*/true, |
| 4820 | /*AllowDestructorName=*/true, |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4821 | AllowConstructorName, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4822 | ParsedType(), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4823 | TemplateKWLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4824 | D.getName()) || |
| 4825 | // Once we're past the identifier, if the scope was bad, mark the |
| 4826 | // whole declarator bad. |
| 4827 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4828 | D.SetIdentifier(0, Tok.getLocation()); |
| 4829 | D.setInvalidType(true); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4830 | } else { |
| 4831 | // Parsed the unqualified-id; update range information and move along. |
| 4832 | if (D.getSourceRange().getBegin().isInvalid()) |
| 4833 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 4834 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4835 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4836 | goto PastIdentifier; |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4837 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4838 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4839 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4840 | "There's a C++-specific check for tok::identifier above"); |
| 4841 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 4842 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 4843 | ConsumeToken(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4844 | goto PastIdentifier; |
Richard Smith | 9ce302e | 2013-07-11 05:10:21 +0000 | [diff] [blame] | 4845 | } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) { |
Richard Smith | f39720b | 2013-10-13 22:12:28 +0000 | [diff] [blame] | 4846 | // A virt-specifier isn't treated as an identifier if it appears after a |
| 4847 | // trailing-return-type. |
| 4848 | if (D.getContext() != Declarator::TrailingReturnContext || |
| 4849 | !isCXX11VirtSpecifier(Tok)) { |
| 4850 | Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id) |
| 4851 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 4852 | D.SetIdentifier(0, Tok.getLocation()); |
| 4853 | ConsumeToken(); |
| 4854 | goto PastIdentifier; |
| 4855 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4856 | } |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4857 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4858 | if (Tok.is(tok::l_paren)) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4859 | // direct-declarator: '(' declarator ')' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 4860 | // direct-declarator: '(' attributes declarator ')' |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4861 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 4862 | ParseParenDeclarator(D); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4863 | |
| 4864 | // If the declarator was parenthesized, we entered the declarator |
| 4865 | // scope when parsing the parenthesized declarator, then exited |
| 4866 | // the scope already. Re-enter the scope, if we need to. |
| 4867 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4868 | // If there was an error parsing parenthesized declarator, declarator |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4869 | // scope may have been entered before. Don't do it again. |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4870 | if (!D.isInvalidType() && |
| 4871 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4872 | // Change the declaration context for name lookup, until this function |
| 4873 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4874 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4875 | } |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4876 | } else if (D.mayOmitIdentifier()) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4877 | // This could be something simple like "int" (in which case the declarator |
| 4878 | // portion is empty), if an abstract-declarator is allowed. |
| 4879 | D.SetIdentifier(0, Tok.getLocation()); |
Richard Smith | b19337f | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4880 | |
| 4881 | // The grammar for abstract-pack-declarator does not allow grouping parens. |
| 4882 | // FIXME: Revisit this once core issue 1488 is resolved. |
| 4883 | if (D.hasEllipsis() && D.hasGroupingParens()) |
| 4884 | Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()), |
| 4885 | diag::ext_abstract_pack_declarator_parens); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4886 | } else { |
David Blaikie | 5d577a2 | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 4887 | if (Tok.getKind() == tok::annot_pragma_parser_crash) |
David Blaikie | 5bd4c2a | 2012-08-21 18:56:49 +0000 | [diff] [blame] | 4888 | LLVM_BUILTIN_TRAP; |
Douglas Gregor | d9f92e2 | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 4889 | if (D.getContext() == Declarator::MemberContext) |
| 4890 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 4891 | << D.getDeclSpec().getSourceRange(); |
Richard Trieu | 9c67267 | 2013-01-26 02:31:38 +0000 | [diff] [blame] | 4892 | else if (getLangOpts().CPlusPlus) { |
| 4893 | if (Tok.is(tok::period) || Tok.is(tok::arrow)) |
| 4894 | Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow); |
Richard Trieu | 2f58696 | 2013-09-05 02:31:33 +0000 | [diff] [blame] | 4895 | else { |
| 4896 | SourceLocation Loc = D.getCXXScopeSpec().getEndLoc(); |
| 4897 | if (Tok.isAtStartOfLine() && Loc.isValid()) |
| 4898 | Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id) |
| 4899 | << getLangOpts().CPlusPlus; |
| 4900 | else |
| 4901 | Diag(Tok, diag::err_expected_unqualified_id) |
| 4902 | << getLangOpts().CPlusPlus; |
| 4903 | } |
Richard Trieu | 9c67267 | 2013-01-26 02:31:38 +0000 | [diff] [blame] | 4904 | } else |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 4905 | Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_paren; |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 4906 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 8c5dd73 | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 4907 | D.setInvalidType(true); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4908 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4909 | |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4910 | PastIdentifier: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4911 | assert(D.isPastIdentifier() && |
| 4912 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4913 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4914 | // Don't parse attributes unless we have parsed an unparenthesized name. |
| 4915 | if (D.hasName() && !D.getNumTypeObjects()) |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4916 | MaybeParseCXX11Attributes(D); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4917 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4918 | while (1) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4919 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4920 | // Enter function-declaration scope, limiting any declarators to the |
| 4921 | // function prototype scope, including parameter declarators. |
| 4922 | ParseScope PrototypeScope(this, |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4923 | Scope::FunctionPrototypeScope|Scope::DeclScope| |
| 4924 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 4925 | ? Scope::FunctionDeclarationScope : 0)); |
| 4926 | |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4927 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 4928 | // In such a case, check if we actually have a function declarator; if it |
| 4929 | // is not, the declarator has been fully parsed. |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4930 | bool IsAmbiguous = false; |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 4931 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 4932 | // The name of the declarator, if any, is tentatively declared within |
| 4933 | // a possible direct initializer. |
| 4934 | TentativelyDeclaredIdentifiers.push_back(D.getIdentifier()); |
| 4935 | bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous); |
| 4936 | TentativelyDeclaredIdentifiers.pop_back(); |
| 4937 | if (!IsFunctionDecl) |
| 4938 | break; |
| 4939 | } |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4940 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4941 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4942 | T.consumeOpen(); |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4943 | ParseFunctionDeclarator(D, attrs, T, IsAmbiguous); |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4944 | PrototypeScope.Exit(); |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4945 | } else if (Tok.is(tok::l_square)) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 4946 | ParseBracketDeclarator(D); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4947 | } else { |
| 4948 | break; |
| 4949 | } |
| 4950 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4951 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4952 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4953 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 4954 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4955 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4956 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 4957 | /// |
| 4958 | /// direct-declarator: |
| 4959 | /// '(' declarator ')' |
| 4960 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4961 | /// direct-declarator '(' parameter-type-list ')' |
| 4962 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4963 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4964 | /// parameter-type-list[opt] ')' |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4965 | /// |
| 4966 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4967 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4968 | T.consumeOpen(); |
| 4969 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4970 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4971 | |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4972 | // Eat any attributes before we look at whether this is a grouping or function |
| 4973 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 4974 | // the type being built up, for example: |
| 4975 | // int (__attribute__(()) *x)(long y) |
| 4976 | // If this ends up not being a grouping paren, the attribute applies to the |
| 4977 | // first argument, for example: |
| 4978 | // int (__attribute__(()) int x) |
| 4979 | // In either case, we need to eat any attributes to be able to determine what |
| 4980 | // sort of paren this is. |
| 4981 | // |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4982 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4983 | bool RequiresArg = false; |
| 4984 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4985 | ParseGNUAttributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4986 | |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4987 | // We require that the argument list (if this is a non-grouping paren) be |
| 4988 | // present even if the attribute list was empty. |
| 4989 | RequiresArg = true; |
| 4990 | } |
Chad Rosier | eea9ca7 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 4991 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4992 | // Eat any Microsoft extensions. |
Chad Rosier | eea9ca7 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 4993 | ParseMicrosoftTypeAttributes(attrs); |
| 4994 | |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4995 | // Eat any Borland extensions. |
Ted Kremenek | 5eec2b0 | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 4996 | if (Tok.is(tok::kw___pascal)) |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4997 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4998 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4999 | // If we haven't past the identifier yet (or where the identifier would be |
| 5000 | // stored, if this is an abstract declarator), then this is probably just |
| 5001 | // grouping parens. However, if this could be an abstract-declarator, then |
| 5002 | // this could also be the start of function arguments (consider 'void()'). |
| 5003 | bool isGrouping; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5004 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5005 | if (!D.mayOmitIdentifier()) { |
| 5006 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 5007 | // paren, because we haven't seen the identifier yet. |
| 5008 | isGrouping = true; |
| 5009 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 5010 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) && |
| 5011 | NextToken().is(tok::r_paren)) || // C++ int(...) |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5012 | isDeclarationSpecifier() || // 'int(int)' is a function. |
| 5013 | isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function. |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5014 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 5015 | // considered to be a type, not a K&R identifier-list. |
| 5016 | isGrouping = false; |
| 5017 | } else { |
| 5018 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 5019 | isGrouping = true; |
| 5020 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5021 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5022 | // If this is a grouping paren, handle: |
| 5023 | // direct-declarator: '(' declarator ')' |
| 5024 | // direct-declarator: '(' attributes declarator ')' |
| 5025 | if (isGrouping) { |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5026 | SourceLocation EllipsisLoc = D.getEllipsisLoc(); |
| 5027 | D.setEllipsisLoc(SourceLocation()); |
| 5028 | |
Argyrios Kyrtzidis | 8ae3684 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 5029 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5030 | D.setGroupingParens(true); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5031 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5032 | // Match the ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5033 | T.consumeClose(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5034 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5035 | T.getCloseLocation()), |
| 5036 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 8ae3684 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 5037 | |
| 5038 | D.setGroupingParens(hadGroupingParens); |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5039 | |
| 5040 | // An ellipsis cannot be placed outside parentheses. |
| 5041 | if (EllipsisLoc.isValid()) |
| 5042 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 5043 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5044 | return; |
| 5045 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5046 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5047 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 5048 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5049 | // identifier (and remember where it would have been), then call into |
| 5050 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5051 | D.SetIdentifier(0, Tok.getLocation()); |
| 5052 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5053 | // Enter function-declaration scope, limiting any declarators to the |
| 5054 | // function prototype scope, including parameter declarators. |
| 5055 | ParseScope PrototypeScope(this, |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 5056 | Scope::FunctionPrototypeScope | Scope::DeclScope | |
| 5057 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 5058 | ? Scope::FunctionDeclarationScope : 0)); |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5059 | ParseFunctionDeclarator(D, attrs, T, false, RequiresArg); |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5060 | PrototypeScope.Exit(); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5061 | } |
| 5062 | |
| 5063 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 5064 | /// declarator D up to a paren, which indicates that we are parsing function |
| 5065 | /// arguments. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5066 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5067 | /// If FirstArgAttrs is non-null, then the caller parsed those arguments |
| 5068 | /// immediately after the open paren - they should be considered to be the |
| 5069 | /// first argument of a parameter. |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5070 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5071 | /// If RequiresArg is true, then the first argument of the function is required |
| 5072 | /// to be present and required to not be an identifier list. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5073 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5074 | /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt], |
| 5075 | /// (C++11) ref-qualifier[opt], exception-specification[opt], |
| 5076 | /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt]. |
| 5077 | /// |
| 5078 | /// [C++11] exception-specification: |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5079 | /// dynamic-exception-specification |
| 5080 | /// noexcept-specification |
| 5081 | /// |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5082 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5083 | ParsedAttributes &FirstArgAttrs, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5084 | BalancedDelimiterTracker &Tracker, |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5085 | bool IsAmbiguous, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5086 | bool RequiresArg) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5087 | assert(getCurScope()->isFunctionPrototypeScope() && |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5088 | "Should call from a Function scope"); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5089 | // lparen is already consumed! |
| 5090 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 5091 | |
| 5092 | // This should be true when the function has typed arguments. |
| 5093 | // Otherwise, it is treated as a K&R-style function. |
| 5094 | bool HasProto = false; |
| 5095 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5096 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5097 | // Remember where we see an ellipsis, if any. |
| 5098 | SourceLocation EllipsisLoc; |
| 5099 | |
| 5100 | DeclSpec DS(AttrFactory); |
| 5101 | bool RefQualifierIsLValueRef = true; |
| 5102 | SourceLocation RefQualifierLoc; |
Douglas Gregor | e248eea | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 5103 | SourceLocation ConstQualifierLoc; |
| 5104 | SourceLocation VolatileQualifierLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5105 | ExceptionSpecificationType ESpecType = EST_None; |
| 5106 | SourceRange ESpecRange; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5107 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 5108 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5109 | ExprResult NoexceptExpr; |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5110 | ParsedAttributes FnAttrs(AttrFactory); |
Richard Smith | 700537c | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 5111 | TypeResult TrailingReturnType; |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5112 | |
James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 5113 | Actions.ActOnStartFunctionDeclarator(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5114 | /* LocalEndLoc is the end location for the local FunctionTypeLoc. |
| 5115 | EndLoc is the end location for the function declarator. |
| 5116 | They differ for trailing return types. */ |
| 5117 | SourceLocation StartLoc, LocalEndLoc, EndLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5118 | SourceLocation LParenLoc, RParenLoc; |
| 5119 | LParenLoc = Tracker.getOpenLocation(); |
| 5120 | StartLoc = LParenLoc; |
| 5121 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5122 | if (isFunctionDeclaratorIdentifierList()) { |
| 5123 | if (RequiresArg) |
| 5124 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 5125 | |
| 5126 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 5127 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5128 | Tracker.consumeClose(); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5129 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5130 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5131 | EndLoc = RParenLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5132 | } else { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5133 | if (Tok.isNot(tok::r_paren)) |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5134 | ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, |
| 5135 | EllipsisLoc); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5136 | else if (RequiresArg) |
| 5137 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 5138 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5139 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5140 | |
| 5141 | // If we have the closing ')', eat it. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5142 | Tracker.consumeClose(); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5143 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5144 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5145 | EndLoc = RParenLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5146 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5147 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5148 | // FIXME: Accept these components in any order, and produce fixits to |
| 5149 | // correct the order if the user gets it wrong. Ideally we should deal |
| 5150 | // with the virt-specifier-seq and pure-specifier in the same way. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5151 | |
| 5152 | // Parse cv-qualifier-seq[opt]. |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5153 | ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false, |
| 5154 | /*CXX11AttributesAllowed*/ false, |
| 5155 | /*AtomicAllowed*/ false); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5156 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
| 5157 | EndLoc = DS.getSourceRange().getEnd(); |
| 5158 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 5159 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
| 5160 | } |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5161 | |
| 5162 | // Parse ref-qualifier[opt]. |
| 5163 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5164 | Diag(Tok, getLangOpts().CPlusPlus11 ? |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5165 | diag::warn_cxx98_compat_ref_qualifier : |
| 5166 | diag::ext_ref_qualifier); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5167 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5168 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 5169 | RefQualifierLoc = ConsumeToken(); |
| 5170 | EndLoc = RefQualifierLoc; |
| 5171 | } |
| 5172 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5173 | // C++11 [expr.prim.general]p3: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5174 | // If a declaration declares a member function or member function |
| 5175 | // 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] | 5176 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5177 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5178 | // declarator. |
Richard Smith | ad1bbb9 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 5179 | // FIXME: currently, "static" case isn't handled correctly. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5180 | bool IsCXX11MemberFunction = |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5181 | getLangOpts().CPlusPlus11 && |
Richard Smith | ad1bbb9 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 5182 | (D.getContext() == Declarator::MemberContext |
| 5183 | ? !D.getDeclSpec().isFriendSpecified() |
| 5184 | : D.getContext() == Declarator::FileContext && |
| 5185 | D.getCXXScopeSpec().isValid() && |
| 5186 | Actions.CurContext->isRecord()); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5187 | Sema::CXXThisScopeRAII ThisScope(Actions, |
| 5188 | dyn_cast<CXXRecordDecl>(Actions.CurContext), |
Richard Smith | 01141a9 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 5189 | DS.getTypeQualifiers() | |
Richard Smith | 034185c | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 5190 | (D.getDeclSpec().isConstexprSpecified() && |
| 5191 | !getLangOpts().CPlusPlus1y |
Richard Smith | 01141a9 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 5192 | ? Qualifiers::Const : 0), |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5193 | IsCXX11MemberFunction); |
Richard Smith | 2331bbf | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5194 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5195 | // Parse exception-specification[opt]. |
Richard Smith | 2331bbf | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5196 | ESpecType = tryParseExceptionSpecification(ESpecRange, |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 5197 | DynamicExceptions, |
| 5198 | DynamicExceptionRanges, |
Richard Smith | 2331bbf | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5199 | NoexceptExpr); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5200 | if (ESpecType != EST_None) |
| 5201 | EndLoc = ESpecRange.getEnd(); |
| 5202 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5203 | // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes |
| 5204 | // after the exception-specification. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5205 | MaybeParseCXX11Attributes(FnAttrs); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5206 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5207 | // Parse trailing-return-type[opt]. |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5208 | LocalEndLoc = EndLoc; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5209 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) { |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5210 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5211 | if (D.getDeclSpec().getTypeSpecType() == TST_auto) |
| 5212 | StartLoc = D.getDeclSpec().getTypeSpecTypeLoc(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5213 | LocalEndLoc = Tok.getLocation(); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 5214 | SourceRange Range; |
Richard Smith | 700537c | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 5215 | TrailingReturnType = ParseTrailingReturnType(Range); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5216 | EndLoc = Range.getEnd(); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5217 | } |
| 5218 | } |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5219 | } |
| 5220 | |
| 5221 | // Remember that we parsed a function type, and remember the attributes. |
| 5222 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5223 | IsAmbiguous, |
| 5224 | LParenLoc, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5225 | ParamInfo.data(), ParamInfo.size(), |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5226 | EllipsisLoc, RParenLoc, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5227 | DS.getTypeQualifiers(), |
| 5228 | RefQualifierIsLValueRef, |
Douglas Gregor | e248eea | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 5229 | RefQualifierLoc, ConstQualifierLoc, |
| 5230 | VolatileQualifierLoc, |
Douglas Gregor | ad69e65 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 5231 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5232 | ESpecType, ESpecRange.getBegin(), |
| 5233 | DynamicExceptions.data(), |
| 5234 | DynamicExceptionRanges.data(), |
| 5235 | DynamicExceptions.size(), |
| 5236 | NoexceptExpr.isUsable() ? |
| 5237 | NoexceptExpr.get() : 0, |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5238 | StartLoc, LocalEndLoc, D, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5239 | TrailingReturnType), |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5240 | FnAttrs, EndLoc); |
James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 5241 | |
| 5242 | Actions.ActOnEndFunctionDeclarator(); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5243 | } |
| 5244 | |
| 5245 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 5246 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 5247 | /// |
| 5248 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 5249 | /// abstract-declarators. |
| 5250 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5251 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5252 | && Tok.is(tok::identifier) |
| 5253 | && !TryAltiVecVectorToken() |
| 5254 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 5255 | // 6.7.5.3p11. |
| 5256 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 5257 | // Identifier lists follow a really simple grammar: the identifiers can |
| 5258 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 5259 | // identifier lists are really rare in the brave new modern world, and |
| 5260 | // it is very common for someone to typo a type in a non-K&R style |
| 5261 | // list. If we are presented with something like: "void foo(intptr x, |
| 5262 | // float y)", we don't want to start parsing the function declarator as |
| 5263 | // though it is a K&R style declarator just because intptr is an |
| 5264 | // invalid type. |
| 5265 | // |
| 5266 | // To handle this, we check to see if the token after the first |
| 5267 | // identifier is a "," or ")". Only then do we parse it as an |
| 5268 | // identifier list. |
| 5269 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 5270 | } |
| 5271 | |
| 5272 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 5273 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 5274 | /// |
| 5275 | /// After returning, ParamInfo will hold the parsed parameters. |
| 5276 | /// |
| 5277 | /// identifier-list: [C99 6.7.5] |
| 5278 | /// identifier |
| 5279 | /// identifier-list ',' identifier |
| 5280 | /// |
| 5281 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 5282 | Declarator &D, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5283 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5284 | // If there was no identifier specified for the declarator, either we are in |
| 5285 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 5286 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 5287 | // diagnose this. |
| 5288 | if (!D.getIdentifier()) |
| 5289 | Diag(Tok, diag::ext_ident_list_in_param); |
| 5290 | |
| 5291 | // Maintain an efficient lookup of params we have seen so far. |
| 5292 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 5293 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 5294 | do { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5295 | // If this isn't an identifier, report the error and skip until ')'. |
| 5296 | if (Tok.isNot(tok::identifier)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 5297 | Diag(Tok, diag::err_expected) << tok::identifier; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5298 | SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5299 | // Forget we parsed anything. |
| 5300 | ParamInfo.clear(); |
| 5301 | return; |
| 5302 | } |
| 5303 | |
| 5304 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 5305 | |
| 5306 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 5307 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 5308 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 5309 | |
| 5310 | // Verify that the argument identifier has not already been mentioned. |
| 5311 | if (!ParamsSoFar.insert(ParmII)) { |
| 5312 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 5313 | } else { |
| 5314 | // Remember this identifier in ParamInfo. |
| 5315 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 5316 | Tok.getLocation(), |
| 5317 | 0)); |
| 5318 | } |
| 5319 | |
| 5320 | // Eat the identifier. |
| 5321 | ConsumeToken(); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5322 | // The list continues if we see a comma. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 5323 | } while (TryConsumeToken(tok::comma)); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5324 | } |
| 5325 | |
| 5326 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 5327 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 5328 | /// identifier list. |
| 5329 | /// |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5330 | /// D is the declarator being parsed. If FirstArgAttrs is non-null, then the |
| 5331 | /// caller parsed those arguments immediately after the open paren - they should |
| 5332 | /// be considered to be part of the first parameter. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5333 | /// |
| 5334 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 5335 | /// be the location of the ellipsis, if any was parsed. |
| 5336 | /// |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5337 | /// parameter-type-list: [C99 6.7.5] |
| 5338 | /// parameter-list |
| 5339 | /// parameter-list ',' '...' |
Douglas Gregor | 9bfc2e5 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5340 | /// [C++] parameter-list '...' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5341 | /// |
| 5342 | /// parameter-list: [C99 6.7.5] |
| 5343 | /// parameter-declaration |
| 5344 | /// parameter-list ',' parameter-declaration |
| 5345 | /// |
| 5346 | /// parameter-declaration: [C99 6.7.5] |
| 5347 | /// declaration-specifiers declarator |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5348 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5349 | /// [C++11] initializer-clause |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 5350 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 5351 | /// declaration-specifiers abstract-declarator[opt] |
| 5352 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 5353 | /// '=' assignment-expression |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 5354 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5355 | /// [C++11] attribute-specifier-seq parameter-declaration |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5356 | /// |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5357 | void Parser::ParseParameterDeclarationClause( |
| 5358 | Declarator &D, |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5359 | ParsedAttributes &FirstArgAttrs, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5360 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5361 | SourceLocation &EllipsisLoc) { |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 5362 | do { |
| 5363 | // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq |
| 5364 | // before deciding this was a parameter-declaration-clause. |
| 5365 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5366 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5367 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5368 | // Parse the declaration-specifiers. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5369 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5370 | DeclSpec DS(AttrFactory); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5371 | |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5372 | // Parse any C++11 attributes. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5373 | MaybeParseCXX11Attributes(DS.getAttributes()); |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5374 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5375 | // Skip any Microsoft attributes before a param. |
Chad Rosier | f8a2e70 | 2012-12-20 20:37:53 +0000 | [diff] [blame] | 5376 | MaybeParseMicrosoftAttributes(DS.getAttributes()); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5377 | |
| 5378 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5379 | |
| 5380 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5381 | // 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] | 5382 | // 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] | 5383 | // get rid of a parameter (FirstArgAttrs) and this statement. It might be |
| 5384 | // too much hassle. |
| 5385 | DS.takeAttributesFrom(FirstArgAttrs); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5386 | |
Chris Lattner | de39c3e | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 5387 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5388 | |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5389 | |
| 5390 | // Parse the declarator. This is "PrototypeContext" or |
| 5391 | // "LambdaExprParameterContext", because we must accept either |
| 5392 | // 'declarator' or 'abstract-declarator' here. |
| 5393 | Declarator ParmDeclarator(DS, |
| 5394 | D.getContext() == Declarator::LambdaExprContext ? |
| 5395 | Declarator::LambdaExprParameterContext : |
| 5396 | Declarator::PrototypeContext); |
| 5397 | ParseDeclarator(ParmDeclarator); |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5398 | |
| 5399 | // Parse GNU attributes, if present. |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5400 | MaybeParseGNUAttributes(ParmDeclarator); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5401 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5402 | // Remember this parsed parameter in ParamInfo. |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5403 | IdentifierInfo *ParmII = ParmDeclarator.getIdentifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5404 | |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5405 | // DefArgToks is used when the parsing of default arguments needs |
| 5406 | // to be delayed. |
| 5407 | CachedTokens *DefArgToks = 0; |
| 5408 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5409 | // If no parameter was specified, verify that *something* was specified, |
| 5410 | // otherwise we have a missing type and identifier. |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5411 | if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 && |
| 5412 | ParmDeclarator.getNumTypeObjects() == 0) { |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5413 | // Completely missing, emit error. |
| 5414 | Diag(DSStart, diag::err_missing_param); |
| 5415 | } else { |
| 5416 | // Otherwise, we have something. Add it and let semantic analysis try |
| 5417 | // 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] | 5418 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5419 | // Inform the actions module about the parameter declarator, so it gets |
| 5420 | // added to the current scope. |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5421 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), |
| 5422 | ParmDeclarator); |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5423 | // Parse the default argument, if any. We parse the default |
| 5424 | // arguments in all dialects; the semantic analysis in |
| 5425 | // ActOnParamDefaultArgument will reject the default argument in |
| 5426 | // C. |
| 5427 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5428 | SourceLocation EqualLoc = Tok.getLocation(); |
| 5429 | |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5430 | // Parse the default argument |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5431 | if (D.getContext() == Declarator::MemberContext) { |
| 5432 | // If we're inside a class definition, cache the tokens |
| 5433 | // corresponding to the default argument. We'll actually parse |
| 5434 | // them when we see the end of the class definition. |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5435 | // FIXME: Can we use a smart pointer for Toks? |
| 5436 | DefArgToks = new CachedTokens; |
| 5437 | |
Richard Smith | 1fff95c | 2013-09-12 23:28:08 +0000 | [diff] [blame] | 5438 | if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5439 | delete DefArgToks; |
| 5440 | DefArgToks = 0; |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5441 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 249179c | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5442 | } else { |
| 5443 | // Mark the end of the default argument so that we know when to |
| 5444 | // stop when we parse it later on. |
| 5445 | Token DefArgEnd; |
| 5446 | DefArgEnd.startToken(); |
| 5447 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 5448 | DefArgEnd.setLocation(Tok.getLocation()); |
| 5449 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5450 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 5451 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 249179c | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5452 | } |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5453 | } else { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5454 | // Consume the '='. |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5455 | ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5456 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5457 | // The argument isn't actually potentially evaluated unless it is |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5458 | // used. |
| 5459 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | 7fcbd90 | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 5460 | Sema::PotentiallyEvaluatedIfUsed, |
| 5461 | Param); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5462 | |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5463 | ExprResult DefArgResult; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5464 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
Sebastian Redl | 1678d5f | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5465 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5466 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 1678d5f | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5467 | } else |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5468 | DefArgResult = ParseAssignmentExpression(); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5469 | if (DefArgResult.isInvalid()) { |
| 5470 | Actions.ActOnParamDefaultArgumentError(Param); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5471 | SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5472 | } else { |
| 5473 | // Inform the actions module about the default argument |
| 5474 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5475 | DefArgResult.take()); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5476 | } |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5477 | } |
| 5478 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5479 | |
| 5480 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5481 | ParmDeclarator.getIdentifierLoc(), |
| 5482 | Param, DefArgToks)); |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5483 | } |
| 5484 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 5485 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc) && |
| 5486 | !getLangOpts().CPlusPlus) { |
| 5487 | // We have ellipsis without a preceding ',', which is ill-formed |
| 5488 | // in C. Complain and provide the fix. |
| 5489 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
| 5490 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | 9bfc2e5 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5491 | break; |
| 5492 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5493 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 5494 | // If the next token is a comma, consume it and keep reading arguments. |
| 5495 | } while (TryConsumeToken(tok::comma)); |
Chris Lattner | 6c940e6 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 5496 | } |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5497 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5498 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 5499 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 5500 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 5501 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 5502 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5503 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 5504 | /// attribute-specifier-seq[opt] |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5505 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5506 | if (CheckProhibitedCXX11Attribute()) |
| 5507 | return; |
| 5508 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5509 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 5510 | T.consumeOpen(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5511 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5512 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 5513 | // This code does a fast path to handle some of the most obvious cases. |
| 5514 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5515 | T.consumeClose(); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5516 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5517 | MaybeParseCXX11Attributes(attrs); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5518 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5519 | // Remember that we parsed the empty array type. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5520 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5521 | T.getOpenLocation(), |
| 5522 | T.getCloseLocation()), |
| 5523 | attrs, T.getCloseLocation()); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5524 | return; |
| 5525 | } else if (Tok.getKind() == tok::numeric_constant && |
| 5526 | GetLookAheadToken(1).is(tok::r_square)) { |
| 5527 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5528 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5529 | ConsumeToken(); |
| 5530 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5531 | T.consumeClose(); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5532 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5533 | MaybeParseCXX11Attributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5534 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5535 | // Remember that we parsed a array type, and remember its features. |
Nikola Smiljanic | c531dcc | 2013-01-11 08:33:05 +0000 | [diff] [blame] | 5536 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5537 | ExprRes.release(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5538 | T.getOpenLocation(), |
| 5539 | T.getCloseLocation()), |
| 5540 | attrs, T.getCloseLocation()); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5541 | return; |
| 5542 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5543 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5544 | // If valid, this location is the position where we read the 'static' keyword. |
| 5545 | SourceLocation StaticLoc; |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 5546 | TryConsumeToken(tok::kw_static, StaticLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5547 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5548 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5549 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5550 | DeclSpec DS(AttrFactory); |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5551 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5552 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5553 | // If we haven't already read 'static', check to see if there is one after the |
| 5554 | // type-qualifier-list. |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 5555 | if (!StaticLoc.isValid()) |
| 5556 | TryConsumeToken(tok::kw_static, StaticLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5557 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5558 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5559 | bool isStar = false; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5560 | ExprResult NumElements; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5561 | |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5562 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 5563 | // 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] | 5564 | // the token after the star is a ']'. Since stars in arrays are |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5565 | // infrequent, use of lookahead is not costly here. |
| 5566 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | c439f0d | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 5567 | ConsumeToken(); // Eat the '*'. |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 5568 | |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5569 | if (StaticLoc.isValid()) { |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5570 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5571 | StaticLoc = SourceLocation(); // Drop the static. |
| 5572 | } |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5573 | isStar = true; |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5574 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5575 | // Note, in C89, this production uses the constant-expr production instead |
| 5576 | // of assignment-expr. The only difference is that assignment-expr allows |
| 5577 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 5578 | // 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] | 5579 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5580 | // Parse the constant-expression or assignment-expression now (depending |
| 5581 | // on dialect). |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5582 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5583 | NumElements = ParseConstantExpression(); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5584 | } else { |
| 5585 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 5586 | Sema::ConstantEvaluated); |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5587 | NumElements = ParseAssignmentExpression(); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5588 | } |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 5589 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5590 | |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 5591 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 5592 | if (NumElements.isInvalid()) { |
Chris Lattner | cd2a8c5 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 5593 | D.setInvalidType(true); |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 5594 | // If the expression was invalid, skip it. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5595 | SkipUntil(tok::r_square, StopAtSemi); |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 5596 | return; |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5597 | } |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5598 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5599 | T.consumeClose(); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5600 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5601 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5602 | MaybeParseCXX11Attributes(attrs); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5603 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5604 | // Remember that we parsed a array type, and remember its features. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5605 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 5606 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5607 | NumElements.release(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5608 | T.getOpenLocation(), |
| 5609 | T.getCloseLocation()), |
| 5610 | attrs, T.getCloseLocation()); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5611 | } |
| 5612 | |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5613 | /// [GNU] typeof-specifier: |
| 5614 | /// typeof ( expressions ) |
| 5615 | /// typeof ( type-name ) |
| 5616 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5617 | /// |
| 5618 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5619 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5620 | Token OpTok = Tok; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5621 | SourceLocation StartLoc = ConsumeToken(); |
| 5622 | |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5623 | const bool hasParens = Tok.is(tok::l_paren); |
| 5624 | |
Eli Friedman | 15681d6 | 2012-09-26 04:34:21 +0000 | [diff] [blame] | 5625 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated, |
| 5626 | Sema::ReuseLambdaContextDecl); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5627 | |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5628 | bool isCastExpr; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5629 | ParsedType CastTy; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5630 | SourceRange CastRange; |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5631 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 5632 | CastTy, CastRange); |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5633 | if (hasParens) |
| 5634 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5635 | |
| 5636 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5637 | // FIXME: Not accurate, the range gets one token more than it should. |
| 5638 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5639 | else |
| 5640 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5641 | |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5642 | if (isCastExpr) { |
| 5643 | if (!CastTy) { |
| 5644 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5645 | return; |
Douglas Gregor | 220cac5 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 5646 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5647 | |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5648 | const char *PrevSpec = 0; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5649 | unsigned DiagID; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5650 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5651 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5652 | DiagID, CastTy)) |
| 5653 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5654 | return; |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5655 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5656 | |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5657 | // If we get here, the operand to the typeof was an expresion. |
| 5658 | if (Operand.isInvalid()) { |
| 5659 | DS.SetTypeSpecError(); |
Steve Naroff | 4bd2f71 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 5660 | return; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5661 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5662 | |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5663 | // We might need to transform the operand if it is potentially evaluated. |
| 5664 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 5665 | if (Operand.isInvalid()) { |
| 5666 | DS.SetTypeSpecError(); |
| 5667 | return; |
| 5668 | } |
| 5669 | |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5670 | const char *PrevSpec = 0; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5671 | unsigned DiagID; |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5672 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5673 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5674 | DiagID, Operand.get())) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5675 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5676 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5677 | |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 5678 | /// [C11] atomic-specifier: |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5679 | /// _Atomic ( type-name ) |
| 5680 | /// |
| 5681 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5682 | assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) && |
| 5683 | "Not an atomic specifier"); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5684 | |
| 5685 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5686 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5687 | if (T.consumeOpen()) |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5688 | return; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5689 | |
| 5690 | TypeResult Result = ParseTypeName(); |
| 5691 | if (Result.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5692 | SkipUntil(tok::r_paren, StopAtSemi); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5693 | return; |
| 5694 | } |
| 5695 | |
| 5696 | // Match the ')' |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5697 | T.consumeClose(); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5698 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5699 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5700 | return; |
| 5701 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5702 | DS.setTypeofParensRange(T.getRange()); |
| 5703 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5704 | |
| 5705 | const char *PrevSpec = 0; |
| 5706 | unsigned DiagID; |
| 5707 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
| 5708 | DiagID, Result.release())) |
| 5709 | Diag(StartLoc, DiagID) << PrevSpec; |
| 5710 | } |
| 5711 | |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5712 | |
| 5713 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 5714 | /// from TryAltiVecVectorToken. |
| 5715 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 5716 | Token Next = NextToken(); |
| 5717 | switch (Next.getKind()) { |
| 5718 | default: return false; |
| 5719 | case tok::kw_short: |
| 5720 | case tok::kw_long: |
| 5721 | case tok::kw_signed: |
| 5722 | case tok::kw_unsigned: |
| 5723 | case tok::kw_void: |
| 5724 | case tok::kw_char: |
| 5725 | case tok::kw_int: |
| 5726 | case tok::kw_float: |
| 5727 | case tok::kw_double: |
| 5728 | case tok::kw_bool: |
| 5729 | case tok::kw___pixel: |
| 5730 | Tok.setKind(tok::kw___vector); |
| 5731 | return true; |
| 5732 | case tok::identifier: |
| 5733 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5734 | Tok.setKind(tok::kw___vector); |
| 5735 | return true; |
| 5736 | } |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5737 | if (Next.getIdentifierInfo() == Ident_bool) { |
| 5738 | Tok.setKind(tok::kw___vector); |
| 5739 | return true; |
| 5740 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5741 | return false; |
| 5742 | } |
| 5743 | } |
| 5744 | |
| 5745 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 5746 | const char *&PrevSpec, unsigned &DiagID, |
| 5747 | bool &isInvalid) { |
| 5748 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 5749 | Token Next = NextToken(); |
| 5750 | switch (Next.getKind()) { |
| 5751 | case tok::kw_short: |
| 5752 | case tok::kw_long: |
| 5753 | case tok::kw_signed: |
| 5754 | case tok::kw_unsigned: |
| 5755 | case tok::kw_void: |
| 5756 | case tok::kw_char: |
| 5757 | case tok::kw_int: |
| 5758 | case tok::kw_float: |
| 5759 | case tok::kw_double: |
| 5760 | case tok::kw_bool: |
| 5761 | case tok::kw___pixel: |
| 5762 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5763 | return true; |
| 5764 | case tok::identifier: |
| 5765 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5766 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5767 | return true; |
| 5768 | } |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5769 | if (Next.getIdentifierInfo() == Ident_bool) { |
| 5770 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5771 | return true; |
| 5772 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5773 | break; |
| 5774 | default: |
| 5775 | break; |
| 5776 | } |
Douglas Gregor | 9938e3b | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 5777 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5778 | DS.isTypeAltiVecVector()) { |
| 5779 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 5780 | return true; |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5781 | } else if ((Tok.getIdentifierInfo() == Ident_bool) && |
| 5782 | DS.isTypeAltiVecVector()) { |
| 5783 | isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID); |
| 5784 | return true; |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5785 | } |
| 5786 | return false; |
| 5787 | } |